| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/main/SLEW.cpp $ |
| 21 | * $Revision: 46 $ |
| 22 | * $Date: 4/18/99 5:42a $ |
| 23 | * $Author: Chris $ |
| 24 | * |
| 25 | * Basic slew system |
| 26 | * |
| 27 | * $Log: /DescentIII/main/SLEW.cpp $ |
| 28 | * |
| 29 | * 46 4/18/99 5:42a Chris |
| 30 | * Added the FQ_IGNORE_RENDER_THROUGH_PORTALS flag |
| 31 | * |
| 32 | * 45 4/17/99 2:41p Matt |
| 33 | * Added code to limit movement by axis. |
| 34 | * |
| 35 | * 44 4/14/99 3:57a Jeff |
| 36 | * fixed case mismatch in #includes |
| 37 | * |
| 38 | * 43 4/10/99 5:53p Matt |
| 39 | * Re-added terrain bound limit, on terrain only |
| 40 | * |
| 41 | * 42 4/09/99 12:02p Samir |
| 42 | * joystick changes (Win32 DirectInput support) |
| 43 | * |
| 44 | * 41 3/01/99 6:27p Matt |
| 45 | * Cleaned up handling of the outside-mine flag |
| 46 | * |
| 47 | * 40 2/25/99 10:44p Matt |
| 48 | * Return separate flags for move & rotate |
| 49 | * |
| 50 | * 39 2/24/99 9:39p Matt |
| 51 | * Disabled terrain bounds limitation |
| 52 | * |
| 53 | * 38 2/23/99 5:47p Matt |
| 54 | * Fixed terrain bound limit code |
| 55 | * |
| 56 | * 37 1/15/99 7:52p Chris |
| 57 | * Updated ObjSetPos() to include a f_update_attach_children flag |
| 58 | * |
| 59 | * 36 10/20/98 5:35p Sean |
| 60 | * Temporary slew fix |
| 61 | * |
| 62 | * 35 10/15/98 8:47a Matt |
| 63 | * Changed _DEBUG/RELEASE inconsistancy with slew |
| 64 | * |
| 65 | * 34 10/14/98 4:39p Matt |
| 66 | * Changed OutrageMessageBox() calls to use either Error() or |
| 67 | * EditorMessageBox() |
| 68 | * |
| 69 | * 33 9/22/98 3:54p Samir |
| 70 | * no include slew in release version. |
| 71 | * |
| 72 | * 32 9/11/98 3:00p Craig |
| 73 | * Added ifdef around some editor code |
| 74 | * |
| 75 | * 31 9/04/98 12:23p Samir |
| 76 | * control whether to allow joystick slewing. |
| 77 | * |
| 78 | * 30 7/01/98 4:58p Samir |
| 79 | * moved slew joysitck init from Init.cpp to here. |
| 80 | * |
| 81 | * 29 6/15/98 1:02p Samir |
| 82 | * added pageup and pagedown. |
| 83 | * |
| 84 | * 28 5/20/98 6:59p Samir |
| 85 | * added slew speed slider. |
| 86 | * |
| 87 | * 27 5/18/98 6:58p Samir |
| 88 | * took out annoying mprinrfs. |
| 89 | * |
| 90 | * 26 3/12/98 7:30p Chris |
| 91 | * Added ObjSetOrient |
| 92 | * |
| 93 | * 25 2/17/98 5:31p Samir |
| 94 | * Upped joy slew. |
| 95 | * |
| 96 | * 24 2/15/98 7:57p Matt |
| 97 | * Renamed NewSlewFrame() to be SlewFrame(), and added some mprintfs. |
| 98 | * |
| 99 | * 23 2/13/98 1:21p Samir |
| 100 | * Maybe fixed evil slewing problem. |
| 101 | * |
| 102 | * 22 2/13/98 11:47a Samir |
| 103 | * debug info. |
| 104 | * |
| 105 | * 21 2/12/98 4:08p Samir |
| 106 | * Fixed a boo-boo. |
| 107 | * |
| 108 | * 20 2/12/98 4:01p Samir |
| 109 | * Maybe this will fix slewing. |
| 110 | * |
| 111 | * 19 2/05/98 2:58p Matt |
| 112 | * Cleaned up slew code, and made view mode switch when slewing between |
| 113 | * mine and terrain happen in editor, not slew. |
| 114 | * |
| 115 | * 18 2/04/98 6:09p Matt |
| 116 | * Changed object room number to indicate a terrain cell via a flag. Got |
| 117 | * rid of the object flag which used to indicate terrain. |
| 118 | * |
| 119 | * 17 1/21/98 1:08p Matt |
| 120 | * Made SetViewMode() not take new_viewer parm, since it's not needed |
| 121 | * after my revamp of the editor view system. |
| 122 | * |
| 123 | * 16 1/21/98 12:25p Matt |
| 124 | * Removed unneeded include |
| 125 | * |
| 126 | * 15 1/07/98 10:50a Chris |
| 127 | * Made it safe to slew to the edge of the terrain |
| 128 | * |
| 129 | * 14 12/03/97 7:35p Samir |
| 130 | * New joystick library sturff. |
| 131 | * |
| 132 | * 13 11/16/97 5:53p Matt |
| 133 | * Added some code so that slewing in Room View doesn't call FVI to update |
| 134 | * the room number |
| 135 | * |
| 136 | * 12 11/12/97 12:57p Chris |
| 137 | * Now compile under the main project |
| 138 | * |
| 139 | * 11 10/22/97 4:34p Chris |
| 140 | * Removed a warning |
| 141 | * |
| 142 | * 10 10/22/97 4:30p Chris |
| 143 | * We can now slew between the mine and the terrain |
| 144 | * |
| 145 | * 9 9/22/97 6:18p Matt |
| 146 | * Removed include of stub.h |
| 147 | * |
| 148 | * 8 9/03/97 2:14p Chris |
| 149 | * Made a few const int's to const float's as they actually are and |
| 150 | * removed a literal and replaced it with a FVIF_OUTSIDE |
| 151 | * |
| 152 | * 7 8/25/97 12:41p Chris |
| 153 | * Made sure that all object movements and mine changes result in AABB |
| 154 | * updates. |
| 155 | * |
| 156 | * 6 8/22/97 4:57p Matt |
| 157 | * Only search for new room if slew object has moved |
| 158 | * |
| 159 | * 5 8/05/97 10:50a Matt |
| 160 | * Fixed slewing on terrain |
| 161 | * |
| 162 | * 4 8/04/97 7:40p Matt |
| 163 | * Use FVI to update room number when slewing |
| 164 | * |
| 165 | * 3 8/01/97 10:48a Samir |
| 166 | * Increased null-zone for joystick. Still some calibration problems. |
| 167 | * |
| 168 | * 2 7/17/97 4:34p Chris |
| 169 | * Made sure the orientations get orthagonalized. Yikes. No more |
| 170 | * degenerating orientations. :) |
| 171 | * |
| 172 | * 13 5/13/97 11:40a Samir |
| 173 | * No joystick stuff for noneditor |
| 174 | * |
| 175 | * 12 4/01/97 9:45p Matt |
| 176 | * Don't require an object to have control type of slew in order to slew |
| 177 | * (so we can slew the editor's viewer object) |
| 178 | * |
| 179 | * 11 2/27/97 10:35a Matt |
| 180 | * Got joystick slewing working |
| 181 | * |
| 182 | * 10 2/20/97 4:30p Samir |
| 183 | * Adjust slewing code to return whether a key was pressed as well as |
| 184 | * disallowing slewing when control keys are down. |
| 185 | * |
| 186 | * 9 2/18/97 6:31p Matt |
| 187 | * Changed Frame_time to Frametime, & added Gametime |
| 188 | * |
| 189 | * 8 2/07/97 5:48p Matt |
| 190 | * Renamed vector.h to vecmat.h to fix DevStudio problem |
| 191 | * |
| 192 | * $NoKeywords: $ |
| 193 | */ |
| 194 | |
| 195 | #if (defined(_DEBUG) || defined(EDITOR)) |
| 196 | |
| 197 | #ifdef EDITOR |
| 198 | #include "editor\mainfrm.h" |
| 199 | #include "editor\d3edit.h" |
| 200 | #endif |
| 201 | |
| 202 | #include <stdlib.h> |
| 203 | #include "descent.h" |
| 204 | #include "slew.h" |
| 205 | #include "vecmat.h" |
| 206 | #include "ddio.h" |
| 207 | #include "object.h" |
| 208 | #include "mono.h" |
| 209 | #include "game.h" |
| 210 | #include "joystick.h" |
| 211 | #include "findintersection.h" |
| 212 | #include "room.h" |
| 213 | |
| 214 | // variables for slew system |
| 215 | |
| 216 | #define ROT_SPEED (1.0 / 8.0) // rate of rotation while key held down |
| 217 | #define VEL_SPEED (110.0) // rate of acceleration while key held down |
| 218 | #define JOY_NULL 32 |
| 219 | |
| 220 | float Slew_key_speed = 1.0f; |
| 221 | int Joystick_active = -1; |
| 222 | |
| 223 | // ------------------------------------------------------------------- |
| 224 | |
| 225 | #ifdef EDITOR |
| 226 | void SlewControlInit() { |
| 227 | Joystick_active = -1; |
| 228 | |
| 229 | if (!D3EditState.joy_slewing) |
| 230 | return; |
| 231 | |
| 232 | if (joy_IsValid(JOYSTICK_1)) { |
| 233 | tJoyPos joystate; |
| 234 | tJoystick joyid = JOYSTICK_1; |
| 235 | |
| 236 | Joystick_active = (int)joyid; |
| 237 | joy_GetPos((tJoystick)Joystick_active, &joystate); // get all the stick values |
| 238 | |
| 239 | if ((abs(joystate.x) > 32) || (abs(joystate.y) > 32)) |
| 240 | EditorMessageBox("Warning: Your joystick is not centered. You should either center it now or recalibrate."); |
| 241 | } |
| 242 | } |
| 243 | #endif |
| 244 | |
| 245 | int SlewStop(object *obj) { |
| 246 | if (!obj) |
| 247 | return 0; |
| 248 | |
| 249 | vm_MakeZero(&obj->mtype.phys_info.velocity); |
| 250 | |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | // Resets object's orientation |
| 255 | void SlewResetOrient(object *obj) { |
| 256 | if (!obj) |
| 257 | return; |
| 258 | |
| 259 | ObjSetOrient(obj, &Identity_matrix); |
| 260 | } |
| 261 | |
| 262 | // Moves the object for one frame |
| 263 | int SlewFrame(object *obj, int movement_limitations) { |
| 264 | static int16_t old_joy_x = 0, old_joy_y = 0; // position last time around |
| 265 | int ret_flags = 0; |
| 266 | vector svel, movement; // scaled velocity (per this frame) |
| 267 | matrix rotmat, new_pm; |
| 268 | angvec rotang; |
| 269 | vector rottime; |
| 270 | vector new_pos; |
| 271 | int new_room; |
| 272 | fvi_query fq; |
| 273 | fvi_info hit_info; |
| 274 | int fate; |
| 275 | |
| 276 | float key_timex1 = 0, key_timex0 = 0; |
| 277 | float key_timey1 = 0, key_timey0 = 0; |
| 278 | float key_timez1 = 0, key_timez0 = 0; |
| 279 | float key_timep1 = 0, key_timep0 = 0; |
| 280 | float key_timeh1 = 0, key_timeh0 = 0; |
| 281 | float key_timeb1 = 0, key_timeb0 = 0; |
| 282 | |
| 283 | if (!obj) |
| 284 | return 0; |
| 285 | |
| 286 | // check keyboard for slewing. |
| 287 | key_timex1 = ddio_KeyDownTime(KEY_PAD9); |
| 288 | key_timex0 = ddio_KeyDownTime(KEY_PAD7); |
| 289 | key_timey1 = ddio_KeyDownTime(KEY_PADMINUS); |
| 290 | key_timey0 = ddio_KeyDownTime(KEY_PADPLUS); |
| 291 | key_timez1 = ddio_KeyDownTime(KEY_PAD8); |
| 292 | key_timez0 = ddio_KeyDownTime(KEY_PAD2); |
| 293 | key_timep1 = ddio_KeyDownTime(KEY_LBRACKET); |
| 294 | key_timep0 = ddio_KeyDownTime(KEY_RBRACKET); |
| 295 | key_timeh1 = ddio_KeyDownTime(KEY_PAD6); |
| 296 | key_timeh0 = ddio_KeyDownTime(KEY_PAD4); |
| 297 | key_timeb1 = ddio_KeyDownTime(KEY_PAD1); |
| 298 | key_timeb0 = ddio_KeyDownTime(KEY_PAD3); |
| 299 | |
| 300 | if (!key_timep1) |
| 301 | key_timep1 = ddio_KeyDownTime(KEY_PAGEDOWN); |
| 302 | if (!key_timep0) |
| 303 | key_timep0 = ddio_KeyDownTime(KEY_PAGEUP); |
| 304 | |
| 305 | if (key_timex1 || key_timex0 || key_timey1 || key_timey0 || key_timez1 || key_timez0) |
| 306 | ret_flags |= SLEW_KEY; |
| 307 | if (key_timep1 || key_timep0 || key_timeh1 || key_timeh0 || key_timeb1 || key_timeb0) |
| 308 | ret_flags |= SLEW_KEY; |
| 309 | |
| 310 | if (key_timez0 || key_timez1) { |
| 311 | mprintf_at(1, 0, 0, "Timez0: %.2f ", key_timez0); |
| 312 | mprintf_at(1, 1, 0, "Timez1: %.2f ", key_timez1); |
| 313 | } |
| 314 | |
| 315 | // adjust physics info of object accordingly to keyboard input. |
| 316 | obj->mtype.phys_info.velocity.x += VEL_SPEED * (key_timex1 - key_timex0) * Slew_key_speed; |
| 317 | obj->mtype.phys_info.velocity.y += VEL_SPEED * (key_timey1 - key_timey0) * Slew_key_speed; |
| 318 | obj->mtype.phys_info.velocity.z += VEL_SPEED * (key_timez1 - key_timez0) * Slew_key_speed; |
| 319 | |
| 320 | // mprintf(0,"<%x %x %x>",obj->mtype.phys_info.velocity.x,obj->mtype.phys_info.velocity.y,obj->mtype.phys_info.velocity.z)); |
| 321 | |
| 322 | rottime.x = key_timep1 - key_timep0; |
| 323 | rottime.y = key_timeh1 - key_timeh0; |
| 324 | rottime.z = key_timeb1 - key_timeb0; |
| 325 | rotang.p = (int16_t)(65536.0 * rottime.x * ROT_SPEED * Slew_key_speed); |
| 326 | rotang.h = (int16_t)(65536.0 * rottime.y * ROT_SPEED * Slew_key_speed); |
| 327 | rotang.b = (int16_t)(65536.0 * rottime.z * ROT_SPEED * Slew_key_speed); |
| 328 | |
| 329 | // joystick movement |
| 330 | #ifdef EDITOR |
| 331 | if (Joystick_active != -1) { |
| 332 | int joy_x, joy_y, btns; |
| 333 | tJoyPos joystate; |
| 334 | bool joyx_moved = false; |
| 335 | bool joyy_moved = false; |
| 336 | |
| 337 | joy_GetPos((tJoystick)Joystick_active, &joystate); // get all the stick values |
| 338 | |
| 339 | joy_x = joystate.x; |
| 340 | joy_y = joystate.y; |
| 341 | btns = joystate.buttons; |
| 342 | mprintf_at(2, 1, 0, "JoyX: %d ", joy_x); |
| 343 | mprintf_at(2, 2, 0, "JoyY: %d ", joy_y); |
| 344 | |
| 345 | if (abs(joy_x) < JOY_NULL) |
| 346 | joy_x = 0; |
| 347 | if (abs(joy_y) < JOY_NULL) |
| 348 | joy_y = 0; |
| 349 | |
| 350 | joyx_moved = (abs(joy_x - old_joy_x) > JOY_NULL); |
| 351 | joyy_moved = (abs(joy_y - old_joy_y) > JOY_NULL); |
| 352 | |
| 353 | //@@ if (joyx_moved) |
| 354 | //@@ mprintf(1,"SLEW: Joy X moved\n"); |
| 355 | //@@ if (joyy_moved) |
| 356 | //@@ mprintf(1,"SLEW: Joy Y moved\n"); |
| 357 | |
| 358 | if (btns) { |
| 359 | if (!rotang.p) |
| 360 | rotang.p = -joy_y * 256 * Frametime; |
| 361 | } else { |
| 362 | if (joyy_moved) |
| 363 | obj->mtype.phys_info.velocity.z = (float)-joy_y / 4.0; |
| 364 | } |
| 365 | |
| 366 | if (!rotang.h) |
| 367 | rotang.h = joy_x * 256 * Frametime; |
| 368 | |
| 369 | if (joyx_moved) |
| 370 | old_joy_x = joy_x; |
| 371 | if (joyy_moved) |
| 372 | old_joy_y = joy_y; |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | vm_AnglesToMatrix(&rotmat, rotang.p, rotang.h, rotang.b); |
| 377 | |
| 378 | new_pm = obj->orient * rotmat; |
| 379 | vm_Orthogonalize(&new_pm); |
| 380 | |
| 381 | ObjSetOrient(obj, &new_pm); |
| 382 | |
| 383 | vm_TransposeMatrix(&new_pm); // make those columns rows |
| 384 | |
| 385 | svel = obj->mtype.phys_info.velocity * Frametime; |
| 386 | movement = svel * new_pm; |
| 387 | |
| 388 | if (movement_limitations & 1) |
| 389 | movement.x = 0; |
| 390 | if (movement_limitations & 2) |
| 391 | movement.y = 0; |
| 392 | if (movement_limitations & 4) |
| 393 | movement.z = 0; |
| 394 | |
| 395 | new_pos = obj->pos + movement; |
| 396 | |
| 397 | // Did the object position change? |
| 398 | if ((movement.x != 0.0f) || (movement.y != 0.0f) || (movement.z != 0.0f)) |
| 399 | ret_flags |= SLEW_MOVE; |
| 400 | |
| 401 | if (ret_flags & SLEW_MOVE) { // Get the new room |
| 402 | bool outside_mine = ((obj->flags & OF_OUTSIDE_MINE) != 0); |
| 403 | |
| 404 | mprintf(1, "SLEW: Moved\n"); |
| 405 | |
| 406 | #ifdef EDITOR |
| 407 | if (Editor_view_mode == VM_ROOM) { |
| 408 | // Room number is bogus in room view, so don't update it |
| 409 | new_room = obj->roomnum; |
| 410 | } else |
| 411 | #endif |
| 412 | // NOTE LINK TO ABOVE IF |
| 413 | if (outside_mine) { // starting outside the mine? |
| 414 | |
| 415 | // See if we've moved back into a room |
| 416 | new_room = FindPointRoom(&new_pos); |
| 417 | |
| 418 | if (new_room != -1) { // back in the mine |
| 419 | outside_mine = 0; |
| 420 | mprintf(0, "SLEW: Re-entered mine at room %d\n", new_room); |
| 421 | } else // not back in the mine |
| 422 | new_room = obj->roomnum; |
| 423 | } else { |
| 424 | bool was_outside = (ROOMNUM_OUTSIDE(obj->roomnum) != 0); |
| 425 | |
| 426 | // Limit new position to terrain bounds if outside |
| 427 | if (was_outside) { |
| 428 | if (new_pos.x < 1.0) |
| 429 | new_pos.x = 1.0; |
| 430 | if (new_pos.x > TERRAIN_WIDTH * TERRAIN_SIZE - 1.0) |
| 431 | new_pos.x = TERRAIN_WIDTH * TERRAIN_SIZE - 1.0; |
| 432 | if (new_pos.z < 1.0) |
| 433 | new_pos.z = 1.0; |
| 434 | if (new_pos.z > TERRAIN_DEPTH * TERRAIN_SIZE - 1.0) |
| 435 | new_pos.z = TERRAIN_WIDTH * TERRAIN_SIZE - 1.0; |
| 436 | } |
| 437 | |
| 438 | // Call FVI up get updated room number |
| 439 | fq.p0 = &obj->pos; |
| 440 | fq.startroom = obj->roomnum; |
| 441 | fq.p1 = &new_pos; |
| 442 | fq.rad = 0; |
| 443 | fq.thisobjnum = OBJNUM(obj); |
| 444 | fq.ignore_obj_list = NULL; |
| 445 | fq.flags = FQ_IGNORE_RENDER_THROUGH_PORTALS; |
| 446 | fate = fvi_FindIntersection(&fq, &hit_info); |
| 447 | |
| 448 | // If bad room, don't move |
| 449 | if ((fate == HIT_OUT_OF_TERRAIN_BOUNDS) || (hit_info.hit_room == -1)) { |
| 450 | new_room = obj->roomnum; |
| 451 | new_pos = obj->pos; |
| 452 | } else |
| 453 | new_room = hit_info.hit_room; |
| 454 | |
| 455 | // The object hit a wall, and maybe went outside the mine. |
| 456 | if (fate == HIT_WALL) { |
| 457 | int t; |
| 458 | |
| 459 | mprintf(0, "SLEW: hit wall\n"); |
| 460 | |
| 461 | // Check if we're in a room |
| 462 | t = FindPointRoom(&new_pos); |
| 463 | |
| 464 | if (t != -1) { // We're in a room |
| 465 | new_room = t; |
| 466 | mprintf(0, "SLEW: still in mine in room %d\n", new_room); |
| 467 | } else { // Not in a room. Set a special flag |
| 468 | outside_mine = 1; |
| 469 | mprintf(0, "SLEW: left mine from room %d\n", new_room); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if (new_room != obj->roomnum) { // if we've changed rooms, say so |
| 474 | if (ROOMNUM_OUTSIDE(new_room)) |
| 475 | if (was_outside) |
| 476 | mprintf(0, "SLEW: Moved to cell %d, BOA TR %d\n", CELLNUM(new_room), TERRAIN_REGION(new_room)); |
| 477 | else |
| 478 | mprintf(0, "SLEW: Moved outside to cell %d\n", CELLNUM(new_room)); |
| 479 | else if (was_outside) |
| 480 | mprintf(0, "SLEW: Moved inside to room %d\n", new_room); |
| 481 | else |
| 482 | mprintf(0, "SLEW: Moved into room %d\n", new_room); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // Now we have the new room, so update the object position |
| 487 | ObjSetPos(obj, &new_pos, new_room, NULL, false); |
| 488 | |
| 489 | // Set outside-mine flag if we're outside |
| 490 | if (outside_mine) |
| 491 | obj->flags |= OF_OUTSIDE_MINE; |
| 492 | } |
| 493 | |
| 494 | // Set flag if rotation changed |
| 495 | if ((rotang.p != 0) || (rotang.h != 0) || (rotang.b != 0)) |
| 496 | ret_flags |= SLEW_ROTATE; |
| 497 | |
| 498 | return ret_flags; |
| 499 | } |
| 500 | |
| 501 | #endif |