Freeciv-3.2: /fast/freeciv/nightly/S3_2/ai/default/daicity.c File Reference

Freeciv-3.2

Loading...
Searching...
No Matches
Macros | Functions
daicity.c File Reference
`#include <string.h>`
`#include <math.h>`
`#include "rand.h"`
`#include "registry.h"`
`#include "actions.h"`
`#include "game.h"`
`#include "government.h"`
`#include "research.h"`
`#include "specialist.h"`
`#include "cityhand.h"`
`#include "citytools.h"`
`#include "cityturn.h"`
`#include "notify.h"`
`#include "plrhand.h"`
`#include "srv_log.h"`
`#include "unithand.h"`
`#include "unittools.h"`
`#include "advdata.h"`
`#include "advtools.h"`
`#include "autosettlers.h"`
`#include "advbuilding.h"`
`#include "infracache.h"`
`#include "aitraits.h"`
`#include "difficulty.h"`
`#include "handicaps.h"`
`#include "aihand.h"`
`#include "aitools.h"`
`#include "daidata.h"`
`#include "daidiplomacy.h"`
`#include "daidomestic.h"`
`#include "daimilitary.h"`
`#include "daieffects.h"`
`#include "daiplayer.h"`
`#include "daisettler.h"`
`#include "daicity.h"`
`#include "specvec.h"`

Go to the source code of this file.

## Macros

#define | LOG_BUY LOG_DEBUG
#define | LOG_EMERGENCY LOG_VERBOSE
#define | LOG_WANT LOG_VERBOSE
#define | AI_CITY_RECALC_SPEED 5
#define | AI_BA_RECALC_SPEED 5
#define | SPECVEC_TAG tech
#define | SPECVEC_TYPE struct advance *
#define | SPECVEC_TAG impr
#define | SPECVEC_TYPE const struct impr_type *
#define | city_range_iterate(city_here, list, range, city)
#define | city_range_iterate_end
#define | CITY_EMERGENCY(pcity)

## Functions

static void | dai_city_sell_noncritical (struct city *pcity, bool redundant_only)
static void | resolve_city_emergency (struct ai_type *ait, struct player *pplayer, struct city *pcity)
static void | want_tech_for_improvement_effect (struct ai_type *ait, struct player *pplayer, const struct city *pcity, const struct impr_type *pimprove, const struct advance *tech, adv_want building_want)
void | want_techs_for_improvement_effect (struct ai_type *ait, struct player *pplayer, const struct city *pcity, const struct impr_type *pimprove, struct tech_vector *needed_techs, adv_want building_want)
void | dont_want_tech_obsoleting_impr (struct ai_type *ait, struct player *pplayer, const struct city *pcity, const struct impr_type *pimprove, adv_want building_want)
static void | dai_barbarian_choose_build (struct player *pplayer, struct city *pcity, struct adv_choice *choice)
static void | dai_city_choose_build (struct ai_type *ait, struct player *pplayer, struct city *pcity)
static void | try_to_sell_stuff (struct player *pplayer, struct city *pcity)
static void | increase_maxbuycost (struct player *pplayer, int new_value)
static void | dai_upgrade_units (struct city *pcity, int limit, bool military)
static void | unit_do_disband_trad (struct player *owner, struct unit *punit, const enum action_requester requester)
static void | dai_spend_gold (struct ai_type *ait, struct player *pplayer)
static int | unit_food_upkeep (struct unit *punit)
static int | unit_foodbox_cost (struct unit *punit)
static void | contemplate_terrain_improvements (struct ai_type *ait, struct city *pcity)
void | dai_manage_cities (struct ai_type *ait, struct player *pplayer)
static bool | building_crucial (const struct player *plr, struct impr_type *pimprove, const struct city *pcity)
void | dai_city_alloc (struct ai_type *ait, struct city *pcity)
void | dai_city_free (struct ai_type *ait, struct city *pcity)
void | dai_city_save (struct ai_type *ait, const char *aitstr, struct section_file *file, const struct city *pcity, const char *citystr)
void | dai_city_load (struct ai_type *ait, const char *aitstr, const struct section_file *file, struct city *pcity, const char *citystr)
static int | action_target_neg_util (action_id act_id, const struct city *pcity)
static bool | adjust_wants_for_reqs (struct ai_type *ait, struct player *pplayer, struct city *pcity, const struct impr_type *pimprove, const adv_want v)
adv_want | dai_city_want (struct player *pplayer, struct city *acity, struct adv_data *adv, struct impr_type *pimprove)
static adv_want | base_want (struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove)
static void | adjust_improvement_wants_by_effects (struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const bool already)
static bool | should_force_recalc (struct city *pcity)
void | dai_build_adv_init (struct ai_type *ait, struct player *pplayer)
void | dai_build_adv_adjust (struct ai_type *ait, struct player *pplayer, struct city *wonder_city)
void | dai_consider_wonder_city (struct ai_type *ait, struct city *pcity, bool *result)
static bool | dai_cant_help_req (const struct req_context *context, const struct requirement *req)
bool | dai_can_city_build_improvement_later (const struct city *pcity, const struct impr_type *pimprove)
bool | dai_can_player_build_improvement_later (const struct player *p, const struct impr_type *pimprove)
Impr_type_id | dai_find_source_building (struct city *pcity, enum effect_type effect_type, const struct unit_type *utype)

## Macro Definition Documentation

## ◆ AI_BA_RECALC_SPEED

#define AI_BA_RECALC_SPEED 5

Definition at line 75 of file daicity.c.

## ◆ AI_CITY_RECALC_SPEED

#define AI_CITY_RECALC_SPEED 5

Definition at line 73 of file daicity.c.

## ◆ CITY_EMERGENCY

#define CITY_EMERGENCY | ( |  | pcity | ) |

Value:
(pcity->surplus[O_SHIELD] < 0 || city_unhappy(pcity) \
|| pcity->food_stock + pcity->surplus[O_FOOD] < 0)
city_unhappy
bool city_unhappy(const struct city *pcity)
Definition city.c:1618
O_SHIELD
@ O_SHIELD
Definition fc_types.h:101
O_FOOD
@ O_FOOD
Definition fc_types.h:101

Definition at line 107 of file daicity.c.

## ◆ city_range_iterate

#define city_range_iterate | ( |  | city_here,
|  |  | list,
|  |  | range,
|  |  | city
| ) |  |

Value:
{ \
city_list_iterate(list, city) { \
if (range == REQ_RANGE_PLAYER \
|| range == REQ_RANGE_TEAM \
|| range == REQ_RANGE_ALLIANCE \
|| (range == REQ_RANGE_TRADE_ROUTE \
&& (city == city_here \
|| have_cities_trade_route(city, city_here))) \
|| ((range == REQ_RANGE_CITY || range == REQ_RANGE_LOCAL) \
&& city == city_here) \
|| (range == REQ_RANGE_CONTINENT \
&& tile_continent(city->tile) == \
tile_continent(city_here->tile))) {
list
static struct fc_sockaddr_list * list
Definition clinet.c:102
incite_cost
char * incite_cost
Definition comments.c:74
city
Definition city.h:320
city::tile
struct tile * tile
Definition city.h:322
range
Definition unittype.c:879
tile_continent
#define tile_continent(_tile)
Definition tile.h:92
have_cities_trade_route
bool have_cities_trade_route(const struct city *pc1, const struct city *pc2)
Definition traderoutes.c:534

Definition at line 87 of file daicity.c.

## ◆ city_range_iterate_end

#define city_range_iterate_end

Value:
} \
} city_list_iterate_end; \
}
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:510

Definition at line 102 of file daicity.c.

## ◆ LOG_BUY

#define LOG_BUY LOG_DEBUG

Definition at line 67 of file daicity.c.

## ◆ LOG_EMERGENCY

#define LOG_EMERGENCY LOG_VERBOSE

Definition at line 68 of file daicity.c.

## ◆ LOG_WANT

#define LOG_WANT LOG_VERBOSE

Definition at line 69 of file daicity.c.

## ◆ SPECVEC_TAG [1/2]

#define SPECVEC_TAG tech

Definition at line 77 of file daicity.c.

## ◆ SPECVEC_TAG [2/2]

#define SPECVEC_TAG impr

Definition at line 77 of file daicity.c.

## ◆ SPECVEC_TYPE [1/2]

#define SPECVEC_TYPE struct advance *

Definition at line 78 of file daicity.c.

## ◆ SPECVEC_TYPE [2/2]

#define SPECVEC_TYPE const struct impr_type *

Definition at line 78 of file daicity.c.

## Function Documentation

## ◆ action_target_neg_util()

static int action_target_neg_util | ( | action_id | act_id,
|  | const struct city * | pcity
| ) |  |

static

How undesirable for the owner of a particular city is the fact that it can be a target of a particular action?

The negative utility (how undesirable it is) is given as a positive number. If it is desirable to be the target of an action make the negative utility a negative number since double negative is positive.

Examples: action_target_neg_util(Add to population) = -50 action_target_neg_util(Subtract from population) = 50

Definition at line 1205 of file daicity.c.

Referenced by adjust_improvement_wants_by_effects().

## ◆ adjust_improvement_wants_by_effects()

static void adjust_improvement_wants_by_effects | ( | struct ai_type * | ait,
|  | struct player * | pplayer,
|  | struct city * | pcity,
|  | struct impr_type * | pimprove,
|  | const bool | already
| ) |  |

static

Calculate effects of possible improvements and extra effects of existing improvements. Consequently adjust the desirability of those improvements or the technologies that would make them possible.

This function may (indeed, should) be called even for improvements that a city already has, or can not (yet) build. For existing improvements, it will discourage research of technologies that would make the improvement obsolete or reduce its effectiveness, and encourages technologies that would improve its effectiveness. For improvements that the city can not yet build it will encourage research of the techs and building of the improvements that will make the improvement possible.

A complexity is that there are two sets of requirements to consider: the requirements for the building itself, and the requirements for the effects for the building.

A few base variables: c - number of cities we have in current range u - units we have of currently affected type v - the want for the improvement we are considering

This function contains a whole lot of WAGs. We ignore cond_* for now, thinking that one day we may fulfil the cond_s anyway. In general, we first add bonus for city improvements, then for wonders.

IDEA: Calculate per-continent aggregates of various data, and use this for wonders below for better wonder placements.

Definition at line 1577 of file daicity.c.

Referenced by dai_build_adv_adjust().

## ◆ adjust_wants_for_reqs()

static bool adjust_wants_for_reqs | ( | struct ai_type * | ait,
|  | struct player * | pplayer,
|  | struct city * | pcity,
|  | const struct impr_type * | pimprove,
|  | const adv_want | v
| ) |  |

static

Increase the degree to which we want to meet a set of requirements, because they will enable construction of an improvement with desirable effects.

v is the desire for the improvement.

Returns whether all the requirements are met.

Definition at line 1339 of file daicity.c.

Referenced by adjust_improvement_wants_by_effects(), and adjust_wants_for_reqs().

## ◆ base_want()

static adv_want base_want | ( | struct ai_type * | ait,
|  | struct player * | pplayer,
|  | struct city * | pcity,
|  | struct impr_type * | pimprove
| ) |  |

static

Calculates want for some buildings by actually adding the building and measuring the effect.

Definition at line 1499 of file daicity.c.

Referenced by adjust_improvement_wants_by_effects().

## ◆ building_crucial()

static bool building_crucial | ( | const struct player * | plr,
|  | struct impr_type * | pimprove,
|  | const struct city * | pcity
| ) |  |

static

Are effects provided by this building not needed?

If this function is called for a building that has not yet been constructed, side effect benefits may not be accurately calculated (see improvement.c for details).

Definition at line 963 of file daicity.c.

Referenced by dai_city_sell_noncritical().

## ◆ contemplate_terrain_improvements()

static void contemplate_terrain_improvements | ( | struct ai_type * | ait,
|  | struct city * | pcity
| ) |  |

static

Estimates the want for a terrain improver (aka worker) by creating a virtual unit and feeding it to settler_evaluate_improvements().

TODO: AI does not ship UTYF_SETTLERS around, only UTYF_CITIES - Per

Definition at line 747 of file daicity.c.

Referenced by dai_manage_cities().

## ◆ dai_barbarian_choose_build()

static void dai_barbarian_choose_build | ( | struct player * | pplayer,
|  | struct city * | pcity,
|  | struct adv_choice * | choice
| ) |  |

static

Choose a build for the barbarian player.

TODO: Move this into daimilitary.c TODO: It will be called for each city but doesn't depend on the city, maybe cache it? Although barbarians don't normally have many cities, so can be a bigger bother to cache it.

Definition at line 200 of file daicity.c.

Referenced by dai_city_choose_build().

## ◆ dai_build_adv_adjust()

void dai_build_adv_adjust | ( | struct ai_type * | ait,
|  | struct player * | pplayer,
|  | struct city * | wonder_city
| ) |  |

Calculate how much an AI player should want to build particular improvements, because of the effects of those improvements, and increase the want for technologies that will enable buildings with desirable effects.

Definition at line 1945 of file daicity.c.

Referenced by cai_build_adv_adjust(), and texwai_build_adv_adjust().

## ◆ dai_build_adv_init()

void dai_build_adv_init | ( | struct ai_type * | ait,
|  | struct player * | pplayer
| ) |  |

Initialize building advisor. Calculates data of all players, not only of those controlled by current ai type.

Definition at line 1929 of file daicity.c.

Referenced by cai_build_adv_init(), and texwai_build_adv_init().

## ◆ dai_can_city_build_improvement_later()

bool dai_can_city_build_improvement_later | ( | const struct city * | pcity,
|  | const struct impr_type * | pimprove
| ) |  |

Whether AI expects to be ever able to build given building in the city

Definition at line 2090 of file daicity.c.

Referenced by should_force_recalc().

## ◆ dai_can_player_build_improvement_later()

bool dai_can_player_build_improvement_later | ( | const struct player * | p,
|  | const struct impr_type * | pimprove
| ) |  |

Whether AI expects to be ever able to build given building

Definition at line 2120 of file daicity.c.

Referenced by dai_can_city_build_improvement_later().

## ◆ dai_cant_help_req()

static bool dai_cant_help_req | ( | const struct req_context * | context,
|  | const struct requirement * | req
| ) |  |

static

Default AI does not know ways of ever fulfilling this requirement and should not think about it

Definition at line 2069 of file daicity.c.

Referenced by dai_can_city_build_improvement_later(), and dai_can_player_build_improvement_later().

## ◆ dai_city_alloc()

void dai_city_alloc | ( | struct ai_type * | ait,
|  | struct city * | pcity
| ) |  |

Initialize city for use with default AI.

Definition at line 1109 of file daicity.c.

Referenced by cai_city_alloc().

## ◆ dai_city_choose_build()

static void dai_city_choose_build | ( | struct ai_type * | ait,
|  | struct player * | pplayer,
|  | struct city * | pcity
| ) |  |

static

Chooses what the city will build. Is called after the military advisor put it's choice into pcity->server.ai.choice and "settler advisor" put settler want into pcity->founder_*.

Note that AI cheats – it suffers no penalty for switching from unit to improvement, etc.

Definition at line 249 of file daicity.c.

Referenced by dai_manage_cities().

## ◆ dai_city_free()

void dai_city_free | ( | struct ai_type * | ait,
|  | struct city * | pcity
| ) |  |

Free city from use with default AI.

Definition at line 1122 of file daicity.c.

Referenced by cai_city_free().

## ◆ dai_city_load()

void dai_city_load | ( | struct ai_type * | ait,
|  | const char * | aitstr,
|  | const struct section_file * | file,
|  | struct city * | pcity,
|  | const char * | citystr
| ) |  |

Load ai city segment from savefile

Definition at line 1163 of file daicity.c.

Referenced by cai_city_load(), and texwai_city_load().

## ◆ dai_city_save()

void dai_city_save | ( | struct ai_type * | ait,
|  | const char * | aitstr,
|  | struct section_file * | file,
|  | const struct city * | pcity,
|  | const char * | citystr
| ) |  |

Write ai city segments to savefile

Definition at line 1136 of file daicity.c.

Referenced by cai_city_save(), and texwai_city_save().

## ◆ dai_city_sell_noncritical()

static void dai_city_sell_noncritical | ( | struct city * | pcity,
|  | bool | redundant_only
| ) |  |

static

Sell an noncritical building if there are any in the city.

Definition at line 985 of file daicity.c.

Referenced by dai_manage_cities().

## ◆ dai_city_want()

adv_want dai_city_want | ( | struct player * | pplayer,
|  | struct city * | acity,
|  | struct adv_data * | adv,
|  | struct impr_type * | pimprove
| ) |  |

Calculates city want from some input values. Set pimprove to NULL when nothing in the city has changed, and you just want to know the base want of a city.

Definition at line 1424 of file daicity.c.

Referenced by base_want(), dai_adjust_policies(), dai_build_adv_init(), dai_gov_value(), and dai_tech_base_want().

## ◆ dai_consider_wonder_city()

void dai_consider_wonder_city | ( | struct ai_type * | ait,
|  | struct city * | pcity,
|  | bool * | result
| ) |  |

Is it ok for advisor code to consider given city as wonder city?

Definition at line 2056 of file daicity.c.

Referenced by cai_consider_wonder_city(), and texwai_consider_wonder_city().

## ◆ dai_find_source_building()

Impr_type_id dai_find_source_building | ( | struct city * | pcity,
|  | enum effect_type effect_type effect_type | ,
|  | const struct unit_type * | utype
| ) |  |

Returns a buildable, non-obsolete building that can provide the effect.

Note: this function is an inefficient hack to be used by the old AI. It will never find wonders, since that's not what the AI wants.

Definition at line 2156 of file daicity.c.

Referenced by adjust_ai_unit_choice(), assess_danger(), and military_advisor_choose_build().

## ◆ dai_manage_cities()

void dai_manage_cities | ( | struct ai_type * | ait,
|  | struct player * | pplayer
| ) |  |

One of the top level AI functions. It does (by calling other functions): worker allocations, build choices, extra gold spending.

Definition at line 849 of file daicity.c.

Referenced by dai_do_last_activities().

## ◆ dai_spend_gold()

static void dai_spend_gold | ( | struct ai_type * | ait,
|  | struct player * | pplayer
| ) |  |

static

Buy and upgrade stuff!

Definition at line 521 of file daicity.c.

Referenced by dai_manage_cities().

## ◆ dai_upgrade_units()

static void dai_upgrade_units | ( | struct city * | pcity,
|  | int | limit,
|  | bool | military
| ) |  |

static

Try to upgrade a city's units. limit is the last amount of gold we can end up with after the upgrade. military is if we want to upgrade non- military or military units.

Definition at line 379 of file daicity.c.

Referenced by dai_spend_gold().

## ◆ dont_want_tech_obsoleting_impr()

void dont_want_tech_obsoleting_impr | ( | struct ai_type * | ait,
|  | struct player * | pplayer,
|  | const struct city * | pcity,
|  | const struct impr_type * | pimprove,
|  | adv_want | building_want
| ) |  |

Decrease want for a technology because of the value of that technology in obsoleting an improvement effect.

Definition at line 177 of file daicity.c.

Referenced by adjust_improvement_wants_by_effects().

## ◆ increase_maxbuycost()

static void increase_maxbuycost | ( | struct player * | pplayer,
|  | int | new_value
| ) |  |

static

Increase maxbuycost. This variable indicates (via ai_gold_reserve) to the tax selection code how much money do we need for buying stuff.

Definition at line 369 of file daicity.c.

Referenced by dai_spend_gold(), and dai_upgrade_units().

## ◆ resolve_city_emergency()

static void resolve_city_emergency | ( | struct ai_type * | ait,
|  | struct player * | pplayer,
|  | struct city * | pcity
| ) |  |

static

This function tries desperately to save a city from going under by revolt or starvation of food or resources. We do this by taking over resources held by nearby cities and disbanding units.

TODO: Try to move units into friendly cities to reduce unhappiness instead of disbanding. Also rather starve city than keep it in revolt, as long as we don't lose settlers.

TODO: Make function that tries to save units by moving them into cities that can upkeep them and change homecity rather than just disband. This means we'll have to move this function to beginning of AI turn sequence (before moving units).

"I don't care how slow this is; it will very rarely be used." – Syela

Syela is wrong. It happens quite too often, mostly due to unhappiness. Also, most of the time we are unable to resolve the situation.

Definition at line 1028 of file daicity.c.

Referenced by dai_manage_cities().

## ◆ should_force_recalc()

static bool should_force_recalc | ( | struct city * | pcity | ) |

static

Whether the AI should calculate the building wants for this city this turn, ahead of schedule.

Always recalculate if the city just finished building, so we can make a sensible choice for the next thing to build. Perhaps the improvement we were building has become obsolete, or a different player built the Great Wonder we were building.

Definition at line 1916 of file daicity.c.

Referenced by dai_build_adv_adjust().

## ◆ try_to_sell_stuff()

static void try_to_sell_stuff | ( | struct player * | pplayer,
|  | struct city * | pcity
| ) |  |

static

Sell building from city

Definition at line 353 of file daicity.c.

Referenced by dai_spend_gold().

## ◆ unit_do_disband_trad()

static void unit_do_disband_trad | ( | struct player * | owner,
|  | struct unit * | punit,
|  | const enum action_requester | requester
| ) |  |

static

Try to disband punit in the traditional way.

Try to disband the specified unit. Match the old behavior in what kind of disbanding is tried and who benefits from it.

Definition at line 450 of file daicity.c.

Referenced by dai_spend_gold(), and resolve_city_emergency().

## ◆ unit_food_upkeep()

static int unit_food_upkeep | ( | struct unit * | punit | ) |

static

Calculates a unit's food upkeep (per turn).

Definition at line 689 of file daicity.c.

Referenced by contemplate_terrain_improvements().

## ◆ unit_foodbox_cost()

static int unit_foodbox_cost | ( | struct unit * | punit | ) |

static

Returns how much food a settler will consume out of the city's foodbox when created. If unit has id zero it is assumed to be a virtual unit inside a city.

FIXME: This function should be generalised and then moved into common/unittype.c - Per

Definition at line 708 of file daicity.c.

Referenced by contemplate_terrain_improvements().

## ◆ want_tech_for_improvement_effect()

static void want_tech_for_improvement_effect | ( | struct ai_type * | ait,
|  | struct player * | pplayer,
|  | const struct city * | pcity,
|  | const struct impr_type * | pimprove,
|  | const struct advance * | tech,
|  | adv_want | building_want
| ) |  |

static

Increase want for a technology because of the value of that technology in providing an improvement effect.

The input building_want gives the desire for the improvement; the value may be negative for technologies that produce undesirable effects.

This function must convert from units of 'building want' to 'tech want'. We put this conversion in a function because the 'want' scales are unclear and kludged. Consequently, this conversion might require tweaking.

Definition at line 127 of file daicity.c.

Referenced by dont_want_tech_obsoleting_impr(), and want_techs_for_improvement_effect().

## ◆ want_techs_for_improvement_effect()

void want_techs_for_improvement_effect | ( | struct ai_type * | ait,
|  | struct player * | pplayer,
|  | const struct city * | pcity,
|  | const struct impr_type * | pimprove,
|  | struct tech_vector * | needed_techs,
|  | adv_want | building_want
| ) |  |

Increase want for a technologies because of the value of that technology in providing an improvement effect.

Definition at line 156 of file daicity.c.

Referenced by adjust_improvement_wants_by_effects(), and adjust_wants_for_reqs().