Freeciv-3.3
Loading...
Searching...
No Matches
daiunit.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
20/* utility */
21#include "bitvector.h"
22#include "log.h"
23#include "mem.h"
24#include "nation.h"
25#include "rand.h"
26#include "registry.h"
27#include "shared.h"
28#include "timing.h"
29
30/* common */
31#include "city.h"
32#include "combat.h"
33#include "game.h"
34#include "government.h"
35#include "map.h"
36#include "movement.h"
37#include "packets.h"
38#include "specialist.h"
39#include "traderoutes.h"
40#include "unit.h"
41#include "unitlist.h"
42
43/* common/aicore */
44#include "caravan.h"
45#include "pf_tools.h"
46
47/* server */
48#include "barbarian.h"
49#include "citytools.h"
50#include "cityturn.h"
51#include "diplomats.h"
52#include "maphand.h"
53#include "srv_log.h"
54#include "unithand.h"
55#include "unittools.h"
56
57/* server/advisors */
58#include "advbuilding.h"
59#include "advgoto.h"
60#include "advtools.h"
61#include "autoexplorer.h"
62#include "autoworkers.h"
63
64/* ai */
65#include "difficulty.h"
66#include "handicaps.h"
67
68/* ai/default */
69#include "aidiplomat.h"
70#include "aiferry.h"
71#include "aiguard.h"
72#include "aihand.h"
73#include "daiair.h"
74#include "daicity.h"
75#include "daidata.h"
76#include "daieffects.h"
77#include "daihunter.h"
78#include "dailog.h"
79#include "daimilitary.h"
80#include "daiparadrop.h"
81#include "daiplayer.h"
82#include "daitools.h"
83
84#include "daiunit.h"
85
86
87#define LOGLEVEL_RECOVERY LOG_DEBUG
88#define LOG_CARAVAN LOG_DEBUG
89#define LOG_CARAVAN2 LOG_DEBUG
90#define LOG_CARAVAN3 LOG_DEBUG
91
92static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit);
93static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer,
94 struct unit *punit);
95static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
96 struct unit *punit);
97static void dai_manage_barbarian_leader(struct ai_type *ait,
98 struct player *pplayer,
99 struct unit *leader);
100
101static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap,
102 struct player *pplayer, struct unit *punit);
103static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
104 struct unit *punit);
105static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
106 struct unit *punit);
107
108static bool unit_role_defender(const struct unit_type *punittype);
109static int unit_def_rating_squared(const struct unit *punit,
110 const struct unit *pdef);
111
112/*
113 * Cached values. Updated by update_simple_ai_types.
114 *
115 * This a hack to enable emulation of old loops previously hardwired
116 * as
117 * for (i = U_WARRIORS; i <= U_BATTLESHIP; i++)
118 *
119 * (Could probably just adjust the loops themselves fairly simply,
120 * but this is safer for regression testing.)
121 *
122 * Not dealing with planes yet.
123 *
124 * Terminated by NULL.
125 */
127
128/**********************************************************************/
138static struct city *find_neediest_airlift_city(struct ai_type *ait,
139 const struct player *pplayer)
140{
141 struct city *neediest_city = NULL;
142 int most_danger = 0;
143 int most_urgent = 0;
144
145 city_list_iterate(pplayer->cities, pcity) {
146 struct ai_city *city_data = def_ai_city_data(pcity, ait);
147
148 if (pcity->airlift) {
149 if (city_data->urgency > most_urgent) {
150 most_urgent = city_data->urgency;
152 } else if (0 == most_urgent /* urgency trumps danger */
153 && city_data->danger > most_danger) {
154 most_danger = city_data->danger;
156 }
157 }
159
160 return neediest_city;
161}
162
163/**********************************************************************/
169static void dai_airlift(struct ai_type *ait, struct player *pplayer)
170{
171 struct city *most_needed;
172 int comparison;
173 struct unit *transported;
174 const struct civ_map *nmap = &(wld.map);
175
176 do {
178 comparison = 0;
179 transported = NULL;
180
181 if (!most_needed) {
182 return;
183 }
184
185 unit_list_iterate(pplayer->units, punit) {
186 struct tile *ptile = (unit_tile(punit));
187 struct city *pcity = tile_city(ptile);
188
189 if (pcity) {
190 struct ai_city *city_data = def_ai_city_data(pcity, ait);
191 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
192 const struct unit_type *ptype = unit_type_get(punit);
193
194 if (city_data->urgency == 0
197 && DEFENSE_POWER(ptype) > 2
198 && (unit_data->task == AIUNIT_NONE
199 || unit_data->task == AIUNIT_DEFEND_HOME)
200 && IS_ATTACKER(ptype)) {
201 comparison = city_data->danger;
202 transported = punit;
203 }
204 }
206 if (!transported) {
207 return;
208 }
209 UNIT_LOG(LOG_DEBUG, transported, "airlifted to defend %s",
211 unit_do_action(pplayer, transported->id, most_needed->id,
212 0, "", ACTION_AIRLIFT);
213 } while (TRUE);
214}
215
216/**********************************************************************/
222static bool has_defense(struct city *pcity)
223{
224 struct tile *ptile = city_tile(pcity);
225
226 unit_list_iterate(ptile->units, punit) {
228 && punit->hp != 0) {
230
231 if (pclass->non_native_def_pct > 0
232 || is_native_tile_to_class(pclass, ptile)) {
233 return TRUE;
234 }
235 }
236 }
238
239 return FALSE;
240}
241
242/**********************************************************************/
251{
252 return 2 * utype_build_shield_cost_base(punittype) * punittype->attack_strength /
253 (punittype->attack_strength + punittype->defense_strength);
254}
255
256/**********************************************************************/
259static int unit_att_rating_now(const struct unit *punit)
260{
263}
264
265/**********************************************************************/
268static int unit_att_rating_squared(const struct unit *punit)
269{
270 int v = adv_unit_att_rating(punit);
271
272 return v * v;
273}
274
275/**********************************************************************/
278static int unit_def_rating(const struct unit *attacker,
279 const struct unit *defender)
280{
281 const struct unit_type *def_type = unit_type_get(defender);
282
283 return (get_total_defense_power(attacker, defender)
284 * (attacker->id != 0 ? defender->hp : def_type->hp)
285 * def_type->firepower / POWER_DIVIDER);
286}
287
288/**********************************************************************/
291static int unit_def_rating_squared(const struct unit *attacker,
292 const struct unit *defender)
293{
294 int v = unit_def_rating(attacker, defender);
295
296 return v * v;
297}
298
299/**********************************************************************/
305 const struct unit_type *def_type,
306 struct player *def_player,
307 struct tile *ptile, bool fortified,
308 int veteran)
309{
310 struct civ_map *nmap = &(wld.map);
312 ptile, fortified, veteran)
313 * def_type->hp * def_type->firepower / POWER_DIVIDER;
314
315 return v * v;
316}
317
318/**********************************************************************/
343 int victim_count)
344{
346
347 /* attractiveness danger */
350
351 return desire;
352}
353
354/**********************************************************************/
366static int avg_benefit(int benefit, int loss, double chance)
367{
368 return (int)(((benefit + loss) * chance - loss) * SHIELD_WEIGHTING);
369}
370
371/**********************************************************************/
376 struct tile *ptile0,
377 int *value, int *cost)
378{
379 *cost = 0;
380 *value = 0;
381 square_iterate(&(wld.map), ptile0, 1, ptile) {
382 unit_list_iterate(ptile->units, aunit) {
383 if (aunit != punit
385 int val = adv_unit_att_rating(aunit);
386
387 if (val != 0) {
388 *value += val;
390 }
391 }
394}
395
396/**********************************************************************/
400static bool is_my_turn(struct unit *punit, struct unit *pdef)
401{
402 int val = unit_att_rating_now(punit);
403 int cur, d;
404 const struct unit_type *def_type = unit_type_get(pdef);
405 struct tile *def_tile = unit_tile(pdef);
406 struct civ_map *nmap = &(wld.map);
407
409
410 square_iterate(nmap, def_tile, 1, ptile) {
411 unit_list_iterate(ptile->units, aunit) {
412 if (aunit == punit || unit_owner(aunit) != unit_owner(punit)) {
413 continue;
414 }
416 != ATT_OK)
418 pdef, def_tile)
419 != ATT_OK)) {
420 continue;
421 }
424 FALSE, 0);
425 if (d == 0) {
426 return TRUE; /* Thanks, Markus -- Syela */
427 }
431 FALSE, 0) / d;
432 if (cur > val && ai_fuzzy(unit_owner(punit), TRUE)) {
433 return FALSE;
434 }
437
438 return TRUE;
439}
440
441/**********************************************************************/
446 struct unit *punit,
447 struct tile *ptile,
448 enum action_target_kind *kind)
449{
450 enum gen_action selected;
451 struct city *pcity;
452
454 punit, ptile))
455 != ACTION_NONE
457 punit, ptile))
458 != ACTION_NONE
460 punit, ptile))
461 != ACTION_NONE
463 punit, ptile))
464 != ACTION_NONE) {
465 if (kind != nullptr) {
466 *kind = ATK_STACK;
467 }
468
469 return selected;
470 }
471
473 punit, ptile))
474 != ACTION_NONE) {
475 if (kind != nullptr) {
476 *kind = ATK_TILE;
477 }
478
479 return selected;
480 }
481
482 pcity = tile_city(ptile);
483 if (pcity != nullptr) {
485 punit, pcity))
486 != ACTION_NONE) {
487 if (kind != nullptr) {
488 *kind = ATK_CITY;
489 }
490
491 return selected;
492 }
493 }
494
496 punit, ptile))
497 != ACTION_NONE) {
498 if (kind != nullptr) {
499 *kind = ATK_STACK;
500 }
501
502 return selected;
503 }
504
505 return ACTION_NONE;
506}
507
508/**********************************************************************/
524static int dai_rampage_want(struct unit *punit, struct tile *ptile)
525{
526 struct player *pplayer = unit_owner(punit);
527 struct unit *pdef;
528 struct civ_map *nmap = &(wld.map);
529
531
532 if (can_unit_attack_tile(punit, NULL, ptile)
533 && (pdef = get_defender(nmap, punit, ptile, NULL))
534 /* Action enablers might prevent attacking */
535 && dai_select_tile_attack_action(nmap, punit, ptile, nullptr) != ACTION_NONE) {
536 /* See description of kill_desire() about these variables. */
540
541 attack *= attack;
542
543 /* If the victim is in the city/fortress, we correct the benefit
544 * with our health because there could be reprisal attacks. We
545 * shouldn't send already injured units to useless suicide.
546 * Note that we do not specially encourage attacks against
547 * cities: rampage is a hit-n-run operation. */
548 if (!is_stack_vulnerable(ptile)
549 && unit_list_size(ptile->units) > 1) {
551 }
552
553 /* If we have non-zero attack rating... */
554 if (attack > 0 && is_my_turn(punit, pdef)) {
557
558 /* No need to amortize, our operation takes one turn. */
559 UNIT_LOG(LOG_DEBUG, punit, "Rampage: Desire %d to kill %s(%d,%d)",
560 desire,
563
564 return MAX(0, desire);
565 }
566 } else if (0 == unit_list_size(ptile->units)) {
567 /* No defender. */
568 struct city *pcity = tile_city(ptile);
569
570 /* ...and free foreign city waiting for us. Who would resist! */
571 if (NULL != pcity
572 && pplayers_at_war(pplayer, city_owner(pcity))
575 }
576
577 /* ...or tiny pleasant hut here! */
578 /* FIXME: unhardcode and variate the desire to enter a hut. */
579 if (unit_can_enter_hut(punit, ptile) && !is_barbarian(pplayer)
580 && is_native_tile(unit_type_get(punit), ptile)) {
581 return -RAMPAGE_HUT_OR_BETTER;
582 }
583 }
584
585 return 0;
586}
587
588/**********************************************************************/
591static struct pf_path *find_rampage_target(struct unit *punit,
592 int thresh_adj, int thresh_move)
593{
594 struct pf_map *tgt_map;
595 struct pf_path *path = NULL;
596 struct pf_parameter parameter;
597 /* Coordinates of the best target (initialize to silence compiler) */
598 struct tile *ptile = unit_tile(punit);
599 /* Want of the best target */
600 int max_want = 0;
601 struct player *pplayer = unit_owner(punit);
602 const struct civ_map *nmap = &(wld.map);
603
605 parameter.omniscience = !has_handicap(pplayer, H_MAP);
606 /* When trying to find rampage targets we ignore risks such as
607 * enemy units because we are looking for trouble!
608 * Hence no call ai_avoid_risks()
609 */
610
611 tgt_map = pf_map_new(&parameter);
613 int want;
614 bool move_needed;
615 int thresh;
616
617 if (move_cost > punit->moves_left) {
618 /* This is too far */
619 break;
620 }
621
622 if (has_handicap(pplayer, H_TARGETS)
623 && !map_is_known_and_seen(iter_tile, pplayer, V_MAIN)) {
624 /* The target is under fog of war */
625 continue;
626 }
627
629
630 /* Negative want means move needed even though the tiles are adjacent */
632 || want < 0);
633 /* Select the relevant threshold */
635 want = (want < 0 ? -want : want);
636
637 if (want > max_want && want > thresh) {
638 /* The new want exceeds both the previous maximum
639 * and the relevant threshold, so it's worth recording */
640 max_want = want;
641 ptile = iter_tile;
642 }
644
645 if (max_want > 0) {
646 /* We found something */
647 path = pf_map_path(tgt_map, ptile);
648 fc_assert(path != NULL);
649 }
650
652
653 return path;
654}
655
656/**********************************************************************/
671 int thresh_move)
672{
673 int count = punit->moves_left + 1; /* break any infinite loops */
674 struct pf_path *path = NULL;
675
678
680 /* This teaches the AI about the dangers inherent in occupychance. */
682
683 while (count-- > 0 && punit->moves_left > 0
685 if (!adv_unit_execute_path(punit, path)) {
686 /* Died */
687 count = -1;
688 }
689 pf_path_destroy(path);
690 path = NULL;
691 }
692
693 fc_assert(NULL == path);
694
696 return (count >= 0);
697}
698
699/**********************************************************************/
703static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer,
704 struct unit *punit)
705{
706 struct unit *aunit = aiguard_charge_unit(ait, punit);
707 struct city *acity = aiguard_charge_city(ait, punit);
708 struct tile *ptile;
709
711 CHECK_GUARD(ait, punit);
712
713 if (aunit && unit_owner(aunit) == unit_owner(punit)) {
714 /* protect a unit */
715 if (aunit->goto_tile != NULL) {
716 /* Our charge is going somewhere: maybe we should meet them there */
717 /* FIXME: This probably isn't the best algorithm for this. */
719 int me2goal = real_map_distance(unit_tile(punit), aunit->goto_tile);
720 int them2goal = real_map_distance(unit_tile(aunit), aunit->goto_tile);
721
722 if (me2goal < me2them
726 ptile = aunit->goto_tile;
727 } else {
728 ptile = unit_tile(aunit);
729 }
730 } else {
731 ptile = unit_tile(aunit);
732 }
733 } else if (acity && city_owner(acity) == unit_owner(punit)) {
734 /* protect a city */
735 ptile = acity->tile;
736 } else {
737 /* should be impossible */
738 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "we lost our charge");
740 return;
741 }
742
743 if (same_pos(unit_tile(punit), ptile)) {
744 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "at RV");
745 } else {
746 if (goto_is_sane(punit, ptile)) {
747 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "meeting charge");
748 if (!dai_gothere(ait, pplayer, punit, ptile)) {
749 /* We died */
750 return;
751 }
752 } else {
753 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "can not meet charge");
755 }
756 }
757 /* We might have stopped because of an enemy nearby.
758 * Perhaps we can kill it.*/
761 && same_pos(unit_tile(punit), ptile)) {
762 def_ai_unit_data(punit, ait)->done = TRUE; /* Stay with charge */
763 }
764}
765
766/**********************************************************************/
769static bool unit_role_defender(const struct unit_type *punittype)
770{
772}
773
774/**********************************************************************/
784adv_want look_for_charge(struct ai_type *ait, const struct civ_map *nmap,
785 struct player *pplayer, struct unit *punit,
786 struct unit **aunit, struct city **acity)
787{
788 struct pf_parameter parameter;
789 struct pf_map *pfm;
790 struct city *pcity;
791 struct ai_city *data, *best_data = NULL;
793 int def, best_def = -1;
794 /* Arbitrary: 3 turns. */
795 const int max_move_cost = 3 * unit_move_rate(punit);
796
797 *aunit = NULL;
798 *acity = NULL;
799
800 if (0 == toughness) {
801 /* useless */
802 return 0;
803 }
804
805 pft_fill_unit_parameter(&parameter, nmap, punit);
806 parameter.omniscience = !has_handicap(pplayer, H_MAP);
807 pfm = pf_map_new(&parameter);
808
809 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
810 if (move_cost > max_move_cost) {
811 /* Consider too far. */
812 break;
813 }
814
815 pcity = tile_city(ptile);
816
817 /* Consider unit bodyguard. */
818 unit_list_iterate(ptile->units, buddy) {
819 const struct unit_type *ptype = unit_type_get(punit);
820 const struct unit_type *buddy_type = unit_type_get(buddy);
821
822 /* TODO: allied unit bodyguard? */
824 || unit_owner(buddy) != pplayer
825 || !aiguard_wanted(ait, buddy)
831
832 continue;
833 }
834
836 if (0 >= def) {
837 continue;
838 }
839
841 /* Reduce want based on move cost. We can't do this for
842 * transports since they move around all the time, leading
843 * to hilarious flip-flops. */
844 def >>= move_cost / (2 * unit_move_rate(punit));
845 }
846 if (def > best_def) {
847 *aunit = buddy;
848 *acity = NULL;
849 best_def = def;
850 }
852
853 /* City bodyguard. TODO: allied city bodyguard? */
854 if (ai_fuzzy(pplayer, TRUE)
855 && NULL != pcity
856 && city_owner(pcity) == pplayer
857 && (data = def_ai_city_data(pcity, ait))
858 && 0 < data->urgency) {
859 if (NULL != best_data
860 && (0 < best_data->grave_danger
861 || best_data->urgency > data->urgency
862 || ((best_data->danger > data->danger
863 || AIUNIT_DEFEND_HOME == def_ai_unit_data(punit, ait)->task)
864 && 0 == data->grave_danger))) {
865 /* Chances are we'll be between cities when we are needed the most!
866 * Resuming pf_map_move_costs_iterate()... */
867 continue;
868 }
869 def = (data->danger - assess_defense_quadratic(ait, pcity));
870 if (def <= 0) {
871 continue;
872 }
873 /* Reduce want based on move cost. */
874 def >>= move_cost / (2 * unit_move_rate(punit));
875 if (def > best_def && ai_fuzzy(pplayer, TRUE)) {
876 *acity = pcity;
877 *aunit = NULL;
878 best_def = def;
879 best_data = data;
880 }
881 }
883
885
886 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "%s(), best_def=%d, type=%s (%d, %d)",
889 : (NULL != *aunit ? unit_rule_name(*aunit) : "")),
891 : (NULL != *aunit ?
894 : (NULL != *aunit ?
896
897 return ((best_def * 100) / toughness);
898}
899
900/**********************************************************************/
904 const struct unit_type *followee,
905 struct ai_type *ait)
906{
907 struct unit_type_ai *utai = utype_ai_data(follower, ait);
908
909 unit_type_list_iterate(utai->potential_charges, pcharge) {
910 if (pcharge == followee) {
911 return TRUE;
912 }
914
915 return FALSE;
916}
917
918/**********************************************************************/
921static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap,
922 struct player *pplayer, struct unit *punit)
923{
924 const struct unit_type *punittype = unit_type_get(punit);
925 struct unit_ai *unit_data;
926
928
929 /* Keep barbarians aggressive and primitive */
930 if (is_barbarian(pplayer)) {
931 if (is_land_barbarian(pplayer)) {
933
935 /* Land barbarians pillage */
937 }
938 }
940
941 return;
942 }
943
945
946 /* If I am a bodyguard, check whether I can do my job. */
947 if (unit_data->task == AIUNIT_ESCORT
948 || unit_data->task == AIUNIT_DEFEND_HOME) {
950 }
951
952 if (aiguard_has_charge(ait, punit)
953 && unit_data->task == AIUNIT_ESCORT) {
954 struct unit *aunit = aiguard_charge_unit(ait, punit);
955 struct city *acity = aiguard_charge_city(ait, punit);
956 struct ai_city *city_data = NULL;
957
958 if (acity != NULL) {
960 }
961
962 /* Check if the city we are on our way to rescue is still in danger,
963 * or the unit we should protect is still alive... */
964 if ((aunit && (aiguard_has_guard(ait, aunit) || aiguard_wanted(ait, aunit))
967 && city_data->urgency != 0
968 && city_data->danger > assess_defense_quadratic(ait, acity))) {
969 return; /* Yep! */
970 } else {
971 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL); /* Nope! */
972 }
973 }
974
975 /* Is the unit badly damaged? */
976 if ((unit_data->task == AIUNIT_RECOVER
977 && punit->hp < punittype->hp)
978 || punit->hp < punittype->hp * 0.25) { /* WAG */
979 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "set to hp recovery");
981 return;
982 }
983
986 /* This is a defending unit that doesn't need to stay put.
987 * It needs to defend something, but not necessarily where it's at.
988 * Therefore, it will consider becoming a bodyguard. -- Syela */
989 struct city *acity;
990 struct unit *aunit;
991
992 look_for_charge(ait, nmap, pplayer, punit, &aunit, &acity);
993 if (acity) {
996 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend city");
997 } else if (aunit) {
1000 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend unit");
1001 }
1002 }
1004}
1005
1006/**********************************************************************/
1015static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
1016 struct unit *punit)
1017{
1018 struct city *pcity = aiguard_charge_city(ait, punit);
1019
1021
1022 if (!pcity || city_owner(pcity) != pplayer) {
1024 /* Do not stay defending an allied city forever */
1026 }
1027
1028 if (!pcity) {
1029 /* Try to find a place to rest. Sitting duck out in the wilderness
1030 * is generally a bad idea, since we protect no cities that way, and
1031 * it looks silly. */
1035 }
1036
1037 if (!pcity) {
1039 }
1040
1043 /* ... we survived */
1044 if (pcity) {
1045 UNIT_LOG(LOG_DEBUG, punit, "go to defend %s", city_name_get(pcity));
1046 if (same_pos(unit_tile(punit), pcity->tile)) {
1047 UNIT_LOG(LOG_DEBUG, punit, "go defend successful");
1049 } else {
1050 (void) dai_gothere(ait, pplayer, punit, pcity->tile);
1051 }
1052 } else {
1053 UNIT_LOG(LOG_VERBOSE, punit, "defending nothing...?");
1054 }
1055 }
1056}
1057
1058/**********************************************************************/
1062 const struct unit_type *utype,
1063 int which)
1064{
1065 int attacks;
1066
1067 if (utype_action_takes_all_mp(utype,
1069 attacks = 1;
1070 } else {
1071 attacks = utype->move_rate;
1072 }
1073 city_data->invasion.attack += attacks;
1074 if (which == INVASION_OCCUPY) {
1075 city_data->invasion.occupy++;
1076 }
1077}
1078
1079/**********************************************************************/
1089static bool invasion_funct(struct ai_type *ait, struct unit *punit,
1090 bool dest, int radius, int which)
1091{
1092 struct tile *ptile;
1093 struct player *pplayer = unit_owner(punit);
1094
1096
1097 if (dest) {
1099
1100 ptile = punit->goto_tile;
1101 } else {
1102 ptile = unit_tile(punit);
1103 }
1104
1105 square_iterate(&(wld.map), ptile, radius, tile1) {
1106 struct city *pcity = tile_city(tile1);
1107
1108 if (pcity
1110 && (dest || !has_defense(pcity))) {
1111 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1112
1113 /* Unit itself */
1115
1116 /* Cargo */
1117 unit_cargo_iterate(punit, cargo) {
1118 const struct unit_type *utype = unit_type_get(cargo);
1119
1120 if (IS_ATTACKER(utype)) {
1124 }
1126 }
1128
1129 return TRUE;
1130}
1131
1132/**********************************************************************/
1135bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map,
1136 struct tile *dest_tile,
1137 const struct unit_type *cargo_type,
1138 const struct unit_type *ferry_type,
1139 struct tile **ferry_dest, struct tile **beachhead_tile)
1140{
1141 if (NULL == tile_city(dest_tile)
1143 /* Unit can directly go to 'dest_tile'. */
1144 struct tile *best_tile = NULL;
1146
1147 if (NULL != beachhead_tile) {
1148 *beachhead_tile = dest_tile;
1149 }
1150
1151 adjc_iterate(&(wld.map), dest_tile, ptile) {
1153 if (cost != PF_IMPOSSIBLE_MC
1154 && (NULL == best_tile || cost < best_cost)) {
1155 best_tile = ptile;
1156 best_cost = cost;
1157 }
1159
1160 if (NULL != ferry_dest) {
1162 }
1163
1164 return (PF_IMPOSSIBLE_MC != best_cost);
1165 } else {
1166 /* We need to find a beach around 'dest_tile'. */
1167 struct tile *best_tile = NULL, *best_beach = NULL;
1171
1172 tile_list_append(checked_tiles, dest_tile);
1173 adjc_iterate(&(wld.map), dest_tile, beach) {
1175 /* Can land there. */
1176 adjc_iterate(&(wld.map), beach, ptile) {
1177 if (!tile_list_search(checked_tiles, ptile)
1178 && !is_non_allied_unit_tile(ptile, pplayer,
1179 flagless_ferry)) {
1182 if (cost != PF_IMPOSSIBLE_MC
1183 && (NULL == best_tile || cost < best_cost)) {
1184 best_beach = beach;
1185 best_tile = ptile;
1186 best_cost = cost;
1187 }
1188 }
1190 }
1192
1194
1195 if (NULL != beachhead_tile) {
1197 }
1198 if (NULL != ferry_dest) {
1200 }
1201 return (PF_IMPOSSIBLE_MC != best_cost);
1202 }
1203}
1204
1205/**********************************************************************/
1213adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
1214 struct unit *punit,
1215 struct tile **pdest_tile, struct pf_path **ppath,
1216 struct pf_map **pferrymap,
1217 struct unit **pferryboat,
1218 const struct unit_type **pboattype, int *pmove_time)
1219{
1220 const int attack_value = adv_unit_att_rating(punit); /* basic attack. */
1221 struct pf_parameter parameter;
1222 struct pf_map *punit_map, *ferry_map;
1223 struct pf_position pos;
1225 const struct unit_type *punit_type = unit_type_get(punit);
1226 struct tile *punit_tile = unit_tile(punit);
1227 /* Type of our boat (a future one if ferryboat == NULL). */
1228 const struct unit_type *boattype = NULL;
1229 struct unit *ferryboat = NULL;
1230 struct city *pcity;
1231 struct ai_city *acity_data;
1232 int bcost, bcost_bal; /* Build cost of the attacker (+adjustments). */
1233 bool handicap = has_handicap(pplayer, H_TARGETS);
1234 bool unhap = FALSE; /* Do we make unhappy citizen. */
1235 bool harbor = FALSE; /* Do we have access to sea? */
1236 bool go_by_boat; /* Whether we need a boat or not. */
1237 int vulnerability; /* Enemy defense rating. */
1238 adv_want benefit; /* Benefit from killing the target. */
1239 struct unit *pdefender; /* Enemy city defender. */
1240 int move_time; /* Turns needed to target. */
1241 int reserves;
1242 int attack; /* Our total attack value with reinforcements. */
1243 int victim_count; /* Number of enemies there. */
1244 int needferry; /* Cost of building a ferry boat. */
1245 /* This is a kluge, because if we don't set x and y with !punit->id,
1246 * p_a_w isn't called, and we end up not wanting ironclads and therefore
1247 * never learning steam engine, even though ironclads would be very
1248 * useful. -- Syela */
1249 adv_want bk = 0;
1250 adv_want want; /* Want (amortized) of the operation. */
1251 adv_want best = 0; /* Best of all wants. */
1252 struct tile *goto_dest_tile = NULL;
1253 bool can_occupy;
1254 struct civ_map *nmap = &(wld.map);
1255
1256 /* Very preliminary checks. */
1258 if (NULL != pferrymap) {
1259 *pferrymap = NULL;
1260 }
1261 if (NULL != pferryboat) {
1262 *pferryboat = NULL;
1263 }
1264 if (NULL != pboattype) {
1265 *pboattype = NULL;
1266 }
1267 if (NULL != pmove_time) {
1268 *pmove_time = 0;
1269 }
1270 if (NULL != ppath) {
1271 *ppath = NULL;
1272 }
1273
1274 if (0 == attack_value) {
1275 /* A very poor attacker... probably low on HP. */
1276 return 0;
1277 }
1278
1280
1281
1282 /*** Part 1: Calculate targets ***/
1283
1284 /* This horrible piece of code attempts to calculate the attractiveness of
1285 * enemy cities as targets for our units, by checking how many units are
1286 * going towards it or are near it already. */
1287
1288 /* Reset enemy cities data. */
1290 /* See comment below in next usage of POTENTIALLY_HOSTILE_PLAYER. */
1291 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1292 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1293 continue;
1294 }
1295 city_list_iterate(aplayer->cities, acity) {
1296 struct ai_city *city_data = def_ai_city_data(acity, ait);
1297
1299 &city_data->attack,
1300 &city_data->bcost);
1301 city_data->invasion.attack = 0;
1302 city_data->invasion.occupy = 0;
1305
1306 /* Second, calculate in units on their way there, and mark targets for
1307 * invasion */
1308 unit_list_iterate(pplayer->units, aunit) {
1309 const struct unit_type *atype;
1310
1311 if (aunit == punit) {
1312 continue;
1313 }
1314
1316
1317 /* Dealing with invasion stuff */
1318 if (IS_ATTACKER(atype)) {
1319 if (aunit->activity == ACTIVITY_GOTO) {
1320 if (invasion_funct(ait, aunit, TRUE, 0,
1323 && (pcity = tile_city(aunit->goto_tile))) {
1324 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1325
1328 }
1329 }
1333 } else if (def_ai_unit_data(aunit, ait)->passenger != 0
1335 /* It's a transport with reinforcements */
1336 if (aunit->activity == ACTIVITY_GOTO) {
1338 }
1340 }
1342 /* end horrible initialization subroutine */
1343
1344
1345 /*** Part 2: Now pick one target ***/
1346
1347 /* We first iterate through all cities, then all units, looking
1348 * for a viable target. We also try to gang up on the target
1349 * to avoid spreading out attacks too widely to be inefficient.
1350 */
1351
1353 if (NULL != pcity && (0 == punit->id || pcity->id == punit->homecity)) {
1354 /* I would have thought unhappiness should be taken into account
1355 * irrespectfully the city in which it will surface... -- GB */
1357 }
1358
1361
1363 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1364 punit_map = pf_map_new(&parameter);
1365
1366 if (MOVE_NONE == punit_class->adv.sea_move) {
1367 /* We need boat to move over sea. */
1368 ferryboat = unit_transport_get(punit);
1369
1370 /* First check if we can use the boat we are currently loaded to. */
1371 if (ferryboat == NULL || !is_boat_free(ait, ferryboat, punit, 0)) {
1372 /* No, we cannot control current boat */
1373 ferryboat = NULL;
1374 }
1375
1376 if (NULL == ferryboat) {
1377 /* Try to find new boat */
1378 ferryboat = player_unit_by_number(pplayer,
1379 aiferry_find_boat(ait, punit, 1, NULL));
1380 }
1381
1383 harbor = TRUE;
1384 }
1385 }
1386
1387 if (NULL != ferryboat) {
1388 boattype = unit_type_get(ferryboat);
1389 pft_fill_unit_overlap_param(&parameter, nmap, ferryboat);
1390 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1391 ferry_map = pf_map_new(&parameter);
1392 } else {
1394 if (NULL == boattype) {
1395 /* We pretend that we can have the simplest boat to stimulate tech. */
1397 }
1398 if (NULL != boattype && harbor) {
1399 /* Let's simulate a boat at 'punit' position. */
1401 punit_tile, pplayer);
1402 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1403 ferry_map = pf_map_new(&parameter);
1404 } else {
1405 ferry_map = NULL;
1406 }
1407 }
1408
1410
1412 /* For the virtual unit case, which is when we are called to evaluate
1413 * which units to build, we want to calculate in danger and which
1414 * players we want to make war with in the future. We do _not_ want
1415 * to do this when actually making attacks. */
1416 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1417 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1418 continue; /* Not an enemy. */
1419 }
1420
1421 city_list_iterate(aplayer->cities, acity) {
1422 struct tile *atile = city_tile(acity);
1423
1426 /* Can't attack this city. It is on land. */
1427 continue;
1428 }
1429
1430 if (handicap && !map_is_known(atile, pplayer)) {
1431 /* Can't see it */
1432 continue;
1433 }
1434
1436 go_by_boat = FALSE;
1437 move_time = pos.turn;
1438 } else if (NULL == ferry_map) {
1439 continue; /* Impossible to handle. */
1440 } else {
1441 struct tile *dest, *beach;
1442
1443 if (!find_beachhead(pplayer, ferry_map, atile, punit_type,
1444 boattype, &dest, &beach)) {
1445 continue; /* Impossible to go by boat. */
1446 }
1447 if (!pf_map_position(ferry_map, dest, &pos)) {
1449 continue;
1450 }
1451 move_time = pos.turn; /* Sailing time. */
1452 if (dest != beach) {
1453 move_time++; /* Land time. */
1454 }
1455 if (NULL != ferryboat && unit_tile(ferryboat) != punit_tile) {
1456 if (pf_map_position(punit_map, unit_tile(ferryboat), &pos)) {
1457 move_time += pos.turn; /* Time to reach the boat. */
1458 } else {
1459 continue; /* Cannot reach the boat. */
1460 }
1461 }
1462 go_by_boat = TRUE;
1463 }
1464
1469 } else {
1470 pdefender = NULL;
1471 vulnerability = 0;
1472 benefit = 0;
1473 }
1474
1475 if (1 < move_time) {
1477
1478 if (def_type) {
1482 if (v > vulnerability) {
1483 /* They can build a better defender! */
1484 vulnerability = v;
1486 }
1487 }
1488 }
1489
1491
1492 reserves = (acity_data->invasion.attack
1493 - unit_list_size(acity->tile->units));
1494
1495 if (punit->id == 0) {
1496 /* Real unit would add to reserves once built. */
1499 reserves++;
1500 } else {
1501 reserves += punit_type->move_rate;
1502 }
1503 }
1504
1505 if (0 < reserves && (can_occupy
1506 || 0 < acity_data->invasion.occupy)) {
1507 /* There are units able to occupy the city after all defenders
1508 * are killed! */
1509 benefit += acity_data->worth * reserves / 5;
1510 }
1511
1512 attack = attack_value + acity_data->attack;
1513 attack *= attack;
1514 /* Avoiding handling upkeep aggregation this way -- Syela */
1515
1516 /* AI was not sending enough reinforcements to totally wipe out a city
1517 * and conquer it in one turn.
1518 * This variable enables total carnage. -- Syela */
1520
1521 if (!can_occupy && NULL == pdefender) {
1522 /* Nothing there to bash and we can't occupy!
1523 * Not having this check caused warships yoyoing */
1524 want = 0;
1525 } else if (10 < move_time) {
1526 /* Too far! */
1527 want = 0;
1528 } else if (can_occupy && 0 == acity_data->invasion.occupy
1529 && (0 < acity_data->invasion.attack
1530 || victim_count == 0)) {
1531 /* Units able to occupy really needed there! */
1532 want = bcost * SHIELD_WEIGHTING;
1533 } else {
1534 want = kill_desire(benefit, attack, bcost + acity_data->bcost,
1536 }
1539 /* Build_cost of ferry. */
1540 needferry = (go_by_boat && NULL == ferryboat
1542 /* FIXME: add time to build the ferry? */
1544 want, MAX(1, move_time),
1546
1547 /* BEGIN STEAM-ENGINES-ARE-OUR-FRIENDS KLUGE. */
1548 if (0 >= want && 0 == punit->id && 0 >= best) {
1552 MAX(1, move_time),
1554
1555 if (bk_e > bk) {
1556 *pdest_tile = atile;
1557 if (NULL != pferrymap) {
1559 }
1560 if (NULL != pferryboat) {
1561 *pferryboat = go_by_boat ? ferryboat : NULL;
1562 }
1563 if (NULL != pboattype) {
1565 }
1566 if (NULL != pmove_time) {
1568 }
1569 goto_dest_tile = (go_by_boat && NULL != ferryboat
1570 ? unit_tile(ferryboat) : atile);
1571 bk = bk_e;
1572 }
1573 }
1574 /* END STEAM-ENGINES KLUGE */
1575
1576 if (0 != punit->id
1577 && NULL != ferryboat
1578 && punit_class->adv.sea_move == MOVE_NONE) {
1580 "%s(): with boat %s@(%d, %d) -> %s@(%d, %d)"
1581 " (go_by_boat = %d, move_time = %d, want = " ADV_WANT_PRINTF
1582 ", best = " ADV_WANT_PRINTF ")",
1583 __FUNCTION__, unit_rule_name(ferryboat),
1584 TILE_XY(unit_tile(ferryboat)), city_name_get(acity),
1585 TILE_XY(atile), go_by_boat, move_time, want, best);
1586 }
1587
1588 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1589 /* Yes, we like this target */
1590 best = want;
1591 *pdest_tile = atile;
1592 if (NULL != pferrymap) {
1594 }
1595 if (NULL != pferryboat) {
1596 *pferryboat = go_by_boat ? ferryboat : NULL;
1597 }
1598 if (NULL != pboattype) {
1600 }
1601 if (NULL != pmove_time) {
1603 }
1604 goto_dest_tile = (go_by_boat && NULL != ferryboat
1605 ? unit_tile(ferryboat) : atile);
1606 }
1608
1610 /* I'm not sure the following code is good but it seems to be adequate.
1611 * I am deliberately not adding ferryboat code to the unit_list_iterate.
1612 * -- Syela */
1614 struct tile *atile = unit_tile(aunit);
1615
1616 if (NULL != tile_city(atile)) {
1617 /* already dealt with it. */
1618 continue;
1619 }
1620
1621 if (handicap && !map_is_known(atile, pplayer)) {
1622 /* Can't see the target. */
1623 continue;
1624 }
1625
1627 && 0 == punit->id) {
1628 /* We will not build units just to chase caravans and
1629 * ambassadors. */
1630 continue;
1631 }
1632
1633 /* We have to assume the attack is diplomatically ok.
1634 * We cannot use can_player_attack_tile, because we might not
1635 * be at war with aplayer yet */
1637 || aunit != get_defender(nmap, punit, atile, NULL)) {
1638 /* We cannot attack it, or it is not the main defender. */
1639 continue;
1640 }
1641
1643 /* Cannot reach it. */
1644 continue;
1645 }
1646
1649
1650 move_time = pos.turn;
1651 if (10 < move_time) {
1652 /* Too far. */
1653 want = 0;
1654 } else {
1655 want = kill_desire(benefit, attack, bcost, vulnerability, 1);
1656 /* Take into account maintenance of the unit. */
1657 /* FIXME: Depends on the government. */
1658 want -= move_time * SHIELD_WEIGHTING;
1659 /* Take into account unhappiness
1660 * (costs 2 luxuries to compensate). */
1661 want -= (unhap ? 2 * move_time * TRADE_WEIGHTING : 0);
1662 }
1664 want, MAX(1, move_time), bcost_bal);
1665 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1666 best = want;
1667 *pdest_tile = atile;
1668 if (NULL != pferrymap) {
1669 *pferrymap = NULL;
1670 }
1671 if (NULL != pferryboat) {
1672 *pferryboat = NULL;
1673 }
1674 if (NULL != pboattype) {
1675 *pboattype = NULL;
1676 }
1677 if (NULL != pmove_time) {
1679 }
1681 }
1684
1685 if (NULL != ppath) {
1688 }
1689
1691 if (NULL != ferry_map
1692 && (NULL == pferrymap || *pferrymap != ferry_map)) {
1694 }
1695
1697
1698 return best;
1699}
1700
1701/**********************************************************************/
1710{
1711 struct pf_parameter parameter;
1712 struct pf_map *pfm;
1713 struct player *pplayer = unit_owner(punit);
1714 struct city *pcity, *best_city = NULL;
1715 int best = FC_INFINITY, cur;
1716 const struct civ_map *nmap = &(wld.map);
1717
1718 pft_fill_unit_parameter(&parameter, nmap, punit);
1719 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1720 pfm = pf_map_new(&parameter);
1721
1722 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
1723 if (move_cost > best) {
1724 /* We already found a better city. No need to continue. */
1725 break;
1726 }
1727
1728 pcity = tile_city(ptile);
1729 if (NULL == pcity || !pplayers_allied(pplayer, city_owner(pcity))) {
1730 continue;
1731 }
1732
1733 /* Score based on the move cost. */
1734 cur = move_cost;
1735
1736 /* Note the unit owner may be different from the city owner. */
1737 if (0 == get_unittype_bonus(unit_owner(punit), ptile,
1739 EFT_HP_REGEN)) {
1740 /* If we cannot regen fast our hit points here, let's make some
1741 * penalty. */
1742 cur *= 3;
1743 }
1744
1745 if (cur < best) {
1746 best_city = pcity;
1747 best = cur;
1748 }
1750
1752 return best_city;
1753}
1754
1755/**********************************************************************/
1761 struct player *pplayer,
1762 struct unit *punit)
1763{
1764 struct city *pc;
1765 bool only_continent = TRUE;
1766
1767 if (unit_transported(punit)) {
1768 /* If we are in transport, we can go to any continent.
1769 * Actually, we are not currently in a continent where to stay. */
1771 }
1772
1773 if ((pc = find_closest_city(unit_tile(punit), NULL, pplayer, FALSE,
1776 UNIT_LOG(LOG_DEBUG, punit, "Barbarian heading to conquer %s",
1777 city_name_get(pc));
1778 (void) dai_gothere(ait, pplayer, punit, pc->tile);
1779 } else {
1780 struct unit *ferry = NULL;
1781
1782 if (unit_transported(punit)) {
1783 ferry = unit_transport_get(punit);
1784
1785 /* We already are in a boat so it needs no
1786 * free capacity */
1787 if (!is_boat_free(ait, ferry, punit, 0)) {
1788 /* We cannot control our ferry. */
1789 ferry = NULL;
1790 }
1791 } else {
1792 /* We are not in a boat yet. Search for one. */
1794 if (is_boat_free(ait, aunit, punit, 1)
1796 ferry = aunit;
1797 break;
1798 }
1800 }
1801
1802 if (ferry) {
1803 UNIT_LOG(LOG_DEBUG, punit, "Barbarian sailing to conquer %s",
1804 city_name_get(pc));
1805 (void) aiferry_goto_amphibious(ait, ferry, punit, pc->tile);
1806 } else {
1807 /* This is not an error. Somebody else might be in charge
1808 * of the ferry. */
1809 UNIT_LOG(LOG_DEBUG, punit, "unable to find barbarian ferry");
1810 }
1811 }
1812 } else {
1813 UNIT_LOG(LOG_DEBUG, punit, "Barbarian find no target city");
1814 }
1815}
1816
1817/**********************************************************************/
1823static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
1824 struct unit *punit)
1825{
1826 struct tile *dest_tile;
1827 int id = punit->id;
1828 int ct = 10;
1829 struct city *pcity = NULL;
1830
1832
1833 /* Barbarians pillage, and might keep on doing that so they sometimes
1834 * even finish it. */
1835 if (punit->activity == ACTIVITY_PILLAGE && is_barbarian(pplayer)
1836 && fc_rand(2) == 1) {
1837 return;
1838 }
1839
1840 /* First find easy nearby enemies, anything better than pillage goes.
1841 * NB: We do not need to repeat dai_military_rampage, it is repeats itself
1842 * until it runs out of targets. */
1843 /* FIXME: 1. dai_military_rampage never checks if it should defend newly
1844 * conquered cities.
1845 * FIXME: 2. would be more convenient if it returned FALSE if we run out
1846 * of moves too.*/
1848 return; /* we died */
1849 }
1850
1851 if (punit->moves_left <= 0) {
1852 return;
1853 }
1854
1855 /* Main attack loop */
1856 do {
1857 struct tile *start_tile = unit_tile(punit);
1858 struct pf_path *path;
1859 struct unit *ferryboat;
1860
1861 /* Then find enemies the hard way */
1862 find_something_to_kill(ait, pplayer, punit, &dest_tile, &path,
1863 NULL, &ferryboat, NULL, NULL);
1864 if (!same_pos(unit_tile(punit), dest_tile)) {
1865 if (!is_tiles_adjacent(unit_tile(punit), dest_tile)
1866 || !can_unit_attack_tile(punit, NULL, dest_tile)) {
1867
1868 /* Adjacent and can't attack usually means we are not marines
1869 * and on a ferry. This fixes the problem (usually). */
1870 UNIT_LOG(LOG_DEBUG, punit, "mil att gothere -> (%d, %d)",
1871 TILE_XY(dest_tile));
1872
1873 /* Set ACTIVITY_GOTO more permanently than just inside
1874 * adv_follow_path(). This way other units will know we're
1875 * on our way even if we don't reach target yet. */
1876 punit->goto_tile = dest_tile;
1878 if (NULL != path && !adv_follow_path(punit, path, dest_tile)) {
1879 /* Died. */
1880 pf_path_destroy(path);
1881 return;
1882 }
1883 if (NULL != ferryboat) {
1884 /* Need a boat. */
1885 aiferry_gobyboat(ait, pplayer, punit, dest_tile, FALSE);
1886 pf_path_destroy(path);
1887 return;
1888 }
1889 if (0 >= punit->moves_left) {
1890 /* No moves left. */
1891 pf_path_destroy(path);
1892 return;
1893 }
1894
1895 /* Either we're adjacent or we sitting on the tile. We might be
1896 * sitting on the tile if the enemy that _was_ sitting there
1897 * attacked us and died _and_ we had enough movement to get there */
1898 if (same_pos(unit_tile(punit), dest_tile)) {
1899 UNIT_LOG(LOG_DEBUG, punit, "mil att made it -> (%d, %d)",
1900 TILE_XY(dest_tile));
1901 pf_path_destroy(path);
1902 break;
1903 }
1904 }
1905
1906 if (is_tiles_adjacent(unit_tile(punit), dest_tile)) {
1907 /* Close combat. fstk sometimes want us to attack an adjacent
1908 * enemy that rampage wouldn't */
1909 UNIT_LOG(LOG_DEBUG, punit, "mil att bash -> (%d, %d)",
1910 TILE_XY(dest_tile));
1911 if (!dai_unit_attack(ait, punit, dest_tile)) {
1912 /* Died */
1913 pf_path_destroy(path);
1914 return;
1915 }
1916 } else if (!same_pos(start_tile, unit_tile(punit))) {
1917 /* Got stuck. Possibly because of adjacency to an
1918 * enemy unit. Perhaps we are in luck and are now next to a
1919 * tempting target? Let's find out... */
1922 pf_path_destroy(path);
1923 return;
1924 }
1925
1926 } else {
1927 /* FIXME: This happens a bit too often! */
1928 UNIT_LOG(LOG_DEBUG, punit, "fstk didn't find us a worthy target!");
1929 /* No worthy enemies found, so abort loop */
1930 ct = 0;
1931 }
1932 pf_path_destroy(path);
1933
1934 ct--; /* infinite loops from railroads must be stopped */
1935 } while (punit->moves_left > 0 && ct > 0);
1936
1937 /* Cleanup phase */
1938 if (punit->moves_left == 0) {
1939 return;
1940 }
1942 if (pcity != NULL
1943 && (dai_is_ferry(punit, ait)
1944 || punit->hp < unit_type_get(punit)->hp * 0.50)) { /* WAG */
1945 /* Go somewhere safe */
1946 UNIT_LOG(LOG_DEBUG, punit, "heading to nearest safe house.");
1947 (void) dai_unit_goto(ait, punit, pcity->tile);
1948 } else if (!is_barbarian(pplayer)) {
1949 /* Nothing else to do, so try exploring. */
1950 switch (manage_auto_explorer(punit)) {
1951 case MR_DEATH:
1952 /* don't use punit! */
1953 return;
1954 case MR_OK:
1955 UNIT_LOG(LOG_DEBUG, punit, "nothing else to do, so exploring");
1956 break;
1957 default:
1958 UNIT_LOG(LOG_DEBUG, punit, "nothing to do - no more exploring either");
1959 break;
1960 };
1961 } else {
1962 /* You can still have some moves left here, but barbarians should
1963 not sit helplessly, but advance towards nearest known enemy city */
1964 UNIT_LOG(LOG_DEBUG, punit, "attack: barbarian");
1965 dai_military_attack_barbarian(ait, pplayer, punit);
1966 }
1967 if ((punit = game_unit_by_number(id)) && punit->moves_left > 0) {
1968 UNIT_LOG(LOG_DEBUG, punit, "attack: giving up unit to defense");
1969 dai_military_defend(ait, pplayer, punit);
1970 }
1971}
1972
1973/**********************************************************************/
1977static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
1978{
1979 bool alive = TRUE;
1980 int ferryboat = 0;
1981 struct pf_path *path_to_ferry = NULL;
1982
1983 UNIT_LOG(LOG_CARAVAN, punit, "requesting a boat!");
1984 ferryboat = aiferry_find_boat(ait, punit, 1, &path_to_ferry);
1985 /* going to meet the boat */
1986 if ((ferryboat <= 0)) {
1988 "in find_boat_for_unit cannot find any boats.");
1989 /* if we are undefended on the country side go to a city */
1991 if (current_city == NULL) {
1993 if (city_near != NULL) {
1995 }
1996 }
1997 } else {
1998 if (path_to_ferry != NULL) {
2000 /* Died. */
2003 alive = FALSE;
2004 } else {
2007 alive = TRUE;
2008 }
2009 }
2010 }
2011 return alive;
2012}
2013
2014/**********************************************************************/
2023 struct tile *ctile, struct tile *ptile)
2024{
2025 /* We usually have Inaccessible terrain, so not testing MOVE_FULL == */
2026 bool lm = MOVE_NONE != pclass->adv.land_move,
2027 sm = MOVE_NONE != pclass->adv.sea_move;
2028 struct civ_map *pmap = &(wld.map);
2029
2030 if (lm && sm) {
2031 return FALSE;
2032 }
2033
2034 /* We could use adjc_iterate() but likely often tiles are on the same
2035 * continent and it will be more time to find where they connect */
2038
2039 if (is_ocean_tile(atile) ? sm : lm) {
2040 iterate_outward(pmap, ptile, 1, btile) {
2041 if (tile_continent(btile) == acont) {
2042 return FALSE;
2043 }
2045 }
2047
2048 if (is_tiles_adjacent(ctile, ptile)) {
2049 return FALSE;
2050 }
2051
2052 return TRUE;
2053}
2054
2055/**********************************************************************/
2061static void dai_caravan_goto(struct ai_type *ait, struct player *pplayer,
2062 struct unit *punit,
2063 const struct city *dest_city,
2064 bool help_wonder,
2065 bool required_boat, bool request_boat)
2066{
2067 bool alive = TRUE;
2068 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2069 const struct civ_map *nmap = &(wld.map);
2070
2072
2073 /* if we're not there yet, and we can move, move... */
2074 if (!same_pos(dest_city->tile, unit_tile(punit))
2075 && punit->moves_left != 0) {
2076 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) task %s going to %s in %s %s",
2080 help_wonder ? "help a wonder" : "trade", city_name_get(dest_city),
2081 required_boat ? "with a boat" : "");
2082 if (required_boat) {
2083 /* to trade with boat */
2084 if (request_boat) {
2085 /* Try to find new boat */
2087 } else {
2088 /* if we are not being transported then ask for a boat again */
2089 alive = TRUE;
2092 unit_tile(punit), dest_city->tile)) {
2094 }
2095 }
2096 if (alive) {
2097 /* FIXME: sometimes we get FALSE here just because
2098 * a trireme that we've boarded can't go over an ocean. */
2099 alive = dai_gothere(ait, pplayer, punit, dest_city->tile);
2100 }
2101 } else {
2102 /* to trade without boat */
2104 }
2105 }
2106
2107 /* if moving didn't kill us, and we got to the destination, handle it. */
2108 if (alive && real_map_distance(dest_city->tile, unit_tile(punit)) <= 1) {
2109 /* release the boat! */
2110 if (unit_transported(punit)) {
2112 }
2114 punit, dest_city)) {
2115 /*
2116 * We really don't want to just drop all caravans in immediately.
2117 * Instead, we want to aggregate enough caravans to build instantly.
2118 * -AJS, 990704
2119 */
2120 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) helps build wonder in %s",
2123 punit->id,
2126 unit_do_action(pplayer, punit->id, dest_city->id,
2127 0, "", ACTION_HELP_WONDER);
2129 punit, dest_city)) {
2130 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) creates trade route in %s",
2133 punit->id,
2136 unit_do_action(pplayer, punit->id, dest_city->id,
2137 0, "", ACTION_TRADE_ROUTE);
2139 punit, dest_city)) {
2140 /* Get the one time bonus. */
2141 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) enters marketplace of %s",
2144 punit->id,
2147 unit_do_action(pplayer, punit->id, dest_city->id,
2148 0, "", ACTION_MARKETPLACE);
2149 } else {
2150 enum log_level level = LOG_NORMAL;
2151 char *help_wonder_msg = "help build wonder in";
2152 char *action_msg = "trade with nor enter marketplace in";
2153
2154 if (help_wonder) {
2155 /* A Caravan ordered to help build wonder may arrive after
2156 * enough shields to build the wonder has been produced,
2157 * or after the city changed its production. */
2160 }
2161
2163 "%s %s[%d](%d,%d) unable to %s %s",
2166 punit->id,
2168 action_msg,
2170 }
2171 }
2172}
2173
2174/**********************************************************************/
2178static void caravan_optimize_callback(const struct caravan_result *result,
2179 void *data)
2180{
2181 const struct unit *caravan = data;
2182
2183 log_base(LOG_CARAVAN3, "%s %s[%d](%d,%d) %s: %s %s worth %g",
2185 unit_rule_name(caravan),
2186 caravan->id,
2187 TILE_XY(unit_tile(caravan)),
2188 city_name_get(result->src),
2189 result->help_wonder ? "wonder in" : "trade to",
2190 city_name_get(result->dest),
2191 result->value);
2192}
2193
2194/**********************************************************************/
2198 struct unit *punit)
2199{
2200 struct tile *src = NULL, *dest = NULL, *src_home_city = NULL;
2201 struct city *phome_city = NULL;
2202 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2203
2204 if ((unit_data->task != AIUNIT_NONE)) {
2205 src = unit_tile(punit);
2207 if (phome_city != NULL) {
2209 }
2210 dest = punit->goto_tile;
2211
2212 if (src == NULL || dest == NULL) {
2213 return FALSE;
2214 }
2215
2216 /* If we have a home continent, and are not there.
2217 * (FIXME: well, why?)
2218 * (I guess because home continent is which we were supposed to leave,
2219 * not the target continent) */
2220 if (src_home_city != NULL
2222 return FALSE;
2223 }
2224
2225 if (!goto_is_sane(punit, dest)) {
2226 if (unit_transported(punit)) {
2227 /* If we're being transported */
2228 return FALSE;
2229 }
2230 if ((punit->birth_turn + 15 < game.info.turn)) {
2231 /* We are tired of waiting */
2233
2234 if (ferrys <= 0) {
2235 /* There are no ferrys available... give up */
2236 return TRUE;
2237 } else {
2238 if (punit->birth_turn + 20 < game.info.turn) {
2239 /* We are fed up! */
2240 return TRUE;
2241 }
2242 }
2243 }
2244 }
2245 }
2246
2247 return FALSE;
2248}
2249
2250/**********************************************************************/
2257 struct unit *punit)
2258{
2260 Continent_id continent;
2261
2262 fc_assert(pcity != NULL);
2263
2264 if (unit_class_get(punit)->adv.ferry_types <= 0) {
2265 /* There is just no possible transporters. */
2266 return FALSE;
2267 }
2268 continent = tile_continent(pcity->tile);
2269
2270 /* Look for proper destination city at different continent. */
2271 city_list_iterate(pplayer->cities, acity) {
2273 if (tile_continent(acity->tile) != continent) {
2274 return TRUE;
2275 }
2276 }
2278
2280 if (aplayer == pplayer || !aplayer->is_alive) {
2281 continue;
2282 }
2283 if (pplayers_allied(pplayer, aplayer)) {
2284 city_list_iterate(aplayer->cities, acity) {
2286 if (tile_continent(acity->tile) != continent) {
2287 return TRUE;
2288 }
2289 }
2292 }
2293
2294 return FALSE;
2295}
2296
2297/**********************************************************************/
2301static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
2302{
2303 struct city *nearest = NULL;
2304 int min_dist = FC_INFINITY;
2305 struct tile *current_loc = unit_tile(punit);
2307 bool alive = TRUE;
2308
2310 struct tile *ctile = city_tile(pcity);
2311
2312 if (tile_continent(ctile) == continent) {
2314
2315 if (this_dist < min_dist) {
2317 nearest = pcity;
2318 }
2319 }
2321
2322 if (nearest != NULL) {
2324 if (alive && same_pos(unit_tile(punit), nearest->tile)) {
2326 }
2327 }
2328
2329 return alive;
2330}
2331
2332/**********************************************************************/
2338static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
2339 struct unit *punit)
2340{
2341 struct caravan_parameter parameter;
2342 struct caravan_result result;
2343 const struct city *homecity;
2344 const struct city *dest = NULL;
2345 struct unit_ai *unit_data;
2347 bool expect_boats = pclass->adv.ferry_types > 0;
2348 /* TODO: will pplayer have a boat for punit in a reasonable time? */
2349 bool help_wonder = FALSE;
2350 bool required_boat = FALSE;
2351 bool request_boat = FALSE;
2353 const struct civ_map *nmap = &(wld.map);
2354
2356
2360 /* we only want units that can establish trade, enter marketplace or
2361 * help build wonders */
2362 return;
2363 }
2364
2366
2367 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) task %s to (%d,%d)",
2372
2373 homecity = game_city_by_number(punit->homecity);
2374 if (homecity == NULL && unit_data->task == AIUNIT_TRADE) {
2376 return;
2377 }
2378 homecity = game_city_by_number(punit->homecity);
2379 if (homecity == NULL) {
2380 return;
2381 }
2382 }
2383
2384 if ((unit_data->task == AIUNIT_TRADE
2385 || unit_data->task == AIUNIT_WONDER)) {
2386 /* We are moving to our destination */
2387 /* We check to see if our current goal is feasible */
2390
2391 if ((city_dest == NULL)
2393 || (unit_data->task == AIUNIT_TRADE
2394 && !(can_cities_trade(homecity, city_dest)
2396 priority)))
2397 || (unit_data->task == AIUNIT_WONDER
2398 /* Helping the (new) production is illegal. */
2400 || (unit_data->task == AIUNIT_TRADE
2405 punit, city_dest)))
2406 || (unit_data->task == AIUNIT_WONDER
2409 punit, city_dest))) {
2410 /* destination invalid! */
2412 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) destination invalid!",
2415 } else {
2416 /* destination valid, are we tired of waiting for a boat? */
2420 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) unit tired of waiting!",
2424 } else {
2425 dest = city_dest;
2426 help_wonder = (unit_data->task == AIUNIT_WONDER) ? TRUE : FALSE;
2427 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2429 }
2430 }
2431 }
2432
2433 if (unit_data->task == AIUNIT_NONE) {
2434 if (homecity == NULL) {
2435 /* FIXME: We shouldn't bother in getting homecity for
2436 * caravan that will then be used for wonder building. */
2438 return;
2439 }
2440 homecity = game_city_by_number(punit->homecity);
2441 if (homecity == NULL) {
2442 return;
2443 }
2444 }
2445
2447 /* Make more trade with allies than other peaceful nations
2448 * by considering only allies 50% of the time.
2449 * (the other 50% allies are still considered, but so are other nations) */
2450 if (fc_rand(2)) {
2451 /* Be optimistic about development of relations with no-contact and
2452 * cease-fire nations. */
2453 parameter.allow_foreign_trade = FTL_NONWAR;
2454 } else {
2455 parameter.allow_foreign_trade = FTL_ALLIED;
2456 }
2457
2460 parameter.callback_data = punit;
2461 }
2463 parameter.ignore_transit_time = TRUE;
2464 }
2467 parameter.ignore_transit_time = FALSE;
2468 }
2469 caravan_find_best_destination(nmap, punit, &parameter, &result,
2470 !has_handicap(pplayer, H_MAP));
2471 if (result.dest != NULL) {
2472 /* We did find a new destination for the unit */
2473 dest = result.dest;
2474 help_wonder = result.help_wonder;
2475 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2476 request_boat = required_boat;
2478 (help_wonder) ? AIUNIT_WONDER : AIUNIT_TRADE,
2479 dest->tile);
2480 } else {
2481 dest = NULL;
2482 }
2483 }
2484
2485 if (required_boat && !expect_boats) {
2486 /* Would require boat, but can't have them. Render destination invalid. */
2487 dest = NULL;
2488 }
2489
2490 if (dest != NULL) {
2491 dai_caravan_goto(ait, pplayer, punit, dest, help_wonder,
2492 required_boat, request_boat);
2493 return; /* that may have clobbered the unit */
2494 } else {
2495 /* We have nowhere to go! */
2496 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d), nothing to do!",
2500 /* Should we become a defensive unit? */
2501 }
2502}
2503
2504/**********************************************************************/
2509 struct unit *punit)
2510{
2511 struct player *pplayer = unit_owner(punit);
2512 struct city *pcity = tile_city(unit_tile(punit));
2513 struct city *safe = NULL;
2514 const struct unit_type *punittype = unit_type_get(punit);
2515 bool no_recovery;
2516
2518
2519 if (pcity != NULL) {
2520 /* Rest in the city until the hitpoints are recovered, but attempt
2521 * to protect city from attack (and be opportunistic too)*/
2524 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "recovering hit points.");
2525 } else {
2526 return; /* We died heroically defending our city */
2527 }
2528 } else {
2529 /* Goto to nearest city to recover hit points */
2530 /* Just before, check to see if we can occupy an undefended enemy city */
2533 return; /* Oops, we died */
2534 }
2535
2536 /* Find a city to stay and go there */
2538 if (safe) {
2539 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "going to %s to recover",
2541 if (!dai_unit_goto(ait, punit, safe->tile)) {
2542 log_base(LOGLEVEL_RECOVERY, "died trying to hide and recover");
2543 return;
2544 }
2545 } else {
2546 /* Oops */
2547 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "didn't find a city to recover in!");
2549 dai_military_attack(ait, pplayer, punit);
2550 return;
2551 }
2552 }
2553
2554 /* Is the unit still damaged? If true, and could recover hit points, do so.
2555 * Don't wait if would be losing hitpoints that way! */
2557 if (punit->hp == punittype->hp) {
2558 no_recovery = TRUE;
2559 } else {
2561
2562 if (gain < 0) {
2563 no_recovery = TRUE;
2564 } else if (gain == 0 && !punit->moved) {
2565 /* Isn't gaining even though has not moved. */
2566 no_recovery = TRUE;
2567 }
2568 }
2569 if (no_recovery) {
2570 /* We are ready to go out and kick ass again */
2571 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "ready to kick ass again!");
2573 return;
2574 } else {
2575 def_ai_unit_data(punit, ait)->done = TRUE; /* Sit tight */
2576 }
2577}
2578
2579/**********************************************************************/
2583void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap,
2584 struct player *pplayer, struct unit *punit)
2585{
2586 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2587 int id = punit->id;
2588
2590
2591 /* "Escorting" aircraft should not do anything. They are activated
2592 * by their transport or charge. We do _NOT_ set them to 'done'
2593 * since they may need be activated once our charge moves. */
2594 if (unit_data->task == AIUNIT_ESCORT
2596 return;
2597 }
2598
2601 && has_handicap(pplayer, H_AWAY)) {
2602 /* Don't move sentried or fortified units controlled by a player
2603 * in away mode. */
2604 unit_data->done = TRUE;
2605 return;
2606 }
2607
2608 /* Since military units re-evaluate their actions every turn,
2609 we must make sure that previously reserved ferry is freed. */
2611
2613 /* Try hunting with this unit */
2614 if (dai_hunter_qualify(pplayer, punit)) {
2615 int result, sanity = punit->id;
2616
2617 UNIT_LOG(LOGLEVEL_HUNT, punit, "is qualified as hunter");
2618 result = dai_hunter_manage(ait, pplayer, punit);
2621 return; /* Died */
2622 }
2623 if (result == -1) {
2624 (void) dai_hunter_manage(ait, pplayer, punit); /* More carnage */
2626 return;
2627 } else if (result >= 1) {
2629 return; /* Done moving */
2630 } else if (unit_data->task == AIUNIT_HUNTER) {
2631 /* This should be very rare */
2633 }
2634 } else if (unit_data->task == AIUNIT_HUNTER) {
2636 }
2638
2639 /* Do we have a specific job for this unit? If not, we default
2640 * to attack. */
2641 dai_military_findjob(ait, nmap, pplayer, punit);
2642
2643 switch (unit_data->task) {
2644 case AIUNIT_AUTO_WORKER:
2645 case AIUNIT_BUILD_CITY:
2646 fc_assert(FALSE); /* This is not the place for this role */
2647 break;
2648 case AIUNIT_DEFEND_HOME:
2650 dai_military_defend(ait, pplayer, punit);
2652 break;
2653 case AIUNIT_ATTACK:
2654 case AIUNIT_NONE:
2656 dai_military_attack(ait, pplayer, punit);
2658 break;
2659 case AIUNIT_ESCORT:
2661 dai_military_bodyguard(ait, pplayer, punit);
2663 break;
2664 case AIUNIT_EXPLORE:
2665 switch (manage_auto_explorer(punit)) {
2666 case MR_DEATH:
2667 /* Don't use punit! */
2668 return;
2669 case MR_OK:
2670 UNIT_LOG(LOG_DEBUG, punit, "more exploring");
2671 break;
2672 default:
2673 UNIT_LOG(LOG_DEBUG, punit, "no more exploring either");
2674 break;
2675 };
2676 def_ai_unit_data(punit, ait)->done = (punit->moves_left <= 0);
2677 break;
2678 case AIUNIT_RECOVER:
2682 break;
2683 case AIUNIT_HUNTER:
2684 fc_assert(FALSE); /* Dealt with above */
2685 break;
2686 default:
2688 }
2689
2690 /* If we are still alive, either sentry or fortify. */
2691 if ((punit = game_unit_by_number(id))) {
2693 struct city *pcity = tile_city(unit_tile(punit));
2694
2695 if (unit_list_find(unit_tile(punit)->units,
2696 unit_data->ferryboat)) {
2698 } else if (pcity || punit->activity == ACTIVITY_IDLE) {
2699 /* We do not need to fortify in cities - we fortify and sentry
2700 * according to home defense setup, for easy debugging. */
2701 if (!pcity || unit_data->task == AIUNIT_DEFEND_HOME) {
2706 }
2707 } else {
2709 }
2710 }
2711 }
2712}
2713
2714/**********************************************************************/
2717static void dai_manage_settler(struct ai_type *ait, struct player *pplayer,
2718 struct unit *punit)
2719{
2720 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2721
2723 unit_data->done = TRUE; /* We will manage this unit later... ugh */
2724 /* If BUILD_CITY must remain BUILD_CITY, otherwise turn into autoworker */
2725 if (unit_data->task == AIUNIT_NONE) {
2727 }
2728}
2729
2730/**********************************************************************/
2738void dai_manage_unit(struct ai_type *ait, struct player *pplayer,
2739 struct unit *punit)
2740{
2741 struct unit_ai *unit_data;
2742 struct unit *bodyguard = aiguard_guard_of(ait, punit);
2743 bool is_ferry = FALSE;
2744 const struct unit_type *ptype;
2745 const struct civ_map *nmap = &(wld.map);
2746
2748
2749 /* Don't manage the unit if it is under human orders. */
2750 if (unit_has_orders(punit)) {
2752
2753 UNIT_LOG(LOG_VERBOSE, punit, "is under human orders, aborting AI control.");
2755 unit_data->done = TRUE;
2756 return;
2757 }
2758
2759 /* Check if we have lost our bodyguard. If we never had one, all
2760 * fine. If we had one and lost it, ask for a new one. */
2761 if (!bodyguard && aiguard_has_guard(ait, punit)) {
2762 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "lost bodyguard, asking for new");
2764 }
2765
2767
2768 if (punit->moves_left <= 0) {
2769 /* Can do nothing */
2770 unit_data->done = TRUE;
2771 return;
2772 }
2773
2774 is_ferry = dai_is_ferry(punit, ait);
2775
2777
2780 dai_manage_diplomat(ait, pplayer, punit);
2782 return;
2783 } else if (ptype->adv.worker
2785 dai_manage_settler(ait, pplayer, punit);
2786 return;
2791 dai_manage_caravan(ait, pplayer, punit);
2793 return;
2795 dai_manage_barbarian_leader(ait, pplayer, punit);
2796 return;
2799 dai_manage_paratrooper(ait, pplayer, punit);
2800 return;
2801 } else if (is_ferry && unit_data->task != AIUNIT_HUNTER) {
2803 dai_manage_ferryboat(ait, pplayer, punit);
2805 return;
2806 } else if (utype_fuel(ptype)
2807 && unit_data->task != AIUNIT_ESCORT) {
2809 dai_manage_airunit(ait, pplayer, punit);
2811 return;
2812 } else if (is_losing_hp(punit)) {
2813 /* This unit is losing hitpoints over time */
2814
2815 /* TODO: We can try using air-unit code for helicopters, just
2816 * pretend they have fuel = HP / 3 or something. */
2817 unit_data->done = TRUE; /* we did our best, which was ...
2818 nothing */
2819 return;
2820 } else if (!is_special_unit(punit)) {
2822 UNIT_LOG(LOG_DEBUG, punit, "recruit unit for the military");
2823 dai_manage_military(ait, nmap, pplayer, punit);
2825 return;
2826 } else {
2827 /* what else could this be? -- Syela */
2828 switch (manage_auto_explorer(punit)) {
2829 case MR_DEATH:
2830 /* don't use punit! */
2831 break;
2832 case MR_OK:
2833 UNIT_LOG(LOG_DEBUG, punit, "now exploring");
2834 break;
2835 default:
2836 UNIT_LOG(LOG_DEBUG, punit, "fell through all unit tasks, defending");
2838 dai_military_defend(ait, pplayer, punit);
2839 break;
2840 };
2841 return;
2842 }
2843}
2844
2845/**********************************************************************/
2851static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
2852{
2853 city_list_iterate(pplayer->cities, pcity) {
2854 /* The idea here is that we should never keep more than two
2855 * units in permanent defense. */
2856 int total_defense = 0;
2858 bool emergency = FALSE;
2859 int count = 0;
2864 int entertainers = 0;
2865 bool enough = FALSE;
2866
2869 entertainers += pcity->specialists[sp];
2870 }
2872
2873 martless_unhappy += entertainers; /* We want to use martial law instead
2874 * of entertainers. */
2875
2876 while (!enough
2879 && martless_unhappy > mart_each * count))) {
2880 int best_want = 0;
2881 struct unit *best = NULL;
2882 bool defense_needed = total_defense <= total_attack; /* Defense or martial */
2883
2885 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2886
2887 if ((unit_data->task == AIUNIT_NONE || emergency)
2888 && unit_data->task != AIUNIT_DEFEND_HOME
2889 && unit_owner(punit) == pplayer) {
2890 int want = assess_defense_unit(ait, pcity, punit, FALSE);
2891
2892 if (want > best_want) {
2893 best_want = want;
2894 best = punit;
2895 }
2896 }
2898
2899 if (best == NULL) {
2900 if (defense_needed) {
2901 /* Ooops - try to grab any unit as defender! */
2902 if (emergency) {
2903 CITY_LOG(LOG_DEBUG, pcity, "Not defended properly");
2904 break;
2905 }
2906 emergency = TRUE;
2907 } else {
2908 break;
2909 }
2910 } else {
2911 const struct unit_type *btype = unit_type_get(best);
2912
2913 if ((martless_unhappy < mart_each * count
2914 || count >= mart_max || mart_each <= 0)
2915 && ((count >= 2
2916 && btype->attack_strength > btype->defense_strength)
2917 || (count >= 4
2918 && btype->attack_strength == btype->defense_strength))) {
2919 /* In this case attack would be better defense than fortifying
2920 * to city. */
2921 enough = TRUE;
2922 } else {
2923 int loglevel = pcity->server.debug ? LOG_AI_TEST : LOG_DEBUG;
2924
2926 UNIT_LOG(loglevel, best, "Defending city");
2928 count++;
2929 }
2930 }
2931 }
2932 CITY_LOG(LOG_DEBUG, pcity, "Evaluating defense: %d defense, %d incoming"
2933 ", %d defenders (out of %d)", total_defense, total_attack, count,
2936}
2937
2938/**********************************************************************/
2946void dai_manage_units(struct ai_type *ait, struct player *pplayer)
2947{
2949 dai_airlift(ait, pplayer);
2951
2952 /* Clear previous orders, if desirable, here. */
2953 unit_list_iterate(pplayer->units, punit) {
2954 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2955
2956 unit_data->done = FALSE;
2957 if (unit_data->task == AIUNIT_DEFEND_HOME) {
2959 }
2961
2962 /* Find and set city defenders first - figure out which units are
2963 * allowed to leave home. */
2964 dai_set_defenders(ait, pplayer);
2965
2968 && !def_ai_unit_data(punit, ait)->done) {
2969 /* Though it is usually the passenger who drives the transport,
2970 * the transporter is responsible for managing its passengers. */
2971 dai_manage_unit(ait, pplayer, punit);
2972 }
2974}
2975
2976/**********************************************************************/
2982 const struct city *pcity)
2983{
2984 const struct impr_type *impr_req = NULL;
2985 const struct req_context context = {
2987 .city = pcity,
2988 .tile = city_tile(pcity),
2989 .unittype = putype,
2990 };
2991
2992 requirement_vector_iterate(&putype->build_reqs, preq) {
2994 /* Already there. */
2995 continue;
2996 }
2998 city_owner(pcity), pcity)) {
2999 /* The unit type can't be built at all. */
3000 return NULL;
3001 }
3002 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
3003 /* This is (one of) the building(s) required. */
3004 impr_req = preq->source.value.building;
3005 }
3007
3008 return impr_req;
3009}
3010
3011/**********************************************************************/
3016 const struct unit_type *base)
3017{
3018 /* This is the real function: */
3019 do {
3020 base = base->obsoleted_by;
3021 if (base == test) {
3022 return TRUE;
3023 }
3024 } while (base);
3025 return FALSE;
3026}
3027
3028/**********************************************************************/
3033static void dai_manage_barbarian_leader(struct ai_type *ait,
3034 struct player *pplayer,
3035 struct unit *leader)
3036{
3038 struct pf_parameter parameter;
3039 struct pf_map *pfm;
3040 struct pf_reverse_map *pfrm;
3041 struct unit *worst_danger;
3042 int move_cost, best_move_cost;
3043 int body_guards;
3044 bool alive = TRUE;
3045 const struct civ_map *nmap = &(wld.map);
3046
3048
3049 if (leader->moves_left == 0
3051 && 1 < unit_list_size(leader_tile->units))) {
3053
3054 return;
3055 }
3056
3057 if (is_boss_of_boat(ait, leader)) {
3058 /* We are in charge. Of course, since we are the leader...
3059 * But maybe somebody more militaristic should lead our ship to battle! */
3060
3061 /* First release boat from leaders lead */
3063
3067 && warrior->moves_left > 0) {
3068 /* This seems like a good warrior to lead us in to conquest! */
3069 dai_manage_unit(ait, pplayer, warrior);
3070
3071 /* If we reached our destination, ferryboat already called
3072 * ai_manage_unit() for leader. So no need to continue here.
3073 * Leader might even be dead.
3074 * If this return is removed, surrounding unit_list_iterate()
3075 * has to be replaced with unit_list_iterate_safe()*/
3076 return;
3077 }
3079 }
3080
3081 /* If we are not in charge of the boat, continue as if we
3082 * were not in a boat - we may want to leave the ship now. */
3083
3084 /* Check the total number of units able to protect our leader. */
3085 body_guards = 0;
3086 unit_list_iterate(pplayer->units, punit) {
3089 body_guards++;
3090 }
3092
3093 if (0 < body_guards) {
3094 pft_fill_unit_parameter(&parameter, nmap, leader);
3095 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3096 pfm = pf_map_new(&parameter);
3097
3098 /* Find the closest body guard.
3099 * FIXME: maybe choose the strongest too? */
3100 pf_map_tiles_iterate(pfm, ptile, FALSE) {
3101 unit_list_iterate(ptile->units, punit) {
3102 if (unit_owner(punit) == pplayer
3105 struct pf_path *path = pf_map_path(pfm, ptile);
3106
3107 adv_follow_path(leader, path, ptile);
3108 pf_path_destroy(path);
3110 return;
3111 }
3114
3116 }
3117
3118 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader needs to flee");
3119
3120 /* Check for units we could fear. */
3121 pfrm = pf_reverse_map_new(nmap, pplayer, leader_tile, 3,
3122 !has_handicap(pplayer, H_MAP));
3125
3126 players_iterate(other_player) {
3127 if (other_player == pplayer) {
3128 continue;
3129 }
3130
3131 unit_list_iterate(other_player->units, punit) {
3133 if (PF_IMPOSSIBLE_MC != move_cost && move_cost < best_move_cost) {
3134 best_move_cost = move_cost;
3136 }
3139
3141
3142 if (NULL == worst_danger) {
3144 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: no close enemy.");
3145 return;
3146 }
3147
3149 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3150 pfm = pf_map_new(&parameter);
3152
3153 /* Try to escape. */
3154 do {
3156
3157 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: moves left: %d.",
3158 leader->moves_left);
3159
3162 continue;
3163 }
3164
3165 move_cost = pf_map_move_cost(pfm, near_tile);
3166 if (PF_IMPOSSIBLE_MC != move_cost
3167 && move_cost > best_move_cost) {
3169 "Barbarian leader: safest is (%d, %d), safeness %d",
3171 best_move_cost = move_cost;
3173 }
3175
3176 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: fleeing to (%d, %d).",
3180 "Barbarian leader: reached the safest position.");
3183 return;
3184 }
3185
3187 if (alive) {
3189 /* Didn't move. No point to retry. */
3191 return;
3192 }
3194 }
3195 } while (alive && 0 < leader->moves_left);
3196
3198}
3199
3200/**********************************************************************/
3206void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile,
3207 struct unit *punit,
3208 enum override_bool *result)
3209{
3210 int a = 0, d, db;
3211 struct player *pplayer = unit_owner(punit);
3212 struct city *pcity = tile_city(ptile);
3213 int extras_bonus = 0;
3214
3215 if (is_human(pplayer)) {
3216 /* Use advisors code for humans. */
3217 return;
3218 }
3219
3220 if (pcity != NULL && pplayers_allied(city_owner(pcity), pplayer)
3221 && !is_non_allied_unit_tile(ptile, pplayer,
3223 /* We will be safe in a friendly city */
3224 *result = OVERRIDE_FALSE;
3225 return;
3226 }
3227
3228 /* Calculate how well we can defend at (x,y) */
3229 db = 10 + tile_terrain(ptile)->defense_bonus / 10;
3231
3232 db += (db * extras_bonus) / 100;
3234
3235 adjc_iterate(&(wld.map), ptile, ptile1) {
3236 if (has_handicap(pplayer, H_FOG)
3237 && !map_is_known_and_seen(ptile1, pplayer, V_MAIN)) {
3238 /* We cannot see danger at (ptile1) => assume there is none */
3239 continue;
3240 }
3241 unit_list_iterate(ptile1->units, enemy) {
3242 if (pplayers_at_war(unit_owner(enemy), pplayer)
3244 == ATT_OK)
3246 == ATT_OK)) {
3248 if ((a * a * 10) >= d) {
3249 /* The enemies combined strength is too big! */
3250 *result = OVERRIDE_TRUE;
3251 return;
3252 }
3253 }
3256
3257 *result = OVERRIDE_FALSE;
3258}
3259
3260/**********************************************************************/
3263static void update_simple_ai_types(void)
3264{
3265 int i = 0;
3266
3269
3272 && !(pclass->adv.land_move == MOVE_NONE
3275 && punittype->transport_capacity < 8) {
3277 i++;
3278 }
3280
3282}
3283
3284/**********************************************************************/
3288{
3289 /* TODO: remove the simple_ai_types cache or merge it with a general ai
3290 * cache; see the comment to struct unit_type *simple_ai_types at
3291 * the beginning of this file. */
3293
3295 struct unit_type_ai *utai = fc_malloc(sizeof(*utai));
3296
3297 utai->low_firepower = FALSE;
3298 utai->ferry = FALSE;
3299 utai->missile_platform = FALSE;
3300 utai->carries_occupiers = FALSE;
3301 utai->potential_charges = unit_type_list_new();
3302
3305
3308
3309 /* Confirm firepower */
3311 if (pbonus->type == CBONUS_LOW_FIREPOWER) {
3313 if (utype_has_flag(penemy, pbonus->flag)) {
3314 struct unit_type_ai *utai = utype_ai_data(penemy, ait);
3315
3316 utai->low_firepower = TRUE;
3317 }
3319 }
3321
3322 /* Consider potential cargo */
3323 if (punittype->transport_capacity > 0) {
3324 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3325
3328
3331 utai->missile_platform = TRUE;
3332 } else if (pclass->adv.sea_move != MOVE_NONE
3333 && pcargo->adv.land_move != MOVE_NONE) {
3334 if (pcargo->adv.sea_move != MOVE_FULL) {
3335 utai->ferry = TRUE;
3336 } else {
3337 if (0 != utype_fuel(pctype)) {
3338 utai->ferry = TRUE;
3339 }
3340 }
3341 }
3342
3344 utai->carries_occupiers = TRUE;
3345 }
3346 }
3348 }
3349
3350 /* Consider potential charges */
3353
3354 if (0 < utype_fuel(punittype)
3355 && (0 == utype_fuel(pcharge)
3357 continue;
3358 }
3359
3360 unit_class_list_iterate(pclass->cache.subset_movers, chgcls) {
3361 if (chgcls == utype_class(pcharge)) {
3363 }
3365
3367 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3368 unit_type_list_append(utai->potential_charges, pcharge);
3369 }
3370
3373}
3374
3375/**********************************************************************/
3379{
3381 struct unit_type_ai *utai = utype_ai_data(ptype, ait);
3382
3383 if (utai == NULL) {
3384 continue;
3385 }
3387
3388 unit_type_list_destroy(utai->potential_charges);
3389 free(utai);
3391}
3392
3393/**********************************************************************/
3396void dai_unit_init(struct ai_type *ait, struct unit *punit)
3397{
3398 /* Make sure that contents of unit_ai structure are correctly initialized,
3399 * if you ever allocate it by some other mean than fc_calloc() */
3400 struct unit_ai *unit_data = fc_calloc(1, sizeof(struct unit_ai));
3401
3402 unit_data->done = FALSE;
3403 unit_data->cur_pos = NULL;
3404 unit_data->prev_pos = NULL;
3405 unit_data->target = 0;
3406 BV_CLR_ALL(unit_data->hunted);
3407 unit_data->ferryboat = 0;
3408 unit_data->passenger = 0;
3409 unit_data->bodyguard = 0;
3410 unit_data->charge = 0;
3411
3413}
3414
3415/**********************************************************************/
3418void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
3419{
3420 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3421
3423
3424 BV_CLR_ALL(unit_data->hunted);
3425}
3426
3427/**********************************************************************/
3430void dai_unit_close(struct ai_type *ait, struct unit *punit)
3431{
3432 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3433
3435
3438
3439 if (unit_data != NULL) {
3442 }
3443}
3444
3445/**********************************************************************/
3448void dai_unit_save(struct ai_type *ait, const char *aitstr,
3449 struct section_file *file,
3450 const struct unit *punit, const char *unitstr)
3451{
3452 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3453
3454 secfile_insert_int(file, unit_data->passenger, "%s.%spassenger",
3455 unitstr, aitstr);
3456 secfile_insert_int(file, unit_data->ferryboat, "%s.%sferryboat",
3457 unitstr, aitstr);
3458 secfile_insert_int(file, unit_data->charge, "%s.%scharge",
3459 unitstr, aitstr);
3460 secfile_insert_int(file, unit_data->bodyguard, "%s.%sbodyguard",
3461 unitstr, aitstr);
3462}
3463
3464/**********************************************************************/
3467void dai_unit_load(struct ai_type *ait, const char *aitstr,
3468 const struct section_file *file,
3469 struct unit *punit, const char *unitstr)
3470{
3471 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3472
3473 unit_data->passenger
3474 = secfile_lookup_int_default(file, 0, "%s.%spassenger",
3475 unitstr, aitstr);
3476 unit_data->ferryboat
3477 = secfile_lookup_int_default(file, 0, "%s.%sferryboat",
3478 unitstr, aitstr);
3479 unit_data->charge
3480 = secfile_lookup_int_default(file, 0, "%s.%scharge",
3481 unitstr, aitstr);
3482 unit_data->bodyguard
3483 = secfile_lookup_int_default(file, 0, "%s.%sbodyguard",
3484 unitstr, aitstr);
3485}
3486
3492
3493/**********************************************************************/
3496static bool role_unit_cb(struct unit_type *ptype, void *data)
3497{
3498 struct role_unit_cb_data *cb_data = (struct role_unit_cb_data *)data;
3500 const struct civ_map *nmap = &(wld.map);
3501
3502 if ((cb_data->tc == TC_LAND && pclass->adv.land_move == MOVE_NONE)
3503 || (cb_data->tc == TC_OCEAN && pclass->adv.sea_move == MOVE_NONE)) {
3504 return FALSE;
3505 }
3506
3507 if (cb_data->build_city == NULL
3508 || can_city_build_unit_now(nmap, cb_data->build_city, ptype)) {
3509 return TRUE;
3510 }
3511
3512 return FALSE;
3513}
3514
3515/**********************************************************************/
3519 enum terrain_class tc)
3520{
3521 struct role_unit_cb_data cb_data = { .build_city = pcity, .tc = tc };
3522
3524}
3525
3526/**********************************************************************/
3529bool dai_unit_can_strike_my_unit(const struct unit *attacker,
3530 const struct unit *defender)
3531{
3532 struct pf_parameter parameter;
3533 struct pf_map *pfm;
3534 const struct tile *ptarget = unit_tile(defender);
3535 int max_move_cost = attacker->moves_left;
3536 bool able_to_strike = FALSE;
3537 const struct civ_map *nmap = &(wld.map);
3538
3539 pft_fill_unit_parameter(&parameter, nmap, attacker);
3540 parameter.omniscience = !has_handicap(unit_owner(defender), H_MAP);
3541 pfm = pf_map_new(&parameter);
3542
3543 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
3544 if (move_cost > max_move_cost) {
3545 break;
3546 }
3547
3548 if (ptile == ptarget) {
3550 break;
3551 }
3553
3555
3556 return able_to_strike;
3557}
3558
3559/**********************************************************************/
3562void dai_switch_to_explore(struct ai_type *ait, struct unit *punit,
3563 struct tile *target, enum override_bool *allow)
3564{
3565 struct unit_ai *udata = def_ai_unit_data(punit, ait);
3566
3567 if (udata->task != AIUNIT_NONE && udata->task != AIUNIT_EXPLORE) {
3569
3570 return;
3571 }
3572}
enum gen_action select_actres_action_unit_on_stack(struct civ_map *nmap, enum action_result actres, struct unit *punit, struct tile *ptile)
Definition actions.c:7619
bool is_action_enabled_unit_on_city(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit, const struct city *target_city)
Definition actions.c:3079
enum gen_action select_actres_action_unit_on_tile(struct civ_map *nmap, enum action_result actres, struct unit *punit, struct tile *ptile)
Definition actions.c:7643
enum gen_action select_actres_action_unit_on_city(struct civ_map *nmap, enum action_result actres, struct unit *punit, struct city *pcity)
Definition actions.c:7668
static struct action * action_by_number(action_id act_id)
Definition actions.h:400
#define ACTION_NONE
Definition actions.h:59
#define TRADE_WEIGHTING
Definition advbuilding.h:21
#define SHIELD_WEIGHTING
Definition advbuilding.h:20
bool adv_follow_path(struct unit *punit, struct pf_path *path, struct tile *ptile)
Definition advgoto.c:47
int adv_unit_def_rating_basic_squared(const struct unit *punit)
Definition advgoto.c:415
int adv_unit_def_rating_basic(const struct unit *punit)
Definition advgoto.c:406
int adv_unittype_att_rating(const struct unit_type *punittype, int veteran, int moves_left, int hp)
Definition advgoto.c:385
int adv_unit_att_rating(const struct unit *punit)
Definition advgoto.c:396
int adv_could_unit_move_to_tile(struct unit *punit, struct tile *dest_tile)
Definition advgoto.c:359
bool adv_unit_execute_path(struct unit *punit, struct pf_path *path)
Definition advgoto.c:86
#define POWER_DIVIDER
Definition advtools.h:32
void dai_manage_diplomat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition aidiplomat.c:723
bool aiferry_gobyboat(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile, bool with_bodyguard)
Definition aiferry.c:764
int aiferry_find_boat(struct ai_type *ait, struct unit *punit, int cap, struct pf_path **path)
Definition aiferry.c:495
void dai_manage_ferryboat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition aiferry.c:1113
bool is_boat_free(struct ai_type *ait, struct unit *boat, struct unit *punit, int cap)
Definition aiferry.c:438
bool aiferry_goto_amphibious(struct ai_type *ait, struct unit *ferry, struct unit *passenger, struct tile *ptile)
Definition aiferry.c:730
bool is_boss_of_boat(struct ai_type *ait, struct unit *punit)
Definition aiferry.c:471
int aiferry_avail_boats(struct ai_type *ait, struct player *pplayer)
Definition aiferry.c:353
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Definition aiferry.c:159
void aiferry_clear_boat(struct ai_type *ait, struct unit *punit)
Definition aiferry.c:251
struct city * aiguard_charge_city(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:289
void aiguard_clear_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:117
void aiguard_request_guard(struct ai_type *ait, struct unit *punit)
Definition aiguard.c:227
bool aiguard_has_guard(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:259
void aiguard_assign_guard_unit(struct ai_type *ait, struct unit *charge, struct unit *guard)
Definition aiguard.c:173
bool aiguard_wanted(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:241
void aiguard_assign_guard_city(struct ai_type *ait, struct city *charge, struct unit *guard)
Definition aiguard.c:196
struct unit * aiguard_charge_unit(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:279
void aiguard_clear_guard(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:146
struct unit * aiguard_guard_of(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:269
bool aiguard_has_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:250
void aiguard_update_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:299
#define CHECK_GUARD(ait, guard)
Definition aiguard.h:21
enum unit_move_result manage_auto_explorer(struct unit *punit)
void adv_unit_new_task(struct unit *punit, enum adv_unit_task task, struct tile *ptile)
bool is_land_barbarian(struct player *pplayer)
Definition barbarian.c:75
#define BV_CLR_ALL(bv)
Definition bitvector.h:103
void caravan_find_best_destination(const struct civ_map *nmap, const struct unit *caravan, const struct caravan_parameter *parameter, struct caravan_result *result, bool omniscient)
Definition caravan.c:690
void caravan_parameter_init_from_unit(struct caravan_parameter *parameter, const struct unit *caravan)
Definition caravan.c:53
@ FTL_ALLIED
Definition caravan.h:31
@ FTL_NATIONAL_ONLY
Definition caravan.h:30
@ FTL_NONWAR
Definition caravan.h:33
bool city_production_gets_caravan_shields(const struct universal *tgt)
Definition city.c:1860
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:804
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:947
#define city_list_iterate(citylist, pcity)
Definition city.h:505
#define city_tile(_pcity_)
Definition city.h:561
@ CITIZEN_ANGRY
Definition city.h:268
@ CITIZEN_UNHAPPY
Definition city.h:267
#define city_owner(_pcity_)
Definition city.h:560
#define city_list_iterate_end
Definition city.h:507
@ FEELING_NATIONALITY
Definition city.h:279
struct city * find_closest_city(const struct tile *ptile, const struct city *pexclcity, const struct player *pplayer, bool only_ocean, bool only_continent, bool only_known, bool only_player, bool only_enemy, const struct unit_class *pclass)
Definition citytools.c:856
double unit_win_chance(const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender, const struct action *paction)
Definition combat.c:480
bool is_stack_vulnerable(const struct tile *ptile)
Definition combat.c:994
int get_virtual_defense_power(const struct civ_map *nmap, const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
Definition combat.c:717
enum unit_attack_result unit_attack_units_at_tile_result(const struct unit *punit, const struct action *paction, const struct tile *ptile)
Definition combat.c:257
int get_total_defense_power(const struct unit *attacker, const struct unit *defender)
Definition combat.c:772
enum unit_attack_result unit_attack_unit_at_tile_result(const struct unit *punit, const struct action *paction, const struct unit *pdefender, const struct tile *dest_tile)
Definition combat.c:123
struct unit * get_defender(const struct civ_map *nmap, const struct unit *attacker, const struct tile *ptile, const struct action *paction)
Definition combat.c:841
bool can_unit_attack_tile(const struct unit *punit, const struct action *paction, const struct tile *dest_tile)
Definition combat.c:312
int base_get_defense_power(const struct unit *punit)
Definition combat.c:574
@ ATT_OK
Definition combat.h:35
char * incite_cost
Definition comments.c:76
void dai_manage_airunit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiair.c:492
bool dai_can_requirement_be_met_in_city(const struct requirement *preq, const struct player *pplayer, const struct city *pcity)
Definition daieffects.c:821
bool dai_hunter_qualify(struct player *pplayer, struct unit *punit)
Definition daihunter.c:287
int dai_hunter_manage(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daihunter.c:435
#define BODYGUARD_LOG(ait, loglevel, punit, msg,...)
Definition dailog.h:69
struct unit_type * dai_choose_defender_versus(struct city *pcity, struct unit *attacker)
Definition daimilitary.c:90
int assess_defense_quadratic(struct ai_type *ait, struct city *pcity)
int assess_defense_unit(struct ai_type *ait, struct city *pcity, struct unit *punit, bool igwall)
void dai_manage_paratrooper(struct ai_type *ait, struct player *pplayer, struct unit *punit)
static struct ai_city * def_ai_city_data(const struct city *pcity, struct ai_type *deftype)
Definition daiplayer.h:42
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition daiplayer.h:48
int stack_cost(struct unit *pattacker, struct unit *pdefender)
Definition daitools.c:1306
bool goto_is_sane(struct unit *punit, struct tile *ptile)
Definition daitools.c:429
bool dai_unit_attack(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Definition daitools.c:817
bool dai_unit_goto(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Definition daitools.c:605
void dai_unit_new_task(struct ai_type *ait, struct unit *punit, enum ai_unit_task task, struct tile *ptile)
Definition daitools.c:643
bool dai_assess_military_unhappiness(const struct civ_map *nmap, struct city *pcity)
Definition daitools.c:1441
bool dai_unit_make_homecity(struct unit *punit, struct city *pcity)
Definition daitools.c:755
const char * dai_unit_task_rule_name(const enum ai_unit_task task)
Definition daitools.c:76
adv_want military_amortize(struct player *pplayer, struct city *pcity, adv_want value, int delay, int build_cost)
Definition daitools.c:117
bool dai_gothere(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile)
Definition daitools.c:244
void dai_unit_save(struct ai_type *ait, const char *aitstr, struct section_file *file, const struct unit *punit, const char *unitstr)
Definition daiunit.c:3448
static bool invasion_funct(struct ai_type *ait, struct unit *punit, bool dest, int radius, int which)
Definition daiunit.c:1089
static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:921
static bool has_defense(struct city *pcity)
Definition daiunit.c:222
adv_want kill_desire(adv_want benefit, int attack, int loss, int vuln, int victim_count)
Definition daiunit.c:342
static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2301
static void dai_caravan_goto(struct ai_type *ait, struct player *pplayer, struct unit *punit, const struct city *dest_city, bool help_wonder, bool required_boat, bool request_boat)
Definition daiunit.c:2061
int unittype_def_rating_squared(const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
Definition daiunit.c:304
static bool unit_role_defender(const struct unit_type *punittype)
Definition daiunit.c:769
void dai_units_ruleset_init(struct ai_type *ait)
Definition daiunit.c:3287
#define LOG_CARAVAN
Definition daiunit.c:88
static int unit_att_rating_now(const struct unit *punit)
Definition daiunit.c:259
static void dai_military_attack_barbarian(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1760
bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map, struct tile *dest_tile, const struct unit_type *cargo_type, const struct unit_type *ferry_type, struct tile **ferry_dest, struct tile **beachhead_tile)
Definition daiunit.c:1135
enum gen_action dai_select_tile_attack_action(struct civ_map *nmap, struct unit *punit, struct tile *ptile, enum action_target_kind *kind)
Definition daiunit.c:445
static void update_simple_ai_types(void)
Definition daiunit.c:3263
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Definition daiunit.c:2981
void dai_switch_to_explore(struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
Definition daiunit.c:3562
void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2583
static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:1977
static void reinforcements_cost_and_value(struct unit *punit, struct tile *ptile0, int *value, int *cost)
Definition daiunit.c:375
struct unit_type * simple_ai_types[U_LAST]
Definition daiunit.c:126
static void dai_airlift(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:169
int build_cost_balanced(const struct unit_type *punittype)
Definition daiunit.c:250
bool dai_unit_can_strike_my_unit(const struct unit *attacker, const struct unit *defender)
Definition daiunit.c:3529
bool is_on_unit_upgrade_path(const struct unit_type *test, const struct unit_type *base)
Definition daiunit.c:3015
static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:703
static void single_invader(struct ai_city *city_data, const struct unit_type *utype, int which)
Definition daiunit.c:1061
#define LOG_CARAVAN3
Definition daiunit.c:90
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2946
static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2338
adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile **pdest_tile, struct pf_path **ppath, struct pf_map **pferrymap, struct unit **pferryboat, const struct unit_type **pboattype, int *pmove_time)
Definition daiunit.c:1213
static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2851
void dai_manage_unit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2738
adv_want look_for_charge(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity)
Definition daiunit.c:784
static bool role_unit_cb(struct unit_type *ptype, void *data)
Definition daiunit.c:3496
static void dai_military_attack(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1823
void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3418
static void dai_manage_settler(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2717
static int avg_benefit(int benefit, int loss, double chance)
Definition daiunit.c:366
#define LOG_CARAVAN2
Definition daiunit.c:89
static struct city * find_neediest_airlift_city(struct ai_type *ait, const struct player *pplayer)
Definition daiunit.c:138
static int unit_def_rating(const struct unit *attacker, const struct unit *defender)
Definition daiunit.c:278
static bool dai_is_unit_tired_waiting_boat(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2197
static int dai_rampage_want(struct unit *punit, struct tile *ptile)
Definition daiunit.c:524
bool dai_can_unit_type_follow_unit_type(const struct unit_type *follower, const struct unit_type *followee, struct ai_type *ait)
Definition daiunit.c:903
struct city * find_nearest_safe_city(struct unit *punit)
Definition daiunit.c:1709
static void dai_manage_barbarian_leader(struct ai_type *ait, struct player *pplayer, struct unit *leader)
Definition daiunit.c:3033
static void dai_manage_hitpoint_recovery(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2508
static int unit_att_rating_squared(const struct unit *punit)
Definition daiunit.c:268
static struct pf_path * find_rampage_target(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:591
static bool is_my_turn(struct unit *punit, struct unit *pdef)
Definition daiunit.c:400
static int unit_def_rating_squared(const struct unit *punit, const struct unit *pdef)
Definition daiunit.c:291
void dai_unit_init(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3396
void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
Definition daiunit.c:3206
bool uclass_need_trans_between(struct unit_class *pclass, struct tile *ctile, struct tile *ptile)
Definition daiunit.c:2022
void dai_unit_load(struct ai_type *ait, const char *aitstr, const struct section_file *file, struct unit *punit, const char *unitstr)
Definition daiunit.c:3467
static void caravan_optimize_callback(const struct caravan_result *result, void *data)
Definition daiunit.c:2178
#define LOGLEVEL_RECOVERY
Definition daiunit.c:87
void dai_unit_close(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3430
void dai_units_ruleset_close(struct ai_type *ait)
Definition daiunit.c:3378
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Definition daiunit.c:3518
static void dai_military_defend(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1015
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:670
static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer, struct unit *punit)
Definition daiunit.c:2256
#define POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer)
Definition daiunit.h:72
@ AIUNIT_BUILD_CITY
Definition daiunit.h:27
@ AIUNIT_NONE
Definition daiunit.h:27
@ AIUNIT_ATTACK
Definition daiunit.h:28
@ AIUNIT_EXPLORE
Definition daiunit.h:29
@ AIUNIT_HUNTER
Definition daiunit.h:29
@ AIUNIT_AUTO_WORKER
Definition daiunit.h:27
@ AIUNIT_RECOVER
Definition daiunit.h:29
@ AIUNIT_TRADE
Definition daiunit.h:30
@ AIUNIT_DEFEND_HOME
Definition daiunit.h:28
@ AIUNIT_ESCORT
Definition daiunit.h:28
@ AIUNIT_WONDER
Definition daiunit.h:30
#define RAMPAGE_ANYTHING
Definition daiunit.h:85
#define RAMPAGE_FREE_CITY_OR_BETTER
Definition daiunit.h:87
#define INVASION_ATTACK
Definition daiunit.h:81
#define INVASION_OCCUPY
Definition daiunit.h:80
#define RAMPAGE_HUT_OR_BETTER
Definition daiunit.h:86
#define DEFENSE_POWER(ptype)
Definition daiunit.h:66
#define IS_ATTACKER(ptype)
Definition daiunit.h:70
#define BODYGUARD_RAMPAGE_THRESHOLD
Definition daiunit.h:88
#define ATTACK_POWER(ptype)
Definition daiunit.h:68
static void attack(QVariant data1, QVariant data2)
Definition dialogs.cpp:3494
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:3353
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:77
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:73
bool ai_fuzzy(const struct player *pplayer, bool normal_decision)
Definition difficulty.c:339
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:842
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, const struct action *paction, enum effect_type effect_type)
Definition effects.c:1031
bool unit_can_enter_hut(const struct unit *punit, const struct tile *ptile)
Definition extras.c:720
float adv_want
Definition fc_types.h:1062
@ RPT_CERTAIN
Definition fc_types.h:534
@ AUT_AUTO_WORKER
Definition fc_types.h:230
#define ADV_WANT_PRINTF
Definition fc_types.h:1063
@ O_LUXURY
Definition fc_types.h:102
signed short Continent_id
Definition fc_types.h:232
override_bool
Definition fc_types.h:95
@ OVERRIDE_TRUE
Definition fc_types.h:95
@ OVERRIDE_FALSE
Definition fc_types.h:95
struct civ_game game
Definition game.c:61
struct world wld
Definition game.c:62
struct unit * game_unit_by_number(int id)
Definition game.c:115
struct city * game_city_by_number(int id)
Definition game.c:106
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_TARGETS
Definition handicaps.h:24
@ H_AWAY
Definition handicaps.h:19
@ H_FOG
Definition handicaps.h:26
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_do_output_for_level(level)
Definition log.h:90
#define log_base(level, message,...)
Definition log.h:95
log_level
Definition log.h:29
@ LOG_DEBUG
Definition log.h:35
@ LOG_NORMAL
Definition log.h:33
@ LOG_VERBOSE
Definition log.h:34
bool is_tiles_adjacent(const struct tile *tile0, const struct tile *tile1)
Definition map.c:1076
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:1085
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:675
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:699
#define iterate_outward(nmap, start_tile, max_dist, itr_tile)
Definition map.h:373
#define iterate_outward_end
Definition map.h:377
#define adjc_iterate_end
Definition map.h:439
static int index_to_map_pos_y(int mindex)
Definition map.h:772
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:397
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:434
#define square_iterate_end
Definition map.h:400
static int index_to_map_pos_x(int mindex)
Definition map.h:759
#define pmap(_tile)
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:909
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:935
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_malloc(sz)
Definition mem.h:34
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:350
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:362
int unit_move_rate(const struct unit *punit)
Definition movement.c:89
bool can_attack_from_non_native(const struct unit_type *utype)
Definition movement.c:226
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:522
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:919
bool can_attack_non_native(const struct unit_type *utype)
Definition movement.c:213
#define SINGLE_MOVE
Definition movement.h:26
static bool is_native_tile_to_class(const struct unit_class *punitclass, const struct tile *ptile)
Definition movement.h:88
@ MR_OK
Definition movement.h:35
@ MR_DEATH
Definition movement.h:36
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
struct nation_type * nation_of_unit(const struct unit *punit)
Definition nation.c:463
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_reverse_map_destroy(struct pf_reverse_map *pfrm)
int pf_reverse_map_unit_move_cost(struct pf_reverse_map *pfrm, const struct unit *punit)
void pf_map_destroy(struct pf_map *pfm)
struct pf_reverse_map * pf_reverse_map_new(const struct civ_map *nmap, const struct player *pplayer, struct tile *target_tile, int max_turns, bool omniscient)
int pf_map_move_cost(struct pf_map *pfm, struct tile *ptile)
#define PF_IMPOSSIBLE_MC
#define pf_map_move_costs_iterate_end
#define pf_map_move_costs_iterate(ARG_pfm, NAME_tile, NAME_cost, COND_from_start)
#define pf_map_tiles_iterate(ARG_pfm, NAME_tile, COND_from_start)
#define pf_map_tiles_iterate_end
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:842
void pft_fill_unit_attack_param(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:951
void pft_fill_unit_overlap_param(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:893
void pft_fill_utype_overlap_param(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit_type *punittype, struct tile *pstart_tile, struct player *pplayer)
Definition pf_tools.c:878
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1229
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1409
#define players_iterate_end
Definition player.h:542
#define players_iterate(_pplayer)
Definition player.h:537
static bool is_barbarian(const struct player *pplayer)
Definition player.h:491
#define is_human(plr)
Definition player.h:231
#define fc_rand(_size)
Definition rand.h:56
int secfile_lookup_int_default(const struct section_file *secfile, int def, const char *path,...)
#define secfile_insert_int(secfile, value, path,...)
bool is_req_active(const struct req_context *context, const struct req_context *other_context, const struct requirement *req, const enum req_problem_type prob_type)
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Definition specialist.c:217
#define specialist_type_iterate_end
Definition specialist.h:79
#define specialist_type_iterate(sp)
Definition specialist.h:73
SPECPQ_PRIORITY_TYPE priority
Definition specpq.h:86
#define CITY_LOG(loglevel, pcity, msg,...)
Definition srv_log.h:83
#define LOGLEVEL_HUNT
Definition srv_log.h:35
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
#define LOG_AI_TEST
Definition srv_log.h:38
@ AIT_RECOVER
Definition srv_log.h:68
@ AIT_FSTK
Definition srv_log.h:54
@ AIT_CARAVAN
Definition srv_log.h:56
@ AIT_RAMPAGE
Definition srv_log.h:71
@ AIT_ATTACK
Definition srv_log.h:66
@ AIT_HUNTER
Definition srv_log.h:57
@ AIT_BODYGUARD
Definition srv_log.h:69
@ AIT_DEFENDERS
Definition srv_log.h:55
@ AIT_AIRUNIT
Definition srv_log.h:60
@ AIT_FERRY
Definition srv_log.h:70
@ AIT_MILITARY
Definition srv_log.h:67
@ AIT_DIPLOMAT
Definition srv_log.h:59
@ AIT_AIRLIFT
Definition srv_log.h:58
@ TIMER_STOP
Definition srv_log.h:76
@ TIMER_START
Definition srv_log.h:76
#define TIMING_LOG(timer, activity)
Definition srv_log.h:125
#define LOGLEVEL_BODYGUARD
Definition srv_log.h:30
unsigned int danger
Definition daicity.h:47
int bcost
Definition daicity.h:44
Definition ai.h:50
void(* callback)(const struct caravan_result *result, void *data)
Definition caravan.h:124
enum foreign_trade_limit allow_foreign_trade
Definition caravan.h:99
bool ignore_transit_time
Definition caravan.h:108
void * callback_data
Definition caravan.h:125
const struct city * src
Definition caravan.h:52
const struct city * dest
Definition caravan.h:53
bool help_wonder
Definition caravan.h:57
double value
Definition caravan.h:56
Definition city.h:317
struct adv_city * adv
Definition city.h:449
struct tile * tile
Definition city.h:319
struct packet_game_info info
Definition game.h:89
int occupychance
Definition game.h:177
struct civ_game::@32::@36 server
struct city_list * cities
Definition player.h:281
struct unit_list * units
Definition player.h:282
bool is_alive
Definition player.h:268
const struct player * player
enum terrain_class tc
Definition daiunit.c:3489
struct city * build_city
Definition daiunit.c:3490
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
Continent_id continent
Definition tile.h:54
bool done
Definition daiunit.h:44
bv_unit_classes cargo
Definition unittype.h:563
struct veteran_system * veteran
Definition unittype.h:549
int move_rate
Definition unittype.h:522
Definition unit.h:139
enum unit_activity activity
Definition unit.h:158
int moves_left
Definition unit.h:151
int id
Definition unit.h:146
bool moved
Definition unit.h:175
bool debug
Definition unit.h:236
int hp
Definition unit.h:152
struct unit::@84::@87 server
struct tile * tile
Definition unit.h:141
struct unit_adv * adv
Definition unit.h:238
int homecity
Definition unit.h:147
int birth_turn
Definition unit.h:209
struct tile * goto_tile
Definition unit.h:156
int veteran
Definition unit.h:153
struct player * owner
Definition unit.h:144
struct civ_map map
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
bool is_terrain_class_near_tile(const struct civ_map *nmap, const struct tile *ptile, enum terrain_class tclass)
Definition terrain.c:611
#define is_ocean_tile(ptile)
Definition terrain.h:197
int tile_extras_defense_bonus(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:233
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:89
#define tile_terrain(_tile)
Definition tile.h:115
#define TILE_XY(ptile)
Definition tile.h:43
#define tile_continent(_tile)
Definition tile.h:93
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
bool can_establish_trade_route(const struct city *pc1, const struct city *pc2, int priority)
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2353
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2473
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2291
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:204
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2544
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2246
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2744
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:401
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:411
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2956
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, enum gen_action action)
Definition unit.c:913
int unit_current_good_replace_priority(const struct unit *punit, const struct city *homecity)
Definition unit.c:2983
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:315
bool is_special_unit(const struct unit *punit)
Definition unit.c:368
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2528
bool is_guard_unit(const struct unit *punit)
Definition unit.c:358
bool unit_has_orders(const struct unit *punit)
Definition unit.c:220
#define unit_tile(_pu)
Definition unit.h:406
#define unit_cargo_iterate_end
Definition unit.h:587
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:584
#define CHECK_UNIT(punit)
Definition unit.h:272
#define unit_owner(_pu)
Definition unit.h:405
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.h:442
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unithand.c:6667
void unit_do_action(struct player *pplayer, const int actor_id, const int target_id, const int sub_tgt_id, const char *name, const action_id action_type)
Definition unithand.c:3338
bool unit_server_side_agent_set(struct player *pplayer, struct unit *punit, enum server_side_agent agent)
Definition unithand.c:6565
struct unit * unit_list_find(const struct unit_list *punitlist, int unit_id)
Definition unitlist.c:30
#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
bool utype_action_takes_all_mp(const struct unit_type *putype, struct action *paction)
Definition unittype.c:1195
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
struct unit_type * role_units_iterate_backwards(int role, role_unit_callback cb, void *data)
Definition unittype.c:2240
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2257
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1591
bool utype_has_role(const struct unit_type *punittype, int role)
Definition unittype.c:203
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1472
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1488
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2302
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2503
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:391
bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
Definition unittype.c:212
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:194
bool unit_can_take_over(const struct unit *punit)
Definition unittype.c:268
void * utype_ai_data(const struct unit_type *ptype, const struct ai_type *ai)
Definition unittype.c:2743
bool utype_can_take_over(const struct unit_type *punittype)
Definition unittype.c:280
void utype_set_ai_data(struct unit_type *ptype, const struct ai_type *ai, void *data)
Definition unittype.c:2751
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1442
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:447
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:375
#define utype_class(_t_)
Definition unittype.h:754
#define utype_fuel(ptype)
Definition unittype.h:844
#define combat_bonus_list_iterate_end
Definition unittype.h:487
#define combat_bonus_list_iterate(bonuslist, pbonus)
Definition unittype.h:485
@ MOVE_FULL
Definition unittype.h:144
@ MOVE_NONE
Definition unittype.h:144
#define unit_type_list_iterate(utype_list, ptype)
Definition unittype.h:945
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:622
#define unit_class_list_iterate(uclass_list, pclass)
Definition unittype.h:937
#define unit_type_iterate(_p)
Definition unittype.h:860
#define unit_type_list_iterate_end
Definition unittype.h:947
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:867
#define unit_class_list_iterate_end
Definition unittype.h:939