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.3 2002/06/09 04:41:23 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:47 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:10 root
28 * 10 8/15/99 3:50p Dave
29 * Don't process lightning at the very beginning of a mission.
31 * 9 8/12/99 10:38a Anoop
32 * Removed unnecessary Int3().
34 * 8 8/05/99 2:06a Dave
37 * 7 7/27/99 9:51p Andsager
38 * make mprintf's into nprintf's
40 * 6 7/03/99 5:50p Dave
41 * Make rotated bitmaps draw properly in padlock views.
43 * 5 7/02/99 4:31p Dave
44 * Much more sophisticated lightning support.
46 * 4 6/09/99 10:32a Dave
47 * Made random lighting bolts behave more like the E3 demo. Generally more
50 * 3 5/26/99 11:46a Dave
51 * Added ship-blasting lighting and made the randomization of lighting
52 * much more customizable.
54 * 2 5/24/99 5:45p Dave
55 * Added detail levels to the nebula, with a decent speedup. Split nebula
56 * lightning into its own section.
65 #include "freespace.h"
68 #include "missionparse.h"
70 #include "neblightning.h"
73 #include "multimsgs.h"
75 // ------------------------------------------------------------------------------------------------------
76 // NEBULA LIGHTNING DEFINES/VARS
80 #define MAX_BOLT_TYPES_INTERNAL 11
82 // see lightning.tbl for explanations of these values
83 typedef struct bolt_type {
84 char name[NAME_LENGTH];
105 int Num_bolt_types = 0;
106 bolt_type Bolt_types[MAX_BOLT_TYPES_INTERNAL];
109 int Num_storm_types = 0;
110 storm_type Storm_types[MAX_STORM_TYPES];
113 // actual lightning bolt stuff -------
115 #define MAX_LIGHTNING_NODES 500
117 // nodes in a lightning bolt
121 typedef struct l_node {
122 vector pos; // world position
123 l_node *links[3]; // 3 links for lightning children
125 l_node *next, *prev; // for used and free-lists only
130 l_node Nebl_nodes[MAX_LIGHTNING_NODES];
133 // lightning node lists
134 l_node Nebl_free_list;
135 l_node Nebl_used_list;
137 // actual lightning bolt themselves
138 typedef struct l_bolt {
139 l_node *head; // head of the lightning bolt
140 int bolt_life; // remaining life timestamp
141 ubyte used; // used or not
142 ubyte first_frame; // if he hasn't been rendered at least once
146 vector start, strike, midpoint;
147 int delay; // delay stamp
148 int strikes_left; // #of strikes left
152 #define MAX_LIGHTNING_BOLTS 10
155 l_bolt Nebl_bolts[MAX_LIGHTNING_BOLTS];
156 int Nebl_bolt_count = 0;
158 // one cross-section of a lightning bolt
159 typedef struct l_section {
164 // points on the basic cross section
165 vector Nebl_ring[3] = {
166 { -1.0f, 0.0f, 0.0f },
167 { 1.0f, 0.70f, 0.0f },
168 { 1.0f, -0.70f, 0.0f }
171 // pinched off cross-section
172 vector Nebl_ring_pinched[3] = {
173 { -0.05f, 0.0f, 0.0f },
174 { 0.05f, 0.035f, 0.0f },
175 { 0.05f, -0.035f, 0.0f }
178 // globals used for rendering and generating bolts
179 int Nebl_flash_count = 0; // # of points rendered onscreen for this bolt
180 float Nebl_flash_x = 0.0f; // avg x of the points rendered
181 float Nebl_flash_y = 0.0f; // avg y of the points rendered
182 float Nebl_bang = 0.0; // distance to the viewer object
183 float Nebl_alpha = 0.0f; // alpha to use when rendering the bolt itself
184 float Nebl_glow_alpha = 0.0f; // alpha to use when rendering the bolt glow
185 int Nebl_stamp = -1; // random timestamp for making bolts
186 float Nebl_bolt_len; // length of the current bolt being generated
187 bolt_type *Nebl_type; // bolt type
188 matrix Nebl_bolt_dir; // orientation matrix of the bolt being generated
189 vector Nebl_bolt_start; // start point of the bolt being generated
190 vector Nebl_bolt_strike; // strike point of the bolt being generated
192 // the type of active storm
193 storm_type *Storm = NULL;
198 dc_get_arg(ARG_FLOAT);
199 Bolt_types[DEBUG_BOLT].b_scale = Dc_arg_float;
203 dc_get_arg(ARG_FLOAT);
204 Bolt_types[DEBUG_BOLT].b_rand = Dc_arg_float;
208 dc_get_arg(ARG_FLOAT);
209 Bolt_types[DEBUG_BOLT].b_shrink = Dc_arg_float;
213 dc_get_arg(ARG_FLOAT);
214 Bolt_types[DEBUG_BOLT].b_poly_pct = Dc_arg_float;
218 dc_get_arg(ARG_FLOAT);
219 Bolt_types[DEBUG_BOLT].b_add = Dc_arg_float;
224 Bolt_types[DEBUG_BOLT].num_strikes = Dc_arg_int;
228 dc_get_arg(ARG_FLOAT);
229 Bolt_types[DEBUG_BOLT].noise = Dc_arg_float;
233 dc_get_arg(ARG_FLOAT);
234 Bolt_types[DEBUG_BOLT].b_bright = Dc_arg_float;
239 Bolt_types[DEBUG_BOLT].lifetime = Dc_arg_int;
243 dc_printf("Debug lightning bolt settings :\n");
245 dc_printf("b_scale : %f\n", Bolt_types[DEBUG_BOLT].b_scale);
246 dc_printf("b_rand : %f\n", Bolt_types[DEBUG_BOLT].b_rand);
247 dc_printf("b_shrink : %f\n", Bolt_types[DEBUG_BOLT].b_shrink);
248 dc_printf("b_poly_pct : %f\n", Bolt_types[DEBUG_BOLT].b_poly_pct);
249 dc_printf("b_add : %f\n", Bolt_types[DEBUG_BOLT].b_add);
250 dc_printf("b_strikes : %d\n", Bolt_types[DEBUG_BOLT].num_strikes);
251 dc_printf("b_noise : %f\n", Bolt_types[DEBUG_BOLT].noise);
252 dc_printf("b_bright : %f\n", Bolt_types[DEBUG_BOLT].b_bright);
253 dc_printf("b_lifetime : %d\n", Bolt_types[DEBUG_BOLT].lifetime);
257 // nebula lightning intensity (0.0 to 1.0)
258 float Nebl_intensity = 0.6667f;
260 // min and max times for random lightning
261 int Nebl_random_min = 750; // min random time
262 int Nebl_random_max = 10000; // max random time
264 // min and max times for cruiser lightning
265 int Nebl_cruiser_min = 5000; // min cruiser time
266 int Nebl_cruiser_max = 25000; // max cruiser time
268 // min and max times for cap ships
269 int Nebl_cap_min = 4000; // min cap time
270 int Nebl_cap_max = 18000; // max cap time
272 // min and max time for super caps
273 int Nebl_supercap_min = 3000; // min supercap time
274 int Nebl_supercap_max = 12000; // max supercap time
276 DCF(lightning_intensity, "")
278 dc_get_arg(ARG_FLOAT);
279 float val = Dc_arg_float;
282 } else if(val > 1.0f){
286 Nebl_intensity = 1.0f - val;
289 // ------------------------------------------------------------------------------------------------------
290 // NEBULA LIGHTNING FORWARD DECLARATIONS
293 // "new" a lightning node
296 // "delete" a lightning node
297 void nebl_delete(l_node *lp);
299 // free up a the nodes of the passed in bolt
300 void nebl_release(l_node *bolt_head);
302 // generate a lightning bolt, returns l_left (the "head") and l_right (the "tail")
303 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right);
305 // output top and bottom vectors
306 // fvec == forward vector (eye viewpoint basically. in world coords)
307 // pos == world coordinate of the point we're calculating "around"
308 // w == width of the diff between top and bottom around pos
309 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add );
311 // render a section of the bolt
312 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b);
314 // generate a section
315 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);
318 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev = NULL);
320 // given a valid, complete bolt, jitter him based upon his noise
321 void nebl_jitter(l_bolt *b);
323 // return the index of a given bolt type by name
324 int nebl_get_bolt_index(char *name);
326 // return the index of a given storm type by name
327 int nebl_get_storm_index(char *name);
330 // ------------------------------------------------------------------------------------------------------
331 // NEBULA LIGHTNING FUNCTIONS
334 // initialize nebula lightning at game startup
337 char name[NAME_LENGTH+10] = "";
338 bolt_type bogus_lightning, *l;
339 storm_type bogus_storm, *s;
342 // parse the lightning table
343 read_file_text("lightning.tbl");
349 memset(Bolt_types, 0, sizeof(bolt_type) * MAX_BOLT_TYPES_INTERNAL);
351 // parse the individual lightning bolt types
352 required_string("#Bolts begin");
353 while(!optional_string("#Bolts end")){
355 if(Num_bolt_types >= MAX_BOLT_TYPES){
356 l = &bogus_lightning;
358 l = &Bolt_types[Num_bolt_types];
362 required_string("$Bolt:");
363 stuff_string(l->name, F_NAME, NULL);
366 required_string("+b_scale:");
367 stuff_float(&l->b_scale);
370 required_string("+b_shrink:");
371 stuff_float(&l->b_shrink);
374 required_string("+b_poly_pct:");
375 stuff_float(&l->b_poly_pct);
378 required_string("+b_rand:");
379 stuff_float(&l->b_rand);
382 required_string("+b_add:");
383 stuff_float(&l->b_add);
386 required_string("+b_strikes:");
387 stuff_int(&l->num_strikes);
390 required_string("+b_lifetime:");
391 stuff_int(&l->lifetime);
394 required_string("+b_noise:");
395 stuff_float(&l->noise);
398 required_string("+b_emp:");
399 stuff_float(&l->emp_intensity);
400 stuff_float(&l->emp_time);
403 required_string("+b_texture:");
404 stuff_string(name, F_NAME, NULL);
405 if((l != &bogus_lightning) && !Fred_running){
406 l->texture = bm_load(name);
410 required_string("+b_glow:");
411 stuff_string(name, F_NAME, NULL);
412 if((l != &bogus_lightning) && !Fred_running){
413 l->glow = bm_load(name);
417 required_string("+b_bright:");
418 stuff_float(&l->b_bright);
420 // increment the # of bolt types
421 if(l != &bogus_lightning){
426 // copy the first bolt to the debug bolt
427 memcpy(&Bolt_types[DEBUG_BOLT], &Bolt_types[0], sizeof(bolt_type));
430 required_string("#Storms begin");
431 while(!optional_string("#Storms end")){
433 if(Num_storm_types >= MAX_STORM_TYPES){
436 s = &Storm_types[Num_storm_types];
440 required_string("$Storm:");
441 stuff_string(s->name, F_NAME, NULL);
444 s->num_bolt_types = 0;
445 while(optional_string("+bolt:")){
446 stuff_string(name, F_NAME, NULL);
449 if(s->num_bolt_types < MAX_BOLT_TYPES){
450 s->bolt_types[s->num_bolt_types] = (char)nebl_get_bolt_index(name);
451 Assert(s->bolt_types[s->num_bolt_types] != -1);
457 required_string("+bolt_prec:");
463 required_string("+flavor:");
464 stuff_float(&s->flavor.x);
465 stuff_float(&s->flavor.y);
466 stuff_float(&s->flavor.z);
469 required_string("+random_freq:");
474 required_string("+random_count:");
475 stuff_int(&s->min_count);
476 stuff_int(&s->max_count);
478 // increment the # of bolt types
479 if(s != &bogus_storm){
485 // initialize lightning before entering a level
486 void nebl_level_init()
490 // zero all lightning bolts
491 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
492 Nebl_bolts[idx].head = NULL;
493 Nebl_bolts[idx].bolt_life = -1;
494 Nebl_bolts[idx].used = 0;
497 // initialize node list
499 list_init( &Nebl_free_list );
500 list_init( &Nebl_used_list );
502 // Link all object slots into the free list
503 for (idx=0; idx<MAX_LIGHTNING_NODES; idx++) {
504 list_append(&Nebl_free_list, &Nebl_nodes[idx] );
507 // zero the random timestamp
510 // null the storm. let mission parsing set it up
514 // render all lightning bolts
515 void nebl_render_all()
521 // no lightning in non-nebula missions
522 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
526 // if we have no storm
532 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
533 b = &Nebl_bolts[idx];
535 // if this is being used
537 Assert(b->head != NULL);
544 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
549 bi = &Bolt_types[b->type];
551 // if this guy is still on a delay
553 if(timestamp_elapsed(b->delay)){
560 // if the timestamp on this guy has expired
561 if((b->bolt_life < 0) || timestamp_elapsed(b->bolt_life)){
562 // if this is a multiple strike bolt, jitter it and reset
563 if(b->strikes_left-1 > 0){
564 b->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
569 // by continuing here we skip rendering for one frame, which makes it look more like real lightning
572 // otherwise he's completely done, so release him
574 // maybe free up node data
576 nebl_release(b->head);
581 nprintf(("lightning", "Released bolt. %d used nodes!\n", Num_lnodes));
588 // pick some cool alpha values
589 Nebl_alpha = frand();
590 Nebl_glow_alpha = frand();
592 // otherwise render him
593 Nebl_flash_count = 0;
596 Nebl_bang = 10000000.0f;
597 nebl_render(bi, b->head, b->width);
599 // if this is the first frame he has been rendered, determine if we need to make a flash and sound effect
605 // if we rendered any points
606 if(Nebl_flash_count){
607 Nebl_flash_x /= (float)Nebl_flash_count;
608 Nebl_flash_y /= (float)Nebl_flash_count;
610 // quick distance from the center of the screen
611 float x = Nebl_flash_x - (gr_screen.max_w / 2.0f);
612 float y = Nebl_flash_y - (gr_screen.max_h / 2.0f);
613 float dist = fl_sqrt((x * x) + (y * y));
614 if(dist / (gr_screen.max_w / 2.0f) < 1.0f){
615 flash = 1.0f - (dist / (gr_screen.max_w / 2.0f));
617 // scale the flash by bolt type
618 flash *= bi->b_bright;
620 game_flash(flash, flash, flash);
623 // do some special stuff on the very first strike of the bolt
624 if(b->strikes_left == bi->num_strikes){
627 if(Nebl_bang < 40.0f){
629 } else if(Nebl_bang > 400.0f){
632 bang = 1.0f - (Nebl_bang / 400.0f);
634 if(frand_range(0.0f, 1.0f) < 0.5f){
635 snd_play(&Snds[SND_LIGHTNING_2], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
637 snd_play(&Snds[SND_LIGHTNING_1], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
641 if(bi->emp_intensity > 0.0f){
642 emp_apply(&b->midpoint, 0.0f, vm_vec_dist(&b->start, &b->strike), bi->emp_intensity, bi->emp_time);
651 // process lightning (randomly generate bolts, etc, etc);
656 // non-nebula mission
657 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
661 // non servers in multiplayer don't do this
662 if((Game_mode & GM_MULTIPLAYER) && !MULTIPLAYER_MASTER){
666 // if there's no chosen storm
671 // don't process lightning bolts unless we're a few seconds in
672 if(f2fl(Missiontime) < 3.0f){
677 if(Nebl_stamp == -1){
678 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
683 if(timestamp_elapsed(Nebl_stamp)){
684 // determine how many bolts to spew
685 num_bolts = (int)frand_range((float)Storm->min_count, (float)Storm->max_count);
686 for(idx=0; idx<num_bolts; idx++){
687 // hmm. for now just pick a random bolt type and run with it
691 s1 = (int)frand_range(0.0f, (float)Neb2_slices);
692 s2 = (int)frand_range(0.0f, (float)Neb2_slices);
693 s3 = (int)frand_range(0.0f, (float)Neb2_slices);
695 e1 = (int)frand_range(0.0f, (float)Neb2_slices);
696 e2 = (int)frand_range(0.0f, (float)Neb2_slices);
697 e3 = (int)frand_range(0.0f, (float)Neb2_slices);
699 // never choose the middle cube
700 if((s1 == 2) && (s2 == 2) && (s3 == 2)){
704 if((e1 == 2) && (e2 == 2) && (e3 == 2)){
710 } while((s1 == e1) && (s2 == e2) && (s3 == e3));
712 vector start = Neb2_cubes[s1][s2][s3].pt;
713 vector strike = Neb2_cubes[e1][e2][e3].pt;
715 // add some flavor to the bolt. mmmmmmmm, lightning
716 if(!IS_VEC_NULL(&Storm->flavor)){
717 // start with your basic hot sauce. measure how much you have
718 vector your_basic_hot_sauce;
719 vm_vec_sub(&your_basic_hot_sauce, &strike, &start);
720 float how_much_hot_sauce = vm_vec_normalize(&your_basic_hot_sauce);
722 // now figure out how much of that good wing sauce to add
723 vector wing_sauce = Storm->flavor;
724 if(frand_range(0.0, 1.0f) < 0.5f){
725 vm_vec_scale(&wing_sauce, -1.0f);
727 float how_much_of_that_good_wing_sauce_to_add = vm_vec_normalize(&wing_sauce);
729 // mix the two together, taking care not to add too much
731 if(how_much_of_that_good_wing_sauce_to_add > 1000.0f){
732 how_much_of_that_good_wing_sauce_to_add = 1000.0f;
734 vm_vec_interp_constant(&the_mixture, &your_basic_hot_sauce, &wing_sauce, how_much_of_that_good_wing_sauce_to_add / 1000.0f);
736 // take the final sauce and store it in the proper container
737 vm_vec_scale(&the_mixture, how_much_hot_sauce);
739 // make sure to put it on everything! whee!
740 vm_vec_add(&strike, &start, &the_mixture);
743 int type = (int)frand_range(0.0f, (float)(Storm->num_bolt_types-1));
744 nebl_bolt(Storm->bolt_types[type], &start, &strike);
747 // reset the timestamp
748 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
752 // create a lightning bolt
753 void nebl_bolt(int type, vector *start, vector *strike)
763 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
769 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
770 if(!Nebl_bolts[idx].used){
780 if((type < 0) || ((type >= Num_bolt_types) && (type != DEBUG_BOLT)) ){
783 bi = &Bolt_types[type];
785 // get a pointer to the bolt
786 bolt = &Nebl_bolts[idx];
789 bolt->start = *start;
790 bolt->strike = *strike;
791 bolt->strikes_left = bi->num_strikes;
793 bolt->type = (char)type;
794 bolt->first_frame = 1;
795 bolt->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
797 Nebl_bolt_start = *start;
798 Nebl_bolt_strike = *strike;
801 if(bolt->delay != -1){
802 bolt->delay = timestamp(bolt->delay);
805 // setup the rest of the important bolt data
806 if(vm_vec_same(&Nebl_bolt_start, &Nebl_bolt_strike)){
807 Nebl_bolt_strike.z += 150.0f;
809 Nebl_bolt_len = vm_vec_dist(&Nebl_bolt_start, &Nebl_bolt_strike);
810 vm_vec_sub(&dir, &Nebl_bolt_strike, &Nebl_bolt_start);
813 vm_vec_scale_add(&bolt->midpoint, &Nebl_bolt_start, &dir, 0.5f);
815 bolt_len = vm_vec_normalize(&dir);
816 vm_vector_2_matrix(&Nebl_bolt_dir, &dir, NULL, NULL);
818 // global type for generating the bolt
821 // try and make the bolt
822 if(!nebl_gen(&Nebl_bolt_start, &Nebl_bolt_strike, 0, 4, 0, &bolt->head, &tail)){
823 if(bolt->head != NULL){
824 nebl_release(bolt->head);
832 // setup the rest of the data
834 bolt->width = bi->b_poly_pct * bolt_len;
836 // if i'm a multiplayer master, send a bolt packet
837 if(MULTIPLAYER_MASTER){
838 send_lightning_packet(type, start, strike);
842 // get the current # of active lightning bolts
843 int nebl_get_active_bolts()
845 return Nebl_bolt_count;
848 // get the current # of active nodes
849 int nebl_get_active_nodes()
854 // set the storm (call from mission parse)
855 void nebl_set_storm(char *name)
857 int index = nebl_get_storm_index(name);
861 if((index >= 0) && (index < Num_storm_types)){
862 Storm = &Storm_types[index];
866 // ------------------------------------------------------------------------------------------------------
867 // NEBULA LIGHTNING FORWARD DEFINITIONS
870 // "new" a lightning node
875 // if we're out of nodes
876 if(Num_lnodes >= MAX_LIGHTNING_NODES){
878 nprintf(("lightning", "Out of lightning nodes!\n"));
882 // get a new node off the freelist
883 lp = GET_FIRST(&Nebl_free_list);
884 Assert( lp != &Nebl_free_list ); // shouldn't have the dummy element
886 // remove trailp from the free list
887 list_remove( &Nebl_free_list, lp );
889 // insert trailp onto the end of used list
890 list_append( &Nebl_used_list, lp );
899 // return the pointer
903 // "delete" a lightning node
904 void nebl_delete(l_node *lp)
906 // remove objp from the used list
907 list_remove( &Nebl_used_list, lp );
909 // add objp to the end of the free
910 list_append( &Nebl_free_list, lp );
916 // free a lightning bolt
917 void nebl_release(l_node *whee)
924 // release all of our children
925 if(whee->links[LINK_RIGHT] != NULL){
926 nebl_release(whee->links[LINK_RIGHT]);
928 if(whee->links[LINK_CHILD] != NULL){
929 nebl_release(whee->links[LINK_CHILD]);
936 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right)
938 l_node *child_node = NULL;
939 float d = vm_vec_dist_quick( left, right );
941 // if we've reached the critical point
942 if ( d < 0.30f || (depth > max_depth) ){
944 l_node *new_left = nebl_new();
945 if(new_left == NULL){
948 new_left->links[0] = NULL; new_left->links[1] = NULL; new_left->links[2] = NULL;
949 new_left->pos = vmd_zero_vector;
950 l_node *new_right = nebl_new();
951 if(new_right == NULL){
952 nebl_delete(new_left);
955 new_right->links[0] = NULL; new_right->links[1] = NULL; new_right->links[2] = NULL;
956 new_right->pos = vmd_zero_vector;
959 new_left->pos = *left;
960 new_left->links[LINK_RIGHT] = new_right;
964 new_right->pos = *right;
965 new_right->links[LINK_LEFT] = new_left;
966 *l_right = new_right;
974 vm_vec_avg( &tmp, left, right );
976 // sometimes generate children
977 if(!child && (frand() <= Nebl_type->b_rand)){
978 // get a point on the plane of the strike
980 vm_vec_random_in_circle(&tmp2, &Nebl_bolt_strike, &Nebl_bolt_dir, Nebl_bolt_len * Nebl_type->b_scale, 0);
982 // maybe move away from the plane
984 vm_vec_sub(&dir, &tmp2, &tmp);
985 vm_vec_scale_add(&tmp2, &tmp, &dir, Nebl_type->b_shrink);
989 if(!nebl_gen(&tmp, &tmp2, 0, 2, 1, &child_node, &argh)){
990 if(child_node != NULL){
991 nebl_release(child_node);
997 float scaler = 0.30f;
998 tmp.x += (frand()-0.5f)*d*scaler;
999 tmp.y += (frand()-0.5f)*d*scaler;
1000 tmp.z += (frand()-0.5f)*d*scaler;
1002 // generate left half
1005 if(!nebl_gen( left, &tmp, depth+1, max_depth, child, &ll, &lr )){
1006 if(child_node != NULL){
1007 nebl_release(child_node);
1015 // generate right half
1018 if(!nebl_gen( &tmp, right, depth+1, max_depth, child, &rl, &rr )){
1019 if(child_node != NULL){
1020 nebl_release(child_node);
1031 // splice the two together
1032 lr->links[LINK_RIGHT] = rl->links[LINK_RIGHT];
1033 lr->links[LINK_RIGHT]->links[LINK_LEFT] = lr;
1036 // if we generated a child, stick him on
1037 if(child_node != NULL){
1038 lr->links[LINK_CHILD] = child_node;
1049 // output top and bottom vectors
1050 // fvec == forward vector (eye viewpoint basically. in world coords)
1051 // pos == world coordinate of the point we're calculating "around"
1052 // w == width of the diff between top and bottom around pos
1053 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1060 vm_vec_sub( &rvec, &Eye_position, &temp );
1061 vm_vec_normalize( &rvec );
1063 vm_vec_crossprod(&uvec,fvec,&rvec);
1064 vm_vec_normalize(&uvec);
1066 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1067 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1069 vm_vec_scale_add2( top, &rvec, z_add );
1070 vm_vec_scale_add2( bot, &rvec, z_add );
1073 // render a section of the bolt
1074 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b)
1077 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
1080 // Sets mode. Returns previous mode.
1081 gr_zbuffer_set(GR_ZBUFF_FULL);
1084 for(idx=0; idx<2; idx++){
1086 v[0].u = 0.0f; v[0].v = 0.0f;
1088 v[1] = a->vex[idx+1];
1089 v[1].u = 1.0f; v[1].v = 0.0f;
1091 v[2] = b->vex[idx+1];
1092 v[2].u = 1.0f; v[2].v = 1.0f;
1095 v[3].u = 0.0f; v[3].v = 1.0f;
1098 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha);
1099 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1104 v[0].u = 0.0f; v[0].v = 0.0f;
1107 v[1].u = 1.0f; v[1].v = 0.0f;
1110 v[2].u = 1.0f; v[2].v = 1.0f;
1113 v[3].u = 0.0f; v[3].v = 1.0f;
1115 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha);
1116 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1118 // draw the glow beam
1119 verts[0] = &a->glow_vex[0];
1120 verts[0]->v = 0.0f; verts[0]->u = 0.0f;
1122 verts[1] = &a->glow_vex[1];
1123 verts[1]->v = 1.0f; verts[1]->u = 0.0f;
1125 verts[2] = &b->glow_vex[1];
1126 verts[2]->v = 1.0f; verts[2]->u = 1.0f;
1128 verts[3] = &b->glow_vex[0];
1129 verts[3]->v = 0.0f; verts[3]->u = 1.0f;
1131 gr_set_bitmap(bi->glow, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_glow_alpha);
1132 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1135 // generate a section
1136 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)
1143 vector glow_a, glow_b;
1146 vm_vec_sub(&dir, &a->pos, &b->pos);
1147 vm_vec_copy_normalize(&dir_normal, &dir);
1148 vm_vector_2_matrix(&m, &dir_normal, NULL, NULL);
1150 // distance to player
1151 float bang_dist = vm_vec_dist_quick(&Eye_position, &a->pos);
1152 if(bang_dist < Nebl_bang){
1153 Nebl_bang = bang_dist;
1156 // rotate the basic section into world
1157 for(idx=0; idx<3; idx++){
1160 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1162 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1163 vm_vec_rotate(&pt, &temp, &m);
1165 vm_vec_add2(&pt, &a->pos);
1168 g3_rotate_vertex(&c->vex[idx], &pt);
1169 g3_project_vertex(&c->vex[idx]);
1171 // if first frame, keep track of the average screen pos
1172 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)){
1173 Nebl_flash_x += c->vex[idx].sx;
1174 Nebl_flash_y += c->vex[idx].sy;
1178 // calculate the glow points
1179 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &a->pos, pinch_a ? 0.5f : width * 6.0f, Nebl_type->b_add);
1180 g3_rotate_vertex(&c->glow_vex[0], &glow_a);
1181 g3_project_vertex(&c->glow_vex[0]);
1182 g3_rotate_vertex(&c->glow_vex[1], &glow_b);
1183 g3_project_vertex(&c->glow_vex[1]);
1187 // rotate the basic section into world
1188 for(idx=0; idx<3; idx++){
1191 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1193 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1194 vm_vec_rotate(&pt, &temp, &m);
1196 vm_vec_add2(&pt, &b->pos);
1199 g3_rotate_vertex(&cap->vex[idx], &pt);
1200 g3_project_vertex(&cap->vex[idx]);
1202 // if first frame, keep track of the average screen pos
1203 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)){
1204 Nebl_flash_x += c->vex[idx].sx;
1205 Nebl_flash_y += c->vex[idx].sy;
1210 // calculate the glow points
1211 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &b->pos, pinch_b ? 0.5f : width * 6.0f, bi->b_add);
1212 g3_rotate_vertex(&cap->glow_vex[0], &glow_a);
1213 g3_project_vertex(&cap->glow_vex[0]);
1214 g3_rotate_vertex(&cap->glow_vex[1], &glow_b);
1215 g3_project_vertex(&cap->glow_vex[1]);
1220 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev)
1224 l_section child_start;
1231 // if prev is NULL, we're just starting so we need our start point
1233 Assert(whee->links[LINK_RIGHT] != NULL);
1234 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &start, NULL, 1, 0);
1239 // if we have a child section
1240 if(whee->links[LINK_CHILD]){
1242 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);
1245 nebl_render_section(bi, &child_start, &end);
1248 if(whee->links[LINK_CHILD]->links[LINK_RIGHT] != NULL){
1249 nebl_render(bi, whee->links[LINK_CHILD], width * 0.5f, &end);
1253 // if the next section is an end section
1254 if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] == NULL){
1258 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &temp, &end, 0, 1);
1260 // render the section
1261 nebl_render_section(bi, &start, &end);
1264 else if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] != NULL){
1266 nebl_generate_section(bi, width, whee->links[LINK_RIGHT], whee->links[LINK_RIGHT]->links[LINK_RIGHT], &end, NULL, 0, 0);
1268 // render the section
1269 nebl_render_section(bi, &start, &end);
1271 // recurse through him
1272 nebl_render(bi, whee->links[LINK_RIGHT], width, &end);
1276 // given a valid, complete bolt, jitter him based upon his noise
1277 void nebl_jitter(l_bolt *b)
1283 bolt_type *bi = NULL;
1289 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
1292 bi = &Bolt_types[b->type];
1294 // get the bolt direction
1295 vm_vec_sub(&temp, &b->strike, &b->start);
1296 length = vm_vec_normalize_quick(&temp);
1297 vm_vector_2_matrix(&m, &temp, NULL, NULL);
1299 // jitter all nodes on the main trunk
1301 while(moveup != NULL){
1303 vm_vec_random_in_circle(&moveup->pos, &temp, &m, frand_range(0.0f, length * bi->noise), 0);
1305 // just on the main trunk
1306 moveup = moveup->links[LINK_RIGHT];
1310 // return the index of a given bolt type by name
1311 int nebl_get_bolt_index(char *name)
1315 for(idx=0; idx<Num_bolt_types; idx++){
1316 if(!strcmp(name, Bolt_types[idx].name)){
1324 // return the index of a given storm type by name
1325 int nebl_get_storm_index(char *name)
1329 for(idx=0; idx<Num_bolt_types; idx++){
1330 if(!strcmp(name, Storm_types[idx].name)){