Freeciv21
Develop your civilization from humble roots to a global empire
aisettler.cpp
Go to the documentation of this file.
1 /*
2 _ ._ Copyright (c) 1996-2021 Freeciv21 and Freeciv contributors.
3  \ | This file is part of Freeciv21. Freeciv21 is free software: you
4  \_| can redistribute it and/or modify it under the terms of the
5  .' '. GNU General Public License as published by the Free
6  :O O: Software Foundation, either version 3 of the License,
7  '/ \' or (at your option) any later version. You should have
8  :X: received a copy of the GNU General Public License along with
9  :X: Freeciv21. If not, see https://www.gnu.org/licenses/.
10  */
11 
12 #include <QHash>
13 
14 // utility
15 #include "support.h"
16 #include "timing.h"
17 
18 // common
19 #include "city.h"
20 #include "game.h"
21 #include "government.h"
22 #include "map.h"
23 #include "packets.h"
24 #include "player.h"
25 #include "workertask.h"
26 
27 /* common/aicore */
28 #include "citymap.h"
29 #include "pf_tools.h"
30 
31 // server
32 #include "citytools.h"
33 #include "maphand.h"
34 #include "srv_log.h"
35 #include "unithand.h"
36 #include "unittools.h"
37 
38 /* server/advisors */
39 #include "advdata.h"
40 #include "advgoto.h"
41 #include "advtools.h"
42 #include "autosettlers.h"
43 #include "infracache.h"
44 
45 // ai
46 #include "handicaps.h"
47 
48 /* ai/default */
49 #include "aidata.h"
50 #include "aiferry.h"
51 #include "ailog.h"
52 #include "aiplayer.h"
53 #include "aitools.h"
54 #include "aiunit.h"
55 #include "daicity.h"
56 
57 #include "aisettler.h"
58 
59 // COMMENTS
60 /*
61  This code tries hard to do the right thing, including looking
62  into the future (wrt to government), and also doing this in a
63  modpack friendly manner. However, there are some pieces missing.
64 
65  A tighter integration into the city management code would
66  give more optimal city placements, since existing cities could
67  move their workers around to give a new city better placement.
68  Occasionally you will see cities being placed sub-optimally
69  because the best city center tile is taken when another tile
70  could have been worked instead by the city that took it.
71 
72  The code is not generic enough. It assumes smallpox too much,
73  and should calculate with a future city of a bigger size.
74 
75  We need to stop the build code from running this code all the
76  time and instead try to complete what it has started.
77 */
78 
79 /* Stop looking too hard for better tiles to found a new city at when
80  * settler finds location with at least RESULT_IS_ENOUGH want
81  * points. See city_desirability() for how base want is computed
82  * before amortizing it.
83  *
84  * This is a big WAG to save a big amount of CPU. */
85 #define RESULT_IS_ENOUGH 250
86 
87 #define FERRY_TECH_WANT 500
88 
89 #define GROWTH_PRIORITY 15
90 
91 /* Perfection gives us an idea of how long to search for optimal
92  * solutions, instead of going for quick and dirty solutions that
93  * waste valuable time. Decrease for maps where good city placements
94  * are hard to find. Lower means more perfection. */
95 #define PERFECTION 3
96 
97 /* How much to deemphasise the potential for city growth for city
98  * placements. Decrease this value if large cities are important
99  * or planned. Increase if running strict smallpox. */
100 #define GROWTH_POTENTIAL_DEEMPHASIS 8
101 
102 // Percentage bonus to city locations near an ocean.
103 #define NAVAL_EMPHASIS 20
104 
105 /* Modifier for defense bonus that is applied to city location want.
106  * This is % of defense % to increase want by. */
107 #define DEFENSE_EMPHASIS 20
108 
109 class PFPath;
110 
112  char food; // food output of the tile
113  char trade; // trade output of the tile
114  char shield; // shield output of the tile
115 
116  int sum; // weighted sum of the tile output (used by AI)
117 
118  int reserved; // reservation for this tile; used by print_citymap()
119 
120  int turn; // the turn the values were calculated
121 };
122 
124 struct tile_data_cache *
125 tile_data_cache_copy(const struct tile_data_cache *ptdc);
126 
127 struct ai_settler {
128  QHash<int, const struct tile_data_cache *> *tdc_hash;
129 
130 #ifdef FREECIV_DEBUG
131  struct {
132  int hit;
133  int old;
134  int miss;
135  int save;
136  } cache;
137 #endif // FREECIV_DEBUG
138 };
139 
140 struct cityresult final {
141  cityresult(struct tile *tile);
142  ~cityresult();
143 
144  struct tile *tile;
145  int total = 0; // total value of position
146  int result = -666; // amortized and adjusted total value
147  int corruption = 0, waste = 0;
148  bool overseas = false; // have to use boat to get there
149  bool virt_boat = false; /* virtual boat was used in search,
150  * so need to build one */
151 
152  struct {
153  struct tile_data_cache *tdc = nullptr; /* values of city center; link to
154  * the data in tdc_hash. */
156 
157  struct {
158  struct tile *tile = nullptr; // best other tile
159  int cindex = false; // city-relative index for other tile
160  struct tile_data_cache *tdc = nullptr; /* value of best other tile; link
161  * to the data in tdc_hash. */
163 
164  int remaining = 0; // value of all other tiles
165 
166  // Save the result for print_citymap().
167  QHash<int, const struct tile_data_cache *> tdc_hash;
168 
169  int city_radius_sq; // current squared radius of the city
170 };
171 
172 static const struct tile_data_cache *
173 tdc_plr_get(struct ai_type *ait, struct player *plr, int tindex);
174 static void tdc_plr_set(struct ai_type *ait, struct player *plr, int tindex,
175  const struct tile_data_cache *tdcache);
176 
177 static std::unique_ptr<cityresult> cityresult_fill(struct ai_type *ait,
178  struct player *pplayer,
179  struct tile *center);
180 static bool food_starvation(const std::unique_ptr<cityresult> &result);
181 static bool shield_starvation(const std::unique_ptr<cityresult> &result);
182 static int result_defense_bonus(struct player *pplayer,
183  const std::unique_ptr<cityresult> &result);
184 static int naval_bonus(const std::unique_ptr<cityresult> &result);
185 static void print_cityresult(struct player *pplayer,
186  const std::unique_ptr<cityresult> &cr);
187 std::unique_ptr<cityresult> city_desirability(struct ai_type *ait,
188  struct player *pplayer,
189  struct unit *punit,
190  struct tile *ptile);
191 static std::unique_ptr<cityresult>
192 settler_map_iterate(struct ai_type *ait, struct pf_parameter *parameter,
193  struct unit *punit, int boat_cost);
194 static std::unique_ptr<cityresult>
195 find_best_city_placement(struct ai_type *ait, struct unit *punit,
196  bool look_for_boat, bool use_virt_boat);
197 static bool dai_do_build_city(struct ai_type *ait, struct player *pplayer,
198  struct unit *punit);
199 
204  : tile(ptile), city_radius_sq(game.info.init_city_radius_sq)
205 {
206  fc_assert_action(ptile != nullptr, throw std::bad_alloc());
207 }
208 
213 {
214  for (const auto &ptdc : qAsConst(tdc_hash)) {
215  delete[] ptdc;
216  }
217 }
218 
227 static std::unique_ptr<cityresult> cityresult_fill(struct ai_type *ait,
228  struct player *pplayer,
229  struct tile *center)
230 {
231  struct city *pcity = tile_city(center);
232  struct government *curr_govt = government_of_player(pplayer);
233  struct player *saved_owner = nullptr;
234  struct tile *saved_claimer = nullptr;
235  bool virtual_city = false;
236  bool handicap = has_handicap(pplayer, H_MAP);
237  struct adv_data *adv = adv_data_get(pplayer, nullptr);
238  struct ai_plr *ai = dai_plr_data_get(ait, pplayer, nullptr);
239 
240  fc_assert_ret_val(ai != nullptr, nullptr);
241  fc_assert_ret_val(center != nullptr, nullptr);
242 
243  pplayer->government = adv->goal.govt.gov;
244 
245  // Create a city result and set default values.
246  auto result = std::make_unique<cityresult>(center);
247 
248  if (!pcity) {
249  pcity = create_city_virtual(pplayer, result->tile, "Virtuaville");
250  saved_owner = tile_owner(result->tile);
251  saved_claimer = tile_claimer(result->tile);
252  tile_set_owner(result->tile, pplayer, result->tile); // temporarily
253  city_choose_build_default(pcity); // ??
254  virtual_city = true;
255  }
256 
257  result->city_radius_sq = city_map_radius_sq_get(pcity);
258 
259  city_tile_iterate_index(result->city_radius_sq, result->tile, ptile,
260  cindex)
261  {
262  int tindex = tile_index(ptile);
263  int reserved = citymap_read(ptile);
264  bool city_center = (result->tile == ptile); /*is_city_center()*/
265  struct tile_data_cache *ptdc;
266 
267  if (reserved < 0 || (handicap && !map_is_known(ptile, pplayer))
268  || nullptr != tile_worked(ptile)) {
269  // Tile is reserved or we can't see it
270  ptdc = tile_data_cache_new();
271  ptdc->shield = 0;
272  ptdc->trade = 0;
273  ptdc->food = 0;
274  ptdc->sum = -1;
275  ptdc->reserved = reserved;
276  // ptdc->turn was set by tile_data_cache_new().
277  } else {
278  const struct tile_data_cache *ptdc_hit =
279  tdc_plr_get(ait, pplayer, tindex);
280  if (!ptdc_hit || city_center) {
281  // We cannot read city center from cache
282  ptdc = tile_data_cache_new();
283 
284  // Food
285  ptdc->food = city_tile_output(pcity, ptile, false, O_FOOD);
286  // Shields
287  ptdc->shield = city_tile_output(pcity, ptile, false, O_SHIELD);
288  // Trade
289  ptdc->trade = city_tile_output(pcity, ptile, false, O_TRADE);
290  // Weighted sum
291  ptdc->sum = ptdc->food * adv->food_priority
292  + ptdc->trade * adv->science_priority
293  + ptdc->shield * adv->shield_priority;
294  // Balance perfection
295  ptdc->sum *= PERFECTION / 2;
296  if (ptdc->food >= 2) {
297  ptdc->sum *= 2; // we need this to grow
298  }
299 
300  if (!city_center && virtual_city) {
301  /* real cities and any city center will give us spossibly
302  * skewed results */
303  tdc_plr_set(ait, pplayer, tindex, tile_data_cache_copy(ptdc));
304  }
305  } else {
306  ptdc = tile_data_cache_copy(ptdc_hit);
307  }
308  }
309 
310  // Save reservation status for debugging.
311  ptdc->reserved = reserved;
312 
313  // Avoid crowdedness, except for city center.
314  if (ptdc->sum > 0) {
315  ptdc->sum -= MIN(reserved * GROWTH_PRIORITY, ptdc->sum - 1);
316  }
317 
318  // Calculate city center and best other than city center
319  if (city_center) {
320  // Set city center.
321  result->city_center.tdc = ptdc;
322  } else if (!result->best_other.tdc) {
323  // Set best other tile.
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) {
328  // First add other other to remaining
329  result->remaining +=
330  result->best_other.tdc->sum / GROWTH_POTENTIAL_DEEMPHASIS;
331  // Then make new best other
332  result->best_other.tdc = ptdc;
333  result->best_other.tile = ptile;
334  result->best_other.cindex = cindex;
335  } else {
336  /* Save total remaining calculation, divided by crowdedness
337  * of the area and the emphasis placed on space for growth. */
338  result->remaining += ptdc->sum / GROWTH_POTENTIAL_DEEMPHASIS;
339  }
340 
341  if (result->tdc_hash.contains(cindex)) {
342  delete[] result->tdc_hash.value(cindex);
343  }
344  result->tdc_hash.insert(cindex, ptdc);
345  }
347 
348  // We need a city center.
349  fc_assert_ret_val(result->city_center.tdc != nullptr, nullptr);
350 
351  if (virtual_city) {
352  // Baseline is a size one city (city center + best extra tile).
353  result->total =
354  result->city_center.tdc->sum
355  + (result->best_other.tdc != nullptr ? result->best_other.tdc->sum
356  : 0);
357  } else if (result->best_other.tdc != nullptr) {
358  /* Baseline is best extra tile only. This is why making new cities
359  * is so darn good. */
360  result->total = result->best_other.tdc->sum;
361  } else {
362  // There is no available tile in this city. All is worked.
363  result->total = 0;
364  return result;
365  }
366 
367  // Now we have a valid city center as well as best other tile.
368 
369  if (virtual_city) {
370  auto gov_centers = player_gov_centers(city_owner(pcity));
371  /* Corruption and waste of a size one city deducted. Notice that we
372  * don't do this if 'fulltradesize' is changed, since then we'd
373  * never make cities. */
374  int shield =
375  result->city_center.tdc->shield + result->best_other.tdc->shield;
376  result->waste =
377  adv->shield_priority
378  * city_waste(pcity, O_SHIELD, shield, nullptr, gov_centers);
379 
380  if (game.info.fulltradesize == 1) {
381  int trade =
382  result->city_center.tdc->trade + result->best_other.tdc->trade;
383  result->corruption =
384  adv->science_priority
385  * city_waste(pcity, O_TRADE, trade, nullptr, gov_centers);
386  } else {
387  result->corruption = 0;
388  }
389  } else {
390  auto gov_centers = player_gov_centers(city_owner(pcity));
391  /* Deduct difference in corruption and waste for real cities. Note that
392  * it is possible (with notradesize) that we _gain_ value here. */
393  city_size_add(pcity, 1);
394  result->corruption =
395  adv->science_priority
396  * (city_waste(pcity, O_TRADE, result->best_other.tdc->trade, nullptr,
397  gov_centers)
398  - pcity->waste[O_TRADE]);
399  result->waste =
400  adv->shield_priority
401  * (city_waste(pcity, O_SHIELD, result->best_other.tdc->shield,
402  nullptr, gov_centers)
403  - pcity->waste[O_SHIELD]);
404  city_size_add(pcity, -1);
405  }
406  result->total -= result->corruption;
407  result->total -= result->waste;
408  result->total = MAX(0, result->total);
409 
410  pplayer->government = curr_govt;
411  if (virtual_city) {
412  destroy_city_virtual(pcity);
413  tile_set_owner(result->tile, saved_owner, saved_claimer);
414  }
415 
416  fc_assert_ret_val(result->city_center.tdc->sum >= 0, nullptr);
417  fc_assert_ret_val(result->remaining >= 0, nullptr);
418 
419  return result;
420 }
421 
426 {
427  struct tile_data_cache *ptdc_copy = new tile_data_cache[1]();
428 
429  // Set the turn the tile data cache was created.
430  ptdc_copy->turn = game.info.turn;
431 
432  return ptdc_copy;
433 }
434 
438 struct tile_data_cache *
440 {
441  struct tile_data_cache *ptdc_copy;
442 
443  fc_assert_ret_val(ptdc, nullptr);
444 
445  ptdc_copy = tile_data_cache_new();
446  ptdc_copy->shield = ptdc->shield;
447  ptdc_copy->trade = ptdc->trade;
448  ptdc_copy->food = ptdc->food;
449 
450  ptdc_copy->sum = ptdc->sum;
451  ptdc_copy->reserved = ptdc->reserved;
452  ptdc_copy->turn = ptdc->turn;
453 
454  return ptdc_copy;
455 }
456 
460 static const struct tile_data_cache *
461 tdc_plr_get(struct ai_type *ait, struct player *plr, int tindex)
462 {
463  struct ai_plr *ai = dai_plr_data_get(ait, plr, nullptr);
464 
465  fc_assert_ret_val(ai != nullptr, nullptr);
466  fc_assert_ret_val(ai->settler != nullptr, nullptr);
467  fc_assert_ret_val(ai->settler->tdc_hash != nullptr, nullptr);
468 
469  const struct tile_data_cache *ptdc;
470 
471  ptdc = ai->settler->tdc_hash->value(tindex, nullptr);
472 
473  if (!ptdc) {
474 #ifdef FREECIV_DEBUG
475  ai->settler->cache.miss++;
476 #endif // FREECIV_DEBUG
477  return nullptr;
478  } else if (ptdc->turn != game.info.turn) {
479 #ifdef FREECIV_DEBUG
480  ai->settler->cache.old++;
481 #endif // FREECIV_DEBUG
482  return nullptr;
483  } else {
484 #ifdef FREECIV_DEBUG
485  ai->settler->cache.hit++;
486 #endif // FREECIV_DEBUG
487  return ptdc;
488  }
489 }
490 
494 static void tdc_plr_set(struct ai_type *ait, struct player *plr, int tindex,
495  const struct tile_data_cache *ptdc)
496 {
497  struct ai_plr *ai = dai_plr_data_get(ait, plr, nullptr);
498 
499  fc_assert_ret(ai != nullptr);
500  fc_assert_ret(ai->settler != nullptr);
501  fc_assert_ret(ai->settler->tdc_hash != nullptr);
502  fc_assert_ret(ptdc != nullptr);
503 
504 #ifdef FREECIV_DEBUG
505  ai->settler->cache.save++;
506 #endif // FREECIV_DEBUG
507 
508  if (ai->settler->tdc_hash->contains(tindex)) {
509  delete[] ai->settler->tdc_hash->value(tindex);
510  }
511  ai->settler->tdc_hash->insert(tindex, ptdc);
512 }
513 
517 static bool food_starvation(const std::unique_ptr<cityresult> &result)
518 {
519  /* Avoid starvation: We must have enough food to grow.
520  * Note: this does not handle the case of a newly founded city breaking
521  * even but being immediately able to build an improvement increasing its
522  * yield (such as supermarkets and harbours in the classic ruleset).
523  * /MSS */
524  return (result->city_center.tdc->food
525  + (result->best_other.tdc ? result->best_other.tdc->food : 0)
526  <= game.info.food_cost);
527 }
528 
532 static bool shield_starvation(const std::unique_ptr<cityresult> &result)
533 {
534  // Avoid resource starvation.
535  return (result->city_center.tdc->shield
536  + (result->best_other.tdc ? result->best_other.tdc->shield : 0)
537  == 0);
538 }
539 
544 static int result_defense_bonus(struct player *pplayer,
545  const std::unique_ptr<cityresult> &result)
546 {
547  // Defense modification (as tie breaker mostly)
548  int defense_bonus = 10 + tile_terrain(result->tile)->defense_bonus / 10;
549  int extra_bonus = 0;
550  struct tile *vtile = tile_virtual_new(result->tile);
551  struct city *vcity = create_city_virtual(pplayer, vtile, "");
552 
553  tile_set_worked(vtile, vcity); // Link tile_city(vtile) to vcity.
554  upgrade_city_extras(vcity, nullptr); // Give city free extras.
555  extra_type_iterate(pextra)
556  {
557  if (tile_has_extra(vtile, pextra)) {
558  /* TODO: Do not use full bonus of those road types
559  * that are not native to all important units. */
560  extra_bonus += pextra->defense_bonus;
561  }
562  }
564  tile_virtual_destroy(vtile);
565 
566  defense_bonus += (defense_bonus * extra_bonus) / 100;
567 
568  return 100 / (result->total + 1)
569  * (100 / defense_bonus * DEFENSE_EMPHASIS);
570 }
571 
575 static int naval_bonus(const std::unique_ptr<cityresult> &result)
576 {
577  bool ocean_adjacent = is_terrain_class_near_tile(result->tile, TC_OCEAN);
578 
579  // Adjust for ocean adjacency, which is nice
580  if (ocean_adjacent) {
581  return (result->total * NAVAL_EMPHASIS) / 100;
582  } else {
583  return 0;
584  }
585 }
586 
590 static void print_cityresult(struct player *pplayer,
591  const std::unique_ptr<cityresult> &cr)
592 {
593  int tiles = city_map_tiles(cr->city_radius_sq);
594  const struct tile_data_cache *ptdc;
595 
596  fc_assert_ret(tiles > 0);
597 
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]());
602 
603  city_map_iterate(cr->city_radius_sq, cindex, x, y)
604  {
605  ptdc = cr->tdc_hash.value(cindex, nullptr);
606  fc_assert_ret(ptdc);
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;
611  }
613 
614  // print reservations
615  log_test("cityresult for (x,y,radius_sq) = (%d, %d, %d) - Reservations:",
616  TILE_XY(cr->tile), cr->city_radius_sq);
617  citylog_map_data(LOG_TEST, cr->city_radius_sq, city_map_reserved.data());
618 
619  // print food
620  log_test("cityresult for (x,y,radius_sq) = (%d, %d, %d) - Food:",
621  TILE_XY(cr->tile), cr->city_radius_sq);
622  citylog_map_data(LOG_TEST, cr->city_radius_sq, city_map_food.data());
623 
624  // print shield
625  log_test("cityresult for (x,y,radius_sq) = (%d, %d, %d) - Shield:",
626  TILE_XY(cr->tile), cr->city_radius_sq);
627  citylog_map_data(LOG_TEST, cr->city_radius_sq, city_map_shield.data());
628 
629  // print trade
630  log_test("cityresult for (x,y,radius_sq) = (%d, %d, %d) - Trade:",
631  TILE_XY(cr->tile), cr->city_radius_sq);
632  citylog_map_data(LOG_TEST, cr->city_radius_sq, city_map_trade.data());
633 
634  log_test("city center (%d, %d) %d + best other (abs: %d, %d)"
635  " (cindex: %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,
642  result_defense_bonus(pplayer, cr), naval_bonus(cr));
643  log_test("= %d (%d)", cr->total, cr->result);
644 
645  if (food_starvation(cr)) {
646  log_test(" ** FOOD STARVATION **");
647  }
648  if (shield_starvation(cr)) {
649  log_test(" ** RESOURCE STARVATION **");
650  }
651 }
652 
658 std::unique_ptr<cityresult> city_desirability(struct ai_type *ait,
659  struct player *pplayer,
660  struct unit *punit,
661  struct tile *ptile)
662 {
663  struct city *pcity = tile_city(ptile);
664  struct adv_data *ai = adv_data_get(pplayer, nullptr);
665  std::unique_ptr<cityresult> cr = nullptr;
666 
667  fc_assert_ret_val(punit, nullptr);
668  fc_assert_ret_val(pplayer, nullptr);
669  fc_assert_ret_val(ai, nullptr);
670 
671  if (!city_can_be_built_here(ptile, punit)
672  || (has_handicap(pplayer, H_MAP) && !map_is_known(ptile, pplayer))) {
673  return nullptr;
674  }
675 
676  // Check if another settler has taken a spot within mindist
677  square_iterate(&(wld.map), ptile, game.info.citymindist - 1, tile1)
678  {
679  if (citymap_is_reserved(tile1)) {
680  return nullptr;
681  }
682  }
684 
685  if (adv_danger_at(punit, ptile)) {
686  return nullptr;
687  }
688 
689  if (pcity
690  && (city_size_get(pcity) + unit_pop_value(punit)
691  > game.info.add_to_size_limit)) {
692  // Can't exceed population limit.
693  return nullptr;
694  }
695 
696  if (!pcity && citymap_is_reserved(ptile)) {
697  return nullptr; // reserved, go away
698  }
699 
700  // If (x, y) is an existing city, consider immigration
701  if (pcity && city_owner(pcity) == pplayer) {
702  return nullptr;
703  }
704 
705  cr = cityresult_fill(ait, pplayer, ptile); // Burn CPU, burn!
706  if (!cr) {
707  // Failed to find a good spot
708  return nullptr;
709  }
710 
711  /*** Alright: Now consider building a new city ***/
712 
713  if (food_starvation(cr) || shield_starvation(cr)) {
714  return nullptr;
715  }
716 
717  cr->total += result_defense_bonus(pplayer, cr);
718  cr->total += naval_bonus(cr);
719 
720  // Add remaining points, which is our potential
721  cr->total += cr->remaining;
722 
723  fc_assert_ret_val(cr->total >= 0, nullptr); // Does not frees cr!
724 
725  return cr;
726 }
727 
739 static std::unique_ptr<cityresult>
740 settler_map_iterate(struct ai_type *ait, struct pf_parameter *parameter,
741  struct unit *punit, int boat_cost)
742 {
743  std::unique_ptr<cityresult> best = nullptr;
744  int best_turn = 0; // Which turn we found the best fit
745  struct player *pplayer = unit_owner(punit);
746  struct pf_map *pfm;
747 
748  pfm = pf_map_new(parameter);
749  pf_map_move_costs_iterate(pfm, ptile, move_cost, false)
750  {
751  int turns;
752 
753  if (boat_cost == 0 && unit_class_get(punit)->adv.sea_move == MOVE_NONE
754  && tile_continent(ptile) != tile_continent(unit_tile(punit))) {
755  /* We have an accidential land bridge. Ignore it. It will in all
756  * likelihood go away next turn, or even in a few nanoseconds. */
757  continue;
758  }
759  if (BORDERS_DISABLED != game.info.borders) {
760  struct player *powner = tile_owner(ptile);
761  if (nullptr != powner && powner != pplayer
762  && pplayers_in_peace(powner, pplayer)) {
763  // Land theft does not make for good neighbours.
764  continue;
765  }
766  }
767 
768  // Calculate worth
769  auto cr = city_desirability(ait, pplayer, punit, ptile);
770 
771  // Check if actually found something
772  if (!cr) {
773  continue;
774  }
775 
776  // This algorithm punishes long treks
777  turns = move_cost / parameter->move_rate;
778  cr->result = amortize(cr->total, PERFECTION * turns);
779 
780  /* Reduce want by settler cost. Easier than amortize, but still
781  * weeds out very small wants. ie we create a threshold here. */
782  /* We also penalise here for using a boat (either virtual or real)
783  * it's crude but what isn't?
784  * Settler gets used, boat can make multiple trips. */
785  cr->result -= unit_build_shield_cost_base(punit) + boat_cost / 3;
786 
787  // Find best spot
788  if ((!best && cr->result > 0) || (best && cr->result > best->result)) {
789  // save the new 'best' value.
790  best = std::move(cr);
791  cr = nullptr;
792  best_turn = turns;
793 
794  log_debug("settler map search (search): (%d,%d) %d",
795  TILE_XY(best->tile), best->result);
796  }
797 
798  /* Can we terminate early? We have a 'good enough' spot, and
799  * we don't block the establishment of a better city just one
800  * further step away. */
801  if (best && best->result > RESULT_IS_ENOUGH
802  && turns > parameter->move_rate // sic -- yeah what an explanation!
803  && best_turn < turns /*+ game.info.min_dist_bw_cities*/) {
804  break;
805  }
806  }
808 
809  pf_map_destroy(pfm);
810 
811  if (best) {
812  log_debug("settler map search (final): (%d,%d) %d", TILE_XY(best->tile),
813  best->result);
814  } else {
815  log_debug("settler map search (final): no result");
816  }
817 
818  return best;
819 }
820 
834 static std::unique_ptr<cityresult>
835 find_best_city_placement(struct ai_type *ait, struct unit *punit,
836  bool look_for_boat, bool use_virt_boat)
837 {
838  struct pf_parameter parameter;
839  struct player *pplayer = unit_owner(punit);
840  struct unit *ferry = nullptr;
841  std::unique_ptr<cityresult> cr1 = nullptr, cr2 = nullptr;
842 
843  fc_assert_ret_val(is_ai(pplayer), nullptr);
844  // Only virtual units may use virtual boats:
845  fc_assert_ret_val(0 == punit->id || !use_virt_boat, nullptr);
846 
847  // Phase 1: Consider building cities on our continent
848 
849  pft_fill_unit_parameter(&parameter, punit);
850  parameter.omniscience = !has_handicap(pplayer, H_MAP);
851  cr1 = settler_map_iterate(ait, &parameter, punit, 0);
852 
853  if (cr1 && cr1->result > RESULT_IS_ENOUGH) {
854  // skip further searches
855  return cr1;
856  }
857 
858  // Phase 2: Consider travelling to another continent
859 
860  if (look_for_boat) {
861  auto path = PFPath();
862  int ferry_id = aiferry_find_boat(ait, punit, 1, &path);
863 
864  ferry = game_unit_by_number(ferry_id);
865  }
866 
867  if (ferry
868  || (use_virt_boat
869  && is_terrain_class_near_tile(unit_tile(punit), TC_OCEAN)
870  && tile_city(unit_tile(punit)))) {
871  if (!ferry) {
872  // No boat? Get a virtual one!
873  struct unit_type *boattype =
874  best_role_unit_for_player(pplayer, L_FERRYBOAT);
875 
876  if (boattype == nullptr) {
877  // Sea travel not possible yet. Bump tech want for ferries.
878  boattype = get_role_unit(L_FERRYBOAT, 0);
879 
880  if (nullptr != boattype && A_NEVER != boattype->require_advance) {
881  struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
882 
883  plr_data->tech_want[advance_index(boattype->require_advance)] +=
885  TECH_LOG(ait, LOG_DEBUG, pplayer, boattype->require_advance,
886  "+ %d for %s to ferry settler", FERRY_TECH_WANT,
887  utype_rule_name(boattype));
888  }
889  // return the result from the search on our current continent
890  return cr1;
891  }
892  ferry = unit_virtual_create(pplayer, nullptr, boattype, 0);
893  unit_tile_set(ferry, unit_tile(punit));
894  }
895 
896  fc_assert(dai_is_ferry(ferry, ait));
897  pft_fill_unit_overlap_param(&parameter, ferry);
898  parameter.omniscience = !has_handicap(pplayer, H_MAP);
899  parameter.get_TB = no_fights_or_unknown;
900 
901  /* FIXME: Maybe penalty for using an existing boat is too high?
902  * We shouldn't make the penalty for building a new boat too high though.
903  * Building a new boat is like a war against a weaker enemy --
904  * good for the economy. (c) Bush family */
905  cr2 = settler_map_iterate(ait, &parameter, punit,
907  if (cr2) {
908  cr2->overseas = true;
909  cr2->virt_boat = (ferry->id == 0);
910  }
911 
912  if (ferry->id == 0) {
913  unit_virtual_destroy(ferry);
914  }
915 
916  /* If we use a virtual boat, we must have permission and be emigrating:
917  */
918  // FIXME: These assert do not frees cr2!
919  fc_assert_ret_val(!cr2 || (!cr2->virt_boat || use_virt_boat), nullptr);
920  fc_assert_ret_val(!cr2 || (!cr2->virt_boat || cr2->overseas), nullptr);
921  }
922 
923  if (!cr1) {
924  /* No want for a new city on our current continent; return the result for
925  * traveling by boat. */
926  return cr2;
927  } else if (!cr2) {
928  /* No want for an overseas city; return the result for a city on our
929  * current continent. */
930  return cr1;
931  }
932 
933  /* We want an overseas city and a city on the current continent - select
934  * the best! */
935  if (cr1->result > cr2->result) {
936  return cr1;
937  } else {
938  return cr2;
939  }
940 }
941 
946 {
947  fc_assert_ret(ai != nullptr);
948  fc_assert_ret(ai->settler == nullptr);
949 
950  ai->settler = new ai_settler[1]();
951  ai->settler->tdc_hash = new QHash<int, const struct tile_data_cache *>;
952 
953 #ifdef FREECIV_DEBUG
954  ai->settler->cache.hit = 0;
955  ai->settler->cache.old = 0;
956  ai->settler->cache.miss = 0;
957  ai->settler->cache.save = 0;
958 #endif // FREECIV_DEBUG
959 }
960 
964 void dai_auto_settler_run(struct ai_type *ait, struct player *pplayer,
965  struct unit *punit, struct settlermap *state)
966 {
967  adv_want best_impr = 0; // value of best terrain improvement we can do
968  enum unit_activity best_act;
969  struct extra_type *best_target;
970  struct tile *best_tile = nullptr;
971  PFPath path;
972  struct city *pcity = nullptr;
973 
974  // time it will take worker to complete its given task
975  int completion_time = 0;
976 
977  CHECK_UNIT(punit);
978 
979  /*** If we are on a city mission: Go where we should ***/
980 
981 BUILD_CITY:
982 
983  if (def_ai_unit_data(punit, ait)->task == AIUNIT_BUILD_CITY) {
984  struct tile *ptile = punit->goto_tile;
985  int sanity = punit->id;
986 
987  /* Check that the mission is still possible. If the tile has become
988  * unavailable, call it off. */
989  if (!city_can_be_built_here(ptile, punit)) {
990  dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
991  set_unit_activity(punit, ACTIVITY_IDLE);
992  send_unit_info(nullptr, punit);
993  return; // avoid recursion at all cost
994  } else {
995  // Go there
996  if ((!dai_gothere(ait, pplayer, punit, ptile)
997  && nullptr == game_unit_by_number(sanity))
998  || punit->moves_left <= 0) {
999  return;
1000  }
1001  if (same_pos(unit_tile(punit), ptile)) {
1002  if (!dai_do_build_city(ait, pplayer, punit)) {
1003  UNIT_LOG(LOG_DEBUG, punit, "could not make city on %s",
1004  tile_get_info_text(unit_tile(punit), true, 0));
1005  dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
1006  /* Only known way to end in here is that hut turned in to a city
1007  * when settler entered tile. So this is not going to lead in any
1008  * serious recursion. */
1009  dai_auto_settler_run(ait, pplayer, punit, state);
1010 
1011  return;
1012  } else {
1013  return; // We came, we saw, we built...
1014  }
1015  } else {
1016  UNIT_LOG(LOG_DEBUG, punit, "could not go to target");
1017  // ai_unit_new_role(punit, AIUNIT_NONE, nullptr);
1018  return;
1019  }
1020  }
1021  }
1022 
1023  /*** Try find some work ***/
1024 
1025  if (unit_has_type_flag(punit, UTYF_SETTLERS)) {
1026  struct worker_task *best_task;
1027 
1029 
1030  // Have nearby cities requests?
1031  pcity = settler_evaluate_city_requests(punit, &best_task, &path, state);
1032 
1033  if (pcity != nullptr) {
1034  if (!path.empty()) {
1035  completion_time = path[-1].turn;
1036  best_impr = 1;
1037  best_act = best_task->act;
1038  best_target = best_task->tgt;
1039  best_tile = best_task->ptile;
1040  } else {
1041  pcity = nullptr;
1042  }
1043  }
1044 
1045  if (pcity == nullptr) {
1046  best_impr = settler_evaluate_improvements(
1047  punit, &best_act, &best_target, &best_tile, &path, state);
1048  if (!path.empty()) {
1049  completion_time = path[-1].turn;
1050  }
1051  }
1052  UNIT_LOG(LOG_DEBUG, punit, "impr want " ADV_WANT_PRINTF, best_impr);
1054  }
1055 
1056  if (unit_is_cityfounder(punit)) {
1057  // may use a boat:
1059  auto result = find_best_city_placement(ait, punit, true, false);
1061  if (result && result->result > best_impr) {
1062  UNIT_LOG(LOG_DEBUG, punit, "city want %d", result->result);
1063  if (tile_city(result->tile)) {
1064  UNIT_LOG(LOG_DEBUG, punit, "immigrates to %s (%d, %d)",
1065  city_name_get(tile_city(result->tile)),
1066  TILE_XY(result->tile));
1067  } else {
1068  UNIT_LOG(LOG_DEBUG, punit, "makes city at (%d, %d)",
1069  TILE_XY(result->tile));
1070  if (punit->server.debug) {
1071  print_cityresult(pplayer, result);
1072  }
1073  }
1074  // Go make a city!
1075  adv_unit_new_task(punit, AUT_BUILD_CITY, result->tile);
1076  if (result->best_other.tile && result->best_other.tdc->sum >= 0) {
1077  /* Reserve best other tile (if there is one). It is the tile where
1078  * the first citizen of the city is working. */
1079  citymap_reserve_tile(result->best_other.tile, punit->id);
1080  }
1081  punit->goto_tile = result->tile; // TMP
1082 
1083  /*** Go back to and found a city ***/
1084  path = PFPath();
1085  goto BUILD_CITY;
1086  } else if (best_impr > 0) {
1087  UNIT_LOG(LOG_DEBUG, punit, "improves terrain instead of founding");
1088  /* Terrain improvements follows the old model, and is recalculated
1089  * each turn. */
1090  adv_unit_new_task(punit, AUT_AUTO_SETTLER, best_tile);
1091  } else {
1092  UNIT_LOG(LOG_DEBUG, punit, "cannot find work");
1093  fc_assert(result == nullptr);
1094  dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
1095  return;
1096  }
1097  } else {
1098  // We are a worker or engineer
1099  adv_unit_new_task(punit, AUT_AUTO_SETTLER, best_tile);
1100  }
1101 
1102  if (auto_settler_setup_work(pplayer, punit, state, 0, &path, best_tile,
1103  best_act, &best_target, completion_time)) {
1104  if (pcity != nullptr) {
1105  clear_worker_tasks(pcity);
1106  }
1107  }
1108 }
1109 
1113 void dai_auto_settler_cont(struct ai_type *ait, struct player *pplayer,
1114  struct unit *punit, struct settlermap *state)
1115 {
1116  if (!adv_settler_safe_tile(pplayer, punit, unit_tile(punit))) {
1117  unit_activity_handling(punit, ACTIVITY_IDLE);
1118  }
1119 }
1120 
1124 void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
1125 {
1126  bool caller_closes;
1127  struct ai_plr *ai = dai_plr_data_get(ait, pplayer, &caller_closes);
1128 
1129  fc_assert_ret(ai != nullptr);
1130  fc_assert_ret(ai->settler != nullptr);
1131  fc_assert_ret(ai->settler->tdc_hash != nullptr);
1132 
1133 #ifdef FREECIV_DEBUG
1134  log_debug("[aisettler cache for %s] save: %d, miss: %d, old: %d, hit: %d",
1135  player_name(pplayer), ai->settler->cache.save,
1136  ai->settler->cache.miss, ai->settler->cache.old,
1137  ai->settler->cache.hit);
1138 
1139  ai->settler->cache.hit = 0;
1140  ai->settler->cache.old = 0;
1141  ai->settler->cache.miss = 0;
1142  ai->settler->cache.save = 0;
1143 #endif // FREECIV_DEBUG
1144 
1145  for (const auto *ptdc : qAsConst(*ai->settler->tdc_hash)) {
1146  delete[] ptdc;
1147  }
1148  ai->settler->tdc_hash->clear();
1149 
1150  if (caller_closes) {
1151  dai_data_phase_finished(ait, pplayer);
1152  }
1153 }
1154 
1159 {
1160  fc_assert_ret(ai != nullptr);
1161 
1162  if (ai->settler) {
1163  delete ai->settler->tdc_hash;
1164  delete[] ai->settler;
1165  }
1166  ai->settler = nullptr;
1167 }
1168 
1172 static bool dai_do_build_city(struct ai_type *ait, struct player *pplayer,
1173  struct unit *punit)
1174 {
1175  struct tile *ptile = unit_tile(punit);
1176  struct city *pcity;
1177 
1178  fc_assert_ret_val(pplayer == unit_owner(punit), false);
1179  unit_activity_handling(punit, ACTIVITY_IDLE);
1180 
1181  // Free city reservations
1182  dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
1183 
1184  pcity = tile_city(ptile);
1185  if (pcity) {
1186  /* This can happen for instance when there was hut at this tile
1187  * and it turned in to a city when settler entered tile. */
1188  log_debug("%s: There is already a city at (%d, %d)!",
1189  player_name(pplayer), TILE_XY(ptile));
1190  return false;
1191  }
1192  unit_do_action(pplayer, punit->id, ptile->index, 0,
1193  city_name_suggestion(pplayer, ptile), ACTION_FOUND_CITY);
1194  pcity = tile_city(ptile);
1195  if (!pcity) {
1196  enum ane_kind reason = action_not_enabled_reason(
1197  punit, ACTION_FOUND_CITY, ptile, nullptr, nullptr);
1198 
1199  if (reason == ANEK_CITY_TOO_CLOSE_TGT) {
1200  /* This is acceptable. A hut in the path to the tile may have created
1201  * a city that now is too close. */
1202  log_debug("%s: Failed to build city at (%d, %d)", player_name(pplayer),
1203  TILE_XY(ptile));
1204  } else {
1205  // The request was illegal to begin with.
1206  qCritical("%s: Failed to build city at (%d, %d). Reason id: %d",
1207  player_name(pplayer), TILE_XY(ptile), reason);
1208  }
1209  return false;
1210  }
1211 
1212  /* We have to rebuild at least the cache for this city. This event is
1213  * rare enough we might as well build the whole thing. Who knows what
1214  * else might be cached in the future? */
1215  fc_assert_ret_val(pplayer == city_owner(pcity), false);
1217 
1218  // Init ai.choice. Handling ferryboats might use it.
1219  adv_init_choice(&def_ai_city_data(pcity, ait)->choice);
1220 
1221  return true;
1222 }
1223 
1228 void contemplate_new_city(struct ai_type *ait, struct city *pcity)
1229 {
1230  struct unit *virtualunit;
1231  struct tile *pcenter = city_tile(pcity);
1232  struct player *pplayer = city_owner(pcity);
1233  struct unit_type *unit_type;
1234 
1235  if (game.scenario.prevent_new_cities) {
1236  return;
1237  }
1238 
1239  unit_type = best_role_unit(pcity, action_id_get_role(ACTION_FOUND_CITY));
1240 
1241  if (unit_type == nullptr) {
1242  log_debug("No ACTION_FOUND_CITY role unit available");
1243  return;
1244  }
1245 
1246  // Create a localized "virtual" unit to do operations with.
1247  virtualunit = unit_virtual_create(pplayer, pcity, unit_type, 0);
1248  unit_tile_set(virtualunit, pcenter);
1249 
1250  if (is_ai(pplayer)) {
1251  bool is_coastal = is_terrain_class_near_tile(pcenter, TC_OCEAN);
1252  struct ai_city *city_data = def_ai_city_data(pcity, ait);
1253 
1254  auto result =
1255  find_best_city_placement(ait, virtualunit, is_coastal, is_coastal);
1256 
1257  if (result) {
1258  fc_assert_ret(0 <= result->result); // 'result' is not freed!
1259 
1260  CITY_LOG(LOG_DEBUG, pcity,
1261  "want(%d) to establish city at"
1262  " (%d, %d) and will %s to get there",
1263  result->result, TILE_XY(result->tile),
1264  (result->virt_boat
1265  ? "build a boat"
1266  : (result->overseas ? "use a boat" : "walk")));
1267 
1268  city_data->founder_want =
1269  (result->virt_boat ? -result->result : result->result);
1270  city_data->founder_boat = result->overseas;
1271  } else {
1272  CITY_LOG(LOG_DEBUG, pcity, "want no city");
1273  city_data->founder_want = 0;
1274  }
1275  } else {
1276  // Always failing
1277  fc_assert_ret(is_ai(pplayer));
1278  }
1279 
1280  unit_virtual_destroy(virtualunit);
1281 }
#define action_id_get_role(act_id)
Definition: actions.h:580
void adv_init_choice(struct adv_choice *choice)
Sets the values of the choice to initial values.
Definition: advchoice.cpp:27
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Return a pointer to our data.
Definition: advdata.cpp:591
bool adv_danger_at(struct unit *punit, struct tile *ptile)
Are there dangerous enemies at or adjacent to the tile 'ptile'?
Definition: advgoto.cpp:317
adv_want amortize(adv_want benefit, int delay)
Amortize means gradually paying off a cost or debt over time.
Definition: advtools.cpp:25
void dai_data_phase_finished(struct ai_type *ait, struct player *pplayer)
Clean up ai data after phase finished.
Definition: aidata.cpp:281
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.
Definition: aidata.cpp:304
int aiferry_find_boat(struct ai_type *ait, struct unit *punit, int cap, PFPath *path)
Proper and real PF function for finding a boat.
Definition: aiferry.cpp:490
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Should unit be considered a ferry?
Definition: aiferry.cpp:157
#define TECH_LOG(ait, _, pplayer, padvance, msg,...)
Definition: ailog.h:32
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition: aiplayer.h:47
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition: aiplayer.h:35
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition: aiplayer.h:41
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'.
Definition: aisettler.cpp:658
#define PERFECTION
Definition: aisettler.cpp:95
#define GROWTH_PRIORITY
Definition: aisettler.cpp:89
static bool dai_do_build_city(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Build a city and initialize AI infrastructure cache.
Definition: aisettler.cpp:1172
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 %.
Definition: aisettler.cpp:544
void dai_auto_settler_cont(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct settlermap *state)
Auto settler continuing its work.
Definition: aisettler.cpp:1113
static void print_cityresult(struct player *pplayer, const std::unique_ptr< cityresult > &cr)
For debugging, print the city result table.
Definition: aisettler.cpp:590
#define RESULT_IS_ENOUGH
Definition: aisettler.cpp:85
#define GROWTH_POTENTIAL_DEEMPHASIS
Definition: aisettler.cpp:100
static bool food_starvation(const std::unique_ptr< cityresult > &result)
Check if a city on this location would starve.
Definition: aisettler.cpp:517
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.
Definition: aisettler.cpp:964
#define DEFENSE_EMPHASIS
Definition: aisettler.cpp:107
void dai_auto_settler_init(struct ai_plr *ai)
Initialize ai settler engine.
Definition: aisettler.cpp:945
static int naval_bonus(const std::unique_ptr< cityresult > &result)
Add bonus for coast.
Definition: aisettler.cpp:575
#define NAVAL_EMPHASIS
Definition: aisettler.cpp:103
struct tile_data_cache * tile_data_cache_copy(const struct tile_data_cache *ptdc)
Make copy of tile data cache.
Definition: aisettler.cpp:439
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.
Definition: aisettler.cpp:494
static bool shield_starvation(const std::unique_ptr< cityresult > &result)
Check if a city on this location would lack shields.
Definition: aisettler.cpp:532
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".
Definition: aisettler.cpp:740
void dai_auto_settler_free(struct ai_plr *ai)
Deinitialize ai settler engine.
Definition: aisettler.cpp:1158
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.
Definition: aisettler.cpp:227
#define FERRY_TECH_WANT
Definition: aisettler.cpp:87
void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
Reset ai settler engine.
Definition: aisettler.cpp:1124
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.
Definition: aisettler.cpp:835
struct tile_data_cache * tile_data_cache_new()
Allocate tile data cache.
Definition: aisettler.cpp:425
static const struct tile_data_cache * tdc_plr_get(struct ai_type *ait, struct player *plr, int tindex)
Return player's tile data cache.
Definition: aisettler.cpp:461
void contemplate_new_city(struct ai_type *ait, struct city *pcity)
Return want for city settler.
Definition: aisettler.cpp:1228
void dai_unit_new_task(struct ai_type *ait, struct unit *punit, enum ai_unit_task task, struct tile *ptile)
Ensure unit sanity by telling charge that we won't bodyguard it anymore, tell bodyguard it can roam f...
Definition: aitools.cpp:648
bool dai_gothere(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile)
This is ferry-enabled goto.
Definition: aitools.cpp:258
@ AIUNIT_BUILD_CITY
Definition: aiunit.h:28
@ AIUNIT_NONE
Definition: aiunit.h:26
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.
Definition: city.cpp:1083
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
Definition: city.cpp:1095
void city_size_add(struct city *pcity, int add)
Add a (positive or negative) value to the city size.
Definition: city.cpp:1112
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.
Definition: city.cpp:1423
const char * city_name_get(const struct city *pcity)
Return the name of the city.
Definition: city.cpp:1077
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.
Definition: city.cpp:404
int city_map_radius_sq_get(const struct city *pcity)
Returns the current squared radius of the city.
Definition: city.cpp:130
void destroy_city_virtual(struct city *pcity)
Removes the virtual skeleton of a city.
Definition: city.cpp:3421
void city_choose_build_default(struct city *pcity)
Always tile_set_owner(ptile, pplayer) sometime before this!
Definition: city.cpp:1024
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.
Definition: city.cpp:3108
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.
Definition: city.cpp:1230
int city_map_tiles(int city_radius_sq)
Return the number of tiles for the given city radius.
Definition: city.cpp:164
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Create virtual skeleton for a city.
Definition: city.cpp:3346
citizens city_size_get(const struct city *pcity)
Get the city size.
Definition: city.cpp:1101
#define city_tile_iterate_index_end
Definition: city.h:177
#define city_map_iterate_end
Definition: city.h:148
#define city_map_iterate(_radius_sq, _index, _x, _y)
Definition: city.h:144
#define city_tile_iterate_index(_radius_sq, _city_tile, _tile, _index)
Definition: city.h:169
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...
Definition: citymap.cpp:158
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.
Definition: citymap.cpp:178
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...
Definition: citymap.cpp:172
const char * city_name_suggestion(struct player *pplayer, struct tile *ptile)
Come up with a default name when a new city is about to be built.
Definition: citytools.cpp:451
void clear_worker_tasks(struct city *pcity)
Clear all worker tasks from the city and inform owner.
Definition: citytools.cpp:3423
bool empty() const
ane_kind
Definition: explanation.h:15
@ ANEK_CITY_TOO_CLOSE_TGT
Definition: explanation.h:74
#define extra_type_iterate(_p)
Definition: extras.h:279
#define extra_type_iterate_end
Definition: extras.h:285
float adv_want
Definition: fc_types.h:1144
@ AUT_BUILD_CITY
Definition: fc_types.h:287
@ AUT_AUTO_SETTLER
Definition: fc_types.h:287
#define ADV_WANT_PRINTF
Definition: fc_types.h:1145
@ O_SHIELD
Definition: fc_types.h:86
@ O_FOOD
Definition: fc_types.h:85
@ O_TRADE
Definition: fc_types.h:87
@ BORDERS_DISABLED
Definition: fc_types.h:863
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...
Definition: game.cpp:112
struct civ_game game
Definition: game.cpp:47
struct world wld
Definition: game.cpp:48
struct government * government_of_player(const struct player *pplayer)
Return the government of a player.
Definition: government.cpp:107
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...
Definition: handicaps.cpp:62
@ H_MAP
Definition: handicaps.h:27
void initialize_infrastructure_cache(struct player *pplayer)
Do all tile improvement calculations and cache them for later.
Definition: infracache.cpp:245
constexpr auto LOG_DEBUG
Definition: log.h:27
#define fc_assert_ret(condition)
Definition: log.h:112
#define log_test
Definition: log.h:71
#define fc_assert(condition)
Definition: log.h:89
#define LOG_TEST
Definition: log.h:74
#define fc_assert_ret_val(condition, val)
Definition: log.h:114
#define fc_assert_action(condition, action)
Definition: log.h:104
#define log_debug(message,...)
Definition: log.h:65
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...
Definition: map.cpp:887
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition: map.h:312
#define square_iterate_end
Definition: map.h:315
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Return whether the player knows the tile.
Definition: maphand.cpp:884
bool upgrade_city_extras(struct city *pcity, struct extra_type **gained)
Check city for extra upgrade.
Definition: maphand.cpp:214
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
Definition: path_finding.h:542
#define pf_map_move_costs_iterate(ARG_pfm, NAME_tile, NAME_cost, COND_from_start)
Definition: path_finding.h:532
void pft_fill_unit_overlap_param(struct pf_parameter *parameter, const struct unit *punit)
Switch on one tile overlapping into the non-native terrain.
Definition: pf_tools.cpp:871
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct unit *punit)
Fill classic parameters for an unit.
Definition: pf_tools.cpp:822
enum tile_behavior no_fights_or_unknown(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
PF callback to prohibit going into the unknown.
Definition: pf_tools.cpp:469
std::vector< city * > player_gov_centers(const struct player *pplayer)
Locate the player's government centers.
Definition: player.cpp:1264
const char * player_name(const struct player *pplayer)
Return the leader name of the player.
Definition: player.cpp:816
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied or at peace.
Definition: player.cpp:1355
#define is_ai(plr)
Definition: player.h:227
#define MIN(x, y)
Definition: shared.h:49
#define MAX(x, y)
Definition: shared.h:48
@ AIT_SETTLERS
Definition: srv_log.h:46
@ AIT_WORKERS
Definition: srv_log.h:47
#define UNIT_LOG(_, punit, msg,...)
Definition: srv_log.h:95
@ TIMER_STOP
Definition: srv_log.h:77
@ TIMER_START
Definition: srv_log.h:77
#define CITY_LOG(_, pcity, msg,...)
Definition: srv_log.h:80
#define TIMING_LOG(timer, activity)
Definition: srv_log.h:121
int shield_priority
Definition: advdata.h:102
int food_priority
Definition: advdata.h:103
struct adv_data::@89 goal
struct adv_data::@89::@91 govt
int science_priority
Definition: advdata.h:106
bool founder_boat
Definition: daicity.h:61
int founder_want
Definition: daicity.h:63
Definition: aidata.h:63
struct ai_settler * settler
Definition: aidata.h:92
adv_want tech_want[A_LAST+1]
Definition: aidata.h:95
QHash< int, const struct tile_data_cache * > * tdc_hash
Definition: aisettler.cpp:128
Definition: ai.h:42
Definition: city.h:291
int waste[O_LAST]
Definition: city.h:325
QHash< int, const struct tile_data_cache * > tdc_hash
Definition: aisettler.cpp:167
struct cityresult::@159 best_other
struct tile_data_cache * tdc
Definition: aisettler.cpp:153
int corruption
Definition: aisettler.cpp:147
bool virt_boat
Definition: aisettler.cpp:149
bool overseas
Definition: aisettler.cpp:148
int city_radius_sq
Definition: aisettler.cpp:169
~cityresult()
Destroy a city result.
Definition: aisettler.cpp:212
struct cityresult::@158 city_center
struct tile * tile
Definition: aisettler.cpp:144
cityresult(struct tile *tile)
Allocated a city result.
Definition: aisettler.cpp:203
struct packet_game_info info
Definition: game.h:80
struct packet_scenario_info scenario
Definition: game.h:78
enum tile_behavior(* get_TB)(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
Definition: path_finding.h:380
Definition: player.h:231
struct government * government
Definition: player.h:240
Definition: tile.h:42
int index
Definition: tile.h:43
enum move_level sea_move
Definition: unittype.h:136
struct advance * require_advance
Definition: unittype.h:484
Definition: unit.h:134
int moves_left
Definition: unit.h:147
int id
Definition: unit.h:141
struct unit::@76::@79 server
struct tile * goto_tile
Definition: unit.h:152
enum unit_activity act
Definition: workertask.h:17
struct tile * ptile
Definition: workertask.h:16
struct extra_type * tgt
Definition: workertask.h:18
struct civ_map map
Definition: world_object.h:21
Tech_type_id advance_index(const struct advance *padvance)
Return the advance index.
Definition: tech.cpp:76
#define A_NEVER
Definition: tech.h:44
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.)
Definition: terrain.cpp:495
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...
Definition: tile.cpp:1051
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.
Definition: tile.cpp:799
void tile_set_owner(struct tile *ptile, struct player *pplayer, struct tile *claimer)
Set the owner of a tile (may be nullptr).
Definition: tile.cpp:58
void tile_set_worked(struct tile *ptile, struct city *pcity)
Set the city/worker on the tile (may be nullptr).
Definition: tile.cpp:96
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
Definition: tile.cpp:72
struct tile * tile_virtual_new(const struct tile *ptile)
Returns a virtual tile.
Definition: tile.cpp:997
#define tile_claimer(_tile)
Definition: tile.h:82
#define tile_index(_pt_)
Definition: tile.h:70
#define tile_worked(_tile)
Definition: tile.h:97
#define tile_terrain(_tile)
Definition: tile.h:93
#define TILE_XY(ptile)
Definition: tile.h:36
#define tile_continent(_tile)
Definition: tile.h:74
#define tile_has_extra(ptile, pextra)
Definition: tile.h:130
#define tile_owner(_tile)
Definition: tile.h:78
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Assign a new untargeted task to a unit.
Definition: unit.cpp:1011
bool unit_is_cityfounder(const struct unit *punit)
Is a cityfounder unit?
Definition: unit.cpp:2389
void unit_virtual_destroy(struct unit *punit)
Free the memory used by virtual unit.
Definition: unit.cpp:1588
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Create a virtual unit skeleton.
Definition: unit.cpp:1490
void unit_tile_set(struct unit *punit, struct tile *ptile)
Set the tile location of the unit.
Definition: unit.cpp:1200
#define unit_tile(_pu)
Definition: unit.h:371
#define CHECK_UNIT(punit)
Definition: unit.h:264
#define unit_owner(_pu)
Definition: unit.h:370
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Handle request for changing activity.
Definition: unithand.cpp:5485
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.
Definition: unithand.cpp:2718
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.
Definition: unithand.cpp:1454
void send_unit_info(struct conn_list *dest, struct unit *punit)
Send the unit to the players who need the info.
Definition: unittools.cpp:2808
struct unit_type * best_role_unit(const struct city *pcity, int role)
Return "best" unit this city can build, with given role/flag.
Definition: unittype.cpp:1920
const char * utype_rule_name(const struct unit_type *punittype)
Return the (untranslated) rule name of the unit type.
Definition: unittype.cpp:1274
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.
Definition: unittype.cpp:1950
int unit_build_shield_cost_base(const struct unit *punit)
Returns the number of shields this unit represents.
Definition: unittype.cpp:1185
struct unit_class * unit_class_get(const struct unit *punit)
Returns unit class pointer for a unit.
Definition: unittype.cpp:2151
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
Definition: unittype.cpp:176
struct unit_type * get_role_unit(int role, int role_index)
Return index-th unit with specified role/flag.
Definition: unittype.cpp:1900
int unit_pop_value(const struct unit *punit)
How much population is put to building this unit.
Definition: unittype.cpp:1239
@ MOVE_NONE
Definition: unittype.h:115