2 * $Logfile: /Freespace2/code/Nebula/NebLightning.cpp $
10 * Revision 1.1 2002/05/03 03:28:10 root
14 * 10 8/15/99 3:50p Dave
15 * Don't process lightning at the very beginning of a mission.
17 * 9 8/12/99 10:38a Anoop
18 * Removed unnecessary Int3().
20 * 8 8/05/99 2:06a Dave
23 * 7 7/27/99 9:51p Andsager
24 * make mprintf's into nprintf's
26 * 6 7/03/99 5:50p Dave
27 * Make rotated bitmaps draw properly in padlock views.
29 * 5 7/02/99 4:31p Dave
30 * Much more sophisticated lightning support.
32 * 4 6/09/99 10:32a Dave
33 * Made random lighting bolts behave more like the E3 demo. Generally more
36 * 3 5/26/99 11:46a Dave
37 * Added ship-blasting lighting and made the randomization of lighting
38 * much more customizable.
40 * 2 5/24/99 5:45p Dave
41 * Added detail levels to the nebula, with a decent speedup. Split nebula
42 * lightning into its own section.
51 #include "freespace.h"
54 #include "missionparse.h"
56 #include "neblightning.h"
59 #include "multimsgs.h"
61 // ------------------------------------------------------------------------------------------------------
62 // NEBULA LIGHTNING DEFINES/VARS
66 #define MAX_BOLT_TYPES_INTERNAL 11
68 // see lightning.tbl for explanations of these values
69 typedef struct bolt_type {
70 char name[NAME_LENGTH];
91 int Num_bolt_types = 0;
92 bolt_type Bolt_types[MAX_BOLT_TYPES_INTERNAL];
95 int Num_storm_types = 0;
96 storm_type Storm_types[MAX_STORM_TYPES];
99 // actual lightning bolt stuff -------
101 #define MAX_LIGHTNING_NODES 500
103 // nodes in a lightning bolt
107 typedef struct l_node {
108 vector pos; // world position
109 l_node *links[3]; // 3 links for lightning children
111 l_node *next, *prev; // for used and free-lists only
116 l_node Nebl_nodes[MAX_LIGHTNING_NODES];
119 // lightning node lists
120 l_node Nebl_free_list;
121 l_node Nebl_used_list;
123 // actual lightning bolt themselves
124 typedef struct l_bolt {
125 l_node *head; // head of the lightning bolt
126 int bolt_life; // remaining life timestamp
127 ubyte used; // used or not
128 ubyte first_frame; // if he hasn't been rendered at least once
132 vector start, strike, midpoint;
133 int delay; // delay stamp
134 int strikes_left; // #of strikes left
138 #define MAX_LIGHTNING_BOLTS 10
141 l_bolt Nebl_bolts[MAX_LIGHTNING_BOLTS];
142 int Nebl_bolt_count = 0;
144 // one cross-section of a lightning bolt
145 typedef struct l_section {
150 // points on the basic cross section
151 vector Nebl_ring[3] = {
152 { -1.0f, 0.0f, 0.0f },
153 { 1.0f, 0.70f, 0.0f },
154 { 1.0f, -0.70f, 0.0f }
157 // pinched off cross-section
158 vector Nebl_ring_pinched[3] = {
159 { -0.05f, 0.0f, 0.0f },
160 { 0.05f, 0.035f, 0.0f },
161 { 0.05f, -0.035f, 0.0f }
164 // globals used for rendering and generating bolts
165 int Nebl_flash_count = 0; // # of points rendered onscreen for this bolt
166 float Nebl_flash_x = 0.0f; // avg x of the points rendered
167 float Nebl_flash_y = 0.0f; // avg y of the points rendered
168 float Nebl_bang = 0.0; // distance to the viewer object
169 float Nebl_alpha = 0.0f; // alpha to use when rendering the bolt itself
170 float Nebl_glow_alpha = 0.0f; // alpha to use when rendering the bolt glow
171 int Nebl_stamp = -1; // random timestamp for making bolts
172 float Nebl_bolt_len; // length of the current bolt being generated
173 bolt_type *Nebl_type; // bolt type
174 matrix Nebl_bolt_dir; // orientation matrix of the bolt being generated
175 vector Nebl_bolt_start; // start point of the bolt being generated
176 vector Nebl_bolt_strike; // strike point of the bolt being generated
178 // the type of active storm
179 storm_type *Storm = NULL;
184 dc_get_arg(ARG_FLOAT);
185 Bolt_types[DEBUG_BOLT].b_scale = Dc_arg_float;
189 dc_get_arg(ARG_FLOAT);
190 Bolt_types[DEBUG_BOLT].b_rand = Dc_arg_float;
194 dc_get_arg(ARG_FLOAT);
195 Bolt_types[DEBUG_BOLT].b_shrink = Dc_arg_float;
199 dc_get_arg(ARG_FLOAT);
200 Bolt_types[DEBUG_BOLT].b_poly_pct = Dc_arg_float;
204 dc_get_arg(ARG_FLOAT);
205 Bolt_types[DEBUG_BOLT].b_add = Dc_arg_float;
210 Bolt_types[DEBUG_BOLT].num_strikes = Dc_arg_int;
214 dc_get_arg(ARG_FLOAT);
215 Bolt_types[DEBUG_BOLT].noise = Dc_arg_float;
219 dc_get_arg(ARG_FLOAT);
220 Bolt_types[DEBUG_BOLT].b_bright = Dc_arg_float;
225 Bolt_types[DEBUG_BOLT].lifetime = Dc_arg_int;
229 dc_printf("Debug lightning bolt settings :\n");
231 dc_printf("b_scale : %f\n", Bolt_types[DEBUG_BOLT].b_scale);
232 dc_printf("b_rand : %f\n", Bolt_types[DEBUG_BOLT].b_rand);
233 dc_printf("b_shrink : %f\n", Bolt_types[DEBUG_BOLT].b_shrink);
234 dc_printf("b_poly_pct : %f\n", Bolt_types[DEBUG_BOLT].b_poly_pct);
235 dc_printf("b_add : %f\n", Bolt_types[DEBUG_BOLT].b_add);
236 dc_printf("b_strikes : %d\n", Bolt_types[DEBUG_BOLT].num_strikes);
237 dc_printf("b_noise : %f\n", Bolt_types[DEBUG_BOLT].noise);
238 dc_printf("b_bright : %f\n", Bolt_types[DEBUG_BOLT].b_bright);
239 dc_printf("b_lifetime : %d\n", Bolt_types[DEBUG_BOLT].lifetime);
243 // nebula lightning intensity (0.0 to 1.0)
244 float Nebl_intensity = 0.6667f;
246 // min and max times for random lightning
247 int Nebl_random_min = 750; // min random time
248 int Nebl_random_max = 10000; // max random time
250 // min and max times for cruiser lightning
251 int Nebl_cruiser_min = 5000; // min cruiser time
252 int Nebl_cruiser_max = 25000; // max cruiser time
254 // min and max times for cap ships
255 int Nebl_cap_min = 4000; // min cap time
256 int Nebl_cap_max = 18000; // max cap time
258 // min and max time for super caps
259 int Nebl_supercap_min = 3000; // min supercap time
260 int Nebl_supercap_max = 12000; // max supercap time
262 DCF(lightning_intensity, "")
264 dc_get_arg(ARG_FLOAT);
265 float val = Dc_arg_float;
268 } else if(val > 1.0f){
272 Nebl_intensity = 1.0f - val;
275 // ------------------------------------------------------------------------------------------------------
276 // NEBULA LIGHTNING FORWARD DECLARATIONS
279 // "new" a lightning node
282 // "delete" a lightning node
283 void nebl_delete(l_node *lp);
285 // free up a the nodes of the passed in bolt
286 void nebl_release(l_node *bolt_head);
288 // generate a lightning bolt, returns l_left (the "head") and l_right (the "tail")
289 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right);
291 // output top and bottom vectors
292 // fvec == forward vector (eye viewpoint basically. in world coords)
293 // pos == world coordinate of the point we're calculating "around"
294 // w == width of the diff between top and bottom around pos
295 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add );
297 // render a section of the bolt
298 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b);
300 // generate a section
301 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);
304 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev = NULL);
306 // given a valid, complete bolt, jitter him based upon his noise
307 void nebl_jitter(l_bolt *b);
309 // return the index of a given bolt type by name
310 int nebl_get_bolt_index(char *name);
312 // return the index of a given storm type by name
313 int nebl_get_storm_index(char *name);
316 // ------------------------------------------------------------------------------------------------------
317 // NEBULA LIGHTNING FUNCTIONS
320 // initialize nebula lightning at game startup
323 char name[NAME_LENGTH+10] = "";
324 bolt_type bogus_lightning, *l;
325 storm_type bogus_storm, *s;
328 // parse the lightning table
329 read_file_text("lightning.tbl");
335 memset(Bolt_types, 0, sizeof(bolt_type) * MAX_BOLT_TYPES_INTERNAL);
337 // parse the individual lightning bolt types
338 required_string("#Bolts begin");
339 while(!optional_string("#Bolts end")){
341 if(Num_bolt_types >= MAX_BOLT_TYPES){
342 l = &bogus_lightning;
344 l = &Bolt_types[Num_bolt_types];
348 required_string("$Bolt:");
349 stuff_string(l->name, F_NAME, NULL);
352 required_string("+b_scale:");
353 stuff_float(&l->b_scale);
356 required_string("+b_shrink:");
357 stuff_float(&l->b_shrink);
360 required_string("+b_poly_pct:");
361 stuff_float(&l->b_poly_pct);
364 required_string("+b_rand:");
365 stuff_float(&l->b_rand);
368 required_string("+b_add:");
369 stuff_float(&l->b_add);
372 required_string("+b_strikes:");
373 stuff_int(&l->num_strikes);
376 required_string("+b_lifetime:");
377 stuff_int(&l->lifetime);
380 required_string("+b_noise:");
381 stuff_float(&l->noise);
384 required_string("+b_emp:");
385 stuff_float(&l->emp_intensity);
386 stuff_float(&l->emp_time);
389 required_string("+b_texture:");
390 stuff_string(name, F_NAME, NULL);
391 if((l != &bogus_lightning) && !Fred_running){
392 l->texture = bm_load(name);
396 required_string("+b_glow:");
397 stuff_string(name, F_NAME, NULL);
398 if((l != &bogus_lightning) && !Fred_running){
399 l->glow = bm_load(name);
403 required_string("+b_bright:");
404 stuff_float(&l->b_bright);
406 // increment the # of bolt types
407 if(l != &bogus_lightning){
412 // copy the first bolt to the debug bolt
413 memcpy(&Bolt_types[DEBUG_BOLT], &Bolt_types[0], sizeof(bolt_type));
416 required_string("#Storms begin");
417 while(!optional_string("#Storms end")){
419 if(Num_storm_types >= MAX_STORM_TYPES){
422 s = &Storm_types[Num_storm_types];
426 required_string("$Storm:");
427 stuff_string(s->name, F_NAME, NULL);
430 s->num_bolt_types = 0;
431 while(optional_string("+bolt:")){
432 stuff_string(name, F_NAME, NULL);
435 if(s->num_bolt_types < MAX_BOLT_TYPES){
436 s->bolt_types[s->num_bolt_types] = (char)nebl_get_bolt_index(name);
437 Assert(s->bolt_types[s->num_bolt_types] != -1);
443 required_string("+bolt_prec:");
449 required_string("+flavor:");
450 stuff_float(&s->flavor.x);
451 stuff_float(&s->flavor.y);
452 stuff_float(&s->flavor.z);
455 required_string("+random_freq:");
460 required_string("+random_count:");
461 stuff_int(&s->min_count);
462 stuff_int(&s->max_count);
464 // increment the # of bolt types
465 if(s != &bogus_storm){
471 // initialize lightning before entering a level
472 void nebl_level_init()
476 // zero all lightning bolts
477 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
478 Nebl_bolts[idx].head = NULL;
479 Nebl_bolts[idx].bolt_life = -1;
480 Nebl_bolts[idx].used = 0;
483 // initialize node list
485 list_init( &Nebl_free_list );
486 list_init( &Nebl_used_list );
488 // Link all object slots into the free list
489 for (idx=0; idx<MAX_LIGHTNING_NODES; idx++) {
490 list_append(&Nebl_free_list, &Nebl_nodes[idx] );
493 // zero the random timestamp
496 // null the storm. let mission parsing set it up
500 // render all lightning bolts
501 void nebl_render_all()
507 // no lightning in non-nebula missions
508 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
512 // if we have no storm
518 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
519 b = &Nebl_bolts[idx];
521 // if this is being used
523 Assert(b->head != NULL);
530 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
535 bi = &Bolt_types[b->type];
537 // if this guy is still on a delay
539 if(timestamp_elapsed(b->delay)){
546 // if the timestamp on this guy has expired
547 if((b->bolt_life < 0) || timestamp_elapsed(b->bolt_life)){
548 // if this is a multiple strike bolt, jitter it and reset
549 if(b->strikes_left-1 > 0){
550 b->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
555 // by continuing here we skip rendering for one frame, which makes it look more like real lightning
558 // otherwise he's completely done, so release him
560 // maybe free up node data
562 nebl_release(b->head);
567 nprintf(("lightning", "Released bolt. %d used nodes!\n", Num_lnodes));
574 // pick some cool alpha values
575 Nebl_alpha = frand();
576 Nebl_glow_alpha = frand();
578 // otherwise render him
579 Nebl_flash_count = 0;
582 Nebl_bang = 10000000.0f;
583 nebl_render(bi, b->head, b->width);
585 // if this is the first frame he has been rendered, determine if we need to make a flash and sound effect
591 // if we rendered any points
592 if(Nebl_flash_count){
593 Nebl_flash_x /= (float)Nebl_flash_count;
594 Nebl_flash_y /= (float)Nebl_flash_count;
596 // quick distance from the center of the screen
597 float x = Nebl_flash_x - (gr_screen.max_w / 2.0f);
598 float y = Nebl_flash_y - (gr_screen.max_h / 2.0f);
599 float dist = fl_sqrt((x * x) + (y * y));
600 if(dist / (gr_screen.max_w / 2.0f) < 1.0f){
601 flash = 1.0f - (dist / (gr_screen.max_w / 2.0f));
603 // scale the flash by bolt type
604 flash *= bi->b_bright;
606 game_flash(flash, flash, flash);
609 // do some special stuff on the very first strike of the bolt
610 if(b->strikes_left == bi->num_strikes){
613 if(Nebl_bang < 40.0f){
615 } else if(Nebl_bang > 400.0f){
618 bang = 1.0f - (Nebl_bang / 400.0f);
620 if(frand_range(0.0f, 1.0f) < 0.5f){
621 snd_play(&Snds[SND_LIGHTNING_2], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
623 snd_play(&Snds[SND_LIGHTNING_1], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
627 if(bi->emp_intensity > 0.0f){
628 emp_apply(&b->midpoint, 0.0f, vm_vec_dist(&b->start, &b->strike), bi->emp_intensity, bi->emp_time);
637 // process lightning (randomly generate bolts, etc, etc);
642 // non-nebula mission
643 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
647 // non servers in multiplayer don't do this
648 if((Game_mode & GM_MULTIPLAYER) && !MULTIPLAYER_MASTER){
652 // if there's no chosen storm
657 // don't process lightning bolts unless we're a few seconds in
658 if(f2fl(Missiontime) < 3.0f){
663 if(Nebl_stamp == -1){
664 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
669 if(timestamp_elapsed(Nebl_stamp)){
670 // determine how many bolts to spew
671 num_bolts = (int)frand_range((float)Storm->min_count, (float)Storm->max_count);
672 for(idx=0; idx<num_bolts; idx++){
673 // hmm. for now just pick a random bolt type and run with it
677 s1 = (int)frand_range(0.0f, (float)Neb2_slices);
678 s2 = (int)frand_range(0.0f, (float)Neb2_slices);
679 s3 = (int)frand_range(0.0f, (float)Neb2_slices);
681 e1 = (int)frand_range(0.0f, (float)Neb2_slices);
682 e2 = (int)frand_range(0.0f, (float)Neb2_slices);
683 e3 = (int)frand_range(0.0f, (float)Neb2_slices);
685 // never choose the middle cube
686 if((s1 == 2) && (s2 == 2) && (s3 == 2)){
690 if((e1 == 2) && (e2 == 2) && (e3 == 2)){
696 } while((s1 == e1) && (s2 == e2) && (s3 == e3));
698 vector start = Neb2_cubes[s1][s2][s3].pt;
699 vector strike = Neb2_cubes[e1][e2][e3].pt;
701 // add some flavor to the bolt. mmmmmmmm, lightning
702 if(!IS_VEC_NULL(&Storm->flavor)){
703 // start with your basic hot sauce. measure how much you have
704 vector your_basic_hot_sauce;
705 vm_vec_sub(&your_basic_hot_sauce, &strike, &start);
706 float how_much_hot_sauce = vm_vec_normalize(&your_basic_hot_sauce);
708 // now figure out how much of that good wing sauce to add
709 vector wing_sauce = Storm->flavor;
710 if(frand_range(0.0, 1.0f) < 0.5f){
711 vm_vec_scale(&wing_sauce, -1.0f);
713 float how_much_of_that_good_wing_sauce_to_add = vm_vec_normalize(&wing_sauce);
715 // mix the two together, taking care not to add too much
717 if(how_much_of_that_good_wing_sauce_to_add > 1000.0f){
718 how_much_of_that_good_wing_sauce_to_add = 1000.0f;
720 vm_vec_interp_constant(&the_mixture, &your_basic_hot_sauce, &wing_sauce, how_much_of_that_good_wing_sauce_to_add / 1000.0f);
722 // take the final sauce and store it in the proper container
723 vm_vec_scale(&the_mixture, how_much_hot_sauce);
725 // make sure to put it on everything! whee!
726 vm_vec_add(&strike, &start, &the_mixture);
729 int type = (int)frand_range(0.0f, (float)(Storm->num_bolt_types-1));
730 nebl_bolt(Storm->bolt_types[type], &start, &strike);
733 // reset the timestamp
734 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
738 // create a lightning bolt
739 void nebl_bolt(int type, vector *start, vector *strike)
749 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
755 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
756 if(!Nebl_bolts[idx].used){
766 if((type < 0) || ((type >= Num_bolt_types) && (type != DEBUG_BOLT)) ){
769 bi = &Bolt_types[type];
771 // get a pointer to the bolt
772 bolt = &Nebl_bolts[idx];
775 bolt->start = *start;
776 bolt->strike = *strike;
777 bolt->strikes_left = bi->num_strikes;
779 bolt->type = (char)type;
780 bolt->first_frame = 1;
781 bolt->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
783 Nebl_bolt_start = *start;
784 Nebl_bolt_strike = *strike;
787 if(bolt->delay != -1){
788 bolt->delay = timestamp(bolt->delay);
791 // setup the rest of the important bolt data
792 if(vm_vec_same(&Nebl_bolt_start, &Nebl_bolt_strike)){
793 Nebl_bolt_strike.z += 150.0f;
795 Nebl_bolt_len = vm_vec_dist(&Nebl_bolt_start, &Nebl_bolt_strike);
796 vm_vec_sub(&dir, &Nebl_bolt_strike, &Nebl_bolt_start);
799 vm_vec_scale_add(&bolt->midpoint, &Nebl_bolt_start, &dir, 0.5f);
801 bolt_len = vm_vec_normalize(&dir);
802 vm_vector_2_matrix(&Nebl_bolt_dir, &dir, NULL, NULL);
804 // global type for generating the bolt
807 // try and make the bolt
808 if(!nebl_gen(&Nebl_bolt_start, &Nebl_bolt_strike, 0, 4, 0, &bolt->head, &tail)){
809 if(bolt->head != NULL){
810 nebl_release(bolt->head);
818 // setup the rest of the data
820 bolt->width = bi->b_poly_pct * bolt_len;
822 // if i'm a multiplayer master, send a bolt packet
823 if(MULTIPLAYER_MASTER){
824 send_lightning_packet(type, start, strike);
828 // get the current # of active lightning bolts
829 int nebl_get_active_bolts()
831 return Nebl_bolt_count;
834 // get the current # of active nodes
835 int nebl_get_active_nodes()
840 // set the storm (call from mission parse)
841 void nebl_set_storm(char *name)
843 int index = nebl_get_storm_index(name);
847 if((index >= 0) && (index < Num_storm_types)){
848 Storm = &Storm_types[index];
852 // ------------------------------------------------------------------------------------------------------
853 // NEBULA LIGHTNING FORWARD DEFINITIONS
856 // "new" a lightning node
861 // if we're out of nodes
862 if(Num_lnodes >= MAX_LIGHTNING_NODES){
864 nprintf(("lightning", "Out of lightning nodes!\n"));
868 // get a new node off the freelist
869 lp = GET_FIRST(&Nebl_free_list);
870 Assert( lp != &Nebl_free_list ); // shouldn't have the dummy element
872 // remove trailp from the free list
873 list_remove( &Nebl_free_list, lp );
875 // insert trailp onto the end of used list
876 list_append( &Nebl_used_list, lp );
885 // return the pointer
889 // "delete" a lightning node
890 void nebl_delete(l_node *lp)
892 // remove objp from the used list
893 list_remove( &Nebl_used_list, lp );
895 // add objp to the end of the free
896 list_append( &Nebl_free_list, lp );
902 // free a lightning bolt
903 void nebl_release(l_node *whee)
910 // release all of our children
911 if(whee->links[LINK_RIGHT] != NULL){
912 nebl_release(whee->links[LINK_RIGHT]);
914 if(whee->links[LINK_CHILD] != NULL){
915 nebl_release(whee->links[LINK_CHILD]);
922 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right)
924 l_node *child_node = NULL;
925 float d = vm_vec_dist_quick( left, right );
927 // if we've reached the critical point
928 if ( d < 0.30f || (depth > max_depth) ){
930 l_node *new_left = nebl_new();
931 if(new_left == NULL){
934 new_left->links[0] = NULL; new_left->links[1] = NULL; new_left->links[2] = NULL;
935 new_left->pos = vmd_zero_vector;
936 l_node *new_right = nebl_new();
937 if(new_right == NULL){
938 nebl_delete(new_left);
941 new_right->links[0] = NULL; new_right->links[1] = NULL; new_right->links[2] = NULL;
942 new_right->pos = vmd_zero_vector;
945 new_left->pos = *left;
946 new_left->links[LINK_RIGHT] = new_right;
950 new_right->pos = *right;
951 new_right->links[LINK_LEFT] = new_left;
952 *l_right = new_right;
960 vm_vec_avg( &tmp, left, right );
962 // sometimes generate children
963 if(!child && (frand() <= Nebl_type->b_rand)){
964 // get a point on the plane of the strike
966 vm_vec_random_in_circle(&tmp2, &Nebl_bolt_strike, &Nebl_bolt_dir, Nebl_bolt_len * Nebl_type->b_scale, 0);
968 // maybe move away from the plane
970 vm_vec_sub(&dir, &tmp2, &tmp);
971 vm_vec_scale_add(&tmp2, &tmp, &dir, Nebl_type->b_shrink);
975 if(!nebl_gen(&tmp, &tmp2, 0, 2, 1, &child_node, &argh)){
976 if(child_node != NULL){
977 nebl_release(child_node);
983 float scaler = 0.30f;
984 tmp.x += (frand()-0.5f)*d*scaler;
985 tmp.y += (frand()-0.5f)*d*scaler;
986 tmp.z += (frand()-0.5f)*d*scaler;
988 // generate left half
991 if(!nebl_gen( left, &tmp, depth+1, max_depth, child, &ll, &lr )){
992 if(child_node != NULL){
993 nebl_release(child_node);
1001 // generate right half
1004 if(!nebl_gen( &tmp, right, depth+1, max_depth, child, &rl, &rr )){
1005 if(child_node != NULL){
1006 nebl_release(child_node);
1017 // splice the two together
1018 lr->links[LINK_RIGHT] = rl->links[LINK_RIGHT];
1019 lr->links[LINK_RIGHT]->links[LINK_LEFT] = lr;
1022 // if we generated a child, stick him on
1023 if(child_node != NULL){
1024 lr->links[LINK_CHILD] = child_node;
1035 // output top and bottom vectors
1036 // fvec == forward vector (eye viewpoint basically. in world coords)
1037 // pos == world coordinate of the point we're calculating "around"
1038 // w == width of the diff between top and bottom around pos
1039 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1046 vm_vec_sub( &rvec, &Eye_position, &temp );
1047 vm_vec_normalize( &rvec );
1049 vm_vec_crossprod(&uvec,fvec,&rvec);
1050 vm_vec_normalize(&uvec);
1052 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1053 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1055 vm_vec_scale_add2( top, &rvec, z_add );
1056 vm_vec_scale_add2( bot, &rvec, z_add );
1059 // render a section of the bolt
1060 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b)
1063 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
1066 // Sets mode. Returns previous mode.
1067 gr_zbuffer_set(GR_ZBUFF_FULL);
1070 for(idx=0; idx<2; idx++){
1072 v[0].u = 0.0f; v[0].v = 0.0f;
1074 v[1] = a->vex[idx+1];
1075 v[1].u = 1.0f; v[1].v = 0.0f;
1077 v[2] = b->vex[idx+1];
1078 v[2].u = 1.0f; v[2].v = 1.0f;
1081 v[3].u = 0.0f; v[3].v = 1.0f;
1084 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha);
1085 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1090 v[0].u = 0.0f; v[0].v = 0.0f;
1093 v[1].u = 1.0f; v[1].v = 0.0f;
1096 v[2].u = 1.0f; v[2].v = 1.0f;
1099 v[3].u = 0.0f; v[3].v = 1.0f;
1101 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha);
1102 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1104 // draw the glow beam
1105 verts[0] = &a->glow_vex[0];
1106 verts[0]->v = 0.0f; verts[0]->u = 0.0f;
1108 verts[1] = &a->glow_vex[1];
1109 verts[1]->v = 1.0f; verts[1]->u = 0.0f;
1111 verts[2] = &b->glow_vex[1];
1112 verts[2]->v = 1.0f; verts[2]->u = 1.0f;
1114 verts[3] = &b->glow_vex[0];
1115 verts[3]->v = 0.0f; verts[3]->u = 1.0f;
1117 gr_set_bitmap(bi->glow, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_glow_alpha);
1118 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1121 // generate a section
1122 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)
1129 vector glow_a, glow_b;
1132 vm_vec_sub(&dir, &a->pos, &b->pos);
1133 vm_vec_copy_normalize(&dir_normal, &dir);
1134 vm_vector_2_matrix(&m, &dir_normal, NULL, NULL);
1136 // distance to player
1137 float bang_dist = vm_vec_dist_quick(&Eye_position, &a->pos);
1138 if(bang_dist < Nebl_bang){
1139 Nebl_bang = bang_dist;
1142 // rotate the basic section into world
1143 for(idx=0; idx<3; idx++){
1146 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1148 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1149 vm_vec_rotate(&pt, &temp, &m);
1151 vm_vec_add2(&pt, &a->pos);
1154 g3_rotate_vertex(&c->vex[idx], &pt);
1155 g3_project_vertex(&c->vex[idx]);
1157 // if first frame, keep track of the average screen pos
1158 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)){
1159 Nebl_flash_x += c->vex[idx].sx;
1160 Nebl_flash_y += c->vex[idx].sy;
1164 // calculate the glow points
1165 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &a->pos, pinch_a ? 0.5f : width * 6.0f, Nebl_type->b_add);
1166 g3_rotate_vertex(&c->glow_vex[0], &glow_a);
1167 g3_project_vertex(&c->glow_vex[0]);
1168 g3_rotate_vertex(&c->glow_vex[1], &glow_b);
1169 g3_project_vertex(&c->glow_vex[1]);
1173 // rotate the basic section into world
1174 for(idx=0; idx<3; idx++){
1177 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1179 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1180 vm_vec_rotate(&pt, &temp, &m);
1182 vm_vec_add2(&pt, &b->pos);
1185 g3_rotate_vertex(&cap->vex[idx], &pt);
1186 g3_project_vertex(&cap->vex[idx]);
1188 // if first frame, keep track of the average screen pos
1189 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)){
1190 Nebl_flash_x += c->vex[idx].sx;
1191 Nebl_flash_y += c->vex[idx].sy;
1196 // calculate the glow points
1197 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &b->pos, pinch_b ? 0.5f : width * 6.0f, bi->b_add);
1198 g3_rotate_vertex(&cap->glow_vex[0], &glow_a);
1199 g3_project_vertex(&cap->glow_vex[0]);
1200 g3_rotate_vertex(&cap->glow_vex[1], &glow_b);
1201 g3_project_vertex(&cap->glow_vex[1]);
1206 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev)
1210 l_section child_start;
1217 // if prev is NULL, we're just starting so we need our start point
1219 Assert(whee->links[LINK_RIGHT] != NULL);
1220 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &start, NULL, 1, 0);
1225 // if we have a child section
1226 if(whee->links[LINK_CHILD]){
1228 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);
1231 nebl_render_section(bi, &child_start, &end);
1234 if(whee->links[LINK_CHILD]->links[LINK_RIGHT] != NULL){
1235 nebl_render(bi, whee->links[LINK_CHILD], width * 0.5f, &end);
1239 // if the next section is an end section
1240 if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] == NULL){
1244 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &temp, &end, 0, 1);
1246 // render the section
1247 nebl_render_section(bi, &start, &end);
1250 else if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] != NULL){
1252 nebl_generate_section(bi, width, whee->links[LINK_RIGHT], whee->links[LINK_RIGHT]->links[LINK_RIGHT], &end, NULL, 0, 0);
1254 // render the section
1255 nebl_render_section(bi, &start, &end);
1257 // recurse through him
1258 nebl_render(bi, whee->links[LINK_RIGHT], width, &end);
1262 // given a valid, complete bolt, jitter him based upon his noise
1263 void nebl_jitter(l_bolt *b)
1269 bolt_type *bi = NULL;
1275 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
1278 bi = &Bolt_types[b->type];
1280 // get the bolt direction
1281 vm_vec_sub(&temp, &b->strike, &b->start);
1282 length = vm_vec_normalize_quick(&temp);
1283 vm_vector_2_matrix(&m, &temp, NULL, NULL);
1285 // jitter all nodes on the main trunk
1287 while(moveup != NULL){
1289 vm_vec_random_in_circle(&moveup->pos, &temp, &m, frand_range(0.0f, length * bi->noise), 0);
1291 // just on the main trunk
1292 moveup = moveup->links[LINK_RIGHT];
1296 // return the index of a given bolt type by name
1297 int nebl_get_bolt_index(char *name)
1301 for(idx=0; idx<Num_bolt_types; idx++){
1302 if(!strcmp(name, Bolt_types[idx].name)){
1310 // return the index of a given storm type by name
1311 int nebl_get_storm_index(char *name)
1315 for(idx=0; idx<Num_bolt_types; idx++){
1316 if(!strcmp(name, Storm_types[idx].name)){