Freeciv-3.1
Loading...
Searching...
No Matches
daisettler.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 2004 - The Freeciv Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdio.h>
19#include <string.h>
20
21/* utility */
22#include "mem.h"
23#include "log.h"
24#include "support.h"
25#include "timing.h"
26
27/* common */
28#include "city.h"
29#include "game.h"
30#include "government.h"
31#include "map.h"
32#include "movement.h"
33#include "packets.h"
34#include "player.h"
35
36/* common/aicore */
37#include "citymap.h"
38#include "pf_tools.h"
39
40/* server */
41#include "citytools.h"
42#include "maphand.h"
43#include "srv_log.h"
44#include "unithand.h"
45#include "unittools.h"
46
47/* server/advisors */
48#include "advdata.h"
49#include "advgoto.h"
50#include "advtools.h"
51#include "autosettlers.h"
52#include "infracache.h"
53
54/* ai */
55#include "handicaps.h"
56
57/* ai/default */
58#include "aidata.h"
59#include "aiferry.h"
60#include "ailog.h"
61#include "aiplayer.h"
62#include "aitools.h"
63#include "aiunit.h"
64#include "daicity.h"
65
66#include "daisettler.h"
67
68
69/* COMMENTS */
70/*
71 This code tries hard to do the right thing, including looking
72 into the future (wrt to government), and also doing this in a
73 modpack friendly manner. However, there are some pieces missing.
74
75 A tighter integration into the city management code would
76 give more optimal city placements, since existing cities could
77 move their workers around to give a new city better placement.
78 Occasionally you will see cities being placed sub-optimally
79 because the best city center tile is taken when another tile
80 could have been worked instead by the city that took it.
81
82 The code is not generic enough. It assumes smallpox too much,
83 and should calculate with a future city of a bigger size.
84
85 We need to stop the build code from running this code all the
86 time and instead try to complete what it has started.
87*/
88
89/* Stop looking too hard for better tiles to found a new city at when
90 * settler finds location with at least RESULT_IS_ENOUGH want
91 * points. See city_desirability() for how base want is computed
92 * before amortizing it.
93 *
94 * This is a big WAG to save a big amount of CPU. */
95#define RESULT_IS_ENOUGH 250
96
97#define FERRY_TECH_WANT 500
98
99#define GROWTH_PRIORITY 15
100
101/* Perfection gives us an idea of how long to search for optimal
102 * solutions, instead of going for quick and dirty solutions that
103 * waste valuable time. Decrease for maps where good city placements
104 * are hard to find. Lower means more perfection. */
105#define PERFECTION 3
106
107/* How much to deemphasise the potential for city growth for city
108 * placements. Decrease this value if large cities are important
109 * or planned. Increase if running strict smallpox. */
110#define GROWTH_POTENTIAL_DEEMPHASIS 8
111
112/* Percentage bonus to city locations near an ocean. */
113#define NAVAL_EMPHASIS 20
114
115/* Modifier for defense bonus that is applied to city location want.
116 * This is % of defense % to increase want by. */
117#define DEFENSE_EMPHASIS 20
118
120 short food; /* Food output of the tile */
121 short trade; /* Trade output of the tile */
122 short shield; /* Shield output of the tile */
123
124 int sum; /* Weighted sum of the tile output */
125
126 int reserved; /* Reservation for this tile; used by print_citymap() */
127
128 int turn; /* The turn the values were calculated */
129};
130
131
133struct tile_data_cache *
134 tile_data_cache_copy(const struct tile_data_cache *ptdc);
135static void tile_data_cache_destroy(struct tile_data_cache *ptdc);
136
137/* struct tdcache_hash. */
138#define SPECHASH_TAG tile_data_cache
139#define SPECHASH_INT_KEY_TYPE
140#define SPECHASH_IDATA_TYPE struct tile_data_cache *
141#define SPECHASH_IDATA_FREE tile_data_cache_destroy
142#include "spechash.h"
143
145 struct tile_data_cache_hash *tdc_hash;
146
147#ifdef FREECIV_DEBUG
148 struct {
149 int hit;
150 int old;
151 int miss;
152 int save;
153 } cache;
154#endif /* FREECIV_DEBUG */
155};
156
158 struct tile *tile;
159 adv_want total; /* Total value of position */
160 adv_want result; /* Amortized and adjusted total value */
162 bool overseas; /* Have to use boat to get there */
163 bool virt_boat; /* Virtual boat was used in search,
164 * so need to build one */
165
166 struct {
167 struct tile_data_cache *tdc; /* Values of city center; link to the data
168 * in tdc_hash. */
170
171 struct {
172 struct tile *tile; /* Best other tile */
173 int cindex; /* City-relative index for other tile */
174 struct tile_data_cache *tdc; /* Value of best other tile; link to the
175 * data in tdc_hash. */
177
178 adv_want remaining; /* Value of all other tiles */
179
180 /* Save the result for print_citymap(). */
181 struct tile_data_cache_hash *tdc_hash;
182
183 int city_radius_sq; /* Current squared radius of the city */
184};
185
187
188static const struct tile_data_cache *tdc_plr_get(struct ai_type *ait,
189 struct player *plr,
190 int tindex);
191static void tdc_plr_set(struct ai_type *ait, struct player *plr, int tindex,
192 const struct tile_data_cache *tdcache);
193
194static struct cityresult *cityresult_new(struct tile *ptile);
195static void cityresult_destroy(struct cityresult *result);
196
197static struct cityresult *cityresult_fill(struct ai_type *ait,
198 struct player *pplayer,
199 struct tile *center);
200static bool food_starvation(const struct cityresult *result);
201static bool shield_starvation(const struct cityresult *result);
202static adv_want result_defense_bonus(struct player *pplayer,
203 const struct cityresult *result);
204static adv_want naval_bonus(const struct cityresult *result);
205static void print_cityresult(struct player *pplayer,
206 const struct cityresult *cr);
207struct cityresult *city_desirability(struct ai_type *ait,
208 struct player *pplayer,
209 struct unit *punit, struct tile *ptile);
210static struct cityresult *settler_map_iterate(struct ai_type *ait,
211 struct pf_parameter *parameter,
212 struct unit *punit,
213 int boat_cost);
214static struct cityresult *find_best_city_placement(struct ai_type *ait,
215 struct unit *punit,
216 bool look_for_boat,
217 bool use_virt_boat);
218static enum cb_error_level dai_do_build_city(struct ai_type *ait,
219 struct player *pplayer,
220 struct unit *punit);
221
222/*************************************************************************/
225static struct cityresult *cityresult_new(struct tile *ptile)
226{
227 struct cityresult *result;
228
229 fc_assert_ret_val(ptile != NULL, NULL);
230
231 result = fc_calloc(1, sizeof(*result));
232 result->tile = ptile;
233 result->total = 0;
234 result->result = -666;
235 result->corruption = 0;
236 result->waste = 0;
237 result->overseas = FALSE;
238 result->virt_boat = FALSE;
239
240 /* City centre */
241 result->city_center.tdc = NULL;
242
243 /* First worked tile */
244 result->best_other.tile = NULL;
245 result->best_other.tdc = NULL;
246 result->best_other.cindex = 0;
247
248 result->remaining = 0;
249 result->tdc_hash = tile_data_cache_hash_new();
250 result->city_radius_sq = game.info.init_city_radius_sq;
251
252 return result;
253}
254
255/*************************************************************************/
259{
260 if (result != NULL) {
261 if (result->tdc_hash != NULL) {
262 tile_data_cache_hash_destroy(result->tdc_hash);
263 }
264 free(result);
265 }
266}
267
268/*************************************************************************/
276static struct cityresult *cityresult_fill(struct ai_type *ait,
277 struct player *pplayer,
278 struct tile *center)
279{
280 struct city *pcity = tile_city(center);
281 struct government *curr_govt = government_of_player(pplayer);
282 struct player *saved_owner = NULL;
283 struct tile *saved_claimer = NULL;
284 bool virtual_city = FALSE;
285 bool handicap = has_handicap(pplayer, H_MAP);
286 struct adv_data *adv = adv_data_get(pplayer, NULL);
287 struct cityresult *result;
288 const struct civ_map *nmap = &(wld.map);
289
290 fc_assert_ret_val(adv != NULL, NULL);
291 fc_assert_ret_val(center != NULL, NULL);
292
293 pplayer->government = adv->goal.govt.gov;
294
295 /* Create a city result and set default values. */
296 result = cityresult_new(center);
297
298 if (pcity == NULL) {
299 pcity = create_city_virtual(pplayer, result->tile, "Virtuaville");
300 saved_owner = tile_owner(result->tile);
301 saved_claimer = tile_claimer(result->tile);
302 tile_set_owner(result->tile, pplayer, result->tile); /* Temporarily */
303 city_choose_build_default(nmap, pcity); /* ?? */
304 virtual_city = TRUE;
305 }
306
307 result->city_radius_sq = city_map_radius_sq_get(pcity);
308
309 city_tile_iterate_index(nmap, result->city_radius_sq, result->tile, ptile,
310 cindex) {
311 int tindex = tile_index(ptile);
312 int reserved = citymap_read(ptile);
313 bool city_center = (result->tile == ptile); /* is_city_center() */
314 struct tile_data_cache *ptdc;
315
316 if (reserved < 0
317 || (handicap && !map_is_known(ptile, pplayer))
318 || tile_worked(ptile) != NULL) {
319 /* Tile is reserved or we can't see it */
320 ptdc = tile_data_cache_new();
321 ptdc->shield = 0;
322 ptdc->trade = 0;
323 ptdc->food = 0;
324 ptdc->sum = -1;
325 ptdc->reserved = reserved;
326 /* ptdc->turn was set by tile_data_cache_new(). */
327 } else {
328 const struct tile_data_cache *ptdc_hit = tdc_plr_get(ait, pplayer, tindex);
329
330 if (!ptdc_hit || city_center) {
331 /* We cannot read city center from cache */
332 ptdc = tile_data_cache_new();
333
334 /* Food */
335 ptdc->food = city_tile_output(pcity, ptile, FALSE, O_FOOD);
336 /* Shields */
337 ptdc->shield = city_tile_output(pcity, ptile, FALSE, O_SHIELD);
338 /* Trade */
339 ptdc->trade = city_tile_output(pcity, ptile, FALSE, O_TRADE);
340 /* Weighted sum */
341 ptdc->sum = ptdc->food * adv->food_priority
342 + ptdc->trade * adv->science_priority
343 + ptdc->shield * adv->shield_priority;
344 /* Balance perfection */
345 ptdc->sum *= PERFECTION / 2;
346 if (ptdc->food >= 2) {
347 ptdc->sum *= 2; /* we need this to grow */
348 }
349
350 if (!city_center && virtual_city) {
351 /* Real cities and any city center will give us possibly
352 * skewed results */
353 tdc_plr_set(ait, pplayer, tindex, tile_data_cache_copy(ptdc));
354 }
355 } else {
356 ptdc = tile_data_cache_copy(ptdc_hit);
357 }
358 }
359
360 /* Save reservation status for debugging. */
361 ptdc->reserved = reserved;
362
363 /* Avoid crowdedness, except for city center. */
364 if (ptdc->sum > 0) {
365 ptdc->sum -= MIN(reserved * GROWTH_PRIORITY, ptdc->sum - 1);
366 }
367
368 /* Calculate city center and best other than city center */
369 if (city_center) {
370 /* Set city center. */
371 result->city_center.tdc = ptdc;
372 } else if (!result->best_other.tdc) {
373 /* Set best other tile. */
374 result->best_other.tdc = ptdc;
375 result->best_other.tile = ptile;
376 result->best_other.cindex = cindex;
377 } else if (ptdc->sum > result->best_other.tdc->sum) {
378 /* First add other other to remaining, unless it's unavailable (value < 0) tile. */
379 if (result->best_other.tdc->sum > 0) {
380 result->remaining += result->best_other.tdc->sum
382 }
383 /* Then make new best other */
384 result->best_other.tdc = ptdc;
385 result->best_other.tile = ptile;
386 result->best_other.cindex = cindex;
387 } else {
388 /* Save total remaining calculation, divided by crowdedness
389 * of the area and the emphasis placed on space for growth.
390 * Do not add unavailable (value < 0) tiles. */
391 if (ptdc->sum > 0) {
392 result->remaining += ptdc->sum / GROWTH_POTENTIAL_DEEMPHASIS;
393 }
394 }
395
396 tile_data_cache_hash_replace(result->tdc_hash, cindex, ptdc);
398
399 /* We need a city center. */
400 if (result->city_center.tdc == NULL) {
401 fc_assert(result->city_center.tdc != NULL);
402
403 return NULL;
404 }
405
406 if (virtual_city) {
407 /* Baseline is a size one city (city center + best extra tile). */
408 result->total = result->city_center.tdc->sum
409 + (result->best_other.tdc != NULL
410 ? result->best_other.tdc->sum : 0);
411 } else if (result->best_other.tdc != NULL) {
412 /* Baseline is best extra tile only. This is why making new cities
413 * is so darn good. */
414 result->total = result->best_other.tdc->sum;
415 } else {
416 /* There is no available tile in this city. All is worked. */
417 result->total = 0;
418 return result;
419 }
420
421 /* Now we have a valid city center as well as best other tile. */
422
423 if (virtual_city) {
424 /* Corruption and waste of a size one city deducted. Notice that we
425 * don't do this if 'fulltradesize' is changed, since then we'd
426 * never make cities. */
427 int shield = result->city_center.tdc->shield
428 + result->best_other.tdc->shield;
429 result->waste = adv->shield_priority
430 * city_waste(pcity, O_SHIELD, shield, NULL);
431
432 if (game.info.fulltradesize == 1) {
433 int trade = result->city_center.tdc->trade
434 + result->best_other.tdc->trade;
435 result->corruption = adv->science_priority
436 * city_waste(pcity, O_TRADE, trade, NULL);
437 } else {
438 result->corruption = 0;
439 }
440 } else {
441 /* Deduct difference in corruption and waste for real cities. Note that it
442 * is possible (with notradesize) that we _gain_ value here. */
443 city_size_add(pcity, 1);
444 result->corruption = adv->science_priority
445 * (city_waste(pcity, O_TRADE, result->best_other.tdc->trade, NULL)
446 - pcity->waste[O_TRADE]);
447 result->waste = adv->shield_priority
448 * (city_waste(pcity, O_SHIELD, result->best_other.tdc->shield, NULL)
449 - pcity->waste[O_SHIELD]);
450 city_size_add(pcity, -1);
451 }
452 result->total -= result->corruption;
453 result->total -= result->waste;
454 result->total = MAX(0, result->total);
455
456 pplayer->government = curr_govt;
457 if (virtual_city) {
459 tile_set_owner(result->tile, saved_owner, saved_claimer);
460 }
461
462 fc_assert_ret_val(result->city_center.tdc->sum >= 0, NULL);
463 fc_assert_ret_val(result->remaining >= 0, NULL);
464
465 return result;
466}
467
468/*************************************************************************/
472{
473 struct tile_data_cache *ptdc_copy = fc_calloc(1, sizeof(*ptdc_copy));
474
475 /* Set the turn the tile data cache was created. */
476 ptdc_copy->turn = game.info.turn;
477
478 return ptdc_copy;
479}
480
481/*************************************************************************/
484struct tile_data_cache *
486{
487 struct tile_data_cache *ptdc_copy = tile_data_cache_new();
488
489 fc_assert_ret_val(ptdc, NULL);
490
491 ptdc_copy->shield = ptdc->shield;
492 ptdc_copy->trade = ptdc->trade;
493 ptdc_copy->food = ptdc->food;
494
495 ptdc_copy->sum = ptdc->sum;
496 ptdc_copy->reserved = ptdc->reserved;
497 ptdc_copy->turn = ptdc->turn;
498
499 return ptdc_copy;
500}
501
502/*************************************************************************/
506{
507 if (ptdc != NULL) {
508 free(ptdc);
509 }
510}
511
512/*************************************************************************/
515static const struct tile_data_cache *tdc_plr_get(struct ai_type *ait,
516 struct player *plr,
517 int tindex)
518{
519 struct ai_plr *ai = dai_plr_data_get(ait, plr, NULL);
520
521 fc_assert_ret_val(ai != NULL, NULL);
522 fc_assert_ret_val(ai->settler != NULL, NULL);
523 fc_assert_ret_val(ai->settler->tdc_hash != NULL, NULL);
524
525 struct tile_data_cache *ptdc;
526
527 tile_data_cache_hash_lookup(ai->settler->tdc_hash, tindex, &ptdc);
528
529 if (!ptdc) {
530#ifdef FREECIV_DEBUG
531 ai->settler->cache.miss++;
532#endif /* FREECIV_DEBUG */
533 return NULL;
534 } else if (ptdc->turn != game.info.turn) {
535#ifdef FREECIV_DEBUG
536 ai->settler->cache.old++;
537#endif /* FREECIV_DEBUG */
538 return NULL;
539 } else {
540#ifdef FREECIV_DEBUG
541 ai->settler->cache.hit++;
542#endif /* FREECIV_DEBUG */
543 return ptdc;
544 }
545}
546
547/*************************************************************************/
550static void tdc_plr_set(struct ai_type *ait, struct player *plr, int tindex,
551 const struct tile_data_cache *ptdc)
552{
553 struct ai_plr *ai = dai_plr_data_get(ait, plr, NULL);
554
555 fc_assert_ret(ai != NULL);
556 fc_assert_ret(ai->settler != NULL);
557 fc_assert_ret(ai->settler->tdc_hash != NULL);
558 fc_assert_ret(ptdc != NULL);
559
560#ifdef FREECIV_DEBUG
561 ai->settler->cache.save++;
562#endif /* FREECIV_DEBUG */
563
564 tile_data_cache_hash_replace(ai->settler->tdc_hash, tindex, ptdc);
565}
566
567/*************************************************************************/
570static bool food_starvation(const struct cityresult *result)
571{
572 /* Avoid starvation: We must have enough food to grow.
573 * Note: this does not handle the case of a newly founded city breaking
574 * even but being immediately able to build an improvement increasing its
575 * yield (such as supermarkets and harbours in the classic ruleset).
576 * /MSS */
577 return (result->city_center.tdc->food
578 + (result->best_other.tdc ? result->best_other.tdc->food
579 : 0) <= game.info.food_cost);
580}
581
582/*************************************************************************/
585static bool shield_starvation(const struct cityresult *result)
586{
587 /* Avoid resource starvation. */
588 return (result->city_center.tdc->shield
589 + (result->best_other.tdc ? result->best_other.tdc->shield
590 : 0) == 0);
591}
592
593/*************************************************************************/
597static adv_want result_defense_bonus(struct player *pplayer,
598 const struct cityresult *result)
599{
600 /* Defense modification (as tie breaker mostly) */
601 int defense_bonus
602 = 10 + tile_terrain(result->tile)->defense_bonus / 10;
603 int extra_bonus = 0;
604 struct tile *vtile = tile_virtual_new(result->tile);
605 struct city *vcity = create_city_virtual(pplayer, vtile, "");
606
607 tile_set_worked(vtile, vcity); /* Link tile_city(vtile) to vcity. */
608 upgrade_city_extras(vcity, NULL); /* Give city free extras. */
609 extra_type_iterate(pextra) {
610 if (tile_has_extra(vtile, pextra)) {
611 /* TODO: Do not use full bonus of those road types
612 * that are not native to all important units. */
613 extra_bonus += pextra->defense_bonus;
614 }
617
618 defense_bonus += (defense_bonus * extra_bonus) / 100;
619
620 return 100 / (result->total + 1) * (100 / defense_bonus * DEFENSE_EMPHASIS);
621}
622
623/*************************************************************************/
626static adv_want naval_bonus(const struct cityresult *result)
627{
628 bool ocean_adjacent = is_terrain_class_near_tile(&(wld.map),
629 result->tile, TC_OCEAN);
630
631 /* Adjust for ocean adjacency, which is nice */
632 if (ocean_adjacent) {
633 return (result->total * NAVAL_EMPHASIS) / 100;
634 } else {
635 return 0;
636 }
637}
638
639/*************************************************************************/
642static void print_cityresult(struct player *pplayer,
643 const struct cityresult *cr)
644{
645 int *city_map_reserved, *city_map_food, *city_map_shield, *city_map_trade;
646 int tiles = city_map_tiles(cr->city_radius_sq);
647 struct tile_data_cache *ptdc;
648
649 fc_assert_ret(cr->tdc_hash != NULL);
650 fc_assert_ret(tiles > 0);
651
652 city_map_reserved = fc_calloc(tiles, sizeof(*city_map_reserved));
653 city_map_food = fc_calloc(tiles, sizeof(*city_map_food));
654 city_map_shield = fc_calloc(tiles, sizeof(*city_map_shield));
655 city_map_trade = fc_calloc(tiles, sizeof(*city_map_trade));
656
657 city_map_iterate(cr->city_radius_sq, cindex, x, y) {
658 tile_data_cache_hash_lookup(cr->tdc_hash, cindex, &ptdc);
659 fc_assert_ret(ptdc);
660 city_map_reserved[cindex] = ptdc->reserved;
661 city_map_food[cindex] = ptdc->reserved;
662 city_map_shield[cindex] = ptdc->reserved;
663 city_map_trade[cindex] = ptdc->reserved;
665
666 /* Print reservations */
667 log_test("cityresult for (x, y, radius_sq) = (%d, %d, %d) - Reservations:",
668 TILE_XY(cr->tile), cr->city_radius_sq);
669 citylog_map_data(LOG_TEST, cr->city_radius_sq, city_map_reserved);
670
671 /* Print food */
672 log_test("cityresult for (x, y, radius_sq) = (%d, %d, %d) - Food:",
673 TILE_XY(cr->tile), cr->city_radius_sq);
674 citylog_map_data(LOG_TEST, cr->city_radius_sq, city_map_food);
675
676 /* Print shield */
677 log_test("cityresult for (x, y, radius_sq) = (%d, %d, %d) - Shield:",
678 TILE_XY(cr->tile), cr->city_radius_sq);
679 citylog_map_data(LOG_TEST, cr->city_radius_sq, city_map_shield);
680
681 /* Print trade */
682 log_test("cityresult for (x, y, radius_sq) = (%d, %d, %d) - Trade:",
683 TILE_XY(cr->tile), cr->city_radius_sq);
684 citylog_map_data(LOG_TEST, cr->city_radius_sq, city_map_trade);
685
686 free(city_map_reserved);
687 free(city_map_food);
688 free(city_map_shield);
689 free(city_map_trade);
690
691 log_test("city center (%d, %d) %d + best other (abs: %d, %d)"
692 " (cindex: %d) %d", TILE_XY(cr->tile),
695 log_test("- corr %d - waste %d + remaining " ADV_WANT_PRINTF
696 " + defense bonus " ADV_WANT_PRINTF " + naval bonus " ADV_WANT_PRINTF,
697 cr->corruption, cr->waste, cr->remaining,
698 result_defense_bonus(pplayer, cr), naval_bonus(cr));
699 log_test("= " ADV_WANT_PRINTF " (" ADV_WANT_PRINTF ")", cr->total, cr->result);
700
701 if (food_starvation(cr)) {
702 log_test(" ** FOOD STARVATION **");
703 }
704 if (shield_starvation(cr)) {
705 log_test(" ** RESOURCE STARVATION **");
706 }
707}
708
709/*************************************************************************/
714struct cityresult *city_desirability(struct ai_type *ait, struct player *pplayer,
715 struct unit *punit, struct tile *ptile)
716{
717 struct city *pcity = tile_city(ptile);
718 struct cityresult *cr = NULL;
719 const struct civ_map *nmap = &(wld.map);
720
722 fc_assert_ret_val(pplayer, NULL);
723
724 if (!city_can_be_built_here(nmap, ptile, punit, FALSE)
725 || (has_handicap(pplayer, H_MAP)
726 && !map_is_known(ptile, pplayer))) {
727 return NULL;
728 }
729
730 /* Check if another settler has taken a spot within mindist */
731 square_iterate(&(wld.map), ptile, game.info.citymindist-1, tile1) {
732 if (citymap_is_reserved(tile1)) {
733 return NULL;
734 }
736
737 if (adv_danger_at(punit, ptile)) {
738 return NULL;
739 }
740
741 if (pcity && (city_size_get(pcity) + unit_pop_value(punit)
743 /* Can't exceed population limit. */
744 return NULL;
745 }
746
747 if (!pcity && citymap_is_reserved(ptile)) {
748 return NULL; /* reserved, go away */
749 }
750
751 /* If (x, y) is an existing city, consider immigration */
752 if (pcity && city_owner(pcity) == pplayer) {
753 return NULL;
754 }
755
756 cr = cityresult_fill(ait, pplayer, ptile); /* Burn CPU, burn! */
757 if (!cr) {
758 /* Failed to find a good spot */
759 return NULL;
760 }
761
762 /*** Alright: Now consider building a new city ***/
763
764 if (food_starvation(cr) || shield_starvation(cr)) {
766 return NULL;
767 }
768
769 cr->total += result_defense_bonus(pplayer, cr);
770 cr->total += naval_bonus(cr);
771
772 /* Add remaining points, which is our potential */
773 cr->total += cr->remaining;
774
775 fc_assert_ret_val(cr->total >= 0, NULL); /* Does not free cr! */
776
777 return cr;
778}
779
780/*************************************************************************/
791static struct cityresult *settler_map_iterate(struct ai_type *ait,
792 struct pf_parameter *parameter,
793 struct unit *punit,
794 int boat_cost)
795{
796 struct cityresult *cr = NULL, *best = NULL;
797 int best_turn = 0; /* Which turn we found the best fit */
798 struct player *pplayer = unit_owner(punit);
799 struct pf_map *pfm;
800 struct unit_class *pclass = unit_class_get(punit);
803
804 pfm = pf_map_new(parameter);
805 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
806 int turns;
807
808 if (boat_cost == 0 && pclass->adv.sea_move == MOVE_NONE
809 && tile_continent(ptile) != curcont) {
810 /* We have an accidential land bridge. Ignore it. It will in all
811 * likelihood go away next turn, or even in a few nanoseconds. */
812 continue;
813 }
815 struct player *powner = tile_owner(ptile);
816
817 if (NULL != powner
818 && powner != pplayer
819 && pplayers_in_peace(powner, pplayer)) {
820 /* Land theft does not make for good neighbors. */
821 continue;
822 }
823 }
824
825 /* Calculate worth */
826 cr = city_desirability(ait, pplayer, punit, ptile);
827
828 /* Check if actually found something */
829 if (cr == NULL) {
830 continue;
831 }
832
833 /* This algorithm punishes long treks */
834 turns = move_cost / parameter->move_rate;
835 cr->result = amortize(cr->total, PERFECTION * turns);
836
837 /* Reduce want by settler cost. Easier than amortize, but still
838 * weeds out very small wants. ie we create a threshold here. */
839 /* We also penalise here for using a boat (either virtual or real)
840 * it's crude but what isn't?
841 * Settler gets used, boat can make multiple trips. */
842 cr->result -= cost + boat_cost / 3;
843
844 /* Find best spot */
845 if ((!best && cr->result > 0)
846 || (best && cr->result > best->result)) {
847 /* Destroy the old 'best' value. */
848 cityresult_destroy(best);
849 /* Save the new 'best' value. */
850 best = cr;
851 cr = NULL;
852 best_turn = turns;
853
854 log_debug("settler map search (search): (%d, %d) " ADV_WANT_PRINTF,
855 TILE_XY(best->tile), best->result);
856 } else {
857 /* Destroy the unused result. */
859 cr = NULL;
860 }
861
862 /* Can we terminate early? We have a 'good enough' spot, and
863 * we don't block the establishment of a better city just one
864 * further step away. */
865 if (best && best->result > RESULT_IS_ENOUGH
866 && turns > parameter->move_rate /* sic -- yeah what an explanation! */
867 && best_turn < turns /*+ game.info.min_dist_bw_cities*/) {
868 break;
869 }
871
872 pf_map_destroy(pfm);
873
874 if (best) {
875 log_debug("settler map search (final): (%d, %d) " ADV_WANT_PRINTF,
876 TILE_XY(best->tile), best->result);
877 } else {
878 log_debug("settler map search (final): no result");
879 }
880
881 return best;
882}
883
884/*************************************************************************/
897static struct cityresult *find_best_city_placement(struct ai_type *ait,
898 struct unit *punit,
899 bool look_for_boat,
900 bool use_virt_boat)
901{
902 struct pf_parameter parameter;
903 struct player *pplayer = unit_owner(punit);
904 struct unit *ferry = NULL;
905 struct cityresult *cr1 = NULL, *cr2 = NULL;
906 const struct civ_map *nmap = &(wld.map);
907
908 fc_assert_ret_val(is_ai(pplayer), NULL);
909 /* Only virtual units may use virtual boats: */
910 fc_assert_ret_val(0 == punit->id || !use_virt_boat, NULL);
911
912 /* Phase 1: Consider building cities on our continent */
913
914 pft_fill_unit_parameter(&parameter, nmap, punit);
915 parameter.omniscience = !has_handicap(pplayer, H_MAP);
916 cr1 = settler_map_iterate(ait, &parameter, punit, 0);
917
918 if (cr1 && cr1->result > RESULT_IS_ENOUGH) {
919 /* skip further searches */
920 return cr1;
921 }
922
923 /* Phase 2: Consider travelling to another continent */
924
925 if (look_for_boat) {
926 int ferry_id = aiferry_find_boat(ait, punit, 1, NULL);
927
928 ferry = game_unit_by_number(ferry_id);
929 }
930
931 if (ferry || (use_virt_boat
933 unit_tile(punit), TC_OCEAN)
934 && tile_city(unit_tile(punit)))) {
935 if (!ferry) {
936 /* No boat? Get a virtual one! */
937 struct unit_type *boattype
938 = best_role_unit_for_player(pplayer, L_FERRYBOAT);
939
940 if (boattype == NULL) {
941 /* Sea travel not possible yet. Bump tech want for ferries. */
942 boattype = get_role_unit(L_FERRYBOAT, 0);
943
944 if (NULL != boattype
945 && A_NEVER != boattype->require_advance) {
946 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
947
948 plr_data->tech_want[advance_index(boattype->require_advance)]
950 TECH_LOG(ait, LOG_DEBUG, pplayer, boattype->require_advance,
951 "+ %d for %s to ferry settler",
953 utype_rule_name(boattype));
954 }
955 /* return the result from the search on our current continent */
956 return cr1;
957 }
958 ferry = unit_virtual_create(pplayer, NULL, boattype, 0);
960 }
961
962 fc_assert(dai_is_ferry(ferry, ait));
963 pft_fill_unit_overlap_param(&parameter, nmap, ferry);
964 parameter.omniscience = !has_handicap(pplayer, H_MAP);
965 parameter.get_TB = no_fights_or_unknown;
966
967 /* FIXME: Maybe penalty for using an existing boat is too high?
968 * We shouldn't make the penalty for building a new boat too high though.
969 * Building a new boat is like a war against a weaker enemy --
970 * good for the economy. (c) Bush family */
971 cr2 = settler_map_iterate(ait, &parameter, punit,
973 if (cr2) {
974 cr2->overseas = TRUE;
975 cr2->virt_boat = (ferry->id == 0);
976 }
977
978 if (ferry->id == 0) {
980 }
981
982 /* If we use a virtual boat, we must have permission and be emigrating: */
983 /* FIXME: These assert do not free cr2! */
984 fc_assert_ret_val(!cr2 || (!cr2->virt_boat || use_virt_boat), NULL);
985 fc_assert_ret_val(!cr2 || (!cr2->virt_boat || cr2->overseas), NULL);
986 }
987
988 if (!cr1) {
989 /* No want for a new city on our current continent; return the result for
990 * traveling by boat. */
991 return cr2;
992 } else if (!cr2) {
993 /* No want for an overseas city; return the result for a city on our
994 * current continent. */
995 return cr1;
996 }
997
998 /* We want an overseas city and a city on the current continent - select the
999 * best! */
1000 if (cr1->result > cr2->result) {
1001 cityresult_destroy(cr2);
1002 return cr1;
1003 } else {
1004 cityresult_destroy(cr1);
1005 return cr2;
1006 }
1007}
1008
1009/*************************************************************************/
1013{
1014 fc_assert_ret(ai != NULL);
1015 fc_assert_ret(ai->settler == NULL);
1016
1017 ai->settler = fc_calloc(1, sizeof(*ai->settler));
1018 ai->settler->tdc_hash = tile_data_cache_hash_new();
1019
1020#ifdef FREECIV_DEBUG
1021 ai->settler->cache.hit = 0;
1022 ai->settler->cache.old = 0;
1023 ai->settler->cache.miss = 0;
1024 ai->settler->cache.save = 0;
1025#endif /* FREECIV_DEBUG */
1026}
1027
1028/*************************************************************************/
1031void dai_auto_settler_run(struct ai_type *ait, const struct civ_map *nmap,
1032 struct player *pplayer,
1033 struct unit *punit, struct settlermap *state)
1034{
1035 adv_want best_impr = 0; /* Value of best terrain improvement we can do */
1036 enum unit_activity best_act;
1037 struct extra_type *best_target;
1038 struct tile *best_tile = NULL;
1039 struct pf_path *path = NULL;
1040 struct city *pcity = NULL;
1041
1042 /* Time it will take worker to complete its given task */
1043 int completion_time = 0;
1044
1046
1047 /*** If we are on a city mission: Go where we should ***/
1048
1049BUILD_CITY:
1050
1051 if (def_ai_unit_data(punit, ait)->task == AIUNIT_BUILD_CITY) {
1052 struct tile *ptile = punit->goto_tile;
1053 int sanity = punit->id;
1054
1055 /* Check that the mission is still possible. If the tile has become
1056 * unavailable, call it off. */
1057 if (!city_can_be_built_here(nmap, ptile, punit, FALSE)) {
1058 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
1059 set_unit_activity(punit, ACTIVITY_IDLE);
1060 send_unit_info(NULL, punit);
1061
1062 return; /* Avoid recursion at all cost */
1063 } else {
1064 /* Go there */
1065 if ((!dai_gothere(ait, pplayer, punit, ptile)
1066 && NULL == game_unit_by_number(sanity))
1067 || punit->moves_left <= 0) {
1068 return;
1069 }
1070 if (same_pos(unit_tile(punit), ptile)) {
1071 enum cb_error_level elevel = dai_do_build_city(ait, pplayer, punit);
1072
1073 if (elevel != CBE_OK) {
1074 UNIT_LOG(LOG_DEBUG, punit, "could not make city on %s",
1076 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
1077
1078 if (elevel == CBE_RECOVERABLE) {
1079 /* Only known way to end in here is that hut turned in to a city
1080 * when settler entered tile. So this is not going to lead in any
1081 * serious recursion. */
1082 dai_auto_settler_run(ait, nmap, pplayer, punit, state);
1083 }
1084
1085 return;
1086 } else {
1087 return; /* We came, we saw, we built... */
1088 }
1089 } else {
1090 UNIT_LOG(LOG_DEBUG, punit, "could not go to target");
1091 /* dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL); */
1092 return;
1093 }
1094 }
1095 }
1096
1097 /*** Try find some work ***/
1098
1099 if (unit_has_type_flag(punit, UTYF_SETTLERS)) {
1100 struct worker_task *best_task;
1101
1103
1104 /* Have nearby cities requests? */
1105 pcity = settler_evaluate_city_requests(punit, &best_task, &path, state);
1106
1107 if (pcity != NULL) {
1108 if (path != NULL) {
1109 completion_time = pf_path_last_position(path)->turn;
1110 best_impr = 1;
1111 best_act = best_task->act;
1112 best_target = best_task->tgt;
1113 best_tile = best_task->ptile;
1114 } else {
1115 pcity = NULL;
1116 }
1117 }
1118
1119 if (pcity == NULL) {
1120 best_impr = settler_evaluate_improvements(nmap, punit,
1121 &best_act, &best_target,
1122 &best_tile, &path, state);
1123 if (path) {
1124 completion_time = pf_path_last_position(path)->turn;
1125 }
1126 }
1127 UNIT_LOG(LOG_DEBUG, punit, "impr want " ADV_WANT_PRINTF, best_impr);
1129 }
1130
1132 struct cityresult *result;
1133
1134 /* May use a boat: */
1138 if (result && result->result > best_impr) {
1139 UNIT_LOG(LOG_DEBUG, punit, "city want " ADV_WANT_PRINTF, result->result);
1140 if (tile_city(result->tile)) {
1141 UNIT_LOG(LOG_DEBUG, punit, "immigrates to %s (%d, %d)",
1143 TILE_XY(result->tile));
1144 } else {
1145 UNIT_LOG(LOG_DEBUG, punit, "makes city at (%d, %d)",
1146 TILE_XY(result->tile));
1147 if (punit->server.debug) {
1148 print_cityresult(pplayer, result);
1149 }
1150 }
1151 /* Go make a city! */
1153 if (result->best_other.tile && result->best_other.tdc->sum >= 0) {
1154 /* Reserve best other tile (if there is one). It is the tile where the
1155 * first citizen of the city is working. */
1156 citymap_reserve_tile(result->best_other.tile, punit->id);
1157 }
1158 punit->goto_tile = result->tile; /* TMP */
1159
1161
1162 /*** Go back to and found a city ***/
1163 pf_path_destroy(path);
1164 path = NULL;
1165 goto BUILD_CITY;
1166 } else if (best_impr > 0) {
1167 UNIT_LOG(LOG_DEBUG, punit, "improves terrain instead of founding");
1168 /* Terrain improvements follows the old model, and is recalculated
1169 * each turn. */
1170 if (result) {
1171 /* We had a city result, just worse than best impr */
1173 }
1175 } else {
1176 UNIT_LOG(LOG_DEBUG, punit, "cannot find work");
1177 fc_assert(result == NULL);
1178 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
1179 goto CLEANUP;
1180 }
1181 } else {
1182 /* We are a worker or engineer */
1184 }
1185
1186 if (best_tile != NULL
1187 && auto_settler_setup_work(nmap, pplayer, punit, state, 0, path,
1188 best_tile, best_act, &best_target,
1189 completion_time)) {
1190 if (pcity != NULL) {
1191 clear_worker_tasks(pcity);
1192 }
1193 }
1194
1195CLEANUP:
1196
1197 if (NULL != path) {
1198 pf_path_destroy(path);
1199 }
1200}
1201
1202/*************************************************************************/
1205void dai_auto_settler_cont(struct ai_type *ait, const struct civ_map *nmap,
1206 struct player *pplayer,
1207 struct unit *punit, struct settlermap *state)
1208{
1209 if (!adv_settler_safe_tile(nmap, pplayer, punit,
1210 unit_tile(punit))) {
1211 unit_activity_handling(punit, ACTIVITY_IDLE);
1212 }
1213}
1214
1215/*************************************************************************/
1218void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
1219{
1220 bool caller_closes;
1221 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, &caller_closes);
1222
1223 fc_assert_ret(ai != NULL);
1224 fc_assert_ret(ai->settler != NULL);
1225 fc_assert_ret(ai->settler->tdc_hash != NULL);
1226
1227#ifdef FREECIV_DEBUG
1228 log_debug("[aisettler cache for %s] save: %d, miss: %d, old: %d, hit: %d",
1229 player_name(pplayer), ai->settler->cache.save,
1230 ai->settler->cache.miss, ai->settler->cache.old,
1231 ai->settler->cache.hit);
1232
1233 ai->settler->cache.hit = 0;
1234 ai->settler->cache.old = 0;
1235 ai->settler->cache.miss = 0;
1236 ai->settler->cache.save = 0;
1237#endif /* FREECIV_DEBUG */
1238
1239 tile_data_cache_hash_clear(ai->settler->tdc_hash);
1240
1241 if (caller_closes) {
1242 dai_data_phase_finished(ait, pplayer);
1243 }
1244}
1245
1246/*************************************************************************/
1250{
1251 fc_assert_ret(ai != NULL);
1252
1253 if (ai->settler) {
1254 if (ai->settler->tdc_hash) {
1255 tile_data_cache_hash_destroy(ai->settler->tdc_hash);
1256 }
1257 free(ai->settler);
1258 }
1259 ai->settler = NULL;
1260}
1261
1262/*************************************************************************/
1266 struct player *pplayer,
1267 struct unit *punit)
1268{
1269 struct tile *ptile = unit_tile(punit);
1270 struct city *pcity;
1271
1273 unit_activity_handling(punit, ACTIVITY_IDLE);
1274
1275 /* Free city reservations */
1276 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
1277
1278 pcity = tile_city(ptile);
1279 if (pcity) {
1280 /* This can happen for instance when there was hut at this tile
1281 * and it turned in to a city when settler entered tile. */
1282 log_debug("%s: There is already a city at (%d, %d)!",
1283 player_name(pplayer), TILE_XY(ptile));
1284 return CBE_RECOVERABLE;
1285 }
1286 unit_do_action(pplayer, punit->id, ptile->index,
1287 0, city_name_suggestion(pplayer, ptile),
1288 ACTION_FOUND_CITY);
1289 pcity = tile_city(ptile);
1290 if (!pcity && punit) {
1292 ACTION_FOUND_CITY,
1293 ptile, NULL, NULL);
1294
1295 if (reason == ANEK_CITY_TOO_CLOSE_TGT) {
1296 /* This is acceptable. A hut in the path to the tile may have created
1297 * a city that now is too close. */
1298 log_debug("%s: Failed to build city at (%d, %d)",
1299 player_name(pplayer), TILE_XY(ptile));
1300 } else {
1301 /* The request was illegal to begin with. */
1302 log_error("%s: Failed to build city at (%d, %d). Reason id: %d",
1303 player_name(pplayer), TILE_XY(ptile), reason);
1304 return CBE_FATAL;
1305 }
1306
1307 return CBE_RECOVERABLE;
1308 }
1309
1310 /* We have to rebuild at least the cache for this city. This event is
1311 * rare enough we might as well build the whole thing. Who knows what
1312 * else might be cached in the future? */
1313 fc_assert_ret_val(pplayer == city_owner(pcity), FALSE);
1315
1316 /* Init ai.choice. Handling ferryboats might use it. */
1317 adv_init_choice(&def_ai_city_data(pcity, ait)->choice);
1318
1319 return CBE_OK;
1320}
1321
1322/*************************************************************************/
1326void contemplate_new_city(struct ai_type *ait, struct city *pcity)
1327{
1328 struct unit *virtualunit;
1329 struct tile *pcenter = city_tile(pcity);
1330 struct player *pplayer = city_owner(pcity);
1331 struct unit_type *unit_type;
1332
1334 return;
1335 }
1336
1337 unit_type = best_role_unit(pcity, action_id_get_role(ACTION_FOUND_CITY));
1338
1339 if (unit_type == NULL) {
1340 log_debug("No ACTION_FOUND_CITY role unit available");
1341 return;
1342 }
1343
1344 /* Create a localized "virtual" unit to do operations with. */
1345 virtualunit = unit_virtual_create(pplayer, pcity, unit_type, 0);
1346 unit_tile_set(virtualunit, pcenter);
1347
1348 if (is_ai(pplayer)) {
1349 struct cityresult *result;
1350 bool is_coastal = is_terrain_class_near_tile(&(wld.map), pcenter, TC_OCEAN);
1351 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1352
1353 result = find_best_city_placement(ait, virtualunit, is_coastal, is_coastal);
1354
1355 if (result) {
1356 fc_assert_ret(0 <= result->result); /* 'result' is not freed! */
1357
1358 CITY_LOG(LOG_DEBUG, pcity, "want(" ADV_WANT_PRINTF ") to establish city at"
1359 " (%d, %d) and will %s to get there", result->result,
1360 TILE_XY(result->tile),
1361 (result->virt_boat ? "build a boat" :
1362 (result->overseas ? "use a boat" : "walk")));
1363
1364 city_data->founder_want = (result->virt_boat ?
1365 -result->result : result->result);
1366 city_data->founder_boat = result->overseas;
1367
1368 cityresult_destroy(result);
1369 } else {
1370 CITY_LOG(LOG_DEBUG, pcity, "want no city");
1371 city_data->founder_want = 0;
1372 }
1373 } else {
1374 /* Always failing */
1375 fc_assert_ret(is_ai(pplayer));
1376 }
1377
1378 unit_virtual_destroy(virtualunit);
1379}
#define action_id_get_role(act_id)
Definition actions.h:708
void adv_init_choice(struct adv_choice *choice)
Definition advchoice.c:31
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:593
bool adv_danger_at(struct unit *punit, struct tile *ptile)
Definition advgoto.c:414
adv_want amortize(adv_want benefit, int delay)
Definition advtools.c:29
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Definition aidata.c:308
void dai_data_phase_finished(struct ai_type *ait, struct player *pplayer)
Definition aidata.c:285
int aiferry_find_boat(struct ai_type *ait, struct unit *punit, int cap, struct pf_path **path)
Definition aiferry.c:497
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Definition aiferry.c:161
#define TECH_LOG(ait, loglevel, pplayer, padvance, msg,...)
Definition ailog.h:36
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition aiplayer.h:54
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition aiplayer.h:42
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition aiplayer.h:48
void dai_unit_new_task(struct ai_type *ait, struct unit *punit, enum ai_unit_task task, struct tile *ptile)
Definition aitools.c:644
bool dai_gothere(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile)
Definition aitools.c:245
@ AIUNIT_BUILD_CITY
Definition aiunit.h:27
@ AIUNIT_NONE
Definition aiunit.h:27
void adv_unit_new_task(struct unit *punit, enum adv_unit_task task, struct tile *ptile)
bool auto_settler_setup_work(const struct civ_map *nmap, struct player *pplayer, struct unit *punit, struct settlermap *state, int recursion, struct pf_path *path, struct tile *best_tile, enum unit_activity best_act, struct extra_type **best_target, int completion_time)
struct city * settler_evaluate_city_requests(struct unit *punit, struct worker_task **best_task, struct pf_path **path, struct settlermap *state)
adv_want settler_evaluate_improvements(const struct civ_map *nmap, struct unit *punit, enum unit_activity *best_act, struct extra_type **best_target, struct tile **best_tile, struct pf_path **path, struct settlermap *state)
bool adv_settler_safe_tile(const struct civ_map *nmap, const struct player *pplayer, struct unit *punit, struct tile *ptile)
void city_choose_build_default(const struct civ_map *nmap, struct city *pcity)
Definition city.c:1065
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown)
Definition city.c:3122
const char * city_name_get(const struct city *pcity)
Definition city.c:1115
void city_size_add(struct city *pcity, int add)
Definition city.c:1142
void citylog_map_data(enum log_level level, int radius_sq, int *map_data)
Definition city.c:406
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3343
bool city_can_be_built_here(const struct civ_map *nmap, const struct tile *ptile, const struct unit *punit, bool hut_test)
Definition city.c:1460
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:132
void destroy_city_virtual(struct city *pcity)
Definition city.c:3419
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1259
int city_map_tiles(int city_radius_sq)
Definition city.c:166
#define city_tile(_pcity_)
Definition city.h:544
#define city_tile_iterate_index(_nmap, _radius_sq, _city_tile, _tile, _index)
Definition city.h:193
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
#define city_tile_iterate_index_end
Definition city.h:201
#define city_owner(_pcity_)
Definition city.h:543
#define city_map_iterate_end
Definition city.h:169
#define city_map_iterate(_radius_sq, _index, _x, _y)
Definition city.h:165
void citymap_reserve_tile(struct tile *ptile, int id)
Definition citymap.c:167
bool citymap_is_reserved(struct tile *ptile)
Definition citymap.c:190
int citymap_read(struct tile *ptile)
Definition citymap.c:181
const char * city_name_suggestion(struct player *pplayer, struct tile *ptile)
Definition citytools.c:456
void clear_worker_tasks(struct city *pcity)
Definition citytools.c:3502
#define PERFECTION
Definition daisettler.c:105
#define GROWTH_PRIORITY
Definition daisettler.c:99
static adv_want result_defense_bonus(struct player *pplayer, const struct cityresult *result)
Definition daisettler.c:597
static void tdc_plr_set(struct ai_type *ait, struct player *plr, int tindex, const struct tile_data_cache *tdcache)
Definition daisettler.c:550
cb_error_level
Definition daisettler.c:186
@ CBE_OK
Definition daisettler.c:186
@ CBE_FATAL
Definition daisettler.c:186
@ CBE_RECOVERABLE
Definition daisettler.c:186
static struct cityresult * cityresult_new(struct tile *ptile)
Definition daisettler.c:225
static bool food_starvation(const struct cityresult *result)
Definition daisettler.c:570
static struct cityresult * cityresult_fill(struct ai_type *ait, struct player *pplayer, struct tile *center)
Definition daisettler.c:276
#define RESULT_IS_ENOUGH
Definition daisettler.c:95
#define GROWTH_POTENTIAL_DEEMPHASIS
Definition daisettler.c:110
static void print_cityresult(struct player *pplayer, const struct cityresult *cr)
Definition daisettler.c:642
static adv_want naval_bonus(const struct cityresult *result)
Definition daisettler.c:626
#define DEFENSE_EMPHASIS
Definition daisettler.c:117
void dai_auto_settler_init(struct ai_plr *ai)
#define NAVAL_EMPHASIS
Definition daisettler.c:113
struct tile_data_cache * tile_data_cache_copy(const struct tile_data_cache *ptdc)
Definition daisettler.c:485
void dai_auto_settler_cont(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit, struct settlermap *state)
static bool shield_starvation(const struct cityresult *result)
Definition daisettler.c:585
static struct cityresult * settler_map_iterate(struct ai_type *ait, struct pf_parameter *parameter, struct unit *punit, int boat_cost)
Definition daisettler.c:791
void dai_auto_settler_free(struct ai_plr *ai)
#define FERRY_TECH_WANT
Definition daisettler.c:97
static enum cb_error_level dai_do_build_city(struct ai_type *ait, struct player *pplayer, struct unit *punit)
static void cityresult_destroy(struct cityresult *result)
Definition daisettler.c:258
void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
struct cityresult * city_desirability(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *ptile)
Definition daisettler.c:714
static const struct tile_data_cache * tdc_plr_get(struct ai_type *ait, struct player *plr, int tindex)
Definition daisettler.c:515
void contemplate_new_city(struct ai_type *ait, struct city *pcity)
static struct cityresult * find_best_city_placement(struct ai_type *ait, struct unit *punit, bool look_for_boat, bool use_virt_boat)
Definition daisettler.c:897
void dai_auto_settler_run(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit, struct settlermap *state)
static void tile_data_cache_destroy(struct tile_data_cache *ptdc)
Definition daisettler.c:505
struct tile_data_cache * tile_data_cache_new(void)
Definition daisettler.c:471
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:73
ane_kind
Definition explanation.h:22
@ ANEK_CITY_TOO_CLOSE_TGT
Definition explanation.h:83
#define extra_type_iterate(_p)
Definition extras.h:291
#define extra_type_iterate_end
Definition extras.h:297
float adv_want
Definition fc_types.h:1206
@ AUT_BUILD_CITY
Definition fc_types.h:340
@ AUT_AUTO_SETTLER
Definition fc_types.h:340
#define ADV_WANT_PRINTF
Definition fc_types.h:1207
@ O_SHIELD
Definition fc_types.h:91
@ O_FOOD
Definition fc_types.h:91
@ O_TRADE
Definition fc_types.h:91
signed short Continent_id
Definition fc_types.h:342
@ BORDERS_DISABLED
Definition fc_types.h:891
struct civ_game game
Definition game.c:57
struct world wld
Definition game.c:58
struct unit * game_unit_by_number(int id)
Definition game.c:111
struct government * government_of_player(const struct player *pplayer)
Definition government.c:113
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
Definition handicaps.c:66
@ H_MAP
Definition handicaps.h:28
void initialize_infrastructure_cache(struct player *pplayer)
Definition infracache.c:250
#define fc_assert_ret(condition)
Definition log.h:191
#define log_test
Definition log.h:136
#define fc_assert(condition)
Definition log.h:176
#define LOG_TEST
Definition log.h:139
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_debug(message,...)
Definition log.h:115
@ LOG_DEBUG
Definition log.h:34
#define log_error(message,...)
Definition log.h:103
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:938
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:385
#define square_iterate_end
Definition map.h:388
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:886
bool upgrade_city_extras(struct city *pcity, struct extra_type **gained)
Definition maphand.c:238
#define fc_calloc(n, esz)
Definition mem.h:38
const struct pf_position * pf_path_last_position(const struct pf_path *path)
void pf_path_destroy(struct pf_path *path)
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
void pf_map_destroy(struct pf_map *pfm)
#define pf_map_move_costs_iterate_end
#define pf_map_move_costs_iterate(ARG_pfm, NAME_tile, NAME_cost, COND_from_start)
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:840
void pft_fill_unit_overlap_param(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:891
enum tile_behavior no_fights_or_unknown(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
Definition pf_tools.c:493
const char * player_name(const struct player *pplayer)
Definition player.c:886
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1402
#define is_ai(plr)
Definition player.h:234
#define MIN(x, y)
Definition shared.h:55
#define MAX(x, y)
Definition shared.h:54
#define CITY_LOG(loglevel, pcity, msg,...)
Definition srv_log.h:83
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
@ AIT_SETTLERS
Definition srv_log.h:44
@ AIT_WORKERS
Definition srv_log.h:45
@ TIMER_STOP
Definition srv_log.h:76
@ TIMER_START
Definition srv_log.h:76
#define TIMING_LOG(timer, activity)
Definition srv_log.h:125
int shield_priority
Definition advdata.h:107
struct adv_data::@94::@96 govt
int food_priority
Definition advdata.h:108
struct government * gov
Definition advdata.h:125
int science_priority
Definition advdata.h:111
struct adv_data::@94 goal
bool founder_boat
Definition daicity.h:64
int founder_want
Definition daicity.h:66
struct ai_settler * settler
Definition aidata.h:100
adv_want tech_want[A_LAST+1]
Definition aidata.h:103
struct tile_data_cache_hash * tdc_hash
Definition daisettler.c:145
Definition ai.h:50
Definition city.h:309
int waste[O_LAST]
Definition city.h:344
struct cityresult::@270 city_center
struct tile_data_cache * tdc
Definition daisettler.c:167
adv_want total
Definition daisettler.c:159
struct cityresult::@271 best_other
bool virt_boat
Definition daisettler.c:163
adv_want result
Definition daisettler.c:160
bool overseas
Definition daisettler.c:162
int city_radius_sq
Definition daisettler.c:183
adv_want remaining
Definition daisettler.c:178
struct tile * tile
Definition daisettler.c:158
struct tile_data_cache_hash * tdc_hash
Definition daisettler.c:181
struct packet_game_info info
Definition game.h:89
struct packet_scenario_info scenario
Definition game.h:87
enum borders_mode borders
enum tile_behavior(* get_TB)(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
struct government * government
Definition player.h:258
Definition tile.h:49
int index
Definition tile.h:50
enum move_level sea_move
Definition unittype.h:150
struct unit_class::@84 adv
struct advance * require_advance
Definition unittype.h:500
Definition unit.h:138
int moves_left
Definition unit.h:150
int id
Definition unit.h:145
bool debug
Definition unit.h:231
struct tile * tile
Definition unit.h:140
struct unit::@80::@83 server
struct tile * goto_tile
Definition unit.h:155
enum unit_activity act
Definition workertask.h:23
struct tile * ptile
Definition workertask.h:22
struct extra_type * tgt
Definition workertask.h:24
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
Tech_type_id advance_index(const struct advance *padvance)
Definition tech.c:89
#define A_NEVER
Definition tech.h:51
bool is_terrain_class_near_tile(const struct civ_map *nmap, const struct tile *ptile, enum terrain_class tclass)
Definition terrain.c:600
void tile_virtual_destroy(struct tile *vtile)
Definition tile.c:1018
void tile_set_owner(struct tile *ptile, struct player *pplayer, struct tile *claimer)
Definition tile.c:69
struct tile * tile_virtual_new(const struct tile *ptile)
Definition tile.c:966
const char * tile_get_info_text(const struct tile *ptile, bool include_nuisances, int linebreaks)
Definition tile.c:756
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
void tile_set_worked(struct tile *ptile, struct city *pcity)
Definition tile.c:106
#define tile_claimer(_tile)
Definition tile.h:99
#define tile_index(_pt_)
Definition tile.h:87
#define tile_worked(_tile)
Definition tile.h:113
#define tile_terrain(_tile)
Definition tile.h:109
#define TILE_XY(ptile)
Definition tile.h:42
#define tile_continent(_tile)
Definition tile.h:91
#define tile_has_extra(ptile, pextra)
Definition tile.h:146
#define tile_owner(_tile)
Definition tile.h:95
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1107
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2623
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1617
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1713
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1281
#define unit_tile(_pu)
Definition unit.h:395
#define CHECK_UNIT(punit)
Definition unit.h:268
#define unit_owner(_pu)
Definition unit.h:394
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Definition unithand.c:6179
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)
Definition unithand.c:3155
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)
Definition unithand.c:1770
void send_unit_info(struct conn_list *dest, struct unit *punit)
Definition unittools.c:2794
struct unit_type * best_role_unit(const struct city *pcity, int role)
Definition unittype.c:2319
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2301
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1536
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2346
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2547
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:184
int unit_pop_value(const struct unit *punit)
Definition unittype.c:1595
@ MOVE_NONE
Definition unittype.h:131