| 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/physics/FindIntersection.cpp $ |
| 21 | * $Revision: 231 $ |
| 22 | * $Date: 10/25/99 1:17a $ |
| 23 | * $Author: Chris $ |
| 24 | * |
| 25 | * Find intersection code |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/physics/FindIntersection.cpp $ |
| 28 | * |
| 29 | * 231 10/25/99 1:17a Chris |
| 30 | * |
| 31 | * 230 10/21/99 3:12p Kevin |
| 32 | * Mac merge & memory savings |
| 33 | * |
| 34 | * 229 6/10/99 6:31p Chris |
| 35 | * Added support for the new editor |
| 36 | * |
| 37 | * 228 5/23/99 2:53a Chris |
| 38 | * Removed annoying asserts |
| 39 | * |
| 40 | * 227 5/23/99 12:32a Chris |
| 41 | * Fixed problems with ignoring render though portals |
| 42 | * |
| 43 | * 226 5/20/99 1:16a Chris |
| 44 | * Fixed the last wall stuck cases |
| 45 | * |
| 46 | * 225 5/18/99 11:10a Matt |
| 47 | * Added variable ceiling height. |
| 48 | * |
| 49 | * 224 5/17/99 6:05p Chris |
| 50 | * Added support for hit faces on a submodel |
| 51 | * |
| 52 | * 223 5/17/99 5:01p Matt |
| 53 | * Changed the code to check for objects in visited rooms whether the |
| 54 | * vector started on the terrain or inside. This fixes the problem where |
| 55 | * the mass driver, when fired from outside, could not hit objects inside. |
| 56 | * |
| 57 | * 222 5/12/99 6:16a Chris |
| 58 | * fvi_QuickDistObjectList if stop at closed doors, it also stops at |
| 59 | * blocked/forcefielded portals |
| 60 | * |
| 61 | * 221 5/09/99 3:07p Jason |
| 62 | * added a way to track FVI rays |
| 63 | * |
| 64 | * 220 5/07/99 6:21p Jeff |
| 65 | * hooked up fvi to rtperformance |
| 66 | * |
| 67 | * 219 5/03/99 3:14p Matt |
| 68 | * Fixed bug in Chris's hack. |
| 69 | * |
| 70 | * 218 5/03/99 1:52p Chris |
| 71 | * Hacked FVI to make relinking consistant even with level cracks |
| 72 | * |
| 73 | * 217 4/20/99 8:14p Chris |
| 74 | * Added support for object's that hit the ceiling and for making the |
| 75 | * level always check for the ceiling (inside and outside the mine) |
| 76 | * |
| 77 | * 216 4/18/99 5:42a Chris |
| 78 | * Added the FQ_IGNORE_RENDER_THROUGH_PORTALS flag |
| 79 | * |
| 80 | * 215 4/14/99 1:42a Jeff |
| 81 | * fixed case mismatched #includes |
| 82 | * |
| 83 | * 214 4/12/99 10:21a Chris |
| 84 | * Added another divide by zero assert |
| 85 | * |
| 86 | * 213 4/12/99 10:20a Chris |
| 87 | * Added an assert to catch bad hit obj sizes |
| 88 | * |
| 89 | * 212 4/08/99 4:53p Chris |
| 90 | * |
| 91 | * 211 4/05/99 3:56p Chris |
| 92 | * We should be able to pickup clutter now |
| 93 | * |
| 94 | * 210 4/02/99 10:18a Chris |
| 95 | * SPHERE-SPHERE for player rock collisions |
| 96 | * |
| 97 | * 209 3/31/99 5:16p Matt |
| 98 | * Don't look at non-shell faces when determining what room a point is in |
| 99 | * |
| 100 | * 208 3/09/99 10:52a Chris |
| 101 | * Fix a bug introduced when trying to to fix the trigger problems |
| 102 | * |
| 103 | * 207 3/05/99 1:02p Jason |
| 104 | * sped up raycasting for light coronas |
| 105 | * |
| 106 | * 206 3/04/99 8:15p Matt |
| 107 | * Hack that might help the exit level trigger problem on OEM level 0. |
| 108 | * |
| 109 | * 205 3/04/99 5:54p Chris |
| 110 | * Patched the occassional not found portal trigger |
| 111 | * |
| 112 | * 204 2/25/99 1:54p Chris |
| 113 | * Ignoring weapons now ignores shockwaves, shards, and fireballs |
| 114 | * |
| 115 | * 203 2/16/99 3:09p Chris |
| 116 | * |
| 117 | * 202 2/16/99 1:51p Chris |
| 118 | * FIxed multiple trigger bugs |
| 119 | * |
| 120 | * 201 2/15/99 4:38p Chris |
| 121 | * Damn... Lava was hitting triggers!!!!!! |
| 122 | * |
| 123 | * 200 2/15/99 11:47a Chris |
| 124 | * |
| 125 | * 199 2/11/99 6:25p Chris |
| 126 | * Added PF_NO_COLLIDE_DOORS |
| 127 | * |
| 128 | * 198 2/02/99 8:44a Chris |
| 129 | * I made buildings with AI work correctly (ie really big robots should be |
| 130 | * buildings) |
| 131 | * anim to and from states are now shorts instead of bytes |
| 132 | * |
| 133 | * 197 1/29/99 5:09p Chris |
| 134 | * Made changes for ROCKS |
| 135 | * |
| 136 | * 196 1/29/99 12:48p Matt |
| 137 | * Rewrote the doorway system |
| 138 | * |
| 139 | * 195 1/18/99 8:07p Chris |
| 140 | * Added the no-collide same flag (for flocks and nests) |
| 141 | * |
| 142 | * 194 1/18/99 2:46p Matt |
| 143 | * Combined flags & flags2 fields in object struct |
| 144 | * |
| 145 | * 193 1/15/99 5:59p Chris |
| 146 | * |
| 147 | * 192 1/13/99 6:54a Jeff |
| 148 | * fixed #includes for linux |
| 149 | * |
| 150 | * 191 1/13/99 2:29a Chris |
| 151 | * Massive AI, OSIRIS update |
| 152 | * |
| 153 | * 190 1/01/99 4:53p Chris |
| 154 | * Fixed a problem with long-rays causing unneccessary asserts |
| 155 | * |
| 156 | * 189 1/01/99 4:10p Chris |
| 157 | * Added some const parameters, improved ray cast object collide/rejection |
| 158 | * code |
| 159 | * |
| 160 | * 188 11/23/98 11:06a Chris |
| 161 | * Added support for finding just AIs and Players with |
| 162 | * fvi_QuickDistObjectList |
| 163 | * |
| 164 | * 187 11/02/98 6:15p Chris |
| 165 | * Room AABBs get saved with the level and the sort_face and dec_sort_face |
| 166 | * list s have been removed |
| 167 | * |
| 168 | * 186 10/29/98 5:20p Chris |
| 169 | * Player ships collide smaller now |
| 170 | * |
| 171 | * 185 10/22/98 10:25p Kevin |
| 172 | * took out volatile |
| 173 | * |
| 174 | * 184 10/22/98 10:25p Chris |
| 175 | * Fixed a major bug with fvi_QuickXList() -- preincrement f@cked me. |
| 176 | * |
| 177 | * 183 10/22/98 9:46p Chris |
| 178 | * |
| 179 | * 182 10/22/98 9:45p Chris |
| 180 | * Added some asserts |
| 181 | * |
| 182 | * 181 10/21/98 9:41p Chris |
| 183 | * Improved walking code! |
| 184 | * |
| 185 | * 180 10/21/98 7:44a Chris |
| 186 | * Tweaked player wall collisions |
| 187 | * |
| 188 | * 179 10/20/98 8:05p Sean |
| 189 | * Fixes callers with a start room of -1 |
| 190 | * |
| 191 | * 178 10/20/98 6:58p Chris |
| 192 | * My god - improved the cylinder code. :) |
| 193 | * |
| 194 | * 177 10/18/98 5:36p Chris |
| 195 | * |
| 196 | * 176 10/17/98 12:25p Chris |
| 197 | * Fixed attached flares |
| 198 | * |
| 199 | * 175 10/16/98 8:33p Chris |
| 200 | * Fixed attached flare problem |
| 201 | * |
| 202 | * 174 10/16/98 6:19p Chris |
| 203 | * More linked object fixes |
| 204 | * |
| 205 | * 173 10/16/98 12:27a Chris |
| 206 | * Improved collision code. Finish it tomarrow. |
| 207 | * |
| 208 | * 172 10/15/98 12:22a Matt |
| 209 | * Fixed compile warnings |
| 210 | * |
| 211 | * 171 9/22/98 6:42p Dan |
| 212 | * Temp fix |
| 213 | * |
| 214 | * 170 9/22/98 12:02p Matt |
| 215 | * Fixed compile warnings |
| 216 | * |
| 217 | * 169 9/01/98 12:04p Matt |
| 218 | * Ripped out multi-face portal code |
| 219 | * |
| 220 | * 168 8/28/98 4:33p Chris |
| 221 | * |
| 222 | * 167 8/17/98 3:14p Chris |
| 223 | * Fixed a bug with the FQ_IGNORE_WALLS flag that caused precomputed |
| 224 | * weapons not to relink into the mine from the terrain. |
| 225 | * |
| 226 | * 166 8/03/98 3:59p Chris |
| 227 | * Added support for FQ_IGNORE_WEAPONS, added .000001 attach code, fix a |
| 228 | * bug in polymodel collision detection |
| 229 | * |
| 230 | * 165 7/31/98 11:52a Chris |
| 231 | * Weapons can be persistent. Added ability for objects to be manually |
| 232 | * set for no object collisions. |
| 233 | * |
| 234 | * 164 7/23/98 4:08p Chris |
| 235 | * |
| 236 | * 163 7/23/98 12:46p Chris |
| 237 | * Added a flag to ignore external rooms |
| 238 | * |
| 239 | * 162 7/23/98 12:31p Chris |
| 240 | * |
| 241 | * 161 7/22/98 6:48p Chris |
| 242 | * |
| 243 | * 160 7/22/98 6:42p Chris |
| 244 | * A relink fix for points on a wall |
| 245 | * |
| 246 | * 159 7/22/98 2:31p Chris |
| 247 | * Fixed an odd bug with objects moving farther than a long ray externally |
| 248 | * (never happened until I did the weapon pre-compute stuff) |
| 249 | * |
| 250 | * 158 7/21/98 2:14p Chris |
| 251 | * Some FVI speedups - not done |
| 252 | * |
| 253 | * 157 7/17/98 9:56a Chris |
| 254 | * Intermediate checkin |
| 255 | * |
| 256 | * 156 7/16/98 8:29p Chris |
| 257 | * Partial implementation of the new collide code |
| 258 | * |
| 259 | * 155 7/02/98 2:51p Chris |
| 260 | * Added a fast line to bbox function |
| 261 | * |
| 262 | * 154 6/15/98 6:08p Chris |
| 263 | * Hit_type to hit_type[0] |
| 264 | * |
| 265 | * 153 6/15/98 6:58a Chris |
| 266 | * Comment'ed out most bbox related additions |
| 267 | * |
| 268 | * 152 6/03/98 6:50p Chris |
| 269 | * Fixed some infinity bugs |
| 270 | * |
| 271 | * 151 6/03/98 6:42p Chris |
| 272 | * Added multipoint collision detection an Assert on invalid (infinite |
| 273 | * endpoint). |
| 274 | * |
| 275 | * 150 6/03/98 12:34p Chris |
| 276 | * Fixed a potential bug in subdivided rays |
| 277 | * |
| 278 | * 149 6/01/98 9:23p Chris |
| 279 | * Removed a mprintf |
| 280 | * |
| 281 | * 148 6/01/98 9:23p Chris |
| 282 | * Fixed a bunch of collision problems |
| 283 | * |
| 284 | * 147 6/01/98 11:38a Chris |
| 285 | * Made FQ_SOLID_PORTALS more effective |
| 286 | * |
| 287 | * 146 5/26/98 10:45a Keneta |
| 288 | * Shockwaves should not collide with anything |
| 289 | * |
| 290 | * 145 5/22/98 2:42p Sean |
| 291 | * FROM JASON:Upped max next portals to 50 |
| 292 | * |
| 293 | * 144 5/20/98 1:46p Chris |
| 294 | * Fixed a bug in QuickDistFaceList |
| 295 | * |
| 296 | * 143 5/18/98 11:59a Chris |
| 297 | * |
| 298 | * 142 5/05/98 5:23p Chris |
| 299 | * Faster external room collisions with FQ_EXTERNAL_ROOMS_AS_SPHERE |
| 300 | * |
| 301 | * 141 5/04/98 10:17a Chris |
| 302 | * Improved checking |
| 303 | * |
| 304 | * 140 5/04/98 9:40a Chris |
| 305 | * Improved Physics_player_verbose stuff |
| 306 | * |
| 307 | * 139 5/04/98 12:16a Chris |
| 308 | * More debugging |
| 309 | * |
| 310 | * 138 5/03/98 10:59p Chris |
| 311 | * Fixed problems with external rooms |
| 312 | * |
| 313 | * 137 5/03/98 8:36p Chris |
| 314 | * Additional debug info |
| 315 | * |
| 316 | * 136 4/23/98 5:58p Chris |
| 317 | * OOps. |
| 318 | * |
| 319 | * 135 4/23/98 5:44p Chris |
| 320 | * Working on Door collisions |
| 321 | * |
| 322 | * 134 4/20/98 6:56p Chris |
| 323 | * Removed sphere check for poly2sphere (just AABB) |
| 324 | * |
| 325 | * 133 4/20/98 5:48p Chris |
| 326 | * Patching a collision bug |
| 327 | * |
| 328 | * 132 4/13/98 5:34p Chris |
| 329 | * Sped up fvi. check_hit_objs is not called at all for fvi calls with |
| 330 | * FQ_CHECK_OBJS set |
| 331 | * |
| 332 | * 131 4/13/98 4:41p Chris |
| 333 | * |
| 334 | * 130 4/13/98 3:51p Chris |
| 335 | * Added sphere bubbles to weapons (when wanted) and fixed a small FVI |
| 336 | * bug |
| 337 | * |
| 338 | * 129 4/13/98 2:21p Chris |
| 339 | * Fixed some collision problems dealing with AABBs and Polymodel paging |
| 340 | * in. |
| 341 | * |
| 342 | * 128 4/09/98 12:05p Chris |
| 343 | * Added parenting for all object types. :) |
| 344 | * |
| 345 | * 127 4/08/98 7:38p Chris |
| 346 | * |
| 347 | * 126 4/08/98 2:38p Chris |
| 348 | * Fixed powerup pickup bug |
| 349 | * |
| 350 | * 125 4/07/98 8:14p Chris |
| 351 | * Made powerups easier to hit |
| 352 | * |
| 353 | * 124 4/06/98 4:09p Chris |
| 354 | * Made f_correcting a bool -- NOTE: chrishack -- physics need to handle |
| 355 | * powerups in a good manner (flying through them). |
| 356 | * |
| 357 | * 123 4/06/98 2:54p Chris |
| 358 | * TEMPORARY FIX -- Object collisions are of less priority than wall |
| 359 | * collisions -- keep this until we have multipoint collision detection -- |
| 360 | * chrishack |
| 361 | * |
| 362 | * 122 4/03/98 3:35p Chris |
| 363 | * Fixed problems with FQ_LIGHTMAP_ONLY (has to include external rooms) |
| 364 | * |
| 365 | * 121 4/03/98 11:27a Chris |
| 366 | * Doubled the size of powerups |
| 367 | * |
| 368 | * 120 4/01/98 3:51p Chris |
| 369 | * |
| 370 | * 119 4/01/98 3:46p Chris |
| 371 | * Added hack for powerups and players |
| 372 | * |
| 373 | * 118 3/25/98 1:17p Chris |
| 374 | * Removed a bug. |
| 375 | * |
| 376 | * 117 3/24/98 2:57p Chris |
| 377 | * Fixed a bug in the QuickDistObjectList function |
| 378 | * |
| 379 | * 116 3/23/98 11:37a Chris |
| 380 | * Added the f_lightmap_only parameter to fvi_QuickDistObjectList |
| 381 | * |
| 382 | * 115 3/23/98 11:18a Chris |
| 383 | * Added int fvi_QuickDistObjectList(vector *pos, int init_room_index, |
| 384 | * float rad, int16_t *object_index_list, int max_elements) |
| 385 | * |
| 386 | * |
| 387 | * 114 3/18/98 10:12p Chris |
| 388 | * Added some more bug checking. If 2 objects radii add up to zero, then |
| 389 | * let me know. :) |
| 390 | * |
| 391 | * 113 3/18/98 5:01p Chris |
| 392 | * Fixed a bug with the zero rad wall collision code |
| 393 | * |
| 394 | * 112 3/18/98 4:50p Chris |
| 395 | * Added a speed opt. for zero radius wall collision objects |
| 396 | * |
| 397 | * 111 3/18/98 4:31p Chris |
| 398 | * Speed up fvi and fixed some bugs |
| 399 | * |
| 400 | * 110 3/17/98 3:36p Chris |
| 401 | * Fixed bugs in the relink code and the QuickDistFaceList code. (They |
| 402 | * where using fvi_max_xyz and fvi_min_xyz) |
| 403 | * |
| 404 | * 109 3/17/98 11:33a Chris |
| 405 | * Improved performance (AABB updates on poly-sphere collide) |
| 406 | * |
| 407 | * 108 3/16/98 9:55p Chris |
| 408 | * Working on optimizing fvi |
| 409 | * |
| 410 | * 107 3/16/98 8:25p Chris |
| 411 | * Fixed bugs with the new fvi code |
| 412 | * |
| 413 | * 106 3/16/98 6:55p Chris |
| 414 | * Temporary fix. There is a problem somewhere in the new optimazations. |
| 415 | * |
| 416 | * 105 3/16/98 5:50p Chris |
| 417 | * Added sorted face lists for fvi |
| 418 | * |
| 419 | * 104 3/16/98 2:52p Chris |
| 420 | * Moved some asserts for FQ_NO_RELINK |
| 421 | * |
| 422 | * 103 3/16/98 1:13p Chris |
| 423 | * Speed up |
| 424 | * |
| 425 | * 102 3/16/98 12:50p Chris |
| 426 | * Speed up? |
| 427 | * |
| 428 | * 101 3/16/98 9:46a Chris |
| 429 | * Improved the speed |
| 430 | * |
| 431 | * 100 3/16/98 9:43a Chris |
| 432 | * Added FQ_NO_RELINK |
| 433 | * |
| 434 | * 99 3/16/98 9:25a Chris |
| 435 | * Sped up fvi |
| 436 | * |
| 437 | * 98 3/13/98 5:55p Chris |
| 438 | * Added the new collision spheres |
| 439 | * |
| 440 | * 97 2/23/98 6:50p Jason |
| 441 | * changes to help facilitate fast lighting with shadow volumes |
| 442 | * |
| 443 | * |
| 444 | * 96 2/20/98 1:45p Chris |
| 445 | * Milestone fixes |
| 446 | * |
| 447 | * 95 2/19/98 6:17p Chris |
| 448 | * Added some debug info |
| 449 | * |
| 450 | * 94 2/15/98 2:08p Chris |
| 451 | * |
| 452 | * 93 2/12/98 2:07p Chris |
| 453 | * Fixed the trigger face list |
| 454 | * |
| 455 | * 92 2/11/98 10:04p Chris |
| 456 | * Added FQ_ONLY_DOOR_OBJ and fixed a bug with FQ_ONLY_PLAYER_OBJ |
| 457 | * |
| 458 | * 91 2/10/98 2:45p Chris |
| 459 | * Made objects not get checked if FQ_CHECK_OBJECTS is not set. |
| 460 | * (OBJ_ROOMS are still checked) |
| 461 | * |
| 462 | * 90 2/10/98 2:41p Chris |
| 463 | * Fixed a problem with large objects beening hit when they shouldn't |
| 464 | * |
| 465 | * 89 2/06/98 3:47p Brent |
| 466 | * CHRIS -- I made the MAX_CELLS_VISITED equal to the number of cells on |
| 467 | * the terrain. This is insane, but it allows for the placement of HUGE |
| 468 | * objects. :) -- Thanx Brent. |
| 469 | * |
| 470 | * 88 2/06/98 2:57a Chris |
| 471 | * Added point_collide_with_walls and ignore_robot_collisions |
| 472 | * |
| 473 | * 87 2/05/98 1:58p Chris |
| 474 | * Made the cellnum be a roomnum for hitting terrain |
| 475 | * |
| 476 | * 86 2/04/98 6:23p Matt |
| 477 | * Changed object room number to indicate a terrain cell via a flag. Got |
| 478 | * rid of the object flag which used to indicate terrain. |
| 479 | * |
| 480 | * 85 2/02/98 8:19p Chris |
| 481 | * Fixed the hung on wall problem and the powerup problem. |
| 482 | * |
| 483 | * 84 1/29/98 5:50p Matt |
| 484 | * Changed old camera object type to be viewer object (for editor), and |
| 485 | * now camera objects will just be for game cameras. |
| 486 | * |
| 487 | * 83 1/20/98 12:25p Luke |
| 488 | * CHRIS-- Added a check to make sure that the startseg is not an external |
| 489 | * room |
| 490 | * |
| 491 | * 82 1/20/98 12:22p Chris |
| 492 | * Removed the attempting to patch mprintf |
| 493 | * |
| 494 | * 81 1/19/98 5:05p Chris |
| 495 | * Added ceiling collisions for players and powerups |
| 496 | * |
| 497 | * 80 1/14/98 7:56p Jason |
| 498 | * don't do obj relink if you're in the same room |
| 499 | * |
| 500 | * 79 1/07/98 4:11p Jason |
| 501 | * CHRIS -- Fixed stupid mistake with edge fix -- .y values where getting |
| 502 | * misassigned. |
| 503 | * |
| 504 | * 78 1/06/98 7:14p Chris |
| 505 | * Made edge of world less dangerous |
| 506 | * |
| 507 | * 77 12/22/97 6:19p Chris |
| 508 | * Moved weapon battery firing sound off the projectile (weapon) and into |
| 509 | * the weapon battery. |
| 510 | * |
| 511 | * 76 12/18/97 12:25p Chris |
| 512 | * Sub-divide long rays and use a better flat-line algorithm for the |
| 513 | * terrain fvi. |
| 514 | * |
| 515 | * 75 12/16/97 6:11p Chris |
| 516 | * Attempt to improve FVI for outside |
| 517 | * |
| 518 | * 74 12/03/97 2:36p Sean |
| 519 | * Added another code fix for external room problems |
| 520 | * |
| 521 | * 73 11/24/97 11:41a Chris |
| 522 | * Fixed a small glitch in the fvi_check_node stuff |
| 523 | * |
| 524 | * 72 11/24/97 11:01a Chris |
| 525 | * Removed a debugging assert |
| 526 | * |
| 527 | * 71 11/24/97 10:15a Chris |
| 528 | * Improved fvi |
| 529 | * |
| 530 | * 70 11/21/97 5:26p Jason |
| 531 | * checked in so chris can find a bug |
| 532 | * |
| 533 | * 68 11/20/97 6:02p Chris |
| 534 | * Fixed the quick cell list code |
| 535 | * |
| 536 | * 67 11/17/97 5:46p Chris |
| 537 | * Fixed a problem in line_to_sphere1 it -- see history for diff. |
| 538 | * |
| 539 | * 66 11/11/97 8:39p Chris |
| 540 | * Passed the wrong object to PolyCollide when doing the relative motion |
| 541 | * mapping of polygon moving and hitting a sphere. |
| 542 | * |
| 543 | * 65 11/11/97 11:26a Sean |
| 544 | * CHRIS -- increased MAX_NEXT_PORTALS. It seems that radiosity rays can |
| 545 | * cross a lot of portals in a single room (in this case it was over 5)... |
| 546 | * Beefed it up to 20. It that causes a problem, it can be beefed up more |
| 547 | * without any disadvantage. |
| 548 | * |
| 549 | * 64 11/03/97 5:10p Jason |
| 550 | * Fixed by Chris, forgot to assume fvi_terrain_obj_visit_list to the max |
| 551 | * cells visited limit. |
| 552 | * |
| 553 | * 63 10/29/97 3:39p Matt |
| 554 | * Got rid of compiler warning |
| 555 | * |
| 556 | * 62 10/28/97 5:46p Chris |
| 557 | * Added a quick cell list function |
| 558 | * |
| 559 | * 61 10/28/97 12:20p Chris |
| 560 | * Added support to ignore wall collisions |
| 561 | * |
| 562 | * 60 10/22/97 4:33p Chris |
| 563 | * Improved default hit_obj_type collision types (i.e. when the ray is |
| 564 | * cast with a -1 as its index) |
| 565 | * |
| 566 | * 59 10/22/97 4:30p Chris |
| 567 | * We can now slew between the mine and the terrain |
| 568 | * |
| 569 | * 58 10/22/97 12:42p Chris |
| 570 | * Incremental BBOX stuff and added support for subobject hit returned |
| 571 | * from fvi |
| 572 | * |
| 573 | * 57 10/21/97 4:14p Chris |
| 574 | * Incremental improvements to the fvi/physics/collide integration |
| 575 | * |
| 576 | * 56 10/20/97 3:51p Chris |
| 577 | * Fixed a problem with the POLY_BBOX collide code |
| 578 | * |
| 579 | * 55 10/20/97 3:43p Chris |
| 580 | * Added more collide code |
| 581 | * |
| 582 | * 54 10/20/97 11:55a Chris |
| 583 | * Added some support for the new collide system. |
| 584 | * |
| 585 | * 53 10/15/97 5:08p Chris |
| 586 | * Fixed the collision_side_pnt and added a relative position/velocity |
| 587 | * check |
| 588 | * |
| 589 | * 52 10/14/97 6:56p Chris |
| 590 | * In middle of some incremental improvements |
| 591 | * |
| 592 | * 51 10/07/97 3:51p Chris |
| 593 | * Added support for object backface checking -- external rooms are |
| 594 | * classified as objects |
| 595 | * |
| 596 | * 50 10/06/97 2:36p Chris |
| 597 | * Improved and fixed fvi. |
| 598 | * |
| 599 | * 49 10/06/97 12:36p Chris |
| 600 | * Intermediate fix for terrain rendering problems |
| 601 | * |
| 602 | * 48 10/06/97 10:52a Jason |
| 603 | * added "FQ_IGNORE_NON_LIGHTMAP_OBJECTS" flag |
| 604 | * |
| 605 | * 47 10/05/97 6:14a Chris |
| 606 | * Added terrain speedup. Allowed poly-collision for weapons. |
| 607 | * |
| 608 | * 46 10/03/97 6:24p Chris |
| 609 | * Corrected a mistype to disallow all camera collisions |
| 610 | * |
| 611 | * 45 10/03/97 3:34p Chris |
| 612 | * No camera collisions |
| 613 | * |
| 614 | * 44 10/02/97 7:02p Chris |
| 615 | * Removed LOD optimazation |
| 616 | * |
| 617 | * 43 10/02/97 6:44p Chris |
| 618 | * Added support for quick fvi_checks |
| 619 | * |
| 620 | * 42 10/02/97 5:38p Chris |
| 621 | * Added support for invisible terrain tiles |
| 622 | * |
| 623 | * 41 10/02/97 11:34a Chris |
| 624 | * Added support for external room collisions |
| 625 | * |
| 626 | * 40 9/19/97 6:58p Chris |
| 627 | * Fixed some bugs with the big object system and removed some annoying |
| 628 | * mprintf's |
| 629 | * |
| 630 | * 39 9/19/97 4:56p Jason |
| 631 | * FROM CHRIS: Took out clutter assert in check_hit_obj |
| 632 | * |
| 633 | * 38 9/19/97 1:01p Chris |
| 634 | * Added better large object support |
| 635 | * |
| 636 | * 37 9/17/97 3:24p Chris |
| 637 | * |
| 638 | * 36 9/17/97 11:01a Chris |
| 639 | * Working on poly collision code (not done) and removed dependance on |
| 640 | * segment.h |
| 641 | * |
| 642 | * 35 9/15/97 6:24p Chris |
| 643 | * Added a second chance check if no rooms are initialy found |
| 644 | * |
| 645 | * 34 9/15/97 4:43p Craig |
| 646 | * |
| 647 | * 33 9/15/97 5:20a Chris |
| 648 | * Added sphere2poly support. Rearranged object collisions. |
| 649 | * |
| 650 | * 32 9/12/97 6:36p Chris |
| 651 | * Added collision terrain segment support |
| 652 | * Added some LARGE OBJECT collision support |
| 653 | * |
| 654 | * 31 9/12/97 10:37a Chris |
| 655 | * Added a zero radius speed up for fvi terrain. |
| 656 | * |
| 657 | * 30 9/11/97 7:08p Matt |
| 658 | * Fixed minor bug |
| 659 | * |
| 660 | * 29 9/11/97 3:10p Chris |
| 661 | * |
| 662 | * 28 9/11/97 3:08p Chris |
| 663 | * Added support for weapons flying through holes in transparent textures. |
| 664 | * |
| 665 | * 27 9/11/97 1:58p Chris |
| 666 | * Added ability to ignore moving objects |
| 667 | * |
| 668 | * 26 9/11/97 12:43p Chris |
| 669 | * Added new support for face_physics_info from room.h. Also, changed how |
| 670 | * we detect portals. Also added recorded faces. |
| 671 | * |
| 672 | * 25 9/10/97 11:58a Matt |
| 673 | * Ripped out some unused commented-out code |
| 674 | * |
| 675 | * 24 9/02/97 6:32p Jason |
| 676 | * Fixed fvi_rooms_visited array in the quick face list code. |
| 677 | * |
| 678 | * 23 9/02/97 4:16p Chris |
| 679 | * Fixed the 'clockwise' vertices problem with backface checking. I.e. |
| 680 | * vertices should be oppositely ordered when negating the face normal. |
| 681 | * |
| 682 | * 22 9/02/97 3:06p Chris |
| 683 | * Fixed stupid bug |
| 684 | * |
| 685 | * 21 9/02/97 3:04p Chris |
| 686 | * Fixed quick face list bug |
| 687 | * |
| 688 | * 20 9/02/97 11:57a Chris |
| 689 | * Fix a bug in the quick_dist stuff |
| 690 | * |
| 691 | * 19 9/02/97 11:41a Chris |
| 692 | * Added support the the quick face/room from distance function |
| 693 | * |
| 694 | * 18 8/26/97 1:44p Chris |
| 695 | * Fixed a bug in the clipping of a vector to the terrain boundaries. |
| 696 | * |
| 697 | * 17 8/25/97 6:21p Chris |
| 698 | * Portals only crossed at centerpoint |
| 699 | * |
| 700 | * 16 8/20/97 3:16p Chris |
| 701 | * Working on the edge of terrain problems |
| 702 | * |
| 703 | * 15 8/18/97 1:48a Chris |
| 704 | * Added AABB speedups. Added a new way to do inter-room and mine-terrain |
| 705 | * movements. Added a fvi_relink that correctly uses the hit_seg |
| 706 | * FVIF_OUTSIDE flag |
| 707 | * |
| 708 | * 14 8/12/97 3:47p Chris |
| 709 | * Updated the AABB stuff. |
| 710 | * |
| 711 | * 13 8/12/97 1:13p Chris |
| 712 | * Added AABBs. |
| 713 | * |
| 714 | * 12 8/11/97 5:49p Chris |
| 715 | * Allow for some terrain to mine flights |
| 716 | * |
| 717 | * 11 8/05/97 4:43p Chris |
| 718 | * Added support for fly by textures. Added support (fly by) support for |
| 719 | * fly thru textures. |
| 720 | * |
| 721 | * 10 8/04/97 5:35p Chris |
| 722 | * Added support for back face collisions and new fvi code |
| 723 | * |
| 724 | * 9 8/04/97 12:37p Chris |
| 725 | * We now support fvi in Rooms :) |
| 726 | * |
| 727 | * 8 7/31/97 3:12p Chris |
| 728 | * Fixed a problem with the new iterator based radius checker |
| 729 | * |
| 730 | * 7 7/28/97 1:16p Chris |
| 731 | * Allow for player and robots to be hit by weapons and correct parenting |
| 732 | * of weapons. |
| 733 | * |
| 734 | * 6 7/23/97 6:27p Jason |
| 735 | * added code to support terrain simplification |
| 736 | * |
| 737 | * 5 7/23/97 5:14p Chris |
| 738 | * Used a faster iterator for visiting terrain segments |
| 739 | * |
| 740 | * 4 7/15/97 7:29p Chris |
| 741 | * Added a sound for helicopter blades. |
| 742 | * |
| 743 | * 3 7/15/97 5:35p Chris |
| 744 | * New AI code |
| 745 | * |
| 746 | * 39 6/24/97 4:01p Chris |
| 747 | * Fixed another problem with the .y dependancy. |
| 748 | * |
| 749 | * 38 6/24/97 3:53p Chris |
| 750 | * Fixed a bug with the new .y dependancy |
| 751 | * |
| 752 | * 37 6/24/97 3:29p Chris |
| 753 | * Removed the .pos dependancy for terrain and am using the .y instead |
| 754 | * |
| 755 | * 36 6/18/97 3:52p Chris |
| 756 | * |
| 757 | * 35 6/12/97 2:40p Chris |
| 758 | * |
| 759 | * 34 6/10/97 6:23p Chris |
| 760 | * |
| 761 | * 33 6/10/97 5:32p Chris |
| 762 | * |
| 763 | * 32 6/10/97 5:32p Chris |
| 764 | * |
| 765 | * 31 6/05/97 7:22p Matt |
| 766 | * When get list of faces for mesh side, return the normals for each face. |
| 767 | * Also, added a function to return the four floating verts in a side. |
| 768 | * |
| 769 | * 30 5/27/97 5:36p Chris |
| 770 | * |
| 771 | * 29 5/27/97 5:36p Chris |
| 772 | * Incremental. |
| 773 | * |
| 774 | * 28 5/27/97 5:05p Chris |
| 775 | * Added physics for floating segments. We need more |
| 776 | * code if all mesh faces get their own normals or if mesh sides are used |
| 777 | * for non-floating other sides. |
| 778 | * |
| 779 | * 27 5/13/97 5:52p Chris |
| 780 | * Added ability to exit and enter mine. Also did some |
| 781 | * incremental improvements. |
| 782 | * |
| 783 | * 26 5/06/97 11:18a Chris |
| 784 | * |
| 785 | * 25 5/05/97 5:41a Chris |
| 786 | * Added some better polygon collision code. It is still rough though. |
| 787 | * |
| 788 | * 24 4/17/97 3:10p Chris |
| 789 | * Added edge of world object delete. Also did some incremental |
| 790 | * improvements. |
| 791 | * |
| 792 | * 23 4/16/97 3:25p Chris |
| 793 | * Commented the code |
| 794 | * |
| 795 | * 22 4/16/97 1:39p Chris |
| 796 | * |
| 797 | * 21 4/15/97 6:31p Chris |
| 798 | * Added support for self-correcting and non-correcting |
| 799 | * sphere collisions. This allows objects to ignore sphere |
| 800 | * collisions that happen at initial positions, but allows the walls to |
| 801 | * self-correct edges. |
| 802 | * |
| 803 | * 20 4/15/97 4:13p Chris |
| 804 | * |
| 805 | * 19 4/11/97 3:05a Chris |
| 806 | * Added some terrain collision code. Need ground. |
| 807 | * |
| 808 | * 18 3/24/97 6:29p Chris |
| 809 | * |
| 810 | * 17 3/24/97 3:50p Chris |
| 811 | * Removed some annoying, but informative physics info. |
| 812 | * |
| 813 | * 16 3/24/97 3:27p Chris |
| 814 | * Fixed some corner problems |
| 815 | * |
| 816 | * 15 3/24/97 11:43a Chris |
| 817 | * |
| 818 | * 14 3/21/97 7:08p Chris |
| 819 | * Working on rewritting FVI and Physics |
| 820 | * |
| 821 | * 13 3/20/97 6:26p Chris |
| 822 | * |
| 823 | * 12 3/20/97 6:18p Chris |
| 824 | * |
| 825 | * 11 3/20/97 6:13p Chris |
| 826 | * |
| 827 | * 10 3/20/97 6:06p Chris |
| 828 | * |
| 829 | * 9 3/17/97 7:36p Chris |
| 830 | * Made it easier to hit objects... :) |
| 831 | * |
| 832 | * 8 3/17/97 11:55a Chris |
| 833 | * Added more debug info. |
| 834 | * |
| 835 | * 7 3/14/97 12:18p Chris |
| 836 | * Tweak physics and remove braking for now. |
| 837 | * |
| 838 | * 6 3/13/97 1:17p Chris |
| 839 | * Incremental improvements... |
| 840 | * |
| 841 | * 5 3/12/97 6:31p Chris |
| 842 | * Alpha 1.0 of player "flying" in the mine |
| 843 | * |
| 844 | * 4 3/07/97 7:01p Chris |
| 845 | * Segments now have perfect edges and corners :) |
| 846 | * |
| 847 | * 3 3/03/97 1:03p Chris |
| 848 | * |
| 849 | * 2 3/03/97 5:52a Chris |
| 850 | * Pre-alpha .01 |
| 851 | * |
| 852 | * 1 3/03/97 3:25a Chris |
| 853 | * |
| 854 | * $NoKeywords: $ |
| 855 | */ |
| 856 | |
| 857 | #include <cmath> |
| 858 | #include <cstring> |
| 859 | |
| 860 | #include "3d.h" |
| 861 | #include "collide.h" |
| 862 | #include "doorway.h" |
| 863 | #include "mono.h" |
| 864 | #include "findintersection.h" |
| 865 | #include "physics.h" |
| 866 | #include "player.h" |
| 867 | #include "polymodel.h" |
| 868 | #include "pserror.h" |
| 869 | #include "renderer.h" |
| 870 | #include "room.h" |
| 871 | #include "terrain.h" |
| 872 | #include "weapon.h" |
| 873 | |
| 874 | #ifndef NED_PHYSICS |
| 875 | #include "gametexture.h" |
| 876 | #include "rtperformance.h" // Debug performance includes (do nothing in final release) |
| 877 | #endif |
| 878 | |
| 879 | int FVI_counter; |
| 880 | int FVI_room_counter; |
| 881 | |
| 882 | //------------------------------------------------------------------------------------------ |
| 883 | // Defines and globals for fvi_FindIntersection |
| 884 | //------------------------------------------------------------------------------------------ |
| 885 | |
| 886 | #define MAX_CELLS_VISITED (TERRAIN_DEPTH * TERRAIN_WIDTH) // Maximum terrain cells visited in one fvi call |
| 887 | |
| 888 | bool FVI_always_check_ceiling = false; |
| 889 | |
| 890 | // This doesn't really belong here, but I don't know where else to put it. |
| 891 | float Ceiling_height = MAX_TERRAIN_HEIGHT; |
| 892 | |
| 893 | // Bit fields for quick 'already-checked' checking |
| 894 | static uint8_t |
| 895 | fvi_visit_list[MAX_ROOMS / 8 + 1]; // This bit-field provides a fast check if a mine segment has been visited |
| 896 | static uint8_t fvi_terrain_visit_list[(TERRAIN_DEPTH * TERRAIN_WIDTH) / 8 + |
| 897 | 1]; // This bit-field provides a fast check if a terrain segment has been visited |
| 898 | static uint8_t |
| 899 | fvi_terrain_obj_visit_list[(TERRAIN_DEPTH * TERRAIN_WIDTH) / 8 + |
| 900 | 1]; // This bit-field provides a fast check if a terrain segment has been visited |
| 901 | |
| 902 | // The number rooms and terrain cells that this fvi call visited. |
| 903 | static int fvi_num_rooms_visited; |
| 904 | static int fvi_num_cells_visited; |
| 905 | static int fvi_num_cells_obj_visited; |
| 906 | |
| 907 | // Should we do a terrain check. This flag exists because if we do a terrain check, it always does a full check. So, |
| 908 | // we only have to do it once. |
| 909 | static bool f_check_terrain; |
| 910 | static bool fvi_zero_rad; |
| 911 | |
| 912 | // Unordered list of rooms and terrain cells that this fvi call visited. |
| 913 | // DAJ changed to ushorts to save memory |
| 914 | static uint16_t fvi_rooms_visited[MAX_ROOMS]; // This should be a small number (100 to 1000) |
| 915 | static uint16_t fvi_cells_visited[MAX_CELLS_VISITED]; // Use this so that we do not have to use 256x256 elements |
| 916 | static uint16_t fvi_cells_obj_visited[MAX_CELLS_VISITED]; |
| 917 | |
| 918 | // Fvi wall collision stuff |
| 919 | static float fvi_wall_sphere_rad; |
| 920 | static vector fvi_wall_sphere_offset; |
| 921 | static vector fvi_wall_sphere_p0; |
| 922 | static vector fvi_wall_sphere_p1; |
| 923 | |
| 924 | static float fvi_anim_sphere_rad; |
| 925 | static vector fvi_anim_sphere_offset; |
| 926 | static vector fvi_anim_sphere_p0; |
| 927 | static vector fvi_anim_sphere_p1; |
| 928 | |
| 929 | // Fvi information pointers. |
| 930 | fvi_info *fvi_hit_data_ptr; |
| 931 | fvi_query *fvi_query_ptr; |
| 932 | |
| 933 | // Best collision's distance |
| 934 | float fvi_collision_dist; |
| 935 | |
| 936 | // AABB for the movement |
| 937 | static vector fvi_max_xyz; |
| 938 | static vector fvi_min_xyz; |
| 939 | static vector fvi_movement_delta; |
| 940 | |
| 941 | // AABB for the movement |
| 942 | static vector fvi_wall_max_xyz; |
| 943 | static vector fvi_wall_min_xyz; |
| 944 | |
| 945 | // CHRISHACK -- Do we still need this???? |
| 946 | int fvi_curobj; |
| 947 | int fvi_moveobj; |
| 948 | |
| 949 | // Recorded faces |
| 950 | fvi_face_room_list Fvi_recorded_faces[MAX_RECORDED_FACES]; |
| 951 | int Fvi_num_recorded_faces = 0; |
| 952 | |
| 953 | //------------------------------------------------------------------------------------------ |
| 954 | // Some function def's |
| 955 | //------------------------------------------------------------------------------------------ |
| 956 | |
| 957 | static int do_fvi_terrain(); |
| 958 | static int fvi_room(int room_index, int from_portal, int room_obj = -1); |
| 959 | static void do_fvi_rooms(int initial_room_index); |
| 960 | /// Find the point on the specified plane where the line intersects. |
| 961 | /// - returns: true if point found, false if line parallel to plane. |
| 962 | /// - parameter new_pnt: is the found point on the plane. |
| 963 | /// - parameter plane_pnt: describe the plane. |
| 964 | /// - parameter plane_norm: describe the plane. |
| 965 | /// - parameter p0: are the ends of the line. |
| 966 | /// - parameter p1: are the ends of the line. |
| 967 | /// |
| 968 | /// Assumes that the initial point is not intersecting the plane. |
| 969 | static inline int find_plane_line_intersection(vector *intp, vector *colp, vector *plane_pnt, const vector *plane_norm, |
| 970 | const vector *p0, const vector *p1, float rad); |
| 971 | static bool IsPointInCylinder(vector *normal, vector *cylinder_pnt, vector *edir, float elen, const float rad, |
| 972 | const vector *pnt, vector *mdir, bool *f_collide); |
| 973 | |
| 974 | //! check if a sphere intersects a face -- this can be optimized (only need 2d stuff after rotation) |
| 975 | static int check_vector_to_cylinder(vector *colp, vector *intp, float *col_dist, vector *wall_norm, const vector *p0, |
| 976 | const vector *p1, float rad, vector *ep0, vector *ep1); |
| 977 | |
| 978 | //! check if a sphere intersects a face. |
| 979 | static int check_sphere_to_face(vector *colp, vector *intp, float *col_dist, vector *wall_norm, const vector *p0, |
| 980 | const vector *p1, vector *face_normal, int nv, float rad, vector **vertex_ptr_list); |
| 981 | static void fvi_rooms_objs(void); |
| 982 | static int obj_in_list(int objnum, int *obj_list); |
| 983 | static void make_trigger_face_list(int last_sim_faces); |
| 984 | static bool PhysPastPortal(const room *rp, portal *pp); |
| 985 | |
| 986 | //------------------------------------------------------------------------------------------ |
| 987 | // FVI FUNCTIONS |
| 988 | //------------------------------------------------------------------------------------------ |
| 989 | |
| 990 | void InitFVI() { |
| 991 | memset(fvi_terrain_visit_list, 0, (TERRAIN_DEPTH * TERRAIN_WIDTH) / 8 + 1); |
| 992 | memset(fvi_terrain_obj_visit_list, 0, (TERRAIN_DEPTH * TERRAIN_WIDTH) / 8 + 1); |
| 993 | memset(fvi_visit_list, 0, MAX_ROOMS / 8 + 1); |
| 994 | } |
| 995 | |
| 996 | // find the point on the specified plane where the line intersects |
| 997 | // returns true if point found, false if line parallel to plane |
| 998 | // new_pnt is the found point on the plane |
| 999 | // plane_pnt & plane_norm describe the plane |
| 1000 | // p0 & p1 are the ends of the line |
| 1001 | // Assumes that the initial point is not intersecting the plane |
| 1002 | inline int find_plane_line_intersection(vector *intp, vector *colp, vector *plane_pnt, const vector *plane_norm, |
| 1003 | const vector *p0, const vector *p1, float rad) { |
| 1004 | vector line_vec; // Vector from p0 to p1 |
| 1005 | vector point_plane_vec; // Vector from p0 to a point on the plane |
| 1006 | float proj_dist_line; // Distance projection of line onto the plane normal |
| 1007 | float proj_dist_point_plane; // Distance of the object from the plane |
| 1008 | |
| 1009 | ASSERT(intp != nullptr && plane_pnt != nullptr && colp != nullptr && plane_norm != nullptr && p0 != nullptr && |
| 1010 | p1 != nullptr && rad >= 0.0); |
| 1011 | |
| 1012 | // Line direction |
| 1013 | line_vec = *p1 - *p0; |
| 1014 | |
| 1015 | // Compute the distance to the plane and the distance the line travels in the direction of the normal |
| 1016 | // Negative because if the object is moving toward the plane, it is moving in the opposite direction of the normal |
| 1017 | proj_dist_line = (*plane_norm * line_vec); |
| 1018 | if (proj_dist_line >= 0.0f) |
| 1019 | return 0; |
| 1020 | |
| 1021 | // Vector from p0 to a point on the plane |
| 1022 | point_plane_vec = *plane_pnt - *p0; |
| 1023 | proj_dist_point_plane = (*plane_norm * point_plane_vec); |
| 1024 | |
| 1025 | // Throw out any sphere who's centerpoint is initially behind the face |
| 1026 | if (proj_dist_point_plane > 0.0) |
| 1027 | return 0; |
| 1028 | |
| 1029 | // Use the distance from the edge of the sphere to the plane. If the new proj_dist_point_plane is |
| 1030 | // negative, then the sphere pokes thru the edge at the initial position |
| 1031 | proj_dist_point_plane += rad; |
| 1032 | |
| 1033 | if (proj_dist_point_plane > 0.0 && proj_dist_line < 0.0) { |
| 1034 | *intp = *p0; |
| 1035 | *colp = *intp + *plane_norm * (-rad + proj_dist_point_plane); |
| 1036 | |
| 1037 | return 1; |
| 1038 | } |
| 1039 | |
| 1040 | // cannot intersect wall if we are more than a rad away (closest point check) |
| 1041 | if (proj_dist_point_plane <= proj_dist_line) { |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | // If we are moving almost parallal to the plane, then make sure we are a rad away form it |
| 1046 | // I picked .00000001 from my head. It would be pretty parallel of a pretty short movement and |
| 1047 | // the linear combination below might not product a nice answer |
| 1048 | if (fabs(proj_dist_line) <= 0.00000000001) { |
| 1049 | float plane_dist; |
| 1050 | |
| 1051 | plane_dist = (*p1 - *plane_pnt) * *plane_norm; |
| 1052 | if (plane_dist >= rad) |
| 1053 | return 0; |
| 1054 | |
| 1055 | *intp = *p1 + (rad - plane_dist) * (*plane_norm); |
| 1056 | |
| 1057 | // Make sure the computed new position is not behind the wall. |
| 1058 | ASSERT((*intp - *plane_pnt) * *plane_norm >= -0.01); |
| 1059 | |
| 1060 | } else { |
| 1061 | // The intersection of the line and the plane is a simple linear combination |
| 1062 | *intp = *p0 + (proj_dist_point_plane / proj_dist_line) * line_vec; |
| 1063 | } |
| 1064 | |
| 1065 | // Make sure the computed new position is not colliding with the wall. |
| 1066 | // ASSERT((*intp - *plane_pnt) * *plane_norm >= rad); |
| 1067 | |
| 1068 | // Collision point is a rad. closer in the direction of the normal |
| 1069 | *colp = *intp + *plane_norm * -rad; |
| 1070 | |
| 1071 | return 1; |
| 1072 | } |
| 1073 | |
| 1074 | struct vec2d { |
| 1075 | float i, j; |
| 1076 | }; |
| 1077 | |
| 1078 | // given largest componant of normal, return i & j |
| 1079 | // if largest componant is negative, swap i & j |
| 1080 | static const int ij_table[3][2] = { |
| 1081 | {2, 1}, // pos x biggest |
| 1082 | {0, 2}, // pos y biggest |
| 1083 | {1, 0}, // pos z biggest |
| 1084 | }; |
| 1085 | |
| 1086 | // intersection types |
| 1087 | #define IT_NONE 0 // doesn't touch face at all |
| 1088 | #define IT_FACE 1 // touches face |
| 1089 | #define IT_EDGE 2 // touches edge of face |
| 1090 | #define IT_POINT 3 // touches vertex |
| 1091 | |
| 1092 | // see if a point in inside a face by projecting into 2d |
| 1093 | uint32_t check_point_to_face(vector *colp, vector *face_normal, int nv, vector **vertex_ptr_list) { |
| 1094 | vector_array *colp_array; // Axis-independant version of the collision point |
| 1095 | vector_array *norm; // Axis-independant version of the plane's normal |
| 1096 | vector t; // Temporary vector that holds the magnatude of the normal's x,y,z components (ABS) |
| 1097 | int biggest; // Index of the largest of the three components (0-x, 1-y, 2-z) Axis to ignore :) |
| 1098 | int i, j, edge; // Index for i-axis, Index for j-axis, and the current edge |
| 1099 | uint32_t edgemask; // Bit-field for which side we are outside of |
| 1100 | float check_i, check_j; // (i,j) checkpoint for 2d in/out test |
| 1101 | vector_array *v0, *v1; // Vertices of the current line segment in the 2d in/out check loop |
| 1102 | |
| 1103 | // Lets look at these vectors as arrays :) |
| 1104 | norm = (vector_array *)face_normal; |
| 1105 | colp_array = (vector_array *)colp; |
| 1106 | |
| 1107 | // now do 2d check to see if point is in side |
| 1108 | |
| 1109 | // Get x,y,z components of the normal and put them in array form (so we can pick any two for i,j) |
| 1110 | t.x = fabs(norm->xyz[0]); |
| 1111 | t.y = fabs(norm->xyz[1]); |
| 1112 | t.z = fabs(norm->xyz[2]); |
| 1113 | |
| 1114 | // Determine which axis will be normal to the plane the points are projected onto |
| 1115 | if (t.x > t.y) |
| 1116 | if (t.x > t.z) |
| 1117 | biggest = 0; |
| 1118 | else |
| 1119 | biggest = 2; |
| 1120 | else if (t.y > t.z) |
| 1121 | biggest = 1; |
| 1122 | else |
| 1123 | biggest = 2; |
| 1124 | |
| 1125 | // For a plane with a normal that is in the opposite direction of the axis, |
| 1126 | // we should circle the other direction -- i.e. always circle in clockwise direction with normal (left-handed) |
| 1127 | if (norm->xyz[biggest] > 0) { |
| 1128 | i = ij_table[biggest][0]; |
| 1129 | j = ij_table[biggest][1]; |
| 1130 | } else { |
| 1131 | i = ij_table[biggest][1]; |
| 1132 | j = ij_table[biggest][0]; |
| 1133 | } |
| 1134 | |
| 1135 | // now do the 2d problem in the i,j plane |
| 1136 | |
| 1137 | // Get the i,j check point |
| 1138 | check_i = colp_array->xyz[i]; |
| 1139 | check_j = colp_array->xyz[j]; |
| 1140 | |
| 1141 | // Do a simple 2d cross-product between each line segment and the start point to the check point |
| 1142 | // Go in a clockwise direction, if determinant is negative then point is outside of this multi- |
| 1143 | // side polygon. :) Only works for concave polygons. |
| 1144 | for (edge = edgemask = 0; edge < nv; edge++) { |
| 1145 | vec2d edgevec, checkvec; |
| 1146 | float d; |
| 1147 | |
| 1148 | // v0 = (vector_array *)&Vertices[vertex_list[facenum*3+edge]]; |
| 1149 | // v1 = (vector_array *)&Vertices[vertex_list[facenum*3+((edge+1)%nv)]]; |
| 1150 | v0 = (vector_array *)vertex_ptr_list[edge]; |
| 1151 | v1 = (vector_array *)vertex_ptr_list[(edge + 1) % nv]; |
| 1152 | |
| 1153 | edgevec.i = v1->xyz[i] - v0->xyz[i]; |
| 1154 | edgevec.j = v1->xyz[j] - v0->xyz[j]; |
| 1155 | |
| 1156 | checkvec.i = check_i - v0->xyz[i]; |
| 1157 | checkvec.j = check_j - v0->xyz[j]; |
| 1158 | |
| 1159 | d = checkvec.i * edgevec.j - checkvec.j * edgevec.i; |
| 1160 | |
| 1161 | if (d < 0) // we are outside of triangle |
| 1162 | edgemask |= (1 << edge); |
| 1163 | } |
| 1164 | |
| 1165 | return edgemask; |
| 1166 | } |
| 1167 | |
| 1168 | // decide it it's close enough to hit |
| 1169 | // determine if and where a vector intersects with a sphere |
| 1170 | // vector defined by p0,p1 |
| 1171 | // if there is an intersection this function returns 1, fills in intp, and col_dist else it returns 0 |
| 1172 | // NOTE: Caller should account for the radius of the vector (i.e. no rad. for the vector is passed |
| 1173 | // to this function -- the 2 radii are additive to it is trial and it saves 1 parameter |
| 1174 | int check_vector_to_sphere_1(vector *intp, float *col_dist, const vector *p0, const vector *p1, vector *sphere_pos, |
| 1175 | float sphere_rad, bool f_correcting, bool f_init_collisions) { |
| 1176 | vector line_vec; // Vector direction of line from p0 to p1 |
| 1177 | vector normalized_line_vec; // Normalized line vector |
| 1178 | float mag_line; // Length of the line |
| 1179 | |
| 1180 | vector point_to_center_vec; // Vector from p0 to the center of the sphere |
| 1181 | vector closest_point; // Location the sphere's centerpoint parallel projected onto the line |
| 1182 | float closest_point_dist; // Distance from p0 to the closest_point |
| 1183 | float closest_mag_to_center; // Distance from the clostest_point to the center of the sphere |
| 1184 | |
| 1185 | float shorten; // How much to subtract from the closest_point_dist to get the actual intersection |
| 1186 | // point |
| 1187 | |
| 1188 | ASSERT(sphere_rad > 0.0); |
| 1189 | |
| 1190 | // Get the vectors as usual |
| 1191 | line_vec = *p1 - *p0; |
| 1192 | point_to_center_vec = *sphere_pos - *p0; |
| 1193 | |
| 1194 | if (line_vec * point_to_center_vec <= 0.0f) |
| 1195 | return 0; |
| 1196 | |
| 1197 | // Get the magnitude and direction of the line vector |
| 1198 | normalized_line_vec = line_vec; |
| 1199 | mag_line = vm_NormalizeVector(&normalized_line_vec); |
| 1200 | |
| 1201 | // Compute the location of the point on the line that is perpendicular to the center of the sphere |
| 1202 | closest_point_dist = normalized_line_vec * point_to_center_vec; |
| 1203 | |
| 1204 | // We check for an initial hit, so if closest_point is negative distance, it was a miss (think about it) |
| 1205 | // Otherwise, make sure it is not any farther than would for a collision to happen |
| 1206 | if (closest_point_dist < 0.0 || closest_point_dist >= mag_line + sphere_rad) |
| 1207 | return 0; |
| 1208 | |
| 1209 | // Is the initial p0 position an intersection? If so, warn us and collide immediately. |
| 1210 | if (point_to_center_vec * point_to_center_vec < sphere_rad * sphere_rad) { |
| 1211 | if (f_correcting) { |
| 1212 | /* |
| 1213 | // chrishack |
| 1214 | mprintf(0, "FVI WARNING: Start point is inside of a checked sphere %f %f\n", |
| 1215 | point_to_center_vec * point_to_center_vec, |
| 1216 | sphere_rad * sphere_rad); |
| 1217 | */ |
| 1218 | // chrishack this movement intersection fix is a hack... How do we do correct cylinder/vector interestion? |
| 1219 | vector n_ptc = point_to_center_vec; |
| 1220 | vm_NormalizeVector(&n_ptc); |
| 1221 | |
| 1222 | *intp = |
| 1223 | *p0 - n_ptc * (sphere_rad - (float)sqrt(sphere_rad * sphere_rad - point_to_center_vec * point_to_center_vec)); |
| 1224 | |
| 1225 | *col_dist = 0.0; |
| 1226 | return 1; |
| 1227 | } else if (f_init_collisions) { |
| 1228 | *intp = *p0; |
| 1229 | *col_dist = 0.0; |
| 1230 | |
| 1231 | return 1; |
| 1232 | } else { |
| 1233 | // If not correcting, ignore initial point collisions, as they make no sense. |
| 1234 | return 0; |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | closest_point = *p0 + closest_point_dist * normalized_line_vec; |
| 1239 | closest_mag_to_center = vm_VectorDistance(&closest_point, sphere_pos); |
| 1240 | |
| 1241 | // We are not moving close enough to collide with the circle |
| 1242 | if (closest_mag_to_center >= sphere_rad) |
| 1243 | return 0; |
| 1244 | |
| 1245 | // Pathagorithm Theorom -- the radius is the hypothenus, the other two sides are the distance |
| 1246 | // from the point to the line, and the amount we should subtract from the line to account |
| 1247 | // for the sphere overlapping the line at the closest approach point |
| 1248 | shorten = sqrt(sphere_rad * sphere_rad - closest_mag_to_center * closest_mag_to_center); |
| 1249 | *col_dist = closest_point_dist - shorten; |
| 1250 | if (*col_dist > mag_line) |
| 1251 | return 0; |
| 1252 | |
| 1253 | // Actual collision distance |
| 1254 | *intp = *p0 + *col_dist * normalized_line_vec; |
| 1255 | |
| 1256 | // Actual collision point |
| 1257 | return 1; |
| 1258 | } |
| 1259 | |
| 1260 | bool IsPointInCylinder(vector *normal, vector *cylinder_pnt, vector *edir, float elen, const float rad, |
| 1261 | const vector *pnt, vector *mdir, bool *f_collide) { |
| 1262 | float plen = (*pnt - *cylinder_pnt) * *edir; |
| 1263 | |
| 1264 | if (plen < 0.0f || plen > elen) { |
| 1265 | return false; |
| 1266 | } |
| 1267 | |
| 1268 | vector newp = *cylinder_pnt + *edir * plen; |
| 1269 | *normal = *pnt - newp; |
| 1270 | |
| 1271 | if (vm_NormalizeVector(normal) >= rad) { |
| 1272 | return false; |
| 1273 | } |
| 1274 | |
| 1275 | if (*normal * *mdir >= 0.0f) |
| 1276 | *f_collide = false; |
| 1277 | else |
| 1278 | *f_collide = true; |
| 1279 | |
| 1280 | return true; |
| 1281 | } |
| 1282 | |
| 1283 | // check if a sphere intersects a face -- this can be optimized (only need 2d stuff after rotation) |
| 1284 | int check_vector_to_cylinder(vector *colp, vector *intp, float *col_dist, vector *wall_norm, const vector *p0, |
| 1285 | const vector *p1, float rad, vector *ep0, vector *ep1) { |
| 1286 | matrix edge_orient; |
| 1287 | vector po0, po1; |
| 1288 | vector edgevec = *ep1 - *ep0; |
| 1289 | vector mvec; |
| 1290 | vector closest_pnt; |
| 1291 | |
| 1292 | float edge_len; |
| 1293 | float dist; |
| 1294 | float vector_len; |
| 1295 | float dist_from_origin; |
| 1296 | float dist_to_intersection; |
| 1297 | int i; |
| 1298 | int valid_hit = 0; |
| 1299 | |
| 1300 | vector mvec3d; |
| 1301 | float vector_len3d; |
| 1302 | |
| 1303 | float t[4]; |
| 1304 | vector ivertex[4]; |
| 1305 | |
| 1306 | int valid_t[4]; |
| 1307 | float cole_dist[4]; |
| 1308 | vector inte[4]; |
| 1309 | |
| 1310 | mvec3d = *p1 - *p0; |
| 1311 | vector_len3d = vm_NormalizeVector(&mvec3d); |
| 1312 | |
| 1313 | edge_len = vm_NormalizeVector(&edgevec); |
| 1314 | |
| 1315 | vector init_normal; |
| 1316 | bool f_init_collide; |
| 1317 | |
| 1318 | if (!IsPointInCylinder(&init_normal, ep0, &edgevec, edge_len, rad, p0, &mvec3d, &f_init_collide)) { |
| 1319 | vm_VectorToMatrix(&edge_orient, &edgevec, nullptr, nullptr); |
| 1320 | |
| 1321 | po0 = (*p0 - *ep0) * edge_orient; |
| 1322 | po1 = (*p1 - *ep0) * edge_orient; |
| 1323 | |
| 1324 | po0.z = po1.z = 0.0; |
| 1325 | mvec = po1 - po0; |
| 1326 | vector_len = vm_NormalizeVector(&mvec); |
| 1327 | |
| 1328 | dist = -(mvec * po0); |
| 1329 | |
| 1330 | closest_pnt = po0 + dist * mvec; |
| 1331 | // ASSERT(!(closest_pnt.x == 0.0 && closest_pnt.y == 0.0 && closest_pnt.z == 0)); -- why does this matter? |
| 1332 | |
| 1333 | dist_from_origin = vm_GetMagnitude(&closest_pnt); |
| 1334 | if (dist_from_origin >= rad) |
| 1335 | return 0; |
| 1336 | |
| 1337 | dist_to_intersection = sqrt(rad * rad - dist_from_origin * dist_from_origin); |
| 1338 | |
| 1339 | t[0] = (dist + dist_to_intersection) / vector_len; // (0.0 to 1.0) is on line |
| 1340 | if (t[0] >= 0.0 && t[0] <= 1.0) { |
| 1341 | valid_t[0] = 1; |
| 1342 | } else { |
| 1343 | valid_t[0] = 0; |
| 1344 | } |
| 1345 | |
| 1346 | t[1] = (dist - dist_to_intersection) / vector_len; // (0.0 to 1.0) is on line |
| 1347 | if (t[1] >= 0.0 && t[1] <= 1.0) { |
| 1348 | valid_t[1] = 1; |
| 1349 | } else { |
| 1350 | valid_t[1] = 0; |
| 1351 | } |
| 1352 | |
| 1353 | for (i = 0; i < 2; i++) { |
| 1354 | float t_edge; |
| 1355 | |
| 1356 | if (valid_t[i]) { |
| 1357 | ivertex[i] = *p0 + mvec3d * (vector_len3d * t[i]); |
| 1358 | |
| 1359 | t_edge = ((ivertex[i] - *ep0) * edgevec) / edge_len; |
| 1360 | if (t_edge >= 0.0 && t_edge <= 1.0) { |
| 1361 | cole_dist[i] = vector_len3d * t[i]; |
| 1362 | inte[i] = *ep0 + ((ivertex[i] - *ep0) * edgevec) * edgevec; |
| 1363 | valid_hit = 1; |
| 1364 | /// mprintf(0, "%f,%f,%f to %f,%f, %f\n", XYZ(p0), XYZ(p1)); |
| 1365 | } else { |
| 1366 | valid_t[i] = 0; |
| 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | vector d_vec; |
| 1372 | // check end spheres |
| 1373 | if (check_vector_to_sphere_1(&ivertex[2], &cole_dist[2], p0, p1, ep0, rad, false, true)) { |
| 1374 | t[2] = cole_dist[2] / vector_len3d; |
| 1375 | valid_t[2] = 1; |
| 1376 | valid_hit = 1; |
| 1377 | // mprintf(0, "Sphere %f,%f,%f to %f,%f, %f\n", XYZ(p0), XYZ(p1)); |
| 1378 | d_vec = *ep1 - ivertex[2]; |
| 1379 | vm_NormalizeVector(&d_vec); |
| 1380 | inte[2] = ivertex[2] + rad * (d_vec); |
| 1381 | } else { |
| 1382 | valid_t[2] = 0; |
| 1383 | } |
| 1384 | if (check_vector_to_sphere_1(&ivertex[3], &cole_dist[3], p0, p1, ep1, rad, false, true)) { |
| 1385 | t[3] = cole_dist[3] / vector_len3d; |
| 1386 | valid_t[3] = 1; |
| 1387 | valid_hit = 1; |
| 1388 | d_vec = *ep1 - ivertex[3]; |
| 1389 | vm_NormalizeVector(&d_vec); |
| 1390 | inte[3] = ivertex[3] + rad * (d_vec); |
| 1391 | } else { |
| 1392 | valid_t[3] = 0; |
| 1393 | } |
| 1394 | |
| 1395 | if (valid_hit == 0) |
| 1396 | return 0; |
| 1397 | |
| 1398 | int best_hit_index = -1; |
| 1399 | for (i = 0; i < 4; i++) { |
| 1400 | if (valid_t[i]) { |
| 1401 | if (best_hit_index == -1 || cole_dist[i] < cole_dist[best_hit_index]) { |
| 1402 | best_hit_index = i; |
| 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | // if(best_hit_index < 2) |
| 1408 | |
| 1409 | *colp = inte[best_hit_index]; |
| 1410 | *intp = ivertex[best_hit_index]; |
| 1411 | *col_dist = cole_dist[best_hit_index]; |
| 1412 | |
| 1413 | *wall_norm = *intp - *colp; |
| 1414 | vm_NormalizeVector(wall_norm); |
| 1415 | |
| 1416 | // mprintf(0, "We hit at %f,%f,%f \nwith %f,%f,%f on face\n", XYZ(intp), XYZ(colp)); |
| 1417 | |
| 1418 | return 1; |
| 1419 | } else { |
| 1420 | if (f_init_collide) { |
| 1421 | *col_dist = 0.0f; |
| 1422 | *wall_norm = init_normal; |
| 1423 | *colp = *p0 - init_normal * rad; |
| 1424 | *intp = *p0; |
| 1425 | |
| 1426 | return 1; |
| 1427 | } else { |
| 1428 | return 0; |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | /* |
| 1434 | int check_vector_to_cylinder(vector *colp, vector *intp, float *col_dist, vector *wall_norm, vector *p0, vector *p1, |
| 1435 | float rad, vector *ep0, vector *ep1) |
| 1436 | { |
| 1437 | int i; |
| 1438 | float s; |
| 1439 | float t; |
| 1440 | float pmag; |
| 1441 | vector init_normal; |
| 1442 | bool f_hit = false; |
| 1443 | vector mdir = *p1 - *p0; |
| 1444 | float mlen = vm_NormalizeVector(&mdir); |
| 1445 | vector edir = *ep1 - *ep0; |
| 1446 | float elen = vm_NormalizeVector(&edir); |
| 1447 | bool f_init_collide; |
| 1448 | |
| 1449 | float cdist; // Closest dist |
| 1450 | |
| 1451 | vector perp = (mdir ^ edir); // Determines the normalized perp to both lines |
| 1452 | if(perp == Zero_vector) |
| 1453 | goto check_ends; // We are moving parallal to the cylinder (only end collisions are possible) |
| 1454 | pmag = vm_NormalizeVector(&perp); |
| 1455 | |
| 1456 | ASSERT(pmag != 0.0); |
| 1457 | |
| 1458 | vector brbc = *p0 - *ep0; |
| 1459 | cdist = fabs((brbc) * perp); // Closest distance |
| 1460 | |
| 1461 | if(cdist >= rad) // If the closest point is a more than a rad away, no hit |
| 1462 | return 0; |
| 1463 | |
| 1464 | // Check for an initial collision |
| 1465 | if(!IsPointInCylinder(&init_normal, ep0, &edir, elen, rad, p0, &mdir, &f_init_collide)) |
| 1466 | { |
| 1467 | t = (((brbc) ^ edir) * perp)/pmag; |
| 1468 | |
| 1469 | vector o = perp ^ edir; |
| 1470 | vm_NormalizeVector(&o); |
| 1471 | |
| 1472 | s = fabs(sqrt(rad * rad - pmag * pmag)/(mdir * o)); |
| 1473 | |
| 1474 | vector cyl_pnt[2]; |
| 1475 | vector cyl_norm[2]; |
| 1476 | |
| 1477 | // Determine the 2 potential hitpoint |
| 1478 | cyl_pnt[0] = *p0 + (t - s) * mdir; |
| 1479 | cyl_pnt[1] = *p0 + (t + s) * mdir; |
| 1480 | |
| 1481 | // Determine the 2 potential hit normals |
| 1482 | for(i = 0; i < 2; i++) |
| 1483 | { |
| 1484 | vector hb = cyl_pnt[i] - *ep0; |
| 1485 | cyl_norm[i] = (hb - (hb * edir) * edir)/rad; |
| 1486 | vm_NormalizeVector(&cyl_norm[i]); // Accounts for fp round off. - probably not necessary |
| 1487 | } |
| 1488 | |
| 1489 | bool f_valid[2]; |
| 1490 | f_valid[0] = f_valid[1] = true; |
| 1491 | |
| 1492 | // Determine if points are on the valid |
| 1493 | for(i = 0; i < 2; i++) |
| 1494 | { |
| 1495 | if(f_valid[i]) |
| 1496 | { |
| 1497 | if(cyl_norm[i] * mdir >= 0.0f) // Is it opposite the direction of motion |
| 1498 | { |
| 1499 | f_valid[i] = false; |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | if(f_valid[i]) // Is it on the cylinder |
| 1504 | { |
| 1505 | float proj = (cyl_pnt[i] - *ep0) * edir; |
| 1506 | if(proj < 0.0 || proj > elen) |
| 1507 | { |
| 1508 | f_valid[i] = false; |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | if(f_valid[i]) // Is it on the movement path |
| 1513 | { |
| 1514 | float proj = (cyl_pnt[i] - *p0) * mdir; |
| 1515 | if(proj < 0.0 || proj > mlen) |
| 1516 | { |
| 1517 | f_valid[i] = false; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | // A real collision :) Update all the collision properties |
| 1522 | if(f_valid[i]) |
| 1523 | { |
| 1524 | // Determine the collision distance |
| 1525 | float new_dist = vm_VectorDistance(p0, &cyl_pnt[i]); |
| 1526 | |
| 1527 | *col_dist = new_dist; |
| 1528 | *wall_norm = cyl_norm[i]; |
| 1529 | *intp = cyl_pnt[i]; |
| 1530 | *colp = cyl_pnt[i] - cyl_norm[i] * rad; |
| 1531 | |
| 1532 | f_hit = 1; |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | check_ends: |
| 1537 | |
| 1538 | vector end_pnt; |
| 1539 | |
| 1540 | if(f_hit) |
| 1541 | { |
| 1542 | end_pnt = *intp; |
| 1543 | } |
| 1544 | else |
| 1545 | { |
| 1546 | end_pnt = *p1; |
| 1547 | } |
| 1548 | |
| 1549 | // decide it it's close enough to hit |
| 1550 | // determine if and where a vector intersects with a sphere |
| 1551 | // vector defined by p0,p1 |
| 1552 | // if there is an intersection this function returns 1, fills in intp, and col_dist else it returns 0 |
| 1553 | // NOTE: Caller should account for the radius of the vector (i.e. no rad. for the vector is passed |
| 1554 | // to this function -- the 2 radii are additive to it is trial and it saves 1 parameter |
| 1555 | if(check_vector_to_sphere_1(intp, col_dist, p0, &end_pnt, ep0, rad, false, true)) |
| 1556 | { |
| 1557 | f_hit = 1; |
| 1558 | } |
| 1559 | |
| 1560 | if(f_hit) |
| 1561 | { |
| 1562 | end_pnt = *intp; |
| 1563 | } |
| 1564 | else |
| 1565 | { |
| 1566 | end_pnt = *p1; |
| 1567 | } |
| 1568 | |
| 1569 | if(check_vector_to_sphere_1(intp, col_dist, p0, &end_pnt, ep0, rad, false, true)) |
| 1570 | { |
| 1571 | f_hit = 1; |
| 1572 | } |
| 1573 | |
| 1574 | return f_hit; |
| 1575 | } |
| 1576 | else |
| 1577 | { |
| 1578 | if(f_init_collide) |
| 1579 | { |
| 1580 | *col_dist = 0.0f; |
| 1581 | *wall_norm = init_normal; |
| 1582 | *colp = *p0 - init_normal * rad; |
| 1583 | *intp = *p0; |
| 1584 | |
| 1585 | return 1; |
| 1586 | } |
| 1587 | else |
| 1588 | { |
| 1589 | return 0; |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | */ |
| 1594 | |
| 1595 | // check if a sphere intersects a face |
| 1596 | int check_sphere_to_face(vector *colp, vector *intp, float *col_dist, vector *wall_norm, const vector *p0, |
| 1597 | const vector *p1, vector *face_normal, int nv, float rad, vector **vertex_ptr_list) { |
| 1598 | uint32_t edgemask; |
| 1599 | |
| 1600 | ASSERT(nv > 0 && nv <= 32); // otherwise, we overflow the edgemask -- if we hit this we need to make edgemask a long |
| 1601 | // long and adjust the other functions accordingly |
| 1602 | |
| 1603 | // now do 2d check to see if point is inside the face (if so, we are done) |
| 1604 | edgemask = check_point_to_face(colp, face_normal, nv, vertex_ptr_list); |
| 1605 | |
| 1606 | // If we are inside edgemask is 0, we hit the face. |
| 1607 | if (edgemask == 0) { |
| 1608 | // mprintf(0, "CSTF Hit Face\n"); |
| 1609 | *col_dist = vm_VectorDistance(p0, intp); |
| 1610 | *wall_norm = *face_normal; |
| 1611 | return IT_FACE; |
| 1612 | } else { |
| 1613 | // Although the plane collision point is not in the face, we might hit an edge. |
| 1614 | // If the checkpoint collides with the edge of a face, it could |
| 1615 | // go a little farther before hitting anything |
| 1616 | |
| 1617 | vector *v0, *v1; |
| 1618 | int edgenum; |
| 1619 | |
| 1620 | // If we have no radius we could only hit the face and not an edge or point |
| 1621 | if (rad == 0.0) |
| 1622 | return IT_NONE; |
| 1623 | |
| 1624 | int f_hit = 0; |
| 1625 | vector c_end = *p1; |
| 1626 | |
| 1627 | // get verts for edge we're behind |
| 1628 | for (edgenum = 0; edgenum < nv; edgenum++) { |
| 1629 | if (edgemask & 1) { |
| 1630 | v0 = vertex_ptr_list[edgenum]; |
| 1631 | v1 = vertex_ptr_list[(edgenum + 1) % nv]; |
| 1632 | |
| 1633 | if (check_vector_to_cylinder(colp, intp, col_dist, wall_norm, p0, &c_end, rad, v0, v1)) { |
| 1634 | c_end = *intp; |
| 1635 | f_hit = 1; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | edgemask = edgemask >> 1; |
| 1640 | } |
| 1641 | |
| 1642 | return f_hit; |
| 1643 | } /* checkvec = *p0 - *v0; |
| 1644 | edgevec = *v1 - *v0; |
| 1645 | edgelen = vm_NormalizeVector(&edgevec); |
| 1646 | |
| 1647 | cur_dist = edgevec * checkvec; |
| 1648 | |
| 1649 | closest_point = *v0 + cur_dist * edgevec; |
| 1650 | |
| 1651 | // See if the sphere intersects the edge of the face |
| 1652 | if(!check_vector_to_sphere_1(intp, col_dist, p0, p1, &closest_point, rad, 1)) return IT_NONE; |
| 1653 | checkvec = *intp - *v0; |
| 1654 | cur_dist = edgevec * checkvec; |
| 1655 | if(cur_dist >= 0 && cur_dist <= edgelen) { |
| 1656 | |
| 1657 | // mprintf(0, "Hit edge\n"); |
| 1658 | |
| 1659 | *wall_norm = *intp - closest_point; |
| 1660 | vm_NormalizeVector(wall_norm); |
| 1661 | *colp = *intp + (-rad) * *wall_norm; |
| 1662 | |
| 1663 | return IT_EDGE; |
| 1664 | } |
| 1665 | |
| 1666 | // Look to see if there is a point collision |
| 1667 | else if(cur_dist < 0) closest_point = *v0; |
| 1668 | else closest_point = *v1; |
| 1669 | if(!check_vector_to_sphere_1(intp, col_dist, p0, p1, &closest_point, rad, 1)) return IT_NONE; |
| 1670 | |
| 1671 | // mprintf(0, "Hit point\n"); |
| 1672 | |
| 1673 | *wall_norm = *intp - closest_point; |
| 1674 | vm_NormalizeVector(wall_norm); |
| 1675 | *colp = *intp + (-rad) * *wall_norm; |
| 1676 | |
| 1677 | return IT_POINT; |
| 1678 | }*/ |
| 1679 | } |
| 1680 | |
| 1681 | // returns true if line intersects with face. fills in newp with intersection |
| 1682 | // point on plane, whether or not line intersects side |
| 1683 | // facenum determines which of four possible faces we have |
| 1684 | // note: the seg parm is temporary, until the face itself has a point field |
| 1685 | int check_line_to_face(vector *newp, vector *colp, float *col_dist, vector *wall_norm, const vector *p0, |
| 1686 | const vector *p1, vector *face_normal, vector **vertex_ptr_list, const int nv, const float rad) { |
| 1687 | int f_pli; // Flag for if a plane that defines the face intersects with the line |
| 1688 | int vertnum = 0; |
| 1689 | vector *test = vertex_ptr_list[0]; |
| 1690 | int i; |
| 1691 | |
| 1692 | ASSERT(newp != nullptr && p0 != 0 && p1 != nullptr && rad >= 0.0); |
| 1693 | |
| 1694 | // This is so we always use the same vertex |
| 1695 | for (i = 1; i < nv; i++) { |
| 1696 | if (test > vertex_ptr_list[i]) { |
| 1697 | test = vertex_ptr_list[i]; |
| 1698 | vertnum = i; |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | // Determine the intersection point between the plane(of the face) and the line |
| 1703 | // This point is the center of the circle (not the edge) |
| 1704 | f_pli = find_plane_line_intersection(newp, colp, vertex_ptr_list[vertnum], face_normal, p0, p1, rad); |
| 1705 | |
| 1706 | if (!f_pli) |
| 1707 | return IT_NONE; |
| 1708 | |
| 1709 | // This is the edge point where the actual intersection with the plane is occuring |
| 1710 | // *colp = *newp; |
| 1711 | // if (rad != 0.0) |
| 1712 | // *colp += *face_normal*(-rad); |
| 1713 | |
| 1714 | // mprintf(0, "Check line to face: segment %d, side %d\n", seg - Segments, side); |
| 1715 | |
| 1716 | // We know that we hit the plane, but did we hit the face (in bounds) |
| 1717 | return check_sphere_to_face(colp, newp, col_dist, wall_norm, p0, p1, face_normal, nv, rad, vertex_ptr_list); |
| 1718 | } |
| 1719 | |
| 1720 | // chrishack -- check this later |
| 1721 | // computes the parameters of closest approach of two lines |
| 1722 | // fill in two parameters, t0 & t1. returns 0 if lines are parallel, else 1 |
| 1723 | bool check_line_to_line(float *t1, float *t2, vector *p1, vector *v1, vector *p2, vector *v2) { |
| 1724 | matrix det; |
| 1725 | float d, cross_mag2; // mag squared cross product |
| 1726 | |
| 1727 | det.rvec = *p2 - *p1; |
| 1728 | det.fvec = *v1 ^ *v2; // (crossproduct) |
| 1729 | cross_mag2 = det.fvec * det.fvec; |
| 1730 | |
| 1731 | if (cross_mag2 == 0.0) |
| 1732 | return false; // lines are parallel |
| 1733 | |
| 1734 | det.uvec = *v2; |
| 1735 | d = calc_det_value(&det); |
| 1736 | *t1 = d / cross_mag2; |
| 1737 | |
| 1738 | det.uvec = *v1; |
| 1739 | d = calc_det_value(&det); |
| 1740 | *t2 = d / cross_mag2; |
| 1741 | |
| 1742 | return true; // found point |
| 1743 | } |
| 1744 | |
| 1745 | // determine if a vector intersects with an object |
| 1746 | // if no intersects, returns 0, else fills in intp and returns dist |
| 1747 | int check_vector_to_object(vector *intp, float *col_dist, vector *p0, vector *p1, float rad, object *still_obj, |
| 1748 | object *fvi_obj) { |
| 1749 | float still_size; |
| 1750 | vector still_pos = still_obj->pos; |
| 1751 | float total_size; |
| 1752 | |
| 1753 | int fvi_objnum = fvi_query_ptr->thisobjnum; |
| 1754 | |
| 1755 | if ((still_obj->flags & OF_POLYGON_OBJECT) && still_obj->type != OBJ_POWERUP && still_obj->type != OBJ_WEAPON && |
| 1756 | still_obj->type != OBJ_DEBRIS && still_obj->type != OBJ_ROOM && still_obj->type != OBJ_PLAYER) { |
| 1757 | still_size = Poly_models[still_obj->rtype.pobj_info.model_num].anim_size; |
| 1758 | still_pos += still_obj->anim_sphere_offset; |
| 1759 | } else { |
| 1760 | still_size = still_obj->size; |
| 1761 | } |
| 1762 | |
| 1763 | // chrishack -- why? |
| 1764 | // if (obj->type == OBJ_ROBOT && Robot_info[obj->id].attack_type) |
| 1765 | // size = (size*3)/4; |
| 1766 | // if obj is player, and bumping into other player or a weapon of another coop player, reduce radius |
| 1767 | // if (obj->type == OBJ_PLAYER && |
| 1768 | // ((otherobj->type == OBJ_PLAYER) || |
| 1769 | // ((Game_mode&GM_MULTI_COOP) && otherobj->type == OBJ_WEAPON && |
| 1770 | // otherobj->ctype.laser_info.parent_type |
| 1771 | //== OBJ_PLAYER))) size = size/2; |
| 1772 | |
| 1773 | // This accounts for relative position vs. relative velocity |
| 1774 | if (fvi_objnum != -1 && still_obj->movement_type == MT_PHYSICS && Objects[fvi_objnum].movement_type == MT_PHYSICS) { |
| 1775 | if (still_obj->type != OBJ_POWERUP && Objects[fvi_objnum].type != OBJ_POWERUP) { |
| 1776 | if ((still_pos - Objects[fvi_objnum].pos) * |
| 1777 | (still_obj->mtype.phys_info.velocity - Objects[fvi_objnum].mtype.phys_info.velocity) >= |
| 1778 | 0) { |
| 1779 | #ifndef NED_PHYSICS |
| 1780 | #ifdef _DEBUG |
| 1781 | if (Physics_player_verbose) { |
| 1782 | if (Player_object == &Objects[fvi_objnum]) { |
| 1783 | mprintf(0, "FVI: Earily exit on %d\n", OBJNUM(still_obj)); |
| 1784 | } |
| 1785 | } |
| 1786 | #endif |
| 1787 | #endif |
| 1788 | |
| 1789 | return 0; |
| 1790 | } |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | total_size = still_size + rad; |
| 1795 | |
| 1796 | if (total_size <= 0.0f) { |
| 1797 | #ifdef _DEBUG |
| 1798 | if (fvi_objnum >= 0) |
| 1799 | mprintf(0, "Get Chris: type %d and type %d are zero radius\n", still_obj->type, Objects[fvi_objnum].type); |
| 1800 | else |
| 1801 | mprintf(0, "Get Chris: A non-object tried to hit a zero radii object of type %d\n", still_obj->type); |
| 1802 | #endif |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
| 1806 | // Account for the radius of the vector. |
| 1807 | return check_vector_to_sphere_1(intp, col_dist, p0, p1, &still_pos, total_size, false, true); |
| 1808 | } |
| 1809 | |
| 1810 | inline void compute_movement_AABB(void) { |
| 1811 | const vector delta_movement = fvi_hit_data_ptr->hit_pnt - *fvi_query_ptr->p0; |
| 1812 | |
| 1813 | fvi_min_xyz = fvi_max_xyz = *fvi_query_ptr->p0; |
| 1814 | |
| 1815 | if (delta_movement.x > 0.0f) |
| 1816 | fvi_max_xyz.x += delta_movement.x; |
| 1817 | else |
| 1818 | fvi_min_xyz.x += delta_movement.x; |
| 1819 | |
| 1820 | if (delta_movement.y > 0.0f) |
| 1821 | fvi_max_xyz.y += delta_movement.y; |
| 1822 | else |
| 1823 | fvi_min_xyz.y += delta_movement.y; |
| 1824 | |
| 1825 | if (delta_movement.z > 0.0f) |
| 1826 | fvi_max_xyz.z += delta_movement.z; |
| 1827 | else |
| 1828 | fvi_min_xyz.z += delta_movement.z; |
| 1829 | |
| 1830 | fvi_wall_min_xyz = fvi_min_xyz; |
| 1831 | fvi_wall_max_xyz = fvi_max_xyz; |
| 1832 | |
| 1833 | if (!fvi_zero_rad) { |
| 1834 | if (fvi_query_ptr->thisobjnum < 0) { |
| 1835 | vector offset_vec; |
| 1836 | |
| 1837 | offset_vec.x = fvi_query_ptr->rad; |
| 1838 | offset_vec.y = fvi_query_ptr->rad; |
| 1839 | offset_vec.z = fvi_query_ptr->rad; |
| 1840 | |
| 1841 | fvi_min_xyz -= offset_vec; |
| 1842 | fvi_max_xyz += offset_vec; |
| 1843 | |
| 1844 | fvi_wall_min_xyz = fvi_min_xyz; |
| 1845 | fvi_wall_max_xyz = fvi_max_xyz; |
| 1846 | } else { |
| 1847 | vector max_offset = Objects[fvi_query_ptr->thisobjnum].max_xyz - Objects[fvi_query_ptr->thisobjnum].pos; |
| 1848 | vector min_offset = Objects[fvi_query_ptr->thisobjnum].min_xyz - Objects[fvi_query_ptr->thisobjnum].pos; |
| 1849 | |
| 1850 | fvi_max_xyz += max_offset; |
| 1851 | fvi_min_xyz += min_offset; |
| 1852 | |
| 1853 | // if(!(Objects[fvi_query_ptr->thisobjnum].mtype.phys_info.flags & PF_POINT_COLLIDE_WALLS)) |
| 1854 | { |
| 1855 | fvi_wall_min_xyz = fvi_min_xyz; |
| 1856 | fvi_wall_max_xyz = fvi_max_xyz; |
| 1857 | } |
| 1858 | } |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | inline bool object_object_AABB(object *obj1, object *obj2) { |
| 1863 | bool overlap = true; |
| 1864 | |
| 1865 | if (obj1->max_xyz.x < obj2->min_xyz.x || obj2->max_xyz.x < obj1->min_xyz.x || obj1->max_xyz.z < obj2->min_xyz.z || |
| 1866 | obj2->max_xyz.z < obj1->min_xyz.z || obj1->max_xyz.y < obj2->min_xyz.y || obj2->max_xyz.y < obj1->min_xyz.y) |
| 1867 | overlap = false; |
| 1868 | |
| 1869 | return overlap; |
| 1870 | } |
| 1871 | |
| 1872 | inline bool object_movement_AABB(object *obj) { |
| 1873 | bool overlap = true; |
| 1874 | |
| 1875 | if (obj->max_xyz.x < fvi_min_xyz.x || fvi_max_xyz.x < obj->min_xyz.x || obj->max_xyz.z < fvi_min_xyz.z || |
| 1876 | fvi_max_xyz.z < obj->min_xyz.z || obj->max_xyz.y < fvi_min_xyz.y || fvi_max_xyz.y < obj->min_xyz.y) |
| 1877 | overlap = false; |
| 1878 | |
| 1879 | return overlap; |
| 1880 | } |
| 1881 | |
| 1882 | inline bool object_room_AABB(object *obj, face *room_face) { |
| 1883 | bool overlap = true; |
| 1884 | |
| 1885 | if (obj->max_xyz.y < room_face->min_xyz.y || room_face->max_xyz.y < obj->min_xyz.y || |
| 1886 | obj->max_xyz.x < room_face->min_xyz.x || room_face->max_xyz.x < obj->min_xyz.x || |
| 1887 | obj->max_xyz.z < room_face->min_xyz.z || room_face->max_xyz.z < obj->min_xyz.z) |
| 1888 | overlap = false; |
| 1889 | |
| 1890 | return overlap; |
| 1891 | } |
| 1892 | |
| 1893 | inline bool room_movement_AABB(face *room_face) { |
| 1894 | bool overlap = true; |
| 1895 | |
| 1896 | if (fvi_wall_max_xyz.y < room_face->min_xyz.y || room_face->max_xyz.y < fvi_wall_min_xyz.y || |
| 1897 | fvi_wall_max_xyz.x < room_face->min_xyz.x || room_face->max_xyz.x < fvi_wall_min_xyz.x || |
| 1898 | fvi_wall_max_xyz.z < room_face->min_xyz.z || room_face->max_xyz.z < fvi_wall_min_xyz.z) |
| 1899 | overlap = false; |
| 1900 | |
| 1901 | return overlap; |
| 1902 | } |
| 1903 | |
| 1904 | inline bool room_manual_AABB(const face *room_face, const vector *min_xyz, const vector *max_xyz) { |
| 1905 | bool overlap = true; |
| 1906 | |
| 1907 | if (max_xyz->y < room_face->min_xyz.y || room_face->max_xyz.y < min_xyz->y || max_xyz->x < room_face->min_xyz.x || |
| 1908 | room_face->max_xyz.x < min_xyz->x || max_xyz->z < room_face->min_xyz.z || room_face->max_xyz.z < min_xyz->z) |
| 1909 | overlap = false; |
| 1910 | |
| 1911 | return overlap; |
| 1912 | } |
| 1913 | |
| 1914 | #define MAX_QUICK_ROOMS 20 |
| 1915 | |
| 1916 | // Returns the number of faces that are approximately within the specified radius |
| 1917 | int fvi_QuickDistFaceList(int init_room_index, vector *pos, float rad, fvi_face_room_list *quick_fr_list, |
| 1918 | int max_elements) { |
| 1919 | int num_faces = 0; |
| 1920 | room *cur_room; |
| 1921 | vector min_xyz, max_xyz; |
| 1922 | int next_rooms[MAX_QUICK_ROOMS]; |
| 1923 | int highest_next_room_index; |
| 1924 | int cur_next_room_index; |
| 1925 | int i; |
| 1926 | |
| 1927 | // ASSERT(quick_fr_list != NULL); |
| 1928 | ASSERT(pos != nullptr); |
| 1929 | ASSERT(init_room_index >= 0 && init_room_index <= Highest_room_index && Rooms[init_room_index].used != 0); |
| 1930 | ASSERT(rad >= 0.0f); |
| 1931 | |
| 1932 | // Quick volume |
| 1933 | min_xyz = max_xyz = *pos; |
| 1934 | |
| 1935 | min_xyz.x -= rad; |
| 1936 | min_xyz.y -= rad; |
| 1937 | min_xyz.z -= rad; |
| 1938 | max_xyz.x += rad; |
| 1939 | max_xyz.y += rad; |
| 1940 | max_xyz.z += rad; |
| 1941 | |
| 1942 | // Initially this is the only room in the list |
| 1943 | next_rooms[0] = init_room_index; |
| 1944 | highest_next_room_index = 0; |
| 1945 | cur_next_room_index = 0; |
| 1946 | |
| 1947 | // Use standard fvi list_array / bool list |
| 1948 | fvi_visit_list[init_room_index >> 3] |= 0x01 << (init_room_index % 8); |
| 1949 | fvi_rooms_visited[0] = init_room_index; |
| 1950 | fvi_num_rooms_visited = 1; |
| 1951 | |
| 1952 | while (num_faces < max_elements && cur_next_room_index <= highest_next_room_index) { |
| 1953 | cur_room = &Rooms[next_rooms[cur_next_room_index]]; |
| 1954 | |
| 1955 | // sort shit |
| 1956 | uint8_t msector = 0; |
| 1957 | |
| 1958 | if (min_xyz.x <= cur_room->bbf_min_xyz.x) { |
| 1959 | msector |= 0x01; |
| 1960 | } |
| 1961 | if (min_xyz.y <= cur_room->bbf_min_xyz.y) { |
| 1962 | msector |= 0x02; |
| 1963 | } |
| 1964 | if (min_xyz.z <= cur_room->bbf_min_xyz.z) { |
| 1965 | msector |= 0x04; |
| 1966 | } |
| 1967 | if (max_xyz.x >= cur_room->bbf_max_xyz.x) { |
| 1968 | msector |= 0x08; |
| 1969 | } |
| 1970 | if (max_xyz.y >= cur_room->bbf_max_xyz.y) { |
| 1971 | msector |= 0x10; |
| 1972 | } |
| 1973 | if (max_xyz.z >= cur_room->bbf_max_xyz.z) { |
| 1974 | msector |= 0x20; |
| 1975 | } |
| 1976 | |
| 1977 | const int16_t num_bbf_regions = cur_room->num_bbf_regions; |
| 1978 | int16_t *num_faces_ptr = cur_room->num_bbf; |
| 1979 | uint8_t *bbf_val = cur_room->bbf_list_sector; |
| 1980 | vector *region_min = cur_room->bbf_list_min_xyz; |
| 1981 | vector *region_max = cur_room->bbf_list_max_xyz; |
| 1982 | int16_t **bbf_list_ptr = cur_room->bbf_list; |
| 1983 | |
| 1984 | // Do the actual wall collsion stuff here! |
| 1985 | for (int test1 = 0; test1 < num_bbf_regions; test1++) { |
| 1986 | if (((*bbf_val) & msector) == (*bbf_val)) { |
| 1987 | if (region_min->x > max_xyz.x || region_min->y > max_xyz.y || region_min->z > max_xyz.z || |
| 1988 | region_max->x < min_xyz.x || region_max->y < min_xyz.y || region_max->z < min_xyz.z) |
| 1989 | goto skip_region; |
| 1990 | |
| 1991 | int16_t *cur_face_index_ptr = *bbf_list_ptr; |
| 1992 | |
| 1993 | for (int sort_list_cur = 0; sort_list_cur < (*num_faces_ptr); sort_list_cur++) { |
| 1994 | i = *cur_face_index_ptr; |
| 1995 | cur_face_index_ptr++; |
| 1996 | |
| 1997 | int portal_num; |
| 1998 | int connect_room; |
| 1999 | |
| 2000 | if (!room_manual_AABB(&cur_room->faces[i], &min_xyz, &max_xyz)) |
| 2001 | continue; |
| 2002 | |
| 2003 | if (quick_fr_list != nullptr) { |
| 2004 | if (num_faces < max_elements) { |
| 2005 | quick_fr_list[num_faces].face_index = i; |
| 2006 | quick_fr_list[num_faces].room_index = ROOMNUM(cur_room); |
| 2007 | num_faces++; |
| 2008 | } else |
| 2009 | break; |
| 2010 | } else |
| 2011 | num_faces++; |
| 2012 | |
| 2013 | cur_room->faces[i].flags |= FF_TOUCHED; |
| 2014 | |
| 2015 | portal_num = cur_room->faces[i].portal_num; |
| 2016 | if (portal_num >= 0) { |
| 2017 | connect_room = cur_room->portals[portal_num].croom; |
| 2018 | |
| 2019 | // If the conect_room is not a terrain cell and we still have a slot in the next room list... |
| 2020 | if (connect_room >= 0 && highest_next_room_index + 1 < MAX_QUICK_ROOMS) { |
| 2021 | ASSERT(Rooms[connect_room].used); |
| 2022 | |
| 2023 | if ((fvi_visit_list[connect_room >> 3] & (0x01 << ((connect_room) % 8))) == 0) { |
| 2024 | fvi_visit_list[connect_room >> 3] |= 0x01 << (connect_room % 8); |
| 2025 | fvi_rooms_visited[fvi_num_rooms_visited++] = connect_room; |
| 2026 | |
| 2027 | next_rooms[++highest_next_room_index] = connect_room; |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | } |
| 2032 | } |
| 2033 | skip_region:; |
| 2034 | num_faces_ptr++; |
| 2035 | bbf_val++; |
| 2036 | region_max++; |
| 2037 | region_min++; |
| 2038 | bbf_list_ptr++; |
| 2039 | } |
| 2040 | |
| 2041 | cur_next_room_index++; |
| 2042 | } |
| 2043 | |
| 2044 | // Cleans up the boolean room visit list |
| 2045 | for (i = 0; i < fvi_num_rooms_visited; i++) { |
| 2046 | fvi_visit_list[fvi_rooms_visited[i] >> 3] = 0; |
| 2047 | } |
| 2048 | |
| 2049 | return num_faces; |
| 2050 | } |
| 2051 | |
| 2052 | // Returns the number of faces that are approximately within the specified radius |
| 2053 | int fvi_QuickDistCellList(int init_cell_index, vector *pos, float rad, int *quick_cell_list, int max_elements) { |
| 2054 | int num_cells = 0; |
| 2055 | int next_y_delta; |
| 2056 | int xstart, xend, ystart, yend; |
| 2057 | int check_x, check_y; |
| 2058 | int xcounter, ycounter; |
| 2059 | int cur_node; |
| 2060 | |
| 2061 | ASSERT(quick_cell_list != nullptr); |
| 2062 | ASSERT(pos != nullptr); |
| 2063 | ASSERT(init_cell_index >= 0 && init_cell_index < TERRAIN_WIDTH * TERRAIN_DEPTH); |
| 2064 | ASSERT(rad >= 0.0f); |
| 2065 | |
| 2066 | cur_node = init_cell_index; |
| 2067 | |
| 2068 | // Check worst-case collisions. This includes all nodes within a radius edge of the current node |
| 2069 | check_x = rad / TERRAIN_SIZE + 1; |
| 2070 | check_y = rad / TERRAIN_SIZE + 1; |
| 2071 | |
| 2072 | xstart = cur_node % TERRAIN_WIDTH - check_x; |
| 2073 | xend = cur_node % TERRAIN_WIDTH + check_x; |
| 2074 | ystart = cur_node / TERRAIN_WIDTH - check_y; |
| 2075 | yend = cur_node / TERRAIN_WIDTH + check_y; |
| 2076 | |
| 2077 | if (xstart < 0) |
| 2078 | xstart = 0; |
| 2079 | if (xend >= TERRAIN_WIDTH) |
| 2080 | xend = TERRAIN_WIDTH - 1; |
| 2081 | if (ystart < 0) |
| 2082 | ystart = 0; |
| 2083 | if (yend >= TERRAIN_DEPTH) |
| 2084 | yend = TERRAIN_DEPTH - 1; |
| 2085 | |
| 2086 | // This should be a faster interative why to do a square with center at original position |
| 2087 | cur_node = TERRAIN_WIDTH * ystart + xstart; |
| 2088 | next_y_delta = TERRAIN_WIDTH - (xend - xstart) - 1; |
| 2089 | |
| 2090 | for (ycounter = ystart; ycounter <= yend; ycounter++) { |
| 2091 | for (xcounter = xstart; xcounter <= xend; xcounter++) { |
| 2092 | if ((Terrain_seg[cur_node].y >= pos->y - rad) || (Terrain_seg[cur_node + TERRAIN_WIDTH + 1].y >= pos->y - rad) || |
| 2093 | (Terrain_seg[cur_node + 1].y >= pos->y - rad) || (Terrain_seg[cur_node + TERRAIN_WIDTH].y >= pos->y - rad)) { |
| 2094 | quick_cell_list[num_cells++] = cur_node; |
| 2095 | if (num_cells >= max_elements) |
| 2096 | break; |
| 2097 | } |
| 2098 | cur_node += 1; |
| 2099 | } |
| 2100 | if (num_cells >= max_elements) |
| 2101 | break; |
| 2102 | cur_node += next_y_delta; |
| 2103 | } |
| 2104 | |
| 2105 | return num_cells; |
| 2106 | } |
| 2107 | |
| 2108 | int fvi_QuickDistObjectList(vector *pos, int init_room_index, float rad, int16_t *object_index_list, int max_elements, |
| 2109 | bool f_lightmap_only, bool f_only_players_and_ais, bool f_include_non_collide_objects, |
| 2110 | bool f_stop_at_closed_doors) { |
| 2111 | int num_objects = 0; |
| 2112 | int x; //, y; |
| 2113 | vector delta; |
| 2114 | |
| 2115 | // Quick volume |
| 2116 | delta.x = delta.y = delta.z = rad; |
| 2117 | fvi_min_xyz = fvi_max_xyz = *pos; |
| 2118 | |
| 2119 | fvi_min_xyz -= delta; |
| 2120 | fvi_max_xyz += delta; |
| 2121 | |
| 2122 | fvi_wall_min_xyz = fvi_min_xyz; |
| 2123 | fvi_wall_max_xyz = fvi_max_xyz; |
| 2124 | |
| 2125 | if (ROOMNUM_OUTSIDE(init_room_index)) { |
| 2126 | int num_cells = 0; |
| 2127 | int next_y_delta; |
| 2128 | int xstart, xend, ystart, yend; |
| 2129 | int check_x, check_y; |
| 2130 | int xcounter, ycounter; |
| 2131 | int cur_node; |
| 2132 | |
| 2133 | cur_node = CELLNUM(init_room_index); |
| 2134 | |
| 2135 | // Check worst-case collisions. This includes all nodes within a radius edge of the current node |
| 2136 | check_x = rad / TERRAIN_SIZE + 1; |
| 2137 | check_y = rad / TERRAIN_SIZE + 1; |
| 2138 | |
| 2139 | xstart = cur_node % TERRAIN_WIDTH - check_x; |
| 2140 | xend = cur_node % TERRAIN_WIDTH + check_x; |
| 2141 | ystart = cur_node / TERRAIN_WIDTH - check_y; |
| 2142 | yend = cur_node / TERRAIN_WIDTH + check_y; |
| 2143 | |
| 2144 | if (xstart < 0) |
| 2145 | xstart = 0; |
| 2146 | if (xend >= TERRAIN_WIDTH) |
| 2147 | xend = TERRAIN_WIDTH - 1; |
| 2148 | if (ystart < 0) |
| 2149 | ystart = 0; |
| 2150 | if (yend >= TERRAIN_DEPTH) |
| 2151 | yend = TERRAIN_DEPTH - 1; |
| 2152 | |
| 2153 | // This should be a faster interative why to do a square with center at original position |
| 2154 | cur_node = TERRAIN_WIDTH * ystart + xstart; |
| 2155 | next_y_delta = TERRAIN_WIDTH - (xend - xstart) - 1; |
| 2156 | |
| 2157 | for (ycounter = ystart; ycounter <= yend; ycounter++) { |
| 2158 | for (xcounter = xstart; xcounter <= xend; xcounter++) { |
| 2159 | // Do object stuff |
| 2160 | int cur_obj_index = Terrain_seg[cur_node].objects; |
| 2161 | |
| 2162 | while (cur_obj_index > -1) { |
| 2163 | if (num_objects >= max_elements) |
| 2164 | break; |
| 2165 | |
| 2166 | if ((f_include_non_collide_objects) || CollisionRayResult[Objects[cur_obj_index].type] != RESULT_NOTHING) { |
| 2167 | if (!f_only_players_and_ais || Objects[cur_obj_index].type == OBJ_PLAYER || |
| 2168 | Objects[cur_obj_index].ai_info) { |
| 2169 | if (!(f_lightmap_only && (Objects[cur_obj_index].lighting_render_type != LRT_LIGHTMAPS) && |
| 2170 | Objects[cur_obj_index].type != OBJ_ROOM)) { |
| 2171 | if (object_movement_AABB(&Objects[cur_obj_index]) && !(Objects[cur_obj_index].flags & OF_BIG_OBJECT)) { |
| 2172 | object_index_list[num_objects++] = cur_obj_index; |
| 2173 | ASSERT(num_objects < 0 || num_objects <= max_elements); |
| 2174 | } |
| 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | cur_obj_index = Objects[cur_obj_index].next; |
| 2180 | } |
| 2181 | |
| 2182 | if (num_objects >= max_elements) |
| 2183 | break; |
| 2184 | cur_node += 1; |
| 2185 | } |
| 2186 | if (num_objects >= max_elements) |
| 2187 | break; |
| 2188 | cur_node += next_y_delta; |
| 2189 | } |
| 2190 | |
| 2191 | // Do big object stuff |
| 2192 | for (x = 0; x < Num_big_objects; x++) { |
| 2193 | if (num_objects >= max_elements) |
| 2194 | break; |
| 2195 | |
| 2196 | if ((f_include_non_collide_objects) || CollisionRayResult[Objects[x].type] != RESULT_NOTHING) { |
| 2197 | if (!f_only_players_and_ais || Objects[x].type == OBJ_PLAYER || Objects[x].ai_info) { |
| 2198 | if (!(f_lightmap_only && (Objects[BigObjectList[x]].lighting_render_type != LRT_LIGHTMAPS) && |
| 2199 | Objects[BigObjectList[x]].type != OBJ_ROOM)) { |
| 2200 | if (object_movement_AABB(&Objects[BigObjectList[x]])) { |
| 2201 | object_index_list[num_objects++] = BigObjectList[x]; |
| 2202 | ASSERT(num_objects < 0 || num_objects <= max_elements); |
| 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | } |
| 2208 | } else { |
| 2209 | room *cur_room; |
| 2210 | int next_rooms[MAX_QUICK_ROOMS]; |
| 2211 | int highest_next_room_index; |
| 2212 | int cur_next_room_index; |
| 2213 | int i; |
| 2214 | |
| 2215 | ASSERT(pos != nullptr); |
| 2216 | ASSERT(init_room_index >= 0 && init_room_index <= Highest_room_index && Rooms[init_room_index].used != 0); |
| 2217 | ASSERT(rad >= 0.0f); |
| 2218 | |
| 2219 | // Initially this is the only room in the list |
| 2220 | next_rooms[0] = init_room_index; |
| 2221 | highest_next_room_index = 0; |
| 2222 | cur_next_room_index = 0; |
| 2223 | |
| 2224 | // Use standard fvi list_array / bool list |
| 2225 | fvi_visit_list[init_room_index >> 3] |= 0x01 << (init_room_index % 8); |
| 2226 | fvi_rooms_visited[0] = init_room_index; |
| 2227 | fvi_num_rooms_visited = 1; |
| 2228 | |
| 2229 | while (num_objects <= max_elements && cur_next_room_index <= highest_next_room_index) { |
| 2230 | cur_room = &Rooms[next_rooms[cur_next_room_index]]; |
| 2231 | |
| 2232 | // Do object stuff |
| 2233 | int cur_obj_index = cur_room->objects; |
| 2234 | |
| 2235 | while (cur_obj_index > -1) { |
| 2236 | if (num_objects >= max_elements) |
| 2237 | break; |
| 2238 | if ((f_include_non_collide_objects) || CollisionRayResult[Objects[cur_obj_index].type] != RESULT_NOTHING) { |
| 2239 | if (!f_only_players_and_ais || Objects[cur_obj_index].type == OBJ_PLAYER || Objects[cur_obj_index].ai_info) { |
| 2240 | if (!(f_lightmap_only && (Objects[cur_obj_index].lighting_render_type != LRT_LIGHTMAPS))) { |
| 2241 | if (object_movement_AABB(&Objects[cur_obj_index])) { |
| 2242 | object_index_list[num_objects++] = cur_obj_index; |
| 2243 | ASSERT(num_objects < 0 || num_objects <= max_elements); |
| 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | cur_obj_index = Objects[cur_obj_index].next; |
| 2250 | } |
| 2251 | |
| 2252 | if (num_objects >= max_elements) |
| 2253 | break; |
| 2254 | |
| 2255 | if (f_stop_at_closed_doors && cur_room->flags & RF_DOOR) { |
| 2256 | // Closed doors antenuate a lot |
| 2257 | if (DoorwayGetPosition(cur_room) == 0.0) |
| 2258 | goto skip_room_prop; |
| 2259 | } |
| 2260 | |
| 2261 | for (x = 0; x < cur_room->num_portals; x++) { |
| 2262 | int portal_num; |
| 2263 | int connect_room; |
| 2264 | |
| 2265 | if (f_stop_at_closed_doors) { |
| 2266 | if ((cur_room->portals[x].flags & PF_RENDER_FACES) && |
| 2267 | !(cur_room->portals[x].flags & PF_RENDERED_FLYTHROUGH) || |
| 2268 | (cur_room->portals[x].flags & PF_BLOCK)) { |
| 2269 | continue; |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | i = cur_room->portals[x].portal_face; |
| 2274 | |
| 2275 | if (!room_movement_AABB(&cur_room->faces[i])) |
| 2276 | continue; |
| 2277 | |
| 2278 | portal_num = cur_room->faces[i].portal_num; |
| 2279 | connect_room = cur_room->portals[portal_num].croom; |
| 2280 | |
| 2281 | // If the conect_room is not a terrain cell and we still have a slot in the next room list... |
| 2282 | if (connect_room >= 0 && highest_next_room_index + 1 < MAX_QUICK_ROOMS) { |
| 2283 | ASSERT(Rooms[connect_room].used); |
| 2284 | |
| 2285 | if ((fvi_visit_list[connect_room >> 3] & (0x01 << ((connect_room) % 8))) == 0) { |
| 2286 | fvi_visit_list[connect_room >> 3] |= 0x01 << (connect_room % 8); |
| 2287 | fvi_rooms_visited[fvi_num_rooms_visited++] = connect_room; |
| 2288 | |
| 2289 | ASSERT(highest_next_room_index + 1 >= 0 && highest_next_room_index + 1 < MAX_QUICK_ROOMS); |
| 2290 | next_rooms[++highest_next_room_index] = connect_room; |
| 2291 | } |
| 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | skip_room_prop: |
| 2296 | |
| 2297 | cur_next_room_index++; |
| 2298 | } |
| 2299 | |
| 2300 | // Cleans up the boolean room visit list |
| 2301 | for (i = 0; i < fvi_num_rooms_visited; i++) { |
| 2302 | fvi_visit_list[fvi_rooms_visited[i] >> 3] = 0; |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | return num_objects; |
| 2307 | } |
| 2308 | |
| 2309 | bool fvi_QuickRoomCheck(vector *pos, room *cur_room, bool try_again) { |
| 2310 | vector hit_point; // where we hit |
| 2311 | vector colp; |
| 2312 | float cur_dist; // distance to hit point |
| 2313 | int i; |
| 2314 | bool f_in_room = true; |
| 2315 | |
| 2316 | internal_try_again: |
| 2317 | |
| 2318 | int closest_hit_type = 0; |
| 2319 | float closest_hit_distance = 10000000.0f; |
| 2320 | |
| 2321 | vector min_xyz; |
| 2322 | vector max_xyz; |
| 2323 | vector new_pos; |
| 2324 | |
| 2325 | if (!(cur_room->used) || (cur_room->flags & RF_EXTERNAL)) |
| 2326 | return false; |
| 2327 | |
| 2328 | new_pos = min_xyz = max_xyz = *pos; |
| 2329 | |
| 2330 | if (!try_again) { |
| 2331 | new_pos.x += 10000000.0f; |
| 2332 | max_xyz.x += 10000000.0f; |
| 2333 | } else { |
| 2334 | new_pos.z -= 10000000.0f; |
| 2335 | min_xyz.z -= 10000000.0f; |
| 2336 | new_pos.y -= 10000000.0f; |
| 2337 | min_xyz.y -= 10000000.0f; |
| 2338 | new_pos.x -= 10000000.0f; |
| 2339 | min_xyz.x -= 10000000.0f; |
| 2340 | } |
| 2341 | // mprintf(0, "Checking room %d ", ROOMNUM(cur_room)); |
| 2342 | |
| 2343 | // sort shit |
| 2344 | uint8_t msector = 0; |
| 2345 | |
| 2346 | if (min_xyz.x <= cur_room->bbf_min_xyz.x) { |
| 2347 | msector |= 0x01; |
| 2348 | } |
| 2349 | if (min_xyz.y <= cur_room->bbf_min_xyz.y) { |
| 2350 | msector |= 0x02; |
| 2351 | } |
| 2352 | if (min_xyz.z <= cur_room->bbf_min_xyz.z) { |
| 2353 | msector |= 0x04; |
| 2354 | } |
| 2355 | if (max_xyz.x >= cur_room->bbf_max_xyz.x) { |
| 2356 | msector |= 0x08; |
| 2357 | } |
| 2358 | if (max_xyz.y >= cur_room->bbf_max_xyz.y) { |
| 2359 | msector |= 0x10; |
| 2360 | } |
| 2361 | if (max_xyz.z >= cur_room->bbf_max_xyz.z) { |
| 2362 | msector |= 0x20; |
| 2363 | } |
| 2364 | |
| 2365 | const int16_t num_bbf_regions = cur_room->num_bbf_regions; |
| 2366 | int16_t *num_faces_ptr = cur_room->num_bbf; |
| 2367 | uint8_t *bbf_val = cur_room->bbf_list_sector; |
| 2368 | vector *region_min = cur_room->bbf_list_min_xyz; |
| 2369 | vector *region_max = cur_room->bbf_list_max_xyz; |
| 2370 | int16_t **bbf_list_ptr = cur_room->bbf_list; |
| 2371 | |
| 2372 | // Do the actual wall collsion stuff here! |
| 2373 | for (int test1 = 0; test1 < num_bbf_regions; test1++) { |
| 2374 | if (((*bbf_val) & msector) == (*bbf_val)) { |
| 2375 | if (region_min->x > max_xyz.x || region_min->y > max_xyz.y || region_min->z > max_xyz.z || |
| 2376 | region_max->x < min_xyz.x || region_max->y < min_xyz.y || region_max->z < min_xyz.z) |
| 2377 | goto skip_region; |
| 2378 | |
| 2379 | int16_t *cur_face_index_ptr = *bbf_list_ptr; |
| 2380 | |
| 2381 | for (int sort_list_cur = 0; sort_list_cur < (*num_faces_ptr); sort_list_cur++) { |
| 2382 | i = *cur_face_index_ptr; |
| 2383 | cur_face_index_ptr++; |
| 2384 | |
| 2385 | vector face_normal; |
| 2386 | vector *vertex_ptr_list[MAX_VERTS_PER_FACE]; |
| 2387 | int face_hit_type; |
| 2388 | vector wall_norm; |
| 2389 | int16_t count; |
| 2390 | bool f_backface; |
| 2391 | |
| 2392 | if (cur_room->faces[i].flags & FF_NOT_SHELL) |
| 2393 | continue; |
| 2394 | |
| 2395 | if (!room_manual_AABB(&cur_room->faces[i], &min_xyz, &max_xyz)) |
| 2396 | continue; |
| 2397 | |
| 2398 | f_backface = false; |
| 2399 | |
| 2400 | for (count = 0; count < cur_room->faces[i].num_verts; count++) |
| 2401 | vertex_ptr_list[count] = &cur_room->verts[cur_room->faces[i].face_verts[count]]; |
| 2402 | |
| 2403 | face_normal = cur_room->faces[i].normal; |
| 2404 | |
| 2405 | face_hit_type = check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, pos, &new_pos, &face_normal, |
| 2406 | vertex_ptr_list, cur_room->faces[i].num_verts, 0.0); |
| 2407 | if (!face_hit_type) { |
| 2408 | face_normal *= -1.0f; |
| 2409 | for (count = 0; count < cur_room->faces[i].num_verts; count++) |
| 2410 | vertex_ptr_list[cur_room->faces[i].num_verts - count - 1] = |
| 2411 | &cur_room->verts[cur_room->faces[i].face_verts[count]]; |
| 2412 | |
| 2413 | face_hit_type = check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, pos, &new_pos, &face_normal, |
| 2414 | vertex_ptr_list, cur_room->faces[i].num_verts, 0.0); |
| 2415 | f_backface = true; |
| 2416 | } |
| 2417 | |
| 2418 | // If we hit the face... |
| 2419 | if (face_hit_type) { |
| 2420 | if ((cur_dist <= closest_hit_distance && !f_backface) || (cur_dist < closest_hit_distance && f_backface)) { |
| 2421 | closest_hit_distance = cur_dist; |
| 2422 | |
| 2423 | if (f_backface) |
| 2424 | closest_hit_type = HIT_BACKFACE; |
| 2425 | else |
| 2426 | closest_hit_type = HIT_WALL; |
| 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | skip_region:; |
| 2432 | num_faces_ptr++; |
| 2433 | bbf_val++; |
| 2434 | region_max++; |
| 2435 | region_min++; |
| 2436 | bbf_list_ptr++; |
| 2437 | } |
| 2438 | |
| 2439 | if (closest_hit_type != HIT_WALL) { |
| 2440 | f_in_room = false; |
| 2441 | // mprintf(0, " in room bool = %d, hit_type = %d\n", (int) f_in_room, closest_hit_type); |
| 2442 | } |
| 2443 | |
| 2444 | if (!f_in_room && !try_again) { |
| 2445 | try_again = true; |
| 2446 | f_in_room = true; |
| 2447 | goto internal_try_again; |
| 2448 | } |
| 2449 | |
| 2450 | return f_in_room; |
| 2451 | } |
| 2452 | |
| 2453 | // Long rays are 10 segments or longer in length |
| 2454 | #define MIN_LONG_RAY (TERRAIN_SIZE * 20.0) |
| 2455 | |
| 2456 | inline bool is_long_xz_ray(fvi_query *fq) { |
| 2457 | if ((fabs(fq->p0->x - fq->p1->x) > MIN_LONG_RAY) || (fabs(fq->p0->z - fq->p1->z) > MIN_LONG_RAY)) { |
| 2458 | return true; |
| 2459 | } else { |
| 2460 | return false; |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | void check_ceiling() { |
| 2465 | vector hit_point; |
| 2466 | float cur_dist; |
| 2467 | |
| 2468 | vector face_normal = {0.0, -1.0, 0.0}; |
| 2469 | vector *vertex_ptr_list[4]; |
| 2470 | vector vlist[4]; |
| 2471 | int face_hit_type; |
| 2472 | vector wall_norm; |
| 2473 | vector colp; |
| 2474 | |
| 2475 | // Bail early if hitpnt is not high enough |
| 2476 | if (fvi_query_ptr->rad + fvi_hit_data_ptr->hit_pnt.y < CEILING_HEIGHT) |
| 2477 | return; |
| 2478 | |
| 2479 | vlist[0].x = 0.0; |
| 2480 | vlist[0].y = CEILING_HEIGHT; |
| 2481 | vlist[0].z = 0.0; |
| 2482 | vlist[1].x = TERRAIN_WIDTH * TERRAIN_SIZE; |
| 2483 | vlist[1].y = CEILING_HEIGHT; |
| 2484 | vlist[1].z = 0.0; |
| 2485 | vlist[2].x = TERRAIN_WIDTH * TERRAIN_SIZE; |
| 2486 | vlist[2].y = CEILING_HEIGHT; |
| 2487 | vlist[2].z = TERRAIN_DEPTH * TERRAIN_SIZE; |
| 2488 | vlist[3].x = 0.0; |
| 2489 | vlist[3].y = CEILING_HEIGHT; |
| 2490 | vlist[3].z = TERRAIN_DEPTH * TERRAIN_SIZE; |
| 2491 | |
| 2492 | vertex_ptr_list[0] = &vlist[0]; |
| 2493 | vertex_ptr_list[1] = &vlist[1]; |
| 2494 | vertex_ptr_list[2] = &vlist[2]; |
| 2495 | vertex_ptr_list[3] = &vlist[3]; |
| 2496 | |
| 2497 | // Did we hit this face? |
| 2498 | face_hit_type = check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, fvi_query_ptr->p0, |
| 2499 | &fvi_hit_data_ptr->hit_pnt, &face_normal, vertex_ptr_list, 4, fvi_query_ptr->rad); |
| 2500 | |
| 2501 | if (face_hit_type) { |
| 2502 | if (cur_dist <= fvi_collision_dist) { |
| 2503 | if ((cur_dist < fvi_collision_dist) || !(fvi_query_ptr->flags & FQ_MULTI_POINT)) { |
| 2504 | fvi_hit_data_ptr->num_hits = 0; |
| 2505 | |
| 2506 | fvi_hit_data_ptr->hit_pnt = hit_point; |
| 2507 | fvi_collision_dist = cur_dist; |
| 2508 | compute_movement_AABB(); |
| 2509 | } else if (fvi_hit_data_ptr->num_hits == MAX_HITS) { |
| 2510 | goto ignore_hit; |
| 2511 | } |
| 2512 | |
| 2513 | fvi_hit_data_ptr->hit_type[fvi_hit_data_ptr->num_hits] = HIT_CEILING; |
| 2514 | fvi_hit_data_ptr->hit_wallnorm[fvi_hit_data_ptr->num_hits] = wall_norm; |
| 2515 | // fvi_hit_data_ptr->hit_seg = -1; -- set in the fvi_FindIntersection function |
| 2516 | fvi_hit_data_ptr->hit_object[fvi_hit_data_ptr->num_hits] = -1; |
| 2517 | fvi_hit_data_ptr->hit_face[fvi_hit_data_ptr->num_hits] = 0; |
| 2518 | fvi_hit_data_ptr->hit_face_room[fvi_hit_data_ptr->num_hits] = 0; |
| 2519 | fvi_hit_data_ptr->hit_face_pnt[fvi_hit_data_ptr->num_hits] = colp; |
| 2520 | |
| 2521 | fvi_hit_data_ptr->num_hits++; |
| 2522 | } |
| 2523 | } |
| 2524 | ignore_hit:; |
| 2525 | } |
| 2526 | |
| 2527 | void make_trigger_face_list(int last_sim_faces) { |
| 2528 | int num_real_collisions; |
| 2529 | int x; |
| 2530 | |
| 2531 | num_real_collisions = last_sim_faces; |
| 2532 | |
| 2533 | ASSERT(Fvi_num_recorded_faces <= MAX_RECORDED_FACES); |
| 2534 | |
| 2535 | for (x = last_sim_faces; x < Fvi_num_recorded_faces; x++) { |
| 2536 | vector face_normal; |
| 2537 | vector *vertex_ptr_list[MAX_VERTS_PER_FACE]; |
| 2538 | int face_hit_type; |
| 2539 | vector wall_norm; |
| 2540 | vector colp; |
| 2541 | vector hit_point; |
| 2542 | int16_t count; |
| 2543 | room *cur_room = &Rooms[Fvi_recorded_faces[x].room_index]; |
| 2544 | int i = Fvi_recorded_faces[x].face_index; |
| 2545 | float cur_dist; |
| 2546 | |
| 2547 | for (count = 0; count < cur_room->faces[i].num_verts; count++) |
| 2548 | vertex_ptr_list[count] = &cur_room->verts[cur_room->faces[i].face_verts[count]]; |
| 2549 | /* |
| 2550 | mprintf(0, "FVI:In trigger %f to %f crossed %f\n", |
| 2551 | fvi_query_ptr->p0->z, |
| 2552 | fvi_hit_data_ptr->hit_pnt.z, |
| 2553 | vertex_ptr_list[0]->z); |
| 2554 | */ |
| 2555 | |
| 2556 | face_normal = cur_room->faces[i].normal; |
| 2557 | |
| 2558 | face_hit_type = |
| 2559 | check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, fvi_query_ptr->p0, &fvi_hit_data_ptr->hit_pnt, |
| 2560 | &face_normal, vertex_ptr_list, cur_room->faces[i].num_verts, 0.0f); |
| 2561 | |
| 2562 | if (face_hit_type) { |
| 2563 | // mprintf(0, "FVI:hit trigger\n"); |
| 2564 | Fvi_recorded_faces[num_real_collisions].face_index = i; |
| 2565 | Fvi_recorded_faces[num_real_collisions++].room_index = Fvi_recorded_faces[x].room_index; |
| 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | if (!ROOMNUM_OUTSIDE(fvi_query_ptr->startroom) && !ROOMNUM_OUTSIDE(fvi_hit_data_ptr->hit_room)) { |
| 2570 | int i; |
| 2571 | |
| 2572 | for (i = 0; i < Rooms[fvi_query_ptr->startroom].num_portals; i++) { |
| 2573 | if (Rooms[fvi_query_ptr->startroom].portals[i].croom == fvi_hit_data_ptr->hit_room) { |
| 2574 | int j; |
| 2575 | bool f_found = false; |
| 2576 | |
| 2577 | for (j = 0; j < num_real_collisions; j++) { |
| 2578 | if (Fvi_recorded_faces[j].face_index == Rooms[fvi_query_ptr->startroom].portals[i].portal_face && |
| 2579 | Fvi_recorded_faces[j].room_index == fvi_query_ptr->startroom) { |
| 2580 | f_found = true; |
| 2581 | break; |
| 2582 | } |
| 2583 | } |
| 2584 | |
| 2585 | if (!f_found) { |
| 2586 | Fvi_recorded_faces[num_real_collisions].face_index = Rooms[fvi_query_ptr->startroom].portals[i].portal_face; |
| 2587 | Fvi_recorded_faces[num_real_collisions++].room_index = fvi_query_ptr->startroom; |
| 2588 | } |
| 2589 | } |
| 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | Fvi_num_recorded_faces = num_real_collisions; |
| 2594 | } |
| 2595 | |
| 2596 | // Find out if a vector intersects with anything. |
| 2597 | // Fills in hit_data, an fvi_info structure (see header file). |
| 2598 | // Parms: |
| 2599 | // p0 & startseg describe the start of the vector |
| 2600 | // p1 the end of the vector |
| 2601 | // rad the radius of the cylinder |
| 2602 | // thisobjnum used to prevent an object with colliding with itself |
| 2603 | // ingore_obj ignore collisions with this object |
| 2604 | // check_obj_flag determines whether collisions with objects are checked |
| 2605 | // Returns the hit_data->hit_type |
| 2606 | #ifndef NED_PHYSICS |
| 2607 | extern bool Tracking_FVI; |
| 2608 | #endif |
| 2609 | |
| 2610 | int fvi_FindIntersection(fvi_query *fq, fvi_info *hit_data, bool no_subdivision) { |
| 2611 | int i; |
| 2612 | object *this_obj; |
| 2613 | int last_sim_trigger_faces; |
| 2614 | |
| 2615 | if (fq->startroom == -1) { |
| 2616 | return HIT_NONE; |
| 2617 | } |
| 2618 | |
| 2619 | #ifndef NED_PHYSICS |
| 2620 | if (Tracking_FVI) { |
| 2621 | mprintf(0, "Track FVI - Ray %d, thisobjnum=%d, startroom=%d, rad=%f\n", FVI_counter, fq->thisobjnum, fq->startroom, |
| 2622 | fq->rad); |
| 2623 | } |
| 2624 | #endif |
| 2625 | |
| 2626 | ///////////////////////////////////////// |
| 2627 | // Debug Code |
| 2628 | #ifdef USE_RTP |
| 2629 | INT64 curr_time; |
| 2630 | RTP_GETCLOCK(curr_time); |
| 2631 | |
| 2632 | RTP_tSTARTTIME(fvi_time, curr_time); |
| 2633 | RTP_INCRVALUE(fvi_calls, 1); |
| 2634 | #endif |
| 2635 | |
| 2636 | FVI_counter++; |
| 2637 | ///////////////////////////////////////// |
| 2638 | |
| 2639 | // Setup our globals |
| 2640 | fvi_hit_data_ptr = hit_data; |
| 2641 | fvi_query_ptr = fq; |
| 2642 | |
| 2643 | if (fq->thisobjnum >= 0) |
| 2644 | this_obj = &Objects[fq->thisobjnum]; |
| 2645 | else |
| 2646 | this_obj = nullptr; |
| 2647 | |
| 2648 | if (fq->rad == 0.0f) |
| 2649 | fvi_zero_rad = true; |
| 2650 | else |
| 2651 | fvi_zero_rad = false; |
| 2652 | |
| 2653 | ASSERT(fq != nullptr && hit_data != nullptr); |
| 2654 | |
| 2655 | ASSERT(std::isfinite(fq->p1->x)); // Caller wants to go to infinity! -- Not FVI's fault. |
| 2656 | ASSERT(std::isfinite(fq->p1->y)); // Caller wants to go to infinity! -- Not FVI's fault. |
| 2657 | ASSERT(std::isfinite(fq->p1->z)); // Caller wants to go to infinity! -- Not FVI's fault. |
| 2658 | |
| 2659 | fvi_movement_delta = *fq->p1 - *fq->p0; |
| 2660 | |
| 2661 | // mprintf(0, "FVI:----New search----\n"); |
| 2662 | // mprintf(0, "FVI: P0 is %f, %f, %f\n", XYZ(fq->p0)); |
| 2663 | if (fq->flags & FQ_NEW_RECORD_LIST) { |
| 2664 | Fvi_num_recorded_faces = 0; |
| 2665 | } |
| 2666 | last_sim_trigger_faces = Fvi_num_recorded_faces; |
| 2667 | |
| 2668 | if ((this_obj) && (this_obj->flags & OF_POLYGON_OBJECT) && this_obj->type != OBJ_WEAPON && |
| 2669 | this_obj->type != OBJ_POWERUP && this_obj->type != OBJ_DEBRIS && this_obj->type != OBJ_ROOM && |
| 2670 | this_obj->type != OBJ_PLAYER && fq->rad == this_obj->size) { |
| 2671 | if (this_obj->mtype.phys_info.flags & PF_POINT_COLLIDE_WALLS) { |
| 2672 | fvi_wall_sphere_rad = 0.0f; |
| 2673 | fvi_wall_sphere_offset = Zero_vector; |
| 2674 | fvi_wall_sphere_p0 = *fq->p0; |
| 2675 | fvi_wall_sphere_p1 = *fq->p1; |
| 2676 | } else { |
| 2677 | fvi_wall_sphere_rad = Poly_models[this_obj->rtype.pobj_info.model_num].wall_size; |
| 2678 | fvi_wall_sphere_offset = this_obj->wall_sphere_offset; |
| 2679 | fvi_wall_sphere_p0 = *fq->p0 + fvi_wall_sphere_offset; |
| 2680 | fvi_wall_sphere_p1 = *fq->p1 + fvi_wall_sphere_offset; |
| 2681 | } |
| 2682 | |
| 2683 | fvi_anim_sphere_rad = Poly_models[this_obj->rtype.pobj_info.model_num].anim_size; |
| 2684 | fvi_anim_sphere_offset = this_obj->anim_sphere_offset; |
| 2685 | fvi_anim_sphere_p0 = *fq->p0 + fvi_anim_sphere_offset; |
| 2686 | fvi_anim_sphere_p1 = *fq->p1 + fvi_anim_sphere_offset; |
| 2687 | } else { |
| 2688 | if ((this_obj) && this_obj->type == OBJ_PLAYER && fq->rad == this_obj->size) { |
| 2689 | fvi_wall_sphere_rad = fq->rad * PLAYER_SIZE_SCALAR; |
| 2690 | if (Players[this_obj->id].flags & (PLAYER_FLAGS_DEAD | PLAYER_FLAGS_DYING)) |
| 2691 | fvi_wall_sphere_rad *= 0.5f; |
| 2692 | fvi_wall_sphere_offset = Zero_vector; |
| 2693 | fvi_wall_sphere_p0 = *fq->p0; |
| 2694 | fvi_wall_sphere_p1 = *fq->p1; |
| 2695 | } else if ((this_obj) && this_obj->mtype.phys_info.flags & PF_POINT_COLLIDE_WALLS) { |
| 2696 | fvi_wall_sphere_rad = 0.0f; |
| 2697 | fvi_wall_sphere_offset = Zero_vector; |
| 2698 | fvi_wall_sphere_p0 = *fq->p0; |
| 2699 | fvi_wall_sphere_p1 = *fq->p1; |
| 2700 | } else { |
| 2701 | fvi_wall_sphere_rad = fq->rad; |
| 2702 | fvi_wall_sphere_offset = Zero_vector; |
| 2703 | fvi_wall_sphere_p0 = *fq->p0; |
| 2704 | fvi_wall_sphere_p1 = *fq->p1; |
| 2705 | } |
| 2706 | |
| 2707 | fvi_anim_sphere_rad = fq->rad; |
| 2708 | fvi_anim_sphere_offset = Zero_vector; |
| 2709 | fvi_anim_sphere_p0 = *fq->p0; |
| 2710 | fvi_anim_sphere_p1 = *fq->p1; |
| 2711 | } |
| 2712 | |
| 2713 | fvi_num_rooms_visited = 0; |
| 2714 | fvi_num_cells_visited = 0; |
| 2715 | fvi_num_cells_obj_visited = 0; |
| 2716 | f_check_terrain = true; |
| 2717 | |
| 2718 | // Initially assume we will hit the endpoint -- Do not compute the end segment unless we actually have no collisions |
| 2719 | hit_data->hit_pnt = *fq->p1; |
| 2720 | hit_data->hit_room = -1; |
| 2721 | |
| 2722 | hit_data->num_hits = 0; |
| 2723 | hit_data->hit_type[0] = HIT_NONE; |
| 2724 | hit_data->hit_face_room[0] = -1; |
| 2725 | hit_data->hit_object[0] = -1; |
| 2726 | |
| 2727 | hit_data->n_rooms = 0; |
| 2728 | fvi_collision_dist = vm_VectorDistance(fq->p0, fq->p1) + 0.0000001f; |
| 2729 | |
| 2730 | // Computes a axis-aligned bounding-box that encompasses the area |
| 2731 | compute_movement_AABB(); |
| 2732 | |
| 2733 | if (ROOMNUM_OUTSIDE(fq->startroom)) { |
| 2734 | // Subdivides long rays |
| 2735 | if ((!no_subdivision) && is_long_xz_ray(fq)) { |
| 2736 | |
| 2737 | // Save old pointers |
| 2738 | fvi_info *save_fvi_hit_data_ptr = hit_data; |
| 2739 | fvi_query *save_fvi_query_ptr = fq; |
| 2740 | |
| 2741 | // Copy old data to new data |
| 2742 | fvi_info fvi_new_hit_data = *hit_data; |
| 2743 | fvi_query fvi_new_query = *fq; |
| 2744 | |
| 2745 | // These are the new "moving" line points |
| 2746 | vector new_p0; |
| 2747 | vector new_p1; |
| 2748 | |
| 2749 | // Number of whole subdivisions |
| 2750 | int num_subdivisions = vm_VectorDistance(fq->p0, fq->p1) / MIN_LONG_RAY; |
| 2751 | vector sub_dir; // Direction and magnitude of each subdivision |
| 2752 | int s_hit_type = 0; // Sub-divided hit type |
| 2753 | |
| 2754 | sub_dir = *fq->p1 - *fq->p0; // Direction of movement |
| 2755 | vm_NormalizeVector(&sub_dir); // Normalize it |
| 2756 | sub_dir *= MIN_LONG_RAY; // Scale it to the length of a sub-division |
| 2757 | |
| 2758 | // Determine the first sub-division |
| 2759 | new_p0 = *fq->p0; |
| 2760 | new_p1 = new_p0 + sub_dir; |
| 2761 | |
| 2762 | fvi_new_query.p0 = &new_p0; |
| 2763 | fvi_new_query.p1 = &new_p1; |
| 2764 | |
| 2765 | for (i = 0; i < num_subdivisions; i++) { |
| 2766 | fvi_new_query.startroom = GetTerrainRoomFromPos(&new_p0); |
| 2767 | |
| 2768 | // mprintf(0, "S %d F %f,%f,%f to %f,%f,%f\n", i, XYZ(&new_p0), XYZ(&new_p1)); |
| 2769 | s_hit_type = fvi_FindIntersection(&fvi_new_query, &fvi_new_hit_data, true); |
| 2770 | |
| 2771 | fvi_new_query.flags &= (~FQ_NEW_RECORD_LIST); |
| 2772 | |
| 2773 | if (s_hit_type != HIT_NONE) { |
| 2774 | // mprintf(0, "Hit %d at %f, %f, %f\n", s_hit_type, XYZ(&fvi_new_hit_data.hit_pnt)); |
| 2775 | break; |
| 2776 | } |
| 2777 | |
| 2778 | if (!ROOMNUM_OUTSIDE(fvi_new_hit_data.hit_room)) |
| 2779 | break; |
| 2780 | |
| 2781 | new_p0 = new_p1; |
| 2782 | new_p1 += sub_dir; |
| 2783 | } |
| 2784 | |
| 2785 | // Check up to the real stopping point |
| 2786 | if (s_hit_type == HIT_NONE) { |
| 2787 | new_p1 = *save_fvi_query_ptr->p1; |
| 2788 | s_hit_type = fvi_FindIntersection(&fvi_new_query, &fvi_new_hit_data, true); |
| 2789 | } |
| 2790 | |
| 2791 | hit_data = save_fvi_hit_data_ptr; |
| 2792 | fq = save_fvi_query_ptr; |
| 2793 | |
| 2794 | *hit_data = fvi_new_hit_data; |
| 2795 | #ifdef USE_RTP |
| 2796 | RTP_tENDTIME(fvi_time, curr_time); |
| 2797 | #endif |
| 2798 | return hit_data->hit_type[0]; |
| 2799 | } else |
| 2800 | do_fvi_terrain(); |
| 2801 | } else { |
| 2802 | ASSERT(!(Rooms[fq->startroom].flags & RF_EXTERNAL)); // If we hit this, it is not FVI's fault |
| 2803 | // The caller to fvi has a bug |
| 2804 | FVI_room_counter++; |
| 2805 | // do_fvi_rooms(fq->startroom); |
| 2806 | fvi_room(fq->startroom, -1); |
| 2807 | |
| 2808 | ASSERT(fvi_num_rooms_visited >= 1); |
| 2809 | } |
| 2810 | |
| 2811 | // Check objects in rooms we visited |
| 2812 | fvi_rooms_objs(); |
| 2813 | |
| 2814 | if (FVI_always_check_ceiling && (fq->flags & FQ_CHECK_CEILING)) { |
| 2815 | check_ceiling(); |
| 2816 | } |
| 2817 | |
| 2818 | // Determine hit seg. |
| 2819 | |
| 2820 | // Only do this if not running radiosity |
| 2821 | if (!(fq->flags & FQ_NO_RELINK)) { |
| 2822 | if (fvi_num_rooms_visited == 1 && fvi_num_cells_visited == 0) { |
| 2823 | hit_data->hit_room = fvi_rooms_visited[0]; |
| 2824 | } else if ((hit_data->hit_type[0] == HIT_WALL || hit_data->hit_type[0] == HIT_TERRAIN) && |
| 2825 | (fvi_zero_rad || |
| 2826 | (fvi_query_ptr->thisobjnum >= 0 && |
| 2827 | (Objects[fvi_query_ptr->thisobjnum].mtype.phys_info.flags & PF_POINT_COLLIDE_WALLS))) && |
| 2828 | (ROOMNUM_OUTSIDE(hit_data->hit_face_room[0]) || |
| 2829 | !(Rooms[hit_data->hit_face_room[0]].flags & RF_EXTERNAL))) { |
| 2830 | hit_data->hit_room = hit_data->hit_face_room[0]; |
| 2831 | } else { |
| 2832 | bool f_found_room = false; |
| 2833 | |
| 2834 | for (i = 0; i < fvi_num_rooms_visited && !f_found_room; i++) { |
| 2835 | if (!(Rooms[fvi_rooms_visited[i]].flags & RF_EXTERNAL)) |
| 2836 | if (fvi_QuickRoomCheck(&hit_data->hit_pnt, &Rooms[fvi_rooms_visited[i]])) { |
| 2837 | f_found_room = true; |
| 2838 | hit_data->hit_room = fvi_rooms_visited[i]; |
| 2839 | } |
| 2840 | } |
| 2841 | |
| 2842 | if (!f_found_room && fvi_num_cells_visited >= 1) { |
| 2843 | // We must be outside |
| 2844 | |
| 2845 | // Check the ceiling too |
| 2846 | if (!FVI_always_check_ceiling && (fq->flags & FQ_CHECK_CEILING)) { |
| 2847 | check_ceiling(); |
| 2848 | } |
| 2849 | |
| 2850 | // Determine if we are within the valid terrain bounds |
| 2851 | hit_data->hit_room = GetTerrainRoomFromPos(&hit_data->hit_pnt); |
| 2852 | if (hit_data->hit_room == -1) { |
| 2853 | hit_data->hit_type[0] = HIT_OUT_OF_TERRAIN_BOUNDS; |
| 2854 | } |
| 2855 | } else if (!f_found_room) { |
| 2856 | // mprintf(0, "Attempting to patch\n"); |
| 2857 | for (i = 0; i < fvi_num_rooms_visited && !f_found_room; i++) { |
| 2858 | if (!(Rooms[fvi_rooms_visited[i]].flags & RF_EXTERNAL)) |
| 2859 | if (fvi_QuickRoomCheck(&hit_data->hit_pnt, &Rooms[fvi_rooms_visited[i]]), true) { |
| 2860 | f_found_room = true; |
| 2861 | hit_data->hit_room = fvi_rooms_visited[i]; |
| 2862 | ASSERT(!(Rooms[hit_data->hit_room].flags & RF_EXTERNAL)); |
| 2863 | // break; |
| 2864 | } |
| 2865 | } |
| 2866 | |
| 2867 | ASSERT(no_subdivision || f_found_room); |
| 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | // Do the ASSERTS for FVI. |
| 2872 | ASSERT(!(hit_data->hit_room == -1 && hit_data->hit_type[0] != HIT_OUT_OF_TERRAIN_BOUNDS)); |
| 2873 | ASSERT(!(hit_data->hit_type[0] == HIT_OBJECT && hit_data->hit_object[0] == -1)); |
| 2874 | } |
| 2875 | |
| 2876 | // Clean up the visit list bits |
| 2877 | for (i = 0; i < fvi_num_cells_visited; i++) { |
| 2878 | fvi_terrain_visit_list[fvi_cells_visited[i] >> 3] = 0; |
| 2879 | } |
| 2880 | |
| 2881 | for (i = 0; i < fvi_num_cells_obj_visited; i++) { |
| 2882 | fvi_terrain_obj_visit_list[fvi_cells_obj_visited[i] >> 3] = 0; |
| 2883 | } |
| 2884 | |
| 2885 | for (i = 0; i < fvi_num_rooms_visited; i++) { |
| 2886 | fvi_visit_list[fvi_rooms_visited[i] >> 3] = 0; |
| 2887 | } |
| 2888 | |
| 2889 | if (fvi_query_ptr->flags & FQ_RECORD) { |
| 2890 | make_trigger_face_list(last_sim_trigger_faces); |
| 2891 | } |
| 2892 | |
| 2893 | hit_data->hit_dist = fvi_collision_dist; |
| 2894 | |
| 2895 | // Return the hit type |
| 2896 | #ifdef USE_RTP |
| 2897 | RTP_tENDTIME(fvi_time, curr_time); |
| 2898 | #endif |
| 2899 | return hit_data->hit_type[0]; |
| 2900 | } |
| 2901 | |
| 2902 | int obj_in_list(int objnum, int *obj_list) { |
| 2903 | int t; |
| 2904 | |
| 2905 | while ((t = *obj_list) != -1 && t != objnum) |
| 2906 | obj_list++; |
| 2907 | |
| 2908 | return (t == objnum); |
| 2909 | } |
| 2910 | |
| 2911 | // new function for Mike |
| 2912 | // note: n_segs_visited must be set to zero before this is called |
| 2913 | int sphere_intersects_wall(vector *pnt, int segnum, float rad) { |
| 2914 | /* |
| 2915 | int facemask; |
| 2916 | segment *seg; |
| 2917 | |
| 2918 | segs_visited[n_segs_visited++] = segnum; |
| 2919 | |
| 2920 | facemask = GetSegMasks(pnt,segnum,rad).facemask; |
| 2921 | |
| 2922 | seg = &Segments[segnum]; |
| 2923 | |
| 2924 | if (facemask != 0) { //on the back of at least one face |
| 2925 | |
| 2926 | int side,bit,face; |
| 2927 | |
| 2928 | //for each face we are on the back of, check if intersected |
| 2929 | |
| 2930 | for (side=0,bit=1;side<6 && facemask>=bit;side++) { |
| 2931 | |
| 2932 | for (face=0;face<2;face++,bit<<=1) { |
| 2933 | |
| 2934 | if (facemask & bit) { //on the back of this face |
| 2935 | int face_hit_type; //in what way did we hit the face? |
| 2936 | int num_faces,vertex_list[6]; |
| 2937 | |
| 2938 | //did we go through this wall/door? |
| 2939 | |
| 2940 | if ((seg-Segments)==-1) |
| 2941 | Error("segnum == -1 in sphere_intersects_wall()"); |
| 2942 | |
| 2943 | create_abs_vertex_lists(&num_faces,vertex_list,seg-Segments,side); |
| 2944 | |
| 2945 | face_hit_type = check_sphere_to_face( pnt,&seg->sides[side], |
| 2946 | face,((num_faces==1)?4:3),rad,vertex_list); |
| 2947 | |
| 2948 | if (face_hit_type) { //through this wall/door |
| 2949 | int child,i; |
| 2950 | |
| 2951 | //if what we have hit is a door, check the adjoining seg |
| 2952 | |
| 2953 | child = seg->children[side]; |
| 2954 | |
| 2955 | for (i=0;i<n_segs_visited && child!=segs_visited[i];i++); |
| 2956 | |
| 2957 | if (i==n_segs_visited) { //haven't visited here yet |
| 2958 | |
| 2959 | if (!IS_CHILD(child)) |
| 2960 | return 1; |
| 2961 | else { |
| 2962 | |
| 2963 | if (sphere_intersects_wall(pnt,child,rad)) |
| 2964 | return 1; |
| 2965 | } |
| 2966 | } |
| 2967 | } |
| 2968 | } |
| 2969 | } |
| 2970 | } |
| 2971 | } |
| 2972 | */ |
| 2973 | return 0; |
| 2974 | } |
| 2975 | |
| 2976 | static const int bbox_edges[12][2] = {{0, 1}, {1, 2}, {2, 3}, {3, 0}, {3, 4}, {2, 5}, |
| 2977 | {5, 4}, {4, 7}, {5, 6}, {7, 6}, {7, 0}, {6, 1}}; |
| 2978 | |
| 2979 | static const int bbox_faces[6][4] = {{4, 5, 2, 3}, {7, 6, 5, 4}, {0, 1, 6, 7}, |
| 2980 | {0, 3, 2, 1}, {7, 4, 3, 0}, {1, 2, 5, 3}}; |
| 2981 | /* |
| 2982 | bool BBoxPlaneIntersection(bool fast_exit, vector *collision_point, vector *collision_normal, object *obj, vector |
| 2983 | *new_pos, int nv, vector **vertex_ptr_list, vector *face_normal) |
| 2984 | { |
| 2985 | vector plane_pnt; |
| 2986 | vector verts[12]; |
| 2987 | vector norms[6]; |
| 2988 | poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num]; |
| 2989 | int i, j, k; |
| 2990 | vector rel[32]; |
| 2991 | float dot[32]; |
| 2992 | vector *bbox_vertex_ptr_list[4]; |
| 2993 | |
| 2994 | int num_int_box = 0; |
| 2995 | int num_int_poly = 0; |
| 2996 | vector int_points_box[12]; |
| 2997 | vector int_points_poly[32]; |
| 2998 | int16_t int_faces = 0; |
| 2999 | bool f_int_box = false; |
| 3000 | bool f_int_poly = false; |
| 3001 | |
| 3002 | |
| 3003 | verts[0] = (obj->orient.rvec * pm->mins.x) + |
| 3004 | (obj->orient.uvec * pm->maxs.y) + |
| 3005 | (obj->orient.fvec * pm->mins.z); |
| 3006 | |
| 3007 | verts[1] = (obj->orient.rvec * pm->mins.x) + |
| 3008 | (obj->orient.uvec * pm->mins.y) + |
| 3009 | (obj->orient.fvec * pm->mins.z); |
| 3010 | |
| 3011 | verts[2] = (obj->orient.rvec * pm->maxs.x) + |
| 3012 | (obj->orient.uvec * pm->mins.y) + |
| 3013 | (obj->orient.fvec * pm->mins.z); |
| 3014 | |
| 3015 | verts[3] = (obj->orient.rvec * pm->maxs.x) + |
| 3016 | (obj->orient.uvec * pm->maxs.y) + |
| 3017 | (obj->orient.fvec * pm->mins.z); |
| 3018 | |
| 3019 | verts[4] = (obj->orient.rvec * pm->maxs.x) + |
| 3020 | (obj->orient.uvec * pm->maxs.y) + |
| 3021 | (obj->orient.fvec * pm->maxs.z); |
| 3022 | |
| 3023 | verts[5] = (obj->orient.rvec * pm->maxs.x) + |
| 3024 | (obj->orient.uvec * pm->mins.y) + |
| 3025 | (obj->orient.fvec * pm->maxs.z); |
| 3026 | |
| 3027 | verts[6] = (obj->orient.rvec * pm->mins.x) + |
| 3028 | (obj->orient.uvec * pm->mins.y) + |
| 3029 | (obj->orient.fvec * pm->maxs.z); |
| 3030 | |
| 3031 | verts[7] = (obj->orient.rvec * pm->mins.x) + |
| 3032 | (obj->orient.uvec * pm->maxs.y) + |
| 3033 | (obj->orient.fvec * pm->maxs.z); |
| 3034 | |
| 3035 | |
| 3036 | // mprintf(0, "START\n"); |
| 3037 | for(i = 0; i < 8; i++) |
| 3038 | { |
| 3039 | verts[i] += *new_pos; |
| 3040 | // mprintf(0, "%f %f %f\n", XYZ(&verts[i])); |
| 3041 | } |
| 3042 | // mprintf(0, "END\n"); |
| 3043 | |
| 3044 | norms[0] = obj->orient.rvec; |
| 3045 | norms[1] = obj->orient.fvec; |
| 3046 | norms[2] = -obj->orient.rvec; |
| 3047 | norms[3] = -obj->orient.fvec; |
| 3048 | norms[4] = obj->orient.uvec; |
| 3049 | norms[5] = -obj->orient.uvec; |
| 3050 | |
| 3051 | for(i = 0; i < 8; i++) |
| 3052 | { |
| 3053 | rel[i] = verts[i] - *vertex_ptr_list[0]; |
| 3054 | dot[i] = rel[i] * (*face_normal); |
| 3055 | } |
| 3056 | |
| 3057 | // Get Plane intersection point |
| 3058 | for(i = 0; i < 12; i++) |
| 3059 | { |
| 3060 | vector movement; |
| 3061 | bool found; |
| 3062 | float nmovement; |
| 3063 | |
| 3064 | found = false; |
| 3065 | |
| 3066 | if(dot[bbox_edges[i][0]] > 0.0f && dot[bbox_edges[i][1]] < 0.0f) |
| 3067 | { |
| 3068 | movement = verts[bbox_edges[i][1]] - verts[bbox_edges[i][0]]; |
| 3069 | |
| 3070 | nmovement = -(movement * *face_normal); |
| 3071 | |
| 3072 | plane_pnt = verts[bbox_edges[i][0]]; |
| 3073 | plane_pnt += movement * (dot[bbox_edges[i][0]]/nmovement); |
| 3074 | |
| 3075 | f_int_poly = true; |
| 3076 | found = true; |
| 3077 | } |
| 3078 | else if(dot[bbox_edges[i][0]] < 0.0f && dot[bbox_edges[i][1]] > 0.0f) |
| 3079 | { |
| 3080 | movement = verts[bbox_edges[i][0]] - verts[bbox_edges[i][1]]; |
| 3081 | |
| 3082 | nmovement = -(movement * *face_normal); |
| 3083 | |
| 3084 | plane_pnt = verts[bbox_edges[i][1]]; |
| 3085 | plane_pnt += movement * (dot[bbox_edges[i][1]]/nmovement); |
| 3086 | |
| 3087 | f_int_poly = true; |
| 3088 | found = true; |
| 3089 | } |
| 3090 | |
| 3091 | if((found) && (!check_point_to_face(&plane_pnt, face_normal, nv, vertex_ptr_list))) |
| 3092 | { |
| 3093 | // we hit it |
| 3094 | // mprintf(0, "BBOX Collided with a plane\n"); |
| 3095 | if(fast_exit) |
| 3096 | return true; |
| 3097 | |
| 3098 | int_points_poly[num_int_poly++] = plane_pnt; |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | for(j = 0; j < 6; j++) |
| 3103 | { |
| 3104 | // Now the other way |
| 3105 | // Get Plane intersection point |
| 3106 | for(i = 0; i < nv; i++) |
| 3107 | { |
| 3108 | rel[i] = *vertex_ptr_list[i] - verts[bbox_faces[j][0]]; |
| 3109 | dot[i] = rel[i] * (norms[j]); |
| 3110 | } |
| 3111 | |
| 3112 | for(i = 0; i < nv; i++) |
| 3113 | { |
| 3114 | vector movement; |
| 3115 | bool found; |
| 3116 | float nmovement; |
| 3117 | |
| 3118 | found = false; |
| 3119 | |
| 3120 | if(dot[i] > 0.0f && dot[(i+1)%nv] < 0.0f) |
| 3121 | { |
| 3122 | movement = *vertex_ptr_list[(i+1)%nv] - *vertex_ptr_list[i]; |
| 3123 | |
| 3124 | nmovement = -(movement * norms[j]); |
| 3125 | |
| 3126 | plane_pnt = *vertex_ptr_list[i]; |
| 3127 | plane_pnt += movement * (dot[i]/nmovement); |
| 3128 | |
| 3129 | f_int_box = true; |
| 3130 | found = true; |
| 3131 | } |
| 3132 | else if(dot[i] < 0.0f && dot[(i+1)%nv] > 0.0f) |
| 3133 | { |
| 3134 | movement = *vertex_ptr_list[i] - *vertex_ptr_list[(i+1)%nv]; |
| 3135 | |
| 3136 | nmovement = -(movement * norms[j]); |
| 3137 | |
| 3138 | plane_pnt = *vertex_ptr_list[(i+1)%nv]; |
| 3139 | plane_pnt += movement * (dot[(i+1)%nv]/nmovement); |
| 3140 | |
| 3141 | f_int_box = true; |
| 3142 | found = true; |
| 3143 | } |
| 3144 | |
| 3145 | if(found) |
| 3146 | { |
| 3147 | |
| 3148 | for(k = 0; k < 4; k++) |
| 3149 | { |
| 3150 | bbox_vertex_ptr_list[k] = &verts[bbox_faces[j][k]]; |
| 3151 | } |
| 3152 | |
| 3153 | if (!check_point_to_face(&plane_pnt, &norms[j], 4, bbox_vertex_ptr_list)) |
| 3154 | { |
| 3155 | // we hit it |
| 3156 | // mprintf(0, "BBOX Collided with a plane\n"); |
| 3157 | if(fast_exit) |
| 3158 | return true; |
| 3159 | int_faces |= (0x01 << j); |
| 3160 | int_points_box[num_int_box++] = plane_pnt; |
| 3161 | } |
| 3162 | } |
| 3163 | } |
| 3164 | } |
| 3165 | |
| 3166 | if(num_int_poly || num_int_box) |
| 3167 | { |
| 3168 | vm_MakeZero(collision_point); |
| 3169 | |
| 3170 | for(i = 0; i < num_int_poly; i++) |
| 3171 | { |
| 3172 | *collision_point += int_points_poly[i]; |
| 3173 | } |
| 3174 | |
| 3175 | for(i = 0; i < num_int_box; i++) |
| 3176 | { |
| 3177 | *collision_point += int_points_box[i]; |
| 3178 | } |
| 3179 | |
| 3180 | *collision_point /= (float)(num_int_poly + num_int_box); |
| 3181 | *collision_normal = *face_normal; |
| 3182 | |
| 3183 | return true; |
| 3184 | } |
| 3185 | |
| 3186 | return false; |
| 3187 | } |
| 3188 | */ |
| 3189 | |
| 3190 | vector PointSpeed(object *obj, vector *pos, matrix *orient, vector *rotvel, vector *velocity) { |
| 3191 | vector r1 = *pos - obj->pos; |
| 3192 | vector w1; |
| 3193 | vector n1; |
| 3194 | float temp1; |
| 3195 | |
| 3196 | matrix o_t1 = *orient; |
| 3197 | |
| 3198 | vm_TransposeMatrix(&o_t1); |
| 3199 | vector cmp1 = *rotvel * o_t1; |
| 3200 | ConvertEulerToAxisAmount(&cmp1, &n1, &temp1); |
| 3201 | |
| 3202 | n1 *= temp1; |
| 3203 | |
| 3204 | if (temp1 != 0.0f) { |
| 3205 | vm_CrossProduct(&w1, &n1, &r1); |
| 3206 | } else { |
| 3207 | w1 = Zero_vector; |
| 3208 | } |
| 3209 | |
| 3210 | return *velocity + w1; |
| 3211 | } |
| 3212 | |
| 3213 | // MTS: only used in this file. |
| 3214 | // Hacked for some initial testing |
| 3215 | bool BBoxPlaneIntersection(bool fast_exit, vector *collision_point, vector *collision_normal, object *obj, |
| 3216 | vector *new_pos, int nv, vector **vertex_ptr_list, vector *face_normal, matrix *orient, |
| 3217 | vector *rotvel, vector *velocity) { |
| 3218 | vector plane_pnt; |
| 3219 | vector verts[12]; |
| 3220 | poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num]; |
| 3221 | int i; |
| 3222 | vector rel[32]; |
| 3223 | float dot[32]; |
| 3224 | bool collidable[32]; |
| 3225 | |
| 3226 | int num_int_box = 0; |
| 3227 | vector int_points_box[12]; |
| 3228 | |
| 3229 | verts[0] = (orient->rvec * pm->mins.x) + (orient->uvec * pm->maxs.y) + (orient->fvec * pm->mins.z); |
| 3230 | |
| 3231 | verts[1] = (orient->rvec * pm->mins.x) + (orient->uvec * pm->mins.y) + (orient->fvec * pm->mins.z); |
| 3232 | |
| 3233 | verts[2] = (orient->rvec * pm->maxs.x) + (orient->uvec * pm->mins.y) + (orient->fvec * pm->mins.z); |
| 3234 | |
| 3235 | verts[3] = (orient->rvec * pm->maxs.x) + (orient->uvec * pm->maxs.y) + (orient->fvec * pm->mins.z); |
| 3236 | |
| 3237 | verts[4] = (orient->rvec * pm->maxs.x) + (orient->uvec * pm->maxs.y) + (orient->fvec * pm->maxs.z); |
| 3238 | |
| 3239 | verts[5] = (orient->rvec * pm->maxs.x) + (orient->uvec * pm->mins.y) + (orient->fvec * pm->maxs.z); |
| 3240 | |
| 3241 | verts[6] = (orient->rvec * pm->mins.x) + (orient->uvec * pm->mins.y) + (orient->fvec * pm->maxs.z); |
| 3242 | |
| 3243 | verts[7] = (orient->rvec * pm->mins.x) + (orient->uvec * pm->maxs.y) + (orient->fvec * pm->maxs.z); |
| 3244 | |
| 3245 | for (i = 0; i < 8; i++) { |
| 3246 | verts[i] += *new_pos; |
| 3247 | } |
| 3248 | |
| 3249 | vector xxx_normal = Zero_vector; |
| 3250 | xxx_normal.y = 1.0f; |
| 3251 | |
| 3252 | for (i = 0; i < 8; i++) { |
| 3253 | rel[i] = verts[i]; |
| 3254 | dot[i] = rel[i] * (xxx_normal); |
| 3255 | } |
| 3256 | |
| 3257 | for (i = 0; i < 8; i++) { |
| 3258 | collidable[i] = |
| 3259 | true; //(PointSpeed(&Objects[fvi_query_ptr->thisobjnum], &verts[i], orient, rotvel, velocity).y < 0.0); |
| 3260 | } |
| 3261 | |
| 3262 | // Get Ground intersection points |
| 3263 | for (i = 0; i < 12; i++) { |
| 3264 | vector movement; |
| 3265 | bool found; |
| 3266 | float nmovement; |
| 3267 | |
| 3268 | found = false; |
| 3269 | |
| 3270 | if (dot[bbox_edges[i][0]] > 0.0f && dot[bbox_edges[i][1]] < 0.0f && collidable[bbox_edges[i][1]]) { |
| 3271 | movement = verts[bbox_edges[i][1]] - verts[bbox_edges[i][0]]; |
| 3272 | |
| 3273 | nmovement = -(movement * *face_normal); |
| 3274 | |
| 3275 | plane_pnt = verts[bbox_edges[i][0]]; |
| 3276 | plane_pnt += movement * (dot[bbox_edges[i][0]] / nmovement); |
| 3277 | |
| 3278 | found = true; |
| 3279 | } else if (dot[bbox_edges[i][0]] < 0.0f && dot[bbox_edges[i][1]] > 0.0f && collidable[bbox_edges[i][0]]) { |
| 3280 | movement = verts[bbox_edges[i][0]] - verts[bbox_edges[i][1]]; |
| 3281 | |
| 3282 | nmovement = -(movement * *face_normal); |
| 3283 | |
| 3284 | plane_pnt = verts[bbox_edges[i][1]]; |
| 3285 | plane_pnt += movement * (dot[bbox_edges[i][1]] / nmovement); |
| 3286 | |
| 3287 | found = true; |
| 3288 | } |
| 3289 | |
| 3290 | if (found) { |
| 3291 | if (fast_exit) |
| 3292 | return true; |
| 3293 | |
| 3294 | int_points_box[num_int_box++] = plane_pnt; |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | if (num_int_box) { |
| 3299 | vm_MakeZero(collision_point); |
| 3300 | |
| 3301 | for (i = 0; i < num_int_box; i++) { |
| 3302 | *collision_point += int_points_box[i]; |
| 3303 | } |
| 3304 | |
| 3305 | *collision_point /= (float)(num_int_box); |
| 3306 | *collision_normal = xxx_normal; |
| 3307 | |
| 3308 | // mprintf(0, "BBox %d hits\n", num_int_box); |
| 3309 | |
| 3310 | return true; |
| 3311 | } |
| 3312 | |
| 3313 | return false; |
| 3314 | } |
| 3315 | |
| 3316 | // Returns true if the object is through any walls |
| 3317 | int object_intersects_wall(object *objp) { |
| 3318 | // n_segs_visited = 0; |
| 3319 | // |
| 3320 | // return sphere_intersects_wall(&objp->pos,objp->segnum,objp->size); |
| 3321 | ASSERT(0); |
| 3322 | return 0; |
| 3323 | } |
| 3324 | |
| 3325 | void check_hit_obj(int objnum) { |
| 3326 | vector hit_point; |
| 3327 | float cur_dist; |
| 3328 | const object *obj = &Objects[objnum]; |
| 3329 | bool f_x = false; |
| 3330 | int collision_type; |
| 3331 | int m_obj_index = fvi_query_ptr->thisobjnum; |
| 3332 | object *m_obj = &Objects[m_obj_index]; |
| 3333 | |
| 3334 | if (!(fvi_query_ptr->flags & FQ_CHECK_OBJS) && (obj->type != OBJ_ROOM)) |
| 3335 | return; |
| 3336 | if ((fvi_query_ptr->flags & (FQ_IGNORE_EXTERNAL_ROOMS)) && (obj->type == OBJ_ROOM)) |
| 3337 | return; |
| 3338 | |
| 3339 | if (obj->flags & OF_NO_OBJECT_COLLISIONS) |
| 3340 | return; |
| 3341 | |
| 3342 | if (!(obj->flags & OF_DEAD)) { |
| 3343 | if (m_obj_index != objnum) { |
| 3344 | if (!((m_obj_index > -1) && ((collision_type = CollisionResult[m_obj->type][obj->type]) == RESULT_NOTHING) && |
| 3345 | (CollisionResult[obj->type][m_obj->type] == RESULT_NOTHING))) { |
| 3346 | // Account for ray casting |
| 3347 | if (m_obj_index <= -1 && (CollisionRayResult[obj->type] == RESULT_NOTHING)) |
| 3348 | return; |
| 3349 | |
| 3350 | if (object_movement_AABB(&Objects[objnum])) { |
| 3351 | #ifndef NED_PHYSICS |
| 3352 | #ifdef _DEBUG |
| 3353 | if (Physics_player_verbose) { |
| 3354 | if (OBJNUM(Player_object) == objnum && m_obj_index != -1) { |
| 3355 | mprintf(0, "FVI: %d AABB with player\n", m_obj_index); |
| 3356 | } else if (Player_object == m_obj) { |
| 3357 | mprintf(0, "FVI: Player AABB with %d\n", objnum); |
| 3358 | } |
| 3359 | } |
| 3360 | #endif |
| 3361 | #endif |
| 3362 | |
| 3363 | if (fvi_query_ptr->ignore_obj_list == nullptr || !obj_in_list(objnum, fvi_query_ptr->ignore_obj_list)) { |
| 3364 | if (!ObjectsAreRelated(objnum, m_obj_index)) { |
| 3365 | |
| 3366 | if (m_obj_index < 0) { |
| 3367 | switch (obj->type) { |
| 3368 | case OBJ_ROOM: |
| 3369 | if (fvi_query_ptr->flags & FQ_EXTERNAL_ROOMS_AS_SPHERE) |
| 3370 | collision_type = RESULT_CHECK_SPHERE_SPHERE; |
| 3371 | else |
| 3372 | collision_type = RESULT_CHECK_SPHERE_ROOM; |
| 3373 | break; |
| 3374 | case OBJ_PLAYER: |
| 3375 | if (fvi_query_ptr->flags & FQ_PLAYERS_AS_SPHERE) |
| 3376 | collision_type = RESULT_CHECK_SPHERE_SPHERE; |
| 3377 | else |
| 3378 | collision_type = RESULT_CHECK_SPHERE_POLY; |
| 3379 | break; |
| 3380 | case OBJ_ROBOT: |
| 3381 | if (fvi_query_ptr->flags & FQ_ROBOTS_AS_SPHERE) |
| 3382 | collision_type = RESULT_CHECK_SPHERE_SPHERE; |
| 3383 | else |
| 3384 | collision_type = RESULT_CHECK_SPHERE_POLY; |
| 3385 | break; |
| 3386 | |
| 3387 | default: |
| 3388 | collision_type = RESULT_CHECK_SPHERE_POLY; |
| 3389 | break; |
| 3390 | } |
| 3391 | } else { |
| 3392 | if (((m_obj->type == OBJ_CLUTTER) && (m_obj->mtype.phys_info.flags & PF_GRAVITY) && |
| 3393 | (m_obj->movement_type == MT_PHYSICS) && (obj->type == OBJ_PLAYER)) || |
| 3394 | ((obj->type == OBJ_CLUTTER) && (obj->mtype.phys_info.flags & PF_GRAVITY) && |
| 3395 | (obj->movement_type == MT_PHYSICS) && (m_obj->type == OBJ_PLAYER))) { |
| 3396 | collision_type = RESULT_CHECK_SPHERE_SPHERE; |
| 3397 | } |
| 3398 | |
| 3399 | // Ignore robot collisions if it is specified |
| 3400 | if ((m_obj->mtype.phys_info.flags & PF_NO_SAME_COLLISIONS) && (obj->type == m_obj->type) && |
| 3401 | (obj->id == m_obj->id)) |
| 3402 | return; |
| 3403 | |
| 3404 | // Ignore robot collisions if it is specified |
| 3405 | if ((m_obj->mtype.phys_info.flags & PF_NO_ROBOT_COLLISIONS) && |
| 3406 | (obj->type == OBJ_CLUTTER || obj->type == OBJ_ROBOT || (obj->type == OBJ_BUILDING && obj->ai_info))) |
| 3407 | return; |
| 3408 | |
| 3409 | if ((obj->mtype.phys_info.flags & PF_NO_ROBOT_COLLISIONS) && |
| 3410 | (obj->type == OBJ_CLUTTER || m_obj->type == OBJ_ROBOT || |
| 3411 | (m_obj->type == OBJ_BUILDING && m_obj->ai_info))) |
| 3412 | return; |
| 3413 | |
| 3414 | if ((m_obj->mtype.phys_info.flags & PF_NO_DOOR_COLLISIONS)) { |
| 3415 | if (obj->movement_type != MT_PHYSICS && obj->movement_type != MT_WALKING) { |
| 3416 | return; |
| 3417 | } |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | if (fvi_query_ptr->flags & FQ_IGNORE_NON_LIGHTMAP_OBJECTS) |
| 3422 | if (obj->lighting_render_type != LRT_LIGHTMAPS && obj->type != OBJ_ROOM) |
| 3423 | return; |
| 3424 | |
| 3425 | if (fvi_query_ptr->flags & FQ_IGNORE_POWERUPS) |
| 3426 | if (obj->type == OBJ_POWERUP) |
| 3427 | return; |
| 3428 | |
| 3429 | if (fvi_query_ptr->flags & FQ_IGNORE_WEAPONS) |
| 3430 | if (obj->type == OBJ_WEAPON || obj->type == OBJ_FIREBALL || obj->type == OBJ_SHARD || |
| 3431 | obj->type == OBJ_SHOCKWAVE) |
| 3432 | return; |
| 3433 | |
| 3434 | if (fvi_query_ptr->flags & FQ_IGNORE_MOVING_OBJECTS) |
| 3435 | if (obj->movement_type == MT_PHYSICS || obj->movement_type == MT_WALKING) |
| 3436 | return; |
| 3437 | |
| 3438 | if (obj->type != OBJ_ROOM) { |
| 3439 | if (fvi_query_ptr->flags & FQ_ONLY_PLAYER_OBJ) |
| 3440 | if (obj->type != OBJ_PLAYER) |
| 3441 | return; |
| 3442 | |
| 3443 | if (fvi_query_ptr->flags & FQ_ONLY_DOOR_OBJ) |
| 3444 | if (obj->type != OBJ_DOOR) |
| 3445 | return; |
| 3446 | } |
| 3447 | |
| 3448 | if (obj->type == OBJ_PLAYER && (fvi_query_ptr->flags & FQ_PLAYERS_AS_SPHERE)) |
| 3449 | collision_type = RESULT_CHECK_SPHERE_SPHERE; |
| 3450 | if (obj->type == OBJ_ROBOT && (fvi_query_ptr->flags & FQ_ROBOTS_AS_SPHERE)) |
| 3451 | collision_type = RESULT_CHECK_SPHERE_SPHERE; |
| 3452 | |
| 3453 | switch (collision_type) { |
| 3454 | case RESULT_CHECK_BBOX_ROOM: |
| 3455 | case RESULT_CHECK_SPHERE_ROOM: { |
| 3456 | ASSERT(obj->type == OBJ_ROOM); |
| 3457 | |
| 3458 | fvi_room(obj->id, -1, objnum); |
| 3459 | } break; |
| 3460 | |
| 3461 | case RESULT_CHECK_SPHERE_POLY: |
| 3462 | case RESULT_CHECK_BBOX_POLY: { |
| 3463 | // float dist; |
| 3464 | // vector pos; |
| 3465 | // float size; |
| 3466 | |
| 3467 | if (!(obj->flags & OF_POLYGON_OBJECT)) |
| 3468 | goto sphere_sphere; |
| 3469 | |
| 3470 | // pos = obj->pos + |
| 3471 | // obj->anim_sphere_offset; dist = |
| 3472 | // vm_VectorDistance(&pos, &fvi_anim_sphere_p0); |
| 3473 | // size = Poly_models[obj->rtype.pobj_info.model_num].anim_size; |
| 3474 | |
| 3475 | // if((dist <= size + |
| 3476 | // fvi_anim_sphere_rad) |
| 3477 | // || |
| 3478 | // check_vector_to_object(&hit_point, &cur_dist, &fvi_anim_sphere_p0,&fvi_anim_sphere_p1, |
| 3479 | // fvi_anim_sphere_rad, &Objects[objnum], &Objects[fvi_query_ptr->thisobjnum]) |
| 3480 | // { |
| 3481 | fvi_curobj = objnum; |
| 3482 | fvi_moveobj = m_obj_index; |
| 3483 | if (PolyCollideObject(&Objects[objnum])) { |
| 3484 | compute_movement_AABB(); |
| 3485 | } |
| 3486 | // } |
| 3487 | } break; |
| 3488 | |
| 3489 | case RESULT_CHECK_POLY_SPHERE: |
| 3490 | case RESULT_CHECK_POLY_BBOX: { |
| 3491 | if (!(m_obj->flags & OF_POLYGON_OBJECT)) |
| 3492 | goto sphere_sphere; |
| 3493 | |
| 3494 | // Save the Fvi information pointers. |
| 3495 | fvi_info *temp_fvi_hit_data_ptr = fvi_hit_data_ptr; |
| 3496 | fvi_query *temp_fvi_query_ptr = fvi_query_ptr; |
| 3497 | fvi_info hit_info; |
| 3498 | fvi_query fq; |
| 3499 | float saved_dist = fvi_collision_dist; |
| 3500 | |
| 3501 | vector relative_pos = obj->pos + (*fvi_query_ptr->p0 - *fvi_query_ptr->p1); |
| 3502 | |
| 3503 | fvi_hit_data_ptr = &hit_info; |
| 3504 | fvi_query_ptr = &fq; |
| 3505 | |
| 3506 | fq.p0 = &Objects[objnum].pos; |
| 3507 | fq.p1 = &relative_pos; |
| 3508 | fq.rad = obj->size; |
| 3509 | fq.thisobjnum = objnum; |
| 3510 | fq.flags = 0; |
| 3511 | |
| 3512 | hit_info.num_hits = 0; |
| 3513 | hit_info.hit_type[0] = HIT_NONE; |
| 3514 | |
| 3515 | fvi_curobj = m_obj_index; |
| 3516 | fvi_moveobj = objnum; |
| 3517 | PolyCollideObject(&Objects[temp_fvi_query_ptr->thisobjnum]); |
| 3518 | |
| 3519 | fvi_hit_data_ptr = temp_fvi_hit_data_ptr; |
| 3520 | fvi_query_ptr = temp_fvi_query_ptr; |
| 3521 | |
| 3522 | if (hit_info.hit_type[0] != HIT_NONE) { |
| 3523 | int counter; |
| 3524 | |
| 3525 | if (saved_dist != fvi_collision_dist) { |
| 3526 | fvi_hit_data_ptr->hit_pnt = m_obj->pos + (obj->pos - hit_info.hit_pnt); |
| 3527 | compute_movement_AABB(); |
| 3528 | } |
| 3529 | |
| 3530 | fvi_hit_data_ptr->num_hits = 0; |
| 3531 | |
| 3532 | for (counter = 0; counter < hit_info.num_hits; counter++) { |
| 3533 | fvi_hit_data_ptr->hit_type[fvi_hit_data_ptr->num_hits] = HIT_OBJECT; |
| 3534 | fvi_hit_data_ptr->hit_object[fvi_hit_data_ptr->num_hits] = objnum; |
| 3535 | fvi_hit_data_ptr->hit_face_pnt[fvi_hit_data_ptr->num_hits] = |
| 3536 | fvi_hit_data_ptr->hit_pnt + (hit_info.hit_face_pnt[counter] - m_obj->pos); |
| 3537 | fvi_hit_data_ptr->hit_wallnorm[fvi_hit_data_ptr->num_hits] = -hit_info.hit_wallnorm[counter]; |
| 3538 | fvi_hit_data_ptr->hit_subobject[0] = hit_info.hit_subobject[0]; |
| 3539 | fvi_hit_data_ptr->hit_face[0] = hit_info.hit_face[0]; |
| 3540 | fvi_hit_data_ptr->hit_subobj_fvec = hit_info.hit_subobj_fvec; |
| 3541 | fvi_hit_data_ptr->hit_subobj_uvec = hit_info.hit_subobj_uvec; |
| 3542 | fvi_hit_data_ptr->hit_subobj_pos = hit_info.hit_subobj_pos; |
| 3543 | |
| 3544 | fvi_hit_data_ptr->num_hits++; |
| 3545 | } |
| 3546 | |
| 3547 | ASSERT(!(fvi_hit_data_ptr->num_hits > 1 && !(fvi_query_ptr->flags & FQ_MULTI_POINT))); |
| 3548 | } |
| 3549 | } break; |
| 3550 | |
| 3551 | case RESULT_CHECK_SPHERE_SPHERE: |
| 3552 | case RESULT_CHECK_BBOX_BBOX: |
| 3553 | case RESULT_CHECK_SPHERE_BBOX: |
| 3554 | case RESULT_CHECK_BBOX_SPHERE: { |
| 3555 | sphere_sphere: |
| 3556 | if (check_vector_to_object(&hit_point, &cur_dist, &fvi_anim_sphere_p0, &fvi_anim_sphere_p1, |
| 3557 | fvi_anim_sphere_rad, &Objects[objnum], &Objects[m_obj_index])) { |
| 3558 | // hit_point |
| 3559 | |
| 3560 | if (cur_dist < fvi_collision_dist) { |
| 3561 | |
| 3562 | vector pos_hit; |
| 3563 | float hit_obj_size; |
| 3564 | vector hit_obj_pos; |
| 3565 | |
| 3566 | hit_obj_pos = obj->pos + obj->anim_sphere_offset; |
| 3567 | pos_hit = hit_point - hit_obj_pos; |
| 3568 | |
| 3569 | if ((obj->flags & OF_POLYGON_OBJECT) && obj->type != OBJ_ROOM && obj->type != OBJ_WEAPON && |
| 3570 | obj->type != OBJ_POWERUP && obj->type != OBJ_DEBRIS && obj->type != OBJ_PLAYER) { |
| 3571 | hit_obj_size = Poly_models[obj->rtype.pobj_info.model_num].anim_size; |
| 3572 | } else { |
| 3573 | hit_obj_size = obj->size; |
| 3574 | } |
| 3575 | |
| 3576 | pos_hit = hit_obj_pos + pos_hit * (hit_obj_size / (hit_obj_size + fvi_anim_sphere_rad)); |
| 3577 | ASSERT(hit_obj_size + fvi_anim_sphere_rad > 0.0f); |
| 3578 | |
| 3579 | fvi_collision_dist = cur_dist; |
| 3580 | fvi_hit_data_ptr->hit_pnt = hit_point - fvi_anim_sphere_offset; |
| 3581 | compute_movement_AABB(); |
| 3582 | fvi_hit_data_ptr->num_hits = 1; |
| 3583 | |
| 3584 | fvi_hit_data_ptr->hit_object[0] = objnum; |
| 3585 | fvi_hit_data_ptr->hit_type[0] = HIT_OBJECT; |
| 3586 | fvi_hit_data_ptr->hit_face_pnt[0] = pos_hit; |
| 3587 | fvi_hit_data_ptr->hit_wallnorm[0] = (pos_hit - hit_obj_pos) / hit_obj_size; |
| 3588 | ASSERT(hit_obj_size > 0.0f); |
| 3589 | |
| 3590 | ASSERT(objnum != -1); |
| 3591 | } |
| 3592 | } |
| 3593 | } break; |
| 3594 | |
| 3595 | default: { |
| 3596 | mprintf(0, "Collision of type %d is not programmed yet", collision_type); |
| 3597 | } break; |
| 3598 | } |
| 3599 | } else { |
| 3600 | #ifndef NED_PHYSICS |
| 3601 | #ifdef _DEBUG |
| 3602 | if (Physics_player_verbose) { |
| 3603 | if (OBJNUM(Player_object) == objnum || Player_object == m_obj) { |
| 3604 | mprintf(0, "Related\n"); |
| 3605 | } |
| 3606 | } |
| 3607 | #endif |
| 3608 | #endif |
| 3609 | } |
| 3610 | } else { |
| 3611 | #ifndef NED_PHYSICS |
| 3612 | #ifdef _DEBUG |
| 3613 | if (Physics_player_verbose) { |
| 3614 | if (OBJNUM(Player_object) == objnum || Player_object == m_obj) { |
| 3615 | mprintf(0, "Ignore list\n"); |
| 3616 | } |
| 3617 | } |
| 3618 | #endif |
| 3619 | #endif |
| 3620 | } |
| 3621 | } |
| 3622 | } |
| 3623 | } else { |
| 3624 | #ifndef NED_PHYSICS |
| 3625 | #ifdef _DEBUG |
| 3626 | if (Physics_player_verbose) { |
| 3627 | if (objnum != m_obj_index) { |
| 3628 | if (OBJNUM(Player_object) == objnum || Player_object == m_obj) { |
| 3629 | mprintf(0, "Result nothing %d %d\n", objnum, m_obj_index); |
| 3630 | } |
| 3631 | } |
| 3632 | } |
| 3633 | #endif |
| 3634 | #endif |
| 3635 | } |
| 3636 | } else { |
| 3637 | #ifndef NED_PHYSICS |
| 3638 | #ifdef _DEBUG |
| 3639 | if (Physics_player_verbose) { |
| 3640 | if (OBJNUM(Player_object) == objnum || Player_object == m_obj) { |
| 3641 | mprintf(0, "Dead %d %d\n", objnum, m_obj_index); |
| 3642 | } |
| 3643 | } |
| 3644 | #endif |
| 3645 | #endif |
| 3646 | } |
| 3647 | |
| 3648 | ASSERT(!(fvi_hit_data_ptr->num_hits > 1 && !(fvi_query_ptr->flags & FQ_MULTI_POINT))); |
| 3649 | } |
| 3650 | |
| 3651 | /* // Check each terrain cell |
| 3652 | for(i = 0; i < fvi_num_cells_visited; i++) |
| 3653 | { |
| 3654 | int j_start; |
| 3655 | int j_end; |
| 3656 | |
| 3657 | start_node = GetColTerrainSegFromTerrainSeg(fvi_cells_visited[i]); |
| 3658 | ASSERT(start_node != -1); |
| 3659 | |
| 3660 | if(start_node%COL_TERRAIN_WIDTH) j_start = -1; |
| 3661 | else j_start = 0; |
| 3662 | |
| 3663 | if(start_node%COL_TERRAIN_WIDTH < (COL_TERRAIN_WIDTH - 1)) j_end = 1; |
| 3664 | else j_end = 0; |
| 3665 | |
| 3666 | for(j = j_start; j <= j_end; j++) |
| 3667 | { |
| 3668 | int k_start; |
| 3669 | int k_end; |
| 3670 | |
| 3671 | if(start_node/COL_TERRAIN_WIDTH) k_start = -1; |
| 3672 | else k_start = 0; |
| 3673 | |
| 3674 | if(start_node/COL_TERRAIN_WIDTH < (COL_TERRAIN_DEPTH - 1)) k_end = 1; |
| 3675 | else k_end = 0; |
| 3676 | |
| 3677 | for(k = k_start; k <= k_end; k++) |
| 3678 | { |
| 3679 | int cur_col_node = start_node + j + (k * COL_TERRAIN_WIDTH); |
| 3680 | |
| 3681 | // Only check each col cell once. |
| 3682 | if((fvi_col_terrain_visit_list[cur_col_node >> 3] & (0x01 << (cur_col_node % 8))) == 0) |
| 3683 | { |
| 3684 | ASSERT(fvi_num_col_cells_visited <= MAX_CELLS_VISITED); |
| 3685 | |
| 3686 | fvi_col_terrain_visit_list[cur_col_node >> 3] |= 0x01 << (cur_col_node % 8); |
| 3687 | fvi_col_cells_visited[fvi_num_col_cells_visited] = cur_col_node; |
| 3688 | fvi_num_col_cells_visited++; |
| 3689 | |
| 3690 | for (objnum = Col_terrain_seg[cur_col_node].objects; objnum != -1; objnum = |
| 3691 | Objects[objnum].next) |
| 3692 | { |
| 3693 | if(!(Objects[objnum].flags & OF_BIG_OBJECT)) |
| 3694 | check_hit_obj(objnum); |
| 3695 | } |
| 3696 | } |
| 3697 | } |
| 3698 | } |
| 3699 | } |
| 3700 | */ |
| 3701 | |
| 3702 | #define MAX_BBOX_GROUND_TOLERANCE 0.0001 |
| 3703 | |
| 3704 | /* |
| 3705 | void DoLinearApprox(vector *collision_point, vector *collision_normal, float *hit_dist, float *hit_interval, vector |
| 3706 | *movement_dir, vector *p0, object *obj, int nv, vector **vertex_ptr_list, vector *face_normal) |
| 3707 | { |
| 3708 | vector end_pos; |
| 3709 | bool hit; |
| 3710 | |
| 3711 | *hit_interval /= 2.0f; |
| 3712 | end_pos = *p0 + ((*hit_interval + *hit_dist)* *movement_dir); |
| 3713 | |
| 3714 | float frametime = fvi_query_ptr->frametime * ((*hit_interval + *hit_dist)/vm_VectorDistance(fvi_query_ptr->p0, |
| 3715 | fvi_query_ptr->p1)); matrix orient = *fvi_query_ptr->o_orient; vector rotvel = *fvi_query_ptr->o_rotvel; vector rotforce |
| 3716 | = *fvi_query_ptr->o_rotthrust; angle turnroll = *fvi_query_ptr->o_turnroll; |
| 3717 | //vector thrust = *fvi_query_ptr->o_thrust; |
| 3718 | vector velocity = *fvi_query_ptr->o_velocity; |
| 3719 | //vector movement_pos; |
| 3720 | //vector movement_vec; |
| 3721 | //vector test; |
| 3722 | |
| 3723 | float sim_time_remaining = frametime; |
| 3724 | float old_sim_time_remaining = frametime; |
| 3725 | |
| 3726 | PhysicsDoSimRot(&Objects[fvi_query_ptr->thisobjnum], frametime, &orient, &rotforce, &rotvel, &turnroll); |
| 3727 | // PhysicsDoSimLinear(&Objects[fvi_query_ptr->thisobjnum], &Objects[fvi_query_ptr->thisobjnum].pos, &thrust, |
| 3728 | &velocity, &movement_vec, &movement_pos, frametime); |
| 3729 | |
| 3730 | |
| 3731 | vector moved_vec_n; |
| 3732 | float attempted_dist,actual_dist; |
| 3733 | |
| 3734 | // Save results of this simulation |
| 3735 | old_sim_time_remaining = sim_time_remaining; |
| 3736 | moved_vec_n = end_pos - *p0; |
| 3737 | if(moved_vec_n != Zero_vector) |
| 3738 | { |
| 3739 | actual_dist = vm_NormalizeVector(&moved_vec_n); |
| 3740 | } |
| 3741 | else |
| 3742 | { |
| 3743 | actual_dist = 0.0f; |
| 3744 | } |
| 3745 | |
| 3746 | // Compute more results of this simulation |
| 3747 | attempted_dist = vm_VectorDistance(fvi_query_ptr->p0, fvi_query_ptr->p1); |
| 3748 | sim_time_remaining = sim_time_remaining * ((attempted_dist - actual_dist) / attempted_dist); |
| 3749 | float moved_time = old_sim_time_remaining - sim_time_remaining; |
| 3750 | |
| 3751 | if (sim_time_remaining < 0.0) |
| 3752 | { |
| 3753 | sim_time_remaining = 0.0; |
| 3754 | moved_time = old_sim_time_remaining; |
| 3755 | } |
| 3756 | |
| 3757 | if (sim_time_remaining > old_sim_time_remaining) |
| 3758 | { |
| 3759 | sim_time_remaining = old_sim_time_remaining; |
| 3760 | moved_time = 0.0; |
| 3761 | } |
| 3762 | |
| 3763 | if(old_sim_time_remaining > 0.0) |
| 3764 | { |
| 3765 | velocity = fvi_hit_data_ptr->hit_velocity * (moved_time / old_sim_time_remaining) + velocity * |
| 3766 | (sim_time_remaining / old_sim_time_remaining); |
| 3767 | } |
| 3768 | else |
| 3769 | { |
| 3770 | velocity = fvi_hit_data_ptr->hit_velocity; |
| 3771 | } |
| 3772 | |
| 3773 | hit = BBoxPlaneIntersection(false, collision_point, collision_normal, obj, &end_pos, nv, vertex_ptr_list, |
| 3774 | face_normal, &orient, &rotvel, &velocity); |
| 3775 | |
| 3776 | if(!hit) |
| 3777 | { |
| 3778 | *hit_dist += *hit_interval; |
| 3779 | } |
| 3780 | else |
| 3781 | { |
| 3782 | fvi_hit_data_ptr->hit_pnt = end_pos; |
| 3783 | fvi_hit_data_ptr->hit_turnroll = turnroll; |
| 3784 | fvi_hit_data_ptr->hit_orient = orient; |
| 3785 | fvi_hit_data_ptr->hit_rotvel = rotvel; |
| 3786 | } |
| 3787 | |
| 3788 | if(*hit_interval > MAX_BBOX_GROUND_TOLERANCE) |
| 3789 | DoLinearApprox(collision_point, collision_normal, hit_dist, hit_interval, movement_dir, p0, obj, nv, |
| 3790 | vertex_ptr_list, face_normal); |
| 3791 | |
| 3792 | return; |
| 3793 | } |
| 3794 | */ |
| 3795 | // checks for collisions within a given terrain node (fvi_sub minus the recursiveness). |
| 3796 | // If f_check_local_nodes is set, it will look in surrounding nodes. |
| 3797 | inline void check_terrain_node(int cur_node, bool f_check_local_nodes, bool f_check_ground) { |
| 3798 | vector hit_point; |
| 3799 | float cur_dist; |
| 3800 | int check_x, check_y; |
| 3801 | int tercheck_x, tercheck_y; |
| 3802 | int new_node; |
| 3803 | int xcounter, ycounter; |
| 3804 | int xstart, xend; |
| 3805 | int ystart, yend; |
| 3806 | int terxstart, terxend; |
| 3807 | int terystart, teryend; |
| 3808 | int i; |
| 3809 | int objnum; |
| 3810 | bool f_check_next_ground; |
| 3811 | object *this_obj; |
| 3812 | |
| 3813 | if (fvi_query_ptr->thisobjnum >= 0) |
| 3814 | this_obj = &Objects[fvi_query_ptr->thisobjnum]; |
| 3815 | else |
| 3816 | this_obj = nullptr; |
| 3817 | |
| 3818 | // Object checks |
| 3819 | if ((fvi_terrain_obj_visit_list[cur_node >> 3] & (0x01 << (cur_node % 8))) == 0) { |
| 3820 | ASSERT(cur_node >= 0 && cur_node < TERRAIN_WIDTH * TERRAIN_DEPTH); |
| 3821 | ASSERT(fvi_num_cells_obj_visited < MAX_CELLS_VISITED); |
| 3822 | fvi_terrain_obj_visit_list[cur_node >> 3] |= 0x01 << (cur_node % 8); |
| 3823 | fvi_cells_obj_visited[fvi_num_cells_obj_visited] = cur_node; |
| 3824 | fvi_num_cells_obj_visited++; |
| 3825 | |
| 3826 | if (fvi_query_ptr->flags & FQ_CHECK_OBJS) { |
| 3827 | for (objnum = Terrain_seg[cur_node].objects; objnum != -1; objnum = Objects[objnum].next) { |
| 3828 | ASSERT(objnum != -1); |
| 3829 | if (!(Objects[objnum].flags & OF_BIG_OBJECT)) |
| 3830 | check_hit_obj(objnum); |
| 3831 | } |
| 3832 | } else { |
| 3833 | |
| 3834 | if (!(fvi_query_ptr->flags & FQ_IGNORE_EXTERNAL_ROOMS)) |
| 3835 | for (objnum = Terrain_seg[cur_node].objects; objnum != -1; objnum = Objects[objnum].next) { |
| 3836 | ASSERT(objnum != -1); |
| 3837 | if ((Objects[objnum].type == OBJ_ROOM) && !(Objects[objnum].flags & OF_BIG_OBJECT)) |
| 3838 | check_hit_obj(objnum); |
| 3839 | } |
| 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | // Terrain node checks |
| 3844 | if (f_check_ground) { |
| 3845 | int lod_x = (cur_node % TERRAIN_WIDTH) >> 2; |
| 3846 | int lod_z = (cur_node / TERRAIN_WIDTH) >> 2; |
| 3847 | |
| 3848 | ASSERT(cur_node >= 0 && cur_node < TERRAIN_WIDTH * TERRAIN_DEPTH); |
| 3849 | ASSERT((fvi_terrain_visit_list[cur_node >> 3] & (0x01 << (cur_node % 8))) == 0); |
| 3850 | ASSERT(fvi_num_cells_visited < MAX_CELLS_VISITED); |
| 3851 | |
| 3852 | // Mark the current node as visited |
| 3853 | fvi_terrain_visit_list[cur_node >> 3] |= 0x01 << (cur_node % 8); |
| 3854 | fvi_cells_visited[fvi_num_cells_visited] = cur_node; |
| 3855 | fvi_num_cells_visited++; |
| 3856 | |
| 3857 | if (((float)Terrain_max_height_int[6][lod_z * (TERRAIN_WIDTH >> 2) + lod_x] * TERRAIN_HEIGHT_INCREMENT + |
| 3858 | fvi_query_ptr->rad >= |
| 3859 | fvi_query_ptr->p0->y || |
| 3860 | (float)Terrain_max_height_int[6][lod_z * (TERRAIN_WIDTH >> 2) + lod_x] * TERRAIN_HEIGHT_INCREMENT + |
| 3861 | fvi_query_ptr->rad >= |
| 3862 | fvi_query_ptr->p1->y) && |
| 3863 | !(Terrain_seg[cur_node].flags & TF_INVISIBLE) && |
| 3864 | !(fvi_query_ptr->flags & (FQ_IGNORE_WALLS | FQ_IGNORE_TERRAIN))) { |
| 3865 | |
| 3866 | // check this node for ground collision |
| 3867 | for (i = 0; i < 2; i++) { |
| 3868 | vector face_normal; |
| 3869 | vector *vertex_ptr_list[4]; |
| 3870 | vector vlist[3]; |
| 3871 | int face_hit_type; |
| 3872 | vector wall_norm; |
| 3873 | vector colp; |
| 3874 | |
| 3875 | // There are two triangles per node. Check each of them for collision. |
| 3876 | if (i == 0) { |
| 3877 | int cellnum_upper_right = cur_node + TERRAIN_WIDTH + 1; |
| 3878 | int cellnum_right = cur_node + 1; |
| 3879 | |
| 3880 | vlist[0].x = (cur_node % TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3881 | vlist[0].y = Terrain_seg[cur_node].y; |
| 3882 | vlist[0].z = (cur_node / TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3883 | |
| 3884 | vlist[1].x = (cellnum_upper_right % TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3885 | if (cellnum_upper_right < TERRAIN_WIDTH * TERRAIN_DEPTH) |
| 3886 | vlist[1].y = Terrain_seg[cellnum_upper_right].y; |
| 3887 | else |
| 3888 | vlist[1].y = 0.0; |
| 3889 | vlist[1].z = (cellnum_upper_right / TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3890 | |
| 3891 | vlist[2].x = (cellnum_right % TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3892 | if (cellnum_right < TERRAIN_WIDTH * TERRAIN_DEPTH) |
| 3893 | vlist[2].y = Terrain_seg[cellnum_right].y; |
| 3894 | else |
| 3895 | vlist[2].y = 0.0; |
| 3896 | vlist[2].z = (cellnum_right / TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3897 | |
| 3898 | vertex_ptr_list[0] = &vlist[0]; |
| 3899 | vertex_ptr_list[1] = &vlist[1]; |
| 3900 | vertex_ptr_list[2] = &vlist[2]; |
| 3901 | |
| 3902 | face_normal = TerrainNormals[MAX_TERRAIN_LOD - 1][cur_node].normal2; |
| 3903 | |
| 3904 | } else { |
| 3905 | int cellnum_upper = cur_node + TERRAIN_WIDTH; |
| 3906 | int cellnum_upper_right = cur_node + TERRAIN_WIDTH + 1; |
| 3907 | |
| 3908 | vlist[0].x = (cur_node % TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3909 | vlist[0].y = Terrain_seg[cur_node].y; |
| 3910 | vlist[0].z = (cur_node / TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3911 | |
| 3912 | vlist[1].x = (cellnum_upper % TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3913 | if (cellnum_upper < TERRAIN_WIDTH * TERRAIN_DEPTH) |
| 3914 | vlist[1].y = Terrain_seg[cellnum_upper].y; |
| 3915 | else |
| 3916 | vlist[1].y = 0.0; |
| 3917 | vlist[1].z = (cellnum_upper / TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3918 | |
| 3919 | vlist[2].x = (cellnum_upper_right % TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3920 | if (cellnum_upper_right < TERRAIN_WIDTH * TERRAIN_DEPTH) |
| 3921 | vlist[2].y = Terrain_seg[cellnum_upper_right].y; |
| 3922 | else |
| 3923 | vlist[2].y = 0.0; |
| 3924 | vlist[2].z = (cellnum_upper_right / TERRAIN_WIDTH) * TERRAIN_SIZE; |
| 3925 | |
| 3926 | vertex_ptr_list[0] = &vlist[0]; |
| 3927 | vertex_ptr_list[1] = &vlist[1]; |
| 3928 | vertex_ptr_list[2] = &vlist[2]; |
| 3929 | |
| 3930 | face_normal = TerrainNormals[MAX_TERRAIN_LOD - 1][cur_node].normal1; |
| 3931 | } |
| 3932 | |
| 3933 | // Did we hit this face? |
| 3934 | if ((fvi_query_ptr->thisobjnum >= 0) && |
| 3935 | (Objects[fvi_query_ptr->thisobjnum].mtype.phys_info.flags & PF_POINT_COLLIDE_WALLS)) { |
| 3936 | face_hit_type = check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, fvi_query_ptr->p0, |
| 3937 | &fvi_hit_data_ptr->hit_pnt, &face_normal, vertex_ptr_list, 3, 0.0f); |
| 3938 | } else if ((this_obj) && (this_obj->flags & OF_POLYGON_OBJECT)) { |
| 3939 | face_hit_type = |
| 3940 | check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, &fvi_wall_sphere_p0, &fvi_wall_sphere_p1, |
| 3941 | &face_normal, vertex_ptr_list, 3, fvi_wall_sphere_rad); |
| 3942 | hit_point -= fvi_wall_sphere_offset; |
| 3943 | } else { |
| 3944 | face_hit_type = |
| 3945 | check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, fvi_query_ptr->p0, |
| 3946 | &fvi_hit_data_ptr->hit_pnt, &face_normal, vertex_ptr_list, 3, fvi_query_ptr->rad); |
| 3947 | } |
| 3948 | /* |
| 3949 | if(Objects[fvi_query_ptr->thisobjnum].type == OBJ_CLUTTER) { |
| 3950 | mprintf(0, "Y = %f\n", Objects[fvi_query_ptr->thisobjnum].pos.y); |
| 3951 | } |
| 3952 | */ |
| 3953 | |
| 3954 | // chrisnote - closest hit should be tracked... So, we can call BBPI once with |
| 3955 | // false and all other times with true for fast exit. |
| 3956 | #if 0 |
| 3957 | if (this_obj && this_obj->type == OBJ_CLUTTER) { |
| 3958 | if (!BBoxPlaneIntersection(true, &fvi_hit_data_ptr->hit_face_pnt[0], &fvi_hit_data_ptr->hit_wallnorm[0], |
| 3959 | &Objects[fvi_query_ptr->thisobjnum], fvi_query_ptr->p0, 3, vertex_ptr_list, |
| 3960 | &face_normal, fvi_query_ptr->o_orient, fvi_query_ptr->o_rotvel, |
| 3961 | fvi_query_ptr->o_velocity)) |
| 3962 | ASSERT(1); |
| 3963 | |
| 3964 | if (fvi_hit_data_ptr->hit_type[0] == HIT_NONE && |
| 3965 | BBoxPlaneIntersection(false, &fvi_hit_data_ptr->hit_face_pnt[0], &fvi_hit_data_ptr->hit_wallnorm[0], |
| 3966 | &Objects[fvi_query_ptr->thisobjnum], &fvi_hit_data_ptr->hit_pnt, 3, vertex_ptr_list, |
| 3967 | &face_normal, &fvi_hit_data_ptr->hit_orient, &fvi_hit_data_ptr->hit_rotvel, |
| 3968 | &fvi_hit_data_ptr->hit_velocity)) { |
| 3969 | float hit_dist = 0.0; |
| 3970 | float hit_interval; |
| 3971 | vector movement_dir; |
| 3972 | |
| 3973 | if (!BBoxPlaneIntersection(false, &fvi_hit_data_ptr->hit_face_pnt[0], &fvi_hit_data_ptr->hit_wallnorm[0], |
| 3974 | &Objects[fvi_query_ptr->thisobjnum], fvi_query_ptr->p0, 3, vertex_ptr_list, |
| 3975 | &face_normal, fvi_query_ptr->o_orient, fvi_query_ptr->o_rotvel, |
| 3976 | fvi_query_ptr->o_velocity)) { |
| 3977 | movement_dir = fvi_hit_data_ptr->hit_pnt - *fvi_query_ptr->p0; |
| 3978 | hit_interval = vm_NormalizeVector(&movement_dir); |
| 3979 | |
| 3980 | DoLinearApprox(&fvi_hit_data_ptr->hit_face_pnt[0], &fvi_hit_data_ptr->hit_wallnorm[0], &hit_dist, |
| 3981 | &hit_interval, &movement_dir, fvi_query_ptr->p0, &Objects[fvi_query_ptr->thisobjnum], 3, |
| 3982 | vertex_ptr_list, &face_normal); |
| 3983 | |
| 3984 | fvi_collision_dist = hit_dist; |
| 3985 | } else { |
| 3986 | fvi_hit_data_ptr->hit_orient = *fvi_query_ptr->o_orient; |
| 3987 | fvi_hit_data_ptr->hit_rotvel = *fvi_query_ptr->o_rotvel; |
| 3988 | fvi_hit_data_ptr->hit_turnroll = *fvi_query_ptr->o_turnroll; |
| 3989 | fvi_hit_data_ptr->hit_pnt = *fvi_query_ptr->p0; |
| 3990 | |
| 3991 | fvi_collision_dist = 0.0; |
| 3992 | movement_dir = Zero_vector; |
| 3993 | hit_dist = 0.0f; |
| 3994 | } |
| 3995 | |
| 3996 | fvi_hit_data_ptr->hit_type[0] = HIT_TERRAIN; |
| 3997 | fvi_hit_data_ptr->hit_wallnorm[0].x = 0.0; |
| 3998 | fvi_hit_data_ptr->hit_wallnorm[0].y = 1.0; |
| 3999 | fvi_hit_data_ptr->hit_wallnorm[0].z = 0.0; |
| 4000 | // fvi_hit_data_ptr->hit_seg = -1; -- set in the fvi_FindIntersection function |
| 4001 | // fvi_hit_data_ptr->hit_pnt = *fvi_query_ptr->p0 + hit_dist *movement_dir; |
| 4002 | fvi_hit_data_ptr->hit_face[0] = i; |
| 4003 | fvi_hit_data_ptr->hit_face_room[0] = cur_node; |
| 4004 | // fvi_hit_data_ptr->hit_side_pnt = fvi_hit_data_ptr->hit_pnt; |
| 4005 | // fvi_hit_data_ptr->hit_side_pnt.y = fvi_query_ptr->p0->y - Objects[fvi_query_ptr->thisobjnum].size; |
| 4006 | /* |
| 4007 | if(!BBoxPlaneIntersection(true, |
| 4008 | &fvi_hit_data_ptr->hit_face_pnt[0], &fvi_hit_data_ptr->hit_wallnorm[0], &Objects[fvi_query_ptr->thisobjnum], |
| 4009 | &fvi_hit_data_ptr->hit_pnt, 3, vertex_ptr_list, &face_normal, &fvi_hit_data_ptr->hit_orient, |
| 4010 | &fvi_hit_data_ptr->hit_rotvel, &fvi_hit_data_ptr->hit_rotvel)) |
| 4011 | ASSERT(1); |
| 4012 | */ |
| 4013 | goto ignore_hit; |
| 4014 | } |
| 4015 | } |
| 4016 | #endif |
| 4017 | // If we hit the face... |
| 4018 | if (face_hit_type) { |
| 4019 | if (cur_dist <= fvi_collision_dist) { |
| 4020 | if ((cur_dist < fvi_collision_dist) || !(fvi_query_ptr->flags & FQ_MULTI_POINT)) { |
| 4021 | fvi_hit_data_ptr->num_hits = 0; |
| 4022 | |
| 4023 | fvi_collision_dist = cur_dist; |
| 4024 | fvi_hit_data_ptr->hit_pnt = hit_point; |
| 4025 | compute_movement_AABB(); |
| 4026 | } else if (fvi_hit_data_ptr->num_hits == MAX_HITS) { |
| 4027 | goto ignore_hit; |
| 4028 | } |
| 4029 | |
| 4030 | fvi_hit_data_ptr->hit_object[fvi_hit_data_ptr->num_hits] = -1; |
| 4031 | fvi_hit_data_ptr->hit_type[fvi_hit_data_ptr->num_hits] = HIT_TERRAIN; |
| 4032 | fvi_hit_data_ptr->hit_wallnorm[fvi_hit_data_ptr->num_hits] = wall_norm; |
| 4033 | // fvi_hit_data_ptr->hit_seg = -1; -- set in the fvi_FindIntersection function |
| 4034 | fvi_hit_data_ptr->hit_face[fvi_hit_data_ptr->num_hits] = i; |
| 4035 | fvi_hit_data_ptr->hit_face_room[fvi_hit_data_ptr->num_hits] = (MAKE_ROOMNUM(cur_node)); |
| 4036 | fvi_hit_data_ptr->hit_face_pnt[fvi_hit_data_ptr->num_hits] = colp; |
| 4037 | |
| 4038 | fvi_hit_data_ptr->num_hits++; |
| 4039 | } |
| 4040 | } |
| 4041 | ignore_hit:; |
| 4042 | } |
| 4043 | } |
| 4044 | } |
| 4045 | |
| 4046 | // check local nodes for any collision type, but no recursion for them :) |
| 4047 | if (f_check_local_nodes) { |
| 4048 | int next_y_delta; |
| 4049 | |
| 4050 | // Check worst-case collisions. This includes all nodes within a radius edge of the current node |
| 4051 | tercheck_x = check_x = fvi_query_ptr->rad / TERRAIN_SIZE + 1; |
| 4052 | tercheck_y = check_y = fvi_query_ptr->rad / TERRAIN_SIZE + 1; |
| 4053 | |
| 4054 | if (fvi_query_ptr->flags & FQ_CHECK_OBJS) { |
| 4055 | check_x += CELLS_PER_COL_CELL; |
| 4056 | check_y += CELLS_PER_COL_CELL; |
| 4057 | } |
| 4058 | |
| 4059 | xstart = cur_node % TERRAIN_WIDTH - check_x; |
| 4060 | xend = cur_node % TERRAIN_WIDTH + check_x; |
| 4061 | ystart = cur_node / TERRAIN_WIDTH - check_y; |
| 4062 | yend = cur_node / TERRAIN_WIDTH + check_y; |
| 4063 | |
| 4064 | terxstart = cur_node % TERRAIN_WIDTH - tercheck_x; |
| 4065 | terxend = cur_node % TERRAIN_WIDTH + tercheck_x; |
| 4066 | terystart = cur_node / TERRAIN_WIDTH - tercheck_y; |
| 4067 | teryend = cur_node / TERRAIN_WIDTH + tercheck_y; |
| 4068 | |
| 4069 | if (xstart < 0) |
| 4070 | xstart = 0; |
| 4071 | if (xend >= TERRAIN_WIDTH) |
| 4072 | xend = TERRAIN_WIDTH - 1; |
| 4073 | if (ystart < 0) |
| 4074 | ystart = 0; |
| 4075 | if (yend >= TERRAIN_DEPTH) |
| 4076 | yend = TERRAIN_DEPTH - 1; |
| 4077 | |
| 4078 | // This should be a faster interative why to do a square with center at original position |
| 4079 | new_node = TERRAIN_WIDTH * ystart + xstart; |
| 4080 | next_y_delta = TERRAIN_WIDTH - (xend - xstart) - 1; |
| 4081 | |
| 4082 | for (ycounter = ystart; ycounter <= yend; ycounter++) { |
| 4083 | for (xcounter = xstart; xcounter <= xend; xcounter++) { |
| 4084 | |
| 4085 | if ((fvi_terrain_visit_list[new_node >> 3] & (0x01 << (new_node % 8))) == 0) { |
| 4086 | |
| 4087 | f_check_next_ground = false; |
| 4088 | |
| 4089 | if (ycounter >= terystart && ycounter <= teryend) { |
| 4090 | if ((!f_check_next_ground) && xcounter >= terxstart && xcounter <= terxend) { |
| 4091 | f_check_next_ground = true; |
| 4092 | } |
| 4093 | } |
| 4094 | |
| 4095 | check_terrain_node(new_node, false, f_check_next_ground); |
| 4096 | } |
| 4097 | |
| 4098 | new_node += 1; |
| 4099 | } |
| 4100 | |
| 4101 | new_node += next_y_delta; |
| 4102 | } |
| 4103 | } |
| 4104 | |
| 4105 | return; |
| 4106 | } |
| 4107 | |
| 4108 | /* |
| 4109 | inline void check_square_node(int x, int y, int width) |
| 4110 | { |
| 4111 | // check local nodes for any collision type, but no recursion for them :) |
| 4112 | int next_y_delta; |
| 4113 | |
| 4114 | // Check worst-case collisions. This includes all nodes within a radius edge of the current node |
| 4115 | tercheck_x = check_x = fvi_query_ptr->rad/TERRAIN_SIZE + 1; |
| 4116 | tercheck_y = check_y = fvi_query_ptr->rad/TERRAIN_SIZE + 1; |
| 4117 | |
| 4118 | if(fvi_query_ptr->flags & FQ_CHECK_OBJS) |
| 4119 | { |
| 4120 | check_x += CELLS_PER_COL_CELL; |
| 4121 | check_y += CELLS_PER_COL_CELL; |
| 4122 | } |
| 4123 | |
| 4124 | xstart = cur_node%TERRAIN_WIDTH - check_x; |
| 4125 | xend = cur_node%TERRAIN_WIDTH + check_x; |
| 4126 | ystart = cur_node/TERRAIN_WIDTH - check_y; |
| 4127 | yend = cur_node/TERRAIN_WIDTH + check_y; |
| 4128 | |
| 4129 | terxstart = cur_node%TERRAIN_WIDTH - tercheck_x; |
| 4130 | terxend = cur_node%TERRAIN_WIDTH + tercheck_x; |
| 4131 | terystart = cur_node/TERRAIN_WIDTH - tercheck_y; |
| 4132 | teryend = cur_node/TERRAIN_WIDTH + tercheck_y; |
| 4133 | |
| 4134 | if(xstart < 0) xstart = 0; |
| 4135 | if(xend >= TERRAIN_WIDTH) xend = TERRAIN_WIDTH - 1; |
| 4136 | if(ystart < 0) ystart = 0; |
| 4137 | if(yend >= TERRAIN_DEPTH) yend = TERRAIN_DEPTH - 1; |
| 4138 | |
| 4139 | // This should be a faster interative why to do a square with center at original position |
| 4140 | new_node = TERRAIN_WIDTH * ystart + xstart; |
| 4141 | next_y_delta = TERRAIN_WIDTH - (xend - xstart) - 1; |
| 4142 | |
| 4143 | for(ycounter = ystart; ycounter <= yend; ycounter++) |
| 4144 | { |
| 4145 | for(xcounter = xstart; xcounter <= xend; xcounter++) |
| 4146 | { |
| 4147 | |
| 4148 | if((fvi_terrain_visit_list[new_node >> 3] & (0x01 << (new_node % 8))) == 0) |
| 4149 | { |
| 4150 | |
| 4151 | f_check_next_ground = false; |
| 4152 | |
| 4153 | if(ycounter >= terystart && ycounter <= teryend) |
| 4154 | { |
| 4155 | if((!f_check_next_ground) && xcounter >= terxstart && xcounter <= |
| 4156 | terxend) |
| 4157 | { |
| 4158 | f_check_next_ground = true; |
| 4159 | } |
| 4160 | } |
| 4161 | |
| 4162 | // if(!((fvi_terrain_obj_visit_list[new_node >> 3] & (0x01 << (new_node % 8))) != |
| 4163 | 0)) if(!((fvi_terrain_visit_list[new_node >> 3] & (0x01 << (new_node % 8))) != 0)) check_terrain_node(new_node, false, |
| 4164 | f_check_next_ground); |
| 4165 | } |
| 4166 | |
| 4167 | new_node += 1; |
| 4168 | } |
| 4169 | |
| 4170 | new_node += next_y_delta; |
| 4171 | } |
| 4172 | } |
| 4173 | } |
| 4174 | */ |
| 4175 | |
| 4176 | int do_fvi_terrain() { |
| 4177 | int x1, x2, y1, y2, x, y, delta_y, delta_x, change_x, change_y, length, cur_node, error_term, i; |
| 4178 | |
| 4179 | int new_x, new_y; |
| 4180 | int counter; |
| 4181 | int delta_ter_check = fvi_query_ptr->rad / TERRAIN_SIZE + 1; |
| 4182 | int delta_check = fvi_query_ptr->rad / TERRAIN_SIZE + CELLS_PER_COL_CELL + 1; |
| 4183 | |
| 4184 | // This is the only time we will check the terrain |
| 4185 | f_check_terrain = false; |
| 4186 | |
| 4187 | // We need to know the endpoint |
| 4188 | fvi_hit_data_ptr->hit_room = GetTerrainRoomFromPos(&fvi_hit_data_ptr->hit_pnt); |
| 4189 | |
| 4190 | // End point is out of bounds, so clip it. |
| 4191 | if (fvi_hit_data_ptr->hit_room == -1) { |
| 4192 | float delta = 1.0; |
| 4193 | vector movement = fvi_hit_data_ptr->hit_pnt - *fvi_query_ptr->p0; |
| 4194 | |
| 4195 | if (fvi_hit_data_ptr->hit_pnt.x < (fvi_query_ptr->rad + 0.000001)) { |
| 4196 | delta = (fvi_query_ptr->p0->x - (fvi_query_ptr->rad + 0.000001)) / (-movement.x); |
| 4197 | } else if (fvi_hit_data_ptr->hit_pnt.x > |
| 4198 | (float)((TERRAIN_WIDTH - 1) * TERRAIN_SIZE) - (fvi_query_ptr->rad + 0.000001)) { |
| 4199 | delta = ((float)((TERRAIN_WIDTH - 1) * TERRAIN_SIZE) - (fvi_query_ptr->rad + 0.000001) - fvi_query_ptr->p0->x) / |
| 4200 | (movement.x); |
| 4201 | } |
| 4202 | |
| 4203 | if (fvi_hit_data_ptr->hit_pnt.z < (fvi_query_ptr->rad + 0.000001)) { |
| 4204 | if ((fvi_query_ptr->p0->z - (fvi_query_ptr->rad + 0.000001)) / (-movement.z) < delta) |
| 4205 | delta = (fvi_query_ptr->p0->z - (fvi_query_ptr->rad + 0.000001)) / (-movement.z); |
| 4206 | } else if (fvi_hit_data_ptr->hit_pnt.z > |
| 4207 | (float)((TERRAIN_DEPTH - 1) * TERRAIN_SIZE) - (fvi_query_ptr->rad + 0.000001)) { |
| 4208 | if (((float)((TERRAIN_DEPTH - 1) * TERRAIN_SIZE) - (fvi_query_ptr->rad + 0.000001) - fvi_query_ptr->p0->z) / |
| 4209 | (movement.z) < |
| 4210 | delta) |
| 4211 | delta = ((float)((TERRAIN_DEPTH - 1) * TERRAIN_SIZE) - (fvi_query_ptr->rad + 0.000001) - fvi_query_ptr->p0->z) / |
| 4212 | (movement.z); |
| 4213 | } |
| 4214 | |
| 4215 | fvi_hit_data_ptr->hit_pnt = *fvi_query_ptr->p0 + delta * movement; |
| 4216 | fvi_collision_dist = vm_VectorDistance(&fvi_hit_data_ptr->hit_pnt, fvi_query_ptr->p0); |
| 4217 | |
| 4218 | fvi_hit_data_ptr->hit_room = GetTerrainRoomFromPos(&fvi_hit_data_ptr->hit_pnt); |
| 4219 | |
| 4220 | fvi_hit_data_ptr->hit_type[0] = HIT_OUT_OF_TERRAIN_BOUNDS; |
| 4221 | |
| 4222 | compute_movement_AABB(); |
| 4223 | |
| 4224 | if (fvi_hit_data_ptr->hit_room == -1) |
| 4225 | return fvi_hit_data_ptr->hit_type[0]; |
| 4226 | } |
| 4227 | |
| 4228 | // Determine the start end end nodes |
| 4229 | x1 = CELLNUM(fvi_query_ptr->startroom) % TERRAIN_WIDTH; |
| 4230 | y1 = CELLNUM(fvi_query_ptr->startroom) / TERRAIN_WIDTH; |
| 4231 | |
| 4232 | x2 = CELLNUM(fvi_hit_data_ptr->hit_room) % TERRAIN_WIDTH; |
| 4233 | y2 = CELLNUM(fvi_hit_data_ptr->hit_room) / TERRAIN_WIDTH; |
| 4234 | |
| 4235 | x = x1; |
| 4236 | y = y1; |
| 4237 | |
| 4238 | // How many nodes did I move? |
| 4239 | delta_x = x2 - x1; |
| 4240 | delta_y = y2 - y1; |
| 4241 | |
| 4242 | // check the current node for collsions (if we are done, return) |
| 4243 | check_terrain_node(CELLNUM(fvi_query_ptr->startroom), true, true); |
| 4244 | if (delta_x == 0 && delta_y == 0) |
| 4245 | goto check_big_objs; |
| 4246 | |
| 4247 | // check the end node |
| 4248 | cur_node = y1 * TERRAIN_DEPTH + x2; |
| 4249 | check_terrain_node(cur_node, true, (fvi_terrain_visit_list[cur_node >> 3] & (0x01 << (cur_node % 8))) == 0); |
| 4250 | |
| 4251 | // Do a Breshenham line algorithm |
| 4252 | if (delta_x < 0) { |
| 4253 | change_x = -1; |
| 4254 | delta_x = -delta_x; |
| 4255 | |
| 4256 | } else { |
| 4257 | change_x = 1; |
| 4258 | } |
| 4259 | |
| 4260 | if (delta_y < 0) { |
| 4261 | change_y = -1; |
| 4262 | delta_y = -delta_y; |
| 4263 | |
| 4264 | } else { |
| 4265 | change_y = 1; |
| 4266 | } |
| 4267 | |
| 4268 | error_term = 0; |
| 4269 | i = 1; |
| 4270 | |
| 4271 | if (delta_x < delta_y) { |
| 4272 | length = delta_y + 1; |
| 4273 | |
| 4274 | while (i < length) { |
| 4275 | y += change_y; |
| 4276 | error_term += delta_x; |
| 4277 | |
| 4278 | if (error_term >= delta_y) { |
| 4279 | x += change_x; |
| 4280 | error_term -= delta_y; |
| 4281 | } |
| 4282 | |
| 4283 | if (y >= TERRAIN_DEPTH || y < 0 || x < 0 || x >= TERRAIN_WIDTH) { |
| 4284 | fvi_hit_data_ptr->hit_type[0] = HIT_OUT_OF_TERRAIN_BOUNDS; |
| 4285 | goto check_big_objs; |
| 4286 | } |
| 4287 | |
| 4288 | for (counter = -delta_check; counter <= delta_check; counter++) { |
| 4289 | new_x = x + counter; |
| 4290 | |
| 4291 | if (new_x < 0) { |
| 4292 | counter = -x; |
| 4293 | new_x = 0; |
| 4294 | } |
| 4295 | |
| 4296 | if (new_x >= TERRAIN_WIDTH) |
| 4297 | break; |
| 4298 | |
| 4299 | // Check the current node for collisions -- chrishack -- This can be made iterative |
| 4300 | cur_node = y * TERRAIN_DEPTH + new_x; |
| 4301 | |
| 4302 | if ((fvi_terrain_visit_list[cur_node >> 3] & (0x01 << (cur_node % 8))) == 0) { |
| 4303 | if (counter < -delta_ter_check || counter > delta_ter_check) { |
| 4304 | check_terrain_node(cur_node, false, false); |
| 4305 | } else { |
| 4306 | check_terrain_node(cur_node, false, true); |
| 4307 | } |
| 4308 | } |
| 4309 | } |
| 4310 | |
| 4311 | i++; |
| 4312 | } |
| 4313 | |
| 4314 | } else { |
| 4315 | length = delta_x + 1; |
| 4316 | |
| 4317 | while (i < length) { |
| 4318 | x += change_x; |
| 4319 | error_term += delta_y; |
| 4320 | |
| 4321 | if (error_term >= delta_x) { |
| 4322 | y += change_y; |
| 4323 | error_term -= delta_x; |
| 4324 | } |
| 4325 | |
| 4326 | if (y >= TERRAIN_DEPTH || y < 0 || x < 0 || x >= TERRAIN_WIDTH) { |
| 4327 | fvi_hit_data_ptr->hit_type[0] = HIT_OUT_OF_TERRAIN_BOUNDS; |
| 4328 | goto check_big_objs; |
| 4329 | } |
| 4330 | |
| 4331 | for (counter = -delta_check; counter <= delta_check; counter++) { |
| 4332 | new_y = y + counter; |
| 4333 | |
| 4334 | if (new_y < 0) { |
| 4335 | counter = -y; |
| 4336 | new_y = 0; |
| 4337 | } |
| 4338 | if (new_y >= TERRAIN_DEPTH) |
| 4339 | break; |
| 4340 | |
| 4341 | // Check the current node for collisions -- chrishack -- This can be made iterative |
| 4342 | cur_node = new_y * TERRAIN_DEPTH + x; |
| 4343 | |
| 4344 | if ((fvi_terrain_visit_list[cur_node >> 3] & (0x01 << (cur_node % 8))) == 0) { |
| 4345 | if (counter < -delta_ter_check || counter > delta_ter_check) { |
| 4346 | check_terrain_node(cur_node, false, false); |
| 4347 | } else { |
| 4348 | check_terrain_node(cur_node, false, true); |
| 4349 | } |
| 4350 | } |
| 4351 | } |
| 4352 | |
| 4353 | i++; |
| 4354 | } |
| 4355 | } |
| 4356 | |
| 4357 | ASSERT(x == x2 && y == y2); |
| 4358 | |
| 4359 | check_big_objs: // Check Big objects |
| 4360 | if (fvi_query_ptr->flags & FQ_CHECK_OBJS) { |
| 4361 | for (i = 0; i < Num_big_objects; i++) { |
| 4362 | ASSERT(BigObjectList[i] >= 0); |
| 4363 | check_hit_obj(BigObjectList[i]); |
| 4364 | // mprintf(0, "CHecking BIG %d\n", i); |
| 4365 | } |
| 4366 | } else { |
| 4367 | if (!(fvi_query_ptr->flags & FQ_IGNORE_EXTERNAL_ROOMS)) |
| 4368 | for (i = 0; i < Num_big_objects; i++) { |
| 4369 | if (Objects[BigObjectList[i]].type == OBJ_ROOM) |
| 4370 | check_hit_obj(BigObjectList[i]); |
| 4371 | // mprintf(0, "CHecking BIG %d\n", i); |
| 4372 | } |
| 4373 | } |
| 4374 | |
| 4375 | return fvi_hit_data_ptr->hit_type[0]; |
| 4376 | } |
| 4377 | |
| 4378 | // NOTE: CHRIS increased MAX_NEXT_PORTALS because radiosity rays tend to hit more portals |
| 4379 | // than in game stuff. It has no performance effect; so, I should be fine. |
| 4380 | #define MAX_NEXT_PORTALS 50 |
| 4381 | |
| 4382 | void fvi_rooms_objs(void) { |
| 4383 | int objnum; |
| 4384 | int i; |
| 4385 | room *cur_room; |
| 4386 | |
| 4387 | // first, see if vector hit any objects in this segment |
| 4388 | if (!(fvi_query_ptr->flags & FQ_CHECK_OBJS)) |
| 4389 | return; |
| 4390 | |
| 4391 | for (i = 0; i < fvi_num_rooms_visited; i++) { |
| 4392 | cur_room = &Rooms[fvi_rooms_visited[i]]; |
| 4393 | ASSERT((fvi_visit_list[ROOMNUM(cur_room) >> 3] & (0x01 << (ROOMNUM(cur_room) % 8))) != 0); |
| 4394 | ASSERT(ROOMNUM(cur_room) >= 0 && ROOMNUM(cur_room) <= Highest_room_index && cur_room->used); |
| 4395 | |
| 4396 | for (objnum = cur_room->objects; objnum != -1; objnum = Objects[objnum].next) { |
| 4397 | ASSERT(objnum != -1); |
| 4398 | check_hit_obj(objnum); |
| 4399 | } |
| 4400 | } |
| 4401 | } |
| 4402 | |
| 4403 | // Flags for GetFaceAlpha() |
| 4404 | #define FA_CONSTANT 1 // face has a constant alpha for the whole face |
| 4405 | #define FA_VERTEX 2 // face has different alpha per vertex |
| 4406 | #define FA_TRANSPARENT 4 // face has transparency (i.e. per pixel 1-bit alpha) |
| 4407 | |
| 4408 | // Determines if a face draws with alpha blending |
| 4409 | // Parameters: fp - pointer to the face in question |
| 4410 | // bm_handle - the handle for the bitmap for this frame, or -1 if don't care about |
| 4411 | // transparence Returns: bitmask describing the alpha blending for the face |
| 4412 | // the return bits are the ATF_ flags in renderer.h |
| 4413 | inline int GetFaceAlpha(face *fp, int bm_handle) { |
| 4414 | int ret = AT_ALWAYS; |
| 4415 | if (GameTextures[fp->tmap].flags & TF_SATURATE) { |
| 4416 | if (fp->flags & FF_VERTEX_ALPHA) |
| 4417 | ret = AT_SATURATE_TEXTURE_VERTEX; |
| 4418 | else |
| 4419 | ret = AT_SATURATE_TEXTURE; |
| 4420 | } else { |
| 4421 | // Check the face's texture for an alpha value |
| 4422 | if (GameTextures[fp->tmap].alpha < 1.0) |
| 4423 | ret |= ATF_CONSTANT; |
| 4424 | |
| 4425 | // Someday we'll probably check the bitmap's alpha, too |
| 4426 | // Check for vertex alpha flag |
| 4427 | if (fp->flags & FF_VERTEX_ALPHA) |
| 4428 | ret |= ATF_VERTEX; |
| 4429 | |
| 4430 | // Check for transparency |
| 4431 | if (GameBitmaps[bm_handle].format != BITMAP_FORMAT_4444 && GameTextures[fp->tmap].flags & TF_TMAP2) |
| 4432 | ret |= ATF_TEXTURE; |
| 4433 | } |
| 4434 | |
| 4435 | return ret; |
| 4436 | } |
| 4437 | |
| 4438 | bool PhysPastPortal(const room *rp, portal *pp) { |
| 4439 | // If we don't render the portal's faces, then we see through it |
| 4440 | if (!(pp->flags & PF_RENDER_FACES)) |
| 4441 | return 1; |
| 4442 | |
| 4443 | // Check if the face's texture has transparency |
| 4444 | face *fp = &rp->faces[pp->portal_face]; |
| 4445 | int bm_handle = GetTextureBitmap(fp->tmap, 0); |
| 4446 | |
| 4447 | if (GetFaceAlpha(fp, bm_handle)) |
| 4448 | return 1; // Face has alpha or transparency, so we can see through it |
| 4449 | else |
| 4450 | return 0; // Not transparent, so no render past |
| 4451 | } |
| 4452 | |
| 4453 | int fvi_room(int room_index, int from_portal, int room_obj) { |
| 4454 | vector hit_point; // where we hit |
| 4455 | float cur_dist; // distance to hit point |
| 4456 | const room *cur_room = &Rooms[room_index]; |
| 4457 | int16_t i; |
| 4458 | int next_portals[MAX_NEXT_PORTALS]; |
| 4459 | int num_next_portals = 0; |
| 4460 | int next_portal_index; |
| 4461 | int portal_num; |
| 4462 | // vector col_point[32]; |
| 4463 | // vector col_normal[32]; |
| 4464 | int num_cols = 0; |
| 4465 | object *this_obj; |
| 4466 | uint8_t msector = 0; |
| 4467 | |
| 4468 | if (fvi_min_xyz.x <= cur_room->bbf_min_xyz.x) { |
| 4469 | msector |= 0x01; |
| 4470 | } |
| 4471 | if (fvi_min_xyz.y <= cur_room->bbf_min_xyz.y) { |
| 4472 | msector |= 0x02; |
| 4473 | } |
| 4474 | if (fvi_min_xyz.z <= cur_room->bbf_min_xyz.z) { |
| 4475 | msector |= 0x04; |
| 4476 | } |
| 4477 | if (fvi_max_xyz.x >= cur_room->bbf_max_xyz.x) { |
| 4478 | msector |= 0x08; |
| 4479 | } |
| 4480 | if (fvi_max_xyz.y >= cur_room->bbf_max_xyz.y) { |
| 4481 | msector |= 0x10; |
| 4482 | } |
| 4483 | if (fvi_max_xyz.z >= cur_room->bbf_max_xyz.z) { |
| 4484 | msector |= 0x20; |
| 4485 | } |
| 4486 | |
| 4487 | if (fvi_query_ptr->thisobjnum >= 0) |
| 4488 | this_obj = &Objects[fvi_query_ptr->thisobjnum]; |
| 4489 | else |
| 4490 | this_obj = nullptr; |
| 4491 | |
| 4492 | ASSERT(room_index >= 0 && room_index <= Highest_room_index); |
| 4493 | ASSERT(Rooms[room_index].used); |
| 4494 | ASSERT((fvi_visit_list[room_index >> 3] & (0x01 << (room_index % 8))) == 0); |
| 4495 | |
| 4496 | if (!(cur_room->flags & RF_EXTERNAL)) { |
| 4497 | fvi_visit_list[room_index >> 3] |= 0x01 << (room_index % 8); |
| 4498 | fvi_rooms_visited[fvi_num_rooms_visited] = room_index; |
| 4499 | fvi_num_rooms_visited++; |
| 4500 | |
| 4501 | ASSERT(fvi_num_rooms_visited <= MAX_ROOMS); |
| 4502 | } |
| 4503 | |
| 4504 | if (fvi_query_ptr->flags & FQ_IGNORE_WALLS) { |
| 4505 | vector face_normal; |
| 4506 | vector *vertex_ptr_list[MAX_VERTS_PER_FACE]; |
| 4507 | int16_t count; |
| 4508 | bool f_backface; |
| 4509 | int face_info; |
| 4510 | int j; //, k; |
| 4511 | |
| 4512 | for (j = 0; j < cur_room->num_portals; j++) { |
| 4513 | i = cur_room->portals[j].portal_face; |
| 4514 | |
| 4515 | portal_num = cur_room->faces[i].portal_num; |
| 4516 | |
| 4517 | // if ((msector & cur_room->faces[i].sector) != cur_room->faces[i].sector) continue; |
| 4518 | if (!room_movement_AABB(&cur_room->faces[i])) |
| 4519 | continue; |
| 4520 | |
| 4521 | face_info = GetFacePhysicsFlags(cur_room, &cur_room->faces[i]); |
| 4522 | if (face_info == FPT_IGNORE) |
| 4523 | continue; |
| 4524 | |
| 4525 | if (portal_num >= 0 && portal_num == from_portal) |
| 4526 | continue; |
| 4527 | |
| 4528 | f_backface = false; |
| 4529 | |
| 4530 | for (count = 0; count < cur_room->faces[i].num_verts; count++) |
| 4531 | vertex_ptr_list[count] = &cur_room->verts[cur_room->faces[i].face_verts[count]]; |
| 4532 | |
| 4533 | face_normal = cur_room->faces[i].normal; |
| 4534 | |
| 4535 | // Add the portal if we are within a AABB of it. |
| 4536 | if ((face_info & FPF_PORTAL)) { |
| 4537 | // If we can cross a portal, add it to the next portal list if it is not already there |
| 4538 | if (!(face_info & FPF_SOLID) && !(fvi_query_ptr->flags & FQ_SOLID_PORTALS)) { |
| 4539 | bool f_add_next_portal; |
| 4540 | |
| 4541 | f_add_next_portal = true; |
| 4542 | |
| 4543 | for (next_portal_index = 0; next_portal_index < num_next_portals; next_portal_index++) { |
| 4544 | if (next_portals[next_portal_index] == portal_num) |
| 4545 | f_add_next_portal = false; |
| 4546 | } |
| 4547 | |
| 4548 | if (f_add_next_portal) { |
| 4549 | ASSERT(num_next_portals < MAX_NEXT_PORTALS); |
| 4550 | next_portals[num_next_portals++] = portal_num; |
| 4551 | |
| 4552 | if ((fvi_query_ptr->flags & FQ_RECORD) && (face_info & FPF_RECORD)) { |
| 4553 | ASSERT(Fvi_num_recorded_faces < MAX_RECORDED_FACES); |
| 4554 | if (Fvi_num_recorded_faces < MAX_RECORDED_FACES) { |
| 4555 | Fvi_recorded_faces[Fvi_num_recorded_faces].face_index = i; |
| 4556 | Fvi_recorded_faces[Fvi_num_recorded_faces++].room_index = room_index; |
| 4557 | } |
| 4558 | } |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | } |
| 4563 | } else { |
| 4564 | const int16_t num_bbf_regions = cur_room->num_bbf_regions; |
| 4565 | int16_t *num_faces_ptr = cur_room->num_bbf; |
| 4566 | uint8_t *bbf_val = cur_room->bbf_list_sector; |
| 4567 | vector *region_min = cur_room->bbf_list_min_xyz; |
| 4568 | vector *region_max = cur_room->bbf_list_max_xyz; |
| 4569 | int16_t **bbf_list_ptr = cur_room->bbf_list; |
| 4570 | |
| 4571 | // Do the actual wall collsion stuff here! |
| 4572 | for (int test1 = 0; test1 < num_bbf_regions; test1++) { |
| 4573 | if (((*bbf_val) & msector) == (*bbf_val)) { |
| 4574 | if (region_min->x > fvi_wall_max_xyz.x || region_min->y > fvi_wall_max_xyz.y || |
| 4575 | region_min->z > fvi_wall_max_xyz.z || region_max->x < fvi_wall_min_xyz.x || |
| 4576 | region_max->y < fvi_wall_min_xyz.y || region_max->z < fvi_wall_min_xyz.z) |
| 4577 | goto skip_region; |
| 4578 | |
| 4579 | if (fvi_zero_rad && FastVectorBBox((float *)region_min, (float *)region_max, (float *)fvi_query_ptr->p0, |
| 4580 | (float *)&fvi_movement_delta) == false) |
| 4581 | goto skip_region; |
| 4582 | |
| 4583 | int16_t *cur_face_index_ptr = *bbf_list_ptr; |
| 4584 | |
| 4585 | for (int sort_list_cur = 0; sort_list_cur < (*num_faces_ptr); sort_list_cur++) { |
| 4586 | vector face_normal; |
| 4587 | vector *vertex_ptr_list[MAX_VERTS_PER_FACE]; |
| 4588 | int face_hit_type; |
| 4589 | vector wall_norm; |
| 4590 | vector colp; |
| 4591 | int16_t count; |
| 4592 | bool f_backface; |
| 4593 | int face_info; |
| 4594 | face *cur_face; |
| 4595 | |
| 4596 | i = *cur_face_index_ptr; |
| 4597 | cur_face = &cur_room->faces[*cur_face_index_ptr]; |
| 4598 | cur_face_index_ptr++; |
| 4599 | |
| 4600 | const vector *cf_max = &cur_face->max_xyz; |
| 4601 | const vector *cf_min = &cur_face->min_xyz; |
| 4602 | |
| 4603 | if (cf_min->x > fvi_wall_max_xyz.x || cf_min->y > fvi_wall_max_xyz.y || cf_min->z > fvi_wall_max_xyz.z || |
| 4604 | cf_max->x < fvi_wall_min_xyz.x || cf_max->y < fvi_wall_min_xyz.y || cf_max->z < fvi_wall_min_xyz.z) |
| 4605 | continue; |
| 4606 | |
| 4607 | if (fvi_zero_rad && !FastVectorBBox((float *)cf_min, (float *)cf_max, (float *)fvi_query_ptr->p0, |
| 4608 | (float *)&fvi_movement_delta)) |
| 4609 | continue; |
| 4610 | |
| 4611 | portal_num = cur_face->portal_num; |
| 4612 | if (portal_num >= 0 && portal_num == from_portal) |
| 4613 | continue; |
| 4614 | |
| 4615 | face_info = GetFacePhysicsFlags(cur_room, cur_face); |
| 4616 | if (face_info == FPT_IGNORE) |
| 4617 | continue; |
| 4618 | |
| 4619 | f_backface = false; |
| 4620 | |
| 4621 | for (count = 0; count < cur_face->num_verts; count++) |
| 4622 | vertex_ptr_list[count] = &cur_room->verts[cur_face->face_verts[count]]; |
| 4623 | |
| 4624 | face_normal = cur_face->normal; |
| 4625 | |
| 4626 | // Add the portal if we are within a AABB of it. |
| 4627 | if ((face_info & FPF_PORTAL)) { |
| 4628 | if ((fvi_query_ptr->flags & FQ_RECORD) && (face_info & FPF_RECORD)) { |
| 4629 | ASSERT(Fvi_num_recorded_faces < MAX_RECORDED_FACES); |
| 4630 | if (Fvi_num_recorded_faces < MAX_RECORDED_FACES) { |
| 4631 | Fvi_recorded_faces[Fvi_num_recorded_faces].face_index = i; |
| 4632 | Fvi_recorded_faces[Fvi_num_recorded_faces++].room_index = room_index; |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | // If we can cross a portal, add it to the next portal list if it is not already there |
| 4637 | if (!(face_info & FPF_SOLID) && !(fvi_query_ptr->flags & FQ_SOLID_PORTALS)) { |
| 4638 | bool f_add_next_portal = true; |
| 4639 | |
| 4640 | for (next_portal_index = 0; next_portal_index < num_next_portals; next_portal_index++) { |
| 4641 | if (next_portals[next_portal_index] == portal_num) { |
| 4642 | f_add_next_portal = false; |
| 4643 | break; |
| 4644 | } |
| 4645 | } |
| 4646 | |
| 4647 | if (f_add_next_portal) { |
| 4648 | ASSERT(num_next_portals < MAX_NEXT_PORTALS); |
| 4649 | next_portals[num_next_portals++] = portal_num; |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | if ((fvi_query_ptr->flags & FQ_IGNORE_RENDER_THROUGH_PORTALS) && |
| 4654 | (PhysPastPortal(cur_room, &cur_room->portals[portal_num]))) { |
| 4655 | bool f_add_next_portal = true; |
| 4656 | |
| 4657 | for (next_portal_index = 0; next_portal_index < num_next_portals; next_portal_index++) { |
| 4658 | if (next_portals[next_portal_index] == portal_num) { |
| 4659 | f_add_next_portal = false; |
| 4660 | break; |
| 4661 | } |
| 4662 | } |
| 4663 | |
| 4664 | if (f_add_next_portal) { |
| 4665 | ASSERT(num_next_portals < MAX_NEXT_PORTALS); |
| 4666 | next_portals[num_next_portals++] = portal_num; |
| 4667 | } |
| 4668 | |
| 4669 | continue; |
| 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | // Did we hit this face? |
| 4674 | if ((this_obj) && (this_obj->mtype.phys_info.flags & PF_POINT_COLLIDE_WALLS)) { |
| 4675 | face_hit_type = check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, fvi_query_ptr->p0, |
| 4676 | &fvi_hit_data_ptr->hit_pnt, &face_normal, vertex_ptr_list, |
| 4677 | cur_face->num_verts, 0.0f); |
| 4678 | } else if ((this_obj) && (this_obj->flags & OF_POLYGON_OBJECT)) { |
| 4679 | face_hit_type = |
| 4680 | check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, &fvi_wall_sphere_p0, &fvi_wall_sphere_p1, |
| 4681 | &face_normal, vertex_ptr_list, cur_face->num_verts, fvi_wall_sphere_rad); |
| 4682 | hit_point -= fvi_wall_sphere_offset; |
| 4683 | } else { |
| 4684 | face_hit_type = check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, fvi_query_ptr->p0, |
| 4685 | &fvi_hit_data_ptr->hit_pnt, &face_normal, vertex_ptr_list, |
| 4686 | cur_face->num_verts, fvi_query_ptr->rad); |
| 4687 | } |
| 4688 | |
| 4689 | if ((((fvi_query_ptr->flags & FQ_OBJ_BACKFACE) && (cur_room->flags & RF_EXTERNAL)) || |
| 4690 | ((fvi_query_ptr->flags & FQ_BACKFACE) && !(cur_room->flags & RF_EXTERNAL))) && |
| 4691 | (!face_hit_type)) { |
| 4692 | face_normal *= -1.0f; |
| 4693 | for (count = 0; count < cur_face->num_verts; count++) |
| 4694 | vertex_ptr_list[cur_face->num_verts - count - 1] = &cur_room->verts[cur_face->face_verts[count]]; |
| 4695 | |
| 4696 | face_hit_type = check_line_to_face(&hit_point, &colp, &cur_dist, &wall_norm, fvi_query_ptr->p0, |
| 4697 | &fvi_hit_data_ptr->hit_pnt, &face_normal, vertex_ptr_list, |
| 4698 | cur_face->num_verts, fvi_query_ptr->rad); |
| 4699 | f_backface = true; |
| 4700 | } |
| 4701 | |
| 4702 | if (face_hit_type && (face_info & FPF_TRANSPARENT) && (fvi_query_ptr->flags & FQ_TRANSPOINT) && |
| 4703 | CheckTransparentPoint(&colp, cur_room, i)) { |
| 4704 | // Go through the hole |
| 4705 | face_hit_type = HIT_NONE; |
| 4706 | } |
| 4707 | |
| 4708 | // If we hit the face... |
| 4709 | if (face_hit_type) { |
| 4710 | if ((fvi_query_ptr->flags & FQ_RECORD) && (face_info & FPF_RECORD) && |
| 4711 | !(Fvi_recorded_faces[Fvi_num_recorded_faces - 1].face_index == i && |
| 4712 | Fvi_recorded_faces[Fvi_num_recorded_faces - 1].room_index == room_index)) { |
| 4713 | ASSERT(Fvi_num_recorded_faces < MAX_RECORDED_FACES); |
| 4714 | if (Fvi_num_recorded_faces < MAX_RECORDED_FACES) { |
| 4715 | Fvi_recorded_faces[Fvi_num_recorded_faces].face_index = i; |
| 4716 | Fvi_recorded_faces[Fvi_num_recorded_faces++].room_index = room_index; |
| 4717 | } |
| 4718 | } |
| 4719 | |
| 4720 | if (cur_dist <= fvi_collision_dist && (face_info & (FPF_SOLID | FPF_TRANSPARENT))) { |
| 4721 | |
| 4722 | if ((cur_dist < fvi_collision_dist) || !(fvi_query_ptr->flags & FQ_MULTI_POINT)) { |
| 4723 | fvi_hit_data_ptr->num_hits = 0; |
| 4724 | |
| 4725 | fvi_collision_dist = cur_dist; |
| 4726 | fvi_hit_data_ptr->hit_pnt = hit_point; |
| 4727 | compute_movement_AABB(); |
| 4728 | } else if (fvi_hit_data_ptr->num_hits == MAX_HITS) { |
| 4729 | goto ignore_hit; |
| 4730 | } |
| 4731 | |
| 4732 | if (f_backface) |
| 4733 | fvi_hit_data_ptr->hit_type[fvi_hit_data_ptr->num_hits] = HIT_BACKFACE; |
| 4734 | else |
| 4735 | fvi_hit_data_ptr->hit_type[fvi_hit_data_ptr->num_hits] = HIT_WALL; |
| 4736 | |
| 4737 | fvi_hit_data_ptr->hit_wallnorm[fvi_hit_data_ptr->num_hits] = wall_norm; |
| 4738 | // fvi_hit_data_ptr->hit_seg = -1; -- set in the fvi_FindIntersection function |
| 4739 | fvi_hit_data_ptr->hit_object[fvi_hit_data_ptr->num_hits] = room_obj; |
| 4740 | fvi_hit_data_ptr->hit_face[fvi_hit_data_ptr->num_hits] = i; |
| 4741 | fvi_hit_data_ptr->hit_face_room[fvi_hit_data_ptr->num_hits] = room_index; // Segment of the best hit |
| 4742 | fvi_hit_data_ptr->hit_face_pnt[fvi_hit_data_ptr->num_hits] = colp; |
| 4743 | |
| 4744 | fvi_hit_data_ptr->num_hits++; |
| 4745 | } |
| 4746 | } |
| 4747 | } |
| 4748 | } |
| 4749 | skip_region:; |
| 4750 | ignore_hit:; |
| 4751 | num_faces_ptr++; |
| 4752 | bbf_val++; |
| 4753 | region_max++; |
| 4754 | region_min++; |
| 4755 | bbf_list_ptr++; |
| 4756 | } |
| 4757 | } |
| 4758 | // repeated: ; |
| 4759 | |
| 4760 | /* if(num_cols > 1) |
| 4761 | { |
| 4762 | int i; |
| 4763 | vector new_normal; |
| 4764 | float len; |
| 4765 | |
| 4766 | for(i = 0; i < num_cols; i++) |
| 4767 | { |
| 4768 | new_normal += col_normal[i]; |
| 4769 | } |
| 4770 | |
| 4771 | len = vm_NormalizeVector(&new_normal); |
| 4772 | |
| 4773 | if(len == 0.0f) |
| 4774 | { |
| 4775 | mprintf(0, "PHYSICS WARNING: Normals cancal out\n"); |
| 4776 | new_normal = col_normal[0]; |
| 4777 | } |
| 4778 | |
| 4779 | fvi_hit_data_ptr->hit_pnt = fvi_hit_data_ptr->hit_pnt - (fvi_query_ptr->rad * (new_normal)); |
| 4780 | } |
| 4781 | */ |
| 4782 | |
| 4783 | if (!(fvi_query_ptr->flags & FQ_SOLID_PORTALS)) { |
| 4784 | // Accounts for doors that leave a |
| 4785 | for (i = 0; i < cur_room->num_portals; i++) { |
| 4786 | int c_room = cur_room->portals[i].croom; |
| 4787 | |
| 4788 | if ((c_room > 0) && (Rooms[c_room].flags & RF_DOOR)) { |
| 4789 | bool f_add_next_portal = true; |
| 4790 | for (next_portal_index = 0; next_portal_index < num_next_portals; next_portal_index++) { |
| 4791 | if (next_portals[next_portal_index] == i) { |
| 4792 | f_add_next_portal = false; |
| 4793 | break; |
| 4794 | } |
| 4795 | } |
| 4796 | |
| 4797 | if (f_add_next_portal) { |
| 4798 | ASSERT(num_next_portals < MAX_NEXT_PORTALS); |
| 4799 | next_portals[num_next_portals++] = i; |
| 4800 | } |
| 4801 | } |
| 4802 | } |
| 4803 | |
| 4804 | // Check all potentially crossed portals |
| 4805 | for (next_portal_index = 0; next_portal_index < num_next_portals; next_portal_index++) { |
| 4806 | int connect_room; |
| 4807 | |
| 4808 | portal_num = next_portals[next_portal_index]; |
| 4809 | connect_room = cur_room->portals[portal_num].croom; |
| 4810 | |
| 4811 | if (!(Rooms[connect_room].flags & RF_EXTERNAL)) { |
| 4812 | if ((fvi_visit_list[connect_room >> 3] & (0x01 << ((connect_room) % 8))) == 0) { |
| 4813 | /* |
| 4814 | mprintf(0, "A portal %d to room %d,from room %d,with %d cportal\n", |
| 4815 | portal_num, |
| 4816 | cur_room->portals[portal_num].croom, |
| 4817 | room_index, |
| 4818 | cur_room->portals[portal_num].cportal); |
| 4819 | */ |
| 4820 | fvi_room(connect_room, cur_room->portals[portal_num].cportal); |
| 4821 | } |
| 4822 | } else if (f_check_terrain) { |
| 4823 | fvi_info hit_data_terrain = *fvi_hit_data_ptr; |
| 4824 | fvi_query query_terrain = *fvi_query_ptr; |
| 4825 | fvi_info *temp_hit_data = fvi_hit_data_ptr; |
| 4826 | fvi_query *temp_query = fvi_query_ptr; |
| 4827 | |
| 4828 | fvi_hit_data_ptr = &hit_data_terrain; |
| 4829 | fvi_query_ptr = &query_terrain; |
| 4830 | |
| 4831 | query_terrain.startroom = GetTerrainRoomFromPos(query_terrain.p0); |
| 4832 | |
| 4833 | // mprintf(0, "We might go outside\n"); |
| 4834 | |
| 4835 | // This is quick, so do it here. |
| 4836 | hit_data_terrain.hit_room = GetTerrainRoomFromPos(&hit_data_terrain.hit_pnt); |
| 4837 | |
| 4838 | if (hit_data_terrain.hit_room == -1) |
| 4839 | hit_data_terrain.hit_type[0] = HIT_OUT_OF_TERRAIN_BOUNDS; |
| 4840 | |
| 4841 | // Find the hit data!!! |
| 4842 | do_fvi_terrain(); |
| 4843 | |
| 4844 | // This is quick, so do it here. |
| 4845 | hit_data_terrain.hit_room = GetTerrainRoomFromPos(&hit_data_terrain.hit_pnt); |
| 4846 | if (hit_data_terrain.hit_room == -1) |
| 4847 | hit_data_terrain.hit_type[0] = HIT_OUT_OF_TERRAIN_BOUNDS; |
| 4848 | |
| 4849 | // Reset the fvi global pointers to handle in mine stuff. |
| 4850 | fvi_hit_data_ptr = temp_hit_data; |
| 4851 | fvi_query_ptr = temp_query; |
| 4852 | |
| 4853 | // Make sure we register the hit |
| 4854 | if (hit_data_terrain.hit_type[0] != HIT_NONE) |
| 4855 | *fvi_hit_data_ptr = hit_data_terrain; |
| 4856 | } |
| 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | // quit_looking: |
| 4861 | |
| 4862 | ASSERT(!(fvi_hit_data_ptr->hit_type[0] == HIT_OBJECT && fvi_hit_data_ptr->hit_object[0] == -1)); |
| 4863 | |
| 4864 | return fvi_hit_data_ptr->hit_type[0]; |
| 4865 | } |