Freeciv21
Develop your civilization from humble roots to a global empire
aiunit.h File Reference
#include "combat.h"
#include "fc_types.h"
#include "unittype.h"
+ Include dependency graph for aiunit.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  unit_ai
 
struct  unit_type_ai
 

Macros

#define WAR(plr1, plr2)   (player_diplstate_get(plr1, plr2)->type == DS_WAR)
 
#define NEVER_MET(plr1, plr2)    (player_diplstate_get(plr1, plr2)->type == DS_NO_CONTACT)
 
#define DEFENSE_POWER(ptype)    (ptype->defense_strength * ptype->hp * ptype->firepower)
 
#define ATTACK_POWER(ptype)    (ptype->attack_strength * ptype->hp * ptype->firepower)
 
#define IS_ATTACKER(ptype)    (ptype->attack_strength > ptype->transport_capacity)
 
#define POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer)
 
#define UNITTYPE_COSTS(ut)
 
#define INVASION_OCCUPY   0
 
#define INVASION_ATTACK   1
 
#define RAMPAGE_ANYTHING   1
 
#define RAMPAGE_HUT_OR_BETTER   99998
 
#define RAMPAGE_FREE_CITY_OR_BETTER   99999
 
#define BODYGUARD_RAMPAGE_THRESHOLD   (SHIELD_WEIGHTING * 4)
 
#define simple_ai_unit_type_iterate(_ut)
 
#define simple_ai_unit_type_iterate_end
 

Enumerations

enum  ai_unit_task {
  AIUNIT_NONE , AIUNIT_AUTO_SETTLER , AIUNIT_BUILD_CITY , AIUNIT_DEFEND_HOME ,
  AIUNIT_ATTACK , AIUNIT_ESCORT , AIUNIT_EXPLORE , AIUNIT_RECOVER ,
  AIUNIT_HUNTER , AIUNIT_TRADE , AIUNIT_WONDER
}
 

Functions

bool dai_military_rampage (struct unit *punit, int thresh_adj, int thresh_move)
 Find and kill anything reachable within this turn and worth more than the relevant of the given thresholds until we have run out of juicy targets or movement. More...
 
void dai_manage_units (struct ai_type *ait, struct player *pplayer)
 Master manage unit function. More...
 
void dai_manage_unit (struct ai_type *ait, struct player *pplayer, struct unit *punit)
 manage one unit Careful: punit may have been destroyed upon return from this routine! More...
 
void dai_manage_military (struct ai_type *ait, struct player *pplayer, struct unit *punit)
 Decide what to do with a military unit. More...
 
struct cityfind_nearest_safe_city (struct unit *punit)
 Find safe city to recover in. More...
 
int look_for_charge (struct ai_type *ait, struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity)
 See if we can find something to defend. More...
 
bool dai_can_unit_type_follow_unit_type (const struct unit_type *follower, const struct unit_type *followee, struct ai_type *ait)
 See if the follower can follow the followee. More...
 
bool find_beachhead (const struct player *pplayer, struct pf_map *ferry_map, struct tile *dest_tile, const struct unit_type *cargo_type, struct tile **ferry_dest, struct tile **beachhead_tile)
 Returns TRUE if a beachhead as been found to reach 'dest_tile'. More...
 
int find_something_to_kill (ai_type *ait, player *pplayer, unit *punit, tile **pdest_tile, PFPath *ppath, pf_map **pferrymap, unit **pferryboat, const unit_type **pboattype, int *pmove_time)
 
int build_cost_balanced (const struct unit_type *punittype)
 In the words of Syela: "Using funky fprime variable instead of f in the denom, so that def=1 units are penalized correctly. More...
 
int unittype_def_rating_squared (const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
 Defence rating of def_type unit against att_type unit, squared. More...
 
int kill_desire (int benefit, int attack, int loss, int vuln, int attack_count)
 Compute how much we want to kill certain victim we've chosen, counted in SHIELDs. More...
 
const struct impr_typeutype_needs_improvement (const struct unit_type *putype, const struct city *pcity)
 Returns an improvement that will make it possible to build units of the specified type the specified city. More...
 
void dai_consider_tile_dangerous (struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
 Are there dangerous enemies at or adjacent to the tile 'ptile'? More...
 
void dai_units_ruleset_init (struct ai_type *ait)
 Initialise the unit data from the ruleset for the AI. More...
 
void dai_units_ruleset_close (struct ai_type *ait)
 Close AI unit type data. More...
 
void dai_unit_init (struct ai_type *ait, struct unit *punit)
 Initialize unit for use with default AI. More...
 
void dai_unit_turn_end (struct ai_type *ait, struct unit *punit)
 Free unit from use with default AI. More...
 
void dai_unit_close (struct ai_type *ait, struct unit *punit)
 Free unit from use with default AI. More...
 
void dai_unit_save (struct ai_type *ait, const char *aitstr, struct section_file *file, const struct unit *punit, const char *unitstr)
 Save AI data of a unit. More...
 
void dai_unit_load (struct ai_type *ait, const char *aitstr, const struct section_file *file, struct unit *punit, const char *unitstr)
 Load AI data of a unit. More...
 
struct unit_typedai_role_utype_for_terrain_class (struct city *pcity, int role, enum terrain_class tc)
 Get unit type player can build, suitable to role, with given move type. More...
 
bool dai_unit_can_strike_my_unit (const struct unit *attacker, const struct unit *defender)
 Returns whether 'attacker' can attack 'defender' immediately. More...
 
void dai_switch_to_explore (struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
 Switch to autoexploring. More...
 

Variables

struct unit_typesimple_ai_types [U_LAST]
 

Macro Definition Documentation

◆ ATTACK_POWER

#define ATTACK_POWER (   ptype)     (ptype->attack_strength * ptype->hp * ptype->firepower)

Definition at line 73 of file aiunit.h.

◆ BODYGUARD_RAMPAGE_THRESHOLD

#define BODYGUARD_RAMPAGE_THRESHOLD   (SHIELD_WEIGHTING * 4)

Definition at line 93 of file aiunit.h.

◆ DEFENSE_POWER

#define DEFENSE_POWER (   ptype)     (ptype->defense_strength * ptype->hp * ptype->firepower)

Definition at line 71 of file aiunit.h.

◆ INVASION_ATTACK

#define INVASION_ATTACK   1

Definition at line 86 of file aiunit.h.

◆ INVASION_OCCUPY

#define INVASION_OCCUPY   0

Definition at line 85 of file aiunit.h.

◆ IS_ATTACKER

#define IS_ATTACKER (   ptype)     (ptype->attack_strength > ptype->transport_capacity)

Definition at line 75 of file aiunit.h.

◆ NEVER_MET

#define NEVER_MET (   plr1,
  plr2 
)     (player_diplstate_get(plr1, plr2)->type == DS_NO_CONTACT)

Definition at line 69 of file aiunit.h.

◆ POTENTIALLY_HOSTILE_PLAYER

#define POTENTIALLY_HOSTILE_PLAYER (   ait,
  pplayer,
  aplayer 
)
Value:
(WAR(pplayer, aplayer) || NEVER_MET(pplayer, aplayer) \
|| dai_diplomacy_get(ait, pplayer, aplayer)->countdown >= 0)
struct ai_dip_intel * dai_diplomacy_get(struct ai_type *ait, const struct player *plr1, const struct player *plr2)
Returns diplomatic state type between two players.
Definition: aidata.cpp:397
#define WAR(plr1, plr2)
Definition: aiunit.h:68
#define NEVER_MET(plr1, plr2)
Definition: aiunit.h:69
int countdown
Definition: aidata.h:54

Definition at line 77 of file aiunit.h.

◆ RAMPAGE_ANYTHING

#define RAMPAGE_ANYTHING   1

Definition at line 90 of file aiunit.h.

◆ RAMPAGE_FREE_CITY_OR_BETTER

#define RAMPAGE_FREE_CITY_OR_BETTER   99999

Definition at line 92 of file aiunit.h.

◆ RAMPAGE_HUT_OR_BETTER

#define RAMPAGE_HUT_OR_BETTER   99998

Definition at line 91 of file aiunit.h.

◆ simple_ai_unit_type_iterate

#define simple_ai_unit_type_iterate (   _ut)
Value:
{ \
struct unit_type *_ut; \
int _ut##_index = 0; \
while (nullptr != (_ut = simple_ai_types[_ut##_index++])) {
struct unit_type * simple_ai_types[U_LAST]
Definition: aiunit.cpp:112

Definition at line 142 of file aiunit.h.

◆ simple_ai_unit_type_iterate_end

#define simple_ai_unit_type_iterate_end
Value:
} \
}

Definition at line 148 of file aiunit.h.

◆ UNITTYPE_COSTS

#define UNITTYPE_COSTS (   ut)
Value:
(ut->pop_cost * 3 + ut->happy_cost + ut->upkeep[O_SHIELD] \
+ ut->upkeep[O_FOOD] + ut->upkeep[O_GOLD])
@ O_SHIELD
Definition: fc_types.h:86
@ O_FOOD
Definition: fc_types.h:85
@ O_GOLD
Definition: fc_types.h:88

Definition at line 80 of file aiunit.h.

◆ WAR

#define WAR (   plr1,
  plr2 
)    (player_diplstate_get(plr1, plr2)->type == DS_WAR)

Definition at line 68 of file aiunit.h.

Enumeration Type Documentation

◆ ai_unit_task

Enumerator
AIUNIT_NONE 
AIUNIT_AUTO_SETTLER 
AIUNIT_BUILD_CITY 
AIUNIT_DEFEND_HOME 
AIUNIT_ATTACK 
AIUNIT_ESCORT 
AIUNIT_EXPLORE 
AIUNIT_RECOVER 
AIUNIT_HUNTER 
AIUNIT_TRADE 
AIUNIT_WONDER 

Definition at line 25 of file aiunit.h.

Function Documentation

◆ build_cost_balanced()

int build_cost_balanced ( const struct unit_type punittype)

In the words of Syela: "Using funky fprime variable instead of f in the denom, so that def=1 units are penalized correctly.

"

Translation (GB): build_cost_balanced is used in the denominator of the want equation (see, e.g. find_something_to_kill) instead of just build_cost to make AI build more balanced units (with def > 1).

Definition at line 240 of file aiunit.cpp.

Referenced by dai_evaluate_tile_for_air_attack(), find_something_to_kill(), and process_attacker_want().

◆ dai_can_unit_type_follow_unit_type()

bool dai_can_unit_type_follow_unit_type ( const struct unit_type follower,
const struct unit_type followee,
struct ai_type ait 
)

See if the follower can follow the followee.

Definition at line 834 of file aiunit.cpp.

Referenced by look_for_charge(), and process_attacker_want().

◆ dai_consider_tile_dangerous()

void dai_consider_tile_dangerous ( struct ai_type ait,
struct tile ptile,
struct unit punit,
enum override_bool result 
)

Are there dangerous enemies at or adjacent to the tile 'ptile'?

Always override advisor danger detection since we are omniscient and advisor is not.

Definition at line 3017 of file aiunit.cpp.

Referenced by cai_consider_tile_dangerous(), texwai_consider_tile_dangerous(), and twai_consider_tile_dangerous().

◆ dai_manage_military()

void dai_manage_military ( struct ai_type ait,
struct player pplayer,
struct unit punit 
)

Decide what to do with a military unit.

It will be managed once only. It is up to the caller to try again if it has moves left.

Definition at line 2399 of file aiunit.cpp.

Referenced by dai_manage_paratrooper(), and dai_manage_unit().

◆ dai_manage_unit()

void dai_manage_unit ( struct ai_type ait,
struct player pplayer,
struct unit punit 
)

manage one unit Careful: punit may have been destroyed upon return from this routine!

Gregor: This function is a very limited approach because if a unit has several flags the first one in order of appearance in this function will be used.

Definition at line 2551 of file aiunit.cpp.

Referenced by dai_manage_barbarian_leader(), and dai_manage_units().

◆ dai_manage_units()

void dai_manage_units ( struct ai_type ait,
struct player pplayer 
)

Master manage unit function.

A manage function should set the unit to 'done' when it should no longer be touched by this code, and its role should be reset to IDLE when its role has accomplished its mission or the manage function fails to have or no longer has any use for the unit.

Definition at line 2761 of file aiunit.cpp.

Referenced by dai_do_first_activities().

◆ dai_military_rampage()

bool dai_military_rampage ( struct unit punit,
int  thresh_adj,
int  thresh_move 
)

Find and kill anything reachable within this turn and worth more than the relevant of the given thresholds until we have run out of juicy targets or movement.

The first threshold is for attacking which will leave us where we stand (attacking adjacent units), the second is for attacking distant (but within reach) targets.

For example, if unit is a bodyguard on duty, it should call dai_military_rampage(punit, 100, RAMPAGE_FREE_CITY_OR_BETTER) meaning "we will move _only_ to pick up a free city but we are happy to attack adjacent squares as long as they are worthy of it".

Returns TRUE if survived the rampage session.

Definition at line 592 of file aiunit.cpp.

Referenced by dai_manage_hitpoint_recovery(), dai_manage_paratrooper(), dai_military_attack(), dai_military_bodyguard(), and dai_military_defend().

◆ dai_role_utype_for_terrain_class()

struct unit_type* dai_role_utype_for_terrain_class ( struct city pcity,
int  role,
enum terrain_class  tc 
)

Get unit type player can build, suitable to role, with given move type.

Definition at line 3344 of file aiunit.cpp.

Referenced by contemplate_terrain_improvements(), and dai_city_choose_build().

◆ dai_switch_to_explore()

void dai_switch_to_explore ( struct ai_type ait,
struct unit punit,
struct tile target,
enum override_bool allow 
)

Switch to autoexploring.

Definition at line 3390 of file aiunit.cpp.

Referenced by cai_switch_to_explore(), texwai_switch_to_explore(), and twai_switch_to_explore().

◆ dai_unit_can_strike_my_unit()

bool dai_unit_can_strike_my_unit ( const struct unit attacker,
const struct unit defender 
)

Returns whether 'attacker' can attack 'defender' immediately.

Definition at line 3356 of file aiunit.cpp.

Referenced by dai_hunter_try_launch().

◆ dai_unit_close()

void dai_unit_close ( struct ai_type ait,
struct unit punit 
)

Free unit from use with default AI.

Definition at line 3264 of file aiunit.cpp.

Referenced by cai_unit_close(), texwai_unit_free(), and twai_unit_free().

◆ dai_unit_init()

void dai_unit_init ( struct ai_type ait,
struct unit punit 
)

Initialize unit for use with default AI.

Definition at line 3229 of file aiunit.cpp.

Referenced by cai_unit_init(), texwai_unit_alloc(), and twai_unit_alloc().

◆ dai_unit_load()

void dai_unit_load ( struct ai_type ait,
const char *  aitstr,
const struct section_file file,
struct unit punit,
const char *  unitstr 
)

Load AI data of a unit.

Definition at line 3302 of file aiunit.cpp.

Referenced by cai_unit_load(), texwai_unit_load(), and twai_unit_load().

◆ dai_unit_save()

void dai_unit_save ( struct ai_type ait,
const char *  aitstr,
struct section_file file,
const struct unit punit,
const char *  unitstr 
)

Save AI data of a unit.

Definition at line 3283 of file aiunit.cpp.

Referenced by cai_unit_save(), texwai_unit_save(), and twai_unit_save().

◆ dai_unit_turn_end()

void dai_unit_turn_end ( struct ai_type ait,
struct unit punit 
)

Free unit from use with default AI.

Definition at line 3252 of file aiunit.cpp.

Referenced by cai_unit_turn_end(), texwai_unit_turn_end(), and twai_unit_turn_end().

◆ dai_units_ruleset_close()

void dai_units_ruleset_close ( struct ai_type ait)

Close AI unit type data.

Definition at line 3208 of file aiunit.cpp.

Referenced by cai_units_ruleset_close(), texwai_units_ruleset_close(), and twai_units_ruleset_close().

◆ dai_units_ruleset_init()

void dai_units_ruleset_init ( struct ai_type ait)

Initialise the unit data from the ruleset for the AI.

Definition at line 3101 of file aiunit.cpp.

Referenced by cai_units_ruleset_init(), texwai_units_ruleset_init(), and twai_units_ruleset_init().

◆ find_beachhead()

bool find_beachhead ( const struct player pplayer,
struct pf_map ferry_map,
struct tile dest_tile,
const struct unit_type cargo_type,
struct tile **  ferry_dest,
struct tile **  beachhead_tile 
)

Returns TRUE if a beachhead as been found to reach 'dest_tile'.

Definition at line 1037 of file aiunit.cpp.

Referenced by find_something_to_kill(), and process_attacker_want().

◆ find_nearest_safe_city()

struct city* find_nearest_safe_city ( struct unit punit)

Find safe city to recover in.

An allied player's city is just as good as one of our own, since both replenish our hitpoints and reduce unhappiness.

TODO: Actually check how safe the city is. This is a difficult decision not easily taken, since we also want to protect unsafe cities, at least most of the time.

Definition at line 1620 of file aiunit.cpp.

Referenced by dai_find_boat_for_unit(), dai_manage_hitpoint_recovery(), dai_manage_paratrooper(), and dai_military_attack().

◆ find_something_to_kill()

int find_something_to_kill ( ai_type ait,
player pplayer,
unit punit,
tile **  pdest_tile,
PFPath ppath,
pf_map **  pferrymap,
unit **  pferryboat,
const unit_type **  pboattype,
int *  pmove_time 
)

◆ kill_desire()

int kill_desire ( int  benefit,
int  attack,
int  loss,
int  vuln,
int  victim_count 
)

Compute how much we want to kill certain victim we've chosen, counted in SHIELDs.

FIXME?: The equation is not accurate as the other values can vary for other victims on same tile (we take values from best defender) - however I believe it's accurate just enough now and lost speed isn't worth that. –pasky

Benefit is something like 'attractiveness' of the victim, how nice it would be to destroy it. Larger value, worse loss for enemy.

Attack is the total possible attack power we can throw on the victim. Note that it usually comes squared.

Loss is the possible loss when we would lose the unit we are attacking with (in SHIELDs).

Vuln is vulnerability of our unit when attacking the enemy. Note that it usually comes squared as well.

Victim count is number of victims stacked in the target tile. Note that we shouldn't treat cities as a stack (despite the code using this function) - the scaling is probably different. (extremely dodgy usage of it – GB)

Definition at line 334 of file aiunit.cpp.

Referenced by dai_evaluate_tile_for_air_attack(), find_something_to_kill(), and process_attacker_want().

◆ look_for_charge()

int look_for_charge ( struct ai_type ait,
struct player pplayer,
struct unit punit,
struct unit **  aunit,
struct city **  acity 
)

See if we can find something to defend.

Called both by wannabe bodyguards and building want estimation code. Returns desirability for using this unit as a bodyguard or for defending a city.

We do not consider units with higher movement than us, or units that are native to terrains or extras not native to us, as potential charges. Nor do we attempt to bodyguard units with higher defence than us, or military units with lower attack than us that are not transports.

Definition at line 707 of file aiunit.cpp.

Referenced by dai_military_findjob(), and dai_unit_consider_bodyguard().

◆ unittype_def_rating_squared()

int unittype_def_rating_squared ( const struct unit_type att_type,
const struct unit_type def_type,
struct player def_player,
struct tile ptile,
bool  fortified,
int  veteran 
)

Defence rating of def_type unit against att_type unit, squared.

See get_virtual_defense_power for the arguments att_type, def_type, x, y, fortified and veteran.

Definition at line 296 of file aiunit.cpp.

Referenced by find_something_to_kill(), kill_something_with(), and process_attacker_want().

◆ utype_needs_improvement()

const struct impr_type* utype_needs_improvement ( const struct unit_type putype,
const struct city pcity 
)

Returns an improvement that will make it possible to build units of the specified type the specified city.

Returns FALSE if no new improvement will make it possible or if no improvement is needed.

Definition at line 2802 of file aiunit.cpp.

Referenced by dai_wants_defender_against(), dai_wants_role_unit(), and process_attacker_want().

Variable Documentation

◆ simple_ai_types

struct unit_type* simple_ai_types[U_LAST]
extern

Definition at line 112 of file aiunit.cpp.

Referenced by update_simple_ai_types().