Freeciv-3.1
Loading...
Searching...
No Matches
autosettlers.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
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 <math.h>
19#include <stdio.h>
20#include <string.h>
21
22/* utility */
23#include "log.h"
24#include "mem.h"
25#include "support.h"
26#include "timing.h"
27
28/* common */
29#include "ai.h"
30#include "city.h"
31#include "game.h"
32#include "government.h"
33#include "map.h"
34#include "movement.h"
35#include "packets.h"
36#include "unitlist.h"
37
38/* common/aicore */
39#include "citymap.h"
40#include "path_finding.h"
41#include "pf_tools.h"
42
43/* server */
44#include "citytools.h"
45#include "maphand.h"
46#include "plrhand.h"
47#include "srv_log.h"
48#include "unithand.h"
49#include "unittools.h"
50
51/* server/advisors */
52#include "advbuilding.h"
53#include "advdata.h"
54#include "advgoto.h"
55#include "advtools.h"
56#include "infracache.h"
57
58/* ai */
59#include "handicaps.h"
60
61#include "autosettlers.h"
62
63/* This factor is multiplied on when calculating the want. This is done
64 * to avoid rounding errors in comparisons when looking for the best
65 * possible work. However before returning the final want we have to
66 * divide by it again. This loses accuracy but is needed since the want
67 * values are used for comparison by the AI in trying to calculate the
68 * goodness of building worker units. */
69#define WORKER_FACTOR 1024
70
71struct settlermap {
72 int enroute; /* unit ID of settler en route to this tile */
73 int eta; /* estimated number of turns until enroute arrives */
74};
75
79
80static struct timer *as_timer = NULL;
81
82/**********************************************************************/
86{
88 as_timer = NULL;
89}
90
91/**********************************************************************/
95{
96 int i;
97
98 i = 0;
100 ACTRES_CULTIVATE);
102 ACTRES_PLANT);
104 ACTRES_TRANSFORM_TERRAIN);
106
107 i = 0;
109 ACTRES_IRRIGATE);
111 ACTRES_MINE);
113 ACTRES_ROAD);
115 ACTRES_BASE);
117
118 i = 0;
120 ACTRES_CLEAN_POLLUTION);
122 ACTRES_CLEAN_FALLOUT);
123 /* We could have ACTRES_PILLAGE here, but currently we don't */
125}
126
127/**********************************************************************/
134 struct tile *ptile, struct road_type *proad)
135{
136#define MAX_DEP_ROADS 5
137
138 int bonus = 0, i;
139 bool potential_road[12], real_road[12], is_slow[12];
140 int dx[12] = {-1, 0, 1, -1, 1, -1, 0, 1, 0, -2, 2, 0};
141 int dy[12] = {-1, -1, -1, 0, 0, 1, 1, 1, -2, 0, 0, 2};
142 int x, y;
143 int rnbr;
144 struct road_type *pdep_roads[MAX_DEP_ROADS];
145 int dep_rnbr[MAX_DEP_ROADS];
146 int dep_count = 0;
147 struct extra_type *pextra;
148
149 if (proad == NULL) {
150 return 0;
151 }
152
153 rnbr = road_number(proad);
154 pextra = road_extra_get(proad);
155
156 road_deps_iterate(&(pextra->reqs), pdep) {
157 if (dep_count < MAX_DEP_ROADS) {
158 pdep_roads[dep_count] = pdep;
159 dep_rnbr[dep_count++] = road_number(pdep);
160 }
162
163 index_to_map_pos(&x, &y, tile_index(ptile));
164 for (i = 0; i < 12; i++) {
165 struct tile *tile1 = map_pos_to_tile(nmap, x + dx[i], y + dy[i]);
166
167 if (!tile1) {
168 real_road[i] = FALSE;
169 potential_road[i] = FALSE;
170 is_slow[i] = FALSE; /* FIXME: should be TRUE? */
171 } else {
172 int build_time = terrain_extra_build_time(tile_terrain(tile1), ACTIVITY_GEN_ROAD, pextra);
173 int j;
174
175 real_road[i] = tile_has_road(tile1, proad);
176 potential_road[i] = real_road[i];
177 for (j = 0 ; !potential_road[i] && j < dep_count ; j++) {
178 potential_road[i] = tile_has_road(tile1, pdep_roads[j]);
179 }
180
181 /* If TRUE, this value indicates that this tile does not need
182 * a road connector. This is set for terrains which cannot have
183 * road or where road takes "too long" to build. */
184 is_slow[i] = (build_time == 0 || build_time > 5);
185
186 if (!real_road[i]) {
187 unit_list_iterate(tile1->units, punit) {
188 if (punit->activity == ACTIVITY_GEN_ROAD) {
189 /* If a road, or its dependency is being built here, consider as if it's already
190 * built. */
191 int build_rnbr;
192
194
196
197 if (build_rnbr == rnbr) {
198 real_road[i] = TRUE;
199 potential_road[i] = TRUE;
200 }
201 for (j = 0 ; !potential_road[i] && j < dep_count ; j++) {
202 if (build_rnbr == dep_rnbr[j]) {
203 potential_road[i] = TRUE;
204 }
205 }
206 }
208 }
209 }
210 }
211
212 if (current_topo_has_flag(TF_HEX)) {
213 /* On hex map, road is always a benefit */
214 bonus += 20; /* Later divided by 20 */
215
216 /* Road is more valuable when even longer road around does not exist. */
217 for (i = 0; i < 12; i++) {
218 if (!real_road[i]) {
219 bonus += 3;
220 }
221 }
222
223 /* Scale down the bonus. */
224 bonus /= 20;
225 } else {
226 /*
227 * Consider the following tile arrangement (numbered in hex):
228 *
229 * 8
230 * 012
231 * 93 4A
232 * 567
233 * B
234 *
235 * these are the tiles defined by the (dx,dy) arrays above.
236 *
237 * Then the following algorithm is supposed to determine if it's a good
238 * idea to build a road here. Note this won't work well for hex maps
239 * since the (dx,dy) arrays will not cover the same tiles.
240 *
241 * FIXME: if you can understand the algorithm below please rewrite this
242 * explanation!
243 */
244 if (potential_road[0]
245 && !real_road[1] && !real_road[3]
246 && (!real_road[2] || !real_road[8])
247 && (!is_slow[2] || !is_slow[4] || !is_slow[7]
248 || !is_slow[6] || !is_slow[5])) {
249 bonus++;
250 }
251 if (potential_road[2]
252 && !real_road[1] && !real_road[4]
253 && (!real_road[7] || !real_road[10])
254 && (!is_slow[0] || !is_slow[3] || !is_slow[7]
255 || !is_slow[6] || !is_slow[5])) {
256 bonus++;
257 }
258 if (potential_road[5]
259 && !real_road[6] && !real_road[3]
260 && (!real_road[5] || !real_road[11])
261 && (!is_slow[2] || !is_slow[4] || !is_slow[7]
262 || !is_slow[1] || !is_slow[0])) {
263 bonus++;
264 }
265 if (potential_road[7]
266 && !real_road[6] && !real_road[4]
267 && (!real_road[0] || !real_road[9])
268 && (!is_slow[2] || !is_slow[3] || !is_slow[0]
269 || !is_slow[1] || !is_slow[5])) {
270 bonus++;
271 }
272
273 /* A
274 * B*B
275 * CCC
276 *
277 * We are at tile *. If tile A has a road, and neither B tile does, and
278 * one C tile is a valid destination, then we might want a road here.
279 *
280 * Of course the same logic applies if you rotate the diagram.
281 */
282 if (potential_road[1] && !real_road[4] && !real_road[3]
283 && (!is_slow[5] || !is_slow[6] || !is_slow[7])) {
284 bonus++;
285 }
286 if (potential_road[3] && !real_road[1] && !real_road[6]
287 && (!is_slow[2] || !is_slow[4] || !is_slow[7])) {
288 bonus++;
289 }
290 if (potential_road[4] && !real_road[1] && !real_road[6]
291 && (!is_slow[0] || !is_slow[3] || !is_slow[5])) {
292 bonus++;
293 }
294 if (potential_road[6] && !real_road[4] && !real_road[3]
295 && (!is_slow[0] || !is_slow[1] || !is_slow[2])) {
296 bonus++;
297 }
298 }
299
300 return bonus;
301}
302
303/**********************************************************************/
309static void consider_settler_action(const struct player *pplayer,
310 enum unit_activity act,
311 struct extra_type *target,
312 adv_want extra,
313 adv_want new_tile_value,
314 adv_want old_tile_value,
315 bool in_use, int delay,
317 adv_want *best_old_tile_value,
318 int *best_extra,
319 bool *improve_worked,
320 int *best_delay,
321 enum unit_activity *best_act,
322 struct extra_type **best_target,
323 struct tile **best_tile,
324 struct tile *ptile)
325{
326 bool improves;
327 adv_want total_value = 0;
328 adv_want base_value = 0;
329 int old_improvement_value;
330
331 fc_assert(act != ACTIVITY_LAST);
332
333 if (extra < 0) {
334 extra = 0;
335 }
336
337 if (new_tile_value > old_tile_value) {
338 improves = TRUE;
339 } else if (ADV_WANTS_EQ(new_tile_value, old_tile_value) && extra > 0) {
340 improves = TRUE;
341 } else {
342 improves = FALSE;
343 }
344
345 /* find the present value of the future benefit of this action */
346 if (improves || extra > 0) {
347 if (!(*improve_worked) && !in_use) {
348 /* Going to improve tile that is not yet in use.
349 * Getting the best possible total for next citizen to work on is more
350 * important than amount tile gets improved. */
351 if (improves && (new_tile_value > *best_value
352 || (ADV_WANTS_EQ(new_tile_value, *best_value)
353 && old_tile_value < *best_old_tile_value))) {
354 *best_value = new_tile_value;
355 *best_old_tile_value = old_tile_value;
356 *best_extra = extra;
357 *best_act = act;
358 *best_target = target;
359 *best_tile = ptile;
360 *best_delay = delay;
361 }
362
363 return;
364 }
365
366 /* At least one of the previous best or current tile is in use
367 * Prefer the tile that gets improved more, regardless of
368 * the resulting total */
369
370 base_value = new_tile_value - old_tile_value;
371 total_value = base_value * WORKER_FACTOR;
372 if (!in_use) {
373 total_value /= 2;
374 }
375 total_value += extra * WORKER_FACTOR;
376
377 /* Use factor to prevent rounding errors */
378 total_value = amortize(total_value, delay);
379
380 if (*improve_worked) {
381 old_improvement_value = *best_value;
382 } else {
383 /* Convert old best_value to improvement value compatible with in_use
384 * tile value */
385 old_improvement_value = amortize((*best_value - *best_old_tile_value) * WORKER_FACTOR / 2,
386 *best_delay);
387 }
388
389 if (total_value > old_improvement_value
390 || (ADV_WANTS_EQ(total_value, old_improvement_value)
391 && old_tile_value > *best_old_tile_value)) {
392 if (in_use) {
393 *best_value = total_value;
394 *improve_worked = TRUE;
395 } else {
396 *best_value = new_tile_value;
397 *improve_worked = FALSE;
398 }
399 *best_old_tile_value = old_tile_value;
400 *best_extra = extra;
401 *best_act = act;
402 *best_target = target;
403 *best_tile = ptile;
404 *best_delay = delay;
405 }
406 }
407}
408
409/**********************************************************************/
412static enum tile_behavior
413autosettler_tile_behavior(const struct tile *ptile,
414 enum known_type known,
415 const struct pf_parameter *param)
416{
417 const struct player *owner = tile_owner(ptile);
418
419 if (NULL != owner && !pplayers_allied(owner, param->owner)) {
420 return TB_IGNORE;
421 }
422 return TB_NORMAL;
423}
424
425/**********************************************************************/
443 struct unit *punit,
444 enum unit_activity *best_act,
445 struct extra_type **best_target,
446 struct tile **best_tile,
447 struct pf_path **path,
448 struct settlermap *state)
449{
450 const struct player *pplayer = unit_owner(punit);
451 struct pf_parameter parameter;
452 struct pf_map *pfm;
453 struct pf_position pos;
454 adv_want oldv; /* Current value of consideration tile. */
455 adv_want best_oldv = 9999; /* oldv of best target so far; compared if
456 * newv == best_newv; not initialized to zero,
457 * so that newv = 0 activities are not chosen. */
458 adv_want best_newv = 0;
459 bool improve_worked = FALSE;
460 int best_extra = 0;
461 int best_delay = 0;
462
463 /* Closest worker, if any, headed towards target tile */
464 struct unit *enroute = NULL;
465
466 pft_fill_unit_parameter(&parameter, nmap, punit);
467 parameter.omniscience = !has_handicap(pplayer, H_MAP);
469 pfm = pf_map_new(&parameter);
470
471 city_list_iterate(pplayer->cities, pcity) {
472 struct tile *pcenter = city_tile(pcity);
473
474 /* Try to work near the city */
475 city_tile_iterate_index(nmap, city_map_radius_sq_get(pcity), pcenter, ptile,
476 cindex) {
477 bool consider = TRUE;
478 bool in_use = (tile_worked(ptile) == pcity);
479
480 if (!in_use && !city_can_work_tile(pcity, ptile)) {
481 /* Don't risk bothering with this tile. */
482 continue;
483 }
484
485 if (!adv_settler_safe_tile(nmap, pplayer, punit, ptile)) {
486 /* Too dangerous place */
487 continue;
488 }
489
490 /* Do not go to tiles that already have workers there. */
491 unit_list_iterate(ptile->units, aunit) {
492 if (unit_owner(aunit) == pplayer
493 && aunit->id != punit->id
494 && unit_has_type_flag(aunit, UTYF_SETTLERS)) {
495 consider = FALSE;
496 }
498
499 if (!consider) {
500 continue;
501 }
502
503 if (state) {
504 enroute = player_unit_by_number(pplayer,
505 state[tile_index(ptile)].enroute);
506 }
507
508 if (pf_map_position(pfm, ptile, &pos)) {
509 int eta = FC_INFINITY, inbound_distance = FC_INFINITY, turns;
510
511 if (enroute) {
512 eta = state[tile_index(ptile)].eta;
513 inbound_distance = real_map_distance(ptile, unit_tile(enroute));
514 }
515
516 /* Only consider this tile if we are closer in time and space to
517 * it than our other worker (if any) travelling to the site. */
518 if ((enroute && enroute->id == punit->id)
519 || pos.turn < eta
520 || (pos.turn == eta
522 < inbound_distance))) {
523
524 if (enroute) {
526 "Considering (%d, %d) because we're closer "
527 "(%d, %d) than %d (%d, %d)",
528 TILE_XY(ptile), pos.turn,
530 enroute->id, eta, inbound_distance);
531 }
532
533 oldv = city_tile_value(pcity, ptile, 0, 0);
534
535 /* Now, consider various activities... */
537 struct extra_type *target = NULL;
538 enum extra_cause cause =
540 enum extra_rmcause rmcause =
542
543 if (cause != EC_NONE) {
544 target = next_extra_for_tile(ptile, cause, pplayer,
545 punit);
546 } else if (rmcause != ERM_NONE) {
547 target = prev_extra_in_tile(ptile, rmcause, pplayer,
548 punit);
549 }
550
551 if (adv_city_worker_act_get(pcity, cindex,
552 action_id_get_activity(act)) >= 0
556 ptile,
557 parameter.omniscience,
558 ptile, target))) {
559 adv_want base_value
560 = adv_city_worker_act_get(pcity, cindex,
562
563 turns = pos.turn
566 ptile, target);
567 if (pos.moves_left == 0) {
568 /* We need moves left to begin activity immediately. */
569 turns++;
570 }
571
574 target, 0.0, base_value,
575 oldv, in_use, turns,
576 &best_newv, &best_oldv, &best_extra,
577 &improve_worked,
578 &best_delay, best_act, best_target,
579 best_tile, ptile);
580
581 } /* endif: can the worker perform this action */
583
584 extra_type_iterate(pextra) {
585 enum unit_activity act = ACTIVITY_LAST;
586 enum unit_activity eval_act = ACTIVITY_LAST;
587 adv_want base_value;
588 bool removing = tile_has_extra(ptile, pextra);
589
590 if (removing) {
592 struct action *taction = action_by_number(try_act);
593
594 if (is_extra_removed_by_action(pextra, taction)) {
595 /* We do not even evaluate actions we can't do.
596 * Removal is not considered prerequisite for anything */
598 action_speculate_unit_on_tile(nmap, try_act,
599 punit,
601 ptile,
602 parameter.omniscience,
603 ptile, pextra))) {
604 act = actres_get_activity(taction->result);
605 eval_act = actres_get_activity(taction->result);
606 break;
607 }
608 }
610 } else {
611 as_extra_action_iterate(try_act) {
612 struct action *taction = action_by_number(try_act);
613
614 if (is_extra_caused_by_action(pextra, taction)) {
615 eval_act = action_id_get_activity(try_act);
617 action_speculate_unit_on_tile(nmap, try_act,
618 punit,
620 ptile,
621 parameter.omniscience,
622 ptile, pextra))) {
623 act = actres_get_activity(taction->result);
624 break;
625 }
626 }
628 }
629
630 if (eval_act == ACTIVITY_LAST) {
631 /* No activity can provide (or remove) the extra */
632 continue;
633 }
634
635 if (removing) {
636 base_value = adv_city_worker_rmextra_get(pcity, cindex, pextra);
637 } else {
638 base_value = adv_city_worker_extra_get(pcity, cindex, pextra);
639 }
640
641 if (base_value >= 0) {
642 adv_want extra;
643 struct road_type *proad;
644
645 turns = pos.turn + get_turns_for_activity_at(punit, eval_act,
646 ptile, pextra);
647 if (pos.moves_left == 0) {
648 /* We need moves left to begin activity immediately. */
649 turns++;
650 }
651
652 proad = extra_road_get(pextra);
653
654 if (proad != NULL && road_provides_move_bonus(proad)) {
655 int mc_multiplier = 1;
656 int mc_divisor = 1;
657 int old_move_cost = tile_terrain(ptile)->movement_cost * SINGLE_MOVE;
658
659 /* Here 'old' means actually 'without the evaluated': In case of
660 * removal activity it's the value after the removal. */
661
662 extra_type_by_cause_iterate(EC_ROAD, pold) {
663 if (tile_has_extra(ptile, pold) && pold != pextra) {
664 struct road_type *po_road = extra_road_get(pold);
665
666 /* This ignores the fact that new road may be native to units that
667 * old road is not. */
668 if (po_road->move_cost < old_move_cost) {
669 old_move_cost = po_road->move_cost;
670 }
671 }
673
674 if (proad->move_cost < old_move_cost) {
675 if (proad->move_cost >= terrain_control.move_fragments) {
676 mc_divisor = proad->move_cost / terrain_control.move_fragments;
677 } else {
678 if (proad->move_cost == 0) {
679 mc_multiplier = 2;
680 } else {
681 mc_multiplier = 1 - proad->move_cost;
682 }
683 mc_multiplier += old_move_cost;
684 }
685 }
686
687 extra = adv_settlers_road_bonus(nmap, ptile, proad)
688 * mc_multiplier / mc_divisor;
689
690 } else {
691 extra = 0;
692 }
693
694 if (extra_has_flag(pextra, EF_GLOBAL_WARMING)) {
695 extra -= pplayer->ai_common.warmth;
696 }
697 if (extra_has_flag(pextra, EF_NUCLEAR_WINTER)) {
698 extra -= pplayer->ai_common.frost;
699 }
700
701 if (removing) {
702 extra = -extra;
703 }
704
705 if (act != ACTIVITY_LAST) {
706 consider_settler_action(pplayer, act, pextra, extra, base_value,
707 oldv, in_use, turns,
708 &best_newv, &best_oldv, &best_extra,
709 &improve_worked,
710 &best_delay, best_act, best_target,
711 best_tile, ptile);
712 } else {
713 fc_assert(!removing);
714
715 road_deps_iterate(&(pextra->reqs), pdep) {
716 struct extra_type *dep_tgt;
717
718 dep_tgt = road_extra_get(pdep);
719
721 action_speculate_unit_on_tile(nmap, ACTION_ROAD,
722 punit, unit_home(punit), ptile,
723 parameter.omniscience,
724 ptile, dep_tgt))) {
725 /* Consider building dependency road for later upgrade to target extra.
726 * Here we set value to be sum of dependency
727 * road and target extra values, which increases want, and turns is sum
728 * of dependency and target build turns, which decreases want. This can
729 * result in either bigger or lesser want than when checking dependency
730 * road for the sake of itself when its turn in extra_type_iterate() is. */
731 int dep_turns = turns + get_turns_for_activity_at(punit,
732 ACTIVITY_GEN_ROAD,
733 ptile,
734 dep_tgt);
735 adv_want dep_value
736 = base_value + adv_city_worker_extra_get(pcity, cindex, dep_tgt);
737
738 consider_settler_action(pplayer, ACTIVITY_GEN_ROAD, dep_tgt, extra,
739 dep_value,
740 oldv, in_use, dep_turns,
741 &best_newv, &best_oldv, &best_extra,
742 &improve_worked,
743 &best_delay, best_act, best_target,
744 best_tile, ptile);
745 }
747
748 extra_deps_iterate(&(pextra->reqs), pdep) {
749 /* Roads handled above already */
750 if (!is_extra_caused_by(pdep, EC_ROAD)) {
751 enum unit_activity eval_dep_act = ACTIVITY_LAST;
752 action_id eval_dep_action;
753
754 as_extra_action_iterate(try_act) {
755 struct action *taction = action_by_number(try_act);
756
757 if (is_extra_caused_by_action(pdep, taction)) {
758 eval_dep_action = try_act;
759 eval_dep_act = action_id_get_activity(try_act);
760 break;
761 }
763
764 if (eval_dep_act != ACTIVITY_LAST) {
766 action_speculate_unit_on_tile(nmap, eval_dep_action,
767 punit, unit_home(punit), ptile,
768 parameter.omniscience,
769 ptile, pdep))) {
770 /* Consider building dependency extra for later upgrade to
771 * target extra. See similar road implementation above for
772 * extended commentary. */
773 int dep_turns = turns + get_turns_for_activity_at(punit,
774 eval_dep_act,
775 ptile,
776 pdep);
777 adv_want dep_value
778 = base_value + adv_city_worker_extra_get(pcity, cindex,
779 pdep);
780
781 consider_settler_action(pplayer, eval_dep_act, pdep,
782 0.0, dep_value, oldv, in_use,
783 dep_turns, &best_newv, &best_oldv,
784 &best_extra, &improve_worked,
785 &best_delay,
786 best_act, best_target,
787 best_tile, ptile);
788 }
789 }
790 }
792 }
793 }
795 } /* endif: can we arrive sooner than current worker, if any? */
796 } /* endif: are we travelling to a legal destination? */
799
800 if (!improve_worked) {
801 /* best_newv contains total value of improved tile. Check amount of improvement
802 * instead. */
803 best_newv = amortize((best_newv - best_oldv + best_extra) * WORKER_FACTOR, best_delay);
804 }
805 best_newv /= WORKER_FACTOR;
806
807 best_newv = MAX(best_newv, 0); /* sanity */
808
809 if (best_newv > 0) {
810 log_debug("Settler %d@(%d,%d) wants to %s at (%d,%d) with desire " ADV_WANT_PRINTF,
812 get_activity_text(*best_act), TILE_XY(*best_tile), best_newv);
813 } else {
814 /* Fill in dummy values. The callers should check if the return value
815 * is > 0 but this will avoid confusing them. */
816 *best_act = ACTIVITY_IDLE;
817 *best_tile = NULL;
818 }
819
820 if (path) {
821 *path = *best_tile ? pf_map_path(pfm, *best_tile) : NULL;
822 }
823
824 pf_map_destroy(pfm);
825
826 return best_newv;
827}
828
829/**********************************************************************/
833 struct worker_task **best_task,
834 struct pf_path **path,
835 struct settlermap *state)
836{
837 const struct player *pplayer = unit_owner(punit);
838 struct pf_parameter parameter;
839 struct pf_map *pfm;
840 struct pf_position pos;
841 int best_value = -1;
842 struct worker_task *best = NULL;
843 struct city *taskcity = NULL;
844 int dist = FC_INFINITY;
845 const struct civ_map *nmap = &(wld.map);
846
847 pft_fill_unit_parameter(&parameter, nmap, punit);
848 parameter.omniscience = !has_handicap(pplayer, H_MAP);
850 pfm = pf_map_new(&parameter);
851
852 /* Have nearby cities requests? */
853 city_list_iterate(pplayer->cities, pcity) {
854 worker_task_list_iterate(pcity->task_reqs, ptask) {
855 bool consider = TRUE;
856
857 /* Do not go to tiles that already have workers there. */
858 unit_list_iterate(ptask->ptile->units, aunit) {
859 if (unit_owner(aunit) == pplayer
860 && aunit->id != punit->id
861 && unit_has_type_flag(aunit, UTYF_SETTLERS)) {
862 consider = FALSE;
863 }
865
866 if (consider
868 parameter.omniscience,
869 ptask->tgt, ptask->ptile)) {
870 /* closest worker, if any, headed towards target tile */
871 struct unit *enroute = NULL;
872
873 if (state) {
874 enroute = player_unit_by_number(pplayer,
875 state[tile_index(ptask->ptile)].enroute);
876 }
877
878 if (pf_map_position(pfm, ptask->ptile, &pos)) {
879 int value = (ptask->want + 1) * 10 / (pos.turn + 1);
880
881 if (value > best_value) {
882 int eta = FC_INFINITY, inbound_distance = FC_INFINITY;
883
884 if (enroute) {
885 eta = state[tile_index(ptask->ptile)].eta;
886 inbound_distance = real_map_distance(ptask->ptile, unit_tile(enroute));
887 }
888
889 /* Only consider this tile if we are closer in time and space to
890 * it than our other worker (if any) travelling to the site. */
891 if (pos.turn < dist
892 && ((enroute && enroute->id == punit->id)
893 || pos.turn < eta
894 || (pos.turn == eta
895 && (real_map_distance(ptask->ptile, unit_tile(punit))
896 < inbound_distance)))) {
897 dist = pos.turn;
898 best = ptask;
899 best_value = value;
900 taskcity = pcity;
901 }
902 }
903 }
904 }
907
908 *best_task = best;
909
910 if (path != NULL) {
911 *path = best ? pf_map_path(pfm, best->ptile) : NULL;
912 }
913
914 pf_map_destroy(pfm);
915
916 return taskcity;
917}
918
919/**********************************************************************/
922#define LOG_SETTLER LOG_DEBUG
923void auto_settler_findwork(const struct civ_map *nmap,
924 struct player *pplayer,
925 struct unit *punit,
926 struct settlermap *state,
927 int recursion)
928{
929 struct worker_task *best_task;
930 enum unit_activity best_act;
931 struct tile *best_tile = NULL;
932 struct extra_type *best_target;
933 struct pf_path *path = NULL;
934 struct city *taskcity;
935
936 /* Time it will take worker to complete its given task */
937 int completion_time = 0;
938
939 /* Terminate what might be an infinite recursion of two units
940 * displacing each other, but leave enough space for
941 * finite recursion. */
942 if (recursion > 5
943 && recursion > unit_list_size(pplayer->units) * 1.5) {
944 log_warn("Workers displacing each other recursing too much.");
945
947 set_unit_activity(punit, ACTIVITY_IDLE);
948 send_unit_info(NULL, punit);
949
950 return; /* Avoid further recursion. */
951 }
952
954
955 fc_assert_ret(pplayer && punit);
957 || unit_has_type_flag(punit, UTYF_SETTLERS));
958
959 /* Have nearby cities requests? */
960
961 taskcity = settler_evaluate_city_requests(punit, &best_task, &path, state);
962
963 if (taskcity != NULL) {
964 if (path != NULL) {
965 completion_time = pf_path_last_position(path)->turn;
966 }
967
969
970 best_target = best_task->tgt;
971
972 if (auto_settler_setup_work(nmap, pplayer, punit, state, recursion,
973 path, best_task->ptile, best_task->act,
974 &best_target, completion_time)) {
975 clear_worker_task(taskcity, best_task);
976 }
977
978 if (path != NULL) {
979 pf_path_destroy(path);
980 }
981
982 return;
983 }
984
985 /*** Try find some work ***/
986
987 if (unit_has_type_flag(punit, UTYF_SETTLERS)) {
989 settler_evaluate_improvements(nmap, punit, &best_act, &best_target,
990 &best_tile, &path, state);
991 if (path) {
992 completion_time = pf_path_last_position(path)->turn;
993 }
995
997
998 auto_settler_setup_work(nmap, pplayer, punit, state, recursion, path,
999 best_tile, best_act, &best_target,
1000 completion_time);
1001
1002 if (NULL != path) {
1003 pf_path_destroy(path);
1004 }
1005 }
1006}
1007
1008/**********************************************************************/
1012bool auto_settler_setup_work(const struct civ_map *nmap,
1013 struct player *pplayer, struct unit *punit,
1014 struct settlermap *state, int recursion,
1015 struct pf_path *path,
1016 struct tile *best_tile,
1017 enum unit_activity best_act,
1018 struct extra_type **best_target,
1019 int completion_time)
1020{
1021 /* Run the "autosettler" program */
1023 struct pf_map *pfm = NULL;
1024 struct pf_parameter parameter;
1025 bool working = FALSE;
1026 struct unit *displaced;
1027
1028 if (!best_tile) {
1029 UNIT_LOG(LOG_DEBUG, punit, "giving up trying to improve terrain");
1030 return FALSE; /* We cannot do anything */
1031 }
1032
1033 /* Mark the square as taken. */
1034 displaced = player_unit_by_number(pplayer,
1035 state[tile_index(best_tile)].enroute);
1036
1037 if (displaced) {
1038 fc_assert(state[tile_index(best_tile)].enroute == displaced->id);
1039 fc_assert(state[tile_index(best_tile)].eta > completion_time
1040 || (state[tile_index(best_tile)].eta == completion_time
1041 && (real_map_distance(best_tile, unit_tile(punit))
1042 < real_map_distance(best_tile,
1043 unit_tile(displaced)))));
1045 "%d (%d,%d) has displaced %d (%d,%d) for worksite %d,%d",
1046 punit->id, completion_time,
1047 real_map_distance(best_tile, unit_tile(punit)),
1048 displaced->id, state[tile_index(best_tile)].eta,
1049 real_map_distance(best_tile, unit_tile(displaced)),
1050 TILE_XY(best_tile));
1051 }
1052
1053 state[tile_index(best_tile)].enroute = punit->id;
1054 state[tile_index(best_tile)].eta = completion_time;
1055
1056 if (displaced) {
1057 struct tile *goto_tile = punit->goto_tile;
1058 int saved_id = punit->id;
1059 struct tile *old_pos = unit_tile(punit);
1060
1061 displaced->goto_tile = NULL;
1062 auto_settler_findwork(nmap, pplayer, displaced, state, recursion + 1);
1063 if (NULL == player_unit_by_number(pplayer, saved_id)) {
1064 /* Actions of the displaced settler somehow caused this settler
1065 * to die. (maybe by recursively giving control back to this unit)
1066 */
1067 return FALSE;
1068 }
1069 if (goto_tile != punit->goto_tile || old_pos != unit_tile(punit)
1070 || punit->activity != ACTIVITY_IDLE) {
1071 /* Actions of the displaced settler somehow caused this settler
1072 * to get a new job, or to already move toward current job.
1073 * (A displaced B, B displaced C, C displaced A)
1074 */
1076 "%d itself acted due to displacement recursion. "
1077 "Was going from (%d, %d) to (%d, %d). "
1078 "Now heading from (%d, %d) to (%d, %d).",
1079 punit->id,
1080 TILE_XY(old_pos), TILE_XY(goto_tile),
1082 return FALSE;
1083 }
1084 }
1085
1087 "is heading to do %s(%s) at (%d, %d)",
1088 unit_activity_name(best_act),
1089 best_target && *best_target ? extra_rule_name(*best_target) : "-",
1090 TILE_XY(best_tile));
1091
1092 if (!path) {
1093 pft_fill_unit_parameter(&parameter, nmap, punit);
1094 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1096 pfm = pf_map_new(&parameter);
1097 path = pf_map_path(pfm, best_tile);
1098 }
1099
1100 if (path) {
1101 bool alive;
1102
1103 alive = adv_follow_path(punit, path, best_tile);
1104
1105 if (alive && same_pos(unit_tile(punit), best_tile)
1106 && punit->moves_left > 0) {
1107 /* Reached destination and can start working immediately */
1108 if (activity_requires_target(best_act)) {
1109 unit_activity_handling_targeted(punit, best_act, best_target);
1110 } else {
1111 unit_activity_handling(punit, best_act);
1112 }
1113 send_unit_info(NULL, punit); /* FIXME: probably duplicate */
1114
1116 "reached its worksite and started work");
1117 working = TRUE;
1118 } else if (alive) {
1120 "didn't start work yet; got to (%d, %d) with "
1121 "%d move frags left", TILE_XY(unit_tile(punit)),
1122 punit->moves_left);
1123 }
1124 } else {
1126 "does not find path (%d, %d) -> (%d, %d)",
1127 TILE_XY(unit_tile(punit)), TILE_XY(best_tile));
1128 }
1129
1130 if (pfm) {
1131 pf_map_destroy(pfm);
1132 }
1133
1134 return working;
1135 }
1136
1137 return FALSE;
1138}
1139#undef LOG_SETTLER
1140
1141/**********************************************************************/
1144bool adv_settler_safe_tile(const struct civ_map *nmap,
1145 const struct player *pplayer, struct unit *punit,
1146 struct tile *ptile)
1147{
1148 unit_list_iterate(ptile->units, defender) {
1149 if (is_military_unit(defender)) {
1150 return TRUE;
1151 }
1153
1154 if (is_square_threatened(nmap, pplayer, ptile,
1155 !has_handicap(pplayer, H_FOG))) {
1156 return FALSE;
1157 }
1158
1159 return TRUE;
1160}
1161
1162/**********************************************************************/
1166void auto_settlers_player(struct player *pplayer)
1167{
1168 struct settlermap *state;
1169 const struct civ_map *nmap = &(wld.map);
1170
1171 state = fc_calloc(MAP_INDEX_SIZE, sizeof(*state));
1172
1175
1176 if (is_ai(pplayer)) {
1177 /* Set up our city map. */
1178 citymap_turn_init(pplayer);
1179 }
1180
1181 whole_map_iterate(nmap, ptile) {
1182 state[tile_index(ptile)].enroute = -1;
1183 state[tile_index(ptile)].eta = FC_INFINITY;
1185
1186 /* Initialize the infrastructure cache, which is used shortly. */
1188
1189 /* An extra consideration for the benefit of cleaning up pollution/fallout.
1190 * This depends heavily on the calculations in update_environmental_upset.
1191 * Aside from that it's more or less a WAG that simply grows incredibly
1192 * large as an environmental disaster approaches. */
1193 pplayer->ai_common.warmth
1196 pplayer->ai_common.frost
1199
1200 log_debug("Warmth = %d, game.globalwarming=%d",
1202 log_debug("Frost = %d, game.nuclearwinter=%d",
1204
1205 /* Auto-settle with a settler unit if it's under AI control (e.g. human
1206 * player auto-settler mode) or if the player is an AI. But don't
1207 * auto-settle with a unit under orders even for an AI player - these come
1208 * from the human player and take precedence. */
1210 if ((punit->ssa_controller == SSA_AUTOSETTLER || is_ai(pplayer))
1211 && (unit_type_get(punit)->adv.worker
1214 && punit->moves_left > 0) {
1215 log_debug("%s %s at (%d, %d) is controlled by server side agent %s.",
1219 server_side_agent_name(SSA_AUTOSETTLER));
1220 if (punit->activity == ACTIVITY_SENTRY) {
1221 unit_activity_handling(punit, ACTIVITY_IDLE);
1222 }
1223 if (punit->activity == ACTIVITY_GOTO && punit->moves_left > 0) {
1224 unit_activity_handling(punit, ACTIVITY_IDLE);
1225 }
1226 if (punit->activity != ACTIVITY_IDLE) {
1227 if (!is_ai(pplayer)) {
1228 if (!adv_settler_safe_tile(nmap, pplayer, punit,
1229 unit_tile(punit))) {
1230 unit_activity_handling(punit, ACTIVITY_IDLE);
1231 }
1232 } else {
1233 CALL_PLR_AI_FUNC(settler_cont, pplayer, pplayer, punit, state);
1234 }
1235 }
1236 if (punit->activity == ACTIVITY_IDLE) {
1237 if (!is_ai(pplayer)) {
1238 auto_settler_findwork(nmap, pplayer, punit, state, 0);
1239 } else {
1240 CALL_PLR_AI_FUNC(settler_run, pplayer, pplayer, punit, state);
1241 }
1242 }
1243 }
1245 /* Reset auto settler state for the next run. */
1246 if (is_ai(pplayer)) {
1247 CALL_PLR_AI_FUNC(settler_reset, pplayer, pplayer);
1248 }
1249
1250 if (timer_in_use(as_timer)) {
1251
1252#ifdef LOG_TIMERS
1253 log_verbose("%s autosettlers consumed %g milliseconds.",
1255 1000.0 * timer_read_seconds(as_timer));
1256#else
1257 log_verbose("%s autosettlers finished",
1259#endif
1260
1261 }
1262
1263 FC_FREE(state);
1264}
1265
1266/**********************************************************************/
1270 struct tile *ptile)
1271{
1272 if (punit->server.adv->task == task) {
1273 /* Already that task */
1274 return;
1275 }
1276
1277 punit->server.adv->task = task;
1278
1279 CALL_PLR_AI_FUNC(unit_task, unit_owner(punit), punit, task, ptile);
1280}
1281
1282/**********************************************************************/
1287 enum unit_activity activity,
1288 bool omniscient_cheat,
1289 struct extra_type *target,
1290 const struct tile *ptile)
1291{
1292 const struct civ_map *nmap = &(wld.map);
1293
1294 action_by_activity_iterate(paction, activity) {
1295 if (action_get_actor_kind(paction) != AAK_UNIT) {
1296 /* Not relevant. */
1297 continue;
1298 }
1299
1300 switch (action_get_target_kind(paction)) {
1301 case ATK_CITY:
1303 nmap, paction->id,
1304 punit, unit_home(punit), ptile,
1305 omniscient_cheat,
1306 tile_city(ptile)));
1307 case ATK_UNIT:
1308 fc_assert_ret_val(action_get_target_kind(paction) != ATK_UNIT,
1309 FALSE);
1310 break;
1311 case ATK_UNITS:
1313 nmap, paction->id,
1314 punit, unit_home(punit), ptile,
1315 omniscient_cheat,
1316 ptile));
1317 case ATK_TILE:
1319 nmap, paction->id,
1320 punit, unit_home(punit), ptile,
1321 omniscient_cheat,
1322 ptile, target));
1323 case ATK_EXTRAS:
1325 nmap, paction->id,
1326 punit, unit_home(punit), ptile,
1327 omniscient_cheat,
1328 ptile, target));
1329 case ATK_SELF:
1331 nmap, paction->id,
1332 punit, unit_home(punit), ptile,
1333 omniscient_cheat));
1334 case ATK_COUNT:
1335 fc_assert_ret_val(action_get_target_kind(paction) != ATK_COUNT,
1336 FALSE);
1337 break;
1338 }
1340
1341 log_debug("No action found for activity %d", activity);
1342
1343 return FALSE;
1344}
struct act_prob action_speculate_unit_on_extras(const struct civ_map *nmap, action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, bool omniscient_cheat, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:6591
enum action_actor_kind action_get_actor_kind(const struct action *paction)
Definition actions.c:1730
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:6703
struct act_prob action_speculate_unit_on_self(const struct civ_map *nmap, action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, bool omniscient_cheat)
Definition actions.c:6625
void action_list_end(action_id *act_list, int size)
Definition actions.c:7397
struct act_prob action_speculate_unit_on_units(const struct civ_map *nmap, action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, bool omniscient_cheat, const struct tile *target)
Definition actions.c:6524
enum unit_activity actres_get_activity(enum action_result result)
Definition actions.c:2136
void action_list_add_all_by_result(action_id *act_list, int *position, enum action_result result)
Definition actions.c:7414
struct act_prob action_speculate_unit_on_city(const struct civ_map *nmap, const action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, const bool omniscient_cheat, const struct city *target)
Definition actions.c:6456
struct act_prob action_speculate_unit_on_tile(const struct civ_map *nmap, action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, bool omniscient_cheat, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:6557
enum action_target_kind action_get_target_kind(const struct action *paction)
Definition actions.c:1740
#define action_by_activity_iterate(_paction_, _activity_)
Definition actions.h:496
static struct action * action_by_number(action_id act_id)
Definition actions.h:638
#define action_by_activity_iterate_end
Definition actions.h:500
#define MAX_NUM_ACTIONS
Definition actions.h:296
#define action_id_get_activity(act_id)
Definition actions.h:712
#define COOLING_FACTOR
Definition advbuilding.h:31
#define WARMING_FACTOR
Definition advbuilding.h:30
bool adv_follow_path(struct unit *punit, struct pf_path *path, struct tile *ptile)
Definition advgoto.c:47
adv_want amortize(adv_want benefit, int delay)
Definition advtools.c:29
#define ADV_WANTS_EQ(_w1, _w2)
Definition advtools.h:23
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:374
static void consider_settler_action(const struct player *pplayer, enum unit_activity act, struct extra_type *target, adv_want extra, adv_want new_tile_value, adv_want old_tile_value, bool in_use, int delay, adv_want *best_value, adv_want *best_old_tile_value, int *best_extra, bool *improve_worked, int *best_delay, enum unit_activity *best_act, struct extra_type **best_target, struct tile **best_tile, struct tile *ptile)
action_id as_actions_rmextra[MAX_NUM_ACTIONS]
void adv_unit_new_task(struct unit *punit, enum adv_unit_task task, struct tile *ptile)
action_id as_actions_transform[MAX_NUM_ACTIONS]
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)
void auto_settlers_ruleset_init(void)
static enum tile_behavior autosettler_tile_behavior(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
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)
void auto_settlers_player(struct player *pplayer)
action_id as_actions_extra[MAX_NUM_ACTIONS]
adv_want adv_settlers_road_bonus(const struct civ_map *nmap, struct tile *ptile, struct road_type *proad)
bool adv_settler_safe_tile(const struct civ_map *nmap, const struct player *pplayer, struct unit *punit, struct tile *ptile)
void adv_settlers_free(void)
static struct timer * as_timer
bool auto_settlers_speculate_can_act_at(const struct unit *punit, enum unit_activity activity, bool omniscient_cheat, struct extra_type *target, const struct tile *ptile)
#define WORKER_FACTOR
#define MAX_DEP_ROADS
void auto_settler_findwork(const struct civ_map *nmap, struct player *pplayer, struct unit *punit, struct settlermap *state, int recursion)
#define as_transform_action_iterate_end
#define as_rmextra_action_iterate(_act_)
#define as_rmextra_action_iterate_end
#define as_extra_action_iterate_end
#define as_extra_action_iterate(_act_)
#define as_transform_action_iterate(_act_)
int city_map_radius_sq_get(const struct city *pcity)
Definition city.c:132
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1429
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_tile(_pcity_)
Definition city.h:544
#define city_tile_iterate_index(_nmap, _radius_sq, _city_tile, _tile, _index)
Definition city.h:193
#define city_tile_iterate_index_end
Definition city.h:201
#define city_list_iterate_end
Definition city.h:490
void citymap_turn_init(struct player *pplayer)
Definition citymap.c:61
void clear_worker_task(struct city *pcity, struct worker_task *ptask)
Definition citytools.c:3476
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 extra_type * next_extra_for_tile(const struct tile *ptile, enum extra_cause cause, const struct player *pplayer, const struct unit *punit)
Definition extras.c:740
bool extra_has_flag(const struct extra_type *pextra, enum extra_flag_id flag)
Definition extras.c:810
struct extra_type * prev_extra_in_tile(const struct tile *ptile, enum extra_rmcause rmcause, const struct player *pplayer, const struct unit *punit)
Definition extras.c:765
enum extra_cause activity_to_extra_cause(enum unit_activity act)
Definition extras.c:1028
bool is_extra_caused_by_action(const struct extra_type *pextra, const struct action *paction)
Definition extras.c:1006
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:195
bool is_extra_removed_by_action(const struct extra_type *pextra, const struct action *paction)
Definition extras.c:1017
enum extra_rmcause activity_to_extra_rmcause(enum unit_activity act)
Definition extras.c:1049
#define extra_type_iterate(_p)
Definition extras.h:291
#define extra_deps_iterate(_reqs, _dep)
Definition extras.h:338
#define extra_type_iterate_end
Definition extras.h:297
#define is_extra_caused_by(e, c)
Definition extras.h:196
#define extra_deps_iterate_end
Definition extras.h:346
#define extra_road_get(_e_)
Definition extras.h:185
#define extra_type_by_cause_iterate_end
Definition extras.h:315
#define extra_type_by_cause_iterate(_cause, _extra)
Definition extras.h:309
float adv_want
Definition fc_types.h:1206
adv_unit_task
Definition fc_types.h:340
@ AUT_NONE
Definition fc_types.h:340
@ AUT_AUTO_SETTLER
Definition fc_types.h:340
int action_id
Definition fc_types.h:359
#define EC_NONE
Definition fc_types.h:967
#define ERM_NONE
Definition fc_types.h:992
#define ADV_WANT_PRINTF
Definition fc_types.h:1207
struct civ_game game
Definition game.c:57
struct world wld
Definition game.c:58
struct city * owner
Definition citydlg.c:219
static struct tile * pos
Definition finddlg.c:53
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
Definition handicaps.c:66
@ H_MAP
Definition handicaps.h:28
@ H_FOG
Definition handicaps.h:26
adv_want adv_city_worker_act_get(const struct city *pcity, int city_tile_index, enum unit_activity act_id)
Definition infracache.c:359
int adv_city_worker_extra_get(const struct city *pcity, int city_tile_index, const struct extra_type *pextra)
Definition infracache.c:429
void initialize_infrastructure_cache(struct player *pplayer)
Definition infracache.c:250
int adv_city_worker_rmextra_get(const struct city *pcity, int city_tile_index, const struct extra_type *pextra)
Definition infracache.c:446
adv_want city_tile_value(const struct city *pcity, const struct tile *ptile, int foodneed, int prodneed)
Definition infracache.c:302
#define fc_assert_ret(condition)
Definition log.h:191
#define log_warn(message,...)
Definition log.h:105
#define log_verbose(message,...)
Definition log.h:109
#define fc_assert(condition)
Definition log.h:176
#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
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Definition map.c:417
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:938
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:628
struct terrain_misc terrain_control
Definition map.c:69
#define current_topo_has_flag(flag)
Definition map.h:45
#define MAP_INDEX_SIZE
Definition map.h:131
#define whole_map_iterate(_map, _tile)
Definition map.h:539
#define whole_map_iterate_end
Definition map.h:548
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:227
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#define SINGLE_MOVE
Definition movement.h:24
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:137
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:443
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)
struct pf_path * pf_map_path(struct pf_map *pfm, struct tile *ptile)
bool pf_map_position(struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
void pf_map_destroy(struct pf_map *pfm)
tile_behavior
@ TB_NORMAL
@ TB_IGNORE
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:840
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1205
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1381
#define is_ai(plr)
Definition player.h:234
struct extra_type * road_extra_get(const struct road_type *proad)
Definition road.c:42
Road_type_id road_number(const struct road_type *proad)
Definition road.c:32
bool road_provides_move_bonus(const struct road_type *proad)
Definition road.c:499
#define road_deps_iterate(_reqs, _dep)
Definition road.h:159
#define road_deps_iterate_end
Definition road.h:168
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
static int recursion[AIT_LAST]
Definition srv_log.c:44
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
#define LOG_AI_TEST
Definition srv_log.h:38
@ 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
enum action_result result
Definition actions.h:382
Definition city.h:309
struct packet_game_info info
Definition game.h:89
struct requirement_vector reqs
Definition extras.h:102
enum tile_behavior(* get_TB)(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
const struct player * owner
int warmth
Definition player.h:127
int frost
Definition player.h:127
struct city_list * cities
Definition player.h:281
struct player_ai ai_common
Definition player.h:288
struct unit_list * units
Definition player.h:282
int move_cost
Definition road.h:78
Definition tile.h:49
struct unit_list * units
Definition tile.h:57
Definition timing.c:81
enum adv_unit_task task
Definition unit.h:89
bool worker
Definition unittype.h:556
Definition unit.h:138
enum unit_activity activity
Definition unit.h:157
int moves_left
Definition unit.h:150
int id
Definition unit.h:145
bool debug
Definition unit.h:231
struct extra_type * activity_target
Definition unit.h:164
struct unit_adv * adv
Definition unit.h:233
struct unit::@80::@83 server
struct tile * goto_tile
Definition unit.h:155
enum server_side_agent ssa_controller
Definition unit.h:172
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
int terrain_extra_build_time(const struct terrain *pterrain, enum unit_activity activity, const struct extra_type *tgt)
Definition terrain.c:682
bool tile_has_road(const struct tile *ptile, const struct road_type *proad)
Definition tile.c:829
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:87
#define tile_worked(_tile)
Definition tile.h:113
known_type
Definition tile.h:34
#define tile_terrain(_tile)
Definition tile.h:109
#define TILE_XY(ptile)
Definition tile.h:42
#define tile_has_extra(ptile, pextra)
Definition tile.h:146
#define tile_owner(_tile)
Definition tile.h:95
bool timer_in_use(struct timer *t)
Definition timing.c:202
void timer_destroy(struct timer *t)
Definition timing.c:191
void timer_start(struct timer *t)
Definition timing.c:224
double timer_read_seconds(struct timer *t)
Definition timing.c:344
struct timer * timer_renew(struct timer *t, enum timer_timetype type, enum timer_use use)
Definition timing.c:176
#define TIMER_DEBUG
Definition timing.h:59
@ TIMER_CPU
Definition timing.h:40
static int best_value(const void *a, const void *b)
int get_turns_for_activity_at(const struct unit *punit, enum unit_activity activity, const struct tile *ptile, struct extra_type *tgt)
Definition unit.c:521
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1107
bool is_military_unit(const struct unit *punit)
Definition unit.c:319
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2623
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:625
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:357
bool unit_has_orders(const struct unit *punit)
Definition unit.c:204
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:541
#define unit_tile(_pu)
Definition unit.h:395
#define CHECK_UNIT(punit)
Definition unit.h:268
#define unit_owner(_pu)
Definition unit.h:394
#define unit_home(_pu_)
Definition unit.h:393
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Definition unithand.c:6179
bool unit_activity_handling_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type **new_target)
Definition unithand.c:6246
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_safe(unitlist, _unit)
Definition unitlist.h:39
#define unit_list_iterate_end
Definition unitlist.h:33
#define unit_list_iterate_safe_end
Definition unitlist.h:61
void send_unit_info(struct conn_list *dest, struct unit *punit)
Definition unittools.c:2794
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1639
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:184
#define worker_task_list_iterate(tasklist, ptask)
Definition workertask.h:33
#define worker_task_list_iterate_end
Definition workertask.h:35