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.4 2002/06/17 06:33:09 relnev
19 * ryan's struct patch for gcc 2.95
21 * Revision 1.3 2002/06/09 04:41:23 relnev
22 * added copyright header
24 * Revision 1.2 2002/05/07 03:16:47 theoddone33
25 * The Great Newline Fix
27 * Revision 1.1.1.1 2002/05/03 03:28:10 root
31 * 10 8/15/99 3:50p Dave
32 * Don't process lightning at the very beginning of a mission.
34 * 9 8/12/99 10:38a Anoop
35 * Removed unnecessary Int3().
37 * 8 8/05/99 2:06a Dave
40 * 7 7/27/99 9:51p Andsager
41 * make mprintf's into nprintf's
43 * 6 7/03/99 5:50p Dave
44 * Make rotated bitmaps draw properly in padlock views.
46 * 5 7/02/99 4:31p Dave
47 * Much more sophisticated lightning support.
49 * 4 6/09/99 10:32a Dave
50 * Made random lighting bolts behave more like the E3 demo. Generally more
53 * 3 5/26/99 11:46a Dave
54 * Added ship-blasting lighting and made the randomization of lighting
55 * much more customizable.
57 * 2 5/24/99 5:45p Dave
58 * Added detail levels to the nebula, with a decent speedup. Split nebula
59 * lightning into its own section.
68 #include "freespace.h"
71 #include "missionparse.h"
73 #include "neblightning.h"
76 #include "multimsgs.h"
78 // ------------------------------------------------------------------------------------------------------
79 // NEBULA LIGHTNING DEFINES/VARS
83 #define MAX_BOLT_TYPES_INTERNAL 11
85 // see lightning.tbl for explanations of these values
86 typedef struct bolt_type {
87 char name[NAME_LENGTH];
108 int Num_bolt_types = 0;
109 bolt_type Bolt_types[MAX_BOLT_TYPES_INTERNAL];
112 int Num_storm_types = 0;
113 storm_type Storm_types[MAX_STORM_TYPES];
116 // actual lightning bolt stuff -------
118 #define MAX_LIGHTNING_NODES 500
120 // nodes in a lightning bolt
124 typedef struct l_node {
125 vector pos; // world position
126 l_node *links[3]; // 3 links for lightning children
128 l_node *next, *prev; // for used and free-lists only
133 l_node Nebl_nodes[MAX_LIGHTNING_NODES];
136 // lightning node lists
137 l_node Nebl_free_list;
138 l_node Nebl_used_list;
140 // actual lightning bolt themselves
141 typedef struct l_bolt {
142 l_node *head; // head of the lightning bolt
143 int bolt_life; // remaining life timestamp
144 ubyte used; // used or not
145 ubyte first_frame; // if he hasn't been rendered at least once
149 vector start, strike, midpoint;
150 int delay; // delay stamp
151 int strikes_left; // #of strikes left
155 #define MAX_LIGHTNING_BOLTS 10
158 l_bolt Nebl_bolts[MAX_LIGHTNING_BOLTS];
159 int Nebl_bolt_count = 0;
161 // one cross-section of a lightning bolt
162 typedef struct l_section {
167 // points on the basic cross section
168 vector Nebl_ring[3] = {
169 { -1.0f, 0.0f, 0.0f },
170 { 1.0f, 0.70f, 0.0f },
171 { 1.0f, -0.70f, 0.0f }
174 // pinched off cross-section
175 vector Nebl_ring_pinched[3] = {
176 { -0.05f, 0.0f, 0.0f },
177 { 0.05f, 0.035f, 0.0f },
178 { 0.05f, -0.035f, 0.0f }
181 // globals used for rendering and generating bolts
182 int Nebl_flash_count = 0; // # of points rendered onscreen for this bolt
183 float Nebl_flash_x = 0.0f; // avg x of the points rendered
184 float Nebl_flash_y = 0.0f; // avg y of the points rendered
185 float Nebl_bang = 0.0; // distance to the viewer object
186 float Nebl_alpha = 0.0f; // alpha to use when rendering the bolt itself
187 float Nebl_glow_alpha = 0.0f; // alpha to use when rendering the bolt glow
188 int Nebl_stamp = -1; // random timestamp for making bolts
189 float Nebl_bolt_len; // length of the current bolt being generated
190 bolt_type *Nebl_type; // bolt type
191 matrix Nebl_bolt_dir; // orientation matrix of the bolt being generated
192 vector Nebl_bolt_start; // start point of the bolt being generated
193 vector Nebl_bolt_strike; // strike point of the bolt being generated
195 // the type of active storm
196 storm_type *Storm = NULL;
201 dc_get_arg(ARG_FLOAT);
202 Bolt_types[DEBUG_BOLT].b_scale = Dc_arg_float;
206 dc_get_arg(ARG_FLOAT);
207 Bolt_types[DEBUG_BOLT].b_rand = Dc_arg_float;
211 dc_get_arg(ARG_FLOAT);
212 Bolt_types[DEBUG_BOLT].b_shrink = Dc_arg_float;
216 dc_get_arg(ARG_FLOAT);
217 Bolt_types[DEBUG_BOLT].b_poly_pct = Dc_arg_float;
221 dc_get_arg(ARG_FLOAT);
222 Bolt_types[DEBUG_BOLT].b_add = Dc_arg_float;
227 Bolt_types[DEBUG_BOLT].num_strikes = Dc_arg_int;
231 dc_get_arg(ARG_FLOAT);
232 Bolt_types[DEBUG_BOLT].noise = Dc_arg_float;
236 dc_get_arg(ARG_FLOAT);
237 Bolt_types[DEBUG_BOLT].b_bright = Dc_arg_float;
242 Bolt_types[DEBUG_BOLT].lifetime = Dc_arg_int;
246 dc_printf("Debug lightning bolt settings :\n");
248 dc_printf("b_scale : %f\n", Bolt_types[DEBUG_BOLT].b_scale);
249 dc_printf("b_rand : %f\n", Bolt_types[DEBUG_BOLT].b_rand);
250 dc_printf("b_shrink : %f\n", Bolt_types[DEBUG_BOLT].b_shrink);
251 dc_printf("b_poly_pct : %f\n", Bolt_types[DEBUG_BOLT].b_poly_pct);
252 dc_printf("b_add : %f\n", Bolt_types[DEBUG_BOLT].b_add);
253 dc_printf("b_strikes : %d\n", Bolt_types[DEBUG_BOLT].num_strikes);
254 dc_printf("b_noise : %f\n", Bolt_types[DEBUG_BOLT].noise);
255 dc_printf("b_bright : %f\n", Bolt_types[DEBUG_BOLT].b_bright);
256 dc_printf("b_lifetime : %d\n", Bolt_types[DEBUG_BOLT].lifetime);
260 // nebula lightning intensity (0.0 to 1.0)
261 float Nebl_intensity = 0.6667f;
263 // min and max times for random lightning
264 int Nebl_random_min = 750; // min random time
265 int Nebl_random_max = 10000; // max random time
267 // min and max times for cruiser lightning
268 int Nebl_cruiser_min = 5000; // min cruiser time
269 int Nebl_cruiser_max = 25000; // max cruiser time
271 // min and max times for cap ships
272 int Nebl_cap_min = 4000; // min cap time
273 int Nebl_cap_max = 18000; // max cap time
275 // min and max time for super caps
276 int Nebl_supercap_min = 3000; // min supercap time
277 int Nebl_supercap_max = 12000; // max supercap time
279 DCF(lightning_intensity, "")
281 dc_get_arg(ARG_FLOAT);
282 float val = Dc_arg_float;
285 } else if(val > 1.0f){
289 Nebl_intensity = 1.0f - val;
292 // ------------------------------------------------------------------------------------------------------
293 // NEBULA LIGHTNING FORWARD DECLARATIONS
296 // "new" a lightning node
299 // "delete" a lightning node
300 void nebl_delete(l_node *lp);
302 // free up a the nodes of the passed in bolt
303 void nebl_release(l_node *bolt_head);
305 // generate a lightning bolt, returns l_left (the "head") and l_right (the "tail")
306 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right);
308 // output top and bottom vectors
309 // fvec == forward vector (eye viewpoint basically. in world coords)
310 // pos == world coordinate of the point we're calculating "around"
311 // w == width of the diff between top and bottom around pos
312 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add );
314 // render a section of the bolt
315 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b);
317 // generate a section
318 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);
321 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev = NULL);
323 // given a valid, complete bolt, jitter him based upon his noise
324 void nebl_jitter(l_bolt *b);
326 // return the index of a given bolt type by name
327 int nebl_get_bolt_index(char *name);
329 // return the index of a given storm type by name
330 int nebl_get_storm_index(char *name);
333 // ------------------------------------------------------------------------------------------------------
334 // NEBULA LIGHTNING FUNCTIONS
337 // initialize nebula lightning at game startup
340 char name[NAME_LENGTH+10] = "";
341 bolt_type bogus_lightning, *l;
342 storm_type bogus_storm, *s;
345 // parse the lightning table
346 read_file_text("lightning.tbl");
352 memset(Bolt_types, 0, sizeof(bolt_type) * MAX_BOLT_TYPES_INTERNAL);
354 // parse the individual lightning bolt types
355 required_string("#Bolts begin");
356 while(!optional_string("#Bolts end")){
358 if(Num_bolt_types >= MAX_BOLT_TYPES){
359 l = &bogus_lightning;
361 l = &Bolt_types[Num_bolt_types];
365 required_string("$Bolt:");
366 stuff_string(l->name, F_NAME, NULL);
369 required_string("+b_scale:");
370 stuff_float(&l->b_scale);
373 required_string("+b_shrink:");
374 stuff_float(&l->b_shrink);
377 required_string("+b_poly_pct:");
378 stuff_float(&l->b_poly_pct);
381 required_string("+b_rand:");
382 stuff_float(&l->b_rand);
385 required_string("+b_add:");
386 stuff_float(&l->b_add);
389 required_string("+b_strikes:");
390 stuff_int(&l->num_strikes);
393 required_string("+b_lifetime:");
394 stuff_int(&l->lifetime);
397 required_string("+b_noise:");
398 stuff_float(&l->noise);
401 required_string("+b_emp:");
402 stuff_float(&l->emp_intensity);
403 stuff_float(&l->emp_time);
406 required_string("+b_texture:");
407 stuff_string(name, F_NAME, NULL);
408 if((l != &bogus_lightning) && !Fred_running){
409 l->texture = bm_load(name);
413 required_string("+b_glow:");
414 stuff_string(name, F_NAME, NULL);
415 if((l != &bogus_lightning) && !Fred_running){
416 l->glow = bm_load(name);
420 required_string("+b_bright:");
421 stuff_float(&l->b_bright);
423 // increment the # of bolt types
424 if(l != &bogus_lightning){
429 // copy the first bolt to the debug bolt
430 memcpy(&Bolt_types[DEBUG_BOLT], &Bolt_types[0], sizeof(bolt_type));
433 required_string("#Storms begin");
434 while(!optional_string("#Storms end")){
436 if(Num_storm_types >= MAX_STORM_TYPES){
439 s = &Storm_types[Num_storm_types];
443 required_string("$Storm:");
444 stuff_string(s->name, F_NAME, NULL);
447 s->num_bolt_types = 0;
448 while(optional_string("+bolt:")){
449 stuff_string(name, F_NAME, NULL);
452 if(s->num_bolt_types < MAX_BOLT_TYPES){
453 s->bolt_types[s->num_bolt_types] = (char)nebl_get_bolt_index(name);
454 Assert(s->bolt_types[s->num_bolt_types] != -1);
460 required_string("+bolt_prec:");
466 required_string("+flavor:");
467 stuff_float(&s->flavor.xyz.x);
468 stuff_float(&s->flavor.xyz.y);
469 stuff_float(&s->flavor.xyz.z);
472 required_string("+random_freq:");
477 required_string("+random_count:");
478 stuff_int(&s->min_count);
479 stuff_int(&s->max_count);
481 // increment the # of bolt types
482 if(s != &bogus_storm){
488 // initialize lightning before entering a level
489 void nebl_level_init()
493 // zero all lightning bolts
494 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
495 Nebl_bolts[idx].head = NULL;
496 Nebl_bolts[idx].bolt_life = -1;
497 Nebl_bolts[idx].used = 0;
500 // initialize node list
502 list_init( &Nebl_free_list );
503 list_init( &Nebl_used_list );
505 // Link all object slots into the free list
506 for (idx=0; idx<MAX_LIGHTNING_NODES; idx++) {
507 list_append(&Nebl_free_list, &Nebl_nodes[idx] );
510 // zero the random timestamp
513 // null the storm. let mission parsing set it up
517 // render all lightning bolts
518 void nebl_render_all()
524 // no lightning in non-nebula missions
525 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
529 // if we have no storm
535 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
536 b = &Nebl_bolts[idx];
538 // if this is being used
540 Assert(b->head != NULL);
547 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
552 bi = &Bolt_types[b->type];
554 // if this guy is still on a delay
556 if(timestamp_elapsed(b->delay)){
563 // if the timestamp on this guy has expired
564 if((b->bolt_life < 0) || timestamp_elapsed(b->bolt_life)){
565 // if this is a multiple strike bolt, jitter it and reset
566 if(b->strikes_left-1 > 0){
567 b->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
572 // by continuing here we skip rendering for one frame, which makes it look more like real lightning
575 // otherwise he's completely done, so release him
577 // maybe free up node data
579 nebl_release(b->head);
584 nprintf(("lightning", "Released bolt. %d used nodes!\n", Num_lnodes));
591 // pick some cool alpha values
592 Nebl_alpha = frand();
593 Nebl_glow_alpha = frand();
595 // otherwise render him
596 Nebl_flash_count = 0;
599 Nebl_bang = 10000000.0f;
600 nebl_render(bi, b->head, b->width);
602 // if this is the first frame he has been rendered, determine if we need to make a flash and sound effect
608 // if we rendered any points
609 if(Nebl_flash_count){
610 Nebl_flash_x /= (float)Nebl_flash_count;
611 Nebl_flash_y /= (float)Nebl_flash_count;
613 // quick distance from the center of the screen
614 float x = Nebl_flash_x - (gr_screen.max_w / 2.0f);
615 float y = Nebl_flash_y - (gr_screen.max_h / 2.0f);
616 float dist = fl_sqrt((x * x) + (y * y));
617 if(dist / (gr_screen.max_w / 2.0f) < 1.0f){
618 flash = 1.0f - (dist / (gr_screen.max_w / 2.0f));
620 // scale the flash by bolt type
621 flash *= bi->b_bright;
623 game_flash(flash, flash, flash);
626 // do some special stuff on the very first strike of the bolt
627 if(b->strikes_left == bi->num_strikes){
630 if(Nebl_bang < 40.0f){
632 } else if(Nebl_bang > 400.0f){
635 bang = 1.0f - (Nebl_bang / 400.0f);
637 if(frand_range(0.0f, 1.0f) < 0.5f){
638 snd_play(&Snds[SND_LIGHTNING_2], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
640 snd_play(&Snds[SND_LIGHTNING_1], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
644 if(bi->emp_intensity > 0.0f){
645 emp_apply(&b->midpoint, 0.0f, vm_vec_dist(&b->start, &b->strike), bi->emp_intensity, bi->emp_time);
654 // process lightning (randomly generate bolts, etc, etc);
659 // non-nebula mission
660 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
664 // non servers in multiplayer don't do this
665 if((Game_mode & GM_MULTIPLAYER) && !MULTIPLAYER_MASTER){
669 // if there's no chosen storm
674 // don't process lightning bolts unless we're a few seconds in
675 if(f2fl(Missiontime) < 3.0f){
680 if(Nebl_stamp == -1){
681 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
686 if(timestamp_elapsed(Nebl_stamp)){
687 // determine how many bolts to spew
688 num_bolts = (int)frand_range((float)Storm->min_count, (float)Storm->max_count);
689 for(idx=0; idx<num_bolts; idx++){
690 // hmm. for now just pick a random bolt type and run with it
694 s1 = (int)frand_range(0.0f, (float)Neb2_slices);
695 s2 = (int)frand_range(0.0f, (float)Neb2_slices);
696 s3 = (int)frand_range(0.0f, (float)Neb2_slices);
698 e1 = (int)frand_range(0.0f, (float)Neb2_slices);
699 e2 = (int)frand_range(0.0f, (float)Neb2_slices);
700 e3 = (int)frand_range(0.0f, (float)Neb2_slices);
702 // never choose the middle cube
703 if((s1 == 2) && (s2 == 2) && (s3 == 2)){
707 if((e1 == 2) && (e2 == 2) && (e3 == 2)){
713 } while((s1 == e1) && (s2 == e2) && (s3 == e3));
715 vector start = Neb2_cubes[s1][s2][s3].pt;
716 vector strike = Neb2_cubes[e1][e2][e3].pt;
718 // add some flavor to the bolt. mmmmmmmm, lightning
719 if(!IS_VEC_NULL(&Storm->flavor)){
720 // start with your basic hot sauce. measure how much you have
721 vector your_basic_hot_sauce;
722 vm_vec_sub(&your_basic_hot_sauce, &strike, &start);
723 float how_much_hot_sauce = vm_vec_normalize(&your_basic_hot_sauce);
725 // now figure out how much of that good wing sauce to add
726 vector wing_sauce = Storm->flavor;
727 if(frand_range(0.0, 1.0f) < 0.5f){
728 vm_vec_scale(&wing_sauce, -1.0f);
730 float how_much_of_that_good_wing_sauce_to_add = vm_vec_normalize(&wing_sauce);
732 // mix the two together, taking care not to add too much
734 if(how_much_of_that_good_wing_sauce_to_add > 1000.0f){
735 how_much_of_that_good_wing_sauce_to_add = 1000.0f;
737 vm_vec_interp_constant(&the_mixture, &your_basic_hot_sauce, &wing_sauce, how_much_of_that_good_wing_sauce_to_add / 1000.0f);
739 // take the final sauce and store it in the proper container
740 vm_vec_scale(&the_mixture, how_much_hot_sauce);
742 // make sure to put it on everything! whee!
743 vm_vec_add(&strike, &start, &the_mixture);
746 int type = (int)frand_range(0.0f, (float)(Storm->num_bolt_types-1));
747 nebl_bolt(Storm->bolt_types[type], &start, &strike);
750 // reset the timestamp
751 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
755 // create a lightning bolt
756 void nebl_bolt(int type, vector *start, vector *strike)
766 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
772 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
773 if(!Nebl_bolts[idx].used){
783 if((type < 0) || ((type >= Num_bolt_types) && (type != DEBUG_BOLT)) ){
786 bi = &Bolt_types[type];
788 // get a pointer to the bolt
789 bolt = &Nebl_bolts[idx];
792 bolt->start = *start;
793 bolt->strike = *strike;
794 bolt->strikes_left = bi->num_strikes;
796 bolt->type = (char)type;
797 bolt->first_frame = 1;
798 bolt->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
800 Nebl_bolt_start = *start;
801 Nebl_bolt_strike = *strike;
804 if(bolt->delay != -1){
805 bolt->delay = timestamp(bolt->delay);
808 // setup the rest of the important bolt data
809 if(vm_vec_same(&Nebl_bolt_start, &Nebl_bolt_strike)){
810 Nebl_bolt_strike.xyz.z += 150.0f;
812 Nebl_bolt_len = vm_vec_dist(&Nebl_bolt_start, &Nebl_bolt_strike);
813 vm_vec_sub(&dir, &Nebl_bolt_strike, &Nebl_bolt_start);
816 vm_vec_scale_add(&bolt->midpoint, &Nebl_bolt_start, &dir, 0.5f);
818 bolt_len = vm_vec_normalize(&dir);
819 vm_vector_2_matrix(&Nebl_bolt_dir, &dir, NULL, NULL);
821 // global type for generating the bolt
824 // try and make the bolt
825 if(!nebl_gen(&Nebl_bolt_start, &Nebl_bolt_strike, 0, 4, 0, &bolt->head, &tail)){
826 if(bolt->head != NULL){
827 nebl_release(bolt->head);
835 // setup the rest of the data
837 bolt->width = bi->b_poly_pct * bolt_len;
839 // if i'm a multiplayer master, send a bolt packet
840 if(MULTIPLAYER_MASTER){
841 send_lightning_packet(type, start, strike);
845 // get the current # of active lightning bolts
846 int nebl_get_active_bolts()
848 return Nebl_bolt_count;
851 // get the current # of active nodes
852 int nebl_get_active_nodes()
857 // set the storm (call from mission parse)
858 void nebl_set_storm(char *name)
860 int index = nebl_get_storm_index(name);
864 if((index >= 0) && (index < Num_storm_types)){
865 Storm = &Storm_types[index];
869 // ------------------------------------------------------------------------------------------------------
870 // NEBULA LIGHTNING FORWARD DEFINITIONS
873 // "new" a lightning node
878 // if we're out of nodes
879 if(Num_lnodes >= MAX_LIGHTNING_NODES){
881 nprintf(("lightning", "Out of lightning nodes!\n"));
885 // get a new node off the freelist
886 lp = GET_FIRST(&Nebl_free_list);
887 Assert( lp != &Nebl_free_list ); // shouldn't have the dummy element
889 // remove trailp from the free list
890 list_remove( &Nebl_free_list, lp );
892 // insert trailp onto the end of used list
893 list_append( &Nebl_used_list, lp );
902 // return the pointer
906 // "delete" a lightning node
907 void nebl_delete(l_node *lp)
909 // remove objp from the used list
910 list_remove( &Nebl_used_list, lp );
912 // add objp to the end of the free
913 list_append( &Nebl_free_list, lp );
919 // free a lightning bolt
920 void nebl_release(l_node *whee)
927 // release all of our children
928 if(whee->links[LINK_RIGHT] != NULL){
929 nebl_release(whee->links[LINK_RIGHT]);
931 if(whee->links[LINK_CHILD] != NULL){
932 nebl_release(whee->links[LINK_CHILD]);
939 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right)
941 l_node *child_node = NULL;
942 float d = vm_vec_dist_quick( left, right );
944 // if we've reached the critical point
945 if ( d < 0.30f || (depth > max_depth) ){
947 l_node *new_left = nebl_new();
948 if(new_left == NULL){
951 new_left->links[0] = NULL; new_left->links[1] = NULL; new_left->links[2] = NULL;
952 new_left->pos = vmd_zero_vector;
953 l_node *new_right = nebl_new();
954 if(new_right == NULL){
955 nebl_delete(new_left);
958 new_right->links[0] = NULL; new_right->links[1] = NULL; new_right->links[2] = NULL;
959 new_right->pos = vmd_zero_vector;
962 new_left->pos = *left;
963 new_left->links[LINK_RIGHT] = new_right;
967 new_right->pos = *right;
968 new_right->links[LINK_LEFT] = new_left;
969 *l_right = new_right;
977 vm_vec_avg( &tmp, left, right );
979 // sometimes generate children
980 if(!child && (frand() <= Nebl_type->b_rand)){
981 // get a point on the plane of the strike
983 vm_vec_random_in_circle(&tmp2, &Nebl_bolt_strike, &Nebl_bolt_dir, Nebl_bolt_len * Nebl_type->b_scale, 0);
985 // maybe move away from the plane
987 vm_vec_sub(&dir, &tmp2, &tmp);
988 vm_vec_scale_add(&tmp2, &tmp, &dir, Nebl_type->b_shrink);
992 if(!nebl_gen(&tmp, &tmp2, 0, 2, 1, &child_node, &argh)){
993 if(child_node != NULL){
994 nebl_release(child_node);
1000 float scaler = 0.30f;
1001 tmp.xyz.x += (frand()-0.5f)*d*scaler;
1002 tmp.xyz.y += (frand()-0.5f)*d*scaler;
1003 tmp.xyz.z += (frand()-0.5f)*d*scaler;
1005 // generate left half
1008 if(!nebl_gen( left, &tmp, depth+1, max_depth, child, &ll, &lr )){
1009 if(child_node != NULL){
1010 nebl_release(child_node);
1018 // generate right half
1021 if(!nebl_gen( &tmp, right, depth+1, max_depth, child, &rl, &rr )){
1022 if(child_node != NULL){
1023 nebl_release(child_node);
1034 // splice the two together
1035 lr->links[LINK_RIGHT] = rl->links[LINK_RIGHT];
1036 lr->links[LINK_RIGHT]->links[LINK_LEFT] = lr;
1039 // if we generated a child, stick him on
1040 if(child_node != NULL){
1041 lr->links[LINK_CHILD] = child_node;
1052 // output top and bottom vectors
1053 // fvec == forward vector (eye viewpoint basically. in world coords)
1054 // pos == world coordinate of the point we're calculating "around"
1055 // w == width of the diff between top and bottom around pos
1056 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1063 vm_vec_sub( &rvec, &Eye_position, &temp );
1064 vm_vec_normalize( &rvec );
1066 vm_vec_crossprod(&uvec,fvec,&rvec);
1067 vm_vec_normalize(&uvec);
1069 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1070 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1072 vm_vec_scale_add2( top, &rvec, z_add );
1073 vm_vec_scale_add2( bot, &rvec, z_add );
1076 // render a section of the bolt
1077 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b)
1080 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
1083 // Sets mode. Returns previous mode.
1084 gr_zbuffer_set(GR_ZBUFF_FULL);
1087 for(idx=0; idx<2; idx++){
1089 v[0].u = 0.0f; v[0].v = 0.0f;
1091 v[1] = a->vex[idx+1];
1092 v[1].u = 1.0f; v[1].v = 0.0f;
1094 v[2] = b->vex[idx+1];
1095 v[2].u = 1.0f; v[2].v = 1.0f;
1098 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);
1107 v[0].u = 0.0f; v[0].v = 0.0f;
1110 v[1].u = 1.0f; v[1].v = 0.0f;
1113 v[2].u = 1.0f; v[2].v = 1.0f;
1116 v[3].u = 0.0f; v[3].v = 1.0f;
1118 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha);
1119 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1121 // draw the glow beam
1122 verts[0] = &a->glow_vex[0];
1123 verts[0]->v = 0.0f; verts[0]->u = 0.0f;
1125 verts[1] = &a->glow_vex[1];
1126 verts[1]->v = 1.0f; verts[1]->u = 0.0f;
1128 verts[2] = &b->glow_vex[1];
1129 verts[2]->v = 1.0f; verts[2]->u = 1.0f;
1131 verts[3] = &b->glow_vex[0];
1132 verts[3]->v = 0.0f; verts[3]->u = 1.0f;
1134 gr_set_bitmap(bi->glow, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_glow_alpha);
1135 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1138 // generate a section
1139 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)
1146 vector glow_a, glow_b;
1149 vm_vec_sub(&dir, &a->pos, &b->pos);
1150 vm_vec_copy_normalize(&dir_normal, &dir);
1151 vm_vector_2_matrix(&m, &dir_normal, NULL, NULL);
1153 // distance to player
1154 float bang_dist = vm_vec_dist_quick(&Eye_position, &a->pos);
1155 if(bang_dist < Nebl_bang){
1156 Nebl_bang = bang_dist;
1159 // rotate the basic section into world
1160 for(idx=0; idx<3; idx++){
1163 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1165 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1166 vm_vec_rotate(&pt, &temp, &m);
1168 vm_vec_add2(&pt, &a->pos);
1171 g3_rotate_vertex(&c->vex[idx], &pt);
1172 g3_project_vertex(&c->vex[idx]);
1174 // if first frame, keep track of the average screen pos
1175 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)){
1176 Nebl_flash_x += c->vex[idx].sx;
1177 Nebl_flash_y += c->vex[idx].sy;
1181 // calculate the glow points
1182 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &a->pos, pinch_a ? 0.5f : width * 6.0f, Nebl_type->b_add);
1183 g3_rotate_vertex(&c->glow_vex[0], &glow_a);
1184 g3_project_vertex(&c->glow_vex[0]);
1185 g3_rotate_vertex(&c->glow_vex[1], &glow_b);
1186 g3_project_vertex(&c->glow_vex[1]);
1190 // rotate the basic section into world
1191 for(idx=0; idx<3; idx++){
1194 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1196 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1197 vm_vec_rotate(&pt, &temp, &m);
1199 vm_vec_add2(&pt, &b->pos);
1202 g3_rotate_vertex(&cap->vex[idx], &pt);
1203 g3_project_vertex(&cap->vex[idx]);
1205 // if first frame, keep track of the average screen pos
1206 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)){
1207 Nebl_flash_x += c->vex[idx].sx;
1208 Nebl_flash_y += c->vex[idx].sy;
1213 // calculate the glow points
1214 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &b->pos, pinch_b ? 0.5f : width * 6.0f, bi->b_add);
1215 g3_rotate_vertex(&cap->glow_vex[0], &glow_a);
1216 g3_project_vertex(&cap->glow_vex[0]);
1217 g3_rotate_vertex(&cap->glow_vex[1], &glow_b);
1218 g3_project_vertex(&cap->glow_vex[1]);
1223 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev)
1227 l_section child_start;
1234 // if prev is NULL, we're just starting so we need our start point
1236 Assert(whee->links[LINK_RIGHT] != NULL);
1237 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &start, NULL, 1, 0);
1242 // if we have a child section
1243 if(whee->links[LINK_CHILD]){
1245 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);
1248 nebl_render_section(bi, &child_start, &end);
1251 if(whee->links[LINK_CHILD]->links[LINK_RIGHT] != NULL){
1252 nebl_render(bi, whee->links[LINK_CHILD], width * 0.5f, &end);
1256 // if the next section is an end section
1257 if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] == NULL){
1261 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &temp, &end, 0, 1);
1263 // render the section
1264 nebl_render_section(bi, &start, &end);
1267 else if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] != NULL){
1269 nebl_generate_section(bi, width, whee->links[LINK_RIGHT], whee->links[LINK_RIGHT]->links[LINK_RIGHT], &end, NULL, 0, 0);
1271 // render the section
1272 nebl_render_section(bi, &start, &end);
1274 // recurse through him
1275 nebl_render(bi, whee->links[LINK_RIGHT], width, &end);
1279 // given a valid, complete bolt, jitter him based upon his noise
1280 void nebl_jitter(l_bolt *b)
1286 bolt_type *bi = NULL;
1292 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
1295 bi = &Bolt_types[b->type];
1297 // get the bolt direction
1298 vm_vec_sub(&temp, &b->strike, &b->start);
1299 length = vm_vec_normalize_quick(&temp);
1300 vm_vector_2_matrix(&m, &temp, NULL, NULL);
1302 // jitter all nodes on the main trunk
1304 while(moveup != NULL){
1306 vm_vec_random_in_circle(&moveup->pos, &temp, &m, frand_range(0.0f, length * bi->noise), 0);
1308 // just on the main trunk
1309 moveup = moveup->links[LINK_RIGHT];
1313 // return the index of a given bolt type by name
1314 int nebl_get_bolt_index(char *name)
1318 for(idx=0; idx<Num_bolt_types; idx++){
1319 if(!strcmp(name, Bolt_types[idx].name)){
1327 // return the index of a given storm type by name
1328 int nebl_get_storm_index(char *name)
1332 for(idx=0; idx<Num_bolt_types; idx++){
1333 if(!strcmp(name, Storm_types[idx].name)){