Freeciv-3.4
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 "daiair.h"
70#include "daicity.h"
71#include "daidata.h"
72#include "daidiplomat.h"
73#include "daieffects.h"
74#include "daiferry.h"
75#include "daiguard.h"
76#include "daihunter.h"
77#include "dailog.h"
78#include "daimilitary.h"
79#include "daiparadrop.h"
80#include "daiplayer.h"
81#include "daitools.h"
82
83#include "daiunit.h"
84
85
86#define LOGLEVEL_RECOVERY LOG_DEBUG
87#define LOG_CARAVAN LOG_DEBUG
88#define LOG_CARAVAN2 LOG_DEBUG
89#define LOG_CARAVAN3 LOG_DEBUG
90
91static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit);
92static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer,
93 struct unit *punit);
94static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
95 struct unit *punit);
96static void dai_manage_barbarian_leader(struct ai_type *ait,
97 struct player *pplayer,
98 struct unit *leader);
99
100static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap,
101 struct player *pplayer, struct unit *punit);
102static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
103 struct unit *punit);
104static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
105 struct unit *punit);
106
107static bool unit_role_defender(const struct unit_type *punittype);
108static int unit_def_rating_squared(const struct unit *punit,
109 const struct unit *pdef);
110
111/*
112 * Cached values. Updated by update_simple_ai_types.
113 *
114 * This a hack to enable emulation of old loops previously hardwired
115 * as
116 * for (i = U_WARRIORS; i <= U_BATTLESHIP; i++)
117 *
118 * (Could probably just adjust the loops themselves fairly simply,
119 * but this is safer for regression testing.)
120 *
121 * Not dealing with planes yet.
122 *
123 * Terminated by nullptr.
124 */
126
127/**********************************************************************/
137static struct city *find_neediest_airlift_city(struct ai_type *ait,
138 const struct player *pplayer)
139{
140 struct city *neediest_city = nullptr;
141 int most_danger = 0;
142 int most_urgent = 0;
143
144 city_list_iterate(pplayer->cities, pcity) {
145 struct ai_city *city_data = def_ai_city_data(pcity, ait);
146
147 if (pcity->airlift) {
148 if (city_data->urgency > most_urgent) {
149 most_urgent = city_data->urgency;
151 } else if (0 == most_urgent /* Urgency trumps danger */
152 && city_data->danger > most_danger) {
153 most_danger = city_data->danger;
155 }
156 }
158
159 return neediest_city;
160}
161
162/**********************************************************************/
168static void dai_airlift(struct ai_type *ait, struct player *pplayer)
169{
170 struct city *most_needed;
171 int comparison;
172 struct unit *transported;
173 const struct civ_map *nmap = &(wld.map);
174
175 do {
177 comparison = 0;
178 transported = nullptr;
179
180 if (!most_needed) {
181 return;
182 }
183
184 unit_list_iterate(pplayer->units, punit) {
185 struct tile *ptile = (unit_tile(punit));
186 struct city *pcity = tile_city(ptile);
187
188 if (pcity) {
189 struct ai_city *city_data = def_ai_city_data(pcity, ait);
190 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
191 const struct unit_type *ptype = unit_type_get(punit);
192
193 if (city_data->urgency == 0
196 && DEFENSE_POWER(ptype) > 2
197 && (unit_data->task == AIUNIT_NONE
198 || unit_data->task == AIUNIT_DEFEND_HOME)
199 && IS_ATTACKER(ptype)) {
200 comparison = city_data->danger;
201 transported = punit;
202 }
203 }
205 if (!transported) {
206 return;
207 }
208 UNIT_LOG(LOG_DEBUG, transported, "airlifted to defend %s",
210 unit_do_action(pplayer, transported->id, most_needed->id,
211 0, "", ACTION_AIRLIFT);
212 } while (TRUE);
213}
214
215/**********************************************************************/
221static bool has_defense(struct city *pcity)
222{
223 struct tile *ptile = city_tile(pcity);
224
225 unit_list_iterate(ptile->units, punit) {
227 && punit->hp != 0) {
229
230 if (pclass->non_native_def_pct > 0
231 || is_native_tile_to_class(pclass, ptile)) {
232 return TRUE;
233 }
234 }
235 }
237
238 return FALSE;
239}
240
241/**********************************************************************/
250{
251 return 2 * utype_build_shield_cost_base(punittype) * punittype->attack_strength /
252 (punittype->attack_strength + punittype->defense_strength);
253}
254
255/**********************************************************************/
258static int unit_att_rating_now(const struct unit *punit)
259{
262}
263
264/**********************************************************************/
267static int unit_att_rating_squared(const struct unit *punit)
268{
269 int v = adv_unit_att_rating(punit);
270
271 return v * v;
272}
273
274/**********************************************************************/
277static int unit_def_rating(const struct unit *attacker,
278 const struct unit *defender)
279{
280 const struct unit_type *def_type = unit_type_get(defender);
281
282 return (get_total_defense_power(attacker, defender)
283 * (attacker->id != 0 ? defender->hp : def_type->hp)
284 * def_type->firepower / POWER_DIVIDER);
285}
286
287/**********************************************************************/
290static int unit_def_rating_squared(const struct unit *attacker,
291 const struct unit *defender)
292{
293 int v = unit_def_rating(attacker, defender);
294
295 return v * v;
296}
297
298/**********************************************************************/
304 const struct unit_type *def_type,
305 struct player *def_player,
306 struct tile *ptile, bool fortified,
307 int veteran)
308{
309 struct civ_map *nmap = &(wld.map);
311 ptile, fortified, veteran)
312 * def_type->hp * def_type->firepower / POWER_DIVIDER;
313
314 return v * v;
315}
316
317/**********************************************************************/
342 int victim_count)
343{
345
346 /* attractiveness danger */
349
350 return desire;
351}
352
353/**********************************************************************/
365static int avg_benefit(int benefit, int loss, double chance)
366{
367 return (int)(((benefit + loss) * chance - loss) * SHIELD_WEIGHTING);
368}
369
370/**********************************************************************/
375 struct tile *ptile0,
376 int *value, int *cost)
377{
378 *cost = 0;
379 *value = 0;
380 square_iterate(&(wld.map), ptile0, 1, ptile) {
381 unit_list_iterate(ptile->units, aunit) {
382 if (aunit != punit
384 int val = adv_unit_att_rating(aunit);
385
386 if (val != 0) {
387 *value += val;
389 }
390 }
393}
394
395/**********************************************************************/
399static bool is_my_turn(struct unit *punit, struct unit *pdef)
400{
401 int val = unit_att_rating_now(punit);
402 int cur, d;
403 const struct unit_type *def_type = unit_type_get(pdef);
404 struct tile *def_tile = unit_tile(pdef);
405 struct civ_map *nmap = &(wld.map);
406
408
409 square_iterate(nmap, def_tile, 1, ptile) {
410 unit_list_iterate(ptile->units, aunit) {
411 if (aunit == punit || unit_owner(aunit) != unit_owner(punit)) {
412 continue;
413 }
415 != ATT_OK)
417 pdef, def_tile)
418 != ATT_OK)) {
419 continue;
420 }
423 FALSE, 0);
424 if (d == 0) {
425 return TRUE; /* Thanks, Markus -- Syela */
426 }
430 FALSE, 0) / d;
431 if (cur > val && ai_fuzzy(unit_owner(punit), TRUE)) {
432 return FALSE;
433 }
436
437 return TRUE;
438}
439
440/**********************************************************************/
445 struct unit *punit,
446 struct tile *ptile)
447{
448 enum gen_action selected;
449
451 punit, ptile))
452 == ACTION_NONE
454 punit, ptile))
455 == ACTION_NONE
457 punit, ptile))
458 == ACTION_NONE) {
459 return ACTION_NONE;
460 }
461
462 return selected;
463}
464
465/**********************************************************************/
481static int dai_rampage_want(struct unit *punit, struct tile *ptile)
482{
483 struct player *pplayer = unit_owner(punit);
484 struct unit *pdef;
485 struct civ_map *nmap = &(wld.map);
486
488
489 if (can_unit_attack_tile(punit, nullptr, ptile)
490 && (pdef = get_defender(nmap, punit, ptile, nullptr))
491 /* Action enablers might prevent attacking */
493 /* See description of kill_desire() about these variables. */
497
498 attack *= attack;
499
500 /* If the victim is in the city/fortress, we correct the benefit
501 * with our health because there could be reprisal attacks.
502 * We shouldn't send already injured units to useless suicide.
503 * Note that we do not specially encourage attacks against
504 * cities: rampage is a hit-n-run operation. */
505 if (!is_stack_vulnerable(ptile)
506 && unit_list_size(ptile->units) > 1) {
508 }
509
510 /* If we have non-zero attack rating... */
511 if (attack > 0 && is_my_turn(punit, pdef)) {
512 double chance = unit_win_chance(nmap, punit, pdef, nullptr);
514
515 /* No need to amortize, our operation takes one turn. */
516 UNIT_LOG(LOG_DEBUG, punit, "Rampage: Desire %d to kill %s(%d,%d)",
517 desire,
520
521 return MAX(0, desire);
522 }
523 } else if (0 == unit_list_size(ptile->units)) {
524 /* No defender. */
525 struct city *pcity = tile_city(ptile);
526
527 /* ...and free foreign city waiting for us. Who would resist! */
528 if (pcity != nullptr
529 && pplayers_at_war(pplayer, city_owner(pcity))
532 }
533
534 /* ...or tiny pleasant hut here! */
535 /* FIXME: unhardcode and variate the desire to enter a hut. */
536 if (unit_can_enter_hut(punit, ptile) && !is_barbarian(pplayer)
537 && is_native_tile(unit_type_get(punit), ptile)) {
538 return -RAMPAGE_HUT_OR_BETTER;
539 }
540 }
541
542 return 0;
543}
544
545/**********************************************************************/
548static struct pf_path *find_rampage_target(struct unit *punit,
549 int thresh_adj, int thresh_move)
550{
551 struct pf_map *tgt_map;
552 struct pf_path *path = nullptr;
553 struct pf_parameter parameter;
554 /* Coordinates of the best target (initialize to silence compiler) */
555 struct tile *ptile = unit_tile(punit);
556 /* Want of the best target */
557 int max_want = 0;
558 struct player *pplayer = unit_owner(punit);
559 const struct civ_map *nmap = &(wld.map);
560
562 parameter.omniscience = !has_handicap(pplayer, H_MAP);
563 /* When trying to find rampage targets we ignore risks such as
564 * enemy units because we are looking for trouble!
565 * Hence no call ai_avoid_risks()
566 */
567
568 tgt_map = pf_map_new(&parameter);
570 int want;
571 bool move_needed;
572 int thresh;
573
574 if (move_cost > punit->moves_left) {
575 /* This is too far */
576 break;
577 }
578
579 if (has_handicap(pplayer, H_TARGETS)
580 && !map_is_known_and_seen(iter_tile, pplayer, V_MAIN)) {
581 /* The target is under fog of war */
582 continue;
583 }
584
586
587 /* Negative want means move needed even though the tiles are adjacent */
589 || want < 0);
590 /* Select the relevant threshold */
592 want = (want < 0 ? -want : want);
593
594 if (want > max_want && want > thresh) {
595 /* The new want exceeds both the previous maximum
596 * and the relevant threshold, so it's worth recording */
597 max_want = want;
598 ptile = iter_tile;
599 }
601
602 if (max_want > 0) {
603 /* We found something */
604 path = pf_map_path(tgt_map, ptile);
605 fc_assert(path != nullptr);
606 }
607
609
610 return path;
611}
612
613/**********************************************************************/
628 int thresh_move)
629{
630 int count = punit->moves_left + 1; /* Break any infinite loops */
631 struct pf_path *path = nullptr;
632
635
637 /* This teaches the AI about the dangers inherent in occupychance. */
639
640 while (count-- > 0 && punit->moves_left > 0
642 if (!adv_unit_execute_path(punit, path)) {
643 /* Died */
644 count = -1;
645 }
646 pf_path_destroy(path);
647 path = nullptr;
648 }
649
650 fc_assert(path == nullptr);
651
653 return (count >= 0);
654}
655
656/**********************************************************************/
660static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer,
661 struct unit *punit)
662{
663 struct unit *aunit = aiguard_charge_unit(ait, punit);
664 struct city *acity = aiguard_charge_city(ait, punit);
665 struct tile *ptile;
666
668 CHECK_GUARD(ait, punit);
669
670 if (aunit && unit_owner(aunit) == unit_owner(punit)) {
671 /* protect a unit */
672 if (aunit->goto_tile != nullptr) {
673 /* Our charge is going somewhere: maybe we should meet them there */
674 /* FIXME: This probably isn't the best algorithm for this. */
676 int me2goal = real_map_distance(unit_tile(punit), aunit->goto_tile);
677 int them2goal = real_map_distance(unit_tile(aunit), aunit->goto_tile);
678
679 if (me2goal < me2them
683 ptile = aunit->goto_tile;
684 } else {
685 ptile = unit_tile(aunit);
686 }
687 } else {
688 ptile = unit_tile(aunit);
689 }
690 } else if (acity && city_owner(acity) == unit_owner(punit)) {
691 /* protect a city */
692 ptile = acity->tile;
693 } else {
694 /* should be impossible */
695 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "we lost our charge");
696 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
697 return;
698 }
699
700 if (same_pos(unit_tile(punit), ptile)) {
701 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "at RV");
702 } else {
703 if (goto_is_sane(punit, ptile)) {
704 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "meeting charge");
705 if (!dai_gothere(ait, pplayer, punit, ptile)) {
706 /* We died */
707 return;
708 }
709 } else {
710 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "can not meet charge");
711 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
712 }
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 = nullptr;
751 int def, best_def = -1;
752 /* Arbitrary: 3 turns. */
753 const int max_move_cost = 3 * unit_move_rate(punit);
754
755 *aunit = nullptr;
756 *acity = nullptr;
757
758 if (toughness == 0) {
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 = nullptr;
807 best_def = def;
808 }
810
811 /* City bodyguard. TODO: allied city bodyguard? */
812 if (ai_fuzzy(pplayer, TRUE)
813 && pcity != nullptr
814 && city_owner(pcity) == pplayer
815 && (data = def_ai_city_data(pcity, ait))
816 && 0 < data->urgency) {
817 if (best_data != nullptr
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 = nullptr;
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)",
846 (*acity != nullptr ? city_name_get(*acity)
847 : (*aunit != nullptr ? unit_rule_name(*aunit) : "")),
849 : (*aunit != nullptr ?
852 : (*aunit != nullptr ?
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 }
897 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
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 = nullptr;
915
916 if (acity != nullptr) {
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, nullptr); /* 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");
938 dai_unit_new_task(ait, punit, AIUNIT_RECOVER, nullptr);
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. */
990 pcity = find_closest_city(unit_tile(punit), nullptr, pplayer,
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 (tile_city(dest_tile) == nullptr
1101 /* Unit can directly go to 'dest_tile'. */
1102 struct tile *best_tile = nullptr;
1104
1105 if (beachhead_tile != nullptr) {
1106 *beachhead_tile = dest_tile;
1107 }
1108
1109 adjc_iterate(&(wld.map), dest_tile, ptile) {
1111 if (cost != PF_IMPOSSIBLE_MC
1112 && (best_tile == nullptr || cost < best_cost)) {
1113 best_tile = ptile;
1114 best_cost = cost;
1115 }
1117
1118 if (ferry_dest != nullptr) {
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 = nullptr, *best_beach = nullptr;
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 && (best_tile == nullptr || cost < best_cost)) {
1142 best_beach = beach;
1143 best_tile = ptile;
1144 best_cost = cost;
1145 }
1146 }
1148 }
1150
1152
1153 if (beachhead_tile != nullptr) {
1155 }
1156 if (ferry_dest != nullptr) {
1158 }
1159
1160 return (PF_IMPOSSIBLE_MC != best_cost);
1161 }
1162}
1163
1164/**********************************************************************/
1172adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
1173 struct unit *punit,
1174 struct tile **pdest_tile, struct pf_path **ppath,
1175 struct pf_map **pferrymap,
1176 struct unit **pferryboat,
1177 const struct unit_type **pboattype, int *pmove_time)
1178{
1179 const int attack_value = adv_unit_att_rating(punit); /* Basic attack. */
1180 struct pf_parameter parameter;
1181 struct pf_map *punit_map, *ferry_map;
1182 struct pf_position pos;
1184 const struct unit_type *punit_type = unit_type_get(punit);
1185 struct tile *punit_tile = unit_tile(punit);
1186 /* Type of our boat (a future one if ferryboat == nullptr). */
1187 const struct unit_type *boattype = nullptr;
1188 struct unit *ferryboat = nullptr;
1189 struct city *pcity;
1190 struct ai_city *acity_data;
1191 int bcost, bcost_bal; /* Build cost of the attacker (+adjustments). */
1192 bool handicap = has_handicap(pplayer, H_TARGETS);
1193 bool unhap = FALSE; /* Do we make unhappy citizen. */
1194 bool harbor = FALSE; /* Do we have access to sea? */
1195 bool go_by_boat; /* Whether we need a boat or not. */
1196 int vulnerability; /* Enemy defense rating. */
1197 adv_want benefit; /* Benefit from killing the target. */
1198 struct unit *pdefender; /* Enemy city defender. */
1199 int move_time; /* Turns needed to target. */
1200 int reserves;
1201 int attack; /* Our total attack value with reinforcements. */
1202 int victim_count; /* Number of enemies there. */
1203 int needferry; /* Cost of building a ferry boat. */
1204 /* This is a kluge, because if we don't set x and y with !punit->id,
1205 * p_a_w isn't called, and we end up not wanting ironclads and therefore
1206 * never learning steam engine, even though ironclads would be very
1207 * useful. -- Syela */
1208 adv_want bk = 0;
1209 adv_want want; /* Want (amortized) of the operation. */
1210 adv_want best = 0; /* Best of all wants. */
1211 struct tile *goto_dest_tile = nullptr;
1212 bool can_occupy;
1213 struct civ_map *nmap = &(wld.map);
1214
1215 /* Very preliminary checks. */
1217 if (pferrymap != nullptr) {
1218 *pferrymap = nullptr;
1219 }
1220 if (pferryboat != nullptr) {
1221 *pferryboat = nullptr;
1222 }
1223 if (pboattype != nullptr) {
1224 *pboattype = nullptr;
1225 }
1226 if (pmove_time != nullptr) {
1227 *pmove_time = 0;
1228 }
1229 if (ppath != nullptr) {
1230 *ppath = nullptr;
1231 }
1232
1233 if (attack_value == 0) {
1234 /* A very poor attacker... probably low on HP. */
1235 return 0;
1236 }
1237
1239
1240
1241 /*** Part 1: Calculate targets ***/
1242
1243 /* This horrible piece of code attempts to calculate the attractiveness of
1244 * enemy cities as targets for our units, by checking how many units are
1245 * going towards it or are near it already. */
1246
1247 /* Reset enemy cities data. */
1249 /* See comment below in next usage of POTENTIALLY_HOSTILE_PLAYER. */
1250 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1251 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1252 continue;
1253 }
1254 city_list_iterate(aplayer->cities, acity) {
1255 struct ai_city *city_data = def_ai_city_data(acity, ait);
1256
1258 &city_data->attack,
1259 &city_data->bcost);
1260 city_data->invasion.attack = 0;
1261 city_data->invasion.occupy = 0;
1264
1265 /* Second, calculate in units on their way there, and mark targets for
1266 * invasion */
1267 unit_list_iterate(pplayer->units, aunit) {
1268 const struct unit_type *atype;
1269
1270 if (aunit == punit) {
1271 continue;
1272 }
1273
1275
1276 /* Dealing with invasion stuff */
1277 if (IS_ATTACKER(atype)) {
1278 if (aunit->activity == ACTIVITY_GOTO) {
1279 if (invasion_funct(ait, aunit, TRUE, 0,
1282 && (pcity = tile_city(aunit->goto_tile))) {
1283 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1284
1287 }
1288 }
1292 } else if (def_ai_unit_data(aunit, ait)->passenger != 0
1294 /* It's a transport with reinforcements */
1295 if (aunit->activity == ACTIVITY_GOTO) {
1297 }
1299 }
1301 /* End horrible initialization subroutine */
1302
1303
1304 /*** Part 2: Now pick one target ***/
1305
1306 /* We first iterate through all cities, then all units, looking
1307 * for a viable target. We also try to gang up on the target
1308 * to avoid spreading out attacks too widely to be inefficient.
1309 */
1310
1312 if (pcity != nullptr && (punit->id == 0 || pcity->id == punit->homecity)) {
1313 /* I would have thought unhappiness should be taken into account
1314 * irrespectfully the city in which it will surface... -- GB */
1316 }
1317
1320
1322 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1323 punit_map = pf_map_new(&parameter);
1324
1325 if (MOVE_NONE == punit_class->adv.sea_move) {
1326 /* We need boat to move over sea. */
1327 ferryboat = unit_transport_get(punit);
1328
1329 /* First check if we can use the boat we are currently loaded to. */
1330 if (ferryboat == nullptr || !is_boat_free(ait, ferryboat, punit, 0)) {
1331 /* No, we cannot control current boat */
1332 ferryboat = nullptr;
1333 }
1334
1335 if (ferryboat == nullptr) {
1336 /* Try to find new boat */
1337 ferryboat = player_unit_by_number(pplayer,
1338 aiferry_find_boat(ait, punit, 1,
1339 nullptr));
1340 }
1341
1343 TC_OCEAN)) {
1344 harbor = TRUE;
1345 }
1346 }
1347
1348 if (ferryboat != nullptr) {
1349 boattype = unit_type_get(ferryboat);
1350 pft_fill_unit_overlap_param(&parameter, nmap, ferryboat);
1351 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1352 ferry_map = pf_map_new(&parameter);
1353 } else {
1355 if (boattype == nullptr) {
1356 /* We pretend that we can have the simplest boat to stimulate tech. */
1358 }
1359 if (boattype != nullptr && harbor) {
1360 /* Let's simulate a boat at 'punit' position. */
1362 punit_tile, pplayer);
1363 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1364 ferry_map = pf_map_new(&parameter);
1365 } else {
1366 ferry_map = nullptr;
1367 }
1368 }
1369
1371
1373 /* For the virtual unit case, which is when we are called to evaluate
1374 * which units to build, we want to calculate in danger and which
1375 * players we want to make war with in the future. We do _not_ want
1376 * to do this when actually making attacks. */
1377 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1378 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1379 continue; /* Not an enemy. */
1380 }
1381
1382 city_list_iterate(aplayer->cities, acity) {
1383 struct tile *atile = city_tile(acity);
1384
1387 /* Can't attack this city. It is on land. */
1388 continue;
1389 }
1390
1391 if (handicap && !map_is_known(atile, pplayer)) {
1392 /* Can't see it */
1393 continue;
1394 }
1395
1397 go_by_boat = FALSE;
1398 move_time = pos.turn;
1399 } else if (ferry_map == nullptr) {
1400 continue; /* Impossible to handle. */
1401 } else {
1402 struct tile *dest, *beach;
1403
1404 if (!find_beachhead(pplayer, ferry_map, atile, punit_type,
1405 boattype, &dest, &beach)) {
1406 continue; /* Impossible to go by boat. */
1407 }
1408 if (!pf_map_position(ferry_map, dest, &pos)) {
1410 continue;
1411 }
1412 move_time = pos.turn; /* Sailing time. */
1413 if (dest != beach) {
1414 move_time++; /* Land time. */
1415 }
1416 if (ferryboat != nullptr && unit_tile(ferryboat) != punit_tile) {
1417 if (pf_map_position(punit_map, unit_tile(ferryboat), &pos)) {
1418 move_time += pos.turn; /* Time to reach the boat. */
1419 } else {
1420 continue; /* Cannot reach the boat. */
1421 }
1422 }
1423 go_by_boat = TRUE;
1424 }
1425
1428 nullptr))) {
1431 } else {
1432 pdefender = nullptr;
1433 vulnerability = 0;
1434 benefit = 0;
1435 }
1436
1437 if (1 < move_time) {
1439
1440 if (def_type) {
1444
1445 if (v > vulnerability) {
1446 /* They can build a better defender! */
1447 vulnerability = v;
1449 }
1450 }
1451 }
1452
1454
1455 reserves = (acity_data->invasion.attack
1456 - unit_list_size(acity->tile->units));
1457
1458 if (punit->id == 0) {
1459 /* Real unit would add to reserves once built. */
1462 reserves++;
1463 } else {
1464 reserves += punit_type->move_rate;
1465 }
1466 }
1467
1468 if (0 < reserves && (can_occupy
1469 || 0 < acity_data->invasion.occupy)) {
1470 /* There are units able to occupy the city after all defenders
1471 * are killed! */
1472 benefit += acity_data->worth * reserves / 5;
1473 }
1474
1475 attack = attack_value + acity_data->attack;
1476 attack *= attack;
1477 /* Avoiding handling upkeep aggregation this way -- Syela */
1478
1479 /* AI was not sending enough reinforcements to totally wipe out a city
1480 * and conquer it in one turn.
1481 * This variable enables total carnage. -- Syela */
1483
1484 if (!can_occupy && pdefender == nullptr) {
1485 /* Nothing there to bash and we can't occupy!
1486 * Not having this check caused warships yoyoing */
1487 want = 0;
1488 } else if (10 < move_time) {
1489 /* Too far! */
1490 want = 0;
1491 } else if (can_occupy && 0 == acity_data->invasion.occupy
1492 && (0 < acity_data->invasion.attack
1493 || victim_count == 0)) {
1494 /* Units able to occupy really needed there! */
1495 want = bcost * SHIELD_WEIGHTING;
1496 } else {
1497 want = kill_desire(benefit, attack, bcost + acity_data->bcost,
1499 }
1502 /* Build_cost of ferry. */
1503 needferry = (go_by_boat && ferryboat == nullptr
1504 ? utype_build_shield_cost(acity, nullptr, boattype) : 0);
1505 /* FIXME: Add time to build the ferry? */
1507 want, MAX(1, move_time),
1509
1510 /* BEGIN STEAM-ENGINES-ARE-OUR-FRIENDS KLUGE. */
1511 if (0 >= want && 0 == punit->id && 0 >= best) {
1515 MAX(1, move_time),
1517
1518 if (bk_e > bk) {
1519 *pdest_tile = atile;
1520 if (pferrymap != nullptr) {
1521 *pferrymap = go_by_boat ? ferry_map : nullptr;
1522 }
1523 if (pferryboat != nullptr) {
1524 *pferryboat = go_by_boat ? ferryboat : nullptr;
1525 }
1526 if (pboattype != nullptr) {
1527 *pboattype = go_by_boat ? boattype : nullptr;
1528 }
1529 if (pmove_time != nullptr) {
1531 }
1532 goto_dest_tile = (go_by_boat && ferryboat != nullptr
1533 ? unit_tile(ferryboat) : atile);
1534 bk = bk_e;
1535 }
1536 }
1537 /* END STEAM-ENGINES KLUGE */
1538
1539 if (punit->id != 0
1540 && ferryboat != nullptr
1541 && punit_class->adv.sea_move == MOVE_NONE) {
1543 "%s(): with boat %s@(%d, %d) -> %s@(%d, %d)"
1544 " (go_by_boat = %d, move_time = %d, want = " ADV_WANT_PRINTF
1545 ", best = " ADV_WANT_PRINTF ")",
1546 __FUNCTION__, unit_rule_name(ferryboat),
1547 TILE_XY(unit_tile(ferryboat)), city_name_get(acity),
1548 TILE_XY(atile), go_by_boat, move_time, want, best);
1549 }
1550
1551 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1552 /* Yes, we like this target */
1553 best = want;
1554 *pdest_tile = atile;
1555 if (pferrymap != nullptr) {
1556 *pferrymap = go_by_boat ? ferry_map : nullptr;
1557 }
1558 if (pferryboat != nullptr) {
1559 *pferryboat = go_by_boat ? ferryboat : nullptr;
1560 }
1561 if (pboattype != nullptr) {
1562 *pboattype = go_by_boat ? boattype : nullptr;
1563 }
1564 if (pmove_time != nullptr) {
1566 }
1567 goto_dest_tile = (go_by_boat && ferryboat != nullptr
1568 ? unit_tile(ferryboat) : atile);
1569 }
1571
1573 /* I'm not sure the following code is good but it seems to be adequate.
1574 * I am deliberately not adding ferryboat code to the unit_list_iterate().
1575 * -- Syela */
1577 struct tile *atile = unit_tile(aunit);
1578
1579 if (tile_city(atile) != nullptr) {
1580 /* Already dealt with it. */
1581 continue;
1582 }
1583
1584 if (handicap && !map_is_known(atile, pplayer)) {
1585 /* Can't see the target. */
1586 continue;
1587 }
1588
1590 && 0 == punit->id) {
1591 /* We will not build units just to chase caravans and
1592 * ambassadors. */
1593 continue;
1594 }
1595
1596 /* We have to assume the attack is diplomatically ok.
1597 * We cannot use can_player_attack_tile(), because we might not
1598 * be at war with aplayer yet */
1599 if (!can_unit_attack_tile(punit, nullptr, atile)
1600 || aunit != get_defender(nmap, punit, atile, nullptr)) {
1601 /* We cannot attack it, or it is not the main defender. */
1602 continue;
1603 }
1604
1606 /* Cannot reach it. */
1607 continue;
1608 }
1609
1612
1613 move_time = pos.turn;
1614 if (10 < move_time) {
1615 /* Too far. */
1616 want = 0;
1617 } else {
1618 want = kill_desire(benefit, attack, bcost, vulnerability, 1);
1619 /* Take into account maintenance of the unit. */
1620 /* FIXME: Depends on the government. */
1621 want -= move_time * SHIELD_WEIGHTING;
1622 /* Take into account unhappiness
1623 * (costs 2 luxuries to compensate). */
1624 want -= (unhap ? 2 * move_time * TRADE_WEIGHTING : 0);
1625 }
1627 want, MAX(1, move_time), bcost_bal);
1628 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1629 best = want;
1630 *pdest_tile = atile;
1631 if (pferrymap != nullptr) {
1632 *pferrymap = nullptr;
1633 }
1634 if (pferryboat != nullptr) {
1635 *pferryboat = nullptr;
1636 }
1637 if (pboattype != nullptr) {
1638 *pboattype = nullptr;
1639 }
1640 if (pmove_time != nullptr) {
1642 }
1644 }
1647
1648 if (ppath != nullptr) {
1649 *ppath = (goto_dest_tile != nullptr && goto_dest_tile != punit_tile
1650 ? pf_map_path(punit_map, goto_dest_tile) : nullptr);
1651 }
1652
1654 if (ferry_map != nullptr
1655 && (pferrymap == nullptr || *pferrymap != ferry_map)) {
1657 }
1658
1660
1661 return best;
1662}
1663
1664/**********************************************************************/
1673{
1674 struct pf_parameter parameter;
1675 struct pf_map *pfm;
1676 struct player *pplayer = unit_owner(punit);
1677 struct city *pcity, *best_city = nullptr;
1678 int best = FC_INFINITY, cur;
1679 const struct civ_map *nmap = &(wld.map);
1680
1681 pft_fill_unit_parameter(&parameter, nmap, punit);
1682 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1683 pfm = pf_map_new(&parameter);
1684
1685 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
1686 if (move_cost > best) {
1687 /* We already found a better city. No need to continue. */
1688 break;
1689 }
1690
1691 pcity = tile_city(ptile);
1692 if (pcity == nullptr || !pplayers_allied(pplayer, city_owner(pcity))) {
1693 continue;
1694 }
1695
1696 /* Score based on the move cost. */
1697 cur = move_cost;
1698
1699 /* Note the unit owner may be different from the city owner. */
1700 if (0 == get_unittype_bonus(unit_owner(punit), ptile,
1701 unit_type_get(punit), nullptr,
1702 EFT_HP_REGEN)) {
1703 /* If we cannot regen fast our hit points here, let's make some
1704 * penalty. */
1705 cur *= 3;
1706 }
1707
1708 if (cur < best) {
1709 best_city = pcity;
1710 best = cur;
1711 }
1713
1715 return best_city;
1716}
1717
1718/**********************************************************************/
1724 struct player *pplayer,
1725 struct unit *punit)
1726{
1727 struct city *pc;
1728 bool only_continent = TRUE;
1729
1730 if (unit_transported(punit)) {
1731 /* If we are in transport, we can go to any continent.
1732 * Actually, we are not currently in a continent where to stay. */
1734 }
1735
1736 if ((pc = find_closest_city(unit_tile(punit), nullptr, pplayer, FALSE,
1737 only_continent, FALSE, FALSE, TRUE, nullptr))) {
1739 UNIT_LOG(LOG_DEBUG, punit, "Barbarian heading to conquer %s",
1740 city_name_get(pc));
1741 (void) dai_gothere(ait, pplayer, punit, pc->tile);
1742 } else {
1743 struct unit *ferry = nullptr;
1744
1745 if (unit_transported(punit)) {
1746 ferry = unit_transport_get(punit);
1747
1748 /* We already are in a boat so it needs no
1749 * free capacity */
1750 if (!is_boat_free(ait, ferry, punit, 0)) {
1751 /* We cannot control our ferry. */
1752 ferry = nullptr;
1753 }
1754 } else {
1755 /* We are not in a boat yet. Search for one. */
1757 if (is_boat_free(ait, aunit, punit, 1)
1759 ferry = aunit;
1760 break;
1761 }
1763 }
1764
1765 if (ferry) {
1766 UNIT_LOG(LOG_DEBUG, punit, "Barbarian sailing to conquer %s",
1767 city_name_get(pc));
1768 (void) aiferry_goto_amphibious(ait, ferry, punit, pc->tile);
1769 } else {
1770 /* This is not an error. Somebody else might be in charge
1771 * of the ferry. */
1772 UNIT_LOG(LOG_DEBUG, punit, "unable to find barbarian ferry");
1773 }
1774 }
1775 } else {
1776 UNIT_LOG(LOG_DEBUG, punit, "Barbarian find no target city");
1777 }
1778}
1779
1780/**********************************************************************/
1786static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
1787 struct unit *punit)
1788{
1789 struct tile *dest_tile;
1790 int id = punit->id;
1791 int ct = 10;
1792 struct city *pcity = nullptr;
1793
1795
1796 /* Barbarians pillage, and might keep on doing that so they sometimes
1797 * even finish it. */
1798 if (punit->activity == ACTIVITY_PILLAGE && is_barbarian(pplayer)
1799 && fc_rand(2) == 1) {
1800 return;
1801 }
1802
1803 /* First find easy nearby enemies, anything better than pillage goes.
1804 * NB: We do not need to repeat dai_military_rampage(), it is repeats itself
1805 * until it runs out of targets. */
1806 /* FIXME: 1. dai_military_rampage() never checks if it should defend newly
1807 * conquered cities.
1808 * FIXME: 2. would be more convenient if it returned FALSE if we run out
1809 * of moves too.*/
1811 return; /* We died */
1812 }
1813
1814 if (punit->moves_left <= 0) {
1815 return;
1816 }
1817
1818 /* Main attack loop */
1819 do {
1820 struct tile *start_tile = unit_tile(punit);
1821 struct pf_path *path;
1822 struct unit *ferryboat;
1823
1824 /* Then find enemies the hard way */
1825 find_something_to_kill(ait, pplayer, punit, &dest_tile, &path,
1826 nullptr, &ferryboat, nullptr, nullptr);
1827 if (!same_pos(unit_tile(punit), dest_tile)) {
1828 if (!is_tiles_adjacent(unit_tile(punit), dest_tile)
1829 || !can_unit_attack_tile(punit, nullptr, dest_tile)) {
1830
1831 /* Adjacent and can't attack usually means we are not marines
1832 * and on a ferry. This fixes the problem (usually). */
1833 UNIT_LOG(LOG_DEBUG, punit, "mil att gothere -> (%d, %d)",
1834 TILE_XY(dest_tile));
1835
1836 /* Set ACTIVITY_GOTO more permanently than just inside
1837 * adv_follow_path(). This way other units will know we're
1838 * on our way even if we don't reach target yet. */
1839 punit->goto_tile = dest_tile;
1841 if (path != nullptr && !adv_follow_path(punit, path, dest_tile)) {
1842 /* Died. */
1843 pf_path_destroy(path);
1844 return;
1845 }
1846 if (ferryboat != nullptr) {
1847 /* Need a boat. */
1848 aiferry_gobyboat(ait, pplayer, punit, dest_tile, FALSE);
1849 pf_path_destroy(path);
1850 return;
1851 }
1852 if (punit->moves_left <= 0) {
1853 /* No moves left. */
1854 pf_path_destroy(path);
1855 return;
1856 }
1857
1858 /* Either we're adjacent or we sitting on the tile. We might be
1859 * sitting on the tile if the enemy that _was_ sitting there
1860 * attacked us and died _and_ we had enough movement to get there */
1861 if (same_pos(unit_tile(punit), dest_tile)) {
1862 UNIT_LOG(LOG_DEBUG, punit, "mil att made it -> (%d, %d)",
1863 TILE_XY(dest_tile));
1864 pf_path_destroy(path);
1865 break;
1866 }
1867 }
1868
1869 if (is_tiles_adjacent(unit_tile(punit), dest_tile)) {
1870 /* Close combat. fstk sometimes want us to attack an adjacent
1871 * enemy that rampage wouldn't */
1872 UNIT_LOG(LOG_DEBUG, punit, "mil att bash -> (%d, %d)",
1873 TILE_XY(dest_tile));
1874 if (!dai_unit_attack(ait, punit, dest_tile)) {
1875 /* Died */
1876 pf_path_destroy(path);
1877 return;
1878 }
1879 } else if (!same_pos(start_tile, unit_tile(punit))) {
1880 /* Got stuck. Possibly because of adjacency to an
1881 * enemy unit. Perhaps we are in luck and are now next to a
1882 * tempting target? Let's find out... */
1885 pf_path_destroy(path);
1886 return;
1887 }
1888
1889 } else {
1890 /* FIXME: This happens a bit too often! */
1891 UNIT_LOG(LOG_DEBUG, punit, "fstk didn't find us a worthy target!");
1892 /* No worthy enemies found, so abort loop */
1893 ct = 0;
1894 }
1895 pf_path_destroy(path);
1896
1897 ct--; /* Infinite loops from railroads must be stopped */
1898 } while (punit->moves_left > 0 && ct > 0);
1899
1900 /* Cleanup phase */
1901 if (punit->moves_left == 0) {
1902 return;
1903 }
1905 if (pcity != nullptr
1906 && (dai_is_ferry(punit, ait)
1907 || punit->hp < unit_type_get(punit)->hp * 0.50)) { /* WAG */
1908 /* Go somewhere safe */
1909 UNIT_LOG(LOG_DEBUG, punit, "heading to nearest safe house.");
1910 (void) dai_unit_goto(ait, punit, pcity->tile);
1911 } else if (!is_barbarian(pplayer)) {
1912 /* Nothing else to do, so try exploring. */
1913 switch (manage_auto_explorer(punit)) {
1914 case MR_DEATH:
1915 /* Don't use punit! */
1916 return;
1917 case MR_OK:
1918 UNIT_LOG(LOG_DEBUG, punit, "nothing else to do, so exploring");
1919 break;
1920 default:
1921 UNIT_LOG(LOG_DEBUG, punit, "nothing to do - no more exploring either");
1922 break;
1923 };
1924 } else {
1925 /* You can still have some moves left here, but barbarians should
1926 not sit helplessly, but advance towards nearest known enemy city */
1927 UNIT_LOG(LOG_DEBUG, punit, "attack: barbarian");
1928 dai_military_attack_barbarian(ait, pplayer, punit);
1929 }
1930
1931 if ((punit = game_unit_by_number(id)) && punit->moves_left > 0) {
1932 UNIT_LOG(LOG_DEBUG, punit, "attack: giving up unit to defense");
1933 dai_military_defend(ait, pplayer, punit);
1934 }
1935}
1936
1937/**********************************************************************/
1941static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
1942{
1943 bool alive = TRUE;
1944 int ferryboat = 0;
1945 struct pf_path *path_to_ferry = nullptr;
1946
1947 UNIT_LOG(LOG_CARAVAN, punit, "requesting a boat!");
1948 ferryboat = aiferry_find_boat(ait, punit, 1, &path_to_ferry);
1949 /* Going to meet the boat */
1950 if ((ferryboat <= 0)) {
1952 "in find_boat_for_unit cannot find any boats.");
1953 /* If we are undefended on the country side go to a city */
1955
1956 if (current_city == nullptr) {
1958
1959 if (city_near != nullptr) {
1961 }
1962 }
1963 } else {
1964 if (path_to_ferry != nullptr) {
1966 /* Died. */
1968 path_to_ferry = nullptr;
1969 alive = FALSE;
1970 } else {
1972 path_to_ferry = nullptr;
1973 alive = TRUE;
1974 }
1975 }
1976 }
1977
1978 return alive;
1979}
1980
1981/**********************************************************************/
1990 struct tile *ctile, struct tile *ptile)
1991{
1992 /* We usually have Inaccessible terrain, so not testing MOVE_FULL == */
1993 bool lm = MOVE_NONE != pclass->adv.land_move,
1994 sm = MOVE_NONE != pclass->adv.sea_move;
1995 struct civ_map *pmap = &(wld.map);
1996
1997 if (lm && sm) {
1998 return FALSE;
1999 }
2000
2001 /* We could use adjc_iterate() but likely often tiles are on the same
2002 * continent and it will be more time to find where they connect */
2005
2006 if (is_ocean_tile(atile) ? sm : lm) {
2007 iterate_outward(pmap, ptile, 1, btile) {
2008 if (tile_continent(btile) == acont) {
2009 return FALSE;
2010 }
2012 }
2014
2015 if (is_tiles_adjacent(ctile, ptile)) {
2016 return FALSE;
2017 }
2018
2019 return TRUE;
2020}
2021
2022/**********************************************************************/
2028static void dai_caravan_goto(struct ai_type *ait, struct player *pplayer,
2029 struct unit *punit,
2030 const struct city *dest_city,
2031 bool help_wonder,
2032 bool required_boat, bool request_boat)
2033{
2034 bool alive = TRUE;
2035 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2036 const struct civ_map *nmap = &(wld.map);
2037
2038 fc_assert_ret(dest_city != nullptr);
2039
2040 /* if we're not there yet, and we can move, move... */
2041 if (!same_pos(dest_city->tile, unit_tile(punit))
2042 && punit->moves_left != 0) {
2043 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) task %s going to %s in %s %s",
2047 help_wonder ? "help a wonder" : "trade", city_name_get(dest_city),
2048 required_boat ? "with a boat" : "");
2049 if (required_boat) {
2050 /* To trade with boat */
2051 if (request_boat) {
2052 /* Try to find new boat */
2054 } else {
2055 /* If we are not being transported then ask for a boat again */
2056 alive = TRUE;
2059 unit_tile(punit), dest_city->tile)) {
2061 }
2062 }
2063 if (alive) {
2064 /* FIXME: Sometimes we get FALSE here just because
2065 * a trireme that we've boarded can't go over an ocean. */
2066 alive = dai_gothere(ait, pplayer, punit, dest_city->tile);
2067 }
2068 } else {
2069 /* To trade without boat */
2071 }
2072 }
2073
2074 /* If moving didn't kill us, and we got to the destination, handle it. */
2075 if (alive && real_map_distance(dest_city->tile, unit_tile(punit)) <= 1) {
2076 /* Release the boat! */
2077 if (unit_transported(punit)) {
2079 }
2081 punit, dest_city)) {
2082 /*
2083 * We really don't want to just drop all caravans in immediately.
2084 * Instead, we want to aggregate enough caravans to build instantly.
2085 * -AJS, 990704
2086 */
2087 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) helps build wonder in %s",
2090 punit->id,
2093 unit_do_action(pplayer, punit->id, dest_city->id,
2094 0, "", ACTION_HELP_WONDER);
2096 punit, dest_city)) {
2097 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) creates trade route in %s",
2100 punit->id,
2103 unit_do_action(pplayer, punit->id, dest_city->id,
2104 0, "", ACTION_TRADE_ROUTE);
2106 punit, dest_city)) {
2107 /* Get the one time bonus. */
2108 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) enters marketplace of %s",
2111 punit->id,
2114 unit_do_action(pplayer, punit->id, dest_city->id,
2115 0, "", ACTION_MARKETPLACE);
2116 } else {
2117 enum log_level level = LOG_NORMAL;
2118
2119 if (help_wonder) {
2120 /* A Caravan ordered to help build wonder may arrive after
2121 * enough shields to build the wonder is produced. */
2123 }
2124
2126 "%s %s[%d](%d,%d) unable to trade with %s",
2129 punit->id,
2132 }
2133 }
2134}
2135
2136/**********************************************************************/
2140static void caravan_optimize_callback(const struct caravan_result *result,
2141 void *data)
2142{
2143 const struct unit *caravan = data;
2144
2145 log_base(LOG_CARAVAN3, "%s %s[%d](%d,%d) %s: %s %s worth %g",
2147 unit_rule_name(caravan),
2148 caravan->id,
2149 TILE_XY(unit_tile(caravan)),
2150 city_name_get(result->src),
2151 result->help_wonder ? "wonder in" : "trade to",
2152 city_name_get(result->dest),
2153 result->value);
2154}
2155
2156/**********************************************************************/
2160 struct unit *punit)
2161{
2162 struct tile *src = nullptr, *dest = nullptr, *src_home_city = nullptr;
2163 struct city *phome_city = nullptr;
2164 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2165
2166 if ((unit_data->task != AIUNIT_NONE)) {
2167 src = unit_tile(punit);
2169 if (phome_city != nullptr) {
2171 }
2172 dest = punit->goto_tile;
2173
2174 if (src == nullptr || dest == nullptr) {
2175 return FALSE;
2176 }
2177
2178 /* If we have a home continent, and are not there.
2179 * (FIXME: well, why?)
2180 * (I guess because home continent is which we were supposed to leave,
2181 * not the target continent) */
2182 if (src_home_city != nullptr
2184 return FALSE;
2185 }
2186
2187 if (!goto_is_sane(punit, dest)) {
2188 if (unit_transported(punit)) {
2189 /* If we're being transported */
2190 return FALSE;
2191 }
2192 if ((punit->birth_turn + 15 < game.info.turn)) {
2193 /* We are tired of waiting */
2195
2196 if (ferrys <= 0) {
2197 /* There are no ferrys available... give up */
2198 return TRUE;
2199 } else {
2200 if (punit->birth_turn + 20 < game.info.turn) {
2201 /* We are fed up! */
2202 return TRUE;
2203 }
2204 }
2205 }
2206 }
2207 }
2208
2209 return FALSE;
2210}
2211
2212/**********************************************************************/
2219 struct unit *punit)
2220{
2222 Continent_id continent;
2223
2224 fc_assert(pcity != nullptr);
2225
2226 if (unit_class_get(punit)->adv.ferry_types <= 0) {
2227 /* There is just no possible transporters. */
2228 return FALSE;
2229 }
2230 continent = tile_continent(pcity->tile);
2231
2232 /* Look for proper destination city at different continent. */
2233 city_list_iterate(pplayer->cities, acity) {
2235 if (tile_continent(acity->tile) != continent) {
2236 return TRUE;
2237 }
2238 }
2240
2242 if (aplayer == pplayer || !aplayer->is_alive) {
2243 continue;
2244 }
2245 if (pplayers_allied(pplayer, aplayer)) {
2246 city_list_iterate(aplayer->cities, acity) {
2248 if (tile_continent(acity->tile) != continent) {
2249 return TRUE;
2250 }
2251 }
2254 }
2255
2256 return FALSE;
2257}
2258
2259/**********************************************************************/
2263static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
2264{
2265 struct city *nearest = nullptr;
2266 int min_dist = FC_INFINITY;
2267 struct tile *current_loc = unit_tile(punit);
2269 bool alive = TRUE;
2270
2272 struct tile *ctile = city_tile(pcity);
2273
2274 if (tile_continent(ctile) == continent) {
2276
2277 if (this_dist < min_dist) {
2279 nearest = pcity;
2280 }
2281 }
2283
2284 if (nearest != nullptr) {
2286 if (alive && same_pos(unit_tile(punit), nearest->tile)) {
2288 }
2289 }
2290
2291 return alive;
2292}
2293
2294/**********************************************************************/
2300static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
2301 struct unit *punit)
2302{
2303 struct caravan_parameter parameter;
2304 struct caravan_result result;
2305 const struct city *homecity;
2306 const struct city *dest = nullptr;
2307 struct unit_ai *unit_data;
2309 bool expect_boats = pclass->adv.ferry_types > 0;
2310 /* TODO: Will pplayer have a boat for punit in a reasonable time? */
2311 bool help_wonder = FALSE;
2312 bool required_boat = FALSE;
2313 bool request_boat = FALSE;
2315 const struct civ_map *nmap = &(wld.map);
2316
2318
2322 /* We only want units that can establish trade, enter marketplace or
2323 * help build wonders */
2324 return;
2325 }
2326
2328
2329 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) task %s to (%d,%d)",
2334
2335 homecity = game_city_by_number(punit->homecity);
2336 if (homecity == nullptr && unit_data->task == AIUNIT_TRADE) {
2338 return;
2339 }
2340 homecity = game_city_by_number(punit->homecity);
2341 if (homecity == nullptr) {
2342 return;
2343 }
2344 }
2345
2346 if ((unit_data->task == AIUNIT_TRADE
2347 || unit_data->task == AIUNIT_WONDER)) {
2348 /* We are moving to our destination */
2349 /* We check to see if our current goal is feasible */
2351 struct goods_type *pgood = unit_current_goods(punit, homecity);
2352
2353 if ((city_dest == nullptr)
2355 || (unit_data->task == AIUNIT_TRADE
2356 && !(can_cities_trade(homecity, city_dest)
2358 pgood->replace_priority)))
2359 || (unit_data->task == AIUNIT_WONDER
2360 /* Helping the (new) production is illegal. */
2362 || (unit_data->task == AIUNIT_TRADE
2367 punit, city_dest)))
2368 || (unit_data->task == AIUNIT_WONDER
2371 punit, city_dest))) {
2372 /* Destination invalid! */
2373 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2374 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) destination invalid!",
2377 } else {
2378 /* Destination valid. Are we tired of waiting for a boat? */
2381 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2382 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) unit tired of waiting!",
2386 } else {
2387 dest = city_dest;
2388 help_wonder = (unit_data->task == AIUNIT_WONDER) ? TRUE : FALSE;
2389 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2391 }
2392 }
2393 }
2394
2395 if (unit_data->task == AIUNIT_NONE) {
2396 if (homecity == nullptr) {
2397 /* FIXME: We shouldn't bother in getting homecity for
2398 * caravan that will then be used for wonder building. */
2400 return;
2401 }
2402 homecity = game_city_by_number(punit->homecity);
2403 if (homecity == nullptr) {
2404 return;
2405 }
2406 }
2407
2409 /* Make more trade with allies than other peaceful nations
2410 * by considering only allies 50% of the time.
2411 * (the other 50% allies are still considered, but so are other nations) */
2412 if (fc_rand(2)) {
2413 /* Be optimistic about development of relations with no-contact and
2414 * cease-fire nations. */
2415 parameter.allow_foreign_trade = FTL_NONWAR;
2416 } else {
2417 parameter.allow_foreign_trade = FTL_ALLIED;
2418 }
2419
2422 parameter.callback_data = punit;
2423 }
2425 parameter.ignore_transit_time = TRUE;
2426 }
2429 parameter.ignore_transit_time = FALSE;
2430 }
2431 caravan_find_best_destination(nmap, punit, &parameter, &result,
2432 !has_handicap(pplayer, H_MAP));
2433 if (result.dest != nullptr) {
2434 /* We did find a new destination for the unit */
2435 dest = result.dest;
2436 help_wonder = result.help_wonder;
2437 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2438 request_boat = required_boat;
2440 (help_wonder) ? AIUNIT_WONDER : AIUNIT_TRADE,
2441 dest->tile);
2442 } else {
2443 dest = nullptr;
2444 }
2445 }
2446
2447 if (required_boat && !expect_boats) {
2448 /* Would require boat, but can't have them. Render destination invalid. */
2449 dest = nullptr;
2450 }
2451
2452 if (dest != nullptr) {
2453 dai_caravan_goto(ait, pplayer, punit, dest, help_wonder,
2454 required_boat, request_boat);
2455 return; /* That may have clobbered the unit */
2456 } else {
2457 /* We have nowhere to go! */
2458 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d), nothing to do!",
2462 /* Should we become a defensive unit? */
2463 }
2464}
2465
2466/**********************************************************************/
2471 struct unit *punit)
2472{
2473 struct player *pplayer = unit_owner(punit);
2474 struct city *pcity = tile_city(unit_tile(punit));
2475 struct city *safe = nullptr;
2476 const struct unit_type *punittype = unit_type_get(punit);
2477 bool no_recovery;
2478
2480
2481 if (pcity != nullptr) {
2482 /* Rest in the city until the hitpoints are recovered, but attempt
2483 * to protect city from attack (and be opportunistic too)*/
2486 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "recovering hit points.");
2487 } else {
2488 return; /* We died heroically defending our city */
2489 }
2490 } else {
2491 /* Goto to nearest city to recover hit points */
2492 /* Just before, check to see if we can occupy an undefended enemy city */
2495 return; /* Oops, we died */
2496 }
2497
2498 /* Find a city to stay and go there */
2500 if (safe) {
2501 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "going to %s to recover",
2503 if (!dai_unit_goto(ait, punit, safe->tile)) {
2504 log_base(LOGLEVEL_RECOVERY, "died trying to hide and recover");
2505 return;
2506 }
2507 } else {
2508 /* Oops */
2509 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "didn't find a city to recover in!");
2510 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2511 dai_military_attack(ait, pplayer, punit);
2512 return;
2513 }
2514 }
2515
2516 /* Is the unit still damaged? If true, and could recover hit points, do so.
2517 * Don't wait if would be losing hitpoints that way! */
2519 if (punit->hp == punittype->hp) {
2520 no_recovery = TRUE;
2521 } else {
2523
2524 if (gain < 0) {
2525 no_recovery = TRUE;
2526 } else if (gain == 0 && !punit->moved) {
2527 /* Isn't gaining even though has not moved. */
2528 no_recovery = TRUE;
2529 }
2530 }
2531 if (no_recovery) {
2532 /* We are ready to go out and kick ass again */
2533 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "ready to kick ass again!");
2534 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2535 return;
2536 } else {
2537 def_ai_unit_data(punit, ait)->done = TRUE; /* Sit tight */
2538 }
2539}
2540
2541/**********************************************************************/
2545void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap,
2546 struct player *pplayer, struct unit *punit)
2547{
2548 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2549 int id = punit->id;
2550
2552
2553 /* "Escorting" aircraft should not do anything. They are activated
2554 * by their transport or charge. We do _NOT_ set them to 'done'
2555 * since they may need be activated once our charge moves. */
2556 if (unit_data->task == AIUNIT_ESCORT
2558 return;
2559 }
2560
2563 && has_handicap(pplayer, H_AWAY)) {
2564 /* Don't move sentried or fortified units controlled by a player
2565 * in away mode. */
2566 unit_data->done = TRUE;
2567 return;
2568 }
2569
2570 /* Since military units re-evaluate their actions every turn,
2571 we must make sure that previously reserved ferry is freed. */
2573
2575 /* Try hunting with this unit */
2576 if (dai_hunter_qualify(pplayer, punit)) {
2577 int result, sanity = punit->id;
2578
2579 UNIT_LOG(LOGLEVEL_HUNT, punit, "is qualified as hunter");
2580 result = dai_hunter_manage(ait, pplayer, punit);
2581 if (game_unit_by_number(sanity) == nullptr) {
2583 return; /* Died */
2584 }
2585 if (result == -1) {
2586 (void) dai_hunter_manage(ait, pplayer, punit); /* More carnage */
2588 return;
2589 } else if (result >= 1) {
2591 return; /* Done moving */
2592 } else if (unit_data->task == AIUNIT_HUNTER) {
2593 /* This should be very rare */
2594 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2595 }
2596 } else if (unit_data->task == AIUNIT_HUNTER) {
2597 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2598 }
2600
2601 /* Do we have a specific job for this unit? If not, we default
2602 * to attack. */
2603 dai_military_findjob(ait, nmap, pplayer, punit);
2604
2605 switch (unit_data->task) {
2606 case AIUNIT_AUTO_WORKER:
2607 case AIUNIT_BUILD_CITY:
2608 fc_assert(FALSE); /* This is not the place for this role */
2609 break;
2610 case AIUNIT_DEFEND_HOME:
2612 dai_military_defend(ait, pplayer, punit);
2614 break;
2615 case AIUNIT_ATTACK:
2616 case AIUNIT_NONE:
2618 dai_military_attack(ait, pplayer, punit);
2620 break;
2621 case AIUNIT_ESCORT:
2623 dai_military_bodyguard(ait, pplayer, punit);
2625 break;
2626 case AIUNIT_EXPLORE:
2627 switch (manage_auto_explorer(punit)) {
2628 case MR_DEATH:
2629 /* Don't use punit! */
2630 return;
2631 case MR_OK:
2632 UNIT_LOG(LOG_DEBUG, punit, "more exploring");
2633 break;
2634 default:
2635 UNIT_LOG(LOG_DEBUG, punit, "no more exploring either");
2636 break;
2637 };
2638 def_ai_unit_data(punit, ait)->done = (punit->moves_left <= 0);
2639 break;
2640 case AIUNIT_RECOVER:
2644 break;
2645 case AIUNIT_HUNTER:
2646 fc_assert(FALSE); /* Dealt with above */
2647 break;
2648 default:
2650 }
2651
2652 /* If we are still alive, either sentry or fortify. */
2653 if ((punit = game_unit_by_number(id))) {
2655 struct city *pcity = tile_city(unit_tile(punit));
2656
2657 if (unit_list_find(unit_tile(punit)->units,
2658 unit_data->ferryboat)) {
2660 } else if (pcity || punit->activity == ACTIVITY_IDLE) {
2661 /* We do not need to fortify in cities - we fortify and sentry
2662 * according to home defense setup, for easy debugging. */
2663 if (!pcity || unit_data->task == AIUNIT_DEFEND_HOME) {
2668 }
2669 } else {
2671 }
2672 }
2673 }
2674}
2675
2676/**********************************************************************/
2679static void dai_manage_settler(struct ai_type *ait, struct player *pplayer,
2680 struct unit *punit)
2681{
2682 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2683
2685 unit_data->done = TRUE; /* We will manage this unit later... ugh */
2686 /* If BUILD_CITY must remain BUILD_CITY, otherwise turn into autoworker */
2687 if (unit_data->task == AIUNIT_NONE) {
2689 }
2690}
2691
2692/**********************************************************************/
2700void dai_manage_unit(struct ai_type *ait, struct player *pplayer,
2701 struct unit *punit)
2702{
2703 struct unit_ai *unit_data;
2704 struct unit *bodyguard = aiguard_guard_of(ait, punit);
2705 bool is_ferry = FALSE;
2706 const struct unit_type *ptype;
2707 const struct civ_map *nmap = &(wld.map);
2708
2710
2711 /* Don't manage the unit if it is under human orders. */
2712 if (unit_has_orders(punit)) {
2714
2715 UNIT_LOG(LOG_VERBOSE, punit, "is under human orders, aborting AI control.");
2716 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2717 unit_data->done = TRUE;
2718
2719 return;
2720 }
2721
2722 /* Check if we have lost our bodyguard. If we never had one, all
2723 * fine. If we had one and lost it, ask for a new one. */
2724 if (!bodyguard && aiguard_has_guard(ait, punit)) {
2725 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "lost bodyguard, asking for new");
2727 }
2728
2730
2731 if (punit->moves_left <= 0) {
2732 /* Can do nothing */
2733 unit_data->done = TRUE;
2734
2735 return;
2736 }
2737
2738 is_ferry = dai_is_ferry(punit, ait);
2739
2741
2744 dai_manage_diplomat(ait, pplayer, punit);
2746
2747 return;
2748 } else if (ptype->adv.worker
2750 dai_manage_settler(ait, pplayer, punit);
2751
2752 return;
2757 dai_manage_caravan(ait, pplayer, punit);
2759
2760 return;
2762 dai_manage_barbarian_leader(ait, pplayer, punit);
2763
2764 return;
2767 dai_manage_paratrooper(ait, pplayer, punit);
2768
2769 return;
2770 } else if (is_ferry && unit_data->task != AIUNIT_HUNTER) {
2772 dai_manage_ferryboat(ait, pplayer, punit);
2774
2775 return;
2776 } else if (utype_fuel(ptype)
2777 && unit_data->task != AIUNIT_ESCORT) {
2779 dai_manage_airunit(ait, pplayer, punit);
2781
2782 return;
2783 } else if (is_losing_hp(punit)) {
2784 /* This unit is losing hitpoints over time */
2785
2786 /* TODO: We can try using air-unit code for helicopters, just
2787 * pretend they have fuel = HP / 3 or something. */
2788 unit_data->done = TRUE; /* we did our best, which was ...
2789 nothing */
2790 return;
2791 } else if (!is_special_unit(punit)) {
2793 UNIT_LOG(LOG_DEBUG, punit, "recruit unit for the military");
2794 dai_manage_military(ait, nmap, pplayer, punit);
2796
2797 return;
2798 } else {
2799 /* what else could this be? -- Syela */
2800 switch (manage_auto_explorer(punit)) {
2801 case MR_DEATH:
2802 /* don't use punit! */
2803 break;
2804 case MR_OK:
2805 UNIT_LOG(LOG_DEBUG, punit, "now exploring");
2806 break;
2807 default:
2808 UNIT_LOG(LOG_DEBUG, punit, "fell through all unit tasks, defending");
2810 dai_military_defend(ait, pplayer, punit);
2811 break;
2812 };
2813
2814 return;
2815 }
2816}
2817
2818/**********************************************************************/
2824static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
2825{
2826 city_list_iterate(pplayer->cities, pcity) {
2827 /* The idea here is that we should never keep more than two
2828 * units in permanent defense. */
2829 int total_defense = 0;
2831 bool emergency = FALSE;
2832 int count = 0;
2837 int entertainers = 0;
2838 bool enough = FALSE;
2839
2842 entertainers += pcity->specialists[sp];
2843 }
2845
2846 martless_unhappy += entertainers; /* We want to use martial law instead
2847 * of entertainers. */
2848
2849 while (!enough
2852 && martless_unhappy > mart_each * count))) {
2853 int best_want = 0;
2854 struct unit *best = nullptr;
2855 bool defense_needed = total_defense <= total_attack; /* Defense or martial */
2856
2858 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2859
2860 if ((unit_data->task == AIUNIT_NONE || emergency)
2861 && unit_data->task != AIUNIT_DEFEND_HOME
2862 && unit_owner(punit) == pplayer) {
2863 int want = assess_defense_unit(ait, pcity, punit, FALSE);
2864
2865 if (want > best_want) {
2866 best_want = want;
2867 best = punit;
2868 }
2869 }
2871
2872 if (best == nullptr) {
2873 if (defense_needed) {
2874 /* Ooops - try to grab any unit as defender! */
2875 if (emergency) {
2876 CITY_LOG(LOG_DEBUG, pcity, "Not defended properly");
2877 break;
2878 }
2879 emergency = TRUE;
2880 } else {
2881 break;
2882 }
2883 } else {
2884 const struct unit_type *btype = unit_type_get(best);
2885
2886 if ((martless_unhappy < mart_each * count
2887 || count >= mart_max || mart_each <= 0)
2888 && ((count >= 2
2889 && btype->attack_strength > btype->defense_strength)
2890 || (count >= 4
2891 && btype->attack_strength == btype->defense_strength))) {
2892 /* In this case attack would be better defense than fortifying
2893 * to city. */
2894 enough = TRUE;
2895 } else {
2896 int loglevel = pcity->server.debug ? LOG_AI_TEST : LOG_DEBUG;
2897
2899 UNIT_LOG(loglevel, best, "Defending city");
2901 count++;
2902 }
2903 }
2904 }
2905 CITY_LOG(LOG_DEBUG, pcity, "Evaluating defense: %d defense, %d incoming"
2906 ", %d defenders (out of %d)", total_defense, total_attack, count,
2909}
2910
2911/**********************************************************************/
2919void dai_manage_units(struct ai_type *ait, struct player *pplayer)
2920{
2922 dai_airlift(ait, pplayer);
2924
2925 /* Clear previous orders, if desirable, here. */
2926 unit_list_iterate(pplayer->units, punit) {
2927 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2928
2929 unit_data->done = FALSE;
2930 if (unit_data->task == AIUNIT_DEFEND_HOME) {
2931 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2932 }
2934
2935 /* Find and set city defenders first - figure out which units are
2936 * allowed to leave home. */
2937 dai_set_defenders(ait, pplayer);
2938
2940 if ((!unit_transported(punit)
2941 || unit_owner(unit_transport_get(punit)) != pplayer)
2942 && !def_ai_unit_data(punit, ait)->done) {
2943 /* Though it is usually the passenger who drives the transport,
2944 * the transporter is responsible for managing its passengers. */
2945 dai_manage_unit(ait, pplayer, punit);
2946 }
2948}
2949
2950/**********************************************************************/
2956 const struct city *pcity)
2957{
2958 const struct impr_type *impr_req = nullptr;
2959 const struct req_context context = {
2961 .city = pcity,
2962 .tile = city_tile(pcity),
2963 .unittype = putype,
2964 };
2965
2966 requirement_vector_iterate(&putype->build_reqs, preq) {
2967 if (is_req_active(&context, nullptr, preq, RPT_CERTAIN)) {
2968 /* Already there. */
2969 continue;
2970 }
2972 city_owner(pcity), pcity)) {
2973 /* The unit type can't be built at all. */
2974 return nullptr;
2975 }
2976 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2977 /* This is (one of) the building(s) required. */
2978 impr_req = preq->source.value.building;
2979 }
2981
2982 return impr_req;
2983}
2984
2985/**********************************************************************/
2990 const struct unit_type *base)
2991{
2992 /* This is the real function: */
2993 do {
2994 base = base->obsoleted_by;
2995 if (base == test) {
2996 return TRUE;
2997 }
2998 } while (base);
2999 return FALSE;
3000}
3001
3002/**********************************************************************/
3006static void dai_manage_barbarian_leader(struct ai_type *ait,
3007 struct player *pplayer,
3008 struct unit *leader)
3009{
3011 struct pf_parameter parameter;
3012 struct pf_map *pfm;
3013 struct pf_reverse_map *pfrm;
3014 struct unit *worst_danger;
3015 int move_cost, best_move_cost;
3016 int body_guards;
3017 bool alive = TRUE;
3018 const struct civ_map *nmap = &(wld.map);
3019
3021
3022 if (leader->moves_left == 0
3024 && 1 < unit_list_size(leader_tile->units))) {
3026
3027 return;
3028 }
3029
3030 if (is_boss_of_boat(ait, leader)) {
3031 /* We are in charge. Of course, since we are the leader...
3032 * But maybe somebody more militaristic should lead our ship to battle! */
3033
3034 /* First release boat from leaders lead */
3036
3040 && warrior->moves_left > 0) {
3041 /* This seems like a good warrior to lead us in to conquest! */
3042 dai_manage_unit(ait, pplayer, warrior);
3043
3044 /* If we reached our destination, ferryboat already called
3045 * ai_manage_unit() for leader. So no need to continue here.
3046 * Leader might even be dead.
3047 * If this return is removed, surrounding unit_list_iterate()
3048 * has to be replaced with unit_list_iterate_safe()*/
3049 return;
3050 }
3052 }
3053
3054 /* If we are not in charge of the boat, continue as if we
3055 * were not in a boat - we may want to leave the ship now. */
3056
3057 /* Check the total number of units able to protect our leader. */
3058 body_guards = 0;
3059 unit_list_iterate(pplayer->units, punit) {
3062 body_guards++;
3063 }
3065
3066 if (0 < body_guards) {
3067 pft_fill_unit_parameter(&parameter, nmap, leader);
3068 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3069 pfm = pf_map_new(&parameter);
3070
3071 /* Find the closest body guard.
3072 * FIXME: Maybe choose the strongest too? */
3073 pf_map_tiles_iterate(pfm, ptile, FALSE) {
3074 unit_list_iterate(ptile->units, punit) {
3075 if (unit_owner(punit) == pplayer
3078 struct pf_path *path = pf_map_path(pfm, ptile);
3079
3080 adv_follow_path(leader, path, ptile);
3081 pf_path_destroy(path);
3083
3084 return;
3085 }
3088
3090 }
3091
3092 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader needs to flee");
3093
3094 /* Check for units we could fear. */
3095 pfrm = pf_reverse_map_new(nmap, pplayer, leader_tile, 3,
3096 !has_handicap(pplayer, H_MAP));
3097 worst_danger = nullptr;
3099
3100 players_iterate(other_player) {
3101 if (other_player == pplayer) {
3102 continue;
3103 }
3104
3105 unit_list_iterate(other_player->units, punit) {
3107 if (PF_IMPOSSIBLE_MC != move_cost && move_cost < best_move_cost) {
3108 best_move_cost = move_cost;
3110 }
3113
3115
3116 if (worst_danger == nullptr) {
3118 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: no close enemy.");
3119 return;
3120 }
3121
3123 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3124 pfm = pf_map_new(&parameter);
3126
3127 /* Try to escape. */
3128 do {
3130
3131 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: moves left: %d.",
3132 leader->moves_left);
3133
3136 continue;
3137 }
3138
3139 move_cost = pf_map_move_cost(pfm, near_tile);
3140 if (PF_IMPOSSIBLE_MC != move_cost
3141 && move_cost > best_move_cost) {
3143 "Barbarian leader: safest is (%d, %d), safeness %d",
3145 best_move_cost = move_cost;
3147 }
3149
3150 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: fleeing to (%d, %d).",
3154 "Barbarian leader: reached the safest position.");
3157 return;
3158 }
3159
3161 if (alive) {
3163 /* Didn't move. No point to retry. */
3165 return;
3166 }
3168 }
3169 } while (alive && 0 < leader->moves_left);
3170
3172}
3173
3174/**********************************************************************/
3180void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile,
3181 struct unit *punit,
3182 enum override_bool *result)
3183{
3184 int a = 0, d, db;
3185 struct player *pplayer = unit_owner(punit);
3186 struct city *pcity = tile_city(ptile);
3187 int extras_bonus = 0;
3188
3189 if (is_human(pplayer)) {
3190 /* Use advisors code for humans. */
3191 return;
3192 }
3193
3194 if (pcity != nullptr && pplayers_allied(city_owner(pcity), pplayer)
3195 && !is_non_allied_unit_tile(ptile, pplayer,
3197 /* We will be safe in a friendly city */
3198 *result = OVERRIDE_FALSE;
3199 return;
3200 }
3201
3202 /* Calculate how well we can defend at (x,y) */
3203 db = 10 + tile_terrain(ptile)->defense_bonus / 10;
3205
3206 db += (db * extras_bonus) / 100;
3208
3209 adjc_iterate(&(wld.map), ptile, ptile1) {
3210 if (has_handicap(pplayer, H_FOG)
3211 && !map_is_known_and_seen(ptile1, pplayer, V_MAIN)) {
3212 /* We cannot see danger at (ptile1) => assume there is none */
3213 continue;
3214 }
3215 unit_list_iterate(ptile1->units, enemy) {
3216 if (pplayers_at_war(unit_owner(enemy), pplayer)
3217 && (unit_attack_unit_at_tile_result(enemy, nullptr, punit, ptile)
3218 == ATT_OK)
3219 && (unit_attack_units_at_tile_result(enemy, nullptr, ptile)
3220 == ATT_OK)) {
3222 if ((a * a * 10) >= d) {
3223 /* The enemies combined strength is too big! */
3224 *result = OVERRIDE_TRUE;
3225 return;
3226 }
3227 }
3230
3231 *result = OVERRIDE_FALSE;
3232}
3233
3234/**********************************************************************/
3237static void update_simple_ai_types(void)
3238{
3239 int i = 0;
3240
3243
3246 && !(pclass->adv.land_move == MOVE_NONE
3249 && punittype->transport_capacity < 8) {
3251 i++;
3252 }
3254
3255 simple_ai_types[i] = nullptr;
3256}
3257
3258/**********************************************************************/
3262{
3263 /* TODO: Remove the simple_ai_types cache or merge it with a general ai
3264 * cache; see the comment to struct unit_type *simple_ai_types at
3265 * the beginning of this file. */
3267
3269 struct unit_type_ai *utai = fc_malloc(sizeof(*utai));
3270
3271 utai->low_firepower = FALSE;
3272 utai->ferry = FALSE;
3273 utai->missile_platform = FALSE;
3274 utai->carries_occupiers = FALSE;
3275 utai->potential_charges = unit_type_list_new();
3276
3279
3282
3283 /* Confirm firepower */
3285 if (pbonus->type == CBONUS_LOW_FIREPOWER) {
3287 if (utype_has_flag(penemy, pbonus->flag)) {
3288 struct unit_type_ai *utai = utype_ai_data(penemy, ait);
3289
3290 utai->low_firepower = TRUE;
3291 }
3293 }
3295
3296 /* Consider potential cargo */
3297 if (punittype->transport_capacity > 0) {
3298 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3299
3302
3305 utai->missile_platform = TRUE;
3306 } else if (pclass->adv.sea_move != MOVE_NONE
3307 && pcargo->adv.land_move != MOVE_NONE) {
3308 if (pcargo->adv.sea_move != MOVE_FULL) {
3309 utai->ferry = TRUE;
3310 } else {
3311 if (0 != utype_fuel(pctype)) {
3312 utai->ferry = TRUE;
3313 }
3314 }
3315 }
3316
3318 utai->carries_occupiers = TRUE;
3319 }
3320 }
3322 }
3323
3324 /* Consider potential charges */
3327
3328 if (0 < utype_fuel(punittype)
3329 && (0 == utype_fuel(pcharge)
3331 continue;
3332 }
3333
3334 unit_class_list_iterate(pclass->cache.subset_movers, chgcls) {
3335 if (chgcls == utype_class(pcharge)) {
3337 }
3339
3341 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3342 unit_type_list_append(utai->potential_charges, pcharge);
3343 }
3344
3347}
3348
3349/**********************************************************************/
3353{
3355 struct unit_type_ai *utai = utype_ai_data(ptype, ait);
3356
3357 if (utai == nullptr) {
3358 continue;
3359 }
3360 utype_set_ai_data(ptype, ait, nullptr);
3361
3362 unit_type_list_destroy(utai->potential_charges);
3363 free(utai);
3365}
3366
3367/**********************************************************************/
3370void dai_unit_init(struct ai_type *ait, struct unit *punit)
3371{
3372 /* Make sure that contents of unit_ai structure are correctly initialized,
3373 * if you ever allocate it by some other mean than fc_calloc() */
3374 struct unit_ai *unit_data = fc_calloc(1, sizeof(struct unit_ai));
3375
3376 unit_data->done = FALSE;
3377 unit_data->cur_pos = nullptr;
3378 unit_data->prev_pos = nullptr;
3379 unit_data->target = 0;
3380 BV_CLR_ALL(unit_data->hunted);
3381 unit_data->ferryboat = 0;
3382 unit_data->passenger = 0;
3383 unit_data->bodyguard = 0;
3384 unit_data->charge = 0;
3385
3387}
3388
3389/**********************************************************************/
3392void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
3393{
3394 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3395
3396 fc_assert_ret(unit_data != nullptr);
3397
3398 BV_CLR_ALL(unit_data->hunted);
3399}
3400
3401/**********************************************************************/
3404void dai_unit_close(struct ai_type *ait, struct unit *punit)
3405{
3406 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3407
3408 fc_assert_ret(unit_data != nullptr);
3409
3412
3413 if (unit_data != nullptr) {
3414 unit_set_ai_data(punit, ait, nullptr);
3416 }
3417}
3418
3419/**********************************************************************/
3422void dai_unit_save(struct ai_type *ait, const char *aitstr,
3423 struct section_file *file,
3424 const struct unit *punit, const char *unitstr)
3425{
3426 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3427
3428 secfile_insert_int(file, unit_data->passenger, "%s.%spassenger",
3429 unitstr, aitstr);
3430 secfile_insert_int(file, unit_data->ferryboat, "%s.%sferryboat",
3431 unitstr, aitstr);
3432 secfile_insert_int(file, unit_data->charge, "%s.%scharge",
3433 unitstr, aitstr);
3434 secfile_insert_int(file, unit_data->bodyguard, "%s.%sbodyguard",
3435 unitstr, aitstr);
3436}
3437
3438/**********************************************************************/
3441void dai_unit_load(struct ai_type *ait, const char *aitstr,
3442 const struct section_file *file,
3443 struct unit *punit, const char *unitstr)
3444{
3445 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3446
3447 unit_data->passenger
3448 = secfile_lookup_int_default(file, 0, "%s.%spassenger",
3449 unitstr, aitstr);
3450 unit_data->ferryboat
3451 = secfile_lookup_int_default(file, 0, "%s.%sferryboat",
3452 unitstr, aitstr);
3453 unit_data->charge
3454 = secfile_lookup_int_default(file, 0, "%s.%scharge",
3455 unitstr, aitstr);
3456 unit_data->bodyguard
3457 = secfile_lookup_int_default(file, 0, "%s.%sbodyguard",
3458 unitstr, aitstr);
3459}
3460
3466
3467/**********************************************************************/
3470static bool role_unit_cb(struct unit_type *ptype, void *data)
3471{
3472 struct role_unit_cb_data *cb_data = (struct role_unit_cb_data *)data;
3474 const struct civ_map *nmap = &(wld.map);
3475
3476 if ((cb_data->tc == TC_LAND && pclass->adv.land_move == MOVE_NONE)
3477 || (cb_data->tc == TC_OCEAN && pclass->adv.sea_move == MOVE_NONE)) {
3478 return FALSE;
3479 }
3480
3481 if (cb_data->build_city == nullptr
3482 || can_city_build_unit_now(nmap, cb_data->build_city, ptype,
3483 RPT_CERTAIN)) {
3484 return TRUE;
3485 }
3486
3487 return FALSE;
3488}
3489
3490/**********************************************************************/
3494 enum terrain_class tc)
3495{
3496 struct role_unit_cb_data cb_data = { .build_city = pcity, .tc = tc };
3497
3499}
3500
3501/**********************************************************************/
3504bool dai_unit_can_strike_my_unit(const struct unit *attacker,
3505 const struct unit *defender)
3506{
3507 struct pf_parameter parameter;
3508 struct pf_map *pfm;
3509 const struct tile *ptarget = unit_tile(defender);
3510 int max_move_cost = attacker->moves_left;
3511 bool able_to_strike = FALSE;
3512 const struct civ_map *nmap = &(wld.map);
3513
3514 pft_fill_unit_parameter(&parameter, nmap, attacker);
3515 parameter.omniscience = !has_handicap(unit_owner(defender), H_MAP);
3516 pfm = pf_map_new(&parameter);
3517
3518 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
3519 if (move_cost > max_move_cost) {
3520 break;
3521 }
3522
3523 if (ptile == ptarget) {
3525 break;
3526 }
3528
3530
3531 return able_to_strike;
3532}
3533
3534/**********************************************************************/
3537void dai_switch_to_explore(struct ai_type *ait, struct unit *punit,
3538 struct tile *target, enum override_bool *allow)
3539{
3540 struct unit_ai *udata = def_ai_unit_data(punit, ait);
3541
3542 if (udata->task != AIUNIT_NONE && udata->task != AIUNIT_EXPLORE) {
3544
3545 return;
3546 }
3547}
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:7561
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:3103
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
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:1915
const char * city_name_get(const struct city *pcity)
Definition city.c:1155
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:808
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype, const enum req_problem_type prob_type)
Definition city.c:957
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:565
@ CITIZEN_ANGRY
Definition city.h:269
@ CITIZEN_UNHAPPY
Definition city.h:268
#define city_owner(_pcity_)
Definition city.h:564
#define city_list_iterate_end
Definition city.h:510
@ FEELING_NATIONALITY
Definition city.h:280
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:77
void dai_manage_airunit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiair.c:490
void dai_manage_diplomat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
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 aiferry_gobyboat(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile, bool with_bodyguard)
Definition daiferry.c:764
int aiferry_find_boat(struct ai_type *ait, struct unit *punit, int cap, struct pf_path **path)
Definition daiferry.c:495
void dai_manage_ferryboat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiferry.c:1119
bool is_boat_free(struct ai_type *ait, struct unit *boat, struct unit *punit, int cap)
Definition daiferry.c:438
bool aiferry_goto_amphibious(struct ai_type *ait, struct unit *ferry, struct unit *passenger, struct tile *ptile)
Definition daiferry.c:730
bool is_boss_of_boat(struct ai_type *ait, struct unit *punit)
Definition daiferry.c:471
int aiferry_avail_boats(struct ai_type *ait, struct player *pplayer)
Definition daiferry.c:353
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Definition daiferry.c:159
void aiferry_clear_boat(struct ai_type *ait, struct unit *punit)
Definition daiferry.c:251
struct city * aiguard_charge_city(struct ai_type *ait, struct unit *guard)
Definition daiguard.c:289
void aiguard_clear_charge(struct ai_type *ait, struct unit *guard)
Definition daiguard.c:117
void aiguard_request_guard(struct ai_type *ait, struct unit *punit)
Definition daiguard.c:227
bool aiguard_has_guard(struct ai_type *ait, struct unit *charge)
Definition daiguard.c:259
void aiguard_assign_guard_unit(struct ai_type *ait, struct unit *charge, struct unit *guard)
Definition daiguard.c:173
bool aiguard_wanted(struct ai_type *ait, struct unit *charge)
Definition daiguard.c:241
void aiguard_assign_guard_city(struct ai_type *ait, struct city *charge, struct unit *guard)
Definition daiguard.c:196
struct unit * aiguard_charge_unit(struct ai_type *ait, struct unit *guard)
Definition daiguard.c:279
void aiguard_clear_guard(struct ai_type *ait, struct unit *charge)
Definition daiguard.c:146
struct unit * aiguard_guard_of(struct ai_type *ait, struct unit *charge)
Definition daiguard.c:269
bool aiguard_has_charge(struct ai_type *ait, struct unit *guard)
Definition daiguard.c:250
void aiguard_update_charge(struct ai_type *ait, struct unit *guard)
Definition daiguard.c:299
#define CHECK_GUARD(ait, guard)
Definition daiguard.h:21
bool dai_hunter_qualify(struct player *pplayer, struct unit *punit)
Definition daihunter.c:288
int dai_hunter_manage(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daihunter.c:436
#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:89
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:44
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition daiplayer.h:50
int stack_cost(struct unit *pattacker, struct unit *pdefender)
Definition daitools.c:1308
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:1444
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:3422
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:221
adv_want kill_desire(adv_want benefit, int attack, int loss, int vuln, int victim_count)
Definition daiunit.c:341
static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2263
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:2028
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:303
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:3261
#define LOG_CARAVAN
Definition daiunit.c:87
static int unit_att_rating_now(const struct unit *punit)
Definition daiunit.c:258
enum gen_action dai_select_tile_attack_action(struct civ_map *nmap, struct unit *punit, struct tile *ptile)
Definition daiunit.c:444
static void dai_military_attack_barbarian(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1723
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:3237
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Definition daiunit.c:2955
void dai_switch_to_explore(struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
Definition daiunit.c:3537
void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2545
static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:1941
static void reinforcements_cost_and_value(struct unit *punit, struct tile *ptile0, int *value, int *cost)
Definition daiunit.c:374
struct unit_type * simple_ai_types[U_LAST]
Definition daiunit.c:125
static void dai_airlift(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:168
int build_cost_balanced(const struct unit_type *punittype)
Definition daiunit.c:249
bool dai_unit_can_strike_my_unit(const struct unit *attacker, const struct unit *defender)
Definition daiunit.c:3504
bool is_on_unit_upgrade_path(const struct unit_type *test, const struct unit_type *base)
Definition daiunit.c:2989
static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:660
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:89
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2919
static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2300
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:1172
static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2824
void dai_manage_unit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2700
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:3470
static void dai_military_attack(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1786
void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3392
static void dai_manage_settler(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2679
static int avg_benefit(int benefit, int loss, double chance)
Definition daiunit.c:365
#define LOG_CARAVAN2
Definition daiunit.c:88
static struct city * find_neediest_airlift_city(struct ai_type *ait, const struct player *pplayer)
Definition daiunit.c:137
static int unit_def_rating(const struct unit *attacker, const struct unit *defender)
Definition daiunit.c:277
static bool dai_is_unit_tired_waiting_boat(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2159
static int dai_rampage_want(struct unit *punit, struct tile *ptile)
Definition daiunit.c:481
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:1672
static void dai_manage_barbarian_leader(struct ai_type *ait, struct player *pplayer, struct unit *leader)
Definition daiunit.c:3006
static void dai_manage_hitpoint_recovery(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2470
static int unit_att_rating_squared(const struct unit *punit)
Definition daiunit.c:267
static struct pf_path * find_rampage_target(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:548
static bool is_my_turn(struct unit *punit, struct unit *pdef)
Definition daiunit.c:399
static int unit_def_rating_squared(const struct unit *punit, const struct unit *pdef)
Definition daiunit.c:290
void dai_unit_init(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3370
void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
Definition daiunit.c:3180
bool uclass_need_trans_between(struct unit_class *pclass, struct tile *ctile, struct tile *ptile)
Definition daiunit.c:1989
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:3441
static void caravan_optimize_callback(const struct caravan_result *result, void *data)
Definition daiunit.c:2140
#define LOGLEVEL_RECOVERY
Definition daiunit.c:86
void dai_unit_close(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3404
void dai_units_ruleset_close(struct ai_type *ait)
Definition daiunit.c:3352
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Definition daiunit.c:3493
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:627
static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer, struct unit *punit)
Definition daiunit.c:2218
#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:3055
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2977
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:1036
bool unit_can_enter_hut(const struct unit *punit, const struct tile *ptile)
Definition extras.c:720
float adv_want
Definition fc_types.h:1050
@ RPT_CERTAIN
Definition fc_types.h:516
@ AUT_AUTO_WORKER
Definition fc_types.h:231
#define ADV_WANT_PRINTF
Definition fc_types.h:1051
@ O_LUXURY
Definition fc_types.h:103
signed short Continent_id
Definition fc_types.h:233
override_bool
Definition fc_types.h:96
@ OVERRIDE_TRUE
Definition fc_types.h:96
@ OVERRIDE_FALSE
Definition fc_types.h:96
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
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:461
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:1217
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1376
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1397
#define players_iterate_end
Definition player.h:552
#define players_iterate(_pplayer)
Definition player.h:547
static bool is_barbarian(const struct player *pplayer)
Definition player.h:499
#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:270
#define normal_specialist_type_iterate(sp)
Definition specialist.h:89
#define normal_specialist_type_iterate_end
Definition specialist.h:95
#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:318
struct adv_city * adv
Definition city.h:450
struct tile * tile
Definition city.h:320
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:3463
struct city * build_city
Definition daiunit.c:3464
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:565
struct veteran_system * veteran
Definition unittype.h:551
int move_rate
Definition unittype.h:524
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
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:615
#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:234
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:2343
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2461
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2281
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:205
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2525
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2236
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2722
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:402
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:412
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2942
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:912
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:316
bool is_special_unit(const struct unit *punit)
Definition unit.c:369
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2511
bool is_guard_unit(const struct unit *punit)
Definition unit.c:359
bool unit_has_orders(const struct unit *punit)
Definition unit.c:221
#define unit_tile(_pu)
Definition unit.h:408
#define unit_cargo_iterate_end
Definition unit.h:602
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:599
#define CHECK_UNIT(punit)
Definition unit.h:273
#define unit_owner(_pu)
Definition unit.h:407
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.h:446
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unithand.c:6708
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:3350
bool unit_server_side_agent_set(struct player *pplayer, struct unit *punit, enum server_side_agent agent)
Definition unithand.c:6606
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:1216
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:126
struct unit_type * role_units_iterate_backwards(int role, role_unit_callback cb, void *data)
Definition unittype.c:2267
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2284
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1613
bool utype_has_role(const struct unit_type *punittype, int role)
Definition unittype.c:224
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1493
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1509
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2330
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2535
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:412
bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
Definition unittype.c:233
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:215
bool unit_can_take_over(const struct unit *punit)
Definition unittype.c:289
void * utype_ai_data(const struct unit_type *ptype, const struct ai_type *ai)
Definition unittype.c:2776
bool utype_can_take_over(const struct unit_type *punittype)
Definition unittype.c:301
void utype_set_ai_data(struct unit_type *ptype, const struct ai_type *ai, void *data)
Definition unittype.c:2784
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1463
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:468
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:396
#define utype_class(_t_)
Definition unittype.h:756
#define utype_fuel(ptype)
Definition unittype.h:849
#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:950
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624
#define unit_class_list_iterate(uclass_list, pclass)
Definition unittype.h:942
#define unit_type_iterate(_p)
Definition unittype.h:865
#define unit_type_list_iterate_end
Definition unittype.h:952
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:872
#define unit_class_list_iterate_end
Definition unittype.h:944