| 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/damage.cpp $ |
| 21 | * $Revision: 164 $ |
| 22 | * $Date: 3/22/00 12:13p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Applies and handles damage to game entities. |
| 26 | * |
| 27 | * $Log: /DescentIII/main/damage.cpp $ |
| 28 | * |
| 29 | * 164 3/22/00 12:13p Matt |
| 30 | * Added some if statements to not damage & kill objects when playing back |
| 31 | * a demo. |
| 32 | * |
| 33 | * 163 1/26/00 9:19p Jeff |
| 34 | * added support for IntelliVIBE DLL |
| 35 | * |
| 36 | * 162 10/21/99 2:44p Matt |
| 37 | * Mac merge |
| 38 | * |
| 39 | * 161 10/08/99 4:54p Nate |
| 40 | * Fixed bug that prevented custom damage modification from working |
| 41 | * |
| 42 | * 160 10/08/99 4:28p Chris |
| 43 | * Added the forcefield and glass breaking override options |
| 44 | * |
| 45 | * 159 9/22/99 11:58a Jeff |
| 46 | * always call the multiplayer event when player shields change and then |
| 47 | * check to see if the player should be killed |
| 48 | * |
| 49 | * 158 7/26/99 11:07a Jason |
| 50 | * fixed broke glass bug |
| 51 | * |
| 52 | * 157 5/22/99 10:27p Jason |
| 53 | * changes for multiplayer and buildings, ai |
| 54 | * |
| 55 | * 156 5/21/99 4:30a Matt |
| 56 | * Added commented-out code to play sound for sparking death, in case we |
| 57 | * ever get a sound we like. |
| 58 | * |
| 59 | * 155 5/17/99 9:23p Matt |
| 60 | * When applying damage to player, only play invulnerability sound if |
| 61 | * would otherwise be playing a sound. |
| 62 | * |
| 63 | * 154 5/09/99 10:50p Jason |
| 64 | * made napalm rocket burn people longer |
| 65 | * |
| 66 | * 153 5/04/99 6:53p Jeff |
| 67 | * added event for when a player dies |
| 68 | * |
| 69 | * 152 4/30/99 2:32p Matt |
| 70 | * Doubled the velocity of straight-up flying deaths to make Josh's pop |
| 71 | * machines cooler. |
| 72 | * |
| 73 | * 151 4/30/99 3:28a Chris |
| 74 | * Flares disappear when they hit bad surfaces (volatile and lava) |
| 75 | * |
| 76 | * 150 4/29/99 6:48p Matt |
| 77 | * Made volatile surfaces apply damage and lava surfaces catch the player |
| 78 | * on fire. Both now generate the steam puff effect. |
| 79 | * |
| 80 | * 149 4/28/99 4:48a Chris |
| 81 | * Added the no-scale damage by diff checkbox |
| 82 | * |
| 83 | * 148 4/27/99 12:12a Matt |
| 84 | * Added option to make object not tumble in flying death |
| 85 | * |
| 86 | * 147 4/26/99 7:07p Jason |
| 87 | * fixed multiplayer bug with destroy object |
| 88 | * |
| 89 | * 146 4/26/99 6:23p Jeff |
| 90 | * don't apply damage to OBJ_DUMMYs! (It cause problems in multiplayer |
| 91 | * with the guidebot) |
| 92 | * |
| 93 | * 145 4/25/99 10:38p Matt |
| 94 | * Made the Osiris kill object function work on players. |
| 95 | * |
| 96 | * 144 4/25/99 10:19p Matt |
| 97 | * Fixed multiplayer and demo problems will killing an object from script, |
| 98 | * and cleaned up the death code a bit in the process. |
| 99 | * |
| 100 | * 143 4/24/99 11:38p Matt |
| 101 | * Set the death delay for electrical deaths on default-death objects. |
| 102 | * This has apparently been broken since I rewrote the death system ages |
| 103 | * ago, but no one reported it. |
| 104 | * |
| 105 | * 142 4/24/99 4:32p Jason |
| 106 | * made fusion damage sound not be irritating |
| 107 | * |
| 108 | * 141 4/23/99 12:32a Matt |
| 109 | * Added a death info option to play the explosion sound at the start of |
| 110 | * the delay, instead of at the end when the object actually dies. |
| 111 | * |
| 112 | * 140 4/22/99 4:15p Matt |
| 113 | * Don't bash the delay time when setting the flying physics for dying |
| 114 | * objects. For default deaths, this was already set to the same values |
| 115 | * when setting up the death, and for explicit deaths, use the value from |
| 116 | * the page. |
| 117 | * |
| 118 | * 139 4/21/99 3:01p Matt |
| 119 | * Added a new type for dying objects that have AI, instead of keeping a |
| 120 | * flag in the dying info. |
| 121 | * |
| 122 | * 138 4/21/99 12:40p Jason |
| 123 | * made explosion system framerate independant |
| 124 | * |
| 125 | * 137 4/21/99 12:22p Matt |
| 126 | * Added extra time to delay-from-anim time to allow object to finish its |
| 127 | * current anim before starting the death anim. |
| 128 | * |
| 129 | * 136 4/21/99 11:05a Kevin |
| 130 | * new ps_rand and ps_srand to replace rand & srand |
| 131 | * |
| 132 | * 135 4/14/99 2:50a Jeff |
| 133 | * fixed some case mismatched #includes |
| 134 | * |
| 135 | * 134 4/12/99 6:15p Samir |
| 136 | * Sound priorities pass 1 |
| 137 | * |
| 138 | * 133 4/10/99 6:32p Matt |
| 139 | * Don't allow glass to be broken twice. |
| 140 | * |
| 141 | * 132 4/06/99 6:24p Jason |
| 142 | * various fixes for multiplayer |
| 143 | * |
| 144 | * 131 4/06/99 6:02p Matt |
| 145 | * Added score system |
| 146 | * |
| 147 | * 130 4/02/99 11:23a Matt |
| 148 | * Made KillObject not take a death_info struct, but rather the death info |
| 149 | * as individual parameters. Moved death_info into objinfo.h, since it's |
| 150 | * only used for generic objects. Took out fade-away death hack, now that |
| 151 | * fade-away deaths can be explicitely set. |
| 152 | * |
| 153 | * 129 4/01/99 4:28p Matt |
| 154 | * Cleaned up handling of dying flag to prevent infinite recursion. Jeff |
| 155 | * will test. |
| 156 | * |
| 157 | * 128 3/29/99 11:20a Matt |
| 158 | * Added more flexibility to death delays, and added fade away deaths. |
| 159 | * |
| 160 | * 127 3/22/99 1:47p Matt |
| 161 | * Moved breaking glass code from collide.cpp to damage.cpp |
| 162 | * |
| 163 | * 126 3/08/99 5:10p Chris |
| 164 | * Make upsidedown robots fall faster (use the loss anti-grav flag) |
| 165 | * |
| 166 | * 125 3/05/99 6:41p Matt |
| 167 | * In default death, instead of adding 3 second to delay time, calculate |
| 168 | * the time remaining the robot's current animation and add that. |
| 169 | * |
| 170 | * 124 3/05/99 3:32p Matt |
| 171 | * Added hack for making nomads fade out |
| 172 | * |
| 173 | * 123 3/05/99 11:30a Kevin |
| 174 | * Fixed really stupid bug |
| 175 | * |
| 176 | * 122 3/03/99 5:41p Matt |
| 177 | * Don't scale applied damage if damage type is GD_SCRIPTED |
| 178 | * |
| 179 | * 121 2/28/99 6:20p Matt |
| 180 | * Added the ability to set death types for generic objects. |
| 181 | * |
| 182 | * 120 2/26/99 2:03p Matt |
| 183 | * Got blastable doors working, and cleaned up concussive force code. |
| 184 | * |
| 185 | * 119 2/25/99 10:58a Matt |
| 186 | * Added new explosion system. |
| 187 | * |
| 188 | * 118 2/22/99 2:03p Jason |
| 189 | * added different damages for players and generics |
| 190 | * |
| 191 | * 117 2/13/99 6:16p Jeff |
| 192 | * fixed ApplyDamageTo functions so they only damage what objects they are |
| 193 | * supposed to |
| 194 | * |
| 195 | * 116 2/13/99 12:29a Jeff |
| 196 | * don't apply damage to OBJ_DUMMYs |
| 197 | * |
| 198 | * 115 2/10/99 1:47p Matt |
| 199 | * Changed object handle symbolic constants |
| 200 | * |
| 201 | * 114 2/08/99 5:17p Jason |
| 202 | * took out EMD effect and added more weapon options |
| 203 | * |
| 204 | * 113 2/07/99 1:19a Jeff |
| 205 | * added new multiplayer game events EVT_GAMEOBJKILLED and |
| 206 | * EVT_GAMEOBJDESTROYED |
| 207 | * |
| 208 | * 112 2/04/99 2:05p Matt |
| 209 | * Added blastable doors |
| 210 | * |
| 211 | * 111 2/04/99 11:36a Jason |
| 212 | * fixed some multi bugs |
| 213 | * |
| 214 | * 110 2/03/99 12:15p Jason |
| 215 | * added multiplayer vis optimizations |
| 216 | * |
| 217 | * 109 2/02/99 8:43a Chris |
| 218 | * I made buildings with AI work correctly (ie really big robots should be |
| 219 | * buildings) |
| 220 | * anim to and from states are now shorts instead of bytes |
| 221 | * |
| 222 | * 108 2/01/99 4:25p Jeff |
| 223 | * fixed player invulnerabilty after cinematic sequence and death bug |
| 224 | * |
| 225 | * 107 2/01/99 12:55p Jeff |
| 226 | * don't damage player if OF_DESTROYABLE is not set |
| 227 | * |
| 228 | * 106 1/28/99 12:09p Jeff |
| 229 | * added force feedback to player shake...fixed spelling error in define |
| 230 | * for forcefeedback |
| 231 | * |
| 232 | * 105 1/20/99 12:31p Chris |
| 233 | * Include the weapon_handle in evt_damage |
| 234 | * |
| 235 | * 104 1/20/99 2:13a Chris |
| 236 | * It is now possible for robots to have special immunities, resistances, |
| 237 | * and vunerabilities |
| 238 | * |
| 239 | * 103 1/18/99 2:46p Matt |
| 240 | * Combined flags & flags2 fields in object struct |
| 241 | * |
| 242 | * 102 1/17/99 7:20p Jeff |
| 243 | * doh! put back in call to multiplayer event on object damage |
| 244 | * |
| 245 | * 101 1/15/99 3:49a Jeff |
| 246 | * removed calls to multiplayer dll events (EVT_DAMAGED) |
| 247 | * |
| 248 | * 100 1/13/99 2:42p Jason |
| 249 | * added damage event |
| 250 | * |
| 251 | * 99 1/13/99 2:28a Chris |
| 252 | * Massive AI, OSIRIS update |
| 253 | * |
| 254 | * 98 1/08/99 2:55p Samir |
| 255 | * Ripped out OSIRIS1. |
| 256 | * |
| 257 | * 97 1/06/99 3:07p Jason |
| 258 | * toned down redness |
| 259 | * |
| 260 | * 96 12/13/98 7:47p Chris |
| 261 | * Make walkers die slowly more often |
| 262 | * |
| 263 | * 95 12/07/98 2:26p Jason |
| 264 | * fixed a bug with peer to peer damage on the client end |
| 265 | * |
| 266 | * 94 12/04/98 3:03p Jason |
| 267 | * |
| 268 | * 93 12/04/98 1:41p Jason |
| 269 | * made napalm do even less damage |
| 270 | * |
| 271 | * 92 12/03/98 3:39p Jason |
| 272 | * added peer 2 peer style damage |
| 273 | * |
| 274 | * 91 12/02/98 10:30a Jason |
| 275 | * added additional damage types for client-side multiplayer |
| 276 | * |
| 277 | * 90 12/01/98 10:57a Jason |
| 278 | * fixed compiler warning |
| 279 | * |
| 280 | * 89 11/19/98 5:40p Kevin |
| 281 | * Demo system |
| 282 | * |
| 283 | * 88 11/19/98 12:22p Jason |
| 284 | * optimizations |
| 285 | * |
| 286 | * 87 11/13/98 2:25p Samir |
| 287 | * modify game music info when damaged or killed robot |
| 288 | * |
| 289 | * 86 11/13/98 12:29p Jason |
| 290 | * changes for weapons |
| 291 | * |
| 292 | * 85 11/11/98 2:46p Kevin |
| 293 | * Demo recording system work |
| 294 | * |
| 295 | * 84 11/11/98 11:41a Jason |
| 296 | * added sunlight damage |
| 297 | * |
| 298 | * 83 11/10/98 3:12p Chris |
| 299 | * Got rid of a const double to float warning |
| 300 | * |
| 301 | * 82 10/30/98 4:24p Jason |
| 302 | * changes for multiplayer |
| 303 | * |
| 304 | * 81 10/22/98 2:58p Chris |
| 305 | * Difficulty levels are in beta |
| 306 | * |
| 307 | * 80 10/22/98 10:50a Samir |
| 308 | * only register kill for robots so accuracy rating can be closer. |
| 309 | * |
| 310 | * 79 10/19/98 7:17p Matt |
| 311 | * Added system to support different types of damage to the player and |
| 312 | * have these different types make different sounds. |
| 313 | * |
| 314 | * 78 10/14/98 1:02p Jason |
| 315 | * fixed FindSoundName issues |
| 316 | * |
| 317 | * 77 10/06/98 11:27a Jason |
| 318 | * added new death type for robots |
| 319 | * |
| 320 | * 76 9/30/98 5:35p Jason |
| 321 | * added multiplayer menu bailing for samir |
| 322 | * |
| 323 | * 75 9/25/98 5:02p Jason |
| 324 | * fixed parenting problems |
| 325 | * |
| 326 | * 74 9/17/98 6:08p Jason |
| 327 | * more tweaks for effects |
| 328 | * |
| 329 | * 73 9/17/98 3:03p Jason |
| 330 | * added lightning and invul hit effects |
| 331 | * |
| 332 | * 72 9/14/98 4:17p Jason |
| 333 | * added friendly fire damage |
| 334 | * |
| 335 | * 71 8/14/98 6:26p Kevin |
| 336 | * for chris: dealt with killer==NULL in ExplodeObject |
| 337 | * |
| 338 | * 70 8/14/98 4:58p Jeff |
| 339 | * only if damage>0 does it play sound |
| 340 | * |
| 341 | * 69 8/14/98 12:49p Chris |
| 342 | * Working on attach system |
| 343 | * |
| 344 | * 68 8/12/98 12:04p Chris |
| 345 | * Attach system version .5 (still needs more work to be multiplayer |
| 346 | * friendly) |
| 347 | * |
| 348 | * 67 8/10/98 5:24p Jason |
| 349 | * made robots have a little variation when dying |
| 350 | * |
| 351 | * 66 8/03/98 5:56p Jason |
| 352 | * got fusion cannon working correctly |
| 353 | * |
| 354 | * 65 7/31/98 5:23p Jason |
| 355 | * added ship armor scalars |
| 356 | * |
| 357 | * 64 7/30/98 11:09a Jason |
| 358 | * added weapons that freeze and deform terrain |
| 359 | * |
| 360 | * 63 7/21/98 12:13p Jason |
| 361 | * Told DLL about which weapon was used to kill a player |
| 362 | * |
| 363 | * 62 7/15/98 2:33p Jason |
| 364 | * added scalar variables for various player skills |
| 365 | * |
| 366 | * 61 7/15/98 12:48p Jeff |
| 367 | * put in calls to handle new multiplayer event...when an object's shields |
| 368 | * change |
| 369 | * |
| 370 | * 60 6/30/98 11:39a Jason |
| 371 | * added AdditionalDamage packet type for multiplayer |
| 372 | * |
| 373 | * 59 6/29/98 12:12p Kevin |
| 374 | * Added robot damage and death packets |
| 375 | * |
| 376 | * 58 6/26/98 7:26p Jason |
| 377 | * checked in so kevin can work on coop |
| 378 | * |
| 379 | * 57 6/26/98 2:14p Jason |
| 380 | * fixed InitiatePlayerDeath bug with dead robots |
| 381 | * |
| 382 | * 56 6/02/98 6:04p Jason |
| 383 | * undid pre-e3 invul change |
| 384 | * |
| 385 | * 55 5/25/98 6:38p Matt |
| 386 | * Added needed include. |
| 387 | * |
| 388 | * 54 5/25/98 4:16p Jason |
| 389 | * made invulnerability different for E3 demo |
| 390 | * |
| 391 | * 53 5/22/98 6:22p Chris |
| 392 | * Improved Dynamic path allocation |
| 393 | * |
| 394 | * 52 5/19/98 2:41a Chris |
| 395 | * Added shockwave's -- enjoy. :) |
| 396 | * |
| 397 | * 51 5/04/98 4:04p Chris |
| 398 | * Minestone checkin' -- energy effect and more AI functionality |
| 399 | * |
| 400 | * 50 5/04/98 12:55p Jason |
| 401 | * fixed NULL pointer bug |
| 402 | * |
| 403 | * 49 5/01/98 6:50p Samir |
| 404 | * call initiate player death with melee if killed by robot. |
| 405 | * |
| 406 | * 48 5/01/98 3:41p Chris |
| 407 | * |
| 408 | * 47 5/31/98 3:07p Chris |
| 409 | * Robots die slowly more often. Made robots die slowly when the killer |
| 410 | * object is a |
| 411 | * player or robot. (Melee attacks and bumping damage deaths) |
| 412 | * |
| 413 | * 46 4/19/98 5:00p Jason |
| 414 | * added cool napalm effect, plus made object effects dynamically |
| 415 | * allocated |
| 416 | * |
| 417 | * 45 4/17/98 3:57p Jason |
| 418 | * added microwave effect |
| 419 | * |
| 420 | * 44 4/17/98 1:59p Jason |
| 421 | * added cool object effects |
| 422 | * |
| 423 | * 43 4/10/98 3:28p Jason |
| 424 | * Turn off guided missile when you get hit |
| 425 | * |
| 426 | * 42 4/09/98 5:17p Jason |
| 427 | * got guided working in multiplayer |
| 428 | * |
| 429 | * 41 4/06/98 4:05p Jason |
| 430 | * made suicides work correctly |
| 431 | * |
| 432 | * 40 4/06/98 2:54p Jason |
| 433 | * yet more multiplayer changes |
| 434 | * |
| 435 | * 39 4/06/98 12:14p Jason |
| 436 | * changes to multiplayer |
| 437 | * |
| 438 | * 38 3/31/98 12:16p Jason |
| 439 | * some small changes for explosions |
| 440 | * |
| 441 | * 37 3/30/98 5:11p Jason |
| 442 | * more changes for game/dll integration |
| 443 | * |
| 444 | * 36 3/23/98 4:51p Jason |
| 445 | * incremental checkin for multiplay |
| 446 | * |
| 447 | * 35 3/23/98 4:36p Jason |
| 448 | * make powerups non-destroyable in multiplay |
| 449 | * |
| 450 | * 34 3/12/98 7:30p Chris |
| 451 | * Added ObjSetOrient |
| 452 | * |
| 453 | * 33 2/25/98 4:31p Jason |
| 454 | * changes for explosions |
| 455 | * |
| 456 | * 32 2/18/98 4:12a Jason |
| 457 | * took out ridiculous explosions |
| 458 | * |
| 459 | * 31 2/16/98 11:19p Chris |
| 460 | * Added support for melee robots |
| 461 | * |
| 462 | * 30 2/16/98 4:55p Chris |
| 463 | * Added default sounds for explosions |
| 464 | * |
| 465 | * 29 2/10/98 11:32a Samir |
| 466 | * Added D3X support when a player gets killed to the gamemode script. |
| 467 | * |
| 468 | * 28 2/09/98 5:38p Jeff |
| 469 | * changed EVT_PLAYERKILLED to EVT_GAMEPLAYERKILLED for osiris |
| 470 | * |
| 471 | * 27 2/05/98 2:54p Jason |
| 472 | * changes for explosions |
| 473 | * |
| 474 | * 26 2/04/98 9:28p Jason |
| 475 | * added some new weapons effects |
| 476 | * |
| 477 | * 25 2/04/98 6:09p Matt |
| 478 | * Changed object room number to indicate a terrain cell via a flag. Got |
| 479 | * rid of the object flag which used to indicate terrain. |
| 480 | * |
| 481 | * 24 2/03/98 4:04p Jeff |
| 482 | * added OSIRIS events for player killed and exploded |
| 483 | * |
| 484 | * 23 1/28/98 1:11p Jason |
| 485 | * took shields field out of Player struct |
| 486 | * |
| 487 | * 22 1/28/98 12:00p Jason |
| 488 | * more changes for multiplayer |
| 489 | * |
| 490 | * 21 1/21/98 6:09p Jason |
| 491 | * Got player deaths working in multiplayer |
| 492 | * |
| 493 | * 20 1/21/98 1:11p Jason |
| 494 | * incremental checkin for multiplayer |
| 495 | * |
| 496 | * 19 1/20/98 6:01p Jason |
| 497 | * first pass at getting multiplayer deaths working |
| 498 | * |
| 499 | * 18 1/19/98 6:23p Chris |
| 500 | * Fixed a crash when the killer is no longer alive. |
| 501 | * |
| 502 | * 17 1/19/98 10:04a Matt |
| 503 | * Added new object handle system |
| 504 | * |
| 505 | * 16 1/15/98 6:49p Jason |
| 506 | * incremental checkin for multiplayer |
| 507 | * |
| 508 | * 15 1/05/98 3:54p Chris |
| 509 | * Added ambient and explosion sounds |
| 510 | * |
| 511 | * 14 1/05/98 11:02a Jason |
| 512 | * made shake magnitude proportional to the distance from the explosion |
| 513 | * |
| 514 | * |
| 515 | * 13 12/12/97 6:13p Jason |
| 516 | * added sparks |
| 517 | * |
| 518 | * 12 12/08/97 5:21p Jason |
| 519 | * added code for destroyable buildings that leave their base object |
| 520 | * around |
| 521 | * |
| 522 | * 11 12/08/97 3:28p Jason |
| 523 | * tweaked explosions |
| 524 | * |
| 525 | * 10 12/03/97 5:53p Jason |
| 526 | * worked on cooler explosion effects |
| 527 | * |
| 528 | * 9 12/01/97 11:02a Jason |
| 529 | * made camera shake from explosions |
| 530 | * |
| 531 | * 8 12/01/97 9:50a Chris |
| 532 | * Added support for concussive forces, generalized robot collisions to |
| 533 | * generic collisions |
| 534 | * |
| 535 | * 7 11/18/97 4:37p Jason |
| 536 | * added visible damage system |
| 537 | * |
| 538 | * 6 11/04/97 5:50p Jason |
| 539 | * made #define for strange number (.6) |
| 540 | * |
| 541 | * 5 10/24/97 2:47p Jason |
| 542 | * added blast rings to robots |
| 543 | * |
| 544 | * 4 10/20/97 4:46p Jason |
| 545 | * changes for explosions |
| 546 | * |
| 547 | * 3 10/08/97 6:35p Samir |
| 548 | * Dying player ships get marked as destroyed. When death sequence is |
| 549 | * over, they are marked as dead. This is needed to better control |
| 550 | * movement of the dying ship. |
| 551 | * |
| 552 | * 2 10/03/97 11:58a Samir |
| 553 | * Added mission name |
| 554 | * |
| 555 | * 1 10/01/97 4:50p Samir |
| 556 | * Damage for players and robots. |
| 557 | * |
| 558 | * $NoKeywords: $ |
| 559 | */ |
| 560 | |
| 561 | #include <stdlib.h> |
| 562 | #include <memory.h> |
| 563 | #include "damage.h" |
| 564 | #include "object.h" |
| 565 | #include "objinfo.h" |
| 566 | #include "player.h" |
| 567 | #include "fireball.h" |
| 568 | #include "gameevent.h" |
| 569 | #include "hlsoundlib.h" |
| 570 | #include "multi.h" |
| 571 | #include "game.h" |
| 572 | #include "sounds.h" |
| 573 | #include "soundload.h" |
| 574 | #include "game2dll.h" |
| 575 | #include "weapon.h" |
| 576 | #include "ship.h" |
| 577 | #include "attach.h" |
| 578 | #include "difficulty.h" |
| 579 | #include "demofile.h" |
| 580 | #include "d3music.h" |
| 581 | #include "osiris_dll.h" |
| 582 | #include "D3ForceFeedback.h" |
| 583 | #include "multi_server.h" |
| 584 | #include "doorway.h" |
| 585 | #include "DeathInfo.h" |
| 586 | #include "AIGoal.h" |
| 587 | #include "viseffect.h" |
| 588 | #include "psrand.h" |
| 589 | |
| 590 | #include <algorithm> |
| 591 | |
| 592 | static void SetDeformDamageEffect(object *obj); |
| 593 | static void ApplyFreezeDamageEffect(object *obj); |
| 594 | static void DecreasePlayerShields(int slot, float damage); |
| 595 | static void DoEDrainEffect(int eventnum, void *data); |
| 596 | static void PlayPlayerDamageSound(object *playerobj, int damage_type); |
| 597 | static void GenerateDefaultDeath(object *obj, int *death_flags, float *delay_time); |
| 598 | static void PlayPlayerInvulnerabilitySound(object *playerobj); |
| 599 | static void SetFlyingPhysics(object *objp, bool tumbles); |
| 600 | static float GetDeathAnimTime(object *objp); |
| 601 | static void SetFallingPhysics(object *objp); |
| 602 | |
| 603 | // Shake variables |
| 604 | static matrix Old_player_orient; |
| 605 | float Shake_magnitude = 0.0; |
| 606 | |
| 607 | // Causes an object to deform |
| 608 | void SetDeformDamageEffect(object *obj) { |
| 609 | if (obj->effect_info) { |
| 610 | obj->effect_info->type_flags |= EF_DEFORM; |
| 611 | obj->effect_info->deform_time = 1.0; |
| 612 | obj->effect_info->deform_range = .3f; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | #define MAX_NAPALM_DAMAGE_TIME 10.0f |
| 617 | // Causes an object to have napalm attached to it |
| 618 | void SetNapalmDamageEffect(object *obj, object *killer, int weapon_id) { |
| 619 | if (obj->effect_info) { |
| 620 | obj->effect_info->type_flags |= EF_NAPALMED; |
| 621 | |
| 622 | if (Weapons[weapon_id].flags & WF_INVISIBLE) |
| 623 | obj->effect_info->damage_time += (MAX_NAPALM_DAMAGE_TIME / 5.0f); |
| 624 | else |
| 625 | obj->effect_info->damage_time += (MAX_NAPALM_DAMAGE_TIME / 1.5f); |
| 626 | |
| 627 | obj->effect_info->damage_time = std::min(MAX_NAPALM_DAMAGE_TIME, obj->effect_info->damage_time); |
| 628 | |
| 629 | if (obj->type == OBJ_PLAYER) |
| 630 | obj->effect_info->damage_per_second = Weapons[weapon_id].player_damage; |
| 631 | else |
| 632 | obj->effect_info->damage_per_second = Weapons[weapon_id].generic_damage; |
| 633 | |
| 634 | if (Gametime - obj->effect_info->last_damage_time > 1.0f) |
| 635 | obj->effect_info->last_damage_time = 0; |
| 636 | |
| 637 | if (killer != NULL) |
| 638 | obj->effect_info->damage_handle = killer->handle; |
| 639 | else |
| 640 | obj->effect_info->damage_handle = OBJECT_HANDLE_NONE; |
| 641 | |
| 642 | if (obj->effect_info->sound_handle == SOUND_NONE_INDEX) |
| 643 | obj->effect_info->sound_handle = Sound_system.Play3dSound(SOUND_PLAYER_BURNING, SND_PRIORITY_HIGHEST, obj); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // Causes an object to have napalm attached to it |
| 648 | void ApplyFreezeDamageEffect(object *obj) { |
| 649 | if (obj->effect_info) { |
| 650 | if (!(obj->effect_info->type_flags & EF_FREEZE)) |
| 651 | obj->effect_info->freeze_scalar = 1.0; |
| 652 | |
| 653 | obj->effect_info->type_flags |= EF_FREEZE; |
| 654 | obj->effect_info->freeze_scalar -= .2f; |
| 655 | |
| 656 | if (obj->effect_info->freeze_scalar < .3) |
| 657 | obj->effect_info->freeze_scalar = .3f; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // Event to handle damage effect |
| 662 | void DoDamageEffect(int eventnum, void *data) { |
| 663 | float damage_norm = Players[Player_num].damage_magnitude / MAX_DAMAGE_MAG; |
| 664 | |
| 665 | if (damage_norm > 1.0) |
| 666 | damage_norm = 1.0; |
| 667 | |
| 668 | DrawAlphaBlendedScreen(1.0, 0, 0, .4 * damage_norm); |
| 669 | |
| 670 | // If we're all done, just stop. Else, make another event! |
| 671 | if (Players[Player_num].damage_magnitude < .0001f) |
| 672 | Players[Player_num].damage_magnitude = 0.0; |
| 673 | else |
| 674 | CreateNewEvent(RENDER_EVENT, DAMAGE_EFFECT, 0, NULL, 0, DoDamageEffect); |
| 675 | } |
| 676 | |
| 677 | // Decreases a players shields |
| 678 | void DecreasePlayerShields(int slot, float damage) { |
| 679 | object *playerobj = &Objects[Players[slot].objnum]; |
| 680 | playerobj->shields -= damage; |
| 681 | Players[slot].damage_magnitude += damage; |
| 682 | |
| 683 | if (slot == Player_num) { |
| 684 | if ((FindEventID(DAMAGE_EFFECT)) == -1) |
| 685 | CreateNewEvent(RENDER_EVENT, DAMAGE_EFFECT, 0, NULL, 0, DoDamageEffect); |
| 686 | |
| 687 | if (Game_mode & GM_MULTI) |
| 688 | Multi_bail_ui_menu = true; |
| 689 | |
| 690 | Game_music_info.player_damaged = true; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | // Event to handle damage effect |
| 695 | void DoEDrainEffect(int eventnum, void *data) { |
| 696 | float edrain_norm = Players[Player_num].edrain_magnitude / MAX_EDRAIN_MAG; |
| 697 | |
| 698 | if (edrain_norm > 1.0) |
| 699 | edrain_norm = 1.0; |
| 700 | |
| 701 | DrawAlphaBlendedScreen(0, 0, 1.0, .6 * edrain_norm); |
| 702 | |
| 703 | // If we're all done, just stop. Else, make another event! |
| 704 | if (Players[Player_num].edrain_magnitude < .0001f) |
| 705 | Players[Player_num].edrain_magnitude = 0.0; |
| 706 | else |
| 707 | CreateNewEvent(RENDER_EVENT, EDRAIN_EFFECT, 0, NULL, 0, DoEDrainEffect); |
| 708 | } |
| 709 | |
| 710 | // Decreases a players shields |
| 711 | void DecreasePlayerEnergy(int slot, float energy) { |
| 712 | Players[slot].energy -= energy; |
| 713 | Players[slot].edrain_magnitude += energy; |
| 714 | |
| 715 | if (slot == Player_num) { |
| 716 | if ((FindEventID(EDRAIN_EFFECT)) == -1) |
| 717 | CreateNewEvent(RENDER_EVENT, EDRAIN_EFFECT, 0, NULL, 0, DoEDrainEffect); |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | // Plays the sound for a player being damaged |
| 722 | void PlayPlayerDamageSound(object *playerobj, int damage_type) { |
| 723 | int sound_handle; |
| 724 | |
| 725 | switch (damage_type) { |
| 726 | case PD_ENERGY_WEAPON: |
| 727 | sound_handle = SOUND_HIT_BY_ENERGY_WEAPON; |
| 728 | break; |
| 729 | case PD_MATTER_WEAPON: |
| 730 | sound_handle = SOUND_HIT_BY_MATTER_WEAPON; |
| 731 | break; |
| 732 | case PD_CONCUSSIVE_FORCE: |
| 733 | sound_handle = SOUND_HIT_BY_CONCUSSIVE_FORCE; |
| 734 | break; |
| 735 | case PD_WALL_HIT: |
| 736 | sound_handle = SOUND_PLAYER_HIT_WALL; |
| 737 | break; |
| 738 | case PD_VOLATILE_HISS: |
| 739 | sound_handle = SOUND_VOLATILE_HISS; |
| 740 | break; |
| 741 | |
| 742 | default: |
| 743 | Int3(); |
| 744 | case PD_MELEE_ATTACK: // melee sounds handled in aimain.cpp for now |
| 745 | case PD_NONE: |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | Sound_system.Play3dSound(sound_handle, SND_PRIORITY_HIGHEST, playerobj); |
| 750 | } |
| 751 | |
| 752 | // Plays the sound for a player being damaged |
| 753 | void PlayPlayerInvulnerabilitySound(object *playerobj) { |
| 754 | Sound_system.Play3dSound(SOUND_METALLIC_DOOR_HIT, SND_PRIORITY_HIGHEST, playerobj); |
| 755 | } |
| 756 | |
| 757 | // Kills the player |
| 758 | // weapon_id can be -1 for no weapon |
| 759 | void KillPlayer(object *playerobj, object *killer, float damage_amount, int weapon_id) { |
| 760 | // Save the killer |
| 761 | Players[playerobj->id].killer_objnum = killer ? OBJNUM(killer) : 0; |
| 762 | |
| 763 | // Set some stuff |
| 764 | Players[playerobj->id].damage_magnitude = 0; |
| 765 | Players[playerobj->id].edrain_magnitude = 0; |
| 766 | |
| 767 | tOSIRISEventInfo ei; |
| 768 | ei.evt_player_dies.it_handle = playerobj->handle; |
| 769 | Osiris_CallLevelEvent(EVT_PLAYER_DIES, &ei); |
| 770 | |
| 771 | // If single-player, record & initiate death |
| 772 | if (!(Game_mode & GM_MULTI)) { |
| 773 | |
| 774 | if (Demo_flags == DF_RECORDING) |
| 775 | DemoWritePlayerDeath(playerobj, (killer && IS_ROBOT(killer))); |
| 776 | |
| 777 | if (Demo_flags != DF_PLAYBACK) |
| 778 | InitiatePlayerDeath(playerobj, (killer && IS_ROBOT(killer))); |
| 779 | } else if (Netgame.local_role == LR_SERVER) { // Multiplayer & server |
| 780 | |
| 781 | // Call DLL stuff |
| 782 | DLLInfo.me_handle = playerobj->handle; |
| 783 | |
| 784 | if (killer != NULL) |
| 785 | DLLInfo.it_handle = killer->handle; |
| 786 | else |
| 787 | DLLInfo.it_handle = OBJECT_HANDLE_NONE; |
| 788 | |
| 789 | DLLInfo.iParam = weapon_id; |
| 790 | |
| 791 | CallGameDLL(EVT_GAMEPLAYERKILLED, &DLLInfo); |
| 792 | |
| 793 | // pick a fate for this guy |
| 794 | int fate = PlayerChooseDeathFate(playerobj->id, damage_amount, false); |
| 795 | |
| 796 | // Send it to everyone |
| 797 | MultiSendPlayerDead(playerobj->id, fate); |
| 798 | |
| 799 | // Figure out new rankings |
| 800 | GetNewRankings(playerobj, killer); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | // Applies damage to a player object, returns false if damage wasn't applied due to things like |
| 805 | // invunerability |
| 806 | bool ApplyDamageToPlayer(object *playerobj, object *killer, int damage_type, float damage_amount, int server_says, |
| 807 | int weapon_id, bool playsound) { |
| 808 | if (!playerobj || playerobj->type != OBJ_PLAYER) { |
| 809 | // bail it's not a player (no damage should be applied to ghosts or observer's either |
| 810 | return false; |
| 811 | } |
| 812 | |
| 813 | // apply damage to the current player! |
| 814 | object *weapon_obj = NULL; |
| 815 | |
| 816 | if (killer != NULL && killer->type == OBJ_WEAPON) { |
| 817 | weapon_obj = killer; |
| 818 | killer = ObjGetUltimateParent(killer); |
| 819 | |
| 820 | if (!(Game_mode & GM_MULTI) || ((Game_mode & GM_MULTI) && Netgame.local_role == LR_SERVER)) |
| 821 | weapon_id = weapon_obj->id; |
| 822 | if ((Game_mode & GM_MULTI) && Netgame.local_role == LR_CLIENT && !server_says && (Netgame.flags & NF_PEER_PEER)) |
| 823 | weapon_id = weapon_obj->id; |
| 824 | } |
| 825 | |
| 826 | if ((Players[playerobj->id].flags & PLAYER_FLAGS_DYING) || (Players[playerobj->id].flags & PLAYER_FLAGS_DEAD)) { |
| 827 | PlayPlayerDamageSound(playerobj, damage_type); |
| 828 | return false; |
| 829 | } |
| 830 | |
| 831 | if ((Players[playerobj->id].flags & PLAYER_FLAGS_INVULNERABLE) || ((Player_object->flags & OF_DESTROYABLE) == 0)) { |
| 832 | if (damage_type != PD_NONE) |
| 833 | PlayPlayerInvulnerabilitySound(playerobj); |
| 834 | |
| 835 | if (weapon_obj != NULL) { |
| 836 | Players[playerobj->id].invul_magnitude = 1; |
| 837 | Players[playerobj->id].invul_vector = weapon_obj->pos - playerobj->pos; |
| 838 | vm_NormalizeVectorFast(&Players[playerobj->id].invul_vector); |
| 839 | } |
| 840 | return false; |
| 841 | } else { |
| 842 | if (!(Game_mode & GM_MULTI)) { |
| 843 | damage_amount *= Players[playerobj->id].armor_scalar; |
| 844 | damage_amount *= Ships[Players[playerobj->id].ship_index].armor_scalar; |
| 845 | |
| 846 | DecreasePlayerShields(playerobj->id, damage_amount); |
| 847 | |
| 848 | if (damage_amount > 0 && playsound) |
| 849 | PlayPlayerDamageSound(playerobj, damage_type); |
| 850 | |
| 851 | if (Players[playerobj->id].guided_obj != NULL) |
| 852 | ReleaseGuidedMissile(playerobj->id); |
| 853 | |
| 854 | if (Players[playerobj->id].user_timeout_obj != NULL) |
| 855 | ReleaseUserTimeoutMissile(playerobj->id); |
| 856 | |
| 857 | if (weapon_id != 255) { |
| 858 | if (Weapons[weapon_id].flags & WF_MICROWAVE) |
| 859 | SetDeformDamageEffect(playerobj); |
| 860 | if (Weapons[weapon_id].flags & WF_FREEZE) |
| 861 | ApplyFreezeDamageEffect(playerobj); |
| 862 | if (Weapons[weapon_id].flags & WF_NAPALM) |
| 863 | SetNapalmDamageEffect(playerobj, killer, weapon_id); |
| 864 | } |
| 865 | |
| 866 | if (playerobj->shields < 0) { |
| 867 | KillPlayer(playerobj, killer, damage_amount, weapon_id); |
| 868 | } |
| 869 | } else { |
| 870 | // If this is a peer to peer game, and its me taking damage, then ask the server to damage me! |
| 871 | if (Netgame.local_role == LR_CLIENT && (Netgame.flags & NF_PEER_PEER) && !server_says) { |
| 872 | if (playerobj->id == Player_num) { |
| 873 | MultiSendRequestPeerDamage(killer, weapon_id, damage_type, damage_amount); |
| 874 | return true; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | if ((Netgame.flags & NF_PEER_PEER) && Netgame.local_role == LR_SERVER && !server_says) { |
| 879 | if (playerobj->id != Player_num) |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | if (Netgame.local_role == LR_SERVER || server_says) { |
| 884 | if (Netgame.local_role == LR_SERVER) { |
| 885 | // Check to see if we're on the same team |
| 886 | if (killer != NULL && killer->type == OBJ_PLAYER) { |
| 887 | if (killer->id != playerobj->id) { |
| 888 | if (Players[killer->id].team == Players[playerobj->id].team && !(Netgame.flags & NF_DAMAGE_FRIENDLY)) |
| 889 | return false; // don't hurt players on your own team |
| 890 | } |
| 891 | |
| 892 | damage_amount *= Players[killer->id].damage_scalar; |
| 893 | } |
| 894 | |
| 895 | damage_amount *= Players[playerobj->id].armor_scalar; |
| 896 | damage_amount *= Ships[Players[playerobj->id].ship_index].armor_scalar; |
| 897 | } |
| 898 | |
| 899 | DecreasePlayerShields(playerobj->id, damage_amount); |
| 900 | if (damage_amount > 0 && playsound) |
| 901 | PlayPlayerDamageSound(playerobj, damage_type); |
| 902 | if (playerobj->id == Player_num && Players[playerobj->id].guided_obj != NULL) |
| 903 | ReleaseGuidedMissile(playerobj->id); |
| 904 | |
| 905 | if (playerobj->id == Player_num && Players[playerobj->id].user_timeout_obj != NULL) |
| 906 | ReleaseUserTimeoutMissile(playerobj->id); |
| 907 | |
| 908 | if (weapon_id != 255) { |
| 909 | if (Weapons[weapon_id].flags & WF_MICROWAVE) |
| 910 | SetDeformDamageEffect(playerobj); |
| 911 | if (Weapons[weapon_id].flags & WF_NAPALM) |
| 912 | SetNapalmDamageEffect(playerobj, killer, weapon_id); |
| 913 | if (Weapons[weapon_id].flags & WF_FREEZE) |
| 914 | ApplyFreezeDamageEffect(playerobj); |
| 915 | } |
| 916 | |
| 917 | // Tell others about this tragic event |
| 918 | if (Netgame.local_role == LR_SERVER) { |
| 919 | if (weapon_id != 255) { |
| 920 | // If this is an electrical weapon then we should special case the damage |
| 921 | if (Weapons[weapon_id].flags & WF_ELECTRICAL) { |
| 922 | Multi_additional_damage[playerobj->id] += damage_amount; |
| 923 | Multi_additional_damage_type[playerobj->id] = PD_ENERGY_WEAPON; |
| 924 | } else |
| 925 | MultiSendDamagePlayer(playerobj->id, weapon_id, damage_type, damage_amount); |
| 926 | } else { |
| 927 | if (weapon_obj != NULL) |
| 928 | MultiSendDamagePlayer(playerobj->id, 255, damage_type, damage_amount); |
| 929 | else { |
| 930 | Multi_additional_damage[playerobj->id] += damage_amount; |
| 931 | Multi_additional_damage_type[playerobj->id] = damage_type; |
| 932 | } |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | if (Netgame.local_role == LR_SERVER) { |
| 938 | // Call DLL stuff |
| 939 | DLLInfo.me_handle = playerobj->handle; |
| 940 | if (killer != NULL) |
| 941 | DLLInfo.it_handle = killer->handle; |
| 942 | else |
| 943 | DLLInfo.it_handle = OBJECT_HANDLE_NONE; |
| 944 | DLLInfo.fParam = damage_amount; |
| 945 | CallGameDLL(EVT_GAMEOBJECTSHIELDSCHANGED, &DLLInfo); |
| 946 | |
| 947 | if (playerobj->shields < 0) { |
| 948 | KillPlayer(playerobj, killer, damage_amount, weapon_id); |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | return true; |
| 955 | } |
| 956 | |
| 957 | // Fills in death info to roughly appoximate the deaths from the old explosion system |
| 958 | void GenerateDefaultDeath(object *obj, int *death_flags, float *delay_time) { |
| 959 | // Set some defaults |
| 960 | *death_flags = 0; |
| 961 | *delay_time = 0.0; |
| 962 | |
| 963 | if ((obj->type != OBJ_ROBOT && !(obj->type == OBJ_BUILDING && obj->ai_info)) || |
| 964 | (obj->movement_type != MT_WALKING && ((ps_rand() % 3) != 0)) || |
| 965 | (obj->movement_type == MT_WALKING && ((ps_rand() % 10) > 5))) { |
| 966 | |
| 967 | // Quick death |
| 968 | *death_flags = 0; |
| 969 | *delay_time = 0.0; |
| 970 | |
| 971 | // Maybe set blast ring |
| 972 | if ((ps_rand() % 3) == 0) |
| 973 | *death_flags |= DF_BLAST_RING; |
| 974 | } else { // slow death |
| 975 | |
| 976 | int alternate_death = 0; |
| 977 | bool f_upsidedown_walker = (obj->movement_type == MT_WALKING) && (obj->orient.uvec.y < 0.0f); |
| 978 | |
| 979 | if (!f_upsidedown_walker && (ps_rand() % 2) == 1) { |
| 980 | *death_flags = DF_DELAY_SPARKS; |
| 981 | alternate_death = 1; |
| 982 | } |
| 983 | |
| 984 | if (obj->type == OBJ_ROBOT || (obj->type == OBJ_BUILDING && obj->ai_info)) { |
| 985 | if (alternate_death || (obj->movement_type != MT_PHYSICS && obj->movement_type != MT_WALKING) || |
| 986 | (obj->movement_type == MT_WALKING && !f_upsidedown_walker)) { |
| 987 | *death_flags = DF_DELAY_SPARKS; |
| 988 | *delay_time = 3.0f; |
| 989 | alternate_death = 1; |
| 990 | } else { |
| 991 | if (obj->movement_type != MT_WALKING) |
| 992 | *death_flags = 0; |
| 993 | else |
| 994 | *death_flags = DF_DELAY_LOSES_ANTIGRAV; |
| 995 | } |
| 996 | |
| 997 | // Alternate death must last 3 seconds to correspond with sound effect |
| 998 | // If you change this assumption, tell Jerry |
| 999 | if (!alternate_death && Object_info[obj->id].anim) { |
| 1000 | if (obj->control_type == CT_AI && |
| 1001 | Object_info[obj->id].anim[obj->ai_info->movement_type].elem[AS_DEATH].to != 0.0f) { |
| 1002 | // compute time remaining in current animation |
| 1003 | float extra_time = obj->rtype.pobj_info.anim_time * |
| 1004 | (obj->rtype.pobj_info.anim_end_frame - obj->rtype.pobj_info.anim_frame) / |
| 1005 | (obj->rtype.pobj_info.anim_end_frame - obj->rtype.pobj_info.anim_start_frame); |
| 1006 | extra_time = std::min<float>(extra_time, 3.0); // limit extra time to 3 seconds |
| 1007 | *delay_time = Object_info[obj->id].anim[obj->ai_info->movement_type].elem[AS_DEATH].spc + 0.25 + extra_time; |
| 1008 | // Walkers last a little longer |
| 1009 | if (obj->movement_type == MT_WALKING) |
| 1010 | *delay_time += 2.0f; |
| 1011 | } else |
| 1012 | *delay_time = 2.0f; |
| 1013 | } |
| 1014 | } else { // If building, make it shoot up |
| 1015 | *death_flags |= DF_DELAY_FLYING; |
| 1016 | if (obj->orient.uvec.y == 1.0f) |
| 1017 | *delay_time = 2.0f; |
| 1018 | else |
| 1019 | *delay_time = 0.7f; |
| 1020 | } |
| 1021 | |
| 1022 | // Some deaths get blast ring |
| 1023 | if ((ps_rand() % 8) == 0) |
| 1024 | *death_flags |= DF_BLAST_RING; |
| 1025 | } |
| 1026 | |
| 1027 | // Set flags for all deaths |
| 1028 | *death_flags |= DF_FIREBALL + DF_BREAKS_APART + DF_CONTACT_FIREBALL + DF_CONTACT_BREAKS_APART + DF_DEBRIS_SMOKES + |
| 1029 | DF_DEBRIS_FIREBALL; |
| 1030 | } |
| 1031 | |
| 1032 | // Applies a default death to an object |
| 1033 | // Parameters: objp - the object to destroy |
| 1034 | // killer - the object who is killing it, or NULL |
| 1035 | // damage - how much damage was applied in the death blow? |
| 1036 | void KillObject(object *objp, object *killer, float damage) { |
| 1037 | int death_flags = -1; |
| 1038 | float delay_time; |
| 1039 | |
| 1040 | // Make sure a valid type |
| 1041 | ASSERT(IS_GENERIC(objp->type) || (objp->type == OBJ_DOOR)); |
| 1042 | |
| 1043 | // Bail if already dying or exploding |
| 1044 | if ((objp->flags & OF_DYING) || (objp->flags & OF_DEAD)) |
| 1045 | return; |
| 1046 | |
| 1047 | // Get death info from the object page |
| 1048 | if (IS_GENERIC(objp->type)) { |
| 1049 | |
| 1050 | // Get random probability |
| 1051 | int r = (ps_rand() * 100 / (D3_RAND_MAX + 1)) + 1; // in range 1 to 100 |
| 1052 | |
| 1053 | // Loop through death types looking for chosen one |
| 1054 | for (int i = 0; i < MAX_DEATH_TYPES; i++) { |
| 1055 | int p = Object_info[objp->id].death_probabilities[i]; |
| 1056 | if (r <= p) { |
| 1057 | float delay_min, delay_max; |
| 1058 | |
| 1059 | death_flags = Object_info[objp->id].death_types[i].flags; |
| 1060 | |
| 1061 | delay_min = Object_info[objp->id].death_types[i].delay_min; |
| 1062 | delay_max = Object_info[objp->id].death_types[i].delay_max; |
| 1063 | |
| 1064 | delay_time = delay_min + (delay_max - delay_min) * ps_rand() / D3_RAND_MAX; |
| 1065 | |
| 1066 | mprintf(0, "Using %d\n", i); |
| 1067 | break; |
| 1068 | } |
| 1069 | r -= p; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | // If no death from page, generate default |
| 1074 | if (death_flags == -1) |
| 1075 | GenerateDefaultDeath(objp, &death_flags, &delay_time); |
| 1076 | |
| 1077 | // Now kill the object |
| 1078 | KillObject(objp, killer, damage, death_flags, delay_time); |
| 1079 | } |
| 1080 | |
| 1081 | // Sets the physics parameters for a falling object |
| 1082 | void SetFallingPhysics(object *objp) { |
| 1083 | // Set gravity & other parms |
| 1084 | objp->mtype.phys_info.flags = PF_GRAVITY | PF_BOUNCE; |
| 1085 | objp->mtype.phys_info.num_bounces = 0; |
| 1086 | objp->mtype.phys_info.coeff_restitution = .03f; |
| 1087 | if (objp->mtype.phys_info.mass < 20.0) |
| 1088 | objp->mtype.phys_info.mass = 20.0f; |
| 1089 | objp->mtype.phys_info.drag = .00001f; |
| 1090 | objp->mtype.phys_info.rotdrag = .00001f; |
| 1091 | |
| 1092 | // Special stuff for walkers |
| 1093 | if (objp->movement_type == MT_WALKING) { |
| 1094 | objp->mtype.phys_info.rotvel = Zero_vector; |
| 1095 | objp->mtype.phys_info.flags |= PF_POINT_COLLIDE_WALLS; |
| 1096 | float proj = objp->mtype.phys_info.velocity * objp->orient.uvec; |
| 1097 | if (proj < 0.0f) |
| 1098 | objp->mtype.phys_info.velocity -= proj * objp->orient.uvec; |
| 1099 | |
| 1100 | objp->mtype.phys_info.velocity += objp->orient.uvec * (3.0f + ((float)ps_rand() / D3_RAND_MAX) * 5.0); |
| 1101 | objp->movement_type = MT_PHYSICS; |
| 1102 | } else { // not a walker |
| 1103 | |
| 1104 | // If not spinning much, give the object a good spin |
| 1105 | if (vm_GetMagnitude(&objp->mtype.phys_info.rotvel) < 4000.0f) { |
| 1106 | objp->mtype.phys_info.rotvel.x = (float)((60000.0f * (float)(D3_RAND_MAX / 2 - ps_rand())) / (float)(D3_RAND_MAX / 2)); |
| 1107 | objp->mtype.phys_info.rotvel.y = (float)((60000.0f * (float)(D3_RAND_MAX / 2 - ps_rand())) / (float)(D3_RAND_MAX / 2)); |
| 1108 | objp->mtype.phys_info.rotvel.z = (float)((60000.0f * (float)(D3_RAND_MAX / 2 - ps_rand())) / (float)(D3_RAND_MAX / 2)); |
| 1109 | } |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | // Sets the physics parameters for an object the flies in the air when dies |
| 1114 | void SetFlyingPhysics(object *objp, bool tumbles) { |
| 1115 | // Set gravity & other parms |
| 1116 | objp->mtype.phys_info.flags = PF_GRAVITY | PF_NO_COLLIDE; |
| 1117 | objp->mtype.phys_info.coeff_restitution = .03f; |
| 1118 | objp->mtype.phys_info.mass = 50000.0f; |
| 1119 | objp->mtype.phys_info.drag = .01f; |
| 1120 | objp->mtype.phys_info.rotdrag = .01f; |
| 1121 | |
| 1122 | objp->movement_type = MT_PHYSICS; |
| 1123 | |
| 1124 | if (tumbles) { |
| 1125 | // Make y spin a little bit more that x or z |
| 1126 | objp->mtype.phys_info.rotvel.x = (float)((40000.0f * (float)(D3_RAND_MAX / 2 - ps_rand())) / (float)(D3_RAND_MAX / 2)); |
| 1127 | objp->mtype.phys_info.rotvel.y = (float)((60000.0f * (float)(D3_RAND_MAX / 2 - ps_rand())) / (float)(D3_RAND_MAX / 2)); |
| 1128 | objp->mtype.phys_info.rotvel.z = (float)((40000.0f * (float)(D3_RAND_MAX / 2 - ps_rand())) / (float)(D3_RAND_MAX / 2)); |
| 1129 | } |
| 1130 | |
| 1131 | if (objp->orient.uvec.y == 1.0f) { |
| 1132 | objp->mtype.phys_info.velocity.y = (float)(3 * (ps_rand() % 15)) + 20.0; |
| 1133 | objp->mtype.phys_info.velocity.x = 0; |
| 1134 | objp->mtype.phys_info.velocity.z = 0; |
| 1135 | |
| 1136 | // Doubled the velocity on 4/30/99 to make Josh's pop machines cooler. -MT |
| 1137 | objp->mtype.phys_info.velocity.y *= 2.0; |
| 1138 | } else { |
| 1139 | objp->mtype.phys_info.velocity = objp->orient.uvec * ((float)(2.0f * (ps_rand() % 15)) + 7.0f); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | // Figure out how long it will take the dying anim to play |
| 1144 | float GetDeathAnimTime(object *objp) { |
| 1145 | float death_time = 0.0; |
| 1146 | |
| 1147 | if (objp->control_type == CT_AI) { |
| 1148 | |
| 1149 | ASSERT(IS_GENERIC(objp->type)); |
| 1150 | |
| 1151 | if (Object_info[objp->id].anim[objp->ai_info->movement_type].elem[AS_DEATH].to != 0) { |
| 1152 | |
| 1153 | // calculate how long for the object to finish its current animation before starting death anim |
| 1154 | float extra_time = objp->rtype.pobj_info.anim_time * |
| 1155 | (objp->rtype.pobj_info.anim_end_frame - objp->rtype.pobj_info.anim_frame) / |
| 1156 | (objp->rtype.pobj_info.anim_end_frame - objp->rtype.pobj_info.anim_start_frame); |
| 1157 | extra_time = std::min<float>(extra_time, 3.0); // limit extra time to 3 seconds |
| 1158 | mprintf(0, "extra_time = %2f\n", extra_time); |
| 1159 | |
| 1160 | death_time = Object_info[objp->id].anim[objp->ai_info->movement_type].elem[AS_DEATH].spc + 0.25 + extra_time; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | return death_time; |
| 1165 | } |
| 1166 | |
| 1167 | // Kill an object |
| 1168 | // The caller can specify death information; if none is specified, a default death will be used. |
| 1169 | // Parameters: objp - the object to destroy |
| 1170 | // killer - the object who is killing it, or NULL |
| 1171 | // damage - how much damage was applied in the death blow |
| 1172 | // death_flags - how the object dies |
| 1173 | // delay_time - how long to delay, if from anim flag not set |
| 1174 | void KillObject(object *objp, object *killer, float damage, int death_flags, float delay_time) { |
| 1175 | // Make sure a valid type |
| 1176 | ASSERT(IS_GENERIC(objp->type) || (objp->type == OBJ_DOOR)); |
| 1177 | |
| 1178 | // Bail if already dying or exploding |
| 1179 | if ((objp->flags & OF_DYING) || (objp->flags & OF_DEAD)) |
| 1180 | return; |
| 1181 | |
| 1182 | // Send out multiplayer info |
| 1183 | if ((Game_mode & GM_MULTI) && (Netgame.local_role == LR_SERVER)) { |
| 1184 | int seed = ps_rand(); |
| 1185 | MultiSendKillObject(objp, killer, damage, death_flags, delay_time, seed); |
| 1186 | ps_srand(seed); |
| 1187 | } |
| 1188 | |
| 1189 | // Record demo |
| 1190 | if (Demo_flags == DF_RECORDING) { |
| 1191 | int seed = ps_rand(); |
| 1192 | DemoWriteKillObject(objp, killer, damage, death_flags, delay_time, seed); |
| 1193 | ps_srand(seed); |
| 1194 | } |
| 1195 | |
| 1196 | // Say this this object is dying |
| 1197 | objp->flags |= OF_DYING; |
| 1198 | |
| 1199 | // If object is a door, let the doorway system know |
| 1200 | if (objp->type == OBJ_DOOR) |
| 1201 | DoorwayDestroy(objp); |
| 1202 | |
| 1203 | // Set the delay if from anim |
| 1204 | if (death_flags & DF_DELAY_FROM_ANIM) |
| 1205 | delay_time = GetDeathAnimTime(objp); |
| 1206 | |
| 1207 | // Check for delay |
| 1208 | if (delay_time == 0.0) { // No delay. Dies now |
| 1209 | |
| 1210 | // If blast rings set, create 0-2 delayed blast rings |
| 1211 | if (death_flags & DF_BLAST_RING) { |
| 1212 | float ring_size = OBJECT_OUTSIDE(objp) ? (objp->size * 3) : objp->size; |
| 1213 | int extras = ps_rand() % 3; |
| 1214 | for (int i = 0; i < extras; i++) |
| 1215 | CreateNewEvent(0, 0, (i * .1) + .1, objp, sizeof(*objp), DoBlastRingEvent); |
| 1216 | } |
| 1217 | |
| 1218 | // Register a kill if the killer is a player |
| 1219 | if (killer && (killer->type == OBJ_PLAYER) && IS_GENERIC(objp->type)) |
| 1220 | PlayerScoreAdd(killer->id, Object_info[objp->id].score); |
| 1221 | |
| 1222 | // Destroy the object now |
| 1223 | DestroyObject(objp, damage * 3.0, death_flags); |
| 1224 | } else { // Some sort of delayed death |
| 1225 | |
| 1226 | // For upside-down walkers, force simple delay |
| 1227 | bool f_upsidedown_walker = (objp->movement_type == MT_WALKING) && (objp->orient.uvec.y < 0.0f); |
| 1228 | if (f_upsidedown_walker) |
| 1229 | death_flags |= DF_DELAY_LOSES_ANTIGRAV; |
| 1230 | |
| 1231 | // Set dying control |
| 1232 | int old_control_type = objp->control_type; |
| 1233 | SetObjectControlType(objp, CT_DYING); |
| 1234 | |
| 1235 | // Set dying flags |
| 1236 | objp->ctype.dying_info.death_flags = death_flags; |
| 1237 | objp->ctype.dying_info.last_smoke_time = -1; |
| 1238 | objp->ctype.dying_info.last_spark_time = -1; |
| 1239 | objp->ctype.dying_info.last_fireball_time = -1; |
| 1240 | |
| 1241 | // If the killer is a player, save so can add score when the object dies |
| 1242 | if (killer && (killer->type == OBJ_PLAYER) && IS_GENERIC(objp->type)) |
| 1243 | objp->ctype.dying_info.killer_playernum = killer->id; |
| 1244 | else |
| 1245 | objp->ctype.dying_info.killer_playernum = -1; |
| 1246 | |
| 1247 | // Set the timer |
| 1248 | objp->ctype.dying_info.delay_time = delay_time; |
| 1249 | |
| 1250 | // If loses anti-grav, set physics so object falls |
| 1251 | if (death_flags & DF_DELAY_LOSES_ANTIGRAV) |
| 1252 | SetFallingPhysics(objp); |
| 1253 | |
| 1254 | // Deal with some goal stuff. Ask Chris about this |
| 1255 | if ((objp->type == OBJ_ROBOT) || ((objp->type == OBJ_BUILDING) && objp->ai_info)) { |
| 1256 | if ((old_control_type == CT_AI) && |
| 1257 | (Object_info[objp->id].anim[objp->ai_info->movement_type].elem[AS_DEATH].to != 0.0f)) { |
| 1258 | SetObjectControlType(objp, CT_DYING_AND_AI); |
| 1259 | int next_anim = AS_DEATH; |
| 1260 | GoalAddGoal(objp, AIG_SET_ANIM, (void *)&next_anim, ACTIVATION_BLEND_LEVEL); |
| 1261 | mprintf(0, "Start dying anim "); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | // Deal with objects that fly |
| 1266 | if (death_flags & DF_DELAY_FLYING) { |
| 1267 | SetFlyingPhysics(objp, !(death_flags & DF_DELAY_NO_TUMBLE_FLY)); |
| 1268 | |
| 1269 | // Create a fireball |
| 1270 | if (death_flags & DF_FIREBALL) |
| 1271 | CreateObjectFireball(objp); |
| 1272 | } |
| 1273 | |
| 1274 | // Deal with objects that fade out |
| 1275 | if (death_flags & DF_DELAY_FADE_AWAY) { |
| 1276 | ASSERT(objp->effect_info != NULL); |
| 1277 | objp->effect_info->type_flags |= EF_FADING_OUT; |
| 1278 | objp->effect_info->fade_time = objp->effect_info->fade_max_time = objp->ctype.dying_info.delay_time; |
| 1279 | } |
| 1280 | |
| 1281 | // Play the sound now if flagged to do so |
| 1282 | if (death_flags & DF_DELAY_SOUND) |
| 1283 | PlayObjectExplosionSound(objp); |
| 1284 | // else if (death_flags & DF_DELAY_SPARKS) //If no special sound & doing sparks, play sparks sound |
| 1285 | // Sound_system.Play3dSound(SOUND_ELECTRICAL_DEATH, SND_PRIORITY_NORMAL, objp); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | // Applies damage to robot objects. Handled differently than damage to players for obvious reasons. |
| 1290 | bool ApplyDamageToGeneric(object *hit_obj, object *killer, int damage_type, float damage, int server_says, |
| 1291 | int weapon_id) { |
| 1292 | ASSERT(hit_obj != NULL); |
| 1293 | |
| 1294 | if (hit_obj->type == OBJ_DUMMY) { |
| 1295 | // as per ChrisP, dummies should be ignored here |
| 1296 | return false; |
| 1297 | } |
| 1298 | |
| 1299 | if (hit_obj->flags & OF_AI_DEATH) { |
| 1300 | return false; |
| 1301 | } |
| 1302 | |
| 1303 | // I don't think there's any problem with calling this function on an object that's not a generic |
| 1304 | // or a door, but I don't see why you would. |
| 1305 | ASSERT(IS_GENERIC(hit_obj->type) || (hit_obj->type == OBJ_DOOR)); |
| 1306 | |
| 1307 | object *weapon_obj = killer; |
| 1308 | |
| 1309 | if (damage_type != GD_SCRIPTED && |
| 1310 | !(IS_GENERIC(hit_obj->type) && (Object_info[hit_obj->id].flags & OIF_NO_DIFF_SCALE_DAMAGE))) |
| 1311 | damage *= Diff_robot_damage[DIFF_LEVEL]; |
| 1312 | |
| 1313 | if (killer != NULL && killer->type == OBJ_WEAPON) { |
| 1314 | killer = ObjGetUltimateParent(killer); |
| 1315 | |
| 1316 | if (!(Game_mode & GM_MULTI) || (Game_mode & GM_MULTI && Netgame.local_role == LR_SERVER)) |
| 1317 | weapon_id = weapon_obj->id; |
| 1318 | } |
| 1319 | |
| 1320 | if (hit_obj->flags & OF_DESTROYABLE && (!(hit_obj->flags & OF_USE_DESTROYED_POLYMODEL))) { |
| 1321 | if ((Game_mode & GM_MULTI) && (hit_obj->flags & OF_DESTROYED)) { |
| 1322 | return false; // This object has already been sent to the server to be blown up! |
| 1323 | } |
| 1324 | |
| 1325 | if (hit_obj->type == OBJ_POWERUP && (Game_mode & GM_MULTI)) |
| 1326 | return false; |
| 1327 | |
| 1328 | if ((!(Game_mode & GM_MULTI)) || server_says || ((Netgame.local_role == LR_SERVER) && (Game_mode & GM_MULTI))) { |
| 1329 | tOSIRISEventInfo ei; |
| 1330 | if (killer != NULL) |
| 1331 | ei.evt_damaged.it_handle = killer->handle; |
| 1332 | else |
| 1333 | ei.evt_damaged.it_handle = OBJECT_HANDLE_NONE; |
| 1334 | |
| 1335 | if (weapon_obj) |
| 1336 | ei.evt_damaged.weapon_handle = weapon_obj->handle; |
| 1337 | else |
| 1338 | ei.evt_damaged.weapon_handle = OBJECT_HANDLE_NONE; |
| 1339 | |
| 1340 | ei.evt_damaged.damage_type = damage_type; |
| 1341 | ei.evt_damaged.damage = damage; |
| 1342 | Osiris_CallEvent(hit_obj, EVT_DAMAGED, &ei); |
| 1343 | |
| 1344 | // This statement allows a robot to have special resistances or vulerabilites to damage types |
| 1345 | damage = ei.evt_damaged.damage; |
| 1346 | if (damage == 0.0f) { |
| 1347 | return false; |
| 1348 | } |
| 1349 | |
| 1350 | hit_obj->shields -= damage; |
| 1351 | |
| 1352 | if (Game_mode & GM_MULTI) { // only the server will be calling this |
| 1353 | if (killer != NULL) |
| 1354 | DLLInfo.it_handle = killer->handle; |
| 1355 | else |
| 1356 | DLLInfo.it_handle = OBJECT_HANDLE_NONE; |
| 1357 | |
| 1358 | DLLInfo.me_handle = hit_obj->handle; |
| 1359 | DLLInfo.fParam = damage; |
| 1360 | CallGameDLL(EVT_GAMEOBJECTSHIELDSCHANGED, &DLLInfo); |
| 1361 | } |
| 1362 | } |
| 1363 | if ((Game_mode & GM_MULTI) && (Netgame.local_role == LR_SERVER)) { |
| 1364 | // Let everyone know about the damage |
| 1365 | MultiSendDamageObject(hit_obj, killer, damage, weapon_id); |
| 1366 | } |
| 1367 | |
| 1368 | if ((hit_obj->shields < 0) && (hit_obj->flags & OF_AI_DO_DEATH)) { |
| 1369 | hit_obj->shields = 0; |
| 1370 | hit_obj->flags |= OF_AI_DEATH; |
| 1371 | } |
| 1372 | |
| 1373 | if ((hit_obj->shields < 0) && !(hit_obj->flags & OF_DYING)) { |
| 1374 | // CURRRENTLY - atttached objects remain attached when dying (it looks better) -- chrishack |
| 1375 | // if (!((Game_mode & GM_MULTI) && (Netgame.local_role == LR_CLIENT))) |
| 1376 | // { |
| 1377 | // // Attached objects have a 50% chance of falling off their parent when they die |
| 1378 | // if(ps_rand() >= (D3_RAND_MAX >> 1)) |
| 1379 | // { |
| 1380 | // UnattachFromParent(hit_obj); |
| 1381 | // } |
| 1382 | // } |
| 1383 | |
| 1384 | if (Game_mode & GM_MULTI) { |
| 1385 | |
| 1386 | DLLInfo.me_handle = hit_obj->handle; |
| 1387 | DLLInfo.it_handle = (killer) ? killer->handle : OBJECT_HANDLE_NONE; |
| 1388 | CallGameDLL(EVT_GAMEOBJKILLED, &DLLInfo); |
| 1389 | |
| 1390 | if (Netgame.local_role == LR_SERVER) |
| 1391 | KillObject(hit_obj, killer, damage); |
| 1392 | } else { |
| 1393 | // music hook |
| 1394 | if (killer && killer->type == OBJ_PLAYER && hit_obj->ai_info) { |
| 1395 | if (hit_obj->ai_info->awareness > AWARE_BARELY && hit_obj->ai_info->target_handle == killer->handle) |
| 1396 | Game_music_info.n_hostiles_player_killed++; |
| 1397 | } |
| 1398 | |
| 1399 | if (Demo_flags != DF_PLAYBACK) |
| 1400 | KillObject(hit_obj, killer, damage); |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | if ((!(Game_mode & GM_MULTI)) || server_says || ((Netgame.local_role == LR_SERVER) && (Game_mode & GM_MULTI))) { |
| 1405 | if (weapon_id != 255) { |
| 1406 | if (Weapons[weapon_id].flags & WF_MICROWAVE) |
| 1407 | SetDeformDamageEffect(hit_obj); |
| 1408 | if (Weapons[weapon_id].flags & WF_NAPALM) |
| 1409 | SetNapalmDamageEffect(hit_obj, killer, weapon_id); |
| 1410 | if (Weapons[weapon_id].flags & WF_FREEZE) |
| 1411 | ApplyFreezeDamageEffect(hit_obj); |
| 1412 | } |
| 1413 | return true; |
| 1414 | } |
| 1415 | return false; |
| 1416 | } else |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | // Adds a bit of shake to the camera |
| 1421 | void AddToShakeMagnitude(float delta) { |
| 1422 | |
| 1423 | ASSERT(delta >= 0.0); |
| 1424 | Shake_magnitude += delta; |
| 1425 | } |
| 1426 | |
| 1427 | // Sets a definite shake to the camera |
| 1428 | void SetShakeMagnitude(float delta) { |
| 1429 | |
| 1430 | ASSERT(delta >= 0.0); |
| 1431 | Shake_magnitude = delta; |
| 1432 | } |
| 1433 | |
| 1434 | #define MAX_SHAKE_MAGNITUDE 120 |
| 1435 | |
| 1436 | // Shakes player by some random amount |
| 1437 | void ShakePlayer() { |
| 1438 | if (Shake_magnitude > MAX_SHAKE_MAGNITUDE) |
| 1439 | Shake_magnitude = MAX_SHAKE_MAGNITUDE; |
| 1440 | |
| 1441 | float mag = Shake_magnitude; |
| 1442 | |
| 1443 | uint16_t pitch_adjust = ((ps_rand() % 5) - 2) * mag; |
| 1444 | uint16_t bank_adjust = ((ps_rand() % 5) - 2) * mag; |
| 1445 | uint16_t heading_adjust = ((ps_rand() % 5) - 2) * mag; |
| 1446 | matrix m, tempm; |
| 1447 | |
| 1448 | Old_player_orient = Player_object->orient; |
| 1449 | |
| 1450 | if (Shake_magnitude < .00001) |
| 1451 | return; |
| 1452 | |
| 1453 | vm_AnglesToMatrix(&m, pitch_adjust, heading_adjust, bank_adjust); |
| 1454 | |
| 1455 | DoForceForShake(Shake_magnitude); |
| 1456 | Shake_magnitude -= (Frametime * (MAX_SHAKE_MAGNITUDE / 3)); |
| 1457 | |
| 1458 | if (Shake_magnitude < 0) |
| 1459 | Shake_magnitude = 0.0; |
| 1460 | |
| 1461 | tempm = m * Player_object->orient; |
| 1462 | |
| 1463 | ObjSetOrient(Player_object, &tempm); |
| 1464 | } |
| 1465 | |
| 1466 | // Restores the player orientation matrix after shaking |
| 1467 | void UnshakePlayer() { ObjSetOrient(Player_object, &Old_player_orient); } |
| 1468 | |
| 1469 | #include "terrain.h" |
| 1470 | #include "collide.h" |
| 1471 | |
| 1472 | #define SHARD_MAX_EDGE_LEN 3.0f |
| 1473 | |
| 1474 | // Break the specified (glass) face into shards |
| 1475 | // Parameters: rp, facenum - the face to break. The face must be a portal face. |
| 1476 | // hitpnt - the point on the face where the face shatters. If NULL, uses center |
| 1477 | // point of |
| 1478 | // face hitvec - the direction in which the thing that's breaking the glass is |
| 1479 | // moving. If NULL, uses the negative of the surface normal |
| 1480 | void BreakGlassFace(room *rp, int facenum, vector *hitpnt, vector *hitvec) { |
| 1481 | int roomnum; |
| 1482 | vector t_hitpnt, t_hitvec; |
| 1483 | face *fp = &rp->faces[facenum]; |
| 1484 | |
| 1485 | ASSERT(fp->portal_num != -1); |
| 1486 | portal *pp0 = &rp->portals[fp->portal_num]; |
| 1487 | portal *pp1 = &Rooms[pp0->croom].portals[pp0->cportal]; |
| 1488 | |
| 1489 | // If face already broken, bail |
| 1490 | if (!(pp0->flags & PF_RENDER_FACES)) |
| 1491 | return; |
| 1492 | |
| 1493 | // Set hitpnt if not specified |
| 1494 | if (hitpnt == NULL) { |
| 1495 | ComputeCenterPointOnFace(&t_hitpnt, rp, facenum); |
| 1496 | hitpnt = &t_hitpnt; |
| 1497 | } |
| 1498 | |
| 1499 | // Set hitvec if not specified |
| 1500 | if (hitvec == NULL) { |
| 1501 | t_hitvec = -fp->normal; |
| 1502 | hitvec = &t_hitvec; |
| 1503 | } |
| 1504 | |
| 1505 | // Get roomnum |
| 1506 | if (rp->flags & RF_EXTERNAL) |
| 1507 | roomnum = MAKE_ROOMNUM(GetTerrainCellFromPos(hitpnt)); |
| 1508 | else |
| 1509 | roomnum = ROOMNUM(rp); |
| 1510 | |
| 1511 | // Play sound |
| 1512 | pos_state pos; |
| 1513 | pos.position = hitpnt; |
| 1514 | pos.orient = (matrix *)&Identity_matrix; |
| 1515 | pos.roomnum = roomnum; |
| 1516 | |
| 1517 | if (sound_override_glass_breaking == -1) |
| 1518 | Sound_system.Play3dSound(SOUND_BREAKING_GLASS, SND_PRIORITY_HIGH, &pos); |
| 1519 | else |
| 1520 | Sound_system.Play3dSound(sound_override_glass_breaking, SND_PRIORITY_HIGH, &pos); |
| 1521 | |
| 1522 | // Clear render flags |
| 1523 | pp0->flags &= ~PF_RENDER_FACES; |
| 1524 | pp1->flags &= ~PF_RENDER_FACES; |
| 1525 | |
| 1526 | // Get the UV for the hit point |
| 1527 | float hitpnt_u, hitpnt_v; |
| 1528 | FindHitpointUV(&hitpnt_u, &hitpnt_v, hitpnt, rp, facenum); |
| 1529 | |
| 1530 | // Calculate shard movement vector |
| 1531 | vector shardvec = *hitvec; |
| 1532 | vm_NormalizeVectorFast(&shardvec); |
| 1533 | |
| 1534 | // Create shards |
| 1535 | vector prevpoint = rp->verts[fp->face_verts[fp->num_verts - 1]]; |
| 1536 | float prev_u = fp->face_uvls[fp->num_verts - 1].u, prev_v = fp->face_uvls[fp->num_verts - 1].v; |
| 1537 | for (int vertnum = 0; vertnum < fp->num_verts;) { |
| 1538 | int objnum; |
| 1539 | vector curpoint = rp->verts[fp->face_verts[vertnum]], center; |
| 1540 | float cur_u = fp->face_uvls[vertnum].u, cur_v = fp->face_uvls[vertnum].v; |
| 1541 | |
| 1542 | // Check if should split edge |
| 1543 | if (vm_VectorDistanceQuick(&prevpoint, &curpoint) > SHARD_MAX_EDGE_LEN) { |
| 1544 | curpoint = (curpoint + prevpoint) / 2; |
| 1545 | cur_u = (cur_u + prev_u) / 2; |
| 1546 | cur_v = (cur_v + prev_v) / 2; |
| 1547 | } else // no split |
| 1548 | vertnum++; |
| 1549 | |
| 1550 | // Calculate the center point |
| 1551 | center = (curpoint + prevpoint + *hitpnt) / 3.0; |
| 1552 | |
| 1553 | // Create the object |
| 1554 | objnum = ObjCreate(OBJ_SHARD, 0, roomnum, ¢er, NULL); |
| 1555 | |
| 1556 | // Setup shard info |
| 1557 | if (objnum != -1) { |
| 1558 | object *objp = &Objects[objnum]; |
| 1559 | shard_info_s *si = &objp->rtype.shard_info; |
| 1560 | |
| 1561 | si->points[0] = prevpoint - objp->pos; |
| 1562 | si->points[1] = curpoint - objp->pos; |
| 1563 | si->points[2] = *hitpnt - objp->pos; |
| 1564 | |
| 1565 | si->u[0] = prev_u; |
| 1566 | si->v[0] = prev_v; |
| 1567 | si->u[1] = cur_u; |
| 1568 | si->v[1] = cur_v; |
| 1569 | si->u[2] = hitpnt_u; |
| 1570 | si->v[2] = hitpnt_v; |
| 1571 | |
| 1572 | si->normal = fp->normal; |
| 1573 | si->tmap = fp->tmap; |
| 1574 | |
| 1575 | // Set velocity |
| 1576 | objp->mtype.phys_info.velocity = shardvec * (50.0 + 100.0 * (objnum % 3)); |
| 1577 | |
| 1578 | // Set rotational velocity |
| 1579 | objp->mtype.phys_info.rotvel.x = 25000.0 * (objnum % 5); |
| 1580 | objp->mtype.phys_info.rotvel.y = 25000.0 * (objnum % 2); |
| 1581 | objp->mtype.phys_info.rotvel.z = 25000.0 * (objnum % 3); |
| 1582 | |
| 1583 | prevpoint = curpoint; |
| 1584 | prev_u = cur_u; |
| 1585 | prev_v = cur_v; |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | // Do multiplayer stuff |
| 1590 | if ((Game_mode & GM_MULTI) && Netgame.local_role == LR_SERVER) { |
| 1591 | MultiSendBreakGlass(rp, facenum); |
| 1592 | |
| 1593 | if (Num_broke_glass != MAX_BROKE_GLASS) { |
| 1594 | Broke_glass_rooms[Num_broke_glass] = rp - Rooms; |
| 1595 | Broke_glass_faces[Num_broke_glass] = facenum; |
| 1596 | Num_broke_glass++; |
| 1597 | } |
| 1598 | } |
| 1599 | } |