2 * $Logfile: /Freespace2/code/Weapon/Beam.cpp $
7 * all sorts of cool stuff about ships
10 * Revision 1.2 2002/05/07 03:16:53 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:11 root
17 * 68 9/09/99 11:40p Dave
18 * Handle an Assert() in beam code. Added supernova sounds. Play the right
19 * 2 end movies properly, based upon what the player did in the mission.
21 * 67 9/09/99 2:36p Mikek
22 * Put back in the "1.0f +" in BEAM_TYPE_A aiming. Not the best way to
25 * 65 9/08/99 10:29p Dave
26 * Make beam sound pausing and unpausing much safer.
28 * 64 9/06/99 12:46a Andsager
29 * Add weapon_explosion_ani LOD
31 * 63 9/03/99 5:12p Mikek
32 * Change miss_factor code, making it a lot more likely for beams to miss
33 * and also making them increasingly likely to miss with each subsequent
34 * shot in the multi-shot burst.
36 * 62 8/30/99 5:01p Dave
37 * Made d3d do less state changing in the nebula. Use new chat server for
40 * 61 8/28/99 7:29p Dave
41 * Fixed wingmen persona messaging. Make sure locked turrets don't count
42 * towards the # attacking a player.
44 * 60 8/27/99 9:07p Dave
45 * LOD explosions. Improved beam weapon accuracy.
47 * 59 8/26/99 10:15a Dave
48 * Don't apply beam whacks to docked ships.
50 * 58 7/31/99 1:16p Dave
51 * Use larger font for 1024 HUD flash text box. Make beam weapons aware of
52 * weapon subsystem damage on firing ship.
54 * 57 7/22/99 4:00p Dave
55 * Fixed beam weapon muzzle glow rendering. Externalized hud shield info.
57 * 56 7/19/99 7:20p Dave
58 * Beam tooling. Specialized player-killed-self messages. Fixed d3d nebula
61 * 55 7/15/99 9:20a Andsager
62 * FS2_DEMO initial checkin
64 * 54 7/09/99 5:54p Dave
65 * Seperated cruiser types into individual types. Added tons of new
66 * briefing icons. Campaign screen.
68 * 53 7/08/99 10:53a Dave
69 * New multiplayer interpolation scheme. Not 100% done yet, but still
70 * better than the old way.
72 * 52 7/02/99 10:51p Dave
73 * Limit friendly beam fire damage. :(
75 * 51 7/01/99 11:44a Dave
76 * Updated object sound system to allow multiple obj sounds per ship.
77 * Added hit-by-beam sound. Added killed by beam sound.
79 * 50 6/29/99 7:39p Dave
80 * Lots of small bug fixes.
82 * 49 6/29/99 2:53p Dave
83 * Re-enabled beam lighting.
85 * 48 6/25/99 3:04p Dave
88 * 47 6/24/99 3:00p Dave
91 * 46 6/23/99 4:49p Dave
92 * Temporarily removed beam lighting.
94 * 45 6/21/99 7:25p Dave
95 * netplayer pain packet. Added type E unmoving beams.
97 * 44 6/18/99 5:16p Dave
98 * Added real beam weapon lighting. Fixed beam weapon sounds. Added MOTD
99 * dialog to PXO screen.
101 * 43 6/14/99 10:45a Dave
102 * Made beam weapons specify accuracy by skill level in the weapons.tbl
104 * 42 6/09/99 2:55p Andsager
105 * Allow multiple asteroid subtypes (of large, medium, small) and follow
108 * 41 6/08/99 6:02p Jamesa
109 * Handle case where type B beam start and end directions are really
112 * 40 6/04/99 2:16p Dave
113 * Put in shrink effect for beam weapons.
115 * 39 5/14/99 11:47a Andsager
116 * Added beam_get_weapon_info_index(object *bm)
118 * 38 5/12/99 10:43a Andsager
119 * Increase max beam length
121 * 37 5/08/99 8:25p Dave
122 * Upped object pairs. First run of nebula lightning.
124 * 36 5/05/99 9:02p Dave
125 * Fixed D3D aabitmap rendering. Spiffed up nebula effect a bit (added
126 * rotations, tweaked values, made bitmap selection more random). Fixed
127 * D3D beam weapon clipping problem. Added D3d frame dumping.
129 * 35 5/03/99 9:07a Dave
130 * Pirate Bob. Changed beam test code a bit.
132 * 34 4/27/99 12:16a Dave
133 * Fixed beam weapon muzzle glow problem. Fixed premature timeout on the
134 * pxo server list screen. Fixed secondary firing for hosts on a
135 * standalone. Fixed wacky multiplayer weapon "shuddering" problem.
137 * 33 4/25/99 6:12p Johnson
138 * Fixed bug where multi-shot beams were getting the shot count from the
141 * 32 4/25/99 3:36p Dave
142 * Fixed nebula table code. Tweaked beam weapon explosion stuff.
144 * 31 4/23/99 2:33p Johnson
145 * Allow beams to shoot at missiles properly.
147 * 30 4/23/99 12:01p Johnson
148 * Added SIF_HUGE_SHIP
150 * 29 4/22/99 11:06p Dave
151 * Final pass at beam weapons. Solidified a lot of stuff. All that remains
152 * now is to tweak and fix bugs as they come up. No new beam weapon
155 * 28 4/21/99 6:15p Dave
156 * Did some serious housecleaning in the beam code. Made it ready to go
157 * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
158 * a handy macro for recalculating collision pairs for a given object.
160 * 27 4/20/99 6:39p Dave
161 * Almost done with artillery targeting. Added support for downloading
162 * images on the PXO screen.
164 * 26 4/19/99 11:01p Dave
165 * More sophisticated targeting laser support. Temporary checkin.
167 * 25 4/19/99 4:54p Johnson
168 * Removed infinite loop from beam_render_all() oops :)
170 * 24 4/16/99 5:54p Dave
171 * Support for on/off style "stream" weapons. Real early support for
172 * target-painting lasers.
174 * 23 4/04/99 2:13p Dave
175 * Put in area effect beam weapons. May be a bit too expensive though.
177 * 22 4/02/99 9:55a Dave
178 * Added a few more options in the weapons.tbl for beam weapons. Attempt
179 * at putting "pain" packets into multiplayer.
181 * 21 3/31/99 8:24p Dave
182 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
183 * and background nebulae. Added per-ship non-dimming pixel colors.
185 * 20 3/08/99 7:03p Dave
186 * First run of new object update system. Looks very promising.
188 * 19 3/04/99 6:09p Dave
189 * Added in sexpressions for firing beams and checking for if a ship is
192 * 18 3/02/99 9:25p Dave
193 * Added a bunch of model rendering debug code. Started work on fixing
194 * beam weapon wacky firing.
196 * 17 2/21/99 1:48p Dave
197 * Some code for monitoring datarate for multiplayer in detail.
199 * 16 2/11/99 3:08p Dave
200 * PXO refresh button. Very preliminary squad war support.
202 * 15 2/05/99 3:23p Mattf
203 * Made beams a little more forgiving when checking object types.
205 * 14 2/05/99 12:52p Dave
206 * Fixed Glide nondarkening textures.
208 * 13 2/04/99 6:29p Dave
209 * First full working rev of FS2 PXO support. Fixed Glide lighting
212 * 12 1/30/99 9:02p Dave
215 * 11 1/30/99 1:29a Dave
216 * Fixed nebula thumbnail problem. Full support for 1024x768 choose pilot
217 * screen. Fixed beam weapon death messages.
219 * 10 1/29/99 7:08p Johnson
220 * Put in fix for beam weapons which are on dying ships.
222 * 9 1/29/99 12:47a Dave
223 * Put in sounds for beam weapon. A bunch of interface screens (tech
226 * 8 1/27/99 9:56a Dave
227 * Temporary checkin of beam weapons for Dan to make cool sounds.
229 * 7 1/24/99 11:37p Dave
230 * First full rev of beam weapons. Very customizable. Removed some bogus
231 * Int3()'s in low level net code.
233 * 6 1/21/99 2:06p Dave
234 * Final checkin for multiplayer testing.
236 * 5 1/21/99 10:45a Dave
237 * More beam weapon stuff. Put in warmdown time.
239 * 4 1/14/99 12:48a Dave
240 * Todo list bug fixes. Made a pass at putting briefing icons back into
241 * FRED. Sort of works :(
243 * 3 1/12/99 12:53a Dave
244 * More work on beam weapons - made collision detection very efficient -
245 * collide against all object types properly - made 3 movement types
246 * smooth. Put in test code to check for possible non-darkening pixels on
249 * 2 1/08/99 2:08p Dave
250 * Fixed software rendering for pofview. Super early support for AWACS and
257 #include "linklist.h"
259 #include "objcollide.h"
261 #include "freespace.h"
264 #include "alphacolors.h"
266 #include "fireballs.h"
268 #include "asteroid.h"
270 #include "multimsgs.h"
272 #include "particle.h"
278 #include "hudmessage.h"
280 #include "lighting.h"
282 // ------------------------------------------------------------------------------------------------
283 // BEAM WEAPON DEFINES/VARS
286 // use this to extend a beam to "infinity"
287 #define BEAM_FAR_LENGTH 30000.0f
289 // this is the constant which defines when a beam is an "area" beam. meaning, when we switch on sphereline checking and when
290 // a beam gets "stopped" by an object. It is a percentage of the object radius which the beam must be wider than
291 #define BEAM_AREA_PERCENT 0.4f
293 // randomness factor - all beam weapon aiming is adjusted by +/- some factor within this range
294 #define BEAM_RANDOM_FACTOR 0.4f
297 #define BEAM_DAMAGE_TIME 170 // apply damage
298 #define MAX_SHOT_POINTS 30
299 #define SHOT_POINT_TIME 200 // 5 arcs a second
301 #define TOOLTIME 1500.0f
303 // max # of collisions we'll allow per frame
304 #define MAX_FRAME_COLLISIONS 5
307 #define BF_SAFETY (1<<0) // if this is set, don't collide or render for this frame. lifetime still increases though
308 #define BF_SHRINK (1<<1) // if this is set, the beam is in the warmdown phase
310 // beam struct (the actual weapon/object)
311 typedef struct beam {
313 int objnum; // our own objnum
314 int weapon_info_index;
315 int sig; // signature for the shooting object
316 object *objp; // the shooting object (who owns the turret that I am being fired from)
317 object *target; // target object
318 ship_subsys *target_subsys; // targeted subsys
319 int target_sig; // target sig
320 ship_subsys *subsys; // subsys its being fired from
321 beam *next, *prev; // link list stuff
322 vector targeting_laser_offset;
323 int framecount; // how many frames the beam has been active
324 int flags; // see BF_* defines
325 float shrink; // shrink factor
328 int warmup_stamp; // timestamp for "warming up"
329 int warmdown_stamp; // timestamp for "warming down"
330 int type; // see BEAM_TYPE_* defines in beam.h
331 float life_left; // in seconds
332 float life_total; // total life
333 // this vector has very special meaning. BEFORE performing collision checks, it basically implies a "direction". meaning
334 // the vector between it and last_start is where the beam will be aiming. AFTER performing collision checks, it is the
335 // literal world collision point on the object (or meaningless, if we hit nothing). The function beam_move_all_pre() is
336 // responsible for then setting it up pre-collision time
339 int shot_index; // for type D beam weapons
342 beam_collision r_collisions[MAX_FRAME_COLLISIONS]; // recent collisions
343 int r_collision_count; // # of recent collisions
345 // collision info for this frame
346 beam_collision f_collisions[MAX_FRAME_COLLISIONS]; // collisions for the current frame
347 int f_collision_count; // # of collisions we recorded this frame
349 // looping sound info, HANDLE
350 int beam_sound_loop; // -1 if none
355 // exactly how the beam will behave. by passing this is multiplayer from server to client, we can ensure that
356 // everything looks the same
360 beam Beams[MAX_BEAMS]; // all beams
361 beam Beam_free_list; // free beams
362 beam Beam_used_list; // used beams
363 int Beam_count = 0; // how many beams are in use
365 // octant indices. These are "good" pairs of octants to use for beam target
366 #define BEAM_NUM_GOOD_OCTANTS 8
367 int Beam_good_slash_octants[BEAM_NUM_GOOD_OCTANTS][4] = {
368 { 2, 5, 1, 0 }, // octant, octant, min/max pt, min/max pt
377 int Beam_good_shot_octants[BEAM_NUM_GOOD_OCTANTS][4] = {
378 { 5, 0, 1, 0 }, // octant, octant, min/max pt, min/max pt
388 // damage cap values for friendly beam fire
389 float Beam_friendly_cap[NUM_SKILL_LEVELS] = { 0.0f, 5.0f, 10.0f, 20.0f, 30.0f };
391 // beam lighting effects
392 int Beam_lighting = 1;
394 // debug stuff - keep track of how many collision tests we perform a second and how many we toss a second
395 #define BEAM_TEST_STAMP_TIME 4000 // every 4 seconds
396 int Beam_test_stamp = -1;
397 int Beam_test_ints = 0;
398 int Beam_test_ship = 0;
399 int Beam_test_ast = 0;
400 int Beam_test_framecount = 0;
402 // beam warmup completion %
403 #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 )
405 // beam warmdown completion %
406 #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 )
408 // timestamp for spewing muzzle particles
409 int Beam_muzzle_stamp = -1;
411 // link into the physics paused system
412 extern int physics_paused;
414 // beam lighting info
415 #define MAX_BEAM_LIGHT_INFO 100
416 typedef struct beam_light_info {
417 beam *bm; // beam casting the light
418 int objnum; // object getting light cast on it
419 ubyte source; // 0 to light the shooter, 1 for lighting any ship the beam passes, 2 to light the collision ship
420 vector c_point; // collision point for type 2 lights
423 beam_light_info Beam_lights[MAX_BEAM_LIGHT_INFO];
424 int Beam_light_count = 0;
426 float b_whack_small = 500.0f;
427 float b_whack_big = 1500.0f;
428 float b_whack_damage = 150.0f;
429 DCF(b_whack_small, "")
431 dc_get_arg(ARG_FLOAT);
432 b_whack_small = Dc_arg_float;
436 dc_get_arg(ARG_FLOAT);
437 b_whack_big = Dc_arg_float;
439 DCF(b_whack_damage, "")
441 dc_get_arg(ARG_FLOAT);
442 b_whack_damage = Dc_arg_float;
446 // ------------------------------------------------------------------------------------------------
447 // BEAM WEAPON FORWARD DECLARATIONS
451 void beam_delete(beam *b);
453 // handle a hit on a specific object
454 void beam_handle_collisions(beam *b);
457 void beam_get_binfo(beam *b, float accuracy, int num_shots);
459 // aim the beam (setup last_start and last_shot - the endpoints). also recalculates object collision info
460 void beam_aim(beam *b);
463 void beam_type_a_move(beam *b);
466 void beam_type_b_move(beam *b);
469 void beam_type_c_move(beam *b);
472 void beam_type_d_move(beam *b);
473 void beam_type_d_get_status(beam *b, int *shot_index, int *fire_wait);
476 void beam_type_e_move(beam *b);
478 // given a model #, and an object, stuff 2 good world coord points
479 void beam_get_octant_points(int modelnum, object *objp, int oct_index, int oct_array[BEAM_NUM_GOOD_OCTANTS][4], vector *v1, vector *v2);
481 // given an object, return its model num
482 int beam_get_model(object *objp);
484 // for a given object, and a firing beam, determine its critical dot product and range
485 void beam_get_cull_vals(object *objp, beam *b, float *cull_dot, float *cull_dist);
487 // get the total possible cone for a given beam in radians
488 float beam_get_cone_dot(beam *b);
490 // for rendering the beam effect
491 // output top and bottom vectors
492 // fvec == forward vector (eye viewpoint basically. in world coords)
493 // pos == world coordinate of the point we're calculating "around"
494 // w == width of the diff between top and bottom around pos
495 void beam_calc_facing_pts(vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add);
497 // render the muzzle glow for a beam weapon
498 void beam_render_muzzle_glow(beam *b);
500 // generate particles for the muzzle glow
501 void beam_generate_muzzle_particles(beam *b);
503 // throw some jitter into the aim - based upon shot_aim
504 void beam_jitter_aim(beam *b, float aim);
506 // if it is legal for the beam to continue firing
507 // returns -1 if the beam should stop firing immediately
508 // returns 0 if the beam should go to warmdown
509 // returns 1 if the beam can continue along its way
510 int beam_ok_to_fire(beam *b);
512 // start the warmup phase for the beam
513 void beam_start_warmup(beam *b);
515 // start the firing phase for the beam, return 0 if the beam failed to start, and should be deleted altogether
516 int beam_start_firing(beam *b);
518 // start the warmdown phase for the beam
519 void beam_start_warmdown(beam *b);
521 // add a collision to the beam for this frame (to be evaluated later)
522 void beam_add_collision(beam *b, object *hit_object, mc_info *cinfo);
524 // sort collisions for the frame
525 int beam_sort_collisions_func(const void *e1, const void *e2);
527 // get the width of the widest section of the beam
528 float beam_get_widest(beam *b);
530 // mark an object as being lit
531 void beam_add_light(beam *b, int objnum, int source, vector *c_point);
533 // apply lighting from any beams
534 void beam_apply_lighting();
536 // recalculate beam sounds (looping sounds relative to the player)
537 void beam_recalc_sounds(beam *b);
539 // apply a whack to a ship
540 void beam_apply_whack(beam *b, object *objp, vector *hit_point);
542 // return the amount of damage which should be applied to a ship. basically, filters friendly fire damage
543 float beam_get_ship_damage(beam *b, object *objp);
545 // if the beam is likely to tool a given target before its lifetime expires
546 int beam_will_tool_target(beam *b, object *objp);
549 // ------------------------------------------------------------------------------------------------
550 // BEAM WEAPON FUNCTIONS
553 // init at game startup
557 list_init( &Beam_free_list );
558 list_init( &Beam_used_list );
561 // initialize beam weapons for this level
562 void beam_level_init()
568 list_init( &Beam_free_list );
569 list_init( &Beam_used_list );
570 memset(Beams, 0, sizeof(beam) * MAX_BEAMS);
572 // Link all object slots into the free list
573 for (idx=0; idx<MAX_BEAMS; idx++) {
574 Beams[idx].objnum = -1;
575 list_append(&Beam_free_list, &Beams[idx] );
578 // reset muzzle particle spew timestamp
579 Beam_muzzle_stamp = -1;
582 // shutdown beam weapons for this level
583 void beam_level_close()
586 list_init( &Beam_free_list );
587 list_init( &Beam_used_list );
590 // fire a beam, returns nonzero on success. the innards of the code handle all the rest, foo
591 int beam_fire(beam_fire_info *fire_info)
599 if(fire_info == NULL){
604 // if we're out of beams, bail
605 if(Beam_count >= MAX_BEAMS){
609 // for now, only allow ship targets
610 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)){
614 // make sure the beam_info_index is valid
615 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));
616 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)){
619 wip = &Weapon_info[fire_info->beam_info_index];
621 // make sure a ship is firing this
622 Assert((fire_info->shooter->type == OBJ_SHIP) && (fire_info->shooter->instance >= 0) && (fire_info->shooter->instance < MAX_SHIPS));
623 if((fire_info->shooter->type != OBJ_SHIP) || (fire_info->shooter->instance < 0) && (fire_info->shooter->instance >= MAX_SHIPS)){
626 firing_ship = &Ships[fire_info->shooter->instance];
629 new_item = GET_FIRST(&Beam_free_list);
630 Assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
631 if(new_item == &Beam_free_list){
635 // remove from the free list
636 list_remove( &Beam_free_list, new_item );
638 // insert onto the end of used list
639 list_append( &Beam_used_list, new_item );
644 // fill in some values
645 new_item->warmup_stamp = -1;
646 new_item->warmdown_stamp = -1;
647 new_item->weapon_info_index = fire_info->beam_info_index;
648 new_item->objp = fire_info->shooter;
649 new_item->sig = fire_info->shooter->signature;
650 new_item->subsys = fire_info->turret;
651 new_item->life_left = wip->b_info.beam_life;
652 new_item->life_total = wip->b_info.beam_life;
653 new_item->r_collision_count = 0;
654 new_item->f_collision_count = 0;
655 new_item->target = fire_info->target;
656 new_item->target_subsys = fire_info->target_subsys;
657 new_item->target_sig = fire_info->target->signature;
658 new_item->beam_sound_loop = -1;
659 new_item->type = wip->b_info.beam_type;
660 new_item->targeting_laser_offset = fire_info->targeting_laser_offset;
661 new_item->framecount = 0;
663 new_item->shot_index = 0;
664 new_item->shrink = 1.0f;
665 new_item->team = (char)firing_ship->team;
667 // if the targeted subsystem is not NULL, force it to be a type A beam
668 if(new_item->target_subsys != NULL){
669 new_item->type = BEAM_TYPE_A;
672 // type D weapons can only fire at small ships and missiles
673 if(new_item->type == BEAM_TYPE_D){
674 // if its a targeted ship, get the target ship
675 if((fire_info->target != NULL) && (fire_info->target->type == OBJ_SHIP) && (fire_info->target->instance >= 0)){
676 ship *target_ship = &Ships[fire_info->target->instance];
678 // maybe force to be a type A
679 if(Ship_info[target_ship->ship_info_index].flags & (SIF_BIG_SHIP | SIF_CAPITAL | SIF_SUPERCAP | SIF_FREIGHTER | SIF_DRYDOCK | SIF_CARGO)){
680 new_item->type = BEAM_TYPE_A;
685 // ----------------------------------------------------------------------
686 // THIS IS THE CRITICAL POINT FOR MULTIPLAYER
687 // beam_get_binfo(...) determines exactly how the beam will behave over the course of its life
688 // it fills in binfo, which we can pass to clients in multiplayer
689 if(fire_info->beam_info_override != NULL){
690 new_item->binfo = *fire_info->beam_info_override;
692 beam_get_binfo(new_item, fire_info->accuracy, wip->b_info.beam_shots); // to fill in b_info - the set of directional aim vectors
695 // create the associated object
696 objnum = obj_create(OBJ_BEAM, -1, new_item - Beams, &vmd_identity_matrix, &vmd_zero_vector, 1.0f, OF_COLLIDES);
699 beam_delete(new_item);
700 nprintf(("General", "obj_create() failed for beam weapon! bah!\n"));
703 new_item->objnum = objnum;
705 // this sets up all info for the first frame the beam fires
706 beam_aim(new_item); // to fill in shot_point, etc.
708 // check to see if its legal to fire at this guy
709 if(beam_ok_to_fire(new_item) != 1){
710 beam_delete(new_item);
711 mprintf(("Killing beam at initial fire because of illegal targeting!!!\n"));
715 // if we're a multiplayer master - send a packet
716 if(MULTIPLAYER_MASTER){
717 send_beam_fired_packet(fire_info->shooter, fire_info->turret, fire_info->target, fire_info->beam_info_index, &new_item->binfo);
720 // start the warmup phase
721 beam_start_warmup(new_item);
726 // fire a targeting beam, returns objnum on success. a much much simplified version of a beam weapon
727 // targeting lasers last _one_ frame. For a continuous stream - they must be created every frame.
728 // this allows it to work smoothly in multiplayer (detect "trigger down". every frame just create a targeting laser firing straight out of the
729 // object. this way you get all the advantages of nice rendering and collisions).
730 // NOTE : only references beam_info_index and shooter
731 int beam_fire_targeting(beam_fire_info *fire_info)
739 if(fire_info == NULL){
744 // if we're out of beams, bail
745 if(Beam_count >= MAX_BEAMS){
749 // make sure the beam_info_index is valid
750 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));
751 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)){
754 wip = &Weapon_info[fire_info->beam_info_index];
756 // make sure a ship is firing this
757 Assert((fire_info->shooter->type == OBJ_SHIP) && (fire_info->shooter->instance >= 0) && (fire_info->shooter->instance < MAX_SHIPS));
758 if((fire_info->shooter->type != OBJ_SHIP) || (fire_info->shooter->instance < 0) && (fire_info->shooter->instance >= MAX_SHIPS)){
761 firing_ship = &Ships[fire_info->shooter->instance];
765 new_item = GET_FIRST(&Beam_free_list);
766 Assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
768 // remove from the free list
769 list_remove( &Beam_free_list, new_item );
771 // insert onto the end of used list
772 list_append( &Beam_used_list, new_item );
777 // maybe allocate some extra data based on the beam type
778 Assert(wip->b_info.beam_type == BEAM_TYPE_C);
779 if(wip->b_info.beam_type != BEAM_TYPE_C){
783 // fill in some values
784 new_item->warmup_stamp = -1;
785 new_item->warmdown_stamp = -1;
786 new_item->weapon_info_index = fire_info->beam_info_index;
787 new_item->objp = fire_info->shooter;
788 new_item->sig = NULL;
789 new_item->subsys = NULL;
790 new_item->life_left = 0;
791 new_item->life_total = 0;
792 new_item->r_collision_count = 0;
793 new_item->f_collision_count = 0;
794 new_item->target = NULL;
795 new_item->target_subsys = NULL;
796 new_item->target_sig = NULL;
797 new_item->beam_sound_loop = -1;
798 new_item->type = BEAM_TYPE_C;
799 new_item->targeting_laser_offset = fire_info->targeting_laser_offset;
800 new_item->framecount = 0;
802 new_item->shot_index = 0;
803 new_item->team = (char)firing_ship->team;
805 // type c is a very special weapon type - binfo has no meaning
807 // create the associated object
808 objnum = obj_create(OBJ_BEAM, -1, new_item - Beams, &vmd_identity_matrix, &vmd_zero_vector, 1.0f, OF_COLLIDES);
811 beam_delete(new_item);
812 nprintf(("General", "obj_create() failed for beam weapon! bah!\n"));
815 new_item->objnum = objnum;
817 // this sets up all info for the first frame the beam fires
818 beam_aim(new_item); // to fill in shot_point, etc.
823 // return an object index of the guy who's firing this beam
824 int beam_get_parent(object *bm)
828 // get a handle to the beam
829 Assert(bm->type == OBJ_BEAM);
830 Assert(bm->instance >= 0);
831 if(bm->type != OBJ_BEAM){
834 if(bm->instance < 0){
837 b = &Beams[bm->instance];
839 Assert(b->objp != NULL);
844 // if the object handle is invalid
845 if(b->objp->signature != b->sig){
850 return OBJ_INDEX(b->objp);
853 // return weapon_info_index of beam
854 int beam_get_weapon_info_index(object *bm)
856 Assert(bm->type == OBJ_BEAM);
857 if (bm->type != OBJ_BEAM) {
861 Assert(bm->instance >= 0 && bm->instance < MAX_BEAMS);
862 if (bm->instance < 0) {
866 // return weapon_info_index
867 return Beams[bm->instance].weapon_info_index;
872 // given a beam object, get the # of collisions which happened during the last collision check (typically, last frame)
873 int beam_get_num_collisions(int objnum)
876 if((objnum < 0) || (objnum >= MAX_OBJECTS)){
880 if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
884 if((Beams[Objects[objnum].instance].objnum != objnum) || (Beams[Objects[objnum].instance].objnum < 0)){
889 // return the # of recent collisions
890 return Beams[Objects[objnum].instance].r_collision_count;
893 // stuff collision info, returns 1 on success
894 int beam_get_collision(int objnum, int num, int *collision_objnum, mc_info **cinfo)
897 if((objnum < 0) || (objnum >= MAX_OBJECTS)){
901 if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
905 if((Beams[Objects[objnum].instance].objnum != objnum) || (Beams[Objects[objnum].instance].objnum < 0)){
909 if(num >= Beams[Objects[objnum].instance].r_collision_count){
915 *cinfo = &Beams[Objects[objnum].instance].r_collisions[num].cinfo;
916 *collision_objnum = Beams[Objects[objnum].instance].r_collisions[num].c_objnum;
920 // pause all looping beam sounds
921 void beam_pause_sounds()
925 // set all beam volumes to 0
926 moveup = GET_FIRST(&Beam_used_list);
930 while(moveup != END_OF_LIST(&Beam_used_list)){
931 // set the volume to 0, if he has a looping beam sound
932 if(moveup->beam_sound_loop >= 0){
933 snd_set_volume(moveup->beam_sound_loop, 0.0f);
937 moveup = GET_NEXT(moveup);
941 // unpause looping beam sounds
942 void beam_unpause_sounds()
946 // recalc all beam sounds
947 moveup = GET_FIRST(&Beam_used_list);
951 while(moveup != END_OF_LIST(&Beam_used_list)){
952 beam_recalc_sounds(moveup);
955 moveup = GET_NEXT(moveup);
960 // -----------------------------===========================------------------------------
961 // BEAM MOVEMENT FUNCTIONS
962 // -----------------------------===========================------------------------------
964 // move a type A beam weapon
965 void beam_type_a_move(beam *b)
970 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
971 // get the "originating point" of the beam for this frame. essentially bashes last_start
972 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
974 // if the "warming up" timestamp has not expired
975 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
979 // put the "last_shot" point arbitrarily far away
980 vm_vec_sub(&dir, &b->last_shot, &b->last_start);
981 vm_vec_normalize_quick(&dir);
982 vm_vec_scale_add(&b->last_shot, &b->last_start, &dir, BEAM_FAR_LENGTH);
983 Assert(is_valid_vec(&b->last_shot));
986 // move a type B beam weapon
987 #define BEAM_T(b) ( ((b->binfo.delta_ang / b->life_total) * (b->life_total - b->life_left)) / b->binfo.delta_ang )
988 void beam_type_b_move(beam *b)
994 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
995 // get the "originating point" of the beam for this frame. essentially bashes last_start
996 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
998 // if the "warming up" timestamp has not expired
999 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1003 // if the two direction vectors are _really_ close together, just use the original direction
1004 dot_save = vm_vec_dot(&b->binfo.dir_a, &b->binfo.dir_b);
1005 if((double)dot_save >= 0.999999999){
1006 actual_dir = b->binfo.dir_a;
1008 // otherwise move towards the dir we calculated when firing this beam
1010 vm_vec_interp_constant(&actual_dir, &b->binfo.dir_a, &b->binfo.dir_b, BEAM_T(b));
1013 // now recalculate shot_point to be shooting through our new point
1014 vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, BEAM_FAR_LENGTH);
1015 int is_valid = is_valid_vec(&b->last_shot);
1018 actual_dir = b->binfo.dir_a;
1019 vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, BEAM_FAR_LENGTH);
1024 void beam_type_c_move(beam *b)
1029 if(b->objp == NULL){
1034 // type c beams only last one frame so we never have to "move" them.
1035 temp = b->targeting_laser_offset;
1036 vm_vec_unrotate(&b->last_start, &temp, &b->objp->orient);
1037 vm_vec_add2(&b->last_start, &b->objp->pos);
1038 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->objp->orient.fvec, BEAM_FAR_LENGTH);
1042 void beam_type_d_move(beam *b)
1044 int shot_index, fire_wait;
1045 vector temp, temp2, dir;
1047 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1048 // get the "originating point" of the beam for this frame. essentially bashes last_start
1049 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
1051 // if the "warming up" timestamp has not expired
1052 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1056 // determine what stage of the beam we're in
1057 beam_type_d_get_status(b, &shot_index, &fire_wait);
1059 // if we've changed shot index
1060 if(shot_index != b->shot_index){
1061 // set the new index
1062 b->shot_index = shot_index;
1068 // if we're in the fire wait stage
1069 b->flags &= ~BF_SAFETY;
1071 b->flags |= BF_SAFETY;
1074 // put the "last_shot" point arbitrarily far away
1075 vm_vec_sub(&dir, &b->last_shot, &b->last_start);
1076 vm_vec_normalize_quick(&dir);
1077 vm_vec_scale_add(&b->last_shot, &b->last_start, &dir, BEAM_FAR_LENGTH);
1078 Assert(is_valid_vec(&b->last_shot));
1080 void beam_type_d_get_status(beam *b, int *shot_index, int *fire_wait)
1082 float shot_time = b->life_total / (float)b->binfo.shot_count;
1083 float beam_time = b->life_total - b->life_left;
1085 // determine what "shot" we're on
1086 *shot_index = (int)(beam_time / shot_time);
1087 Assert(*shot_index < b->binfo.shot_count);
1088 if(*shot_index >= b->binfo.shot_count){
1089 *shot_index = b->binfo.shot_count - 1;
1092 // determine if its the firing or waiting section of the shot (fire happens first, THEN wait)
1094 if(beam_time > ((shot_time * (*shot_index)) + (shot_time * 0.5f))){
1100 void beam_type_e_move(beam *b)
1102 vector temp, turret_norm;
1104 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1105 // get the "originating point" of the beam for this frame. essentially bashes last_start
1106 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &turret_norm, 1, &temp);
1108 // if the "warming up" timestamp has not expired
1109 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1113 // put the "last_shot" point arbitrarily far away
1114 vm_vec_scale_add(&b->last_shot, &b->last_start, &turret_norm, BEAM_FAR_LENGTH);
1115 Assert(is_valid_vec(&b->last_shot));
1118 // pre-move (before collision checking - but AFTER ALL OTHER OBJECTS HAVE BEEN MOVED)
1119 void beam_move_all_pre()
1124 // zero lights for this frame yet
1125 Beam_light_count = 0;
1127 // traverse through all active beams
1128 moveup = GET_FIRST(&Beam_used_list);
1129 while(moveup != END_OF_LIST(&Beam_used_list)){
1133 // unset collision info
1134 b->f_collision_count = 0;
1136 if(!physics_paused){
1139 // type A beam weapons don't move
1141 beam_type_a_move(b);
1144 // type B beam weapons move across the target somewhat randomly
1146 beam_type_b_move(b);
1149 // type C beam weapons are attached to a fighter - pointing forward
1151 beam_type_c_move(b);
1156 beam_type_d_move(b);
1161 beam_type_e_move(b);
1164 // illegal beam type
1171 moveup = GET_NEXT(moveup);
1175 // post-collision time processing for beams
1176 void beam_move_all_post()
1181 beam_weapon_info *bwi;
1183 // traverse through all active beams
1184 moveup = GET_FIRST(&Beam_used_list);
1185 while(moveup != END_OF_LIST(&Beam_used_list)){
1186 bwi = &Weapon_info[moveup->weapon_info_index].b_info;
1188 // check the status of the beam
1189 bf_status = beam_ok_to_fire(moveup);
1191 // if we're warming up
1192 if(moveup->warmup_stamp != -1){
1193 next_one = GET_NEXT(moveup);
1195 // should we be stopping?
1197 beam_delete(moveup);
1199 // if the warming up timestamp has expired, start firing
1200 if(timestamp_elapsed(moveup->warmup_stamp)){
1202 if(!beam_start_firing(moveup)){
1203 beam_delete(moveup);
1206 // add a muzzle light for the shooter
1207 beam_add_light(moveup, OBJ_INDEX(moveup->objp), 0, NULL);
1215 // if we're warming down
1216 else if(moveup->warmdown_stamp != -1){
1217 next_one = GET_NEXT(moveup);
1219 // should we be stopping?
1221 beam_delete(moveup);
1223 // if we're done warming down, the beam is finished
1224 if(timestamp_elapsed(moveup->warmdown_stamp)){
1225 beam_delete(moveup);
1234 // otherwise, we're firing away.........
1236 // add a muzzle light for the shooter
1237 beam_add_light(moveup, OBJ_INDEX(moveup->objp), 0, NULL);
1239 // subtract out the life left for the beam
1240 if(!physics_paused){
1241 moveup->life_left -= flFrametime;
1244 // if we're past the shrink point, start shrinking the beam
1245 if(moveup->life_left <= (moveup->life_total * bwi->beam_shrink_factor)){
1246 moveup->flags |= BF_SHRINK;
1249 // if we're shrinking the beam
1250 if(moveup->flags & BF_SHRINK){
1251 moveup->shrink -= bwi->beam_shrink_pct * flFrametime;
1252 if(moveup->shrink < 0.1f){
1253 moveup->shrink = 0.1f;
1259 next_one = GET_NEXT(moveup);
1261 // if beam should abruptly stop
1262 if(bf_status == -1){
1263 beam_delete(moveup);
1265 // if the beam should just power down
1267 beam_start_warmdown(moveup);
1275 // increment framecount
1276 moveup->framecount++;
1278 // type c weapons live for one frame only
1279 if(moveup->type == BEAM_TYPE_C){
1280 if(moveup->framecount > 1){
1281 next_one = GET_NEXT(moveup);
1283 beam_delete(moveup);
1288 // done firing, so go into the warmdown phase
1290 if((moveup->life_left <= 0.0f) && (moveup->warmdown_stamp == -1)){
1291 beam_start_warmdown(moveup);
1293 moveup = GET_NEXT(moveup);
1298 // handle any collisions which occured collision (will take care of applying damage to all objects which got hit)
1299 beam_handle_collisions(moveup);
1301 // recalculate beam sounds
1302 beam_recalc_sounds(moveup);
1305 moveup = GET_NEXT(moveup);
1308 // apply all beam lighting
1309 beam_apply_lighting();
1311 // process beam culling info
1314 if(Beam_test_stamp == -1){
1315 Beam_test_stamp = timestamp(BEAM_TEST_STAMP_TIME);
1317 Beam_test_framecount = 0;
1319 if(timestamp_elapsed(Beam_test_stamp)){
1320 // report the results
1321 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));
1324 Beam_test_stamp = timestamp(BEAM_TEST_STAMP_TIME);
1328 Beam_test_framecount = 0;
1330 Beam_test_framecount++;
1337 // -----------------------------===========================------------------------------
1338 // BEAM RENDERING FUNCTIONS
1339 // -----------------------------===========================------------------------------
1341 // render a beam weapon
1342 #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);
1343 #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);
1344 #define P_VERTICES() do { for(idx=0; idx<4; idx++){ g3_project_vertex(verts[idx]); } } while(0);
1346 void beam_render(beam_weapon_info *bwi, vector *start, vector *shot, float shrink)
1349 vertex h1[4]; // halves of a beam section
1350 vertex *verts[4] = { &h1[0], &h1[1], &h1[2], &h1[3] };
1351 vector fvec, top1, bottom1, top2, bottom2;
1354 // bogus weapon info index
1359 // if the beam start and endpoints are the same
1360 if(vm_vec_same(start, shot)){
1364 // get beam direction
1365 vm_vec_sub(&fvec, shot, start);
1366 vm_vec_normalize_quick(&fvec);
1368 // turn off backface culling
1371 // draw all sections
1372 for(s_idx=0; s_idx<bwi->beam_num_sections; s_idx++){
1373 // calculate the beam points
1374 scale = frand_range(1.0f - bwi->sections[s_idx].flicker, 1.0f + bwi->sections[s_idx].flicker);
1375 beam_calc_facing_pts(&top1, &bottom1, &fvec, start, bwi->sections[s_idx].width * scale * shrink, bwi->sections[s_idx].z_add);
1376 beam_calc_facing_pts(&top2, &bottom2, &fvec, shot, bwi->sections[s_idx].width * scale * scale * shrink, bwi->sections[s_idx].z_add);
1377 R_VERTICES(); // rotate and project the vertices
1379 STUFF_VERTICES(); // stuff the beam with creamy goodness (texture coords)
1381 // set the right texture with additive alpha, and draw the poly
1382 gr_set_bitmap(bwi->sections[s_idx].texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
1383 g3_draw_poly( 4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1386 // turn backface culling back on
1390 // generate particles for the muzzle glow
1391 int hack_time = 100;
1394 dc_get_arg(ARG_INT);
1395 hack_time = Dc_arg_int;
1397 void beam_generate_muzzle_particles(beam *b)
1402 vector turret_norm, turret_pos, particle_pos, particle_dir, p_temp;
1404 particle_info pinfo;
1406 // if our hack stamp has expired
1407 if(!((Beam_muzzle_stamp == -1) || timestamp_elapsed(Beam_muzzle_stamp))){
1411 // never generate anything past about 1/5 of the beam fire time
1412 if(b->warmup_stamp == -1){
1417 wip = &Weapon_info[b->weapon_info_index];
1419 // no specified particle for this beam weapon
1420 if(wip->b_info.beam_particle_ani < 0){
1424 // reset the hack stamp
1425 Beam_muzzle_stamp = timestamp(hack_time);
1427 // randomly generate 10 to 20 particles
1428 particle_count = (int)frand_range(0.0f, (float)wip->b_info.beam_particle_count);
1430 // get turret info - position and normal
1431 turret_pos = b->subsys->system_info->pnt;
1432 turret_norm = b->subsys->system_info->turret_norm;
1434 // randomly perturb a vector within a cone around the normal
1435 vm_vector_2_matrix(&m, &turret_norm, NULL, NULL);
1436 for(idx=0; idx<particle_count; idx++){
1437 // get a random point in the cone
1438 vm_vec_random_cone(&particle_dir, &turret_norm, wip->b_info.beam_particle_angle, &m);
1439 p_temp = turret_pos;
1440 vm_vec_scale_add(&p_temp, &turret_pos, &particle_dir, wip->b_info.beam_muzzle_radius * frand_range(0.75f, 0.9f));
1442 // transform into world coords
1443 vm_vec_unrotate(&particle_pos, &p_temp, &b->objp->orient);
1444 vm_vec_add2(&particle_pos, &b->objp->pos);
1445 p_temp = particle_dir;
1446 vm_vec_unrotate(&particle_dir, &p_temp, &b->objp->orient);
1448 // now generate some interesting values for the particle
1449 float p_time_ref = wip->b_info.beam_life + ((float)wip->b_info.beam_warmup / 1000.0f);
1450 float p_life = frand_range(p_time_ref * 0.5f, p_time_ref * 0.7f);
1451 float p_vel = (wip->b_info.beam_muzzle_radius / p_life) * frand_range(0.85f, 1.2f);
1452 vm_vec_scale(&particle_dir, -p_vel);
1454 memset(&pinfo, 0, sizeof(particle_info));
1455 pinfo.pos = particle_pos;
1456 pinfo.vel = particle_dir;
1457 pinfo.lifetime = p_life;
1458 pinfo.attached_objnum = -1;
1459 pinfo.attached_sig = 0;
1460 pinfo.rad = wip->b_info.beam_particle_radius;
1462 pinfo.type = PARTICLE_BITMAP;
1463 pinfo.optional_data = wip->b_info.beam_particle_ani;
1464 pinfo.tracer_length = -1.0f;
1465 particle_create(&pinfo);
1469 // render the muzzle glow for a beam weapon
1470 void beam_render_muzzle_glow(beam *b)
1473 weapon_info *wip = &Weapon_info[b->weapon_info_index];
1474 beam_weapon_info *bwi = &Weapon_info[b->weapon_info_index].b_info;
1475 float pct, rand_val;
1477 // if we don't have a glow bitmap
1478 if(bwi->beam_glow_bitmap < 0){
1482 // if the beam is warming up, scale the glow
1483 if(b->warmup_stamp != -1){
1485 pct = BEAM_WARMUP_PCT(b);
1488 // if the beam is warming down
1489 if(b->warmdown_stamp != -1){
1491 pct = 1.0f - BEAM_WARMDOWN_PCT(b);
1494 // otherwise the beam is really firing
1497 rand_val = frand_range(0.90f, 1.0f);
1501 g3_rotate_vertex(&v, &b->last_start);
1502 gr_set_bitmap( bwi->beam_glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.8f * pct);
1504 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * rand_val, TMAP_FLAG_TEXTURED);
1508 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.75f * rand_val, TMAP_FLAG_TEXTURED);
1511 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.45f * rand_val, TMAP_FLAG_TEXTURED);
1514 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.25f * rand_val, TMAP_FLAG_TEXTURED);
1518 // render all beam weapons
1519 void beam_render_all()
1523 // traverse through all active beams
1524 moveup = GET_FIRST(&Beam_used_list);
1525 while(moveup != END_OF_LIST(&Beam_used_list)){
1526 // each beam type renders a little bit differently
1527 if((moveup->warmup_stamp == -1) && (moveup->warmdown_stamp == -1) && !(moveup->flags & BF_SAFETY)){
1528 // HACK - if this is the first frame the beam is firing, don't render it
1529 if(moveup->life_left >= moveup->life_total - 0.0001f){
1531 moveup = GET_NEXT(moveup);
1535 // render the beam itself
1536 Assert(moveup->weapon_info_index >= 0);
1537 if(moveup->weapon_info_index < 0){
1538 moveup = GET_NEXT(moveup);
1541 beam_render(&Weapon_info[moveup->weapon_info_index].b_info, &moveup->last_start, &moveup->last_shot, moveup->shrink);
1544 // render the muzzle glow
1545 beam_render_muzzle_glow(moveup);
1547 // maybe generate some muzzle particles
1548 beam_generate_muzzle_particles(moveup);
1551 moveup = GET_NEXT(moveup);
1555 // output top and bottom vectors
1556 // fvec == forward vector (eye viewpoint basically. in world coords)
1557 // pos == world coordinate of the point we're calculating "around"
1558 // w == width of the diff between top and bottom around pos
1559 void beam_calc_facing_pts( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1566 vm_vec_sub( &rvec, &Eye_position, &temp );
1567 vm_vec_normalize( &rvec );
1569 vm_vec_crossprod(&uvec,fvec,&rvec);
1570 vm_vec_normalize(&uvec);
1572 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1573 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1576 // light scale factor
1577 float blight = 25.5f;
1580 dc_get_arg(ARG_FLOAT);
1581 blight = Dc_arg_float;
1584 // call to add a light source to a small object
1585 void beam_add_light_small(beam *bm, object *objp, vector *pt_override = NULL)
1588 beam_weapon_info *bwi;
1601 Assert(objp != NULL);
1605 Assert(bm->weapon_info_index >= 0);
1606 wip = &Weapon_info[bm->weapon_info_index];
1610 noise = frand_range(1.0f - bwi->sections[0].flicker, 1.0f + bwi->sections[0].flicker);
1612 // widest part of the beam
1613 float light_rad = beam_get_widest(bm) * blight * noise;
1615 // nearest point on the beam, and its distance to the ship
1617 if(pt_override == NULL){
1619 vm_vec_dist_to_line(&objp->pos, &bm->last_start, &bm->last_shot, &near_pt, &dist);
1620 if(dist > light_rad){
1624 near_pt = *pt_override;
1627 // average rgb of the beam
1628 float fr = (float)wip->laser_color_1.red / 255.0f;
1629 float fg = (float)wip->laser_color_1.green / 255.0f;
1630 float fb = (float)wip->laser_color_1.blue / 255.0f;
1632 // add a unique light
1633 // noise *= 0.1f; // a little less noise here, since we want the beam to generally cast a bright light
1634 light_add_point_unique(&near_pt, light_rad * 0.0001f, light_rad, 1.0f, fr, fg, fb, OBJ_INDEX(objp));
1637 // call to add a light source to a large object
1638 void beam_add_light_large(beam *bm, object *objp, vector *pt0, vector *pt1)
1641 beam_weapon_info *bwi;
1654 Assert(objp != NULL);
1658 Assert(bm->weapon_info_index >= 0);
1659 wip = &Weapon_info[bm->weapon_info_index];
1663 noise = frand_range(1.0f - bwi->sections[0].flicker, 1.0f + bwi->sections[0].flicker);
1665 // widest part of the beam
1666 float light_rad = beam_get_widest(bm) * blight * noise;
1668 // average rgb of the beam
1669 float fr = (float)wip->laser_color_1.red / 255.0f;
1670 float fg = (float)wip->laser_color_1.green / 255.0f;
1671 float fb = (float)wip->laser_color_1.blue / 255.0f;
1673 // add a unique light
1674 noise *= 0.1f; // a little less noise here, since we want the beam to generally cast a bright light
1675 light_add_tube(pt0, pt1, 1.0f, light_rad, 1.0f * noise, fr, fg, fb, OBJ_INDEX(objp));
1678 // mark an object as being lit
1679 void beam_add_light(beam *b, int objnum, int source, vector *c_point)
1683 // if we're out of light slots!
1684 if(Beam_light_count >= MAX_BEAM_LIGHT_INFO){
1690 l = &Beam_lights[Beam_light_count++];
1693 l->source = (ubyte)source;
1695 // only type 2 lights (from collisions) need a collision point
1696 if(c_point != NULL){
1697 l->c_point = *c_point;
1699 Assert(source != 2);
1706 // apply lighting from any beams
1707 void beam_apply_lighting()
1712 beam_weapon_info *bwi;
1714 // convert all beam lights into real lights
1715 for(idx=0; idx<Beam_light_count; idx++){
1717 l = &Beam_lights[idx];
1720 if((l->objnum < 0) || (l->objnum >= MAX_OBJECTS) || (l->bm == NULL)){
1724 bwi = &Weapon_info[l->bm->weapon_info_index].b_info;
1726 // different light types
1728 // from the muzzle of the gun
1730 // a few meters in from the of muzzle
1731 vm_vec_sub(&dir, &l->bm->last_start, &l->bm->last_shot);
1732 vm_vec_normalize_quick(&dir);
1733 vm_vec_scale_add(&pt, &l->bm->last_start, &dir, bwi->beam_muzzle_radius * 5.0f);
1735 beam_add_light_small(l->bm, &Objects[l->objnum], &pt);
1738 // from the beam passing by
1741 switch(Objects[l->objnum].type){
1743 Assert(Objects[l->objnum].instance >= 0);
1746 if(Ship_info[Ships[Objects[l->objnum].instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)){
1747 beam_add_light_large(l->bm, &Objects[l->objnum], &l->bm->last_start, &l->bm->last_shot);
1751 beam_add_light_small(l->bm, &Objects[l->objnum]);
1755 // asteroids get small lights
1757 beam_add_light_small(l->bm, &Objects[l->objnum]);
1760 // debris gets small lights
1762 beam_add_light_small(l->bm, &Objects[l->objnum]);
1769 // a few meters from the collision point
1770 vm_vec_sub(&dir, &l->bm->last_start, &l->c_point);
1771 vm_vec_normalize_quick(&dir);
1772 vm_vec_scale_add(&pt, &l->c_point, &dir, bwi->beam_muzzle_radius * 5.0f);
1774 beam_add_light_small(l->bm, &Objects[l->objnum], &pt);
1780 // -----------------------------===========================------------------------------
1781 // BEAM BOOKKEEPING FUNCTIONS
1782 // -----------------------------===========================------------------------------
1785 void beam_delete(beam *b)
1787 // remove from active list and put on free list
1788 list_remove(&Beam_used_list, b);
1789 list_append(&Beam_free_list, b);
1791 // delete our associated object
1792 // Assert(b->objnum >= 0);
1794 obj_delete(b->objnum);
1798 // kill the beam looping sound
1799 if(b->beam_sound_loop != -1){
1800 snd_stop(b->beam_sound_loop);
1801 b->beam_sound_loop = -1;
1806 Assert(Beam_count >= 0);
1807 nprintf(("General", "Recycled beam (%d beams remaining)\n", Beam_count));
1810 // given an object, return its model num
1811 int beam_get_model(object *objp)
1814 Assert(objp->instance >= 0);
1815 if(objp->instance < 0){
1822 return Ships[objp->instance].modelnum;
1825 Assert(Weapons[objp->instance].weapon_info_index >= 0);
1826 if(Weapons[objp->instance].weapon_info_index < 0){
1829 return Weapon_info[Weapons[objp->instance].weapon_info_index].model_num;
1832 Assert(Debris[objp->instance].is_hull);
1833 if(!Debris[objp->instance].is_hull){
1836 return Debris[objp->instance].model_num;
1840 subtype = Asteroids[objp->instance].asteroid_subtype;
1841 Assert(Asteroids[objp->instance].type >= 0);
1842 if(Asteroids[objp->instance].type < 0){
1845 return Asteroid_info[Asteroids[objp->instance].type].model_num[subtype];
1849 // this shouldn't happen too often
1850 mprintf(("Beam couldn't find a good find a good object model/type!! (%d)", objp->type));
1859 // start the warmup phase for the beam
1860 void beam_start_warmup(beam *b)
1862 // set the warmup stamp
1863 b->warmup_stamp = timestamp(Weapon_info[b->weapon_info_index].b_info.beam_warmup);
1865 // start playing warmup sound
1866 if(!(Game_mode & GM_STANDALONE_SERVER) && (Weapon_info[b->weapon_info_index].b_info.beam_warmup_sound >= 0)){
1867 snd_play_3d(&Snds[Weapon_info[b->weapon_info_index].b_info.beam_warmup_sound], &b->last_start, &View_position);
1871 // start the firing phase for the beam, return 0 if the beam failed to start, and should be deleted altogether
1872 int beam_start_firing(beam *b)
1874 // kill the warmup stamp so the rest of the code knows its firing
1875 b->warmup_stamp = -1;
1877 // any special stuff for each weapon type
1879 // re-aim type A and D beam weapons here, otherwise they tend to miss
1898 // determine if we can legitimately start firing, or if we need to take other action
1899 switch(beam_ok_to_fire(b)){
1904 beam_start_warmdown(b);
1908 // start the beam firing sound now, if we haven't already
1909 if((b->beam_sound_loop == -1) && (Weapon_info[b->weapon_info_index].b_info.beam_loop_sound >= 0)){
1910 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);
1913 snd_play_3d(&Snds[SND_BEAM_SHOT], &b->last_start, &View_position);
1920 // start the warmdown phase for the beam
1921 void beam_start_warmdown(beam *b)
1924 b->warmdown_stamp = timestamp(Weapon_info[b->weapon_info_index].b_info.beam_warmdown);
1926 // start the warmdown sound
1927 if(Weapon_info[b->weapon_info_index].b_info.beam_warmdown_sound >= 0){
1928 snd_play_3d(&Snds[Weapon_info[b->weapon_info_index].b_info.beam_warmdown_sound], &b->last_start, &View_position);
1931 // kill the beam looping sound
1932 if(b->beam_sound_loop != -1){
1933 snd_stop(b->beam_sound_loop);
1934 b->beam_sound_loop = -1;
1938 // recalculate beam sounds (looping sounds relative to the player)
1939 void beam_recalc_sounds(beam *b)
1941 beam_weapon_info *bwi;
1944 Assert(b->weapon_info_index >= 0);
1945 if(b->weapon_info_index < 0){
1948 bwi = &Weapon_info[b->weapon_info_index].b_info;
1950 // update the sound position relative to the player
1951 if(b->beam_sound_loop != -1){
1952 // get the point closest to the player's viewing position
1953 switch(vm_vec_dist_to_line(&View_position, &b->last_start, &b->last_shot, &pos, NULL)){
1954 // behind the beam, so use the start pos
1956 pos = b->last_start;
1959 // use the closest point
1961 // already calculated in vm_vec_dist_to_line(...)
1964 // past the beam, so use the shot pos
1970 snd_update_3d_pos(b->beam_sound_loop, &Snds[bwi->beam_loop_sound], &pos);
1975 // -----------------------------===========================------------------------------
1976 // BEAM AIMING FUNCTIONS
1977 // -----------------------------===========================------------------------------
1980 void beam_get_binfo(beam *b, float accuracy, int num_shots)
1985 vector turret_point, turret_norm;
1986 beam_weapon_info *bwi;
1989 // where the shot is originating from (b->last_start gets filled in)
1990 ship_get_global_turret_gun_info(b->objp, b->subsys, &turret_point, &turret_norm, 1, &p2);
1992 // get a model # to work with
1993 model_num = beam_get_model(b->target);
1998 // get beam weapon info
1999 Assert(b->weapon_info_index >= 0);
2000 if(b->weapon_info_index < 0){
2003 bwi = &Weapon_info[b->weapon_info_index].b_info;
2006 skill_level = Game_skill_level;
2007 if(Game_skill_level >= NUM_SKILL_LEVELS){
2008 skill_level = NUM_SKILL_LEVELS - 1;
2010 if(Game_skill_level < 0){
2014 // stuff num shots even though its only used for type D weapons
2015 b->binfo.shot_count = (ubyte)num_shots;
2016 if(b->binfo.shot_count > MAX_BEAM_SHOTS){
2017 b->binfo.shot_count = MAX_BEAM_SHOTS;
2020 // generate the proper amount of directional vectors
2022 // pick an accuracy. beam will be properly aimed at actual fire time
2024 // all we will do is decide whether or not we will hit - type A beam weapons are re-aimed immediately before firing
2025 b->binfo.shot_aim[0] = frand_range(0.0f, 1.0f + bwi->beam_miss_factor[skill_level] * accuracy);
2026 b->binfo.shot_count = 1;
2028 // get random model points, this is useful for big ships, because we never miss when shooting at them
2029 submodel_get_two_random_points(model_num, 0, &b->binfo.dir_a, &b->binfo.dir_b);
2032 // just 2 points in the "slash"
2034 beam_get_octant_points(model_num, b->target, (int)frand_range(0.0f, BEAM_NUM_GOOD_OCTANTS), Beam_good_slash_octants, &oct1, &oct2);
2037 vm_vec_sub(&b->binfo.dir_a, &oct1, &turret_point);
2038 vm_vec_normalize(&b->binfo.dir_a);
2041 vm_vec_sub(&b->binfo.dir_b, &oct2, &turret_point);
2042 vm_vec_normalize(&b->binfo.dir_b);
2045 b->binfo.delta_ang = fl_abs(vm_vec_delta_ang_norm(&b->binfo.dir_a, &b->binfo.dir_b, NULL));
2048 // nothing for this beam - its very special case
2052 // type D beams fire at small ship multiple times
2054 // get a bunch of shot aims
2055 for(idx=0; idx<b->binfo.shot_count; idx++){
2056 // MK, 9/3/99: Added pow() function to make increasingly likely to miss with subsequent shots. 30% more likely with each shot.
2057 float r = ((float) pow(1.3f, idx)) * bwi->beam_miss_factor[skill_level] * accuracy;
2058 b->binfo.shot_aim[idx] = frand_range(0.0f, 1.0f + r);
2062 // type e beams just fire straight
2064 b->binfo.shot_aim[0] = 0.0000001f;
2065 b->binfo.shot_count = 1;
2066 b->binfo.dir_a = turret_norm;
2067 b->binfo.dir_b = turret_norm;
2075 // aim the beam (setup last_start and last_shot - the endpoints). also recalculates collision pairs
2076 void beam_aim(beam *b)
2081 // type C beam weapons have no target
2082 if(b->target == NULL){
2083 Assert(b->type == BEAM_TYPE_C);
2084 if(b->type != BEAM_TYPE_C){
2088 // get a model # to work with
2090 model_num = beam_get_model(b->target);
2096 // setup our initial shot point and aim direction
2099 // where the shot is originating from (b->last_start gets filled in)
2100 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2102 // if we're targeting a subsystem - shoot directly at it
2103 if(b->target_subsys != NULL){
2104 // unrotate the center of the subsystem
2105 vm_vec_unrotate(&b->last_shot, &b->target_subsys->system_info->pnt, &b->target->orient);
2106 vm_vec_add2(&b->last_shot, &b->target->pos);
2107 vm_vec_sub(&temp, &b->last_shot, &b->last_start);
2109 vm_vec_scale_add(&b->last_shot, &b->last_start, &temp, 2.0f);
2113 // if we're shooting at a big ship - shoot directly at the model
2114 if((b->target->type == OBJ_SHIP) && (Ship_info[Ships[b->target->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP))){
2115 // rotate into world coords
2116 vm_vec_unrotate(&temp, &b->binfo.dir_a, &b->target->orient);
2117 vm_vec_add2(&temp, &b->target->pos);
2119 // get the shot point
2120 vm_vec_sub(&p2, &temp, &b->last_start);
2121 vm_vec_scale_add(&b->last_shot, &b->last_start, &p2, 2.0f);
2125 // point at the center of the target, then jitter based on shot_aim
2126 b->last_shot = b->target->pos;
2127 beam_jitter_aim(b, b->binfo.shot_aim[0]);
2131 // where the shot is originating from (b->last_start gets filled in)
2132 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2134 // set the shot point
2135 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->binfo.dir_a, BEAM_FAR_LENGTH);
2136 Assert(is_valid_vec(&b->last_shot));
2141 temp = b->targeting_laser_offset;
2142 vm_vec_unrotate(&b->last_start, &temp, &b->objp->orient);
2143 vm_vec_add2(&b->last_start, &b->objp->pos);
2144 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->objp->orient.fvec, BEAM_FAR_LENGTH);
2148 // where the shot is originating from (b->last_start gets filled in)
2149 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2151 // point at the center of the target, then jitter based on shot_aim
2152 b->last_shot = b->target->pos;
2153 beam_jitter_aim(b, b->binfo.shot_aim[b->shot_index]);
2154 nprintf(("AI", "Frame %i: FIRING\n", Framecount));
2158 // where the shot is originating from (b->last_start gets filled in)
2159 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2161 // point directly in the direction of the turret
2162 vm_vec_scale_add(&b->last_shot, &b->last_start, &temp, BEAM_FAR_LENGTH);
2169 // recalculate object pairs
2170 OBJ_RECALC_PAIRS((&Objects[b->objnum]));
2173 // given a model #, and an object, stuff 2 good world coord points
2174 void beam_get_octant_points(int modelnum, object *objp, int oct_index, int oct_array[BEAM_NUM_GOOD_OCTANTS][4], vector *v1, vector *v2)
2176 vector t1, t2, temp;
2177 polymodel *m = model_get(modelnum);
2179 // bad bad bad bad bad bad
2185 Assert((oct_index >= 0) && (oct_index < BEAM_NUM_GOOD_OCTANTS));
2187 // randomly pick octants
2188 t1 = oct_array[oct_index][2] ? m->octants[oct_array[oct_index][0]].max : m->octants[oct_array[oct_index][0]].min;
2189 t2 = oct_array[oct_index][3] ? m->octants[oct_array[oct_index][1]].max : m->octants[oct_array[oct_index][1]].min;
2190 Assert(!vm_vec_same(&t1, &t2));
2192 // get them in world coords
2193 vm_vec_unrotate(&temp, &t1, &objp->orient);
2194 vm_vec_add(v1, &temp, &objp->pos);
2195 vm_vec_unrotate(&temp, &t2, &objp->orient);
2196 vm_vec_add(v2, &temp, &objp->pos);
2199 // throw some jitter into the aim - based upon shot_aim
2200 void beam_jitter_aim(beam *b, float aim)
2202 vector forward, circle;
2204 float subsys_strength;
2206 // if the weapons subsystem is damaged or destroyed
2207 if((b->objp != NULL) && (b->objp->signature == b->sig) && (b->objp->type == OBJ_SHIP) && (b->objp->instance >= 0) && (b->objp->instance < MAX_SHIPS)){
2208 // get subsytem strength
2209 subsys_strength = ship_get_subsystem_strength(&Ships[b->objp->instance], SUBSYSTEM_WEAPONS);
2211 // when subsytem strength is 0, double the aim error factor
2212 aim += aim * (1.0f - subsys_strength);
2215 // shot aim is a direct linear factor of the target model's radius.
2216 // so, pick a random point on the circle
2217 vm_vec_sub(&forward, &b->last_shot, &b->last_start);
2218 vm_vec_normalize_quick(&forward);
2221 vm_vector_2_matrix(&m, &forward, NULL, NULL);
2223 // get a vector on the circle - this should appear to be pretty random
2224 // vm_vec_scale_add(&circle, &b->last_shot, &m.rvec, aim * b->target->radius);
2225 vm_vec_random_in_circle(&circle, &b->last_shot, &m, aim * b->target->radius, 0);
2227 // get the vector pointing to the circle point
2228 vm_vec_sub(&forward, &circle, &b->last_start);
2229 vm_vec_scale_add(&b->last_shot, &b->last_start, &forward, 2.0f);
2233 // -----------------------------===========================------------------------------
2234 // BEAM COLLISION FUNCTIONS
2235 // -----------------------------===========================------------------------------
2237 // collide a beam with a ship, returns 1 if we can ignore all future collisions between the 2 objects
2238 int beam_collide_ship(obj_pair *pair)
2243 mc_info test_collide;
2253 Assert(pair->a->instance >= 0);
2254 Assert(pair->a->type == OBJ_BEAM);
2255 Assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2256 b = &Beams[pair->a->instance];
2258 // Don't check collisions for warping out player if past stage 1.
2259 if ( Player->control_mode >= PCM_WARPOUT_STAGE1) {
2260 if ( pair->a == Player_obj ) return 0;
2261 if ( pair->b == Player_obj ) return 0;
2264 // if the "warming up" timestamp has not expired
2265 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2269 // if the beam is on "safety", don't collide with anything
2270 if(b->flags & BF_SAFETY){
2274 // if the colliding object is the shooting object, return 1 so this is culled
2275 if(pair->b == b->objp){
2279 // try and get a model
2280 model_num = beam_get_model(pair->b);
2292 Assert(pair->b->type == OBJ_SHIP);
2293 Assert(pair->b->instance >= 0);
2294 if((pair->b->type != OBJ_SHIP) || (pair->b->instance < 0)){
2297 shipp = &Ships[pair->b->instance];
2298 sip = &Ship_info[shipp->ship_info_index];
2300 // get the widest portion of the beam
2301 widest = beam_get_widest(b);
2304 test_collide.model_num = model_num;
2305 test_collide.submodel_num = -1;
2306 test_collide.orient = &pair->b->orient;
2307 test_collide.pos = &pair->b->pos;
2308 test_collide.p0 = &b->last_start;
2309 test_collide.p1 = &b->last_shot;
2311 // maybe do a sphereline
2312 if(widest > pair->b->radius * BEAM_AREA_PERCENT){
2313 test_collide.radius = beam_get_widest(b) * 0.5f;
2314 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_SPHERELINE;
2316 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2318 model_collide(&test_collide);
2321 if(test_collide.num_hits){
2322 // add to the collision list
2323 beam_add_collision(b, pair->b, &test_collide);
2326 // add this guy to the lighting list
2327 beam_add_light(b, OBJ_INDEX(pair->b), 1, NULL);
2329 // reset timestamp to timeout immediately
2330 pair->next_check_time = timestamp(0);
2335 // collide a beam with an asteroid, returns 1 if we can ignore all future collisions between the 2 objects
2336 int beam_collide_asteroid(obj_pair *pair)
2339 mc_info test_collide;
2348 Assert(pair->a->instance >= 0);
2349 Assert(pair->a->type == OBJ_BEAM);
2350 Assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2351 b = &Beams[pair->a->instance];
2353 // if the "warming up" timestamp has not expired
2354 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2358 // if the beam is on "safety", don't collide with anything
2359 if(b->flags & BF_SAFETY){
2363 // if the colliding object is the shooting object, return 1 so this is culled
2364 if(pair->b == b->objp){
2368 // try and get a model
2369 model_num = beam_get_model(pair->b);
2381 test_collide.model_num = model_num;
2382 test_collide.submodel_num = -1;
2383 test_collide.orient = &pair->b->orient;
2384 test_collide.pos = &pair->b->pos;
2385 test_collide.p0 = &b->last_start;
2386 test_collide.p1 = &b->last_shot;
2387 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2388 model_collide(&test_collide);
2391 if(test_collide.num_hits){
2392 // add to the collision list
2393 beam_add_collision(b, pair->b, &test_collide);
2396 // add this guy to the lighting list
2397 beam_add_light(b, OBJ_INDEX(pair->b), 1, NULL);
2399 // reset timestamp to timeout immediately
2400 pair->next_check_time = timestamp(0);
2405 // collide a beam with a missile, returns 1 if we can ignore all future collisions between the 2 objects
2406 int beam_collide_missile(obj_pair *pair)
2409 mc_info test_collide;
2418 Assert(pair->a->instance >= 0);
2419 Assert(pair->a->type == OBJ_BEAM);
2420 Assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2421 b = &Beams[pair->a->instance];
2423 // if the "warming up" timestamp has not expired
2424 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2428 // if the beam is on "safety", don't collide with anything
2429 if(b->flags & BF_SAFETY){
2433 // if the colliding object is the shooting object, return 1 so this is culled
2434 if(pair->b == b->objp){
2438 // try and get a model
2439 model_num = beam_get_model(pair->b);
2450 test_collide.model_num = model_num;
2451 test_collide.submodel_num = -1;
2452 test_collide.orient = &pair->b->orient;
2453 test_collide.pos = &pair->b->pos;
2454 test_collide.p0 = &b->last_start;
2455 test_collide.p1 = &b->last_shot;
2456 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2457 model_collide(&test_collide);
2460 if(test_collide.num_hits){
2461 // add to the collision list
2462 beam_add_collision(b, pair->b, &test_collide);
2465 // reset timestamp to timeout immediately
2466 pair->next_check_time = timestamp(0);
2471 // collide a beam with debris, returns 1 if we can ignore all future collisions between the 2 objects
2472 int beam_collide_debris(obj_pair *pair)
2475 mc_info test_collide;
2484 Assert(pair->a->instance >= 0);
2485 Assert(pair->a->type == OBJ_BEAM);
2486 Assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2487 b = &Beams[pair->a->instance];
2489 // if the "warming up" timestamp has not expired
2490 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2494 // if the beam is on "safety", don't collide with anything
2495 if(b->flags & BF_SAFETY){
2499 // if the colliding object is the shooting object, return 1 so this is culled
2500 if(pair->b == b->objp){
2504 // try and get a model
2505 model_num = beam_get_model(pair->b);
2516 test_collide.model_num = model_num;
2517 test_collide.submodel_num = -1;
2518 test_collide.orient = &pair->b->orient;
2519 test_collide.pos = &pair->b->pos;
2520 test_collide.p0 = &b->last_start;
2521 test_collide.p1 = &b->last_shot;
2522 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2523 model_collide(&test_collide);
2526 if(test_collide.num_hits){
2527 // add to the collision list
2528 beam_add_collision(b, pair->b, &test_collide);
2531 // add this guy to the lighting list
2532 beam_add_light(b, OBJ_INDEX(pair->b), 1, NULL);
2534 // reset timestamp to timeout immediately
2535 pair->next_check_time = timestamp(0);
2540 // early-out function for when adding object collision pairs, return 1 if the pair should be ignored
2541 int beam_collide_early_out(object *a, object *b)
2545 float cull_dist, cull_dot;
2546 vector dot_test, dot_test2, dist_test;
2549 Assert(a->instance >= 0);
2550 if(a->instance < 0){
2553 Assert(a->type == OBJ_BEAM);
2554 if(a->type != OBJ_BEAM){
2557 Assert(Beams[a->instance].objnum == OBJ_INDEX(a));
2558 if(Beams[a->instance].objnum != OBJ_INDEX(a)){
2561 bm = &Beams[a->instance];
2562 Assert(bm->weapon_info_index >= 0);
2563 if(bm->weapon_info_index < 0){
2566 bwi = &Weapon_info[bm->weapon_info_index];
2568 // if the second object has an invalid instance, bail
2569 if(b->instance < 0){
2578 // targeting lasers only hit ships
2579 if(bwi->b_info.beam_type == BEAM_TYPE_C){
2584 // targeting lasers only hit ships
2585 if(bwi->b_info.beam_type == BEAM_TYPE_C){
2588 // don't ever collide with non hull pieces
2589 if(!Debris[b->instance].is_hull){
2594 // targeting lasers only hit ships
2595 if(bwi->b_info.beam_type == BEAM_TYPE_C){
2598 // don't ever collide against laser weapons - duh
2599 if(Weapon_info[Weapons[b->instance].weapon_info_index].subtype == WP_LASER){
2605 // get full cull value
2606 beam_get_cull_vals(b, bm, &cull_dot, &cull_dist);
2608 // if the object fails these conditions, bail
2609 vm_vec_sub(&dist_test, &b->pos, &bm->last_start);
2610 dot_test = dist_test;
2611 vm_vec_sub(&dot_test2, &bm->last_shot, &bm->last_start);
2612 vm_vec_normalize_quick(&dot_test);
2613 vm_vec_normalize_quick(&dot_test2);
2614 // cull_dist == DIST SQUARED FOO!
2615 if((vm_vec_dotprod(&dot_test, &dot_test2) < cull_dot) && (vm_vec_mag_squared(&dist_test) > cull_dist)){
2623 // add a collision to the beam for this frame (to be evaluated later)
2624 void beam_add_collision(beam *b, object *hit_object, mc_info *cinfo)
2629 // if we haven't reached the limit for beam collisions, just add
2630 if(b->f_collision_count < MAX_FRAME_COLLISIONS){
2631 bc = &b->f_collisions[b->f_collision_count++];
2632 bc->c_objnum = OBJ_INDEX(hit_object);
2639 // otherwise, we've got to do some checking, ick.
2640 // I guess we can always just remove the farthest item
2642 for(idx=0; idx<MAX_FRAME_COLLISIONS; idx++){
2643 if((bc == NULL) || (b->f_collisions[idx].cinfo.hit_dist > bc->cinfo.hit_dist)){
2644 bc = &b->f_collisions[idx];
2653 bc->c_objnum = OBJ_INDEX(hit_object);
2657 // sort collisions for the frame
2658 int beam_sort_collisions_func(const void *e1, const void *e2)
2660 beam_collision *b1 = (beam_collision*)e1;
2661 beam_collision *b2 = (beam_collision*)e2;
2663 return b1->cinfo.hit_dist < b2->cinfo.hit_dist ? -1 : 1;
2666 // handle a hit on a specific object
2667 void beam_handle_collisions(beam *b)
2670 beam_collision r_coll[MAX_FRAME_COLLISIONS];
2671 int r_coll_count = 0;
2672 beam_weapon_info *bwi;
2676 // early out if we had no collisions
2677 if(b->f_collision_count <= 0){
2681 // get beam weapon info
2682 if((b->weapon_info_index < 0) || (b->weapon_info_index >= Num_weapon_types)){
2686 bwi = &Weapon_info[b->weapon_info_index].b_info;
2687 wi = &Weapon_info[b->weapon_info_index];
2689 // get the widest part of the beam
2690 widest = beam_get_widest(b);
2692 // the first thing we need to do is sort the collisions, from closest to farthest
2693 qsort(b->f_collisions, b->f_collision_count, sizeof(beam_collision), beam_sort_collisions_func);
2695 // now apply all collisions until we reach a ship which "stops" the beam or we reach the end of the list
2696 for(idx=0; idx<b->f_collision_count; idx++){
2700 int target = b->f_collisions[idx].c_objnum;
2702 // if we have an invalid object
2703 if((target < 0) || (target >= MAX_OBJECTS)){
2707 // try and get a model to deal with
2708 model_num = beam_get_model(&Objects[target]);
2714 beam_add_light(b, target, 2, &b->f_collisions[idx].cinfo.hit_point_world);
2716 // add to the recent collision list
2717 r_coll[r_coll_count].c_objnum = target;
2718 r_coll[r_coll_count].c_sig = Objects[target].signature;
2719 r_coll[r_coll_count].c_stamp = -1;
2720 r_coll[r_coll_count].cinfo = b->f_collisions[idx].cinfo;
2722 // if he was already on the recent collision list, copy his timestamp
2723 // also, be sure not to play the impact sound again.
2724 for(s_idx=0; s_idx<b->r_collision_count; s_idx++){
2725 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)){
2727 r_coll[r_coll_count].c_stamp = b->r_collisions[s_idx].c_stamp;
2729 // don't play the impact sound again
2734 // if the damage timestamp has expired or is not set yet, apply damage
2735 if((r_coll[r_coll_count].c_stamp == -1) || timestamp_elapsed(r_coll[r_coll_count].c_stamp)){
2737 r_coll[r_coll_count].c_stamp = timestamp(BEAM_DAMAGE_TIME);
2740 // if no damage - don't even indicate it has been hit
2741 if(wi->damage <= 0){
2745 // increment collision count
2748 // play the impact sound
2750 snd_play_3d( &Snds[wi->impact_snd], &b->f_collisions[idx].cinfo.hit_point_world, &Eye_position );
2754 if(do_damage && !physics_paused){
2755 // maybe draw an explosion
2756 if(wi->impact_weapon_expl_index >= 0){
2757 int ani_handle = weapon_get_expl_handle(wi->impact_weapon_expl_index, &b->f_collisions[idx].cinfo.hit_point_world, wi->impact_explosion_radius);
2758 particle_create( &b->f_collisions[idx].cinfo.hit_point_world, &vmd_zero_vector, 0.0f, wi->impact_explosion_radius, PARTICLE_BITMAP_PERSISTENT, ani_handle );
2761 switch(Objects[target].type){
2763 // hit the debris - the debris hit code takes care of checking for MULTIPLAYER_CLIENT, etc
2764 debris_hit(&Objects[target], &Objects[b->objnum], &b->f_collisions[idx].cinfo.hit_point_world, Weapon_info[b->weapon_info_index].damage);
2768 // detonate the missile
2769 Assert(Weapon_info[Weapons[Objects[target].instance].weapon_info_index].subtype == WP_MISSILE);
2770 if(!(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_MASTER){
2771 weapon_hit(&Objects[target], NULL, &Objects[target].pos);
2777 if(!(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_MASTER){
2778 asteroid_hit(&Objects[target], &Objects[b->objnum], &b->f_collisions[idx].cinfo.hit_point_world, Weapon_info[b->weapon_info_index].damage);
2783 // hit the ship - again, the innards of this code handle multiplayer cases
2784 // maybe vaporize ship.
2785 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);
2787 // if this is the first hit on the player ship. whack him
2789 beam_apply_whack(b, &Objects[target], &b->f_collisions[idx].cinfo.hit_point_world);
2795 // if the radius of the target is somewhat close to the radius of the beam, "stop" the beam here
2796 // for now : if its smaller than about 1/3 the radius of the ship
2797 if(widest <= (Objects[target].radius * BEAM_AREA_PERCENT) && !beam_will_tool_target(b, &Objects[target])){
2798 // set last_shot so we know where to properly draw the beam
2799 b->last_shot = b->f_collisions[idx].cinfo.hit_point_world;
2800 Assert(is_valid_vec(&b->last_shot));
2802 // done wif the beam
2807 // store the new recent collisions
2808 for(idx=0; idx<r_coll_count; idx++){
2809 b->r_collisions[idx] = r_coll[idx];
2811 b->r_collision_count = r_coll_count;
2814 // for a given object, and a firing beam, determine its critical dot product and range
2815 void beam_get_cull_vals(object *objp, beam *b, float *cull_dot, float *cull_dist)
2818 // debris and asteroids are classified as slow moving small objects
2819 // use cull_dot == potential cone of beam + 10% and 50.0 meters
2822 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.10f);
2823 *cull_dist = 50.0f * 50.0f;
2826 // treat missiles as fast-moving small objects
2828 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.5f);
2829 *cull_dist = 300.0f * 300.0f;
2833 // for cap ships, only cull for 90deg or better
2835 if(Ship_info[Ships[objp->instance].ship_info_index].flags & SIF_CAPITAL){
2842 // for large ships, cull at some multiple of the radius
2843 if(Ship_info[Ships[objp->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)){
2844 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.25f);
2846 *cull_dist = (objp->radius * 1.3f) * (objp->radius * 1.3f);
2850 // for everthing else, cull the same as missiles
2851 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.5f);
2852 *cull_dist = 300.0f * 300.0f;
2856 // BAD BAD BAD - but this code will cause everything to cull properly
2863 // FIXME - make sure we are truthfull representing the "cone" for all beam types
2864 // get the total possible cone for a given beam in radians
2865 float beam_get_cone_dot(beam *b)
2872 // even though these beams don't move, return a _very_ small value
2873 return (float)cos(fl_radian(50.5f));
2876 return vm_vec_dotprod(&b->binfo.dir_a, &b->binfo.dir_b);
2886 // if it is legal for the beam to fire, or continue firing
2887 int beam_ok_to_fire(beam *b)
2889 // type C beams are ok to fire all the time
2890 if(Weapon_info[b->weapon_info_index].b_info.beam_type == BEAM_TYPE_C){
2894 // if my own object is invalid, stop firing
2895 if(b->objp->signature != b->sig){
2896 mprintf(("BEAM : killing beam because of invalid parent object SIGNATURE!\n"));
2900 // if my own object is a ghost
2901 if(b->objp->type != OBJ_SHIP){
2902 mprintf(("BEAM : killing beam because of invalid parent object TYPE!\n"));
2906 // if the shooting turret is destroyed
2907 if(b->subsys->current_hits <= 0.0f){
2908 mprintf(("BEAM : killing beam because turret has been destroyed!\n"));
2912 // if the beam will be firing out of its FOV, power it down
2913 vector aim_dir, temp;
2914 vector turret_dir, turret_pos;
2915 vm_vec_sub(&aim_dir, &b->last_shot, &b->last_start);
2916 vm_vec_normalize(&aim_dir);
2917 ship_get_global_turret_gun_info(b->objp, b->subsys, &turret_pos, &turret_dir, 1, &temp);
2918 if(vm_vec_dotprod(&aim_dir, &turret_dir) < b->subsys->system_info->turret_fov){
2919 mprintf(("BEAM : powering beam down because of FOV condition!\n"));
2923 // ok to fire/continue firing
2927 // get the width of the widest section of the beam
2928 float beam_get_widest(beam *b)
2931 float widest = -1.0f;
2934 Assert(b->weapon_info_index >= 0);
2935 if(b->weapon_info_index < 0){
2940 for(idx=0; idx<Weapon_info[b->weapon_info_index].b_info.beam_num_sections; idx++){
2941 if(Weapon_info[b->weapon_info_index].b_info.sections[idx].width > widest){
2942 widest = Weapon_info[b->weapon_info_index].b_info.sections[idx].width;
2947 return widest * b->shrink;
2950 // apply a whack to a ship
2951 void beam_apply_whack(beam *b, object *objp, vector *hit_point)
2957 Assert((b != NULL) && (objp != NULL) && (hit_point != NULL));
2958 if((b == NULL) || (objp == NULL) || (hit_point == NULL)){
2961 Assert(b->weapon_info_index >= 0);
2962 wip = &Weapon_info[b->weapon_info_index];
2963 Assert((objp != NULL) && (objp->type == OBJ_SHIP) && (objp->instance >= 0) && (objp->instance < MAX_SHIPS));
2964 if((objp == NULL) || (objp->type != OBJ_SHIP) || (objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
2967 shipp = &Ships[objp->instance];
2968 if((shipp->ai_index < 0) || (shipp->ai_index >= MAX_AI_INFO)){
2972 // don't whack docking ships
2973 if(Ai_info[shipp->ai_index].ai_flags & AIF_DOCKED){
2977 // determine how big of a whack to apply
2981 if(wip->damage < b_whack_damage){
2982 whack = b_whack_small;
2984 whack = b_whack_big;
2988 vector whack_dir, temp;
2989 vm_vec_dist_to_line(&objp->pos, &b->last_start, &b->last_shot, &temp, &dist);
2990 vm_vec_sub(&whack_dir, &objp->pos, &temp);
2991 vm_vec_normalize(&whack_dir);
2992 vm_vec_scale(&whack_dir, whack);
2995 ship_apply_whack(&whack_dir, hit_point, objp);
2998 // return the amount of damage which should be applied to a ship. basically, filters friendly fire damage
2999 float beam_get_ship_damage(beam *b, object *objp)
3001 // if the beam is on the same team as the object
3002 Assert((objp != NULL) && (b != NULL));
3003 if((objp == NULL) || (b == NULL)){
3006 Assert((objp->type == OBJ_SHIP) && (objp->instance >= 0) && (objp->instance < MAX_SHIPS));
3007 if((objp->type != OBJ_SHIP) || (objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
3012 if((b->team == Ships[objp->instance].team) && (Weapon_info[b->weapon_info_index].damage > Beam_friendly_cap[Game_skill_level])){
3013 return Beam_friendly_cap[Game_skill_level];
3017 return Weapon_info[b->weapon_info_index].damage;
3020 // if the beam is likely to tool a given target before its lifetime expires
3021 int beam_will_tool_target(beam *b, object *objp)
3023 weapon_info *wip = &Weapon_info[b->weapon_info_index];
3024 float damage_in_a_few_seconds;
3031 // if the object is not a ship, bail
3032 if(objp->type != OBJ_SHIP){
3035 if((objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
3039 // if the beam is going to apply more damage in about 1 and a half than the hull of the ship can take
3040 damage_in_a_few_seconds = (TOOLTIME / (float)BEAM_DAMAGE_TIME) * wip->damage;
3041 if(objp->hull_strength < damage_in_a_few_seconds){
3050 float beam_accuracy = 1.0f;
3053 dc_get_arg(ARG_FLOAT);
3054 beam_accuracy = Dc_arg_float;
3061 for(idx=0; idx<Num_weapon_types; idx++){
3062 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3064 dc_printf("Beam %d : %s\n", b_count, Weapon_info[idx].name);
3068 void beam_test(int whee)
3071 object *orion, *fenris;
3072 ship_subsys *orion_turret, *fenris_turret, *fenris_radar, *orion_radar, *lookup;
3075 nprintf(("General", "Running beam test\n"));
3077 // lookup some stuff
3078 s1 = ship_name_lookup("GTD Orion 1");
3080 orion = &Objects[Ships[s1].objnum];
3081 s2 = ship_name_lookup("GTC Fenris 2");
3083 fenris = &Objects[Ships[s2].objnum];
3086 lookup = GET_FIRST(&Ships[s1].subsys_list);
3087 orion_turret = NULL;
3089 while(lookup != END_OF_LIST(&Ships[s1].subsys_list)){
3091 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret07")){
3092 orion_turret = lookup;
3096 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3097 orion_radar = lookup;
3100 lookup = GET_NEXT(lookup);
3102 Assert(orion_turret != NULL);
3103 Assert(orion_radar != NULL);
3104 lookup = GET_FIRST(&Ships[s2].subsys_list);
3105 fenris_turret = NULL;
3106 fenris_radar = NULL;
3107 while(lookup != END_OF_LIST(&Ships[s2].subsys_list)){
3109 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret07")){
3110 fenris_turret = lookup;
3114 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3115 fenris_radar = lookup;
3118 lookup = GET_NEXT(lookup);
3120 Assert(fenris_turret != NULL);
3121 Assert(fenris_radar != NULL);
3123 memset(&f, 0, sizeof(beam_fire_info));
3124 f.accuracy = beam_accuracy;
3125 f.beam_info_index = -1;
3126 f.beam_info_override = NULL;
3129 f.target_subsys = fenris_turret;
3130 f.turret = orion_turret;
3132 // find the first beam
3134 int beam_first = -1;
3137 for(idx=0; idx<Num_weapon_types; idx++){
3138 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3150 // maybe fire it, if its valid
3151 f.beam_info_index = beam_first + whee - 1;
3152 if(Weapon_info[f.beam_info_index].wi_flags & WIF_BEAM){
3153 HUD_printf("Firing %s\n", Weapon_info[f.beam_info_index].name);
3158 void beam_test_new(int whee)
3161 object *orion, *fenris, *herc2, *herc3, *herc6, *alpha;
3162 ship_subsys *orion_turret, *fenris_turret, *fenris_radar, *orion_radar, *lookup;
3165 nprintf(("General", "Running beam test\n"));
3167 // lookup some stuff
3168 s1 = ship_name_lookup("GTD Orion 1");
3170 orion = &Objects[Ships[s1].objnum];
3171 s2 = ship_name_lookup("GTC Fenris 2");
3173 fenris = &Objects[Ships[s2].objnum];
3174 s3 = ship_name_lookup("GTF Hercules 2");
3176 herc2 = &Objects[Ships[s3].objnum];
3177 s3 = ship_name_lookup("GTF Hercules 3");
3179 herc3 = &Objects[Ships[s3].objnum];
3180 s3 = ship_name_lookup("GTF Hercules 6");
3182 herc6 = &Objects[Ships[s3].objnum];
3183 s3 = ship_name_lookup("Alpha 1");
3185 alpha = &Objects[Ships[s3].objnum];
3188 lookup = GET_FIRST(&Ships[s1].subsys_list);
3189 orion_turret = NULL;
3191 while(lookup != END_OF_LIST(&Ships[s1].subsys_list)){
3193 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret07")){
3194 orion_turret = lookup;
3198 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3199 orion_radar = lookup;
3202 lookup = GET_NEXT(lookup);
3204 Assert(orion_turret != NULL);
3205 Assert(orion_radar != NULL);
3206 lookup = GET_FIRST(&Ships[s2].subsys_list);
3207 fenris_turret = NULL;
3208 fenris_radar = NULL;
3209 while(lookup != END_OF_LIST(&Ships[s2].subsys_list)){
3211 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret03")){
3212 fenris_turret = lookup;
3216 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3217 fenris_radar = lookup;
3220 lookup = GET_NEXT(lookup);
3222 Assert(fenris_turret != NULL);
3223 Assert(fenris_radar != NULL);
3225 memset(&f, 0, sizeof(beam_fire_info));
3226 f.accuracy = beam_accuracy;
3227 f.beam_info_override = NULL;
3230 f.target_subsys = NULL;
3231 f.turret = fenris_turret;
3234 // find the first beam
3236 int beam_first = -1;
3239 for(idx=0; idx<Num_weapon_types; idx++){
3240 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3252 // maybe fire it, if its valid
3253 f.beam_info_index = beam_first + whee - 1;
3254 if(Weapon_info[f.beam_info_index].wi_flags & WIF_BEAM){
3255 HUD_printf("Firing %s\n", Weapon_info[f.beam_info_index].name);