Debian Codegraph @

Descent3/fireball.cpp

raw symbols SCIP
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/fireball.cpp $
21 * $Revision: 241 $
22 * $Date: 4/19/00 5:07p $
23 * $Author: Matt $
24 *
25 * Fireball code
26 *
27 * $Log: /DescentIII/main/fireball.cpp $
28 *
29 * 241 4/19/00 5:07p Matt
30 * From Duane for 1.4
31 * Added checks, asserts, and fixes for bad return values
32 *
33 * 240 3/22/00 12:13p Matt
34 * Added some if statements to not damage & kill objects when playing back
35 * a demo.
36 *
37 * 239 3/20/00 12:07p Matt
38 * Merge of Duane's post-1.3 changes.
39 * Check for no available vis effects.
40 *
41 * 238 10/24/99 1:30a 3dsmax
42 * if submodel face has 0 vertices, bail out nicely when creating visfx
43 * electric bolts.
44 *
45 * 237 10/12/99 11:01a Jeff
46 * added grey lightning
47 *
48 * 236 9/21/99 2:54p Jeff
49 * added grey spark fireball (for multicolored sparks)
50 *
51 * 235 7/28/99 4:15p Kevin
52 * Macintosh
53 *
54 * 234 5/22/99 10:27p Jason
55 * changes for multiplayer and buildings, ai
56 *
57 * 233 5/22/99 12:40a Matt
58 * Don't create spliters for non-renderable submodels. (This fixed the
59 * error texture that was sometimes visible when a blastable grate door
60 * blew up.)
61 *
62 * 232 5/21/99 7:53p Jason
63 * fixed bug where powerups in multiplayer would get out of sync due to
64 * blackshark
65 *
66 * 231 5/10/99 10:22p Ardussi
67 * changes to compile on Mac
68 *
69 * 230 5/10/99 8:28p Jason
70 * made blackshark more interesting indoors
71 *
72 * 229 5/10/99 1:53a Jason
73 * add blackshark explosion
74 *
75 * 228 5/07/99 7:30p Chris
76 * Check for backfaces with concussive forces
77 *
78 * 227 5/06/99 7:18p Matt
79 * Fixed turret object names.
80 *
81 * 226 5/06/99 5:44p Matt
82 * Added three more dead turrets
83 *
84 * 225 5/06/99 3:16a Matt
85 * Added semi-hacked dead model for the Swatter turret.
86 *
87 * 224 5/05/99 2:33p Jason
88 * fixed some multiplayer issues with client objects
89 *
90 * 223 5/02/99 7:42p Jason
91 * added fireball error checking
92 *
93 * 222 5/01/99 4:59p Jason
94 * change blue blast ring
95 *
96 * 221 4/30/99 5:42p Jason
97 * changes for blast rings
98 *
99 * 220 4/29/99 2:16a Samir
100 * adjusted some sound priority stuff
101 *
102 * 219 4/23/99 12:32a Matt
103 * Deleted cut-and-paste-error comments.
104 *
105 * 218 4/23/99 12:27a Matt
106 * Moved the code that plays an object's explosion sound into a function.
107 *
108 * 217 4/22/99 8:29p Kevin
109 * made psrand.h come after stdlib.h
110 *
111 * 216 4/21/99 5:31p Jason
112 * changed smoke chances
113 *
114 * 215 4/21/99 3:01p Matt
115 * Added a new type for dying objects that have AI, instead of keeping a
116 * flag in the dying info.
117 *
118 * 214 4/21/99 12:41p Jason
119 * make explosion system framerate independant
120 *
121 * 213 4/21/99 11:05a Kevin
122 * new ps_rand and ps_srand to replace rand & srand
123 *
124 * 212 4/19/99 3:45a Jeff
125 * fixed min/max problem
126 *
127 * 211 4/18/99 10:55p Chris
128 * Added ignore own concussive blasts
129 *
130 * 210 4/14/99 2:50a Jeff
131 * fixed some case mismatched #includes
132 *
133 * 209 4/10/99 5:09p Samir
134 * prioritize sounds, pass 1
135 *
136 * 208 4/06/99 6:53p Jason
137 * fixed source safe bug
138 *
139 * 207 4/06/99 6:02p Matt
140 * Added score system
141 *
142 * 205 4/05/99 4:39p Jason
143 * added groovy new smoke trails
144 *
145 * 204 4/02/99 3:57p Jason
146 * sped up vis effect stuff
147 *
148 * 203 4/01/99 10:10p Matt
149 * Re-added my death system changes from version 197 that Jason so kindly
150 * blew away with version 198.
151 *
152 * 202 4/01/99 5:39p Matt
153 * Fixed (hopefully) bug introduced in last version.
154 *
155 * 201 4/01/99 4:28p Matt
156 * Cleaned up handling of dying flag to prevent infinite recursion. Jeff
157 * will test.
158 *
159 * 200 4/01/99 1:10p Jason
160 * made vis effects better for the lighting system
161 *
162 * 199 3/31/99 11:40a Jason
163 * added support for attached thick lightning
164 *
165 * 198 3/29/99 7:30p Jason
166 * added cool new energy effect
167 *
168 * 196 3/27/99 2:15p Jason
169 * added better thick lightining
170 *
171 * 195 3/25/99 3:29p Jason
172 * added option to randomize powerup respawn points
173 *
174 * 194 3/24/99 5:54p Jason
175 * fixed size for thick lightning
176 *
177 * 193 3/24/99 12:34p Jason
178 * fixed concussive damage bailing out too early
179 *
180 * 192 3/09/99 1:35p Jason
181 * made blackshark not suck in energy weapons
182 *
183 * 191 3/05/99 6:42p Matt
184 * On death, only do shockwave and shake if an object breaks apart or has
185 * fireballs. Also, check for no sound before calling play sound.
186 *
187 * 190 3/05/99 3:32p Matt
188 * Made 'remains' work for object deaths.
189 *
190 * 189 3/02/99 4:18p Jason
191 * fixed black smoke bug
192 *
193 * 188 3/01/99 11:28a Matt
194 * Made debris radius half the size of the polymodel, so it sits closer to
195 * the floor when it stops bouncing.
196 *
197 * 187 2/28/99 5:31p Matt
198 * Removed mprintf()
199 *
200 * 186 2/26/99 3:52p Jason
201 * fixed mprintf
202 *
203 * 185 2/26/99 2:03p Matt
204 * Got blastable doors working, and cleaned up concussive force code.
205 *
206 * 184 2/25/99 10:58a Matt
207 * Added new explosion system.
208 *
209 * 183 2/23/99 10:50a Matt
210 * Check for spew type of -1 is DoDeathSpew()
211 *
212 * 182 2/22/99 2:03p Jason
213 * added different damages for players and generics
214 *
215 * 181 2/13/99 12:30a Jeff
216 * an OBJ_DUMMY shouldn't be blown up...Int3 put in to catch this
217 *
218 * 180 2/10/99 3:38p Jason
219 * table file filter fixups
220 *
221 * 179 2/10/99 1:47p Matt
222 * Changed object handle symbolic constants
223 *
224 * 178 2/09/99 2:38p Jason
225 * added destroyable faces
226 *
227 * 177 2/09/99 9:58a Chris
228 * Massive BOA update :) Terrain happy now. Vis happy now. Sound happy
229 * now.
230 *
231 * 176 2/06/99 10:30p Jason
232 * fixed some blackshark stuff
233 *
234 * 175 2/05/99 1:38p Jason
235 * dropped splinter count
236 *
237 * 174 2/02/99 8:43a Chris
238 * I made buildings with AI work correctly (ie really big robots should be
239 * buildings)
240 * anim to and from states are now shorts instead of bytes
241 *
242 * 173 2/01/99 10:16a Chris
243 * Fixed mass prop. bug and accounted for the fact that we hack in a new
244 * mass for debris
245 *
246 * 172 1/29/99 5:09p Chris
247 * Made changes for ROCKS
248 *
249 * 171 1/26/99 6:39p Jason
250 * added wall effects code
251 *
252 * 170 1/21/99 11:15p Jeff
253 * pulled out some structs and defines from header files and moved them
254 * into seperate header files so that multiplayer dlls don't require major
255 * game headers, just those new headers. Side effect is a shorter build
256 * time. Also cleaned up some header file #includes that weren't needed.
257 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
258 * manage.h and multi.h
259 *
260 * 169 1/20/99 2:13a Chris
261 * It is now possible for robots to have special immunities, resistances,
262 * and vunerabilities
263 *
264 * 168 1/18/99 2:46p Matt
265 * Combined flags & flags2 fields in object struct
266 *
267 * 167 12/13/98 9:18p Chris
268 * Improved influence values for in-code goals (10000 to 1.0). Added
269 * GF_ORIENT stuff. :)
270 *
271 * 166 12/13/98 7:17p Chris
272 * Improved walker death code
273 *
274 * 165 12/11/98 5:08p Jason
275 * changed mass driver effect
276 *
277 * 164 11/30/98 6:02p Jay
278 * Fixed a bug with concussive forces being applied to room objects
279 *
280 * 163 11/19/98 6:37p Jason
281 * made a better black shark effect
282 *
283 * 162 11/19/98 4:35p Chris
284 * tweaked it
285 *
286 * 161 11/19/98 4:21p Chris
287 * Got rid of some wierd rotation cases with slow deaths from being killed
288 * with the mass driver
289 *
290 * 160 11/19/98 12:22p Jason
291 * optimizations
292 *
293 * 159 11/18/98 2:53p Jason
294 * added snow effect
295 *
296 * 158 11/16/98 6:03p Jason
297 * took out some comments
298 *
299 * 157 11/11/98 2:46p Kevin
300 * Demo recording system work
301 *
302 * 156 10/29/98 6:01p Jason
303 * added multiple coronas
304 *
305 * 155 10/28/98 11:51a Jason
306 * fixed some multiplayer problems, plus redid coop a bit to make it
307 * cleaner
308 *
309 * 154 10/22/98 5:40p Matt
310 * If no viewer object, bail from several fireball funtions that use that
311 * variable.
312 *
313 * 153 10/19/98 7:18p Matt
314 * Added system to support different types of damage to the player and
315 * have these different types make different sounds.
316 *
317 * 152 10/17/98 3:14p Jason
318 * made electrical death less deforming
319 *
320 * 150 10/16/98 1:41p Jason
321 * changes for the demo
322 *
323 * 149 10/15/98 11:09a Chris
324 * Added visibility checks for concussive blasts
325 *
326 * 148 10/12/98 9:28a Jason
327 * fixed some rendering bugs
328 *
329 * 147 10/07/98 6:55p Jason
330 * added better lightning death
331 *
332 * 146 10/07/98 5:17p Jason
333 * added new invul hit effect
334 *
335 * 145 10/06/98 11:41a Jason
336 * tweaked electrical death
337 *
338 * 144 10/06/98 11:27a Jason
339 * added new death type for robots
340 *
341 * 143 9/29/98 7:15p Jason
342 * added axis billboards
343 *
344 * 142 9/29/98 12:49p Jason
345 * worked on matcen effects and lightning
346 *
347 * 141 9/18/98 8:23p Jason
348 * fixed insidious vis effect errors
349 *
350 * 140 9/17/98 3:03p Jason
351 * added lightning and invul hit effects
352 *
353 * 139 9/11/98 6:40p Jason
354 * added smoke puffs
355 *
356 * 138 9/09/98 4:49p Jason
357 * tweaked black shark lightning effect
358 *
359 * 137 9/09/98 4:28p Jason
360 * added better weapon effects
361 *
362 * 136 8/31/98 5:29p Jason
363 * made explosions better with addition of sound effects
364 *
365 * 135 8/21/98 12:29p Jason
366 * made smoke puffs draw more inside the mine
367 *
368 * 134 8/20/98 4:59p Nate
369 * Limitted the velocity of created debris to +-100000.0f
370 *
371 * 133 8/19/98 2:49p Jason
372 * Fixed black smoked to be more noticable
373 *
374 * 132 8/17/98 10:48a Jason
375 * added more lightning code
376 *
377 * 131 8/17/98 1:32a Chris
378 * Fixed a type cast bug the resulted in rand_vec.y always equaling .4
379 * and then zero after normalization (as the others ranged from D3_RAND_MAX/2
380 * to -D3_RAND_MAX/2)
381 *
382 * 130 8/17/98 1:15a Chris
383 * By default, obj_debris don't make a sound when they die
384 *
385 * 129 8/16/98 6:17p Chris
386 * Added generic object spewing code
387 *
388 * 128 8/10/98 5:24p Jason
389 * don't do blast flash if you can't see the blast
390 *
391 * 127 8/03/98 11:12a Jason
392 * fixed some zero foward vector errors
393 *
394 * 126 7/24/98 6:08p Jason
395 * added some more procedurals
396 *
397 * 125 7/24/98 4:45p Jason
398 * added DrawSphere
399 *
400 * 124 6/29/98 5:06p Jason
401 * made a somewhat better gravity explosion
402 *
403 *
404 * 123 6/25/98 5:17p Jason
405 * checked in for mark to check out warp effect
406 *
407 * 122 6/25/98 12:19p Jason
408 * made gravity effect do different damage in single vs multiplayer
409 *
410 * 121 6/23/98 3:34p Jason
411 * added cool lighting effect to gravity field
412 *
413 * 120 6/22/98 6:52p Jason
414 * changed gravity field effect somewhat
415 *
416 * 119 6/22/98 6:26p Jason
417 * added gravity field effect for weapons
418 *
419 * 118 6/15/98 7:45a Chris
420 * FIxed a bug with making shockwaves
421 *
422 * 117 5/27/98 5:53p Chris
423 * No more flying turrets
424 *
425 * 116 5/26/98 6:54p Jason
426 * viseffect optimizations
427 *
428 * 115 5/26/98 3:36p Chris
429 * Tweaked explosions
430 *
431 * 114 5/26/98 3:24p Chris
432 * Oops. I was corrupting the orientation
433 *
434 * 113 5/19/98 4:42a Chris
435 * Added shockwave's -- enjoy. :)
436 *
437 * 112 5/15/98 4:21p Chris
438 * Oops
439 *
440 * 111 5/15/98 3:53p Keneta
441 * Make objects on ceiling not fly upward
442 *
443 * 110 5/14/98 7:40p Samir
444 * shaking cockpit.
445 *
446 * 109 5/14/98 2:17p Jason
447 * more changes to be memory friendly
448 *
449 * 108 5/11/98 4:40p Chris
450 * AI info is now a malloc'ed pointer
451 *
452 * 107 5/01/98 6:23p Jason
453 * made better puddle rain
454 *
455 * 106 5/01/98 3:41p Chris
456 *
457 * 105 5/31/98 3:12p Chris
458 * Made objects die with death anims.
459 *
460 * 104 5/31/98 11:01a Chris
461 * Made debris less heavy
462 *
463 * 103 4/30/98 6:45p Jason
464 * more changes for weather
465 *
466 * 102 4/30/98 3:39p Jason
467 * don't make non-billowingfireballs rotate
468 *
469 * 101 4/30/98 12:22p Jason
470 * did some lo-res model optimizations
471 *
472 * 100 4/29/98 11:38a Jason
473 * added some weather effects (first pass)
474 *
475 * 99 4/22/98 3:24p Jason
476 * fixed colored disk problem
477 *
478 * 98 4/17/98 12:45p Jason
479 * various changes for multiplayer
480 *
481 * 97 4/15/98 3:28p Jason
482 * changed glow stuff to work with new system
483 *
484 * 96 4/15/98 2:33p Jason
485 * changed some smoke trail stuff
486 *
487 * 95 4/13/98 4:19p Chris
488 * Added PF_IGNORE_CONCUSSIVE_FORCES
489 *
490 * 94 4/10/98 2:17p Jason
491 * added afterburner fireball
492 *
493 * 93 4/09/98 5:17p Jason
494 * got guided working in multiplayer
495 *
496 * 92 4/07/98 3:31p Jason
497 * got particle effects working with weapons
498 *
499 * 91 4/06/98 1:35p Chris
500 * Fixed a multiplayer bug
501 *
502 * 90 4/02/98 6:42p Jason
503 * added blue blast ring effect
504 *
505 * 89 4/02/98 3:54p Jason
506 * first pass in getting polymodel paging to work
507 *
508 * 88 3/31/98 12:16p Jason
509 * some small changes for explosions
510 *
511 * 87 3/27/98 4:46p Jason
512 * changes for voodoo2
513 *
514 * 86 3/23/98 4:54p Chris
515 * Fixed problem with powerups getting concussive force
516 *
517 * 85 3/20/98 9:35p Jason
518 * fixed stupid error with last rev
519 *
520 * 83 3/20/98 5:51p Jason
521 * more changes for multiplayer
522 *
523 * 82 3/19/98 3:44p Chris
524 *
525 * 81 3/19/98 3:44p Chris
526 * Multiplayer enchancement for collisions
527 *
528 * 80 3/19/98 1:25p Chris
529 * Improved size stuff used for concussive effects
530 *
531 * 79 3/19/98 1:17p Chris
532 * Concussive blasts effect weapons and not powerups
533 *
534 * 78 3/13/98 4:17p Jason
535 * took out mprintf
536 *
537 * 77 3/12/98 7:30p Chris
538 * Added ObjSetOrient
539 *
540 * 76 3/12/98 7:14p Jason
541 * fixed FOV bug
542 *
543 * 75 3/09/98 5:58p Jason
544 * draw powerups with saturated alpha rings
545 *
546 * 74 3/06/98 4:09p Jason
547 * don't do glows if not in hardware
548 *
549 * 73 2/27/98 11:56a Chris
550 * Improved the distance formula for concussive blasts
551 *
552 * 72 2/25/98 4:59p Jason
553 * got dynamic explosion lighting working with vis-effects
554 *
555 * 71 2/25/98 4:31p Jason
556 * changes for explosions
557 *
558 * 70 2/19/98 6:20p Chris
559 * FindPointRoom() call is not for in-game.
560 *
561 * 69 2/19/98 1:10p Jason
562 * toned down building explosions a bit
563 *
564 * 68 2/16/98 2:24p Jason
565 * fixed bad indexing into subobjects problem
566 *
567 * 67 2/10/98 6:03p Jason
568 * took out ugly blast sphere graphic
569 *
570 * 66 2/05/98 2:54p Jason
571 * changes for explosions
572 *
573 * 65 2/04/98 9:28p Jason
574 * added some new weapons effects
575 *
576 * 64 2/04/98 6:09p Matt
577 * Changed object room number to indicate a terrain cell via a flag. Got
578 * rid of the object flag which used to indicate terrain.
579 *
580 */
581 #include <memory.h>
582 #include "pstypes.h"
583 #include "pserror.h"
584 #include "fireball.h"
585 #include "vclip.h"
586 #include "object.h"
587 #include "cockpit.h"
588 #include "game.h"
589 #include "3d.h"
590 #include "mono.h"
591 #include "room.h"
592 #include "polymodel.h"
593 #include "objinfo.h"
594 #include "gametexture.h"
595 #include "splinter.h"
596 #include "physics.h"
597 #include "damage.h"
598 #include "gameevent.h"
599 #include "weapon.h"
600 #include "viseffect.h"
601 #include "spew.h"
602 #include "hlsoundlib.h"
603 #include "sounds.h"
604 #include "gameloop.h"
605 #include "multi.h"
606 #include "AIGoal.h"
607 #include "AIMain.h"
608 #include "ship.h"
609 #include "BOA.h"
610 #include "demofile.h"
611 #include "ObjScript.h"
612 #include <stdlib.h>
613 #include <string.h>
614 #include "psrand.h"
615
616 #include <algorithm>
617
618 static object *CreateSubobjectDebris(object *parent, int subobj_num, float explosion_mag, int death_flags);
619 static void DrawSmolderingObject(object *obj);
620 static int GetRandomExplosion(float size);
621 static void CreateBlueBlastRing(vector *pos, int index, float lifetime, float max_size, int roomnum, int objnum,
622 int force_up);
623
624 // If an objects size is bigger than this, we create size/threshold extra explosions
625 #define EXTRA_EXPLOSION_THRESHOLD 15
626 fireball Fireballs[NUM_FIREBALLS] = {
627 {"ExplosionAA.oaf", FT_EXPLOSION, SMALL_TEXTURE, .9f, 3.0}, // MED_EXPLOSION2
628 {"ExplosionBB.oaf", FT_EXPLOSION, SMALL_TEXTURE, .9f, 2.0}, // SMALL_EXPLOSION2
629 {"explosionCC.oaf", FT_EXPLOSION, SMALL_TEXTURE, .9f, 3.0f}, // MED_EXPLOSION
630 {"explosionDD.oaf", FT_EXPLOSION, SMALL_TEXTURE, .9f, 3.0f}, // MED_EXPLOSION3
631 {"ExplosionE.oaf", FT_EXPLOSION, SMALL_TEXTURE, .9f, 3.0}, // BIG_EXPLOSION
632 {"ExplosionFF.oaf", FT_EXPLOSION, SMALL_TEXTURE, 1.0f, 1.0f}, // BILLOWING
633 {"explosionG.oaf", FT_EXPLOSION, SMALL_TEXTURE, 1.0f, 2.0f}, // SMALL_EXPLOSION_INDEX
634 {"smokepuff.oaf", FT_SMOKE, SMALL_TEXTURE, .7f, .7f}, // MED_SMOKE_INDEX
635 {"black_smoke.oaf", FT_SMOKE, SMALL_TEXTURE, .7f, 1.0f}, // BLACK_SMOKE
636 {"BlastRingOrange.ogf", FT_EFFECT, SMALL_TEXTURE, 1.0, 1.0}, // RED_BLAST_RING
637 {"smokepuff.oaf", FT_SMOKE, SMALL_TEXTURE, .4f, .7f}, // SMOKE_TRAIL
638 {"smokepuff.oaf", FT_EXPLOSION, SMALL_TEXTURE, .7f, 3.0}, // CUSTOM_EXPLOSION
639 {"explosionblast2.ogf", FT_EXPLOSION, NORMAL_TEXTURE, .7f, .7f}, // SHRINKING_BLAST
640 {"black_smoke.oaf", FT_SMOKE, SMALL_TEXTURE, .7f, 1.0f}, // SMOLDERING
641 {"warp.oaf", FT_EFFECT, NORMAL_TEXTURE, 1.0, 1.0}, // SHRINKING_BLAST2
642 {"Hotspark.ogf", FT_SPARK, SMALL_TEXTURE, 1.0, 1.0}, // HOT_SPARK
643 {"Coolspark.ogf", FT_SPARK, SMALL_TEXTURE, 1.0, 1.0}, // COOL_SPARK
644 {"thrustball.ogf", FT_EFFECT, SMALL_TEXTURE, 1.0, 1.0}, // GRADIENT_BALL
645 {"NOIMAGE", FT_EFFECT, SMALL_TEXTURE, .7f, 3.0}, // SPRAY
646 {"NOIMAGE", FT_EFFECT, SMALL_TEXTURE, .7f, 3.0}, // FADING_LINE
647 {"muzzleflash.ogf", FT_EFFECT, SMALL_TEXTURE, .7f, 3.0}, // MUZZLE_FLASH
648 {"shiphit.ogf", FT_EFFECT, NORMAL_TEXTURE, .7f, 3.0}, // SHIP HIT EFFECT
649 {"BlastRingBlue.ogf", FT_EFFECT, SMALL_TEXTURE, .7f, 3.0}, // BLUE SHIELD RING
650 {"NOIMAGE", FT_EFFECT, SMALL_TEXTURE, .7f, 3.0}, // PARTICLE
651 {"explosion.oaf", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // AFTERBURNER
652 {"NOIMAGE", FT_EXPLOSION, SMALL_TEXTURE, 1.0f, 2.0f}, // NAPALM BALL
653 {"LightningOriginA.ogf", FT_EXPLOSION, SMALL_TEXTURE, 1.0f, 2.0f}, // LIGHTNING ORIGINA
654 {"LightningOriginB.ogf", FT_EXPLOSION, SMALL_TEXTURE, 1.0f, 2.0f}, // LIGHTNING ORIGINB
655 {"Raindrop.ogf", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // Windshield drop
656 {"Puddle.ogf", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // Puddle drop
657 {"NOIMAGE", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // Gravity effect
658 {"NOIMAGE", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // LIGHTNING_BOLT_INDEX
659 {"InvulnerabilityHit.ogf", FT_EFFECT, NORMAL_TEXTURE, 1.0f, 2.0f}, // Invul shield hit effect
660 {"NOIMAGE", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // SINE_WAVE_INDEX
661 {"NOIMAGE", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // AXIS_BILLBOARD_INDEX
662 {"StarFlare6.ogf", FT_EFFECT, NORMAL_TEXTURE, 1.0f, 2.0f}, // DEFAULT_CORONA
663 {"HeadlightFlare.ogf", FT_EFFECT, NORMAL_TEXTURE, 1.0f, 2.0f}, // HEADLIGHT_CORONA
664 {"StarFlare.ogf", FT_EFFECT, NORMAL_TEXTURE, 1.0f, 2.0f}, // STAR_CORONA
665 {"SunFlare.ogf", FT_EFFECT, NORMAL_TEXTURE, 1.0f, 2.0f}, // SUN_CORONA
666 {"Whiteball.ogf", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // SNOWFLAKE_INDEX
667 {"NOIMAGE", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // THICK_LIGHTNING_INDEX
668 {"NapalmFire.oaf", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // BLUE_FIRE_INDEX
669 {"Rocklette1.ogf", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // RUBBLE1_INDEX
670 {"Rocklette2.ogf", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // RUBBLE2_INDEX
671 {"Whiteball.ogf", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // WATER_SPLASH_INDEX
672 {"lg.oaf", FT_EFFECT, SMALL_TEXTURE, 1.0f, 2.0f}, // SHATTER_INDEX
673 {"lg.oaf", FT_EFFECT, SMALL_TEXTURE, 1.0f, 2.0f}, // SHATTER_INDEX2
674 {"NOIMAGE", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // BILLBOARD_SMOKETRAIL_INDEX
675 {"NOIMAGE", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}, // MASSDRIVER_EFFECT_INDEX
676 {"ExplosionBlkShrk.oaf", FT_EXPLOSION, SMALL_TEXTURE, .9f, 3.0}, // BLUE_EXPLOSION_INDEX
677 {"Coolspark.ogf", FT_SPARK, SMALL_TEXTURE, 1.0, 1.0}, // GRAY_SPARK_INDEX
678 {"NOIMAGE", FT_EFFECT, TINY_TEXTURE, 1.0f, 2.0f}}; // GRAY_LIGHTNING_BOLT_INDEX
679
680 // Initializes the explosion system, loading needed bitmaps, etc
681 void InitFireballs() {
682 int i, num_fireballs = NUM_FIREBALLS;
683 for (i = 0; i < num_fireballs; i++) {
684 if (!(stricmp(Fireballs[i].name, "NOIMAGE")))
685 continue;
686 int bm_handle = LoadTextureImage(IGNORE_TABLE(Fireballs[i].name), NULL, Fireballs[i].tex_size, 0);
687 if (bm_handle < 0) {
688 Error("Couldn't load fireball %s in InitFireballs.", Fireballs[i].name);
689 return;
690 }
691 ASSERT(bm_handle >= 0);
692 Fireballs[i].bm_handle = bm_handle;
693 }
694
695 // convert the grey spark into grayscale
696 uint16_t *data;
697 int size;
698 float recip32 = 1.0f / 32.0f;
699 data = bm_data(Fireballs[GRAY_SPARK_INDEX].bm_handle, 0);
700 size = bm_w(Fireballs[GRAY_SPARK_INDEX].bm_handle, 0) * bm_h(Fireballs[GRAY_SPARK_INDEX].bm_handle, 0);
701 for (i = 0; i < size; i++) {
702 uint16_t col565 = data[i];
703 if (col565 == 0x07e0) {
704 data[i] = NEW_TRANSPARENT_COLOR;
705 } else {
706 uint8_t r = (uint8_t)((col565 & 0xf800) >> 11);
707 uint8_t g = (uint8_t)((col565 & 0x07c0) >> 6);
708 uint8_t b = (uint8_t)(col565 & 0x001f);
709 float brightness = ((r * 0.30f) + (g * 0.59f) + (b * 0.11f)) * recip32;
710 uint8_t elem = (uint8_t)(255.0f * brightness);
711 if (brightness > 1.0f)
712 elem = 255;
713 data[i] = GR_RGB16(elem, elem, elem) | OPAQUE_FLAG;
714 }
715 }
716 GameBitmaps[Fireballs[GRAY_SPARK_INDEX].bm_handle].flags |= BF_CHANGED;
717 }
718
719 // Given an object, renders the representation of this fireball
720 void DrawFireballObject(object *obj) {
721 ASSERT(obj->type == OBJ_FIREBALL);
722
723 // First check to see if these are special types
724 if (obj->id == BLAST_RING_INDEX) {
725 DrawBlastRingObject(obj);
726 return;
727 }
728 if (obj->id == GRAVITY_FIELD_INDEX) {
729 // Draw two blast rings
730 matrix tempm;
731 vm_MakeIdentity(&tempm);
732 ObjSetOrient(obj, &tempm);
733 DrawBlastRingObject(obj);
734 return;
735 }
736 if (obj->id == SMOLDERING_INDEX) {
737 DrawSmolderingObject(obj);
738 return;
739 }
740 float norm_time;
741 float time_live = Gametime - obj->creation_time;
742 float size = obj->size;
743
744 int objnum = obj - Objects;
745 int rot_angle;
746 int bm_handle;
747 if (Fireballs[obj->id].type == FT_BILLOW)
748 rot_angle = ((objnum * 5000) + (FrameCount * 160)) % 65536;
749 else
750 rot_angle = ((objnum * 5000) + (FrameCount * 30)) % 65536;
751
752 norm_time = time_live / obj->lifetime;
753 if (norm_time >= 1)
754 norm_time = .99999f; // don't go over!
755 if (obj->id == SMOKE_TRAIL_INDEX) // If its a smoke trail, get image from texture
756 {
757 int texnum = obj->ctype.blast_info.bm_handle;
758 if (GameTextures[texnum].flags & TF_ANIMATED) {
759 PageInVClip(GameTextures[texnum].bm_handle);
760 vclip *vc = &GameVClips[GameTextures[texnum].bm_handle];
761 int int_frame = vc->num_frames * norm_time;
762 bm_handle = vc->frames[int_frame];
763 } else
764 bm_handle = GameTextures[texnum].bm_handle;
765 } else if (obj->id == CUSTOM_EXPLOSION_INDEX) // Do custom
766 {
767 bm_handle = GetTextureBitmap(obj->ctype.blast_info.bm_handle, 0);
768 if (!(GameTextures[obj->ctype.blast_info.bm_handle].flags & TF_ANIMATED)) {
769 // If this is just a single bitmap, scale it based on time
770 if (norm_time < .5) // Ramp up quickly
771 {
772 float temp_norm = norm_time / .2;
773 temp_norm = std::min(1.0f, temp_norm);
774 size *= temp_norm;
775 } else // ramp down slowly
776 {
777 float temp_norm = ((norm_time - .5) / .5);
778 size *= (1.0 - temp_norm);
779 }
780 }
781 } else if (Fireballs[obj->id].type == FT_SPARK) // Do spark
782 {
783 bm_handle = Fireballs[obj->id].bm_handle;
784 size *= (1.0 - norm_time);
785
786 } else {
787 vclip *vc = &GameVClips[Fireballs[obj->id].bm_handle];
788 int int_frame = vc->num_frames * norm_time;
789 bm_handle = vc->frames[int_frame];
790 }
791 if (Fireballs[obj->id].type == FT_SMOKE) {
792 if (norm_time > .3) {
793 float temp_time = (norm_time - .3);
794 temp_time /= .7f;
795 size *= (1 + (temp_time * 2.3));
796 }
797 }
798 // Set some alpha
799 if (Fireballs[obj->id].type == FT_SMOKE || obj->id == CUSTOM_EXPLOSION_INDEX)
800 rend_SetAlphaType(AT_CONSTANT + AT_TEXTURE);
801 else
802 rend_SetAlphaType(AT_SATURATE_TEXTURE);
803 float val;
804 if (norm_time > .5)
805 val = 1.0 - ((norm_time - .5) / .5);
806 else
807 val = 1.0;
808 if (Fireballs[obj->id].type == FT_SMOKE)
809 rend_SetAlphaValue(val * SMOKE_ALPHA * 255);
810 else
811 rend_SetAlphaValue(val * FIREBALL_ALPHA * 255);
812 rend_SetOverlayType(OT_NONE);
813
814 rend_SetZBias(-2.0f);
815 rend_SetZBufferWriteMask(0);
816 rend_SetWrapType(WT_CLAMP);
817 rend_SetLighting(LS_NONE);
818 // Cap size
819 if (size > MAX_FIREBALL_SIZE)
820 size = MAX_FIREBALL_SIZE;
821
822 // Draw!!
823 g3_DrawRotatedBitmap(&obj->pos, rot_angle, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle);
824 rend_SetZBias(0);
825 rend_SetZBufferWriteMask(1);
826 }
827 // Creates a gravity field that sucks objects into it
828 int CreateGravityField(vector *pos, int roomnum, float size, float time, int parent_handle) {
829 int index = CreateFireball(pos, GRAVITY_FIELD_INDEX, roomnum, REAL_FIREBALL);
830 if (index < 0)
831 return -1;
832 object *obj = &Objects[index];
833
834 obj->lifetime = time;
835 obj->lifeleft = time;
836 obj->size = 1;
837 obj->ctype.blast_info.max_size = size;
838 obj->ctype.blast_info.bm_handle = Fireballs[BLUE_BLAST_RING_INDEX].bm_handle;
839 obj->parent_handle = parent_handle;
840 return index;
841 }
842 // Creates a fireball and then sets its size
843 int CreateFireball(vector *pos, int fireball_num, int roomnum, int realtype) {
844 int objnum;
845
846 ASSERT(roomnum != -1);
847 if (realtype == VISUAL_FIREBALL) // If this is a visual effect then create it instead!
848 return (VisEffectCreate(VIS_FIREBALL, fireball_num, roomnum, pos));
849 else {
850 objnum = ObjCreate(OBJ_FIREBALL, fireball_num, roomnum, pos, NULL);
851 if (objnum < 0) {
852 mprintf(0, "Couldn't create fireball object!\n");
853 return -1;
854 }
855 Objects[objnum].size = Fireballs[fireball_num].size;
856 Objects[objnum].flags |= OF_USES_LIFELEFT;
857
858 Objects[objnum].lifeleft = Fireballs[fireball_num].total_life;
859 Objects[objnum].lifetime = Objects[objnum].lifeleft;
860 }
861
862 return objnum;
863 }
864 // TODO: MTS: used only in this file.
865 // Creates a fireball object with a custom texture/vclip
866 int CreateCustomFireballObject(vector *pos, int fireball_num, int tex_handle, int roomnum) {
867 int objnum;
868 ASSERT(GameTextures[tex_handle].used >= 1);
869
870 if (roomnum == -1)
871 return -1;
872 objnum = ObjCreate(OBJ_FIREBALL, fireball_num, roomnum, pos, NULL);
873 if (objnum < 0) {
874 mprintf(0, "Couldn't create fireball object!\n");
875 return -1;
876 }
877 Objects[objnum].size = Fireballs[fireball_num].size;
878 Objects[objnum].flags |= OF_USES_LIFELEFT;
879 float lifetime;
880 // If this is an animated texture then use the life of the vclip
881 if (GameTextures[tex_handle].flags & TF_ANIMATED) {
882 PageInVClip(GameTextures[tex_handle].bm_handle);
883 vclip *vc = &GameVClips[GameTextures[tex_handle].bm_handle];
884 lifetime = vc->frame_time * vc->num_frames;
885 } else // Just set it to half a second
886 lifetime = .5;
887 Objects[objnum].lifeleft = lifetime;
888 Objects[objnum].lifetime = lifetime;
889 Objects[objnum].ctype.blast_info.bm_handle = tex_handle;
890
891 return objnum;
892 }
893 //----------------------------------
894 // Putting in some explosion stuff
895 //----------------------------------
896 // Creates a debris piece that goes off in a given direction, with a given magnitude
897 object *CreateSubobjectDebrisDirected(object *parent, int subobj_num, vector *dir, float explosion_mag,
898 int death_flags) {
899 int objnum;
900 object *obj;
901 ASSERT(parent->flags & OF_POLYGON_OBJECT);
902 objnum = ObjCreate(OBJ_DEBRIS, 0, parent->roomnum, &parent->pos, &parent->orient);
903
904 if (objnum < 0 || objnum > Highest_object_index) {
905 mprintf(0, "WARNING: Debris not created!\n");
906 return NULL;
907 }
908 obj = &Objects[objnum];
909
910 ASSERT(subobj_num < MAX_SUBOBJECTS);
911 int model_num = parent->rtype.pobj_info.model_num;
912
913 // Set polygon-object-specific data
914 obj->rtype.pobj_info.model_num = model_num;
915 obj->rtype.pobj_info.subobj_flags = 0x00000001 << subobj_num;
916 obj->rtype.pobj_info.tmap_override = parent->rtype.pobj_info.tmap_override;
917 // Set physics data for this object
918 ASSERT(obj->movement_type == MT_PHYSICS);
919 obj->mtype.phys_info.velocity = *dir;
920 obj->mtype.phys_info.velocity *= explosion_mag;
921 // obj->mtype.phys_info.velocity += parent->mtype.phys_info.velocity;
922 if (obj->mtype.phys_info.velocity.x > 100000.0f)
923 obj->mtype.phys_info.velocity.x = 100000.0f;
924 if (obj->mtype.phys_info.velocity.y > 100000.0f)
925 obj->mtype.phys_info.velocity.y = 100000.0f;
926 if (obj->mtype.phys_info.velocity.z > 100000.0f)
927 obj->mtype.phys_info.velocity.z = 100000.0f;
928 if (obj->mtype.phys_info.velocity.x < -100000.0f)
929 obj->mtype.phys_info.velocity.x = -100000.0f;
930 if (obj->mtype.phys_info.velocity.y < -100000.0f)
931 obj->mtype.phys_info.velocity.y = -100000.0f;
932 if (obj->mtype.phys_info.velocity.z < -100000.0f)
933 obj->mtype.phys_info.velocity.z = -100000.0f;
934 obj->mtype.phys_info.rotvel = parent->mtype.phys_info.rotvel;
935 vm_MakeZero(&obj->mtype.phys_info.rotthrust);
936 obj->size = Poly_models[obj->rtype.pobj_info.model_num].submodel[subobj_num].rad * 0.5;
937 obj->mtype.phys_info.mass = 40.0;
938 obj->mtype.phys_info.drag = .001f;
939
940 obj->mtype.phys_info.flags = (PF_GRAVITY | PF_BOUNCE | PF_FIXED_ROT_VELOCITY);
941 obj->mtype.phys_info.coeff_restitution = .2f;
942 obj->mtype.phys_info.rotvel.x = (float)((60000.0f * (float)(D3_RAND_MAX / 2 - ps_rand())) / (float)(D3_RAND_MAX / 2));
943 obj->mtype.phys_info.rotvel.y = (float)((60000.0f * (float)(D3_RAND_MAX / 2 - ps_rand())) / (float)(D3_RAND_MAX / 2));
944 obj->mtype.phys_info.rotvel.z = (float)((60000.0f * (float)(D3_RAND_MAX / 2 - ps_rand())) / (float)(D3_RAND_MAX / 2));
945 if (death_flags & DF_DEBRIS_REMAINS) {
946 obj->mtype.phys_info.num_bounces = 8;
947 obj->mtype.phys_info.flags |= PF_STICK;
948 obj->flags &= ~OF_USES_LIFELEFT;
949 // ChrisP needs to add some code here to make the debris behave resonably
950 } else {
951 obj->mtype.phys_info.num_bounces = 2;
952 obj->lifeleft = 2.0 + ((ps_rand() % 50) * .05);
953 obj->flags |= OF_USES_LIFELEFT;
954 }
955 ASSERT(obj->control_type == CT_DEBRIS);
956 // Copy death flags debris
957 obj->ctype.debris_info.death_flags = death_flags;
958 obj->ctype.debris_info.last_smoke_time = -1;
959 // Done!
960 return obj;
961 }
962 // Creates a subobject debris piece that goes off in a random direction
963 object *CreateSubobjectDebris(object *parent, int subobj_num, float explosion_mag, int death_flags) {
964 vector rand_vec;
965 // Set physics data for this object
966 rand_vec.x = (float)(D3_RAND_MAX / 2 - ps_rand());
967 rand_vec.y = (float)(D3_RAND_MAX / 2 - ps_rand()) + .2f * D3_RAND_MAX; // a habit of moving upwards
968 rand_vec.z = (float)(D3_RAND_MAX / 2 - ps_rand());
969
970 vm_NormalizeVectorFast(&rand_vec);
971 explosion_mag *= 1.0f + ((float)(D3_RAND_MAX / 2 - ps_rand()) / (float)(D3_RAND_MAX / 2) * 0.05); // +5/-5 percent
972 return CreateSubobjectDebrisDirected(parent, subobj_num, &rand_vec, explosion_mag, death_flags);
973 }
974 // Create extra fireballs for an exploding object
975 void CreateExtraFireballs(object *obj, float size_scale) {
976 // Create some extra explosions to start later
977 float expl_size = obj->size * size_scale;
978 int size_add = expl_size / EXTRA_EXPLOSION_THRESHOLD;
979 int extras = size_add + (ps_rand() % 4);
980 int i;
981 if (obj->type == OBJ_BUILDING && OBJECT_OUTSIDE(obj))
982 extras += 4;
983 // Cap at 12
984 extras = std::min(12, extras);
985 // mprintf(0,"Creating %d extra explosions\n",extras);
986 CreateRandomSparks(extras, &obj->pos, obj->roomnum);
987 for (i = 0; i < extras; i++) {
988 vector pos = obj->pos;
989 float vals[10], add_time;
990 float upwards;
991 // Make this a billowing explosion
992 if (obj->type == OBJ_BUILDING && OBJECT_OUTSIDE(obj)) {
993 float increment = (obj->size * 1.5) / extras;
994 pos.y += (increment * i);
995 add_time = (1.0 / extras) * i;
996 upwards = 60;
997 } else {
998 pos.x += (((ps_rand() % 100) / 50.0) - 1.0) * (obj->size / 2);
999 pos.y += (((ps_rand() % 100) / 50.0) - 1.0) * (obj->size / 2);
1000 pos.z += (((ps_rand() % 100) / 50.0) - 1.0) * (obj->size / 2);
1001 add_time = .3 + ((ps_rand() % 100) / 200.0);
1002 upwards = 0;
1003 }
1004
1005 vals[0] = pos.x;
1006 vals[1] = pos.y;
1007 vals[2] = pos.z;
1008 if (size_add > 0) {
1009 if (ps_rand() % 2)
1010 vals[3] = BIG_EXPLOSION_INDEX;
1011 else
1012 vals[3] = GetRandomMediumExplosion();
1013 } else
1014 vals[3] = GetRandomMediumExplosion();
1015 vals[4] = obj->roomnum;
1016 vals[5] = expl_size;
1017 vals[6] = upwards;
1018 // Bash some values for billowing explosions
1019 if ((obj->type == OBJ_BUILDING) && (OBJECT_OUTSIDE(obj))) {
1020 float norm = ((float)(i + 1)) / extras;
1021 // norm=1.0-norm;
1022 vals[3] = GetRandomBillowingExplosion();
1023 vals[5] *= (.5 + (.5 * norm));
1024 }
1025
1026 CreateNewEvent(0, 0, add_time, vals, sizeof(float) * 7, DoExplosionEvent);
1027 }
1028 }
1029 // Break apart a polygon model into pieces of debris
1030 void BreakApartModel(object *obj, float explosion_mag, int death_flags) {
1031 poly_model *pm = GetPolymodelPointer(obj->rtype.pobj_info.model_num);
1032 if (pm->n_models > 1) {
1033 int i;
1034
1035 // Create debris pieces from subobjects
1036 for (i = 1; i < pm->n_models; i++)
1037 CreateSubobjectDebris(obj, i, explosion_mag, death_flags);
1038 // Change size for collision purposes
1039 obj->size = Poly_models[obj->rtype.pobj_info.model_num].submodel[0].rad;
1040 }
1041 }
1042 // Splinter stuff
1043 #define MAX_SPLINTERS_PER_OBJECT 10
1044 // Creates individual face particles from the main body of an object
1045 void CreateSplintersFromBody(object *obj, float explosion_mag, float lifetime) {
1046 ASSERT(obj->flags & OF_POLYGON_OBJECT);
1047 int facenums[MAX_SPLINTERS_PER_OBJECT];
1048 vector rot_vecs[MAX_SUBOBJECTS];
1049 int num_splinters = 0;
1050 int i, t;
1051 int parent_objnum = obj - Objects;
1052 matrix m;
1053 m = obj->orient;
1054 vm_TransposeMatrix(&m);
1055 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
1056 // If submodel 0 not renderable, bail
1057 if (IsNonRenderableSubmodel(pm, 0))
1058 return;
1059 bsp_info *sm = &pm->submodel[0];
1060 if (sm->num_faces < MAX_SPLINTERS_PER_OBJECT) {
1061 for (i = 0; i < sm->num_faces; i++) {
1062 facenums[i] = i;
1063 num_splinters++;
1064 }
1065 } else {
1066 int limit = std::min(sm->num_faces, MAX_SPLINTERS_PER_OBJECT);
1067 for (i = 0; i < limit; i++) {
1068 facenums[i] = ps_rand() % sm->num_faces;
1069 num_splinters++;
1070 }
1071 }
1072 // Now, create those splinters!
1073 for (i = 0; i < num_splinters; i++) {
1074 int facenum = facenums[i];
1075 vector dest, center;
1076 int num_verts = std::min<float>(sm->faces[facenum].nverts, MAX_VERTS_PER_SPLINTER);
1077
1078 vm_MakeZero(&dest);
1079 vm_MakeZero(&center);
1080 // First get the center of this particular face
1081 for (t = 0; t < num_verts; t++) {
1082 vector pnt = sm->verts[sm->faces[facenum].vertnums[t]];
1083 vector temp_vec;
1084
1085 temp_vec = pnt * m;
1086 rot_vecs[t] = temp_vec;
1087 center += temp_vec;
1088 }
1089
1090 center /= num_verts;
1091 dest = center + obj->pos;
1092
1093 // Now create splinter with that faces center position
1094 int s_objnum = ObjCreate(OBJ_SPLINTER, pm - Poly_models, obj->roomnum, &dest, NULL);
1095 if (s_objnum < 0) {
1096 mprintf(0, "Couldn't create splinter object!\n");
1097 return;
1098 } else {
1099 // Fill in relevant info
1100
1101 object *splinter_obj = &Objects[s_objnum];
1102 splinter_obj->ctype.splinter_info.subobj_num = 0;
1103 splinter_obj->ctype.splinter_info.facenum = facenum;
1104 for (t = 0; t < num_verts; t++)
1105 splinter_obj->ctype.splinter_info.verts[t] = (rot_vecs[t]) - center;
1106 splinter_obj->ctype.splinter_info.center = center;
1107 vector subvec = dest - obj->pos;
1108 vm_NormalizeVector(&subvec);
1109 ObjSetOrient(splinter_obj, &Identity_matrix);
1110 splinter_obj->mtype.phys_info.velocity =
1111 (obj->mtype.phys_info.velocity) + (((subvec * obj->size) / lifetime) * 4);
1112 splinter_obj->mtype.phys_info.flags |= PF_FIXED_ROT_VELOCITY;
1113 splinter_obj->mtype.phys_info.rotvel = splinter_obj->mtype.phys_info.velocity * 400;
1114 // Randomize by 20 percent plus or minus
1115 splinter_obj->mtype.phys_info.velocity *= 1.0 + ((((ps_rand() % 100) - 50) / 50.0) * .2);
1116 splinter_obj->mtype.phys_info.rotvel *= 1.0 + ((((ps_rand() % 100) - 50) / 50.0) * .2);
1117
1118 vm_MakeZero(&splinter_obj->mtype.phys_info.rotthrust);
1119 splinter_obj->size = .5f;
1120 splinter_obj->mtype.phys_info.mass = 2500.0f;
1121 splinter_obj->mtype.phys_info.drag = .01f;
1122
1123 splinter_obj->mtype.phys_info.flags |= (PF_GRAVITY) | (PF_BOUNCE);
1124
1125 splinter_obj->mtype.phys_info.num_bounces = 0;
1126 splinter_obj->movement_type = MT_PHYSICS;
1127 SetObjectControlType(splinter_obj, CT_SPLINTER);
1128 splinter_obj->render_type = RT_SPLINTER;
1129 // Randomize the lifetime a little
1130 float mod_lifetime = lifetime;
1131 mod_lifetime += (((ps_rand() % 9) - 4) * .1);
1132 if (((splinter_obj - Objects) % 14) == 0)
1133 mod_lifetime += ((ps_rand() % 3) * .5);
1134 splinter_obj->lifeleft = mod_lifetime;
1135 splinter_obj->lifetime = mod_lifetime;
1136 splinter_obj->flags |= OF_USES_LIFELEFT;
1137 }
1138 }
1139 }
1140 void DoDeathSpew(object *parent) {
1141 ASSERT(IS_GENERIC(parent->type));
1142 int i, j;
1143 int num_last_type = 0;
1144 uint8_t f_dspew = Object_info[parent->id].f_dspew;
1145
1146 if (!parent)
1147 return;
1148 for (i = 0; i < MAX_DSPEW_TYPES; i++) {
1149 int type;
1150 int id;
1151 id = Object_info[parent->id].dspew[i];
1152 if (id == -1)
1153 continue; // spew type set to none
1154 type = Object_info[id].type;
1155 int max_spewed = Object_info[parent->id].dspew_number[i];
1156 int rand_val = (int)((float)D3_RAND_MAX * Object_info[parent->id].dspew_percent[i]);
1157 // Skip it if the player has that weapon battery enabled
1158 // If multiplayer, it will spew type 2 if no type 1 is spewed
1159 if (i == 0 && (f_dspew & DSF_ONLY_IF_PLAYER_HAS_OBJ_1)) {
1160 bool f_skip = false;
1161
1162 if (Game_mode & GM_MULTI) {
1163 f_dspew &= ~DSF_ONLY_IF_PLAYER_HAS_OBJ_1;
1164 f_dspew |= DSF_ONLY_IF_NO_1;
1165 } else if (type == OBJ_POWERUP) {
1166 player *playp = &Players[0];
1167 for (j = 0; j < MAX_PLAYER_WEAPONS; j++) {
1168 if (playp->weapon_flags & HAS_FLAG(j)) {
1169 int p_id = Ships[playp->ship_index].spew_powerup[j];
1170 if (p_id == id) {
1171 f_skip = true;
1172 break;
1173 }
1174 }
1175 }
1176 if (f_skip)
1177 continue;
1178 }
1179 }
1180
1181 if (i == 1 && (f_dspew & DSF_ONLY_IF_NO_1) && num_last_type != 0)
1182 continue;
1183 num_last_type = 0;
1184 for (j = 0; j < max_spewed; j++) {
1185 if (ps_rand() <= rand_val) {
1186 int objnum = ObjCreate(type, id, parent->roomnum, &parent->pos, NULL);
1187 num_last_type++;
1188 if (objnum < 0) {
1189 mprintf(0, "Couldn't spew object!\n");
1190 return;
1191 }
1192 object *obj = &Objects[objnum];
1193
1194 // Set random velocity for powerups
1195 obj->mtype.phys_info.velocity.x = ((ps_rand() / (float)D3_RAND_MAX) - .5f) * 35.0;
1196 obj->mtype.phys_info.velocity.z = ((ps_rand() / (float)D3_RAND_MAX) - .5f) * 35.0;
1197 obj->mtype.phys_info.velocity.y = ((ps_rand() / (float)D3_RAND_MAX) - .5f) * 35.0;
1198 InitObjectScripts(obj);
1199 // Send object to other players
1200 if (Game_mode & GM_MULTI) {
1201 if (Netgame.local_role == LR_SERVER) {
1202 MultiSendObject(obj, 0);
1203 }
1204 }
1205 }
1206 }
1207 }
1208 }
1209
1210 // Creates a fireball vis effect for the specified object
1211 // The explosion size is based on the object size times the size_scale
1212 // The fireball type will be randomly selected based on the explosion size
1213 // Returns the visnum of the fireball
1214 int CreateObjectFireball(object *objp, float size_scale) {
1215 int expl_type = GetRandomExplosion(objp->size * size_scale);
1216 float expl_size = objp->size * size_scale * 2.0;
1217 int fireball_visnum = CreateFireball(&objp->pos, expl_type, objp->roomnum, VISUAL_FIREBALL);
1218 if (fireball_visnum >= 0)
1219 VisEffects[fireball_visnum].size = expl_size;
1220 return fireball_visnum;
1221 }
1222 // -------------------------------------------------------------------------------------------------------
1223 // do whatever needs to be done for this piece of debris for this frame
1224 void DoDebrisFrame(object *obj) {
1225 ASSERT(obj->control_type == CT_DEBRIS);
1226 // Get death flags
1227 int death_flags = obj->ctype.debris_info.death_flags;
1228 // See if we're dead
1229 if ((obj->lifeleft <= 0.0) || (obj->mtype.phys_info.num_bounces < 1)) {
1230 // If explodes, create fireball & sound
1231 if (death_flags & DF_DEBRIS_FIREBALL) {
1232 Sound_system.Play3dSound(SOUND_DEBRIS_EXPLODE, SND_PRIORITY_NORMAL, obj);
1233 CreateObjectFireball(obj);
1234 }
1235 // If debris has a blast ring, create it
1236 if (death_flags & DF_DEBRIS_BLAST_RING)
1237 CreateObjectBlastRing(obj);
1238 // Make debris go away, or make it inactive
1239 if (!(death_flags & DF_DEBRIS_REMAINS))
1240 SetObjectDeadFlag(obj);
1241 else
1242 SetObjectControlType(obj, CT_NONE);
1243 // Done with this debris
1244 return;
1245 }
1246 // Create smoke, if this debris smokes
1247 if (death_flags & DF_DEBRIS_SMOKES) {
1248 if (Gametime - obj->ctype.debris_info.last_smoke_time > .015 && (rand() % 2)) {
1249 // Create a small flame puff every now and then
1250 int visnum = CreateFireball(&obj->pos, BLACK_SMOKE_INDEX, obj->roomnum, VISUAL_FIREBALL);
1251
1252 if (visnum >= 0)
1253 VisEffects[visnum].size = obj->size / 2; // Make small!
1254 obj->ctype.debris_info.last_smoke_time = Gametime;
1255 }
1256 }
1257 }
1258 // Create electrical bolts on an object
1259 void CreateElectricalBolts(object *objp, int num_bolts) {
1260 for (int i = 0; i < num_bolts; i++) {
1261 poly_model *pm = &Poly_models[objp->rtype.pobj_info.model_num];
1262 int subnum = ps_rand() % pm->n_models;
1263 bsp_info *sm = &pm->submodel[subnum];
1264 if (sm->nverts > 0) {
1265 int visnum = VisEffectCreate(VIS_FIREBALL, LIGHTNING_BOLT_INDEX, objp->roomnum, &objp->pos);
1266 if (visnum >= 0) {
1267 vis_effect *vis = &VisEffects[visnum];
1268 vis->lifeleft = 1.0;
1269 vis->lifetime = 1.0;
1270 vis->end_pos = objp->pos;
1271 vis->velocity.x = .15f;
1272 vis->velocity.y = 3;
1273 vis->attach_info.obj_handle = objp->handle;
1274 vis->attach_info.subnum = subnum;
1275 vis->attach_info.vertnum = ps_rand() % sm->nverts;
1276 vis->attach_info.end_vertnum = ps_rand() % sm->nverts;
1277 vis->attach_info.modelnum = objp->rtype.pobj_info.model_num;
1278 vis->flags = VF_USES_LIFELEFT | VF_EXPAND | VF_ATTACHED;
1279 }
1280 } else {
1281 mprintf(0, "VIS: Submodel %s has 0 vertices!!!\n", sm->name);
1282 }
1283 }
1284 }
1285 // Create a smoke spewer for the specified object
1286 void CreateObjectSmokeSpewer(object *objp) {
1287 spewinfo myspew;
1288 myspew.random = 0;
1289 myspew.use_gunpoint = false;
1290 myspew.real_obj = false;
1291 myspew.pt.origin = objp->pos;
1292 myspew.pt.normal.x = 0;
1293 myspew.pt.normal.y = 1.0;
1294 myspew.pt.normal.z = 0;
1295 myspew.pt.room_num = objp->roomnum;
1296 myspew.effect_type = BLACK_SMOKE_INDEX;
1297 myspew.drag = .01f;
1298 myspew.mass = 10;
1299 myspew.time_int = .25;
1300 myspew.longevity = 30;
1301 myspew.lifetime = 2.0;
1302 myspew.size = 10.0;
1303 myspew.speed = 50;
1304 SpewCreate(&myspew);
1305 }
1306 // Play the explosion sound for this object
1307 void PlayObjectExplosionSound(object *objp) {
1308 int sound;
1309 if (objp->type == OBJ_DOOR) {
1310 sound = SOUND_ROBOT_EXPLODE_1;
1311 } else {
1312 ASSERT(IS_GENERIC(objp->type));
1313 sound = Object_info[objp->id].sounds[GSI_EXPLODE];
1314 if (sound == SOUND_NONE_INDEX) { // check for default
1315 if (objp->type == OBJ_BUILDING)
1316 sound = SOUND_BUILDING_EXPLODE;
1317 else
1318 sound = (ps_rand() > D3_RAND_MAX / 2) ? SOUND_ROBOT_EXPLODE_1 : SOUND_ROBOT_EXPLODE_2;
1319 }
1320 }
1321 if (sound != -1)
1322 Sound_system.Play3dSound(sound, SND_PRIORITY_HIGH, objp);
1323 }
1324 const char *dead_object_types[] = {
1325 "swatter", "swatter(deadmodel)", "Hangturret", "Securityturret(DEAD)",
1326 "Lance", "Lance(DEAD)", "L10 swatter", "swatter(deadmodel)",
1327 };
1328 #define N_DEAD_OBJECT_TYPES (sizeof(dead_object_types) / sizeof(*dead_object_types) / 2)
1329 #include "osiris_predefs.h"
1330 // Creates a dead version of the given object
1331 void CreateDeadObject(object *objp) {
1332 if (objp->type == OBJ_ROBOT) {
1333 for (int i = 0; i < N_DEAD_OBJECT_TYPES; i++) {
1334 if (stricmp(Object_info[objp->id].name, dead_object_types[i * 2]) == 0) {
1335 int id = FindObjectIDName(dead_object_types[i * 2 + 1]);
1336 if (id != -1) {
1337 vector old_point, pos;
1338 PhysCalcGround(&old_point, NULL, objp, 0); // Old ground point
1339 poly_model *pm = GetPolymodelPointer(Object_info[id].render_handle);
1340 pos = old_point - pm->ground_slots[0].pnt * ~objp->orient;
1341 osipf_ObjCreate(Object_info[id].type, id, objp->roomnum, &pos, &objp->orient);
1342 }
1343 return; // done
1344 }
1345 }
1346 }
1347 }
1348 #define FADE_TIME 2.0
1349 // Destroy an object immediately
1350 void DestroyObject(object *objp, float explosion_mag, int death_flags) {
1351 ASSERT(IS_GENERIC(objp->type) || (objp->type == OBJ_DOOR));
1352 // Say this this object is dying (if not already set)
1353 objp->flags |= OF_DYING;
1354 // Create blast ring if enabled for this object
1355 if (death_flags & DF_BLAST_RING)
1356 CreateObjectBlastRing(objp);
1357 // Do a shake & shockwave only if the object either breaks apart or has fireballs
1358 if ((death_flags & DF_FIREBALL) || (death_flags & DF_BREAKS_APART)) {
1359 // Shake the player. The odd magnitude is from Jason's old code.
1360 // maybe it would make more sense to base the shake on the explosion size
1361 float dist = vm_VectorDistanceQuick(&Player_object->pos, &objp->pos);
1362 if (dist / objp->size < 30.0)
1363 AddToShakeMagnitude(objp->size * 5.0 - dist / 6.0);
1364 // Do a shockwave
1365 MakeShockwave(objp, objp->handle);
1366 }
1367 // Play sound if not flagged to play at start of delay
1368 if (!(death_flags & DF_DELAY_SOUND))
1369 PlayObjectExplosionSound(objp);
1370 // Do death spew if generic object
1371 if (IS_GENERIC(objp->type)) {
1372 if (!((Game_mode & GM_MULTI) && (Netgame.local_role != LR_SERVER)))
1373 if (Demo_flags != DF_PLAYBACK)
1374 DoDeathSpew(objp);
1375 }
1376 ASSERT(objp->flags & OF_POLYGON_OBJECT);
1377 ASSERT(objp->rtype.pobj_info.subobj_flags == 0xFFFFFFFF);
1378 // Create fireballs
1379 int fireball_visnum = -1;
1380 if (death_flags & DF_FIREBALL) {
1381 // Determine size of explosion
1382 float size_scale;
1383 switch (DEATH_EXPL_SIZE(death_flags)) {
1384 case DF_EXPL_SMALL:
1385 size_scale = 0.5f;
1386 break;
1387 case DF_EXPL_MEDIUM:
1388 size_scale = 1.0f;
1389 break;
1390 case DF_EXPL_LARGE:
1391 size_scale = 1.6f;
1392 break;
1393 default:
1394 Int3();
1395 }
1396 // Create big fireball
1397 fireball_visnum = CreateObjectFireball(objp, size_scale);
1398 // Create extra fireballs
1399 CreateExtraFireballs(objp, size_scale);
1400 }
1401 // Switch to dying model
1402 if (objp->rtype.pobj_info.dying_model_num != -1)
1403 objp->rtype.pobj_info.model_num = objp->rtype.pobj_info.dying_model_num;
1404 // Create debris pieces
1405 if (death_flags & DF_BREAKS_APART)
1406 BreakApartModel(objp, explosion_mag, death_flags);
1407 // Create splinters. Base life of the life of the fireball created earlier
1408 if (death_flags & DF_FIREBALL) {
1409 float fireball_lifeleft = (fireball_visnum != -1) ? VisEffects[fireball_visnum].lifeleft : 2.0;
1410 CreateSplintersFromBody(objp, explosion_mag, OBJECT_OUTSIDE(objp) ? fireball_lifeleft : (fireball_lifeleft / 2.0));
1411 }
1412 // Make sure its ok to delete if this is a netplayer game
1413 if ((Game_mode & GM_MULTI) && (Netgame.local_role == LR_CLIENT) && (objp->flags & OF_SERVER_OBJECT))
1414 ASSERT(objp->flags & OF_SERVER_SAYS_DELETE);
1415 // Make object go away or become inert
1416 if (death_flags & DF_REMAINS) { // Make object do nothing
1417 SetObjectControlType(objp, CT_NONE);
1418 objp->type = OBJ_DEBRIS; // do it won't do idle animation
1419 objp->movement_type = MT_NONE;
1420 } else if (death_flags & DF_FADE_AWAY) {
1421 ASSERT(objp->effect_info != NULL);
1422 objp->effect_info->type_flags |= EF_FADING_OUT;
1423 objp->effect_info->fade_time = objp->effect_info->fade_max_time = FADE_TIME;
1424 objp->lifeleft = FADE_TIME;
1425 objp->flags |= OF_USES_LIFELEFT;
1426 objp->control_type = CT_NONE;
1427 } else { // Mark the object to die
1428 CreateDeadObject(objp);
1429 SetObjectDeadFlag(objp);
1430 }
1431 }
1432 #define ELECTRICAL_DEATH_TIME 3.0
1433 // Process a dying object for one frame
1434 void DoDyingFrame(object *objp) {
1435 // Make sure types & flags are right
1436 ASSERT(objp->flags & OF_DYING);
1437 ASSERT((objp->control_type == CT_DYING) || (objp->control_type == CT_DYING_AND_AI));
1438 // Get death flags
1439 int death_flags = objp->ctype.dying_info.death_flags;
1440 // Update delay time
1441 objp->ctype.dying_info.delay_time -= Frametime;
1442 // Done dying?
1443 if (objp->ctype.dying_info.delay_time < 0) {
1444 // Kill the object for real
1445 DestroyObject(objp, 30, death_flags);
1446 // Add to score
1447 if ((objp->ctype.dying_info.killer_playernum != -1) && IS_GENERIC(objp->type))
1448 PlayerScoreAdd(objp->ctype.dying_info.killer_playernum, Object_info[objp->id].score);
1449 // We're done
1450 return;
1451 }
1452 // If sparking death, do sparks
1453 if (death_flags & DF_DELAY_SPARKS) {
1454 float dying_norm = objp->ctype.dying_info.delay_time / ELECTRICAL_DEATH_TIME;
1455 dying_norm = std::min<float>(dying_norm, 1.0);
1456 // Do deform stuff
1457 objp->effect_info->type_flags |= EF_DEFORM;
1458 objp->effect_info->deform_time = 1.0;
1459 objp->effect_info->deform_range = (1.0 - dying_norm) * .2f;
1460 int dying_chance = (dying_norm * 10) + 1;
1461 // Create random electrical effect
1462 if (Gametime - objp->ctype.dying_info.last_spark_time > .01) {
1463 if ((ps_rand() % dying_chance) == 0) {
1464 int num_bolts = ((1.0 - dying_norm) * 5.0) + 3;
1465 CreateElectricalBolts(objp, num_bolts);
1466 }
1467 objp->ctype.dying_info.last_spark_time = Gametime;
1468 }
1469 }
1470 // If fireball death, do fireballs
1471 if (death_flags & DF_DELAY_FIREBALL) {
1472 vector velocity_norm = objp->mtype.phys_info.velocity;
1473 vm_NormalizeVector(&velocity_norm);
1474 vector pos = objp->pos - (velocity_norm * (objp->size / 2));
1475 if (OBJECT_OUTSIDE(objp))
1476 CreateFireball(&pos, BLACK_SMOKE_INDEX, objp->roomnum, VISUAL_FIREBALL);
1477 // Create an explosion that follows every now and then
1478 if ((Gametime - objp->ctype.dying_info.last_fireball_time > .01) && (ps_rand() % 3) == 0) {
1479 if (!(objp->flags & OF_POLYGON_OBJECT))
1480 return;
1481 objp->ctype.dying_info.last_fireball_time = Gametime;
1482 vector dest;
1483 poly_model *pm = &Poly_models[objp->rtype.pobj_info.model_num];
1484 bsp_info *sm = &pm->submodel[0];
1485 int vertnum = ps_rand() % sm->nverts;
1486 GetPolyModelPointInWorld(&dest, &Poly_models[objp->rtype.pobj_info.model_num], &objp->pos, &objp->orient, 0,
1487 &sm->verts[vertnum]);
1488 int visnum = CreateFireball(&dest, GetRandomSmallExplosion(), objp->roomnum, VISUAL_FIREBALL);
1489 if (visnum >= 0) // DAJ this pervents a -1 array index
1490 {
1491 VisEffects[visnum].size += ((ps_rand() % 20) / 20.0) * 3.0;
1492 if ((ps_rand() % 2)) {
1493 VisEffects[visnum].movement_type = MT_PHYSICS;
1494 VisEffects[visnum].velocity = objp->mtype.phys_info.velocity;
1495 VisEffects[visnum].mass = objp->mtype.phys_info.mass;
1496 VisEffects[visnum].drag = objp->mtype.phys_info.drag;
1497 }
1498 }
1499 }
1500 }
1501 // If smoke death, do smoke
1502 if (death_flags & DF_DELAY_SMOKES) {
1503 // see if we should draw some smoke
1504 if ((Gametime - objp->ctype.dying_info.last_smoke_time > .01) &&
1505 (ps_rand() % 3) == 0) { // don't draw most of the time
1506 objp->ctype.dying_info.last_smoke_time = Gametime;
1507
1508 // Make even less likely inside
1509 if (OBJECT_OUTSIDE(objp) || ((ps_rand() % 2) == 0))
1510 CreateFireball(&objp->pos, BLACK_SMOKE_INDEX, objp->roomnum, VISUAL_FIREBALL);
1511 }
1512 }
1513 }
1514 // Pulls every object near the gravity field into its center
1515 void DoGravityFieldEffect(object *obj) {
1516 float max_size = obj->ctype.blast_info.max_size;
1517 float lifenorm = (obj->lifetime - obj->lifeleft) / obj->lifetime;
1518 float dist, force;
1519 vector vforce;
1520 int i;
1521 object *hit_obj_ptr;
1522 float time_norm = 1.0 - (obj->lifeleft / obj->lifetime);
1523 obj->size = max_size;
1524 float sphere_norm = 1.0 - ((lifenorm - .1) * 1.5);
1525 if (sphere_norm < 0)
1526 sphere_norm = 0;
1527 SetShakeMagnitude(25);
1528 float vdist = vm_VectorDistanceQuick(&Viewer_object->pos, &obj->pos);
1529 CreateRandomSparks(3, &obj->pos, obj->roomnum, -1, 2);
1530
1531 if (!(obj->flags & OF_SERVER_SAYS_DELETE)) {
1532 obj->flags |= OF_SERVER_SAYS_DELETE;
1533 int visnum = VisEffectCreate(VIS_FIREBALL, SUN_CORONA_INDEX, obj->roomnum, &obj->pos);
1534 if (visnum >= 0) {
1535 vis_effect *vis = &VisEffects[visnum];
1536 vis->size = max_size / 4;
1537 vis->lifeleft = obj->lifetime;
1538 vis->lifetime = obj->lifetime;
1539 vis->flags = VF_USES_LIFELEFT | VF_WINDSHIELD_EFFECT;
1540 }
1541 // Create lightning from the heavens
1542 if (OBJECT_OUTSIDE(obj) && vdist > 5) {
1543 int visnum = VisEffectCreate(VIS_FIREBALL, THICK_LIGHTNING_INDEX, obj->roomnum, &obj->pos);
1544 if (visnum >= 0) {
1545 vis_effect *vis = &VisEffects[visnum];
1546 vis->lifeleft = obj->lifetime;
1547 vis->lifetime = obj->lifetime;
1548 vis->end_pos = obj->pos;
1549
1550 vis->end_pos.y = obj->pos.y + (MAX_TERRAIN_HEIGHT * 2);
1551 vis->velocity.x = 2;
1552 vis->velocity.y = 1;
1553 vis->velocity.z = 3;
1554 vis->custom_handle = FindTextureName("ThickLineLightning");
1555 vis->billboard_info.width = 70;
1556 vis->billboard_info.texture = 0;
1557
1558 vis->lighting_color = OPAQUE_FLAG | GR_RGB16(100, 150, 255);
1559 vis->flags = VF_USES_LIFELEFT | VF_WINDSHIELD_EFFECT;
1560 vis->size = 50;
1561 }
1562 }
1563 // Create lightning from the heavens
1564 if (!OBJECT_OUTSIDE(obj)) {
1565 ps_srand(Gametime * 100);
1566 int count = 0;
1567 // Cast 3 rays out from the center
1568 for (i = 0; i < 3; i++) {
1569 again:
1570 vector rand_vector, test_vector;
1571 rand_vector.x = ((ps_rand() % 100 - 50) / 50.0) + .1; // So we don't get a zero vector
1572 rand_vector.y = ((ps_rand() % 100) - 50) / 50.0;
1573 rand_vector.z = ((ps_rand() % 100) - 50) / 50.0;
1574 vm_NormalizeVector(&rand_vector);
1575 test_vector = obj->pos + (rand_vector * 1000);
1576 vector dest_pos;
1577 int dest_room;
1578 fvi_query fq;
1579 fvi_info hit_info;
1580 int fate;
1581 fq.p0 = &obj->pos;
1582 fq.p1 = &test_vector;
1583 fq.startroom = obj->roomnum;
1584
1585 fq.rad = 0.0f;
1586 fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_NON_LIGHTMAP_OBJECTS;
1587 fq.thisobjnum = OBJNUM(obj);
1588 fq.ignore_obj_list = NULL;
1589 fate = fvi_FindIntersection(&fq, &hit_info);
1590 dest_pos = hit_info.hit_pnt;
1591 dest_room = hit_info.hit_room;
1592 if (count < 5) {
1593 if (vm_VectorDistance(&obj->pos, &dest_pos) < 2) {
1594 count++;
1595 goto again;
1596 } else
1597 count = 0;
1598 }
1599 int visnum = VisEffectCreate(VIS_FIREBALL, THICK_LIGHTNING_INDEX, obj->roomnum, &obj->pos);
1600 if (visnum >= 0) {
1601 vis_effect *vis = &VisEffects[visnum];
1602 vis->lifeleft = obj->lifetime;
1603 vis->lifetime = obj->lifetime;
1604
1605 vis->end_pos = dest_pos;
1606
1607 vis->custom_handle = FindTextureName("ThickLineLightning");
1608 vis->billboard_info.width = 25;
1609 vis->billboard_info.texture = 1;
1610 vis->velocity.x = 2;
1611 vis->velocity.y = 1;
1612 vis->velocity.z = .5;
1613
1614 vis->lighting_color = GR_RGB16(100, 150, 255);
1615 vis->flags = VF_USES_LIFELEFT | VF_WINDSHIELD_EFFECT | VF_LINK_TO_VIEWER;
1616 vis->size = 50;
1617 }
1618 }
1619 }
1620 }
1621 // Create random strands of lightning
1622 for (i = 0; i < 3 && vdist > 5; i++) {
1623 int visnum = VisEffectCreate(VIS_FIREBALL, LIGHTNING_BOLT_INDEX, obj->roomnum, &obj->pos);
1624 if (visnum >= 0) {
1625 vis_effect *vis = &VisEffects[visnum];
1626 vis->lifeleft = Frametime;
1627 vis->lifetime = Frametime;
1628 vis->end_pos = obj->pos;
1629 vis->end_pos.x += (((float)(ps_rand() % 100) - 50) / 50.0) * sphere_norm * max_size;
1630 vis->end_pos.y += (((float)(ps_rand() % 100) - 50) / 50.0) * sphere_norm * max_size;
1631 vis->end_pos.z += (((float)(ps_rand() % 100) - 50) / 50.0) * sphere_norm * max_size;
1632 vis->custom_handle = visnum;
1633 vis->velocity.x = 2;
1634 vis->velocity.y = .25;
1635 vis->flags = VF_USES_LIFELEFT;
1636 vis->size = 100;
1637 }
1638 }
1639
1640 for (i = 0; i <= Highest_object_index; i++) {
1641 hit_obj_ptr = &Objects[i];
1642 if (!(hit_obj_ptr->type == OBJ_PLAYER || hit_obj_ptr->type == OBJ_ROBOT || hit_obj_ptr->type == OBJ_WEAPON ||
1643 hit_obj_ptr->type == OBJ_POWERUP || hit_obj_ptr->type == OBJ_CLUTTER ||
1644 (hit_obj_ptr->type == OBJ_BUILDING && hit_obj_ptr->ai_info)))
1645 continue;
1646 if (hit_obj_ptr->type == OBJ_WEAPON && !(Weapons[hit_obj_ptr->id].flags & WF_MATTER_WEAPON))
1647 continue;
1648 if (obj == hit_obj_ptr)
1649 continue;
1650 if (hit_obj_ptr->flags & OF_DEAD)
1651 continue;
1652 if (hit_obj_ptr->movement_type != MT_PHYSICS)
1653 continue;
1654 // The distance is actually the objects' centers minus some of the hit object's radius (I set it to 80%)
1655 if ((hit_obj_ptr->flags & OF_POLYGON_OBJECT) && hit_obj_ptr->type != OBJ_WEAPON &&
1656 hit_obj_ptr->type != OBJ_POWERUP && hit_obj_ptr->type != OBJ_DEBRIS) {
1657 vector pos;
1658 pos = hit_obj_ptr->pos + hit_obj_ptr->wall_sphere_offset;
1659 dist = vm_VectorDistanceQuick(&pos, &obj->pos) - Poly_models[hit_obj_ptr->rtype.pobj_info.model_num].wall_size;
1660 } else {
1661 dist = vm_VectorDistanceQuick(&hit_obj_ptr->pos, &obj->pos) - hit_obj_ptr->size;
1662 }
1663
1664 if (dist < 0.0f)
1665 dist = 0.0f;
1666 if (dist <= (obj->ctype.blast_info.max_size * 2)) {
1667
1668 float dist_norm = 1.0 - (dist / (obj->ctype.blast_info.max_size * 2));
1669 force = dist_norm * time_norm * 30;
1670 // Cap it
1671 if (force > 500)
1672 force = 500;
1673
1674 // Find the force vector on the object
1675 vm_GetNormalizedDirFast(&vforce, &hit_obj_ptr->pos, &obj->pos);
1676 vforce *= (-force); // Sucking vector
1677 if (hit_obj_ptr->mtype.phys_info.mass > 1)
1678 vforce *= (hit_obj_ptr->mtype.phys_info.mass * 5);
1679 // Don't affect powerups in a multiplayer game
1680 if (!((Game_mode & GM_MULTI) && hit_obj_ptr->type == OBJ_POWERUP))
1681 phys_apply_force(hit_obj_ptr, &vforce);
1682 if (hit_obj_ptr != Viewer_object) {
1683 // Create strands of lightning
1684 int visnum = VisEffectCreate(VIS_FIREBALL, LIGHTNING_BOLT_INDEX, obj->roomnum, &obj->pos);
1685 if (visnum >= 0) {
1686 vis_effect *vis = &VisEffects[visnum];
1687 vis->lifeleft = Frametime;
1688 vis->lifetime = Frametime;
1689 vis->end_pos = hit_obj_ptr->pos;
1690 vis->custom_handle = hit_obj_ptr - Objects;
1691 vis->flags = VF_USES_LIFELEFT | VF_NO_Z_ADJUST;
1692 vis->size = dist;
1693 vis->velocity.x = 1;
1694 vis->velocity.y = .25;
1695 }
1696 if ((ps_rand() % 8) == 0)
1697 CreateRandomSparks(ps_rand() % 3, &hit_obj_ptr->pos, hit_obj_ptr->roomnum, -1, (ps_rand() % 2) + 2);
1698 }
1699
1700 // Peg the object to the gravity field center if it is close
1701 hit_obj_ptr->mtype.phys_info.flags |= PF_DESTINATION_POS;
1702 hit_obj_ptr->mtype.phys_info.dest_pos = obj->pos;
1703 }
1704 }
1705 }
1706 // do whatever needs to be done for this explosion for this frame
1707 void DoExplosionFrame(object *obj) {
1708 ASSERT(obj->control_type == CT_EXPLOSION);
1709 ASSERT(obj->type != OBJ_PLAYER);
1710 if (obj->id == GRAVITY_FIELD_INDEX) {
1711 DoGravityFieldEffect(obj);
1712 }
1713
1714 // See if we should die of old age
1715 if (obj->lifeleft <= 0.0) {
1716 if (obj->id == GRAVITY_FIELD_INDEX) {
1717 // Create a big ass explosion
1718 if (OBJECT_OUTSIDE(obj))
1719 CreateBlastRing(&obj->pos, BLAST_RING_INDEX, DAMAGE_RING_TIME, obj->ctype.blast_info.max_size * 3, obj->roomnum,
1720 1);
1721 else
1722 CreateBlastRing(&obj->pos, BLAST_RING_INDEX, DAMAGE_RING_TIME, obj->ctype.blast_info.max_size, obj->roomnum, 1);
1723
1724 // Blow the damn thing up and register a kill.
1725
1726 float mag = vm_VectorDistanceQuick(&obj->pos, &Player_object->pos);
1727 if (mag > obj->ctype.blast_info.max_size)
1728 mag = 0;
1729 else
1730 mag = 1.0 - (mag / obj->ctype.blast_info.max_size);
1731
1732 if (OBJECT_OUTSIDE(Viewer_object))
1733 AddToShakeMagnitude(mag * obj->ctype.blast_info.max_size * 5);
1734 else
1735 AddToShakeMagnitude(mag * obj->ctype.blast_info.max_size * 2);
1736 obj->impact_time = 0;
1737 obj->impact_size = obj->ctype.blast_info.max_size;
1738 obj->impact_force = obj->impact_size * 2;
1739 obj->impact_player_damage = obj->impact_size * 3;
1740 obj->impact_generic_damage = obj->impact_size * 3;
1741
1742 if (Game_mode & GM_MULTI) // Do different damage to players if in multiplayer
1743 DoConcussiveForce(obj, obj->parent_handle, 1.0f / 5.0f);
1744 else
1745 DoConcussiveForce(obj, obj->parent_handle, 1.0);
1746 CreateRandomSparks(200, &obj->pos, obj->roomnum, -1, (ps_rand() % 2) + 2);
1747
1748 int explode_visnum = CreateFireball(&obj->pos, BLUE_EXPLOSION_INDEX, obj->roomnum, VISUAL_FIREBALL);
1749 if (explode_visnum >= 0)
1750 VisEffects[explode_visnum].size = obj->ctype.blast_info.max_size;
1751 }
1752
1753 // We died of old age
1754 SetObjectDeadFlag(obj);
1755 obj->lifeleft = 0.0;
1756 }
1757 }
1758 void MakeShockwave(object *explode_obj_ptr, int parent_handle) {
1759 if (explode_obj_ptr->impact_time > 0.0) {
1760 object *p = ObjGet(parent_handle);
1761 object *top_parent = p;
1762 int handle;
1763 ASSERT(explode_obj_ptr);
1764 while (p) {
1765 p = ObjGet(p->parent_handle);
1766 if (p) {
1767 top_parent = p;
1768 }
1769 }
1770 if (top_parent) {
1771 handle = top_parent->handle;
1772 } else {
1773 handle = OBJECT_HANDLE_NONE;
1774 }
1775 int objnum = ObjCreate(OBJ_SHOCKWAVE, 0, explode_obj_ptr->roomnum, &explode_obj_ptr->pos, NULL, handle);
1776 if (objnum >= 0) {
1777 Objects[objnum].control_type = CT_NONE;
1778 Objects[objnum].movement_type = MT_SHOCKWAVE;
1779 Objects[objnum].render_type = RT_NONE;
1780 Objects[objnum].lifeleft = explode_obj_ptr->impact_time;
1781 Objects[objnum].impact_size = explode_obj_ptr->impact_size;
1782 Objects[objnum].impact_force = explode_obj_ptr->impact_force;
1783 Objects[objnum].impact_player_damage = explode_obj_ptr->impact_player_damage;
1784 Objects[objnum].impact_generic_damage = explode_obj_ptr->impact_generic_damage;
1785 Objects[objnum].impact_time = explode_obj_ptr->impact_time;
1786 Objects[objnum].flags |= OF_USES_LIFELEFT;
1787 }
1788 } else {
1789 DoConcussiveForce(explode_obj_ptr, parent_handle);
1790 }
1791 }
1792 // object *object_create_explosion_sub(object *explode_obj_ptr, int16_t segnum, vms_vector * position, fix size, int
1793 // vclip_type, fix maxdamage, fix maxdistance, fix maxforce, int parent )
1794 void DoConcussiveForce(object *explode_obj_ptr, int parent_handle, float player_scalar) {
1795 int objnum;
1796 float max_player_damage = explode_obj_ptr->impact_player_damage;
1797 float max_generic_damage = explode_obj_ptr->impact_generic_damage;
1798 float maxforce = explode_obj_ptr->impact_force;
1799 float maxdistance = explode_obj_ptr->impact_size;
1800 float effect_distance;
1801 int16_t parent;
1802 object *parent_obj;
1803 if (explode_obj_ptr->type == OBJ_SHOCKWAVE) {
1804 effect_distance =
1805 explode_obj_ptr->impact_size * ((Gametime - explode_obj_ptr->creation_time) / explode_obj_ptr->impact_time);
1806 if (effect_distance > explode_obj_ptr->impact_size)
1807 effect_distance = explode_obj_ptr->impact_size;
1808 } else {
1809 effect_distance = explode_obj_ptr->impact_size;
1810 }
1811 parent_obj = ObjGet(parent_handle);
1812 if (parent_obj != NULL)
1813 parent = OBJNUM(parent_obj);
1814 objnum = OBJNUM(explode_obj_ptr);
1815 if (explode_obj_ptr->impact_size <= 0.0)
1816 return;
1817 {
1818 float dist, force;
1819 vector vforce;
1820 float damage;
1821 int i;
1822 object *hit_obj_ptr;
1823
1824 for (i = 0; i <= Highest_object_index; i++) {
1825 // Weapons used to be affected by badass explosions, but this introduces serious problems.
1826 // When a smart bomb blows up, if one of its children goes right towards a nearby wall, it will
1827 // blow up, blowing up all the children. So I remove it. MK, 09/11/94
1828 // if ( (hit_obj_ptr!=explode_obj_ptr) && !(hit_obj_ptr->flags&OF_DEAD) &&
1829 //((hit_obj_ptr->type==OBJ_WEAPON /*&& (hit_obj_ptr->id==PROXIMITY_ID || hit_obj_ptr->id==SUPERPROX_ID ||
1830 // hit_obj_ptr->id==PMINE_ID)*/) || (hit_obj_ptr->type == OBJ_CNTRLCEN) || (hit_obj_ptr->type==OBJ_PLAYER) ||
1831 //((hit_obj_ptr->type==OBJ_ROBOT) && ((Objects[parent].type != OBJ_ROBOT) || (Objects[parent].id !=
1832 // hit_obj_ptr->id))))) {
1833 hit_obj_ptr = &Objects[i];
1834 // Check for types that get concussive force
1835 if (!((hit_obj_ptr->type == OBJ_WEAPON) || (hit_obj_ptr->type == OBJ_ROBOT) ||
1836 (hit_obj_ptr->type == OBJ_BUILDING) || (hit_obj_ptr->type == OBJ_PLAYER) ||
1837 (hit_obj_ptr->type == OBJ_DOOR) || (hit_obj_ptr->type == OBJ_CLUTTER)))
1838 continue;
1839 if ((hit_obj_ptr != explode_obj_ptr) && !(hit_obj_ptr->flags & OF_DEAD)) {
1840 if (hit_obj_ptr->movement_type == MT_PHYSICS || hit_obj_ptr->movement_type == MT_WALKING) {
1841 if (hit_obj_ptr->mtype.phys_info.flags & PF_IGNORE_CONCUSSIVE_FORCES)
1842 continue;
1843 }
1844 if (hit_obj_ptr->movement_type == MT_PHYSICS || hit_obj_ptr->movement_type == MT_WALKING) {
1845 if ((hit_obj_ptr->mtype.phys_info.flags & PF_IGNORE_OWN_CONC_FORCES) &&
1846 ObjGetUltimateParent(hit_obj_ptr) == ObjGetUltimateParent(explode_obj_ptr))
1847 continue;
1848 }
1849 // The distance is actually the objects' centers minus some of the hit object's radius (I set it to 80%)
1850 if ((hit_obj_ptr->flags & OF_POLYGON_OBJECT) && hit_obj_ptr->type != OBJ_WEAPON &&
1851 hit_obj_ptr->type != OBJ_POWERUP && hit_obj_ptr->type != OBJ_DEBRIS) {
1852 vector pos;
1853 pos = hit_obj_ptr->pos + hit_obj_ptr->wall_sphere_offset;
1854 dist = vm_VectorDistanceQuick(&pos, &explode_obj_ptr->pos) -
1855 Poly_models[hit_obj_ptr->rtype.pobj_info.model_num].wall_size;
1856 } else {
1857 dist = vm_VectorDistanceQuick(&hit_obj_ptr->pos, &explode_obj_ptr->pos) - hit_obj_ptr->size;
1858 }
1859
1860 if (dist < 0.0f)
1861 dist = 0.0f;
1862 // Make damage be from 'maxdamage' to 0.0, where 0.0 is 'maxdistance' away;
1863 if (dist <= effect_distance) {
1864 // mprintf(0, "Distance = %f\n", dist);
1865 if (1 /*object_to_object_visibility(obj, hit_obj_ptr, FQ_TRANSWALL)*/) {
1866 fvi_query fq;
1867 fvi_info hit_info;
1868 int fate;
1869 if (explode_obj_ptr->type == OBJ_SHOCKWAVE) {
1870 int index = i >> 5;
1871 int bit = i % 32;
1872 if (explode_obj_ptr->mtype.shock_info.damaged_list[index] & (0x00000001 << bit)) {
1873 continue;
1874 } else {
1875 explode_obj_ptr->mtype.shock_info.damaged_list[index] |= (0x00000001 << bit);
1876 }
1877 }
1878 // This section excludes objects that are not visible at shockwave passby time
1879 {
1880 if (!BOA_IsVisible(hit_obj_ptr->roomnum, explode_obj_ptr->roomnum)) {
1881 continue;
1882 }
1883 vector subvec = hit_obj_ptr->pos - explode_obj_ptr->pos;
1884 vm_NormalizeVectorFast(&subvec);
1885 vector hitvec = (explode_obj_ptr->pos + (subvec * .05f));
1886 fq.p0 = &hitvec;
1887 fq.p1 = &hit_obj_ptr->pos;
1888 fq.startroom = explode_obj_ptr->roomnum;
1889
1890 fq.rad = 0.0f;
1891 fq.flags = FQ_CHECK_OBJS | FQ_ONLY_DOOR_OBJ | FQ_BACKFACE | FQ_NO_RELINK;
1892 fq.thisobjnum = OBJNUM(explode_obj_ptr);
1893 fq.ignore_obj_list = NULL;
1894 fate = fvi_FindIntersection(&fq, &hit_info);
1895 if (fate != HIT_NONE) {
1896 continue;
1897 }
1898 }
1899 if (hit_obj_ptr->type == OBJ_PLAYER)
1900 damage = max_player_damage - (dist / maxdistance) * max_player_damage;
1901 else
1902 damage = max_generic_damage - (dist / maxdistance) * max_generic_damage;
1903
1904 force = maxforce - (dist / maxdistance) * maxforce;
1905 // mprintf(0, "f %f d %f\n", force, damage);
1906 // Find the force vector on the object
1907 vm_GetNormalizedDirFast(&vforce, &hit_obj_ptr->pos, &explode_obj_ptr->pos);
1908 vforce *= force;
1909
1910 if (hit_obj_ptr->type == OBJ_CLUTTER) // Reduces the ping pong effect
1911 vforce *= .5f;
1912 if (hit_obj_ptr->movement_type == MT_PHYSICS)
1913 phys_apply_force(hit_obj_ptr, &vforce);
1914 // Apply damage to players, generics, and doors
1915 if (damage) {
1916
1917 if (hit_obj_ptr->type == OBJ_PLAYER) {
1918 if (Demo_flags != DF_PLAYBACK) {
1919 ApplyDamageToPlayer(hit_obj_ptr, parent_obj, PD_CONCUSSIVE_FORCE, damage * player_scalar);
1920 mprintf(0, "Applying %f damage to player from force.\n", damage);
1921 }
1922
1923 // shake player cockpit if damage is pretty bad.
1924 if (hit_obj_ptr->id == Player_num) {
1925 float mag = damage / 200.0f;
1926 vector vec = vforce;
1927 vm_NormalizeVector(&vec);
1928 // mprintf(0, "force: %.1f,%.1f,%.1f mag=%.2f\n", vec.x,vec.y,vec.z,mag);
1929 StartCockpitShake(mag, &vec);
1930 }
1931 } else if (IS_GENERIC(hit_obj_ptr->type) || (hit_obj_ptr->type == OBJ_DOOR)) {
1932 if (Demo_flags != DF_PLAYBACK)
1933 ApplyDamageToGeneric(hit_obj_ptr, parent_obj, GD_CONCUSSIVE, damage);
1934 }
1935 }
1936 } else {
1937 // mprintf(0, "No badass: robot=%2i, dist=%7.3f, maxdistance=%7.3f .\n", i, f2fl(dist), f2fl(maxdistance));
1938 } // end if (object_to_object_visibility...
1939 } // end if (dist < maxdistance)
1940 } // end for
1941 }
1942 } // end if (maxdamage...
1943 // return obj;
1944 }
1945 void CreateBlueBlastRing(vector *pos, int index, float lifetime, float max_size, int roomnum, int objnum,
1946 int force_up) {
1947 if (!Viewer_object)
1948 return;
1949 if (objnum == -1) // Create a new object
1950 {
1951 objnum = ObjCreate(OBJ_FIREBALL, index, roomnum, pos, NULL);
1952 if (objnum < 0) // DAJ -1FIX
1953 {
1954 mprintf(0, "Couldn't create blast object!\n");
1955 return;
1956 }
1957 Objects[objnum].size = 1.0;
1958 Objects[objnum].flags |= OF_USES_LIFELEFT;
1959 Objects[objnum].ctype.blast_info.max_size = max_size;
1960 }
1961 matrix tempm;
1962 // Point straight up
1963
1964 if (ps_rand() % 2 || force_up) {
1965 memset(&tempm, 0, sizeof(matrix));
1966 tempm.rvec.x = 1.0;
1967 tempm.uvec.z = -1.0;
1968 tempm.fvec.y = 1.0;
1969 } else {
1970 // Face the viewer
1971 vector fvec = Viewer_object->pos - *pos;
1972 vm_NormalizeVectorFast(&fvec);
1973 if (vm_GetMagnitudeFast(&fvec) < .5)
1974 return;
1975 vm_VectorToMatrix(&tempm, &fvec, NULL, NULL);
1976 }
1977 ObjSetOrient(&Objects[objnum], &tempm);
1978 Objects[objnum].lifeleft = lifetime / 2;
1979 Objects[objnum].lifetime = lifetime / 2;
1980 Objects[objnum].ctype.blast_info.bm_handle = Fireballs[BLUE_BLAST_RING_INDEX].bm_handle;
1981 }
1982 // Creates a blast ring to be drawn
1983 int CreateBlastRing(vector *pos, int index, float lifetime, float max_size, int roomnum, int force_blue) {
1984 int objnum;
1985 float vals[4];
1986 fvi_info hit_info;
1987 fvi_query fq;
1988 if (!Viewer_object)
1989 return -1;
1990 // First create a flash if the viewer can see it
1991 fq.p0 = &Viewer_object->pos;
1992 fq.p1 = pos;
1993 fq.startroom = Viewer_object->roomnum;
1994
1995 fq.rad = 0.01f;
1996 fq.flags = FQ_NO_RELINK;
1997 fq.thisobjnum = -1;
1998 fq.ignore_obj_list = NULL;
1999 int fate = fvi_FindIntersection(&fq, &hit_info);
2000 if (fate == HIT_NONE) {
2001 vals[0] = 1.0;
2002 vals[1] = 1.0;
2003 vals[2] = 1.0;
2004 vals[3] = .8f;
2005 CreateNewEvent(RENDER_EVENT, 0, 0, &vals, sizeof(float) * 4, DrawAlphaEvent);
2006 }
2007
2008 objnum = ObjCreate(OBJ_FIREBALL, index, roomnum, pos, NULL);
2009 if (objnum < 0) {
2010 mprintf(0, "Couldn't create blast object!\n");
2011 return -1;
2012 }
2013 Objects[objnum].size = max_size;
2014 Objects[objnum].flags |= OF_USES_LIFELEFT;
2015 Objects[objnum].lifeleft = lifetime;
2016 Objects[objnum].lifetime = lifetime;
2017 Objects[objnum].ctype.blast_info.max_size = max_size;
2018 Objects[objnum].ctype.blast_info.bm_handle = Fireballs[index].bm_handle;
2019 if ((ps_rand() % 2)) {
2020 ObjSetOrient(&Objects[objnum], &Identity_matrix);
2021 if ((ps_rand() % 3) == 0 || force_blue)
2022 CreateBlueBlastRing(pos, index, lifetime, max_size, roomnum, -1, force_blue);
2023 } else
2024 CreateBlueBlastRing(pos, index, lifetime, max_size, roomnum, objnum, force_blue);
2025
2026 return objnum;
2027 }
2028 // TODO: MTS: only used in this file.
2029 // Creates a standard blast ring for an object
2030 int CreateObjectBlastRing(object *objp) {
2031 float ring_size = OBJECT_OUTSIDE(objp) ? (objp->size * 3) : objp->size;
2032 return CreateBlastRing(&objp->pos, BLAST_RING_INDEX, DAMAGE_RING_TIME, ring_size, objp->roomnum);
2033 }
2034 // TODO: MTS: Not used?
2035 // Creates a smolding smoke to be drawn
2036 int CreateSmolderingObject(vector *pos, int index, float lifetime, float max_size, int roomnum) {
2037 int objnum;
2038 vector new_pos = *pos;
2039 new_pos.y = GetTerrainGroundPoint(&new_pos);
2040
2041 objnum = ObjCreate(OBJ_FIREBALL, index, roomnum, &new_pos, NULL);
2042 if (objnum < 0) {
2043 mprintf(0, "Couldn't create blast object!\n");
2044 return -1;
2045 }
2046 Objects[objnum].size = 1.0;
2047 Objects[objnum].flags |= OF_USES_LIFELEFT;
2048 Objects[objnum].lifeleft = lifetime;
2049 Objects[objnum].lifetime = lifetime;
2050 Objects[objnum].ctype.blast_info.max_size = max_size;
2051 return objnum;
2052 }
2053 // Draws a colored alpha disk...useful for cool lighting effects
2054 void DrawColoredDisk(vector *pos, float r, float g, float b, float inner_alpha, float outer_alpha, float size,
2055 uint8_t saturate, uint8_t lod) {
2056 rend_SetZBufferWriteMask(0);
2057 DrawColoredRing(pos, r, g, b, inner_alpha, outer_alpha, size, 0, saturate, lod);
2058 rend_SetZBufferWriteMask(1);
2059 }
2060 // TODO: MTS: Not used?
2061 // Draws a glowing cone of light using a bitmap
2062 void DrawColoredGlow(vector *pos, float r, float g, float b, float size) {
2063 rend_SetTextureType(TT_LINEAR);
2064 rend_SetAlphaType(AT_SATURATE_TEXTURE);
2065 rend_SetLighting(LS_GOURAUD);
2066 rend_SetColorModel(CM_RGB);
2067 ddgr_color color = GR_RGB(r * 255, g * 255, b * 255);
2068 int bm_handle = Fireballs[GRADIENT_BALL_INDEX].bm_handle;
2069 g3_DrawBitmap(pos, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle, color);
2070 }
2071
2072 // Draws a colored alpha ring...useful for cool lighting effects
2073 void DrawColoredRing(vector *pos, float r, float g, float b, float inner_alpha, float outer_alpha, float size,
2074 float inner_ring_ratio, uint8_t saturate, uint8_t lod) {
2075 static vector circleVecs[3][32];
2076 static int lodSegments[3] = {32, 16, 8};
2077 static bool firstCall = true;
2078
2079 if (firstCall) {
2080 // initialize the circleVecs
2081 firstCall = false;
2082 int lodi;
2083 for (lodi = 0; lodi < 3; ++lodi) {
2084 int numSegments = lodSegments[lodi];
2085 int ringIncrement = 65536 / numSegments;
2086 int ringAngle = 0;
2087 int i;
2088 for (i = 0; i < numSegments; ++i, ringAngle += ringIncrement) {
2089 float ringSin = FixSin(ringAngle);
2090 float ringCos = FixCos(ringAngle);
2091 circleVecs[lodi][i].x = ringCos;
2092 circleVecs[lodi][i].y = ringSin;
2093 circleVecs[lodi][i].z = 0.0f;
2094 }
2095 }
2096 }
2097
2098 // setup renderer
2099 rend_SetLighting(LS_GOURAUD);
2100 rend_SetTextureType(TT_FLAT);
2101 rend_SetOverlayType(OT_NONE);
2102 rend_SetColorModel(CM_RGB);
2103 rend_SetFlatColor(GR_RGB(r * 255, g * 255, b * 255));
2104 rend_SetAlphaType((saturate) ? AT_SATURATE_VERTEX : AT_VERTEX);
2105
2106 // check LOD level
2107 ASSERT(lod >= 0 && lod <= 2);
2108 int i, numSegments = lodSegments[lod];
2109
2110 // get the view matrix
2111 matrix viewOrient;
2112 g3_GetUnscaledMatrix(&viewOrient);
2113 viewOrient = ~viewOrient;
2114
2115 // setup the points
2116 g3Point innerPoints[32], outerPoints[32];
2117 float innerRingSize = size * inner_ring_ratio;
2118 float outerRingSize = size;
2119 for (i = 0; i < numSegments; ++i) {
2120 vector worldSpaceCircleVec = circleVecs[lod][i] * viewOrient;
2121 vector innerPos = (*pos) + (worldSpaceCircleVec * innerRingSize);
2122 vector outerPos = (*pos) + (worldSpaceCircleVec * outerRingSize);
2123
2124 g3_RotatePoint(&innerPoints[i], &innerPos);
2125 innerPoints[i].p3_flags |= PF_RGBA;
2126 innerPoints[i].p3_a = inner_alpha;
2127 innerPoints[i].p3_r = r;
2128 innerPoints[i].p3_g = g;
2129 innerPoints[i].p3_b = b;
2130
2131 g3_RotatePoint(&outerPoints[i], &outerPos);
2132 outerPoints[i].p3_flags |= PF_RGBA;
2133 outerPoints[i].p3_a = outer_alpha;
2134 outerPoints[i].p3_r = r;
2135 outerPoints[i].p3_g = g;
2136 outerPoints[i].p3_b = b;
2137 }
2138
2139 // and draw...
2140 g3Point *pntList[4];
2141 for (i = 0; i < numSegments; ++i) {
2142 int next = (i + 1) % numSegments;
2143 pntList[0] = &outerPoints[i];
2144 pntList[1] = &outerPoints[next];
2145 pntList[2] = &innerPoints[next];
2146 pntList[3] = &innerPoints[i];
2147 g3_DrawPoly(4, pntList, 0);
2148 }
2149 }
2150
2151 // TODO: MTS: not used?
2152 // Draws a sphere with the appropriate texture. If texture=-1, then uses rgb as colors
2153 void DrawSphere(vector *pos, float r, float g, float b, float alpha, float size, int texture, uint8_t saturate) {
2154 static vector sphere_vecs[16][16];
2155 static int first = 1;
2156 g3Point sphere_points[16][16], *pntlist[4], draw_points[4];
2157 int bm_handle = -1;
2158 ddgr_color color = GR_RGB(r * 255, g * 255, b * 255);
2159
2160 int i, t;
2161
2162 if (size < 0)
2163 return;
2164 if (first) {
2165 first = 0;
2166 // Create a unit sphere
2167 int increment = 65536 / 16;
2168 for (i = 0; i < 16; i++) {
2169 for (t = 0; t < 16; t++) {
2170 matrix tempm;
2171 angle pitch, heading;
2172 pitch = i * increment;
2173 heading = t * increment;
2174
2175 vm_AnglesToMatrix(&tempm, pitch, heading, 0);
2176 sphere_vecs[i][t] = tempm.fvec;
2177 }
2178 }
2179 }
2180 rend_SetZBufferWriteMask(0);
2181 draw_points[0].p3_u = 0;
2182 draw_points[0].p3_v = 0;
2183 draw_points[1].p3_u = 1.0 + ((ps_rand() % 100) / 500.0);
2184 draw_points[1].p3_v = 0;
2185 draw_points[2].p3_u = 1.0 + ((ps_rand() % 100) / 500.0);
2186 draw_points[2].p3_v = 1.0 + ((ps_rand() % 100) / 500.0);
2187 draw_points[3].p3_u = 0;
2188 draw_points[3].p3_v = 1.0 + ((ps_rand() % 100) / 500.0);
2189
2190 if (texture == -1) {
2191 rend_SetTextureType(TT_FLAT);
2192 rend_SetOverlayType(OT_NONE);
2193 rend_SetColorModel(CM_RGB);
2194 rend_SetLighting(LS_NONE);
2195 rend_SetFlatColor(color);
2196 rend_SetAlphaType(AT_CONSTANT);
2197 rend_SetAlphaValue(alpha * 255.0);
2198 } else {
2199 rend_SetTextureType(TT_LINEAR);
2200 rend_SetOverlayType(OT_NONE);
2201 rend_SetColorModel(CM_MONO);
2202 rend_SetLighting(LS_NONE);
2203 rend_SetAlphaType(AT_SATURATE_TEXTURE);
2204 rend_SetAlphaValue(alpha * 255.0);
2205 bm_handle = GetTextureBitmap(texture, 0);
2206 }
2207
2208 for (i = 0; i < 16; i++) {
2209 for (t = 0; t < 16; t++) {
2210 vector temp_vec = *pos + (sphere_vecs[i][t] * size);
2211 g3_RotatePoint(&sphere_points[i][t], &temp_vec);
2212 }
2213 }
2214 for (i = 0; i < 16; i++) {
2215 int next_i = (i + 1) % 16;
2216 for (t = 0; t < 16; t++) {
2217 int next_t = (t + 1) % 16;
2218 draw_points[0] = sphere_points[i][t];
2219 draw_points[1] = sphere_points[i][next_t];
2220 draw_points[2] = sphere_points[next_i][next_t];
2221 draw_points[3] = sphere_points[next_i][t];
2222
2223 for (int k = 0; k < 4; k++) {
2224 draw_points[k].p3_flags |= PF_UV | PF_RGBA;
2225 g3_CodePoint(&draw_points[k]);
2226 pntlist[k] = &draw_points[k];
2227 }
2228 g3_CheckAndDrawPoly(4, pntlist, bm_handle, NULL, &sphere_vecs[0][0]);
2229 }
2230 }
2231 rend_SetZBufferWriteMask(1);
2232 }
2233 #define BLAST_RING_ALPHA 1.0f
2234 // TODO: MTS: only used in this file.
2235 // Draws a blast ring
2236 void DrawBlastRingObject(object *obj) {
2237 vector inner_vecs[30], outer_vecs[30];
2238 g3Point inner_points[30], outer_points[30];
2239 float lifenorm = (obj->lifetime - obj->lifeleft) / obj->lifetime;
2240 float cur_size = lifenorm * obj->ctype.blast_info.max_size;
2241 int i;
2242 g3Point *pntlist[4];
2243 static int grav_first = 1;
2244 static int grav_texture;
2245
2246 if (obj->id == GRAVITY_FIELD_INDEX)
2247 cur_size = (1 - lifenorm) * obj->ctype.blast_info.max_size;
2248
2249 int bm_handle = obj->ctype.blast_info.bm_handle;
2250 int num_segments = 20;
2251 int ring_increment = 65536 / num_segments;
2252 int ring_angle = 0;
2253
2254 if (lifenorm > 1.0)
2255 lifenorm = 1.0;
2256 // First draw a blast sphere
2257 float sphere_norm;
2258 int rot_angle;
2259 // Scale lifetime to make it look like a lensflare
2260 if (lifenorm < .1) {
2261 sphere_norm = lifenorm * 10;
2262 rot_angle = (FrameCount * 20) % 65536;
2263 } else {
2264 sphere_norm = 1.0 - ((lifenorm - .1) * 1.5);
2265 rot_angle = (FrameCount * 550) % 65536;
2266 if (sphere_norm < 0)
2267 sphere_norm = 0;
2268 }
2269 float sphere_alpha = sphere_norm;
2270 int disk_handle;
2271 float disk_size = sphere_norm * obj->ctype.blast_info.max_size;
2272 if (obj->id == GRAVITY_FIELD_INDEX) {
2273 rend_SetZBias(-(cur_size / 2));
2274 DrawColoredDisk(&obj->pos, .3f, .5f, 1, .9f, 0, disk_size, 1, 0);
2275 // Set states for the ring
2276 rend_SetOverlayType(OT_NONE);
2277 rend_SetLighting(LS_NONE);
2278 rend_SetTextureType(TT_LINEAR);
2279 rend_SetAlphaType(AT_SATURATE_TEXTURE);
2280 rend_SetAlphaValue(sphere_alpha * 255.0);
2281 rend_SetZBufferWriteMask(0);
2282
2283 } else {
2284 disk_handle = Fireballs[SHRINKING_BLAST_INDEX].bm_handle;
2285
2286 rend_SetOverlayType(OT_NONE);
2287 rend_SetLighting(LS_NONE);
2288 rend_SetTextureType(TT_LINEAR);
2289 rend_SetAlphaType(AT_SATURATE_TEXTURE);
2290 rend_SetAlphaValue(sphere_alpha * 255.0);
2291 rend_SetZBufferWriteMask(0);
2292 rend_SetZBias(-(cur_size / 2));
2293 if (sphere_norm >= 0.0)
2294 g3_DrawRotatedBitmap(&obj->pos, rot_angle, disk_size, (disk_size * bm_h(disk_handle, 0)) / bm_w(disk_handle, 0),
2295 disk_handle);
2296 }
2297 // Now draw a blast ring
2298 if (lifenorm < .2) {
2299 lifenorm = lifenorm * 3;
2300 } else
2301 lifenorm = .6 + (((lifenorm - .2) / .8) * .4);
2302 if (lifenorm > 1)
2303 lifenorm = 1;
2304
2305 rend_SetAlphaType(AT_SATURATE_TEXTURE_VERTEX);
2306 rend_SetAlphaValue(255);
2307 ring_angle = 0;
2308 cur_size *= 2;
2309 float alpha = 1.0;
2310 if (lifenorm > .5)
2311 alpha = 1.0 - ((lifenorm - .5) / .5);
2312 for (i = 0; i < num_segments; i++, ring_angle += ring_increment) {
2313 float ring_sin = FixSin(((int)(ring_angle + (lifenorm * 65536)) % 65536));
2314 float ring_cos = FixCos(((int)(ring_angle + (lifenorm * 65536)) % 65536));
2315
2316 inner_vecs[i] = obj->orient.rvec * (ring_cos * cur_size * .50);
2317 inner_vecs[i] += obj->orient.fvec * (ring_sin * cur_size * .50);
2318 inner_vecs[i] += obj->pos;
2319 outer_vecs[i] = obj->orient.rvec * (ring_cos * cur_size);
2320 outer_vecs[i] += obj->orient.fvec * (ring_sin * cur_size);
2321 outer_vecs[i] += obj->pos;
2322
2323 g3_RotatePoint(&inner_points[i], &inner_vecs[i]);
2324 g3_RotatePoint(&outer_points[i], &outer_vecs[i]);
2325 outer_points[i].p3_flags |= PF_UV | PF_RGBA;
2326 inner_points[i].p3_flags |= PF_UV | PF_RGBA;
2327 outer_points[i].p3_a = 1.0 - lifenorm;
2328 inner_points[i].p3_a = 0;
2329 }
2330 for (i = 0; i < num_segments; i++) {
2331 int next = (i + 1) % num_segments;
2332 outer_points[i].p3_u = 0;
2333 outer_points[next].p3_u = 1.0;
2334 outer_points[i].p3_v = 0;
2335 outer_points[next].p3_v = 0;
2336 inner_points[i].p3_u = 0;
2337 inner_points[next].p3_u = 1.0;
2338 inner_points[i].p3_v = 1;
2339 inner_points[next].p3_v = 1;
2340 pntlist[0] = &outer_points[i];
2341 pntlist[1] = &outer_points[next];
2342 pntlist[2] = &inner_points[next];
2343 pntlist[3] = &inner_points[i];
2344 g3_DrawPoly(4, pntlist, bm_handle);
2345 }
2346 rend_SetZBias(0);
2347 rend_SetZBufferWriteMask(1);
2348 }
2349
2350 // Creates a blast ring
2351 void DoBlastRingEvent(int eventnum, void *data) {
2352 object *obj = (object *)data;
2353 CreateObjectBlastRing(obj);
2354 }
2355 // TODO: MTS: only used in this file.
2356 // Creates an explosion
2357 void DoExplosionEvent(int eventnum, void *data) {
2358 float *vals = (float *)data;
2359 vector pos;
2360 int fireball_num, roomnum;
2361 float size;
2362 float upwards_velocity;
2363 pos_state sound_pos;
2364 pos.x = vals[0];
2365 pos.y = vals[1];
2366 pos.z = vals[2];
2367 fireball_num = (int)vals[3];
2368 roomnum = (int)vals[4];
2369 size = vals[5];
2370 upwards_velocity = vals[6];
2371 int visnum = CreateFireball(&pos, fireball_num, roomnum, VISUAL_FIREBALL);
2372 if (visnum >= 0) {
2373 vector zero_vec = {0, 0, 0};
2374 matrix id_mat = Identity_matrix;
2375
2376 sound_pos.position = &pos;
2377 sound_pos.roomnum = roomnum;
2378 sound_pos.orient = &id_mat;
2379 sound_pos.velocity = &zero_vec;
2380
2381 if (ps_rand() > D3_RAND_MAX / 2)
2382 Sound_system.Play3dSound(SOUND_ROBOT_EXPLODE_1, SND_PRIORITY_HIGH, &sound_pos);
2383 else
2384 Sound_system.Play3dSound(SOUND_ROBOT_EXPLODE_2, SND_PRIORITY_HIGH, &sound_pos);
2385 VisEffects[visnum].size = size;
2386 if (upwards_velocity > .001) {
2387 VisEffects[visnum].movement_type = MT_PHYSICS;
2388 VisEffects[visnum].phys_flags = PF_FIXED_VELOCITY;
2389 VisEffects[visnum].velocity.x = 0;
2390 VisEffects[visnum].velocity.z = 0;
2391 VisEffects[visnum].velocity.y = upwards_velocity;
2392 }
2393 }
2394 }
2395 // An event handler that simply draws an alpha blended poly on top of the screen
2396 // Takes a 4 element array of floats int r,g,b,a format
2397 void DrawAlphaEvent(int eventnum, void *data) {
2398 float *vals = (float *)data;
2399
2400 DrawAlphaBlendedScreen(vals[0], vals[1], vals[2], vals[3]);
2401 }
2402 // Returns a random explosion based on the explosion size
2403 int GetRandomExplosion(float size) {
2404 if (size > EXTRA_EXPLOSION_THRESHOLD)
2405 return BIG_EXPLOSION_INDEX;
2406 else
2407 return GetRandomMediumExplosion();
2408 }
2409 // TODO: MTS: only found in this file.
2410 // Returns a random medium sized explosion
2411 int GetRandomMediumExplosion() {
2412 int choices[] = {MED_EXPLOSION_INDEX, MED_EXPLOSION_INDEX2, MED_EXPLOSION_INDEX3};
2413 int pick = ps_rand() % 3;
2414 return (choices[pick]);
2415 }
2416 int GetRandomSmallExplosion() {
2417 int choices[] = {SMALL_EXPLOSION_INDEX, SMALL_EXPLOSION_INDEX2};
2418 int pick = ps_rand() % 2;
2419 return (choices[pick]);
2420 }
2421 // TODO: MTS: only found in this file.
2422 int GetRandomBillowingExplosion() {
2423 int choices[] = {BILLOWING_INDEX, MED_EXPLOSION_INDEX2};
2424 int pick = ps_rand() % 2;
2425 return (choices[pick]);
2426 }
2427 void DrawSmolderingObject(object *obj) {
2428 vector veca;
2429 angvec angs;
2430 vector world_verts[4];
2431 g3Point pnts[4], *pntlist[4];
2432 if (!Viewer_object)
2433 return;
2434 vm_ExtractAnglesFromMatrix(&angs, &Viewer_object->orient);
2435 veca.x = FixCos(angs.h);
2436 veca.z = FixSin(angs.h);
2437 veca.y = 0;
2438 world_verts[0] = obj->pos - (veca * 10);
2439 world_verts[1] = obj->pos + (veca * 10);
2440 world_verts[2] = obj->pos + (veca * 10);
2441 world_verts[3] = obj->pos - (veca * 10);
2442 world_verts[0].y += obj->ctype.blast_info.max_size;
2443 world_verts[1].y += obj->ctype.blast_info.max_size;
2444 for (int i = 0; i < 4; i++) {
2445 g3_RotatePoint(&pnts[i], &world_verts[i]);
2446 pnts[i].p3_flags |= PF_UV;
2447 pntlist[i] = &pnts[i];
2448 }
2449 pnts[0].p3_u = 0;
2450 pnts[0].p3_v = 0;
2451 pnts[1].p3_u = 1;
2452 pnts[1].p3_v = 0;
2453 pnts[2].p3_u = 1;
2454 pnts[2].p3_v = 1;
2455 pnts[3].p3_u = 0;
2456 pnts[3].p3_v = 1;
2457 rend_SetTextureType(TT_LINEAR);
2458 rend_SetLighting(LS_NONE);
2459 rend_SetOverlayType(OT_NONE);
2460 rend_SetAlphaType(AT_CONSTANT + AT_TEXTURE);
2461 rend_SetAlphaValue(.4 * 255);
2462
2463 vclip *vc = &GameVClips[Fireballs[obj->id].bm_handle];
2464 int bm_handle = vc->frames[3];
2465 g3_DrawPoly(4, pntlist, bm_handle);
2466 }
2467 // Creates end points that simulate lightning
2468 void CreateLightningRodPositions(vector *src, vector *dest, vector *world_vecs, int num_segments, float rand_mag,
2469 bool do_flat) {
2470 vector delta_vec = *dest - *src;
2471 matrix mat;
2472
2473 // Setup some interpolants
2474 float mag = vm_GetMagnitudeFast(&delta_vec);
2475 float delta_dist = mag / num_segments;
2476 delta_vec /= mag;
2477 // Create matrix for randomization
2478 vm_VectorToMatrix(&mat, &delta_vec, NULL, NULL);
2479
2480 vector cur_pos = *src;
2481
2482 vector from = cur_pos;
2483 world_vecs[0] = from;
2484 world_vecs[num_segments - 1] = *dest;
2485 // Create the points
2486 for (int i = 1; i < num_segments - 1; i++, cur_pos += (delta_vec * delta_dist)) {
2487 vector to = cur_pos + (delta_vec * delta_dist);
2488 // vector to=world_vecs[i-1]+(delta_vec*delta_dist);
2489 to += mat.rvec * ((((ps_rand() % 200) - 100) / 100.0) * rand_mag);
2490 if (!do_flat)
2491 to += mat.uvec * ((((ps_rand() % 200) - 100) / 100.0) * rand_mag);
2492 ASSERT(i < 50);
2493
2494 world_vecs[i] = to;
2495 }
2496 }