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.4 2002/06/09 04:41:29 relnev
19 * added copyright header
21 * Revision 1.3 2002/06/02 00:31:36 relnev
22 * implemented osregistry
24 * Revision 1.2 2002/05/07 03:16:53 theoddone33
25 * The Great Newline Fix
27 * Revision 1.1.1.1 2002/05/03 03:28:11 root
31 * 68 9/09/99 11:40p Dave
32 * Handle an Assert() in beam code. Added supernova sounds. Play the right
33 * 2 end movies properly, based upon what the player did in the mission.
35 * 67 9/09/99 2:36p Mikek
36 * Put back in the "1.0f +" in BEAM_TYPE_A aiming. Not the best way to
39 * 65 9/08/99 10:29p Dave
40 * Make beam sound pausing and unpausing much safer.
42 * 64 9/06/99 12:46a Andsager
43 * Add weapon_explosion_ani LOD
45 * 63 9/03/99 5:12p Mikek
46 * Change miss_factor code, making it a lot more likely for beams to miss
47 * and also making them increasingly likely to miss with each subsequent
48 * shot in the multi-shot burst.
50 * 62 8/30/99 5:01p Dave
51 * Made d3d do less state changing in the nebula. Use new chat server for
54 * 61 8/28/99 7:29p Dave
55 * Fixed wingmen persona messaging. Make sure locked turrets don't count
56 * towards the # attacking a player.
58 * 60 8/27/99 9:07p Dave
59 * LOD explosions. Improved beam weapon accuracy.
61 * 59 8/26/99 10:15a Dave
62 * Don't apply beam whacks to docked ships.
64 * 58 7/31/99 1:16p Dave
65 * Use larger font for 1024 HUD flash text box. Make beam weapons aware of
66 * weapon subsystem damage on firing ship.
68 * 57 7/22/99 4:00p Dave
69 * Fixed beam weapon muzzle glow rendering. Externalized hud shield info.
71 * 56 7/19/99 7:20p Dave
72 * Beam tooling. Specialized player-killed-self messages. Fixed d3d nebula
75 * 55 7/15/99 9:20a Andsager
76 * FS2_DEMO initial checkin
78 * 54 7/09/99 5:54p Dave
79 * Seperated cruiser types into individual types. Added tons of new
80 * briefing icons. Campaign screen.
82 * 53 7/08/99 10:53a Dave
83 * New multiplayer interpolation scheme. Not 100% done yet, but still
84 * better than the old way.
86 * 52 7/02/99 10:51p Dave
87 * Limit friendly beam fire damage. :(
89 * 51 7/01/99 11:44a Dave
90 * Updated object sound system to allow multiple obj sounds per ship.
91 * Added hit-by-beam sound. Added killed by beam sound.
93 * 50 6/29/99 7:39p Dave
94 * Lots of small bug fixes.
96 * 49 6/29/99 2:53p Dave
97 * Re-enabled beam lighting.
99 * 48 6/25/99 3:04p Dave
102 * 47 6/24/99 3:00p Dave
105 * 46 6/23/99 4:49p Dave
106 * Temporarily removed beam lighting.
108 * 45 6/21/99 7:25p Dave
109 * netplayer pain packet. Added type E unmoving beams.
111 * 44 6/18/99 5:16p Dave
112 * Added real beam weapon lighting. Fixed beam weapon sounds. Added MOTD
113 * dialog to PXO screen.
115 * 43 6/14/99 10:45a Dave
116 * Made beam weapons specify accuracy by skill level in the weapons.tbl
118 * 42 6/09/99 2:55p Andsager
119 * Allow multiple asteroid subtypes (of large, medium, small) and follow
122 * 41 6/08/99 6:02p Jamesa
123 * Handle case where type B beam start and end directions are really
126 * 40 6/04/99 2:16p Dave
127 * Put in shrink effect for beam weapons.
129 * 39 5/14/99 11:47a Andsager
130 * Added beam_get_weapon_info_index(object *bm)
132 * 38 5/12/99 10:43a Andsager
133 * Increase max beam length
135 * 37 5/08/99 8:25p Dave
136 * Upped object pairs. First run of nebula lightning.
138 * 36 5/05/99 9:02p Dave
139 * Fixed D3D aabitmap rendering. Spiffed up nebula effect a bit (added
140 * rotations, tweaked values, made bitmap selection more random). Fixed
141 * D3D beam weapon clipping problem. Added D3d frame dumping.
143 * 35 5/03/99 9:07a Dave
144 * Pirate Bob. Changed beam test code a bit.
146 * 34 4/27/99 12:16a Dave
147 * Fixed beam weapon muzzle glow problem. Fixed premature timeout on the
148 * pxo server list screen. Fixed secondary firing for hosts on a
149 * standalone. Fixed wacky multiplayer weapon "shuddering" problem.
151 * 33 4/25/99 6:12p Johnson
152 * Fixed bug where multi-shot beams were getting the shot count from the
155 * 32 4/25/99 3:36p Dave
156 * Fixed nebula table code. Tweaked beam weapon explosion stuff.
158 * 31 4/23/99 2:33p Johnson
159 * Allow beams to shoot at missiles properly.
161 * 30 4/23/99 12:01p Johnson
162 * Added SIF_HUGE_SHIP
164 * 29 4/22/99 11:06p Dave
165 * Final pass at beam weapons. Solidified a lot of stuff. All that remains
166 * now is to tweak and fix bugs as they come up. No new beam weapon
169 * 28 4/21/99 6:15p Dave
170 * Did some serious housecleaning in the beam code. Made it ready to go
171 * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
172 * a handy macro for recalculating collision pairs for a given object.
174 * 27 4/20/99 6:39p Dave
175 * Almost done with artillery targeting. Added support for downloading
176 * images on the PXO screen.
178 * 26 4/19/99 11:01p Dave
179 * More sophisticated targeting laser support. Temporary checkin.
181 * 25 4/19/99 4:54p Johnson
182 * Removed infinite loop from beam_render_all() oops :)
184 * 24 4/16/99 5:54p Dave
185 * Support for on/off style "stream" weapons. Real early support for
186 * target-painting lasers.
188 * 23 4/04/99 2:13p Dave
189 * Put in area effect beam weapons. May be a bit too expensive though.
191 * 22 4/02/99 9:55a Dave
192 * Added a few more options in the weapons.tbl for beam weapons. Attempt
193 * at putting "pain" packets into multiplayer.
195 * 21 3/31/99 8:24p Dave
196 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
197 * and background nebulae. Added per-ship non-dimming pixel colors.
199 * 20 3/08/99 7:03p Dave
200 * First run of new object update system. Looks very promising.
202 * 19 3/04/99 6:09p Dave
203 * Added in sexpressions for firing beams and checking for if a ship is
206 * 18 3/02/99 9:25p Dave
207 * Added a bunch of model rendering debug code. Started work on fixing
208 * beam weapon wacky firing.
210 * 17 2/21/99 1:48p Dave
211 * Some code for monitoring datarate for multiplayer in detail.
213 * 16 2/11/99 3:08p Dave
214 * PXO refresh button. Very preliminary squad war support.
216 * 15 2/05/99 3:23p Mattf
217 * Made beams a little more forgiving when checking object types.
219 * 14 2/05/99 12:52p Dave
220 * Fixed Glide nondarkening textures.
222 * 13 2/04/99 6:29p Dave
223 * First full working rev of FS2 PXO support. Fixed Glide lighting
226 * 12 1/30/99 9:02p Dave
229 * 11 1/30/99 1:29a Dave
230 * Fixed nebula thumbnail problem. Full support for 1024x768 choose pilot
231 * screen. Fixed beam weapon death messages.
233 * 10 1/29/99 7:08p Johnson
234 * Put in fix for beam weapons which are on dying ships.
236 * 9 1/29/99 12:47a Dave
237 * Put in sounds for beam weapon. A bunch of interface screens (tech
240 * 8 1/27/99 9:56a Dave
241 * Temporary checkin of beam weapons for Dan to make cool sounds.
243 * 7 1/24/99 11:37p Dave
244 * First full rev of beam weapons. Very customizable. Removed some bogus
245 * Int3()'s in low level net code.
247 * 6 1/21/99 2:06p Dave
248 * Final checkin for multiplayer testing.
250 * 5 1/21/99 10:45a Dave
251 * More beam weapon stuff. Put in warmdown time.
253 * 4 1/14/99 12:48a Dave
254 * Todo list bug fixes. Made a pass at putting briefing icons back into
255 * FRED. Sort of works :(
257 * 3 1/12/99 12:53a Dave
258 * More work on beam weapons - made collision detection very efficient -
259 * collide against all object types properly - made 3 movement types
260 * smooth. Put in test code to check for possible non-darkening pixels on
263 * 2 1/08/99 2:08p Dave
264 * Fixed software rendering for pofview. Super early support for AWACS and
271 #include "linklist.h"
273 #include "objcollide.h"
275 #include "freespace.h"
278 #include "alphacolors.h"
280 #include "fireballs.h"
282 #include "asteroid.h"
284 #include "multimsgs.h"
286 #include "particle.h"
292 #include "hudmessage.h"
294 #include "lighting.h"
296 // ------------------------------------------------------------------------------------------------
297 // BEAM WEAPON DEFINES/VARS
300 // use this to extend a beam to "infinity"
301 #define BEAM_FAR_LENGTH 30000.0f
303 // this is the constant which defines when a beam is an "area" beam. meaning, when we switch on sphereline checking and when
304 // a beam gets "stopped" by an object. It is a percentage of the object radius which the beam must be wider than
305 #define BEAM_AREA_PERCENT 0.4f
307 // randomness factor - all beam weapon aiming is adjusted by +/- some factor within this range
308 #define BEAM_RANDOM_FACTOR 0.4f
311 #define BEAM_DAMAGE_TIME 170 // apply damage
312 #define MAX_SHOT_POINTS 30
313 #define SHOT_POINT_TIME 200 // 5 arcs a second
315 #define TOOLTIME 1500.0f
317 // max # of collisions we'll allow per frame
318 #define MAX_FRAME_COLLISIONS 5
321 #define BF_SAFETY (1<<0) // if this is set, don't collide or render for this frame. lifetime still increases though
322 #define BF_SHRINK (1<<1) // if this is set, the beam is in the warmdown phase
324 // beam struct (the actual weapon/object)
325 typedef struct beam {
327 int objnum; // our own objnum
328 int weapon_info_index;
329 int sig; // signature for the shooting object
330 object *objp; // the shooting object (who owns the turret that I am being fired from)
331 object *target; // target object
332 ship_subsys *target_subsys; // targeted subsys
333 int target_sig; // target sig
334 ship_subsys *subsys; // subsys its being fired from
335 beam *next, *prev; // link list stuff
336 vector targeting_laser_offset;
337 int framecount; // how many frames the beam has been active
338 int flags; // see BF_* defines
339 float shrink; // shrink factor
342 int warmup_stamp; // timestamp for "warming up"
343 int warmdown_stamp; // timestamp for "warming down"
344 int type; // see BEAM_TYPE_* defines in beam.h
345 float life_left; // in seconds
346 float life_total; // total life
347 // this vector has very special meaning. BEFORE performing collision checks, it basically implies a "direction". meaning
348 // the vector between it and last_start is where the beam will be aiming. AFTER performing collision checks, it is the
349 // literal world collision point on the object (or meaningless, if we hit nothing). The function beam_move_all_pre() is
350 // responsible for then setting it up pre-collision time
353 int shot_index; // for type D beam weapons
356 beam_collision r_collisions[MAX_FRAME_COLLISIONS]; // recent collisions
357 int r_collision_count; // # of recent collisions
359 // collision info for this frame
360 beam_collision f_collisions[MAX_FRAME_COLLISIONS]; // collisions for the current frame
361 int f_collision_count; // # of collisions we recorded this frame
363 // looping sound info, HANDLE
364 int beam_sound_loop; // -1 if none
369 // exactly how the beam will behave. by passing this is multiplayer from server to client, we can ensure that
370 // everything looks the same
374 beam Beams[MAX_BEAMS]; // all beams
375 beam Beam_free_list; // free beams
376 beam Beam_used_list; // used beams
377 int Beam_count = 0; // how many beams are in use
379 // octant indices. These are "good" pairs of octants to use for beam target
380 #define BEAM_NUM_GOOD_OCTANTS 8
381 int Beam_good_slash_octants[BEAM_NUM_GOOD_OCTANTS][4] = {
382 { 2, 5, 1, 0 }, // octant, octant, min/max pt, min/max pt
391 int Beam_good_shot_octants[BEAM_NUM_GOOD_OCTANTS][4] = {
392 { 5, 0, 1, 0 }, // octant, octant, min/max pt, min/max pt
402 // damage cap values for friendly beam fire
403 float Beam_friendly_cap[NUM_SKILL_LEVELS] = { 0.0f, 5.0f, 10.0f, 20.0f, 30.0f };
405 // beam lighting effects
406 int Beam_lighting = 1;
408 // debug stuff - keep track of how many collision tests we perform a second and how many we toss a second
409 #define BEAM_TEST_STAMP_TIME 4000 // every 4 seconds
410 int Beam_test_stamp = -1;
411 int Beam_test_ints = 0;
412 int Beam_test_ship = 0;
413 int Beam_test_ast = 0;
414 int Beam_test_framecount = 0;
416 // beam warmup completion %
417 #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 )
419 // beam warmdown completion %
420 #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 )
422 // timestamp for spewing muzzle particles
423 int Beam_muzzle_stamp = -1;
425 // link into the physics paused system
426 extern int physics_paused;
428 // beam lighting info
429 #define MAX_BEAM_LIGHT_INFO 100
430 typedef struct beam_light_info {
431 beam *bm; // beam casting the light
432 int objnum; // object getting light cast on it
433 ubyte source; // 0 to light the shooter, 1 for lighting any ship the beam passes, 2 to light the collision ship
434 vector c_point; // collision point for type 2 lights
437 beam_light_info Beam_lights[MAX_BEAM_LIGHT_INFO];
438 int Beam_light_count = 0;
440 float b_whack_small = 500.0f;
441 float b_whack_big = 1500.0f;
442 float b_whack_damage = 150.0f;
443 DCF(b_whack_small, "")
445 dc_get_arg(ARG_FLOAT);
446 b_whack_small = Dc_arg_float;
450 dc_get_arg(ARG_FLOAT);
451 b_whack_big = Dc_arg_float;
453 DCF(b_whack_damage, "")
455 dc_get_arg(ARG_FLOAT);
456 b_whack_damage = Dc_arg_float;
460 // ------------------------------------------------------------------------------------------------
461 // BEAM WEAPON FORWARD DECLARATIONS
465 void beam_delete(beam *b);
467 // handle a hit on a specific object
468 void beam_handle_collisions(beam *b);
471 void beam_get_binfo(beam *b, float accuracy, int num_shots);
473 // aim the beam (setup last_start and last_shot - the endpoints). also recalculates object collision info
474 void beam_aim(beam *b);
477 void beam_type_a_move(beam *b);
480 void beam_type_b_move(beam *b);
483 void beam_type_c_move(beam *b);
486 void beam_type_d_move(beam *b);
487 void beam_type_d_get_status(beam *b, int *shot_index, int *fire_wait);
490 void beam_type_e_move(beam *b);
492 // given a model #, and an object, stuff 2 good world coord points
493 void beam_get_octant_points(int modelnum, object *objp, int oct_index, int oct_array[BEAM_NUM_GOOD_OCTANTS][4], vector *v1, vector *v2);
495 // given an object, return its model num
496 int beam_get_model(object *objp);
498 // for a given object, and a firing beam, determine its critical dot product and range
499 void beam_get_cull_vals(object *objp, beam *b, float *cull_dot, float *cull_dist);
501 // get the total possible cone for a given beam in radians
502 float beam_get_cone_dot(beam *b);
504 // for rendering the beam effect
505 // output top and bottom vectors
506 // fvec == forward vector (eye viewpoint basically. in world coords)
507 // pos == world coordinate of the point we're calculating "around"
508 // w == width of the diff between top and bottom around pos
509 void beam_calc_facing_pts(vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add);
511 // render the muzzle glow for a beam weapon
512 void beam_render_muzzle_glow(beam *b);
514 // generate particles for the muzzle glow
515 void beam_generate_muzzle_particles(beam *b);
517 // throw some jitter into the aim - based upon shot_aim
518 void beam_jitter_aim(beam *b, float aim);
520 // if it is legal for the beam to continue firing
521 // returns -1 if the beam should stop firing immediately
522 // returns 0 if the beam should go to warmdown
523 // returns 1 if the beam can continue along its way
524 int beam_ok_to_fire(beam *b);
526 // start the warmup phase for the beam
527 void beam_start_warmup(beam *b);
529 // start the firing phase for the beam, return 0 if the beam failed to start, and should be deleted altogether
530 int beam_start_firing(beam *b);
532 // start the warmdown phase for the beam
533 void beam_start_warmdown(beam *b);
535 // add a collision to the beam for this frame (to be evaluated later)
536 void beam_add_collision(beam *b, object *hit_object, mc_info *cinfo);
538 // sort collisions for the frame
539 int beam_sort_collisions_func(const void *e1, const void *e2);
541 // get the width of the widest section of the beam
542 float beam_get_widest(beam *b);
544 // mark an object as being lit
545 void beam_add_light(beam *b, int objnum, int source, vector *c_point);
547 // apply lighting from any beams
548 void beam_apply_lighting();
550 // recalculate beam sounds (looping sounds relative to the player)
551 void beam_recalc_sounds(beam *b);
553 // apply a whack to a ship
554 void beam_apply_whack(beam *b, object *objp, vector *hit_point);
556 // return the amount of damage which should be applied to a ship. basically, filters friendly fire damage
557 float beam_get_ship_damage(beam *b, object *objp);
559 // if the beam is likely to tool a given target before its lifetime expires
560 int beam_will_tool_target(beam *b, object *objp);
563 // ------------------------------------------------------------------------------------------------
564 // BEAM WEAPON FUNCTIONS
567 // init at game startup
571 list_init( &Beam_free_list );
572 list_init( &Beam_used_list );
575 // initialize beam weapons for this level
576 void beam_level_init()
582 list_init( &Beam_free_list );
583 list_init( &Beam_used_list );
584 memset(Beams, 0, sizeof(beam) * MAX_BEAMS);
586 // Link all object slots into the free list
587 for (idx=0; idx<MAX_BEAMS; idx++) {
588 Beams[idx].objnum = -1;
589 list_append(&Beam_free_list, &Beams[idx] );
592 // reset muzzle particle spew timestamp
593 Beam_muzzle_stamp = -1;
596 // shutdown beam weapons for this level
597 void beam_level_close()
600 list_init( &Beam_free_list );
601 list_init( &Beam_used_list );
604 // fire a beam, returns nonzero on success. the innards of the code handle all the rest, foo
605 int beam_fire(beam_fire_info *fire_info)
613 if(fire_info == NULL){
618 // if we're out of beams, bail
619 if(Beam_count >= MAX_BEAMS){
623 // for now, only allow ship targets
624 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)){
628 // make sure the beam_info_index is valid
629 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));
630 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)){
633 wip = &Weapon_info[fire_info->beam_info_index];
635 // make sure a ship is firing this
636 Assert((fire_info->shooter->type == OBJ_SHIP) && (fire_info->shooter->instance >= 0) && (fire_info->shooter->instance < MAX_SHIPS));
637 if((fire_info->shooter->type != OBJ_SHIP) || (fire_info->shooter->instance < 0) && (fire_info->shooter->instance >= MAX_SHIPS)){
640 firing_ship = &Ships[fire_info->shooter->instance];
643 new_item = GET_FIRST(&Beam_free_list);
644 Assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
645 if(new_item == &Beam_free_list){
649 // remove from the free list
650 list_remove( &Beam_free_list, new_item );
652 // insert onto the end of used list
653 list_append( &Beam_used_list, new_item );
658 // fill in some values
659 new_item->warmup_stamp = -1;
660 new_item->warmdown_stamp = -1;
661 new_item->weapon_info_index = fire_info->beam_info_index;
662 new_item->objp = fire_info->shooter;
663 new_item->sig = fire_info->shooter->signature;
664 new_item->subsys = fire_info->turret;
665 new_item->life_left = wip->b_info.beam_life;
666 new_item->life_total = wip->b_info.beam_life;
667 new_item->r_collision_count = 0;
668 new_item->f_collision_count = 0;
669 new_item->target = fire_info->target;
670 new_item->target_subsys = fire_info->target_subsys;
671 new_item->target_sig = fire_info->target->signature;
672 new_item->beam_sound_loop = -1;
673 new_item->type = wip->b_info.beam_type;
674 new_item->targeting_laser_offset = fire_info->targeting_laser_offset;
675 new_item->framecount = 0;
677 new_item->shot_index = 0;
678 new_item->shrink = 1.0f;
679 new_item->team = (char)firing_ship->team;
681 // if the targeted subsystem is not NULL, force it to be a type A beam
682 if(new_item->target_subsys != NULL){
683 new_item->type = BEAM_TYPE_A;
686 // type D weapons can only fire at small ships and missiles
687 if(new_item->type == BEAM_TYPE_D){
688 // if its a targeted ship, get the target ship
689 if((fire_info->target != NULL) && (fire_info->target->type == OBJ_SHIP) && (fire_info->target->instance >= 0)){
690 ship *target_ship = &Ships[fire_info->target->instance];
692 // maybe force to be a type A
693 if(Ship_info[target_ship->ship_info_index].flags & (SIF_BIG_SHIP | SIF_CAPITAL | SIF_SUPERCAP | SIF_FREIGHTER | SIF_DRYDOCK | SIF_CARGO)){
694 new_item->type = BEAM_TYPE_A;
699 // ----------------------------------------------------------------------
700 // THIS IS THE CRITICAL POINT FOR MULTIPLAYER
701 // beam_get_binfo(...) determines exactly how the beam will behave over the course of its life
702 // it fills in binfo, which we can pass to clients in multiplayer
703 if(fire_info->beam_info_override != NULL){
704 new_item->binfo = *fire_info->beam_info_override;
706 beam_get_binfo(new_item, fire_info->accuracy, wip->b_info.beam_shots); // to fill in b_info - the set of directional aim vectors
709 // create the associated object
710 objnum = obj_create(OBJ_BEAM, -1, new_item - Beams, &vmd_identity_matrix, &vmd_zero_vector, 1.0f, OF_COLLIDES);
713 beam_delete(new_item);
714 nprintf(("General", "obj_create() failed for beam weapon! bah!\n"));
717 new_item->objnum = objnum;
719 // this sets up all info for the first frame the beam fires
720 beam_aim(new_item); // to fill in shot_point, etc.
722 // check to see if its legal to fire at this guy
723 if(beam_ok_to_fire(new_item) != 1){
724 beam_delete(new_item);
725 mprintf(("Killing beam at initial fire because of illegal targeting!!!\n"));
729 // if we're a multiplayer master - send a packet
730 if(MULTIPLAYER_MASTER){
731 send_beam_fired_packet(fire_info->shooter, fire_info->turret, fire_info->target, fire_info->beam_info_index, &new_item->binfo);
734 // start the warmup phase
735 beam_start_warmup(new_item);
740 // fire a targeting beam, returns objnum on success. a much much simplified version of a beam weapon
741 // targeting lasers last _one_ frame. For a continuous stream - they must be created every frame.
742 // this allows it to work smoothly in multiplayer (detect "trigger down". every frame just create a targeting laser firing straight out of the
743 // object. this way you get all the advantages of nice rendering and collisions).
744 // NOTE : only references beam_info_index and shooter
745 int beam_fire_targeting(beam_fire_info *fire_info)
753 if(fire_info == NULL){
758 // if we're out of beams, bail
759 if(Beam_count >= MAX_BEAMS){
763 // make sure the beam_info_index is valid
764 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));
765 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)){
768 wip = &Weapon_info[fire_info->beam_info_index];
770 // make sure a ship is firing this
771 Assert((fire_info->shooter->type == OBJ_SHIP) && (fire_info->shooter->instance >= 0) && (fire_info->shooter->instance < MAX_SHIPS));
772 if((fire_info->shooter->type != OBJ_SHIP) || (fire_info->shooter->instance < 0) && (fire_info->shooter->instance >= MAX_SHIPS)){
775 firing_ship = &Ships[fire_info->shooter->instance];
779 new_item = GET_FIRST(&Beam_free_list);
780 Assert( new_item != &Beam_free_list ); // shouldn't have the dummy element
782 // remove from the free list
783 list_remove( &Beam_free_list, new_item );
785 // insert onto the end of used list
786 list_append( &Beam_used_list, new_item );
791 // maybe allocate some extra data based on the beam type
792 Assert(wip->b_info.beam_type == BEAM_TYPE_C);
793 if(wip->b_info.beam_type != BEAM_TYPE_C){
797 // fill in some values
798 new_item->warmup_stamp = -1;
799 new_item->warmdown_stamp = -1;
800 new_item->weapon_info_index = fire_info->beam_info_index;
801 new_item->objp = fire_info->shooter;
803 new_item->subsys = NULL;
804 new_item->life_left = 0;
805 new_item->life_total = 0;
806 new_item->r_collision_count = 0;
807 new_item->f_collision_count = 0;
808 new_item->target = NULL;
809 new_item->target_subsys = NULL;
810 new_item->target_sig = 0;
811 new_item->beam_sound_loop = -1;
812 new_item->type = BEAM_TYPE_C;
813 new_item->targeting_laser_offset = fire_info->targeting_laser_offset;
814 new_item->framecount = 0;
816 new_item->shot_index = 0;
817 new_item->team = (char)firing_ship->team;
819 // type c is a very special weapon type - binfo has no meaning
821 // create the associated object
822 objnum = obj_create(OBJ_BEAM, -1, new_item - Beams, &vmd_identity_matrix, &vmd_zero_vector, 1.0f, OF_COLLIDES);
825 beam_delete(new_item);
826 nprintf(("General", "obj_create() failed for beam weapon! bah!\n"));
829 new_item->objnum = objnum;
831 // this sets up all info for the first frame the beam fires
832 beam_aim(new_item); // to fill in shot_point, etc.
837 // return an object index of the guy who's firing this beam
838 int beam_get_parent(object *bm)
842 // get a handle to the beam
843 Assert(bm->type == OBJ_BEAM);
844 Assert(bm->instance >= 0);
845 if(bm->type != OBJ_BEAM){
848 if(bm->instance < 0){
851 b = &Beams[bm->instance];
853 Assert(b->objp != NULL);
858 // if the object handle is invalid
859 if(b->objp->signature != b->sig){
864 return OBJ_INDEX(b->objp);
867 // return weapon_info_index of beam
868 int beam_get_weapon_info_index(object *bm)
870 Assert(bm->type == OBJ_BEAM);
871 if (bm->type != OBJ_BEAM) {
875 Assert(bm->instance >= 0 && bm->instance < MAX_BEAMS);
876 if (bm->instance < 0) {
880 // return weapon_info_index
881 return Beams[bm->instance].weapon_info_index;
886 // given a beam object, get the # of collisions which happened during the last collision check (typically, last frame)
887 int beam_get_num_collisions(int objnum)
890 if((objnum < 0) || (objnum >= MAX_OBJECTS)){
894 if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
898 if((Beams[Objects[objnum].instance].objnum != objnum) || (Beams[Objects[objnum].instance].objnum < 0)){
903 // return the # of recent collisions
904 return Beams[Objects[objnum].instance].r_collision_count;
907 // stuff collision info, returns 1 on success
908 int beam_get_collision(int objnum, int num, int *collision_objnum, mc_info **cinfo)
911 if((objnum < 0) || (objnum >= MAX_OBJECTS)){
915 if((Objects[objnum].instance < 0) || (Objects[objnum].instance >= MAX_BEAMS)){
919 if((Beams[Objects[objnum].instance].objnum != objnum) || (Beams[Objects[objnum].instance].objnum < 0)){
923 if(num >= Beams[Objects[objnum].instance].r_collision_count){
929 *cinfo = &Beams[Objects[objnum].instance].r_collisions[num].cinfo;
930 *collision_objnum = Beams[Objects[objnum].instance].r_collisions[num].c_objnum;
934 // pause all looping beam sounds
935 void beam_pause_sounds()
939 // set all beam volumes to 0
940 moveup = GET_FIRST(&Beam_used_list);
944 while(moveup != END_OF_LIST(&Beam_used_list)){
945 // set the volume to 0, if he has a looping beam sound
946 if(moveup->beam_sound_loop >= 0){
947 snd_set_volume(moveup->beam_sound_loop, 0.0f);
951 moveup = GET_NEXT(moveup);
955 // unpause looping beam sounds
956 void beam_unpause_sounds()
960 // recalc all beam sounds
961 moveup = GET_FIRST(&Beam_used_list);
965 while(moveup != END_OF_LIST(&Beam_used_list)){
966 beam_recalc_sounds(moveup);
969 moveup = GET_NEXT(moveup);
974 // -----------------------------===========================------------------------------
975 // BEAM MOVEMENT FUNCTIONS
976 // -----------------------------===========================------------------------------
978 // move a type A beam weapon
979 void beam_type_a_move(beam *b)
984 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
985 // get the "originating point" of the beam for this frame. essentially bashes last_start
986 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
988 // if the "warming up" timestamp has not expired
989 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
993 // put the "last_shot" point arbitrarily far away
994 vm_vec_sub(&dir, &b->last_shot, &b->last_start);
995 vm_vec_normalize_quick(&dir);
996 vm_vec_scale_add(&b->last_shot, &b->last_start, &dir, BEAM_FAR_LENGTH);
997 Assert(is_valid_vec(&b->last_shot));
1000 // move a type B beam weapon
1001 #define BEAM_T(b) ( ((b->binfo.delta_ang / b->life_total) * (b->life_total - b->life_left)) / b->binfo.delta_ang )
1002 void beam_type_b_move(beam *b)
1008 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1009 // get the "originating point" of the beam for this frame. essentially bashes last_start
1010 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
1012 // if the "warming up" timestamp has not expired
1013 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1017 // if the two direction vectors are _really_ close together, just use the original direction
1018 dot_save = vm_vec_dot(&b->binfo.dir_a, &b->binfo.dir_b);
1019 if((double)dot_save >= 0.999999999){
1020 actual_dir = b->binfo.dir_a;
1022 // otherwise move towards the dir we calculated when firing this beam
1024 vm_vec_interp_constant(&actual_dir, &b->binfo.dir_a, &b->binfo.dir_b, BEAM_T(b));
1027 // now recalculate shot_point to be shooting through our new point
1028 vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, BEAM_FAR_LENGTH);
1029 int is_valid = is_valid_vec(&b->last_shot);
1032 actual_dir = b->binfo.dir_a;
1033 vm_vec_scale_add(&b->last_shot, &b->last_start, &actual_dir, BEAM_FAR_LENGTH);
1038 void beam_type_c_move(beam *b)
1043 if(b->objp == NULL){
1048 // type c beams only last one frame so we never have to "move" them.
1049 temp = b->targeting_laser_offset;
1050 vm_vec_unrotate(&b->last_start, &temp, &b->objp->orient);
1051 vm_vec_add2(&b->last_start, &b->objp->pos);
1052 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->objp->orient.fvec, BEAM_FAR_LENGTH);
1056 void beam_type_d_move(beam *b)
1058 int shot_index, fire_wait;
1059 vector temp, temp2, dir;
1061 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1062 // get the "originating point" of the beam for this frame. essentially bashes last_start
1063 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &temp2);
1065 // if the "warming up" timestamp has not expired
1066 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1070 // determine what stage of the beam we're in
1071 beam_type_d_get_status(b, &shot_index, &fire_wait);
1073 // if we've changed shot index
1074 if(shot_index != b->shot_index){
1075 // set the new index
1076 b->shot_index = shot_index;
1082 // if we're in the fire wait stage
1083 b->flags &= ~BF_SAFETY;
1085 b->flags |= BF_SAFETY;
1088 // put the "last_shot" point arbitrarily far away
1089 vm_vec_sub(&dir, &b->last_shot, &b->last_start);
1090 vm_vec_normalize_quick(&dir);
1091 vm_vec_scale_add(&b->last_shot, &b->last_start, &dir, BEAM_FAR_LENGTH);
1092 Assert(is_valid_vec(&b->last_shot));
1094 void beam_type_d_get_status(beam *b, int *shot_index, int *fire_wait)
1096 float shot_time = b->life_total / (float)b->binfo.shot_count;
1097 float beam_time = b->life_total - b->life_left;
1099 // determine what "shot" we're on
1100 *shot_index = (int)(beam_time / shot_time);
1101 Assert(*shot_index < b->binfo.shot_count);
1102 if(*shot_index >= b->binfo.shot_count){
1103 *shot_index = b->binfo.shot_count - 1;
1106 // determine if its the firing or waiting section of the shot (fire happens first, THEN wait)
1108 if(beam_time > ((shot_time * (*shot_index)) + (shot_time * 0.5f))){
1114 void beam_type_e_move(beam *b)
1116 vector temp, turret_norm;
1118 // LEAVE THIS HERE OTHERWISE MUZZLE GLOWS DRAW INCORRECTLY WHEN WARMING UP OR DOWN
1119 // get the "originating point" of the beam for this frame. essentially bashes last_start
1120 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &turret_norm, 1, &temp);
1122 // if the "warming up" timestamp has not expired
1123 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
1127 // put the "last_shot" point arbitrarily far away
1128 vm_vec_scale_add(&b->last_shot, &b->last_start, &turret_norm, BEAM_FAR_LENGTH);
1129 Assert(is_valid_vec(&b->last_shot));
1132 // pre-move (before collision checking - but AFTER ALL OTHER OBJECTS HAVE BEEN MOVED)
1133 void beam_move_all_pre()
1138 // zero lights for this frame yet
1139 Beam_light_count = 0;
1141 // traverse through all active beams
1142 moveup = GET_FIRST(&Beam_used_list);
1143 while(moveup != END_OF_LIST(&Beam_used_list)){
1147 // unset collision info
1148 b->f_collision_count = 0;
1150 if(!physics_paused){
1153 // type A beam weapons don't move
1155 beam_type_a_move(b);
1158 // type B beam weapons move across the target somewhat randomly
1160 beam_type_b_move(b);
1163 // type C beam weapons are attached to a fighter - pointing forward
1165 beam_type_c_move(b);
1170 beam_type_d_move(b);
1175 beam_type_e_move(b);
1178 // illegal beam type
1185 moveup = GET_NEXT(moveup);
1189 // post-collision time processing for beams
1190 void beam_move_all_post()
1195 beam_weapon_info *bwi;
1197 // traverse through all active beams
1198 moveup = GET_FIRST(&Beam_used_list);
1199 while(moveup != END_OF_LIST(&Beam_used_list)){
1200 bwi = &Weapon_info[moveup->weapon_info_index].b_info;
1202 // check the status of the beam
1203 bf_status = beam_ok_to_fire(moveup);
1205 // if we're warming up
1206 if(moveup->warmup_stamp != -1){
1207 next_one = GET_NEXT(moveup);
1209 // should we be stopping?
1211 beam_delete(moveup);
1213 // if the warming up timestamp has expired, start firing
1214 if(timestamp_elapsed(moveup->warmup_stamp)){
1216 if(!beam_start_firing(moveup)){
1217 beam_delete(moveup);
1220 // add a muzzle light for the shooter
1221 beam_add_light(moveup, OBJ_INDEX(moveup->objp), 0, NULL);
1229 // if we're warming down
1230 else if(moveup->warmdown_stamp != -1){
1231 next_one = GET_NEXT(moveup);
1233 // should we be stopping?
1235 beam_delete(moveup);
1237 // if we're done warming down, the beam is finished
1238 if(timestamp_elapsed(moveup->warmdown_stamp)){
1239 beam_delete(moveup);
1248 // otherwise, we're firing away.........
1250 // add a muzzle light for the shooter
1251 beam_add_light(moveup, OBJ_INDEX(moveup->objp), 0, NULL);
1253 // subtract out the life left for the beam
1254 if(!physics_paused){
1255 moveup->life_left -= flFrametime;
1258 // if we're past the shrink point, start shrinking the beam
1259 if(moveup->life_left <= (moveup->life_total * bwi->beam_shrink_factor)){
1260 moveup->flags |= BF_SHRINK;
1263 // if we're shrinking the beam
1264 if(moveup->flags & BF_SHRINK){
1265 moveup->shrink -= bwi->beam_shrink_pct * flFrametime;
1266 if(moveup->shrink < 0.1f){
1267 moveup->shrink = 0.1f;
1273 next_one = GET_NEXT(moveup);
1275 // if beam should abruptly stop
1276 if(bf_status == -1){
1277 beam_delete(moveup);
1279 // if the beam should just power down
1281 beam_start_warmdown(moveup);
1289 // increment framecount
1290 moveup->framecount++;
1292 // type c weapons live for one frame only
1293 if(moveup->type == BEAM_TYPE_C){
1294 if(moveup->framecount > 1){
1295 next_one = GET_NEXT(moveup);
1297 beam_delete(moveup);
1302 // done firing, so go into the warmdown phase
1304 if((moveup->life_left <= 0.0f) && (moveup->warmdown_stamp == -1)){
1305 beam_start_warmdown(moveup);
1307 moveup = GET_NEXT(moveup);
1312 // handle any collisions which occured collision (will take care of applying damage to all objects which got hit)
1313 beam_handle_collisions(moveup);
1315 // recalculate beam sounds
1316 beam_recalc_sounds(moveup);
1319 moveup = GET_NEXT(moveup);
1322 // apply all beam lighting
1323 beam_apply_lighting();
1325 // process beam culling info
1328 if(Beam_test_stamp == -1){
1329 Beam_test_stamp = timestamp(BEAM_TEST_STAMP_TIME);
1331 Beam_test_framecount = 0;
1333 if(timestamp_elapsed(Beam_test_stamp)){
1334 // report the results
1335 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));
1338 Beam_test_stamp = timestamp(BEAM_TEST_STAMP_TIME);
1342 Beam_test_framecount = 0;
1344 Beam_test_framecount++;
1351 // -----------------------------===========================------------------------------
1352 // BEAM RENDERING FUNCTIONS
1353 // -----------------------------===========================------------------------------
1355 // render a beam weapon
1356 #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);
1357 #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);
1358 #define P_VERTICES() do { for(idx=0; idx<4; idx++){ g3_project_vertex(verts[idx]); } } while(0);
1360 void beam_render(beam_weapon_info *bwi, vector *start, vector *shot, float shrink)
1363 vertex h1[4]; // halves of a beam section
1364 vertex *verts[4] = { &h1[0], &h1[1], &h1[2], &h1[3] };
1365 vector fvec, top1, bottom1, top2, bottom2;
1368 // bogus weapon info index
1373 // if the beam start and endpoints are the same
1374 if(vm_vec_same(start, shot)){
1378 // get beam direction
1379 vm_vec_sub(&fvec, shot, start);
1380 vm_vec_normalize_quick(&fvec);
1382 // turn off backface culling
1385 // draw all sections
1386 for(s_idx=0; s_idx<bwi->beam_num_sections; s_idx++){
1387 // calculate the beam points
1388 scale = frand_range(1.0f - bwi->sections[s_idx].flicker, 1.0f + bwi->sections[s_idx].flicker);
1389 beam_calc_facing_pts(&top1, &bottom1, &fvec, start, bwi->sections[s_idx].width * scale * shrink, bwi->sections[s_idx].z_add);
1390 beam_calc_facing_pts(&top2, &bottom2, &fvec, shot, bwi->sections[s_idx].width * scale * scale * shrink, bwi->sections[s_idx].z_add);
1391 R_VERTICES(); // rotate and project the vertices
1393 STUFF_VERTICES(); // stuff the beam with creamy goodness (texture coords)
1395 // set the right texture with additive alpha, and draw the poly
1396 gr_set_bitmap(bwi->sections[s_idx].texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.9999f);
1397 g3_draw_poly( 4, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT);
1400 // turn backface culling back on
1404 // generate particles for the muzzle glow
1405 int hack_time = 100;
1408 dc_get_arg(ARG_INT);
1409 hack_time = Dc_arg_int;
1411 void beam_generate_muzzle_particles(beam *b)
1416 vector turret_norm, turret_pos, particle_pos, particle_dir, p_temp;
1418 particle_info pinfo;
1420 // if our hack stamp has expired
1421 if(!((Beam_muzzle_stamp == -1) || timestamp_elapsed(Beam_muzzle_stamp))){
1425 // never generate anything past about 1/5 of the beam fire time
1426 if(b->warmup_stamp == -1){
1431 wip = &Weapon_info[b->weapon_info_index];
1433 // no specified particle for this beam weapon
1434 if(wip->b_info.beam_particle_ani < 0){
1438 // reset the hack stamp
1439 Beam_muzzle_stamp = timestamp(hack_time);
1441 // randomly generate 10 to 20 particles
1442 particle_count = (int)frand_range(0.0f, (float)wip->b_info.beam_particle_count);
1444 // get turret info - position and normal
1445 turret_pos = b->subsys->system_info->pnt;
1446 turret_norm = b->subsys->system_info->turret_norm;
1448 // randomly perturb a vector within a cone around the normal
1449 vm_vector_2_matrix(&m, &turret_norm, NULL, NULL);
1450 for(idx=0; idx<particle_count; idx++){
1451 // get a random point in the cone
1452 vm_vec_random_cone(&particle_dir, &turret_norm, wip->b_info.beam_particle_angle, &m);
1453 p_temp = turret_pos;
1454 vm_vec_scale_add(&p_temp, &turret_pos, &particle_dir, wip->b_info.beam_muzzle_radius * frand_range(0.75f, 0.9f));
1456 // transform into world coords
1457 vm_vec_unrotate(&particle_pos, &p_temp, &b->objp->orient);
1458 vm_vec_add2(&particle_pos, &b->objp->pos);
1459 p_temp = particle_dir;
1460 vm_vec_unrotate(&particle_dir, &p_temp, &b->objp->orient);
1462 // now generate some interesting values for the particle
1463 float p_time_ref = wip->b_info.beam_life + ((float)wip->b_info.beam_warmup / 1000.0f);
1464 float p_life = frand_range(p_time_ref * 0.5f, p_time_ref * 0.7f);
1465 float p_vel = (wip->b_info.beam_muzzle_radius / p_life) * frand_range(0.85f, 1.2f);
1466 vm_vec_scale(&particle_dir, -p_vel);
1468 memset(&pinfo, 0, sizeof(particle_info));
1469 pinfo.pos = particle_pos;
1470 pinfo.vel = particle_dir;
1471 pinfo.lifetime = p_life;
1472 pinfo.attached_objnum = -1;
1473 pinfo.attached_sig = 0;
1474 pinfo.rad = wip->b_info.beam_particle_radius;
1476 pinfo.type = PARTICLE_BITMAP;
1477 pinfo.optional_data = wip->b_info.beam_particle_ani;
1478 pinfo.tracer_length = -1.0f;
1479 particle_create(&pinfo);
1483 // render the muzzle glow for a beam weapon
1484 void beam_render_muzzle_glow(beam *b)
1487 weapon_info *wip = &Weapon_info[b->weapon_info_index];
1488 beam_weapon_info *bwi = &Weapon_info[b->weapon_info_index].b_info;
1489 float pct, rand_val;
1491 // if we don't have a glow bitmap
1492 if(bwi->beam_glow_bitmap < 0){
1496 // if the beam is warming up, scale the glow
1497 if(b->warmup_stamp != -1){
1499 pct = BEAM_WARMUP_PCT(b);
1502 // if the beam is warming down
1503 if(b->warmdown_stamp != -1){
1505 pct = 1.0f - BEAM_WARMDOWN_PCT(b);
1508 // otherwise the beam is really firing
1511 rand_val = frand_range(0.90f, 1.0f);
1515 g3_rotate_vertex(&v, &b->last_start);
1516 gr_set_bitmap( bwi->beam_glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 0.8f * pct);
1518 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * rand_val, TMAP_FLAG_TEXTURED);
1522 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.75f * rand_val, TMAP_FLAG_TEXTURED);
1525 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.45f * rand_val, TMAP_FLAG_TEXTURED);
1528 g3_draw_bitmap(&v, 0, wip->b_info.beam_muzzle_radius * pct * 0.25f * rand_val, TMAP_FLAG_TEXTURED);
1532 // render all beam weapons
1533 void beam_render_all()
1537 // traverse through all active beams
1538 moveup = GET_FIRST(&Beam_used_list);
1539 while(moveup != END_OF_LIST(&Beam_used_list)){
1540 // each beam type renders a little bit differently
1541 if((moveup->warmup_stamp == -1) && (moveup->warmdown_stamp == -1) && !(moveup->flags & BF_SAFETY)){
1542 // HACK - if this is the first frame the beam is firing, don't render it
1543 if(moveup->life_left >= moveup->life_total - 0.0001f){
1545 moveup = GET_NEXT(moveup);
1549 // render the beam itself
1550 Assert(moveup->weapon_info_index >= 0);
1551 if(moveup->weapon_info_index < 0){
1552 moveup = GET_NEXT(moveup);
1555 beam_render(&Weapon_info[moveup->weapon_info_index].b_info, &moveup->last_start, &moveup->last_shot, moveup->shrink);
1558 // render the muzzle glow
1559 beam_render_muzzle_glow(moveup);
1561 // maybe generate some muzzle particles
1562 beam_generate_muzzle_particles(moveup);
1565 moveup = GET_NEXT(moveup);
1569 // output top and bottom vectors
1570 // fvec == forward vector (eye viewpoint basically. in world coords)
1571 // pos == world coordinate of the point we're calculating "around"
1572 // w == width of the diff between top and bottom around pos
1573 void beam_calc_facing_pts( vector *top, vector *bot, vector *fvec, vector *pos, float w, float z_add )
1580 vm_vec_sub( &rvec, &Eye_position, &temp );
1581 vm_vec_normalize( &rvec );
1583 vm_vec_crossprod(&uvec,fvec,&rvec);
1584 vm_vec_normalize(&uvec);
1586 vm_vec_scale_add( top, &temp, &uvec, w/2.0f );
1587 vm_vec_scale_add( bot, &temp, &uvec, -w/2.0f );
1590 // light scale factor
1591 float blight = 25.5f;
1594 dc_get_arg(ARG_FLOAT);
1595 blight = Dc_arg_float;
1598 // call to add a light source to a small object
1599 void beam_add_light_small(beam *bm, object *objp, vector *pt_override = NULL)
1602 beam_weapon_info *bwi;
1615 Assert(objp != NULL);
1619 Assert(bm->weapon_info_index >= 0);
1620 wip = &Weapon_info[bm->weapon_info_index];
1624 noise = frand_range(1.0f - bwi->sections[0].flicker, 1.0f + bwi->sections[0].flicker);
1626 // widest part of the beam
1627 float light_rad = beam_get_widest(bm) * blight * noise;
1629 // nearest point on the beam, and its distance to the ship
1631 if(pt_override == NULL){
1633 vm_vec_dist_to_line(&objp->pos, &bm->last_start, &bm->last_shot, &near_pt, &dist);
1634 if(dist > light_rad){
1638 near_pt = *pt_override;
1641 // average rgb of the beam
1642 float fr = (float)wip->laser_color_1.red / 255.0f;
1643 float fg = (float)wip->laser_color_1.green / 255.0f;
1644 float fb = (float)wip->laser_color_1.blue / 255.0f;
1646 // add a unique light
1647 // noise *= 0.1f; // a little less noise here, since we want the beam to generally cast a bright light
1648 light_add_point_unique(&near_pt, light_rad * 0.0001f, light_rad, 1.0f, fr, fg, fb, OBJ_INDEX(objp));
1651 // call to add a light source to a large object
1652 void beam_add_light_large(beam *bm, object *objp, vector *pt0, vector *pt1)
1655 beam_weapon_info *bwi;
1668 Assert(objp != NULL);
1672 Assert(bm->weapon_info_index >= 0);
1673 wip = &Weapon_info[bm->weapon_info_index];
1677 noise = frand_range(1.0f - bwi->sections[0].flicker, 1.0f + bwi->sections[0].flicker);
1679 // widest part of the beam
1680 float light_rad = beam_get_widest(bm) * blight * noise;
1682 // average rgb of the beam
1683 float fr = (float)wip->laser_color_1.red / 255.0f;
1684 float fg = (float)wip->laser_color_1.green / 255.0f;
1685 float fb = (float)wip->laser_color_1.blue / 255.0f;
1687 // add a unique light
1688 noise *= 0.1f; // a little less noise here, since we want the beam to generally cast a bright light
1689 light_add_tube(pt0, pt1, 1.0f, light_rad, 1.0f * noise, fr, fg, fb, OBJ_INDEX(objp));
1692 // mark an object as being lit
1693 void beam_add_light(beam *b, int objnum, int source, vector *c_point)
1697 // if we're out of light slots!
1698 if(Beam_light_count >= MAX_BEAM_LIGHT_INFO){
1704 l = &Beam_lights[Beam_light_count++];
1707 l->source = (ubyte)source;
1709 // only type 2 lights (from collisions) need a collision point
1710 if(c_point != NULL){
1711 l->c_point = *c_point;
1713 Assert(source != 2);
1720 // apply lighting from any beams
1721 void beam_apply_lighting()
1726 beam_weapon_info *bwi;
1728 // convert all beam lights into real lights
1729 for(idx=0; idx<Beam_light_count; idx++){
1731 l = &Beam_lights[idx];
1734 if((l->objnum < 0) || (l->objnum >= MAX_OBJECTS) || (l->bm == NULL)){
1738 bwi = &Weapon_info[l->bm->weapon_info_index].b_info;
1740 // different light types
1742 // from the muzzle of the gun
1744 // a few meters in from the of muzzle
1745 vm_vec_sub(&dir, &l->bm->last_start, &l->bm->last_shot);
1746 vm_vec_normalize_quick(&dir);
1747 vm_vec_scale_add(&pt, &l->bm->last_start, &dir, bwi->beam_muzzle_radius * 5.0f);
1749 beam_add_light_small(l->bm, &Objects[l->objnum], &pt);
1752 // from the beam passing by
1755 switch(Objects[l->objnum].type){
1757 Assert(Objects[l->objnum].instance >= 0);
1760 if(Ship_info[Ships[Objects[l->objnum].instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)){
1761 beam_add_light_large(l->bm, &Objects[l->objnum], &l->bm->last_start, &l->bm->last_shot);
1765 beam_add_light_small(l->bm, &Objects[l->objnum]);
1769 // asteroids get small lights
1771 beam_add_light_small(l->bm, &Objects[l->objnum]);
1774 // debris gets small lights
1776 beam_add_light_small(l->bm, &Objects[l->objnum]);
1783 // a few meters from the collision point
1784 vm_vec_sub(&dir, &l->bm->last_start, &l->c_point);
1785 vm_vec_normalize_quick(&dir);
1786 vm_vec_scale_add(&pt, &l->c_point, &dir, bwi->beam_muzzle_radius * 5.0f);
1788 beam_add_light_small(l->bm, &Objects[l->objnum], &pt);
1794 // -----------------------------===========================------------------------------
1795 // BEAM BOOKKEEPING FUNCTIONS
1796 // -----------------------------===========================------------------------------
1799 void beam_delete(beam *b)
1801 // remove from active list and put on free list
1802 list_remove(&Beam_used_list, b);
1803 list_append(&Beam_free_list, b);
1805 // delete our associated object
1806 // Assert(b->objnum >= 0);
1808 obj_delete(b->objnum);
1812 // kill the beam looping sound
1813 if(b->beam_sound_loop != -1){
1814 snd_stop(b->beam_sound_loop);
1815 b->beam_sound_loop = -1;
1820 Assert(Beam_count >= 0);
1821 nprintf(("General", "Recycled beam (%d beams remaining)\n", Beam_count));
1824 // given an object, return its model num
1825 int beam_get_model(object *objp)
1828 Assert(objp->instance >= 0);
1829 if(objp->instance < 0){
1836 return Ships[objp->instance].modelnum;
1839 Assert(Weapons[objp->instance].weapon_info_index >= 0);
1840 if(Weapons[objp->instance].weapon_info_index < 0){
1843 return Weapon_info[Weapons[objp->instance].weapon_info_index].model_num;
1846 Assert(Debris[objp->instance].is_hull);
1847 if(!Debris[objp->instance].is_hull){
1850 return Debris[objp->instance].model_num;
1854 subtype = Asteroids[objp->instance].asteroid_subtype;
1855 Assert(Asteroids[objp->instance].type >= 0);
1856 if(Asteroids[objp->instance].type < 0){
1859 return Asteroid_info[Asteroids[objp->instance].type].model_num[subtype];
1863 // this shouldn't happen too often
1864 mprintf(("Beam couldn't find a good find a good object model/type!! (%d)", objp->type));
1873 // start the warmup phase for the beam
1874 void beam_start_warmup(beam *b)
1876 // set the warmup stamp
1877 b->warmup_stamp = timestamp(Weapon_info[b->weapon_info_index].b_info.beam_warmup);
1879 // start playing warmup sound
1880 if(!(Game_mode & GM_STANDALONE_SERVER) && (Weapon_info[b->weapon_info_index].b_info.beam_warmup_sound >= 0)){
1881 snd_play_3d(&Snds[Weapon_info[b->weapon_info_index].b_info.beam_warmup_sound], &b->last_start, &View_position);
1885 // start the firing phase for the beam, return 0 if the beam failed to start, and should be deleted altogether
1886 int beam_start_firing(beam *b)
1888 // kill the warmup stamp so the rest of the code knows its firing
1889 b->warmup_stamp = -1;
1891 // any special stuff for each weapon type
1893 // re-aim type A and D beam weapons here, otherwise they tend to miss
1912 // determine if we can legitimately start firing, or if we need to take other action
1913 switch(beam_ok_to_fire(b)){
1918 beam_start_warmdown(b);
1922 // start the beam firing sound now, if we haven't already
1923 if((b->beam_sound_loop == -1) && (Weapon_info[b->weapon_info_index].b_info.beam_loop_sound >= 0)){
1924 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);
1927 snd_play_3d(&Snds[SND_BEAM_SHOT], &b->last_start, &View_position);
1934 // start the warmdown phase for the beam
1935 void beam_start_warmdown(beam *b)
1938 b->warmdown_stamp = timestamp(Weapon_info[b->weapon_info_index].b_info.beam_warmdown);
1940 // start the warmdown sound
1941 if(Weapon_info[b->weapon_info_index].b_info.beam_warmdown_sound >= 0){
1942 snd_play_3d(&Snds[Weapon_info[b->weapon_info_index].b_info.beam_warmdown_sound], &b->last_start, &View_position);
1945 // kill the beam looping sound
1946 if(b->beam_sound_loop != -1){
1947 snd_stop(b->beam_sound_loop);
1948 b->beam_sound_loop = -1;
1952 // recalculate beam sounds (looping sounds relative to the player)
1953 void beam_recalc_sounds(beam *b)
1955 beam_weapon_info *bwi;
1958 Assert(b->weapon_info_index >= 0);
1959 if(b->weapon_info_index < 0){
1962 bwi = &Weapon_info[b->weapon_info_index].b_info;
1964 // update the sound position relative to the player
1965 if(b->beam_sound_loop != -1){
1966 // get the point closest to the player's viewing position
1967 switch(vm_vec_dist_to_line(&View_position, &b->last_start, &b->last_shot, &pos, NULL)){
1968 // behind the beam, so use the start pos
1970 pos = b->last_start;
1973 // use the closest point
1975 // already calculated in vm_vec_dist_to_line(...)
1978 // past the beam, so use the shot pos
1984 snd_update_3d_pos(b->beam_sound_loop, &Snds[bwi->beam_loop_sound], &pos);
1989 // -----------------------------===========================------------------------------
1990 // BEAM AIMING FUNCTIONS
1991 // -----------------------------===========================------------------------------
1994 void beam_get_binfo(beam *b, float accuracy, int num_shots)
1999 vector turret_point, turret_norm;
2000 beam_weapon_info *bwi;
2003 // where the shot is originating from (b->last_start gets filled in)
2004 ship_get_global_turret_gun_info(b->objp, b->subsys, &turret_point, &turret_norm, 1, &p2);
2006 // get a model # to work with
2007 model_num = beam_get_model(b->target);
2012 // get beam weapon info
2013 Assert(b->weapon_info_index >= 0);
2014 if(b->weapon_info_index < 0){
2017 bwi = &Weapon_info[b->weapon_info_index].b_info;
2020 skill_level = Game_skill_level;
2021 if(Game_skill_level >= NUM_SKILL_LEVELS){
2022 skill_level = NUM_SKILL_LEVELS - 1;
2024 if(Game_skill_level < 0){
2028 // stuff num shots even though its only used for type D weapons
2029 b->binfo.shot_count = (ubyte)num_shots;
2030 if(b->binfo.shot_count > MAX_BEAM_SHOTS){
2031 b->binfo.shot_count = MAX_BEAM_SHOTS;
2034 // generate the proper amount of directional vectors
2036 // pick an accuracy. beam will be properly aimed at actual fire time
2038 // all we will do is decide whether or not we will hit - type A beam weapons are re-aimed immediately before firing
2039 b->binfo.shot_aim[0] = frand_range(0.0f, 1.0f + bwi->beam_miss_factor[skill_level] * accuracy);
2040 b->binfo.shot_count = 1;
2042 // get random model points, this is useful for big ships, because we never miss when shooting at them
2043 submodel_get_two_random_points(model_num, 0, &b->binfo.dir_a, &b->binfo.dir_b);
2046 // just 2 points in the "slash"
2048 beam_get_octant_points(model_num, b->target, (int)frand_range(0.0f, BEAM_NUM_GOOD_OCTANTS), Beam_good_slash_octants, &oct1, &oct2);
2051 vm_vec_sub(&b->binfo.dir_a, &oct1, &turret_point);
2052 vm_vec_normalize(&b->binfo.dir_a);
2055 vm_vec_sub(&b->binfo.dir_b, &oct2, &turret_point);
2056 vm_vec_normalize(&b->binfo.dir_b);
2059 b->binfo.delta_ang = fl_abs(vm_vec_delta_ang_norm(&b->binfo.dir_a, &b->binfo.dir_b, NULL));
2062 // nothing for this beam - its very special case
2066 // type D beams fire at small ship multiple times
2068 // get a bunch of shot aims
2069 for(idx=0; idx<b->binfo.shot_count; idx++){
2070 // MK, 9/3/99: Added pow() function to make increasingly likely to miss with subsequent shots. 30% more likely with each shot.
2071 float r = ((float) pow(1.3f, idx)) * bwi->beam_miss_factor[skill_level] * accuracy;
2072 b->binfo.shot_aim[idx] = frand_range(0.0f, 1.0f + r);
2076 // type e beams just fire straight
2078 b->binfo.shot_aim[0] = 0.0000001f;
2079 b->binfo.shot_count = 1;
2080 b->binfo.dir_a = turret_norm;
2081 b->binfo.dir_b = turret_norm;
2089 // aim the beam (setup last_start and last_shot - the endpoints). also recalculates collision pairs
2090 void beam_aim(beam *b)
2095 // type C beam weapons have no target
2096 if(b->target == NULL){
2097 Assert(b->type == BEAM_TYPE_C);
2098 if(b->type != BEAM_TYPE_C){
2102 // get a model # to work with
2104 model_num = beam_get_model(b->target);
2110 // setup our initial shot point and aim direction
2113 // where the shot is originating from (b->last_start gets filled in)
2114 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2116 // if we're targeting a subsystem - shoot directly at it
2117 if(b->target_subsys != NULL){
2118 // unrotate the center of the subsystem
2119 vm_vec_unrotate(&b->last_shot, &b->target_subsys->system_info->pnt, &b->target->orient);
2120 vm_vec_add2(&b->last_shot, &b->target->pos);
2121 vm_vec_sub(&temp, &b->last_shot, &b->last_start);
2123 vm_vec_scale_add(&b->last_shot, &b->last_start, &temp, 2.0f);
2127 // if we're shooting at a big ship - shoot directly at the model
2128 if((b->target->type == OBJ_SHIP) && (Ship_info[Ships[b->target->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP))){
2129 // rotate into world coords
2130 vm_vec_unrotate(&temp, &b->binfo.dir_a, &b->target->orient);
2131 vm_vec_add2(&temp, &b->target->pos);
2133 // get the shot point
2134 vm_vec_sub(&p2, &temp, &b->last_start);
2135 vm_vec_scale_add(&b->last_shot, &b->last_start, &p2, 2.0f);
2139 // point at the center of the target, then jitter based on shot_aim
2140 b->last_shot = b->target->pos;
2141 beam_jitter_aim(b, b->binfo.shot_aim[0]);
2145 // where the shot is originating from (b->last_start gets filled in)
2146 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2148 // set the shot point
2149 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->binfo.dir_a, BEAM_FAR_LENGTH);
2150 Assert(is_valid_vec(&b->last_shot));
2155 temp = b->targeting_laser_offset;
2156 vm_vec_unrotate(&b->last_start, &temp, &b->objp->orient);
2157 vm_vec_add2(&b->last_start, &b->objp->pos);
2158 vm_vec_scale_add(&b->last_shot, &b->last_start, &b->objp->orient.fvec, BEAM_FAR_LENGTH);
2162 // where the shot is originating from (b->last_start gets filled in)
2163 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2165 // point at the center of the target, then jitter based on shot_aim
2166 b->last_shot = b->target->pos;
2167 beam_jitter_aim(b, b->binfo.shot_aim[b->shot_index]);
2168 nprintf(("AI", "Frame %i: FIRING\n", Framecount));
2172 // where the shot is originating from (b->last_start gets filled in)
2173 ship_get_global_turret_gun_info(b->objp, b->subsys, &b->last_start, &temp, 1, &p2);
2175 // point directly in the direction of the turret
2176 vm_vec_scale_add(&b->last_shot, &b->last_start, &temp, BEAM_FAR_LENGTH);
2183 // recalculate object pairs
2184 OBJ_RECALC_PAIRS((&Objects[b->objnum]));
2187 // given a model #, and an object, stuff 2 good world coord points
2188 void beam_get_octant_points(int modelnum, object *objp, int oct_index, int oct_array[BEAM_NUM_GOOD_OCTANTS][4], vector *v1, vector *v2)
2190 vector t1, t2, temp;
2191 polymodel *m = model_get(modelnum);
2193 // bad bad bad bad bad bad
2199 Assert((oct_index >= 0) && (oct_index < BEAM_NUM_GOOD_OCTANTS));
2201 // randomly pick octants
2202 t1 = oct_array[oct_index][2] ? m->octants[oct_array[oct_index][0]].max : m->octants[oct_array[oct_index][0]].min;
2203 t2 = oct_array[oct_index][3] ? m->octants[oct_array[oct_index][1]].max : m->octants[oct_array[oct_index][1]].min;
2204 Assert(!vm_vec_same(&t1, &t2));
2206 // get them in world coords
2207 vm_vec_unrotate(&temp, &t1, &objp->orient);
2208 vm_vec_add(v1, &temp, &objp->pos);
2209 vm_vec_unrotate(&temp, &t2, &objp->orient);
2210 vm_vec_add(v2, &temp, &objp->pos);
2213 // throw some jitter into the aim - based upon shot_aim
2214 void beam_jitter_aim(beam *b, float aim)
2216 vector forward, circle;
2218 float subsys_strength;
2220 // if the weapons subsystem is damaged or destroyed
2221 if((b->objp != NULL) && (b->objp->signature == b->sig) && (b->objp->type == OBJ_SHIP) && (b->objp->instance >= 0) && (b->objp->instance < MAX_SHIPS)){
2222 // get subsytem strength
2223 subsys_strength = ship_get_subsystem_strength(&Ships[b->objp->instance], SUBSYSTEM_WEAPONS);
2225 // when subsytem strength is 0, double the aim error factor
2226 aim += aim * (1.0f - subsys_strength);
2229 // shot aim is a direct linear factor of the target model's radius.
2230 // so, pick a random point on the circle
2231 vm_vec_sub(&forward, &b->last_shot, &b->last_start);
2232 vm_vec_normalize_quick(&forward);
2235 vm_vector_2_matrix(&m, &forward, NULL, NULL);
2237 // get a vector on the circle - this should appear to be pretty random
2238 // vm_vec_scale_add(&circle, &b->last_shot, &m.rvec, aim * b->target->radius);
2239 vm_vec_random_in_circle(&circle, &b->last_shot, &m, aim * b->target->radius, 0);
2241 // get the vector pointing to the circle point
2242 vm_vec_sub(&forward, &circle, &b->last_start);
2243 vm_vec_scale_add(&b->last_shot, &b->last_start, &forward, 2.0f);
2247 // -----------------------------===========================------------------------------
2248 // BEAM COLLISION FUNCTIONS
2249 // -----------------------------===========================------------------------------
2251 // collide a beam with a ship, returns 1 if we can ignore all future collisions between the 2 objects
2252 int beam_collide_ship(obj_pair *pair)
2257 mc_info test_collide;
2267 Assert(pair->a->instance >= 0);
2268 Assert(pair->a->type == OBJ_BEAM);
2269 Assert(Beams[pair->a->instance].objnum == OBJ_INDEX(pair->a));
2270 b = &Beams[pair->a->instance];
2272 // Don't check collisions for warping out player if past stage 1.
2273 if ( Player->control_mode >= PCM_WARPOUT_STAGE1) {
2274 if ( pair->a == Player_obj ) return 0;
2275 if ( pair->b == Player_obj ) return 0;
2278 // if the "warming up" timestamp has not expired
2279 if((b->warmup_stamp != -1) || (b->warmdown_stamp != -1)){
2283 // if the beam is on "safety", don't collide with anything
2284 if(b->flags & BF_SAFETY){
2288 // if the colliding object is the shooting object, return 1 so this is culled
2289 if(pair->b == b->objp){
2293 // try and get a model
2294 model_num = beam_get_model(pair->b);
2306 Assert(pair->b->type == OBJ_SHIP);
2307 Assert(pair->b->instance >= 0);
2308 if((pair->b->type != OBJ_SHIP) || (pair->b->instance < 0)){
2311 shipp = &Ships[pair->b->instance];
2312 sip = &Ship_info[shipp->ship_info_index];
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 Assert(pair->a->instance >= 0);
2363 Assert(pair->a->type == OBJ_BEAM);
2364 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 Assert(pair->a->instance >= 0);
2433 Assert(pair->a->type == OBJ_BEAM);
2434 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 Assert(pair->a->instance >= 0);
2499 Assert(pair->a->type == OBJ_BEAM);
2500 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 Assert(a->instance >= 0);
2564 if(a->instance < 0){
2567 Assert(a->type == OBJ_BEAM);
2568 if(a->type != OBJ_BEAM){
2571 Assert(Beams[a->instance].objnum == OBJ_INDEX(a));
2572 if(Beams[a->instance].objnum != OBJ_INDEX(a)){
2575 bm = &Beams[a->instance];
2576 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];
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;
2686 beam_weapon_info *bwi;
2690 // early out if we had no collisions
2691 if(b->f_collision_count <= 0){
2695 // get beam weapon info
2696 if((b->weapon_info_index < 0) || (b->weapon_info_index >= Num_weapon_types)){
2700 bwi = &Weapon_info[b->weapon_info_index].b_info;
2701 wi = &Weapon_info[b->weapon_info_index];
2703 // get the widest part of the beam
2704 widest = beam_get_widest(b);
2706 // the first thing we need to do is sort the collisions, from closest to farthest
2707 qsort(b->f_collisions, b->f_collision_count, sizeof(beam_collision), beam_sort_collisions_func);
2709 // now apply all collisions until we reach a ship which "stops" the beam or we reach the end of the list
2710 for(idx=0; idx<b->f_collision_count; idx++){
2714 int target = b->f_collisions[idx].c_objnum;
2716 // if we have an invalid object
2717 if((target < 0) || (target >= MAX_OBJECTS)){
2721 // try and get a model to deal with
2722 model_num = beam_get_model(&Objects[target]);
2728 beam_add_light(b, target, 2, &b->f_collisions[idx].cinfo.hit_point_world);
2730 // add to the recent collision list
2731 r_coll[r_coll_count].c_objnum = target;
2732 r_coll[r_coll_count].c_sig = Objects[target].signature;
2733 r_coll[r_coll_count].c_stamp = -1;
2734 r_coll[r_coll_count].cinfo = b->f_collisions[idx].cinfo;
2736 // if he was already on the recent collision list, copy his timestamp
2737 // also, be sure not to play the impact sound again.
2738 for(s_idx=0; s_idx<b->r_collision_count; s_idx++){
2739 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)){
2741 r_coll[r_coll_count].c_stamp = b->r_collisions[s_idx].c_stamp;
2743 // don't play the impact sound again
2748 // if the damage timestamp has expired or is not set yet, apply damage
2749 if((r_coll[r_coll_count].c_stamp == -1) || timestamp_elapsed(r_coll[r_coll_count].c_stamp)){
2751 r_coll[r_coll_count].c_stamp = timestamp(BEAM_DAMAGE_TIME);
2754 // if no damage - don't even indicate it has been hit
2755 if(wi->damage <= 0){
2759 // increment collision count
2762 // play the impact sound
2764 snd_play_3d( &Snds[wi->impact_snd], &b->f_collisions[idx].cinfo.hit_point_world, &Eye_position );
2768 if(do_damage && !physics_paused){
2769 // maybe draw an explosion
2770 if(wi->impact_weapon_expl_index >= 0){
2771 int ani_handle = weapon_get_expl_handle(wi->impact_weapon_expl_index, &b->f_collisions[idx].cinfo.hit_point_world, wi->impact_explosion_radius);
2772 particle_create( &b->f_collisions[idx].cinfo.hit_point_world, &vmd_zero_vector, 0.0f, wi->impact_explosion_radius, PARTICLE_BITMAP_PERSISTENT, ani_handle );
2775 switch(Objects[target].type){
2777 // hit the debris - the debris hit code takes care of checking for MULTIPLAYER_CLIENT, etc
2778 debris_hit(&Objects[target], &Objects[b->objnum], &b->f_collisions[idx].cinfo.hit_point_world, Weapon_info[b->weapon_info_index].damage);
2782 // detonate the missile
2783 Assert(Weapon_info[Weapons[Objects[target].instance].weapon_info_index].subtype == WP_MISSILE);
2784 if(!(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_MASTER){
2785 weapon_hit(&Objects[target], NULL, &Objects[target].pos);
2791 if(!(Game_mode & GM_MULTIPLAYER) || MULTIPLAYER_MASTER){
2792 asteroid_hit(&Objects[target], &Objects[b->objnum], &b->f_collisions[idx].cinfo.hit_point_world, Weapon_info[b->weapon_info_index].damage);
2797 // hit the ship - again, the innards of this code handle multiplayer cases
2798 // maybe vaporize ship.
2799 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);
2801 // if this is the first hit on the player ship. whack him
2803 beam_apply_whack(b, &Objects[target], &b->f_collisions[idx].cinfo.hit_point_world);
2809 // if the radius of the target is somewhat close to the radius of the beam, "stop" the beam here
2810 // for now : if its smaller than about 1/3 the radius of the ship
2811 if(widest <= (Objects[target].radius * BEAM_AREA_PERCENT) && !beam_will_tool_target(b, &Objects[target])){
2812 // set last_shot so we know where to properly draw the beam
2813 b->last_shot = b->f_collisions[idx].cinfo.hit_point_world;
2814 Assert(is_valid_vec(&b->last_shot));
2816 // done wif the beam
2821 // store the new recent collisions
2822 for(idx=0; idx<r_coll_count; idx++){
2823 b->r_collisions[idx] = r_coll[idx];
2825 b->r_collision_count = r_coll_count;
2828 // for a given object, and a firing beam, determine its critical dot product and range
2829 void beam_get_cull_vals(object *objp, beam *b, float *cull_dot, float *cull_dist)
2832 // debris and asteroids are classified as slow moving small objects
2833 // use cull_dot == potential cone of beam + 10% and 50.0 meters
2836 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.10f);
2837 *cull_dist = 50.0f * 50.0f;
2840 // treat missiles as fast-moving small objects
2842 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.5f);
2843 *cull_dist = 300.0f * 300.0f;
2847 // for cap ships, only cull for 90deg or better
2849 if(Ship_info[Ships[objp->instance].ship_info_index].flags & SIF_CAPITAL){
2856 // for large ships, cull at some multiple of the radius
2857 if(Ship_info[Ships[objp->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)){
2858 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.25f);
2860 *cull_dist = (objp->radius * 1.3f) * (objp->radius * 1.3f);
2864 // for everthing else, cull the same as missiles
2865 *cull_dot = 1.0f - ((1.0f - beam_get_cone_dot(b)) * 1.5f);
2866 *cull_dist = 300.0f * 300.0f;
2870 // BAD BAD BAD - but this code will cause everything to cull properly
2877 // FIXME - make sure we are truthfull representing the "cone" for all beam types
2878 // get the total possible cone for a given beam in radians
2879 float beam_get_cone_dot(beam *b)
2886 // even though these beams don't move, return a _very_ small value
2887 return (float)cos(fl_radian(50.5f));
2890 return vm_vec_dotprod(&b->binfo.dir_a, &b->binfo.dir_b);
2900 // if it is legal for the beam to fire, or continue firing
2901 int beam_ok_to_fire(beam *b)
2903 // type C beams are ok to fire all the time
2904 if(Weapon_info[b->weapon_info_index].b_info.beam_type == BEAM_TYPE_C){
2908 // if my own object is invalid, stop firing
2909 if(b->objp->signature != b->sig){
2910 mprintf(("BEAM : killing beam because of invalid parent object SIGNATURE!\n"));
2914 // if my own object is a ghost
2915 if(b->objp->type != OBJ_SHIP){
2916 mprintf(("BEAM : killing beam because of invalid parent object TYPE!\n"));
2920 // if the shooting turret is destroyed
2921 if(b->subsys->current_hits <= 0.0f){
2922 mprintf(("BEAM : killing beam because turret has been destroyed!\n"));
2926 // if the beam will be firing out of its FOV, power it down
2927 vector aim_dir, temp;
2928 vector turret_dir, turret_pos;
2929 vm_vec_sub(&aim_dir, &b->last_shot, &b->last_start);
2930 vm_vec_normalize(&aim_dir);
2931 ship_get_global_turret_gun_info(b->objp, b->subsys, &turret_pos, &turret_dir, 1, &temp);
2932 if(vm_vec_dotprod(&aim_dir, &turret_dir) < b->subsys->system_info->turret_fov){
2933 mprintf(("BEAM : powering beam down because of FOV condition!\n"));
2937 // ok to fire/continue firing
2941 // get the width of the widest section of the beam
2942 float beam_get_widest(beam *b)
2945 float widest = -1.0f;
2948 Assert(b->weapon_info_index >= 0);
2949 if(b->weapon_info_index < 0){
2954 for(idx=0; idx<Weapon_info[b->weapon_info_index].b_info.beam_num_sections; idx++){
2955 if(Weapon_info[b->weapon_info_index].b_info.sections[idx].width > widest){
2956 widest = Weapon_info[b->weapon_info_index].b_info.sections[idx].width;
2961 return widest * b->shrink;
2964 // apply a whack to a ship
2965 void beam_apply_whack(beam *b, object *objp, vector *hit_point)
2971 Assert((b != NULL) && (objp != NULL) && (hit_point != NULL));
2972 if((b == NULL) || (objp == NULL) || (hit_point == NULL)){
2975 Assert(b->weapon_info_index >= 0);
2976 wip = &Weapon_info[b->weapon_info_index];
2977 Assert((objp != NULL) && (objp->type == OBJ_SHIP) && (objp->instance >= 0) && (objp->instance < MAX_SHIPS));
2978 if((objp == NULL) || (objp->type != OBJ_SHIP) || (objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
2981 shipp = &Ships[objp->instance];
2982 if((shipp->ai_index < 0) || (shipp->ai_index >= MAX_AI_INFO)){
2986 // don't whack docking ships
2987 if(Ai_info[shipp->ai_index].ai_flags & AIF_DOCKED){
2991 // determine how big of a whack to apply
2995 if(wip->damage < b_whack_damage){
2996 whack = b_whack_small;
2998 whack = b_whack_big;
3002 vector whack_dir, temp;
3003 vm_vec_dist_to_line(&objp->pos, &b->last_start, &b->last_shot, &temp, &dist);
3004 vm_vec_sub(&whack_dir, &objp->pos, &temp);
3005 vm_vec_normalize(&whack_dir);
3006 vm_vec_scale(&whack_dir, whack);
3009 ship_apply_whack(&whack_dir, hit_point, objp);
3012 // return the amount of damage which should be applied to a ship. basically, filters friendly fire damage
3013 float beam_get_ship_damage(beam *b, object *objp)
3015 // if the beam is on the same team as the object
3016 Assert((objp != NULL) && (b != NULL));
3017 if((objp == NULL) || (b == NULL)){
3020 Assert((objp->type == OBJ_SHIP) && (objp->instance >= 0) && (objp->instance < MAX_SHIPS));
3021 if((objp->type != OBJ_SHIP) || (objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
3026 if((b->team == Ships[objp->instance].team) && (Weapon_info[b->weapon_info_index].damage > Beam_friendly_cap[Game_skill_level])){
3027 return Beam_friendly_cap[Game_skill_level];
3031 return Weapon_info[b->weapon_info_index].damage;
3034 // if the beam is likely to tool a given target before its lifetime expires
3035 int beam_will_tool_target(beam *b, object *objp)
3037 weapon_info *wip = &Weapon_info[b->weapon_info_index];
3038 float damage_in_a_few_seconds;
3045 // if the object is not a ship, bail
3046 if(objp->type != OBJ_SHIP){
3049 if((objp->instance < 0) || (objp->instance >= MAX_SHIPS)){
3053 // if the beam is going to apply more damage in about 1 and a half than the hull of the ship can take
3054 damage_in_a_few_seconds = (TOOLTIME / (float)BEAM_DAMAGE_TIME) * wip->damage;
3055 if(objp->hull_strength < damage_in_a_few_seconds){
3064 float beam_accuracy = 1.0f;
3067 dc_get_arg(ARG_FLOAT);
3068 beam_accuracy = Dc_arg_float;
3075 for(idx=0; idx<Num_weapon_types; idx++){
3076 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3078 dc_printf("Beam %d : %s\n", b_count, Weapon_info[idx].name);
3082 void beam_test(int whee)
3085 object *orion, *fenris;
3086 ship_subsys *orion_turret, *fenris_turret, *fenris_radar, *orion_radar, *lookup;
3089 nprintf(("General", "Running beam test\n"));
3091 // lookup some stuff
3092 s1 = ship_name_lookup("GTD Orion 1");
3094 orion = &Objects[Ships[s1].objnum];
3095 s2 = ship_name_lookup("GTC Fenris 2");
3097 fenris = &Objects[Ships[s2].objnum];
3100 lookup = GET_FIRST(&Ships[s1].subsys_list);
3101 orion_turret = NULL;
3103 while(lookup != END_OF_LIST(&Ships[s1].subsys_list)){
3105 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret07")){
3106 orion_turret = lookup;
3110 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3111 orion_radar = lookup;
3114 lookup = GET_NEXT(lookup);
3116 Assert(orion_turret != NULL);
3117 Assert(orion_radar != NULL);
3118 lookup = GET_FIRST(&Ships[s2].subsys_list);
3119 fenris_turret = NULL;
3120 fenris_radar = NULL;
3121 while(lookup != END_OF_LIST(&Ships[s2].subsys_list)){
3123 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret07")){
3124 fenris_turret = lookup;
3128 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3129 fenris_radar = lookup;
3132 lookup = GET_NEXT(lookup);
3134 Assert(fenris_turret != NULL);
3135 Assert(fenris_radar != NULL);
3137 memset(&f, 0, sizeof(beam_fire_info));
3138 f.accuracy = beam_accuracy;
3139 f.beam_info_index = -1;
3140 f.beam_info_override = NULL;
3143 f.target_subsys = fenris_turret;
3144 f.turret = orion_turret;
3146 // find the first beam
3148 int beam_first = -1;
3151 for(idx=0; idx<Num_weapon_types; idx++){
3152 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3164 // maybe fire it, if its valid
3165 f.beam_info_index = beam_first + whee - 1;
3166 if(Weapon_info[f.beam_info_index].wi_flags & WIF_BEAM){
3167 HUD_printf("Firing %s\n", Weapon_info[f.beam_info_index].name);
3172 void beam_test_new(int whee)
3175 object *orion, *fenris, *herc2, *herc3, *herc6, *alpha;
3176 ship_subsys *orion_turret, *fenris_turret, *fenris_radar, *orion_radar, *lookup;
3179 nprintf(("General", "Running beam test\n"));
3181 // lookup some stuff
3182 s1 = ship_name_lookup("GTD Orion 1");
3184 orion = &Objects[Ships[s1].objnum];
3185 s2 = ship_name_lookup("GTC Fenris 2");
3187 fenris = &Objects[Ships[s2].objnum];
3188 s3 = ship_name_lookup("GTF Hercules 2");
3190 herc2 = &Objects[Ships[s3].objnum];
3191 s3 = ship_name_lookup("GTF Hercules 3");
3193 herc3 = &Objects[Ships[s3].objnum];
3194 s3 = ship_name_lookup("GTF Hercules 6");
3196 herc6 = &Objects[Ships[s3].objnum];
3197 s3 = ship_name_lookup("Alpha 1");
3199 alpha = &Objects[Ships[s3].objnum];
3202 lookup = GET_FIRST(&Ships[s1].subsys_list);
3203 orion_turret = NULL;
3205 while(lookup != END_OF_LIST(&Ships[s1].subsys_list)){
3207 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret07")){
3208 orion_turret = lookup;
3212 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3213 orion_radar = lookup;
3216 lookup = GET_NEXT(lookup);
3218 Assert(orion_turret != NULL);
3219 Assert(orion_radar != NULL);
3220 lookup = GET_FIRST(&Ships[s2].subsys_list);
3221 fenris_turret = NULL;
3222 fenris_radar = NULL;
3223 while(lookup != END_OF_LIST(&Ships[s2].subsys_list)){
3225 if((lookup->system_info->type == SUBSYSTEM_TURRET) && !stricmp(lookup->system_info->subobj_name, "turret03")){
3226 fenris_turret = lookup;
3230 if(lookup->system_info->type == SUBSYSTEM_RADAR){
3231 fenris_radar = lookup;
3234 lookup = GET_NEXT(lookup);
3236 Assert(fenris_turret != NULL);
3237 Assert(fenris_radar != NULL);
3239 memset(&f, 0, sizeof(beam_fire_info));
3240 f.accuracy = beam_accuracy;
3241 f.beam_info_override = NULL;
3244 f.target_subsys = NULL;
3245 f.turret = fenris_turret;
3248 // find the first beam
3250 int beam_first = -1;
3253 for(idx=0; idx<Num_weapon_types; idx++){
3254 if(Weapon_info[idx].wi_flags & WIF_BEAM){
3266 // maybe fire it, if its valid
3267 f.beam_info_index = beam_first + whee - 1;
3268 if(Weapon_info[f.beam_info_index].wi_flags & WIF_BEAM){
3269 HUD_printf("Firing %s\n", Weapon_info[f.beam_info_index].name);