Freeciv21
Develop your civilization from humble roots to a global empire
aitools.cpp File Reference
#include "bitvector.h"
#include "log.h"
#include "shared.h"
#include "city.h"
#include "combat.h"
#include "game.h"
#include "government.h"
#include "map.h"
#include "movement.h"
#include "nation.h"
#include "packets.h"
#include "player.h"
#include "unit.h"
#include "unitlist.h"
#include "citymap.h"
#include "pf_tools.h"
#include "cityturn.h"
#include "maphand.h"
#include "score.h"
#include "srv_log.h"
#include "unithand.h"
#include "unittools.h"
#include "advdata.h"
#include "advgoto.h"
#include "advtools.h"
#include "infracache.h"
#include "handicaps.h"
#include "aidata.h"
#include "aiferry.h"
#include "aiguard.h"
#include "ailog.h"
#include "aiplayer.h"
#include "aitech.h"
#include "aiunit.h"
#include "daimilitary.h"
#include "aitools.h"
+ Include dependency graph for aitools.cpp:

Go to the source code of this file.

Macros

#define LOGLEVEL_GOTHERE   LOG_DEBUG
 
#define LONG_TIME   4
 

Functions

const char * dai_unit_task_rule_name (const enum ai_unit_task task)
 Return the (untranslated) rule name of the ai_unit_task. More...
 
const char * dai_choice_rule_name (const struct adv_choice *choice)
 Return the (untranslated) rule name of the adv_choice. More...
 
int military_amortize (struct player *pplayer, struct city *pcity, int value, int delay, int build_cost)
 Amortize a want modified by the shields (build_cost) we risk losing. More...
 
void dai_consider_plr_dangerous (struct ai_type *ait, struct player *plr1, struct player *plr2, enum override_bool *result)
 There are some signs that a player might be dangerous: We are at war with him, he has done lots of ignoble things to us, he is an ally of one of our enemies (a ticking bomb to be sure), he is our war target, we don't like him, diplomatic state is neutral or we have case fire. More...
 
static bool dai_gothere_bodyguard (struct ai_type *ait, struct unit *punit, struct tile *dest_tile)
 A helper function for ai_gothere. More...
 
bool dai_gothere (struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile)
 This is ferry-enabled goto. More...
 
struct tileimmediate_destination (struct unit *punit, struct tile *dest_tile)
 Returns the destination for a unit moving towards a given final destination. More...
 
void dai_log_path (struct unit *punit, const PFPath &path, struct pf_parameter *parameter)
 Log the cost of travelling a path. More...
 
bool dai_unit_goto_constrained (struct ai_type *ait, struct unit *punit, struct tile *ptile, struct pf_parameter *parameter)
 Go to specified destination, subject to given PF constraints, but do not disturb existing role or activity and do not clear the role's destination. More...
 
bool goto_is_sane (struct unit *punit, struct tile *ptile)
 Use pathfinding to determine whether a GOTO is possible, considering all aspects of the unit being moved and the terrain under consideration. More...
 
void dai_fill_unit_param (struct ai_type *ait, struct pf_parameter *parameter, struct adv_risk_cost *risk_cost, struct unit *punit, struct tile *ptile)
 Set up the constraints on a path for an AI unit. More...
 
bool dai_unit_goto (struct ai_type *ait, struct unit *punit, struct tile *ptile)
 Go to specified destination but do not disturb existing role or activity and do not clear the role's destination. More...
 
void dai_unit_new_adv_task (struct ai_type *ait, struct unit *punit, enum adv_unit_task task, struct tile *ptile)
 Adviser task for unit has been changed. More...
 
void dai_unit_new_task (struct ai_type *ait, struct unit *punit, enum ai_unit_task task, struct tile *ptile)
 Ensure unit sanity by telling charge that we won't bodyguard it anymore, tell bodyguard it can roam free if our job is done, add and remove city spot reservation, and set destination. More...
 
bool dai_unit_make_homecity (struct unit *punit, struct city *pcity)
 Try to make pcity our new homecity. More...
 
static void dai_unit_bodyguard_move (struct ai_type *ait, struct unit *bodyguard, struct tile *ptile)
 Move a bodyguard along with another unit. More...
 
bool dai_unit_attack (struct ai_type *ait, struct unit *punit, struct tile *ptile)
 Move and attack with an ai unit. More...
 
void dai_unit_move_or_attack (struct ai_type *ait, struct unit *punit, struct tile *ptile, const PFPath &path, int step)
 Ai unit moving function called from AI interface. More...
 
bool dai_unit_move (struct ai_type *ait, struct unit *punit, struct tile *ptile)
 Move a unit. More...
 
int stack_cost (struct unit *pattacker, struct unit *pdefender)
 Calculate the value of the target unit including the other units which will die in a successful attack. More...
 
void dai_government_change (struct player *pplayer, struct government *gov)
 Change government, pretty fast... More...
 
int dai_gold_reserve (struct player *pplayer)
 Credits the AI wants to have in reserves. More...
 
bool dai_choose_role_unit (struct ai_type *ait, struct player *pplayer, struct city *pcity, struct adv_choice *choice, enum choice_type type, int role, int want, bool need_boat)
 Calls dai_wants_role_unit to choose the best unit with the given role and set tech wants. More...
 
void dai_build_adv_override (struct ai_type *ait, struct city *pcity, struct adv_choice *choice)
 Consider overriding building target selected by common advisor code. More...
 
bool dai_assess_military_unhappiness (struct city *pcity)
 "The following evaluates the unhappiness caused by military units in the field (or aggressive) at a city when at Republic or Democracy. More...
 

Macro Definition Documentation

◆ LOGLEVEL_GOTHERE

#define LOGLEVEL_GOTHERE   LOG_DEBUG

Definition at line 248 of file aitools.cpp.

◆ LONG_TIME

#define LONG_TIME   4

Definition at line 462 of file aitools.cpp.

Function Documentation

◆ dai_assess_military_unhappiness()

bool dai_assess_military_unhappiness ( struct city pcity)

"The following evaluates the unhappiness caused by military units in the field (or aggressive) at a city when at Republic or Democracy.

Now generalised somewhat for government rulesets, though I'm not sure whether it is fully general for all possible parameters/ combinations." –dwp

Definition at line 1203 of file aitools.cpp.

Referenced by find_something_to_kill(), and process_attacker_want().

◆ dai_build_adv_override()

void dai_build_adv_override ( struct ai_type ait,
struct city pcity,
struct adv_choice choice 
)

Consider overriding building target selected by common advisor code.

Definition at line 1155 of file aitools.cpp.

Referenced by cai_build_adv_override(), texwai_build_adv_override(), and twai_build_adv_override().

◆ dai_choice_rule_name()

const char* dai_choice_rule_name ( const struct adv_choice choice)

Return the (untranslated) rule name of the adv_choice.

You don't have to free the return pointer.

Definition at line 105 of file aitools.cpp.

Referenced by dai_city_choose_build(), dai_spend_gold(), and military_advisor_choose_build().

◆ dai_choose_role_unit()

bool dai_choose_role_unit ( struct ai_type ait,
struct player pplayer,
struct city pcity,
struct adv_choice choice,
enum choice_type  type,
int  role,
int  want,
bool  need_boat 
)

Calls dai_wants_role_unit to choose the best unit with the given role and set tech wants.

Sets choice->value.utype when we can build something.

Definition at line 1131 of file aitools.cpp.

Referenced by domestic_advisor_choose_build(), and kill_something_with().

◆ dai_consider_plr_dangerous()

void dai_consider_plr_dangerous ( struct ai_type ait,
struct player plr1,
struct player plr2,
enum override_bool result 
)

There are some signs that a player might be dangerous: We are at war with him, he has done lots of ignoble things to us, he is an ally of one of our enemies (a ticking bomb to be sure), he is our war target, we don't like him, diplomatic state is neutral or we have case fire.

This function is used for example to check if pplayer can leave his city undefended when aplayer's units are near it.

Definition at line 156 of file aitools.cpp.

Referenced by cai_consider_plr_dangerous(), texwai_consider_plr_dangerous(), and twai_consider_plr_dangerous().

◆ dai_fill_unit_param()

void dai_fill_unit_param ( struct ai_type ait,
struct pf_parameter parameter,
struct adv_risk_cost risk_cost,
struct unit punit,
struct tile ptile 
)

Set up the constraints on a path for an AI unit.

parameter: constraints (output) risk_cost: auxiliary data used by the constraints (output) ptile: the destination of the unit. For ferries, the destination may be a coastal land tile, in which case the ferry should stop on an adjacent tile.

Definition at line 475 of file aitools.cpp.

Referenced by dai_unit_goto().

◆ dai_gold_reserve()

int dai_gold_reserve ( struct player pplayer)

Credits the AI wants to have in reserves.

We need some gold to bribe and incite cities.

"I still don't trust this function" – Syela

Definition at line 1120 of file aitools.cpp.

Referenced by dai_diplomat_bribe_nearby(), dai_manage_taxes(), and dai_spend_gold().

◆ dai_gothere()

bool dai_gothere ( struct ai_type ait,
struct player pplayer,
struct unit punit,
struct tile dest_tile 
)

This is ferry-enabled goto.

Should not normally be used for non-ferried units (i.e. planes or ships), use dai_unit_goto instead.

Return values: TRUE if got to or next to our destination, FALSE otherwise.

TODO: A big one is rendezvous points. When this is implemented, we won't have to be at the coast to ask for a boat to come to us.

Definition at line 258 of file aitools.cpp.

Referenced by dai_auto_settler_run(), dai_caravan_goto(), dai_military_attack_barbarian(), dai_military_bodyguard(), and dai_military_defend().

◆ dai_gothere_bodyguard()

static bool dai_gothere_bodyguard ( struct ai_type ait,
struct unit punit,
struct tile dest_tile 
)
static

A helper function for ai_gothere.

Estimates the dangers we will be facing at our destination and tries to find/request a bodyguard if needed.

Definition at line 175 of file aitools.cpp.

Referenced by dai_gothere().

◆ dai_government_change()

void dai_government_change ( struct player pplayer,
struct government gov 
)

Change government, pretty fast...

Definition at line 1099 of file aitools.cpp.

Referenced by dai_manage_government().

◆ dai_log_path()

void dai_log_path ( struct unit punit,
const PFPath path,
struct pf_parameter parameter 
)

Log the cost of travelling a path.

Definition at line 359 of file aitools.cpp.

Referenced by dai_unit_goto_constrained().

◆ dai_unit_attack()

bool dai_unit_attack ( struct ai_type ait,
struct unit punit,
struct tile ptile 
)

Move and attack with an ai unit.

We do not wait for server reply.

Definition at line 825 of file aitools.cpp.

Referenced by dai_hunter_try_launch(), dai_manage_airunit(), dai_military_attack(), and dai_unit_move_or_attack().

◆ dai_unit_bodyguard_move()

static void dai_unit_bodyguard_move ( struct ai_type ait,
struct unit bodyguard,
struct tile ptile 
)
static

Move a bodyguard along with another unit.

We assume that unit has already been moved to ptile which is a valid, safe tile, and that our bodyguard has not. This is an ai_unit_* auxiliary function, do not use elsewhere.

Definition at line 792 of file aitools.cpp.

Referenced by dai_unit_attack(), and dai_unit_move().

◆ dai_unit_goto()

bool dai_unit_goto ( struct ai_type ait,
struct unit punit,
struct tile ptile 
)

Go to specified destination but do not disturb existing role or activity and do not clear the role's destination.

Return FALSE iff we died.

Definition at line 609 of file aitools.cpp.

Referenced by dai_caravan_goto(), dai_find_boat_for_unit(), dai_gothere(), dai_hunter_try_launch(), dai_manage_barbarian_leader(), dai_manage_hitpoint_recovery(), dai_manage_paratrooper(), dai_military_attack(), and search_homecity_for_caravan().

◆ dai_unit_goto_constrained()

bool dai_unit_goto_constrained ( struct ai_type ait,
struct unit punit,
struct tile ptile,
struct pf_parameter parameter 
)

Go to specified destination, subject to given PF constraints, but do not disturb existing role or activity and do not clear the role's destination.

Return FALSE iff we died.

parameter: the PF constraints on the computed path. The unit will move as far along the computed path is it can; the movement code will impose all the real constraints (ZoC, etc).

Definition at line 381 of file aitools.cpp.

Referenced by dai_unit_goto().

◆ dai_unit_make_homecity()

bool dai_unit_make_homecity ( struct unit punit,
struct city pcity 
)

Try to make pcity our new homecity.

Fails if we can't upkeep it. Assumes success from server.

Definition at line 763 of file aitools.cpp.

Referenced by search_homecity_for_caravan().

◆ dai_unit_move()

bool dai_unit_move ( struct ai_type ait,
struct unit punit,
struct tile ptile 
)

Move a unit.

Do not attack. Do not leave bodyguard. For AI units.

This function returns only when we have a reply from the server and we can tell the calling function what happened to the move request. (Right now it is not a big problem, since we call the server directly.)

Definition at line 947 of file aitools.cpp.

Referenced by dai_unit_bodyguard_move(), and dai_unit_move_or_attack().

◆ dai_unit_move_or_attack()

void dai_unit_move_or_attack ( struct ai_type ait,
struct unit punit,
struct tile ptile,
const PFPath path,
int  step 
)

Ai unit moving function called from AI interface.

Definition at line 928 of file aitools.cpp.

Referenced by cai_unit_move_or_attack(), texwai_unit_move_or_attack(), and twai_unit_move_or_attack().

◆ dai_unit_new_adv_task()

void dai_unit_new_adv_task ( struct ai_type ait,
struct unit punit,
enum adv_unit_task  task,
struct tile ptile 
)

Adviser task for unit has been changed.

Definition at line 624 of file aitools.cpp.

Referenced by cai_unit_new_adv_task(), texwai_unit_new_adv_task(), and twai_unit_new_adv_task().

◆ dai_unit_new_task()

void dai_unit_new_task ( struct ai_type ait,
struct unit punit,
enum ai_unit_task  task,
struct tile ptile 
)

Ensure unit sanity by telling charge that we won't bodyguard it anymore, tell bodyguard it can roam free if our job is done, add and remove city spot reservation, and set destination.

If we set a unit to hunter, also reserve its target, and try to load it with cruise missiles or nukes to bring along.

Definition at line 648 of file aitools.cpp.

Referenced by dai_auto_settler_run(), dai_diplomat_city(), dai_do_build_city(), dai_hunter_manage(), dai_manage_caravan(), dai_manage_diplomat(), dai_manage_hitpoint_recovery(), dai_manage_military(), dai_manage_unit(), dai_manage_units(), dai_military_bodyguard(), dai_military_findjob(), dai_set_defenders(), and dai_unit_new_adv_task().

◆ dai_unit_task_rule_name()

const char* dai_unit_task_rule_name ( const enum ai_unit_task  task)

Return the (untranslated) rule name of the ai_unit_task.

You don't have to free the return pointer.

Definition at line 70 of file aitools.cpp.

Referenced by dai_caravan_goto(), dai_manage_caravan(), and dai_unit_new_task().

◆ goto_is_sane()

bool goto_is_sane ( struct unit punit,
struct tile ptile 
)

Use pathfinding to determine whether a GOTO is possible, considering all aspects of the unit being moved and the terrain under consideration.

Don't bother with pathfinding if the unit is already there.

Definition at line 436 of file aitools.cpp.

Referenced by dai_gothere(), dai_is_unit_tired_waiting_boat(), dai_manage_barbarian_leader(), dai_military_bodyguard(), and dai_unit_goto_constrained().

◆ immediate_destination()

struct tile* immediate_destination ( struct unit punit,
struct tile dest_tile 
)

Returns the destination for a unit moving towards a given final destination.

That is, it gives a suitable way-point, if necessary. For example, aircraft need these way-points to refuel.

Definition at line 314 of file aitools.cpp.

Referenced by dai_unit_goto_constrained().

◆ military_amortize()

int military_amortize ( struct player pplayer,
struct city pcity,
int  value,
int  delay,
int  build_cost 
)

Amortize a want modified by the shields (build_cost) we risk losing.

We add the build time of the unit(s) we risk to amortize delay. The build time is calculated as the build cost divided by the production output of the unit's homecity or the city where we want to produce the unit. If the city has less than average shield output, we instead use the average, to encourage long-term thinking.

Definition at line 132 of file aitools.cpp.

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

◆ stack_cost()

int stack_cost ( struct unit pattacker,
struct unit pdefender 
)

Calculate the value of the target unit including the other units which will die in a successful attack.

Definition at line 1072 of file aitools.cpp.

Referenced by dai_evaluate_tile_for_air_attack(), and dai_rampage_want().