2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Nebula/NebLightning.cpp $
18 * Revision 1.7 2004/09/20 01:31:44 theoddone33
21 * Revision 1.6 2004/07/04 11:39:06 taylor
22 * fix missing debrief text, crash on exit, path separator's, warning fixes, no GR_SOFT
24 * Revision 1.5 2003/05/25 02:30:43 taylor
27 * Revision 1.4 2002/06/17 06:33:09 relnev
28 * ryan's struct patch for gcc 2.95
30 * Revision 1.3 2002/06/09 04:41:23 relnev
31 * added copyright header
33 * Revision 1.2 2002/05/07 03:16:47 theoddone33
34 * The Great Newline Fix
36 * Revision 1.1.1.1 2002/05/03 03:28:10 root
40 * 10 8/15/99 3:50p Dave
41 * Don't process lightning at the very beginning of a mission.
43 * 9 8/12/99 10:38a Anoop
44 * Removed unnecessary Int3().
46 * 8 8/05/99 2:06a Dave
49 * 7 7/27/99 9:51p Andsager
50 * make mprintf's into nprintf's
52 * 6 7/03/99 5:50p Dave
53 * Make rotated bitmaps draw properly in padlock views.
55 * 5 7/02/99 4:31p Dave
56 * Much more sophisticated lightning support.
58 * 4 6/09/99 10:32a Dave
59 * Made random lighting bolts behave more like the E3 demo. Generally more
62 * 3 5/26/99 11:46a Dave
63 * Added ship-blasting lighting and made the randomization of lighting
64 * much more customizable.
66 * 2 5/24/99 5:45p Dave
67 * Added detail levels to the nebula, with a decent speedup. Split nebula
68 * lightning into its own section.
77 #include "freespace.h"
80 #include "missionparse.h"
82 #include "neblightning.h"
85 #include "multimsgs.h"
87 // ------------------------------------------------------------------------------------------------------
88 // NEBULA LIGHTNING DEFINES/VARS
92 #define MAX_BOLT_TYPES_INTERNAL 11
94 // see lightning.tbl for explanations of these values
95 typedef struct bolt_type {
96 char name[NAME_LENGTH];
117 int Num_bolt_types = 0;
118 bolt_type Bolt_types[MAX_BOLT_TYPES_INTERNAL];
121 int Num_storm_types = 0;
122 storm_type Storm_types[MAX_STORM_TYPES];
125 // actual lightning bolt stuff -------
127 #define MAX_LIGHTNING_NODES 500
129 // nodes in a lightning bolt
133 typedef struct l_node {
134 vector pos; // world position
135 l_node *links[3]; // 3 links for lightning children
137 l_node *next, *prev; // for used and free-lists only
142 l_node Nebl_nodes[MAX_LIGHTNING_NODES];
145 // lightning node lists
146 l_node Nebl_free_list;
147 l_node Nebl_used_list;
149 // actual lightning bolt themselves
150 typedef struct l_bolt {
151 l_node *head; // head of the lightning bolt
152 int bolt_life; // remaining life timestamp
153 ubyte used; // used or not
154 ubyte first_frame; // if he hasn't been rendered at least once
158 vector start, strike, midpoint;
159 int delay; // delay stamp
160 int strikes_left; // #of strikes left
164 #define MAX_LIGHTNING_BOLTS 10
167 l_bolt Nebl_bolts[MAX_LIGHTNING_BOLTS];
168 int Nebl_bolt_count = 0;
170 // one cross-section of a lightning bolt
171 typedef struct l_section {
176 // points on the basic cross section
177 vector Nebl_ring[3] = {
178 { { { -1.0f, 0.0f, 0.0f } } },
179 { { { 1.0f, 0.70f, 0.0f } } },
180 { { { 1.0f, -0.70f, 0.0f } } }
183 // pinched off cross-section
184 vector Nebl_ring_pinched[3] = {
185 { { { -0.05f, 0.0f, 0.0f } } },
186 { { { 0.05f, 0.035f, 0.0f } } },
187 { { { 0.05f, -0.035f, 0.0f } } }
190 // globals used for rendering and generating bolts
191 int Nebl_flash_count = 0; // # of points rendered onscreen for this bolt
192 float Nebl_flash_x = 0.0f; // avg x of the points rendered
193 float Nebl_flash_y = 0.0f; // avg y of the points rendered
194 float Nebl_bang = 0.0; // distance to the viewer object
195 float Nebl_alpha = 0.0f; // alpha to use when rendering the bolt itself
196 float Nebl_glow_alpha = 0.0f; // alpha to use when rendering the bolt glow
197 int Nebl_stamp = -1; // random timestamp for making bolts
198 float Nebl_bolt_len; // length of the current bolt being generated
199 bolt_type *Nebl_type; // bolt type
200 matrix Nebl_bolt_dir; // orientation matrix of the bolt being generated
201 vector Nebl_bolt_start; // start point of the bolt being generated
202 vector Nebl_bolt_strike; // strike point of the bolt being generated
204 // the type of active storm
205 storm_type *Storm = NULL;
210 dc_get_arg(ARG_FLOAT);
211 Bolt_types[DEBUG_BOLT].b_scale = Dc_arg_float;
215 dc_get_arg(ARG_FLOAT);
216 Bolt_types[DEBUG_BOLT].b_rand = Dc_arg_float;
220 dc_get_arg(ARG_FLOAT);
221 Bolt_types[DEBUG_BOLT].b_shrink = Dc_arg_float;
225 dc_get_arg(ARG_FLOAT);
226 Bolt_types[DEBUG_BOLT].b_poly_pct = Dc_arg_float;
230 dc_get_arg(ARG_FLOAT);
231 Bolt_types[DEBUG_BOLT].b_add = Dc_arg_float;
236 Bolt_types[DEBUG_BOLT].num_strikes = Dc_arg_int;
240 dc_get_arg(ARG_FLOAT);
241 Bolt_types[DEBUG_BOLT].noise = Dc_arg_float;
245 dc_get_arg(ARG_FLOAT);
246 Bolt_types[DEBUG_BOLT].b_bright = Dc_arg_float;
251 Bolt_types[DEBUG_BOLT].lifetime = Dc_arg_int;
255 dc_printf("Debug lightning bolt settings :\n");
257 dc_printf("b_scale : %f\n", Bolt_types[DEBUG_BOLT].b_scale);
258 dc_printf("b_rand : %f\n", Bolt_types[DEBUG_BOLT].b_rand);
259 dc_printf("b_shrink : %f\n", Bolt_types[DEBUG_BOLT].b_shrink);
260 dc_printf("b_poly_pct : %f\n", Bolt_types[DEBUG_BOLT].b_poly_pct);
261 dc_printf("b_add : %f\n", Bolt_types[DEBUG_BOLT].b_add);
262 dc_printf("b_strikes : %d\n", Bolt_types[DEBUG_BOLT].num_strikes);
263 dc_printf("b_noise : %f\n", Bolt_types[DEBUG_BOLT].noise);
264 dc_printf("b_bright : %f\n", Bolt_types[DEBUG_BOLT].b_bright);
265 dc_printf("b_lifetime : %d\n", Bolt_types[DEBUG_BOLT].lifetime);
269 // nebula lightning intensity (0.0 to 1.0)
270 float Nebl_intensity = 0.6667f;
272 // min and max times for random lightning
273 int Nebl_random_min = 750; // min random time
274 int Nebl_random_max = 10000; // max random time
276 // min and max times for cruiser lightning
277 int Nebl_cruiser_min = 5000; // min cruiser time
278 int Nebl_cruiser_max = 25000; // max cruiser time
280 // min and max times for cap ships
281 int Nebl_cap_min = 4000; // min cap time
282 int Nebl_cap_max = 18000; // max cap time
284 // min and max time for super caps
285 int Nebl_supercap_min = 3000; // min supercap time
286 int Nebl_supercap_max = 12000; // max supercap time
288 DCF(lightning_intensity, "")
290 dc_get_arg(ARG_FLOAT);
291 float val = Dc_arg_float;
294 } else if(val > 1.0f){
298 Nebl_intensity = 1.0f - val;
301 // ------------------------------------------------------------------------------------------------------
302 // NEBULA LIGHTNING FORWARD DECLARATIONS
305 // "new" a lightning node
308 // "delete" a lightning node
309 void nebl_delete(l_node *lp);
311 // free up a the nodes of the passed in bolt
312 void nebl_release(l_node *bolt_head);
314 // generate a lightning bolt, returns l_left (the "head") and l_right (the "tail")
315 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right);
317 // output top and bottom vectors
318 // fvec == forward vector (eye viewpoint basically. in world coords)
319 // pos == world coordinate of the point we're calculating "around"
320 // w == width of the diff between top and bottom around pos
321 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add );
323 // render a section of the bolt
324 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b);
326 // generate a section
327 void nebl_generate_section(bolt_type *bi, float width, l_node *a, l_node *b, l_section *c, l_section *cap, int pinch_a, int pinch_b);
330 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev = NULL);
332 // given a valid, complete bolt, jitter him based upon his noise
333 void nebl_jitter(l_bolt *b);
335 // return the index of a given bolt type by name
336 int nebl_get_bolt_index(char *name);
338 // return the index of a given storm type by name
339 int nebl_get_storm_index(char *name);
342 // ------------------------------------------------------------------------------------------------------
343 // NEBULA LIGHTNING FUNCTIONS
346 // initialize nebula lightning at game startup
350 char name[NAME_LENGTH+10] = "";
351 bolt_type bogus_lightning, *l;
352 storm_type bogus_storm, *s;
355 // parse the lightning table
356 read_file_text("lightning.tbl");
362 memset(Bolt_types, 0, sizeof(bolt_type) * MAX_BOLT_TYPES_INTERNAL);
364 // parse the individual lightning bolt types
365 required_string("#Bolts begin");
366 while(!optional_string("#Bolts end")){
368 if(Num_bolt_types >= MAX_BOLT_TYPES){
369 l = &bogus_lightning;
371 l = &Bolt_types[Num_bolt_types];
375 required_string("$Bolt:");
376 stuff_string(l->name, F_NAME, NULL);
379 required_string("+b_scale:");
380 stuff_float(&l->b_scale);
383 required_string("+b_shrink:");
384 stuff_float(&l->b_shrink);
387 required_string("+b_poly_pct:");
388 stuff_float(&l->b_poly_pct);
391 required_string("+b_rand:");
392 stuff_float(&l->b_rand);
395 required_string("+b_add:");
396 stuff_float(&l->b_add);
399 required_string("+b_strikes:");
400 stuff_int(&l->num_strikes);
403 required_string("+b_lifetime:");
404 stuff_int(&l->lifetime);
407 required_string("+b_noise:");
408 stuff_float(&l->noise);
411 required_string("+b_emp:");
412 stuff_float(&l->emp_intensity);
413 stuff_float(&l->emp_time);
416 required_string("+b_texture:");
417 stuff_string(name, F_NAME, NULL);
418 if((l != &bogus_lightning) && !Fred_running){
419 l->texture = bm_load(name);
423 required_string("+b_glow:");
424 stuff_string(name, F_NAME, NULL);
425 if((l != &bogus_lightning) && !Fred_running){
426 l->glow = bm_load(name);
430 required_string("+b_bright:");
431 stuff_float(&l->b_bright);
433 // increment the # of bolt types
434 if(l != &bogus_lightning){
439 // copy the first bolt to the debug bolt
440 memcpy(&Bolt_types[DEBUG_BOLT], &Bolt_types[0], sizeof(bolt_type));
443 required_string("#Storms begin");
444 while(!optional_string("#Storms end")){
446 if(Num_storm_types >= MAX_STORM_TYPES){
449 s = &Storm_types[Num_storm_types];
453 required_string("$Storm:");
454 stuff_string(s->name, F_NAME, NULL);
457 s->num_bolt_types = 0;
458 while(optional_string("+bolt:")){
459 stuff_string(name, F_NAME, NULL);
462 if(s->num_bolt_types < MAX_BOLT_TYPES){
463 s->bolt_types[s->num_bolt_types] = (char)nebl_get_bolt_index(name);
464 SDL_assert(s->bolt_types[s->num_bolt_types] != -1);
470 required_string("+bolt_prec:");
476 required_string("+flavor:");
477 stuff_float(&s->flavor.xyz.x);
478 stuff_float(&s->flavor.xyz.y);
479 stuff_float(&s->flavor.xyz.z);
482 required_string("+random_freq:");
487 required_string("+random_count:");
488 stuff_int(&s->min_count);
489 stuff_int(&s->max_count);
491 // increment the # of bolt types
492 if(s != &bogus_storm){
499 // initialize lightning before entering a level
500 void nebl_level_init()
504 // zero all lightning bolts
505 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
506 Nebl_bolts[idx].head = NULL;
507 Nebl_bolts[idx].bolt_life = -1;
508 Nebl_bolts[idx].used = 0;
511 // initialize node list
513 list_init( &Nebl_free_list );
514 list_init( &Nebl_used_list );
516 // Link all object slots into the free list
517 for (idx=0; idx<MAX_LIGHTNING_NODES; idx++) {
518 list_append(&Nebl_free_list, &Nebl_nodes[idx] );
521 // zero the random timestamp
524 // null the storm. let mission parsing set it up
528 // render all lightning bolts
529 void nebl_render_all()
535 // no lightning in non-nebula missions
536 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
540 // if we have no storm
546 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
547 b = &Nebl_bolts[idx];
549 // if this is being used
551 SDL_assert(b->head != NULL);
558 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
563 bi = &Bolt_types[(int)b->type];
565 // if this guy is still on a delay
567 if(timestamp_elapsed(b->delay)){
574 // if the timestamp on this guy has expired
575 if((b->bolt_life < 0) || timestamp_elapsed(b->bolt_life)){
576 // if this is a multiple strike bolt, jitter it and reset
577 if(b->strikes_left-1 > 0){
578 b->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
583 // by continuing here we skip rendering for one frame, which makes it look more like real lightning
586 // otherwise he's completely done, so release him
588 // maybe free up node data
590 nebl_release(b->head);
595 nprintf(("lightning", "Released bolt. %d used nodes!\n", Num_lnodes));
602 // pick some cool alpha values
603 Nebl_alpha = frand();
604 Nebl_glow_alpha = frand();
606 // otherwise render him
607 Nebl_flash_count = 0;
610 Nebl_bang = 10000000.0f;
611 nebl_render(bi, b->head, b->width);
613 // if this is the first frame he has been rendered, determine if we need to make a flash and sound effect
619 // if we rendered any points
620 if(Nebl_flash_count){
621 Nebl_flash_x /= (float)Nebl_flash_count;
622 Nebl_flash_y /= (float)Nebl_flash_count;
624 // quick distance from the center of the screen
625 float x = Nebl_flash_x - (gr_screen.max_w / 2.0f);
626 float y = Nebl_flash_y - (gr_screen.max_h / 2.0f);
627 float dist = fl_sqrt((x * x) + (y * y));
628 if(dist / (gr_screen.max_w / 2.0f) < 1.0f){
629 flash = 1.0f - (dist / (gr_screen.max_w / 2.0f));
631 // scale the flash by bolt type
632 flash *= bi->b_bright;
634 game_flash(flash, flash, flash);
637 // do some special stuff on the very first strike of the bolt
638 if(b->strikes_left == bi->num_strikes){
641 if(Nebl_bang < 40.0f){
643 } else if(Nebl_bang > 400.0f){
646 bang = 1.0f - (Nebl_bang / 400.0f);
648 if(frand_range(0.0f, 1.0f) < 0.5f){
649 snd_play(&Snds[SND_LIGHTNING_2], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
651 snd_play(&Snds[SND_LIGHTNING_1], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
655 if(bi->emp_intensity > 0.0f){
656 emp_apply(&b->midpoint, 0.0f, vm_vec_dist(&b->start, &b->strike), bi->emp_intensity, bi->emp_time);
665 // process lightning (randomly generate bolts, etc, etc);
670 // non-nebula mission
671 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
675 // non servers in multiplayer don't do this
676 if((Game_mode & GM_MULTIPLAYER) && !MULTIPLAYER_MASTER){
680 // if there's no chosen storm
685 // don't process lightning bolts unless we're a few seconds in
686 if(f2fl(Missiontime) < 3.0f){
691 if(Nebl_stamp == -1){
692 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
697 if(timestamp_elapsed(Nebl_stamp)){
698 // determine how many bolts to spew
699 num_bolts = (int)frand_range((float)Storm->min_count, (float)Storm->max_count);
700 for(idx=0; idx<num_bolts; idx++){
701 // hmm. for now just pick a random bolt type and run with it
705 s1 = (int)frand_range(0.0f, (float)Neb2_slices);
706 s2 = (int)frand_range(0.0f, (float)Neb2_slices);
707 s3 = (int)frand_range(0.0f, (float)Neb2_slices);
709 e1 = (int)frand_range(0.0f, (float)Neb2_slices);
710 e2 = (int)frand_range(0.0f, (float)Neb2_slices);
711 e3 = (int)frand_range(0.0f, (float)Neb2_slices);
713 // never choose the middle cube
714 if((s1 == 2) && (s2 == 2) && (s3 == 2)){
718 if((e1 == 2) && (e2 == 2) && (e3 == 2)){
724 } while((s1 == e1) && (s2 == e2) && (s3 == e3));
726 vector start = Neb2_cubes[s1][s2][s3].pt;
727 vector strike = Neb2_cubes[e1][e2][e3].pt;
729 // add some flavor to the bolt. mmmmmmmm, lightning
730 if(!IS_VEC_NULL(&Storm->flavor)){
731 // start with your basic hot sauce. measure how much you have
732 vector your_basic_hot_sauce;
733 vm_vec_sub(&your_basic_hot_sauce, &strike, &start);
734 float how_much_hot_sauce = vm_vec_normalize(&your_basic_hot_sauce);
736 // now figure out how much of that good wing sauce to add
737 vector wing_sauce = Storm->flavor;
738 if(frand_range(0.0, 1.0f) < 0.5f){
739 vm_vec_scale(&wing_sauce, -1.0f);
741 float how_much_of_that_good_wing_sauce_to_add = vm_vec_normalize(&wing_sauce);
743 // mix the two together, taking care not to add too much
745 if(how_much_of_that_good_wing_sauce_to_add > 1000.0f){
746 how_much_of_that_good_wing_sauce_to_add = 1000.0f;
748 vm_vec_interp_constant(&the_mixture, &your_basic_hot_sauce, &wing_sauce, how_much_of_that_good_wing_sauce_to_add / 1000.0f);
750 // take the final sauce and store it in the proper container
751 vm_vec_scale(&the_mixture, how_much_hot_sauce);
753 // make sure to put it on everything! whee!
754 vm_vec_add(&strike, &start, &the_mixture);
757 int type = (int)frand_range(0.0f, (float)(Storm->num_bolt_types-1));
758 nebl_bolt(Storm->bolt_types[type], &start, &strike);
761 // reset the timestamp
762 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
766 // create a lightning bolt
767 void nebl_bolt(int type, vector *start, vector *strike)
777 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
783 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
784 if(!Nebl_bolts[idx].used){
794 if((type < 0) || ((type >= Num_bolt_types) && (type != DEBUG_BOLT)) ){
797 bi = &Bolt_types[type];
799 // get a pointer to the bolt
800 bolt = &Nebl_bolts[idx];
803 bolt->start = *start;
804 bolt->strike = *strike;
805 bolt->strikes_left = bi->num_strikes;
807 bolt->type = (char)type;
808 bolt->first_frame = 1;
809 bolt->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
811 Nebl_bolt_start = *start;
812 Nebl_bolt_strike = *strike;
815 if(bolt->delay != -1){
816 bolt->delay = timestamp(bolt->delay);
819 // setup the rest of the important bolt data
820 if(vm_vec_same(&Nebl_bolt_start, &Nebl_bolt_strike)){
821 Nebl_bolt_strike.xyz.z += 150.0f;
823 Nebl_bolt_len = vm_vec_dist(&Nebl_bolt_start, &Nebl_bolt_strike);
824 vm_vec_sub(&dir, &Nebl_bolt_strike, &Nebl_bolt_start);
827 vm_vec_scale_add(&bolt->midpoint, &Nebl_bolt_start, &dir, 0.5f);
829 bolt_len = vm_vec_normalize(&dir);
830 vm_vector_2_matrix(&Nebl_bolt_dir, &dir, NULL, NULL);
832 // global type for generating the bolt
835 // try and make the bolt
836 if(!nebl_gen(&Nebl_bolt_start, &Nebl_bolt_strike, 0, 4, 0, &bolt->head, &tail)){
837 if(bolt->head != NULL){
838 nebl_release(bolt->head);
846 // setup the rest of the data
848 bolt->width = bi->b_poly_pct * bolt_len;
850 // if i'm a multiplayer master, send a bolt packet
851 if(MULTIPLAYER_MASTER){
852 send_lightning_packet(type, start, strike);
856 // get the current # of active lightning bolts
857 int nebl_get_active_bolts()
859 return Nebl_bolt_count;
862 // get the current # of active nodes
863 int nebl_get_active_nodes()
868 // set the storm (call from mission parse)
869 void nebl_set_storm(char *name)
871 int index = nebl_get_storm_index(name);
875 if((index >= 0) && (index < Num_storm_types)){
876 Storm = &Storm_types[index];
880 // ------------------------------------------------------------------------------------------------------
881 // NEBULA LIGHTNING FORWARD DEFINITIONS
884 // "new" a lightning node
889 // if we're out of nodes
890 if(Num_lnodes >= MAX_LIGHTNING_NODES){
892 nprintf(("lightning", "Out of lightning nodes!\n"));
896 // get a new node off the freelist
897 lp = GET_FIRST(&Nebl_free_list);
898 SDL_assert( lp != &Nebl_free_list ); // shouldn't have the dummy element
900 // remove trailp from the free list
901 list_remove( &Nebl_free_list, lp );
903 // insert trailp onto the end of used list
904 list_append( &Nebl_used_list, lp );
913 // return the pointer
917 // "delete" a lightning node
918 void nebl_delete(l_node *lp)
920 // remove objp from the used list
921 list_remove( &Nebl_used_list, lp );
923 // add objp to the end of the free
924 list_append( &Nebl_free_list, lp );
930 // free a lightning bolt
931 void nebl_release(l_node *whee)
938 // release all of our children
939 if(whee->links[LINK_RIGHT] != NULL){
940 nebl_release(whee->links[LINK_RIGHT]);
942 if(whee->links[LINK_CHILD] != NULL){
943 nebl_release(whee->links[LINK_CHILD]);
950 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right)
952 l_node *child_node = NULL;
953 float d = vm_vec_dist_quick( left, right );
955 // if we've reached the critical point
956 if ( d < 0.30f || (depth > max_depth) ){
958 l_node *new_left = nebl_new();
959 if(new_left == NULL){
962 new_left->links[0] = NULL; new_left->links[1] = NULL; new_left->links[2] = NULL;
963 new_left->pos = vmd_zero_vector;
964 l_node *new_right = nebl_new();
965 if(new_right == NULL){
966 nebl_delete(new_left);
969 new_right->links[0] = NULL; new_right->links[1] = NULL; new_right->links[2] = NULL;
970 new_right->pos = vmd_zero_vector;
973 new_left->pos = *left;
974 new_left->links[LINK_RIGHT] = new_right;
978 new_right->pos = *right;
979 new_right->links[LINK_LEFT] = new_left;
980 *l_right = new_right;
988 vm_vec_avg( &tmp, left, right );
990 // sometimes generate children
991 if(!child && (frand() <= Nebl_type->b_rand)){
992 // get a point on the plane of the strike
994 vm_vec_random_in_circle(&tmp2, &Nebl_bolt_strike, &Nebl_bolt_dir, Nebl_bolt_len * Nebl_type->b_scale, 0);
996 // maybe move away from the plane
998 vm_vec_sub(&dir, &tmp2, &tmp);
999 vm_vec_scale_add(&tmp2, &tmp, &dir, Nebl_type->b_shrink);
1003 if(!nebl_gen(&tmp, &tmp2, 0, 2, 1, &child_node, &argh)){
1004 if(child_node != NULL){
1005 nebl_release(child_node);
1011 float scaler = 0.30f;
1012 tmp.xyz.x += (frand()-0.5f)*d*scaler;
1013 tmp.xyz.y += (frand()-0.5f)*d*scaler;
1014 tmp.xyz.z += (frand()-0.5f)*d*scaler;
1016 // generate left half
1019 if(!nebl_gen( left, &tmp, depth+1, max_depth, child, &ll, &lr )){
1020 if(child_node != NULL){
1021 nebl_release(child_node);
1029 // generate right half
1032 if(!nebl_gen( &tmp, right, depth+1, max_depth, child, &rl, &rr )){
1033 if(child_node != NULL){
1034 nebl_release(child_node);
1045 // splice the two together
1046 lr->links[LINK_RIGHT] = rl->links[LINK_RIGHT];
1047 lr->links[LINK_RIGHT]->links[LINK_LEFT] = lr;
1050 // if we generated a child, stick him on
1051 if(child_node != NULL){
1052 lr->links[LINK_CHILD] = child_node;
1063 // output top and bottom vectors
1064 // fvec == forward vector (eye viewpoint basically. in world coords)
1065 // pos == world coordinate of the point we're calculating "around"
1066 // w == width of the diff between top and bottom around pos
1067 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1074 vm_vec_sub( &rvec, &Eye_position, &temp );
1075 vm_vec_normalize( &rvec );
1077 vm_vec_crossprod(&uvec,fvec,&rvec);
1078 vm_vec_normalize(&uvec);
1080 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1081 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1083 vm_vec_scale_add2( top, &rvec, z_add );
1084 vm_vec_scale_add2( bot, &rvec, z_add );
1087 // render a section of the bolt
1088 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b)
1091 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
1094 // Sets mode. Returns previous mode.
1095 gr_zbuffer_set(GR_ZBUFF_FULL);
1098 for(idx=0; idx<2; idx++){
1100 v[0].u = 0.0f; v[0].v = 0.0f;
1102 v[1] = a->vex[idx+1];
1103 v[1].u = 1.0f; v[1].v = 0.0f;
1105 v[2] = b->vex[idx+1];
1106 v[2].u = 1.0f; v[2].v = 1.0f;
1109 v[3].u = 0.0f; v[3].v = 1.0f;
1112 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha, -1, -1);
1113 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1118 v[0].u = 0.0f; v[0].v = 0.0f;
1121 v[1].u = 1.0f; v[1].v = 0.0f;
1124 v[2].u = 1.0f; v[2].v = 1.0f;
1127 v[3].u = 0.0f; v[3].v = 1.0f;
1129 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha, -1, -1);
1130 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1132 // draw the glow beam
1133 verts[0] = &a->glow_vex[0];
1134 verts[0]->v = 0.0f; verts[0]->u = 0.0f;
1136 verts[1] = &a->glow_vex[1];
1137 verts[1]->v = 1.0f; verts[1]->u = 0.0f;
1139 verts[2] = &b->glow_vex[1];
1140 verts[2]->v = 1.0f; verts[2]->u = 1.0f;
1142 verts[3] = &b->glow_vex[0];
1143 verts[3]->v = 0.0f; verts[3]->u = 1.0f;
1145 gr_set_bitmap(bi->glow, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_glow_alpha, -1, -1);
1146 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1149 // generate a section
1150 void nebl_generate_section(bolt_type *bi, float width, l_node *a, l_node *b, l_section *c, l_section *cap, int pinch_a, int pinch_b)
1157 vector glow_a, glow_b;
1160 vm_vec_sub(&dir, &a->pos, &b->pos);
1161 vm_vec_copy_normalize(&dir_normal, &dir);
1162 vm_vector_2_matrix(&m, &dir_normal, NULL, NULL);
1164 // distance to player
1165 float bang_dist = vm_vec_dist_quick(&Eye_position, &a->pos);
1166 if(bang_dist < Nebl_bang){
1167 Nebl_bang = bang_dist;
1170 // rotate the basic section into world
1171 for(idx=0; idx<3; idx++){
1174 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1176 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1177 vm_vec_rotate(&pt, &temp, &m);
1179 vm_vec_add2(&pt, &a->pos);
1182 g3_rotate_vertex(&c->vex[idx], &pt);
1183 g3_project_vertex(&c->vex[idx]);
1185 // if first frame, keep track of the average screen pos
1186 if((c->vex[idx].sx >= 0) && (c->vex[idx].sx < gr_screen.max_w) && (c->vex[idx].sy >= 0) && (c->vex[idx].sy < gr_screen.max_h)){
1187 Nebl_flash_x += c->vex[idx].sx;
1188 Nebl_flash_y += c->vex[idx].sy;
1192 // calculate the glow points
1193 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &a->pos, pinch_a ? 0.5f : width * 6.0f, Nebl_type->b_add);
1194 g3_rotate_vertex(&c->glow_vex[0], &glow_a);
1195 g3_project_vertex(&c->glow_vex[0]);
1196 g3_rotate_vertex(&c->glow_vex[1], &glow_b);
1197 g3_project_vertex(&c->glow_vex[1]);
1201 // rotate the basic section into world
1202 for(idx=0; idx<3; idx++){
1205 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1207 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1208 vm_vec_rotate(&pt, &temp, &m);
1210 vm_vec_add2(&pt, &b->pos);
1213 g3_rotate_vertex(&cap->vex[idx], &pt);
1214 g3_project_vertex(&cap->vex[idx]);
1216 // if first frame, keep track of the average screen pos
1217 if( (c->vex[idx].sx >= 0) && (c->vex[idx].sx < gr_screen.max_w) && (c->vex[idx].sy >= 0) && (c->vex[idx].sy < gr_screen.max_h)){
1218 Nebl_flash_x += c->vex[idx].sx;
1219 Nebl_flash_y += c->vex[idx].sy;
1224 // calculate the glow points
1225 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &b->pos, pinch_b ? 0.5f : width * 6.0f, bi->b_add);
1226 g3_rotate_vertex(&cap->glow_vex[0], &glow_a);
1227 g3_project_vertex(&cap->glow_vex[0]);
1228 g3_rotate_vertex(&cap->glow_vex[1], &glow_b);
1229 g3_project_vertex(&cap->glow_vex[1]);
1234 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev)
1238 l_section child_start;
1245 // if prev is NULL, we're just starting so we need our start point
1247 SDL_assert(whee->links[LINK_RIGHT] != NULL);
1248 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &start, NULL, 1, 0);
1253 // if we have a child section
1254 if(whee->links[LINK_CHILD]){
1256 nebl_generate_section(bi, width * 0.5f, whee, whee->links[LINK_CHILD], &child_start, &end, 0, whee->links[LINK_CHILD]->links[LINK_RIGHT] == NULL ? 1 : 0);
1259 nebl_render_section(bi, &child_start, &end);
1262 if(whee->links[LINK_CHILD]->links[LINK_RIGHT] != NULL){
1263 nebl_render(bi, whee->links[LINK_CHILD], width * 0.5f, &end);
1267 // if the next section is an end section
1268 if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] == NULL){
1272 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &temp, &end, 0, 1);
1274 // render the section
1275 nebl_render_section(bi, &start, &end);
1278 else if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] != NULL){
1280 nebl_generate_section(bi, width, whee->links[LINK_RIGHT], whee->links[LINK_RIGHT]->links[LINK_RIGHT], &end, NULL, 0, 0);
1282 // render the section
1283 nebl_render_section(bi, &start, &end);
1285 // recurse through him
1286 nebl_render(bi, whee->links[LINK_RIGHT], width, &end);
1290 // given a valid, complete bolt, jitter him based upon his noise
1291 void nebl_jitter(l_bolt *b)
1297 bolt_type *bi = NULL;
1303 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
1306 bi = &Bolt_types[(int)b->type];
1308 // get the bolt direction
1309 vm_vec_sub(&temp, &b->strike, &b->start);
1310 length = vm_vec_normalize_quick(&temp);
1311 vm_vector_2_matrix(&m, &temp, NULL, NULL);
1313 // jitter all nodes on the main trunk
1315 while(moveup != NULL){
1317 vm_vec_random_in_circle(&moveup->pos, &temp, &m, frand_range(0.0f, length * bi->noise), 0);
1319 // just on the main trunk
1320 moveup = moveup->links[LINK_RIGHT];
1324 // return the index of a given bolt type by name
1325 int nebl_get_bolt_index(char *name)
1329 for(idx=0; idx<Num_bolt_types; idx++){
1330 if(!strcmp(name, Bolt_types[idx].name)){
1338 // return the index of a given storm type by name
1339 int nebl_get_storm_index(char *name)
1343 for(idx=0; idx<Num_bolt_types; idx++){
1344 if(!strcmp(name, Storm_types[idx].name)){