Debian Codegraph @

Descent3/viseffect.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/viseffect.cpp $
21 * $Revision: 145 $
22 * $Date: 4/19/00 5:26p $
23 * $Author: Matt $
24 *
25 * System for creating small visual effects visects
26 *
27 * $Log: /DescentIII/main/viseffect.cpp $
28 *
29 * 145 4/19/00 5:26p Matt
30 * From Duane for 1.4
31 * Changes to InitVisEffects()
32 *
33 * 144 3/21/00 9:58a Matt
34 * Changed to Mac-only the code that sets a variable number of vis effects
35 * based on texture quality.
36 *
37 * 143 3/20/00 2:33p Matt
38 * Merge of Duane's post-1.3 changes.
39 * Small change in freeing viseffect memory.
40 * Fixed botched change with sine wave code.
41 *
42 * 141 11/05/99 3:17p Jeff
43 * fixed visalloc bug for pc
44 *
45 * 140 10/22/99 2:38p Jay
46 * Update for the FinalBoss Laser Effect
47 *
48 * 139 10/21/99 9:29p Jeff
49 * B.A. Macintosh code merge
50 *
51 * 138 10/12/99 11:08a Jeff
52 * grey lightning effect
53 *
54 * 137 9/21/99 2:54p Jeff
55 * grey spark's want lighting
56 *
57 * 136 7/20/99 1:00p Jason
58 * added auto katmai support
59 *
60 * 135 5/17/99 6:11p Jason
61 * moved center of lightning effect
62 *
63 * 134 5/14/99 12:15p Jason
64 * fixed vis effect problem for luke
65 *
66 * 133 5/13/99 3:42p Ardussi
67 * changes for compiling on the Mac
68 *
69 * 132 5/10/99 8:20a Chris
70 * Fixed bug with weather lightning effects and Non-render objects
71 *
72 * 131 5/10/99 1:53a Jason
73 * add blackshark explosion
74 *
75 * 130 5/05/99 6:30p Jason
76 * toned down mass driver effect
77 *
78 * 129 5/04/99 7:30p Jason
79 * fixed viseffect linking problem
80 *
81 * 128 5/02/99 4:14p Jason
82 * fixed bug with attached viseffects
83 *
84 * 127 5/02/99 1:36a Jason
85 * added moving object lighting viseffects
86 *
87 * 126 5/02/99 12:21a Jason
88 * fixed crash bug with napalm vis effects
89 *
90 * 125 5/01/99 4:58p Jason
91 * made real viseffects not get created if not seen
92 *
93 * 124 4/30/99 7:51p Jason
94 * fixed lighting coming from controlled viseffects
95 *
96 * 123 4/30/99 6:21p Jeff
97 * fixed autotile (Jason)
98 *
99 * 122 4/30/99 5:42p Jason
100 * changes for blast rings
101 *
102 * 121 4/28/99 11:28a Jason
103 * made real viseffects not cast light
104 *
105 * 120 4/26/99 7:42p Jason
106 * fixed spew effect
107 *
108 * 119 4/21/99 11:05a Kevin
109 * new ps_rand and ps_srand to replace rand & srand
110 *
111 * 118 4/19/99 3:18p Jason
112 * made real vis effects work
113 *
114 * 117 4/19/99 3:47a Jeff
115 * fixed min/max for Linux
116 *
117 * 116 4/14/99 3:57a Jeff
118 * fixed case mismatch in #includes
119 *
120 * 115 4/13/99 12:16p Jason
121 * more tweaks for the mass driver
122 *
123 * 114 4/09/99 7:49p Jason
124 * yet more mass driver fixes
125 *
126 * 113 4/09/99 7:26p Jason
127 * added better mass driver effect
128 *
129 * 112 4/07/99 3:53p Jason
130 * fixed lightning autotile problem
131 *
132 * 111 4/06/99 6:25p Jason
133 * more mass driver tweaks
134 *
135 * 110 4/06/99 4:14p Jason
136 * made mass driver a little cooler
137 *
138 * 109 4/06/99 1:51p Jason
139 * added new mass driver effect
140 *
141 * 108 4/05/99 4:39p Jason
142 * added groovy new smoke trails
143 *
144 * 107 4/03/99 4:25p Jason
145 * sped up attached vis effects by a large amount
146 *
147 * 106 4/02/99 3:58p Jason
148 * sped up vis effect stuff
149 *
150 * 105 4/01/99 1:10p Jason
151 * made vis effects better for the lighting system
152 *
153 * 104 3/31/99 1:20p Kevin
154 * Fix for Demos and Gamegauge -- to deal with situations where time goes
155 * backwards
156 *
157 * 103 3/31/99 11:42a Jason
158 * added support for attached thick lightning
159 *
160 * 102 3/30/99 7:18p Jason
161 * made blast rings more noticeable
162 *
163 * 101 3/30/99 6:01p Jason
164 * added new procedural effect
165 *
166 * 100 3/30/99 5:19p Jason
167 * fixed saturation of thicklighting
168 *
169 * 99 3/29/99 7:30p Jason
170 * added cool new energy effect
171 *
172 * 98 3/27/99 2:15p Jason
173 * added better thick lightining
174 *
175 * 97 3/25/99 3:30p Jason
176 *
177 * 96 3/24/99 6:28p Jason
178 * fixed size with thick lightning
179 *
180 * 95 3/23/99 4:10p Jason
181 * more tweaks for line sparks
182 *
183 * 94 3/23/99 3:52p Jason
184 * fixed sine wave bug
185 *
186 * 93 3/23/99 12:50p Jason
187 * added line sparks
188 *
189 * 92 3/22/99 11:17a Jason
190 * Katmai enhancements
191 *
192 * 91 2/25/99 11:01a Matt
193 * Added new explosion system.
194 *
195 * 90 2/15/99 4:10p Jason
196 * made non-vis objects kill all their attach sounds/objects
197 *
198 * 89 2/09/99 2:39p Jason
199 * added destroyable faces
200 *
201 * 88 1/26/99 6:39p Jason
202 * added wall effects code
203 *
204 * 87 1/21/99 11:15p Jeff
205 * pulled out some structs and defines from header files and moved them
206 * into seperate header files so that multiplayer dlls don't require major
207 * game headers, just those new headers. Side effect is a shorter build
208 * time. Also cleaned up some header file #includes that weren't needed.
209 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
210 * manage.h and multi.h
211 *
212 * 86 12/11/98 5:08p Jason
213 * changed mass driver effect
214 *
215 * 85 11/19/98 6:38p Jason
216 * made a better black shark effect
217 *
218 * 84 11/18/98 2:54p Jason
219 * added snow effect
220 *
221 * 83 11/10/98 3:28p Jason
222 * made dynamic lighting work more sensibly
223 *
224 * 82 10/21/98 12:47p Jason
225 * fixed stupid bug with my last rev
226 *
227 * 81 10/20/98 9:52p Jason
228 * took out assert
229 *
230 * 80 10/05/98 11:35p Matt
231 * Added assert
232 *
233 * 79 10/05/98 7:29p Jason
234 * added cool black smoke
235 *
236 * 78 9/30/98 2:26p Jason
237 * fixed billboard bug
238 *
239 * 77 9/30/98 12:59p Jason
240 * added some cool matcen effects
241 *
242 * 76 9/29/98 7:15p Jason
243 * added axis billboards
244 *
245 * 75 9/29/98 12:49p Jason
246 * worked on matcen effects and lightning
247 *
248 * 74 9/28/98 4:41p Jason
249 * made explosions give dynamic light
250 *
251 * 73 9/24/98 12:57p Jason
252 * more state limited optimizations
253 *
254 * 72 9/21/98 7:01p Jason
255 * miscellaneous visual tweaks
256 *
257 * 71 9/21/98 6:53p Sean
258 * took out stupid debug assert
259 *
260 * 70 9/18/98 8:23p Jason
261 * fixed insidious vis effect errors
262 *
263 * 69 9/18/98 11:18a Jason
264 * fixed miscellaneous bugs with camera system
265 *
266 * 68 9/17/98 6:08p Jason
267 * more tweaks for effects
268 *
269 * 67 9/17/98 3:03p Jason
270 * added lightning and invul hit effects
271 *
272 * 66 9/15/98 6:39p Jason
273 * made medium smoke saturate
274 *
275 * 65 9/15/98 4:31p Jason
276 * added more functionality for the dedicated server
277 *
278 * 64 9/09/98 4:49p Jason
279 * tweaked black shark lightning effect
280 *
281 * 63 9/09/98 4:30p Jason
282 * fixed a remaining (and insidious) bug with gunpoint instancing
283 *
284 * 62 9/09/98 4:28p Jason
285 * added better weapon effects
286 *
287 * 61 8/17/98 12:10p Chris
288 * Fixed MAJOR bug in getting gunpoint positions
289 *
290 * 60 8/03/98 11:12a Jason
291 * fixed some zero foward vector errors
292 *
293 * 59 6/24/98 3:56p Jason
294 * added code for new omega cannon
295 *
296 * 58 6/23/98 3:34p Jason
297 * added cool lighting effect to gravity field
298 *
299 * 57 6/22/98 6:52p Jason
300 * changed gravity field effect somewhat
301 *
302 * 56 6/22/98 6:26p Jason
303 * added gravity field effect for weapons
304 *
305 * 55 6/12/98 5:14p Jason
306 * made viseffects deal with zbuffer better
307 *
308 * 54 6/10/98 4:47p Jason
309 * smoke trail test
310 *
311 * 53 6/02/98 11:02a Jason
312 * Post E3 Checkin
313 *
314 * 52 5/27/98 5:17p Jason
315 * fixed some bugs for the E3 Demo
316 *
317 * 51 5/26/98 6:54p Jason
318 * viseffect optimizations
319 *
320 * 50 5/26/98 6:36p Jason
321 * made particle viseffects not relink
322 *
323 * 49 5/15/98 11:22a Jason
324 * make smoke trail saturation settable by designers
325 *
326 * 48 5/13/98 12:05p Jason
327 * made napalm explosions bigger for bigger objects
328 *
329 * 47 5/06/98 1:25p Jason
330 * more viseffect tweaks
331 *
332 * 46 5/06/98 12:55p Jason
333 * did some vis effect optimizations
334 *
335 * 45 5/01/98 6:23p Jason
336 * made better puddle rain
337 *
338 * 44 4/30/98 6:45p Jason
339 * more changes for weather
340 *
341 * 43 4/30/98 3:39p Jason
342 * don't make non-billowingfireballs rotate
343 *
344 * 42 4/30/98 11:55a Jason
345 * weather tweaks
346 *
347 * 41 4/29/98 3:27p Jason
348 * added weather engine
349 *
350 * 40 4/29/98 12:50p Jason
351 * more rain changes
352 *
353 * 39 4/29/98 11:38a Jason
354 * added some weather effects (first pass)
355 *
356 * 38 4/23/98 6:38p Jason
357 * made bitmaps use 1555 format
358 *
359 * 37 4/20/98 3:58p Jason
360 * made viseffects attach to subobjects other than zero
361 *
362 * 36 4/19/98 5:00p Jason
363 * added cool napalm effect, plus made object effects dynamically
364 * allocated
365 *
366 * 35 4/17/98 12:45p Jason
367 * various changes for multiplayer
368 *
369 * 34 4/15/98 2:33p Jason
370 * changed some smoke trail stuff
371 *
372 * 33 4/15/98 12:22p Jason
373 * lots of miscellaneous stuff pertaining to lighting and vis effects
374 *
375 * 32 4/10/98 12:39p Jason
376 * added expanding explosion bitmaps
377 *
378 * 31 4/09/98 4:53p Jason
379 * fixed particle bug
380 *
381 * 30 4/07/98 3:31p Jason
382 * got particle effects working with weapons
383 *
384 * 29 4/07/98 12:54p Jason
385 * changes for viseffects and multiplayer dll
386 *
387 * 28 4/02/98 12:24p Jason
388 * trimmed some fat from our structures
389 *
390 * 27 3/31/98 2:22p Jason
391 * bug fix for dead vis effects
392 *
393 * 26 3/30/98 12:46a Jason
394 * sped up deletion of vis effects
395 *
396 * 25 3/09/98 1:00p Jason
397 * toned down explosion lighting
398 *
399 * 24 3/05/98 6:56p Jason
400 * temp fix for out of bound errors on viseffects
401 *
402 * 23 2/26/98 3:20p Jason
403 * changes for faster lighting
404 *
405 * 22 2/25/98 4:59p Jason
406 * got dynamic explosion lighting working with vis-effects
407 *
408 * 21 2/25/98 4:31p Jason
409 * changes for explosions
410 *
411 * 20 2/17/98 1:50p Jason
412 * fixed viseffect zbias problem
413 *
414 * 19 2/17/98 1:00p Jason
415 * fixed viseffect clipping
416 *
417 * 18 2/16/98 2:37p Jason
418 * added real viseffects
419 *
420 * 17 2/06/98 1:00p Jason
421 * did some state setting for blast rings
422 *
423 * 16 2/05/98 6:53p Jason
424 * added new weapon slot
425 *
426 * 15 2/05/98 2:54p Jason
427 * changes for explosions
428 *
429 * 14 2/05/98 12:37p Jason
430 * added more weapon effects
431 *
432 * 13 2/04/98 9:28p Jason
433 * added some new weapons effects
434 *
435 * 12 2/04/98 6:09p Matt
436 * Changed object room number to indicate a terrain cell via a flag. Got
437 * rid of the object flag which used to indicate terrain.
438 *
439 */
440
441 #include <algorithm>
442 #include <cstdlib>
443 #include <memory.h>
444
445 #include "config.h"
446 #include "dedicated_server.h"
447 #include "fireball.h"
448 #include "game.h"
449 #include "gametexture.h"
450 #include "lighting.h"
451 #include "mem.h"
452 #include "object.h"
453 #include "physics.h"
454 #include "player.h"
455 #include "polymodel.h"
456 #include "psrand.h"
457 #include "room.h"
458 #include "terrain.h"
459 #include "vclip.h"
460 #include "viseffect.h"
461 #include "weapon.h"
462 #include "weather.h"
463
464 // DAJ vis_effect VisEffects[max_vis_effects];
465 // DAJ uint16_t VisDeadList[max_vis_effects];
466 // DAJ static int16_t Vis_free_list[max_vis_effects];
467
468 vis_effect *VisEffects = NULL;
469
470 static int16_t *Vis_free_list = NULL;
471 uint16_t *VisDeadList = NULL;
472
473 uint16_t max_vis_effects = 0;
474
475 int NumVisDead = 0;
476 int Highest_vis_effect_index = 0;
477 int Num_vis_effects = 0;
478
479 void ShutdownVisEffects() {
480 if (VisEffects)
481 mem_free(VisEffects);
482 if (VisDeadList)
483 mem_free(VisDeadList);
484 if (Vis_free_list)
485 mem_free(Vis_free_list);
486 }
487
488 // Goes through our array and clears the slots out
489 void InitVisEffects() {
490 static uint16_t old_max_vis = 0;
491 max_vis_effects = MAX_VIS_EFFECTS; // always peg to max on PC
492
493 if (old_max_vis == max_vis_effects)
494 return;
495
496 if (VisEffects != NULL) {
497 VisEffects = (vis_effect *)mem_realloc(VisEffects, sizeof(vis_effect) * max_vis_effects);
498 VisDeadList = (uint16_t *)mem_realloc(VisDeadList, sizeof(uint16_t) * max_vis_effects);
499 Vis_free_list = (int16_t *)mem_realloc(Vis_free_list, sizeof(int16_t) * max_vis_effects);
500 } else if (VisEffects == NULL) {
501 VisEffects = (vis_effect *)mem_malloc(sizeof(vis_effect) * max_vis_effects);
502 VisDeadList = (uint16_t *)mem_malloc(sizeof(uint16_t) * max_vis_effects);
503 Vis_free_list = (int16_t *)mem_malloc(sizeof(int16_t) * max_vis_effects);
504 }
505 for (int i = 0; i < max_vis_effects; i++) {
506 VisEffects[i].type = VIS_NONE;
507 VisEffects[i].roomnum = -1;
508 VisEffects[i].prev = -1;
509 VisEffects[i].next = -1;
510 Vis_free_list[i] = i;
511 }
512 old_max_vis = max_vis_effects;
513 Num_vis_effects = 0;
514 Highest_vis_effect_index = 0;
515
516 atexit(ShutdownVisEffects);
517 }
518
519 // Returns the next free viseffect
520 int VisEffectAllocate() {
521 if (Num_vis_effects == max_vis_effects) {
522 mprintf(0, "Couldn't allocate vis effect!\n");
523 return -1;
524 }
525
526 int n = Vis_free_list[Num_vis_effects++];
527 ASSERT(VisEffects[n].type == VIS_NONE); // Get Jason
528
529 if (n > Highest_vis_effect_index) {
530 Highest_vis_effect_index = n;
531 }
532
533 return n;
534 }
535
536 // Frees up a viseffect for use
537 int VisEffectFree(int visnum) {
538 ASSERT(visnum >= 0 && visnum <= max_vis_effects);
539
540 Vis_free_list[--Num_vis_effects] = visnum;
541 VisEffects[visnum].type = VIS_NONE;
542
543 if (visnum == Highest_vis_effect_index) {
544 while (VisEffects[Highest_vis_effect_index].type != VIS_NONE && Highest_vis_effect_index > 0)
545 Highest_vis_effect_index--;
546 }
547
548 return 1;
549 }
550
551 int VisEffectInitType(vis_effect *vis) {
552 int ret = 1;
553
554 vis->size = Fireballs[vis->id].size;
555 vis->flags |= VF_USES_LIFELEFT;
556 vis->lifeleft = Fireballs[vis->id].total_life;
557 vis->lifetime = vis->lifeleft;
558
559 if (Fireballs[vis->id].type == FT_EXPLOSION && vis->id != CUSTOM_EXPLOSION_INDEX && vis->id != NAPALM_BALL_INDEX)
560 vis->lighting_color = OPAQUE_FLAG | GR_RGB16(255, 180, 20);
561
562 return ret;
563 }
564
565 // initialize a new viseffect. adds to the list for the given room
566 // returns the vis number
567 int VisEffectCreate(uint8_t type, uint8_t id, int roomnum, vector *pos) {
568 int visnum;
569 vis_effect *vis;
570
571 if ((Game_mode & GM_MULTI) && Dedicated_server)
572 return -1; // Dedicated servers don't need to create vis effects
573
574 if (ROOMNUM_OUTSIDE(roomnum)) {
575 if (CELLNUM(roomnum) > TERRAIN_WIDTH * TERRAIN_DEPTH)
576 return -1;
577 if (CELLNUM(roomnum) < 0)
578 return -1;
579
580 if (Gametime - Last_terrain_render_time > 5.0)
581 return -1;
582 } else {
583 ASSERT(roomnum >= 0 && roomnum <= Highest_room_index && Rooms[roomnum].used);
584
585 // Don't create viseffects in rooms we haven't seen in a while
586 if (Gametime - Rooms[roomnum].last_render_time > 5.0)
587 return -1;
588 }
589
590 // Find next free object
591 visnum = VisEffectAllocate();
592
593 if (visnum == -1) // no free objects
594 return -1;
595
596 ASSERT(VisEffects[visnum].type == VIS_NONE); // make sure unused
597
598 vis = &VisEffects[visnum];
599
600 ASSERT(vis->roomnum == -1);
601
602 memset(vis, 0, sizeof(vis_effect));
603
604 // Fill in fields
605 vis->type = VIS_FIREBALL;
606 vis->id = id;
607 vis->pos = *pos;
608 vis->flags = 0;
609 vis->phys_flags = 0;
610 vis->roomnum = roomnum;
611 vis->movement_type = MT_NONE;
612 vis->attach_info.end_vertnum = -1;
613 vis->next = -1;
614 vis->prev = -1;
615 vis->lighting_color = 0;
616
617 ASSERT(roomnum != -1);
618
619 vis->creation_time = Gametime;
620
621 // Initialize all the type-specific info
622 VisEffectInitType(vis);
623
624 VisEffectLink(visnum, roomnum);
625
626 return visnum;
627 }
628
629 // Creates vis effects but has the caller set their parameters
630 // initialize a new viseffect. adds to the list for the given room
631 // returns the vis number
632 int VisEffectCreateControlled(uint8_t type, object *parent, uint8_t id, int roomnum, vector *pos, float lifetime,
633 vector *velocity, int phys_flags, float size, float drag, float mass, bool isreal) {
634 int visnum;
635 vis_effect *vis;
636 static int napalm_id = -1;
637
638 if (napalm_id == -1)
639 napalm_id = FindWeaponName("Napalm");
640
641 if (isreal) {
642 ASSERT(parent != NULL); // You have a spew that has no parent object?
643
644 if (ROOMNUM_OUTSIDE(roomnum)) {
645 if (CELLNUM(roomnum) > TERRAIN_WIDTH * TERRAIN_DEPTH)
646 return -1;
647 if (CELLNUM(roomnum) < 0)
648 return -1;
649
650 if (Gametime - Last_terrain_render_time > 5.0)
651 return -1;
652 } else {
653 ASSERT(roomnum >= 0 && roomnum <= Highest_room_index && Rooms[roomnum].used);
654
655 // Don't create real objects in rooms we haven't seen in a while
656 if (Gametime - Rooms[roomnum].last_render_time > 5.0)
657 return -1;
658 }
659
660 int objnum = CreateAndFireWeapon(pos, velocity, parent, napalm_id);
661 if (objnum >= 0) {
662 object *obj = &Objects[objnum];
663
664 obj->lifeleft = lifetime;
665 obj->lifetime = lifetime;
666 obj->movement_type = MT_PHYSICS;
667
668 if (mass > 0 && drag > 0) {
669 obj->mtype.phys_info.mass = mass;
670 obj->mtype.phys_info.drag = drag;
671 }
672
673 if (size > 0)
674 obj->size = size;
675
676 obj->mtype.phys_info.velocity = *velocity;
677
678 if (phys_flags)
679 obj->mtype.phys_info.flags = phys_flags;
680
681 obj->ctype.laser_info.casts_light = false;
682 }
683
684 return objnum;
685 }
686
687 visnum = VisEffectCreate(type, id, roomnum, pos);
688
689 if (visnum < 0)
690 return -1; // No vis effects free
691
692 vis = &VisEffects[visnum];
693
694 vis->lifeleft = lifetime;
695 vis->lifetime = lifetime;
696 vis->movement_type = MT_PHYSICS;
697 vis->mass = mass;
698 vis->drag = drag;
699
700 if (size > 0)
701 vis->size = size;
702
703 vis->velocity = *velocity;
704
705 // float mag=vm_GetMagnitudeFast (&vis->velocity);
706 // mprintf(0,"CREATION:Velocity mag is %f\n",mag);
707
708 if (phys_flags)
709 vis->phys_flags = phys_flags;
710
711 if (ROOMNUM_OUTSIDE(roomnum))
712 vis->phys_flags |= PF_NO_COLLIDE;
713
714 vis->flags |= VF_NO_Z_ADJUST;
715 vis->lighting_color &= ~(0x8000);
716
717 return visnum;
718 }
719
720 // link the viseffect into the list for its room
721 // Does nothing for effects over terrain
722 void VisEffectLink(int visnum, int roomnum) {
723 vis_effect *vis = &VisEffects[visnum];
724
725 ASSERT(visnum != -1);
726
727 if (!ROOMNUM_OUTSIDE(vis->roomnum))
728 ASSERT(roomnum >= 0 && roomnum <= Highest_room_index);
729
730 vis->roomnum = roomnum;
731
732 if (ROOMNUM_OUTSIDE(vis->roomnum))
733 return;
734
735 vis->next = Rooms[roomnum].vis_effects;
736 Rooms[roomnum].vis_effects = visnum;
737 ASSERT(vis->next != visnum);
738
739 vis->prev = -1;
740
741 if (vis->next != -1)
742 VisEffects[vis->next].prev = visnum;
743 }
744
745 // Unlinks a viseffect from a room
746 // Does nothing for terrain
747 void VisEffectUnlink(int visnum) {
748 vis_effect *vis = &VisEffects[visnum];
749
750 ASSERT(visnum != -1);
751
752 if (ROOMNUM_OUTSIDE(vis->roomnum))
753 return;
754 else {
755
756 room *rp = &Rooms[vis->roomnum];
757
758 if (vis->prev == -1)
759 rp->vis_effects = vis->next;
760 else
761 VisEffects[vis->prev].next = vis->next;
762
763 if (vis->next != -1)
764 VisEffects[vis->next].prev = vis->prev;
765
766 vis->roomnum = -1;
767 }
768 }
769
770 // when an effect has moved into a new room, this function unlinks it
771 // from its old room and links it into the new room
772 void VisEffectRelink(int visnum, int newroomnum) {
773 ASSERT(visnum >= 0 && visnum < max_vis_effects);
774
775 VisEffectUnlink(visnum);
776 VisEffectLink(visnum, newroomnum);
777 }
778
779 // Kills all the effects that are dead
780 void VisEffectDeleteDead() {
781 int i;
782 for (i = 0; i < NumVisDead; i++) {
783 VisEffectDelete(VisDeadList[i]);
784 }
785
786 NumVisDead = 0;
787 }
788
789 // remove viseffect from the world
790 void VisEffectDelete(int visnum) {
791 vis_effect *vis = &VisEffects[visnum];
792
793 ASSERT(visnum != -1);
794 ASSERT(vis->type != VIS_NONE);
795
796 VisEffectUnlink(visnum);
797
798 vis->type = VIS_NONE; // unused!
799 vis->roomnum = -1; // zero it!
800
801 VisEffectFree(visnum);
802 }
803
804 // Frees all the objects that are currently in use
805 void FreeAllVisEffects() {
806
807 for (int i = 0; i < max_vis_effects; i++)
808 if (VisEffects[i].type != VIS_NONE)
809 VisEffectDelete(i);
810 }
811
812 // Creates a some sparks that go in random directions
813 void CreateRandomLineSparks(int num_sparks, vector *pos, int roomnum, uint16_t color, float force_scalar) {
814 // Make more sparks if Katmai
815 if (Katmai)
816 num_sparks *= 2;
817
818 // Create some sparks
819 for (int i = 0; i < num_sparks; i++) {
820 int visnum = VisEffectCreate(VIS_FIREBALL, FADING_LINE_INDEX, roomnum, pos);
821 if (visnum >= 0) {
822 vis_effect *vis = &VisEffects[visnum];
823
824 vis->movement_type = MT_PHYSICS;
825 vis->mass = 500;
826 vis->drag = .001f;
827 vis->phys_flags |= PF_GRAVITY | PF_NO_COLLIDE;
828
829 vis->velocity.x = (ps_rand() % 100) - 50;
830 vis->velocity.y = (ps_rand() % 100);
831 vis->velocity.z = (ps_rand() % 100) - 50;
832
833 vm_NormalizeVectorFast(&vis->velocity);
834
835 vis->velocity *= 20 + (ps_rand() % 10);
836 vis->velocity *= force_scalar;
837 vis->size = .7 + ((ps_rand() % 10) * .04);
838 vis->flags |= VF_USES_LIFELEFT;
839 float lifetime = 1 + ((ps_rand() % 10) * .15);
840 vis->lifeleft = lifetime;
841 vis->lifetime = lifetime;
842
843 if (color == 0)
844 vis->lighting_color = GR_RGB16(200 + (ps_rand() % 50), 150 + (ps_rand() % 50), ps_rand() % 50);
845 else
846 vis->lighting_color = color;
847 }
848 }
849 }
850
851 // Creates a some sparks that go in random directions
852 void CreateRandomSparks(int num_sparks, vector *pos, int roomnum, int which_index, float force_scalar) {
853 // Make more sparks if Katmai
854 if (Katmai)
855 num_sparks *= 2;
856
857 // Create some sparks
858 for (int i = 0; i < num_sparks; i++) {
859 int sparknum;
860 int index;
861
862 if (ps_rand() % 2)
863 index = HOT_SPARK_INDEX;
864 else
865 index = COOL_SPARK_INDEX;
866
867 if (which_index != -1)
868 index = which_index;
869
870 sparknum = VisEffectCreate(VIS_FIREBALL, index, roomnum, pos);
871
872 if (sparknum >= 0) {
873 vis_effect *vis = &VisEffects[sparknum];
874
875 vis->movement_type = MT_PHYSICS;
876 vis->mass = 100;
877 vis->drag = .1f;
878
879 vis->phys_flags |= PF_GRAVITY | PF_NO_COLLIDE;
880
881 vis->velocity.x = (ps_rand() % 100) - 50;
882 vis->velocity.y = (ps_rand() % 100);
883 vis->velocity.z = (ps_rand() % 100) - 50;
884
885 vm_NormalizeVectorFast(&vis->velocity);
886 vis->velocity *= 10 + (ps_rand() % 10);
887 vis->velocity *= force_scalar;
888 vis->size = .2 + ((ps_rand() % 10) * .01);
889 vis->flags |= VF_USES_LIFELEFT;
890 float lifetime = 1 + ((ps_rand() % 10) * .15);
891 vis->lifeleft = lifetime;
892 vis->lifetime = lifetime;
893 }
894 }
895 }
896
897 // Creates a some particles that go in random directions
898 void CreateRandomParticles(int num_sparks, vector *pos, int roomnum, int bm_handle, float size, float life) {
899 // Create some sparks
900 float tenth_life = life / 10.0;
901 float tenth_size = size / 10.0;
902
903 for (int i = 0; i < num_sparks; i++) {
904 int sparknum;
905
906 sparknum = VisEffectCreate(VIS_FIREBALL, PARTICLE_INDEX, roomnum, pos);
907
908 if (sparknum >= 0) {
909 vis_effect *vis = &VisEffects[sparknum];
910
911 vis->movement_type = MT_PHYSICS;
912 vis->mass = 100;
913 vis->drag = .1f;
914
915 vis->phys_flags |= PF_GRAVITY | PF_NO_COLLIDE;
916
917 vis->velocity.x = (ps_rand() % 100) - 50;
918 vis->velocity.y = (ps_rand() % 100);
919 vis->velocity.z = (ps_rand() % 100) - 50;
920
921 vm_NormalizeVectorFast(&vis->velocity);
922 vis->velocity *= 10 + (ps_rand() % 10);
923 vis->size = size + (((ps_rand() % 11) - 5) * tenth_size);
924 vis->flags |= VF_USES_LIFELEFT;
925 float lifetime = life + (((ps_rand() % 11) - 5) * tenth_life);
926 vis->lifeleft = lifetime;
927 vis->lifetime = lifetime;
928 vis->custom_handle = bm_handle;
929 }
930 }
931 }
932
933 // Draws a weapons fading line
934 void DrawVisFadingLine(vis_effect *vis) {
935 float norm_time;
936 float time_live = Gametime - vis->creation_time;
937 float size = vis->size;
938
939 int visnum = vis - VisEffects;
940 norm_time = time_live / vis->lifetime;
941
942 if (norm_time >= 1)
943 norm_time = .99999f; // don't go over!
944
945 rend_SetAlphaType(AT_SATURATE_VERTEX);
946 rend_SetTextureType(TT_FLAT);
947 rend_SetLighting(LS_GOURAUD);
948 rend_SetColorModel(CM_RGB);
949 rend_SetOverlayType(OT_NONE);
950
951 vector vecs[2];
952 g3Point pnts[2];
953 int i;
954
955 vecs[0] = vis->pos;
956 vecs[1] = vis->end_pos;
957
958 if (!(vis->flags & VF_WINDSHIELD_EFFECT)) // bash end position
959 {
960 vector vel = -vis->velocity;
961 vm_NormalizeVectorFast(&vel);
962 vecs[1] = vis->pos + (vel * vis->size);
963 }
964
965 ddgr_color color = GR_16_TO_COLOR(vis->lighting_color);
966 int r = GR_COLOR_RED(color);
967 int g = GR_COLOR_GREEN(color);
968 int b = GR_COLOR_BLUE(color);
969
970 for (i = 0; i < 2; i++) {
971 g3_RotatePoint(&pnts[i], &vecs[i]);
972 pnts[i].p3_flags |= PF_RGBA;
973 pnts[i].p3_r = (r / 255.0);
974 pnts[i].p3_g = (g / 255.0);
975 ;
976 pnts[i].p3_b = (b / 255.0);
977 ;
978 }
979
980 if (vis->flags & VF_WINDSHIELD_EFFECT)
981 pnts[0].p3_a = .3f;
982 else
983 pnts[0].p3_a = 1.0 - norm_time;
984
985 pnts[1].p3_a = 0.0;
986
987 g3_DrawSpecialLine(&pnts[0], &pnts[1]);
988 }
989
990 // Draws a blast ring vis effect
991 void DrawVisBlastRing(vis_effect *vis) {
992 vector inner_vecs[30], outer_vecs[30];
993 g3Point inner_points[30], outer_points[30];
994 float lifenorm = (vis->lifetime - vis->lifeleft) / vis->lifetime;
995 float cur_size = lifenorm * vis->size;
996 int i;
997 g3Point *pntlist[4];
998 matrix orient;
999 vector fvec;
1000
1001 if (vis->flags & VF_REVERSE) {
1002 lifenorm = 1 - lifenorm;
1003 cur_size = lifenorm * vis->size;
1004 }
1005
1006 if (vis->flags & VF_PLANAR)
1007 fvec = vis->end_pos;
1008 else {
1009 fvec = Viewer_object->pos - vis->pos;
1010 vm_NormalizeVectorFast(&fvec);
1011 }
1012
1013 if (vm_GetMagnitudeFast(&fvec) < .5)
1014 return;
1015 vm_VectorToMatrix(&orient, &fvec, NULL, NULL);
1016
1017 int num_segments = 20;
1018
1019 int ring_increment = 65536 / num_segments;
1020 int ring_angle = 0;
1021
1022 if (lifenorm > 1.0)
1023 lifenorm = 1.0;
1024
1025 rend_SetAlphaType(AT_SATURATE_TEXTURE_VERTEX);
1026 rend_SetOverlayType(OT_NONE);
1027 rend_SetTextureType(TT_LINEAR);
1028 rend_SetLighting(LS_NONE);
1029 rend_SetZBias(-1.0);
1030 rend_SetZBufferWriteMask(0);
1031
1032 ring_angle = 0;
1033
1034 for (i = 0; i < num_segments; i++, ring_angle += ring_increment) {
1035 float ring_sin = FixSin(ring_angle);
1036 float ring_cos = FixCos(ring_angle);
1037
1038 inner_vecs[i] = orient.rvec * (ring_cos * (cur_size / 2));
1039 inner_vecs[i] += orient.uvec * (ring_sin * (cur_size / 2));
1040 inner_vecs[i] += vis->pos;
1041
1042 outer_vecs[i] = orient.rvec * (ring_cos * cur_size);
1043 outer_vecs[i] += orient.uvec * (ring_sin * cur_size);
1044 outer_vecs[i] += vis->pos;
1045
1046 g3_RotatePoint(&inner_points[i], &inner_vecs[i]);
1047 g3_RotatePoint(&outer_points[i], &outer_vecs[i]);
1048 outer_points[i].p3_flags |= PF_UV | PF_RGBA;
1049 inner_points[i].p3_flags |= PF_UV | PF_RGBA;
1050
1051 outer_points[i].p3_a = (1.0 - lifenorm);
1052 inner_points[i].p3_a = 0;
1053 inner_points[i].p3_l = 1;
1054 inner_points[i].p3_l = 1;
1055 }
1056
1057 for (i = 0; i < num_segments; i++) {
1058 int next = (i + 1) % num_segments;
1059
1060 outer_points[i].p3_u = 0 + lifenorm;
1061 outer_points[next].p3_u = 1.0 + lifenorm;
1062 outer_points[i].p3_v = 0;
1063 outer_points[next].p3_v = 0;
1064
1065 inner_points[i].p3_u = 0 + lifenorm;
1066 inner_points[next].p3_u = 1.0 + lifenorm;
1067 inner_points[i].p3_v = 1;
1068 inner_points[next].p3_v = 1;
1069
1070 pntlist[0] = &outer_points[i];
1071 pntlist[1] = &outer_points[next];
1072 pntlist[2] = &inner_points[next];
1073 pntlist[3] = &inner_points[i];
1074
1075 g3_DrawPoly(4, pntlist, vis->custom_handle);
1076 }
1077
1078 rend_SetZBufferWriteMask(1);
1079 rend_SetZBias(0);
1080 }
1081
1082 // Draws a raindrop on the players windshield
1083 void DrawVisRainDrop(vis_effect *vis) {
1084 float norm_time;
1085 float time_live = Gametime - vis->creation_time;
1086 float size = vis->size;
1087
1088 int visnum = vis - VisEffects;
1089 int bm_handle;
1090 fireball *fb = &Fireballs[vis->id];
1091
1092 norm_time = time_live / vis->lifetime;
1093
1094 if (norm_time >= 1)
1095 norm_time = .99999f; // don't go over!
1096
1097 size *= (1 - (norm_time / 2));
1098
1099 bm_handle = fb->bm_handle;
1100
1101 float val;
1102 if (norm_time > .5)
1103 val = 1.0 - ((norm_time - .5) / .5);
1104 else if (norm_time < .1)
1105 val = norm_time / .1;
1106 else
1107 val = 1.0;
1108
1109 rend_SetAlphaValue(val * .4 * 255);
1110 rend_SetOverlayType(OT_NONE);
1111 rend_SetWrapType(WT_CLAMP);
1112 rend_SetLighting(LS_NONE);
1113
1114 // Set position
1115 vector pos;
1116
1117 if (vis->id == RAINDROP_INDEX) {
1118 rend_SetZBufferState(0);
1119 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1120 rend_SetAlphaValue(val * .4 * 255);
1121
1122 pos = Viewer_object->pos;
1123 pos += Viewer_object->orient.rvec * vis->pos.x;
1124 pos += Viewer_object->orient.uvec * vis->pos.y;
1125 pos += Viewer_object->orient.fvec * vis->pos.z;
1126 g3_DrawRotatedBitmap(&pos, 0, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle);
1127 rend_SetZBufferState(1);
1128 } else {
1129 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1130 rend_SetAlphaValue(val * .2 * 255);
1131 rend_SetZBufferWriteMask(0);
1132 pos = vis->pos;
1133 ASSERT(!((vis->end_pos.x == 0.0) && (vis->end_pos.y == 0.0) && (vis->end_pos.z == 0.0)));
1134 g3_DrawPlanarRotatedBitmap(&pos, &vis->end_pos, 0, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0),
1135 bm_handle);
1136 rend_SetZBufferWriteMask(1);
1137 }
1138
1139 rend_SetWrapType(WT_WRAP);
1140 }
1141
1142 // Draws a snowflake
1143 void DrawVisSnowflake(vis_effect *vis) {
1144 float norm_time;
1145 float time_live = Gametime - vis->creation_time;
1146 float size = vis->size;
1147
1148 int visnum = vis - VisEffects;
1149 int bm_handle;
1150 fireball *fb = &Fireballs[vis->id];
1151
1152 norm_time = time_live / vis->lifetime;
1153
1154 if (norm_time >= 1)
1155 norm_time = .99999f; // don't go over!
1156
1157 // size*=(1-(norm_time/2));
1158
1159 bm_handle = fb->bm_handle;
1160
1161 float val;
1162
1163 val = 1.0 - (norm_time);
1164
1165 rend_SetAlphaValue(val * .6 * 255);
1166 rend_SetOverlayType(OT_NONE);
1167 // rend_SetWrapType (WT_CLAMP);
1168 rend_SetLighting(LS_NONE);
1169 // rend_SetZBufferState (0);
1170 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1171
1172 ddgr_color color = GR_16_TO_COLOR(vis->lighting_color);
1173 g3_DrawBitmap(&vis->pos, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle, color);
1174
1175 // rend_SetZBufferState (1);
1176 // rend_SetWrapType (WT_WRAP);
1177 }
1178
1179 // Draws a lighting bolt from one area to another
1180 // Velocity.x is how much randomness goes into drawing
1181 // Velocity.y is the scalar that effects how many segments to draw
1182 void DrawVisLightningBolt(vis_effect *vis) {
1183 vector line_norm;
1184 float line_mag;
1185 int num_segs;
1186 float lightning_mag;
1187
1188 g3Point pnt1, pnt2;
1189
1190 line_norm = vis->pos - vis->end_pos;
1191 line_mag = vm_GetMagnitudeFast(&line_norm);
1192
1193 line_norm /= line_mag;
1194
1195 if (line_mag < 1)
1196 return;
1197
1198 float alpha_norm;
1199
1200 if (vis->flags & VF_EXPAND) {
1201 num_segs = line_mag * vis->velocity.y;
1202 lightning_mag = vis->velocity.x;
1203
1204 alpha_norm = vis->lifeleft / vis->lifetime;
1205 } else {
1206 num_segs = line_mag * vis->velocity.y;
1207 lightning_mag = vis->velocity.x;
1208
1209 alpha_norm = .7f;
1210
1211 // Make it powerup up based on distance
1212 if (line_mag > 30 && (vis->flags & VF_NO_Z_ADJUST)) {
1213 float scalar = 1.0 - ((line_mag - 30) / 150.0);
1214 if (scalar < 0)
1215 return;
1216
1217 alpha_norm *= scalar;
1218 }
1219 }
1220
1221 if (num_segs < 2)
1222 return;
1223
1224 // Set some states
1225
1226 rend_SetTextureType(TT_FLAT);
1227 rend_SetAlphaType(AT_SATURATE_VERTEX);
1228 rend_SetLighting(LS_NONE);
1229 rend_SetZBufferWriteMask(0);
1230
1231 if (vis->id == GRAY_LIGHTNING_BOLT_INDEX) {
1232 // get the color from the struct
1233 rend_SetFlatColor(GR_16_TO_COLOR(vis->lighting_color));
1234 } else {
1235 rend_SetFlatColor(GR_RGB(10, 60, 200));
1236 }
1237
1238 pnt1.p3_a = alpha_norm;
1239 pnt2.p3_a = alpha_norm;
1240
1241 vector vecs[50];
1242
1243 num_segs = std::min(num_segs, 50);
1244
1245 CreateLightningRodPositions(&vis->pos, &vis->end_pos, vecs, num_segs, lightning_mag, false);
1246
1247 for (int i = 0; i < num_segs - 1; i++) {
1248 g3_RotatePoint(&pnt1, &vecs[i]);
1249 g3_RotatePoint(&pnt2, &vecs[i + 1]);
1250
1251 pnt1.p3_flags |= PF_RGBA;
1252 pnt2.p3_flags |= PF_RGBA;
1253
1254 g3_DrawSpecialLine(&pnt1, &pnt2);
1255 }
1256
1257 rend_SetZBufferWriteMask(1);
1258 }
1259
1260 // Draws a lighting bolt sine wave from one area to another
1261 // Velocity.x represents how many increments to take (a scalar)
1262 // Velocity.y represents how "wide" the arcs are from the center of the line
1263 void DrawVisSineWave(vis_effect *vis) {
1264 vector line_norm;
1265 float line_mag;
1266 matrix mat;
1267 int num_segs;
1268
1269 vector from, to, base_from, rvec, uvec;
1270 g3Point pnt1, pnt2;
1271
1272 line_norm = vis->pos - vis->end_pos;
1273 line_mag = vm_GetMagnitudeFast(&line_norm);
1274 if (line_mag < .1)
1275 return;
1276
1277 line_norm /= line_mag;
1278
1279 float alpha_norm;
1280
1281 if (vis->flags & VF_EXPAND)
1282 alpha_norm = vis->lifeleft / vis->lifetime;
1283
1284 num_segs = vis->velocity.x * line_mag; // /2
1285 line_norm /= vis->velocity.x; // *2
1286
1287 alpha_norm = vis->lifeleft / vis->lifetime;
1288
1289 vm_VectorToMatrix(&mat, &line_norm, &vis->velocity, NULL);
1290 rvec = mat.rvec * vis->velocity.y; // /4
1291 uvec = mat.uvec * vis->velocity.y; // /4
1292
1293 // Set some states
1294
1295 rend_SetTextureType(TT_FLAT);
1296 rend_SetAlphaType(AT_SATURATE_VERTEX);
1297 rend_SetLighting(LS_NONE);
1298 rend_SetZBufferWriteMask(0);
1299 rend_SetFlatColor(GR_RGB(10, 60, 200));
1300 int cur_sin = (vis - VisEffects) * 5000;
1301
1302 cur_sin += (FrameCount * 2000);
1303
1304 base_from = vis->end_pos;
1305 from = base_from;
1306
1307 pnt1.p3_a = alpha_norm;
1308 pnt2.p3_a = alpha_norm;
1309
1310 for (int i = 0; i < num_segs; i++, base_from += line_norm) {
1311 to = base_from + line_norm + (FixSin((cur_sin) % 65536) * uvec);
1312 to += (FixCos((cur_sin) % 65536) * rvec);
1313
1314 g3_RotatePoint(&pnt1, &from);
1315 g3_RotatePoint(&pnt2, &to);
1316
1317 pnt1.p3_flags |= PF_RGBA;
1318 pnt2.p3_flags |= PF_RGBA;
1319
1320 g3_DrawSpecialLine(&pnt1, &pnt2);
1321
1322 from = to;
1323
1324 cur_sin += 4000;
1325 }
1326
1327 rend_SetZBufferWriteMask(1);
1328 }
1329
1330 // Calculates the corners for a billboard
1331 // Returns 0 if off screen, or 1 if we should draw
1332 int GetBillboardCorners(g3Point *pnts, g3Point *top_point, g3Point *bot_point, float width) {
1333 // get the camera's world position
1334 vector viewerPos;
1335 g3_GetViewPosition(&viewerPos);
1336
1337 // calculate the vector from the top point to the bottom point
1338 ASSERT(bot_point->p3_flags & PF_ORIGPOINT);
1339 ASSERT(top_point->p3_flags & PF_ORIGPOINT);
1340 vector deltaVec = bot_point->p3_vecPreRot - top_point->p3_vecPreRot;
1341 vm_NormalizeVector(&deltaVec);
1342
1343 // get the vector from the camera to the top point
1344 vector top = top_point->p3_vecPreRot - viewerPos;
1345 vm_NormalizeVector(&top);
1346
1347 // calculate the vector out from the 'rod' that is facing the camera
1348 vector rodNorm;
1349 vm_CrossProduct(&rodNorm, &deltaVec, &top);
1350 vm_NormalizeVector(&rodNorm);
1351
1352 // get the offset vector
1353 vector tempv = rodNorm * width;
1354
1355 // setup the points
1356 int i, codesAND = 0xFF;
1357 for (i = 0; i < 4; ++i) {
1358 float scale = (i == 0 || i == 3) ? -1.0f : 1.0f;
1359 vector bbPt = ((i < 2) ? top_point->p3_vecPreRot : bot_point->p3_vecPreRot) + (tempv * scale);
1360
1361 // initialize the point
1362 codesAND &= g3_RotatePoint(&pnts[i], &bbPt);
1363 }
1364
1365 if (codesAND)
1366 return 0;
1367
1368 return 1;
1369 }
1370
1371 // Draws a thick lighting bolt from one area to another
1372 // Velocity.x is how many times to saturate
1373 // Velocity.y is how fast the lightning moves across the v component
1374 // Velocity.z is how many times the effect is tiled across the v component
1375 // If Billboard_info.texture is not zero, it does autotiling
1376 void DrawVisThickLightning(vis_effect *vis) {
1377 // compute the corners of a rod. fills in vertbuf.
1378 g3Point top_point, bot_point;
1379 g3Point pnts[4], *pntlist[4];
1380
1381 g3_RotatePoint(&top_point, &vis->pos);
1382 g3_RotatePoint(&bot_point, &vis->end_pos);
1383
1384 if (!GetBillboardCorners(pnts, &top_point, &bot_point, vis->billboard_info.width))
1385 return;
1386
1387 vector line_norm = vis->end_pos - vis->pos;
1388 float line_mag = vm_GetMagnitudeFast(&line_norm);
1389
1390 line_norm /= line_mag;
1391
1392 if (line_mag < 1)
1393 return;
1394
1395 int texnum = vis->custom_handle;
1396 int bm_handle = GetTextureBitmap(texnum, 0, true);
1397 float tile_factor = vis->velocity.z;
1398 float alpha_norm;
1399 int i, codes_and;
1400
1401 if (vis->flags & VF_EXPAND)
1402 alpha_norm = vis->lifeleft / vis->lifetime;
1403 else
1404 alpha_norm = .7f;
1405
1406 rend_SetOverlayType(OT_NONE);
1407 rend_SetTextureType(TT_LINEAR);
1408 rend_SetLighting(LS_FLAT_GOURAUD);
1409 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1410 rend_SetWrapType(WT_WRAP);
1411 rend_SetFlatColor(GR_16_TO_COLOR(vis->lighting_color));
1412 rend_SetAlphaValue(255 * alpha_norm);
1413 rend_SetZBufferWriteMask(0);
1414
1415 for (i = 0, codes_and = 0xff; i < 4; i++) {
1416 pntlist[i] = &pnts[i];
1417 }
1418
1419 if (vis->billboard_info.texture) // do autotiling
1420 {
1421 float ratio = line_mag / (float)vis->billboard_info.width;
1422 tile_factor *= ratio;
1423 }
1424
1425 pnts[0].p3_u = 0;
1426 pnts[0].p3_v = 0;
1427
1428 pnts[1].p3_u = 1;
1429 pnts[1].p3_v = 0;
1430
1431 pnts[2].p3_u = 1;
1432 pnts[2].p3_v = tile_factor;
1433
1434 pnts[3].p3_u = 0;
1435 pnts[3].p3_v = tile_factor;
1436
1437 float vchange = 0;
1438
1439 if (vis->velocity.y != 0) {
1440 int int_time = Gametime / vis->velocity.y;
1441 float norm_time = Gametime - (int_time * vis->velocity.y);
1442 norm_time /= vis->velocity.y;
1443
1444 vchange = norm_time;
1445 }
1446
1447 for (i = 0; i < 4; i++) {
1448 pnts[i].p3_v += vchange;
1449 pnts[i].p3_flags |= PF_UV;
1450 }
1451
1452 // Now draw this thing!
1453 for (i = 0; i < vis->velocity.x; i++) {
1454 g3_DrawPoly(4, pntlist, bm_handle);
1455 }
1456
1457 rend_SetZBufferWriteMask(1);
1458 }
1459
1460 // Draws a bitmap that can orient around an axis
1461 void DrawVisAxisBillboard(vis_effect *vis) {
1462 float norm_time, alpha_norm = 1;
1463 float uchange = 0, vchange = 0;
1464 float time_live = Gametime - vis->creation_time;
1465 float size = vis->size;
1466
1467 int visnum = vis - VisEffects;
1468 int bm_handle;
1469 int i;
1470
1471 // Get corners of this billboard
1472 g3Point top_point, bot_point;
1473 g3Point pnts[4], *pntlist[4];
1474
1475 g3_RotatePoint(&top_point, &vis->pos);
1476 g3_RotatePoint(&bot_point, &vis->end_pos);
1477
1478 if (!GetBillboardCorners(pnts, &top_point, &bot_point, vis->billboard_info.width))
1479 return;
1480
1481 for (i = 0; i < 4; i++) {
1482 pnts[i].p3_flags |= PF_UV;
1483 pntlist[i] = &pnts[i];
1484 }
1485
1486 fireball *fb = &Fireballs[vis->id];
1487
1488 norm_time = time_live / vis->lifetime;
1489
1490 if (vis->flags & VF_EXPAND)
1491 alpha_norm = vis->lifeleft / vis->lifetime;
1492
1493 if (norm_time >= 1)
1494 norm_time = 0.99999f; // don't go over!
1495
1496 if (vis->billboard_info.texture) // If its a texture, get image from texture
1497 {
1498 int texnum = vis->custom_handle;
1499 if (GameTextures[texnum].flags & TF_ANIMATED) {
1500 vclip *vc = &GameVClips[GameTextures[texnum].bm_handle];
1501 int int_frame = vc->num_frames * norm_time;
1502 bm_handle = vc->frames[int_frame];
1503 } else
1504 bm_handle = GetTextureBitmap(texnum, 0);
1505
1506 if (GameTextures[texnum].flags & TF_SATURATE)
1507 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1508 else
1509 rend_SetAlphaType(ATF_CONSTANT + ATF_TEXTURE);
1510
1511 rend_SetAlphaValue(alpha_norm * GameTextures[texnum].alpha * 255);
1512
1513 if (GameTextures[texnum].slide_u != 0) {
1514 int int_time = Gametime / GameTextures[texnum].slide_u;
1515 float norm_time = Gametime - (int_time * GameTextures[texnum].slide_u);
1516 norm_time /= GameTextures[texnum].slide_u;
1517
1518 uchange = norm_time;
1519 }
1520
1521 if (GameTextures[texnum].slide_v != 0) {
1522 int int_time = Gametime / GameTextures[texnum].slide_v;
1523 float norm_time = Gametime - (int_time * GameTextures[texnum].slide_v);
1524 norm_time /= GameTextures[texnum].slide_v;
1525 vchange = norm_time;
1526 }
1527 } else {
1528 bm_handle = vis->custom_handle;
1529 rend_SetAlphaType(ATF_CONSTANT + ATF_TEXTURE);
1530 rend_SetAlphaValue(alpha_norm * 255);
1531 }
1532
1533 rend_SetOverlayType(OT_NONE);
1534
1535 rend_SetZBufferWriteMask(0);
1536
1537 if (uchange == 0 && vchange == 0)
1538 rend_SetWrapType(WT_CLAMP);
1539 else
1540 rend_SetWrapType(WT_WRAP);
1541
1542 rend_SetLighting(LS_NONE);
1543 rend_SetColorModel(CM_MONO);
1544 rend_SetTextureType(TT_LINEAR);
1545
1546 pnts[0].p3_u = 0;
1547 pnts[0].p3_v = 0;
1548
1549 pnts[1].p3_u = 1;
1550 pnts[1].p3_v = 0;
1551
1552 pnts[2].p3_u = 1;
1553 pnts[2].p3_v = 1;
1554
1555 pnts[3].p3_u = 0;
1556 pnts[3].p3_v = 1;
1557
1558 for (i = 0; i < 4; i++) {
1559 pnts[i].p3_u += uchange;
1560 pnts[i].p3_v += vchange;
1561 }
1562
1563 g3_DrawPoly(4, pntlist, bm_handle);
1564
1565 rend_SetZBufferWriteMask(1);
1566
1567 rend_SetWrapType(WT_WRAP);
1568 }
1569
1570 // Draws a bitmap that can orient around an axis
1571 void DrawVisBillboardSmoketrail(vis_effect *vis) {
1572 float norm_time, alpha_norm = 1;
1573 float uchange = 0, vchange = 0;
1574 float time_live = Gametime - vis->creation_time;
1575 float size = vis->size;
1576
1577 int visnum = vis - VisEffects;
1578 int bm_handle;
1579
1580 // Get corners of this billboard
1581 g3Point top_point, bot_point;
1582 g3Point pnts[4], *pntlist[4];
1583
1584 g3_RotatePoint(&top_point, &vis->pos);
1585 g3_RotatePoint(&bot_point, &vis->end_pos);
1586
1587 norm_time = time_live / vis->lifetime;
1588 alpha_norm = vis->lifeleft / vis->lifetime;
1589 float width = vis->billboard_info.width * alpha_norm;
1590
1591 if (alpha_norm > 0.8f) {
1592 // Make this smoke puff a bit smaller at the beginning of its life
1593 float newnorm = 1.0f - ((alpha_norm - 0.8f) / 0.2f);
1594 width *= newnorm;
1595 }
1596
1597 alpha_norm *= 0.7f;
1598
1599 if (!GetBillboardCorners(pnts, &top_point, &bot_point, width))
1600 return;
1601
1602 fireball *fb = &Fireballs[vis->id];
1603
1604 for (int i = 0; i < 4; i++) {
1605 pnts[i].p3_flags |= PF_UV | PF_RGBA;
1606 pntlist[i] = &pnts[i];
1607 pnts[i].p3_a = alpha_norm;
1608 }
1609
1610 if (norm_time >= 1)
1611 norm_time = 0.99999f; // don't go over!
1612
1613 int texnum = vis->custom_handle;
1614 if (GameTextures[texnum].flags & TF_ANIMATED) {
1615 vclip *vc = &GameVClips[GameTextures[texnum].bm_handle];
1616 int int_frame = vc->num_frames * norm_time;
1617 bm_handle = vc->frames[int_frame];
1618 } else
1619 bm_handle = GetTextureBitmap(texnum, 0);
1620
1621 if (GameTextures[texnum].flags & TF_SATURATE)
1622 rend_SetAlphaType(AT_SATURATE_TEXTURE_VERTEX);
1623 else
1624 rend_SetAlphaType(AT_TEXTURE_VERTEX);
1625
1626 rend_SetOverlayType(OT_NONE);
1627
1628 rend_SetZBufferWriteMask(0);
1629
1630 rend_SetFlatColor(GR_16_TO_COLOR(vis->lighting_color));
1631 rend_SetColorModel(CM_MONO);
1632 rend_SetTextureType(TT_LINEAR);
1633 rend_SetLighting(LS_FLAT_GOURAUD);
1634
1635 pnts[0].p3_u = 0;
1636 pnts[0].p3_v = 0;
1637
1638 pnts[1].p3_u = 1;
1639 pnts[1].p3_v = 0;
1640
1641 pnts[2].p3_u = 1;
1642 pnts[2].p3_v = 1;
1643
1644 pnts[3].p3_u = 0;
1645 pnts[3].p3_v = 1;
1646
1647 g3_DrawPoly(4, pntlist, bm_handle);
1648
1649 rend_SetZBufferWriteMask(1);
1650 }
1651
1652 // Draws a long "stick" to represent the mass driver trail
1653 void DrawVisMassDriverEffect(vis_effect *vis, bool f_boss) {
1654 int i, t, k;
1655 static int masstrail = -1;
1656
1657 if (masstrail == -1)
1658 masstrail = FindTextureName("MassTrail");
1659 ASSERT(masstrail != -1); // DAJ -1FIX
1660
1661 vector center_vecs[2];
1662 g3Point arc_points[32], *pntlist[32];
1663
1664 center_vecs[0] = vis->pos;
1665 center_vecs[1] = vis->end_pos;
1666
1667 vector normvec = center_vecs[1] - center_vecs[0];
1668 matrix orient;
1669
1670 vm_NormalizeVector(&normvec);
1671 vm_VectorToMatrix(&orient, &normvec, NULL, NULL);
1672
1673 float size = ((float)vis->billboard_info.width) * .25;
1674 int circle_pieces = 16;
1675
1676 if (f_boss) {
1677 size *= 3.0f;
1678 }
1679
1680 float mag = vm_VectorDistanceQuick(&vis->pos, &vis->end_pos);
1681 vector dir_norm = (vis->end_pos - vis->pos) / mag;
1682 float alpha_norm = (vis->lifeleft / vis->lifetime) * .5;
1683 int bm_handle = GetTextureBitmap(vis->custom_handle, 0);
1684
1685 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1686 rend_SetOverlayType(OT_NONE);
1687 rend_SetTextureType(TT_LINEAR);
1688 rend_SetLighting(LS_FLAT_GOURAUD);
1689 rend_SetZBufferWriteMask(0);
1690 if (f_boss)
1691 rend_SetFlatColor(GR_RGB(255, 170, 170));
1692 else
1693 rend_SetFlatColor(GR_RGB(200, 200, 255));
1694
1695 rend_SetAlphaValue(255 * alpha_norm);
1696
1697 for (i = 0; i < 2; i++) {
1698 for (t = 0; t < circle_pieces; t++) {
1699 float norm = (float)t / (float)circle_pieces;
1700 vector arc_vec = center_vecs[i];
1701
1702 arc_vec += (orient.uvec * (size / 4) * FixSin(norm * 65536));
1703 arc_vec -= (orient.rvec * (size / 4) * FixCos(norm * 65536));
1704
1705 g3_RotatePoint(&arc_points[i * circle_pieces + t], &arc_vec);
1706 arc_points[i * circle_pieces + t].p3_flags |= PF_UV | PF_RGBA;
1707 }
1708 }
1709
1710 for (k = 0; k < circle_pieces; k++) {
1711 int next = (k + 1) % circle_pieces;
1712 arc_points[k].p3_u = 0;
1713 arc_points[k].p3_v = mag / 2;
1714
1715 arc_points[k + circle_pieces].p3_u = 0;
1716 arc_points[k + circle_pieces].p3_v = 0;
1717
1718 arc_points[next + circle_pieces].p3_u = 1;
1719 arc_points[next + circle_pieces].p3_v = 0;
1720
1721 arc_points[next].p3_u = 1;
1722 arc_points[next].p3_v = mag / 2;
1723
1724 pntlist[0] = &arc_points[t + k];
1725 pntlist[1] = &arc_points[t + k - circle_pieces];
1726 pntlist[2] = &arc_points[t + next - circle_pieces];
1727 pntlist[3] = &arc_points[t + next];
1728
1729 g3_DrawPoly(4, pntlist, bm_handle);
1730 }
1731
1732 for (i = 0; i < 2; i++) {
1733 for (t = 0; t < circle_pieces; t++) {
1734 float norm = (float)t / (float)circle_pieces;
1735 vector arc_vec = center_vecs[i];
1736
1737 arc_vec += (orient.uvec * (size)*FixSin(norm * 65536));
1738 arc_vec -= (orient.rvec * (size)*FixCos(norm * 65536));
1739
1740 g3_RotatePoint(&arc_points[i * circle_pieces + t], &arc_vec);
1741 arc_points[i * circle_pieces + t].p3_flags |= PF_UV;
1742 }
1743 }
1744
1745 rend_SetFlatColor(GR_16_TO_COLOR(vis->lighting_color));
1746 rend_SetAlphaValue(255 * alpha_norm);
1747
1748 for (k = 0; k < circle_pieces; k++) {
1749 int next = (k + 1) % circle_pieces;
1750 arc_points[k].p3_u = 0;
1751 arc_points[k].p3_v = mag / 2;
1752
1753 arc_points[k + circle_pieces].p3_u = 0;
1754 arc_points[k + circle_pieces].p3_v = 0;
1755
1756 arc_points[next + circle_pieces].p3_u = 1;
1757 arc_points[next + circle_pieces].p3_v = 0;
1758
1759 arc_points[next].p3_u = 1;
1760 arc_points[next].p3_v = mag / 2;
1761
1762 pntlist[0] = &arc_points[t + k];
1763 pntlist[1] = &arc_points[t + k - circle_pieces];
1764 pntlist[2] = &arc_points[t + next - circle_pieces];
1765 pntlist[3] = &arc_points[t + next];
1766
1767 if (!g3_DrawPoly(4, pntlist, bm_handle))
1768 return;
1769 }
1770
1771 // Now draw some rings
1772 int rings = mag / 3;
1773 float fsize = 3;
1774
1775 if (rings > 400) {
1776 rings = 400;
1777 fsize = mag / 400.0;
1778 }
1779
1780 rend_SetLighting(LS_NONE);
1781 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1782 rend_SetAlphaValue(255 * alpha_norm);
1783
1784 bm_handle = GetTextureBitmap(masstrail, 0);
1785
1786 int int_gametime = Gametime;
1787 int frameroll = (Gametime - int_gametime) * -(65536 * 4);
1788
1789 if (!f_boss) {
1790 for (i = 1; i < rings; i++) {
1791 int rot_angle = (i * 2000) % 65536;
1792 vector pos = vis->pos + (dir_norm * (i * fsize));
1793 float new_size = 1.0;
1794 new_size += FixSin((i * 9000) + frameroll) * .3;
1795
1796 g3_DrawPlanarRotatedBitmap(&pos, &dir_norm, rot_angle, new_size,
1797 (new_size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle);
1798 }
1799 }
1800 rend_SetZBufferWriteMask(1);
1801 }
1802
1803 // Renders a vis effect
1804 void DrawVisEffect(vis_effect *vis) {
1805 ASSERT(vis->type != VIS_NONE);
1806
1807 // First check to see if these are special types
1808
1809 if (vis->id == LIGHTNING_BOLT_INDEX || vis->id == GRAY_LIGHTNING_BOLT_INDEX) {
1810 DrawVisLightningBolt(vis);
1811 return;
1812 } else if (vis->id == MASSDRIVER_EFFECT_INDEX) {
1813 DrawVisMassDriverEffect(vis, false);
1814 return;
1815 } else if (vis->id == MERCBOSS_MASSDRIVER_EFFECT_INDEX) {
1816 DrawVisMassDriverEffect(vis, true);
1817 return;
1818 } else if (vis->id == BILLBOARD_SMOKETRAIL_INDEX) {
1819 DrawVisBillboardSmoketrail(vis);
1820 return;
1821 } else if (vis->id == THICK_LIGHTNING_INDEX) {
1822 DrawVisThickLightning(vis);
1823 return;
1824 } else if (vis->id == SINE_WAVE_INDEX) {
1825 DrawVisSineWave(vis);
1826 return;
1827 } else if (vis->id == BLAST_RING_INDEX) {
1828 DrawVisBlastRing(vis);
1829 return;
1830 } else if (vis->id == FADING_LINE_INDEX) {
1831 DrawVisFadingLine(vis);
1832 return;
1833 } else if (vis->id == SNOWFLAKE_INDEX) {
1834 DrawVisSnowflake(vis);
1835 return;
1836 } else if (vis->id == RAINDROP_INDEX || vis->id == PUDDLEDROP_INDEX) {
1837 DrawVisRainDrop(vis);
1838 return;
1839 } else if (vis->id == AXIS_BILLBOARD_INDEX) {
1840 DrawVisAxisBillboard(vis);
1841 return;
1842 }
1843
1844 float norm_time;
1845 float time_live = Gametime - vis->creation_time;
1846
1847 // This hack is needed for the demo system and Gamegauge, since it adjusts gametime during
1848 // the game there are times when this could be a negative number (which would be bad)
1849 if (time_live < 0)
1850 time_live = 0;
1851
1852 float size = vis->size;
1853
1854 // Bigger explosions for Katmai
1855 if (Katmai) {
1856 if (vis->id == BIG_EXPLOSION_INDEX || vis->id == MED_EXPLOSION_INDEX || vis->id == MED_EXPLOSION_INDEX2 ||
1857 vis->id == MED_EXPLOSION_INDEX3)
1858 size *= 1.8f;
1859 }
1860
1861 int visnum = vis - VisEffects;
1862 int rot_angle;
1863 int bm_handle;
1864
1865 fireball *fb = &Fireballs[vis->id];
1866
1867 if (fb->type == FT_BILLOW)
1868 rot_angle = ((visnum * 5000) + (FrameCount * 160)) % 65536;
1869 else if (vis->flags & VF_ATTACHED)
1870 rot_angle = 0;
1871 else if (vis->id == RUBBLE1_INDEX || vis->id == RUBBLE2_INDEX)
1872 rot_angle = ((visnum * 5000) + (FrameCount * 860)) % 65536;
1873 else if (vis->id == SUN_CORONA_INDEX) {
1874 rot_angle = ((visnum * 5000) + (FrameCount * 500)) % 65536;
1875 size *= 1.0 + ((rand() % 10) / 100);
1876 } else
1877 rot_angle = (visnum * 5000) % 65536;
1878
1879 norm_time = time_live / vis->lifetime;
1880
1881 // TEMP!!
1882 if (vis->flags & VF_ATTACHED) {
1883 int int_time_live = time_live;
1884 norm_time = time_live - int_time_live;
1885 }
1886
1887 if (norm_time >= 1)
1888 norm_time = .99999f; // don't go over!
1889
1890 if (vis->flags & VF_EXPAND) {
1891 size = (vis->size / 2) + ((vis->size * norm_time) / 2);
1892 }
1893
1894 if (vis->id == SMOKE_TRAIL_INDEX) // If its a smoke trail, get image from texture
1895 {
1896 int texnum = vis->custom_handle;
1897 if (GameTextures[texnum].flags & TF_ANIMATED) {
1898 vclip *vc = &GameVClips[GameTextures[texnum].bm_handle];
1899 int int_frame = vc->num_frames * norm_time;
1900 bm_handle = vc->frames[int_frame];
1901 } else
1902 bm_handle = GameTextures[texnum].bm_handle;
1903 } else if (vis->id == SPRAY_INDEX) {
1904 int vnum = vis->custom_handle;
1905 vclip *vc = &GameVClips[vnum];
1906 int int_frame = vc->num_frames * norm_time;
1907 bm_handle = vc->frames[int_frame];
1908
1909 // if (norm_time<.5)
1910 // size=1+((vis->size-1)*(norm_time));
1911 } else if (vis->id == CUSTOM_EXPLOSION_INDEX || vis->id == PARTICLE_INDEX) // Do custom
1912 {
1913 if ((GameTextures[vis->custom_handle].flags & TF_ANIMATED)) {
1914 int vnum = GameTextures[vis->custom_handle].bm_handle;
1915 vclip *vc = &GameVClips[vnum];
1916 int int_frame = vc->num_frames * norm_time;
1917 bm_handle = vc->frames[int_frame];
1918 } else
1919 bm_handle = GetTextureBitmap(vis->custom_handle, 0);
1920 } else if (fb->type == FT_SPARK) // Do spark
1921 {
1922 bm_handle = fb->bm_handle;
1923 size *= (1.0 - norm_time);
1924
1925 } else if (vis->id == SUN_CORONA_INDEX || vis->id == MUZZLE_FLASH_INDEX || vis->id == RUBBLE1_INDEX ||
1926 vis->id == RUBBLE2_INDEX) {
1927 bm_handle = fb->bm_handle;
1928 } else {
1929 vclip *vc = &GameVClips[fb->bm_handle];
1930 int int_frame = vc->num_frames * norm_time;
1931 bm_handle = vc->frames[int_frame];
1932 }
1933
1934 if (fb->type == FT_SMOKE) {
1935 if (norm_time > .3) {
1936 float temp_time = (norm_time - .3);
1937 temp_time /= .7f;
1938
1939 if (vis->flags & VF_REVERSE)
1940 size /= (1 + (temp_time * 2.3));
1941 else
1942 size *= (1 + (temp_time * 2.3));
1943 }
1944 }
1945
1946 // Set some alpha
1947 if (vis->id == SMOKE_TRAIL_INDEX || vis->id == CUSTOM_EXPLOSION_INDEX || vis->id == PARTICLE_INDEX) {
1948 if (GameTextures[vis->custom_handle].flags & TF_SATURATE)
1949 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1950 else
1951 rend_SetAlphaType(ATF_CONSTANT + ATF_TEXTURE);
1952 } else if (vis->id == BLACK_SMOKE_INDEX) {
1953 rend_SetAlphaType(AT_LIGHTMAP_BLEND);
1954 } else if ((fb->type == FT_SMOKE && vis->id != MED_SMOKE_INDEX) || vis->id == RUBBLE1_INDEX ||
1955 vis->id == RUBBLE2_INDEX) {
1956 rend_SetAlphaType(ATF_CONSTANT + ATF_TEXTURE);
1957 } else
1958 rend_SetAlphaType(AT_SATURATE_TEXTURE);
1959
1960 float val;
1961 if (norm_time > .5)
1962 val = 1.0 - ((norm_time - .5) / .5);
1963 else
1964 val = 1.0;
1965
1966 // Cap size
1967 if (size > MAX_FIREBALL_SIZE)
1968 size = MAX_FIREBALL_SIZE;
1969 if ((vis->id != BIG_EXPLOSION_INDEX && vis->id != BLUE_EXPLOSION_INDEX) && size > (MAX_FIREBALL_SIZE / 2))
1970 size = MAX_FIREBALL_SIZE / 2;
1971
1972 if (vis->id == SMOKE_TRAIL_INDEX || vis->id == CUSTOM_EXPLOSION_INDEX || vis->id == PARTICLE_INDEX)
1973 rend_SetAlphaValue(val * GameTextures[vis->custom_handle].alpha * 255);
1974 else if (fb->type == FT_SMOKE)
1975 rend_SetAlphaValue(val * SMOKE_ALPHA * 255);
1976 else if (vis->id == RUBBLE1_INDEX || vis->id == RUBBLE2_INDEX)
1977 rend_SetAlphaValue(255);
1978 else if (vis->id == MUZZLE_FLASH_INDEX)
1979 rend_SetAlphaValue(128);
1980 else if (vis->flags & VF_ATTACHED)
1981 rend_SetAlphaValue(FIREBALL_ALPHA * 255);
1982 else
1983 rend_SetAlphaValue(val * FIREBALL_ALPHA * 255);
1984
1985 rend_SetOverlayType(OT_NONE);
1986
1987 if (!(vis->flags & VF_NO_Z_ADJUST))
1988 rend_SetZBias(-size);
1989
1990 rend_SetZBufferWriteMask(0);
1991 rend_SetWrapType(WT_CLAMP);
1992 rend_SetLighting(LS_NONE);
1993
1994 // Draw!!
1995 if (vis->id == RUBBLE1_INDEX || vis->id == RUBBLE2_INDEX || vis->id == GRAY_SPARK_INDEX) {
1996 int color = GR_16_TO_COLOR(vis->lighting_color);
1997 g3_DrawRotatedBitmap(&vis->pos, rot_angle, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle,
1998 color);
1999 } else {
2000 if (vis->flags & VF_PLANAR)
2001 g3_DrawPlanarRotatedBitmap(&vis->pos, &vis->end_pos, rot_angle, size,
2002 (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle);
2003 else
2004 g3_DrawRotatedBitmap(&vis->pos, rot_angle, size, (size * bm_h(bm_handle, 0)) / bm_w(bm_handle, 0), bm_handle);
2005 }
2006
2007 rend_SetZBias(0.0f);
2008 rend_SetZBufferWriteMask(1);
2009
2010 rend_SetWrapType(WT_WRAP);
2011 }
2012
2013 void VisEffectSetDeadFlag(vis_effect *vis) {
2014 if (vis->flags & VF_DEAD)
2015 return;
2016 if (vis->type == VIS_NONE)
2017 return;
2018
2019 vis->flags |= VF_DEAD;
2020
2021 VisDeadList[NumVisDead++] = vis - VisEffects;
2022 }
2023
2024 // Moves
2025 void VisEffectMoveOne(vis_effect *vis) {
2026 if (vis->flags & VF_USES_LIFELEFT)
2027 vis->lifeleft -= Frametime; //...inevitable countdown towards death
2028
2029 // Chris, do your stuff here
2030 if (vis->movement_type == MT_PHYSICS)
2031 do_vis_physics_sim(vis);
2032
2033 if (vis->flags & VF_USES_LIFELEFT) {
2034 if (vis->lifeleft < 0)
2035 VisEffectSetDeadFlag(vis);
2036 }
2037
2038 if (vis->id == SNOWFLAKE_INDEX) {
2039 Weather.snowflakes_to_create++;
2040
2041 vis->pos += vis->velocity * Frametime;
2042
2043 if (vis->pos.y < 1)
2044 VisEffectSetDeadFlag(vis);
2045 }
2046
2047 // Do attached viseffect stuff here
2048 if (vis->flags & VF_ATTACHED) {
2049 int objnum = vis->attach_info.obj_handle & HANDLE_OBJNUM_MASK;
2050 uint32_t sig = vis->attach_info.obj_handle & HANDLE_COUNT_MASK;
2051 object *obj = &Objects[objnum];
2052
2053 if ((obj->flags & OF_DEAD) || (obj->handle & HANDLE_COUNT_MASK) != sig) {
2054 // The object we're attached to doesn't exist anymore
2055 VisEffectSetDeadFlag(vis);
2056
2057 } else if (obj->type == OBJ_PLAYER && (Players[obj->id].flags & (PLAYER_FLAGS_DYING | PLAYER_FLAGS_DEAD))) {
2058 // The object we're attached to doesn't exist anymore
2059 VisEffectSetDeadFlag(vis);
2060 } else {
2061 if (vis->id == THICK_LIGHTNING_INDEX) {
2062 if (vis->flags & VF_PLANAR) {
2063 // Do object to object attachment
2064 int dest_objnum = vis->attach_info.dest_objhandle & HANDLE_OBJNUM_MASK;
2065 uint32_t dest_sig = vis->attach_info.dest_objhandle & HANDLE_COUNT_MASK;
2066 object *dest_obj = &Objects[dest_objnum];
2067
2068 if ((dest_obj->flags & OF_DEAD) || (dest_obj->handle & HANDLE_COUNT_MASK) != dest_sig) {
2069 // The object we're attached to doesn't exist anymore
2070 VisEffectSetDeadFlag(vis);
2071 return;
2072 }
2073
2074 if (dest_obj->type == OBJ_PLAYER &&
2075 (Players[dest_obj->id].flags & (PLAYER_FLAGS_DYING | PLAYER_FLAGS_DEAD))) {
2076 VisEffectSetDeadFlag(vis);
2077 return;
2078 }
2079
2080 vis->pos = obj->pos;
2081 vis->end_pos = dest_obj->pos;
2082
2083 // If were are shooting to the exact center of the viewer object then move
2084 // the positions a little bit or it will look wrong:
2085 if (obj == Viewer_object) {
2086 vis->pos -= (obj->orient.uvec * .1f);
2087 }
2088
2089 if (dest_obj == Viewer_object) {
2090 vis->end_pos -= (dest_obj->orient.uvec * .1f);
2091 }
2092
2093 } else {
2094 if (obj->rtype.pobj_info.model_num != vis->attach_info.modelnum) {
2095 VisEffectSetDeadFlag(vis);
2096 } else {
2097 WeaponCalcGun(&vis->pos, NULL, obj, vis->attach_info.vertnum);
2098 WeaponCalcGun(&vis->end_pos, NULL, obj, vis->attach_info.end_vertnum);
2099 }
2100 }
2101 } else {
2102 float normalized_time[MAX_SUBOBJECTS];
2103
2104 if (obj->lowest_attached_vis == -1) {
2105 obj->lowest_attached_vis = vis - VisEffects;
2106 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
2107
2108 int i;
2109
2110 SetNormalizedTimeObj(obj, normalized_time);
2111 SetModelAngles(pm, normalized_time);
2112 SetModelInterpPos(pm, normalized_time);
2113
2114 for (i = vis - VisEffects; i <= Highest_vis_effect_index; i++) {
2115 vis_effect *this_vis = &VisEffects[i];
2116 if (this_vis->type != VIS_NONE && (this_vis->flags & VF_ATTACHED) &&
2117 ((this_vis->attach_info.obj_handle & HANDLE_OBJNUM_MASK) == objnum) &&
2118 this_vis->id != THICK_LIGHTNING_INDEX) {
2119 bsp_info *sm = &pm->submodel[this_vis->attach_info.subnum];
2120 GetPolyModelPointInWorld(&this_vis->pos, pm, &obj->pos, &obj->orient, this_vis->attach_info.subnum,
2121 normalized_time, &sm->verts[this_vis->attach_info.vertnum], NULL);
2122
2123 if (this_vis->attach_info.end_vertnum != -1)
2124 GetPolyModelPointInWorld(&this_vis->end_pos, pm, &obj->pos, &obj->orient, this_vis->attach_info.subnum,
2125 normalized_time, &sm->verts[this_vis->attach_info.end_vertnum], NULL);
2126 }
2127 }
2128 }
2129 }
2130
2131 // Relink if need be
2132 if (obj->roomnum != vis->roomnum)
2133 VisEffectRelink(vis - VisEffects, obj->roomnum);
2134 }
2135 }
2136
2137 // Do vis effect explosion lighting
2138 if (Detail_settings.Dynamic_lighting && vis->lighting_color != 0 && (vis->lighting_color & OPAQUE_FLAG) &&
2139 (vis->flags & VF_USES_LIFELEFT) && !(vis->flags & VF_DEAD)) {
2140 float scalar;
2141
2142 scalar = (vis->lifetime - vis->lifeleft) / vis->lifetime;
2143
2144 if (scalar > .5) {
2145 scalar -= .5;
2146
2147 scalar = 1.0 - (scalar / .5);
2148 } else {
2149 scalar *= 2;
2150 }
2151
2152 if (scalar > .05) {
2153 int color = GR_16_TO_COLOR(vis->lighting_color);
2154 float r = (GR_COLOR_RED(color)) / 255.0;
2155 float g = (GR_COLOR_GREEN(color)) / 255.0;
2156 float b = (GR_COLOR_BLUE(color)) / 255.0;
2157
2158 if (ROOMNUM_OUTSIDE(vis->roomnum)) {
2159 int cellnum = CELLNUM(vis->roomnum);
2160
2161 if (cellnum >= 0 && cellnum < TERRAIN_WIDTH * TERRAIN_DEPTH)
2162 ApplyLightingToTerrain(&vis->pos, cellnum, vis->size * scalar * 3, r, g, b);
2163 else
2164 mprintf(0, "Vis effect not in world!\n");
2165 } else {
2166 if (vis->roomnum >= 0 && vis->roomnum <= Highest_room_index && Rooms[vis->roomnum].used)
2167 ApplyLightingToRooms(&vis->pos, vis->roomnum, vis->size * scalar * 3, r, g, b);
2168 }
2169 }
2170 }
2171
2172 // Link this effect to the viewer if needed
2173 if (vis->flags & VF_LINK_TO_VIEWER) {
2174 if (vis->roomnum != Viewer_object->roomnum) {
2175 VisEffectRelink(vis - VisEffects, Viewer_object->roomnum);
2176 }
2177 }
2178 }
2179
2180 // Moves our visuals
2181 void VisEffectMoveAll() {
2182 int i;
2183
2184 for (i = 0; i <= Highest_vis_effect_index; i++) {
2185 if (VisEffects[i].type != VIS_NONE)
2186 VisEffectMoveOne(&VisEffects[i]);
2187 }
2188 }
2189
2190 /*
2191 // Attaches viseffects that move with an object
2192 void AttachRandomVisEffectsToObject (int num,int handle,object *obj)
2193 {
2194 int i;
2195 vector zero_pos={0,0,0};
2196 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
2197
2198
2199
2200 for (i=0;i<num;i++)
2201 {
2202
2203 int visnum=VisEffectCreate(VIS_FIREBALL,NAPALM_BALL_INDEX,obj->roomnum,&zero_pos);
2204 if (visnum>=0)
2205 {
2206 vis_effect *vis=&VisEffects[visnum];
2207 vis->size=1.0;
2208 vis->lifetime=15.0;
2209 vis->lifeleft=15.0;
2210 vis->flags |=VF_ATTACHED;
2211 vis->attach_info.obj_handle=obj->handle;
2212
2213 int subnum=ps_rand()%pm->n_models;
2214 bsp_info *sm=&pm->submodel[subnum];
2215
2216 vis->attach_info.subnum=subnum;
2217 vis->attach_info.vertnum=ps_rand()%sm->nverts;
2218
2219 }
2220 }
2221 }*/
2222
2223 // Attaches viseffects that move with an object
2224 void AttachRandomNapalmEffectsToObject(object *obj) {
2225 if (obj->flags & OF_DEAD)
2226 return;
2227
2228 vector velocity_norm = obj->mtype.phys_info.velocity;
2229 vm_NormalizeVector(&velocity_norm);
2230 vector pos = obj->pos - (velocity_norm * (obj->size / 2));
2231
2232 if (obj->movement_type == MT_PHYSICS && (OBJECT_OUTSIDE(obj) && (ps_rand() % 3) == 0) || (ps_rand() % 3) == 0)
2233 CreateFireball(&pos, BLACK_SMOKE_INDEX, obj->roomnum, VISUAL_FIREBALL);
2234
2235 float size_scalar = obj->size / 7.0;
2236
2237 size_scalar = std::clamp(size_scalar, 1.0f, 4.0f);
2238
2239 // Create an explosion that follows every now and then
2240 if ((ps_rand() % 3) == 0) {
2241 if (!(obj->flags & OF_POLYGON_OBJECT))
2242 return;
2243
2244 int num = 1;
2245
2246 num += (obj->size / 15);
2247
2248 for (int i = 0; i < num; i++) {
2249 vector dest;
2250 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
2251
2252 if (pm->n_models == 0)
2253 return;
2254
2255 int subnum = ps_rand() % pm->n_models;
2256
2257 if (IsNonRenderableSubmodel(pm, subnum))
2258 continue;
2259
2260 bsp_info *sm = &pm->submodel[subnum];
2261
2262 if (sm->nverts == 0)
2263 return;
2264
2265 int vertnum = ps_rand() % sm->nverts;
2266
2267 GetPolyModelPointInWorld(&dest, &Poly_models[obj->rtype.pobj_info.model_num], &obj->pos, &obj->orient, subnum,
2268 &sm->verts[vertnum]);
2269 int visnum = VisEffectCreate(VIS_FIREBALL, GetRandomSmallExplosion(), obj->roomnum, &dest);
2270 if (visnum == -1)
2271 return;
2272
2273 VisEffects[visnum].size += ((ps_rand() % 20) / 20.0) * 1.0;
2274
2275 VisEffects[visnum].size *= size_scalar;
2276
2277 if ((ps_rand() % 2)) {
2278 if (obj->movement_type == MT_PHYSICS) {
2279 VisEffects[visnum].movement_type = MT_PHYSICS;
2280 VisEffects[visnum].velocity = obj->mtype.phys_info.velocity;
2281 VisEffects[visnum].mass = obj->mtype.phys_info.mass;
2282 VisEffects[visnum].drag = obj->mtype.phys_info.drag;
2283 }
2284 }
2285 }
2286 }
2287 }