| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Miscellaneous functions stripped out of med.c |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdarg.h> |
| 29 |
#include <string.h> |
| 30 |
#include "gr.h" |
| 31 |
#include "ui.h" |
| 32 |
#include "3d.h" |
| 33 |
#include "u_mem.h" |
| 34 |
#include "dxxerror.h" |
| 35 |
#include "func.h" |
| 36 |
#include "inferno.h" |
| 37 |
#include "editor/editor.h" |
| 38 |
#include "editor/esegment.h" |
| 39 |
#include "editor/medmisc.h" |
| 40 |
#include "segment.h" |
| 41 |
#include "render.h" |
| 42 |
#include "object.h" |
| 43 |
#include "meddraw.h" // For draw_World |
| 44 |
#include "kdefs.h" |
| 45 |
|
| 46 |
#if DXX_USE_OGL |
| 47 |
#include "ogl_init.h" |
| 48 |
#endif |
| 49 |
|
| 50 |
#include "compiler-range_for.h" |
| 51 |
|
| 52 |
//return 2d distance, i.e, sqrt(x*x + y*y) |
| 53 |
#ifdef __WATCOMC__ |
| 54 |
long dist_2d(long x,long y); |
| 55 |
|
| 56 |
#pragma aux dist_2d parm [eax] [ebx] value [eax] modify [ecx edx] = \ |
| 57 |
"imul eax" \ |
| 58 |
"xchg ebx,eax" \ |
| 59 |
"mov ecx,edx" \ |
| 60 |
"imul eax" \ |
| 61 |
"add eax,ebx" \ |
| 62 |
"adc edx,ecx" \ |
| 63 |
"call quad_sqrt"; |
| 64 |
#else |
| 65 |
#include <math.h> |
| 66 |
static long dist_2d(long x,long y) { |
| 67 |
return sqrt(static_cast<double>(x) * static_cast<double>(x) + static_cast<double>(y) * static_cast<double>(y)); |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
// Given mouse movement in dx, dy, returns a 3x3 rotation matrix in RotMat. |
| 72 |
// Taken from Graphics Gems III, page 51, "The Rolling Ball" |
| 73 |
|
| 74 |
void GetMouseRotation( int idx, int idy, vms_matrix * RotMat ) |
| 75 |
{ |
| 76 |
fix dr, cos_theta, sin_theta, denom, cos_theta1; |
| 77 |
fix Radius = i2f(100); |
| 78 |
fix dx,dy; |
| 79 |
fix dxdr,dydr; |
| 80 |
|
| 81 |
idy *= -1; |
| 82 |
|
| 83 |
dx = i2f(idx); dy = i2f(idy); |
| 84 |
|
| 85 |
dr = dist_2d(dx,dy); |
| 86 |
|
| 87 |
denom = dist_2d(Radius,dr); |
| 88 |
|
| 89 |
cos_theta = fixdiv(Radius,denom); |
| 90 |
sin_theta = fixdiv(dr,denom); |
| 91 |
|
| 92 |
cos_theta1 = f1_0 - cos_theta; |
| 93 |
|
| 94 |
dxdr = fixdiv(dx,dr); |
| 95 |
dydr = fixdiv(dy,dr); |
| 96 |
|
| 97 |
RotMat->rvec.x = cos_theta + fixmul(fixmul(dydr,dydr),cos_theta1); |
| 98 |
RotMat->uvec.x = - fixmul(fixmul(dxdr,dydr),cos_theta1); |
| 99 |
RotMat->fvec.x = fixmul(dxdr,sin_theta); |
| 100 |
|
| 101 |
RotMat->rvec.y = RotMat->uvec.x; |
| 102 |
RotMat->uvec.y = cos_theta + fixmul(fixmul(dxdr,dxdr),cos_theta1); |
| 103 |
RotMat->fvec.y = fixmul(dydr,sin_theta); |
| 104 |
|
| 105 |
RotMat->rvec.z = -RotMat->fvec.x; |
| 106 |
RotMat->uvec.z = -RotMat->fvec.y; |
| 107 |
RotMat->fvec.z = cos_theta; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
int Gameview_lockstep; //if set, view is locked to Curseg |
| 112 |
|
| 113 |
int ToggleLockstep() |
| 114 |
{ |
| 115 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 116 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 117 |
Gameview_lockstep = !Gameview_lockstep; |
| 118 |
if (Gameview_lockstep == 0) { |
| 119 |
//if (keypress != KEY_L) |
| 120 |
diagnostic_message("[Ctrl-L] - Lock mode OFF"); |
| 121 |
//else |
| 122 |
// diagnostic_message("Lock mode OFF"); |
| 123 |
} |
| 124 |
if (Gameview_lockstep) { |
| 125 |
//if (keypress != KEY_L) |
| 126 |
diagnostic_message("[Ctrl-L] Lock mode ON"); |
| 127 |
//else |
| 128 |
// diagnostic_message("Lock mode ON"); |
| 129 |
|
| 130 |
Cursegp = imsegptridx(ConsoleObject->segnum); |
| 131 |
med_create_new_segment_from_cursegp(); |
| 132 |
auto &vcvertptr = Vertices.vcptr; |
| 133 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 134 |
Update_flags = UF_ED_STATE_CHANGED; |
| 135 |
} |
| 136 |
return Gameview_lockstep; |
| 137 |
} |
| 138 |
|
| 139 |
int medlisp_delete_segment(void) |
| 140 |
{ |
| 141 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 142 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 143 |
if (!med_delete_segment(Cursegp)) { |
| 144 |
if (Lock_view_to_cursegp) |
| 145 |
{ |
| 146 |
auto &vcvertptr = Vertices.vcptr; |
| 147 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 148 |
} |
| 149 |
autosave_mine(mine_filename); |
| 150 |
undo_status[Autosave_count] = "Delete Segment UNDONE."; |
| 151 |
Update_flags |= UF_WORLD_CHANGED; |
| 152 |
mine_changed = 1; |
| 153 |
diagnostic_message("Segment deleted."); |
| 154 |
warn_if_concave_segments(); // This could be faster -- just check if deleted segment was concave, warn accordingly |
| 155 |
} |
| 156 |
|
| 157 |
return 1; |
| 158 |
} |
| 159 |
|
| 160 |
int medlisp_scale_segment(void) |
| 161 |
{ |
| 162 |
vms_vector scale; |
| 163 |
|
| 164 |
scale.x = fl2f(static_cast<float>(func_get_param(0))); |
| 165 |
scale.y = fl2f(static_cast<float>(func_get_param(1))); |
| 166 |
scale.z = fl2f(static_cast<float>(func_get_param(2))); |
| 167 |
med_create_new_segment(scale); |
| 168 |
med_rotate_segment(Cursegp,vm_angles_2_matrix(Seg_orientation)); |
| 169 |
Update_flags |= UF_WORLD_CHANGED; |
| 170 |
mine_changed = 1; |
| 171 |
|
| 172 |
return 1; |
| 173 |
} |
| 174 |
|
| 175 |
int medlisp_rotate_segment(void) |
| 176 |
{ |
| 177 |
Seg_orientation.p = func_get_param(0); |
| 178 |
Seg_orientation.b = func_get_param(1); |
| 179 |
Seg_orientation.h = func_get_param(2); |
| 180 |
med_rotate_segment(Cursegp,vm_angles_2_matrix(Seg_orientation)); |
| 181 |
Update_flags |= UF_WORLD_CHANGED | UF_VIEWPOINT_MOVED; |
| 182 |
mine_changed = 1; |
| 183 |
return 1; |
| 184 |
} |
| 185 |
|
| 186 |
int ToggleLockViewToCursegp(void) |
| 187 |
{ |
| 188 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 189 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 190 |
Lock_view_to_cursegp = !Lock_view_to_cursegp; |
| 191 |
Update_flags = UF_ED_STATE_CHANGED; |
| 192 |
if (Lock_view_to_cursegp) { |
| 193 |
//if (keypress != KEY_V+KEY_CTRLED) |
| 194 |
diagnostic_message("[ctrl-V] View locked to Cursegp."); |
| 195 |
//else |
| 196 |
// diagnostic_message("View locked to Cursegp."); |
| 197 |
auto &vcvertptr = Vertices.vcptr; |
| 198 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 199 |
} else { |
| 200 |
//if (keypress != KEY_V+KEY_CTRLED) |
| 201 |
diagnostic_message("[ctrl-V] View not locked to Cursegp."); |
| 202 |
//else |
| 203 |
// diagnostic_message("View not locked to Cursegp."); |
| 204 |
} |
| 205 |
return Lock_view_to_cursegp; |
| 206 |
} |
| 207 |
|
| 208 |
int ToggleDrawAllSegments() |
| 209 |
{ |
| 210 |
Draw_all_segments = !Draw_all_segments; |
| 211 |
Update_flags = UF_ED_STATE_CHANGED; |
| 212 |
if (Draw_all_segments == 1) { |
| 213 |
//if (keypress != KEY_A+KEY_CTRLED) |
| 214 |
diagnostic_message("[ctrl-A] Draw all segments ON."); |
| 215 |
//else |
| 216 |
// diagnostic_message("Draw all segments ON."); |
| 217 |
} |
| 218 |
if (Draw_all_segments == 0) { |
| 219 |
//if (keypress != KEY_A+KEY_CTRLED) |
| 220 |
diagnostic_message("[ctrl-A] Draw all segments OFF."); |
| 221 |
//else |
| 222 |
// diagnostic_message("Draw all segments OFF."); |
| 223 |
} |
| 224 |
return Draw_all_segments; |
| 225 |
} |
| 226 |
|
| 227 |
int Big_depth{6}; |
| 228 |
|
| 229 |
int IncreaseDrawDepth(void) |
| 230 |
{ |
| 231 |
Big_depth++; |
| 232 |
Update_flags = UF_ED_STATE_CHANGED; |
| 233 |
return 1; |
| 234 |
} |
| 235 |
|
| 236 |
int DecreaseDrawDepth(void) |
| 237 |
{ |
| 238 |
if (Big_depth > 1) { |
| 239 |
Big_depth--; |
| 240 |
Update_flags = UF_ED_STATE_CHANGED; |
| 241 |
} |
| 242 |
return 1; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
int ToggleCoordAxes() |
| 247 |
{ |
| 248 |
// Toggle display of coordinate axes. |
| 249 |
Show_axes_flag = !Show_axes_flag; |
| 250 |
LargeView.ev_changed = 1; |
| 251 |
if (Show_axes_flag == 1) { |
| 252 |
//if (keypress != KEY_D+KEY_CTRLED) |
| 253 |
diagnostic_message("[ctrl-D] Coordinate axes ON."); |
| 254 |
//else |
| 255 |
// diagnostic_message("Coordinate axes ON."); |
| 256 |
} |
| 257 |
if (Show_axes_flag == 0) { |
| 258 |
//if (keypress != KEY_D+KEY_CTRLED) |
| 259 |
diagnostic_message("[ctrl-D] Coordinate axes OFF."); |
| 260 |
//else |
| 261 |
// diagnostic_message("Coordinate axes OFF."); |
| 262 |
} |
| 263 |
return Show_axes_flag; |
| 264 |
} |
| 265 |
|
| 266 |
int med_keypad_goto_prev() |
| 267 |
{ |
| 268 |
ui_pad_goto_prev(); |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
int med_keypad_goto_next() |
| 273 |
{ |
| 274 |
ui_pad_goto_next(); |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
int med_keypad_goto() |
| 279 |
{ |
| 280 |
ui_pad_goto(func_get_param(0)); |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
int render_3d_in_big_window{0}; |
| 285 |
|
| 286 |
int medlisp_update_screen() |
| 287 |
{ |
| 288 |
#if 1 //def OGL |
| 289 |
Update_flags = UF_ALL; |
| 290 |
#endif |
| 291 |
|
| 292 |
if (!render_3d_in_big_window) |
| 293 |
range_for (const auto vn, Views) |
| 294 |
if (vn->ev_changed || (Update_flags & (UF_WORLD_CHANGED | UF_VIEWPOINT_MOVED | UF_ED_STATE_CHANGED))) |
| 295 |
{ |
| 296 |
draw_world(vn->ev_canv, vn, Cursegp, Big_depth); |
| 297 |
vn->ev_changed = 0; |
| 298 |
} |
| 299 |
|
| 300 |
if (Update_flags & (UF_WORLD_CHANGED|UF_GAME_VIEW_CHANGED|UF_ED_STATE_CHANGED)) { |
| 301 |
grs_canvas *render_canv,*show_canv; |
| 302 |
|
| 303 |
if (render_3d_in_big_window) { |
| 304 |
render_canv = LargeView.ev_canv; |
| 305 |
show_canv = LargeView.ev_canv; |
| 306 |
} |
| 307 |
else { |
| 308 |
render_canv = Canv_editor_game; |
| 309 |
show_canv = Canv_editor_game; |
| 310 |
} |
| 311 |
|
| 312 |
gr_set_current_canvas(render_canv); |
| 313 |
window_rendered_data window; |
| 314 |
render_frame(*grd_curcanv, 0, window); |
| 315 |
|
| 316 |
Assert(render_canv->cv_bitmap.bm_w == show_canv->cv_bitmap.bm_w && |
| 317 |
render_canv->cv_bitmap.bm_h == show_canv->cv_bitmap.bm_h); |
| 318 |
(void)show_canv; |
| 319 |
} |
| 320 |
|
| 321 |
Update_flags=UF_NONE; //clear flags |
| 322 |
|
| 323 |
return 1; |
| 324 |
} |
| 325 |
|
| 326 |
vms_vector med_point_2_vec(grs_canvas *const canv, const short sx, const short sy) |
| 327 |
{ |
| 328 |
gr_set_current_canvas(canv); |
| 329 |
|
| 330 |
g3_start_frame(*grd_curcanv); |
| 331 |
g3_set_view_matrix(Viewer->pos,Viewer->orient,Render_zoom); |
| 332 |
const auto v{g3_point_2_vec(sx, sy)}; |
| 333 |
g3_end_frame(); |
| 334 |
return v; |
| 335 |
} |
| 336 |
|
| 337 |
void draw_world_from_game(void) |
| 338 |
{ |
| 339 |
if (ModeFlag == 2) |
| 340 |
draw_world(Views[0]->ev_canv,Views[0],Cursegp,Big_depth); |
| 341 |
} |
| 342 |
|
| 343 |
int UndoCommand() |
| 344 |
{ |
| 345 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 346 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 347 |
int u; |
| 348 |
|
| 349 |
u = undo(); |
| 350 |
if (Lock_view_to_cursegp) |
| 351 |
{ |
| 352 |
auto &vcvertptr = Vertices.vcptr; |
| 353 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 354 |
} |
| 355 |
if (u == 0) { |
| 356 |
if (Autosave_count==9) diagnostic_message(undo_status[0]); |
| 357 |
else |
| 358 |
diagnostic_message(undo_status[Autosave_count+1]); |
| 359 |
} |
| 360 |
else |
| 361 |
if (u == 1) diagnostic_message("Can't Undo."); |
| 362 |
else |
| 363 |
if (u == 2) diagnostic_message("Can't Undo - Autosave OFF"); |
| 364 |
Update_flags |= UF_WORLD_CHANGED; |
| 365 |
mine_changed = 1; |
| 366 |
warn_if_concave_segments(); |
| 367 |
return 1; |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
int ToggleAutosave() |
| 372 |
{ |
| 373 |
Autosave_flag = !Autosave_flag; |
| 374 |
if (Autosave_flag == 1) |
| 375 |
diagnostic_message("Autosave ON."); |
| 376 |
else |
| 377 |
diagnostic_message("Autosave OFF."); |
| 378 |
return Autosave_flag; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
int AttachSegment() |
| 383 |
{ |
| 384 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 385 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 386 |
if (med_attach_segment(Cursegp, vmsegptr(&New_segment), Curside, AttachSide)==4) // Used to be WBACK instead of Curside |
| 387 |
diagnostic_message("Cannot attach segment - already a connection on current side."); |
| 388 |
else { |
| 389 |
if (Lock_view_to_cursegp) |
| 390 |
{ |
| 391 |
auto &vcvertptr = Vertices.vcptr; |
| 392 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 393 |
} |
| 394 |
Seg_orientation = {}; |
| 395 |
Curside = sidenum_t::WBACK; |
| 396 |
Update_flags |= UF_WORLD_CHANGED; |
| 397 |
autosave_mine(mine_filename); |
| 398 |
undo_status[Autosave_count] = "Attach Segment UNDONE."; |
| 399 |
mine_changed = 1; |
| 400 |
warn_if_concave_segment(Cursegp); |
| 401 |
} |
| 402 |
return 1; |
| 403 |
} |
| 404 |
|
| 405 |
int CreateDefaultNewSegment() |
| 406 |
{ |
| 407 |
// Create a default segment for New_segment. |
| 408 |
med_create_new_segment({DEFAULT_X_SIZE, DEFAULT_Y_SIZE, DEFAULT_Z_SIZE}); |
| 409 |
mine_changed = 1; |
| 410 |
|
| 411 |
return 1; |
| 412 |
} |
| 413 |
|
| 414 |
int CreateDefaultNewSegmentandAttach() |
| 415 |
{ |
| 416 |
CreateDefaultNewSegment(); |
| 417 |
AttachSegment(); |
| 418 |
|
| 419 |
return 1; |
| 420 |
} |
| 421 |
|
| 422 |
int ExchangeMarkandCurseg() |
| 423 |
{ |
| 424 |
// If Markedsegp != Cursegp, and Markedsegp->segnum != -1, exchange Markedsegp and Cursegp |
| 425 |
if (Markedsegp) |
| 426 |
if (Markedsegp->segnum != segment_none) { |
| 427 |
using std::swap; |
| 428 |
swap(Markedsegp, Cursegp); |
| 429 |
swap(Markedside, Curside); |
| 430 |
med_create_new_segment_from_cursegp(); |
| 431 |
Update_flags |= UF_ED_STATE_CHANGED; |
| 432 |
mine_changed = 1; |
| 433 |
} |
| 434 |
return 1; |
| 435 |
} |
| 436 |
|
| 437 |
int ClearSelectedList(void) |
| 438 |
{ |
| 439 |
Selected_segs.clear(); |
| 440 |
Update_flags |= UF_WORLD_CHANGED; |
| 441 |
|
| 442 |
diagnostic_message("Selected list cleared."); |
| 443 |
|
| 444 |
return 1; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
int ClearFoundList(void) |
| 449 |
{ |
| 450 |
Found_segs.clear(); |
| 451 |
Update_flags |= UF_WORLD_CHANGED; |
| 452 |
|
| 453 |
diagnostic_message("Found list cleared."); |
| 454 |
|
| 455 |
return 1; |
| 456 |
} |
| 457 |
|
| 458 |
// --------------------------------------------------------------------------------------------------- |
| 459 |
void set_view_target_from_segment(fvcvertptr &vcvertptr, const shared_segment &sp) |
| 460 |
{ |
| 461 |
if (Funky_chase_mode) |
| 462 |
{ |
| 463 |
//set_chase_matrix(sp); |
| 464 |
} |
| 465 |
else { |
| 466 |
vms_vector tv{}; |
| 467 |
range_for (const auto &v, sp.verts) |
| 468 |
vm_vec_add2(tv, vcvertptr(v)); |
| 469 |
|
| 470 |
vm_vec_scale(tv,F1_0/MAX_VERTICES_PER_SEGMENT); |
| 471 |
Ed_view_target = tv; |
| 472 |
} |
| 473 |
Update_flags |= UF_VIEWPOINT_MOVED; |
| 474 |
|
| 475 |
} |