2 * $Logfile: /Freespace2/code/Weapon/Swarm.cpp $
7 * C module for managing swarm missiles
10 * Revision 1.1 2002/05/03 03:28:11 root
14 * 8 5/20/99 7:00p Dave
15 * Added alternate type names for ships. Changed swarm missile table
18 * 7 2/25/99 4:19p Dave
19 * Added multiplayer_beta defines. Added cd_check define. Fixed a few
20 * release build warnings. Added more data to the squad war request and
23 * 6 1/30/99 1:46p Andsager
24 * Clean up code. Remove int3, replace warning.
26 * 5 1/29/99 2:47p Andsager
27 * Put asset back in for turret_swarm_missiles
29 * 4 1/29/99 2:25p Andsager
30 * Added turret_swarm_missiles
32 * 3 11/05/98 5:55p Dave
33 * Big pass at reducing #includes
35 * 2 10/07/98 10:54a Dave
38 * 1 10/07/98 10:51a Dave
40 * 14 5/03/98 5:52p Chad
41 * AL: Fix bug with WIF_SWARM flag getting cleared from weapon info
43 * 13 3/31/98 5:19p John
44 * Removed demo/save/restore. Made NDEBUG defined compile. Removed a
45 * bunch of debug stuff out of player file. Made model code be able to
46 * unload models and malloc out only however many models are needed.
49 * 12 2/26/98 10:08p Hoffoss
50 * Rewrote state saving and restoring to fix bugs and simplify the code.
52 * 11 2/13/98 3:36p Jim
53 * AL: Fix problem with swarm missiles that occurred at low framerates
55 * 10 1/14/98 3:02p Sandeep
56 * Fix bug where parent ship dies in the same frame where swarm missiles
59 * 9 11/06/97 12:27a Mike
60 * Better avoid behavior.
61 * Modify ai_turn_towards_vector() to take a flag parameter.
63 * 8 11/03/97 9:42p Lawrance
64 * make swarm paths more random, fix bug with wild swarmers after homing
67 * 7 8/24/97 10:26p Lawrance
68 * implemented homing swarm missiles
70 * 6 8/14/97 12:27a Mike
71 * Changed ai_turn_towards_vector() to take bank_override, had to change
72 * numerous calls to it.
74 * 5 8/13/97 4:45p Allender
75 * fixed ship_fire_primary and fire_secondary to not take parameter for
76 * determining whether to count ammo or not. Fixed countermeasure firing
79 * 4 8/13/97 12:06p Lawrance
80 * make swarm bursts more random
82 * 3 8/11/97 9:50a Lawrance
83 * make swarmers more erratic
85 * 2 8/10/97 6:16p Lawrance
86 * split off swarm missile code into a separate file
95 #include "freespace.h" // for Missiontime
98 #define SWARM_DIST_OFFSET 2.0 // distance swarm missile should vary from original path
99 #define SWARM_CONE_LENGTH 10000.0f // used to pick a target point far in the distance
100 #define SWARM_CHANGE_DIR_TIME 400 // time to force change in direction of swarm missile
101 #define SWARM_ANGLE_CHANGE (4*PI/180) // in rad
102 #define SWARM_MISSILE_DELAY 150 // time delay between each swarm missile that is fired
103 #define SWARM_TIME_VARIANCE 100 // max time variance when deciding when to change swarm missile course
105 #define SWARM_DIST_STOP_SWARMING 300
107 #define TURRET_SWARM_VALIDITY_CHECKTIME 5000 // number of ms between checks on turret_swam_info checks
109 #define SWARM_USED (1<<0)
110 #define SWARM_POSITIVE_PATH (1<<1)
112 typedef struct swarm_info {
114 int change_timestamp;
115 vector original_target;
117 vector circle_rvec, circle_uvec;
120 int path_num; // which path swarm missile is currently following
121 int homing_objnum; // object number that swarm missile is homing on, -1 if not homing
122 int change_time; // when swarm missile should next update direction, based on missile speed
124 float last_dist; // last distance to target
127 typedef struct turret_swarm_info {
135 ship_subsys* target_subsys;
140 #define MAX_SWARM_MISSILES 50
141 swarm_info Swarm_missiles[MAX_SWARM_MISSILES];
143 #define MAX_TURRET_SWARM_INFO 50
144 turret_swarm_info Turret_swarm_info[MAX_TURRET_SWARM_INFO];
146 int Turret_swarm_validity_next_check_time;
148 // ------------------------------------------------------------------
149 // swarm_level_init()
151 // Called at the start of each new mission
153 void swarm_level_init()
157 turret_swarm_info *tswarmp;
159 for ( i = 0; i < MAX_SWARM_MISSILES; i++ ) {
160 swarmp = &Swarm_missiles[i];
162 swarmp->change_timestamp = 1;
163 swarmp->path_num = -1;
166 for (i=0; i<MAX_TURRET_SWARM_INFO; i++) {
167 tswarmp = &Turret_swarm_info[i];
169 tswarmp->num_to_launch = 0;
170 tswarmp->parent_objnum = -1;
171 tswarmp->parent_sig = -1;
172 tswarmp->target_objnum = -1;
173 tswarmp->target_sig = -1;
174 tswarmp->turret = NULL;
175 tswarmp->target_subsys = NULL;
176 tswarmp->time_to_fire = 0;
179 Turret_swarm_validity_next_check_time = timestamp(TURRET_SWARM_VALIDITY_CHECKTIME);
182 // ------------------------------------------------------------------
183 // swarm_maybe_fire_missile()
185 // Check if there are any swarm missiles to fire, and if enough time
186 // has elapsed since last one fired, go ahead and fire it.
188 // This is called once per ship frame in ship_move()
190 void swarm_maybe_fire_missile(int shipnum)
194 int weapon_info_index;
196 Assert(shipnum >= 0 && shipnum < MAX_SHIPS );
197 sp = &Ships[shipnum];
199 if ( sp->num_swarm_missiles_to_fire <= 0 )
203 if ( swp->current_secondary_bank == -1 ) {
204 sp->num_swarm_missiles_to_fire = 0;
208 weapon_info_index = swp->secondary_bank_weapons[swp->current_secondary_bank];
209 Assert( weapon_info_index >= 0 && weapon_info_index < MAX_WEAPON_TYPES );
211 // if current secondary bank is not a swarm missile, return
212 if ( !(Weapon_info[weapon_info_index].wi_flags & WIF_SWARM) ) {
213 sp->num_swarm_missiles_to_fire = 0;
217 if ( timestamp_elapsed(sp->next_swarm_fire) ) {
218 sp->next_swarm_fire = timestamp(SWARM_MISSILE_DELAY);
219 ship_fire_secondary( &Objects[sp->objnum], 1 );
220 sp->num_swarm_missiles_to_fire--;
224 // ------------------------------------------------------------------
227 // Get a free swarm missile entry, and initialize the struct members
232 swarm_info *swarmp = NULL;
234 for ( i = 0; i < MAX_SWARM_MISSILES; i++ ) {
235 swarmp = &Swarm_missiles[i];
236 if ( !(swarmp->flags & SWARM_USED) )
240 if ( i >= MAX_SWARM_MISSILES ) {
241 nprintf(("Warning","No more swarm missiles are available\n"));
246 swarmp->change_timestamp = 1;
247 swarmp->path_num = -1;
248 swarmp->homing_objnum = -1;
250 swarmp->flags |= SWARM_USED;
254 // ------------------------------------------------------------------
258 void swarm_delete(int i)
262 Assert(i >= 0 && i < MAX_SWARM_MISSILES);
263 swarmp = &Swarm_missiles[i];
265 if ( !(swarmp->flags & SWARM_USED) ) {
266 Int3(); // tried to delete a swarm missile that didn't exist, get Alan
272 // ------------------------------------------------------------------
273 // swarm_update_direction()
275 // Check if we want to update the direction of a swarm missile.
277 void swarm_update_direction(object *objp, float frametime)
283 vector obj_to_target;
284 float vel, target_dist, radius, missile_speed, missile_dist;
287 Assert(objp->instance >= 0 && objp->instance < MAX_WEAPONS);
289 wp = &Weapons[objp->instance];
291 if (wp->swarm_index == -1) {
295 wip = &Weapon_info[wp->weapon_info_index];
296 hobjp = wp->homing_object;
297 pi = &Objects[wp->objnum].phys_info;
298 swarmp = &Swarm_missiles[wp->swarm_index];
300 // check if homing is lost.. if it is then get a new path to move swarm missile along
301 if ( swarmp->homing_objnum != -1 && hobjp == &obj_used_list ) {
302 swarmp->change_timestamp = 1;
303 swarmp->path_num = -1;
304 swarmp->homing_objnum = -1;
307 if ( hobjp != &obj_used_list ) {
308 swarmp->homing_objnum = OBJ_INDEX(hobjp);
311 if ( timestamp_elapsed(swarmp->change_timestamp) ) {
313 if ( swarmp->path_num == -1 ) {
314 if ( Objects[objp->parent].type != OBJ_SHIP ) {
315 //AL: parent ship died... so just pick some random paths
316 swarmp->path_num = myrand()%4;
319 parent_shipp = &Ships[Objects[objp->parent].instance];
320 swarmp->path_num = (parent_shipp->next_swarm_path++)%4;
322 if ( parent_shipp->next_swarm_path%4 == 0 ) {
323 swarmp->flags ^= SWARM_POSITIVE_PATH;
327 vm_vec_scale_add(&swarmp->original_target, &objp->pos, &objp->orient.fvec, SWARM_CONE_LENGTH);
328 swarmp->circle_rvec = objp->orient.rvec;
329 swarmp->circle_uvec = objp->orient.uvec;
331 swarmp->change_count = 1;
332 swarmp->change_time = fl2i(SWARM_CHANGE_DIR_TIME + SWARM_TIME_VARIANCE*(frand() - 0.5f) * 2);
334 vm_vec_zero(&swarmp->last_offset);
336 missile_speed = pi->speed;
337 missile_dist = missile_speed * swarmp->change_time/1000.0f;
338 if ( missile_dist < SWARM_DIST_OFFSET ) {
339 missile_dist=i2fl(SWARM_DIST_OFFSET);
341 swarmp->angle_offset = (float)(asin(SWARM_DIST_OFFSET / missile_dist));
342 Assert(!_isnan(swarmp->angle_offset) );
345 swarmp->change_timestamp = timestamp(swarmp->change_time);
347 // check if swarm missile is homing, if so need to calculate a new target pos to turn towards
348 if ( hobjp != &obj_used_list && f2fl(Missiontime - wp->creation_time) > 0.5f ) {
349 swarmp->original_target = wp->homing_pos;
351 // Calculate a rvec and uvec that will determine the displacement from the
352 // intended target. Use crossprod to generate a right vector, from the missile
353 // up vector and the vector connecting missile to the homing object.
354 swarmp->circle_uvec = objp->orient.uvec;
355 swarmp->circle_rvec = objp->orient.rvec;
357 missile_speed = pi->speed;
358 missile_dist = missile_speed * swarmp->change_time/1000.0f;
359 swarmp->angle_offset = (float)(asin(SWARM_DIST_OFFSET / missile_dist));
360 Assert(!_isnan(swarmp->angle_offset) );
363 vm_vec_sub(&obj_to_target, &swarmp->original_target, &objp->pos);
364 target_dist = vm_vec_mag_quick(&obj_to_target);
365 swarmp->last_dist = target_dist;
367 // If homing swarm missile is close to target, let missile home in on original target
368 if ( target_dist < SWARM_DIST_STOP_SWARMING ) {
369 swarmp->new_target = swarmp->original_target;
370 goto swarm_new_target_calced;
373 radius = (float)tan(swarmp->angle_offset) * target_dist;
374 vector rvec_component, uvec_component;
376 swarmp->change_count++;
377 if ( swarmp->change_count > 2 ) {
378 swarmp->flags ^= SWARM_POSITIVE_PATH;
379 swarmp->change_count = 0;
382 // pick a new path number to follow once at center
383 if ( swarmp->change_count == 1 ) {
384 swarmp->path_num = swarmp->path_num + myrand()%3;
385 if ( swarmp->path_num > 3 ) {
386 swarmp->path_num = 0;
390 vm_vec_zero(&rvec_component);
391 vm_vec_zero(&uvec_component);
393 switch ( swarmp->path_num ) {
394 case 0: // straight up and down
395 if ( swarmp->flags & SWARM_POSITIVE_PATH )
396 vm_vec_copy_scale( &uvec_component, &swarmp->circle_uvec, radius);
398 vm_vec_copy_scale( &uvec_component, &swarmp->circle_uvec, -radius);
401 case 1: // left/right
402 if ( swarmp->flags & SWARM_POSITIVE_PATH )
403 vm_vec_copy_scale( &rvec_component, &swarmp->circle_rvec, radius);
405 vm_vec_copy_scale( &rvec_component, &swarmp->circle_rvec, -radius);
408 case 2: // top/right - bottom/left
409 if ( swarmp->flags & SWARM_POSITIVE_PATH ) {
410 vm_vec_copy_scale( &rvec_component, &swarmp->circle_rvec, radius);
411 vm_vec_copy_scale( &uvec_component, &swarmp->circle_uvec, radius);
414 vm_vec_copy_scale( &rvec_component, &swarmp->circle_rvec, -radius);
415 vm_vec_copy_scale( &uvec_component, &swarmp->circle_uvec, -radius);
419 case 3: // top-left - bottom/right
420 if ( swarmp->flags & SWARM_POSITIVE_PATH ) {
421 vm_vec_copy_scale( &rvec_component, &swarmp->circle_rvec, -radius);
422 vm_vec_copy_scale( &uvec_component, &swarmp->circle_uvec, radius);
425 vm_vec_copy_scale( &rvec_component, &swarmp->circle_rvec, radius);
426 vm_vec_copy_scale( &uvec_component, &swarmp->circle_uvec, -radius);
434 swarmp->new_target = swarmp->original_target;
435 vm_vec_zero(&swarmp->last_offset);
436 vm_vec_add(&swarmp->last_offset, &uvec_component, &rvec_component);
437 vm_vec_add2(&swarmp->new_target, &swarmp->last_offset);
440 if ( hobjp != &obj_used_list && f2fl(Missiontime - wp->creation_time) > 0.5f ) {
442 swarmp->new_target = swarmp->original_target;
443 if ( swarmp->last_dist < SWARM_DIST_STOP_SWARMING ) {
444 swarmp->new_target = wp->homing_pos;
445 goto swarm_new_target_calced;
448 vm_vec_add2(&swarmp->new_target, &swarmp->last_offset);
452 swarm_new_target_calced:
454 ai_turn_towards_vector(&swarmp->new_target, objp, frametime, wip->turn_time, NULL, NULL, 0.0f, 0);
455 vel = vm_vec_mag(&objp->phys_info.desired_vel);
456 vm_vec_copy_scale(&objp->phys_info.desired_vel, &objp->orient.fvec, vel);
459 // ------------------------------------------------------------------
460 // turret_swarm_create()
462 // Get a free swarm missile entry, and initialize the struct members
464 int turret_swarm_create()
467 turret_swarm_info *tswarmp = NULL;
469 for (i=0; i<MAX_TURRET_SWARM_INFO; i++) {
470 tswarmp = &Turret_swarm_info[i];
471 if ( !(tswarmp->flags & SWARM_USED) )
475 if ( i >= MAX_TURRET_SWARM_INFO ) {
476 nprintf(("Warning","No more turret swarm info slots are available\n"));
482 tswarmp->num_to_launch = 0;
483 tswarmp->parent_objnum = -1;
484 tswarmp->parent_sig = -1;
485 tswarmp->target_objnum = -1;
486 tswarmp->target_sig = -1;
487 tswarmp->turret = NULL;
488 tswarmp->target_subsys = NULL;
489 tswarmp->time_to_fire = 0;
491 tswarmp->flags |= SWARM_USED;
495 // ------------------------------------------------------------------
496 // turret_swarm_delete()
498 void turret_swarm_delete(int i)
500 turret_swarm_info *tswarmp;
502 Assert(i >= 0 && i < MAX_TURRET_SWARM_INFO);
503 tswarmp = &Turret_swarm_info[i];
505 if ( !(tswarmp->flags & SWARM_USED) ) {
506 Int3(); // tried to delete a swarm missile that didn't exist, get DaveA
512 // Set up turret swarm info struct
513 void turret_swarm_set_up_info(int parent_objnum, ship_subsys *turret, int turret_weapon_class)
515 turret_swarm_info *tsi;
516 object *parent_obj, *target_obj;
521 // weapon info pointer
522 Assert((turret_weapon_class >= 0) && (turret_weapon_class < Num_weapon_types));
523 if((turret_weapon_class < 0) || (turret_weapon_class >= Num_weapon_types)){
526 wip = &Weapon_info[turret_weapon_class];
529 Assert((parent_objnum >= 0) && (parent_objnum < MAX_OBJECTS));
530 if((parent_objnum < 0) || (parent_objnum >= MAX_OBJECTS)){
533 parent_obj = &Objects[parent_objnum];
534 Assert(parent_obj->type == OBJ_SHIP);
535 shipp = &Ships[parent_obj->instance];
536 Assert((turret->turret_enemy_objnum >= 0) && (turret->turret_enemy_objnum < MAX_OBJECTS));
537 if((turret->turret_enemy_objnum < 0) || (turret->turret_enemy_objnum >= MAX_OBJECTS)){
540 target_obj = &Objects[turret->turret_enemy_objnum];
542 // valid swarm weapon
543 Assert((wip->wi_flags & WIF_SWARM) && (wip->swarm_count > 0));
544 if(!(wip->wi_flags & WIF_SWARM) || (wip->swarm_count <= 0)){
548 // get turret_swarm_info
549 tsi_index = turret_swarm_create();
550 if (tsi_index == -1) {
554 // set turret to point to tsi
555 tsi = &Turret_swarm_info[tsi_index];
556 if (turret->turret_swarm_info_index != -1) {
557 // overlapping firing intervals..., stop old and start new
558 mprintf(("Overlapping turret swarm firing intervals"));
559 turret_swarm_delete(turret->turret_swarm_info_index);
560 turret->turret_swarm_info_index = -1;
561 shipp->num_turret_swarm_info--;
563 turret->turret_swarm_info_index = tsi_index;
565 // increment ship tsi counter
566 shipp->num_turret_swarm_info++;
568 // make sure time is sufficient to launch all the missiles before next volley
570 Assert(wip->swarm_count * SWARM_MISSILE_DELAY < wip->fire_wait * 1000.0f);
574 tsi->num_to_launch = wip->swarm_count;
575 tsi->parent_objnum = parent_objnum;
576 tsi->parent_sig = parent_obj->signature;
577 tsi->target_objnum = turret->turret_enemy_objnum;
578 tsi->target_sig = target_obj->signature;
579 tsi->turret = turret;
580 tsi->target_subsys = turret->targeted_subsys;
581 tsi->time_to_fire = 1; // first missile next frame
584 void turret_swarm_fire_from_turret(ship_subsys *turret, int parent_objnum, int target_objnum, ship_subsys *target_subsys);
585 // check if ship has turret ready to fire swarm type missiles
586 void turret_swarm_maybe_fire_missile(int shipnum)
588 ship *shipp = &Ships[shipnum];
590 turret_swarm_info *tsi;
591 object *parent_obj, *target_obj;
592 int target_objnum, num_turret_swarm_turrets_left;
594 // check if ship has any turrets ready to fire
595 if (shipp->num_turret_swarm_info <= 0) {
596 Assert(shipp->num_turret_swarm_info == 0);
600 // ship obj which has fired turret swarm missiles
601 parent_obj = &Objects[shipp->objnum];
602 num_turret_swarm_turrets_left = shipp->num_turret_swarm_info;
604 // search ship subsystems for turrets with valid turret_swarm_info_index
605 for (subsys = GET_FIRST(&shipp->subsys_list); subsys != END_OF_LIST(&shipp->subsys_list); subsys = GET_NEXT(subsys)) {
606 if (subsys->turret_swarm_info_index != -1) {
608 num_turret_swarm_turrets_left--;
609 Assert(num_turret_swarm_turrets_left >= 0);
611 // get turret_swarm_info
612 Assert( (subsys->turret_swarm_info_index >= 0) && (subsys->turret_swarm_info_index < MAX_TURRET_SWARM_INFO) );
613 tsi = &Turret_swarm_info[subsys->turret_swarm_info_index];
615 // check if parent ship is valid (via signature)
616 if ( (tsi->parent_sig == parent_obj->signature) ) {
618 // make sure we have the right turret.
619 Assert(tsi->turret == subsys);
621 // check if time to fire
622 if (timestamp_elapsed(tsi->time_to_fire)) {
623 Assert(tsi->num_to_launch > 0);
625 // check target still alive
627 if (tsi->target_objnum > -1) {
628 target_obj= &Objects[tsi->target_objnum];
630 if (target_obj->signature == tsi->target_sig) {
631 target_objnum = tsi->target_objnum;
633 // poor target, it died
634 tsi->target_objnum = -1;
638 // make sure turret is still alive and fire swarmer
639 if (subsys->current_hits > 0) {
640 turret_swarm_fire_from_turret(tsi->turret, tsi->parent_objnum, target_objnum, tsi->target_subsys);
644 tsi->time_to_fire = timestamp(SWARM_MISSILE_DELAY);
647 tsi->num_to_launch--;
649 if (tsi->num_to_launch == 0) {
650 shipp->num_turret_swarm_info--;
651 turret_swarm_delete(subsys->turret_swarm_info_index);
652 subsys->turret_swarm_info_index = -1;
656 Warning(LOCATION, "Found turret swarm info on ship: %s with turret: %s, but signature does not match.", shipp->ship_name, subsys->system_info->subobj_name);
657 shipp->num_turret_swarm_info--;
658 turret_swarm_delete(subsys->turret_swarm_info_index);
659 subsys->turret_swarm_info_index = -1;
664 Assert(num_turret_swarm_turrets_left == 0);
667 // check Turret_swarm_info for info that are invalid - ie, ships died while firing.
668 void turret_swarm_check_validity()
671 turret_swarm_info *tswarmp;
674 if (timestamp_elapsed(Turret_swarm_validity_next_check_time)) {
677 Turret_swarm_validity_next_check_time = timestamp(TURRET_SWARM_VALIDITY_CHECKTIME);
679 // go through all Turret_swarm_info, check obj and obj->signature
680 for (i=0; i<MAX_TURRET_SWARM_INFO; i++) {
681 tswarmp = &Turret_swarm_info[i];
683 if (tswarmp->flags & SWARM_USED) {
684 ship_obj = &Objects[tswarmp->parent_objnum];
685 if (ship_obj->type == OBJ_SHIP) {
686 if (ship_obj->signature == tswarmp->parent_sig) {
691 turret_swarm_delete(i);