2 * $Logfile: /Freespace2/code/Weapon/Beam.cpp $
7 * all sorts of cool stuff about ships
10 * Revision 1.1 2002/05/03 03:28:11 root
14 * 68 9/09/99 11:40p Dave
15 * Handle an Assert() in beam code. Added supernova sounds. Play the right
16 * 2 end movies properly, based upon what the player did in the mission.
18 * 67 9/09/99 2:36p Mikek
19 * Put back in the "1.0f +" in BEAM_TYPE_A aiming. Not the best way to
22 * 65 9/08/99 10:29p Dave
23 * Make beam sound pausing and unpausing much safer.
25 * 64 9/06/99 12:46a Andsager
26 * Add weapon_explosion_ani LOD
28 * 63 9/03/99 5:12p Mikek
29 * Change miss_factor code, making it a lot more likely for beams to miss
30 * and also making them increasingly likely to miss with each subsequent
31 * shot in the multi-shot burst.
33 * 62 8/30/99 5:01p Dave
34 * Made d3d do less state changing in the nebula. Use new chat server for
37 * 61 8/28/99 7:29p Dave
38 * Fixed wingmen persona messaging. Make sure locked turrets don't count
39 * towards the # attacking a player.
41 * 60 8/27/99 9:07p Dave
42 * LOD explosions. Improved beam weapon accuracy.
44 * 59 8/26/99 10:15a Dave
45 * Don't apply beam whacks to docked ships.
47 * 58 7/31/99 1:16p Dave
48 * Use larger font for 1024 HUD flash text box. Make beam weapons aware of
49 * weapon subsystem damage on firing ship.
51 * 57 7/22/99 4:00p Dave
52 * Fixed beam weapon muzzle glow rendering. Externalized hud shield info.
54 * 56 7/19/99 7:20p Dave
55 * Beam tooling. Specialized player-killed-self messages. Fixed d3d nebula
58 * 55 7/15/99 9:20a Andsager
59 * FS2_DEMO initial checkin
61 * 54 7/09/99 5:54p Dave
62 * Seperated cruiser types into individual types. Added tons of new
63 * briefing icons. Campaign screen.
65 * 53 7/08/99 10:53a Dave
66 * New multiplayer interpolation scheme. Not 100% done yet, but still
67 * better than the old way.
69 * 52 7/02/99 10:51p Dave
70 * Limit friendly beam fire damage. :(
72 * 51 7/01/99 11:44a Dave
73 * Updated object sound system to allow multiple obj sounds per ship.
74 * Added hit-by-beam sound. Added killed by beam sound.
76 * 50 6/29/99 7:39p Dave
77 * Lots of small bug fixes.
79 * 49 6/29/99 2:53p Dave
80 * Re-enabled beam lighting.
82 * 48 6/25/99 3:04p Dave
85 * 47 6/24/99 3:00p Dave
88 * 46 6/23/99 4:49p Dave
89 * Temporarily removed beam lighting.
91 * 45 6/21/99 7:25p Dave
92 * netplayer pain packet. Added type E unmoving beams.
94 * 44 6/18/99 5:16p Dave
95 * Added real beam weapon lighting. Fixed beam weapon sounds. Added MOTD
96 * dialog to PXO screen.
98 * 43 6/14/99 10:45a Dave
99 * Made beam weapons specify accuracy by skill level in the weapons.tbl
101 * 42 6/09/99 2:55p Andsager
102 * Allow multiple asteroid subtypes (of large, medium, small) and follow
105 * 41 6/08/99 6:02p Jamesa
106 * Handle case where type B beam start and end directions are really
109 * 40 6/04/99 2:16p Dave
110 * Put in shrink effect for beam weapons.
112 * 39 5/14/99 11:47a Andsager
113 * Added beam_get_weapon_info_index(object *bm)
115 * 38 5/12/99 10:43a Andsager
116 * Increase max beam length
118 * 37 5/08/99 8:25p Dave
119 * Upped object pairs. First run of nebula lightning.
121 * 36 5/05/99 9:02p Dave
122 * Fixed D3D aabitmap rendering. Spiffed up nebula effect a bit (added
123 * rotations, tweaked values, made bitmap selection more random). Fixed
124 * D3D beam weapon clipping problem. Added D3d frame dumping.
126 * 35 5/03/99 9:07a Dave
127 * Pirate Bob. Changed beam test code a bit.
129 * 34 4/27/99 12:16a Dave
130 * Fixed beam weapon muzzle glow problem. Fixed premature timeout on the
131 * pxo server list screen. Fixed secondary firing for hosts on a
132 * standalone. Fixed wacky multiplayer weapon "shuddering" problem.
134 * 33 4/25/99 6:12p Johnson
135 * Fixed bug where multi-shot beams were getting the shot count from the
138 * 32 4/25/99 3:36p Dave
139 * Fixed nebula table code. Tweaked beam weapon explosion stuff.
141 * 31 4/23/99 2:33p Johnson
142 * Allow beams to shoot at missiles properly.
144 * 30 4/23/99 12:01p Johnson
145 * Added SIF_HUGE_SHIP
147 * 29 4/22/99 11:06p Dave
148 * Final pass at beam weapons. Solidified a lot of stuff. All that remains
149 * now is to tweak and fix bugs as they come up. No new beam weapon
152 * 28 4/21/99 6:15p Dave
153 * Did some serious housecleaning in the beam code. Made it ready to go
154 * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
155 * a handy macro for recalculating collision pairs for a given object.
157 * 27 4/20/99 6:39p Dave
158 * Almost done with artillery targeting. Added support for downloading
159 * images on the PXO screen.
161 * 26 4/19/99 11:01p Dave
162 * More sophisticated targeting laser support. Temporary checkin.
164 * 25 4/19/99 4:54p Johnson
165 * Removed infinite loop from beam_render_all() oops :)
167 * 24 4/16/99 5:54p Dave
168 * Support for on/off style "stream" weapons. Real early support for
169 * target-painting lasers.
171 * 23 4/04/99 2:13p Dave
172 * Put in area effect beam weapons. May be a bit too expensive though.
174 * 22 4/02/99 9:55a Dave
175 * Added a few more options in the weapons.tbl for beam weapons. Attempt
176 * at putting "pain" packets into multiplayer.
178 * 21 3/31/99 8:24p Dave
179 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
180 * and background nebulae. Added per-ship non-dimming pixel colors.
182 * 20 3/08/99 7:03p Dave
183 * First run of new object update system. Looks very promising.
185 * 19 3/04/99 6:09p Dave
186 * Added in sexpressions for firing beams and checking for if a ship is
189 * 18 3/02/99 9:25p Dave
190 * Added a bunch of model rendering debug code. Started work on fixing
191 * beam weapon wacky firing.
193 * 17 2/21/99 1:48p Dave
194 * Some code for monitoring datarate for multiplayer in detail.
196 * 16 2/11/99 3:08p Dave
197 * PXO refresh button. Very preliminary squad war support.
199 * 15 2/05/99 3:23p Mattf
200 * Made beams a little more forgiving when checking object types.
202 * 14 2/05/99 12:52p Dave
203 * Fixed Glide nondarkening textures.
205 * 13 2/04/99 6:29p Dave
206 * First full working rev of FS2 PXO support. Fixed Glide lighting
209 * 12 1/30/99 9:02p Dave
212 * 11 1/30/99 1:29a Dave
213 * Fixed nebula thumbnail problem. Full support for 1024x768 choose pilot
214 * screen. Fixed beam weapon death messages.
216 * 10 1/29/99 7:08p Johnson
217 * Put in fix for beam weapons which are on dying ships.
219 * 9 1/29/99 12:47a Dave
220 * Put in sounds for beam weapon. A bunch of interface screens (tech
223 * 8 1/27/99 9:56a Dave
224 * Temporary checkin of beam weapons for Dan to make cool sounds.
226 * 7 1/24/99 11:37p Dave
227 * First full rev of beam weapons. Very customizable. Removed some bogus
228 * Int3()'s in low level net code.
230 * 6 1/21/99 2:06p Dave
231 * Final checkin for multiplayer testing.
233 * 5 1/21/99 10:45a Dave
234 * More beam weapon stuff. Put in warmdown time.
236 * 4 1/14/99 12:48a Dave
237 * Todo list bug fixes. Made a pass at putting briefing icons back into
238 * FRED. Sort of works :(
240 * 3 1/12/99 12:53a Dave
241 * More work on beam weapons - made collision detection very efficient -
242 * collide against all object types properly - made 3 movement types
243 * smooth. Put in test code to check for possible non-darkening pixels on
246 * 2 1/08/99 2:08p Dave
247 * Fixed software rendering for pofview. Super early support for AWACS and
254 #include "linklist.h"
256 #include "objcollide.h"
258 #include "freespace.h"
261 #include "alphacolors.h"
263 #include "fireballs.h"
265 #include "asteroid.h"
267 #include "multimsgs.h"
269 #include "particle.h"
275 #include "hudmessage.h"
277 #include "lighting.h"
279 // ------------------------------------------------------------------------------------------------
280 // BEAM WEAPON DEFINES/VARS
283 // use this to extend a beam to "infinity"
284 #define BEAM_FAR_LENGTH 30000.0f
286 // this is the constant which defines when a beam is an "area" beam. meaning, when we switch on sphereline checking and when
287 // a beam gets "stopped" by an object. It is a percentage of the object radius which the beam must be wider than
288 #define BEAM_AREA_PERCENT 0.4f
290 // randomness factor - all beam weapon aiming is adjusted by +/- some factor within this range
291 #define BEAM_RANDOM_FACTOR 0.4f
294 #define BEAM_DAMAGE_TIME 170 // apply damage
295 #define MAX_SHOT_POINTS 30
296 #define SHOT_POINT_TIME 200 // 5 arcs a second
298 #define TOOLTIME 1500.0f
300 // max # of collisions we'll allow per frame
301 #define MAX_FRAME_COLLISIONS 5
304 #define BF_SAFETY (1<<0) // if this is set, don't collide or render for this frame. lifetime still increases though
305 #define BF_SHRINK (1<<1) // if this is set, the beam is in the warmdown phase
307 // beam struct (the actual weapon/object)
308 typedef struct beam {
310 int objnum; // our own objnum
311 int weapon_info_index;
312 int sig; // signature for the shooting object
313 object *objp; // the shooting object (who owns the turret that I am being fired from)
314 object *target; // target object
315 ship_subsys *target_subsys; // targeted subsys
316 int target_sig; // target sig
317 ship_subsys *subsys; // subsys its being fired from
318 beam *next, *prev; // link list stuff
319 vector targeting_laser_offset;
320 int framecount; // how many frames the beam has been active
321 int flags; // see BF_* defines
322 float shrink; // shrink factor
325 int warmup_stamp; // timestamp for "warming up"
326 int warmdown_stamp; // timestamp for "warming down"
327 int type; // see BEAM_TYPE_* defines in beam.h
328 float life_left; // in seconds
329 float life_total; // total life
330 // this vector has very special meaning. BEFORE performing collision checks, it basically implies a "direction". meaning
331 // the vector between it and last_start is where the beam will be aiming. AFTER performing collision checks, it is the
332 // literal world collision point on the object (or meaningless, if we hit nothing). The function beam_move_all_pre() is
333 // responsible for then setting it up pre-collision time
336 int shot_index; // for type D beam weapons
339 beam_collision r_collisions[MAX_FRAME_COLLISIONS]; // recent collisions
340 int r_collision_count; // # of recent collisions
342 // collision info for this frame
343 beam_collision f_collisions[MAX_FRAME_COLLISIONS]; // collisions for the current frame
344 int f_collision_count; // # of collisions we recorded this frame
346 // looping sound info, HANDLE
347 int beam_sound_loop; // -1 if none
352 // exactly how the beam will behave. by passing this is multiplayer from server to client, we can ensure that
353 // everything looks the same
357 beam Beams[MAX_BEAMS]; // all beams
358 beam Beam_free_list; // free beams
359 beam Beam_used_list; // used beams
360 int Beam_count = 0; // how many beams are in use
362 // octant indices. These are "good" pairs of octants to use for beam target
363 #define BEAM_NUM_GOOD_OCTANTS 8
364 int Beam_good_slash_octants[BEAM_NUM_GOOD_OCTANTS][4] = {
365 { 2, 5, 1, 0 }, // octant, octant, min/max pt, min/max pt
374 int Beam_good_shot_octants[BEAM_NUM_GOOD_OCTANTS][4] = {
375 { 5, 0, 1, 0 }, // octant, octant, min/max pt, min/max pt
385 // damage cap values for friendly beam fire
386 float Beam_friendly_cap[NUM_SKILL_LEVELS] = { 0.0f, 5.0f, 10.0f, 20.0f, 30.0f };
388 // beam lighting effects
389 int Beam_lighting = 1;
391 // debug stuff - keep track of how many collision tests we perform a second and how many we toss a second
392 #define BEAM_TEST_STAMP_TIME 4000 // every 4 seconds
393 int Beam_test_stamp = -1;
394 int Beam_test_ints = 0;
395 int Beam_test_ship = 0;
396 int Beam_test_ast = 0;
397 int Beam_test_framecount = 0;
399 // beam warmup completion %
400 #define BEAM_WARMUP_PCT(b) ( ((float)Weapon_info[b->weapon_info_index].b_info.beam_warmup - (float)timestamp_until(b->warmup_stamp)) / (float)Weapon_info[b->weapon_info_index].b_info.beam_warmup )
402 // beam warmdown completion %
403 #define BEAM_WARMDOWN_PCT(b) ( ((float)Weapon_info[b->weapon_info_index].b_info.beam_warmdown - (float)timestamp_until(b->warmdown_stamp)) / (float)Weapon_info[b->weapon_info_index].b_info.beam_warmdown )
405 // timestamp for spewing muzzle particles
406 int Beam_muzzle_stamp = -1;
408 // link into the physics paused system
409 extern int physics_paused;
411 // beam lighting info
412 #define MAX_BEAM_LIGHT_INFO 100
413 typedef struct beam_light_info {
414 beam *bm; // beam casting the light
415 int objnum; // object getting light cast on it
416 ubyte source; // 0 to light the shooter, 1 for lighting any ship the beam passes, 2 to light the collision ship
417 vector c_point; // collision point for type 2 lights
420 beam_light_info Beam_lights[MAX_BEAM_LIGHT_INFO];
421 int Beam_light_count = 0;
423 float b_whack_small = 500.0f;
424 float b_whack_big = 1500.0f;
425 float b_whack_damage = 150.0f;
426 DCF(b_whack_small, "")
428 dc_get_arg(ARG_FLOAT);
429 b_whack_small = Dc_arg_float;
433 dc_get_arg(ARG_FLOAT);
434 b_whack_big = Dc_arg_float;
436 DCF(b_whack_damage, "")
438 dc_get_arg(ARG_FLOAT);
439 b_whack_damage = Dc_arg_float;
443 // ------------------------------------------------------------------------------------------------
444 // BEAM WEAPON FORWARD DECLARATIONS
448 void beam_delete(beam *b);
450 // handle a hit on a specific object
451 void beam_handle_collisions(beam *b);
454 void beam_get_binfo(beam *b, float accuracy, int num_shots);
456 // aim the beam (setup last_start and last_shot - the endpoints). also recalculates object collision info
457 void beam_aim(beam *b);
460 void beam_type_a_move(beam *b);
463 void beam_type_b_move(beam *b);
466 void beam_type_c_move(beam *b);
469 void beam_type_d_move(beam *b);
470 void beam_type_d_get_status(beam *b, int *shot_index, int *fire_wait);
473 void beam_type_e_move(beam *b);
475 // given a model #, and an object, stuff 2 good world coord points
476 void beam_get_octant_points(int modelnum, object *objp, int oct_index, int oct_array[BEAM_NUM_GOOD_OCTANTS][4], vector *v1, vector *v2);
478 // given an object, return its model num
479 int beam_get_model(object *objp);
481 // for a given object, and a firing beam, determine its critical dot product and range
482 void beam_get_cull_vals(object *objp, beam *b, float *cull_dot, float *cull_dist);
484 // get the total possible cone for a given beam in radians
485 float beam_get_cone_dot(beam *b);
487 // for rendering the beam effect
488 // output top and bottom vectors
489 // fvec == forward vector (eye viewpoint basically. in world coords)
490 // pos == world coordinate of the point we're calculating "around"
491 // w == width of the diff between top and bottom around pos
492 void beam_calc_facing_pts(vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add);
494 // render the muzzle glow for a beam weapon
495 void beam_render_muzzle_glow(beam *b);
497 // generate particles for the muzzle glow
498 void beam_generate_muzzle_particles(beam *b);
500 // throw some jitter into the aim - based upon shot_aim
501 void beam_jitter_aim(beam *b, float aim);
503 // if it is legal for the beam to continue firing
504 // returns -1 if the beam should stop firing immediately
505 // returns 0 if the beam should go to warmdown
506 // returns 1 if the beam can continue along its way
507 int beam_ok_to_fire(beam *b);
509 // start the warmup phase for the beam
510 void beam_start_warmup(beam *b);
512 // start the firing phase for the beam, return 0 if the beam failed to start, and should be deleted altogether
513 int beam_start_firing(beam *b);
515 // start the warmdown phase for the beam
516 void beam_start_warmdown(beam *b);
518 // add a collision to the beam for this frame (to be evaluated later)
519 void beam_add_collision(beam *b, object *hit_object, mc_info *cinfo);
521 // sort collisions for the frame
522 int beam_sort_collisions_func(const void *e1, const void *e2);
524 // get the width of the widest section of the beam
525 float beam_get_widest(beam *b);
527 // mark an object as being lit
528 void beam_add_light(beam *b, int objnum, int source, vector *c_point);
530 // apply lighting from any beams
531 void beam_apply_lighting();
533 // recalculate beam sounds (looping sounds relative to the player)
534 void beam_recalc_sounds(beam *b);
536 // apply a whack to a ship
537 void beam_apply_whack(beam *b, object *objp, vector *hit_point);
539 // return the amount of damage which should be applied to a ship. basically, filters friendly fire damage
540 float beam_get_ship_damage(beam *b, object *objp);
542 // if the beam is likely to tool a given target before its lifetime expires
543 int beam_will_tool_target(beam *b, object *objp);
546 // ------------------------------------------------------------------------------------------------
547 // BEAM WEAPON FUNCTIONS
550 // init at game startup
554 list_init( &Beam_free_list );
555 list_init( &Beam_used_list );
558 // initialize beam weapons for this level
559 void beam_level_init()
565 list_init( &Beam_free_list );
566 list_init( &Beam_used_list );
567 memset(Beams, 0, sizeof(beam) * MAX_BEAMS);
569 // Link all object slots into the free list
570 for (idx=0; idx<MAX_BEAMS; idx++) {
571 Beams[idx].objnum = -1;
572 list_append(&Beam_free_list, &Beams[idx] );
575 // reset muzzle particle spew timestamp
576 Beam_muzzle_stamp = -1;
579 // shutdown beam weapons for this level
580 void beam_level_close()
583 list_init( &Beam_free_list );
584 list_init( &Beam_used_list );
587 // fire a beam, returns nonzero on success. the innards of the code handle all the rest, foo
588 int beam_fire(beam_fire_info *fire_info)
596 if(fire_info == NULL){
601 // if we're out of beams, bail
602 if(Beam_count >= MAX_BEAMS){
606 // for now, only allow ship targets
607 if((fire_info->target->type != OBJ_SHIP) && (fire_info->target->type != OBJ_ASTEROID) && (fire_info->target->type != OBJ_DEBRIS) && (fire_info->target->type != OBJ_WEAPON)){
611 // make sure the beam_info_index is valid
612 Assert((fire_info->beam_info_index >= 0) && (fire_info->beam_info_index < MAX_WEAPON_TYPES) && (Weapon_info[fire_info->beam_info_index].wi_flags & WIF_BEAM));
613 if((fire_info->beam_info_index < 0) || (fire_info->beam_info_index >= MAX_WEAPON_TYPES) || !(Weapon_info[fire_info->beam_info_index].wi_flags & WIF_BEAM)){
616 wip = &Weapon_info[fire_info->beam_info_index];
618 // make sure a ship is firing this
619 Assert((fire_info->shooter->type == OBJ_SHIP) && (fire_info->shooter->instance >= 0) && (fire_info->shooter->instance < MAX_SHIPS));
620 if((fire_info->shooter->type != OBJ_SHIP) || (fire_info->shooter->instance < 0) && (fire_info->shooter->instance >= MAX_SHIPS)){
623 firing_ship = &Ships[fire_info->shooter->instance];
626 new_item = GET_FIRST(&Beam_free_list);
627 Assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
628 if(new_item == &Beam_free_list){
632 // remove from the free list
633 list_remove( &Beam_free_list, new_item );
635 // insert onto the end of used list
636 list_append( &Beam_used_list, new_item );
641 // fill in some values
642 new_item->warmup_stamp = -1;
643 new_item->warmdown_stamp = -1;
644 new_item->weapon_info_index = fire_info->beam_info_index;
645 new_item->objp = fire_info->shooter;
646 new_item->sig = fire_info->shooter->signature;
647 new_item->subsys = fire_info->turret;
648 new_item->life_left = wip->b_info.beam_life;
649 new_item->life_total = wip->b_info.beam_life;
650 new_item->r_collision_count = 0;
651 new_item->f_collision_count = 0;
652 new_item->target = fire_info->target;
653 new_item->target_subsys = fire_info->target_subsys;
654 new_item->target_sig = fire_info->target->signature;
655 new_item->beam_sound_loop = -1;
656 new_item->type = wip->b_info.beam_type;
657 new_item->targeting_laser_offset = fire_info->targeting_laser_offset;
658 new_item->framecount = 0;
660 new_item->shot_index = 0;
661 new_item->shrink = 1.0f;
662 new_item->team = (char)firing_ship->team;
664 // if the targeted subsystem is not NULL, force it to be a type A beam
665 if(new_item->target_subsys != NULL){
666 new_item->type = BEAM_TYPE_A;
669 // type D weapons can only fire at small ships and missiles
670 if(new_item->type == BEAM_TYPE_D){
671 // if its a targeted ship, get the target ship
672 if((fire_info->target != NULL) && (fire_info->target->type == OBJ_SHIP) && (fire_info->target->instance >= 0)){
673 ship *target_ship = &Ships[fire_info->target->instance];
675 // maybe force to be a type A
676 if(Ship_info[target_ship->ship_info_index].flags & (SIF_BIG_SHIP | SIF_CAPITAL | SIF_SUPERCAP | SIF_FREIGHTER | SIF_DRYDOCK | SIF_CARGO)){
677 new_item->type = BEAM_TYPE_A;
682 // ----------------------------------------------------------------------
683 // THIS IS THE CRITICAL POINT FOR MULTIPLAYER
684 // beam_get_binfo(...) determines exactly how the beam will behave over the course of its life
685 // it fills in binfo, which we can pass to clients in multiplayer
686 if(fire_info->beam_info_override != NULL){
687 new_item->binfo = *fire_info->beam_info_override;
689 beam_get_binfo(new_item, fire_info->accuracy, wip->b_info.beam_shots); // to fill in b_info - the set of directional aim vectors
692 // create the associated object
693 objnum = obj_create(OBJ_BEAM, -1, new_item - Beams, &vmd_identity_matrix, &vmd_zero_vector, 1.0f, OF_COLLIDES);
696 beam_delete(new_item);
697 nprintf(("General", "obj_create() failed for beam weapon! bah!\n"));
700 new_item->objnum = objnum;
702 // this sets up all info for the first frame the beam fires
703 beam_aim(new_item); // to fill in shot_point, etc.
705 // check to see if its legal to fire at this guy
706 if(beam_ok_to_fire(new_item) != 1){
707 beam_delete(new_item);
708 mprintf(("Killing beam at initial fire because of illegal targeting!!!\n"));
712 // if we're a multiplayer master - send a packet
713 if(MULTIPLAYER_MASTER){
714 send_beam_fired_packet(fire_info->shooter, fire_info->turret, fire_info->target, fire_info->beam_info_index, &new_item->binfo);
717 // start the warmup phase
718 beam_start_warmup(new_item);
723 // fire a targeting beam, returns objnum on success. a much much simplified version of a beam weapon
724 // targeting lasers last _one_ frame. For a continuous stream - they must be created every frame.
725 // this allows it to work smoothly in multiplayer (detect "trigger down". every frame just create a targeting laser firing straight out of the
726 // object. this way you get all the advantages of nice rendering and collisions).
727 // NOTE : only references beam_info_index and shooter
728 int beam_fire_targeting(beam_fire_info *fire_info)
736 if(fire_info == NULL){
741 // if we're out of beams, bail
742 if(Beam_count >= MAX_BEAMS){
746 // make sure the beam_info_index is valid
747 Assert((fire_info->beam_info_index >= 0) && (fire_info->beam_info_index < MAX_WEAPON_TYPES) && (Weapon_info[fire_info->beam_info_index].wi_flags & WIF_BEAM));
748 if((fire_info->beam_info_index < 0) || (fire_info->beam_info_index >= MAX_WEAPON_TYPES) || !(Weapon_info[fire_info->beam_info_index].wi_flags & WIF_BEAM)){
751 wip = &Weapon_info[fire_info->beam_info_index];
753 // make sure a ship is firing this
754 Assert((fire_info->shooter->type == OBJ_SHIP) && (fire_info->shooter->instance >= 0) && (fire_info->shooter->instance < MAX_SHIPS));
755 if((fire_info->shooter->type != OBJ_SHIP) || (fire_info->shooter->instance < 0) && (fire_info->shooter->instance >= MAX_SHIPS)){
758 firing_ship = &Ships[fire_info->shooter->instance];
762 new_item = GET_FIRST(&Beam_free_list);
763 Assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
765 // remove from the free list
766 list_remove( &Beam_free_list, new_item );
768 // insert onto the end of used list
769 list_append( &Beam_used_list, new_item );
774 // maybe allocate some extra data based on the beam type
775 Assert(wip->b_info.beam_type == BEAM_TYPE_C);
776 if(wip->b_info.beam_type != BEAM_TYPE_C){
780 // fill in some values
781 new_item->warmup_stamp = -1;
782 new_item->warmdown_stamp = -1;
783 new_item->weapon_info_index = fire_info->beam_info_index;
784 new_item->objp = fire_info->shooter;
785 new_item->sig = NULL;
786 new_item->subsys = NULL;
787 new_item->life_left = 0;
788 new_item->life_total = 0;
789 new_item->r_collision_count = 0;
790 new_item->f_collision_count = 0;
791 new_item->target = NULL;
792 new_item->target_subsys = NULL;
793 new_item->target_sig = NULL;
794 new_item->beam_sound_loop = -1;
795 new_item->type = BEAM_TYPE_C;
796 new_item->targeting_laser_offset = fire_info->targeting_laser_offset;
797 new_item->framecount = 0;
799 new_item->shot_index = 0;
800 new_item->team = (char)firing_ship->team;
802 // type c is a very special weapon type - binfo has no meaning
804 // create the associated object
805 objnum = obj_create(OBJ_BEAM, -1, new_item - Beams, &vmd_identity_matrix, &vmd_zero_vector, 1.0f, OF_COLLIDES);
808 beam_delete(new_item);
809 nprintf(("General", "obj_create() failed for beam weapon! bah!\n"));
812 new_item->objnum = objnum;
814 // this sets up all info for the first frame the beam fires
815 beam_aim(new_item); // to fill in shot_point, etc.
820 // return an object index of the guy who's firing this beam
821 int beam_get_parent(object *bm)
825 // get a handle to the beam
826 Assert(bm->type == OBJ_BEAM);
827 Assert(bm->instance >= 0);
828 if(bm->type != OBJ_BEAM){
831 if(bm->instance < 0){
834 b = &Beams[bm->instance];
836 Assert(b->objp != NULL);
841 // if the object handle is invalid
842 if(b->objp->signature != b->sig){
847 return OBJ_INDEX(b->objp);
850 // return weapon_info_index of beam
851 int beam_get_weapon_info_index(object *bm)
853 Assert(bm->type == OBJ_BEAM);
854 if (bm->type != OBJ_BEAM) {
858 Assert(bm->instance >= 0 && bm->instance < MAX_BEAMS);
859 if (bm->instance < 0) {
863 // return weapon_info_index
864 return Beams[bm->instance].weapon_info_index;
869 // given a beam object, get the # of collisions which happened during the last collision check (typically, last frame)
870 int beam_get_num_collisions(int objnum)
873 if((objnum < 0) || (objnum >= MAX_OBJECTS)){
877 if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
881 if((Beams[Objects[objnum].instance].objnum != objnum) || (Beams[Objects[objnum].instance].objnum < 0)){
886 // return the # of recent collisions
887 return Beams[Objects[objnum].instance].r_collision_count;
890 // stuff collision info, returns 1 on success
891 int beam_get_collision(int objnum, int num, int *collision_objnum, mc_info **cinfo)
894 if((objnum < 0) || (objnum >= MAX_OBJECTS)){
898 if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
902 if((Beams[Objects[objnum].instance].objnum != objnum) || (Beams[Objects[objnum].instance].objnum < 0)){
906 if(num >= Beams[Objects[objnum].instance].r_collision_count){
912 *cinfo = &Beams[Objects[objnum].instance].r_collisions[num].cinfo;
913 *collision_objnum = Beams[Objects[objnum].instance].r_collisions[num].c_objnum;
917 // pause all looping beam sounds
918 void beam_pause_sounds()
922 // set all beam volumes to 0
923 moveup = GET_FIRST(&Beam_used_list);
927 while(moveup != END_OF_LIST(&Beam_used_list)){
928 // set the volume to 0, if he has a looping beam sound
929 if(moveup->beam_sound_loop >= 0){
930 snd_set_volume(moveup->beam_sound_loop, 0.0f);
934 moveup = GET_NEXT(moveup);
938 // unpause looping beam sounds
939 void beam_unpause_sounds()
943 // recalc all beam sounds
944 moveup = GET_FIRST(&Beam_used_list);
948 while(moveup != END_OF_LIST(&Beam_used_list)){
949 beam_recalc_sounds(moveup);
952 moveup = GET_NEXT(moveup);
957 // -----------------------------===========================------------------------------
958 // BEAM MOVEMENT FUNCTIONS
959 // -----------------------------===========================------------------------------
961 // move a type A beam weapon
962 void beam_type_a_move(beam *b)
967 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
968 // get the "originating point" of the beam for this frame. essentially bashes last_start
969 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
971 // if the "warming up" timestamp has not expired
972 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
976 // put the "last_shot" point arbitrarily far away
977 vm_vec_sub(&dir, &b->last_shot, &b->last_start);
978 vm_vec_normalize_quick(&dir);
979 vm_vec_scale_add(&b->last_shot, &b->last_start, &dir, BEAM_FAR_LENGTH);
980 Assert(is_valid_vec(&b->last_shot));
983 // move a type B beam weapon
984 #define BEAM_T(b) ( ((b->binfo.delta_ang / b->life_total) * (b->life_total - b->life_left)) / b->binfo.delta_ang )
985 void beam_type_b_move(beam *b)
991 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
992 // get the "originating point" of the beam for this frame. essentially bashes last_start
993 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
995 // if the "warming up" timestamp has not expired
996 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1000 // if the two direction vectors are _really_ close together, just use the original direction
1001 dot_save = vm_vec_dot(&b->binfo.dir_a, &b->binfo.dir_b);
1002 if((double)dot_save >= 0.999999999){
1003 actual_dir = b->binfo.dir_a;
1005 // otherwise move towards the dir we calculated when firing this beam
1007 vm_vec_interp_constant(&actual_dir, &b->binfo.dir_a, &b->binfo.dir_b, BEAM_T(b));
1010 // now recalculate shot_point to be shooting through our new point
1011 vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, BEAM_FAR_LENGTH);
1012 int is_valid = is_valid_vec(&b->last_shot);
1015 actual_dir = b->binfo.dir_a;
1016 vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, BEAM_FAR_LENGTH);
1021 void beam_type_c_move(beam *b)
1026 if(b->objp == NULL){
1031 // type c beams only last one frame so we never have to "move" them.
1032 temp = b->targeting_laser_offset;
1033 vm_vec_unrotate(&b->last_start, &temp, &b->objp->orient);
1034 vm_vec_add2(&b->last_start, &b->objp->pos);
1035 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->objp->orient.fvec, BEAM_FAR_LENGTH);
1039 void beam_type_d_move(beam *b)
1041 int shot_index, fire_wait;
1042 vector temp, temp2, dir;
1044 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1045 // get the "originating point" of the beam for this frame. essentially bashes last_start
1046 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
1048 // if the "warming up" timestamp has not expired
1049 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1053 // determine what stage of the beam we're in
1054 beam_type_d_get_status(b, &shot_index, &fire_wait);
1056 // if we've changed shot index
1057 if(shot_index != b->shot_index){
1058 // set the new index
1059 b->shot_index = shot_index;
1065 // if we're in the fire wait stage
1066 b->flags &= ~BF_SAFETY;
1068 b->flags |= BF_SAFETY;
1071 // put the "last_shot" point arbitrarily far away
1072 vm_vec_sub(&dir, &b->last_shot, &b->last_start);
1073 vm_vec_normalize_quick(&dir);
1074 vm_vec_scale_add(&b->last_shot, &b->last_start, &dir, BEAM_FAR_LENGTH);
1075 Assert(is_valid_vec(&b->last_shot));
1077 void beam_type_d_get_status(beam *b, int *shot_index, int *fire_wait)
1079 float shot_time = b->life_total / (float)b->binfo.shot_count;
1080 float beam_time = b->life_total - b->life_left;
1082 // determine what "shot" we're on
1083 *shot_index = (int)(beam_time / shot_time);
1084 Assert(*shot_index < b->binfo.shot_count);
1085 if(*shot_index >= b->binfo.shot_count){
1086 *shot_index = b->binfo.shot_count - 1;
1089 // determine if its the firing or waiting section of the shot (fire happens first, THEN wait)
1091 if(beam_time > ((shot_time * (*shot_index)) + (shot_time * 0.5f))){
1097 void beam_type_e_move(beam *b)
1099 vector temp, turret_norm;
1101 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1102 // get the "originating point" of the beam for this frame. essentially bashes last_start
1103 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &turret_norm, 1, &temp);
1105 // if the "warming up" timestamp has not expired
1106 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1110 // put the "last_shot" point arbitrarily far away
1111 vm_vec_scale_add(&b->last_shot, &b->last_start, &turret_norm, BEAM_FAR_LENGTH);
1112 Assert(is_valid_vec(&b->last_shot));
1115 // pre-move (before collision checking - but AFTER ALL OTHER OBJECTS HAVE BEEN MOVED)
1116 void beam_move_all_pre()
1121 // zero lights for this frame yet
1122 Beam_light_count = 0;
1124 // traverse through all active beams
1125 moveup = GET_FIRST(&Beam_used_list);
1126 while(moveup != END_OF_LIST(&Beam_used_list)){
1130 // unset collision info
1131 b->f_collision_count = 0;
1133 if(!physics_paused){
1136 // type A beam weapons don't move
1138 beam_type_a_move(b);
1141 // type B beam weapons move across the target somewhat randomly
1143 beam_type_b_move(b);
1146 // type C beam weapons are attached to a fighter - pointing forward
1148 beam_type_c_move(b);
1153 beam_type_d_move(b);
1158 beam_type_e_move(b);
1161 // illegal beam type
1168 moveup = GET_NEXT(moveup);
1172 // post-collision time processing for beams
1173 void beam_move_all_post()
1178 beam_weapon_info *bwi;
1180 // traverse through all active beams
1181 moveup = GET_FIRST(&Beam_used_list);
1182 while(moveup != END_OF_LIST(&Beam_used_list)){
1183 bwi = &Weapon_info[moveup->weapon_info_index].b_info;
1185 // check the status of the beam
1186 bf_status = beam_ok_to_fire(moveup);
1188 // if we're warming up
1189 if(moveup->warmup_stamp != -1){
1190 next_one = GET_NEXT(moveup);
1192 // should we be stopping?
1194 beam_delete(moveup);
1196 // if the warming up timestamp has expired, start firing
1197 if(timestamp_elapsed(moveup->warmup_stamp)){
1199 if(!beam_start_firing(moveup)){
1200 beam_delete(moveup);
1203 // add a muzzle light for the shooter
1204 beam_add_light(moveup, OBJ_INDEX(moveup->objp), 0, NULL);
1212 // if we're warming down
1213 else if(moveup->warmdown_stamp != -1){
1214 next_one = GET_NEXT(moveup);
1216 // should we be stopping?
1218 beam_delete(moveup);
1220 // if we're done warming down, the beam is finished
1221 if(timestamp_elapsed(moveup->warmdown_stamp)){
1222 beam_delete(moveup);
1231 // otherwise, we're firing away.........
1233 // add a muzzle light for the shooter
1234 beam_add_light(moveup, OBJ_INDEX(moveup->objp), 0, NULL);
1236 // subtract out the life left for the beam
1237 if(!physics_paused){
1238 moveup->life_left -= flFrametime;
1241 // if we're past the shrink point, start shrinking the beam
1242 if(moveup->life_left <= (moveup->life_total * bwi->beam_shrink_factor)){
1243 moveup->flags |= BF_SHRINK;
1246 // if we're shrinking the beam
1247 if(moveup->flags & BF_SHRINK){
1248 moveup->shrink -= bwi->beam_shrink_pct * flFrametime;
1249 if(moveup->shrink < 0.1f){
1250 moveup->shrink = 0.1f;
1256 next_one = GET_NEXT(moveup);
1258 // if beam should abruptly stop
1259 if(bf_status == -1){
1260 beam_delete(moveup);
1262 // if the beam should just power down
1264 beam_start_warmdown(moveup);
1272 // increment framecount
1273 moveup->framecount++;
1275 // type c weapons live for one frame only
1276 if(moveup->type == BEAM_TYPE_C){
1277 if(moveup->framecount > 1){
1278 next_one = GET_NEXT(moveup);
1280 beam_delete(moveup);
1285 // done firing, so go into the warmdown phase
1287 if((moveup->life_left <= 0.0f) && (moveup->warmdown_stamp == -1)){
1288 beam_start_warmdown(moveup);
1290 moveup = GET_NEXT(moveup);
1295 // handle any collisions which occured collision (will take care of applying damage to all objects which got hit)
1296 beam_handle_collisions(moveup);
1298 // recalculate beam sounds
1299 beam_recalc_sounds(moveup);
1302 moveup = GET_NEXT(moveup);
1305 // apply all beam lighting
1306 beam_apply_lighting();
1308 // process beam culling info
1311 if(Beam_test_stamp == -1){
1312 Beam_test_stamp = timestamp(BEAM_TEST_STAMP_TIME);
1314 Beam_test_framecount = 0;
1316 if(timestamp_elapsed(Beam_test_stamp)){
1317 // report the results
1318 nprintf(("General", "Performed %f beam ints/frame (%d, %d, %d, %d), over %f seconds\n", (float)Beam_test_ints/(float)Beam_test_framecount, Beam_test_ints, Beam_test_framecount, Beam_test_ship, Beam_test_ast, (float)BEAM_TEST_STAMP_TIME / 1000.0f));
1321 Beam_test_stamp = timestamp(BEAM_TEST_STAMP_TIME);
1325 Beam_test_framecount = 0;
1327 Beam_test_framecount++;
1334 // -----------------------------===========================------------------------------
1335 // BEAM RENDERING FUNCTIONS
1336 // -----------------------------===========================------------------------------
1338 // render a beam weapon
1339 #define STUFF_VERTICES() do { verts[0]->u = 0.0f; verts[0]->v = 0.0f; verts[1]->u = 1.0f; verts[1]->v = 0.0f; verts[2]->u = 1.0f; verts[2]->v = 1.0f; verts[3]->u = 0.0f; verts[3]->v = 1.0f; } while(0);
1340 #define R_VERTICES() do { g3_rotate_vertex(verts[0], &bottom1); g3_rotate_vertex(verts[1], &bottom2); g3_rotate_vertex(verts[2], &top2); g3_rotate_vertex(verts[3], &top1); } while(0);
1341 #define P_VERTICES() do { for(idx=0; idx<4; idx++){ g3_project_vertex(verts[idx]); } } while(0);
1343 void beam_render(beam_weapon_info *bwi, vector *start, vector *shot, float shrink)
1346 vertex h1[4]; // halves of a beam section
1347 vertex *verts[4] = { &h1[0], &h1[1], &h1[2], &h1[3] };
1348 vector fvec, top1, bottom1, top2, bottom2;
1351 // bogus weapon info index
1356 // if the beam start and endpoints are the same
1357 if(vm_vec_same(start, shot)){
1361 // get beam direction
1362 vm_vec_sub(&fvec, shot, start);
1363 vm_vec_normalize_quick(&fvec);
1365 // turn off backface culling
1368 // draw all sections
1369 for(s_idx=0; s_idx<bwi->beam_num_sections; s_idx++){
1370 // calculate the beam points
1371 scale = frand_range(1.0f - bwi->sections[s_idx].flicker, 1.0f + bwi->sections[s_idx].flicker);
1372 beam_calc_facing_pts(&top1, &bottom1, &fvec, start, bwi->sections[s_idx].width * scale * shrink, bwi->sections[s_idx].z_add);
1373 beam_calc_facing_pts(&top2, &bottom2, &fvec, shot, bwi->sections[s_idx].width * scale * scale * shrink, bwi->sections[s_idx].z_add);
1374 R_VERTICES(); // rotate and project the vertices
1376 STUFF_VERTICES(); // stuff the beam with creamy goodness (texture coords)
1378 // set the right texture with additive alpha, and draw the poly
1379 gr_set_bitmap(bwi->sections[s_idx].texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
1380 g3_draw_poly( 4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1383 // turn backface culling back on
1387 // generate particles for the muzzle glow
1388 int hack_time = 100;
1391 dc_get_arg(ARG_INT);
1392 hack_time = Dc_arg_int;
1394 void beam_generate_muzzle_particles(beam *b)
1399 vector turret_norm, turret_pos, particle_pos, particle_dir, p_temp;
1401 particle_info pinfo;
1403 // if our hack stamp has expired
1404 if(!((Beam_muzzle_stamp == -1) || timestamp_elapsed(Beam_muzzle_stamp))){
1408 // never generate anything past about 1/5 of the beam fire time
1409 if(b->warmup_stamp == -1){
1414 wip = &Weapon_info[b->weapon_info_index];
1416 // no specified particle for this beam weapon
1417 if(wip->b_info.beam_particle_ani < 0){
1421 // reset the hack stamp
1422 Beam_muzzle_stamp = timestamp(hack_time);
1424 // randomly generate 10 to 20 particles
1425 particle_count = (int)frand_range(0.0f, (float)wip->b_info.beam_particle_count);
1427 // get turret info - position and normal
1428 turret_pos = b->subsys->system_info->pnt;
1429 turret_norm = b->subsys->system_info->turret_norm;
1431 // randomly perturb a vector within a cone around the normal
1432 vm_vector_2_matrix(&m, &turret_norm, NULL, NULL);
1433 for(idx=0; idx<particle_count; idx++){
1434 // get a random point in the cone
1435 vm_vec_random_cone(&particle_dir, &turret_norm, wip->b_info.beam_particle_angle, &m);
1436 p_temp = turret_pos;
1437 vm_vec_scale_add(&p_temp, &turret_pos, &particle_dir, wip->b_info.beam_muzzle_radius * frand_range(0.75f, 0.9f));
1439 // transform into world coords
1440 vm_vec_unrotate(&particle_pos, &p_temp, &b->objp->orient);
1441 vm_vec_add2(&particle_pos, &b->objp->pos);
1442 p_temp = particle_dir;
1443 vm_vec_unrotate(&particle_dir, &p_temp, &b->objp->orient);
1445 // now generate some interesting values for the particle
1446 float p_time_ref = wip->b_info.beam_life + ((float)wip->b_info.beam_warmup / 1000.0f);
1447 float p_life = frand_range(p_time_ref * 0.5f, p_time_ref * 0.7f);
1448 float p_vel = (wip->b_info.beam_muzzle_radius / p_life) * frand_range(0.85f, 1.2f);
1449 vm_vec_scale(&particle_dir, -p_vel);
1451 memset(&pinfo, 0, sizeof(particle_info));
1452 pinfo.pos = particle_pos;
1453 pinfo.vel = particle_dir;
1454 pinfo.lifetime = p_life;
1455 pinfo.attached_objnum = -1;
1456 pinfo.attached_sig = 0;
1457 pinfo.rad = wip->b_info.beam_particle_radius;
1459 pinfo.type = PARTICLE_BITMAP;
1460 pinfo.optional_data = wip->b_info.beam_particle_ani;
1461 pinfo.tracer_length = -1.0f;
1462 particle_create(&pinfo);
1466 // render the muzzle glow for a beam weapon
1467 void beam_render_muzzle_glow(beam *b)
1470 weapon_info *wip = &Weapon_info[b->weapon_info_index];
1471 beam_weapon_info *bwi = &Weapon_info[b->weapon_info_index].b_info;
1472 float pct, rand_val;
1474 // if we don't have a glow bitmap
1475 if(bwi->beam_glow_bitmap < 0){
1479 // if the beam is warming up, scale the glow
1480 if(b->warmup_stamp != -1){
1482 pct = BEAM_WARMUP_PCT(b);
1485 // if the beam is warming down
1486 if(b->warmdown_stamp != -1){
1488 pct = 1.0f - BEAM_WARMDOWN_PCT(b);
1491 // otherwise the beam is really firing
1494 rand_val = frand_range(0.90f, 1.0f);
1498 g3_rotate_vertex(&v, &b->last_start);
1499 gr_set_bitmap( bwi->beam_glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.8f * pct);
1501 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * rand_val, TMAP_FLAG_TEXTURED);
1505 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.75f * rand_val, TMAP_FLAG_TEXTURED);
1508 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.45f * rand_val, TMAP_FLAG_TEXTURED);
1511 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.25f * rand_val, TMAP_FLAG_TEXTURED);
1515 // render all beam weapons
1516 void beam_render_all()
1520 // traverse through all active beams
1521 moveup = GET_FIRST(&Beam_used_list);
1522 while(moveup != END_OF_LIST(&Beam_used_list)){
1523 // each beam type renders a little bit differently
1524 if((moveup->warmup_stamp == -1) && (moveup->warmdown_stamp == -1) && !(moveup->flags & BF_SAFETY)){
1525 // HACK - if this is the first frame the beam is firing, don't render it
1526 if(moveup->life_left >= moveup->life_total - 0.0001f){
1528 moveup = GET_NEXT(moveup);
1532 // render the beam itself
1533 Assert(moveup->weapon_info_index >= 0);
1534 if(moveup->weapon_info_index < 0){
1535 moveup = GET_NEXT(moveup);
1538 beam_render(&Weapon_info[moveup->weapon_info_index].b_info, &moveup->last_start, &moveup->last_shot, moveup->shrink);
1541 // render the muzzle glow
1542 beam_render_muzzle_glow(moveup);
1544 // maybe generate some muzzle particles
1545 beam_generate_muzzle_particles(moveup);
1548 moveup = GET_NEXT(moveup);
1552 // output top and bottom vectors
1553 // fvec == forward vector (eye viewpoint basically. in world coords)
1554 // pos == world coordinate of the point we're calculating "around"
1555 // w == width of the diff between top and bottom around pos
1556 void beam_calc_facing_pts( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1563 vm_vec_sub( &rvec, &Eye_position, &temp );
1564 vm_vec_normalize( &rvec );
1566 vm_vec_crossprod(&uvec,fvec,&rvec);
1567 vm_vec_normalize(&uvec);
1569 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1570 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1573 // light scale factor
1574 float blight = 25.5f;
1577 dc_get_arg(ARG_FLOAT);
1578 blight = Dc_arg_float;
1581 // call to add a light source to a small object
1582 void beam_add_light_small(beam *bm, object *objp, vector *pt_override = NULL)
1585 beam_weapon_info *bwi;
1598 Assert(objp != NULL);
1602 Assert(bm->weapon_info_index >= 0);
1603 wip = &Weapon_info[bm->weapon_info_index];
1607 noise = frand_range(1.0f - bwi->sections[0].flicker, 1.0f + bwi->sections[0].flicker);
1609 // widest part of the beam
1610 float light_rad = beam_get_widest(bm) * blight * noise;
1612 // nearest point on the beam, and its distance to the ship
1614 if(pt_override == NULL){
1616 vm_vec_dist_to_line(&objp->pos, &bm->last_start, &bm->last_shot, &near_pt, &dist);
1617 if(dist > light_rad){
1621 near_pt = *pt_override;
1624 // average rgb of the beam
1625 float fr = (float)wip->laser_color_1.red / 255.0f;
1626 float fg = (float)wip->laser_color_1.green / 255.0f;
1627 float fb = (float)wip->laser_color_1.blue / 255.0f;
1629 // add a unique light
1630 // noise *= 0.1f; // a little less noise here, since we want the beam to generally cast a bright light
1631 light_add_point_unique(&near_pt, light_rad * 0.0001f, light_rad, 1.0f, fr, fg, fb, OBJ_INDEX(objp));
1634 // call to add a light source to a large object
1635 void beam_add_light_large(beam *bm, object *objp, vector *pt0, vector *pt1)
1638 beam_weapon_info *bwi;
1651 Assert(objp != NULL);
1655 Assert(bm->weapon_info_index >= 0);
1656 wip = &Weapon_info[bm->weapon_info_index];
1660 noise = frand_range(1.0f - bwi->sections[0].flicker, 1.0f + bwi->sections[0].flicker);
1662 // widest part of the beam
1663 float light_rad = beam_get_widest(bm) * blight * noise;
1665 // average rgb of the beam
1666 float fr = (float)wip->laser_color_1.red / 255.0f;
1667 float fg = (float)wip->laser_color_1.green / 255.0f;
1668 float fb = (float)wip->laser_color_1.blue / 255.0f;
1670 // add a unique light
1671 noise *= 0.1f; // a little less noise here, since we want the beam to generally cast a bright light
1672 light_add_tube(pt0, pt1, 1.0f, light_rad, 1.0f * noise, fr, fg, fb, OBJ_INDEX(objp));
1675 // mark an object as being lit
1676 void beam_add_light(beam *b, int objnum, int source, vector *c_point)
1680 // if we're out of light slots!
1681 if(Beam_light_count >= MAX_BEAM_LIGHT_INFO){
1687 l = &Beam_lights[Beam_light_count++];
1690 l->source = (ubyte)source;
1692 // only type 2 lights (from collisions) need a collision point
1693 if(c_point != NULL){
1694 l->c_point = *c_point;
1696 Assert(source != 2);
1703 // apply lighting from any beams
1704 void beam_apply_lighting()
1709 beam_weapon_info *bwi;
1711 // convert all beam lights into real lights
1712 for(idx=0; idx<Beam_light_count; idx++){
1714 l = &Beam_lights[idx];
1717 if((l->objnum < 0) || (l->objnum >= MAX_OBJECTS) || (l->bm == NULL)){
1721 bwi = &Weapon_info[l->bm->weapon_info_index].b_info;
1723 // different light types
1725 // from the muzzle of the gun
1727 // a few meters in from the of muzzle
1728 vm_vec_sub(&dir, &l->bm->last_start, &l->bm->last_shot);
1729 vm_vec_normalize_quick(&dir);
1730 vm_vec_scale_add(&pt, &l->bm->last_start, &dir, bwi->beam_muzzle_radius * 5.0f);
1732 beam_add_light_small(l->bm, &Objects[l->objnum], &pt);
1735 // from the beam passing by
1738 switch(Objects[l->objnum].type){
1740 Assert(Objects[l->objnum].instance >= 0);
1743 if(Ship_info[Ships[Objects[l->objnum].instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)){
1744 beam_add_light_large(l->bm, &Objects[l->objnum], &l->bm->last_start, &l->bm->last_shot);
1748 beam_add_light_small(l->bm, &Objects[l->objnum]);
1752 // asteroids get small lights
1754 beam_add_light_small(l->bm, &Objects[l->objnum]);
1757 // debris gets small lights
1759 beam_add_light_small(l->bm, &Objects[l->objnum]);
1766 // a few meters from the collision point
1767 vm_vec_sub(&dir, &l->bm->last_start, &l->c_point);
1768 vm_vec_normalize_quick(&dir);
1769 vm_vec_scale_add(&pt, &l->c_point, &dir, bwi->beam_muzzle_radius * 5.0f);
1771 beam_add_light_small(l->bm, &Objects[l->objnum], &pt);
1777 // -----------------------------===========================------------------------------
1778 // BEAM BOOKKEEPING FUNCTIONS
1779 // -----------------------------===========================------------------------------
1782 void beam_delete(beam *b)
1784 // remove from active list and put on free list
1785 list_remove(&Beam_used_list, b);
1786 list_append(&Beam_free_list, b);
1788 // delete our associated object
1789 // Assert(b->objnum >= 0);
1791 obj_delete(b->objnum);
1795 // kill the beam looping sound
1796 if(b->beam_sound_loop != -1){
1797 snd_stop(b->beam_sound_loop);
1798 b->beam_sound_loop = -1;
1803 Assert(Beam_count >= 0);
1804 nprintf(("General", "Recycled beam (%d beams remaining)\n", Beam_count));
1807 // given an object, return its model num
1808 int beam_get_model(object *objp)
1811 Assert(objp->instance >= 0);
1812 if(objp->instance < 0){
1819 return Ships[objp->instance].modelnum;
1822 Assert(Weapons[objp->instance].weapon_info_index >= 0);
1823 if(Weapons[objp->instance].weapon_info_index < 0){
1826 return Weapon_info[Weapons[objp->instance].weapon_info_index].model_num;
1829 Assert(Debris[objp->instance].is_hull);
1830 if(!Debris[objp->instance].is_hull){
1833 return Debris[objp->instance].model_num;
1837 subtype = Asteroids[objp->instance].asteroid_subtype;
1838 Assert(Asteroids[objp->instance].type >= 0);
1839 if(Asteroids[objp->instance].type < 0){
1842 return Asteroid_info[Asteroids[objp->instance].type].model_num[subtype];
1846 // this shouldn't happen too often
1847 mprintf(("Beam couldn't find a good find a good object model/type!! (%d)", objp->type));
1856 // start the warmup phase for the beam
1857 void beam_start_warmup(beam *b)
1859 // set the warmup stamp
1860 b->warmup_stamp = timestamp(Weapon_info[b->weapon_info_index].b_info.beam_warmup);
1862 // start playing warmup sound
1863 if(!(Game_mode & GM_STANDALONE_SERVER) && (Weapon_info[b->weapon_info_index].b_info.beam_warmup_sound >= 0)){
1864 snd_play_3d(&Snds[Weapon_info[b->weapon_info_index].b_info.beam_warmup_sound], &b->last_start, &View_position);
1868 // start the firing phase for the beam, return 0 if the beam failed to start, and should be deleted altogether
1869 int beam_start_firing(beam *b)
1871 // kill the warmup stamp so the rest of the code knows its firing
1872 b->warmup_stamp = -1;
1874 // any special stuff for each weapon type
1876 // re-aim type A and D beam weapons here, otherwise they tend to miss
1895 // determine if we can legitimately start firing, or if we need to take other action
1896 switch(beam_ok_to_fire(b)){
1901 beam_start_warmdown(b);
1905 // start the beam firing sound now, if we haven't already
1906 if((b->beam_sound_loop == -1) && (Weapon_info[b->weapon_info_index].b_info.beam_loop_sound >= 0)){
1907 b->beam_sound_loop = snd_play_3d(&Snds[Weapon_info[b->weapon_info_index].b_info.beam_loop_sound], &b->last_start, &View_position, 0.0f, NULL, 1, 1.0, SND_PRIORITY_SINGLE_INSTANCE, NULL, 1.0f, 1);
1910 snd_play_3d(&Snds[SND_BEAM_SHOT], &b->last_start, &View_position);
1917 // start the warmdown phase for the beam
1918 void beam_start_warmdown(beam *b)
1921 b->warmdown_stamp = timestamp(Weapon_info[b->weapon_info_index].b_info.beam_warmdown);
1923 // start the warmdown sound
1924 if(Weapon_info[b->weapon_info_index].b_info.beam_warmdown_sound >= 0){
1925 snd_play_3d(&Snds[Weapon_info[b->weapon_info_index].b_info.beam_warmdown_sound], &b->last_start, &View_position);
1928 // kill the beam looping sound
1929 if(b->beam_sound_loop != -1){
1930 snd_stop(b->beam_sound_loop);
1931 b->beam_sound_loop = -1;
1935 // recalculate beam sounds (looping sounds relative to the player)
1936 void beam_recalc_sounds(beam *b)
1938 beam_weapon_info *bwi;
1941 Assert(b->weapon_info_index >= 0);
1942 if(b->weapon_info_index < 0){
1945 bwi = &Weapon_info[b->weapon_info_index].b_info;
1947 // update the sound position relative to the player
1948 if(b->beam_sound_loop != -1){
1949 // get the point closest to the player's viewing position
1950 switch(vm_vec_dist_to_line(&View_position, &b->last_start, &b->last_shot, &pos, NULL)){
1951 // behind the beam, so use the start pos
1953 pos = b->last_start;
1956 // use the closest point
1958 // already calculated in vm_vec_dist_to_line(...)
1961 // past the beam, so use the shot pos
1967 snd_update_3d_pos(b->beam_sound_loop, &Snds[bwi->beam_loop_sound], &pos);
1972 // -----------------------------===========================------------------------------
1973 // BEAM AIMING FUNCTIONS
1974 // -----------------------------===========================------------------------------
1977 void beam_get_binfo(beam *b, float accuracy, int num_shots)
1982 vector turret_point, turret_norm;
1983 beam_weapon_info *bwi;
1986 // where the shot is originating from (b->last_start gets filled in)
1987 ship_get_global_turret_gun_info(b->objp, b->subsys, &turret_point, &turret_norm, 1, &p2);
1989 // get a model # to work with
1990 model_num = beam_get_model(b->target);
1995 // get beam weapon info
1996 Assert(b->weapon_info_index >= 0);
1997 if(b->weapon_info_index < 0){
2000 bwi = &Weapon_info[b->weapon_info_index].b_info;
2003 skill_level = Game_skill_level;
2004 if(Game_skill_level >= NUM_SKILL_LEVELS){
2005 skill_level = NUM_SKILL_LEVELS - 1;
2007 if(Game_skill_level < 0){
2011 // stuff num shots even though its only used for type D weapons
2012 b->binfo.shot_count = (ubyte)num_shots;
2013 if(b->binfo.shot_count > MAX_BEAM_SHOTS){
2014 b->binfo.shot_count = MAX_BEAM_SHOTS;
2017 // generate the proper amount of directional vectors
2019 // pick an accuracy. beam will be properly aimed at actual fire time
2021 // all we will do is decide whether or not we will hit - type A beam weapons are re-aimed immediately before firing
2022 b->binfo.shot_aim[0] = frand_range(0.0f, 1.0f + bwi->beam_miss_factor[skill_level] * accuracy);
2023 b->binfo.shot_count = 1;
2025 // get random model points, this is useful for big ships, because we never miss when shooting at them
2026 submodel_get_two_random_points(model_num, 0, &b->binfo.dir_a, &b->binfo.dir_b);
2029 // just 2 points in the "slash"
2031 beam_get_octant_points(model_num, b->target, (int)frand_range(0.0f, BEAM_NUM_GOOD_OCTANTS), Beam_good_slash_octants, &oct1, &oct2);
2034 vm_vec_sub(&b->binfo.dir_a, &oct1, &turret_point);
2035 vm_vec_normalize(&b->binfo.dir_a);
2038 vm_vec_sub(&b->binfo.dir_b, &oct2, &turret_point);
2039 vm_vec_normalize(&b->binfo.dir_b);
2042 b->binfo.delta_ang = fl_abs(vm_vec_delta_ang_norm(&b->binfo.dir_a, &b->binfo.dir_b, NULL));
2045 // nothing for this beam - its very special case
2049 // type D beams fire at small ship multiple times
2051 // get a bunch of shot aims
2052 for(idx=0; idx<b->binfo.shot_count; idx++){
2053 // MK, 9/3/99: Added pow() function to make increasingly likely to miss with subsequent shots. 30% more likely with each shot.
2054 float r = ((float) pow(1.3f, idx)) * bwi->beam_miss_factor[skill_level] * accuracy;
2055 b->binfo.shot_aim[idx] = frand_range(0.0f, 1.0f + r);
2059 // type e beams just fire straight
2061 b->binfo.shot_aim[0] = 0.0000001f;
2062 b->binfo.shot_count = 1;
2063 b->binfo.dir_a = turret_norm;
2064 b->binfo.dir_b = turret_norm;
2072 // aim the beam (setup last_start and last_shot - the endpoints). also recalculates collision pairs
2073 void beam_aim(beam *b)
2078 // type C beam weapons have no target
2079 if(b->target == NULL){
2080 Assert(b->type == BEAM_TYPE_C);
2081 if(b->type != BEAM_TYPE_C){
2085 // get a model # to work with
2087 model_num = beam_get_model(b->target);
2093 // setup our initial shot point and aim direction
2096 // where the shot is originating from (b->last_start gets filled in)
2097 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2099 // if we're targeting a subsystem - shoot directly at it
2100 if(b->target_subsys != NULL){
2101 // unrotate the center of the subsystem
2102 vm_vec_unrotate(&b->last_shot, &b->target_subsys->system_info->pnt, &b->target->orient);
2103 vm_vec_add2(&b->last_shot, &b->target->pos);
2104 vm_vec_sub(&temp, &b->last_shot, &b->last_start);
2106 vm_vec_scale_add(&b->last_shot, &b->last_start, &temp, 2.0f);
2110 // if we're shooting at a big ship - shoot directly at the model
2111 if((b->target->type == OBJ_SHIP) && (Ship_info[Ships[b->target->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP))){
2112 // rotate into world coords
2113 vm_vec_unrotate(&temp, &b->binfo.dir_a, &b->target->orient);
2114 vm_vec_add2(&temp, &b->target->pos);
2116 // get the shot point
2117 vm_vec_sub(&p2, &temp, &b->last_start);
2118 vm_vec_scale_add(&b->last_shot, &b->last_start, &p2, 2.0f);
2122 // point at the center of the target, then jitter based on shot_aim
2123 b->last_shot = b->target->pos;
2124 beam_jitter_aim(b, b->binfo.shot_aim[0]);
2128 // where the shot is originating from (b->last_start gets filled in)
2129 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2131 // set the shot point
2132 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->binfo.dir_a, BEAM_FAR_LENGTH);
2133 Assert(is_valid_vec(&b->last_shot));
2138 temp = b->targeting_laser_offset;
2139 vm_vec_unrotate(&b->last_start, &temp, &b->objp->orient);
2140 vm_vec_add2(&b->last_start, &b->objp->pos);
2141 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->objp->orient.fvec, BEAM_FAR_LENGTH);
2145 // where the shot is originating from (b->last_start gets filled in)
2146 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2148 // point at the center of the target, then jitter based on shot_aim
2149 b->last_shot = b->target->pos;
2150 beam_jitter_aim(b, b->binfo.shot_aim[b->shot_index]);
2151 nprintf(("AI", "Frame %i: FIRING\n", Framecount));
2155 // where the shot is originating from (b->last_start gets filled in)
2156 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2158 // point directly in the direction of the turret
2159 vm_vec_scale_add(&b->last_shot, &b->last_start, &temp, BEAM_FAR_LENGTH);
2166 // recalculate object pairs
2167 OBJ_RECALC_PAIRS((&Objects[b->objnum]));
2170 // given a model #, and an object, stuff 2 good world coord points
2171 void beam_get_octant_points(int modelnum, object *objp, int oct_index, int oct_array[BEAM_NUM_GOOD_OCTANTS][4], vector *v1, vector *v2)
2173 vector t1, t2, temp;
2174 polymodel *m = model_get(modelnum);
2176 // bad bad bad bad bad bad
2182 Assert((oct_index >= 0) && (oct_index < BEAM_NUM_GOOD_OCTANTS));
2184 // randomly pick octants
2185 t1 = oct_array[oct_index][2] ? m->octants[oct_array[oct_index][0]].max : m->octants[oct_array[oct_index][0]].min;
2186 t2 = oct_array[oct_index][3] ? m->octants[oct_array[oct_index][1]].max : m->octants[oct_array[oct_index][1]].min;
2187 Assert(!vm_vec_same(&t1, &t2));
2189 // get them in world coords
2190 vm_vec_unrotate(&temp, &t1, &objp->orient);
2191 vm_vec_add(v1, &temp, &objp->pos);
2192 vm_vec_unrotate(&temp, &t2, &objp->orient);
2193 vm_vec_add(v2, &temp, &objp->pos);
2196 // throw some jitter into the aim - based upon shot_aim
2197 void beam_jitter_aim(beam *b, float aim)
2199 vector forward, circle;
2201 float subsys_strength;
2203 // if the weapons subsystem is damaged or destroyed
2204 if((b->objp != NULL) && (b->objp->signature == b->sig) && (b->objp->type == OBJ_SHIP) && (b->objp->instance >= 0) && (b->objp->instance < MAX_SHIPS)){
2205 // get subsytem strength
2206 subsys_strength = ship_get_subsystem_strength(&Ships[b->objp->instance], SUBSYSTEM_WEAPONS);
2208 // when subsytem strength is 0, double the aim error factor
2209 aim += aim * (1.0f - subsys_strength);
2212 // shot aim is a direct linear factor of the target model's radius.
2213 // so, pick a random point on the circle
2214 vm_vec_sub(&forward, &b->last_shot, &b->last_start);
2215 vm_vec_normalize_quick(&forward);
2218 vm_vector_2_matrix(&m, &forward, NULL, NULL);
2220 // get a vector on the circle - this should appear to be pretty random
2221 // vm_vec_scale_add(&circle, &b->last_shot, &m.rvec, aim * b->target->radius);
2222 vm_vec_random_in_circle(&circle, &b->last_shot, &m, aim * b->target->radius, 0);
2224 // get the vector pointing to the circle point
2225 vm_vec_sub(&forward, &circle, &b->last_start);
2226 vm_vec_scale_add(&b->last_shot, &b->last_start, &forward, 2.0f);
2230 // -----------------------------===========================------------------------------
2231 // BEAM COLLISION FUNCTIONS
2232 // -----------------------------===========================------------------------------
2234 // collide a beam with a ship, returns 1 if we can ignore all future collisions between the 2 objects
2235 int beam_collide_ship(obj_pair *pair)
2240 mc_info test_collide;
2250 Assert(pair->a->instance >= 0);
2251 Assert(pair->a->type == OBJ_BEAM);
2252 Assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2253 b = &Beams[pair->a->instance];
2255 // Don't check collisions for warping out player if past stage 1.
2256 if ( Player->control_mode >= PCM_WARPOUT_STAGE1) {
2257 if ( pair->a == Player_obj ) return 0;
2258 if ( pair->b == Player_obj ) return 0;
2261 // if the "warming up" timestamp has not expired
2262 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2266 // if the beam is on "safety", don't collide with anything
2267 if(b->flags & BF_SAFETY){
2271 // if the colliding object is the shooting object, return 1 so this is culled
2272 if(pair->b == b->objp){
2276 // try and get a model
2277 model_num = beam_get_model(pair->b);
2289 Assert(pair->b->type == OBJ_SHIP);
2290 Assert(pair->b->instance >= 0);
2291 if((pair->b->type != OBJ_SHIP) || (pair->b->instance < 0)){
2294 shipp = &Ships[pair->b->instance];
2295 sip = &Ship_info[shipp->ship_info_index];
2297 // get the widest portion of the beam
2298 widest = beam_get_widest(b);
2301 test_collide.model_num = model_num;
2302 test_collide.submodel_num = -1;
2303 test_collide.orient = &pair->b->orient;
2304 test_collide.pos = &pair->b->pos;
2305 test_collide.p0 = &b->last_start;
2306 test_collide.p1 = &b->last_shot;
2308 // maybe do a sphereline
2309 if(widest > pair->b->radius * BEAM_AREA_PERCENT){
2310 test_collide.radius = beam_get_widest(b) * 0.5f;
2311 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_SPHERELINE;
2313 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2315 model_collide(&test_collide);
2318 if(test_collide.num_hits){
2319 // add to the collision list
2320 beam_add_collision(b, pair->b, &test_collide);
2323 // add this guy to the lighting list
2324 beam_add_light(b, OBJ_INDEX(pair->b), 1, NULL);
2326 // reset timestamp to timeout immediately
2327 pair->next_check_time = timestamp(0);
2332 // collide a beam with an asteroid, returns 1 if we can ignore all future collisions between the 2 objects
2333 int beam_collide_asteroid(obj_pair *pair)
2336 mc_info test_collide;
2345 Assert(pair->a->instance >= 0);
2346 Assert(pair->a->type == OBJ_BEAM);
2347 Assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2348 b = &Beams[pair->a->instance];
2350 // if the "warming up" timestamp has not expired
2351 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2355 // if the beam is on "safety", don't collide with anything
2356 if(b->flags & BF_SAFETY){
2360 // if the colliding object is the shooting object, return 1 so this is culled
2361 if(pair->b == b->objp){
2365 // try and get a model
2366 model_num = beam_get_model(pair->b);
2378 test_collide.model_num = model_num;
2379 test_collide.submodel_num = -1;
2380 test_collide.orient = &pair->b->orient;
2381 test_collide.pos = &pair->b->pos;
2382 test_collide.p0 = &b->last_start;
2383 test_collide.p1 = &b->last_shot;
2384 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2385 model_collide(&test_collide);
2388 if(test_collide.num_hits){
2389 // add to the collision list
2390 beam_add_collision(b, pair->b, &test_collide);
2393 // add this guy to the lighting list
2394 beam_add_light(b, OBJ_INDEX(pair->b), 1, NULL);
2396 // reset timestamp to timeout immediately
2397 pair->next_check_time = timestamp(0);
2402 // collide a beam with a missile, returns 1 if we can ignore all future collisions between the 2 objects
2403 int beam_collide_missile(obj_pair *pair)
2406 mc_info test_collide;
2415 Assert(pair->a->instance >= 0);
2416 Assert(pair->a->type == OBJ_BEAM);
2417 Assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2418 b = &Beams[pair->a->instance];
2420 // if the "warming up" timestamp has not expired
2421 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2425 // if the beam is on "safety", don't collide with anything
2426 if(b->flags & BF_SAFETY){
2430 // if the colliding object is the shooting object, return 1 so this is culled
2431 if(pair->b == b->objp){
2435 // try and get a model
2436 model_num = beam_get_model(pair->b);
2447 test_collide.model_num = model_num;
2448 test_collide.submodel_num = -1;
2449 test_collide.orient = &pair->b->orient;
2450 test_collide.pos = &pair->b->pos;
2451 test_collide.p0 = &b->last_start;
2452 test_collide.p1 = &b->last_shot;
2453 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2454 model_collide(&test_collide);
2457 if(test_collide.num_hits){
2458 // add to the collision list
2459 beam_add_collision(b, pair->b, &test_collide);
2462 // reset timestamp to timeout immediately
2463 pair->next_check_time = timestamp(0);
2468 // collide a beam with debris, returns 1 if we can ignore all future collisions between the 2 objects
2469 int beam_collide_debris(obj_pair *pair)
2472 mc_info test_collide;
2481 Assert(pair->a->instance >= 0);
2482 Assert(pair->a->type == OBJ_BEAM);
2483 Assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2484 b = &Beams[pair->a->instance];
2486 // if the "warming up" timestamp has not expired
2487 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2491 // if the beam is on "safety", don't collide with anything
2492 if(b->flags & BF_SAFETY){
2496 // if the colliding object is the shooting object, return 1 so this is culled
2497 if(pair->b == b->objp){
2501 // try and get a model
2502 model_num = beam_get_model(pair->b);
2513 test_collide.model_num = model_num;
2514 test_collide.submodel_num = -1;
2515 test_collide.orient = &pair->b->orient;
2516 test_collide.pos = &pair->b->pos;
2517 test_collide.p0 = &b->last_start;
2518 test_collide.p1 = &b->last_shot;
2519 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2520 model_collide(&test_collide);
2523 if(test_collide.num_hits){
2524 // add to the collision list
2525 beam_add_collision(b, pair->b, &test_collide);
2528 // add this guy to the lighting list
2529 beam_add_light(b, OBJ_INDEX(pair->b), 1, NULL);
2531 // reset timestamp to timeout immediately
2532 pair->next_check_time = timestamp(0);
2537 // early-out function for when adding object collision pairs, return 1 if the pair should be ignored
2538 int beam_collide_early_out(object *a, object *b)
2542 float cull_dist, cull_dot;
2543 vector dot_test, dot_test2, dist_test;
2546 Assert(a->instance >= 0);
2547 if(a->instance < 0){
2550 Assert(a->type == OBJ_BEAM);
2551 if(a->type != OBJ_BEAM){
2554 Assert(Beams[a->instance].objnum == OBJ_INDEX(a));
2555 if(Beams[a->instance].objnum != OBJ_INDEX(a)){
2558 bm = &Beams[a->instance];
2559 Assert(bm->weapon_info_index >= 0);
2560 if(bm->weapon_info_index < 0){
2563 bwi = &Weapon_info[bm->weapon_info_index];
2565 // if the second object has an invalid instance, bail
2566 if(b->instance < 0){
2575 // targeting lasers only hit ships
2576 if(bwi->b_info.beam_type == BEAM_TYPE_C){
2581 // targeting lasers only hit ships
2582 if(bwi->b_info.beam_type == BEAM_TYPE_C){
2585 // don't ever collide with non hull pieces
2586 if(!Debris[b->instance].is_hull){
2591 // targeting lasers only hit ships
2592 if(bwi->b_info.beam_type == BEAM_TYPE_C){
2595 // don't ever collide against laser weapons - duh
2596 if(Weapon_info[Weapons[b->instance].weapon_info_index].subtype == WP_LASER){
2602 // get full cull value
2603 beam_get_cull_vals(b, bm, &cull_dot, &cull_dist);
2605 // if the object fails these conditions, bail
2606 vm_vec_sub(&dist_test, &b->pos, &bm->last_start);
2607 dot_test = dist_test;
2608 vm_vec_sub(&dot_test2, &bm->last_shot, &bm->last_start);
2609 vm_vec_normalize_quick(&dot_test);
2610 vm_vec_normalize_quick(&dot_test2);
2611 // cull_dist == DIST SQUARED FOO!
2612 if((vm_vec_dotprod(&dot_test, &dot_test2) < cull_dot) && (vm_vec_mag_squared(&dist_test) > cull_dist)){
2620 // add a collision to the beam for this frame (to be evaluated later)
2621 void beam_add_collision(beam *b, object *hit_object, mc_info *cinfo)
2626 // if we haven't reached the limit for beam collisions, just add
2627 if(b->f_collision_count < MAX_FRAME_COLLISIONS){
2628 bc = &b->f_collisions[b->f_collision_count++];
2629 bc->c_objnum = OBJ_INDEX(hit_object);
2636 // otherwise, we've got to do some checking, ick.
2637 // I guess we can always just remove the farthest item
2639 for(idx=0; idx<MAX_FRAME_COLLISIONS; idx++){
2640 if((bc == NULL) || (b->f_collisions[idx].cinfo.hit_dist > bc->cinfo.hit_dist)){
2641 bc = &b->f_collisions[idx];
2650 bc->c_objnum = OBJ_INDEX(hit_object);
2654 // sort collisions for the frame
2655 int beam_sort_collisions_func(const void *e1, const void *e2)
2657 beam_collision *b1 = (beam_collision*)e1;
2658 beam_collision *b2 = (beam_collision*)e2;
2660 return b1->cinfo.hit_dist < b2->cinfo.hit_dist ? -1 : 1;
2663 // handle a hit on a specific object
2664 void beam_handle_collisions(beam *b)
2667 beam_collision r_coll[MAX_FRAME_COLLISIONS];
2668 int r_coll_count = 0;
2669 beam_weapon_info *bwi;
2673 // early out if we had no collisions
2674 if(b->f_collision_count <= 0){
2678 // get beam weapon info
2679 if((b->weapon_info_index < 0) || (b->weapon_info_index >= Num_weapon_types)){
2683 bwi = &Weapon_info[b->weapon_info_index].b_info;
2684 wi = &Weapon_info[b->weapon_info_index];
2686 // get the widest part of the beam
2687 widest = beam_get_widest(b);
2689 // the first thing we need to do is sort the collisions, from closest to farthest
2690 qsort(b->f_collisions, b->f_collision_count, sizeof(beam_collision), beam_sort_collisions_func);
2692 // now apply all collisions until we reach a ship which "stops" the beam or we reach the end of the list
2693 for(idx=0; idx<b->f_collision_count; idx++){
2697 int target = b->f_collisions[idx].c_objnum;
2699 // if we have an invalid object
2700 if((target < 0) || (target >= MAX_OBJECTS)){
2704 // try and get a model to deal with
2705 model_num = beam_get_model(&Objects[target]);
2711 beam_add_light(b, target, 2, &b->f_collisions[idx].cinfo.hit_point_world);
2713 // add to the recent collision list
2714 r_coll[r_coll_count].c_objnum = target;
2715 r_coll[r_coll_count].c_sig = Objects[target].signature;
2716 r_coll[r_coll_count].c_stamp = -1;
2717 r_coll[r_coll_count].cinfo = b->f_collisions[idx].cinfo;
2719 // if he was already on the recent collision list, copy his timestamp
2720 // also, be sure not to play the impact sound again.
2721 for(s_idx=0; s_idx<b->r_collision_count; s_idx++){
2722 if((r_coll[r_coll_count].c_objnum == b->r_collisions[s_idx].c_objnum) && (r_coll[r_coll_count].c_sig == b->r_collisions[s_idx].c_sig)){
2724 r_coll[r_coll_count].c_stamp = b->r_collisions[s_idx].c_stamp;
2726 // don't play the impact sound again
2731 // if the damage timestamp has expired or is not set yet, apply damage
2732 if((r_coll[r_coll_count].c_stamp == -1) || timestamp_elapsed(r_coll[r_coll_count].c_stamp)){
2734 r_coll[r_coll_count].c_stamp = timestamp(BEAM_DAMAGE_TIME);
2737 // if no damage - don't even indicate it has been hit
2738 if(wi->damage <= 0){
2742 // increment collision count
2745 // play the impact sound
2747 snd_play_3d( &Snds[wi->impact_snd], &b->f_collisions[idx].cinfo.hit_point_world, &Eye_position );
2751 if(do_damage && !physics_paused){
2752 // maybe draw an explosion
2753 if(wi->impact_weapon_expl_index >= 0){
2754 int ani_handle = weapon_get_expl_handle(wi->impact_weapon_expl_index, &b->f_collisions[idx].cinfo.hit_point_world, wi->impact_explosion_radius);
2755 particle_create( &b->f_collisions[idx].cinfo.hit_point_world, &vmd_zero_vector, 0.0f, wi->impact_explosion_radius, PARTICLE_BITMAP_PERSISTENT, ani_handle );
2758 switch(Objects[target].type){
2760 // hit the debris - the debris hit code takes care of checking for MULTIPLAYER_CLIENT, etc
2761 debris_hit(&Objects[target], &Objects[b->objnum], &b->f_collisions[idx].cinfo.hit_point_world, Weapon_info[b->weapon_info_index].damage);
2765 // detonate the missile
2766 Assert(Weapon_info[Weapons[Objects[target].instance].weapon_info_index].subtype == WP_MISSILE);
2767 if(!(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_MASTER){
2768 weapon_hit(&Objects[target], NULL, &Objects[target].pos);
2774 if(!(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_MASTER){
2775 asteroid_hit(&Objects[target], &Objects[b->objnum], &b->f_collisions[idx].cinfo.hit_point_world, Weapon_info[b->weapon_info_index].damage);
2780 // hit the ship - again, the innards of this code handle multiplayer cases
2781 // maybe vaporize ship.
2782 ship_apply_local_damage(&Objects[target], &Objects[b->objnum], &b->f_collisions[idx].cinfo.hit_point_world, beam_get_ship_damage(b, &Objects[target]), -1);
2784 // if this is the first hit on the player ship. whack him
2786 beam_apply_whack(b, &Objects[target], &b->f_collisions[idx].cinfo.hit_point_world);
2792 // if the radius of the target is somewhat close to the radius of the beam, "stop" the beam here
2793 // for now : if its smaller than about 1/3 the radius of the ship
2794 if(widest <= (Objects[target].radius * BEAM_AREA_PERCENT) && !beam_will_tool_target(b, &Objects[target])){
2795 // set last_shot so we know where to properly draw the beam
2796 b->last_shot = b->f_collisions[idx].cinfo.hit_point_world;
2797 Assert(is_valid_vec(&b->last_shot));
2799 // done wif the beam
2804 // store the new recent collisions
2805 for(idx=0; idx<r_coll_count; idx++){
2806 b->r_collisions[idx] = r_coll[idx];
2808 b->r_collision_count = r_coll_count;
2811 // for a given object, and a firing beam, determine its critical dot product and range
2812 void beam_get_cull_vals(object *objp, beam *b, float *cull_dot, float *cull_dist)
2815 // debris and asteroids are classified as slow moving small objects
2816 // use cull_dot == potential cone of beam + 10% and 50.0 meters
2819 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.10f);
2820 *cull_dist = 50.0f * 50.0f;
2823 // treat missiles as fast-moving small objects
2825 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.5f);
2826 *cull_dist = 300.0f * 300.0f;
2830 // for cap ships, only cull for 90deg or better
2832 if(Ship_info[Ships[objp->instance].ship_info_index].flags & SIF_CAPITAL){
2839 // for large ships, cull at some multiple of the radius
2840 if(Ship_info[Ships[objp->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)){
2841 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.25f);
2843 *cull_dist = (objp->radius * 1.3f) * (objp->radius * 1.3f);
2847 // for everthing else, cull the same as missiles
2848 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.5f);
2849 *cull_dist = 300.0f * 300.0f;
2853 // BAD BAD BAD - but this code will cause everything to cull properly
2860 // FIXME - make sure we are truthfull representing the "cone" for all beam types
2861 // get the total possible cone for a given beam in radians
2862 float beam_get_cone_dot(beam *b)
2869 // even though these beams don't move, return a _very_ small value
2870 return (float)cos(fl_radian(50.5f));
2873 return vm_vec_dotprod(&b->binfo.dir_a, &b->binfo.dir_b);
2883 // if it is legal for the beam to fire, or continue firing
2884 int beam_ok_to_fire(beam *b)
2886 // type C beams are ok to fire all the time
2887 if(Weapon_info[b->weapon_info_index].b_info.beam_type == BEAM_TYPE_C){
2891 // if my own object is invalid, stop firing
2892 if(b->objp->signature != b->sig){
2893 mprintf(("BEAM : killing beam because of invalid parent object SIGNATURE!\n"));
2897 // if my own object is a ghost
2898 if(b->objp->type != OBJ_SHIP){
2899 mprintf(("BEAM : killing beam because of invalid parent object TYPE!\n"));
2903 // if the shooting turret is destroyed
2904 if(b->subsys->current_hits <= 0.0f){
2905 mprintf(("BEAM : killing beam because turret has been destroyed!\n"));
2909 // if the beam will be firing out of its FOV, power it down
2910 vector aim_dir, temp;
2911 vector turret_dir, turret_pos;
2912 vm_vec_sub(&aim_dir, &b->last_shot, &b->last_start);
2913 vm_vec_normalize(&aim_dir);
2914 ship_get_global_turret_gun_info(b->objp, b->subsys, &turret_pos, &turret_dir, 1, &temp);
2915 if(vm_vec_dotprod(&aim_dir, &turret_dir) < b->subsys->system_info->turret_fov){
2916 mprintf(("BEAM : powering beam down because of FOV condition!\n"));
2920 // ok to fire/continue firing
2924 // get the width of the widest section of the beam
2925 float beam_get_widest(beam *b)
2928 float widest = -1.0f;
2931 Assert(b->weapon_info_index >= 0);
2932 if(b->weapon_info_index < 0){
2937 for(idx=0; idx<Weapon_info[b->weapon_info_index].b_info.beam_num_sections; idx++){
2938 if(Weapon_info[b->weapon_info_index].b_info.sections[idx].width > widest){
2939 widest = Weapon_info[b->weapon_info_index].b_info.sections[idx].width;
2944 return widest * b->shrink;
2947 // apply a whack to a ship
2948 void beam_apply_whack(beam *b, object *objp, vector *hit_point)
2954 Assert((b != NULL) && (objp != NULL) && (hit_point != NULL));
2955 if((b == NULL) || (objp == NULL) || (hit_point == NULL)){
2958 Assert(b->weapon_info_index >= 0);
2959 wip = &Weapon_info[b->weapon_info_index];
2960 Assert((objp != NULL) && (objp->type == OBJ_SHIP) && (objp->instance >= 0) && (objp->instance < MAX_SHIPS));
2961 if((objp == NULL) || (objp->type != OBJ_SHIP) || (objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
2964 shipp = &Ships[objp->instance];
2965 if((shipp->ai_index < 0) || (shipp->ai_index >= MAX_AI_INFO)){
2969 // don't whack docking ships
2970 if(Ai_info[shipp->ai_index].ai_flags & AIF_DOCKED){
2974 // determine how big of a whack to apply
2978 if(wip->damage < b_whack_damage){
2979 whack = b_whack_small;
2981 whack = b_whack_big;
2985 vector whack_dir, temp;
2986 vm_vec_dist_to_line(&objp->pos, &b->last_start, &b->last_shot, &temp, &dist);
2987 vm_vec_sub(&whack_dir, &objp->pos, &temp);
2988 vm_vec_normalize(&whack_dir);
2989 vm_vec_scale(&whack_dir, whack);
2992 ship_apply_whack(&whack_dir, hit_point, objp);
2995 // return the amount of damage which should be applied to a ship. basically, filters friendly fire damage
2996 float beam_get_ship_damage(beam *b, object *objp)
2998 // if the beam is on the same team as the object
2999 Assert((objp != NULL) && (b != NULL));
3000 if((objp == NULL) || (b == NULL)){
3003 Assert((objp->type == OBJ_SHIP) && (objp->instance >= 0) && (objp->instance < MAX_SHIPS));
3004 if((objp->type != OBJ_SHIP) || (objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
3009 if((b->team == Ships[objp->instance].team) && (Weapon_info[b->weapon_info_index].damage > Beam_friendly_cap[Game_skill_level])){
3010 return Beam_friendly_cap[Game_skill_level];
3014 return Weapon_info[b->weapon_info_index].damage;
3017 // if the beam is likely to tool a given target before its lifetime expires
3018 int beam_will_tool_target(beam *b, object *objp)
3020 weapon_info *wip = &Weapon_info[b->weapon_info_index];
3021 float damage_in_a_few_seconds;
3028 // if the object is not a ship, bail
3029 if(objp->type != OBJ_SHIP){
3032 if((objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
3036 // if the beam is going to apply more damage in about 1 and a half than the hull of the ship can take
3037 damage_in_a_few_seconds = (TOOLTIME / (float)BEAM_DAMAGE_TIME) * wip->damage;
3038 if(objp->hull_strength < damage_in_a_few_seconds){
3047 float beam_accuracy = 1.0f;
3050 dc_get_arg(ARG_FLOAT);
3051 beam_accuracy = Dc_arg_float;
3058 for(idx=0; idx<Num_weapon_types; idx++){
3059 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3061 dc_printf("Beam %d : %s\n", b_count, Weapon_info[idx].name);
3065 void beam_test(int whee)
3068 object *orion, *fenris;
3069 ship_subsys *orion_turret, *fenris_turret, *fenris_radar, *orion_radar, *lookup;
3072 nprintf(("General", "Running beam test\n"));
3074 // lookup some stuff
3075 s1 = ship_name_lookup("GTD Orion 1");
3077 orion = &Objects[Ships[s1].objnum];
3078 s2 = ship_name_lookup("GTC Fenris 2");
3080 fenris = &Objects[Ships[s2].objnum];
3083 lookup = GET_FIRST(&Ships[s1].subsys_list);
3084 orion_turret = NULL;
3086 while(lookup != END_OF_LIST(&Ships[s1].subsys_list)){
3088 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret07")){
3089 orion_turret = lookup;
3093 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3094 orion_radar = lookup;
3097 lookup = GET_NEXT(lookup);
3099 Assert(orion_turret != NULL);
3100 Assert(orion_radar != NULL);
3101 lookup = GET_FIRST(&Ships[s2].subsys_list);
3102 fenris_turret = NULL;
3103 fenris_radar = NULL;
3104 while(lookup != END_OF_LIST(&Ships[s2].subsys_list)){
3106 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret07")){
3107 fenris_turret = lookup;
3111 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3112 fenris_radar = lookup;
3115 lookup = GET_NEXT(lookup);
3117 Assert(fenris_turret != NULL);
3118 Assert(fenris_radar != NULL);
3120 memset(&f, 0, sizeof(beam_fire_info));
3121 f.accuracy = beam_accuracy;
3122 f.beam_info_index = -1;
3123 f.beam_info_override = NULL;
3126 f.target_subsys = fenris_turret;
3127 f.turret = orion_turret;
3129 // find the first beam
3131 int beam_first = -1;
3134 for(idx=0; idx<Num_weapon_types; idx++){
3135 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3147 // maybe fire it, if its valid
3148 f.beam_info_index = beam_first + whee - 1;
3149 if(Weapon_info[f.beam_info_index].wi_flags & WIF_BEAM){
3150 HUD_printf("Firing %s\n", Weapon_info[f.beam_info_index].name);
3155 void beam_test_new(int whee)
3158 object *orion, *fenris, *herc2, *herc3, *herc6, *alpha;
3159 ship_subsys *orion_turret, *fenris_turret, *fenris_radar, *orion_radar, *lookup;
3162 nprintf(("General", "Running beam test\n"));
3164 // lookup some stuff
3165 s1 = ship_name_lookup("GTD Orion 1");
3167 orion = &Objects[Ships[s1].objnum];
3168 s2 = ship_name_lookup("GTC Fenris 2");
3170 fenris = &Objects[Ships[s2].objnum];
3171 s3 = ship_name_lookup("GTF Hercules 2");
3173 herc2 = &Objects[Ships[s3].objnum];
3174 s3 = ship_name_lookup("GTF Hercules 3");
3176 herc3 = &Objects[Ships[s3].objnum];
3177 s3 = ship_name_lookup("GTF Hercules 6");
3179 herc6 = &Objects[Ships[s3].objnum];
3180 s3 = ship_name_lookup("Alpha 1");
3182 alpha = &Objects[Ships[s3].objnum];
3185 lookup = GET_FIRST(&Ships[s1].subsys_list);
3186 orion_turret = NULL;
3188 while(lookup != END_OF_LIST(&Ships[s1].subsys_list)){
3190 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret07")){
3191 orion_turret = lookup;
3195 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3196 orion_radar = lookup;
3199 lookup = GET_NEXT(lookup);
3201 Assert(orion_turret != NULL);
3202 Assert(orion_radar != NULL);
3203 lookup = GET_FIRST(&Ships[s2].subsys_list);
3204 fenris_turret = NULL;
3205 fenris_radar = NULL;
3206 while(lookup != END_OF_LIST(&Ships[s2].subsys_list)){
3208 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret03")){
3209 fenris_turret = lookup;
3213 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3214 fenris_radar = lookup;
3217 lookup = GET_NEXT(lookup);
3219 Assert(fenris_turret != NULL);
3220 Assert(fenris_radar != NULL);
3222 memset(&f, 0, sizeof(beam_fire_info));
3223 f.accuracy = beam_accuracy;
3224 f.beam_info_override = NULL;
3227 f.target_subsys = NULL;
3228 f.turret = fenris_turret;
3231 // find the first beam
3233 int beam_first = -1;
3236 for(idx=0; idx<Num_weapon_types; idx++){
3237 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3249 // maybe fire it, if its valid
3250 f.beam_info_index = beam_first + whee - 1;
3251 if(Weapon_info[f.beam_info_index].wi_flags & WIF_BEAM){
3252 HUD_printf("Firing %s\n", Weapon_info[f.beam_info_index].name);