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{
449 enum gen_action selected;
450
452 punit, ptile))
453 == ACTION_NONE
455 punit, ptile))
456 == ACTION_NONE
458 punit, ptile))
459 == ACTION_NONE) {
460 return ACTION_NONE;
461 }
462
463 return selected;
464}
465
466/**********************************************************************/
482static int dai_rampage_want(struct unit *punit, struct tile *ptile)
483{
484 struct player *pplayer = unit_owner(punit);
485 struct unit *pdef;
486 struct civ_map *nmap = &(wld.map);
487
489
490 if (can_unit_attack_tile(punit, NULL, ptile)
491 && (pdef = get_defender(nmap, punit, ptile, NULL))
492 /* Action enablers might prevent attacking */
494 /* See description of kill_desire() about these variables. */
498
499 attack *= attack;
500
501 /* If the victim is in the city/fortress, we correct the benefit
502 * with our health because there could be reprisal attacks. We
503 * shouldn't send already injured units to useless suicide.
504 * Note that we do not specially encourage attacks against
505 * cities: rampage is a hit-n-run operation. */
506 if (!is_stack_vulnerable(ptile)
507 && unit_list_size(ptile->units) > 1) {
509 }
510
511 /* If we have non-zero attack rating... */
512 if (attack > 0 && is_my_turn(punit, pdef)) {
515
516 /* No need to amortize, our operation takes one turn. */
517 UNIT_LOG(LOG_DEBUG, punit, "Rampage: Desire %d to kill %s(%d,%d)",
518 desire,
521
522 return MAX(0, desire);
523 }
524 } else if (0 == unit_list_size(ptile->units)) {
525 /* No defender. */
526 struct city *pcity = tile_city(ptile);
527
528 /* ...and free foreign city waiting for us. Who would resist! */
529 if (NULL != pcity
530 && pplayers_at_war(pplayer, city_owner(pcity))
533 }
534
535 /* ...or tiny pleasant hut here! */
536 /* FIXME: unhardcode and variate the desire to enter a hut. */
537 if (unit_can_enter_hut(punit, ptile) && !is_barbarian(pplayer)
538 && is_native_tile(unit_type_get(punit), ptile)) {
539 return -RAMPAGE_HUT_OR_BETTER;
540 }
541 }
542
543 return 0;
544}
545
546/**********************************************************************/
549static struct pf_path *find_rampage_target(struct unit *punit,
550 int thresh_adj, int thresh_move)
551{
552 struct pf_map *tgt_map;
553 struct pf_path *path = NULL;
554 struct pf_parameter parameter;
555 /* Coordinates of the best target (initialize to silence compiler) */
556 struct tile *ptile = unit_tile(punit);
557 /* Want of the best target */
558 int max_want = 0;
559 struct player *pplayer = unit_owner(punit);
560 const struct civ_map *nmap = &(wld.map);
561
563 parameter.omniscience = !has_handicap(pplayer, H_MAP);
564 /* When trying to find rampage targets we ignore risks such as
565 * enemy units because we are looking for trouble!
566 * Hence no call ai_avoid_risks()
567 */
568
569 tgt_map = pf_map_new(&parameter);
571 int want;
572 bool move_needed;
573 int thresh;
574
575 if (move_cost > punit->moves_left) {
576 /* This is too far */
577 break;
578 }
579
580 if (has_handicap(pplayer, H_TARGETS)
581 && !map_is_known_and_seen(iter_tile, pplayer, V_MAIN)) {
582 /* The target is under fog of war */
583 continue;
584 }
585
587
588 /* Negative want means move needed even though the tiles are adjacent */
590 || want < 0);
591 /* Select the relevant threshold */
593 want = (want < 0 ? -want : want);
594
595 if (want > max_want && want > thresh) {
596 /* The new want exceeds both the previous maximum
597 * and the relevant threshold, so it's worth recording */
598 max_want = want;
599 ptile = iter_tile;
600 }
602
603 if (max_want > 0) {
604 /* We found something */
605 path = pf_map_path(tgt_map, ptile);
606 fc_assert(path != NULL);
607 }
608
610
611 return path;
612}
613
614/**********************************************************************/
629 int thresh_move)
630{
631 int count = punit->moves_left + 1; /* break any infinite loops */
632 struct pf_path *path = NULL;
633
636
638 /* This teaches the AI about the dangers inherent in occupychance. */
640
641 while (count-- > 0 && punit->moves_left > 0
643 if (!adv_unit_execute_path(punit, path)) {
644 /* Died */
645 count = -1;
646 }
647 pf_path_destroy(path);
648 path = NULL;
649 }
650
651 fc_assert(NULL == path);
652
654 return (count >= 0);
655}
656
657/**********************************************************************/
661static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer,
662 struct unit *punit)
663{
664 struct unit *aunit = aiguard_charge_unit(ait, punit);
665 struct city *acity = aiguard_charge_city(ait, punit);
666 struct tile *ptile;
667
669 CHECK_GUARD(ait, punit);
670
671 if (aunit && unit_owner(aunit) == unit_owner(punit)) {
672 /* protect a unit */
673 if (aunit->goto_tile != NULL) {
674 /* Our charge is going somewhere: maybe we should meet them there */
675 /* FIXME: This probably isn't the best algorithm for this. */
677 int me2goal = real_map_distance(unit_tile(punit), aunit->goto_tile);
678 int them2goal = real_map_distance(unit_tile(aunit), aunit->goto_tile);
679
680 if (me2goal < me2them
684 ptile = aunit->goto_tile;
685 } else {
686 ptile = unit_tile(aunit);
687 }
688 } else {
689 ptile = unit_tile(aunit);
690 }
691 } else if (acity && city_owner(acity) == unit_owner(punit)) {
692 /* protect a city */
693 ptile = acity->tile;
694 } else {
695 /* should be impossible */
696 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "we lost our charge");
698 return;
699 }
700
701 if (same_pos(unit_tile(punit), ptile)) {
702 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "at RV");
703 } else {
704 if (goto_is_sane(punit, ptile)) {
705 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "meeting charge");
706 if (!dai_gothere(ait, pplayer, punit, ptile)) {
707 /* We died */
708 return;
709 }
710 } else {
711 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "can not meet charge");
713 }
714 }
715 /* We might have stopped because of an enemy nearby.
716 * Perhaps we can kill it.*/
719 && same_pos(unit_tile(punit), ptile)) {
720 def_ai_unit_data(punit, ait)->done = TRUE; /* Stay with charge */
721 }
722}
723
724/**********************************************************************/
727static bool unit_role_defender(const struct unit_type *punittype)
728{
730}
731
732/**********************************************************************/
742adv_want look_for_charge(struct ai_type *ait, const struct civ_map *nmap,
743 struct player *pplayer, struct unit *punit,
744 struct unit **aunit, struct city **acity)
745{
746 struct pf_parameter parameter;
747 struct pf_map *pfm;
748 struct city *pcity;
749 struct ai_city *data, *best_data = NULL;
751 int def, best_def = -1;
752 /* Arbitrary: 3 turns. */
753 const int max_move_cost = 3 * unit_move_rate(punit);
754
755 *aunit = NULL;
756 *acity = NULL;
757
758 if (0 == toughness) {
759 /* useless */
760 return 0;
761 }
762
763 pft_fill_unit_parameter(&parameter, nmap, punit);
764 parameter.omniscience = !has_handicap(pplayer, H_MAP);
765 pfm = pf_map_new(&parameter);
766
767 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
768 if (move_cost > max_move_cost) {
769 /* Consider too far. */
770 break;
771 }
772
773 pcity = tile_city(ptile);
774
775 /* Consider unit bodyguard. */
776 unit_list_iterate(ptile->units, buddy) {
777 const struct unit_type *ptype = unit_type_get(punit);
778 const struct unit_type *buddy_type = unit_type_get(buddy);
779
780 /* TODO: allied unit bodyguard? */
782 || unit_owner(buddy) != pplayer
783 || !aiguard_wanted(ait, buddy)
789
790 continue;
791 }
792
794 if (0 >= def) {
795 continue;
796 }
797
799 /* Reduce want based on move cost. We can't do this for
800 * transports since they move around all the time, leading
801 * to hilarious flip-flops. */
802 def >>= move_cost / (2 * unit_move_rate(punit));
803 }
804 if (def > best_def) {
805 *aunit = buddy;
806 *acity = NULL;
807 best_def = def;
808 }
810
811 /* City bodyguard. TODO: allied city bodyguard? */
812 if (ai_fuzzy(pplayer, TRUE)
813 && NULL != pcity
814 && city_owner(pcity) == pplayer
815 && (data = def_ai_city_data(pcity, ait))
816 && 0 < data->urgency) {
817 if (NULL != best_data
818 && (0 < best_data->grave_danger
819 || best_data->urgency > data->urgency
820 || ((best_data->danger > data->danger
821 || AIUNIT_DEFEND_HOME == def_ai_unit_data(punit, ait)->task)
822 && 0 == data->grave_danger))) {
823 /* Chances are we'll be between cities when we are needed the most!
824 * Resuming pf_map_move_costs_iterate()... */
825 continue;
826 }
827 def = (data->danger - assess_defense_quadratic(ait, pcity));
828 if (def <= 0) {
829 continue;
830 }
831 /* Reduce want based on move cost. */
832 def >>= move_cost / (2 * unit_move_rate(punit));
833 if (def > best_def && ai_fuzzy(pplayer, TRUE)) {
834 *acity = pcity;
835 *aunit = NULL;
836 best_def = def;
837 best_data = data;
838 }
839 }
841
843
844 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "%s(), best_def=%d, type=%s (%d, %d)",
847 : (NULL != *aunit ? unit_rule_name(*aunit) : "")),
849 : (NULL != *aunit ?
852 : (NULL != *aunit ?
854
855 return ((best_def * 100) / toughness);
856}
857
858/**********************************************************************/
862 const struct unit_type *followee,
863 struct ai_type *ait)
864{
865 struct unit_type_ai *utai = utype_ai_data(follower, ait);
866
867 unit_type_list_iterate(utai->potential_charges, pcharge) {
868 if (pcharge == followee) {
869 return TRUE;
870 }
872
873 return FALSE;
874}
875
876/**********************************************************************/
879static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap,
880 struct player *pplayer, struct unit *punit)
881{
882 const struct unit_type *punittype = unit_type_get(punit);
883 struct unit_ai *unit_data;
884
886
887 /* Keep barbarians aggressive and primitive */
888 if (is_barbarian(pplayer)) {
889 if (is_land_barbarian(pplayer)) {
891
893 /* Land barbarians pillage */
895 }
896 }
898
899 return;
900 }
901
903
904 /* If I am a bodyguard, check whether I can do my job. */
905 if (unit_data->task == AIUNIT_ESCORT
906 || unit_data->task == AIUNIT_DEFEND_HOME) {
908 }
909
910 if (aiguard_has_charge(ait, punit)
911 && unit_data->task == AIUNIT_ESCORT) {
912 struct unit *aunit = aiguard_charge_unit(ait, punit);
913 struct city *acity = aiguard_charge_city(ait, punit);
914 struct ai_city *city_data = NULL;
915
916 if (acity != NULL) {
918 }
919
920 /* Check if the city we are on our way to rescue is still in danger,
921 * or the unit we should protect is still alive... */
922 if ((aunit && (aiguard_has_guard(ait, aunit) || aiguard_wanted(ait, aunit))
925 && city_data->urgency != 0
926 && city_data->danger > assess_defense_quadratic(ait, acity))) {
927 return; /* Yep! */
928 } else {
929 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL); /* Nope! */
930 }
931 }
932
933 /* Is the unit badly damaged? */
934 if ((unit_data->task == AIUNIT_RECOVER
935 && punit->hp < punittype->hp)
936 || punit->hp < punittype->hp * 0.25) { /* WAG */
937 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "set to hp recovery");
939 return;
940 }
941
944 /* This is a defending unit that doesn't need to stay put.
945 * It needs to defend something, but not necessarily where it's at.
946 * Therefore, it will consider becoming a bodyguard. -- Syela */
947 struct city *acity;
948 struct unit *aunit;
949
950 look_for_charge(ait, nmap, pplayer, punit, &aunit, &acity);
951 if (acity) {
954 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend city");
955 } else if (aunit) {
958 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend unit");
959 }
960 }
962}
963
964/**********************************************************************/
973static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
974 struct unit *punit)
975{
976 struct city *pcity = aiguard_charge_city(ait, punit);
977
979
980 if (!pcity || city_owner(pcity) != pplayer) {
982 /* Do not stay defending an allied city forever */
984 }
985
986 if (!pcity) {
987 /* Try to find a place to rest. Sitting duck out in the wilderness
988 * is generally a bad idea, since we protect no cities that way, and
989 * it looks silly. */
993 }
994
995 if (!pcity) {
997 }
998
1001 /* ... we survived */
1002 if (pcity) {
1003 UNIT_LOG(LOG_DEBUG, punit, "go to defend %s", city_name_get(pcity));
1004 if (same_pos(unit_tile(punit), pcity->tile)) {
1005 UNIT_LOG(LOG_DEBUG, punit, "go defend successful");
1007 } else {
1008 (void) dai_gothere(ait, pplayer, punit, pcity->tile);
1009 }
1010 } else {
1011 UNIT_LOG(LOG_VERBOSE, punit, "defending nothing...?");
1012 }
1013 }
1014}
1015
1016/**********************************************************************/
1020 const struct unit_type *utype,
1021 int which)
1022{
1023 int attacks;
1024
1025 if (utype_action_takes_all_mp(utype,
1027 attacks = 1;
1028 } else {
1029 attacks = utype->move_rate;
1030 }
1031 city_data->invasion.attack += attacks;
1032 if (which == INVASION_OCCUPY) {
1033 city_data->invasion.occupy++;
1034 }
1035}
1036
1037/**********************************************************************/
1047static bool invasion_funct(struct ai_type *ait, struct unit *punit,
1048 bool dest, int radius, int which)
1049{
1050 struct tile *ptile;
1051 struct player *pplayer = unit_owner(punit);
1052
1054
1055 if (dest) {
1057
1058 ptile = punit->goto_tile;
1059 } else {
1060 ptile = unit_tile(punit);
1061 }
1062
1063 square_iterate(&(wld.map), ptile, radius, tile1) {
1064 struct city *pcity = tile_city(tile1);
1065
1066 if (pcity
1068 && (dest || !has_defense(pcity))) {
1069 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1070
1071 /* Unit itself */
1073
1074 /* Cargo */
1075 unit_cargo_iterate(punit, cargo) {
1076 const struct unit_type *utype = unit_type_get(cargo);
1077
1078 if (IS_ATTACKER(utype)) {
1082 }
1084 }
1086
1087 return TRUE;
1088}
1089
1090/**********************************************************************/
1093bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map,
1094 struct tile *dest_tile,
1095 const struct unit_type *cargo_type,
1096 const struct unit_type *ferry_type,
1097 struct tile **ferry_dest, struct tile **beachhead_tile)
1098{
1099 if (NULL == tile_city(dest_tile)
1101 /* Unit can directly go to 'dest_tile'. */
1102 struct tile *best_tile = NULL;
1104
1105 if (NULL != beachhead_tile) {
1106 *beachhead_tile = dest_tile;
1107 }
1108
1109 adjc_iterate(&(wld.map), dest_tile, ptile) {
1111 if (cost != PF_IMPOSSIBLE_MC
1112 && (NULL == best_tile || cost < best_cost)) {
1113 best_tile = ptile;
1114 best_cost = cost;
1115 }
1117
1118 if (NULL != ferry_dest) {
1120 }
1121
1122 return (PF_IMPOSSIBLE_MC != best_cost);
1123 } else {
1124 /* We need to find a beach around 'dest_tile'. */
1125 struct tile *best_tile = NULL, *best_beach = NULL;
1129
1130 tile_list_append(checked_tiles, dest_tile);
1131 adjc_iterate(&(wld.map), dest_tile, beach) {
1133 /* Can land there. */
1134 adjc_iterate(&(wld.map), beach, ptile) {
1135 if (!tile_list_search(checked_tiles, ptile)
1136 && !is_non_allied_unit_tile(ptile, pplayer,
1137 flagless_ferry)) {
1140 if (cost != PF_IMPOSSIBLE_MC
1141 && (NULL == best_tile || cost < best_cost)) {
1142 best_beach = beach;
1143 best_tile = ptile;
1144 best_cost = cost;
1145 }
1146 }
1148 }
1150
1152
1153 if (NULL != beachhead_tile) {
1155 }
1156 if (NULL != ferry_dest) {
1158 }
1159 return (PF_IMPOSSIBLE_MC != best_cost);
1160 }
1161}
1162
1163/**********************************************************************/
1171adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
1172 struct unit *punit,
1173 struct tile **pdest_tile, struct pf_path **ppath,
1174 struct pf_map **pferrymap,
1175 struct unit **pferryboat,
1176 const struct unit_type **pboattype, int *pmove_time)
1177{
1178 const int attack_value = adv_unit_att_rating(punit); /* basic attack. */
1179 struct pf_parameter parameter;
1180 struct pf_map *punit_map, *ferry_map;
1181 struct pf_position pos;
1183 const struct unit_type *punit_type = unit_type_get(punit);
1184 struct tile *punit_tile = unit_tile(punit);
1185 /* Type of our boat (a future one if ferryboat == NULL). */
1186 const struct unit_type *boattype = NULL;
1187 struct unit *ferryboat = NULL;
1188 struct city *pcity;
1189 struct ai_city *acity_data;
1190 int bcost, bcost_bal; /* Build cost of the attacker (+adjustments). */
1191 bool handicap = has_handicap(pplayer, H_TARGETS);
1192 bool unhap = FALSE; /* Do we make unhappy citizen. */
1193 bool harbor = FALSE; /* Do we have access to sea? */
1194 bool go_by_boat; /* Whether we need a boat or not. */
1195 int vulnerability; /* Enemy defense rating. */
1196 adv_want benefit; /* Benefit from killing the target. */
1197 struct unit *pdefender; /* Enemy city defender. */
1198 int move_time; /* Turns needed to target. */
1199 int reserves;
1200 int attack; /* Our total attack value with reinforcements. */
1201 int victim_count; /* Number of enemies there. */
1202 int needferry; /* Cost of building a ferry boat. */
1203 /* This is a kluge, because if we don't set x and y with !punit->id,
1204 * p_a_w isn't called, and we end up not wanting ironclads and therefore
1205 * never learning steam engine, even though ironclads would be very
1206 * useful. -- Syela */
1207 adv_want bk = 0;
1208 adv_want want; /* Want (amortized) of the operation. */
1209 adv_want best = 0; /* Best of all wants. */
1210 struct tile *goto_dest_tile = NULL;
1211 bool can_occupy;
1212 struct civ_map *nmap = &(wld.map);
1213
1214 /* Very preliminary checks. */
1216 if (NULL != pferrymap) {
1217 *pferrymap = NULL;
1218 }
1219 if (NULL != pferryboat) {
1220 *pferryboat = NULL;
1221 }
1222 if (NULL != pboattype) {
1223 *pboattype = NULL;
1224 }
1225 if (NULL != pmove_time) {
1226 *pmove_time = 0;
1227 }
1228 if (NULL != ppath) {
1229 *ppath = NULL;
1230 }
1231
1232 if (0 == attack_value) {
1233 /* A very poor attacker... probably low on HP. */
1234 return 0;
1235 }
1236
1238
1239
1240 /*** Part 1: Calculate targets ***/
1241
1242 /* This horrible piece of code attempts to calculate the attractiveness of
1243 * enemy cities as targets for our units, by checking how many units are
1244 * going towards it or are near it already. */
1245
1246 /* Reset enemy cities data. */
1248 /* See comment below in next usage of POTENTIALLY_HOSTILE_PLAYER. */
1249 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1250 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1251 continue;
1252 }
1253 city_list_iterate(aplayer->cities, acity) {
1254 struct ai_city *city_data = def_ai_city_data(acity, ait);
1255
1257 &city_data->attack,
1258 &city_data->bcost);
1259 city_data->invasion.attack = 0;
1260 city_data->invasion.occupy = 0;
1263
1264 /* Second, calculate in units on their way there, and mark targets for
1265 * invasion */
1266 unit_list_iterate(pplayer->units, aunit) {
1267 const struct unit_type *atype;
1268
1269 if (aunit == punit) {
1270 continue;
1271 }
1272
1274
1275 /* Dealing with invasion stuff */
1276 if (IS_ATTACKER(atype)) {
1277 if (aunit->activity == ACTIVITY_GOTO) {
1278 if (invasion_funct(ait, aunit, TRUE, 0,
1281 && (pcity = tile_city(aunit->goto_tile))) {
1282 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1283
1286 }
1287 }
1291 } else if (def_ai_unit_data(aunit, ait)->passenger != 0
1293 /* It's a transport with reinforcements */
1294 if (aunit->activity == ACTIVITY_GOTO) {
1296 }
1298 }
1300 /* end horrible initialization subroutine */
1301
1302
1303 /*** Part 2: Now pick one target ***/
1304
1305 /* We first iterate through all cities, then all units, looking
1306 * for a viable target. We also try to gang up on the target
1307 * to avoid spreading out attacks too widely to be inefficient.
1308 */
1309
1311 if (NULL != pcity && (0 == punit->id || pcity->id == punit->homecity)) {
1312 /* I would have thought unhappiness should be taken into account
1313 * irrespectfully the city in which it will surface... -- GB */
1315 }
1316
1319
1321 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1322 punit_map = pf_map_new(&parameter);
1323
1324 if (MOVE_NONE == punit_class->adv.sea_move) {
1325 /* We need boat to move over sea. */
1326 ferryboat = unit_transport_get(punit);
1327
1328 /* First check if we can use the boat we are currently loaded to. */
1329 if (ferryboat == NULL || !is_boat_free(ait, ferryboat, punit, 0)) {
1330 /* No, we cannot control current boat */
1331 ferryboat = NULL;
1332 }
1333
1334 if (NULL == ferryboat) {
1335 /* Try to find new boat */
1336 ferryboat = player_unit_by_number(pplayer,
1337 aiferry_find_boat(ait, punit, 1, NULL));
1338 }
1339
1341 harbor = TRUE;
1342 }
1343 }
1344
1345 if (NULL != ferryboat) {
1346 boattype = unit_type_get(ferryboat);
1347 pft_fill_unit_overlap_param(&parameter, nmap, ferryboat);
1348 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1349 ferry_map = pf_map_new(&parameter);
1350 } else {
1352 if (NULL == boattype) {
1353 /* We pretend that we can have the simplest boat to stimulate tech. */
1355 }
1356 if (NULL != boattype && harbor) {
1357 /* Let's simulate a boat at 'punit' position. */
1359 punit_tile, pplayer);
1360 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1361 ferry_map = pf_map_new(&parameter);
1362 } else {
1363 ferry_map = NULL;
1364 }
1365 }
1366
1368
1370 /* For the virtual unit case, which is when we are called to evaluate
1371 * which units to build, we want to calculate in danger and which
1372 * players we want to make war with in the future. We do _not_ want
1373 * to do this when actually making attacks. */
1374 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1375 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1376 continue; /* Not an enemy. */
1377 }
1378
1379 city_list_iterate(aplayer->cities, acity) {
1380 struct tile *atile = city_tile(acity);
1381
1384 /* Can't attack this city. It is on land. */
1385 continue;
1386 }
1387
1388 if (handicap && !map_is_known(atile, pplayer)) {
1389 /* Can't see it */
1390 continue;
1391 }
1392
1394 go_by_boat = FALSE;
1395 move_time = pos.turn;
1396 } else if (NULL == ferry_map) {
1397 continue; /* Impossible to handle. */
1398 } else {
1399 struct tile *dest, *beach;
1400
1401 if (!find_beachhead(pplayer, ferry_map, atile, punit_type,
1402 boattype, &dest, &beach)) {
1403 continue; /* Impossible to go by boat. */
1404 }
1405 if (!pf_map_position(ferry_map, dest, &pos)) {
1407 continue;
1408 }
1409 move_time = pos.turn; /* Sailing time. */
1410 if (dest != beach) {
1411 move_time++; /* Land time. */
1412 }
1413 if (NULL != ferryboat && unit_tile(ferryboat) != punit_tile) {
1414 if (pf_map_position(punit_map, unit_tile(ferryboat), &pos)) {
1415 move_time += pos.turn; /* Time to reach the boat. */
1416 } else {
1417 continue; /* Cannot reach the boat. */
1418 }
1419 }
1420 go_by_boat = TRUE;
1421 }
1422
1427 } else {
1428 pdefender = NULL;
1429 vulnerability = 0;
1430 benefit = 0;
1431 }
1432
1433 if (1 < move_time) {
1435
1436 if (def_type) {
1440 if (v > vulnerability) {
1441 /* They can build a better defender! */
1442 vulnerability = v;
1444 }
1445 }
1446 }
1447
1449
1450 reserves = (acity_data->invasion.attack
1451 - unit_list_size(acity->tile->units));
1452
1453 if (punit->id == 0) {
1454 /* Real unit would add to reserves once built. */
1457 reserves++;
1458 } else {
1459 reserves += punit_type->move_rate;
1460 }
1461 }
1462
1463 if (0 < reserves && (can_occupy
1464 || 0 < acity_data->invasion.occupy)) {
1465 /* There are units able to occupy the city after all defenders
1466 * are killed! */
1467 benefit += acity_data->worth * reserves / 5;
1468 }
1469
1470 attack = attack_value + acity_data->attack;
1471 attack *= attack;
1472 /* Avoiding handling upkeep aggregation this way -- Syela */
1473
1474 /* AI was not sending enough reinforcements to totally wipe out a city
1475 * and conquer it in one turn.
1476 * This variable enables total carnage. -- Syela */
1478
1479 if (!can_occupy && NULL == pdefender) {
1480 /* Nothing there to bash and we can't occupy!
1481 * Not having this check caused warships yoyoing */
1482 want = 0;
1483 } else if (10 < move_time) {
1484 /* Too far! */
1485 want = 0;
1486 } else if (can_occupy && 0 == acity_data->invasion.occupy
1487 && (0 < acity_data->invasion.attack
1488 || victim_count == 0)) {
1489 /* Units able to occupy really needed there! */
1490 want = bcost * SHIELD_WEIGHTING;
1491 } else {
1492 want = kill_desire(benefit, attack, bcost + acity_data->bcost,
1494 }
1497 /* Build_cost of ferry. */
1498 needferry = (go_by_boat && NULL == ferryboat
1500 /* FIXME: add time to build the ferry? */
1502 want, MAX(1, move_time),
1504
1505 /* BEGIN STEAM-ENGINES-ARE-OUR-FRIENDS KLUGE. */
1506 if (0 >= want && 0 == punit->id && 0 >= best) {
1510 MAX(1, move_time),
1512
1513 if (bk_e > bk) {
1514 *pdest_tile = atile;
1515 if (NULL != pferrymap) {
1517 }
1518 if (NULL != pferryboat) {
1519 *pferryboat = go_by_boat ? ferryboat : NULL;
1520 }
1521 if (NULL != pboattype) {
1523 }
1524 if (NULL != pmove_time) {
1526 }
1527 goto_dest_tile = (go_by_boat && NULL != ferryboat
1528 ? unit_tile(ferryboat) : atile);
1529 bk = bk_e;
1530 }
1531 }
1532 /* END STEAM-ENGINES KLUGE */
1533
1534 if (0 != punit->id
1535 && NULL != ferryboat
1536 && punit_class->adv.sea_move == MOVE_NONE) {
1538 "%s(): with boat %s@(%d, %d) -> %s@(%d, %d)"
1539 " (go_by_boat = %d, move_time = %d, want = " ADV_WANT_PRINTF
1540 ", best = " ADV_WANT_PRINTF ")",
1541 __FUNCTION__, unit_rule_name(ferryboat),
1542 TILE_XY(unit_tile(ferryboat)), city_name_get(acity),
1543 TILE_XY(atile), go_by_boat, move_time, want, best);
1544 }
1545
1546 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1547 /* Yes, we like this target */
1548 best = want;
1549 *pdest_tile = atile;
1550 if (NULL != pferrymap) {
1552 }
1553 if (NULL != pferryboat) {
1554 *pferryboat = go_by_boat ? ferryboat : NULL;
1555 }
1556 if (NULL != pboattype) {
1558 }
1559 if (NULL != pmove_time) {
1561 }
1562 goto_dest_tile = (go_by_boat && NULL != ferryboat
1563 ? unit_tile(ferryboat) : atile);
1564 }
1566
1568 /* I'm not sure the following code is good but it seems to be adequate.
1569 * I am deliberately not adding ferryboat code to the unit_list_iterate.
1570 * -- Syela */
1572 struct tile *atile = unit_tile(aunit);
1573
1574 if (NULL != tile_city(atile)) {
1575 /* already dealt with it. */
1576 continue;
1577 }
1578
1579 if (handicap && !map_is_known(atile, pplayer)) {
1580 /* Can't see the target. */
1581 continue;
1582 }
1583
1585 && 0 == punit->id) {
1586 /* We will not build units just to chase caravans and
1587 * ambassadors. */
1588 continue;
1589 }
1590
1591 /* We have to assume the attack is diplomatically ok.
1592 * We cannot use can_player_attack_tile, because we might not
1593 * be at war with aplayer yet */
1595 || aunit != get_defender(nmap, punit, atile, NULL)) {
1596 /* We cannot attack it, or it is not the main defender. */
1597 continue;
1598 }
1599
1601 /* Cannot reach it. */
1602 continue;
1603 }
1604
1607
1608 move_time = pos.turn;
1609 if (10 < move_time) {
1610 /* Too far. */
1611 want = 0;
1612 } else {
1613 want = kill_desire(benefit, attack, bcost, vulnerability, 1);
1614 /* Take into account maintenance of the unit. */
1615 /* FIXME: Depends on the government. */
1616 want -= move_time * SHIELD_WEIGHTING;
1617 /* Take into account unhappiness
1618 * (costs 2 luxuries to compensate). */
1619 want -= (unhap ? 2 * move_time * TRADE_WEIGHTING : 0);
1620 }
1622 want, MAX(1, move_time), bcost_bal);
1623 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1624 best = want;
1625 *pdest_tile = atile;
1626 if (NULL != pferrymap) {
1627 *pferrymap = NULL;
1628 }
1629 if (NULL != pferryboat) {
1630 *pferryboat = NULL;
1631 }
1632 if (NULL != pboattype) {
1633 *pboattype = NULL;
1634 }
1635 if (NULL != pmove_time) {
1637 }
1639 }
1642
1643 if (NULL != ppath) {
1646 }
1647
1649 if (NULL != ferry_map
1650 && (NULL == pferrymap || *pferrymap != ferry_map)) {
1652 }
1653
1655
1656 return best;
1657}
1658
1659/**********************************************************************/
1668{
1669 struct pf_parameter parameter;
1670 struct pf_map *pfm;
1671 struct player *pplayer = unit_owner(punit);
1672 struct city *pcity, *best_city = NULL;
1673 int best = FC_INFINITY, cur;
1674 const struct civ_map *nmap = &(wld.map);
1675
1676 pft_fill_unit_parameter(&parameter, nmap, punit);
1677 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1678 pfm = pf_map_new(&parameter);
1679
1680 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
1681 if (move_cost > best) {
1682 /* We already found a better city. No need to continue. */
1683 break;
1684 }
1685
1686 pcity = tile_city(ptile);
1687 if (NULL == pcity || !pplayers_allied(pplayer, city_owner(pcity))) {
1688 continue;
1689 }
1690
1691 /* Score based on the move cost. */
1692 cur = move_cost;
1693
1694 /* Note the unit owner may be different from the city owner. */
1695 if (0 == get_unittype_bonus(unit_owner(punit), ptile,
1697 EFT_HP_REGEN)) {
1698 /* If we cannot regen fast our hit points here, let's make some
1699 * penalty. */
1700 cur *= 3;
1701 }
1702
1703 if (cur < best) {
1704 best_city = pcity;
1705 best = cur;
1706 }
1708
1710 return best_city;
1711}
1712
1713/**********************************************************************/
1719 struct player *pplayer,
1720 struct unit *punit)
1721{
1722 struct city *pc;
1723 bool only_continent = TRUE;
1724
1725 if (unit_transported(punit)) {
1726 /* If we are in transport, we can go to any continent.
1727 * Actually, we are not currently in a continent where to stay. */
1729 }
1730
1731 if ((pc = find_closest_city(unit_tile(punit), NULL, pplayer, FALSE,
1734 UNIT_LOG(LOG_DEBUG, punit, "Barbarian heading to conquer %s",
1735 city_name_get(pc));
1736 (void) dai_gothere(ait, pplayer, punit, pc->tile);
1737 } else {
1738 struct unit *ferry = NULL;
1739
1740 if (unit_transported(punit)) {
1741 ferry = unit_transport_get(punit);
1742
1743 /* We already are in a boat so it needs no
1744 * free capacity */
1745 if (!is_boat_free(ait, ferry, punit, 0)) {
1746 /* We cannot control our ferry. */
1747 ferry = NULL;
1748 }
1749 } else {
1750 /* We are not in a boat yet. Search for one. */
1752 if (is_boat_free(ait, aunit, punit, 1)
1754 ferry = aunit;
1755 break;
1756 }
1758 }
1759
1760 if (ferry) {
1761 UNIT_LOG(LOG_DEBUG, punit, "Barbarian sailing to conquer %s",
1762 city_name_get(pc));
1763 (void) aiferry_goto_amphibious(ait, ferry, punit, pc->tile);
1764 } else {
1765 /* This is not an error. Somebody else might be in charge
1766 * of the ferry. */
1767 UNIT_LOG(LOG_DEBUG, punit, "unable to find barbarian ferry");
1768 }
1769 }
1770 } else {
1771 UNIT_LOG(LOG_DEBUG, punit, "Barbarian find no target city");
1772 }
1773}
1774
1775/**********************************************************************/
1781static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
1782 struct unit *punit)
1783{
1784 struct tile *dest_tile;
1785 int id = punit->id;
1786 int ct = 10;
1787 struct city *pcity = NULL;
1788
1790
1791 /* Barbarians pillage, and might keep on doing that so they sometimes
1792 * even finish it. */
1793 if (punit->activity == ACTIVITY_PILLAGE && is_barbarian(pplayer)
1794 && fc_rand(2) == 1) {
1795 return;
1796 }
1797
1798 /* First find easy nearby enemies, anything better than pillage goes.
1799 * NB: We do not need to repeat dai_military_rampage, it is repeats itself
1800 * until it runs out of targets. */
1801 /* FIXME: 1. dai_military_rampage never checks if it should defend newly
1802 * conquered cities.
1803 * FIXME: 2. would be more convenient if it returned FALSE if we run out
1804 * of moves too.*/
1806 return; /* we died */
1807 }
1808
1809 if (punit->moves_left <= 0) {
1810 return;
1811 }
1812
1813 /* Main attack loop */
1814 do {
1815 struct tile *start_tile = unit_tile(punit);
1816 struct pf_path *path;
1817 struct unit *ferryboat;
1818
1819 /* Then find enemies the hard way */
1820 find_something_to_kill(ait, pplayer, punit, &dest_tile, &path,
1821 NULL, &ferryboat, NULL, NULL);
1822 if (!same_pos(unit_tile(punit), dest_tile)) {
1823 if (!is_tiles_adjacent(unit_tile(punit), dest_tile)
1824 || !can_unit_attack_tile(punit, NULL, dest_tile)) {
1825
1826 /* Adjacent and can't attack usually means we are not marines
1827 * and on a ferry. This fixes the problem (usually). */
1828 UNIT_LOG(LOG_DEBUG, punit, "mil att gothere -> (%d, %d)",
1829 TILE_XY(dest_tile));
1830
1831 /* Set ACTIVITY_GOTO more permanently than just inside
1832 * adv_follow_path(). This way other units will know we're
1833 * on our way even if we don't reach target yet. */
1834 punit->goto_tile = dest_tile;
1836 if (NULL != path && !adv_follow_path(punit, path, dest_tile)) {
1837 /* Died. */
1838 pf_path_destroy(path);
1839 return;
1840 }
1841 if (NULL != ferryboat) {
1842 /* Need a boat. */
1843 aiferry_gobyboat(ait, pplayer, punit, dest_tile, FALSE);
1844 pf_path_destroy(path);
1845 return;
1846 }
1847 if (0 >= punit->moves_left) {
1848 /* No moves left. */
1849 pf_path_destroy(path);
1850 return;
1851 }
1852
1853 /* Either we're adjacent or we sitting on the tile. We might be
1854 * sitting on the tile if the enemy that _was_ sitting there
1855 * attacked us and died _and_ we had enough movement to get there */
1856 if (same_pos(unit_tile(punit), dest_tile)) {
1857 UNIT_LOG(LOG_DEBUG, punit, "mil att made it -> (%d, %d)",
1858 TILE_XY(dest_tile));
1859 pf_path_destroy(path);
1860 break;
1861 }
1862 }
1863
1864 if (is_tiles_adjacent(unit_tile(punit), dest_tile)) {
1865 /* Close combat. fstk sometimes want us to attack an adjacent
1866 * enemy that rampage wouldn't */
1867 UNIT_LOG(LOG_DEBUG, punit, "mil att bash -> (%d, %d)",
1868 TILE_XY(dest_tile));
1869 if (!dai_unit_attack(ait, punit, dest_tile)) {
1870 /* Died */
1871 pf_path_destroy(path);
1872 return;
1873 }
1874 } else if (!same_pos(start_tile, unit_tile(punit))) {
1875 /* Got stuck. Possibly because of adjacency to an
1876 * enemy unit. Perhaps we are in luck and are now next to a
1877 * tempting target? Let's find out... */
1880 pf_path_destroy(path);
1881 return;
1882 }
1883
1884 } else {
1885 /* FIXME: This happens a bit too often! */
1886 UNIT_LOG(LOG_DEBUG, punit, "fstk didn't find us a worthy target!");
1887 /* No worthy enemies found, so abort loop */
1888 ct = 0;
1889 }
1890 pf_path_destroy(path);
1891
1892 ct--; /* infinite loops from railroads must be stopped */
1893 } while (punit->moves_left > 0 && ct > 0);
1894
1895 /* Cleanup phase */
1896 if (punit->moves_left == 0) {
1897 return;
1898 }
1900 if (pcity != NULL
1901 && (dai_is_ferry(punit, ait)
1902 || punit->hp < unit_type_get(punit)->hp * 0.50)) { /* WAG */
1903 /* Go somewhere safe */
1904 UNIT_LOG(LOG_DEBUG, punit, "heading to nearest safe house.");
1905 (void) dai_unit_goto(ait, punit, pcity->tile);
1906 } else if (!is_barbarian(pplayer)) {
1907 /* Nothing else to do, so try exploring. */
1908 switch (manage_auto_explorer(punit)) {
1909 case MR_DEATH:
1910 /* don't use punit! */
1911 return;
1912 case MR_OK:
1913 UNIT_LOG(LOG_DEBUG, punit, "nothing else to do, so exploring");
1914 break;
1915 default:
1916 UNIT_LOG(LOG_DEBUG, punit, "nothing to do - no more exploring either");
1917 break;
1918 };
1919 } else {
1920 /* You can still have some moves left here, but barbarians should
1921 not sit helplessly, but advance towards nearest known enemy city */
1922 UNIT_LOG(LOG_DEBUG, punit, "attack: barbarian");
1923 dai_military_attack_barbarian(ait, pplayer, punit);
1924 }
1925 if ((punit = game_unit_by_number(id)) && punit->moves_left > 0) {
1926 UNIT_LOG(LOG_DEBUG, punit, "attack: giving up unit to defense");
1927 dai_military_defend(ait, pplayer, punit);
1928 }
1929}
1930
1931/**********************************************************************/
1935static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
1936{
1937 bool alive = TRUE;
1938 int ferryboat = 0;
1939 struct pf_path *path_to_ferry = NULL;
1940
1941 UNIT_LOG(LOG_CARAVAN, punit, "requesting a boat!");
1942 ferryboat = aiferry_find_boat(ait, punit, 1, &path_to_ferry);
1943 /* going to meet the boat */
1944 if ((ferryboat <= 0)) {
1946 "in find_boat_for_unit cannot find any boats.");
1947 /* if we are undefended on the country side go to a city */
1949 if (current_city == NULL) {
1951 if (city_near != NULL) {
1953 }
1954 }
1955 } else {
1956 if (path_to_ferry != NULL) {
1958 /* Died. */
1961 alive = FALSE;
1962 } else {
1965 alive = TRUE;
1966 }
1967 }
1968 }
1969 return alive;
1970}
1971
1972/**********************************************************************/
1981 struct tile *ctile, struct tile *ptile)
1982{
1983 /* We usually have Inaccessible terrain, so not testing MOVE_FULL == */
1984 bool lm = MOVE_NONE != pclass->adv.land_move,
1985 sm = MOVE_NONE != pclass->adv.sea_move;
1986 struct civ_map *pmap = &(wld.map);
1987
1988 if (lm && sm) {
1989 return FALSE;
1990 }
1991
1992 /* We could use adjc_iterate() but likely often tiles are on the same
1993 * continent and it will be more time to find where they connect */
1996
1997 if (is_ocean_tile(atile) ? sm : lm) {
1998 iterate_outward(pmap, ptile, 1, btile) {
1999 if (tile_continent(btile) == acont) {
2000 return FALSE;
2001 }
2003 }
2005
2006 if (is_tiles_adjacent(ctile, ptile)) {
2007 return FALSE;
2008 }
2009
2010 return TRUE;
2011}
2012
2013/**********************************************************************/
2019static void dai_caravan_goto(struct ai_type *ait, struct player *pplayer,
2020 struct unit *punit,
2021 const struct city *dest_city,
2022 bool help_wonder,
2023 bool required_boat, bool request_boat)
2024{
2025 bool alive = TRUE;
2026 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2027 const struct civ_map *nmap = &(wld.map);
2028
2030
2031 /* if we're not there yet, and we can move, move... */
2032 if (!same_pos(dest_city->tile, unit_tile(punit))
2033 && punit->moves_left != 0) {
2034 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) task %s going to %s in %s %s",
2038 help_wonder ? "help a wonder" : "trade", city_name_get(dest_city),
2039 required_boat ? "with a boat" : "");
2040 if (required_boat) {
2041 /* to trade with boat */
2042 if (request_boat) {
2043 /* Try to find new boat */
2045 } else {
2046 /* if we are not being transported then ask for a boat again */
2047 alive = TRUE;
2050 unit_tile(punit), dest_city->tile)) {
2052 }
2053 }
2054 if (alive) {
2055 /* FIXME: sometimes we get FALSE here just because
2056 * a trireme that we've boarded can't go over an ocean. */
2057 alive = dai_gothere(ait, pplayer, punit, dest_city->tile);
2058 }
2059 } else {
2060 /* to trade without boat */
2062 }
2063 }
2064
2065 /* if moving didn't kill us, and we got to the destination, handle it. */
2066 if (alive && real_map_distance(dest_city->tile, unit_tile(punit)) <= 1) {
2067 /* release the boat! */
2068 if (unit_transported(punit)) {
2070 }
2072 punit, dest_city)) {
2073 /*
2074 * We really don't want to just drop all caravans in immediately.
2075 * Instead, we want to aggregate enough caravans to build instantly.
2076 * -AJS, 990704
2077 */
2078 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) helps build wonder in %s",
2081 punit->id,
2084 unit_do_action(pplayer, punit->id, dest_city->id,
2085 0, "", ACTION_HELP_WONDER);
2087 punit, dest_city)) {
2088 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) creates trade route in %s",
2091 punit->id,
2094 unit_do_action(pplayer, punit->id, dest_city->id,
2095 0, "", ACTION_TRADE_ROUTE);
2097 punit, dest_city)) {
2098 /* Get the one time bonus. */
2099 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) enters marketplace of %s",
2102 punit->id,
2105 unit_do_action(pplayer, punit->id, dest_city->id,
2106 0, "", ACTION_MARKETPLACE);
2107 } else {
2108 enum log_level level = LOG_NORMAL;
2109
2110 if (help_wonder) {
2111 /* A Caravan ordered to help build wonder may arrive after
2112 * enough shields to build the wonder is produced. */
2114 }
2115
2117 "%s %s[%d](%d,%d) unable to trade with %s",
2120 punit->id,
2123 }
2124 }
2125}
2126
2127/**********************************************************************/
2131static void caravan_optimize_callback(const struct caravan_result *result,
2132 void *data)
2133{
2134 const struct unit *caravan = data;
2135
2136 log_base(LOG_CARAVAN3, "%s %s[%d](%d,%d) %s: %s %s worth %g",
2138 unit_rule_name(caravan),
2139 caravan->id,
2140 TILE_XY(unit_tile(caravan)),
2141 city_name_get(result->src),
2142 result->help_wonder ? "wonder in" : "trade to",
2143 city_name_get(result->dest),
2144 result->value);
2145}
2146
2147/**********************************************************************/
2151 struct unit *punit)
2152{
2153 struct tile *src = NULL, *dest = NULL, *src_home_city = NULL;
2154 struct city *phome_city = NULL;
2155 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2156
2157 if ((unit_data->task != AIUNIT_NONE)) {
2158 src = unit_tile(punit);
2160 if (phome_city != NULL) {
2162 }
2163 dest = punit->goto_tile;
2164
2165 if (src == NULL || dest == NULL) {
2166 return FALSE;
2167 }
2168
2169 /* If we have a home continent, and are not there.
2170 * (FIXME: well, why?)
2171 * (I guess because home continent is which we were supposed to leave,
2172 * not the target continent) */
2173 if (src_home_city != NULL
2175 return FALSE;
2176 }
2177
2178 if (!goto_is_sane(punit, dest)) {
2179 if (unit_transported(punit)) {
2180 /* If we're being transported */
2181 return FALSE;
2182 }
2183 if ((punit->birth_turn + 15 < game.info.turn)) {
2184 /* We are tired of waiting */
2186
2187 if (ferrys <= 0) {
2188 /* There are no ferrys available... give up */
2189 return TRUE;
2190 } else {
2191 if (punit->birth_turn + 20 < game.info.turn) {
2192 /* We are fed up! */
2193 return TRUE;
2194 }
2195 }
2196 }
2197 }
2198 }
2199
2200 return FALSE;
2201}
2202
2203/**********************************************************************/
2210 struct unit *punit)
2211{
2213 Continent_id continent;
2214
2215 fc_assert(pcity != NULL);
2216
2217 if (unit_class_get(punit)->adv.ferry_types <= 0) {
2218 /* There is just no possible transporters. */
2219 return FALSE;
2220 }
2221 continent = tile_continent(pcity->tile);
2222
2223 /* Look for proper destination city at different continent. */
2224 city_list_iterate(pplayer->cities, acity) {
2226 if (tile_continent(acity->tile) != continent) {
2227 return TRUE;
2228 }
2229 }
2231
2233 if (aplayer == pplayer || !aplayer->is_alive) {
2234 continue;
2235 }
2236 if (pplayers_allied(pplayer, aplayer)) {
2237 city_list_iterate(aplayer->cities, acity) {
2239 if (tile_continent(acity->tile) != continent) {
2240 return TRUE;
2241 }
2242 }
2245 }
2246
2247 return FALSE;
2248}
2249
2250/**********************************************************************/
2254static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
2255{
2256 struct city *nearest = NULL;
2257 int min_dist = FC_INFINITY;
2258 struct tile *current_loc = unit_tile(punit);
2260 bool alive = TRUE;
2261
2263 struct tile *ctile = city_tile(pcity);
2264
2265 if (tile_continent(ctile) == continent) {
2267
2268 if (this_dist < min_dist) {
2270 nearest = pcity;
2271 }
2272 }
2274
2275 if (nearest != NULL) {
2277 if (alive && same_pos(unit_tile(punit), nearest->tile)) {
2279 }
2280 }
2281
2282 return alive;
2283}
2284
2285/**********************************************************************/
2291static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
2292 struct unit *punit)
2293{
2294 struct caravan_parameter parameter;
2295 struct caravan_result result;
2296 const struct city *homecity;
2297 const struct city *dest = NULL;
2298 struct unit_ai *unit_data;
2300 bool expect_boats = pclass->adv.ferry_types > 0;
2301 /* TODO: will pplayer have a boat for punit in a reasonable time? */
2302 bool help_wonder = FALSE;
2303 bool required_boat = FALSE;
2304 bool request_boat = FALSE;
2306 const struct civ_map *nmap = &(wld.map);
2307
2309
2313 /* we only want units that can establish trade, enter marketplace or
2314 * help build wonders */
2315 return;
2316 }
2317
2319
2320 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) task %s to (%d,%d)",
2325
2326 homecity = game_city_by_number(punit->homecity);
2327 if (homecity == NULL && unit_data->task == AIUNIT_TRADE) {
2329 return;
2330 }
2331 homecity = game_city_by_number(punit->homecity);
2332 if (homecity == NULL) {
2333 return;
2334 }
2335 }
2336
2337 if ((unit_data->task == AIUNIT_TRADE
2338 || unit_data->task == AIUNIT_WONDER)) {
2339 /* We are moving to our destination */
2340 /* We check to see if our current goal is feasible */
2342 struct goods_type *pgood = unit_current_goods(punit, homecity);
2343
2344 if ((city_dest == NULL)
2346 || (unit_data->task == AIUNIT_TRADE
2347 && !(can_cities_trade(homecity, city_dest)
2349 pgood->replace_priority)))
2350 || (unit_data->task == AIUNIT_WONDER
2351 /* Helping the (new) production is illegal. */
2353 || (unit_data->task == AIUNIT_TRADE
2358 punit, city_dest)))
2359 || (unit_data->task == AIUNIT_WONDER
2362 punit, city_dest))) {
2363 /* destination invalid! */
2365 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) destination invalid!",
2368 } else {
2369 /* destination valid, are we tired of waiting for a boat? */
2373 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) unit tired of waiting!",
2377 } else {
2378 dest = city_dest;
2379 help_wonder = (unit_data->task == AIUNIT_WONDER) ? TRUE : FALSE;
2380 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2382 }
2383 }
2384 }
2385
2386 if (unit_data->task == AIUNIT_NONE) {
2387 if (homecity == NULL) {
2388 /* FIXME: We shouldn't bother in getting homecity for
2389 * caravan that will then be used for wonder building. */
2391 return;
2392 }
2393 homecity = game_city_by_number(punit->homecity);
2394 if (homecity == NULL) {
2395 return;
2396 }
2397 }
2398
2400 /* Make more trade with allies than other peaceful nations
2401 * by considering only allies 50% of the time.
2402 * (the other 50% allies are still considered, but so are other nations) */
2403 if (fc_rand(2)) {
2404 /* Be optimistic about development of relations with no-contact and
2405 * cease-fire nations. */
2406 parameter.allow_foreign_trade = FTL_NONWAR;
2407 } else {
2408 parameter.allow_foreign_trade = FTL_ALLIED;
2409 }
2410
2413 parameter.callback_data = punit;
2414 }
2416 parameter.ignore_transit_time = TRUE;
2417 }
2420 parameter.ignore_transit_time = FALSE;
2421 }
2422 caravan_find_best_destination(nmap, punit, &parameter, &result,
2423 !has_handicap(pplayer, H_MAP));
2424 if (result.dest != NULL) {
2425 /* We did find a new destination for the unit */
2426 dest = result.dest;
2427 help_wonder = result.help_wonder;
2428 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2429 request_boat = required_boat;
2431 (help_wonder) ? AIUNIT_WONDER : AIUNIT_TRADE,
2432 dest->tile);
2433 } else {
2434 dest = NULL;
2435 }
2436 }
2437
2438 if (required_boat && !expect_boats) {
2439 /* Would require boat, but can't have them. Render destination invalid. */
2440 dest = NULL;
2441 }
2442
2443 if (dest != NULL) {
2444 dai_caravan_goto(ait, pplayer, punit, dest, help_wonder,
2445 required_boat, request_boat);
2446 return; /* that may have clobbered the unit */
2447 } else {
2448 /* We have nowhere to go! */
2449 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d), nothing to do!",
2453 /* Should we become a defensive unit? */
2454 }
2455}
2456
2457/**********************************************************************/
2462 struct unit *punit)
2463{
2464 struct player *pplayer = unit_owner(punit);
2465 struct city *pcity = tile_city(unit_tile(punit));
2466 struct city *safe = NULL;
2467 const struct unit_type *punittype = unit_type_get(punit);
2468 bool no_recovery;
2469
2471
2472 if (pcity != NULL) {
2473 /* Rest in the city until the hitpoints are recovered, but attempt
2474 * to protect city from attack (and be opportunistic too)*/
2477 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "recovering hit points.");
2478 } else {
2479 return; /* We died heroically defending our city */
2480 }
2481 } else {
2482 /* Goto to nearest city to recover hit points */
2483 /* Just before, check to see if we can occupy an undefended enemy city */
2486 return; /* Oops, we died */
2487 }
2488
2489 /* Find a city to stay and go there */
2491 if (safe) {
2492 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "going to %s to recover",
2494 if (!dai_unit_goto(ait, punit, safe->tile)) {
2495 log_base(LOGLEVEL_RECOVERY, "died trying to hide and recover");
2496 return;
2497 }
2498 } else {
2499 /* Oops */
2500 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "didn't find a city to recover in!");
2502 dai_military_attack(ait, pplayer, punit);
2503 return;
2504 }
2505 }
2506
2507 /* Is the unit still damaged? If true, and could recover hit points, do so.
2508 * Don't wait if would be losing hitpoints that way! */
2510 if (punit->hp == punittype->hp) {
2511 no_recovery = TRUE;
2512 } else {
2514
2515 if (gain < 0) {
2516 no_recovery = TRUE;
2517 } else if (gain == 0 && !punit->moved) {
2518 /* Isn't gaining even though has not moved. */
2519 no_recovery = TRUE;
2520 }
2521 }
2522 if (no_recovery) {
2523 /* We are ready to go out and kick ass again */
2524 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "ready to kick ass again!");
2526 return;
2527 } else {
2528 def_ai_unit_data(punit, ait)->done = TRUE; /* Sit tight */
2529 }
2530}
2531
2532/**********************************************************************/
2536void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap,
2537 struct player *pplayer, struct unit *punit)
2538{
2539 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2540 int id = punit->id;
2541
2543
2544 /* "Escorting" aircraft should not do anything. They are activated
2545 * by their transport or charge. We do _NOT_ set them to 'done'
2546 * since they may need be activated once our charge moves. */
2547 if (unit_data->task == AIUNIT_ESCORT
2549 return;
2550 }
2551
2554 && has_handicap(pplayer, H_AWAY)) {
2555 /* Don't move sentried or fortified units controlled by a player
2556 * in away mode. */
2557 unit_data->done = TRUE;
2558 return;
2559 }
2560
2561 /* Since military units re-evaluate their actions every turn,
2562 we must make sure that previously reserved ferry is freed. */
2564
2566 /* Try hunting with this unit */
2567 if (dai_hunter_qualify(pplayer, punit)) {
2568 int result, sanity = punit->id;
2569
2570 UNIT_LOG(LOGLEVEL_HUNT, punit, "is qualified as hunter");
2571 result = dai_hunter_manage(ait, pplayer, punit);
2574 return; /* Died */
2575 }
2576 if (result == -1) {
2577 (void) dai_hunter_manage(ait, pplayer, punit); /* More carnage */
2579 return;
2580 } else if (result >= 1) {
2582 return; /* Done moving */
2583 } else if (unit_data->task == AIUNIT_HUNTER) {
2584 /* This should be very rare */
2586 }
2587 } else if (unit_data->task == AIUNIT_HUNTER) {
2589 }
2591
2592 /* Do we have a specific job for this unit? If not, we default
2593 * to attack. */
2594 dai_military_findjob(ait, nmap, pplayer, punit);
2595
2596 switch (unit_data->task) {
2597 case AIUNIT_AUTO_WORKER:
2598 case AIUNIT_BUILD_CITY:
2599 fc_assert(FALSE); /* This is not the place for this role */
2600 break;
2601 case AIUNIT_DEFEND_HOME:
2603 dai_military_defend(ait, pplayer, punit);
2605 break;
2606 case AIUNIT_ATTACK:
2607 case AIUNIT_NONE:
2609 dai_military_attack(ait, pplayer, punit);
2611 break;
2612 case AIUNIT_ESCORT:
2614 dai_military_bodyguard(ait, pplayer, punit);
2616 break;
2617 case AIUNIT_EXPLORE:
2618 switch (manage_auto_explorer(punit)) {
2619 case MR_DEATH:
2620 /* Don't use punit! */
2621 return;
2622 case MR_OK:
2623 UNIT_LOG(LOG_DEBUG, punit, "more exploring");
2624 break;
2625 default:
2626 UNIT_LOG(LOG_DEBUG, punit, "no more exploring either");
2627 break;
2628 };
2629 def_ai_unit_data(punit, ait)->done = (punit->moves_left <= 0);
2630 break;
2631 case AIUNIT_RECOVER:
2635 break;
2636 case AIUNIT_HUNTER:
2637 fc_assert(FALSE); /* Dealt with above */
2638 break;
2639 default:
2641 }
2642
2643 /* If we are still alive, either sentry or fortify. */
2644 if ((punit = game_unit_by_number(id))) {
2646 struct city *pcity = tile_city(unit_tile(punit));
2647
2648 if (unit_list_find(unit_tile(punit)->units,
2649 unit_data->ferryboat)) {
2651 } else if (pcity || punit->activity == ACTIVITY_IDLE) {
2652 /* We do not need to fortify in cities - we fortify and sentry
2653 * according to home defense setup, for easy debugging. */
2654 if (!pcity || unit_data->task == AIUNIT_DEFEND_HOME) {
2659 }
2660 } else {
2662 }
2663 }
2664 }
2665}
2666
2667/**********************************************************************/
2670static void dai_manage_settler(struct ai_type *ait, struct player *pplayer,
2671 struct unit *punit)
2672{
2673 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2674
2676 unit_data->done = TRUE; /* We will manage this unit later... ugh */
2677 /* If BUILD_CITY must remain BUILD_CITY, otherwise turn into autoworker */
2678 if (unit_data->task == AIUNIT_NONE) {
2680 }
2681}
2682
2683/**********************************************************************/
2691void dai_manage_unit(struct ai_type *ait, struct player *pplayer,
2692 struct unit *punit)
2693{
2694 struct unit_ai *unit_data;
2695 struct unit *bodyguard = aiguard_guard_of(ait, punit);
2696 bool is_ferry = FALSE;
2697 const struct unit_type *ptype;
2698 const struct civ_map *nmap = &(wld.map);
2699
2701
2702 /* Don't manage the unit if it is under human orders. */
2703 if (unit_has_orders(punit)) {
2705
2706 UNIT_LOG(LOG_VERBOSE, punit, "is under human orders, aborting AI control.");
2708 unit_data->done = TRUE;
2709 return;
2710 }
2711
2712 /* Check if we have lost our bodyguard. If we never had one, all
2713 * fine. If we had one and lost it, ask for a new one. */
2714 if (!bodyguard && aiguard_has_guard(ait, punit)) {
2715 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "lost bodyguard, asking for new");
2717 }
2718
2720
2721 if (punit->moves_left <= 0) {
2722 /* Can do nothing */
2723 unit_data->done = TRUE;
2724 return;
2725 }
2726
2727 is_ferry = dai_is_ferry(punit, ait);
2728
2730
2733 dai_manage_diplomat(ait, pplayer, punit);
2735 return;
2736 } else if (ptype->adv.worker
2738 dai_manage_settler(ait, pplayer, punit);
2739 return;
2744 dai_manage_caravan(ait, pplayer, punit);
2746 return;
2748 dai_manage_barbarian_leader(ait, pplayer, punit);
2749 return;
2752 dai_manage_paratrooper(ait, pplayer, punit);
2753 return;
2754 } else if (is_ferry && unit_data->task != AIUNIT_HUNTER) {
2756 dai_manage_ferryboat(ait, pplayer, punit);
2758 return;
2759 } else if (utype_fuel(ptype)
2760 && unit_data->task != AIUNIT_ESCORT) {
2762 dai_manage_airunit(ait, pplayer, punit);
2764 return;
2765 } else if (is_losing_hp(punit)) {
2766 /* This unit is losing hitpoints over time */
2767
2768 /* TODO: We can try using air-unit code for helicopters, just
2769 * pretend they have fuel = HP / 3 or something. */
2770 unit_data->done = TRUE; /* we did our best, which was ...
2771 nothing */
2772 return;
2773 } else if (!is_special_unit(punit)) {
2775 UNIT_LOG(LOG_DEBUG, punit, "recruit unit for the military");
2776 dai_manage_military(ait, nmap, pplayer, punit);
2778 return;
2779 } else {
2780 /* what else could this be? -- Syela */
2781 switch (manage_auto_explorer(punit)) {
2782 case MR_DEATH:
2783 /* don't use punit! */
2784 break;
2785 case MR_OK:
2786 UNIT_LOG(LOG_DEBUG, punit, "now exploring");
2787 break;
2788 default:
2789 UNIT_LOG(LOG_DEBUG, punit, "fell through all unit tasks, defending");
2791 dai_military_defend(ait, pplayer, punit);
2792 break;
2793 };
2794 return;
2795 }
2796}
2797
2798/**********************************************************************/
2804static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
2805{
2806 city_list_iterate(pplayer->cities, pcity) {
2807 /* The idea here is that we should never keep more than two
2808 * units in permanent defense. */
2809 int total_defense = 0;
2811 bool emergency = FALSE;
2812 int count = 0;
2817 int entertainers = 0;
2818 bool enough = FALSE;
2819
2822 entertainers += pcity->specialists[sp];
2823 }
2825
2826 martless_unhappy += entertainers; /* We want to use martial law instead
2827 * of entertainers. */
2828
2829 while (!enough
2832 && martless_unhappy > mart_each * count))) {
2833 int best_want = 0;
2834 struct unit *best = NULL;
2835 bool defense_needed = total_defense <= total_attack; /* Defense or martial */
2836
2838 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2839
2840 if ((unit_data->task == AIUNIT_NONE || emergency)
2841 && unit_data->task != AIUNIT_DEFEND_HOME
2842 && unit_owner(punit) == pplayer) {
2843 int want = assess_defense_unit(ait, pcity, punit, FALSE);
2844
2845 if (want > best_want) {
2846 best_want = want;
2847 best = punit;
2848 }
2849 }
2851
2852 if (best == NULL) {
2853 if (defense_needed) {
2854 /* Ooops - try to grab any unit as defender! */
2855 if (emergency) {
2856 CITY_LOG(LOG_DEBUG, pcity, "Not defended properly");
2857 break;
2858 }
2859 emergency = TRUE;
2860 } else {
2861 break;
2862 }
2863 } else {
2864 const struct unit_type *btype = unit_type_get(best);
2865
2866 if ((martless_unhappy < mart_each * count
2867 || count >= mart_max || mart_each <= 0)
2868 && ((count >= 2
2869 && btype->attack_strength > btype->defense_strength)
2870 || (count >= 4
2871 && btype->attack_strength == btype->defense_strength))) {
2872 /* In this case attack would be better defense than fortifying
2873 * to city. */
2874 enough = TRUE;
2875 } else {
2876 int loglevel = pcity->server.debug ? LOG_AI_TEST : LOG_DEBUG;
2877
2879 UNIT_LOG(loglevel, best, "Defending city");
2881 count++;
2882 }
2883 }
2884 }
2885 CITY_LOG(LOG_DEBUG, pcity, "Evaluating defense: %d defense, %d incoming"
2886 ", %d defenders (out of %d)", total_defense, total_attack, count,
2889}
2890
2891/**********************************************************************/
2899void dai_manage_units(struct ai_type *ait, struct player *pplayer)
2900{
2902 dai_airlift(ait, pplayer);
2904
2905 /* Clear previous orders, if desirable, here. */
2906 unit_list_iterate(pplayer->units, punit) {
2907 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2908
2909 unit_data->done = FALSE;
2910 if (unit_data->task == AIUNIT_DEFEND_HOME) {
2912 }
2914
2915 /* Find and set city defenders first - figure out which units are
2916 * allowed to leave home. */
2917 dai_set_defenders(ait, pplayer);
2918
2921 && !def_ai_unit_data(punit, ait)->done) {
2922 /* Though it is usually the passenger who drives the transport,
2923 * the transporter is responsible for managing its passengers. */
2924 dai_manage_unit(ait, pplayer, punit);
2925 }
2927}
2928
2929/**********************************************************************/
2935 const struct city *pcity)
2936{
2937 const struct impr_type *impr_req = NULL;
2938 const struct req_context context = {
2940 .city = pcity,
2941 .tile = city_tile(pcity),
2942 .unittype = putype,
2943 };
2944
2945 requirement_vector_iterate(&putype->build_reqs, preq) {
2947 /* Already there. */
2948 continue;
2949 }
2951 city_owner(pcity), pcity)) {
2952 /* The unit type can't be built at all. */
2953 return NULL;
2954 }
2955 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2956 /* This is (one of) the building(s) required. */
2957 impr_req = preq->source.value.building;
2958 }
2960
2961 return impr_req;
2962}
2963
2964/**********************************************************************/
2969 const struct unit_type *base)
2970{
2971 /* This is the real function: */
2972 do {
2973 base = base->obsoleted_by;
2974 if (base == test) {
2975 return TRUE;
2976 }
2977 } while (base);
2978 return FALSE;
2979}
2980
2981/**********************************************************************/
2986static void dai_manage_barbarian_leader(struct ai_type *ait,
2987 struct player *pplayer,
2988 struct unit *leader)
2989{
2991 struct pf_parameter parameter;
2992 struct pf_map *pfm;
2993 struct pf_reverse_map *pfrm;
2994 struct unit *worst_danger;
2995 int move_cost, best_move_cost;
2996 int body_guards;
2997 bool alive = TRUE;
2998 const struct civ_map *nmap = &(wld.map);
2999
3001
3002 if (leader->moves_left == 0
3004 && 1 < unit_list_size(leader_tile->units))) {
3006
3007 return;
3008 }
3009
3010 if (is_boss_of_boat(ait, leader)) {
3011 /* We are in charge. Of course, since we are the leader...
3012 * But maybe somebody more militaristic should lead our ship to battle! */
3013
3014 /* First release boat from leaders lead */
3016
3020 && warrior->moves_left > 0) {
3021 /* This seems like a good warrior to lead us in to conquest! */
3022 dai_manage_unit(ait, pplayer, warrior);
3023
3024 /* If we reached our destination, ferryboat already called
3025 * ai_manage_unit() for leader. So no need to continue here.
3026 * Leader might even be dead.
3027 * If this return is removed, surrounding unit_list_iterate()
3028 * has to be replaced with unit_list_iterate_safe()*/
3029 return;
3030 }
3032 }
3033
3034 /* If we are not in charge of the boat, continue as if we
3035 * were not in a boat - we may want to leave the ship now. */
3036
3037 /* Check the total number of units able to protect our leader. */
3038 body_guards = 0;
3039 unit_list_iterate(pplayer->units, punit) {
3042 body_guards++;
3043 }
3045
3046 if (0 < body_guards) {
3047 pft_fill_unit_parameter(&parameter, nmap, leader);
3048 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3049 pfm = pf_map_new(&parameter);
3050
3051 /* Find the closest body guard.
3052 * FIXME: maybe choose the strongest too? */
3053 pf_map_tiles_iterate(pfm, ptile, FALSE) {
3054 unit_list_iterate(ptile->units, punit) {
3055 if (unit_owner(punit) == pplayer
3058 struct pf_path *path = pf_map_path(pfm, ptile);
3059
3060 adv_follow_path(leader, path, ptile);
3061 pf_path_destroy(path);
3063 return;
3064 }
3067
3069 }
3070
3071 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader needs to flee");
3072
3073 /* Check for units we could fear. */
3074 pfrm = pf_reverse_map_new(nmap, pplayer, leader_tile, 3,
3075 !has_handicap(pplayer, H_MAP));
3078
3079 players_iterate(other_player) {
3080 if (other_player == pplayer) {
3081 continue;
3082 }
3083
3084 unit_list_iterate(other_player->units, punit) {
3086 if (PF_IMPOSSIBLE_MC != move_cost && move_cost < best_move_cost) {
3087 best_move_cost = move_cost;
3089 }
3092
3094
3095 if (NULL == worst_danger) {
3097 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: no close enemy.");
3098 return;
3099 }
3100
3102 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3103 pfm = pf_map_new(&parameter);
3105
3106 /* Try to escape. */
3107 do {
3109
3110 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: moves left: %d.",
3111 leader->moves_left);
3112
3115 continue;
3116 }
3117
3118 move_cost = pf_map_move_cost(pfm, near_tile);
3119 if (PF_IMPOSSIBLE_MC != move_cost
3120 && move_cost > best_move_cost) {
3122 "Barbarian leader: safest is (%d, %d), safeness %d",
3124 best_move_cost = move_cost;
3126 }
3128
3129 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: fleeing to (%d, %d).",
3133 "Barbarian leader: reached the safest position.");
3136 return;
3137 }
3138
3140 if (alive) {
3142 /* Didn't move. No point to retry. */
3144 return;
3145 }
3147 }
3148 } while (alive && 0 < leader->moves_left);
3149
3151}
3152
3153/**********************************************************************/
3159void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile,
3160 struct unit *punit,
3161 enum override_bool *result)
3162{
3163 int a = 0, d, db;
3164 struct player *pplayer = unit_owner(punit);
3165 struct city *pcity = tile_city(ptile);
3166 int extras_bonus = 0;
3167
3168 if (is_human(pplayer)) {
3169 /* Use advisors code for humans. */
3170 return;
3171 }
3172
3173 if (pcity != NULL && pplayers_allied(city_owner(pcity), pplayer)
3174 && !is_non_allied_unit_tile(ptile, pplayer,
3176 /* We will be safe in a friendly city */
3177 *result = OVERRIDE_FALSE;
3178 return;
3179 }
3180
3181 /* Calculate how well we can defend at (x,y) */
3182 db = 10 + tile_terrain(ptile)->defense_bonus / 10;
3184
3185 db += (db * extras_bonus) / 100;
3187
3188 adjc_iterate(&(wld.map), ptile, ptile1) {
3189 if (has_handicap(pplayer, H_FOG)
3190 && !map_is_known_and_seen(ptile1, pplayer, V_MAIN)) {
3191 /* We cannot see danger at (ptile1) => assume there is none */
3192 continue;
3193 }
3194 unit_list_iterate(ptile1->units, enemy) {
3195 if (pplayers_at_war(unit_owner(enemy), pplayer)
3197 == ATT_OK)
3199 == ATT_OK)) {
3201 if ((a * a * 10) >= d) {
3202 /* The enemies combined strength is too big! */
3203 *result = OVERRIDE_TRUE;
3204 return;
3205 }
3206 }
3209
3210 *result = OVERRIDE_FALSE;
3211}
3212
3213/**********************************************************************/
3216static void update_simple_ai_types(void)
3217{
3218 int i = 0;
3219
3222
3225 && !(pclass->adv.land_move == MOVE_NONE
3228 && punittype->transport_capacity < 8) {
3230 i++;
3231 }
3233
3235}
3236
3237/**********************************************************************/
3241{
3242 /* TODO: remove the simple_ai_types cache or merge it with a general ai
3243 * cache; see the comment to struct unit_type *simple_ai_types at
3244 * the beginning of this file. */
3246
3248 struct unit_type_ai *utai = fc_malloc(sizeof(*utai));
3249
3250 utai->low_firepower = FALSE;
3251 utai->ferry = FALSE;
3252 utai->missile_platform = FALSE;
3253 utai->carries_occupiers = FALSE;
3254 utai->potential_charges = unit_type_list_new();
3255
3258
3261
3262 /* Confirm firepower */
3264 if (pbonus->type == CBONUS_LOW_FIREPOWER) {
3266 if (utype_has_flag(penemy, pbonus->flag)) {
3267 struct unit_type_ai *utai = utype_ai_data(penemy, ait);
3268
3269 utai->low_firepower = TRUE;
3270 }
3272 }
3274
3275 /* Consider potential cargo */
3276 if (punittype->transport_capacity > 0) {
3277 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3278
3281
3284 utai->missile_platform = TRUE;
3285 } else if (pclass->adv.sea_move != MOVE_NONE
3286 && pcargo->adv.land_move != MOVE_NONE) {
3287 if (pcargo->adv.sea_move != MOVE_FULL) {
3288 utai->ferry = TRUE;
3289 } else {
3290 if (0 != utype_fuel(pctype)) {
3291 utai->ferry = TRUE;
3292 }
3293 }
3294 }
3295
3297 utai->carries_occupiers = TRUE;
3298 }
3299 }
3301 }
3302
3303 /* Consider potential charges */
3306
3307 if (0 < utype_fuel(punittype)
3308 && (0 == utype_fuel(pcharge)
3310 continue;
3311 }
3312
3313 unit_class_list_iterate(pclass->cache.subset_movers, chgcls) {
3314 if (chgcls == utype_class(pcharge)) {
3316 }
3318
3320 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3321 unit_type_list_append(utai->potential_charges, pcharge);
3322 }
3323
3326}
3327
3328/**********************************************************************/
3332{
3334 struct unit_type_ai *utai = utype_ai_data(ptype, ait);
3335
3336 if (utai == NULL) {
3337 continue;
3338 }
3340
3341 unit_type_list_destroy(utai->potential_charges);
3342 free(utai);
3344}
3345
3346/**********************************************************************/
3349void dai_unit_init(struct ai_type *ait, struct unit *punit)
3350{
3351 /* Make sure that contents of unit_ai structure are correctly initialized,
3352 * if you ever allocate it by some other mean than fc_calloc() */
3353 struct unit_ai *unit_data = fc_calloc(1, sizeof(struct unit_ai));
3354
3355 unit_data->done = FALSE;
3356 unit_data->cur_pos = NULL;
3357 unit_data->prev_pos = NULL;
3358 unit_data->target = 0;
3359 BV_CLR_ALL(unit_data->hunted);
3360 unit_data->ferryboat = 0;
3361 unit_data->passenger = 0;
3362 unit_data->bodyguard = 0;
3363 unit_data->charge = 0;
3364
3366}
3367
3368/**********************************************************************/
3371void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
3372{
3373 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3374
3376
3377 BV_CLR_ALL(unit_data->hunted);
3378}
3379
3380/**********************************************************************/
3383void dai_unit_close(struct ai_type *ait, struct unit *punit)
3384{
3385 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3386
3388
3391
3392 if (unit_data != NULL) {
3395 }
3396}
3397
3398/**********************************************************************/
3401void dai_unit_save(struct ai_type *ait, const char *aitstr,
3402 struct section_file *file,
3403 const struct unit *punit, const char *unitstr)
3404{
3405 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3406
3407 secfile_insert_int(file, unit_data->passenger, "%s.%spassenger",
3408 unitstr, aitstr);
3409 secfile_insert_int(file, unit_data->ferryboat, "%s.%sferryboat",
3410 unitstr, aitstr);
3411 secfile_insert_int(file, unit_data->charge, "%s.%scharge",
3412 unitstr, aitstr);
3413 secfile_insert_int(file, unit_data->bodyguard, "%s.%sbodyguard",
3414 unitstr, aitstr);
3415}
3416
3417/**********************************************************************/
3420void dai_unit_load(struct ai_type *ait, const char *aitstr,
3421 const struct section_file *file,
3422 struct unit *punit, const char *unitstr)
3423{
3424 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3425
3426 unit_data->passenger
3427 = secfile_lookup_int_default(file, 0, "%s.%spassenger",
3428 unitstr, aitstr);
3429 unit_data->ferryboat
3430 = secfile_lookup_int_default(file, 0, "%s.%sferryboat",
3431 unitstr, aitstr);
3432 unit_data->charge
3433 = secfile_lookup_int_default(file, 0, "%s.%scharge",
3434 unitstr, aitstr);
3435 unit_data->bodyguard
3436 = secfile_lookup_int_default(file, 0, "%s.%sbodyguard",
3437 unitstr, aitstr);
3438}
3439
3445
3446/**********************************************************************/
3449static bool role_unit_cb(struct unit_type *ptype, void *data)
3450{
3451 struct role_unit_cb_data *cb_data = (struct role_unit_cb_data *)data;
3453 const struct civ_map *nmap = &(wld.map);
3454
3455 if ((cb_data->tc == TC_LAND && pclass->adv.land_move == MOVE_NONE)
3456 || (cb_data->tc == TC_OCEAN && pclass->adv.sea_move == MOVE_NONE)) {
3457 return FALSE;
3458 }
3459
3460 if (cb_data->build_city == NULL
3461 || can_city_build_unit_now(nmap, cb_data->build_city, ptype)) {
3462 return TRUE;
3463 }
3464
3465 return FALSE;
3466}
3467
3468/**********************************************************************/
3472 enum terrain_class tc)
3473{
3474 struct role_unit_cb_data cb_data = { .build_city = pcity, .tc = tc };
3475
3477}
3478
3479/**********************************************************************/
3482bool dai_unit_can_strike_my_unit(const struct unit *attacker,
3483 const struct unit *defender)
3484{
3485 struct pf_parameter parameter;
3486 struct pf_map *pfm;
3487 const struct tile *ptarget = unit_tile(defender);
3488 int max_move_cost = attacker->moves_left;
3489 bool able_to_strike = FALSE;
3490 const struct civ_map *nmap = &(wld.map);
3491
3492 pft_fill_unit_parameter(&parameter, nmap, attacker);
3493 parameter.omniscience = !has_handicap(unit_owner(defender), H_MAP);
3494 pfm = pf_map_new(&parameter);
3495
3496 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
3497 if (move_cost > max_move_cost) {
3498 break;
3499 }
3500
3501 if (ptile == ptarget) {
3503 break;
3504 }
3506
3508
3509 return able_to_strike;
3510}
3511
3512/**********************************************************************/
3515void dai_switch_to_explore(struct ai_type *ait, struct unit *punit,
3516 struct tile *target, enum override_bool *allow)
3517{
3518 struct unit_ai *udata = def_ai_unit_data(punit, ait);
3519
3520 if (udata->task != AIUNIT_NONE && udata->task != AIUNIT_EXPLORE) {
3522
3523 return;
3524 }
3525}
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
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:682
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:490
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:3401
static bool invasion_funct(struct ai_type *ait, struct unit *punit, bool dest, int radius, int which)
Definition daiunit.c:1047
static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:879
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:2254
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:2019
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:727
void dai_units_ruleset_init(struct ai_type *ait)
Definition daiunit.c:3240
#define LOG_CARAVAN
Definition daiunit.c:88
static int unit_att_rating_now(const struct unit *punit)
Definition daiunit.c:259
enum gen_action dai_select_tile_attack_action(struct civ_map *nmap, struct unit *punit, struct tile *ptile)
Definition daiunit.c:445
static void dai_military_attack_barbarian(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1718
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:1093
static void update_simple_ai_types(void)
Definition daiunit.c:3216
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Definition daiunit.c:2934
void dai_switch_to_explore(struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
Definition daiunit.c:3515
void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2536
static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:1935
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:3482
bool is_on_unit_upgrade_path(const struct unit_type *test, const struct unit_type *base)
Definition daiunit.c:2968
static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:661
static void single_invader(struct ai_city *city_data, const struct unit_type *utype, int which)
Definition daiunit.c:1019
#define LOG_CARAVAN3
Definition daiunit.c:90
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2899
static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2291
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:1171
static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2804
void dai_manage_unit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2691
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:742
static bool role_unit_cb(struct unit_type *ptype, void *data)
Definition daiunit.c:3449
static void dai_military_attack(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1781
void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3371
static void dai_manage_settler(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2670
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:2150
static int dai_rampage_want(struct unit *punit, struct tile *ptile)
Definition daiunit.c:482
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:861
struct city * find_nearest_safe_city(struct unit *punit)
Definition daiunit.c:1667
static void dai_manage_barbarian_leader(struct ai_type *ait, struct player *pplayer, struct unit *leader)
Definition daiunit.c:2986
static void dai_manage_hitpoint_recovery(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2461
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:549
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:3349
void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
Definition daiunit.c:3159
bool uclass_need_trans_between(struct unit_class *pclass, struct tile *ctile, struct tile *ptile)
Definition daiunit.c:1980
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:3420
static void caravan_optimize_callback(const struct caravan_result *result, void *data)
Definition daiunit.c:2131
#define LOGLEVEL_RECOVERY
Definition daiunit.c:87
void dai_unit_close(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3383
void dai_units_ruleset_close(struct ai_type *ait)
Definition daiunit.c:3331
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Definition daiunit.c:3471
static void dai_military_defend(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:973
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:628
static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer, struct unit *punit)
Definition daiunit.c:2209
#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:3054
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2976
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct 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:78
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:74
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:899
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:925
#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:843
void pft_fill_unit_attack_param(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:952
void pft_fill_unit_overlap_param(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:894
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:879
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
#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:3442
struct city * build_city
Definition daiunit.c:3443
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:140
enum unit_activity activity
Definition unit.h:159
int moves_left
Definition unit.h:152
int id
Definition unit.h:147
bool moved
Definition unit.h:176
bool debug
Definition unit.h:237
int hp
Definition unit.h:153
struct unit::@84::@87 server
struct tile * tile
Definition unit.h:142
struct unit_adv * adv
Definition unit.h:239
int homecity
Definition unit.h:148
int birth_turn
Definition unit.h:210
struct tile * goto_tile
Definition unit.h:157
int veteran
Definition unit.h:154
struct player * owner
Definition unit.h:145
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)
struct goods_type * unit_current_goods(const struct unit *punit, const struct city *homecity)
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 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:407
#define unit_cargo_iterate_end
Definition unit.h:588
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:585
#define CHECK_UNIT(punit)
Definition unit.h:273
#define unit_owner(_pu)
Definition unit.h:406
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.h:443
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unithand.c:6665
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:6563
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