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.5 2003/05/25 02:30:43 taylor
21 * Revision 1.4 2002/06/17 06:33:09 relnev
22 * ryan's struct patch for gcc 2.95
24 * Revision 1.3 2002/06/09 04:41:23 relnev
25 * added copyright header
27 * Revision 1.2 2002/05/07 03:16:47 theoddone33
28 * The Great Newline Fix
30 * Revision 1.1.1.1 2002/05/03 03:28:10 root
34 * 10 8/15/99 3:50p Dave
35 * Don't process lightning at the very beginning of a mission.
37 * 9 8/12/99 10:38a Anoop
38 * Removed unnecessary Int3().
40 * 8 8/05/99 2:06a Dave
43 * 7 7/27/99 9:51p Andsager
44 * make mprintf's into nprintf's
46 * 6 7/03/99 5:50p Dave
47 * Make rotated bitmaps draw properly in padlock views.
49 * 5 7/02/99 4:31p Dave
50 * Much more sophisticated lightning support.
52 * 4 6/09/99 10:32a Dave
53 * Made random lighting bolts behave more like the E3 demo. Generally more
56 * 3 5/26/99 11:46a Dave
57 * Added ship-blasting lighting and made the randomization of lighting
58 * much more customizable.
60 * 2 5/24/99 5:45p Dave
61 * Added detail levels to the nebula, with a decent speedup. Split nebula
62 * lightning into its own section.
71 #include "freespace.h"
74 #include "missionparse.h"
76 #include "neblightning.h"
79 #include "multimsgs.h"
81 // ------------------------------------------------------------------------------------------------------
82 // NEBULA LIGHTNING DEFINES/VARS
86 #define MAX_BOLT_TYPES_INTERNAL 11
88 // see lightning.tbl for explanations of these values
89 typedef struct bolt_type {
90 char name[NAME_LENGTH];
111 int Num_bolt_types = 0;
112 bolt_type Bolt_types[MAX_BOLT_TYPES_INTERNAL];
115 int Num_storm_types = 0;
116 storm_type Storm_types[MAX_STORM_TYPES];
119 // actual lightning bolt stuff -------
121 #define MAX_LIGHTNING_NODES 500
123 // nodes in a lightning bolt
127 typedef struct l_node {
128 vector pos; // world position
129 l_node *links[3]; // 3 links for lightning children
131 l_node *next, *prev; // for used and free-lists only
136 l_node Nebl_nodes[MAX_LIGHTNING_NODES];
139 // lightning node lists
140 l_node Nebl_free_list;
141 l_node Nebl_used_list;
143 // actual lightning bolt themselves
144 typedef struct l_bolt {
145 l_node *head; // head of the lightning bolt
146 int bolt_life; // remaining life timestamp
147 ubyte used; // used or not
148 ubyte first_frame; // if he hasn't been rendered at least once
152 vector start, strike, midpoint;
153 int delay; // delay stamp
154 int strikes_left; // #of strikes left
158 #define MAX_LIGHTNING_BOLTS 10
161 l_bolt Nebl_bolts[MAX_LIGHTNING_BOLTS];
162 int Nebl_bolt_count = 0;
164 // one cross-section of a lightning bolt
165 typedef struct l_section {
170 // points on the basic cross section
171 vector Nebl_ring[3] = {
172 { -1.0f, 0.0f, 0.0f },
173 { 1.0f, 0.70f, 0.0f },
174 { 1.0f, -0.70f, 0.0f }
177 // pinched off cross-section
178 vector Nebl_ring_pinched[3] = {
179 { -0.05f, 0.0f, 0.0f },
180 { 0.05f, 0.035f, 0.0f },
181 { 0.05f, -0.035f, 0.0f }
184 // globals used for rendering and generating bolts
185 int Nebl_flash_count = 0; // # of points rendered onscreen for this bolt
186 float Nebl_flash_x = 0.0f; // avg x of the points rendered
187 float Nebl_flash_y = 0.0f; // avg y of the points rendered
188 float Nebl_bang = 0.0; // distance to the viewer object
189 float Nebl_alpha = 0.0f; // alpha to use when rendering the bolt itself
190 float Nebl_glow_alpha = 0.0f; // alpha to use when rendering the bolt glow
191 int Nebl_stamp = -1; // random timestamp for making bolts
192 float Nebl_bolt_len; // length of the current bolt being generated
193 bolt_type *Nebl_type; // bolt type
194 matrix Nebl_bolt_dir; // orientation matrix of the bolt being generated
195 vector Nebl_bolt_start; // start point of the bolt being generated
196 vector Nebl_bolt_strike; // strike point of the bolt being generated
198 // the type of active storm
199 storm_type *Storm = NULL;
204 dc_get_arg(ARG_FLOAT);
205 Bolt_types[DEBUG_BOLT].b_scale = Dc_arg_float;
209 dc_get_arg(ARG_FLOAT);
210 Bolt_types[DEBUG_BOLT].b_rand = Dc_arg_float;
214 dc_get_arg(ARG_FLOAT);
215 Bolt_types[DEBUG_BOLT].b_shrink = Dc_arg_float;
219 dc_get_arg(ARG_FLOAT);
220 Bolt_types[DEBUG_BOLT].b_poly_pct = Dc_arg_float;
224 dc_get_arg(ARG_FLOAT);
225 Bolt_types[DEBUG_BOLT].b_add = Dc_arg_float;
230 Bolt_types[DEBUG_BOLT].num_strikes = Dc_arg_int;
234 dc_get_arg(ARG_FLOAT);
235 Bolt_types[DEBUG_BOLT].noise = Dc_arg_float;
239 dc_get_arg(ARG_FLOAT);
240 Bolt_types[DEBUG_BOLT].b_bright = Dc_arg_float;
245 Bolt_types[DEBUG_BOLT].lifetime = Dc_arg_int;
249 dc_printf("Debug lightning bolt settings :\n");
251 dc_printf("b_scale : %f\n", Bolt_types[DEBUG_BOLT].b_scale);
252 dc_printf("b_rand : %f\n", Bolt_types[DEBUG_BOLT].b_rand);
253 dc_printf("b_shrink : %f\n", Bolt_types[DEBUG_BOLT].b_shrink);
254 dc_printf("b_poly_pct : %f\n", Bolt_types[DEBUG_BOLT].b_poly_pct);
255 dc_printf("b_add : %f\n", Bolt_types[DEBUG_BOLT].b_add);
256 dc_printf("b_strikes : %d\n", Bolt_types[DEBUG_BOLT].num_strikes);
257 dc_printf("b_noise : %f\n", Bolt_types[DEBUG_BOLT].noise);
258 dc_printf("b_bright : %f\n", Bolt_types[DEBUG_BOLT].b_bright);
259 dc_printf("b_lifetime : %d\n", Bolt_types[DEBUG_BOLT].lifetime);
263 // nebula lightning intensity (0.0 to 1.0)
264 float Nebl_intensity = 0.6667f;
266 // min and max times for random lightning
267 int Nebl_random_min = 750; // min random time
268 int Nebl_random_max = 10000; // max random time
270 // min and max times for cruiser lightning
271 int Nebl_cruiser_min = 5000; // min cruiser time
272 int Nebl_cruiser_max = 25000; // max cruiser time
274 // min and max times for cap ships
275 int Nebl_cap_min = 4000; // min cap time
276 int Nebl_cap_max = 18000; // max cap time
278 // min and max time for super caps
279 int Nebl_supercap_min = 3000; // min supercap time
280 int Nebl_supercap_max = 12000; // max supercap time
282 DCF(lightning_intensity, "")
284 dc_get_arg(ARG_FLOAT);
285 float val = Dc_arg_float;
288 } else if(val > 1.0f){
292 Nebl_intensity = 1.0f - val;
295 // ------------------------------------------------------------------------------------------------------
296 // NEBULA LIGHTNING FORWARD DECLARATIONS
299 // "new" a lightning node
302 // "delete" a lightning node
303 void nebl_delete(l_node *lp);
305 // free up a the nodes of the passed in bolt
306 void nebl_release(l_node *bolt_head);
308 // generate a lightning bolt, returns l_left (the "head") and l_right (the "tail")
309 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right);
311 // output top and bottom vectors
312 // fvec == forward vector (eye viewpoint basically. in world coords)
313 // pos == world coordinate of the point we're calculating "around"
314 // w == width of the diff between top and bottom around pos
315 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add );
317 // render a section of the bolt
318 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b);
320 // generate a section
321 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);
324 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev = NULL);
326 // given a valid, complete bolt, jitter him based upon his noise
327 void nebl_jitter(l_bolt *b);
329 // return the index of a given bolt type by name
330 int nebl_get_bolt_index(char *name);
332 // return the index of a given storm type by name
333 int nebl_get_storm_index(char *name);
336 // ------------------------------------------------------------------------------------------------------
337 // NEBULA LIGHTNING FUNCTIONS
340 // initialize nebula lightning at game startup
344 char name[NAME_LENGTH+10] = "";
345 bolt_type bogus_lightning, *l;
346 storm_type bogus_storm, *s;
349 // parse the lightning table
350 read_file_text("lightning.tbl");
356 memset(Bolt_types, 0, sizeof(bolt_type) * MAX_BOLT_TYPES_INTERNAL);
358 // parse the individual lightning bolt types
359 required_string("#Bolts begin");
360 while(!optional_string("#Bolts end")){
362 if(Num_bolt_types >= MAX_BOLT_TYPES){
363 l = &bogus_lightning;
365 l = &Bolt_types[Num_bolt_types];
369 required_string("$Bolt:");
370 stuff_string(l->name, F_NAME, NULL);
373 required_string("+b_scale:");
374 stuff_float(&l->b_scale);
377 required_string("+b_shrink:");
378 stuff_float(&l->b_shrink);
381 required_string("+b_poly_pct:");
382 stuff_float(&l->b_poly_pct);
385 required_string("+b_rand:");
386 stuff_float(&l->b_rand);
389 required_string("+b_add:");
390 stuff_float(&l->b_add);
393 required_string("+b_strikes:");
394 stuff_int(&l->num_strikes);
397 required_string("+b_lifetime:");
398 stuff_int(&l->lifetime);
401 required_string("+b_noise:");
402 stuff_float(&l->noise);
405 required_string("+b_emp:");
406 stuff_float(&l->emp_intensity);
407 stuff_float(&l->emp_time);
410 required_string("+b_texture:");
411 stuff_string(name, F_NAME, NULL);
412 if((l != &bogus_lightning) && !Fred_running){
413 l->texture = bm_load(name);
417 required_string("+b_glow:");
418 stuff_string(name, F_NAME, NULL);
419 if((l != &bogus_lightning) && !Fred_running){
420 l->glow = bm_load(name);
424 required_string("+b_bright:");
425 stuff_float(&l->b_bright);
427 // increment the # of bolt types
428 if(l != &bogus_lightning){
433 // copy the first bolt to the debug bolt
434 memcpy(&Bolt_types[DEBUG_BOLT], &Bolt_types[0], sizeof(bolt_type));
437 required_string("#Storms begin");
438 while(!optional_string("#Storms end")){
440 if(Num_storm_types >= MAX_STORM_TYPES){
443 s = &Storm_types[Num_storm_types];
447 required_string("$Storm:");
448 stuff_string(s->name, F_NAME, NULL);
451 s->num_bolt_types = 0;
452 while(optional_string("+bolt:")){
453 stuff_string(name, F_NAME, NULL);
456 if(s->num_bolt_types < MAX_BOLT_TYPES){
457 s->bolt_types[s->num_bolt_types] = (char)nebl_get_bolt_index(name);
458 Assert(s->bolt_types[s->num_bolt_types] != -1);
464 required_string("+bolt_prec:");
470 required_string("+flavor:");
471 stuff_float(&s->flavor.xyz.x);
472 stuff_float(&s->flavor.xyz.y);
473 stuff_float(&s->flavor.xyz.z);
476 required_string("+random_freq:");
481 required_string("+random_count:");
482 stuff_int(&s->min_count);
483 stuff_int(&s->max_count);
485 // increment the # of bolt types
486 if(s != &bogus_storm){
493 // initialize lightning before entering a level
494 void nebl_level_init()
498 // zero all lightning bolts
499 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
500 Nebl_bolts[idx].head = NULL;
501 Nebl_bolts[idx].bolt_life = -1;
502 Nebl_bolts[idx].used = 0;
505 // initialize node list
507 list_init( &Nebl_free_list );
508 list_init( &Nebl_used_list );
510 // Link all object slots into the free list
511 for (idx=0; idx<MAX_LIGHTNING_NODES; idx++) {
512 list_append(&Nebl_free_list, &Nebl_nodes[idx] );
515 // zero the random timestamp
518 // null the storm. let mission parsing set it up
522 // render all lightning bolts
523 void nebl_render_all()
529 // no lightning in non-nebula missions
530 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
534 // if we have no storm
540 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
541 b = &Nebl_bolts[idx];
543 // if this is being used
545 Assert(b->head != NULL);
552 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
557 bi = &Bolt_types[b->type];
559 // if this guy is still on a delay
561 if(timestamp_elapsed(b->delay)){
568 // if the timestamp on this guy has expired
569 if((b->bolt_life < 0) || timestamp_elapsed(b->bolt_life)){
570 // if this is a multiple strike bolt, jitter it and reset
571 if(b->strikes_left-1 > 0){
572 b->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
577 // by continuing here we skip rendering for one frame, which makes it look more like real lightning
580 // otherwise he's completely done, so release him
582 // maybe free up node data
584 nebl_release(b->head);
589 nprintf(("lightning", "Released bolt. %d used nodes!\n", Num_lnodes));
596 // pick some cool alpha values
597 Nebl_alpha = frand();
598 Nebl_glow_alpha = frand();
600 // otherwise render him
601 Nebl_flash_count = 0;
604 Nebl_bang = 10000000.0f;
605 nebl_render(bi, b->head, b->width);
607 // if this is the first frame he has been rendered, determine if we need to make a flash and sound effect
613 // if we rendered any points
614 if(Nebl_flash_count){
615 Nebl_flash_x /= (float)Nebl_flash_count;
616 Nebl_flash_y /= (float)Nebl_flash_count;
618 // quick distance from the center of the screen
619 float x = Nebl_flash_x - (gr_screen.max_w / 2.0f);
620 float y = Nebl_flash_y - (gr_screen.max_h / 2.0f);
621 float dist = fl_sqrt((x * x) + (y * y));
622 if(dist / (gr_screen.max_w / 2.0f) < 1.0f){
623 flash = 1.0f - (dist / (gr_screen.max_w / 2.0f));
625 // scale the flash by bolt type
626 flash *= bi->b_bright;
628 game_flash(flash, flash, flash);
631 // do some special stuff on the very first strike of the bolt
632 if(b->strikes_left == bi->num_strikes){
635 if(Nebl_bang < 40.0f){
637 } else if(Nebl_bang > 400.0f){
640 bang = 1.0f - (Nebl_bang / 400.0f);
642 if(frand_range(0.0f, 1.0f) < 0.5f){
643 snd_play(&Snds[SND_LIGHTNING_2], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
645 snd_play(&Snds[SND_LIGHTNING_1], 0.0f, bang, SND_PRIORITY_DOUBLE_INSTANCE);
649 if(bi->emp_intensity > 0.0f){
650 emp_apply(&b->midpoint, 0.0f, vm_vec_dist(&b->start, &b->strike), bi->emp_intensity, bi->emp_time);
659 // process lightning (randomly generate bolts, etc, etc);
664 // non-nebula mission
665 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
669 // non servers in multiplayer don't do this
670 if((Game_mode & GM_MULTIPLAYER) && !MULTIPLAYER_MASTER){
674 // if there's no chosen storm
679 // don't process lightning bolts unless we're a few seconds in
680 if(f2fl(Missiontime) < 3.0f){
685 if(Nebl_stamp == -1){
686 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
691 if(timestamp_elapsed(Nebl_stamp)){
692 // determine how many bolts to spew
693 num_bolts = (int)frand_range((float)Storm->min_count, (float)Storm->max_count);
694 for(idx=0; idx<num_bolts; idx++){
695 // hmm. for now just pick a random bolt type and run with it
699 s1 = (int)frand_range(0.0f, (float)Neb2_slices);
700 s2 = (int)frand_range(0.0f, (float)Neb2_slices);
701 s3 = (int)frand_range(0.0f, (float)Neb2_slices);
703 e1 = (int)frand_range(0.0f, (float)Neb2_slices);
704 e2 = (int)frand_range(0.0f, (float)Neb2_slices);
705 e3 = (int)frand_range(0.0f, (float)Neb2_slices);
707 // never choose the middle cube
708 if((s1 == 2) && (s2 == 2) && (s3 == 2)){
712 if((e1 == 2) && (e2 == 2) && (e3 == 2)){
718 } while((s1 == e1) && (s2 == e2) && (s3 == e3));
720 vector start = Neb2_cubes[s1][s2][s3].pt;
721 vector strike = Neb2_cubes[e1][e2][e3].pt;
723 // add some flavor to the bolt. mmmmmmmm, lightning
724 if(!IS_VEC_NULL(&Storm->flavor)){
725 // start with your basic hot sauce. measure how much you have
726 vector your_basic_hot_sauce;
727 vm_vec_sub(&your_basic_hot_sauce, &strike, &start);
728 float how_much_hot_sauce = vm_vec_normalize(&your_basic_hot_sauce);
730 // now figure out how much of that good wing sauce to add
731 vector wing_sauce = Storm->flavor;
732 if(frand_range(0.0, 1.0f) < 0.5f){
733 vm_vec_scale(&wing_sauce, -1.0f);
735 float how_much_of_that_good_wing_sauce_to_add = vm_vec_normalize(&wing_sauce);
737 // mix the two together, taking care not to add too much
739 if(how_much_of_that_good_wing_sauce_to_add > 1000.0f){
740 how_much_of_that_good_wing_sauce_to_add = 1000.0f;
742 vm_vec_interp_constant(&the_mixture, &your_basic_hot_sauce, &wing_sauce, how_much_of_that_good_wing_sauce_to_add / 1000.0f);
744 // take the final sauce and store it in the proper container
745 vm_vec_scale(&the_mixture, how_much_hot_sauce);
747 // make sure to put it on everything! whee!
748 vm_vec_add(&strike, &start, &the_mixture);
751 int type = (int)frand_range(0.0f, (float)(Storm->num_bolt_types-1));
752 nebl_bolt(Storm->bolt_types[type], &start, &strike);
755 // reset the timestamp
756 Nebl_stamp = timestamp((int)frand_range((float)Storm->min, (float)Storm->max));
760 // create a lightning bolt
761 void nebl_bolt(int type, vector *start, vector *strike)
771 if(!(The_mission.flags & MISSION_FLAG_FULLNEB)){
777 for(idx=0; idx<MAX_LIGHTNING_BOLTS; idx++){
778 if(!Nebl_bolts[idx].used){
788 if((type < 0) || ((type >= Num_bolt_types) && (type != DEBUG_BOLT)) ){
791 bi = &Bolt_types[type];
793 // get a pointer to the bolt
794 bolt = &Nebl_bolts[idx];
797 bolt->start = *start;
798 bolt->strike = *strike;
799 bolt->strikes_left = bi->num_strikes;
801 bolt->type = (char)type;
802 bolt->first_frame = 1;
803 bolt->bolt_life = timestamp(bi->lifetime / bi->num_strikes);
805 Nebl_bolt_start = *start;
806 Nebl_bolt_strike = *strike;
809 if(bolt->delay != -1){
810 bolt->delay = timestamp(bolt->delay);
813 // setup the rest of the important bolt data
814 if(vm_vec_same(&Nebl_bolt_start, &Nebl_bolt_strike)){
815 Nebl_bolt_strike.xyz.z += 150.0f;
817 Nebl_bolt_len = vm_vec_dist(&Nebl_bolt_start, &Nebl_bolt_strike);
818 vm_vec_sub(&dir, &Nebl_bolt_strike, &Nebl_bolt_start);
821 vm_vec_scale_add(&bolt->midpoint, &Nebl_bolt_start, &dir, 0.5f);
823 bolt_len = vm_vec_normalize(&dir);
824 vm_vector_2_matrix(&Nebl_bolt_dir, &dir, NULL, NULL);
826 // global type for generating the bolt
829 // try and make the bolt
830 if(!nebl_gen(&Nebl_bolt_start, &Nebl_bolt_strike, 0, 4, 0, &bolt->head, &tail)){
831 if(bolt->head != NULL){
832 nebl_release(bolt->head);
840 // setup the rest of the data
842 bolt->width = bi->b_poly_pct * bolt_len;
844 // if i'm a multiplayer master, send a bolt packet
845 if(MULTIPLAYER_MASTER){
846 send_lightning_packet(type, start, strike);
850 // get the current # of active lightning bolts
851 int nebl_get_active_bolts()
853 return Nebl_bolt_count;
856 // get the current # of active nodes
857 int nebl_get_active_nodes()
862 // set the storm (call from mission parse)
863 void nebl_set_storm(char *name)
865 int index = nebl_get_storm_index(name);
869 if((index >= 0) && (index < Num_storm_types)){
870 Storm = &Storm_types[index];
874 // ------------------------------------------------------------------------------------------------------
875 // NEBULA LIGHTNING FORWARD DEFINITIONS
878 // "new" a lightning node
883 // if we're out of nodes
884 if(Num_lnodes >= MAX_LIGHTNING_NODES){
886 nprintf(("lightning", "Out of lightning nodes!\n"));
890 // get a new node off the freelist
891 lp = GET_FIRST(&Nebl_free_list);
892 Assert( lp != &Nebl_free_list ); // shouldn't have the dummy element
894 // remove trailp from the free list
895 list_remove( &Nebl_free_list, lp );
897 // insert trailp onto the end of used list
898 list_append( &Nebl_used_list, lp );
907 // return the pointer
911 // "delete" a lightning node
912 void nebl_delete(l_node *lp)
914 // remove objp from the used list
915 list_remove( &Nebl_used_list, lp );
917 // add objp to the end of the free
918 list_append( &Nebl_free_list, lp );
924 // free a lightning bolt
925 void nebl_release(l_node *whee)
932 // release all of our children
933 if(whee->links[LINK_RIGHT] != NULL){
934 nebl_release(whee->links[LINK_RIGHT]);
936 if(whee->links[LINK_CHILD] != NULL){
937 nebl_release(whee->links[LINK_CHILD]);
944 int nebl_gen(vector *left, vector *right, float depth, float max_depth, int child, l_node **l_left, l_node **l_right)
946 l_node *child_node = NULL;
947 float d = vm_vec_dist_quick( left, right );
949 // if we've reached the critical point
950 if ( d < 0.30f || (depth > max_depth) ){
952 l_node *new_left = nebl_new();
953 if(new_left == NULL){
956 new_left->links[0] = NULL; new_left->links[1] = NULL; new_left->links[2] = NULL;
957 new_left->pos = vmd_zero_vector;
958 l_node *new_right = nebl_new();
959 if(new_right == NULL){
960 nebl_delete(new_left);
963 new_right->links[0] = NULL; new_right->links[1] = NULL; new_right->links[2] = NULL;
964 new_right->pos = vmd_zero_vector;
967 new_left->pos = *left;
968 new_left->links[LINK_RIGHT] = new_right;
972 new_right->pos = *right;
973 new_right->links[LINK_LEFT] = new_left;
974 *l_right = new_right;
982 vm_vec_avg( &tmp, left, right );
984 // sometimes generate children
985 if(!child && (frand() <= Nebl_type->b_rand)){
986 // get a point on the plane of the strike
988 vm_vec_random_in_circle(&tmp2, &Nebl_bolt_strike, &Nebl_bolt_dir, Nebl_bolt_len * Nebl_type->b_scale, 0);
990 // maybe move away from the plane
992 vm_vec_sub(&dir, &tmp2, &tmp);
993 vm_vec_scale_add(&tmp2, &tmp, &dir, Nebl_type->b_shrink);
997 if(!nebl_gen(&tmp, &tmp2, 0, 2, 1, &child_node, &argh)){
998 if(child_node != NULL){
999 nebl_release(child_node);
1005 float scaler = 0.30f;
1006 tmp.xyz.x += (frand()-0.5f)*d*scaler;
1007 tmp.xyz.y += (frand()-0.5f)*d*scaler;
1008 tmp.xyz.z += (frand()-0.5f)*d*scaler;
1010 // generate left half
1013 if(!nebl_gen( left, &tmp, depth+1, max_depth, child, &ll, &lr )){
1014 if(child_node != NULL){
1015 nebl_release(child_node);
1023 // generate right half
1026 if(!nebl_gen( &tmp, right, depth+1, max_depth, child, &rl, &rr )){
1027 if(child_node != NULL){
1028 nebl_release(child_node);
1039 // splice the two together
1040 lr->links[LINK_RIGHT] = rl->links[LINK_RIGHT];
1041 lr->links[LINK_RIGHT]->links[LINK_LEFT] = lr;
1044 // if we generated a child, stick him on
1045 if(child_node != NULL){
1046 lr->links[LINK_CHILD] = child_node;
1057 // output top and bottom vectors
1058 // fvec == forward vector (eye viewpoint basically. in world coords)
1059 // pos == world coordinate of the point we're calculating "around"
1060 // w == width of the diff between top and bottom around pos
1061 void nebl_calc_facing_pts_smart( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1068 vm_vec_sub( &rvec, &Eye_position, &temp );
1069 vm_vec_normalize( &rvec );
1071 vm_vec_crossprod(&uvec,fvec,&rvec);
1072 vm_vec_normalize(&uvec);
1074 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1075 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1077 vm_vec_scale_add2( top, &rvec, z_add );
1078 vm_vec_scale_add2( bot, &rvec, z_add );
1081 // render a section of the bolt
1082 void nebl_render_section(bolt_type *bi, l_section *a, l_section *b)
1085 vertex *verts[4] = {&v[0], &v[1], &v[2], &v[3]};
1088 // Sets mode. Returns previous mode.
1089 gr_zbuffer_set(GR_ZBUFF_FULL);
1092 for(idx=0; idx<2; idx++){
1094 v[0].u = 0.0f; v[0].v = 0.0f;
1096 v[1] = a->vex[idx+1];
1097 v[1].u = 1.0f; v[1].v = 0.0f;
1099 v[2] = b->vex[idx+1];
1100 v[2].u = 1.0f; v[2].v = 1.0f;
1103 v[3].u = 0.0f; v[3].v = 1.0f;
1106 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha);
1107 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1112 v[0].u = 0.0f; v[0].v = 0.0f;
1115 v[1].u = 1.0f; v[1].v = 0.0f;
1118 v[2].u = 1.0f; v[2].v = 1.0f;
1121 v[3].u = 0.0f; v[3].v = 1.0f;
1123 gr_set_bitmap(bi->texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_alpha);
1124 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1126 // draw the glow beam
1127 verts[0] = &a->glow_vex[0];
1128 verts[0]->v = 0.0f; verts[0]->u = 0.0f;
1130 verts[1] = &a->glow_vex[1];
1131 verts[1]->v = 1.0f; verts[1]->u = 0.0f;
1133 verts[2] = &b->glow_vex[1];
1134 verts[2]->v = 1.0f; verts[2]->u = 1.0f;
1136 verts[3] = &b->glow_vex[0];
1137 verts[3]->v = 0.0f; verts[3]->u = 1.0f;
1139 gr_set_bitmap(bi->glow, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Nebl_glow_alpha);
1140 g3_draw_poly(4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1143 // generate a section
1144 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)
1151 vector glow_a, glow_b;
1154 vm_vec_sub(&dir, &a->pos, &b->pos);
1155 vm_vec_copy_normalize(&dir_normal, &dir);
1156 vm_vector_2_matrix(&m, &dir_normal, NULL, NULL);
1158 // distance to player
1159 float bang_dist = vm_vec_dist_quick(&Eye_position, &a->pos);
1160 if(bang_dist < Nebl_bang){
1161 Nebl_bang = bang_dist;
1164 // rotate the basic section into world
1165 for(idx=0; idx<3; idx++){
1168 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1170 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1171 vm_vec_rotate(&pt, &temp, &m);
1173 vm_vec_add2(&pt, &a->pos);
1176 g3_rotate_vertex(&c->vex[idx], &pt);
1177 g3_project_vertex(&c->vex[idx]);
1179 // if first frame, keep track of the average screen pos
1180 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)){
1181 Nebl_flash_x += c->vex[idx].sx;
1182 Nebl_flash_y += c->vex[idx].sy;
1186 // calculate the glow points
1187 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &a->pos, pinch_a ? 0.5f : width * 6.0f, Nebl_type->b_add);
1188 g3_rotate_vertex(&c->glow_vex[0], &glow_a);
1189 g3_project_vertex(&c->glow_vex[0]);
1190 g3_rotate_vertex(&c->glow_vex[1], &glow_b);
1191 g3_project_vertex(&c->glow_vex[1]);
1195 // rotate the basic section into world
1196 for(idx=0; idx<3; idx++){
1199 vm_vec_rotate(&pt, &Nebl_ring_pinched[idx], &m);
1201 vm_vec_copy_scale(&temp, &Nebl_ring[idx], width);
1202 vm_vec_rotate(&pt, &temp, &m);
1204 vm_vec_add2(&pt, &b->pos);
1207 g3_rotate_vertex(&cap->vex[idx], &pt);
1208 g3_project_vertex(&cap->vex[idx]);
1210 // if first frame, keep track of the average screen pos
1211 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)){
1212 Nebl_flash_x += c->vex[idx].sx;
1213 Nebl_flash_y += c->vex[idx].sy;
1218 // calculate the glow points
1219 nebl_calc_facing_pts_smart(&glow_a, &glow_b, &dir_normal, &b->pos, pinch_b ? 0.5f : width * 6.0f, bi->b_add);
1220 g3_rotate_vertex(&cap->glow_vex[0], &glow_a);
1221 g3_project_vertex(&cap->glow_vex[0]);
1222 g3_rotate_vertex(&cap->glow_vex[1], &glow_b);
1223 g3_project_vertex(&cap->glow_vex[1]);
1228 void nebl_render(bolt_type *bi, l_node *whee, float width, l_section *prev)
1232 l_section child_start;
1239 // if prev is NULL, we're just starting so we need our start point
1241 Assert(whee->links[LINK_RIGHT] != NULL);
1242 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &start, NULL, 1, 0);
1247 // if we have a child section
1248 if(whee->links[LINK_CHILD]){
1250 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);
1253 nebl_render_section(bi, &child_start, &end);
1256 if(whee->links[LINK_CHILD]->links[LINK_RIGHT] != NULL){
1257 nebl_render(bi, whee->links[LINK_CHILD], width * 0.5f, &end);
1261 // if the next section is an end section
1262 if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] == NULL){
1266 nebl_generate_section(bi, width, whee, whee->links[LINK_RIGHT], &temp, &end, 0, 1);
1268 // render the section
1269 nebl_render_section(bi, &start, &end);
1272 else if(whee->links[LINK_RIGHT]->links[LINK_RIGHT] != NULL){
1274 nebl_generate_section(bi, width, whee->links[LINK_RIGHT], whee->links[LINK_RIGHT]->links[LINK_RIGHT], &end, NULL, 0, 0);
1276 // render the section
1277 nebl_render_section(bi, &start, &end);
1279 // recurse through him
1280 nebl_render(bi, whee->links[LINK_RIGHT], width, &end);
1284 // given a valid, complete bolt, jitter him based upon his noise
1285 void nebl_jitter(l_bolt *b)
1291 bolt_type *bi = NULL;
1297 if((b->type < 0) || ((b->type >= Num_bolt_types) && (b->type != DEBUG_BOLT)) ){
1300 bi = &Bolt_types[b->type];
1302 // get the bolt direction
1303 vm_vec_sub(&temp, &b->strike, &b->start);
1304 length = vm_vec_normalize_quick(&temp);
1305 vm_vector_2_matrix(&m, &temp, NULL, NULL);
1307 // jitter all nodes on the main trunk
1309 while(moveup != NULL){
1311 vm_vec_random_in_circle(&moveup->pos, &temp, &m, frand_range(0.0f, length * bi->noise), 0);
1313 // just on the main trunk
1314 moveup = moveup->links[LINK_RIGHT];
1318 // return the index of a given bolt type by name
1319 int nebl_get_bolt_index(char *name)
1323 for(idx=0; idx<Num_bolt_types; idx++){
1324 if(!strcmp(name, Bolt_types[idx].name)){
1332 // return the index of a given storm type by name
1333 int nebl_get_storm_index(char *name)
1337 for(idx=0; idx<Num_bolt_types; idx++){
1338 if(!strcmp(name, Storm_types[idx].name)){