FS2_Open
Open source remastering of the Freespace 2 engine
aiturret.cpp File Reference
#include "ai/aibig.h"
#include "ai/aiinternal.h"
#include "asteroid/asteroid.h"
#include "debugconsole/console.h"
#include "freespace2/freespace.h"
#include "gamesnd/gamesnd.h"
#include "globalincs/linklist.h"
#include "globalincs/systemvars.h"
#include "iff_defs/iff_defs.h"
#include "io/timer.h"
#include "math/staticrand.h"
#include "network/multi.h"
#include "network/multimsgs.h"
#include "object/objectdock.h"
#include "parse/scripting.h"
#include "render/3d.h"
#include "ship/ship.h"
#include "ship/shipfx.h"
#include "weapon/beam.h"
#include "weapon/flak.h"
#include "weapon/muzzleflash.h"
#include "weapon/swarm.h"
#include "weapon/weapon.h"
#include <limits.h>

Go to the source code of this file.

Classes

struct  eval_enemy_obj_struct
 

Macros

#define AICODE_TURRET_DUMBFIRE_ANGLE   (0.8f)
 
#define AICODE_TURRET_HEATSEEK_ANGLE   (0.7f)
 
#define AICODE_TURRET_MAX_TIME_IN_RANGE   (5.0f)
 
#define BEAM_NEBULA_RANGE_REDUCE_FACTOR   (0.8f)
 
#define EEOF_BIG_ONLY   (1<<0)
 
#define EEOF_SMALL_ONLY   (1<<1)
 
#define EEOF_TAGGED_ONLY   (1<<2)
 
#define EEOF_BEAM   (1<<3)
 
#define EEOF_FLAK   (1<<4)
 
#define EEOF_LASER   (1<<5)
 
#define EEOF_MISSILE   (1<<6)
 
#define MAX_AIFFT_TURRETS   60
 

Typedefs

typedef struct eval_enemy_obj_struct eval_enemy_obj_struct
 

Functions

int object_in_turret_fov (object *objp, ship_subsys *ss, vec3d *tvec, vec3d *tpos, float dist)
 
bool is_object_radius_in_turret_fov (object *objp, ship_subsys *ss, vec3d *tvec, vec3d *tpos, vec3d *v2e, vec3d *predicted_pos, float distance)
 
int bomb_headed_towards_ship (object *bomb_objp, object *ship_objp)
 
int turret_select_best_weapon (ship_subsys *turret, object *target)
 
bool all_turret_weapons_have_flags (ship_weapon *swp, int flags)
 
bool all_turret_weapons_have_flags2 (ship_weapon *swp, int flags)
 
bool turret_weapon_has_flags (ship_weapon *swp, int flags)
 
bool turret_weapon_has_flags2 (ship_weapon *swp, int flags)
 
int turret_weapon_aggregate_flags (ship_weapon *swp)
 
int turret_weapon_aggregate_flags2 (ship_weapon *swp)
 
bool turret_weapon_has_subtype (ship_weapon *swp, int subtype)
 
weapon_infoget_turret_weapon_wip (ship_weapon *swp, int weapon_num)
 
int get_turret_weapon_next_fire_stamp (ship_weapon *swp, int weapon_num)
 
float longest_turret_weapon_range (ship_weapon *swp)
 
int valid_turret_enemy (object *objp, object *turret_parent)
 
void evaluate_obj_as_target (object *objp, eval_enemy_obj_struct *eeo)
 
int is_target_beam_valid (ship_weapon *swp, object *objp)
 
int get_nearest_turret_objnum (int turret_parent_objnum, ship_subsys *turret_subsys, int enemy_team_mask, vec3d *tpos, vec3d *tvec, int current_enemy, bool big_only_flag, bool small_only_flag, bool tagged_only_flag, bool beam_flag, bool flak_flag, bool laser_flag, bool missile_flag)
 
int find_turret_enemy (ship_subsys *turret_subsys, int objnum, vec3d *tpos, vec3d *tvec, int current_enemy)
 
void ship_get_global_turret_info (object *objp, model_subsystem *tp, vec3d *gpos, vec3d *gvec)
 
void turret_ai_update_aim (ai_info *aip, object *En_Objp, ship_subsys *ss)
 
void ship_get_global_turret_gun_info (object *objp, ship_subsys *ssp, vec3d *gpos, vec3d *gvec, int use_angles, vec3d *targetp)
 
int aifft_rotate_turret (ship *shipp, int parent_objnum, ship_subsys *ss, object *objp, object *lep, vec3d *predicted_enemy_pos, vec3d *gvec)
 
float aifft_compute_turret_dot (object *objp, object *enemy_objp, vec3d *abs_gunposp, ship_subsys *turret_subsysp, ship_subsys *enemy_subsysp)
 
 if (aifft_max_checks<=0)
 
ship_subsysaifft_find_turret_subsys (object *objp, ship_subsys *ssp, object *enemy_objp, float *dot_out)
 
int turret_should_pick_new_target (ship_subsys *turret)
 
void turret_set_next_fire_timestamp (int weapon_num, weapon_info *wip, ship_subsys *turret, ai_info *aip)
 
int turret_should_fire_aspect (ship_subsys *turret, weapon_info *wip, bool in_sight)
 
void turret_update_enemy_in_range (ship_subsys *turret, float seconds)
 
bool turret_fire_weapon (int weapon_num, ship_subsys *turret, int parent_objnum, vec3d *turret_pos, vec3d *turret_fvec, vec3d *predicted_pos=NULL, float flak_range_override=100.0f)
 
void turret_swarm_fire_from_turret (turret_swarm_info *tsi)
 
void ai_fire_from_turret (ship *shipp, ship_subsys *ss, int parent_objnum)
 
bool turret_std_fov_test (ship_subsys *ss, vec3d *gvec, vec3d *v2e, float size_mod)
 
bool turret_adv_fov_test (ship_subsys *ss, vec3d *gvec, vec3d *v2e, float size_mod)
 
bool turret_fov_test (ship_subsys *ss, vec3d *gvec, vec3d *v2e, float size_mod)
 
float get_adjusted_turret_rof (ship_subsys *ss)
 

Variables

float Lethality_range_const = 2.0f
 
float Player_lethality_bump [NUM_SKILL_LEVELS]
 
char * Turret_target_order_names [NUM_TURRET_ORDER_TYPES]
 
int Player_attacking_enabled
 
int Use_parent_target = 0
 
ship_subsysaifft_list [MAX_AIFFT_TURRETS]
 
float aifft_rank [MAX_AIFFT_TURRETS]
 
int aifft_list_size = 0
 
int aifft_max_checks = 5
 
int Num_ai_firing = 0
 
int Num_find_turret_enemy = 0
 
int Num_turrets_fired = 0
 
int Nebula_sec_range
 

Macro Definition Documentation

#define AICODE_TURRET_DUMBFIRE_ANGLE   (0.8f)

Definition at line 34 of file aiturret.cpp.

#define AICODE_TURRET_HEATSEEK_ANGLE   (0.7f)

Definition at line 35 of file aiturret.cpp.

#define AICODE_TURRET_MAX_TIME_IN_RANGE   (5.0f)

Definition at line 36 of file aiturret.cpp.

#define BEAM_NEBULA_RANGE_REDUCE_FACTOR   (0.8f)

Definition at line 37 of file aiturret.cpp.

#define EEOF_BEAM   (1<<3)

Definition at line 59 of file aiturret.cpp.

#define EEOF_BIG_ONLY   (1<<0)

Definition at line 56 of file aiturret.cpp.

#define EEOF_FLAK   (1<<4)

Definition at line 60 of file aiturret.cpp.

#define EEOF_LASER   (1<<5)

Definition at line 61 of file aiturret.cpp.

#define EEOF_MISSILE   (1<<6)

Definition at line 62 of file aiturret.cpp.

#define EEOF_SMALL_ONLY   (1<<1)

Definition at line 57 of file aiturret.cpp.

#define EEOF_TAGGED_ONLY   (1<<2)

Definition at line 58 of file aiturret.cpp.

#define MAX_AIFFT_TURRETS   60

Definition at line 1571 of file aiturret.cpp.

Typedef Documentation

Function Documentation

void ai_fire_from_turret ( ship shipp,
ship_subsys ss,
int  parent_objnum 
)

Definition at line 2219 of file aiturret.cpp.

float aifft_compute_turret_dot ( object objp,
object enemy_objp,
vec3d abs_gunposp,
ship_subsys turret_subsysp,
ship_subsys enemy_subsysp 
)

Determine if subsystem *enemy_subsysp is hittable from objp.

 @return Dot product of vector from point abs_gunposp to *enemy_subsysp, if hittable

Definition at line 1534 of file aiturret.cpp.

ship_subsys* aifft_find_turret_subsys ( object objp,
ship_subsys ssp,
object enemy_objp,
float dot_out 
)

Pick a subsystem to attack on enemy_objp. Only pick one if enemy_objp is a big ship or a capital ship.

Returns
Dot product from turret to subsystem in *dot_out

Definition at line 1596 of file aiturret.cpp.

int aifft_rotate_turret ( ship shipp,
int  parent_objnum,
ship_subsys ss,
object objp,
object lep,
vec3d predicted_enemy_pos,
vec3d gvec 
)

Rotate a turret towards an enemy.

 Some obscure model thing only John Slagel knows about.
 Sets predicted enemy position.
 If the turret (*ss) has a subsystem targeted, the subsystem is used as the predicted point.
Returns
TRUE if caller should use angles in subsequent rotations.

Definition at line 1428 of file aiturret.cpp.

bool all_turret_weapons_have_flags ( ship_weapon swp,
int  flags 
)

Returns true if all weapons in swp have the specified flag

Note
doesn't work for WIF2

Definition at line 262 of file aiturret.cpp.

bool all_turret_weapons_have_flags2 ( ship_weapon swp,
int  flags 
)

Returns true if all weapons in swp have the specified flag

Note
does work for WIF2, but not WIF

Definition at line 284 of file aiturret.cpp.

int bomb_headed_towards_ship ( object bomb_objp,
object ship_objp 
)

Is bomb headed towards given ship?

Parameters
bomb_objpBomb specified
ship_objpShip specified
Returns
1 if bomb_objp is headed towards ship_objp

Definition at line 222 of file aiturret.cpp.

void evaluate_obj_as_target ( object objp,
eval_enemy_obj_struct eeo 
)

Definition at line 575 of file aiturret.cpp.

int find_turret_enemy ( ship_subsys turret_subsys,
int  objnum,
vec3d tpos,
vec3d tvec,
int  current_enemy 
)

Return objnum if enemy found, else return -1;

Parameters
turret_subsysPointer to turret subsystem
objnumParent objnum for the turret
tposPosition of turret (world coords)
tvecForward vector of turret (world coords)
current_enemyObjnum of current turret target

Definition at line 1187 of file aiturret.cpp.

float get_adjusted_turret_rof ( ship_subsys ss)

Definition at line 2884 of file aiturret.cpp.

int get_nearest_turret_objnum ( int  turret_parent_objnum,
ship_subsys turret_subsys,
int  enemy_team_mask,
vec3d tpos,
vec3d tvec,
int  current_enemy,
bool  big_only_flag,
bool  small_only_flag,
bool  tagged_only_flag,
bool  beam_flag,
bool  flak_flag,
bool  laser_flag,
bool  missile_flag 
)

Given an object and an enemy team, return the index of the nearest enemy object.

Parameters
turret_parent_objnumParent objnum for the turret
turret_subsysPointer to system_info for the turret subsystem
enemy_team_maskOR'ed TEAM_ flags for the enemy of the turret parent ship
tposPosition of turret (world coords)
tvecForward vector of turret (world coords)
current_enemyObjnum of current turret target
big_only_flag
small_only_flag
tagged_only_flag
beam_flag
flak_flag
laser_flag
missile_flag

Definition at line 877 of file aiturret.cpp.

int get_turret_weapon_next_fire_stamp ( ship_weapon swp,
int  weapon_num 
)

Definition at line 450 of file aiturret.cpp.

weapon_info* get_turret_weapon_wip ( ship_weapon swp,
int  weapon_num 
)

Use for getting a Weapon_info pointer, given a turret and a turret weapon indice

Returns
a pointer to the Weapon_info for weapon_num

Definition at line 439 of file aiturret.cpp.

if ( aifft_max_checks<=  0)

Definition at line 1581 of file aiturret.cpp.

bool is_object_radius_in_turret_fov ( object objp,
ship_subsys ss,
vec3d tvec,
vec3d tpos,
vec3d v2e,
vec3d predicted_pos,
float  distance 
)

Definition at line 119 of file aiturret.cpp.

int is_target_beam_valid ( ship_weapon swp,
object objp 
)

Is target beam valid?

Returns
0 only if objnum is beam protected and turret is beam turret

Definition at line 842 of file aiturret.cpp.

float longest_turret_weapon_range ( ship_weapon swp)

Returns the longest-ranged weapon on a turret

Todo:
This function is kinda slow

Definition at line 466 of file aiturret.cpp.

int object_in_turret_fov ( object objp,
ship_subsys ss,
vec3d tvec,
vec3d tpos,
float  dist 
)

Is object in turret field of view?

Parameters
objpPointer to object to test
ssShip turret subsystem to test from
tvecTurret initial vector
tposTurret initial position
distDistance from turret to center point of object
Returns
1 if objp is in fov of the specified turret. Otherwise return 0.

Definition at line 101 of file aiturret.cpp.

void ship_get_global_turret_gun_info ( object objp,
ship_subsys ssp,
vec3d gpos,
vec3d gvec,
int  use_angles,
vec3d targetp 
)

Given an object and a turret on that object, return the actual firing point of the gun and its normal.

Note
This uses the current turret angles. We are keeping track of which gun to fire next in the ship specific info for this turret subobject. Use this info to determine which position to fire from next.
Parameters
objpPointer to object
sspPointer to turret subsystem
gposAbsolute position of gun firing point
gvecVector from *gpos to *targetp
use_anglesUse current angles
targetpPointer to target object

Definition at line 1327 of file aiturret.cpp.

void ship_get_global_turret_info ( object objp,
model_subsystem tp,
vec3d gpos,
vec3d gvec 
)

Given an object and a turret on that object, return the global position and forward vector of the turret.

Parameters
objpPointer to object
tpTurrent model system on that object
gpos[Output] Global absolute position of gun firing point
gvec[Output] Global vector
Note
The gun normal is the unrotated gun normal, (the center of the FOV cone), not the actual gun normal given using the current turret heading. But it is rotated into the model's orientation in global space.

Definition at line 1303 of file aiturret.cpp.

bool turret_adv_fov_test ( ship_subsys ss,
vec3d gvec,
vec3d v2e,
float  size_mod 
)

Definition at line 2851 of file aiturret.cpp.

void turret_ai_update_aim ( ai_info aip,
object En_Objp,
ship_subsys ss 
)

Update turret aiming data based on max turret aim update delay

Definition at line 1403 of file aiturret.cpp.

bool turret_fire_weapon ( int  weapon_num,
ship_subsys turret,
int  parent_objnum,
vec3d turret_pos,
vec3d turret_fvec,
vec3d predicted_pos = NULL,
float  flak_range_override = 100.0f 
)

Fire a weapon from a turret

Definition at line 1832 of file aiturret.cpp.

bool turret_fov_test ( ship_subsys ss,
vec3d gvec,
vec3d v2e,
float  size_mod 
)

Definition at line 2873 of file aiturret.cpp.

int turret_select_best_weapon ( ship_subsys turret,
object target 
)

Returns the best weapon on turret for target

Note
All non-negative return values are expressed in what I like to call "widx"s.
Returns
-1 if unable to find a weapon for the target at all.

Definition at line 246 of file aiturret.cpp.

void turret_set_next_fire_timestamp ( int  weapon_num,
weapon_info wip,
ship_subsys turret,
ai_info aip 
)

Set the next fire timestamp for a turret, based on weapon type and ai class

Definition at line 1725 of file aiturret.cpp.

int turret_should_fire_aspect ( ship_subsys turret,
weapon_info wip,
bool  in_sight 
)

Decide if a turret should launch an aspect seeking missile

Definition at line 1804 of file aiturret.cpp.

int turret_should_pick_new_target ( ship_subsys turret)
Returns
!0 if the specified target should scan for a new target, otherwise return 0

Definition at line 1700 of file aiturret.cpp.

bool turret_std_fov_test ( ship_subsys ss,
vec3d gvec,
vec3d v2e,
float  size_mod 
)

Definition at line 2841 of file aiturret.cpp.

void turret_swarm_fire_from_turret ( turret_swarm_info tsi)

Definition at line 2137 of file aiturret.cpp.

void turret_update_enemy_in_range ( ship_subsys turret,
float  seconds 
)

Update how long current target has been in this turrets range

Definition at line 1816 of file aiturret.cpp.

int turret_weapon_aggregate_flags ( ship_weapon swp)

Just gloms all the flags from all the weapons into one variable. More efficient if all you need to do is test for the existence of a flag.

Definition at line 360 of file aiturret.cpp.

int turret_weapon_aggregate_flags2 ( ship_weapon swp)

Just gloms all the flags from all the weapons into one variable. More efficient if all you need to do is test for the existence of a flag.

Definition at line 384 of file aiturret.cpp.

bool turret_weapon_has_flags ( ship_weapon swp,
int  flags 
)

Returns true if any of the weapons in swp have flags

Note
doesn't work for WIF2

Definition at line 306 of file aiturret.cpp.

bool turret_weapon_has_flags2 ( ship_weapon swp,
int  flags 
)

Returns true if any of the weapons in swp have flags

Note
does work for WIF2, but not WIF

Definition at line 334 of file aiturret.cpp.

bool turret_weapon_has_subtype ( ship_weapon swp,
int  subtype 
)

Returns true if any of the weapons in swp have the subtype specified

Note
It might be a little faster to optimize based on WP_LASER should only appear in primaries and WP_MISSILE in secondaries. but in the interest of future coding I leave it like this.

Definition at line 411 of file aiturret.cpp.

int valid_turret_enemy ( object objp,
object turret_parent 
)

Is valid turret target?

Parameters
objpObject that turret is considering as an enemy
turret_parentObject of ship that turret sits on
Returns
!0 if objp can be considered for a turret target, 0 otherwise

Definition at line 507 of file aiturret.cpp.

Variable Documentation

Definition at line 1573 of file aiturret.cpp.

int aifft_list_size = 0

Definition at line 1575 of file aiturret.cpp.

int aifft_max_checks = 5

Definition at line 1576 of file aiturret.cpp.

float aifft_rank[MAX_AIFFT_TURRETS]

Definition at line 1574 of file aiturret.cpp.

float Lethality_range_const = 2.0f

Definition at line 39 of file aiturret.cpp.

int Nebula_sec_range

Given a turret tp and its parent parent_objnum, fire from the turret at its enemy.

Definition at line 307 of file hudlock.cpp.

int Num_ai_firing = 0

Definition at line 2211 of file aiturret.cpp.

int Num_find_turret_enemy = 0

Definition at line 2212 of file aiturret.cpp.

int Num_turrets_fired = 0

Definition at line 2213 of file aiturret.cpp.

int Player_attacking_enabled

Definition at line 1638 of file aicode.cpp.

float Player_lethality_bump[NUM_SKILL_LEVELS]
Initial value:
= {
0.0f, 0.0f, 0.0f, 0.0f, 0.0f
}

Definition at line 45 of file aiturret.cpp.

char* Turret_target_order_names[NUM_TURRET_ORDER_TYPES]
Initial value:
= {
"Bombs",
"Ships",
"Asteroids",
}

Definition at line 50 of file aiturret.cpp.

int Use_parent_target = 0

Definition at line 1175 of file aiturret.cpp.