Freeciv-3.3
Loading...
Searching...
No Matches
Macros | Functions
daimilitary.c File Reference
#include <string.h>
#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 "citytools.h"
#include "cityturn.h"
#include "srv_log.h"
#include "srv_main.h"
#include "advbuilding.h"
#include "advchoice.h"
#include "advdata.h"
#include "advgoto.h"
#include "advtools.h"
#include "infracache.h"
#include "aitraits.h"
#include "difficulty.h"
#include "handicaps.h"
#include "aidiplomat.h"
#include "aiferry.h"
#include "aihand.h"
#include "daiair.h"
#include "daicity.h"
#include "daidata.h"
#include "daieffects.h"
#include "daihunter.h"
#include "dailog.h"
#include "daiparadrop.h"
#include "daiplayer.h"
#include "daitech.h"
#include "daitools.h"
#include "daimilitary.h"

Go to the source code of this file.

Macros

#define CITY_CONQUEST_WORTH(_city_, _data_)    (_data_->worth * 0.9 + (city_size_get(_city_) - 0.5) * 10)
 

Functions

static unsigned int assess_danger (struct ai_type *ait, const struct civ_map *nmap, struct city *pcity, player_unit_list_getter ul_cb)
 
static adv_want dai_unit_attack_desirability (struct ai_type *ait, const struct unit_type *punittype)
 
static adv_want dai_unit_defense_desirability (struct ai_type *ait, const struct unit_type *punittype)
 
struct unit_typedai_choose_defender_versus (struct city *pcity, struct unit *attacker)
 
static struct unit_typedai_choose_attacker (struct ai_type *ait, struct city *pcity, enum terrain_class tc, bool allow_gold_upkeep)
 
static struct unit_typedai_choose_bodyguard (struct ai_type *ait, const struct civ_map *nmap, struct city *pcity, enum terrain_class tc, enum unit_role_id role, bool allow_gold_upkeep)
 
static int base_assess_defense_unit (struct city *pcity, struct unit *punit, bool igwall, bool quadratic, int wall_value)
 
int assess_defense_quadratic (struct ai_type *ait, struct city *pcity)
 
int assess_defense_unit (struct ai_type *ait, struct city *pcity, struct unit *punit, bool igwall)
 
static int assess_defense_backend (struct ai_type *ait, struct city *pcity, bool igwall)
 
int assess_defense (struct ai_type *ait, struct city *pcity)
 
static int assess_defense_igwall (struct ai_type *ait, struct city *pcity)
 
static enum fc_tristate tactical_req_cb (const struct req_context *context, const struct req_context *other_context, const struct requirement *req, void *data, int n_data)
 
static bool action_may_happen_unit_on_city (const action_id wanted_action, const struct unit *actor, const struct city *pcity, int turns)
 
static unsigned int assess_danger_unit (const struct civ_map *nmap, const struct city *pcity, struct pf_reverse_map *pcity_map, const struct unit *punit, int *move_time)
 
void dai_assess_danger_player (struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer)
 
static void dai_reevaluate_building (struct city *pcity, adv_want *value, unsigned int urgency, unsigned int danger, int defense)
 
bool dai_process_defender_want (struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct city *pcity, unsigned int danger, struct adv_choice *choice, adv_want extra_want)
 
static void process_attacker_want (struct ai_type *ait, struct city *pcity, adv_want 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 struct adv_choicekill_something_with (struct ai_type *ait, struct player *pplayer, struct city *pcity, struct unit *myunit, struct adv_choice *choice)
 
static void dai_unit_consider_bodyguard (struct ai_type *ait, struct city *pcity, struct unit_type *punittype, struct adv_choice *choice)
 
static void adjust_ai_unit_choice (struct city *pcity, struct adv_choice *choice)
 
struct adv_choicemilitary_advisor_choose_build (struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct city *pcity, player_unit_list_getter ul_cb)
 

Macro Definition Documentation

◆ CITY_CONQUEST_WORTH

#define CITY_CONQUEST_WORTH (   _city_,
  _data_ 
)     (_data_->worth * 0.9 + (city_size_get(_city_) - 0.5) * 10)

Definition at line 74 of file daimilitary.c.

Function Documentation

◆ action_may_happen_unit_on_city()

static bool action_may_happen_unit_on_city ( const action_id  wanted_action,
const struct unit actor,
const struct city pcity,
int  turns 
)
static

See if there is an enabler for particular action to be targeted at pcity's tile after turns (for 5 turns more). Note that hard reqs are not tested except for utype and that the actor player is ignored; also, it's not tested can utype attack the city's particular terrain.

Definition at line 488 of file daimilitary.c.

Referenced by assess_danger_unit().

◆ 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 1752 of file daimilitary.c.

Referenced by military_advisor_choose_build().

◆ assess_danger()

static unsigned int assess_danger ( struct ai_type ait,
const struct civ_map nmap,
struct city pcity,
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 defense.

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 703 of file daimilitary.c.

Referenced by dai_assess_danger_player(), and military_advisor_choose_build().

◆ assess_danger_unit()

static unsigned int assess_danger_unit ( const struct civ_map nmap,
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 530 of file daimilitary.c.

Referenced by assess_danger().

◆ assess_defense()

int assess_defense ( struct ai_type ait,
struct city pcity 
)

Estimate defense strength of city

Definition at line 344 of file daimilitary.c.

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 328 of file daimilitary.c.

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 353 of file daimilitary.c.

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 280 of file daimilitary.c.

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 314 of file daimilitary.c.

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

◆ dai_assess_danger_player()

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

Call assess_danger() for all cities owned by pplayer.

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

Definition at line 637 of file daimilitary.c.

Referenced by cai_split_by_civil_war(), dai_do_first_activities(), dai_gained_control(), and texwai_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 144 of file daimilitary.c.

Referenced by military_advisor_choose_build().

◆ dai_choose_bodyguard()

static struct unit_type * dai_choose_bodyguard ( struct ai_type ait,
const struct civ_map nmap,
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 186 of file daimilitary.c.

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 90 of file daimilitary.c.

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,
const struct civ_map nmap,
struct player pplayer,
struct city pcity,
unsigned int  danger,
struct adv_choice choice,
adv_want  extra_want 
)

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 1060 of file daimilitary.c.

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,
unsigned int  urgency,
unsigned 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 them. 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 668 of file daimilitary.c.

Referenced by assess_danger().

◆ dai_unit_attack_desirability()

static adv_want dai_unit_attack_desirability ( struct ai_type ait,
const struct unit_type punittype 
)
static

How much we would want that unit to attack with?

Definition at line 1025 of file daimilitary.c.

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 0.0 and DAI_WANT_MILITARY_EMERGENCY (not inclusive) if want is 0 this advisor doesn't want anything

Definition at line 1717 of file daimilitary.c.

Referenced by military_advisor_choose_build().

◆ dai_unit_defense_desirability()

static adv_want dai_unit_defense_desirability ( struct ai_type ait,
const struct unit_type punittype 
)
static

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 988 of file daimilitary.c.

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 1518 of file daimilitary.c.

Referenced by military_advisor_choose_build().

◆ military_advisor_choose_build()

struct adv_choice * military_advisor_choose_build ( struct ai_type ait,
const struct civ_map nmap,
struct player pplayer,
struct city pcity,
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 1782 of file daimilitary.c.

Referenced by dai_manage_cities(), and texai_check_messages().

◆ process_attacker_want()

static void process_attacker_want ( struct ai_type ait,
struct city pcity,
adv_want  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 1240 of file daimilitary.c.

Referenced by kill_something_with().

◆ tactical_req_cb()

static enum fc_tristate tactical_req_cb ( const struct req_context context,
const struct req_context other_context,
const struct requirement req,
void data,
int  n_data 
)
static

While assuming an attack on a target in few turns, tries to guess if a requirement req will be fulfilled for this target n_data for a number of turns after which the strike is awaited (0 means current turn, assumes a strike during 5 turns)

Definition at line 365 of file daimilitary.c.

Referenced by action_may_happen_unit_on_city().