85 #define RESULT_IS_ENOUGH 250
87 #define FERRY_TECH_WANT 500
89 #define GROWTH_PRIORITY 15
100 #define GROWTH_POTENTIAL_DEEMPHASIS 8
103 #define NAVAL_EMPHASIS 20
107 #define DEFENSE_EMPHASIS 20
128 QHash<int, const struct tile_data_cache *> *
tdc_hash;
167 QHash<int, const struct tile_data_cache *>
tdc_hash;
179 struct tile *center);
180 static bool food_starvation(
const std::unique_ptr<cityresult> &result);
183 const std::unique_ptr<cityresult> &result);
184 static int naval_bonus(
const std::unique_ptr<cityresult> &result);
186 const std::unique_ptr<cityresult> &cr);
191 static std::unique_ptr<cityresult>
193 struct unit *punit,
int boat_cost);
194 static std::unique_ptr<cityresult>
196 bool look_for_boat,
bool use_virt_boat);
204 :
tile(ptile), city_radius_sq(
game.info.init_city_radius_sq)
214 for (
const auto &ptdc : qAsConst(
tdc_hash)) {
233 struct player *saved_owner =
nullptr;
234 struct tile *saved_claimer =
nullptr;
235 bool virtual_city =
false;
246 auto result = std::make_unique<cityresult>(center);
264 bool city_center = (result->tile == ptile);
280 if (!ptdc_hit || city_center) {
296 if (ptdc->
food >= 2) {
300 if (!city_center && virtual_city) {
321 result->city_center.tdc = ptdc;
322 }
else if (!result->best_other.tdc) {
324 result->best_other.tdc = ptdc;
325 result->best_other.tile = ptile;
326 result->best_other.cindex = cindex;
327 }
else if (ptdc->
sum > result->best_other.tdc->sum) {
332 result->best_other.tdc = ptdc;
333 result->best_other.tile = ptile;
334 result->best_other.cindex = cindex;
341 if (result->tdc_hash.contains(cindex)) {
342 delete[] result->tdc_hash.value(cindex);
344 result->tdc_hash.insert(cindex, ptdc);
354 result->city_center.tdc->sum
355 + (result->best_other.tdc !=
nullptr ? result->best_other.tdc->sum
357 }
else if (result->best_other.tdc !=
nullptr) {
360 result->total = result->best_other.tdc->sum;
375 result->city_center.tdc->shield + result->best_other.tdc->shield;
382 result->city_center.tdc->trade + result->best_other.tdc->trade;
387 result->corruption = 0;
402 nullptr, gov_centers)
406 result->total -= result->corruption;
407 result->total -= result->waste;
408 result->total =
MAX(0, result->total);
450 ptdc_copy->
sum = ptdc->
sum;
524 return (result->city_center.tdc->food
525 + (result->best_other.tdc ? result->best_other.tdc->food : 0)
535 return (result->city_center.tdc->shield
536 + (result->best_other.tdc ? result->best_other.tdc->shield : 0)
545 const std::unique_ptr<cityresult> &result)
548 int defense_bonus = 10 +
tile_terrain(result->tile)->defense_bonus / 10;
560 extra_bonus += pextra->defense_bonus;
566 defense_bonus += (defense_bonus * extra_bonus) / 100;
568 return 100 / (result->total + 1)
580 if (ocean_adjacent) {
591 const std::unique_ptr<cityresult> &cr)
598 QScopedArrayPointer<int> city_map_reserved(
new int[tiles]());
599 QScopedArrayPointer<int> city_map_food(
new int[tiles]());
600 QScopedArrayPointer<int> city_map_shield(
new int[tiles]());
601 QScopedArrayPointer<int> city_map_trade(
new int[tiles]());
605 ptdc = cr->tdc_hash.value(cindex,
nullptr);
607 city_map_reserved[cindex] = ptdc->
reserved;
608 city_map_food[cindex] = ptdc->
reserved;
609 city_map_shield[cindex] = ptdc->
reserved;
610 city_map_trade[cindex] = ptdc->
reserved;
615 log_test(
"cityresult for (x,y,radius_sq) = (%d, %d, %d) - Reservations:",
616 TILE_XY(cr->tile), cr->city_radius_sq);
620 log_test(
"cityresult for (x,y,radius_sq) = (%d, %d, %d) - Food:",
621 TILE_XY(cr->tile), cr->city_radius_sq);
625 log_test(
"cityresult for (x,y,radius_sq) = (%d, %d, %d) - Shield:",
626 TILE_XY(cr->tile), cr->city_radius_sq);
630 log_test(
"cityresult for (x,y,radius_sq) = (%d, %d, %d) - Trade:",
631 TILE_XY(cr->tile), cr->city_radius_sq);
634 log_test(
"city center (%d, %d) %d + best other (abs: %d, %d)"
636 TILE_XY(cr->tile), cr->city_center.tdc->sum,
637 TILE_XY(cr->best_other.tile), cr->best_other.cindex,
638 cr->best_other.tdc->sum);
639 log_test(
"- corr %d - waste %d + remaining %d"
640 " + defense bonus %d + naval bonus %d",
641 cr->corruption, cr->waste, cr->remaining,
643 log_test(
"= %d (%d)", cr->total, cr->result);
649 log_test(
" ** RESOURCE STARVATION **");
665 std::unique_ptr<cityresult> cr =
nullptr;
721 cr->total += cr->remaining;
739 static std::unique_ptr<cityresult>
741 struct unit *punit,
int boat_cost)
743 std::unique_ptr<cityresult> best =
nullptr;
761 if (
nullptr != powner && powner != pplayer
777 turns = move_cost / parameter->
move_rate;
788 if ((!best && cr->result > 0) || (best && cr->result > best->result)) {
790 best = std::move(cr);
794 log_debug(
"settler map search (search): (%d,%d) %d",
795 TILE_XY(best->tile), best->result);
803 && best_turn < turns ) {
815 log_debug(
"settler map search (final): no result");
834 static std::unique_ptr<cityresult>
836 bool look_for_boat,
bool use_virt_boat)
840 struct unit *ferry =
nullptr;
841 std::unique_ptr<cityresult> cr1 =
nullptr, cr2 =
nullptr;
876 if (boattype ==
nullptr) {
908 cr2->overseas =
true;
909 cr2->virt_boat = (ferry->
id == 0);
912 if (ferry->
id == 0) {
935 if (cr1->result > cr2->result) {
951 ai->
settler->
tdc_hash =
new QHash<int, const struct tile_data_cache *>;
968 enum unit_activity best_act;
970 struct tile *best_tile =
nullptr;
972 struct city *pcity =
nullptr;
975 int completion_time = 0;
985 int sanity = punit->
id;
1033 if (pcity !=
nullptr) {
1034 if (!path.
empty()) {
1035 completion_time = path[-1].turn;
1037 best_act = best_task->
act;
1038 best_target = best_task->
tgt;
1039 best_tile = best_task->
ptile;
1045 if (pcity ==
nullptr) {
1047 punit, &best_act, &best_target, &best_tile, &path, state);
1048 if (!path.
empty()) {
1049 completion_time = path[-1].turn;
1061 if (result && result->result > best_impr) {
1070 if (punit->
server.debug) {
1076 if (result->best_other.tile && result->best_other.tdc->sum >= 0) {
1086 }
else if (best_impr > 0) {
1103 best_act, &best_target, completion_time)) {
1104 if (pcity !=
nullptr) {
1133 #ifdef FREECIV_DEBUG
1134 log_debug(
"[aisettler cache for %s] save: %d, miss: %d, old: %d, hit: %d",
1150 if (caller_closes) {
1188 log_debug(
"%s: There is already a city at (%d, %d)!",
1197 punit, ACTION_FOUND_CITY, ptile,
nullptr,
nullptr);
1206 qCritical(
"%s: Failed to build city at (%d, %d). Reason id: %d",
1230 struct unit *virtualunit;
1242 log_debug(
"No ACTION_FOUND_CITY role unit available");
1250 if (
is_ai(pplayer)) {
1261 "want(%d) to establish city at"
1262 " (%d, %d) and will %s to get there",
1263 result->result,
TILE_XY(result->tile),
1266 : (result->overseas ?
"use a boat" :
"walk")));
1269 (result->virt_boat ? -result->result : result->result);
#define action_id_get_role(act_id)
void adv_init_choice(struct adv_choice *choice)
Sets the values of the choice to initial values.
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Return a pointer to our data.
bool adv_danger_at(struct unit *punit, struct tile *ptile)
Are there dangerous enemies at or adjacent to the tile 'ptile'?
void dai_data_phase_finished(struct ai_type *ait, struct player *pplayer)
Clean up ai data after phase finished.
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Get current default ai data related to player.
int aiferry_find_boat(struct ai_type *ait, struct unit *punit, int cap, PFPath *path)
Proper and real PF function for finding a boat.
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Should unit be considered a ferry?
#define TECH_LOG(ait, _, pplayer, padvance, msg,...)
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
std::unique_ptr< cityresult > city_desirability(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *ptile)
Calculates the desire for founding a new city at 'ptile'.
static bool dai_do_build_city(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Build a city and initialize AI infrastructure cache.
static int result_defense_bonus(struct player *pplayer, const std::unique_ptr< cityresult > &result)
Calculate defense bonus, which is a % of total results equal to a given % of the defense bonus %.
void dai_auto_settler_cont(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct settlermap *state)
Auto settler continuing its work.
static void print_cityresult(struct player *pplayer, const std::unique_ptr< cityresult > &cr)
For debugging, print the city result table.
#define GROWTH_POTENTIAL_DEEMPHASIS
static bool food_starvation(const std::unique_ptr< cityresult > &result)
Check if a city on this location would starve.
void dai_auto_settler_run(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct settlermap *state)
Auto settler that can also build cities.
void dai_auto_settler_init(struct ai_plr *ai)
Initialize ai settler engine.
static int naval_bonus(const std::unique_ptr< cityresult > &result)
Add bonus for coast.
struct tile_data_cache * tile_data_cache_copy(const struct tile_data_cache *ptdc)
Make copy of tile data cache.
static void tdc_plr_set(struct ai_type *ait, struct player *plr, int tindex, const struct tile_data_cache *tdcache)
Store player's tile data cache.
static bool shield_starvation(const std::unique_ptr< cityresult > &result)
Check if a city on this location would lack shields.
static std::unique_ptr< cityresult > settler_map_iterate(struct ai_type *ait, struct pf_parameter *parameter, struct unit *punit, int boat_cost)
Find nearest and best city placement in a PF iteration according to "parameter".
void dai_auto_settler_free(struct ai_plr *ai)
Deinitialize ai settler engine.
static std::unique_ptr< cityresult > cityresult_fill(struct ai_type *ait, struct player *pplayer, struct tile *center)
Fill cityresult struct with useful info about the city spot.
void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
Reset ai settler engine.
static std::unique_ptr< cityresult > find_best_city_placement(struct ai_type *ait, struct unit *punit, bool look_for_boat, bool use_virt_boat)
Find nearest and best city placement or (TODO) a city to immigrate to.
struct tile_data_cache * tile_data_cache_new()
Allocate tile data cache.
static const struct tile_data_cache * tdc_plr_get(struct ai_type *ait, struct player *plr, int tindex)
Return player's tile data cache.
void contemplate_new_city(struct ai_type *ait, struct city *pcity)
Return want for city settler.
bool adv_settler_safe_tile(const struct player *pplayer, struct unit *punit, struct tile *ptile)
Do we consider tile safe for autosettler to work?
void adv_unit_new_task(struct unit *punit, enum adv_unit_task task, struct tile *ptile)
Change unit's advisor task.
bool auto_settler_setup_work(struct player *pplayer, struct unit *punit, struct settlermap *state, int recursion, PFPath *path, struct tile *best_tile, enum unit_activity best_act, struct extra_type **best_target, int completion_time)
Setup our settler to do the work it has found.
adv_want settler_evaluate_improvements(struct unit *punit, enum unit_activity *best_act, struct extra_type **best_target, struct tile **best_tile, PFPath *path, struct settlermap *state)
Finds tiles to improve, using punit.
struct city * settler_evaluate_city_requests(struct unit *punit, struct worker_task **best_task, PFPath *path, struct settlermap *state)
Return best city request to fulfill.
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
void city_size_add(struct city *pcity, int add)
Add a (positive or negative) value to the city size.
bool city_can_be_built_here(const struct tile *ptile, const struct unit *punit)
Returns TRUE if the given unit can build a city at the given map coordinates.
const char * city_name_get(const struct city *pcity)
Return the name of the city.
void citylog_map_data(QtMsgType level, int radius_sq, int *map_data)
Display 'map_data' on a city map with the given radius 'radius_sq' for the requested log level.
int city_map_radius_sq_get(const struct city *pcity)
Returns the current squared radius of the city.
void destroy_city_virtual(struct city *pcity)
Removes the virtual skeleton of a city.
void city_choose_build_default(struct city *pcity)
Always tile_set_owner(ptile, pplayer) sometime before this!
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown, const std::vector< city * > &gov_centers, const cached_waste *pcwaste)
Give corruption/waste generated by city.
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Calculate the output for the tile.
int city_map_tiles(int city_radius_sq)
Return the number of tiles for the given city radius.
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Create virtual skeleton for a city.
citizens city_size_get(const struct city *pcity)
Get the city size.
#define city_tile_iterate_index_end
#define city_map_iterate_end
#define city_map_iterate(_radius_sq, _index, _x, _y)
#define city_tile_iterate_index(_radius_sq, _city_tile, _tile, _index)
void citymap_reserve_tile(struct tile *ptile, int id)
Reserve additional tiles as desired (eg I would reserve best available food tile in addition to adjac...
bool citymap_is_reserved(struct tile *ptile)
A tile is reserved if it contains a city or unit id, or a worker is assigned to it.
int citymap_read(struct tile *ptile)
Returns a positive value if within a city radius, which is 1 x number of cities you are within the ra...
@ ANEK_CITY_TOO_CLOSE_TGT
struct unit * game_unit_by_number(int id)
Find unit out of all units in game: now uses fast idex method, instead of looking through all units o...
struct government * government_of_player(const struct player *pplayer)
Return the government of a player.
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
AI players may have handicaps - allowing them to cheat or preventing them from using certain algorith...
void initialize_infrastructure_cache(struct player *pplayer)
Do all tile improvement calculations and cache them for later.
#define fc_assert_ret(condition)
#define fc_assert(condition)
#define fc_assert_ret_val(condition, val)
#define fc_assert_action(condition, action)
#define log_debug(message,...)
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Are (x1,y1) and (x2,y2) really the same when adjusted? This function might be necessary ALOT of place...
#define square_iterate(nmap, center_tile, radius, tile_itr)
#define square_iterate_end
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Return whether the player knows the tile.
bool upgrade_city_extras(struct city *pcity, struct extra_type **gained)
Check city for extra upgrade.
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
Factory function to create a new map according to the parameter.
void pf_map_destroy(struct pf_map *pfm)
After usage the map must be destroyed.
#define pf_map_move_costs_iterate_end
#define pf_map_move_costs_iterate(ARG_pfm, NAME_tile, NAME_cost, COND_from_start)
std::vector< city * > player_gov_centers(const struct player *pplayer)
Locate the player's government centers.
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied or at peace.
#define UNIT_LOG(_, punit, msg,...)
#define CITY_LOG(_, pcity, msg,...)
#define TIMING_LOG(timer, activity)
struct adv_data::@89 goal
struct adv_data::@89::@91 govt
struct ai_settler * settler
adv_want tech_want[A_LAST+1]
QHash< int, const struct tile_data_cache * > * tdc_hash
QHash< int, const struct tile_data_cache * > tdc_hash
struct cityresult::@159 best_other
struct tile_data_cache * tdc
~cityresult()
Destroy a city result.
struct cityresult::@158 city_center
cityresult(struct tile *tile)
Allocated a city result.
struct packet_game_info info
struct packet_scenario_info scenario
enum tile_behavior(* get_TB)(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
struct government * government
struct advance * require_advance
struct unit::@76::@79 server
Tech_type_id advance_index(const struct advance *padvance)
Return the advance index.
bool is_terrain_class_near_tile(const struct tile *ptile, enum terrain_class tclass)
Is there terrain of the given class near tile? (Does not check ptile itself.)
void tile_virtual_destroy(struct tile *vtile)
Frees all memory used by the virtual tile, including freeing virtual units in the tile's unit list an...
const char * tile_get_info_text(const struct tile *ptile, bool include_nuisances, int linebreaks)
Return a (static) string with tile name describing terrain and extras of some categories.
void tile_set_owner(struct tile *ptile, struct player *pplayer, struct tile *claimer)
Set the owner of a tile (may be nullptr).
void tile_set_worked(struct tile *ptile, struct city *pcity)
Set the city/worker on the tile (may be nullptr).
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
struct tile * tile_virtual_new(const struct tile *ptile)
Returns a virtual tile.
#define tile_claimer(_tile)
#define tile_worked(_tile)
#define tile_terrain(_tile)
#define tile_continent(_tile)
#define tile_has_extra(ptile, pextra)
#define tile_owner(_tile)
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Assign a new untargeted task to a unit.
bool unit_is_cityfounder(const struct unit *punit)
Is a cityfounder unit?
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Create a virtual unit skeleton.
void unit_tile_set(struct unit *punit, struct tile *ptile)
Set the tile location of the unit.
#define CHECK_UNIT(punit)
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Handle request for changing activity.
void unit_do_action(struct player *pplayer, const int actor_id, const int target_id, const int sub_tgt_id, const char *name, const action_id action_type)
Handle unit action.
enum ane_kind action_not_enabled_reason(struct unit *punit, action_id act_id, const struct tile *target_tile, const struct city *target_city, const struct unit *target_unit)
Give the reason kind why an action isn't enabled.
struct unit_type * best_role_unit(const struct city *pcity, int role)
Return "best" unit this city can build, with given role/flag.
const char * utype_rule_name(const struct unit_type *punittype)
Return the (untranslated) rule name of the unit type.
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Return "best" unit the player can build, with given role/flag.
int unit_build_shield_cost_base(const struct unit *punit)
Returns the number of shields this unit represents.
struct unit_class * unit_class_get(const struct unit *punit)
Returns unit class pointer for a unit.
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
struct unit_type * get_role_unit(int role, int role_index)
Return index-th unit with specified role/flag.
int unit_pop_value(const struct unit *punit)
How much population is put to building this unit.