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.6 2004/07/04 11:39:06 taylor
19 * fix missing debrief text, crash on exit, path separator's, warning fixes, no GR_SOFT
21 * Revision 1.5 2003/05/25 02:30:43 taylor
24 * Revision 1.4 2002/06/17 06:33:09 relnev
25 * ryan's struct patch for gcc 2.95
27 * Revision 1.3 2002/06/09 04:41:23 relnev
28 * added copyright header
30 * Revision 1.2 2002/05/07 03:16:47 theoddone33
31 * The Great Newline Fix
33 * Revision 1.1.1.1 2002/05/03 03:28:10 root
37 * 10 8/15/99 3:50p Dave
38 * Don't process lightning at the very beginning of a mission.
40 * 9 8/12/99 10:38a Anoop
41 * Removed unnecessary Int3().
43 * 8 8/05/99 2:06a Dave
46 * 7 7/27/99 9:51p Andsager
47 * make mprintf's into nprintf's
49 * 6 7/03/99 5:50p Dave
50 * Make rotated bitmaps draw properly in padlock views.
52 * 5 7/02/99 4:31p Dave
53 * Much more sophisticated lightning support.
55 * 4 6/09/99 10:32a Dave
56 * Made random lighting bolts behave more like the E3 demo. Generally more
59 * 3 5/26/99 11:46a Dave
60 * Added ship-blasting lighting and made the randomization of lighting
61 * much more customizable.
63 * 2 5/24/99 5:45p Dave
64 * Added detail levels to the nebula, with a decent speedup. Split nebula
65 * lightning into its own section.
74 #include "freespace.h"
77 #include "missionparse.h"
79 #include "neblightning.h"
82 #include "multimsgs.h"
84 // ------------------------------------------------------------------------------------------------------
85 // NEBULA LIGHTNING DEFINES/VARS
89 #define MAX_BOLT_TYPES_INTERNAL 11
91 // see lightning.tbl for explanations of these values
92 typedef struct bolt_type {
93 char name[NAME_LENGTH];
114 int Num_bolt_types = 0;
115 bolt_type Bolt_types[MAX_BOLT_TYPES_INTERNAL];
118 int Num_storm_types = 0;
119 storm_type Storm_types[MAX_STORM_TYPES];
122 // actual lightning bolt stuff -------
124 #define MAX_LIGHTNING_NODES 500
126 // nodes in a lightning bolt
130 typedef struct l_node {
131 vector pos; // world position
132 l_node *links[3]; // 3 links for lightning children
134 l_node *next, *prev; // for used and free-lists only
139 l_node Nebl_nodes[MAX_LIGHTNING_NODES];
142 // lightning node lists
143 l_node Nebl_free_list;
144 l_node Nebl_used_list;
146 // actual lightning bolt themselves
147 typedef struct l_bolt {
148 l_node *head; // head of the lightning bolt
149 int bolt_life; // remaining life timestamp
150 ubyte used; // used or not
151 ubyte first_frame; // if he hasn't been rendered at least once
155 vector start, strike, midpoint;
156 int delay; // delay stamp
157 int strikes_left; // #of strikes left
161 #define MAX_LIGHTNING_BOLTS 10
164 l_bolt Nebl_bolts[MAX_LIGHTNING_BOLTS];
165 int Nebl_bolt_count = 0;
167 // one cross-section of a lightning bolt
168 typedef struct l_section {
173 // points on the basic cross section
174 vector Nebl_ring[3] = {
175 { -1.0f, 0.0f, 0.0f },
176 { 1.0f, 0.70f, 0.0f },
177 { 1.0f, -0.70f, 0.0f }
180 // pinched off cross-section
181 vector Nebl_ring_pinched[3] = {
182 { -0.05f, 0.0f, 0.0f },
183 { 0.05f, 0.035f, 0.0f },
184 { 0.05f, -0.035f, 0.0f }
187 // globals used for rendering and generating bolts
188 int Nebl_flash_count = 0; // # of points rendered onscreen for this bolt
189 float Nebl_flash_x = 0.0f; // avg x of the points rendered
190 float Nebl_flash_y = 0.0f; // avg y of the points rendered
191 float Nebl_bang = 0.0; // distance to the viewer object
192 float Nebl_alpha = 0.0f; // alpha to use when rendering the bolt itself
193 float Nebl_glow_alpha = 0.0f; // alpha to use when rendering the bolt glow
194 int Nebl_stamp = -1; // random timestamp for making bolts
195 float Nebl_bolt_len; // length of the current bolt being generated
196 bolt_type *Nebl_type; // bolt type
197 matrix Nebl_bolt_dir; // orientation matrix of the bolt being generated
198 vector Nebl_bolt_start; // start point of the bolt being generated
199 vector Nebl_bolt_strike; // strike point of the bolt being generated
201 // the type of active storm
202 storm_type *Storm = NULL;
207 dc_get_arg(ARG_FLOAT);
208 Bolt_types[DEBUG_BOLT].b_scale = Dc_arg_float;
212 dc_get_arg(ARG_FLOAT);
213 Bolt_types[DEBUG_BOLT].b_rand = Dc_arg_float;
217 dc_get_arg(ARG_FLOAT);
218 Bolt_types[DEBUG_BOLT].b_shrink = Dc_arg_float;
222 dc_get_arg(ARG_FLOAT);
223 Bolt_types[DEBUG_BOLT].b_poly_pct = Dc_arg_float;
227 dc_get_arg(ARG_FLOAT);
228 Bolt_types[DEBUG_BOLT].b_add = Dc_arg_float;
233 Bolt_types[DEBUG_BOLT].num_strikes = Dc_arg_int;
237 dc_get_arg(ARG_FLOAT);
238 Bolt_types[DEBUG_BOLT].noise = Dc_arg_float;
242 dc_get_arg(ARG_FLOAT);
243 Bolt_types[DEBUG_BOLT].b_bright = Dc_arg_float;
248 Bolt_types[DEBUG_BOLT].lifetime = Dc_arg_int;
252 dc_printf("Debug lightning bolt settings :\n");
254 dc_printf("b_scale : %f\n", Bolt_types[DEBUG_BOLT].b_scale);
255 dc_printf("b_rand : %f\n", Bolt_types[DEBUG_BOLT].b_rand);
256 dc_printf("b_shrink : %f\n", Bolt_types[DEBUG_BOLT].b_shrink);
257 dc_printf("b_poly_pct : %f\n", Bolt_types[DEBUG_BOLT].b_poly_pct);
258 dc_printf("b_add : %f\n", Bolt_types[DEBUG_BOLT].b_add);
259 dc_printf("b_strikes : %d\n", Bolt_types[DEBUG_BOLT].num_strikes);
260 dc_printf("b_noise : %f\n", Bolt_types[DEBUG_BOLT].noise);
261 dc_printf("b_bright : %f\n", Bolt_types[DEBUG_BOLT].b_bright);
262 dc_printf("b_lifetime : %d\n", Bolt_types[DEBUG_BOLT].lifetime);
266 // nebula lightning intensity (0.0 to 1.0)
267 float Nebl_intensity = 0.6667f;
269 // min and max times for random lightning
270 int Nebl_random_min = 750; // min random time
271 int Nebl_random_max = 10000; // max random time
273 // min and max times for cruiser lightning
274 int Nebl_cruiser_min = 5000; // min cruiser time
275 int Nebl_cruiser_max = 25000; // max cruiser time
277 // min and max times for cap ships
278 int Nebl_cap_min = 4000; // min cap time
279 int Nebl_cap_max = 18000; // max cap time
281 // min and max time for super caps
282 int Nebl_supercap_min = 3000; // min supercap time
283 int Nebl_supercap_max = 12000; // max supercap time
285 DCF(lightning_intensity, "")
287 dc_get_arg(ARG_FLOAT);
288 float val = Dc_arg_float;
291 } else if(val > 1.0f){
295 Nebl_intensity = 1.0f - val;
298 // ------------------------------------------------------------------------------------------------------
299 // NEBULA LIGHTNING FORWARD DECLARATIONS
302 // "new" a lightning node
305 // "delete" a lightning node
306 void nebl_delete(l_node *lp);
308 // free up a the nodes of the passed in bolt
309 void nebl_release(l_node *bolt_head);
311 // generate a lightning bolt, returns l_left (the "head") and l_right (the "tail")
312 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right);
314 // output top and bottom vectors
315 // fvec == forward vector (eye viewpoint basically. in world coords)
316 // pos == world coordinate of the point we're calculating "around"
317 // w == width of the diff between top and bottom around pos
318 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add );
320 // render a section of the bolt
321 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b);
323 // generate a section
324 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);
327 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev = NULL);
329 // given a valid, complete bolt, jitter him based upon his noise
330 void nebl_jitter(l_bolt *b);
332 // return the index of a given bolt type by name
333 int nebl_get_bolt_index(char *name);
335 // return the index of a given storm type by name
336 int nebl_get_storm_index(char *name);
339 // ------------------------------------------------------------------------------------------------------
340 // NEBULA LIGHTNING FUNCTIONS
343 // initialize nebula lightning at game startup
347 char name[NAME_LENGTH+10] = "";
348 bolt_type bogus_lightning, *l;
349 storm_type bogus_storm, *s;
352 // parse the lightning table
353 read_file_text("lightning.tbl");
359 memset(Bolt_types, 0, sizeof(bolt_type) * MAX_BOLT_TYPES_INTERNAL);
361 // parse the individual lightning bolt types
362 required_string("#Bolts begin");
363 while(!optional_string("#Bolts end")){
365 if(Num_bolt_types >= MAX_BOLT_TYPES){
366 l = &bogus_lightning;
368 l = &Bolt_types[Num_bolt_types];
372 required_string("$Bolt:");
373 stuff_string(l->name, F_NAME, NULL);
376 required_string("+b_scale:");
377 stuff_float(&l->b_scale);
380 required_string("+b_shrink:");
381 stuff_float(&l->b_shrink);
384 required_string("+b_poly_pct:");
385 stuff_float(&l->b_poly_pct);
388 required_string("+b_rand:");
389 stuff_float(&l->b_rand);
392 required_string("+b_add:");
393 stuff_float(&l->b_add);
396 required_string("+b_strikes:");
397 stuff_int(&l->num_strikes);
400 required_string("+b_lifetime:");
401 stuff_int(&l->lifetime);
404 required_string("+b_noise:");
405 stuff_float(&l->noise);
408 required_string("+b_emp:");
409 stuff_float(&l->emp_intensity);
410 stuff_float(&l->emp_time);
413 required_string("+b_texture:");
414 stuff_string(name, F_NAME, NULL);
415 if((l != &bogus_lightning) && !Fred_running){
416 l->texture = bm_load(name);
420 required_string("+b_glow:");
421 stuff_string(name, F_NAME, NULL);
422 if((l != &bogus_lightning) && !Fred_running){
423 l->glow = bm_load(name);
427 required_string("+b_bright:");
428 stuff_float(&l->b_bright);
430 // increment the # of bolt types
431 if(l != &bogus_lightning){
436 // copy the first bolt to the debug bolt
437 memcpy(&Bolt_types[DEBUG_BOLT], &Bolt_types[0], sizeof(bolt_type));
440 required_string("#Storms begin");
441 while(!optional_string("#Storms end")){
443 if(Num_storm_types >= MAX_STORM_TYPES){
446 s = &Storm_types[Num_storm_types];
450 required_string("$Storm:");
451 stuff_string(s->name, F_NAME, NULL);
454 s->num_bolt_types = 0;
455 while(optional_string("+bolt:")){
456 stuff_string(name, F_NAME, NULL);
459 if(s->num_bolt_types < MAX_BOLT_TYPES){
460 s->bolt_types[s->num_bolt_types] = (char)nebl_get_bolt_index(name);
461 Assert(s->bolt_types[s->num_bolt_types] != -1);
467 required_string("+bolt_prec:");
473 required_string("+flavor:");
474 stuff_float(&s->flavor.xyz.x);
475 stuff_float(&s->flavor.xyz.y);
476 stuff_float(&s->flavor.xyz.z);
479 required_string("+random_freq:");
484 required_string("+random_count:");
485 stuff_int(&s->min_count);
486 stuff_int(&s->max_count);
488 // increment the # of bolt types
489 if(s != &bogus_storm){
496 // initialize lightning before entering a level
497 void nebl_level_init()
501 // zero all lightning bolts
502 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
503 Nebl_bolts[idx].head = NULL;
504 Nebl_bolts[idx].bolt_life = -1;
505 Nebl_bolts[idx].used = 0;
508 // initialize node list
510 list_init( &Nebl_free_list );
511 list_init( &Nebl_used_list );
513 // Link all object slots into the free list
514 for (idx=0; idx<MAX_LIGHTNING_NODES; idx++) {
515 list_append(&Nebl_free_list, &Nebl_nodes[idx] );
518 // zero the random timestamp
521 // null the storm. let mission parsing set it up
525 // render all lightning bolts
526 void nebl_render_all()
532 // no lightning in non-nebula missions
533 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
537 // if we have no storm
543 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
544 b = &Nebl_bolts[idx];
546 // if this is being used
548 Assert(b->head != NULL);
555 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
560 bi = &Bolt_types[(int)b->type];
562 // if this guy is still on a delay
564 if(timestamp_elapsed(b->delay)){
571 // if the timestamp on this guy has expired
572 if((b->bolt_life < 0) || timestamp_elapsed(b->bolt_life)){
573 // if this is a multiple strike bolt, jitter it and reset
574 if(b->strikes_left-1 > 0){
575 b->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
580 // by continuing here we skip rendering for one frame, which makes it look more like real lightning
583 // otherwise he's completely done, so release him
585 // maybe free up node data
587 nebl_release(b->head);
592 nprintf(("lightning", "Released bolt. %d used nodes!\n", Num_lnodes));
599 // pick some cool alpha values
600 Nebl_alpha = frand();
601 Nebl_glow_alpha = frand();
603 // otherwise render him
604 Nebl_flash_count = 0;
607 Nebl_bang = 10000000.0f;
608 nebl_render(bi, b->head, b->width);
610 // if this is the first frame he has been rendered, determine if we need to make a flash and sound effect
616 // if we rendered any points
617 if(Nebl_flash_count){
618 Nebl_flash_x /= (float)Nebl_flash_count;
619 Nebl_flash_y /= (float)Nebl_flash_count;
621 // quick distance from the center of the screen
622 float x = Nebl_flash_x - (gr_screen.max_w / 2.0f);
623 float y = Nebl_flash_y - (gr_screen.max_h / 2.0f);
624 float dist = fl_sqrt((x * x) + (y * y));
625 if(dist / (gr_screen.max_w / 2.0f) < 1.0f){
626 flash = 1.0f - (dist / (gr_screen.max_w / 2.0f));
628 // scale the flash by bolt type
629 flash *= bi->b_bright;
631 game_flash(flash, flash, flash);
634 // do some special stuff on the very first strike of the bolt
635 if(b->strikes_left == bi->num_strikes){
638 if(Nebl_bang < 40.0f){
640 } else if(Nebl_bang > 400.0f){
643 bang = 1.0f - (Nebl_bang / 400.0f);
645 if(frand_range(0.0f, 1.0f) < 0.5f){
646 snd_play(&Snds[SND_LIGHTNING_2], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
648 snd_play(&Snds[SND_LIGHTNING_1], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
652 if(bi->emp_intensity > 0.0f){
653 emp_apply(&b->midpoint, 0.0f, vm_vec_dist(&b->start, &b->strike), bi->emp_intensity, bi->emp_time);
662 // process lightning (randomly generate bolts, etc, etc);
667 // non-nebula mission
668 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
672 // non servers in multiplayer don't do this
673 if((Game_mode & GM_MULTIPLAYER) && !MULTIPLAYER_MASTER){
677 // if there's no chosen storm
682 // don't process lightning bolts unless we're a few seconds in
683 if(f2fl(Missiontime) < 3.0f){
688 if(Nebl_stamp == -1){
689 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
694 if(timestamp_elapsed(Nebl_stamp)){
695 // determine how many bolts to spew
696 num_bolts = (int)frand_range((float)Storm->min_count, (float)Storm->max_count);
697 for(idx=0; idx<num_bolts; idx++){
698 // hmm. for now just pick a random bolt type and run with it
702 s1 = (int)frand_range(0.0f, (float)Neb2_slices);
703 s2 = (int)frand_range(0.0f, (float)Neb2_slices);
704 s3 = (int)frand_range(0.0f, (float)Neb2_slices);
706 e1 = (int)frand_range(0.0f, (float)Neb2_slices);
707 e2 = (int)frand_range(0.0f, (float)Neb2_slices);
708 e3 = (int)frand_range(0.0f, (float)Neb2_slices);
710 // never choose the middle cube
711 if((s1 == 2) && (s2 == 2) && (s3 == 2)){
715 if((e1 == 2) && (e2 == 2) && (e3 == 2)){
721 } while((s1 == e1) && (s2 == e2) && (s3 == e3));
723 vector start = Neb2_cubes[s1][s2][s3].pt;
724 vector strike = Neb2_cubes[e1][e2][e3].pt;
726 // add some flavor to the bolt. mmmmmmmm, lightning
727 if(!IS_VEC_NULL(&Storm->flavor)){
728 // start with your basic hot sauce. measure how much you have
729 vector your_basic_hot_sauce;
730 vm_vec_sub(&your_basic_hot_sauce, &strike, &start);
731 float how_much_hot_sauce = vm_vec_normalize(&your_basic_hot_sauce);
733 // now figure out how much of that good wing sauce to add
734 vector wing_sauce = Storm->flavor;
735 if(frand_range(0.0, 1.0f) < 0.5f){
736 vm_vec_scale(&wing_sauce, -1.0f);
738 float how_much_of_that_good_wing_sauce_to_add = vm_vec_normalize(&wing_sauce);
740 // mix the two together, taking care not to add too much
742 if(how_much_of_that_good_wing_sauce_to_add > 1000.0f){
743 how_much_of_that_good_wing_sauce_to_add = 1000.0f;
745 vm_vec_interp_constant(&the_mixture, &your_basic_hot_sauce, &wing_sauce, how_much_of_that_good_wing_sauce_to_add / 1000.0f);
747 // take the final sauce and store it in the proper container
748 vm_vec_scale(&the_mixture, how_much_hot_sauce);
750 // make sure to put it on everything! whee!
751 vm_vec_add(&strike, &start, &the_mixture);
754 int type = (int)frand_range(0.0f, (float)(Storm->num_bolt_types-1));
755 nebl_bolt(Storm->bolt_types[type], &start, &strike);
758 // reset the timestamp
759 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
763 // create a lightning bolt
764 void nebl_bolt(int type, vector *start, vector *strike)
774 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
780 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
781 if(!Nebl_bolts[idx].used){
791 if((type < 0) || ((type >= Num_bolt_types) && (type != DEBUG_BOLT)) ){
794 bi = &Bolt_types[type];
796 // get a pointer to the bolt
797 bolt = &Nebl_bolts[idx];
800 bolt->start = *start;
801 bolt->strike = *strike;
802 bolt->strikes_left = bi->num_strikes;
804 bolt->type = (char)type;
805 bolt->first_frame = 1;
806 bolt->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
808 Nebl_bolt_start = *start;
809 Nebl_bolt_strike = *strike;
812 if(bolt->delay != -1){
813 bolt->delay = timestamp(bolt->delay);
816 // setup the rest of the important bolt data
817 if(vm_vec_same(&Nebl_bolt_start, &Nebl_bolt_strike)){
818 Nebl_bolt_strike.xyz.z += 150.0f;
820 Nebl_bolt_len = vm_vec_dist(&Nebl_bolt_start, &Nebl_bolt_strike);
821 vm_vec_sub(&dir, &Nebl_bolt_strike, &Nebl_bolt_start);
824 vm_vec_scale_add(&bolt->midpoint, &Nebl_bolt_start, &dir, 0.5f);
826 bolt_len = vm_vec_normalize(&dir);
827 vm_vector_2_matrix(&Nebl_bolt_dir, &dir, NULL, NULL);
829 // global type for generating the bolt
832 // try and make the bolt
833 if(!nebl_gen(&Nebl_bolt_start, &Nebl_bolt_strike, 0, 4, 0, &bolt->head, &tail)){
834 if(bolt->head != NULL){
835 nebl_release(bolt->head);
843 // setup the rest of the data
845 bolt->width = bi->b_poly_pct * bolt_len;
847 // if i'm a multiplayer master, send a bolt packet
848 if(MULTIPLAYER_MASTER){
849 send_lightning_packet(type, start, strike);
853 // get the current # of active lightning bolts
854 int nebl_get_active_bolts()
856 return Nebl_bolt_count;
859 // get the current # of active nodes
860 int nebl_get_active_nodes()
865 // set the storm (call from mission parse)
866 void nebl_set_storm(char *name)
868 int index = nebl_get_storm_index(name);
872 if((index >= 0) && (index < Num_storm_types)){
873 Storm = &Storm_types[index];
877 // ------------------------------------------------------------------------------------------------------
878 // NEBULA LIGHTNING FORWARD DEFINITIONS
881 // "new" a lightning node
886 // if we're out of nodes
887 if(Num_lnodes >= MAX_LIGHTNING_NODES){
889 nprintf(("lightning", "Out of lightning nodes!\n"));
893 // get a new node off the freelist
894 lp = GET_FIRST(&Nebl_free_list);
895 Assert( lp != &Nebl_free_list ); // shouldn't have the dummy element
897 // remove trailp from the free list
898 list_remove( &Nebl_free_list, lp );
900 // insert trailp onto the end of used list
901 list_append( &Nebl_used_list, lp );
910 // return the pointer
914 // "delete" a lightning node
915 void nebl_delete(l_node *lp)
917 // remove objp from the used list
918 list_remove( &Nebl_used_list, lp );
920 // add objp to the end of the free
921 list_append( &Nebl_free_list, lp );
927 // free a lightning bolt
928 void nebl_release(l_node *whee)
935 // release all of our children
936 if(whee->links[LINK_RIGHT] != NULL){
937 nebl_release(whee->links[LINK_RIGHT]);
939 if(whee->links[LINK_CHILD] != NULL){
940 nebl_release(whee->links[LINK_CHILD]);
947 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right)
949 l_node *child_node = NULL;
950 float d = vm_vec_dist_quick( left, right );
952 // if we've reached the critical point
953 if ( d < 0.30f || (depth > max_depth) ){
955 l_node *new_left = nebl_new();
956 if(new_left == NULL){
959 new_left->links[0] = NULL; new_left->links[1] = NULL; new_left->links[2] = NULL;
960 new_left->pos = vmd_zero_vector;
961 l_node *new_right = nebl_new();
962 if(new_right == NULL){
963 nebl_delete(new_left);
966 new_right->links[0] = NULL; new_right->links[1] = NULL; new_right->links[2] = NULL;
967 new_right->pos = vmd_zero_vector;
970 new_left->pos = *left;
971 new_left->links[LINK_RIGHT] = new_right;
975 new_right->pos = *right;
976 new_right->links[LINK_LEFT] = new_left;
977 *l_right = new_right;
985 vm_vec_avg( &tmp, left, right );
987 // sometimes generate children
988 if(!child && (frand() <= Nebl_type->b_rand)){
989 // get a point on the plane of the strike
991 vm_vec_random_in_circle(&tmp2, &Nebl_bolt_strike, &Nebl_bolt_dir, Nebl_bolt_len * Nebl_type->b_scale, 0);
993 // maybe move away from the plane
995 vm_vec_sub(&dir, &tmp2, &tmp);
996 vm_vec_scale_add(&tmp2, &tmp, &dir, Nebl_type->b_shrink);
1000 if(!nebl_gen(&tmp, &tmp2, 0, 2, 1, &child_node, &argh)){
1001 if(child_node != NULL){
1002 nebl_release(child_node);
1008 float scaler = 0.30f;
1009 tmp.xyz.x += (frand()-0.5f)*d*scaler;
1010 tmp.xyz.y += (frand()-0.5f)*d*scaler;
1011 tmp.xyz.z += (frand()-0.5f)*d*scaler;
1013 // generate left half
1016 if(!nebl_gen( left, &tmp, depth+1, max_depth, child, &ll, &lr )){
1017 if(child_node != NULL){
1018 nebl_release(child_node);
1026 // generate right half
1029 if(!nebl_gen( &tmp, right, depth+1, max_depth, child, &rl, &rr )){
1030 if(child_node != NULL){
1031 nebl_release(child_node);
1042 // splice the two together
1043 lr->links[LINK_RIGHT] = rl->links[LINK_RIGHT];
1044 lr->links[LINK_RIGHT]->links[LINK_LEFT] = lr;
1047 // if we generated a child, stick him on
1048 if(child_node != NULL){
1049 lr->links[LINK_CHILD] = child_node;
1060 // output top and bottom vectors
1061 // fvec == forward vector (eye viewpoint basically. in world coords)
1062 // pos == world coordinate of the point we're calculating "around"
1063 // w == width of the diff between top and bottom around pos
1064 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1071 vm_vec_sub( &rvec, &Eye_position, &temp );
1072 vm_vec_normalize( &rvec );
1074 vm_vec_crossprod(&uvec,fvec,&rvec);
1075 vm_vec_normalize(&uvec);
1077 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1078 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1080 vm_vec_scale_add2( top, &rvec, z_add );
1081 vm_vec_scale_add2( bot, &rvec, z_add );
1084 // render a section of the bolt
1085 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b)
1088 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
1091 // Sets mode. Returns previous mode.
1092 gr_zbuffer_set(GR_ZBUFF_FULL);
1095 for(idx=0; idx<2; idx++){
1097 v[0].u = 0.0f; v[0].v = 0.0f;
1099 v[1] = a->vex[idx+1];
1100 v[1].u = 1.0f; v[1].v = 0.0f;
1102 v[2] = b->vex[idx+1];
1103 v[2].u = 1.0f; v[2].v = 1.0f;
1106 v[3].u = 0.0f; v[3].v = 1.0f;
1109 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha);
1110 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1115 v[0].u = 0.0f; v[0].v = 0.0f;
1118 v[1].u = 1.0f; v[1].v = 0.0f;
1121 v[2].u = 1.0f; v[2].v = 1.0f;
1124 v[3].u = 0.0f; v[3].v = 1.0f;
1126 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha);
1127 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1129 // draw the glow beam
1130 verts[0] = &a->glow_vex[0];
1131 verts[0]->v = 0.0f; verts[0]->u = 0.0f;
1133 verts[1] = &a->glow_vex[1];
1134 verts[1]->v = 1.0f; verts[1]->u = 0.0f;
1136 verts[2] = &b->glow_vex[1];
1137 verts[2]->v = 1.0f; verts[2]->u = 1.0f;
1139 verts[3] = &b->glow_vex[0];
1140 verts[3]->v = 0.0f; verts[3]->u = 1.0f;
1142 gr_set_bitmap(bi->glow, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_glow_alpha);
1143 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1146 // generate a section
1147 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)
1154 vector glow_a, glow_b;
1157 vm_vec_sub(&dir, &a->pos, &b->pos);
1158 vm_vec_copy_normalize(&dir_normal, &dir);
1159 vm_vector_2_matrix(&m, &dir_normal, NULL, NULL);
1161 // distance to player
1162 float bang_dist = vm_vec_dist_quick(&Eye_position, &a->pos);
1163 if(bang_dist < Nebl_bang){
1164 Nebl_bang = bang_dist;
1167 // rotate the basic section into world
1168 for(idx=0; idx<3; idx++){
1171 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1173 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1174 vm_vec_rotate(&pt, &temp, &m);
1176 vm_vec_add2(&pt, &a->pos);
1179 g3_rotate_vertex(&c->vex[idx], &pt);
1180 g3_project_vertex(&c->vex[idx]);
1182 // if first frame, keep track of the average screen pos
1183 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)){
1184 Nebl_flash_x += c->vex[idx].sx;
1185 Nebl_flash_y += c->vex[idx].sy;
1189 // calculate the glow points
1190 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &a->pos, pinch_a ? 0.5f : width * 6.0f, Nebl_type->b_add);
1191 g3_rotate_vertex(&c->glow_vex[0], &glow_a);
1192 g3_project_vertex(&c->glow_vex[0]);
1193 g3_rotate_vertex(&c->glow_vex[1], &glow_b);
1194 g3_project_vertex(&c->glow_vex[1]);
1198 // rotate the basic section into world
1199 for(idx=0; idx<3; idx++){
1202 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1204 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1205 vm_vec_rotate(&pt, &temp, &m);
1207 vm_vec_add2(&pt, &b->pos);
1210 g3_rotate_vertex(&cap->vex[idx], &pt);
1211 g3_project_vertex(&cap->vex[idx]);
1213 // if first frame, keep track of the average screen pos
1214 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)){
1215 Nebl_flash_x += c->vex[idx].sx;
1216 Nebl_flash_y += c->vex[idx].sy;
1221 // calculate the glow points
1222 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &b->pos, pinch_b ? 0.5f : width * 6.0f, bi->b_add);
1223 g3_rotate_vertex(&cap->glow_vex[0], &glow_a);
1224 g3_project_vertex(&cap->glow_vex[0]);
1225 g3_rotate_vertex(&cap->glow_vex[1], &glow_b);
1226 g3_project_vertex(&cap->glow_vex[1]);
1231 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev)
1235 l_section child_start;
1242 // if prev is NULL, we're just starting so we need our start point
1244 Assert(whee->links[LINK_RIGHT] != NULL);
1245 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &start, NULL, 1, 0);
1250 // if we have a child section
1251 if(whee->links[LINK_CHILD]){
1253 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);
1256 nebl_render_section(bi, &child_start, &end);
1259 if(whee->links[LINK_CHILD]->links[LINK_RIGHT] != NULL){
1260 nebl_render(bi, whee->links[LINK_CHILD], width * 0.5f, &end);
1264 // if the next section is an end section
1265 if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] == NULL){
1269 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &temp, &end, 0, 1);
1271 // render the section
1272 nebl_render_section(bi, &start, &end);
1275 else if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] != NULL){
1277 nebl_generate_section(bi, width, whee->links[LINK_RIGHT], whee->links[LINK_RIGHT]->links[LINK_RIGHT], &end, NULL, 0, 0);
1279 // render the section
1280 nebl_render_section(bi, &start, &end);
1282 // recurse through him
1283 nebl_render(bi, whee->links[LINK_RIGHT], width, &end);
1287 // given a valid, complete bolt, jitter him based upon his noise
1288 void nebl_jitter(l_bolt *b)
1294 bolt_type *bi = NULL;
1300 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
1303 bi = &Bolt_types[(int)b->type];
1305 // get the bolt direction
1306 vm_vec_sub(&temp, &b->strike, &b->start);
1307 length = vm_vec_normalize_quick(&temp);
1308 vm_vector_2_matrix(&m, &temp, NULL, NULL);
1310 // jitter all nodes on the main trunk
1312 while(moveup != NULL){
1314 vm_vec_random_in_circle(&moveup->pos, &temp, &m, frand_range(0.0f, length * bi->noise), 0);
1316 // just on the main trunk
1317 moveup = moveup->links[LINK_RIGHT];
1321 // return the index of a given bolt type by name
1322 int nebl_get_bolt_index(char *name)
1326 for(idx=0; idx<Num_bolt_types; idx++){
1327 if(!strcmp(name, Bolt_types[idx].name)){
1335 // return the index of a given storm type by name
1336 int nebl_get_storm_index(char *name)
1340 for(idx=0; idx<Num_bolt_types; idx++){
1341 if(!strcmp(name, Storm_types[idx].name)){