Freeciv21
Develop your civilization from humble roots to a global empire
daimilitary.cpp File Reference
#include <cstring>
#include "log.h"
#include "combat.h"
#include "game.h"
#include "government.h"
#include "map.h"
#include "movement.h"
#include "research.h"
#include "specialist.h"
#include "unitlist.h"
#include "pf_tools.h"
#include "srv_log.h"
#include "srv_main.h"
#include "advbuilding.h"
#include "advchoice.h"
#include "advdata.h"
#include "advtools.h"
#include "infracache.h"
#include "aitraits.h"
#include "difficulty.h"
#include "handicaps.h"
#include "aiair.h"
#include "aidata.h"
#include "aidiplomat.h"
#include "aiferry.h"
#include "aihand.h"
#include "aihunt.h"
#include "ailog.h"
#include "aiparatrooper.h"
#include "aiplayer.h"
#include "aitech.h"
#include "aitools.h"
#include "aiunit.h"
#include "daicity.h"
#include "daieffects.h"
#include "daimilitary.h"
+ Include dependency graph for daimilitary.cpp:

Go to the source code of this file.

Functions

static int assess_danger (struct ai_type *ait, struct city *pcity, const struct civ_map *dmap, player_unit_list_getter ul_cb)
 Create cached information about danger, urgency and grave danger to our cities. More...
 
struct unit_typedai_choose_defender_versus (struct city *pcity, struct unit *attacker)
 Choose the best unit the city can build to defend against attacker v. More...
 
static struct unit_typedai_choose_attacker (struct ai_type *ait, struct city *pcity, enum terrain_class tc, bool allow_gold_upkeep)
 Choose best attacker based on movement type. More...
 
static struct unit_typedai_choose_bodyguard (struct ai_type *ait, struct city *pcity, enum terrain_class tc, enum unit_role_id role, bool allow_gold_upkeep)
 Choose best defender based on movement type. More...
 
static int base_assess_defense_unit (struct city *pcity, struct unit *punit, bool igwall, bool quadratic, int wall_value)
 Helper for assess_defense_quadratic and assess_defense_unit. More...
 
int assess_defense_quadratic (struct ai_type *ait, struct city *pcity)
 Need positive feedback in m_a_c_b and bodyguard routines. More...
 
int assess_defense_unit (struct ai_type *ait, struct city *pcity, struct unit *punit, bool igwall)
 One unit only, mostly for findjob; handling boats correctly. More...
 
static int assess_defense_backend (struct ai_type *ait, struct city *pcity, bool igwall)
 Most of the time we don't need/want positive feedback. More...
 
int assess_defense (struct ai_type *ait, struct city *pcity)
 Estimate defense strength of city. More...
 
static int assess_defense_igwall (struct ai_type *ait, struct city *pcity)
 Estimate defense strength of city without considering how buildings help defense. More...
 
static int assess_danger_unit (const struct city *pcity, struct pf_reverse_map *pcity_map, const struct unit *punit, int *move_time)
 How dangerous and far a unit is for a city? More...
 
void dai_assess_danger_player (struct ai_type *ait, struct player *pplayer, const struct civ_map *dmap)
 Call assess_danger() for all cities owned by pplayer. More...
 
static void dai_reevaluate_building (struct city *pcity, adv_want *value, int urgency, int danger, int defense)
 Set (overwrite) our want for a building. More...
 
int dai_unit_defence_desirability (struct ai_type *ait, const struct unit_type *punittype)
 How much we would want that unit to defend a city? (Do not use this function to find bodyguards for ships or air units.) More...
 
int dai_unit_attack_desirability (struct ai_type *ait, const struct unit_type *punittype)
 How much we would want that unit to attack with? More...
 
bool dai_process_defender_want (struct ai_type *ait, struct player *pplayer, struct city *pcity, int danger, struct adv_choice *choice)
 What would be the best defender for that city? Records the best defender type in choice. More...
 
static void process_attacker_want (struct ai_type *ait, struct city *pcity, int value, const struct unit_type *victim_unit_type, struct player *victim_player, int veteran, struct tile *ptile, struct adv_choice *best_choice, struct pf_map *ferry_map, struct unit *boat, const struct unit_type *boattype)
 This function decides, what unit would be best for erasing enemy. More...
 
static struct adv_choicekill_something_with (struct ai_type *ait, struct player *pplayer, struct city *pcity, struct unit *myunit, struct adv_choice *choice)
 This function. More...
 
static void dai_unit_consider_bodyguard (struct ai_type *ait, struct city *pcity, struct unit_type *punittype, struct adv_choice *choice)
 This function should assign a value to choice and want and type, where want is a value between 1 and 100. More...
 
static void adjust_ai_unit_choice (struct city *pcity, struct adv_choice *choice)
 Before building a military unit, AI builds a barracks/port/airport NB: It is assumed this function isn't called in an emergency situation, when we need a defender now. More...
 
struct adv_choicemilitary_advisor_choose_build (struct ai_type *ait, struct player *pplayer, struct city *pcity, const struct civ_map *mamap, player_unit_list_getter ul_cb)
 This function selects either a defender or an attacker to be built. More...
 

Function Documentation

◆ adjust_ai_unit_choice()

static void adjust_ai_unit_choice ( struct city pcity,
struct adv_choice choice 
)
static

Before building a military unit, AI builds a barracks/port/airport NB: It is assumed this function isn't called in an emergency situation, when we need a defender now.

TODO: something more sophisticated, like estimating future demand for military units, considering Sun Tzu instead.

Definition at line 1421 of file daimilitary.cpp.

Referenced by military_advisor_choose_build().

◆ assess_danger()

static int assess_danger ( struct ai_type ait,
struct city pcity,
const struct civ_map dmap,
player_unit_list_getter  ul_cb 
)
static

Create cached information about danger, urgency and grave danger to our cities.

Danger is a weight on how much power enemy units nearby have, which is compared to our defence.

Urgency is the number of hostile units that can attack us in three turns.

Grave danger is number of units that can attack us next turn.

FIXME: We do not consider a paratrooper's mr_req and mr_sub fields. Not a big deal, though.

FIXME: Due to the nature of assess_distance, a city will only be afraid of a boat laden with enemies if it stands on the coast (i.e. is directly reachable by this boat).

Definition at line 481 of file daimilitary.cpp.

Referenced by dai_assess_danger_player(), and military_advisor_choose_build().

◆ assess_danger_unit()

static int assess_danger_unit ( const struct city pcity,
struct pf_reverse_map pcity_map,
const struct unit punit,
int *  move_time 
)
static

How dangerous and far a unit is for a city?

Definition at line 350 of file daimilitary.cpp.

Referenced by assess_danger().

◆ assess_defense()

int assess_defense ( struct ai_type ait,
struct city pcity 
)

Estimate defense strength of city.

Definition at line 333 of file daimilitary.cpp.

Referenced by dai_spend_gold(), and kill_something_with().

◆ assess_defense_backend()

static int assess_defense_backend ( struct ai_type ait,
struct city pcity,
bool  igwall 
)
static

Most of the time we don't need/want positive feedback.

– Syela

It's unclear whether this should treat settlers/caravans as defense. – Syela TODO: It looks like this is never used while deciding if we should attack pcity, if we have pcity defended properly, so I think it should. –pasky

Definition at line 315 of file daimilitary.cpp.

Referenced by assess_defense(), and assess_defense_igwall().

◆ assess_defense_igwall()

static int assess_defense_igwall ( struct ai_type ait,
struct city pcity 
)
static

Estimate defense strength of city without considering how buildings help defense.

Definition at line 342 of file daimilitary.cpp.

Referenced by assess_danger().

◆ assess_defense_quadratic()

int assess_defense_quadratic ( struct ai_type ait,
struct city pcity 
)

Need positive feedback in m_a_c_b and bodyguard routines.

– Syela

Definition at line 262 of file daimilitary.cpp.

Referenced by dai_city_choose_build(), dai_military_findjob(), look_for_charge(), and military_advisor_choose_build().

◆ assess_defense_unit()

int assess_defense_unit ( struct ai_type ait,
struct city pcity,
struct unit punit,
bool  igwall 
)

One unit only, mostly for findjob; handling boats correctly.

980803 – Syela

Definition at line 300 of file daimilitary.cpp.

Referenced by assess_defense_backend(), and dai_set_defenders().

◆ base_assess_defense_unit()

static int base_assess_defense_unit ( struct city pcity,
struct unit punit,
bool  igwall,
bool  quadratic,
int  wall_value 
)
static

Helper for assess_defense_quadratic and assess_defense_unit.

Definition at line 220 of file daimilitary.cpp.

Referenced by assess_defense_quadratic(), and assess_defense_unit().

◆ dai_assess_danger_player()

void dai_assess_danger_player ( struct ai_type ait,
struct player pplayer,
const struct civ_map dmap 
)

Call assess_danger() for all cities owned by pplayer.

This is necessary to initialize some ai data before some ai calculations.

Definition at line 415 of file daimilitary.cpp.

Referenced by cai_split_by_civil_war(), dai_do_first_activities(), dai_gained_control(), texwai_split_by_civil_war(), and twai_split_by_civil_war().

◆ dai_choose_attacker()

static struct unit_type* dai_choose_attacker ( struct ai_type ait,
struct city pcity,
enum terrain_class  tc,
bool  allow_gold_upkeep 
)
static

Choose best attacker based on movement type.

It chooses based on unit desirability without regard to cost, unless costs are equal. This is very wrong. FIXME, use amortize on time to build.

Definition at line 119 of file daimilitary.cpp.

Referenced by military_advisor_choose_build().

◆ dai_choose_bodyguard()

static struct unit_type* dai_choose_bodyguard ( struct ai_type ait,
struct city pcity,
enum terrain_class  tc,
enum unit_role_id  role,
bool  allow_gold_upkeep 
)
static

Choose best defender based on movement type.

It chooses based on unit desirability without regard to cost, unless costs are equal. This is very wrong. FIXME, use amortize on time to build.

We should only be passed with L_DEFEND_GOOD role for now, since this is the only role being considered worthy of bodyguarding in findjob.

Definition at line 165 of file daimilitary.cpp.

Referenced by military_advisor_choose_build().

◆ dai_choose_defender_versus()

struct unit_type* dai_choose_defender_versus ( struct city pcity,
struct unit attacker 
)

Choose the best unit the city can build to defend against attacker v.

Definition at line 72 of file daimilitary.cpp.

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

◆ dai_process_defender_want()

bool dai_process_defender_want ( struct ai_type ait,
struct player pplayer,
struct city pcity,
int  danger,
struct adv_choice choice 
)

What would be the best defender for that city? Records the best defender type in choice.

Also sets the technology want for the units we can't build yet.

Definition at line 767 of file daimilitary.cpp.

Referenced by dai_city_choose_build(), and military_advisor_choose_build().

◆ dai_reevaluate_building()

static void dai_reevaluate_building ( struct city pcity,
adv_want value,
int  urgency,
int  danger,
int  defense 
)
static

Set (overwrite) our want for a building.

Syela tries to explain:

My first attempt to allow ng_wa >= 200 led to stupidity in cities with no defenders and danger = 0 but danger > 0. Capping ng_wa at 100 + urgency led to a failure to buy walls as required. Allowing want > 100 with !urgency led to the AI spending too much gold and falling behind on science. I'm trying again, but this will require yet more tedious observation – Syela

The idea in this horrible function is that there is an enemy nearby that can whack us, so let's build something that can defend against him. If danger is urgent and overwhelming, danger is 200+, if it is only overwhelming, set it depending on danger. If it is underwhelming, set it to 100 plus urgency.

This algorithm is very strange. But I created it by nesting up Syela's convoluted if ... else logic, and it seems to work. – Per

Definition at line 447 of file daimilitary.cpp.

Referenced by assess_danger().

◆ dai_unit_attack_desirability()

int dai_unit_attack_desirability ( struct ai_type ait,
const struct unit_type punittype 
)

How much we would want that unit to attack with?

Definition at line 733 of file daimilitary.cpp.

Referenced by dai_choose_attacker().

◆ dai_unit_consider_bodyguard()

static void dai_unit_consider_bodyguard ( struct ai_type ait,
struct city pcity,
struct unit_type punittype,
struct adv_choice choice 
)
static

This function should assign a value to choice and want and type, where want is a value between 1 and 100.

if want is 0 this advisor doesn't want anything

Definition at line 1385 of file daimilitary.cpp.

Referenced by military_advisor_choose_build().

◆ dai_unit_defence_desirability()

int dai_unit_defence_desirability ( struct ai_type ait,
const struct unit_type punittype 
)

How much we would want that unit to defend a city? (Do not use this function to find bodyguards for ships or air units.)

Definition at line 697 of file daimilitary.cpp.

Referenced by dai_choose_bodyguard(), and dai_process_defender_want().

◆ kill_something_with()

static struct adv_choice* kill_something_with ( struct ai_type ait,
struct player pplayer,
struct city pcity,
struct unit myunit,
struct adv_choice choice 
)
static

This function.

  1. receives (in myunit) a first estimate of what we would like to build.
  2. finds a potential victim for it.
  3. calculates the relevant stats of the victim.
  4. finds the best attacker for this type of victim (in process_attacker_want)
  5. if we still want to attack, records the best attacker in choice. If the target is overseas, the function might suggest building a ferry to carry a land attack unit, instead of the land attack unit itself.

Definition at line 1186 of file daimilitary.cpp.

Referenced by military_advisor_choose_build().

◆ military_advisor_choose_build()

struct adv_choice* military_advisor_choose_build ( struct ai_type ait,
struct player pplayer,
struct city pcity,
const struct civ_map mamap,
player_unit_list_getter  ul_cb 
)

This function selects either a defender or an attacker to be built.

It records its choice into adv_choice struct. If 'choice->want' is 0 this advisor doesn't want anything.

Definition at line 1454 of file daimilitary.cpp.

Referenced by dai_manage_cities().

◆ process_attacker_want()

static void process_attacker_want ( struct ai_type ait,
struct city pcity,
int  value,
const struct unit_type victim_unit_type,
struct player victim_player,
int  veteran,
struct tile ptile,
struct adv_choice best_choice,
struct pf_map ferry_map,
struct unit boat,
const struct unit_type boattype 
)
static

This function decides, what unit would be best for erasing enemy.

It is called, when we just want to kill something, we've found it but we don't have the unit for killing that built yet - here we'll choose the type of that unit.

We will also set increase the technology want to get units which could perform the job better.

I decided this funct wasn't confusing enough, so I made kill_something_with() send it some more variables for it to meddle with. – Syela

'ptile' is location of the target. best_choice is pre-filled with our current choice, we only consider units who can move in all the same terrains for best_choice.

Definition at line 930 of file daimilitary.cpp.

Referenced by kill_something_with().