2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Weapon/Beam.cpp $
15 * all sorts of cool stuff about ships
18 * Revision 1.7 2004/09/20 01:31:45 theoddone33
21 * Revision 1.6 2003/05/25 02:30:44 taylor
24 * Revision 1.5 2002/06/17 06:33:11 relnev
25 * ryan's struct patch for gcc 2.95
27 * Revision 1.4 2002/06/09 04:41:29 relnev
28 * added copyright header
30 * Revision 1.3 2002/06/02 00:31:36 relnev
31 * implemented osregistry
33 * Revision 1.2 2002/05/07 03:16:53 theoddone33
34 * The Great Newline Fix
36 * Revision 1.1.1.1 2002/05/03 03:28:11 root
40 * 68 9/09/99 11:40p Dave
41 * Handle an SDL_assert() in beam code. Added supernova sounds. Play the right
42 * 2 end movies properly, based upon what the player did in the mission.
44 * 67 9/09/99 2:36p Mikek
45 * Put back in the "1.0f +" in BEAM_TYPE_A aiming. Not the best way to
48 * 65 9/08/99 10:29p Dave
49 * Make beam sound pausing and unpausing much safer.
51 * 64 9/06/99 12:46a Andsager
52 * Add weapon_explosion_ani LOD
54 * 63 9/03/99 5:12p Mikek
55 * Change miss_factor code, making it a lot more likely for beams to miss
56 * and also making them increasingly likely to miss with each subsequent
57 * shot in the multi-shot burst.
59 * 62 8/30/99 5:01p Dave
60 * Made d3d do less state changing in the nebula. Use new chat server for
63 * 61 8/28/99 7:29p Dave
64 * Fixed wingmen persona messaging. Make sure locked turrets don't count
65 * towards the # attacking a player.
67 * 60 8/27/99 9:07p Dave
68 * LOD explosions. Improved beam weapon accuracy.
70 * 59 8/26/99 10:15a Dave
71 * Don't apply beam whacks to docked ships.
73 * 58 7/31/99 1:16p Dave
74 * Use larger font for 1024 HUD flash text box. Make beam weapons aware of
75 * weapon subsystem damage on firing ship.
77 * 57 7/22/99 4:00p Dave
78 * Fixed beam weapon muzzle glow rendering. Externalized hud shield info.
80 * 56 7/19/99 7:20p Dave
81 * Beam tooling. Specialized player-killed-self messages. Fixed d3d nebula
84 * 55 7/15/99 9:20a Andsager
85 * FS2_DEMO initial checkin
87 * 54 7/09/99 5:54p Dave
88 * Seperated cruiser types into individual types. Added tons of new
89 * briefing icons. Campaign screen.
91 * 53 7/08/99 10:53a Dave
92 * New multiplayer interpolation scheme. Not 100% done yet, but still
93 * better than the old way.
95 * 52 7/02/99 10:51p Dave
96 * Limit friendly beam fire damage. :(
98 * 51 7/01/99 11:44a Dave
99 * Updated object sound system to allow multiple obj sounds per ship.
100 * Added hit-by-beam sound. Added killed by beam sound.
102 * 50 6/29/99 7:39p Dave
103 * Lots of small bug fixes.
105 * 49 6/29/99 2:53p Dave
106 * Re-enabled beam lighting.
108 * 48 6/25/99 3:04p Dave
111 * 47 6/24/99 3:00p Dave
114 * 46 6/23/99 4:49p Dave
115 * Temporarily removed beam lighting.
117 * 45 6/21/99 7:25p Dave
118 * netplayer pain packet. Added type E unmoving beams.
120 * 44 6/18/99 5:16p Dave
121 * Added real beam weapon lighting. Fixed beam weapon sounds. Added MOTD
122 * dialog to PXO screen.
124 * 43 6/14/99 10:45a Dave
125 * Made beam weapons specify accuracy by skill level in the weapons.tbl
127 * 42 6/09/99 2:55p Andsager
128 * Allow multiple asteroid subtypes (of large, medium, small) and follow
131 * 41 6/08/99 6:02p Jamesa
132 * Handle case where type B beam start and end directions are really
135 * 40 6/04/99 2:16p Dave
136 * Put in shrink effect for beam weapons.
138 * 39 5/14/99 11:47a Andsager
139 * Added beam_get_weapon_info_index(object *bm)
141 * 38 5/12/99 10:43a Andsager
142 * Increase max beam length
144 * 37 5/08/99 8:25p Dave
145 * Upped object pairs. First run of nebula lightning.
147 * 36 5/05/99 9:02p Dave
148 * Fixed D3D aabitmap rendering. Spiffed up nebula effect a bit (added
149 * rotations, tweaked values, made bitmap selection more random). Fixed
150 * D3D beam weapon clipping problem. Added D3d frame dumping.
152 * 35 5/03/99 9:07a Dave
153 * Pirate Bob. Changed beam test code a bit.
155 * 34 4/27/99 12:16a Dave
156 * Fixed beam weapon muzzle glow problem. Fixed premature timeout on the
157 * pxo server list screen. Fixed secondary firing for hosts on a
158 * standalone. Fixed wacky multiplayer weapon "shuddering" problem.
160 * 33 4/25/99 6:12p Johnson
161 * Fixed bug where multi-shot beams were getting the shot count from the
164 * 32 4/25/99 3:36p Dave
165 * Fixed nebula table code. Tweaked beam weapon explosion stuff.
167 * 31 4/23/99 2:33p Johnson
168 * Allow beams to shoot at missiles properly.
170 * 30 4/23/99 12:01p Johnson
171 * Added SIF_HUGE_SHIP
173 * 29 4/22/99 11:06p Dave
174 * Final pass at beam weapons. Solidified a lot of stuff. All that remains
175 * now is to tweak and fix bugs as they come up. No new beam weapon
178 * 28 4/21/99 6:15p Dave
179 * Did some serious housecleaning in the beam code. Made it ready to go
180 * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
181 * a handy macro for recalculating collision pairs for a given object.
183 * 27 4/20/99 6:39p Dave
184 * Almost done with artillery targeting. Added support for downloading
185 * images on the PXO screen.
187 * 26 4/19/99 11:01p Dave
188 * More sophisticated targeting laser support. Temporary checkin.
190 * 25 4/19/99 4:54p Johnson
191 * Removed infinite loop from beam_render_all() oops :)
193 * 24 4/16/99 5:54p Dave
194 * Support for on/off style "stream" weapons. Real early support for
195 * target-painting lasers.
197 * 23 4/04/99 2:13p Dave
198 * Put in area effect beam weapons. May be a bit too expensive though.
200 * 22 4/02/99 9:55a Dave
201 * Added a few more options in the weapons.tbl for beam weapons. Attempt
202 * at putting "pain" packets into multiplayer.
204 * 21 3/31/99 8:24p Dave
205 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
206 * and background nebulae. Added per-ship non-dimming pixel colors.
208 * 20 3/08/99 7:03p Dave
209 * First run of new object update system. Looks very promising.
211 * 19 3/04/99 6:09p Dave
212 * Added in sexpressions for firing beams and checking for if a ship is
215 * 18 3/02/99 9:25p Dave
216 * Added a bunch of model rendering debug code. Started work on fixing
217 * beam weapon wacky firing.
219 * 17 2/21/99 1:48p Dave
220 * Some code for monitoring datarate for multiplayer in detail.
222 * 16 2/11/99 3:08p Dave
223 * PXO refresh button. Very preliminary squad war support.
225 * 15 2/05/99 3:23p Mattf
226 * Made beams a little more forgiving when checking object types.
228 * 14 2/05/99 12:52p Dave
229 * Fixed Glide nondarkening textures.
231 * 13 2/04/99 6:29p Dave
232 * First full working rev of FS2 PXO support. Fixed Glide lighting
235 * 12 1/30/99 9:02p Dave
238 * 11 1/30/99 1:29a Dave
239 * Fixed nebula thumbnail problem. Full support for 1024x768 choose pilot
240 * screen. Fixed beam weapon death messages.
242 * 10 1/29/99 7:08p Johnson
243 * Put in fix for beam weapons which are on dying ships.
245 * 9 1/29/99 12:47a Dave
246 * Put in sounds for beam weapon. A bunch of interface screens (tech
249 * 8 1/27/99 9:56a Dave
250 * Temporary checkin of beam weapons for Dan to make cool sounds.
252 * 7 1/24/99 11:37p Dave
253 * First full rev of beam weapons. Very customizable. Removed some bogus
254 * Int3()'s in low level net code.
256 * 6 1/21/99 2:06p Dave
257 * Final checkin for multiplayer testing.
259 * 5 1/21/99 10:45a Dave
260 * More beam weapon stuff. Put in warmdown time.
262 * 4 1/14/99 12:48a Dave
263 * Todo list bug fixes. Made a pass at putting briefing icons back into
264 * FRED. Sort of works :(
266 * 3 1/12/99 12:53a Dave
267 * More work on beam weapons - made collision detection very efficient -
268 * collide against all object types properly - made 3 movement types
269 * smooth. Put in test code to check for possible non-darkening pixels on
272 * 2 1/08/99 2:08p Dave
273 * Fixed software rendering for pofview. Super early support for AWACS and
280 #include "linklist.h"
282 #include "objcollide.h"
284 #include "freespace.h"
287 #include "alphacolors.h"
289 #include "fireballs.h"
291 #include "asteroid.h"
293 #include "multimsgs.h"
295 #include "particle.h"
301 #include "hudmessage.h"
303 #include "lighting.h"
305 // ------------------------------------------------------------------------------------------------
306 // BEAM WEAPON DEFINES/VARS
309 // use this to extend a beam to "infinity"
310 #define BEAM_FAR_LENGTH 30000.0f
312 // this is the constant which defines when a beam is an "area" beam. meaning, when we switch on sphereline checking and when
313 // a beam gets "stopped" by an object. It is a percentage of the object radius which the beam must be wider than
314 #define BEAM_AREA_PERCENT 0.4f
316 // randomness factor - all beam weapon aiming is adjusted by +/- some factor within this range
317 #define BEAM_RANDOM_FACTOR 0.4f
320 #define BEAM_DAMAGE_TIME 170 // apply damage
321 #define MAX_SHOT_POINTS 30
322 #define SHOT_POINT_TIME 200 // 5 arcs a second
324 #define TOOLTIME 1500.0f
326 // max # of collisions we'll allow per frame
327 #define MAX_FRAME_COLLISIONS 5
330 #define BF_SAFETY (1<<0) // if this is set, don't collide or render for this frame. lifetime still increases though
331 #define BF_SHRINK (1<<1) // if this is set, the beam is in the warmdown phase
333 // beam struct (the actual weapon/object)
334 typedef struct beam {
336 int objnum; // our own objnum
337 int weapon_info_index;
338 int sig; // signature for the shooting object
339 object *objp; // the shooting object (who owns the turret that I am being fired from)
340 object *target; // target object
341 ship_subsys *target_subsys; // targeted subsys
342 int target_sig; // target sig
343 ship_subsys *subsys; // subsys its being fired from
344 beam *next, *prev; // link list stuff
345 vector targeting_laser_offset;
346 int framecount; // how many frames the beam has been active
347 int flags; // see BF_* defines
348 float shrink; // shrink factor
351 int warmup_stamp; // timestamp for "warming up"
352 int warmdown_stamp; // timestamp for "warming down"
353 int type; // see BEAM_TYPE_* defines in beam.h
354 float life_left; // in seconds
355 float life_total; // total life
356 // this vector has very special meaning. BEFORE performing collision checks, it basically implies a "direction". meaning
357 // the vector between it and last_start is where the beam will be aiming. AFTER performing collision checks, it is the
358 // literal world collision point on the object (or meaningless, if we hit nothing). The function beam_move_all_pre() is
359 // responsible for then setting it up pre-collision time
362 int shot_index; // for type D beam weapons
365 beam_collision r_collisions[MAX_FRAME_COLLISIONS]; // recent collisions
366 int r_collision_count; // # of recent collisions
368 // collision info for this frame
369 beam_collision f_collisions[MAX_FRAME_COLLISIONS]; // collisions for the current frame
370 int f_collision_count; // # of collisions we recorded this frame
372 // looping sound info, HANDLE
373 int beam_sound_loop; // -1 if none
378 // exactly how the beam will behave. by passing this is multiplayer from server to client, we can ensure that
379 // everything looks the same
383 beam Beams[MAX_BEAMS]; // all beams
384 beam Beam_free_list; // free beams
385 beam Beam_used_list; // used beams
386 int Beam_count = 0; // how many beams are in use
388 // octant indices. These are "good" pairs of octants to use for beam target
389 #define BEAM_NUM_GOOD_OCTANTS 8
390 int Beam_good_slash_octants[BEAM_NUM_GOOD_OCTANTS][4] = {
391 { 2, 5, 1, 0 }, // octant, octant, min/max pt, min/max pt
400 int Beam_good_shot_octants[BEAM_NUM_GOOD_OCTANTS][4] = {
401 { 5, 0, 1, 0 }, // octant, octant, min/max pt, min/max pt
411 // damage cap values for friendly beam fire
412 float Beam_friendly_cap[NUM_SKILL_LEVELS] = { 0.0f, 5.0f, 10.0f, 20.0f, 30.0f };
414 // beam lighting effects
415 int Beam_lighting = 1;
417 // debug stuff - keep track of how many collision tests we perform a second and how many we toss a second
418 #define BEAM_TEST_STAMP_TIME 4000 // every 4 seconds
419 int Beam_test_stamp = -1;
420 int Beam_test_ints = 0;
421 int Beam_test_ship = 0;
422 int Beam_test_ast = 0;
423 int Beam_test_framecount = 0;
425 // beam warmup completion %
426 #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 )
428 // beam warmdown completion %
429 #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 )
431 // timestamp for spewing muzzle particles
432 int Beam_muzzle_stamp = -1;
434 // link into the physics paused system
435 extern int physics_paused;
437 // beam lighting info
438 #define MAX_BEAM_LIGHT_INFO 100
439 typedef struct beam_light_info {
440 beam *bm; // beam casting the light
441 int objnum; // object getting light cast on it
442 ubyte source; // 0 to light the shooter, 1 for lighting any ship the beam passes, 2 to light the collision ship
443 vector c_point; // collision point for type 2 lights
446 beam_light_info Beam_lights[MAX_BEAM_LIGHT_INFO];
447 int Beam_light_count = 0;
449 float b_whack_small = 500.0f;
450 float b_whack_big = 1500.0f;
451 float b_whack_damage = 150.0f;
452 DCF(b_whack_small, "")
454 dc_get_arg(ARG_FLOAT);
455 b_whack_small = Dc_arg_float;
459 dc_get_arg(ARG_FLOAT);
460 b_whack_big = Dc_arg_float;
462 DCF(b_whack_damage, "")
464 dc_get_arg(ARG_FLOAT);
465 b_whack_damage = Dc_arg_float;
469 // ------------------------------------------------------------------------------------------------
470 // BEAM WEAPON FORWARD DECLARATIONS
474 void beam_delete(beam *b);
476 // handle a hit on a specific object
477 void beam_handle_collisions(beam *b);
480 void beam_get_binfo(beam *b, float accuracy, int num_shots);
482 // aim the beam (setup last_start and last_shot - the endpoints). also recalculates object collision info
483 void beam_aim(beam *b);
486 void beam_type_a_move(beam *b);
489 void beam_type_b_move(beam *b);
492 void beam_type_c_move(beam *b);
495 void beam_type_d_move(beam *b);
496 void beam_type_d_get_status(beam *b, int *shot_index, int *fire_wait);
499 void beam_type_e_move(beam *b);
501 // given a model #, and an object, stuff 2 good world coord points
502 void beam_get_octant_points(int modelnum, object *objp, int oct_index, int oct_array[BEAM_NUM_GOOD_OCTANTS][4], vector *v1, vector *v2);
504 // given an object, return its model num
505 int beam_get_model(object *objp);
507 // for a given object, and a firing beam, determine its critical dot product and range
508 void beam_get_cull_vals(object *objp, beam *b, float *cull_dot, float *cull_dist);
510 // get the total possible cone for a given beam in radians
511 float beam_get_cone_dot(beam *b);
513 // for rendering the beam effect
514 // output top and bottom vectors
515 // fvec == forward vector (eye viewpoint basically. in world coords)
516 // pos == world coordinate of the point we're calculating "around"
517 // w == width of the diff between top and bottom around pos
518 void beam_calc_facing_pts(vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add);
520 // render the muzzle glow for a beam weapon
521 void beam_render_muzzle_glow(beam *b);
523 // generate particles for the muzzle glow
524 void beam_generate_muzzle_particles(beam *b);
526 // throw some jitter into the aim - based upon shot_aim
527 void beam_jitter_aim(beam *b, float aim);
529 // if it is legal for the beam to continue firing
530 // returns -1 if the beam should stop firing immediately
531 // returns 0 if the beam should go to warmdown
532 // returns 1 if the beam can continue along its way
533 int beam_ok_to_fire(beam *b);
535 // start the warmup phase for the beam
536 void beam_start_warmup(beam *b);
538 // start the firing phase for the beam, return 0 if the beam failed to start, and should be deleted altogether
539 int beam_start_firing(beam *b);
541 // start the warmdown phase for the beam
542 void beam_start_warmdown(beam *b);
544 // add a collision to the beam for this frame (to be evaluated later)
545 void beam_add_collision(beam *b, object *hit_object, mc_info *cinfo);
547 // sort collisions for the frame
548 int beam_sort_collisions_func(const void *e1, const void *e2);
550 // get the width of the widest section of the beam
551 float beam_get_widest(beam *b);
553 // mark an object as being lit
554 void beam_add_light(beam *b, int objnum, int source, vector *c_point);
556 // apply lighting from any beams
557 void beam_apply_lighting();
559 // recalculate beam sounds (looping sounds relative to the player)
560 void beam_recalc_sounds(beam *b);
562 // apply a whack to a ship
563 void beam_apply_whack(beam *b, object *objp, vector *hit_point);
565 // return the amount of damage which should be applied to a ship. basically, filters friendly fire damage
566 float beam_get_ship_damage(beam *b, object *objp);
568 // if the beam is likely to tool a given target before its lifetime expires
569 int beam_will_tool_target(beam *b, object *objp);
572 // ------------------------------------------------------------------------------------------------
573 // BEAM WEAPON FUNCTIONS
576 // init at game startup
580 list_init( &Beam_free_list );
581 list_init( &Beam_used_list );
584 // initialize beam weapons for this level
585 void beam_level_init()
591 list_init( &Beam_free_list );
592 list_init( &Beam_used_list );
593 memset(Beams, 0, sizeof(beam) * MAX_BEAMS);
595 // Link all object slots into the free list
596 for (idx=0; idx<MAX_BEAMS; idx++) {
597 Beams[idx].objnum = -1;
598 list_append(&Beam_free_list, &Beams[idx] );
601 // reset muzzle particle spew timestamp
602 Beam_muzzle_stamp = -1;
605 // shutdown beam weapons for this level
606 void beam_level_close()
609 list_init( &Beam_free_list );
610 list_init( &Beam_used_list );
613 // fire a beam, returns nonzero on success. the innards of the code handle all the rest, foo
614 int beam_fire(beam_fire_info *fire_info)
622 if(fire_info == NULL){
627 // if we're out of beams, bail
628 if(Beam_count >= MAX_BEAMS){
632 // for now, only allow ship targets
633 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)){
637 // make sure the beam_info_index is valid
638 SDL_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));
639 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)){
642 wip = &Weapon_info[fire_info->beam_info_index];
644 // make sure a ship is firing this
645 SDL_assert((fire_info->shooter->type == OBJ_SHIP) && (fire_info->shooter->instance >= 0) && (fire_info->shooter->instance < MAX_SHIPS));
646 if((fire_info->shooter->type != OBJ_SHIP) || (fire_info->shooter->instance < 0) || (fire_info->shooter->instance >= MAX_SHIPS)){
649 firing_ship = &Ships[fire_info->shooter->instance];
652 new_item = GET_FIRST(&Beam_free_list);
653 SDL_assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
654 if(new_item == &Beam_free_list){
658 // remove from the free list
659 list_remove( &Beam_free_list, new_item );
661 // insert onto the end of used list
662 list_append( &Beam_used_list, new_item );
667 // fill in some values
668 new_item->warmup_stamp = -1;
669 new_item->warmdown_stamp = -1;
670 new_item->weapon_info_index = fire_info->beam_info_index;
671 new_item->objp = fire_info->shooter;
672 new_item->sig = fire_info->shooter->signature;
673 new_item->subsys = fire_info->turret;
674 new_item->life_left = wip->b_info.beam_life;
675 new_item->life_total = wip->b_info.beam_life;
676 new_item->r_collision_count = 0;
677 new_item->f_collision_count = 0;
678 new_item->target = fire_info->target;
679 new_item->target_subsys = fire_info->target_subsys;
680 new_item->target_sig = fire_info->target->signature;
681 new_item->beam_sound_loop = -1;
682 new_item->type = wip->b_info.beam_type;
683 new_item->targeting_laser_offset = fire_info->targeting_laser_offset;
684 new_item->framecount = 0;
686 new_item->shot_index = 0;
687 new_item->shrink = 1.0f;
688 new_item->team = (char)firing_ship->team;
690 // if the targeted subsystem is not NULL, force it to be a type A beam
691 if(new_item->target_subsys != NULL){
692 new_item->type = BEAM_TYPE_A;
695 // type D weapons can only fire at small ships and missiles
696 if(new_item->type == BEAM_TYPE_D){
697 // if its a targeted ship, get the target ship
698 if((fire_info->target != NULL) && (fire_info->target->type == OBJ_SHIP) && (fire_info->target->instance >= 0)){
699 ship *target_ship = &Ships[fire_info->target->instance];
701 // maybe force to be a type A
702 if(Ship_info[target_ship->ship_info_index].flags & (SIF_BIG_SHIP | SIF_CAPITAL | SIF_SUPERCAP | SIF_FREIGHTER | SIF_DRYDOCK | SIF_CARGO)){
703 new_item->type = BEAM_TYPE_A;
708 // ----------------------------------------------------------------------
709 // THIS IS THE CRITICAL POINT FOR MULTIPLAYER
710 // beam_get_binfo(...) determines exactly how the beam will behave over the course of its life
711 // it fills in binfo, which we can pass to clients in multiplayer
712 if(fire_info->beam_info_override != NULL){
713 new_item->binfo = *fire_info->beam_info_override;
715 beam_get_binfo(new_item, fire_info->accuracy, wip->b_info.beam_shots); // to fill in b_info - the set of directional aim vectors
718 // create the associated object
719 objnum = obj_create(OBJ_BEAM, -1, new_item - Beams, &vmd_identity_matrix, &vmd_zero_vector, 1.0f, OF_COLLIDES);
722 beam_delete(new_item);
723 nprintf(("General", "obj_create() failed for beam weapon! bah!\n"));
726 new_item->objnum = objnum;
728 // this sets up all info for the first frame the beam fires
729 beam_aim(new_item); // to fill in shot_point, etc.
731 // check to see if its legal to fire at this guy
732 if(beam_ok_to_fire(new_item) != 1){
733 beam_delete(new_item);
734 mprintf(("Killing beam at initial fire because of illegal targeting!!!\n"));
738 // if we're a multiplayer master - send a packet
739 if(MULTIPLAYER_MASTER){
740 send_beam_fired_packet(fire_info->shooter, fire_info->turret, fire_info->target, fire_info->beam_info_index, &new_item->binfo);
743 // start the warmup phase
744 beam_start_warmup(new_item);
749 // fire a targeting beam, returns objnum on success. a much much simplified version of a beam weapon
750 // targeting lasers last _one_ frame. For a continuous stream - they must be created every frame.
751 // this allows it to work smoothly in multiplayer (detect "trigger down". every frame just create a targeting laser firing straight out of the
752 // object. this way you get all the advantages of nice rendering and collisions).
753 // NOTE : only references beam_info_index and shooter
754 int beam_fire_targeting(beam_fire_info *fire_info)
762 if(fire_info == NULL){
767 // if we're out of beams, bail
768 if(Beam_count >= MAX_BEAMS){
772 // make sure the beam_info_index is valid
773 SDL_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));
774 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)){
777 wip = &Weapon_info[fire_info->beam_info_index];
779 // make sure a ship is firing this
780 SDL_assert((fire_info->shooter->type == OBJ_SHIP) && (fire_info->shooter->instance >= 0) && (fire_info->shooter->instance < MAX_SHIPS));
781 if((fire_info->shooter->type != OBJ_SHIP) || (fire_info->shooter->instance < 0) || (fire_info->shooter->instance >= MAX_SHIPS)){
784 firing_ship = &Ships[fire_info->shooter->instance];
788 new_item = GET_FIRST(&Beam_free_list);
789 SDL_assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
791 // remove from the free list
792 list_remove( &Beam_free_list, new_item );
794 // insert onto the end of used list
795 list_append( &Beam_used_list, new_item );
800 // maybe allocate some extra data based on the beam type
801 SDL_assert(wip->b_info.beam_type == BEAM_TYPE_C);
802 if(wip->b_info.beam_type != BEAM_TYPE_C){
806 // fill in some values
807 new_item->warmup_stamp = -1;
808 new_item->warmdown_stamp = -1;
809 new_item->weapon_info_index = fire_info->beam_info_index;
810 new_item->objp = fire_info->shooter;
812 new_item->subsys = NULL;
813 new_item->life_left = 0;
814 new_item->life_total = 0;
815 new_item->r_collision_count = 0;
816 new_item->f_collision_count = 0;
817 new_item->target = NULL;
818 new_item->target_subsys = NULL;
819 new_item->target_sig = 0;
820 new_item->beam_sound_loop = -1;
821 new_item->type = BEAM_TYPE_C;
822 new_item->targeting_laser_offset = fire_info->targeting_laser_offset;
823 new_item->framecount = 0;
825 new_item->shot_index = 0;
826 new_item->team = (char)firing_ship->team;
828 // type c is a very special weapon type - binfo has no meaning
830 // create the associated object
831 objnum = obj_create(OBJ_BEAM, -1, new_item - Beams, &vmd_identity_matrix, &vmd_zero_vector, 1.0f, OF_COLLIDES);
834 beam_delete(new_item);
835 nprintf(("General", "obj_create() failed for beam weapon! bah!\n"));
838 new_item->objnum = objnum;
840 // this sets up all info for the first frame the beam fires
841 beam_aim(new_item); // to fill in shot_point, etc.
846 // return an object index of the guy who's firing this beam
847 int beam_get_parent(object *bm)
851 // get a handle to the beam
852 SDL_assert(bm->type == OBJ_BEAM);
853 SDL_assert(bm->instance >= 0);
854 if(bm->type != OBJ_BEAM){
857 if(bm->instance < 0){
860 b = &Beams[bm->instance];
862 SDL_assert(b->objp != NULL);
867 // if the object handle is invalid
868 if(b->objp->signature != b->sig){
873 return OBJ_INDEX(b->objp);
876 // return weapon_info_index of beam
877 int beam_get_weapon_info_index(object *bm)
879 SDL_assert(bm->type == OBJ_BEAM);
880 if (bm->type != OBJ_BEAM) {
884 SDL_assert(bm->instance >= 0 && bm->instance < MAX_BEAMS);
885 if (bm->instance < 0) {
889 // return weapon_info_index
890 return Beams[bm->instance].weapon_info_index;
895 // given a beam object, get the # of collisions which happened during the last collision check (typically, last frame)
896 int beam_get_num_collisions(int objnum)
899 if((objnum < 0) || (objnum >= MAX_OBJECTS)){
903 if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
907 if((Beams[Objects[objnum].instance].objnum != objnum) || (Beams[Objects[objnum].instance].objnum < 0)){
912 // return the # of recent collisions
913 return Beams[Objects[objnum].instance].r_collision_count;
916 // stuff collision info, returns 1 on success
917 int beam_get_collision(int objnum, int num, int *collision_objnum, mc_info **cinfo)
920 if((objnum < 0) || (objnum >= MAX_OBJECTS)){
924 if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
928 if((Beams[Objects[objnum].instance].objnum != objnum) || (Beams[Objects[objnum].instance].objnum < 0)){
932 if(num >= Beams[Objects[objnum].instance].r_collision_count){
938 *cinfo = &Beams[Objects[objnum].instance].r_collisions[num].cinfo;
939 *collision_objnum = Beams[Objects[objnum].instance].r_collisions[num].c_objnum;
943 // pause all looping beam sounds
944 void beam_pause_sounds()
948 // set all beam volumes to 0
949 moveup = GET_FIRST(&Beam_used_list);
953 while(moveup != END_OF_LIST(&Beam_used_list)){
954 // set the volume to 0, if he has a looping beam sound
955 if(moveup->beam_sound_loop >= 0){
956 snd_set_volume(moveup->beam_sound_loop, 0.0f);
960 moveup = GET_NEXT(moveup);
964 // unpause looping beam sounds
965 void beam_unpause_sounds()
969 // recalc all beam sounds
970 moveup = GET_FIRST(&Beam_used_list);
974 while(moveup != END_OF_LIST(&Beam_used_list)){
975 beam_recalc_sounds(moveup);
978 moveup = GET_NEXT(moveup);
983 // -----------------------------===========================------------------------------
984 // BEAM MOVEMENT FUNCTIONS
985 // -----------------------------===========================------------------------------
987 // move a type A beam weapon
988 void beam_type_a_move(beam *b)
993 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
994 // get the "originating point" of the beam for this frame. essentially bashes last_start
995 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
997 // if the "warming up" timestamp has not expired
998 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1002 // put the "last_shot" point arbitrarily far away
1003 vm_vec_sub(&dir, &b->last_shot, &b->last_start);
1004 vm_vec_normalize_quick(&dir);
1005 vm_vec_scale_add(&b->last_shot, &b->last_start, &dir, BEAM_FAR_LENGTH);
1006 SDL_assert(is_valid_vec(&b->last_shot));
1009 // move a type B beam weapon
1010 #define BEAM_T(b) ( ((b->binfo.delta_ang / b->life_total) * (b->life_total - b->life_left)) / b->binfo.delta_ang )
1011 void beam_type_b_move(beam *b)
1017 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1018 // get the "originating point" of the beam for this frame. essentially bashes last_start
1019 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
1021 // if the "warming up" timestamp has not expired
1022 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1026 // if the two direction vectors are _really_ close together, just use the original direction
1027 dot_save = vm_vec_dot(&b->binfo.dir_a, &b->binfo.dir_b);
1028 if((double)dot_save >= 0.999999999){
1029 actual_dir = b->binfo.dir_a;
1031 // otherwise move towards the dir we calculated when firing this beam
1033 vm_vec_interp_constant(&actual_dir, &b->binfo.dir_a, &b->binfo.dir_b, BEAM_T(b));
1036 // now recalculate shot_point to be shooting through our new point
1037 vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, BEAM_FAR_LENGTH);
1038 int is_valid = is_valid_vec(&b->last_shot);
1039 SDL_assert(is_valid);
1041 actual_dir = b->binfo.dir_a;
1042 vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, BEAM_FAR_LENGTH);
1047 void beam_type_c_move(beam *b)
1052 if(b->objp == NULL){
1057 // type c beams only last one frame so we never have to "move" them.
1058 temp = b->targeting_laser_offset;
1059 vm_vec_unrotate(&b->last_start, &temp, &b->objp->orient);
1060 vm_vec_add2(&b->last_start, &b->objp->pos);
1061 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->objp->orient.v.fvec, BEAM_FAR_LENGTH);
1065 void beam_type_d_move(beam *b)
1067 int shot_index, fire_wait;
1068 vector temp, temp2, dir;
1070 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1071 // get the "originating point" of the beam for this frame. essentially bashes last_start
1072 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
1074 // if the "warming up" timestamp has not expired
1075 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1079 // determine what stage of the beam we're in
1080 beam_type_d_get_status(b, &shot_index, &fire_wait);
1082 // if we've changed shot index
1083 if(shot_index != b->shot_index){
1084 // set the new index
1085 b->shot_index = shot_index;
1091 // if we're in the fire wait stage
1092 b->flags &= ~BF_SAFETY;
1094 b->flags |= BF_SAFETY;
1097 // put the "last_shot" point arbitrarily far away
1098 vm_vec_sub(&dir, &b->last_shot, &b->last_start);
1099 vm_vec_normalize_quick(&dir);
1100 vm_vec_scale_add(&b->last_shot, &b->last_start, &dir, BEAM_FAR_LENGTH);
1101 SDL_assert(is_valid_vec(&b->last_shot));
1103 void beam_type_d_get_status(beam *b, int *shot_index, int *fire_wait)
1105 float shot_time = b->life_total / (float)b->binfo.shot_count;
1106 float beam_time = b->life_total - b->life_left;
1108 // determine what "shot" we're on
1109 *shot_index = (int)(beam_time / shot_time);
1110 SDL_assert(*shot_index < b->binfo.shot_count);
1111 if(*shot_index >= b->binfo.shot_count){
1112 *shot_index = b->binfo.shot_count - 1;
1115 // determine if its the firing or waiting section of the shot (fire happens first, THEN wait)
1117 if(beam_time > ((shot_time * (*shot_index)) + (shot_time * 0.5f))){
1123 void beam_type_e_move(beam *b)
1125 vector temp, turret_norm;
1127 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1128 // get the "originating point" of the beam for this frame. essentially bashes last_start
1129 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &turret_norm, 1, &temp);
1131 // if the "warming up" timestamp has not expired
1132 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1136 // put the "last_shot" point arbitrarily far away
1137 vm_vec_scale_add(&b->last_shot, &b->last_start, &turret_norm, BEAM_FAR_LENGTH);
1138 SDL_assert(is_valid_vec(&b->last_shot));
1141 // pre-move (before collision checking - but AFTER ALL OTHER OBJECTS HAVE BEEN MOVED)
1142 void beam_move_all_pre()
1147 // zero lights for this frame yet
1148 Beam_light_count = 0;
1150 // traverse through all active beams
1151 moveup = GET_FIRST(&Beam_used_list);
1152 while(moveup != END_OF_LIST(&Beam_used_list)){
1156 // unset collision info
1157 b->f_collision_count = 0;
1159 if(!physics_paused){
1162 // type A beam weapons don't move
1164 beam_type_a_move(b);
1167 // type B beam weapons move across the target somewhat randomly
1169 beam_type_b_move(b);
1172 // type C beam weapons are attached to a fighter - pointing forward
1174 beam_type_c_move(b);
1179 beam_type_d_move(b);
1184 beam_type_e_move(b);
1187 // illegal beam type
1194 moveup = GET_NEXT(moveup);
1198 // post-collision time processing for beams
1199 void beam_move_all_post()
1204 beam_weapon_info *bwi;
1206 // traverse through all active beams
1207 moveup = GET_FIRST(&Beam_used_list);
1208 while(moveup != END_OF_LIST(&Beam_used_list)){
1209 bwi = &Weapon_info[moveup->weapon_info_index].b_info;
1211 // check the status of the beam
1212 bf_status = beam_ok_to_fire(moveup);
1214 // if we're warming up
1215 if(moveup->warmup_stamp != -1){
1216 next_one = GET_NEXT(moveup);
1218 // should we be stopping?
1220 beam_delete(moveup);
1222 // if the warming up timestamp has expired, start firing
1223 if(timestamp_elapsed(moveup->warmup_stamp)){
1225 if(!beam_start_firing(moveup)){
1226 beam_delete(moveup);
1229 // add a muzzle light for the shooter
1230 beam_add_light(moveup, OBJ_INDEX(moveup->objp), 0, NULL);
1238 // if we're warming down
1239 else if(moveup->warmdown_stamp != -1){
1240 next_one = GET_NEXT(moveup);
1242 // should we be stopping?
1244 beam_delete(moveup);
1246 // if we're done warming down, the beam is finished
1247 if(timestamp_elapsed(moveup->warmdown_stamp)){
1248 beam_delete(moveup);
1257 // otherwise, we're firing away.........
1259 // add a muzzle light for the shooter
1260 beam_add_light(moveup, OBJ_INDEX(moveup->objp), 0, NULL);
1262 // subtract out the life left for the beam
1263 if(!physics_paused){
1264 moveup->life_left -= flFrametime;
1267 // if we're past the shrink point, start shrinking the beam
1268 if(moveup->life_left <= (moveup->life_total * bwi->beam_shrink_factor)){
1269 moveup->flags |= BF_SHRINK;
1272 // if we're shrinking the beam
1273 if(moveup->flags & BF_SHRINK){
1274 moveup->shrink -= bwi->beam_shrink_pct * flFrametime;
1275 if(moveup->shrink < 0.1f){
1276 moveup->shrink = 0.1f;
1282 next_one = GET_NEXT(moveup);
1284 // if beam should abruptly stop
1285 if(bf_status == -1){
1286 beam_delete(moveup);
1288 // if the beam should just power down
1290 beam_start_warmdown(moveup);
1298 // increment framecount
1299 moveup->framecount++;
1301 // type c weapons live for one frame only
1302 if(moveup->type == BEAM_TYPE_C){
1303 if(moveup->framecount > 1){
1304 next_one = GET_NEXT(moveup);
1306 beam_delete(moveup);
1311 // done firing, so go into the warmdown phase
1313 if((moveup->life_left <= 0.0f) && (moveup->warmdown_stamp == -1)){
1314 beam_start_warmdown(moveup);
1316 moveup = GET_NEXT(moveup);
1321 // handle any collisions which occured collision (will take care of applying damage to all objects which got hit)
1322 beam_handle_collisions(moveup);
1324 // recalculate beam sounds
1325 beam_recalc_sounds(moveup);
1328 moveup = GET_NEXT(moveup);
1331 // apply all beam lighting
1332 beam_apply_lighting();
1334 // process beam culling info
1337 if(Beam_test_stamp == -1){
1338 Beam_test_stamp = timestamp(BEAM_TEST_STAMP_TIME);
1340 Beam_test_framecount = 0;
1342 if(timestamp_elapsed(Beam_test_stamp)){
1343 // report the results
1344 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));
1347 Beam_test_stamp = timestamp(BEAM_TEST_STAMP_TIME);
1351 Beam_test_framecount = 0;
1353 Beam_test_framecount++;
1360 // -----------------------------===========================------------------------------
1361 // BEAM RENDERING FUNCTIONS
1362 // -----------------------------===========================------------------------------
1364 // render a beam weapon
1365 #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);
1366 #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);
1367 #define P_VERTICES() do { for(idx=0; idx<4; idx++){ g3_project_vertex(verts[idx]); } } while(0);
1369 void beam_render(beam_weapon_info *bwi, vector *start, vector *shot, float shrink)
1372 vertex h1[4]; // halves of a beam section
1373 vertex *verts[4] = { &h1[0], &h1[1], &h1[2], &h1[3] };
1374 vector fvec, top1, bottom1, top2, bottom2;
1377 // bogus weapon info index
1382 // if the beam start and endpoints are the same
1383 if(vm_vec_same(start, shot)){
1387 // get beam direction
1388 vm_vec_sub(&fvec, shot, start);
1389 vm_vec_normalize_quick(&fvec);
1391 // turn off backface culling
1394 // draw all sections
1395 for(s_idx=0; s_idx<bwi->beam_num_sections; s_idx++){
1396 // calculate the beam points
1397 scale = frand_range(1.0f - bwi->sections[s_idx].flicker, 1.0f + bwi->sections[s_idx].flicker);
1398 beam_calc_facing_pts(&top1, &bottom1, &fvec, start, bwi->sections[s_idx].width * scale * shrink, bwi->sections[s_idx].z_add);
1399 beam_calc_facing_pts(&top2, &bottom2, &fvec, shot, bwi->sections[s_idx].width * scale * scale * shrink, bwi->sections[s_idx].z_add);
1400 R_VERTICES(); // rotate and project the vertices
1402 STUFF_VERTICES(); // stuff the beam with creamy goodness (texture coords)
1404 // set the right texture with additive alpha, and draw the poly
1405 gr_set_bitmap(bwi->sections[s_idx].texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f, -1, -1);
1406 g3_draw_poly( 4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1409 // turn backface culling back on
1413 // generate particles for the muzzle glow
1414 int hack_time = 100;
1417 dc_get_arg(ARG_INT);
1418 hack_time = Dc_arg_int;
1420 void beam_generate_muzzle_particles(beam *b)
1425 vector turret_norm, turret_pos, particle_pos, particle_dir, p_temp;
1427 particle_info pinfo;
1429 // if our hack stamp has expired
1430 if(!((Beam_muzzle_stamp == -1) || timestamp_elapsed(Beam_muzzle_stamp))){
1434 // never generate anything past about 1/5 of the beam fire time
1435 if(b->warmup_stamp == -1){
1440 wip = &Weapon_info[b->weapon_info_index];
1442 // no specified particle for this beam weapon
1443 if(wip->b_info.beam_particle_ani < 0){
1447 // reset the hack stamp
1448 Beam_muzzle_stamp = timestamp(hack_time);
1450 // randomly generate 10 to 20 particles
1451 particle_count = (int)frand_range(0.0f, (float)wip->b_info.beam_particle_count);
1453 // get turret info - position and normal
1454 turret_pos = b->subsys->system_info->pnt;
1455 turret_norm = b->subsys->system_info->turret_norm;
1457 // randomly perturb a vector within a cone around the normal
1458 vm_vector_2_matrix(&m, &turret_norm, NULL, NULL);
1459 for(idx=0; idx<particle_count; idx++){
1460 // get a random point in the cone
1461 vm_vec_random_cone(&particle_dir, &turret_norm, wip->b_info.beam_particle_angle, &m);
1462 p_temp = turret_pos;
1463 vm_vec_scale_add(&p_temp, &turret_pos, &particle_dir, wip->b_info.beam_muzzle_radius * frand_range(0.75f, 0.9f));
1465 // transform into world coords
1466 vm_vec_unrotate(&particle_pos, &p_temp, &b->objp->orient);
1467 vm_vec_add2(&particle_pos, &b->objp->pos);
1468 p_temp = particle_dir;
1469 vm_vec_unrotate(&particle_dir, &p_temp, &b->objp->orient);
1471 // now generate some interesting values for the particle
1472 float p_time_ref = wip->b_info.beam_life + ((float)wip->b_info.beam_warmup / 1000.0f);
1473 float p_life = frand_range(p_time_ref * 0.5f, p_time_ref * 0.7f);
1474 float p_vel = (wip->b_info.beam_muzzle_radius / p_life) * frand_range(0.85f, 1.2f);
1475 vm_vec_scale(&particle_dir, -p_vel);
1477 memset(&pinfo, 0, sizeof(particle_info));
1478 pinfo.pos = particle_pos;
1479 pinfo.vel = particle_dir;
1480 pinfo.lifetime = p_life;
1481 pinfo.attached_objnum = -1;
1482 pinfo.attached_sig = 0;
1483 pinfo.rad = wip->b_info.beam_particle_radius;
1485 pinfo.type = PARTICLE_BITMAP;
1486 pinfo.optional_data = wip->b_info.beam_particle_ani;
1487 pinfo.tracer_length = -1.0f;
1488 particle_create(&pinfo);
1492 // render the muzzle glow for a beam weapon
1493 void beam_render_muzzle_glow(beam *b)
1496 weapon_info *wip = &Weapon_info[b->weapon_info_index];
1497 beam_weapon_info *bwi = &Weapon_info[b->weapon_info_index].b_info;
1498 float pct, rand_val;
1500 // if we don't have a glow bitmap
1501 if(bwi->beam_glow_bitmap < 0){
1505 // if the beam is warming up, scale the glow
1506 if(b->warmup_stamp != -1){
1508 pct = BEAM_WARMUP_PCT(b);
1511 // if the beam is warming down
1512 if(b->warmdown_stamp != -1){
1514 pct = 1.0f - BEAM_WARMDOWN_PCT(b);
1517 // otherwise the beam is really firing
1520 rand_val = frand_range(0.90f, 1.0f);
1524 g3_rotate_vertex(&v, &b->last_start);
1525 gr_set_bitmap( bwi->beam_glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.8f * pct, -1, -1);
1527 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * rand_val, TMAP_FLAG_TEXTURED);
1531 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.75f * rand_val, TMAP_FLAG_TEXTURED);
1534 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.45f * rand_val, TMAP_FLAG_TEXTURED);
1537 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.25f * rand_val, TMAP_FLAG_TEXTURED);
1541 // render all beam weapons
1542 void beam_render_all()
1546 // traverse through all active beams
1547 moveup = GET_FIRST(&Beam_used_list);
1548 while(moveup != END_OF_LIST(&Beam_used_list)){
1549 // each beam type renders a little bit differently
1550 if((moveup->warmup_stamp == -1) && (moveup->warmdown_stamp == -1) && !(moveup->flags & BF_SAFETY)){
1551 // HACK - if this is the first frame the beam is firing, don't render it
1552 if(moveup->life_left >= moveup->life_total - 0.0001f){
1554 moveup = GET_NEXT(moveup);
1558 // render the beam itself
1559 SDL_assert(moveup->weapon_info_index >= 0);
1560 if(moveup->weapon_info_index < 0){
1561 moveup = GET_NEXT(moveup);
1564 beam_render(&Weapon_info[moveup->weapon_info_index].b_info, &moveup->last_start, &moveup->last_shot, moveup->shrink);
1567 // render the muzzle glow
1568 beam_render_muzzle_glow(moveup);
1570 // maybe generate some muzzle particles
1571 beam_generate_muzzle_particles(moveup);
1574 moveup = GET_NEXT(moveup);
1578 // output top and bottom vectors
1579 // fvec == forward vector (eye viewpoint basically. in world coords)
1580 // pos == world coordinate of the point we're calculating "around"
1581 // w == width of the diff between top and bottom around pos
1582 void beam_calc_facing_pts( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1589 vm_vec_sub( &rvec, &Eye_position, &temp );
1590 vm_vec_normalize( &rvec );
1592 vm_vec_crossprod(&uvec,fvec,&rvec);
1593 vm_vec_normalize(&uvec);
1595 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1596 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1599 // light scale factor
1600 float blight = 25.5f;
1603 dc_get_arg(ARG_FLOAT);
1604 blight = Dc_arg_float;
1607 // call to add a light source to a small object
1608 void beam_add_light_small(beam *bm, object *objp, vector *pt_override = NULL)
1611 beam_weapon_info *bwi;
1620 SDL_assert(bm != NULL);
1624 SDL_assert(objp != NULL);
1628 SDL_assert(bm->weapon_info_index >= 0);
1629 wip = &Weapon_info[bm->weapon_info_index];
1633 noise = frand_range(1.0f - bwi->sections[0].flicker, 1.0f + bwi->sections[0].flicker);
1635 // widest part of the beam
1636 float light_rad = beam_get_widest(bm) * blight * noise;
1638 // nearest point on the beam, and its distance to the ship
1640 if(pt_override == NULL){
1642 vm_vec_dist_to_line(&objp->pos, &bm->last_start, &bm->last_shot, &near_pt, &dist);
1643 if(dist > light_rad){
1647 near_pt = *pt_override;
1650 // average rgb of the beam
1651 float fr = (float)wip->laser_color_1.red / 255.0f;
1652 float fg = (float)wip->laser_color_1.green / 255.0f;
1653 float fb = (float)wip->laser_color_1.blue / 255.0f;
1655 // add a unique light
1656 // noise *= 0.1f; // a little less noise here, since we want the beam to generally cast a bright light
1657 light_add_point_unique(&near_pt, light_rad * 0.0001f, light_rad, 1.0f, fr, fg, fb, OBJ_INDEX(objp));
1660 // call to add a light source to a large object
1661 void beam_add_light_large(beam *bm, object *objp, vector *pt0, vector *pt1)
1664 beam_weapon_info *bwi;
1673 SDL_assert(bm != NULL);
1677 SDL_assert(objp != NULL);
1681 SDL_assert(bm->weapon_info_index >= 0);
1682 wip = &Weapon_info[bm->weapon_info_index];
1686 noise = frand_range(1.0f - bwi->sections[0].flicker, 1.0f + bwi->sections[0].flicker);
1688 // widest part of the beam
1689 float light_rad = beam_get_widest(bm) * blight * noise;
1691 // average rgb of the beam
1692 float fr = (float)wip->laser_color_1.red / 255.0f;
1693 float fg = (float)wip->laser_color_1.green / 255.0f;
1694 float fb = (float)wip->laser_color_1.blue / 255.0f;
1696 // add a unique light
1697 noise *= 0.1f; // a little less noise here, since we want the beam to generally cast a bright light
1698 light_add_tube(pt0, pt1, 1.0f, light_rad, 1.0f * noise, fr, fg, fb, OBJ_INDEX(objp));
1701 // mark an object as being lit
1702 void beam_add_light(beam *b, int objnum, int source, vector *c_point)
1706 // if we're out of light slots!
1707 if(Beam_light_count >= MAX_BEAM_LIGHT_INFO){
1713 l = &Beam_lights[Beam_light_count++];
1716 l->source = (ubyte)source;
1718 // only type 2 lights (from collisions) need a collision point
1719 if(c_point != NULL){
1720 l->c_point = *c_point;
1722 SDL_assert(source != 2);
1729 // apply lighting from any beams
1730 void beam_apply_lighting()
1735 beam_weapon_info *bwi;
1737 // convert all beam lights into real lights
1738 for(idx=0; idx<Beam_light_count; idx++){
1740 l = &Beam_lights[idx];
1743 if((l->objnum < 0) || (l->objnum >= MAX_OBJECTS) || (l->bm == NULL)){
1747 bwi = &Weapon_info[l->bm->weapon_info_index].b_info;
1749 // different light types
1751 // from the muzzle of the gun
1753 // a few meters in from the of muzzle
1754 vm_vec_sub(&dir, &l->bm->last_start, &l->bm->last_shot);
1755 vm_vec_normalize_quick(&dir);
1756 vm_vec_scale_add(&pt, &l->bm->last_start, &dir, bwi->beam_muzzle_radius * 5.0f);
1758 beam_add_light_small(l->bm, &Objects[l->objnum], &pt);
1761 // from the beam passing by
1764 switch(Objects[l->objnum].type){
1766 SDL_assert(Objects[l->objnum].instance >= 0);
1769 if(Ship_info[Ships[Objects[l->objnum].instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)){
1770 beam_add_light_large(l->bm, &Objects[l->objnum], &l->bm->last_start, &l->bm->last_shot);
1774 beam_add_light_small(l->bm, &Objects[l->objnum]);
1778 // asteroids get small lights
1780 beam_add_light_small(l->bm, &Objects[l->objnum]);
1783 // debris gets small lights
1785 beam_add_light_small(l->bm, &Objects[l->objnum]);
1792 // a few meters from the collision point
1793 vm_vec_sub(&dir, &l->bm->last_start, &l->c_point);
1794 vm_vec_normalize_quick(&dir);
1795 vm_vec_scale_add(&pt, &l->c_point, &dir, bwi->beam_muzzle_radius * 5.0f);
1797 beam_add_light_small(l->bm, &Objects[l->objnum], &pt);
1803 // -----------------------------===========================------------------------------
1804 // BEAM BOOKKEEPING FUNCTIONS
1805 // -----------------------------===========================------------------------------
1808 void beam_delete(beam *b)
1810 // remove from active list and put on free list
1811 list_remove(&Beam_used_list, b);
1812 list_append(&Beam_free_list, b);
1814 // delete our associated object
1815 // SDL_assert(b->objnum >= 0);
1817 obj_delete(b->objnum);
1821 // kill the beam looping sound
1822 if(b->beam_sound_loop != -1){
1823 snd_stop(b->beam_sound_loop);
1824 b->beam_sound_loop = -1;
1829 SDL_assert(Beam_count >= 0);
1830 nprintf(("General", "Recycled beam (%d beams remaining)\n", Beam_count));
1833 // given an object, return its model num
1834 int beam_get_model(object *objp)
1837 SDL_assert(objp->instance >= 0);
1838 if(objp->instance < 0){
1844 return Ships[objp->instance].modelnum;
1847 SDL_assert(Weapons[objp->instance].weapon_info_index >= 0);
1848 if(Weapons[objp->instance].weapon_info_index < 0){
1851 return Weapon_info[Weapons[objp->instance].weapon_info_index].model_num;
1854 SDL_assert(Debris[objp->instance].is_hull);
1855 if(!Debris[objp->instance].is_hull){
1858 return Debris[objp->instance].model_num;
1860 #if !(defined(FS2_DEMO) || defined(FS1_DEMO))
1862 subtype = Asteroids[objp->instance].asteroid_subtype;
1863 SDL_assert(Asteroids[objp->instance].type >= 0);
1864 if(Asteroids[objp->instance].type < 0){
1867 return Asteroid_info[Asteroids[objp->instance].type].model_num[subtype];
1871 // this shouldn't happen too often
1872 mprintf(("Beam couldn't find a good find a good object model/type!! (%d)", objp->type));
1877 // start the warmup phase for the beam
1878 void beam_start_warmup(beam *b)
1880 // set the warmup stamp
1881 b->warmup_stamp = timestamp(Weapon_info[b->weapon_info_index].b_info.beam_warmup);
1883 // start playing warmup sound
1884 if(!(Game_mode & GM_STANDALONE_SERVER) && (Weapon_info[b->weapon_info_index].b_info.beam_warmup_sound >= 0)){
1885 snd_play_3d(&Snds[Weapon_info[b->weapon_info_index].b_info.beam_warmup_sound], &b->last_start, &View_position);
1889 // start the firing phase for the beam, return 0 if the beam failed to start, and should be deleted altogether
1890 int beam_start_firing(beam *b)
1892 // kill the warmup stamp so the rest of the code knows its firing
1893 b->warmup_stamp = -1;
1895 // any special stuff for each weapon type
1897 // re-aim type A and D beam weapons here, otherwise they tend to miss
1916 // determine if we can legitimately start firing, or if we need to take other action
1917 switch(beam_ok_to_fire(b)){
1922 beam_start_warmdown(b);
1926 // start the beam firing sound now, if we haven't already
1927 if((b->beam_sound_loop == -1) && (Weapon_info[b->weapon_info_index].b_info.beam_loop_sound >= 0)){
1928 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);
1931 snd_play_3d(&Snds[SND_BEAM_SHOT], &b->last_start, &View_position);
1938 // start the warmdown phase for the beam
1939 void beam_start_warmdown(beam *b)
1942 b->warmdown_stamp = timestamp(Weapon_info[b->weapon_info_index].b_info.beam_warmdown);
1944 // start the warmdown sound
1945 if(Weapon_info[b->weapon_info_index].b_info.beam_warmdown_sound >= 0){
1946 snd_play_3d(&Snds[Weapon_info[b->weapon_info_index].b_info.beam_warmdown_sound], &b->last_start, &View_position);
1949 // kill the beam looping sound
1950 if(b->beam_sound_loop != -1){
1951 snd_stop(b->beam_sound_loop);
1952 b->beam_sound_loop = -1;
1956 // recalculate beam sounds (looping sounds relative to the player)
1957 void beam_recalc_sounds(beam *b)
1959 beam_weapon_info *bwi;
1962 SDL_assert(b->weapon_info_index >= 0);
1963 if(b->weapon_info_index < 0){
1966 bwi = &Weapon_info[b->weapon_info_index].b_info;
1968 // update the sound position relative to the player
1969 if(b->beam_sound_loop != -1){
1970 // get the point closest to the player's viewing position
1971 switch(vm_vec_dist_to_line(&View_position, &b->last_start, &b->last_shot, &pos, NULL)){
1972 // behind the beam, so use the start pos
1974 pos = b->last_start;
1977 // use the closest point
1979 // already calculated in vm_vec_dist_to_line(...)
1982 // past the beam, so use the shot pos
1988 snd_update_3d_pos(b->beam_sound_loop, &Snds[bwi->beam_loop_sound], &pos);
1993 // -----------------------------===========================------------------------------
1994 // BEAM AIMING FUNCTIONS
1995 // -----------------------------===========================------------------------------
1998 void beam_get_binfo(beam *b, float accuracy, int num_shots)
2003 vector turret_point, turret_norm;
2004 beam_weapon_info *bwi;
2007 // where the shot is originating from (b->last_start gets filled in)
2008 ship_get_global_turret_gun_info(b->objp, b->subsys, &turret_point, &turret_norm, 1, &p2);
2010 // get a model # to work with
2011 model_num = beam_get_model(b->target);
2016 // get beam weapon info
2017 SDL_assert(b->weapon_info_index >= 0);
2018 if(b->weapon_info_index < 0){
2021 bwi = &Weapon_info[b->weapon_info_index].b_info;
2024 skill_level = Game_skill_level;
2025 if(Game_skill_level >= NUM_SKILL_LEVELS){
2026 skill_level = NUM_SKILL_LEVELS - 1;
2028 if(Game_skill_level < 0){
2032 // stuff num shots even though its only used for type D weapons
2033 b->binfo.shot_count = (ubyte)num_shots;
2034 if(b->binfo.shot_count > MAX_BEAM_SHOTS){
2035 b->binfo.shot_count = MAX_BEAM_SHOTS;
2038 // generate the proper amount of directional vectors
2040 // pick an accuracy. beam will be properly aimed at actual fire time
2042 // all we will do is decide whether or not we will hit - type A beam weapons are re-aimed immediately before firing
2043 b->binfo.shot_aim[0] = frand_range(0.0f, 1.0f + bwi->beam_miss_factor[skill_level] * accuracy);
2044 b->binfo.shot_count = 1;
2046 // get random model points, this is useful for big ships, because we never miss when shooting at them
2047 submodel_get_two_random_points(model_num, 0, &b->binfo.dir_a, &b->binfo.dir_b);
2050 // just 2 points in the "slash"
2052 beam_get_octant_points(model_num, b->target, (int)frand_range(0.0f, BEAM_NUM_GOOD_OCTANTS), Beam_good_slash_octants, &oct1, &oct2);
2055 vm_vec_sub(&b->binfo.dir_a, &oct1, &turret_point);
2056 vm_vec_normalize(&b->binfo.dir_a);
2059 vm_vec_sub(&b->binfo.dir_b, &oct2, &turret_point);
2060 vm_vec_normalize(&b->binfo.dir_b);
2063 b->binfo.delta_ang = fl_abs(vm_vec_delta_ang_norm(&b->binfo.dir_a, &b->binfo.dir_b, NULL));
2066 // nothing for this beam - its very special case
2070 // type D beams fire at small ship multiple times
2072 // get a bunch of shot aims
2073 for(idx=0; idx<b->binfo.shot_count; idx++){
2074 // MK, 9/3/99: Added pow() function to make increasingly likely to miss with subsequent shots. 30% more likely with each shot.
2075 float r = ((float) pow(1.3f, idx)) * bwi->beam_miss_factor[skill_level] * accuracy;
2076 b->binfo.shot_aim[idx] = frand_range(0.0f, 1.0f + r);
2080 // type e beams just fire straight
2082 b->binfo.shot_aim[0] = 0.0000001f;
2083 b->binfo.shot_count = 1;
2084 b->binfo.dir_a = turret_norm;
2085 b->binfo.dir_b = turret_norm;
2093 // aim the beam (setup last_start and last_shot - the endpoints). also recalculates collision pairs
2094 void beam_aim(beam *b)
2099 // type C beam weapons have no target
2100 if(b->target == NULL){
2101 SDL_assert(b->type == BEAM_TYPE_C);
2102 if(b->type != BEAM_TYPE_C){
2106 // get a model # to work with
2108 model_num = beam_get_model(b->target);
2114 // setup our initial shot point and aim direction
2117 // where the shot is originating from (b->last_start gets filled in)
2118 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2120 // if we're targeting a subsystem - shoot directly at it
2121 if(b->target_subsys != NULL){
2122 // unrotate the center of the subsystem
2123 vm_vec_unrotate(&b->last_shot, &b->target_subsys->system_info->pnt, &b->target->orient);
2124 vm_vec_add2(&b->last_shot, &b->target->pos);
2125 vm_vec_sub(&temp, &b->last_shot, &b->last_start);
2127 vm_vec_scale_add(&b->last_shot, &b->last_start, &temp, 2.0f);
2131 // if we're shooting at a big ship - shoot directly at the model
2132 if((b->target->type == OBJ_SHIP) && (Ship_info[Ships[b->target->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP))){
2133 // rotate into world coords
2134 vm_vec_unrotate(&temp, &b->binfo.dir_a, &b->target->orient);
2135 vm_vec_add2(&temp, &b->target->pos);
2137 // get the shot point
2138 vm_vec_sub(&p2, &temp, &b->last_start);
2139 vm_vec_scale_add(&b->last_shot, &b->last_start, &p2, 2.0f);
2143 // point at the center of the target, then jitter based on shot_aim
2144 b->last_shot = b->target->pos;
2145 beam_jitter_aim(b, b->binfo.shot_aim[0]);
2149 // where the shot is originating from (b->last_start gets filled in)
2150 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2152 // set the shot point
2153 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->binfo.dir_a, BEAM_FAR_LENGTH);
2154 SDL_assert(is_valid_vec(&b->last_shot));
2159 temp = b->targeting_laser_offset;
2160 vm_vec_unrotate(&b->last_start, &temp, &b->objp->orient);
2161 vm_vec_add2(&b->last_start, &b->objp->pos);
2162 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->objp->orient.v.fvec, BEAM_FAR_LENGTH);
2166 // where the shot is originating from (b->last_start gets filled in)
2167 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2169 // point at the center of the target, then jitter based on shot_aim
2170 b->last_shot = b->target->pos;
2171 beam_jitter_aim(b, b->binfo.shot_aim[b->shot_index]);
2172 nprintf(("AI", "Frame %i: FIRING\n", Framecount));
2176 // where the shot is originating from (b->last_start gets filled in)
2177 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2179 // point directly in the direction of the turret
2180 vm_vec_scale_add(&b->last_shot, &b->last_start, &temp, BEAM_FAR_LENGTH);
2187 // recalculate object pairs
2188 OBJ_RECALC_PAIRS((&Objects[b->objnum]));
2191 // given a model #, and an object, stuff 2 good world coord points
2192 void beam_get_octant_points(int modelnum, object *objp, int oct_index, int oct_array[BEAM_NUM_GOOD_OCTANTS][4], vector *v1, vector *v2)
2194 vector t1, t2, temp;
2195 polymodel *m = model_get(modelnum);
2197 // bad bad bad bad bad bad
2203 SDL_assert((oct_index >= 0) && (oct_index < BEAM_NUM_GOOD_OCTANTS));
2205 // randomly pick octants
2206 t1 = oct_array[oct_index][2] ? m->octants[oct_array[oct_index][0]].max : m->octants[oct_array[oct_index][0]].min;
2207 t2 = oct_array[oct_index][3] ? m->octants[oct_array[oct_index][1]].max : m->octants[oct_array[oct_index][1]].min;
2208 SDL_assert(!vm_vec_same(&t1, &t2));
2210 // get them in world coords
2211 vm_vec_unrotate(&temp, &t1, &objp->orient);
2212 vm_vec_add(v1, &temp, &objp->pos);
2213 vm_vec_unrotate(&temp, &t2, &objp->orient);
2214 vm_vec_add(v2, &temp, &objp->pos);
2217 // throw some jitter into the aim - based upon shot_aim
2218 void beam_jitter_aim(beam *b, float aim)
2220 vector forward, circle;
2222 float subsys_strength;
2224 // if the weapons subsystem is damaged or destroyed
2225 if((b->objp != NULL) && (b->objp->signature == b->sig) && (b->objp->type == OBJ_SHIP) && (b->objp->instance >= 0) && (b->objp->instance < MAX_SHIPS)){
2226 // get subsytem strength
2227 subsys_strength = ship_get_subsystem_strength(&Ships[b->objp->instance], SUBSYSTEM_WEAPONS);
2229 // when subsytem strength is 0, double the aim error factor
2230 aim += aim * (1.0f - subsys_strength);
2233 // shot aim is a direct linear factor of the target model's radius.
2234 // so, pick a random point on the circle
2235 vm_vec_sub(&forward, &b->last_shot, &b->last_start);
2236 vm_vec_normalize_quick(&forward);
2239 vm_vector_2_matrix(&m, &forward, NULL, NULL);
2241 // get a vector on the circle - this should appear to be pretty random
2242 // vm_vec_scale_add(&circle, &b->last_shot, &m.rvec, aim * b->target->radius);
2243 vm_vec_random_in_circle(&circle, &b->last_shot, &m, aim * b->target->radius, 0);
2245 // get the vector pointing to the circle point
2246 vm_vec_sub(&forward, &circle, &b->last_start);
2247 vm_vec_scale_add(&b->last_shot, &b->last_start, &forward, 2.0f);
2251 // -----------------------------===========================------------------------------
2252 // BEAM COLLISION FUNCTIONS
2253 // -----------------------------===========================------------------------------
2255 // collide a beam with a ship, returns 1 if we can ignore all future collisions between the 2 objects
2256 int beam_collide_ship(obj_pair *pair)
2259 mc_info test_collide;
2269 SDL_assert(pair->a->instance >= 0);
2270 SDL_assert(pair->a->type == OBJ_BEAM);
2271 SDL_assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2272 b = &Beams[pair->a->instance];
2274 // Don't check collisions for warping out player if past stage 1.
2275 if ( Player->control_mode >= PCM_WARPOUT_STAGE1) {
2276 if ( pair->a == Player_obj ) return 0;
2277 if ( pair->b == Player_obj ) return 0;
2280 // if the "warming up" timestamp has not expired
2281 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2285 // if the beam is on "safety", don't collide with anything
2286 if(b->flags & BF_SAFETY){
2290 // if the colliding object is the shooting object, return 1 so this is culled
2291 if(pair->b == b->objp){
2295 // try and get a model
2296 model_num = beam_get_model(pair->b);
2308 SDL_assert(pair->b->type == OBJ_SHIP);
2309 SDL_assert(pair->b->instance >= 0);
2310 if((pair->b->type != OBJ_SHIP) || (pair->b->instance < 0)){
2314 // get the widest portion of the beam
2315 widest = beam_get_widest(b);
2318 test_collide.model_num = model_num;
2319 test_collide.submodel_num = -1;
2320 test_collide.orient = &pair->b->orient;
2321 test_collide.pos = &pair->b->pos;
2322 test_collide.p0 = &b->last_start;
2323 test_collide.p1 = &b->last_shot;
2325 // maybe do a sphereline
2326 if(widest > pair->b->radius * BEAM_AREA_PERCENT){
2327 test_collide.radius = beam_get_widest(b) * 0.5f;
2328 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_SPHERELINE;
2330 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2332 model_collide(&test_collide);
2335 if(test_collide.num_hits){
2336 // add to the collision list
2337 beam_add_collision(b, pair->b, &test_collide);
2340 // add this guy to the lighting list
2341 beam_add_light(b, OBJ_INDEX(pair->b), 1, NULL);
2343 // reset timestamp to timeout immediately
2344 pair->next_check_time = timestamp(0);
2349 // collide a beam with an asteroid, returns 1 if we can ignore all future collisions between the 2 objects
2350 int beam_collide_asteroid(obj_pair *pair)
2353 mc_info test_collide;
2362 SDL_assert(pair->a->instance >= 0);
2363 SDL_assert(pair->a->type == OBJ_BEAM);
2364 SDL_assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2365 b = &Beams[pair->a->instance];
2367 // if the "warming up" timestamp has not expired
2368 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2372 // if the beam is on "safety", don't collide with anything
2373 if(b->flags & BF_SAFETY){
2377 // if the colliding object is the shooting object, return 1 so this is culled
2378 if(pair->b == b->objp){
2382 // try and get a model
2383 model_num = beam_get_model(pair->b);
2395 test_collide.model_num = model_num;
2396 test_collide.submodel_num = -1;
2397 test_collide.orient = &pair->b->orient;
2398 test_collide.pos = &pair->b->pos;
2399 test_collide.p0 = &b->last_start;
2400 test_collide.p1 = &b->last_shot;
2401 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2402 model_collide(&test_collide);
2405 if(test_collide.num_hits){
2406 // add to the collision list
2407 beam_add_collision(b, pair->b, &test_collide);
2410 // add this guy to the lighting list
2411 beam_add_light(b, OBJ_INDEX(pair->b), 1, NULL);
2413 // reset timestamp to timeout immediately
2414 pair->next_check_time = timestamp(0);
2419 // collide a beam with a missile, returns 1 if we can ignore all future collisions between the 2 objects
2420 int beam_collide_missile(obj_pair *pair)
2423 mc_info test_collide;
2432 SDL_assert(pair->a->instance >= 0);
2433 SDL_assert(pair->a->type == OBJ_BEAM);
2434 SDL_assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2435 b = &Beams[pair->a->instance];
2437 // if the "warming up" timestamp has not expired
2438 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2442 // if the beam is on "safety", don't collide with anything
2443 if(b->flags & BF_SAFETY){
2447 // if the colliding object is the shooting object, return 1 so this is culled
2448 if(pair->b == b->objp){
2452 // try and get a model
2453 model_num = beam_get_model(pair->b);
2464 test_collide.model_num = model_num;
2465 test_collide.submodel_num = -1;
2466 test_collide.orient = &pair->b->orient;
2467 test_collide.pos = &pair->b->pos;
2468 test_collide.p0 = &b->last_start;
2469 test_collide.p1 = &b->last_shot;
2470 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2471 model_collide(&test_collide);
2474 if(test_collide.num_hits){
2475 // add to the collision list
2476 beam_add_collision(b, pair->b, &test_collide);
2479 // reset timestamp to timeout immediately
2480 pair->next_check_time = timestamp(0);
2485 // collide a beam with debris, returns 1 if we can ignore all future collisions between the 2 objects
2486 int beam_collide_debris(obj_pair *pair)
2489 mc_info test_collide;
2498 SDL_assert(pair->a->instance >= 0);
2499 SDL_assert(pair->a->type == OBJ_BEAM);
2500 SDL_assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2501 b = &Beams[pair->a->instance];
2503 // if the "warming up" timestamp has not expired
2504 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2508 // if the beam is on "safety", don't collide with anything
2509 if(b->flags & BF_SAFETY){
2513 // if the colliding object is the shooting object, return 1 so this is culled
2514 if(pair->b == b->objp){
2518 // try and get a model
2519 model_num = beam_get_model(pair->b);
2530 test_collide.model_num = model_num;
2531 test_collide.submodel_num = -1;
2532 test_collide.orient = &pair->b->orient;
2533 test_collide.pos = &pair->b->pos;
2534 test_collide.p0 = &b->last_start;
2535 test_collide.p1 = &b->last_shot;
2536 test_collide.flags = MC_CHECK_MODEL | MC_CHECK_RAY;
2537 model_collide(&test_collide);
2540 if(test_collide.num_hits){
2541 // add to the collision list
2542 beam_add_collision(b, pair->b, &test_collide);
2545 // add this guy to the lighting list
2546 beam_add_light(b, OBJ_INDEX(pair->b), 1, NULL);
2548 // reset timestamp to timeout immediately
2549 pair->next_check_time = timestamp(0);
2554 // early-out function for when adding object collision pairs, return 1 if the pair should be ignored
2555 int beam_collide_early_out(object *a, object *b)
2559 float cull_dist, cull_dot;
2560 vector dot_test, dot_test2, dist_test;
2563 SDL_assert(a->instance >= 0);
2564 if(a->instance < 0){
2567 SDL_assert(a->type == OBJ_BEAM);
2568 if(a->type != OBJ_BEAM){
2571 SDL_assert(Beams[a->instance].objnum == OBJ_INDEX(a));
2572 if(Beams[a->instance].objnum != OBJ_INDEX(a)){
2575 bm = &Beams[a->instance];
2576 SDL_assert(bm->weapon_info_index >= 0);
2577 if(bm->weapon_info_index < 0){
2580 bwi = &Weapon_info[bm->weapon_info_index];
2582 // if the second object has an invalid instance, bail
2583 if(b->instance < 0){
2592 // targeting lasers only hit ships
2593 if(bwi->b_info.beam_type == BEAM_TYPE_C){
2598 // targeting lasers only hit ships
2599 if(bwi->b_info.beam_type == BEAM_TYPE_C){
2602 // don't ever collide with non hull pieces
2603 if(!Debris[b->instance].is_hull){
2608 // targeting lasers only hit ships
2609 if(bwi->b_info.beam_type == BEAM_TYPE_C){
2612 // don't ever collide against laser weapons - duh
2613 if(Weapon_info[Weapons[b->instance].weapon_info_index].subtype == WP_LASER){
2619 // get full cull value
2620 beam_get_cull_vals(b, bm, &cull_dot, &cull_dist);
2622 // if the object fails these conditions, bail
2623 vm_vec_sub(&dist_test, &b->pos, &bm->last_start);
2624 dot_test = dist_test;
2625 vm_vec_sub(&dot_test2, &bm->last_shot, &bm->last_start);
2626 vm_vec_normalize_quick(&dot_test);
2627 vm_vec_normalize_quick(&dot_test2);
2628 // cull_dist == DIST SQUARED FOO!
2629 if((vm_vec_dotprod(&dot_test, &dot_test2) < cull_dot) && (vm_vec_mag_squared(&dist_test) > cull_dist)){
2637 // add a collision to the beam for this frame (to be evaluated later)
2638 void beam_add_collision(beam *b, object *hit_object, mc_info *cinfo)
2643 // if we haven't reached the limit for beam collisions, just add
2644 if(b->f_collision_count < MAX_FRAME_COLLISIONS){
2645 bc = &b->f_collisions[b->f_collision_count++];
2646 bc->c_objnum = OBJ_INDEX(hit_object);
2653 // otherwise, we've got to do some checking, ick.
2654 // I guess we can always just remove the farthest item
2656 for(idx=0; idx<MAX_FRAME_COLLISIONS; idx++){
2657 if((bc == NULL) || (b->f_collisions[idx].cinfo.hit_dist > bc->cinfo.hit_dist)){
2658 bc = &b->f_collisions[idx];
2663 SDL_assert(bc != NULL);
2667 bc->c_objnum = OBJ_INDEX(hit_object);
2671 // sort collisions for the frame
2672 int beam_sort_collisions_func(const void *e1, const void *e2)
2674 beam_collision *b1 = (beam_collision*)e1;
2675 beam_collision *b2 = (beam_collision*)e2;
2677 return b1->cinfo.hit_dist < b2->cinfo.hit_dist ? -1 : 1;
2680 // handle a hit on a specific object
2681 void beam_handle_collisions(beam *b)
2684 beam_collision r_coll[MAX_FRAME_COLLISIONS];
2685 int r_coll_count = 0;
2689 // early out if we had no collisions
2690 if(b->f_collision_count <= 0){
2694 // get beam weapon info
2695 if((b->weapon_info_index < 0) || (b->weapon_info_index >= Num_weapon_types)){
2700 wi = &Weapon_info[b->weapon_info_index];
2702 // get the widest part of the beam
2703 widest = beam_get_widest(b);
2705 // the first thing we need to do is sort the collisions, from closest to farthest
2706 qsort(b->f_collisions, b->f_collision_count, sizeof(beam_collision), beam_sort_collisions_func);
2708 // now apply all collisions until we reach a ship which "stops" the beam or we reach the end of the list
2709 for(idx=0; idx<b->f_collision_count; idx++){
2713 int target = b->f_collisions[idx].c_objnum;
2715 // if we have an invalid object
2716 if((target < 0) || (target >= MAX_OBJECTS)){
2720 // try and get a model to deal with
2721 model_num = beam_get_model(&Objects[target]);
2727 beam_add_light(b, target, 2, &b->f_collisions[idx].cinfo.hit_point_world);
2729 // add to the recent collision list
2730 r_coll[r_coll_count].c_objnum = target;
2731 r_coll[r_coll_count].c_sig = Objects[target].signature;
2732 r_coll[r_coll_count].c_stamp = -1;
2733 r_coll[r_coll_count].cinfo = b->f_collisions[idx].cinfo;
2735 // if he was already on the recent collision list, copy his timestamp
2736 // also, be sure not to play the impact sound again.
2737 for(s_idx=0; s_idx<b->r_collision_count; s_idx++){
2738 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)){
2740 r_coll[r_coll_count].c_stamp = b->r_collisions[s_idx].c_stamp;
2742 // don't play the impact sound again
2747 // if the damage timestamp has expired or is not set yet, apply damage
2748 if((r_coll[r_coll_count].c_stamp == -1) || timestamp_elapsed(r_coll[r_coll_count].c_stamp)){
2750 r_coll[r_coll_count].c_stamp = timestamp(BEAM_DAMAGE_TIME);
2753 // if no damage - don't even indicate it has been hit
2754 if(wi->damage <= 0){
2758 // increment collision count
2761 // play the impact sound
2763 snd_play_3d( &Snds[wi->impact_snd], &b->f_collisions[idx].cinfo.hit_point_world, &Eye_position );
2767 if(do_damage && !physics_paused){
2768 // maybe draw an explosion
2769 if(wi->impact_weapon_expl_index >= 0){
2770 int ani_handle = weapon_get_expl_handle(wi->impact_weapon_expl_index, &b->f_collisions[idx].cinfo.hit_point_world, wi->impact_explosion_radius);
2771 particle_create( &b->f_collisions[idx].cinfo.hit_point_world, &vmd_zero_vector, 0.0f, wi->impact_explosion_radius, PARTICLE_BITMAP_PERSISTENT, ani_handle );
2774 switch(Objects[target].type){
2776 // hit the debris - the debris hit code takes care of checking for MULTIPLAYER_CLIENT, etc
2777 debris_hit(&Objects[target], &Objects[b->objnum], &b->f_collisions[idx].cinfo.hit_point_world, Weapon_info[b->weapon_info_index].damage);
2781 // detonate the missile
2782 SDL_assert(Weapon_info[Weapons[Objects[target].instance].weapon_info_index].subtype == WP_MISSILE);
2783 if(!(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_MASTER){
2784 weapon_hit(&Objects[target], NULL, &Objects[target].pos);
2790 if(!(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_MASTER){
2791 asteroid_hit(&Objects[target], &Objects[b->objnum], &b->f_collisions[idx].cinfo.hit_point_world, Weapon_info[b->weapon_info_index].damage);
2796 // hit the ship - again, the innards of this code handle multiplayer cases
2797 // maybe vaporize ship.
2798 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);
2800 // if this is the first hit on the player ship. whack him
2802 beam_apply_whack(b, &Objects[target], &b->f_collisions[idx].cinfo.hit_point_world);
2808 // if the radius of the target is somewhat close to the radius of the beam, "stop" the beam here
2809 // for now : if its smaller than about 1/3 the radius of the ship
2810 if(widest <= (Objects[target].radius * BEAM_AREA_PERCENT) && !beam_will_tool_target(b, &Objects[target])){
2811 // set last_shot so we know where to properly draw the beam
2812 b->last_shot = b->f_collisions[idx].cinfo.hit_point_world;
2813 SDL_assert(is_valid_vec(&b->last_shot));
2815 // done wif the beam
2820 // store the new recent collisions
2821 for(idx=0; idx<r_coll_count; idx++){
2822 b->r_collisions[idx] = r_coll[idx];
2824 b->r_collision_count = r_coll_count;
2827 // for a given object, and a firing beam, determine its critical dot product and range
2828 void beam_get_cull_vals(object *objp, beam *b, float *cull_dot, float *cull_dist)
2831 // debris and asteroids are classified as slow moving small objects
2832 // use cull_dot == potential cone of beam + 10% and 50.0 meters
2835 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.10f);
2836 *cull_dist = 50.0f * 50.0f;
2839 // treat missiles as fast-moving small objects
2841 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.5f);
2842 *cull_dist = 300.0f * 300.0f;
2846 // for cap ships, only cull for 90deg or better
2848 if(Ship_info[Ships[objp->instance].ship_info_index].flags & SIF_CAPITAL){
2855 // for large ships, cull at some multiple of the radius
2856 if(Ship_info[Ships[objp->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)){
2857 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.25f);
2859 *cull_dist = (objp->radius * 1.3f) * (objp->radius * 1.3f);
2863 // for everthing else, cull the same as missiles
2864 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.5f);
2865 *cull_dist = 300.0f * 300.0f;
2869 // BAD BAD BAD - but this code will cause everything to cull properly
2876 // FIXME - make sure we are truthfull representing the "cone" for all beam types
2877 // get the total possible cone for a given beam in radians
2878 float beam_get_cone_dot(beam *b)
2885 // even though these beams don't move, return a _very_ small value
2886 return (float)cos(fl_radian(50.5f));
2889 return vm_vec_dotprod(&b->binfo.dir_a, &b->binfo.dir_b);
2899 // if it is legal for the beam to fire, or continue firing
2900 int beam_ok_to_fire(beam *b)
2902 // type C beams are ok to fire all the time
2903 if(Weapon_info[b->weapon_info_index].b_info.beam_type == BEAM_TYPE_C){
2907 // if my own object is invalid, stop firing
2908 if(b->objp->signature != b->sig){
2909 mprintf(("BEAM : killing beam because of invalid parent object SIGNATURE!\n"));
2913 // if my own object is a ghost
2914 if(b->objp->type != OBJ_SHIP){
2915 mprintf(("BEAM : killing beam because of invalid parent object TYPE!\n"));
2919 // if the shooting turret is destroyed
2920 if(b->subsys->current_hits <= 0.0f){
2921 mprintf(("BEAM : killing beam because turret has been destroyed!\n"));
2925 // if the beam will be firing out of its FOV, power it down
2926 vector aim_dir, temp;
2927 vector turret_dir, turret_pos;
2928 vm_vec_sub(&aim_dir, &b->last_shot, &b->last_start);
2929 vm_vec_normalize(&aim_dir);
2930 ship_get_global_turret_gun_info(b->objp, b->subsys, &turret_pos, &turret_dir, 1, &temp);
2931 if(vm_vec_dotprod(&aim_dir, &turret_dir) < b->subsys->system_info->turret_fov){
2932 mprintf(("BEAM : powering beam down because of FOV condition!\n"));
2936 // ok to fire/continue firing
2940 // get the width of the widest section of the beam
2941 float beam_get_widest(beam *b)
2944 float widest = -1.0f;
2947 SDL_assert(b->weapon_info_index >= 0);
2948 if(b->weapon_info_index < 0){
2953 for(idx=0; idx<Weapon_info[b->weapon_info_index].b_info.beam_num_sections; idx++){
2954 if(Weapon_info[b->weapon_info_index].b_info.sections[idx].width > widest){
2955 widest = Weapon_info[b->weapon_info_index].b_info.sections[idx].width;
2960 return widest * b->shrink;
2963 // apply a whack to a ship
2964 void beam_apply_whack(beam *b, object *objp, vector *hit_point)
2970 SDL_assert((b != NULL) && (objp != NULL) && (hit_point != NULL));
2971 if((b == NULL) || (objp == NULL) || (hit_point == NULL)){
2974 SDL_assert(b->weapon_info_index >= 0);
2975 wip = &Weapon_info[b->weapon_info_index];
2976 SDL_assert((objp != NULL) && (objp->type == OBJ_SHIP) && (objp->instance >= 0) && (objp->instance < MAX_SHIPS));
2977 if((objp == NULL) || (objp->type != OBJ_SHIP) || (objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
2980 shipp = &Ships[objp->instance];
2981 if((shipp->ai_index < 0) || (shipp->ai_index >= MAX_AI_INFO)){
2985 // don't whack docking ships
2986 if(Ai_info[shipp->ai_index].ai_flags & AIF_DOCKED){
2990 // determine how big of a whack to apply
2994 if(wip->damage < b_whack_damage){
2995 whack = b_whack_small;
2997 whack = b_whack_big;
3001 vector whack_dir, temp;
3002 vm_vec_dist_to_line(&objp->pos, &b->last_start, &b->last_shot, &temp, &dist);
3003 vm_vec_sub(&whack_dir, &objp->pos, &temp);
3004 vm_vec_normalize(&whack_dir);
3005 vm_vec_scale(&whack_dir, whack);
3008 ship_apply_whack(&whack_dir, hit_point, objp);
3011 // return the amount of damage which should be applied to a ship. basically, filters friendly fire damage
3012 float beam_get_ship_damage(beam *b, object *objp)
3014 // if the beam is on the same team as the object
3015 SDL_assert((objp != NULL) && (b != NULL));
3016 if((objp == NULL) || (b == NULL)){
3019 SDL_assert((objp->type == OBJ_SHIP) && (objp->instance >= 0) && (objp->instance < MAX_SHIPS));
3020 if((objp->type != OBJ_SHIP) || (objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
3025 if((b->team == Ships[objp->instance].team) && (Weapon_info[b->weapon_info_index].damage > Beam_friendly_cap[Game_skill_level])){
3026 return Beam_friendly_cap[Game_skill_level];
3030 return Weapon_info[b->weapon_info_index].damage;
3033 // if the beam is likely to tool a given target before its lifetime expires
3034 int beam_will_tool_target(beam *b, object *objp)
3036 weapon_info *wip = &Weapon_info[b->weapon_info_index];
3037 float damage_in_a_few_seconds;
3044 // if the object is not a ship, bail
3045 if(objp->type != OBJ_SHIP){
3048 if((objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
3052 // if the beam is going to apply more damage in about 1 and a half than the hull of the ship can take
3053 damage_in_a_few_seconds = (TOOLTIME / (float)BEAM_DAMAGE_TIME) * wip->damage;
3054 if(objp->hull_strength < damage_in_a_few_seconds){
3063 float beam_accuracy = 1.0f;
3066 dc_get_arg(ARG_FLOAT);
3067 beam_accuracy = Dc_arg_float;
3074 for(idx=0; idx<Num_weapon_types; idx++){
3075 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3077 dc_printf("Beam %d : %s\n", b_count, Weapon_info[idx].name);
3081 void beam_test(int whee)
3084 object *orion, *fenris;
3085 ship_subsys *lookup;
3086 ship_subsys *orion_turret = NULL, *orion_radar = NULL;
3087 ship_subsys *fenris_turret = NULL, *fenris_radar = NULL;
3090 nprintf(("General", "Running beam test\n"));
3092 // lookup some stuff
3093 s1 = ship_name_lookup("GTD Orion 1");
3094 SDL_assert(s1 >= 0);
3095 orion = &Objects[Ships[s1].objnum];
3096 s2 = ship_name_lookup("GTC Fenris 2");
3097 SDL_assert(s2 >= 0);
3098 fenris = &Objects[Ships[s2].objnum];
3101 lookup = GET_FIRST(&Ships[s1].subsys_list);
3102 while(lookup != END_OF_LIST(&Ships[s1].subsys_list)){
3104 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !SDL_strcasecmp(lookup->system_info->subobj_name, "turret07")){
3105 orion_turret = lookup;
3110 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3111 orion_radar = lookup;
3115 lookup = GET_NEXT(lookup);
3117 SDL_assert(orion_turret != NULL);
3118 SDL_assert(orion_radar != NULL);
3119 lookup = GET_FIRST(&Ships[s2].subsys_list);
3120 while(lookup != END_OF_LIST(&Ships[s2].subsys_list)){
3122 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !SDL_strcasecmp(lookup->system_info->subobj_name, "turret07")){
3123 fenris_turret = lookup;
3128 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3129 fenris_radar = lookup;
3133 lookup = GET_NEXT(lookup);
3135 SDL_assert(fenris_turret != NULL);
3136 SDL_assert(fenris_radar != NULL);
3138 memset(&f, 0, sizeof(beam_fire_info));
3139 f.accuracy = beam_accuracy;
3140 f.beam_info_index = -1;
3141 f.beam_info_override = NULL;
3144 f.target_subsys = fenris_turret;
3145 f.turret = orion_turret;
3147 // find the first beam
3149 int beam_first = -1;
3152 for(idx=0; idx<Num_weapon_types; idx++){
3153 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3165 // maybe fire it, if its valid
3166 f.beam_info_index = beam_first + whee - 1;
3167 if(Weapon_info[f.beam_info_index].wi_flags & WIF_BEAM){
3168 HUD_printf("Firing %s\n", Weapon_info[f.beam_info_index].name);
3173 void beam_test_new(int whee)
3176 object *fenris, *alpha;
3177 ship_subsys *fenris_turret, *lookup;
3180 nprintf(("General", "Running beam test\n"));
3182 // lookup some stuff
3183 s2 = ship_name_lookup("GTC Fenris 2");
3184 SDL_assert(s2 >= 0);
3185 fenris = &Objects[Ships[s2].objnum];
3186 s3 = ship_name_lookup("Alpha 1");
3187 SDL_assert(s3 >= 0);
3188 alpha = &Objects[Ships[s3].objnum];
3191 lookup = GET_FIRST(&Ships[s2].subsys_list);
3192 fenris_turret = NULL;
3193 while(lookup != END_OF_LIST(&Ships[s2].subsys_list)){
3195 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !SDL_strcasecmp(lookup->system_info->subobj_name, "turret03")){
3196 fenris_turret = lookup;
3200 lookup = GET_NEXT(lookup);
3202 SDL_assert(fenris_turret != NULL);
3204 memset(&f, 0, sizeof(beam_fire_info));
3205 f.accuracy = beam_accuracy;
3206 f.beam_info_override = NULL;
3209 f.target_subsys = NULL;
3210 f.turret = fenris_turret;
3213 // find the first beam
3215 int beam_first = -1;
3218 for(idx=0; idx<Num_weapon_types; idx++){
3219 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3231 // maybe fire it, if its valid
3232 f.beam_info_index = beam_first + whee - 1;
3233 if(Weapon_info[f.beam_info_index].wi_flags & WIF_BEAM){
3234 HUD_printf("Firing %s\n", Weapon_info[f.beam_info_index].name);