![]() |
Freeciv21
Develop your civilization from humble roots to a global empire
|
#include <cstring>#include "rand.h"#include "registry.h"#include "registry_ini.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 "advbuilding.h"#include "advdata.h"#include "autosettlers.h"#include "infracache.h"#include "aitraits.h"#include "difficulty.h"#include "handicaps.h"#include "aidata.h"#include "aihand.h"#include "aiplayer.h"#include "aisettler.h"#include "aitools.h"#include "aiunit.h"#include "daidiplomacy.h"#include "daidomestic.h"#include "daieffects.h"#include "daimilitary.h"#include "daicity.h"#include "specvec.h"
Include dependency graph for daicity.cpp: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) |
| Sell an noncritical building if there are any in the city. More... | |
| static void | resolve_city_emergency (struct ai_type *ait, struct player *pplayer, struct city *pcity) |
| This function tries desperately to save a city from going under by revolt or starvation of food or resources. More... | |
| 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) |
| Increase want for a technology because of the value of that technology in providing an improvement effect. More... | |
| 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. More... | |
| 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. More... | |
| static void | dai_barbarian_choose_build (struct player *pplayer, struct city *pcity, struct adv_choice *choice) |
| Choose a build for the barbarian player. More... | |
| static void | dai_city_choose_build (struct ai_type *ait, struct player *pplayer, struct city *pcity) |
| Chooses what the city will build. More... | |
| static void | try_to_sell_stuff (struct player *pplayer, struct city *pcity) |
| Sell building from city. More... | |
| static void | increase_maxbuycost (struct player *pplayer, int new_value) |
| Increase maxbuycost. More... | |
| static void | dai_upgrade_units (struct city *pcity, int limit, bool military) |
| Try to upgrade a city's units. More... | |
| static void | unit_do_disband_trad (struct player *owner, struct unit *punit, const enum action_requester requester) |
| Try to disband punit in the traditional way. More... | |
| static void | dai_spend_gold (struct ai_type *ait, struct player *pplayer) |
| Buy and upgrade stuff! More... | |
| static int | unit_food_upkeep (struct unit *punit) |
| Calculates a unit's food upkeep (per turn). More... | |
| static int | unit_foodbox_cost (struct unit *punit) |
| Returns how much food a settler will consume out of the city's foodbox when created. More... | |
| static void | contemplate_terrain_improvements (struct ai_type *ait, struct city *pcity) |
| Estimates the want for a terrain improver (aka worker) by creating a virtual unit and feeding it to settler_evaluate_improvements. More... | |
| void | dai_manage_cities (struct ai_type *ait, struct player *pplayer) |
| One of the top level AI functions. More... | |
| static bool | building_crucial (const struct player *plr, struct impr_type *pimprove, const struct city *pcity) |
| Are effects provided by this building not needed? More... | |
| void | dai_city_alloc (struct ai_type *ait, struct city *pcity) |
| Initialize city for use with default AI. More... | |
| void | dai_city_free (struct ai_type *ait, struct city *pcity) |
| Free city from use with default AI. More... | |
| 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. More... | |
| 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. More... | |
| static int | action_target_neg_util (action_id act_id, const struct city *pcity) |
| How undesirable for the owner of a particular city is the fact that it can be a target of a particular action? More... | |
| 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) |
| Increase the degree to which we want to meet a set of requirements, because they will enable construction of an improvement with desirable effects. More... | |
| 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. More... | |
| static adv_want | base_want (struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove) |
| Calculates want for some buildings by actually adding the building and measuring the effect. More... | |
| static void | adjust_improvement_wants_by_effects (struct ai_type *ait, struct player *pplayer, struct city *pcity, struct impr_type *pimprove, const bool already) |
| Calculate effects of possible improvements and extra effects of existing improvements. More... | |
| static bool | should_force_recalc (struct city *pcity) |
| Whether the AI should calculate the building wants for this city this turn, ahead of schedule. More... | |
| void | dai_build_adv_init (struct ai_type *ait, struct player *pplayer) |
| Initialize building advisor. More... | |
| 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. More... | |
| 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? More... | |
| Impr_type_id | dai_find_source_building (struct city *pcity, enum effect_type effect_type, const struct unit_type *utype) |
| Returns a buildable, non-obsolete building that can provide the effect. More... | |
| #define AI_BA_RECALC_SPEED 5 |
Definition at line 69 of file daicity.cpp.
| #define AI_CITY_RECALC_SPEED 5 |
Definition at line 67 of file daicity.cpp.
| #define CITY_EMERGENCY | ( | pcity | ) |
Definition at line 102 of file daicity.cpp.
Definition at line 81 of file daicity.cpp.
| #define city_range_iterate_end |
Definition at line 96 of file daicity.cpp.
| #define LOG_BUY LOG_DEBUG |
Definition at line 61 of file daicity.cpp.
| #define LOG_EMERGENCY LOG_VERBOSE |
Definition at line 62 of file daicity.cpp.
| #define LOG_WANT LOG_VERBOSE |
Definition at line 63 of file daicity.cpp.
| #define SPECVEC_TAG tech |
Definition at line 75 of file daicity.cpp.
| #define SPECVEC_TAG impr |
Definition at line 75 of file daicity.cpp.
| #define SPECVEC_TYPE struct advance * |
Definition at line 76 of file daicity.cpp.
| #define SPECVEC_TYPE const struct impr_type * |
Definition at line 76 of file daicity.cpp.
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 1223 of file daicity.cpp.
Referenced by adjust_improvement_wants_by_effects().
|
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 1576 of file daicity.cpp.
Referenced by dai_build_adv_adjust().
|
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 1336 of file daicity.cpp.
Referenced by adjust_improvement_wants_by_effects().
|
static |
Calculates want for some buildings by actually adding the building and measuring the effect.
Definition at line 1496 of file daicity.cpp.
Referenced by adjust_improvement_wants_by_effects().
|
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 983 of file daicity.cpp.
Referenced by dai_city_sell_noncritical().
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 762 of file daicity.cpp.
Referenced by dai_manage_cities().
|
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 198 of file daicity.cpp.
Referenced by dai_city_choose_build().
| 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 1947 of file daicity.cpp.
Referenced by cai_build_adv_adjust(), texwai_build_adv_adjust(), and twai_build_adv_adjust().
Initialize building advisor.
Calculates data of all players, not only of those controlled by current ai type.
Definition at line 1928 of file daicity.cpp.
Referenced by cai_build_adv_init(), texwai_build_adv_init(), and twai_build_adv_init().
Initialize city for use with default AI.
Definition at line 1129 of file daicity.cpp.
Referenced by cai_city_alloc(), and twai_city_alloc().
|
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 247 of file daicity.cpp.
Referenced by dai_manage_cities().
Free city from use with default AI.
Definition at line 1142 of file daicity.cpp.
Referenced by cai_city_free(), and twai_city_free().
| 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 1185 of file daicity.cpp.
Referenced by cai_city_load(), texwai_city_load(), and twai_city_load().
| 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 1157 of file daicity.cpp.
Referenced by cai_city_save(), texwai_city_save(), and twai_city_save().
|
static |
Sell an noncritical building if there are any in the city.
Definition at line 1005 of file daicity.cpp.
Referenced by dai_manage_cities().
| 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 nullptr when nothing in the city has changed, and you just want to know the base want of a city.
Definition at line 1419 of file daicity.cpp.
Referenced by base_want(), dai_adjust_policies(), dai_build_adv_init(), dai_gov_value(), and dai_tech_base_want().
Is it ok for advisor code to consider given city as wonder city?
Definition at line 2079 of file daicity.cpp.
Referenced by cai_consider_wonder_city(), texwai_consider_wonder_city(), and twai_consider_wonder_city().
| 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 2095 of file daicity.cpp.
Referenced by adjust_ai_unit_choice(), assess_danger(), and military_advisor_choose_build().
One of the top level AI functions.
It does (by calling other functions): worker allocations, build choices, extra gold spending.
Definition at line 861 of file daicity.cpp.
Referenced by dai_do_last_activities().
Buy and upgrade stuff!
Definition at line 525 of file daicity.cpp.
Referenced by dai_manage_cities().
|
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 383 of file daicity.cpp.
Referenced by dai_spend_gold().
| 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 173 of file daicity.cpp.
Referenced by adjust_improvement_wants_by_effects().
|
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 372 of file daicity.cpp.
Referenced by dai_spend_gold(), and dai_upgrade_units().
|
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 1048 of file daicity.cpp.
Referenced by dai_manage_cities().
|
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 1914 of file daicity.cpp.
Referenced by dai_build_adv_adjust().
Sell building from city.
Definition at line 354 of file daicity.cpp.
Referenced by dai_spend_gold().
|
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 455 of file daicity.cpp.
Referenced by dai_spend_gold(), and resolve_city_emergency().
|
static |
Calculates a unit's food upkeep (per turn).
Definition at line 703 of file daicity.cpp.
Referenced by contemplate_terrain_improvements().
|
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 723 of file daicity.cpp.
Referenced by contemplate_terrain_improvements().
|
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 124 of file daicity.cpp.
Referenced by dont_want_tech_obsoleting_impr(), and 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 152 of file daicity.cpp.
Referenced by adjust_improvement_wants_by_effects(), and adjust_wants_for_reqs().