Freeciv-3.1: /fast/freeciv/nightly/S3_1/ai/default/daisettler.c Source File

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
119 struct tile_data_cache {
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
132struct tile_data_cache *tile_data_cache_new(void);
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
144 struct ai_settler {
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
157 struct cityresult {
158 struct tile *tile;
159 adv_want total; /* Total value of position */
160 adv_want result; /* Amortized and adjusted total value */
161 int corruption, waste;
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. */
169 } city_center;
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. */
176 } best_other;
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
186 enum cb_error_level { CBE_OK, CBE_RECOVERABLE, CBE_FATAL };
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/*************************************************************************/
225 static 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/*************************************************************************/
258 static void cityresult_destroy(struct cityresult *result)
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/*************************************************************************/
276 static 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(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
381 / GROWTH_POTENTIAL_DEEMPHASIS;
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);
397 } city_tile_iterate_index_end;
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) {
458 destroy_city_virtual(pcity);
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/*************************************************************************/
471 struct tile_data_cache *tile_data_cache_new(void)
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 *
485 tile_data_cache_copy(const struct tile_data_cache *ptdc)
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/*************************************************************************/
505 static void tile_data_cache_destroy(struct tile_data_cache *ptdc)
506{
507 if (ptdc != NULL) {
508 free(ptdc);
509 }
510}
511
512/*************************************************************************/
515 static 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/*************************************************************************/
550 static 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/*************************************************************************/
570 static 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/*************************************************************************/
585 static 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/*************************************************************************/
597 static 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 }
615 } extra_type_iterate_end;
616 tile_virtual_destroy(vtile);
617
618 defense_bonus += (defense_bonus * extra_bonus) / 100;
619
620 return 100 / (result->total + 1) * (100 / defense_bonus * DEFENSE_EMPHASIS);
621}
622
623/*************************************************************************/
626 static 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/*************************************************************************/
642 static 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;
664 } city_map_iterate_end;
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),
693 cr->city_center.tdc->sum, TILE_XY(cr->best_other.tile),
694 cr->best_other.cindex, cr->best_other.tdc->sum);
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/*************************************************************************/
714 struct 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
720 fc_assert_ret_val(punit, NULL);
721 fc_assert_ret_val(pplayer, NULL);
722
723 if (!city_can_be_built_here(ptile, punit, FALSE)
724 || (has_handicap(pplayer, H_MAP)
725 && !map_is_known(ptile, pplayer))) {
726 return NULL;
727 }
728
729 /* Check if another settler has taken a spot within mindist */
730 square_iterate(&(wld.map), ptile, game.info.citymindist-1, tile1) {
731 if (citymap_is_reserved(tile1)) {
732 return NULL;
733 }
734 } square_iterate_end;
735
736 if (adv_danger_at(punit, ptile)) {
737 return NULL;
738 }
739
740 if (pcity && (city_size_get(pcity) + unit_pop_value(punit)
741 > game.info.add_to_size_limit)) {
742 /* Can't exceed population limit. */
743 return NULL;
744 }
745
746 if (!pcity && citymap_is_reserved(ptile)) {
747 return NULL; /* reserved, go away */
748 }
749
750 /* If (x, y) is an existing city, consider immigration */
751 if (pcity && city_owner(pcity) == pplayer) {
752 return NULL;
753 }
754
755 cr = cityresult_fill(ait, pplayer, ptile); /* Burn CPU, burn! */
756 if (!cr) {
757 /* Failed to find a good spot */
758 return NULL;
759 }
760
761 /*** Alright: Now consider building a new city ***/
762
763 if (food_starvation(cr) || shield_starvation(cr)) {
764 cityresult_destroy(cr);
765 return NULL;
766 }
767
768 cr->total += result_defense_bonus(pplayer, cr);
769 cr->total += naval_bonus(cr);
770
771 /* Add remaining points, which is our potential */
772 cr->total += cr->remaining;
773
774 fc_assert_ret_val(cr->total >= 0, NULL); /* Does not free cr! */
775
776 return cr;
777}
778
779/*************************************************************************/
790 static struct cityresult *settler_map_iterate(struct ai_type *ait,
791 struct pf_parameter *parameter,
792 struct unit *punit,
793 int boat_cost)
794{
795 struct cityresult *cr = NULL, *best = NULL;
796 int best_turn = 0; /* Which turn we found the best fit */
797 struct player *pplayer = unit_owner(punit);
798 struct pf_map *pfm;
799 struct unit_class *pclass = unit_class_get(punit);
800 Continent_id curcont = tile_continent(unit_tile(punit));
801 int cost = unit_build_shield_cost_base(punit);
802
803 pfm = pf_map_new(parameter);
804 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
805 int turns;
806
807 if (boat_cost == 0 && pclass->adv.sea_move == MOVE_NONE
808 && tile_continent(ptile) != curcont) {
809 /* We have an accidential land bridge. Ignore it. It will in all
810 * likelihood go away next turn, or even in a few nanoseconds. */
811 continue;
812 }
813 if (BORDERS_DISABLED != game.info.borders) {
814 struct player *powner = tile_owner(ptile);
815
816 if (NULL != powner
817 && powner != pplayer
818 && pplayers_in_peace(powner, pplayer)) {
819 /* Land theft does not make for good neighbors. */
820 continue;
821 }
822 }
823
824 /* Calculate worth */
825 cr = city_desirability(ait, pplayer, punit, ptile);
826
827 /* Check if actually found something */
828 if (cr == NULL) {
829 continue;
830 }
831
832 /* This algorithm punishes long treks */
833 turns = move_cost / parameter->move_rate;
834 cr->result = amortize(cr->total, PERFECTION * turns);
835
836 /* Reduce want by settler cost. Easier than amortize, but still
837 * weeds out very small wants. ie we create a threshold here. */
838 /* We also penalise here for using a boat (either virtual or real)
839 * it's crude but what isn't?
840 * Settler gets used, boat can make multiple trips. */
841 cr->result -= cost + boat_cost / 3;
842
843 /* Find best spot */
844 if ((!best && cr->result > 0)
845 || (best && cr->result > best->result)) {
846 /* Destroy the old 'best' value. */
847 cityresult_destroy(best);
848 /* Save the new 'best' value. */
849 best = cr;
850 cr = NULL;
851 best_turn = turns;
852
853 log_debug("settler map search (search): (%d, %d) " ADV_WANT_PRINTF,
854 TILE_XY(best->tile), best->result);
855 } else {
856 /* Destroy the unused result. */
857 cityresult_destroy(cr);
858 cr = NULL;
859 }
860
861 /* Can we terminate early? We have a 'good enough' spot, and
862 * we don't block the establishment of a better city just one
863 * further step away. */
864 if (best && best->result > RESULT_IS_ENOUGH
865 && turns > parameter->move_rate /* sic -- yeah what an explanation! */
866 && best_turn < turns /*+ game.info.min_dist_bw_cities*/) {
867 break;
868 }
869 } pf_map_move_costs_iterate_end;
870
871 pf_map_destroy(pfm);
872
873 if (best) {
874 log_debug("settler map search (final): (%d, %d) " ADV_WANT_PRINTF,
875 TILE_XY(best->tile), best->result);
876 } else {
877 log_debug("settler map search (final): no result");
878 }
879
880 return best;
881}
882
883/*************************************************************************/
896 static struct cityresult *find_best_city_placement(struct ai_type *ait,
897 struct unit *punit,
898 bool look_for_boat,
899 bool use_virt_boat)
900{
901 struct pf_parameter parameter;
902 struct player *pplayer = unit_owner(punit);
903 struct unit *ferry = NULL;
904 struct cityresult *cr1 = NULL, *cr2 = NULL;
905 const struct civ_map *nmap = &(wld.map);
906
907 fc_assert_ret_val(is_ai(pplayer), NULL);
908 /* Only virtual units may use virtual boats: */
909 fc_assert_ret_val(0 == punit->id || !use_virt_boat, NULL);
910
911 /* Phase 1: Consider building cities on our continent */
912
913 pft_fill_unit_parameter(&parameter, nmap, punit);
914 parameter.omniscience = !has_handicap(pplayer, H_MAP);
915 cr1 = settler_map_iterate(ait, &parameter, punit, 0);
916
917 if (cr1 && cr1->result > RESULT_IS_ENOUGH) {
918 /* skip further searches */
919 return cr1;
920 }
921
922 /* Phase 2: Consider travelling to another continent */
923
924 if (look_for_boat) {
925 int ferry_id = aiferry_find_boat(ait, punit, 1, NULL);
926
927 ferry = game_unit_by_number(ferry_id);
928 }
929
930 if (ferry || (use_virt_boat
931 && is_terrain_class_near_tile(&(wld.map),
932 unit_tile(punit), TC_OCEAN)
933 && tile_city(unit_tile(punit)))) {
934 if (!ferry) {
935 /* No boat? Get a virtual one! */
936 struct unit_type *boattype
937 = best_role_unit_for_player(pplayer, L_FERRYBOAT);
938
939 if (boattype == NULL) {
940 /* Sea travel not possible yet. Bump tech want for ferries. */
941 boattype = get_role_unit(L_FERRYBOAT, 0);
942
943 if (NULL != boattype
944 && A_NEVER != boattype->require_advance) {
945 struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
946
947 plr_data->tech_want[advance_index(boattype->require_advance)]
948 += FERRY_TECH_WANT;
949 TECH_LOG(ait, LOG_DEBUG, pplayer, boattype->require_advance,
950 "+ %d for %s to ferry settler",
951 FERRY_TECH_WANT,
952 utype_rule_name(boattype));
953 }
954 /* return the result from the search on our current continent */
955 return cr1;
956 }
957 ferry = unit_virtual_create(pplayer, NULL, boattype, 0);
958 unit_tile_set(ferry, unit_tile(punit));
959 }
960
961 fc_assert(dai_is_ferry(ferry, ait));
962 pft_fill_unit_overlap_param(&parameter, nmap, ferry);
963 parameter.omniscience = !has_handicap(pplayer, H_MAP);
964 parameter.get_TB = no_fights_or_unknown;
965
966 /* FIXME: Maybe penalty for using an existing boat is too high?
967 * We shouldn't make the penalty for building a new boat too high though.
968 * Building a new boat is like a war against a weaker enemy --
969 * good for the economy. (c) Bush family */
970 cr2 = settler_map_iterate(ait, &parameter, punit,
971 unit_build_shield_cost_base(ferry));
972 if (cr2) {
973 cr2->overseas = TRUE;
974 cr2->virt_boat = (ferry->id == 0);
975 }
976
977 if (ferry->id == 0) {
978 unit_virtual_destroy(ferry);
979 }
980
981 /* If we use a virtual boat, we must have permission and be emigrating: */
982 /* FIXME: These assert do not free cr2! */
983 fc_assert_ret_val(!cr2 || (!cr2->virt_boat || use_virt_boat), NULL);
984 fc_assert_ret_val(!cr2 || (!cr2->virt_boat || cr2->overseas), NULL);
985 }
986
987 if (!cr1) {
988 /* No want for a new city on our current continent; return the result for
989 * traveling by boat. */
990 return cr2;
991 } else if (!cr2) {
992 /* No want for an overseas city; return the result for a city on our
993 * current continent. */
994 return cr1;
995 }
996
997 /* We want an overseas city and a city on the current continent - select the
998 * best! */
999 if (cr1->result > cr2->result) {
1000 cityresult_destroy(cr2);
1001 return cr1;
1002 } else {
1003 cityresult_destroy(cr1);
1004 return cr2;
1005 }
1006}
1007
1008/*************************************************************************/
1011 void dai_auto_settler_init(struct ai_plr *ai)
1012{
1013 fc_assert_ret(ai != NULL);
1014 fc_assert_ret(ai->settler == NULL);
1015
1016 ai->settler = fc_calloc(1, sizeof(*ai->settler));
1017 ai->settler->tdc_hash = tile_data_cache_hash_new();
1018
1019#ifdef FREECIV_DEBUG
1020 ai->settler->cache.hit = 0;
1021 ai->settler->cache.old = 0;
1022 ai->settler->cache.miss = 0;
1023 ai->settler->cache.save = 0;
1024#endif /* FREECIV_DEBUG */
1025}
1026
1027/*************************************************************************/
1030 void dai_auto_settler_run(struct ai_type *ait, const struct civ_map *nmap,
1031 struct player *pplayer,
1032 struct unit *punit, struct settlermap *state)
1033{
1034 adv_want best_impr = 0; /* Value of best terrain improvement we can do */
1035 enum unit_activity best_act;
1036 struct extra_type *best_target;
1037 struct tile *best_tile = NULL;
1038 struct pf_path *path = NULL;
1039 struct city *pcity = NULL;
1040
1041 /* Time it will take worker to complete its given task */
1042 int completion_time = 0;
1043
1044 CHECK_UNIT(punit);
1045
1046 /*** If we are on a city mission: Go where we should ***/
1047
1048BUILD_CITY:
1049
1050 if (def_ai_unit_data(punit, ait)->task == AIUNIT_BUILD_CITY) {
1051 struct tile *ptile = punit->goto_tile;
1052 int sanity = punit->id;
1053
1054 /* Check that the mission is still possible. If the tile has become
1055 * unavailable, call it off. */
1056 if (!city_can_be_built_here(ptile, punit, FALSE)) {
1057 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
1058 set_unit_activity(punit, ACTIVITY_IDLE);
1059 send_unit_info(NULL, punit);
1060
1061 return; /* Avoid recursion at all cost */
1062 } else {
1063 /* Go there */
1064 if ((!dai_gothere(ait, pplayer, punit, ptile)
1065 && NULL == game_unit_by_number(sanity))
1066 || punit->moves_left <= 0) {
1067 return;
1068 }
1069 if (same_pos(unit_tile(punit), ptile)) {
1070 enum cb_error_level elevel = dai_do_build_city(ait, pplayer, punit);
1071
1072 if (elevel != CBE_OK) {
1073 UNIT_LOG(LOG_DEBUG, punit, "could not make city on %s",
1074 tile_get_info_text(unit_tile(punit), TRUE, 0));
1075 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
1076
1077 if (elevel == CBE_RECOVERABLE) {
1078 /* Only known way to end in here is that hut turned in to a city
1079 * when settler entered tile. So this is not going to lead in any
1080 * serious recursion. */
1081 dai_auto_settler_run(ait, nmap, pplayer, punit, state);
1082 }
1083
1084 return;
1085 } else {
1086 return; /* We came, we saw, we built... */
1087 }
1088 } else {
1089 UNIT_LOG(LOG_DEBUG, punit, "could not go to target");
1090 /* dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL); */
1091 return;
1092 }
1093 }
1094 }
1095
1096 /*** Try find some work ***/
1097
1098 if (unit_has_type_flag(punit, UTYF_SETTLERS)) {
1099 struct worker_task *best_task;
1100
1101 TIMING_LOG(AIT_WORKERS, TIMER_START);
1102
1103 /* Have nearby cities requests? */
1104 pcity = settler_evaluate_city_requests(punit, &best_task, &path, state);
1105
1106 if (pcity != NULL) {
1107 if (path != NULL) {
1108 completion_time = pf_path_last_position(path)->turn;
1109 best_impr = 1;
1110 best_act = best_task->act;
1111 best_target = best_task->tgt;
1112 best_tile = best_task->ptile;
1113 } else {
1114 pcity = NULL;
1115 }
1116 }
1117
1118 if (pcity == NULL) {
1119 best_impr = settler_evaluate_improvements(nmap, punit,
1120 &best_act, &best_target,
1121 &best_tile, &path, state);
1122 if (path) {
1123 completion_time = pf_path_last_position(path)->turn;
1124 }
1125 }
1126 UNIT_LOG(LOG_DEBUG, punit, "impr want " ADV_WANT_PRINTF, best_impr);
1127 TIMING_LOG(AIT_WORKERS, TIMER_STOP);
1128 }
1129
1130 if (unit_is_cityfounder(punit)) {
1131 struct cityresult *result;
1132
1133 /* May use a boat: */
1134 TIMING_LOG(AIT_SETTLERS, TIMER_START);
1135 result = find_best_city_placement(ait, punit, TRUE, FALSE);
1136 TIMING_LOG(AIT_SETTLERS, TIMER_STOP);
1137 if (result && result->result > best_impr) {
1138 UNIT_LOG(LOG_DEBUG, punit, "city want " ADV_WANT_PRINTF, result->result);
1139 if (tile_city(result->tile)) {
1140 UNIT_LOG(LOG_DEBUG, punit, "immigrates to %s (%d, %d)",
1141 city_name_get(tile_city(result->tile)),
1142 TILE_XY(result->tile));
1143 } else {
1144 UNIT_LOG(LOG_DEBUG, punit, "makes city at (%d, %d)",
1145 TILE_XY(result->tile));
1146 if (punit->server.debug) {
1147 print_cityresult(pplayer, result);
1148 }
1149 }
1150 /* Go make a city! */
1151 adv_unit_new_task(punit, AUT_BUILD_CITY, result->tile);
1152 if (result->best_other.tile && result->best_other.tdc->sum >= 0) {
1153 /* Reserve best other tile (if there is one). It is the tile where the
1154 * first citizen of the city is working. */
1155 citymap_reserve_tile(result->best_other.tile, punit->id);
1156 }
1157 punit->goto_tile = result->tile; /* TMP */
1158
1159 cityresult_destroy(result);
1160
1161 /*** Go back to and found a city ***/
1162 pf_path_destroy(path);
1163 path = NULL;
1164 goto BUILD_CITY;
1165 } else if (best_impr > 0) {
1166 UNIT_LOG(LOG_DEBUG, punit, "improves terrain instead of founding");
1167 /* Terrain improvements follows the old model, and is recalculated
1168 * each turn. */
1169 if (result) {
1170 /* We had a city result, just worse than best impr */
1171 cityresult_destroy(result);
1172 }
1173 adv_unit_new_task(punit, AUT_AUTO_SETTLER, best_tile);
1174 } else {
1175 UNIT_LOG(LOG_DEBUG, punit, "cannot find work");
1176 fc_assert(result == NULL);
1177 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
1178 goto CLEANUP;
1179 }
1180 } else {
1181 /* We are a worker or engineer */
1182 adv_unit_new_task(punit, AUT_AUTO_SETTLER, best_tile);
1183 }
1184
1185 if (best_tile != NULL
1186 && auto_settler_setup_work(nmap, pplayer, punit, state, 0, path,
1187 best_tile, best_act, &best_target,
1188 completion_time)) {
1189 if (pcity != NULL) {
1190 clear_worker_tasks(pcity);
1191 }
1192 }
1193
1194CLEANUP:
1195
1196 if (NULL != path) {
1197 pf_path_destroy(path);
1198 }
1199}
1200
1201/*************************************************************************/
1204 void dai_auto_settler_cont(struct ai_type *ait, const struct civ_map *nmap,
1205 struct player *pplayer,
1206 struct unit *punit, struct settlermap *state)
1207{
1208 if (!adv_settler_safe_tile(nmap, pplayer, punit,
1209 unit_tile(punit))) {
1210 unit_activity_handling(punit, ACTIVITY_IDLE);
1211 }
1212}
1213
1214/*************************************************************************/
1217 void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
1218{
1219 bool caller_closes;
1220 struct ai_plr *ai = dai_plr_data_get(ait, pplayer, &caller_closes);
1221
1222 fc_assert_ret(ai != NULL);
1223 fc_assert_ret(ai->settler != NULL);
1224 fc_assert_ret(ai->settler->tdc_hash != NULL);
1225
1226#ifdef FREECIV_DEBUG
1227 log_debug("[aisettler cache for %s] save: %d, miss: %d, old: %d, hit: %d",
1228 player_name(pplayer), ai->settler->cache.save,
1229 ai->settler->cache.miss, ai->settler->cache.old,
1230 ai->settler->cache.hit);
1231
1232 ai->settler->cache.hit = 0;
1233 ai->settler->cache.old = 0;
1234 ai->settler->cache.miss = 0;
1235 ai->settler->cache.save = 0;
1236#endif /* FREECIV_DEBUG */
1237
1238 tile_data_cache_hash_clear(ai->settler->tdc_hash);
1239
1240 if (caller_closes) {
1241 dai_data_phase_finished(ait, pplayer);
1242 }
1243}
1244
1245/*************************************************************************/
1248 void dai_auto_settler_free(struct ai_plr *ai)
1249{
1250 fc_assert_ret(ai != NULL);
1251
1252 if (ai->settler) {
1253 if (ai->settler->tdc_hash) {
1254 tile_data_cache_hash_destroy(ai->settler->tdc_hash);
1255 }
1256 free(ai->settler);
1257 }
1258 ai->settler = NULL;
1259}
1260
1261/*************************************************************************/
1264 static enum cb_error_level dai_do_build_city(struct ai_type *ait,
1265 struct player *pplayer,
1266 struct unit *punit)
1267{
1268 struct tile *ptile = unit_tile(punit);
1269 struct city *pcity;
1270
1271 fc_assert_ret_val(pplayer == unit_owner(punit), FALSE);
1272 unit_activity_handling(punit, ACTIVITY_IDLE);
1273
1274 /* Free city reservations */
1275 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
1276
1277 pcity = tile_city(ptile);
1278 if (pcity) {
1279 /* This can happen for instance when there was hut at this tile
1280 * and it turned in to a city when settler entered tile. */
1281 log_debug("%s: There is already a city at (%d, %d)!",
1282 player_name(pplayer), TILE_XY(ptile));
1283 return CBE_RECOVERABLE;
1284 }
1285 unit_do_action(pplayer, punit->id, ptile->index,
1286 0, city_name_suggestion(pplayer, ptile),
1287 ACTION_FOUND_CITY);
1288 pcity = tile_city(ptile);
1289 if (!pcity && punit) {
1290 enum ane_kind reason = action_not_enabled_reason(punit,
1291 ACTION_FOUND_CITY,
1292 ptile, NULL, NULL);
1293
1294 if (reason == ANEK_CITY_TOO_CLOSE_TGT) {
1295 /* This is acceptable. A hut in the path to the tile may have created
1296 * a city that now is too close. */
1297 log_debug("%s: Failed to build city at (%d, %d)",
1298 player_name(pplayer), TILE_XY(ptile));
1299 } else {
1300 /* The request was illegal to begin with. */
1301 log_error("%s: Failed to build city at (%d, %d). Reason id: %d",
1302 player_name(pplayer), TILE_XY(ptile), reason);
1303 return CBE_FATAL;
1304 }
1305
1306 return CBE_RECOVERABLE;
1307 }
1308
1309 /* We have to rebuild at least the cache for this city. This event is
1310 * rare enough we might as well build the whole thing. Who knows what
1311 * else might be cached in the future? */
1312 fc_assert_ret_val(pplayer == city_owner(pcity), FALSE);
1313 initialize_infrastructure_cache(pplayer);
1314
1315 /* Init ai.choice. Handling ferryboats might use it. */
1316 adv_init_choice(&def_ai_city_data(pcity, ait)->choice);
1317
1318 return CBE_OK;
1319}
1320
1321/*************************************************************************/
1325 void contemplate_new_city(struct ai_type *ait, struct city *pcity)
1326{
1327 struct unit *virtualunit;
1328 struct tile *pcenter = city_tile(pcity);
1329 struct player *pplayer = city_owner(pcity);
1330 struct unit_type *unit_type;
1331
1332 if (game.scenario.prevent_new_cities) {
1333 return;
1334 }
1335
1336 unit_type = best_role_unit(pcity, action_id_get_role(ACTION_FOUND_CITY));
1337
1338 if (unit_type == NULL) {
1339 log_debug("No ACTION_FOUND_CITY role unit available");
1340 return;
1341 }
1342
1343 /* Create a localized "virtual" unit to do operations with. */
1344 virtualunit = unit_virtual_create(pplayer, pcity, unit_type, 0);
1345 unit_tile_set(virtualunit, pcenter);
1346
1347 if (is_ai(pplayer)) {
1348 struct cityresult *result;
1349 bool is_coastal = is_terrain_class_near_tile(&(wld.map), pcenter, TC_OCEAN);
1350 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1351
1352 result = find_best_city_placement(ait, virtualunit, is_coastal, is_coastal);
1353
1354 if (result) {
1355 fc_assert_ret(0 <= result->result); /* 'result' is not freed! */
1356
1357 CITY_LOG(LOG_DEBUG, pcity, "want(" ADV_WANT_PRINTF ") to establish city at"
1358 " (%d, %d) and will %s to get there", result->result,
1359 TILE_XY(result->tile),
1360 (result->virt_boat ? "build a boat" :
1361 (result->overseas ? "use a boat" : "walk")));
1362
1363 city_data->founder_want = (result->virt_boat ?
1364 -result->result : result->result);
1365 city_data->founder_boat = result->overseas;
1366
1367 cityresult_destroy(result);
1368 } else {
1369 CITY_LOG(LOG_DEBUG, pcity, "want no city");
1370 city_data->founder_want = 0;
1371 }
1372 } else {
1373 /* Always failing */
1374 fc_assert_ret(is_ai(pplayer));
1375 }
1376
1377 unit_virtual_destroy(virtualunit);
1378}
action_id_get_role
#define action_id_get_role(act_id)
Definition actions.h:708
adv_init_choice
void adv_init_choice(struct adv_choice *choice)
Definition advchoice.c:31
adv_data_get
struct adv_data * adv_data_get(struct player *pplayer, bool *caller_closes)
Definition advdata.c:593
advdata.h
adv_danger_at
bool adv_danger_at(struct unit *punit, struct tile *ptile)
Definition advgoto.c:414
advgoto.h
amortize
adv_want amortize(adv_want benefit, int delay)
Definition advtools.c:29
advtools.h
dai_plr_data_get
struct ai_plr * dai_plr_data_get(struct ai_type *ait, struct player *pplayer, bool *caller_closes)
Definition aidata.c:308
dai_data_phase_finished
void dai_data_phase_finished(struct ai_type *ait, struct player *pplayer)
Definition aidata.c:285
aidata.h
aiferry_find_boat
int aiferry_find_boat(struct ai_type *ait, struct unit *punit, int cap, struct pf_path **path)
Definition aiferry.c:497
dai_is_ferry
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Definition aiferry.c:161
aiferry.h
ailog.h
TECH_LOG
#define TECH_LOG(ait, loglevel, pplayer, padvance, msg,...)
Definition ailog.h:36
aiplayer.h
def_ai_player_data
static struct ai_plr * def_ai_player_data(const struct player *pplayer, struct ai_type *deftype)
Definition aiplayer.h:54
def_ai_city_data
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition aiplayer.h:42
def_ai_unit_data
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition aiplayer.h:48
dai_unit_new_task
void dai_unit_new_task(struct ai_type *ait, struct unit *punit, enum ai_unit_task task, struct tile *ptile)
Definition aitools.c:642
dai_gothere
bool dai_gothere(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile)
Definition aitools.c:243
aitools.h
aiunit.h
AIUNIT_BUILD_CITY
@ AIUNIT_BUILD_CITY
Definition aiunit.h:27
AIUNIT_NONE
@ AIUNIT_NONE
Definition aiunit.h:27
adv_unit_new_task
void adv_unit_new_task(struct unit *punit, enum adv_unit_task task, struct tile *ptile)
Definition autosettlers.c:1267
auto_settler_setup_work
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)
Definition autosettlers.c:1010
settler_evaluate_city_requests
struct city * settler_evaluate_city_requests(struct unit *punit, struct worker_task **best_task, struct pf_path **path, struct settlermap *state)
Definition autosettlers.c:830
settler_evaluate_improvements
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)
Definition autosettlers.c:442
adv_settler_safe_tile
bool adv_settler_safe_tile(const struct civ_map *nmap, const struct player *pplayer, struct unit *punit, struct tile *ptile)
Definition autosettlers.c:1142
autosettlers.h
city_waste
int city_waste(const struct city *pcity, Output_type_id otype, int total, int *breakdown)
Definition city.c:3110
city_name_get
const char * city_name_get(const struct city *pcity)
Definition city.c:1106
city_can_be_built_here
bool city_can_be_built_here(const struct tile *ptile, const struct unit *punit, bool hut_test)
Definition city.c:1451
city_size_add
void city_size_add(struct city *pcity, int add)
Definition city.c:1133
citylog_map_data
void citylog_map_data(enum log_level level, int radius_sq, int *map_data)
Definition city.c:405
create_city_virtual
struct city * create_city_virtual(struct player *pplayer, struct tile *ptile, const char *name)
Definition city.c:3331
city_map_radius_sq_get
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:131
destroy_city_virtual
void destroy_city_virtual(struct city *pcity)
Definition city.c:3407
city_choose_build_default
void city_choose_build_default(struct city *pcity)
Definition city.c:1056
city_tile_output
int city_tile_output(const struct city *pcity, const struct tile *ptile, bool is_celebrating, Output_type_id otype)
Definition city.c:1250
city_map_tiles
int city_map_tiles(int city_radius_sq)
Definition city.c:165
city.h
city_tile
#define city_tile(_pcity_)
Definition city.h:544
city_tile_iterate_index
#define city_tile_iterate_index(_nmap, _radius_sq, _city_tile, _tile, _index)
Definition city.h:193
city_size_get
static citizens city_size_get(const struct city *pcity)
Definition city.h:549
city_tile_iterate_index_end
#define city_tile_iterate_index_end
Definition city.h:201
city_owner
#define city_owner(_pcity_)
Definition city.h:543
city_map_iterate_end
#define city_map_iterate_end
Definition city.h:169
city_map_iterate
#define city_map_iterate(_radius_sq, _index, _x, _y)
Definition city.h:165
citymap_reserve_tile
void citymap_reserve_tile(struct tile *ptile, int id)
Definition citymap.c:167
citymap_is_reserved
bool citymap_is_reserved(struct tile *ptile)
Definition citymap.c:190
citymap_read
int citymap_read(struct tile *ptile)
Definition citymap.c:181
citymap.h
city_name_suggestion
const char * city_name_suggestion(struct player *pplayer, struct tile *ptile)
Definition citytools.c:451
clear_worker_tasks
void clear_worker_tasks(struct city *pcity)
Definition citytools.c:3488
citytools.h
daicity.h
PERFECTION
#define PERFECTION
Definition daisettler.c:105
GROWTH_PRIORITY
#define GROWTH_PRIORITY
Definition daisettler.c:99
result_defense_bonus
static adv_want result_defense_bonus(struct player *pplayer, const struct cityresult *result)
Definition daisettler.c:597
tdc_plr_set
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
cb_error_level
Definition daisettler.c:186
CBE_OK
@ CBE_OK
Definition daisettler.c:186
CBE_FATAL
@ CBE_FATAL
Definition daisettler.c:186
CBE_RECOVERABLE
@ CBE_RECOVERABLE
Definition daisettler.c:186
cityresult_new
static struct cityresult * cityresult_new(struct tile *ptile)
Definition daisettler.c:225
food_starvation
static bool food_starvation(const struct cityresult *result)
Definition daisettler.c:570
cityresult_fill
static struct cityresult * cityresult_fill(struct ai_type *ait, struct player *pplayer, struct tile *center)
Definition daisettler.c:276
RESULT_IS_ENOUGH
#define RESULT_IS_ENOUGH
Definition daisettler.c:95
GROWTH_POTENTIAL_DEEMPHASIS
#define GROWTH_POTENTIAL_DEEMPHASIS
Definition daisettler.c:110
print_cityresult
static void print_cityresult(struct player *pplayer, const struct cityresult *cr)
Definition daisettler.c:642
naval_bonus
static adv_want naval_bonus(const struct cityresult *result)
Definition daisettler.c:626
DEFENSE_EMPHASIS
#define DEFENSE_EMPHASIS
Definition daisettler.c:117
dai_auto_settler_init
void dai_auto_settler_init(struct ai_plr *ai)
Definition daisettler.c:1011
NAVAL_EMPHASIS
#define NAVAL_EMPHASIS
Definition daisettler.c:113
tile_data_cache_copy
struct tile_data_cache * tile_data_cache_copy(const struct tile_data_cache *ptdc)
Definition daisettler.c:485
dai_auto_settler_cont
void dai_auto_settler_cont(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit, struct settlermap *state)
Definition daisettler.c:1204
shield_starvation
static bool shield_starvation(const struct cityresult *result)
Definition daisettler.c:585
settler_map_iterate
static struct cityresult * settler_map_iterate(struct ai_type *ait, struct pf_parameter *parameter, struct unit *punit, int boat_cost)
Definition daisettler.c:790
dai_auto_settler_free
void dai_auto_settler_free(struct ai_plr *ai)
Definition daisettler.c:1248
FERRY_TECH_WANT
#define FERRY_TECH_WANT
Definition daisettler.c:97
dai_do_build_city
static enum cb_error_level dai_do_build_city(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daisettler.c:1264
cityresult_destroy
static void cityresult_destroy(struct cityresult *result)
Definition daisettler.c:258
dai_auto_settler_reset
void dai_auto_settler_reset(struct ai_type *ait, struct player *pplayer)
Definition daisettler.c:1217
city_desirability
struct cityresult * city_desirability(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *ptile)
Definition daisettler.c:714
tdc_plr_get
static const struct tile_data_cache * tdc_plr_get(struct ai_type *ait, struct player *plr, int tindex)
Definition daisettler.c:515
contemplate_new_city
void contemplate_new_city(struct ai_type *ait, struct city *pcity)
Definition daisettler.c:1325
find_best_city_placement
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:896
dai_auto_settler_run
void dai_auto_settler_run(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit, struct settlermap *state)
Definition daisettler.c:1030
tile_data_cache_destroy
static void tile_data_cache_destroy(struct tile_data_cache *ptdc)
Definition daisettler.c:505
tile_data_cache_new
struct tile_data_cache * tile_data_cache_new(void)
Definition daisettler.c:471
daisettler.h
punit
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
cost
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
ane_kind
Definition explanation.h:22
ANEK_CITY_TOO_CLOSE_TGT
@ ANEK_CITY_TOO_CLOSE_TGT
Definition explanation.h:83
extra_type_iterate
#define extra_type_iterate(_p)
Definition extras.h:291
extra_type_iterate_end
#define extra_type_iterate_end
Definition extras.h:297
adv_want
float adv_want
Definition fc_types.h:1206
AUT_BUILD_CITY
@ AUT_BUILD_CITY
Definition fc_types.h:340
AUT_AUTO_SETTLER
@ AUT_AUTO_SETTLER
Definition fc_types.h:340
ADV_WANT_PRINTF
#define ADV_WANT_PRINTF
Definition fc_types.h:1207
O_SHIELD
@ O_SHIELD
Definition fc_types.h:91
O_FOOD
@ O_FOOD
Definition fc_types.h:91
O_TRADE
@ O_TRADE
Definition fc_types.h:91
Continent_id
signed short Continent_id
Definition fc_types.h:342
BORDERS_DISABLED
@ BORDERS_DISABLED
Definition fc_types.h:891
game
struct civ_game game
Definition game.c:57
wld
struct world wld
Definition game.c:58
game_unit_by_number
struct unit * game_unit_by_number(int id)
Definition game.c:111
game.h
government_of_player
struct government * government_of_player(const struct player *pplayer)
Definition government.c:113
government.h
has_handicap
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
Definition handicaps.c:66
handicaps.h
H_MAP
@ H_MAP
Definition handicaps.h:28
initialize_infrastructure_cache
void initialize_infrastructure_cache(struct player *pplayer)
Definition infracache.c:250
infracache.h
log.h
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
log_test
#define log_test
Definition log.h:136
fc_assert
#define fc_assert(condition)
Definition log.h:176
LOG_TEST
#define LOG_TEST
Definition log.h:139
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_debug
#define log_debug(message,...)
Definition log.h:115
LOG_DEBUG
@ LOG_DEBUG
Definition log.h:34
log_error
#define log_error(message,...)
Definition log.h:103
same_pos
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:938
map.h
square_iterate
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:385
square_iterate_end
#define square_iterate_end
Definition map.h:388
map_is_known
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:886
upgrade_city_extras
bool upgrade_city_extras(struct city *pcity, struct extra_type **gained)
Definition maphand.c:238
maphand.h
mem.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
movement.h
packets.h
pf_path_last_position
const struct pf_position * pf_path_last_position(const struct pf_path *path)
Definition path_finding.c:3531
pf_path_destroy
void pf_path_destroy(struct pf_path *path)
Definition path_finding.c:3411
pf_map_new
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
Definition path_finding.c:3182
pf_map_destroy
void pf_map_destroy(struct pf_map *pfm)
Definition path_finding.c:3206
pf_map_move_costs_iterate_end
#define pf_map_move_costs_iterate_end
Definition path_finding.h:592
pf_map_move_costs_iterate
#define pf_map_move_costs_iterate(ARG_pfm, NAME_tile, NAME_cost, COND_from_start)
Definition path_finding.h:582
pft_fill_unit_parameter
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:840
pft_fill_unit_overlap_param
void pft_fill_unit_overlap_param(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:891
no_fights_or_unknown
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
pf_tools.h
player_name
const char * player_name(const struct player *pplayer)
Definition player.c:886
pplayers_in_peace
bool pplayers_in_peace(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1402
player.h
is_ai
#define is_ai(plr)
Definition player.h:234
MIN
#define MIN(x, y)
Definition shared.h:55
MAX
#define MAX(x, y)
Definition shared.h:54
spechash.h
srv_log.h
CITY_LOG
#define CITY_LOG(loglevel, pcity, msg,...)
Definition srv_log.h:83
UNIT_LOG
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
AIT_SETTLERS
@ AIT_SETTLERS
Definition srv_log.h:44
AIT_WORKERS
@ AIT_WORKERS
Definition srv_log.h:45
TIMER_STOP
@ TIMER_STOP
Definition srv_log.h:76
TIMER_START
@ TIMER_START
Definition srv_log.h:76
TIMING_LOG
#define TIMING_LOG(timer, activity)
Definition srv_log.h:125
adv_data
Definition advdata.h:45
adv_data::shield_priority
int shield_priority
Definition advdata.h:107
adv_data::govt
struct adv_data::@94::@96 govt
adv_data::food_priority
int food_priority
Definition advdata.h:108
adv_data::gov
struct government * gov
Definition advdata.h:125
adv_data::science_priority
int science_priority
Definition advdata.h:111
adv_data::goal
struct adv_data::@94 goal
ai_city
Definition daicity.h:34
ai_city::founder_boat
bool founder_boat
Definition daicity.h:64
ai_city::founder_want
int founder_want
Definition daicity.h:66
ai_plr
Definition aidata.h:71
ai_plr::settler
struct ai_settler * settler
Definition aidata.h:100
ai_plr::tech_want
adv_want tech_want[A_LAST+1]
Definition aidata.h:103
ai_settler
Definition daisettler.c:144
ai_settler::tdc_hash
struct tile_data_cache_hash * tdc_hash
Definition daisettler.c:145
ai_type
Definition ai.h:50
city
Definition city.h:309
city::waste
int waste[O_LAST]
Definition city.h:344
cityresult
Definition daisettler.c:157
cityresult::city_center
struct cityresult::@270 city_center
cityresult::tdc
struct tile_data_cache * tdc
Definition daisettler.c:167
cityresult::cindex
int cindex
Definition daisettler.c:173
cityresult::waste
int waste
Definition daisettler.c:161
cityresult::corruption
int corruption
Definition daisettler.c:161
cityresult::total
adv_want total
Definition daisettler.c:159
cityresult::best_other
struct cityresult::@271 best_other
cityresult::virt_boat
bool virt_boat
Definition daisettler.c:163
cityresult::result
adv_want result
Definition daisettler.c:160
cityresult::overseas
bool overseas
Definition daisettler.c:162
cityresult::city_radius_sq
int city_radius_sq
Definition daisettler.c:183
cityresult::remaining
adv_want remaining
Definition daisettler.c:178
cityresult::tile
struct tile * tile
Definition daisettler.c:158
cityresult::tdc_hash
struct tile_data_cache_hash * tdc_hash
Definition daisettler.c:181
civ_game::info
struct packet_game_info info
Definition game.h:89
civ_game::scenario
struct packet_scenario_info scenario
Definition game.h:87
civ_map
Definition map_types.h:71
extra_type
Definition extras.h:85
government
Definition government.h:52
packet_game_info::fulltradesize
int fulltradesize
Definition packets_gen.h:158
packet_game_info::borders
enum borders_mode borders
Definition packets_gen.h:133
packet_game_info::add_to_size_limit
int add_to_size_limit
Definition packets_gen.h:120
packet_game_info::init_city_radius_sq
int init_city_radius_sq
Definition packets_gen.h:184
packet_game_info::food_cost
int food_cost
Definition packets_gen.h:153
packet_game_info::turn
int turn
Definition packets_gen.h:241
packet_game_info::citymindist
int citymindist
Definition packets_gen.h:147
packet_scenario_info::prevent_new_cities
bool prevent_new_cities
Definition packets_gen.h:1611
pf_map
Definition path_finding.c:78
pf_parameter
Definition path_finding.h:357
pf_parameter::get_TB
enum tile_behavior(* get_TB)(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
Definition path_finding.h:398
pf_parameter::move_rate
int move_rate
Definition path_finding.h:370
pf_parameter::omniscience
bool omniscience
Definition path_finding.h:376
pf_path
Definition path_finding.h:344
pf_position::turn
int turn
Definition path_finding.h:329
player
Definition player.h:249
player::government
struct government * government
Definition player.h:258
settlermap
Definition autosettlers.c:71
tile_data_cache
Definition daisettler.c:119
tile_data_cache::sum
int sum
Definition daisettler.c:124
tile_data_cache::reserved
int reserved
Definition daisettler.c:126
tile_data_cache::shield
short shield
Definition daisettler.c:122
tile_data_cache::trade
short trade
Definition daisettler.c:121
tile_data_cache::food
short food
Definition daisettler.c:120
tile_data_cache::turn
int turn
Definition daisettler.c:128
tile
Definition tile.h:49
tile::index
int index
Definition tile.h:50
unit_class
Definition unittype.h:136
unit_class::sea_move
enum move_level sea_move
Definition unittype.h:150
unit_class::adv
struct unit_class::@84 adv
unit_type
Definition unittype.h:481
unit_type::require_advance
struct advance * require_advance
Definition unittype.h:500
unit
Definition unit.h:138
unit::moves_left
int moves_left
Definition unit.h:150
unit::id
int id
Definition unit.h:145
unit::debug
bool debug
Definition unit.h:231
unit::tile
struct tile * tile
Definition unit.h:140
unit::server
struct unit::@80::@83 server
unit::goto_tile
struct tile * goto_tile
Definition unit.h:155
worker_task
Definition workertask.h:21
worker_task::act
enum unit_activity act
Definition workertask.h:23
worker_task::ptile
struct tile * ptile
Definition workertask.h:22
worker_task::tgt
struct extra_type * tgt
Definition workertask.h:24
world::map
struct civ_map map
Definition world_object.h:38
support.h
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
advance_index
Tech_type_id advance_index(const struct advance *padvance)
Definition tech.c:89
A_NEVER
#define A_NEVER
Definition tech.h:51
is_terrain_class_near_tile
bool is_terrain_class_near_tile(const struct civ_map *nmap, const struct tile *ptile, enum terrain_class tclass)
Definition terrain.c:600
tile_virtual_destroy
void tile_virtual_destroy(struct tile *vtile)
Definition tile.c:1018
tile_set_owner
void tile_set_owner(struct tile *ptile, struct player *pplayer, struct tile *claimer)
Definition tile.c:69
tile_virtual_new
struct tile * tile_virtual_new(const struct tile *ptile)
Definition tile.c:966
tile_get_info_text
const char * tile_get_info_text(const struct tile *ptile, bool include_nuisances, int linebreaks)
Definition tile.c:756
tile_city
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
tile_set_worked
void tile_set_worked(struct tile *ptile, struct city *pcity)
Definition tile.c:106
tile_claimer
#define tile_claimer(_tile)
Definition tile.h:99
tile_index
#define tile_index(_pt_)
Definition tile.h:87
tile_worked
#define tile_worked(_tile)
Definition tile.h:113
tile_terrain
#define tile_terrain(_tile)
Definition tile.h:109
TILE_XY
#define TILE_XY(ptile)
Definition tile.h:42
tile_continent
#define tile_continent(_tile)
Definition tile.h:91
tile_has_extra
#define tile_has_extra(ptile, pextra)
Definition tile.h:146
tile_owner
#define tile_owner(_tile)
Definition tile.h:95
timing.h
set_unit_activity
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1104
unit_is_cityfounder
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2619
unit_virtual_create
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1613
unit_virtual_destroy
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1709
unit_tile_set
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1278
unit_tile
#define unit_tile(_pu)
Definition unit.h:388
CHECK_UNIT
#define CHECK_UNIT(punit)
Definition unit.h:268
unit_owner
#define unit_owner(_pu)
Definition unit.h:387
unit_activity_handling
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Definition unithand.c:6176
unit_do_action
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:3153
action_not_enabled_reason
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
unithand.h
send_unit_info
void send_unit_info(struct conn_list *dest, struct unit *punit)
Definition unittools.c:2797
unittools.h
best_role_unit
struct unit_type * best_role_unit(const struct city *pcity, int role)
Definition unittype.c:2319
get_role_unit
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2301
unit_build_shield_cost_base
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1536
best_role_unit_for_player
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2345
utype_rule_name
const char * utype_rule_name(const struct unit_type *punittype)
Definition unittype.c:1630
unit_class_get
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2546
unit_has_type_flag
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:184
unit_pop_value
int unit_pop_value(const struct unit *punit)
Definition unittype.c:1595
MOVE_NONE
@ MOVE_NONE
Definition unittype.h:131