Freeciv-3.2
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 "nation.h"
36#include "packets.h"
37#include "unitlist.h"
38
39/* common/aicore */
40#include "citymap.h"
41#include "path_finding.h"
42#include "pf_tools.h"
43
44/* server */
45#include "citytools.h"
46#include "maphand.h"
47#include "plrhand.h"
48#include "srv_log.h"
49#include "unithand.h"
50#include "unittools.h"
51
52/* server/advisors */
53#include "advbuilding.h"
54#include "advdata.h"
55#include "advgoto.h"
56#include "advtools.h"
57#include "infracache.h"
58
59/* ai */
60#include "handicaps.h"
61
62#include "autosettlers.h"
63
64/* This factor is multiplied on when calculating the want. This is done
65 * to avoid rounding errors in comparisons when looking for the best
66 * possible work. However before returning the final want we have to
67 * divide by it again. This loses accuracy but is needed since the want
68 * values are used for comparison by the AI in trying to calculate the
69 * goodness of building worker units. */
70#define WORKER_FACTOR 1024
71
72struct settlermap {
73 int enroute; /* unit ID of settler en route to this tile */
74 int eta; /* estimated number of turns until enroute arrives */
75};
76
80
81static struct timer *as_timer = NULL;
82
83/**********************************************************************/
91
92/**********************************************************************/
125
126/**********************************************************************/
133 struct tile *ptile, struct road_type *proad)
134{
135#define MAX_DEP_ROADS 5
136
137 int bonus = 0, i;
138 bool potential_road[12], real_road[12], is_slow[12];
139 int dx[12] = {-1, 0, 1, -1, 1, -1, 0, 1, 0, -2, 2, 0};
140 int dy[12] = {-1, -1, -1, 0, 0, 1, 1, 1, -2, 0, 0, 2};
141 int x, y;
142 int rnbr;
145 int dep_count = 0;
146 struct extra_type *pextra;
147
148 if (proad == NULL) {
149 return 0;
150 }
151
153 pextra = road_extra_get(proad);
154
155 road_deps_iterate(&(pextra->reqs), pdep) {
156 if (dep_count < MAX_DEP_ROADS) {
159 }
161
162 index_to_map_pos(&x, &y, tile_index(ptile));
163 for (i = 0; i < 12; i++) {
164 struct tile *tile1 = map_pos_to_tile(nmap, x + dx[i], y + dy[i]);
165
166 if (!tile1) {
167 real_road[i] = FALSE;
169 is_slow[i] = FALSE; /* FIXME: should be TRUE? */
170 } else {
172 int j;
173
176 for (j = 0 ; !potential_road[i] && j < dep_count ; j++) {
178 }
179
180 /* If TRUE, this value indicates that this tile does not need
181 * a road connector. This is set for terrains which cannot have
182 * road or where road takes "too long" to build. */
183 is_slow[i] = (build_time == 0 || build_time > 5);
184
185 if (!real_road[i]) {
186 unit_list_iterate(tile1->units, punit) {
188 /* If a road, or its dependency is being built here, consider as if it's already
189 * built. */
190 int build_rnbr;
191
193
195
196 if (build_rnbr == rnbr) {
197 real_road[i] = TRUE;
199 }
200 for (j = 0 ; !potential_road[i] && j < dep_count ; j++) {
201 if (build_rnbr == dep_rnbr[j]) {
203 }
204 }
205 }
207 }
208 }
209 }
210
212 /* On hex map, road is always a benefit */
213 bonus += 20; /* Later divided by 20 */
214
215 /* Road is more valuable when even longer road around does not exist. */
216 for (i = 0; i < 12; i++) {
217 if (!real_road[i]) {
218 bonus += 3;
219 }
220 }
221
222 /* Scale down the bonus. */
223 bonus /= 20;
224 } else {
225 /*
226 * Consider the following tile arrangement (numbered in hex):
227 *
228 * 8
229 * 012
230 * 93 4A
231 * 567
232 * B
233 *
234 * these are the tiles defined by the (dx,dy) arrays above.
235 *
236 * Then the following algorithm is supposed to determine if it's a good
237 * idea to build a road here. Note this won't work well for hex maps
238 * since the (dx,dy) arrays will not cover the same tiles.
239 *
240 * FIXME: if you can understand the algorithm below please rewrite this
241 * explanation!
242 */
243 if (potential_road[0]
244 && !real_road[1] && !real_road[3]
245 && (!real_road[2] || !real_road[8])
246 && (!is_slow[2] || !is_slow[4] || !is_slow[7]
247 || !is_slow[6] || !is_slow[5])) {
248 bonus++;
249 }
250 if (potential_road[2]
251 && !real_road[1] && !real_road[4]
252 && (!real_road[7] || !real_road[10])
253 && (!is_slow[0] || !is_slow[3] || !is_slow[7]
254 || !is_slow[6] || !is_slow[5])) {
255 bonus++;
256 }
257 if (potential_road[5]
258 && !real_road[6] && !real_road[3]
259 && (!real_road[5] || !real_road[11])
260 && (!is_slow[2] || !is_slow[4] || !is_slow[7]
261 || !is_slow[1] || !is_slow[0])) {
262 bonus++;
263 }
264 if (potential_road[7]
265 && !real_road[6] && !real_road[4]
266 && (!real_road[0] || !real_road[9])
267 && (!is_slow[2] || !is_slow[3] || !is_slow[0]
268 || !is_slow[1] || !is_slow[5])) {
269 bonus++;
270 }
271
272 /* A
273 * B*B
274 * CCC
275 *
276 * We are at tile *. If tile A has a road, and neither B tile does, and
277 * one C tile is a valid destination, then we might want a road here.
278 *
279 * Of course the same logic applies if you rotate the diagram.
280 */
281 if (potential_road[1] && !real_road[4] && !real_road[3]
282 && (!is_slow[5] || !is_slow[6] || !is_slow[7])) {
283 bonus++;
284 }
285 if (potential_road[3] && !real_road[1] && !real_road[6]
286 && (!is_slow[2] || !is_slow[4] || !is_slow[7])) {
287 bonus++;
288 }
289 if (potential_road[4] && !real_road[1] && !real_road[6]
290 && (!is_slow[0] || !is_slow[3] || !is_slow[5])) {
291 bonus++;
292 }
293 if (potential_road[6] && !real_road[4] && !real_road[3]
294 && (!is_slow[0] || !is_slow[1] || !is_slow[2])) {
295 bonus++;
296 }
297 }
298
299 return bonus;
300}
301
302/**********************************************************************/
308static void consider_settler_action(const struct player *pplayer,
309 enum unit_activity act,
310 struct extra_type *target,
311 adv_want extra,
314 bool in_use, int delay,
317 int *best_extra,
318 bool *improve_worked,
319 int *best_delay,
321 struct extra_type **best_target,
322 struct tile **best_tile,
323 struct tile *ptile)
324{
325 bool improves;
327 adv_want base_value = 0;
329
330 fc_assert(act != ACTIVITY_LAST);
331
332 if (extra < 0) {
333 extra = 0;
334 }
335
337 improves = TRUE;
338 } else if (ADV_WANTS_EQ(new_tile_value, old_tile_value) && extra > 0) {
339 improves = TRUE;
340 } else {
341 improves = FALSE;
342 }
343
344 /* find the present value of the future benefit of this action */
345 if (improves || extra > 0) {
346 if (!(*improve_worked) && !in_use) {
347 /* Going to improve tile that is not yet in use.
348 * Getting the best possible total for next citizen to work on is more
349 * important than amount tile gets improved. */
355 *best_extra = extra;
356 *best_act = act;
357 *best_target = target;
358 *best_tile = ptile;
359 *best_delay = delay;
360 }
361
362 return;
363 }
364
365 /* At least one of the previous best or current tile is in use
366 * Prefer the tile that gets improved more, regardless of
367 * the resulting total */
368
369 base_value = new_tile_value - old_tile_value;
370 total_value = base_value * WORKER_FACTOR;
371 if (!in_use) {
372 total_value /= 2;
373 }
374 total_value += extra * WORKER_FACTOR;
375
376 /* Use factor to prevent rounding errors */
378
379 if (*improve_worked) {
381 } else {
382 /* Convert old best_value to improvement value compatible with in_use
383 * tile value */
385 *best_delay);
386 }
387
391 if (in_use) {
394 } else {
397 }
399 *best_extra = extra;
400 *best_act = act;
401 *best_target = target;
402 *best_tile = ptile;
403 *best_delay = delay;
404 }
405 }
406}
407
408/**********************************************************************/
411static enum tile_behavior
412autosettler_tile_behavior(const struct tile *ptile,
413 enum known_type known,
414 const struct pf_parameter *param)
415{
416 const struct player *owner = tile_owner(ptile);
417
418 if (NULL != owner && !pplayers_allied(owner, param->owner)) {
419 return TB_IGNORE;
420 }
421 return TB_NORMAL;
422}
423
424/**********************************************************************/
442 struct unit *punit,
444 struct extra_type **best_target,
445 struct tile **best_tile,
446 struct pf_path **path,
447 struct settlermap *state)
448{
449 const struct player *pplayer = unit_owner(punit);
450 struct pf_parameter parameter;
451 struct pf_map *pfm;
452 struct pf_position pos;
453 adv_want oldv; /* Current value of consideration tile. */
454 adv_want best_oldv = 9999; /* oldv of best target so far; compared if
455 * newv == best_newv; not initialized to zero,
456 * so that newv = 0 activities are not chosen. */
458 bool improve_worked = FALSE;
459 int best_extra = 0;
460 int best_delay = 0;
461
462 /* Closest worker, if any, headed towards target tile */
463 struct unit *enroute = NULL;
464
465 pft_fill_unit_parameter(&parameter, nmap, punit);
466 parameter.omniscience = !has_handicap(pplayer, H_MAP);
468 pfm = pf_map_new(&parameter);
469
470 city_list_iterate(pplayer->cities, pcity) {
471 struct tile *pcenter = city_tile(pcity);
472
473 /* Try to work near the city */
475 cindex) {
476 bool consider = TRUE;
477 bool in_use = (tile_worked(ptile) == pcity);
478
479 if (!in_use && !city_can_work_tile(pcity, ptile)) {
480 /* Don't risk bothering with this tile. */
481 continue;
482 }
483
484 if (!adv_settler_safe_tile(nmap, pplayer, punit, ptile)) {
485 /* Too dangerous place */
486 continue;
487 }
488
489 /* Do not go to tiles that already have workers there. */
490 unit_list_iterate(ptile->units, aunit) {
491 if (unit_owner(aunit) == pplayer
492 && aunit->id != punit->id
494 consider = FALSE;
495 }
497
498 if (!consider) {
499 continue;
500 }
501
502 if (state) {
503 enroute = player_unit_by_number(pplayer,
504 state[tile_index(ptile)].enroute);
505 }
506
507 if (pf_map_position(pfm, ptile, &pos)) {
508 int eta = FC_INFINITY, inbound_distance = FC_INFINITY, turns;
509
510 if (enroute) {
511 eta = state[tile_index(ptile)].eta;
513 }
514
515 /* Only consider this tile if we are closer in time and space to
516 * it than our other worker (if any) travelling to the site. */
517 if ((enroute && enroute->id == punit->id)
518 || pos.turn < eta
519 || (pos.turn == eta
521 < inbound_distance))) {
522
523 if (enroute) {
525 "Considering (%d, %d) because we're closer "
526 "(%d, %d) than %d (%d, %d)",
527 TILE_XY(ptile), pos.turn,
529 enroute->id, eta, inbound_distance);
530 }
531
532 oldv = city_tile_value(pcity, ptile, 0, 0);
533
534 /* Now, consider various activities... */
536 struct extra_type *target = NULL;
537 enum extra_cause cause =
541
542 if (cause != EC_NONE) {
543 target = next_extra_for_tile(ptile, cause, pplayer,
544 punit);
545 } else if (rmcause != ERM_NONE) {
546 target = prev_extra_in_tile(ptile, rmcause, pplayer,
547 punit);
548 }
549
550 if (adv_city_worker_act_get(pcity, cindex,
551 action_id_get_activity(act)) >= 0
555 ptile,
556 parameter.omniscience,
557 ptile, target))) {
558 adv_want base_value
559 = adv_city_worker_act_get(pcity, cindex,
561
562 turns = pos.turn
565 ptile, target);
566 if (pos.moves_left == 0) {
567 /* We need moves left to begin activity immediately. */
568 turns++;
569 }
570
573 target, 0.0, base_value,
574 oldv, in_use, turns,
578 best_tile, ptile);
579
580 } /* endif: can the worker perform this action */
582
583 extra_type_iterate(pextra) {
584 enum unit_activity act = ACTIVITY_LAST;
586 adv_want base_value;
587 bool removing = tile_has_extra(ptile, pextra);
588
589 if (removing) {
592
593 if (is_extra_removed_by_action(pextra, taction)) {
594 /* We do not even evaluate actions we can't do.
595 * Removal is not considered prerequisite for anything */
598 punit,
600 ptile,
601 parameter.omniscience,
602 ptile, pextra))) {
605 break;
606 }
607 }
609 } else {
612
613 if (is_extra_caused_by_action(pextra, taction)) {
617 punit,
619 ptile,
620 parameter.omniscience,
621 ptile, pextra))) {
623 break;
624 }
625 }
627 }
628
629 if (eval_act == ACTIVITY_LAST) {
630 /* No activity can provide (or remove) the extra */
631 continue;
632 }
633
634 if (removing) {
635 base_value = adv_city_worker_rmextra_get(pcity, cindex, pextra);
636 } else {
637 base_value = adv_city_worker_extra_get(pcity, cindex, pextra);
638 }
639
640 if (base_value >= 0) {
641 adv_want extra;
642 struct road_type *proad;
643
645 ptile, pextra);
646 if (pos.moves_left == 0) {
647 /* We need moves left to begin activity immediately. */
648 turns++;
649 }
650
651 proad = extra_road_get(pextra);
652
654 int mc_multiplier = 1;
655 int mc_divisor = 1;
656 int old_move_cost = tile_terrain(ptile)->movement_cost * SINGLE_MOVE;
657
658 /* Here 'old' means actually 'without the evaluated': In case of
659 * removal activity it's the value after the removal. */
660
662 if (tile_has_extra(ptile, pold) && pold != pextra) {
664
665 /* This ignores the fact that new road may be native to units that
666 * old road is not. */
667 if (po_road->move_cost < old_move_cost) {
668 old_move_cost = po_road->move_cost;
669 }
670 }
672
673 if (proad->move_cost < old_move_cost) {
674 if (proad->move_cost >= terrain_control.move_fragments) {
675 mc_divisor = proad->move_cost / terrain_control.move_fragments;
676 } else {
677 if (proad->move_cost == 0) {
678 mc_multiplier = 2;
679 } else {
680 mc_multiplier = 1 - proad->move_cost;
681 }
683 }
684 }
685
686 extra = adv_settlers_road_bonus(nmap, ptile, proad)
688
689 } else {
690 extra = 0;
691 }
692
693 if (extra_has_flag(pextra, EF_GLOBAL_WARMING)) {
694 extra -= pplayer->ai_common.warmth;
695 }
696 if (extra_has_flag(pextra, EF_NUCLEAR_WINTER)) {
697 extra -= pplayer->ai_common.frost;
698 }
699
700 if (removing) {
701 extra = -extra;
702 }
703
704 if (act != ACTIVITY_LAST) {
705 consider_settler_action(pplayer, act, pextra, extra, base_value,
706 oldv, in_use, turns,
710 best_tile, ptile);
711 } else {
713
714 road_deps_iterate(&(pextra->reqs), pdep) {
715 struct extra_type *dep_tgt;
716
718
721 punit, unit_home(punit), ptile,
722 parameter.omniscience,
723 ptile, dep_tgt))) {
724 /* Consider building dependency road for later upgrade to target extra.
725 * Here we set value to be sum of dependency
726 * road and target extra values, which increases want, and turns is sum
727 * of dependency and target build turns, which decreases want. This can
728 * result in either bigger or lesser want than when checking dependency
729 * road for the sake of itself when its turn in extra_type_iterate() is. */
732 ptile,
733 dep_tgt);
735 = base_value + adv_city_worker_extra_get(pcity, cindex, dep_tgt);
736
738 dep_value,
743 best_tile, ptile);
744 }
746
747 extra_deps_iterate(&(pextra->reqs), pdep) {
748 /* Roads handled above already */
752
755
759 break;
760 }
762
766 punit, unit_home(punit), ptile,
767 parameter.omniscience,
768 ptile, pdep))) {
769 /* Consider building dependency extra for later upgrade to
770 * target extra. See similar road implementation above for
771 * extended commentary. */
774 ptile,
775 pdep);
777 = base_value + adv_city_worker_extra_get(pcity, cindex,
778 pdep);
779
781 0.0, dep_value, oldv, in_use,
784 &best_delay,
786 best_tile, ptile);
787 }
788 }
789 }
791 }
792 }
794 } /* endif: can we arrive sooner than current worker, if any? */
795 } /* endif: are we travelling to a legal destination? */
798
799 if (!improve_worked) {
800 /* best_newv contains total value of improved tile. Check amount of improvement
801 * instead. */
803 }
805
806 best_newv = MAX(best_newv, 0); /* sanity */
807
808 if (best_newv > 0) {
809 log_debug("Settler %d@(%d,%d) wants to %s at (%d,%d) with desire " ADV_WANT_PRINTF,
812 } else {
813 /* Fill in dummy values. The callers should check if the return value
814 * is > 0 but this will avoid confusing them. */
816 *best_tile = NULL;
817 }
818
819 if (path) {
820 *path = *best_tile ? pf_map_path(pfm, *best_tile) : NULL;
821 }
822
824
825 return best_newv;
826}
827
828/**********************************************************************/
832 struct worker_task **best_task,
833 struct pf_path **path,
834 struct settlermap *state)
835{
836 const struct player *pplayer = unit_owner(punit);
837 struct pf_parameter parameter;
838 struct pf_map *pfm;
839 struct pf_position pos;
840 int best_value = -1;
841 struct worker_task *best = NULL;
842 struct city *taskcity = NULL;
843 int dist = FC_INFINITY;
844 const struct civ_map *nmap = &(wld.map);
845
846 pft_fill_unit_parameter(&parameter, nmap, punit);
847 parameter.omniscience = !has_handicap(pplayer, H_MAP);
849 pfm = pf_map_new(&parameter);
850
851 /* Have nearby cities requests? */
852 city_list_iterate(pplayer->cities, pcity) {
853 worker_task_list_iterate(pcity->task_reqs, ptask) {
854 bool consider = TRUE;
855
856 /* Do not go to tiles that already have workers there. */
857 unit_list_iterate(ptask->ptile->units, aunit) {
858 if (unit_owner(aunit) == pplayer
859 && aunit->id != punit->id
861 consider = FALSE;
862 }
864
865 if (consider
867 parameter.omniscience,
868 ptask->tgt, ptask->ptile)) {
869 /* closest worker, if any, headed towards target tile */
870 struct unit *enroute = NULL;
871
872 if (state) {
873 enroute = player_unit_by_number(pplayer,
874 state[tile_index(ptask->ptile)].enroute);
875 }
876
877 if (pf_map_position(pfm, ptask->ptile, &pos)) {
878 int value = (ptask->want + 1) * 10 / (pos.turn + 1);
879
880 if (value > best_value) {
882
883 if (enroute) {
884 eta = state[tile_index(ptask->ptile)].eta;
886 }
887
888 /* Only consider this tile if we are closer in time and space to
889 * it than our other worker (if any) travelling to the site. */
890 if (pos.turn < dist
891 && ((enroute && enroute->id == punit->id)
892 || pos.turn < eta
893 || (pos.turn == eta
895 < inbound_distance)))) {
896 dist = pos.turn;
897 best = ptask;
898 best_value = value;
899 taskcity = pcity;
900 }
901 }
902 }
903 }
906
907 *best_task = best;
908
909 if (path != NULL) {
910 *path = best ? pf_map_path(pfm, best->ptile) : NULL;
911 }
912
914
915 return taskcity;
916}
917
918/**********************************************************************/
921#define LOG_SETTLER LOG_DEBUG
923 struct player *pplayer,
924 struct unit *punit,
925 struct settlermap *state,
926 int recursion)
927{
928 struct worker_task *best_task;
930 struct tile *best_tile = NULL;
931 struct extra_type *best_target;
932 struct pf_path *path = NULL;
933 struct city *taskcity;
934
935 /* Time it will take worker to complete its given task */
936 int completion_time = 0;
937
938 /* Terminate what might be an infinite recursion of two units
939 * displacing each other, but leave enough space for
940 * finite recursion. */
941 if (recursion > 5
942 && recursion > unit_list_size(pplayer->units) * 1.5) {
943 log_warn("Workers displacing each other recursing too much.");
944
948
949 return; /* Avoid further recursion. */
950 }
951
953
954 fc_assert_ret(pplayer && punit);
957
958 /* Have nearby cities requests? */
959
961
962 if (taskcity != NULL) {
963 if (path != NULL) {
965 }
966
968
969 best_target = best_task->tgt;
970
971 if (auto_settler_setup_work(nmap, pplayer, punit, state, recursion,
972 path, best_task->ptile, best_task->act,
975 }
976
977 if (path != NULL) {
978 pf_path_destroy(path);
979 }
980
981 return;
982 }
983
984 /*** Try find some work ***/
985
989 &best_tile, &path, state);
990 if (path) {
992 }
994
996
997 auto_settler_setup_work(nmap, pplayer, punit, state, recursion, path,
1000
1001 if (NULL != path) {
1002 pf_path_destroy(path);
1003 }
1004 }
1005}
1006
1007/**********************************************************************/
1012 struct player *pplayer, struct unit *punit,
1013 struct settlermap *state, int recursion,
1014 struct pf_path *path,
1015 struct tile *best_tile,
1017 struct extra_type **best_target,
1018 int completion_time)
1019{
1020 /* Run the "autosettler" program */
1022 struct pf_map *pfm = NULL;
1023 struct pf_parameter parameter;
1024 bool working = FALSE;
1025 struct unit *displaced;
1026
1027 if (!best_tile) {
1028 UNIT_LOG(LOG_DEBUG, punit, "giving up trying to improve terrain");
1029 return FALSE; /* We cannot do anything */
1030 }
1031
1032 /* Mark the square as taken. */
1034 state[tile_index(best_tile)].enroute);
1035
1036 if (displaced) {
1037 fc_assert(state[tile_index(best_tile)].enroute == displaced->id);
1039 || (state[tile_index(best_tile)].eta == completion_time
1042 unit_tile(displaced)))));
1043 UNIT_LOG(displaced->server.debug ? LOG_AI_TEST : LOG_DEBUG, punit,
1044 "%d (%d,%d) has displaced %d (%d,%d) for worksite %d,%d",
1047 displaced->id, state[tile_index(best_tile)].eta,
1050 }
1051
1052 state[tile_index(best_tile)].enroute = punit->id;
1054
1055 if (displaced) {
1056 struct tile *goto_tile = punit->goto_tile;
1057 int saved_id = punit->id;
1058 struct tile *old_pos = unit_tile(punit);
1059
1060 displaced->goto_tile = NULL;
1061 auto_settler_findwork(nmap, pplayer, displaced, state, recursion + 1);
1062 if (NULL == player_unit_by_number(pplayer, saved_id)) {
1063 /* Actions of the displaced settler somehow caused this settler
1064 * to die. (maybe by recursively giving control back to this unit)
1065 */
1066 return FALSE;
1067 }
1068 if (goto_tile != punit->goto_tile || old_pos != unit_tile(punit)
1069 || punit->activity != ACTIVITY_IDLE) {
1070 /* Actions of the displaced settler somehow caused this settler
1071 * to get a new job, or to already move toward current job.
1072 * (A displaced B, B displaced C, C displaced A)
1073 */
1075 "%d itself acted due to displacement recursion. "
1076 "Was going from (%d, %d) to (%d, %d). "
1077 "Now heading from (%d, %d) to (%d, %d).",
1078 punit->id,
1079 TILE_XY(old_pos), TILE_XY(goto_tile),
1081 return FALSE;
1082 }
1083 }
1084
1086 "is heading to do %s(%s) at (%d, %d)",
1090
1091 if (!path) {
1092 pft_fill_unit_parameter(&parameter, nmap, punit);
1093 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1095 pfm = pf_map_new(&parameter);
1096 path = pf_map_path(pfm, best_tile);
1097 }
1098
1099 if (path) {
1100 bool alive;
1101
1103
1105 && punit->moves_left > 0) {
1106 /* Reached destination and can start working immediately */
1109 } else {
1111 }
1112 send_unit_info(NULL, punit); /* FIXME: probably duplicate */
1113
1115 "reached its worksite and started work");
1116 working = TRUE;
1117 } else if (alive) {
1119 "didn't start work yet; got to (%d, %d) with "
1120 "%d move frags left", TILE_XY(unit_tile(punit)),
1121 punit->moves_left);
1122 }
1123 } else {
1125 "does not find path (%d, %d) -> (%d, %d)",
1127 }
1128
1129 if (pfm) {
1131 }
1132
1133 return working;
1134 }
1135
1136 return FALSE;
1137}
1138#undef LOG_SETTLER
1139
1140/**********************************************************************/
1144 const struct player *pplayer, struct unit *punit,
1145 struct tile *ptile)
1146{
1147 unit_list_iterate(ptile->units, defender) {
1148 if (is_guard_unit(defender)) {
1149 return TRUE;
1150 }
1152
1153 if (is_square_threatened(nmap, pplayer, ptile,
1154 !has_handicap(pplayer, H_FOG))) {
1155 return FALSE;
1156 }
1157
1158 return TRUE;
1159}
1160
1161/**********************************************************************/
1165void auto_settlers_player(struct player *pplayer)
1166{
1167 struct settlermap *state;
1168 const struct civ_map *nmap = &(wld.map);
1169
1170 state = fc_calloc(MAP_INDEX_SIZE, sizeof(*state));
1171
1173 as_timer != NULL ? NULL : "autosettlers");
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.",
1220 if (punit->activity == ACTIVITY_SENTRY) {
1222 }
1223 if (punit->activity == ACTIVITY_GOTO && punit->moves_left > 0) {
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))) {
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
1296 /* Not relevant. */
1297 continue;
1298 }
1299
1301 case ATK_CITY:
1303 nmap, paction->id,
1304 punit, unit_home(punit), ptile,
1306 tile_city(ptile)));
1307 case ATK_UNIT:
1309 FALSE);
1310 break;
1311 case ATK_UNITS:
1313 nmap, paction->id,
1314 punit, unit_home(punit), ptile,
1316 ptile));
1317 case ATK_TILE:
1319 nmap, paction->id,
1320 punit, unit_home(punit), ptile,
1322 ptile, target));
1323 case ATK_EXTRAS:
1325 nmap, paction->id,
1326 punit, unit_home(punit), ptile,
1328 ptile, target));
1329 case ATK_SELF:
1331 nmap, paction->id,
1332 punit, unit_home(punit), ptile,
1334 case 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:5709
enum action_actor_kind action_get_actor_kind(const struct action *paction)
Definition actions.c:1859
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5821
struct act_prob action_speculate_unit_on_stack(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:5642
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:5743
void action_array_add_all_by_result(action_id *act_array, int *position, enum action_result result)
Definition actions.c:6486
void action_array_end(action_id *act_array, int size)
Definition actions.c:6469
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:5574
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:5675
enum action_target_kind action_get_target_kind(const struct action *paction)
Definition actions.c:1869
#define action_by_activity_iterate(_paction_, _activity_)
Definition actions.h:489
static struct action * action_by_number(action_id act_id)
Definition actions.h:635
#define action_by_activity_iterate_end
Definition actions.h:493
#define MAX_NUM_ACTIONS
Definition actions.h:314
#define action_get_activity(_pact_)
Definition actions.h:699
#define action_id_get_activity(act_id)
Definition actions.h:701
#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:377
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:137
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Definition city.c:1456
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
#define city_tile_iterate_index(_nmap, _radius_sq, _city_tile, _tile, _index)
Definition city.h:201
#define city_tile_iterate_index_end
Definition city.h:209
#define city_list_iterate_end
Definition city.h:510
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:3580
char * incite_cost
Definition comments.c:75
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:74
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:765
bool extra_has_flag(const struct extra_type *pextra, enum extra_flag_id flag)
Definition extras.c:861
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:790
enum extra_cause activity_to_extra_cause(enum unit_activity act)
Definition extras.c:1076
bool is_extra_caused_by_action(const struct extra_type *pextra, const struct action *paction)
Definition extras.c:1056
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:203
bool is_extra_removed_by_action(const struct extra_type *pextra, const struct action *paction)
Definition extras.c:1066
enum extra_rmcause activity_to_extra_rmcause(enum unit_activity act)
Definition extras.c:1097
#define extra_type_iterate(_p)
Definition extras.h:315
#define extra_deps_iterate(_reqs, _dep)
Definition extras.h:371
#define extra_type_iterate_end
Definition extras.h:321
#define is_extra_caused_by(e, c)
Definition extras.h:203
#define extra_deps_iterate_end
Definition extras.h:379
#define extra_road_get(_e_)
Definition extras.h:191
#define extra_type_by_cause_iterate_end
Definition extras.h:339
#define extra_type_by_cause_iterate(_cause, _extra)
Definition extras.h:333
float adv_want
Definition fc_types.h:1354
adv_unit_task
Definition fc_types.h:370
@ AUT_NONE
Definition fc_types.h:370
@ AUT_AUTO_SETTLER
Definition fc_types.h:370
int action_id
Definition fc_types.h:389
#define EC_NONE
Definition fc_types.h:1113
#define ERM_NONE
Definition fc_types.h:1136
#define ADV_WANT_PRINTF
Definition fc_types.h:1355
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
struct city * owner
Definition citydlg.c:226
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:419
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:940
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:630
struct terrain_misc terrain_control
Definition map.c:69
#define current_topo_has_flag(flag)
Definition map.h:48
#define MAP_INDEX_SIZE
Definition map.h:137
#define whole_map_iterate(_map, _tile)
Definition map.h:545
#define whole_map_iterate_end
Definition map.h:554
#define index_to_map_pos(pmap_x, pmap_y, mindex)
Definition map.h:233
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#define SINGLE_MOVE
Definition movement.h:26
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
struct nation_type * nation_of_player(const struct player *pplayer)
Definition nation.c:444
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:1229
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1405
#define is_ai(plr)
Definition player.h:230
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:155
#define road_deps_iterate_end
Definition road.h:164
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
struct sprite int int y
Definition sprite_g.h:31
struct sprite int x
Definition sprite_g.h:31
static int recursion[AIT_LAST]
Definition srv_log.c:45
#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
Definition city.h:320
struct packet_game_info info
Definition game.h:89
struct requirement_vector reqs
Definition extras.h:106
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:119
int frost
Definition player.h:119
struct city_list * cities
Definition player.h:279
struct player_ai ai_common
Definition player.h:286
struct unit_list * units
Definition player.h:280
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
Definition timing.c:81
enum adv_unit_task task
Definition unit.h:89
bool worker
Definition unittype.h:575
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
struct unit::@81::@84 server
struct extra_type * activity_target
Definition unit.h:164
struct unit_adv * adv
Definition unit.h:236
struct tile * goto_tile
Definition unit.h:155
enum server_side_agent ssa_controller
Definition unit.h:172
struct tile * ptile
Definition workertask.h:22
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:699
bool tile_has_road(const struct tile *ptile, const struct road_type *proad)
Definition tile.c:844
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:88
#define tile_worked(_tile)
Definition tile.h:114
known_type
Definition tile.h:35
#define tile_terrain(_tile)
Definition tile.h:110
#define TILE_XY(ptile)
Definition tile.h:43
#define tile_has_extra(ptile, pextra)
Definition tile.h:147
#define tile_owner(_tile)
Definition tile.h:96
bool timer_in_use(struct timer *t)
Definition timing.c:242
void timer_destroy(struct timer *t)
Definition timing.c:208
void timer_start(struct timer *t)
Definition timing.c:264
double timer_read_seconds(struct timer *t)
Definition timing.c:384
struct timer * timer_renew(struct timer *t, enum timer_timetype type, enum timer_use use, const char *name)
Definition timing.c:180
#define TIMER_DEBUG
Definition timing.h:61
@ TIMER_CPU
Definition timing.h:41
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:542
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1088
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2641
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:614
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:388
bool is_guard_unit(const struct unit *punit)
Definition unit.c:340
bool unit_has_orders(const struct unit *punit)
Definition unit.c:202
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:566
#define unit_tile(_pu)
Definition unit.h:397
#define CHECK_UNIT(punit)
Definition unit.h:270
#define unit_owner(_pu)
Definition unit.h:396
#define unit_home(_pu_)
Definition unit.h:394
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Definition unithand.c:6471
bool unit_activity_handling_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type **new_target)
Definition unithand.c:6538
#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:2722
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:1587
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:190
#define worker_task_list_iterate(tasklist, ptask)
Definition workertask.h:33
#define worker_task_list_iterate_end
Definition workertask.h:35