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/**********************************************************************/
456static int dai_rampage_want(struct unit *punit, struct tile *ptile)
457{
458 struct player *pplayer = unit_owner(punit);
459 struct unit *pdef;
460 struct civ_map *nmap = &(wld.map);
461
463
464 if (can_unit_attack_tile(punit, nullptr, ptile)
465 && (pdef = get_defender(nmap, punit, ptile, nullptr))
466 /* Action enablers might prevent attacking */
468 punit, ptile)) {
469 /* See description of kill_desire() about these variables. */
473
474 attack *= attack;
475
476 /* If the victim is in the city/fortress, we correct the benefit
477 * with our health because there could be reprisal attacks.
478 * We shouldn't send already injured units to useless suicide.
479 * Note that we do not specially encourage attacks against
480 * cities: rampage is a hit-n-run operation. */
481 if (!is_stack_vulnerable(ptile)
482 && unit_list_size(ptile->units) > 1) {
484 }
485
486 /* If we have non-zero attack rating... */
487 if (attack > 0 && is_my_turn(punit, pdef)) {
488 double chance = unit_win_chance(nmap, punit, pdef, nullptr);
490
491 /* No need to amortize, our operation takes one turn. */
492 UNIT_LOG(LOG_DEBUG, punit, "Rampage: Desire %d to kill %s(%d,%d)",
493 desire,
496
497 return MAX(0, desire);
498 }
499 } else if (0 == unit_list_size(ptile->units)) {
500 /* No defender. */
501 struct city *pcity = tile_city(ptile);
502
503 /* ...and free foreign city waiting for us. Who would resist! */
504 if (pcity != nullptr
505 && pplayers_at_war(pplayer, city_owner(pcity))
508 }
509
510 /* ...or tiny pleasant hut here! */
511 /* FIXME: unhardcode and variate the desire to enter a hut. */
512 if (unit_can_enter_hut(punit, ptile) && !is_barbarian(pplayer)
513 && is_native_tile(unit_type_get(punit), ptile)) {
514 return -RAMPAGE_HUT_OR_BETTER;
515 }
516 }
517
518 return 0;
519}
520
521/**********************************************************************/
524static struct pf_path *find_rampage_target(struct unit *punit,
525 int thresh_adj, int thresh_move)
526{
527 struct pf_map *tgt_map;
528 struct pf_path *path = nullptr;
529 struct pf_parameter parameter;
530 /* Coordinates of the best target (initialize to silence compiler) */
531 struct tile *ptile = unit_tile(punit);
532 /* Want of the best target */
533 int max_want = 0;
534 struct player *pplayer = unit_owner(punit);
535 const struct civ_map *nmap = &(wld.map);
536
538 parameter.omniscience = !has_handicap(pplayer, H_MAP);
539 /* When trying to find rampage targets we ignore risks such as
540 * enemy units because we are looking for trouble!
541 * Hence no call ai_avoid_risks()
542 */
543
544 tgt_map = pf_map_new(&parameter);
546 int want;
547 bool move_needed;
548 int thresh;
549
550 if (move_cost > punit->moves_left) {
551 /* This is too far */
552 break;
553 }
554
555 if (has_handicap(pplayer, H_TARGETS)
556 && !map_is_known_and_seen(iter_tile, pplayer, V_MAIN)) {
557 /* The target is under fog of war */
558 continue;
559 }
560
562
563 /* Negative want means move needed even though the tiles are adjacent */
565 || want < 0);
566 /* Select the relevant threshold */
568 want = (want < 0 ? -want : want);
569
570 if (want > max_want && want > thresh) {
571 /* The new want exceeds both the previous maximum
572 * and the relevant threshold, so it's worth recording */
573 max_want = want;
574 ptile = iter_tile;
575 }
577
578 if (max_want > 0) {
579 /* We found something */
580 path = pf_map_path(tgt_map, ptile);
581 fc_assert(path != nullptr);
582 }
583
585
586 return path;
587}
588
589/**********************************************************************/
604 int thresh_move)
605{
606 int count = punit->moves_left + 1; /* Break any infinite loops */
607 struct pf_path *path = nullptr;
608
611
613 /* This teaches the AI about the dangers inherent in occupychance. */
615
616 while (count-- > 0 && punit->moves_left > 0
618 if (!adv_unit_execute_path(punit, path)) {
619 /* Died */
620 count = -1;
621 }
622 pf_path_destroy(path);
623 path = nullptr;
624 }
625
626 fc_assert(path == nullptr);
627
629 return (count >= 0);
630}
631
632/**********************************************************************/
636static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer,
637 struct unit *punit)
638{
639 struct unit *aunit = aiguard_charge_unit(ait, punit);
640 struct city *acity = aiguard_charge_city(ait, punit);
641 struct tile *ptile;
642
644 CHECK_GUARD(ait, punit);
645
646 if (aunit && unit_owner(aunit) == unit_owner(punit)) {
647 /* protect a unit */
648 if (aunit->goto_tile != nullptr) {
649 /* Our charge is going somewhere: maybe we should meet them there */
650 /* FIXME: This probably isn't the best algorithm for this. */
652 int me2goal = real_map_distance(unit_tile(punit), aunit->goto_tile);
653 int them2goal = real_map_distance(unit_tile(aunit), aunit->goto_tile);
654
655 if (me2goal < me2them
659 ptile = aunit->goto_tile;
660 } else {
661 ptile = unit_tile(aunit);
662 }
663 } else {
664 ptile = unit_tile(aunit);
665 }
666 } else if (acity && city_owner(acity) == unit_owner(punit)) {
667 /* protect a city */
668 ptile = acity->tile;
669 } else {
670 /* should be impossible */
671 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "we lost our charge");
672 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
673 return;
674 }
675
676 if (same_pos(unit_tile(punit), ptile)) {
677 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "at RV");
678 } else {
679 if (goto_is_sane(punit, ptile)) {
680 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "meeting charge");
681 if (!dai_gothere(ait, pplayer, punit, ptile)) {
682 /* We died */
683 return;
684 }
685 } else {
686 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "can not meet charge");
687 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
688 }
689 }
690
691 /* We might have stopped because of an enemy nearby.
692 * Perhaps we can kill it.*/
695 && same_pos(unit_tile(punit), ptile)) {
696 def_ai_unit_data(punit, ait)->done = TRUE; /* Stay with charge */
697 }
698}
699
700/**********************************************************************/
703static bool unit_role_defender(const struct unit_type *punittype)
704{
706}
707
708/**********************************************************************/
718adv_want look_for_charge(struct ai_type *ait, const struct civ_map *nmap,
719 struct player *pplayer, struct unit *punit,
720 struct unit **aunit, struct city **acity)
721{
722 struct pf_parameter parameter;
723 struct pf_map *pfm;
724 struct city *pcity;
725 struct ai_city *data, *best_data = nullptr;
727 int def, best_def = -1;
728 /* Arbitrary: 3 turns. */
729 const int max_move_cost = 3 * unit_move_rate(punit);
730
731 *aunit = nullptr;
732 *acity = nullptr;
733
734 if (toughness == 0) {
735 /* Useless */
736 return 0;
737 }
738
739 pft_fill_unit_parameter(&parameter, nmap, punit);
740 parameter.omniscience = !has_handicap(pplayer, H_MAP);
741 pfm = pf_map_new(&parameter);
742
743 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
744 if (move_cost > max_move_cost) {
745 /* Consider too far. */
746 break;
747 }
748
749 pcity = tile_city(ptile);
750
751 /* Consider unit bodyguard. */
752 unit_list_iterate(ptile->units, buddy) {
753 const struct unit_type *ptype = unit_type_get(punit);
754 const struct unit_type *buddy_type = unit_type_get(buddy);
755
756 /* TODO: allied unit bodyguard? */
758 || unit_owner(buddy) != pplayer
759 || !aiguard_wanted(ait, buddy)
765
766 continue;
767 }
768
770 if (0 >= def) {
771 continue;
772 }
773
775 /* Reduce want based on move cost. We can't do this for
776 * transports since they move around all the time, leading
777 * to hilarious flip-flops. */
778 def >>= move_cost / (2 * unit_move_rate(punit));
779 }
780 if (def > best_def) {
781 *aunit = buddy;
782 *acity = nullptr;
783 best_def = def;
784 }
786
787 /* City bodyguard. TODO: allied city bodyguard? */
788 if (ai_fuzzy(pplayer, TRUE)
789 && pcity != nullptr
790 && city_owner(pcity) == pplayer
791 && (data = def_ai_city_data(pcity, ait))
792 && 0 < data->urgency) {
793 if (best_data != nullptr
794 && (0 < best_data->grave_danger
795 || best_data->urgency > data->urgency
796 || ((best_data->danger > data->danger
797 || AIUNIT_DEFEND_HOME == def_ai_unit_data(punit, ait)->task)
798 && 0 == data->grave_danger))) {
799 /* Chances are we'll be between cities when we are needed the most!
800 * Resuming pf_map_move_costs_iterate()... */
801 continue;
802 }
803 def = (data->danger - assess_defense_quadratic(ait, pcity));
804 if (def <= 0) {
805 continue;
806 }
807 /* Reduce want based on move cost. */
808 def >>= move_cost / (2 * unit_move_rate(punit));
809 if (def > best_def && ai_fuzzy(pplayer, TRUE)) {
810 *acity = pcity;
811 *aunit = nullptr;
812 best_def = def;
813 best_data = data;
814 }
815 }
817
819
820 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "%s(), best_def=%d, type=%s (%d, %d)",
822 (*acity != nullptr ? city_name_get(*acity)
823 : (*aunit != nullptr ? unit_rule_name(*aunit) : "")),
825 : (*aunit != nullptr ?
828 : (*aunit != nullptr ?
830
831 return ((best_def * 100) / toughness);
832}
833
834/**********************************************************************/
838 const struct unit_type *followee,
839 struct ai_type *ait)
840{
841 struct unit_type_ai *utai = utype_ai_data(follower, ait);
842
843 unit_type_list_iterate(utai->potential_charges, pcharge) {
844 if (pcharge == followee) {
845 return TRUE;
846 }
848
849 return FALSE;
850}
851
852/**********************************************************************/
855static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap,
856 struct player *pplayer, struct unit *punit)
857{
858 const struct unit_type *punittype = unit_type_get(punit);
859 struct unit_ai *unit_data;
860
862
863 /* Keep barbarians aggressive and primitive */
864 if (is_barbarian(pplayer)) {
865 if (is_land_barbarian(pplayer)) {
867
869 /* Land barbarians pillage */
871 }
872 }
873 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
874
875 return;
876 }
877
879
880 /* If I am a bodyguard, check whether I can do my job. */
881 if (unit_data->task == AIUNIT_ESCORT
882 || unit_data->task == AIUNIT_DEFEND_HOME) {
884 }
885
886 if (aiguard_has_charge(ait, punit)
887 && unit_data->task == AIUNIT_ESCORT) {
888 struct unit *aunit = aiguard_charge_unit(ait, punit);
889 struct city *acity = aiguard_charge_city(ait, punit);
890 struct ai_city *city_data = nullptr;
891
892 if (acity != nullptr) {
894 }
895
896 /* Check if the city we are on our way to rescue is still in danger,
897 * or the unit we should protect is still alive... */
898 if ((aunit && (aiguard_has_guard(ait, aunit) || aiguard_wanted(ait, aunit))
901 && city_data->urgency != 0
902 && city_data->danger > assess_defense_quadratic(ait, acity))) {
903 return; /* Yep! */
904 } else {
905 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr); /* Nope! */
906 }
907 }
908
909 /* Is the unit badly damaged? */
910 if ((unit_data->task == AIUNIT_RECOVER
911 && punit->hp < punittype->hp)
912 || punit->hp < punittype->hp * 0.25) { /* WAG */
913 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "set to hp recovery");
914 dai_unit_new_task(ait, punit, AIUNIT_RECOVER, nullptr);
915 return;
916 }
917
920 /* This is a defending unit that doesn't need to stay put.
921 * It needs to defend something, but not necessarily where it's at.
922 * Therefore, it will consider becoming a bodyguard. -- Syela */
923 struct city *acity;
924 struct unit *aunit;
925
926 look_for_charge(ait, nmap, pplayer, punit, &aunit, &acity);
927 if (acity) {
930 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend city");
931 } else if (aunit) {
934 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend unit");
935 }
936 }
938}
939
940/**********************************************************************/
949static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
950 struct unit *punit)
951{
952 struct city *pcity = aiguard_charge_city(ait, punit);
953
955
956 if (!pcity || city_owner(pcity) != pplayer) {
958 /* Do not stay defending an allied city forever */
960 }
961
962 if (!pcity) {
963 /* Try to find a place to rest. Sitting duck out in the wilderness
964 * is generally a bad idea, since we protect no cities that way, and
965 * it looks silly. */
966 pcity = find_closest_city(unit_tile(punit), nullptr, pplayer,
969 }
970
971 if (!pcity) {
973 }
974
977 /* ... We survived */
978 if (pcity) {
979 UNIT_LOG(LOG_DEBUG, punit, "go to defend %s", city_name_get(pcity));
981 UNIT_LOG(LOG_DEBUG, punit, "go defend successful");
983 } else {
984 (void) dai_gothere(ait, pplayer, punit, pcity->tile);
985 }
986 } else {
987 UNIT_LOG(LOG_VERBOSE, punit, "defending nothing...?");
988 }
989 }
990}
991
992/**********************************************************************/
995static void single_invader(struct ai_city *city_data,
996 const struct unit_type *utype,
997 int which)
998{
999 int attacks;
1000
1001 if (utype_action_takes_all_mp(utype,
1003 attacks = 1;
1004 } else {
1005 attacks = utype->move_rate;
1006 }
1007 city_data->invasion.attack += attacks;
1008 if (which == INVASION_OCCUPY) {
1009 city_data->invasion.occupy++;
1010 }
1011}
1012
1013/**********************************************************************/
1023static void invasion_funct(struct ai_type *ait, struct unit *punit,
1024 bool dest, int radius, int which)
1025{
1026 struct tile *ptile;
1027 struct player *pplayer = unit_owner(punit);
1028
1030
1031 if (dest) {
1032 ptile = punit->goto_tile;
1033 } else {
1034 ptile = unit_tile(punit);
1035 }
1036
1037 square_iterate(&(wld.map), ptile, radius, tile1) {
1038 struct city *pcity = tile_city(tile1);
1039
1040 if (pcity
1042 && (dest || !has_defense(pcity))) {
1043 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1044
1045 /* Unit itself */
1047
1048 /* Cargo */
1049 unit_cargo_iterate(punit, cargo) {
1050 const struct unit_type *utype = unit_type_get(cargo);
1051
1052 if (IS_ATTACKER(utype)) {
1056 }
1058 }
1060}
1061
1062/**********************************************************************/
1065bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map,
1066 struct tile *dest_tile,
1067 const struct unit_type *cargo_type,
1068 const struct unit_type *ferry_type,
1069 struct tile **ferry_dest, struct tile **beachhead_tile)
1070{
1071 if (tile_city(dest_tile) == nullptr
1073 /* Unit can directly go to 'dest_tile'. */
1074 struct tile *best_tile = nullptr;
1076
1077 if (beachhead_tile != nullptr) {
1078 *beachhead_tile = dest_tile;
1079 }
1080
1081 adjc_iterate(&(wld.map), dest_tile, ptile) {
1083 if (cost != PF_IMPOSSIBLE_MC
1084 && (best_tile == nullptr || cost < best_cost)) {
1085 best_tile = ptile;
1086 best_cost = cost;
1087 }
1089
1090 if (ferry_dest != nullptr) {
1092 }
1093
1094 return (PF_IMPOSSIBLE_MC != best_cost);
1095 } else {
1096 /* We need to find a beach around 'dest_tile'. */
1097 struct tile *best_tile = nullptr, *best_beach = nullptr;
1101
1102 tile_list_append(checked_tiles, dest_tile);
1103 adjc_iterate(&(wld.map), dest_tile, beach) {
1105 /* Can land there. */
1106 adjc_iterate(&(wld.map), beach, ptile) {
1107 if (!tile_list_search(checked_tiles, ptile)
1108 && !is_non_allied_unit_tile(ptile, pplayer,
1109 flagless_ferry)) {
1112 if (cost != PF_IMPOSSIBLE_MC
1113 && (best_tile == nullptr || cost < best_cost)) {
1114 best_beach = beach;
1115 best_tile = ptile;
1116 best_cost = cost;
1117 }
1118 }
1120 }
1122
1124
1125 if (beachhead_tile != nullptr) {
1127 }
1128 if (ferry_dest != nullptr) {
1130 }
1131
1132 return (PF_IMPOSSIBLE_MC != best_cost);
1133 }
1134}
1135
1136/**********************************************************************/
1144adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
1145 struct unit *punit,
1146 struct tile **pdest_tile, struct pf_path **ppath,
1147 struct pf_map **pferrymap,
1148 struct unit **pferryboat,
1149 const struct unit_type **pboattype, int *pmove_time)
1150{
1151 const int attack_value = adv_unit_att_rating(punit); /* Basic attack. */
1152 struct pf_parameter parameter;
1153 struct pf_map *punit_map, *ferry_map;
1154 struct pf_position pos;
1156 const struct unit_type *punit_type = unit_type_get(punit);
1157 struct tile *punit_tile = unit_tile(punit);
1158 /* Type of our boat (a future one if ferryboat == nullptr). */
1159 const struct unit_type *boattype = nullptr;
1160 struct unit *ferryboat = nullptr;
1161 struct city *pcity;
1162 struct ai_city *acity_data;
1163 int bcost, bcost_bal; /* Build cost of the attacker (+adjustments). */
1164 bool handicap = has_handicap(pplayer, H_TARGETS);
1165 bool unhap = FALSE; /* Do we make unhappy citizen. */
1166 bool harbor = FALSE; /* Do we have access to sea? */
1167 bool go_by_boat; /* Whether we need a boat or not. */
1168 int vulnerability; /* Enemy defense rating. */
1169 adv_want benefit; /* Benefit from killing the target. */
1170 struct unit *pdefender; /* Enemy city defender. */
1171 int move_time; /* Turns needed to target. */
1172 int reserves;
1173 int attack; /* Our total attack value with reinforcements. */
1174 int victim_count; /* Number of enemies there. */
1175 int needferry; /* Cost of building a ferry boat. */
1176 /* This is a kluge, because if we don't set x and y with !punit->id,
1177 * p_a_w isn't called, and we end up not wanting ironclads and therefore
1178 * never learning steam engine, even though ironclads would be very
1179 * useful. -- Syela */
1180 adv_want bk = 0;
1181 adv_want want; /* Want (amortized) of the operation. */
1182 adv_want best = 0; /* Best of all wants. */
1183 struct tile *goto_dest_tile = nullptr;
1184 bool can_occupy;
1185 struct civ_map *nmap = &(wld.map);
1186
1187 /* Very preliminary checks. */
1189 if (pferrymap != nullptr) {
1190 *pferrymap = nullptr;
1191 }
1192 if (pferryboat != nullptr) {
1193 *pferryboat = nullptr;
1194 }
1195 if (pboattype != nullptr) {
1196 *pboattype = nullptr;
1197 }
1198 if (pmove_time != nullptr) {
1199 *pmove_time = 0;
1200 }
1201 if (ppath != nullptr) {
1202 *ppath = nullptr;
1203 }
1204
1205 if (attack_value == 0) {
1206 /* A very poor attacker... probably low on HP. */
1207 return 0;
1208 }
1209
1211
1212
1213 /*** Part 1: Calculate targets ***/
1214
1215 /* This horrible piece of code attempts to calculate the attractiveness of
1216 * enemy cities as targets for our units, by checking how many units are
1217 * going towards it or are near it already. */
1218
1219 /* Reset enemy cities data. */
1221 /* See comment below in next usage of POTENTIALLY_HOSTILE_PLAYER. */
1222 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1223 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1224 continue;
1225 }
1226 city_list_iterate(aplayer->cities, acity) {
1227 struct ai_city *city_data = def_ai_city_data(acity, ait);
1228
1230 &city_data->attack,
1231 &city_data->bcost);
1232 city_data->invasion.attack = 0;
1233 city_data->invasion.occupy = 0;
1236
1237 /* Second, calculate in units on their way there, and mark targets for
1238 * invasion */
1239 unit_list_iterate(pplayer->units, aunit) {
1240 const struct unit_type *atype;
1241
1242 if (aunit == punit) {
1243 continue;
1244 }
1245
1247
1248 /* Dealing with invasion stuff */
1249 if (IS_ATTACKER(atype)) {
1250 if (aunit->activity == ACTIVITY_GOTO) {
1251 invasion_funct(ait, aunit, TRUE, 0,
1254 if ((pcity = tile_city(aunit->goto_tile))) {
1255 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1256
1259 }
1260 }
1264 } else if (def_ai_unit_data(aunit, ait)->passenger != 0
1266 /* It's a transport with reinforcements */
1267 if (aunit->activity == ACTIVITY_GOTO) {
1269 }
1271 }
1273 /* End horrible initialization subroutine */
1274
1275
1276 /*** Part 2: Now pick one target ***/
1277
1278 /* We first iterate through all cities, then all units, looking
1279 * for a viable target. We also try to gang up on the target
1280 * to avoid spreading out attacks too widely to be inefficient.
1281 */
1282
1284 if (pcity != nullptr && (punit->id == 0 || pcity->id == punit->homecity)) {
1285 /* I would have thought unhappiness should be taken into account
1286 * irrespectfully the city in which it will surface... -- GB */
1288 }
1289
1292
1294 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1295 punit_map = pf_map_new(&parameter);
1296
1297 if (MOVE_NONE == punit_class->adv.sea_move) {
1298 /* We need boat to move over sea. */
1299 ferryboat = unit_transport_get(punit);
1300
1301 /* First check if we can use the boat we are currently loaded to. */
1302 if (ferryboat == nullptr || !is_boat_free(ait, ferryboat, punit, 0)) {
1303 /* No, we cannot control current boat */
1304 ferryboat = nullptr;
1305 }
1306
1307 if (ferryboat == nullptr) {
1308 /* Try to find new boat */
1309 ferryboat = player_unit_by_number(pplayer,
1310 aiferry_find_boat(ait, punit, 1,
1311 nullptr));
1312 }
1313
1315 TC_OCEAN)) {
1316 harbor = TRUE;
1317 }
1318 }
1319
1320 if (ferryboat != nullptr) {
1321 boattype = unit_type_get(ferryboat);
1322 pft_fill_unit_overlap_param(&parameter, nmap, ferryboat);
1323 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1324 ferry_map = pf_map_new(&parameter);
1325 } else {
1327 if (boattype == nullptr) {
1328 /* We pretend that we can have the simplest boat to stimulate tech. */
1330 }
1331 if (boattype != nullptr && harbor) {
1332 /* Let's simulate a boat at 'punit' position. */
1334 punit_tile, pplayer);
1335 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1336 ferry_map = pf_map_new(&parameter);
1337 } else {
1338 ferry_map = nullptr;
1339 }
1340 }
1341
1343
1345 /* For the virtual unit case, which is when we are called to evaluate
1346 * which units to build, we want to calculate in danger and which
1347 * players we want to make war with in the future. We do _not_ want
1348 * to do this when actually making attacks. */
1349 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1350 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1351 continue; /* Not an enemy. */
1352 }
1353
1354 city_list_iterate(aplayer->cities, acity) {
1355 struct tile *atile = city_tile(acity);
1356
1359 /* Can't attack this city. It is on land. */
1360 continue;
1361 }
1362
1363 if (handicap && !map_is_known(atile, pplayer)) {
1364 /* Can't see it */
1365 continue;
1366 }
1367
1369 go_by_boat = FALSE;
1370 move_time = pos.turn;
1371 } else if (ferry_map == nullptr) {
1372 continue; /* Impossible to handle. */
1373 } else {
1374 struct tile *dest, *beach;
1375
1376 if (!find_beachhead(pplayer, ferry_map, atile, punit_type,
1377 boattype, &dest, &beach)) {
1378 continue; /* Impossible to go by boat. */
1379 }
1380 if (!pf_map_position(ferry_map, dest, &pos)) {
1382 continue;
1383 }
1384 move_time = pos.turn; /* Sailing time. */
1385 if (dest != beach) {
1386 move_time++; /* Land time. */
1387 }
1388 if (ferryboat != nullptr && unit_tile(ferryboat) != punit_tile) {
1389 if (pf_map_position(punit_map, unit_tile(ferryboat), &pos)) {
1390 move_time += pos.turn; /* Time to reach the boat. */
1391 } else {
1392 continue; /* Cannot reach the boat. */
1393 }
1394 }
1395 go_by_boat = TRUE;
1396 }
1397
1400 nullptr))) {
1403 } else {
1404 pdefender = nullptr;
1405 vulnerability = 0;
1406 benefit = 0;
1407 }
1408
1409 if (1 < move_time) {
1411
1412 if (def_type) {
1416
1417 if (v > vulnerability) {
1418 /* They can build a better defender! */
1419 vulnerability = v;
1421 }
1422 }
1423 }
1424
1426
1427 reserves = (acity_data->invasion.attack
1428 - unit_list_size(acity->tile->units));
1429
1430 if (punit->id == 0) {
1431 /* Real unit would add to reserves once built. */
1434 reserves++;
1435 } else {
1436 reserves += punit_type->move_rate;
1437 }
1438 }
1439
1440 if (0 < reserves && (can_occupy
1441 || 0 < acity_data->invasion.occupy)) {
1442 /* There are units able to occupy the city after all defenders
1443 * are killed! */
1444 benefit += acity_data->worth * reserves / 5;
1445 }
1446
1447 attack = attack_value + acity_data->attack;
1448 attack *= attack;
1449 /* Avoiding handling upkeep aggregation this way -- Syela */
1450
1451 /* AI was not sending enough reinforcements to totally wipe out a city
1452 * and conquer it in one turn.
1453 * This variable enables total carnage. -- Syela */
1455
1456 if (!can_occupy && pdefender == nullptr) {
1457 /* Nothing there to bash and we can't occupy!
1458 * Not having this check caused warships yoyoing */
1459 want = 0;
1460 } else if (10 < move_time) {
1461 /* Too far! */
1462 want = 0;
1463 } else if (can_occupy && 0 == acity_data->invasion.occupy
1464 && (0 < acity_data->invasion.attack
1465 || victim_count == 0)) {
1466 /* Units able to occupy really needed there! */
1467 want = bcost * SHIELD_WEIGHTING;
1468 } else {
1469 want = kill_desire(benefit, attack, bcost + acity_data->bcost,
1471 }
1474 /* Build_cost of ferry. */
1475 needferry = (go_by_boat && ferryboat == nullptr
1476 ? utype_build_shield_cost(acity, nullptr, boattype) : 0);
1477 /* FIXME: Add time to build the ferry? */
1479 want, MAX(1, move_time),
1481
1482 /* BEGIN STEAM-ENGINES-ARE-OUR-FRIENDS KLUGE. */
1483 if (0 >= want && 0 == punit->id && 0 >= best) {
1487 MAX(1, move_time),
1489
1490 if (bk_e > bk) {
1491 *pdest_tile = atile;
1492 if (pferrymap != nullptr) {
1493 *pferrymap = go_by_boat ? ferry_map : nullptr;
1494 }
1495 if (pferryboat != nullptr) {
1496 *pferryboat = go_by_boat ? ferryboat : nullptr;
1497 }
1498 if (pboattype != nullptr) {
1499 *pboattype = go_by_boat ? boattype : nullptr;
1500 }
1501 if (pmove_time != nullptr) {
1503 }
1504 goto_dest_tile = (go_by_boat && ferryboat != nullptr
1505 ? unit_tile(ferryboat) : atile);
1506 bk = bk_e;
1507 }
1508 }
1509 /* END STEAM-ENGINES KLUGE */
1510
1511 if (punit->id != 0
1512 && ferryboat != nullptr
1513 && punit_class->adv.sea_move == MOVE_NONE) {
1515 "%s(): with boat %s@(%d, %d) -> %s@(%d, %d)"
1516 " (go_by_boat = %d, move_time = %d, want = " ADV_WANT_PRINTF
1517 ", best = " ADV_WANT_PRINTF ")",
1518 __FUNCTION__, unit_rule_name(ferryboat),
1519 TILE_XY(unit_tile(ferryboat)), city_name_get(acity),
1520 TILE_XY(atile), go_by_boat, move_time, want, best);
1521 }
1522
1523 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1524 /* Yes, we like this target */
1525 best = want;
1526 *pdest_tile = atile;
1527 if (pferrymap != nullptr) {
1528 *pferrymap = go_by_boat ? ferry_map : nullptr;
1529 }
1530 if (pferryboat != nullptr) {
1531 *pferryboat = go_by_boat ? ferryboat : nullptr;
1532 }
1533 if (pboattype != nullptr) {
1534 *pboattype = go_by_boat ? boattype : nullptr;
1535 }
1536 if (pmove_time != nullptr) {
1538 }
1539 goto_dest_tile = (go_by_boat && ferryboat != nullptr
1540 ? unit_tile(ferryboat) : atile);
1541 }
1543
1545 /* I'm not sure the following code is good but it seems to be adequate.
1546 * I am deliberately not adding ferryboat code to the unit_list_iterate().
1547 * -- Syela */
1549 struct tile *atile = unit_tile(aunit);
1550
1551 if (tile_city(atile) != nullptr) {
1552 /* Already dealt with it. */
1553 continue;
1554 }
1555
1556 if (handicap && !map_is_known(atile, pplayer)) {
1557 /* Can't see the target. */
1558 continue;
1559 }
1560
1562 && 0 == punit->id) {
1563 /* We will not build units just to chase caravans and
1564 * ambassadors. */
1565 continue;
1566 }
1567
1568 /* We have to assume the attack is diplomatically ok.
1569 * We cannot use can_player_attack_tile(), because we might not
1570 * be at war with aplayer yet */
1571 if (!can_unit_attack_tile(punit, nullptr, atile)
1572 || aunit != get_defender(nmap, punit, atile, nullptr)) {
1573 /* We cannot attack it, or it is not the main defender. */
1574 continue;
1575 }
1576
1578 /* Cannot reach it. */
1579 continue;
1580 }
1581
1584
1585 move_time = pos.turn;
1586 if (10 < move_time) {
1587 /* Too far. */
1588 want = 0;
1589 } else {
1590 want = kill_desire(benefit, attack, bcost, vulnerability, 1);
1591 /* Take into account maintenance of the unit. */
1592 /* FIXME: Depends on the government. */
1593 want -= move_time * SHIELD_WEIGHTING;
1594 /* Take into account unhappiness
1595 * (costs 2 luxuries to compensate). */
1596 want -= (unhap ? 2 * move_time * TRADE_WEIGHTING : 0);
1597 }
1599 want, MAX(1, move_time), bcost_bal);
1600 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1601 best = want;
1602 *pdest_tile = atile;
1603 if (pferrymap != nullptr) {
1604 *pferrymap = nullptr;
1605 }
1606 if (pferryboat != nullptr) {
1607 *pferryboat = nullptr;
1608 }
1609 if (pboattype != nullptr) {
1610 *pboattype = nullptr;
1611 }
1612 if (pmove_time != nullptr) {
1614 }
1616 }
1619
1620 if (ppath != nullptr) {
1621 *ppath = (goto_dest_tile != nullptr && goto_dest_tile != punit_tile
1622 ? pf_map_path(punit_map, goto_dest_tile) : nullptr);
1623 }
1624
1626 if (ferry_map != nullptr
1627 && (pferrymap == nullptr || *pferrymap != ferry_map)) {
1629 }
1630
1632
1633 return best;
1634}
1635
1636/**********************************************************************/
1645{
1646 struct pf_parameter parameter;
1647 struct pf_map *pfm;
1648 struct player *pplayer = unit_owner(punit);
1649 struct city *pcity, *best_city = nullptr;
1650 int best = FC_INFINITY, cur;
1651 const struct civ_map *nmap = &(wld.map);
1652
1653 pft_fill_unit_parameter(&parameter, nmap, punit);
1654 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1655 pfm = pf_map_new(&parameter);
1656
1657 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
1658 if (move_cost > best) {
1659 /* We already found a better city. No need to continue. */
1660 break;
1661 }
1662
1663 pcity = tile_city(ptile);
1664 if (pcity == nullptr || !pplayers_allied(pplayer, city_owner(pcity))) {
1665 continue;
1666 }
1667
1668 /* Score based on the move cost. */
1669 cur = move_cost;
1670
1671 /* Note the unit owner may be different from the city owner. */
1672 if (0 == get_unittype_bonus(unit_owner(punit), ptile,
1673 unit_type_get(punit), nullptr,
1674 EFT_HP_REGEN)) {
1675 /* If we cannot regen fast our hit points here, let's make some
1676 * penalty. */
1677 cur *= 3;
1678 }
1679
1680 if (cur < best) {
1681 best_city = pcity;
1682 best = cur;
1683 }
1685
1687 return best_city;
1688}
1689
1690/**********************************************************************/
1696 struct player *pplayer,
1697 struct unit *punit)
1698{
1699 struct city *pc;
1700 bool only_continent = TRUE;
1701
1702 if (unit_transported(punit)) {
1703 /* If we are in transport, we can go to any continent.
1704 * Actually, we are not currently in a continent where to stay. */
1706 }
1707
1708 if ((pc = find_closest_city(unit_tile(punit), nullptr, pplayer, FALSE,
1709 only_continent, FALSE, FALSE, TRUE, nullptr))) {
1711 UNIT_LOG(LOG_DEBUG, punit, "Barbarian heading to conquer %s",
1712 city_name_get(pc));
1713 (void) dai_gothere(ait, pplayer, punit, pc->tile);
1714 } else {
1715 struct unit *ferry = nullptr;
1716
1717 if (unit_transported(punit)) {
1718 ferry = unit_transport_get(punit);
1719
1720 /* We already are in a boat so it needs no
1721 * free capacity */
1722 if (!is_boat_free(ait, ferry, punit, 0)) {
1723 /* We cannot control our ferry. */
1724 ferry = nullptr;
1725 }
1726 } else {
1727 /* We are not in a boat yet. Search for one. */
1729 if (is_boat_free(ait, aunit, punit, 1)
1731 ferry = aunit;
1732 break;
1733 }
1735 }
1736
1737 if (ferry) {
1738 UNIT_LOG(LOG_DEBUG, punit, "Barbarian sailing to conquer %s",
1739 city_name_get(pc));
1740 (void) aiferry_goto_amphibious(ait, ferry, punit, pc->tile);
1741 } else {
1742 /* This is not an error. Somebody else might be in charge
1743 * of the ferry. */
1744 UNIT_LOG(LOG_DEBUG, punit, "unable to find barbarian ferry");
1745 }
1746 }
1747 } else {
1748 UNIT_LOG(LOG_DEBUG, punit, "Barbarian find no target city");
1749 }
1750}
1751
1752/**********************************************************************/
1758static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
1759 struct unit *punit)
1760{
1761 struct tile *dest_tile;
1762 int id = punit->id;
1763 int ct = 10;
1764 struct city *pcity = nullptr;
1765
1767
1768 /* Barbarians pillage, and might keep on doing that so they sometimes
1769 * even finish it. */
1770 if (punit->activity == ACTIVITY_PILLAGE && is_barbarian(pplayer)
1771 && fc_rand(2) == 1) {
1772 return;
1773 }
1774
1775 /* First find easy nearby enemies, anything better than pillage goes.
1776 * NB: We do not need to repeat dai_military_rampage(), it is repeats itself
1777 * until it runs out of targets. */
1778 /* FIXME: 1. dai_military_rampage() never checks if it should defend newly
1779 * conquered cities.
1780 * FIXME: 2. would be more convenient if it returned FALSE if we run out
1781 * of moves too.*/
1783 return; /* We died */
1784 }
1785
1786 if (punit->moves_left <= 0) {
1787 return;
1788 }
1789
1790 /* Main attack loop */
1791 do {
1792 struct tile *start_tile = unit_tile(punit);
1793 struct pf_path *path;
1794 struct unit *ferryboat;
1795
1796 /* Then find enemies the hard way */
1797 find_something_to_kill(ait, pplayer, punit, &dest_tile, &path,
1798 nullptr, &ferryboat, nullptr, nullptr);
1799 if (!same_pos(unit_tile(punit), dest_tile)) {
1800 if (!is_tiles_adjacent(unit_tile(punit), dest_tile)
1801 || !can_unit_attack_tile(punit, nullptr, dest_tile)) {
1802
1803 /* Adjacent and can't attack usually means we are not marines
1804 * and on a ferry. This fixes the problem (usually). */
1805 UNIT_LOG(LOG_DEBUG, punit, "mil att gothere -> (%d, %d)",
1806 TILE_XY(dest_tile));
1807
1808 /* Set ACTIVITY_GOTO more permanently than just inside
1809 * adv_follow_path(). This way other units will know we're
1810 * on our way even if we don't reach target yet. */
1811 punit->goto_tile = dest_tile;
1813 if (path != nullptr && !adv_follow_path(punit, path, dest_tile)) {
1814 /* Died. */
1815 pf_path_destroy(path);
1816 return;
1817 }
1818 if (ferryboat != nullptr) {
1819 /* Need a boat. */
1820 aiferry_gobyboat(ait, pplayer, punit, dest_tile, FALSE);
1821 pf_path_destroy(path);
1822 return;
1823 }
1824 if (punit->moves_left <= 0) {
1825 /* No moves left. */
1826 pf_path_destroy(path);
1827 return;
1828 }
1829
1830 /* Either we're adjacent or we sitting on the tile. We might be
1831 * sitting on the tile if the enemy that _was_ sitting there
1832 * attacked us and died _and_ we had enough movement to get there */
1833 if (same_pos(unit_tile(punit), dest_tile)) {
1834 UNIT_LOG(LOG_DEBUG, punit, "mil att made it -> (%d, %d)",
1835 TILE_XY(dest_tile));
1836 pf_path_destroy(path);
1837 break;
1838 }
1839 }
1840
1841 if (is_tiles_adjacent(unit_tile(punit), dest_tile)) {
1842 /* Close combat. fstk sometimes want us to attack an adjacent
1843 * enemy that rampage wouldn't */
1844 UNIT_LOG(LOG_DEBUG, punit, "mil att bash -> (%d, %d)",
1845 TILE_XY(dest_tile));
1846 if (!dai_unit_attack(ait, punit, dest_tile)) {
1847 /* Died */
1848 pf_path_destroy(path);
1849 return;
1850 }
1851 } else if (!same_pos(start_tile, unit_tile(punit))) {
1852 /* Got stuck. Possibly because of adjacency to an
1853 * enemy unit. Perhaps we are in luck and are now next to a
1854 * tempting target? Let's find out... */
1857 pf_path_destroy(path);
1858 return;
1859 }
1860
1861 } else {
1862 /* FIXME: This happens a bit too often! */
1863 UNIT_LOG(LOG_DEBUG, punit, "fstk didn't find us a worthy target!");
1864 /* No worthy enemies found, so abort loop */
1865 ct = 0;
1866 }
1867 pf_path_destroy(path);
1868
1869 ct--; /* Infinite loops from railroads must be stopped */
1870 } while (punit->moves_left > 0 && ct > 0);
1871
1872 /* Cleanup phase */
1873 if (punit->moves_left == 0) {
1874 return;
1875 }
1877 if (pcity != nullptr
1878 && (dai_is_ferry(punit, ait)
1879 || punit->hp < unit_type_get(punit)->hp * 0.50)) { /* WAG */
1880 /* Go somewhere safe */
1881 UNIT_LOG(LOG_DEBUG, punit, "heading to nearest safe house.");
1882 (void) dai_unit_goto(ait, punit, pcity->tile);
1883 } else if (!is_barbarian(pplayer)) {
1884 /* Nothing else to do, so try exploring. */
1885 switch (manage_auto_explorer(punit)) {
1886 case MR_DEATH:
1887 /* Don't use punit! */
1888 return;
1889 case MR_OK:
1890 UNIT_LOG(LOG_DEBUG, punit, "nothing else to do, so exploring");
1891 break;
1892 default:
1893 UNIT_LOG(LOG_DEBUG, punit, "nothing to do - no more exploring either");
1894 break;
1895 };
1896 } else {
1897 /* You can still have some moves left here, but barbarians should
1898 not sit helplessly, but advance towards nearest known enemy city */
1899 UNIT_LOG(LOG_DEBUG, punit, "attack: barbarian");
1900 dai_military_attack_barbarian(ait, pplayer, punit);
1901 }
1902
1903 if ((punit = game_unit_by_number(id)) && punit->moves_left > 0) {
1904 UNIT_LOG(LOG_DEBUG, punit, "attack: giving up unit to defense");
1905 dai_military_defend(ait, pplayer, punit);
1906 }
1907}
1908
1909/**********************************************************************/
1913static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
1914{
1915 bool alive = TRUE;
1916 int ferryboat = 0;
1917 struct pf_path *path_to_ferry = nullptr;
1918
1919 UNIT_LOG(LOG_CARAVAN, punit, "requesting a boat!");
1920 ferryboat = aiferry_find_boat(ait, punit, 1, &path_to_ferry);
1921 /* Going to meet the boat */
1922 if ((ferryboat <= 0)) {
1924 "in find_boat_for_unit cannot find any boats.");
1925 /* If we are undefended on the country side go to a city */
1927
1928 if (current_city == nullptr) {
1930
1931 if (city_near != nullptr) {
1933 }
1934 }
1935 } else {
1936 if (path_to_ferry != nullptr) {
1938 /* Died. */
1940 path_to_ferry = nullptr;
1941 alive = FALSE;
1942 } else {
1944 path_to_ferry = nullptr;
1945 alive = TRUE;
1946 }
1947 }
1948 }
1949
1950 return alive;
1951}
1952
1953/**********************************************************************/
1962 struct tile *ctile, struct tile *ptile)
1963{
1964 /* We usually have Inaccessible terrain, so not testing MOVE_FULL == */
1965 bool lm = MOVE_NONE != pclass->adv.land_move,
1966 sm = MOVE_NONE != pclass->adv.sea_move;
1967 struct civ_map *pmap = &(wld.map);
1968
1969 if (lm && sm) {
1970 return FALSE;
1971 }
1972
1973 /* We could use adjc_iterate() but likely often tiles are on the same
1974 * continent and it will be more time to find where they connect */
1977
1978 if (is_ocean_tile(atile) ? sm : lm) {
1979 iterate_outward(pmap, ptile, 1, btile) {
1980 if (tile_continent(btile) == acont) {
1981 return FALSE;
1982 }
1984 }
1986
1987 if (is_tiles_adjacent(ctile, ptile)) {
1988 return FALSE;
1989 }
1990
1991 return TRUE;
1992}
1993
1994/**********************************************************************/
2000static void dai_caravan_goto(struct ai_type *ait, struct player *pplayer,
2001 struct unit *punit,
2002 const struct city *dest_city,
2003 bool help_wonder,
2004 bool required_boat, bool request_boat)
2005{
2006 bool alive = TRUE;
2007 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2008 const struct civ_map *nmap = &(wld.map);
2009
2010 fc_assert_ret(dest_city != nullptr);
2011
2012 /* if we're not there yet, and we can move, move... */
2013 if (!same_pos(dest_city->tile, unit_tile(punit))
2014 && punit->moves_left != 0) {
2015 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) task %s going to %s in %s %s",
2019 help_wonder ? "help a wonder" : "trade", city_name_get(dest_city),
2020 required_boat ? "with a boat" : "");
2021 if (required_boat) {
2022 /* To trade with boat */
2023 if (request_boat) {
2024 /* Try to find new boat */
2026 } else {
2027 /* If we are not being transported then ask for a boat again */
2028 alive = TRUE;
2031 unit_tile(punit), dest_city->tile)) {
2033 }
2034 }
2035 if (alive) {
2036 /* FIXME: Sometimes we get FALSE here just because
2037 * a trireme that we've boarded can't go over an ocean. */
2038 alive = dai_gothere(ait, pplayer, punit, dest_city->tile);
2039 }
2040 } else {
2041 /* To trade without boat */
2043 }
2044 }
2045
2046 /* If moving didn't kill us, and we got to the destination, handle it. */
2047 if (alive && real_map_distance(dest_city->tile, unit_tile(punit)) <= 1) {
2048 /* Release the boat! */
2049 if (unit_transported(punit)) {
2051 }
2053 punit, dest_city)) {
2054 /*
2055 * We really don't want to just drop all caravans in immediately.
2056 * Instead, we want to aggregate enough caravans to build instantly.
2057 * -AJS, 990704
2058 */
2059 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) helps build wonder in %s",
2062 punit->id,
2065 unit_do_action(pplayer, punit->id, dest_city->id,
2066 0, "", ACTION_HELP_WONDER);
2068 punit, dest_city)) {
2069 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) creates trade route in %s",
2072 punit->id,
2075 unit_do_action(pplayer, punit->id, dest_city->id,
2076 0, "", ACTION_TRADE_ROUTE);
2078 punit, dest_city)) {
2079 /* Get the one time bonus. */
2080 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) enters marketplace of %s",
2083 punit->id,
2086 unit_do_action(pplayer, punit->id, dest_city->id,
2087 0, "", ACTION_MARKETPLACE);
2088 } else {
2089 enum log_level level = LOG_NORMAL;
2090
2091 if (help_wonder) {
2092 /* A Caravan ordered to help build wonder may arrive after
2093 * enough shields to build the wonder is produced. */
2095 }
2096
2098 "%s %s[%d](%d,%d) unable to trade with %s",
2101 punit->id,
2104 }
2105 }
2106}
2107
2108/**********************************************************************/
2112static void caravan_optimize_callback(const struct caravan_result *result,
2113 void *data)
2114{
2115 const struct unit *caravan = data;
2116
2117 log_base(LOG_CARAVAN3, "%s %s[%d](%d,%d) %s: %s %s worth %g",
2119 unit_rule_name(caravan),
2120 caravan->id,
2121 TILE_XY(unit_tile(caravan)),
2122 city_name_get(result->src),
2123 result->help_wonder ? "wonder in" : "trade to",
2124 city_name_get(result->dest),
2125 result->value);
2126}
2127
2128/**********************************************************************/
2132 struct unit *punit)
2133{
2134 struct tile *src = nullptr, *dest = nullptr, *src_home_city = nullptr;
2135 struct city *phome_city = nullptr;
2136 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2137
2138 if ((unit_data->task != AIUNIT_NONE)) {
2139 src = unit_tile(punit);
2141 if (phome_city != nullptr) {
2143 }
2144 dest = punit->goto_tile;
2145
2146 if (src == nullptr || dest == nullptr) {
2147 return FALSE;
2148 }
2149
2150 /* If we have a home continent, and are not there.
2151 * (FIXME: well, why?)
2152 * (I guess because home continent is which we were supposed to leave,
2153 * not the target continent) */
2154 if (src_home_city != nullptr
2156 return FALSE;
2157 }
2158
2159 if (!goto_is_sane(punit, dest)) {
2160 if (unit_transported(punit)) {
2161 /* If we're being transported */
2162 return FALSE;
2163 }
2164 if ((punit->birth_turn + 15 < game.info.turn)) {
2165 /* We are tired of waiting */
2167
2168 if (ferrys <= 0) {
2169 /* There are no ferrys available... give up */
2170 return TRUE;
2171 } else {
2172 if (punit->birth_turn + 20 < game.info.turn) {
2173 /* We are fed up! */
2174 return TRUE;
2175 }
2176 }
2177 }
2178 }
2179 }
2180
2181 return FALSE;
2182}
2183
2184/**********************************************************************/
2191 struct unit *punit)
2192{
2194 Continent_id continent;
2195
2196 fc_assert(pcity != nullptr);
2197
2198 if (unit_class_get(punit)->adv.ferry_types <= 0) {
2199 /* There is just no possible transporters. */
2200 return FALSE;
2201 }
2202 continent = tile_continent(pcity->tile);
2203
2204 /* Look for proper destination city at different continent. */
2205 city_list_iterate(pplayer->cities, acity) {
2207 if (tile_continent(acity->tile) != continent) {
2208 return TRUE;
2209 }
2210 }
2212
2214 if (aplayer == pplayer || !aplayer->is_alive) {
2215 continue;
2216 }
2217 if (pplayers_allied(pplayer, aplayer)) {
2218 city_list_iterate(aplayer->cities, acity) {
2220 if (tile_continent(acity->tile) != continent) {
2221 return TRUE;
2222 }
2223 }
2226 }
2227
2228 return FALSE;
2229}
2230
2231/**********************************************************************/
2235static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
2236{
2237 struct city *nearest = nullptr;
2238 int min_dist = FC_INFINITY;
2239 struct tile *current_loc = unit_tile(punit);
2241 bool alive = TRUE;
2242
2244 struct tile *ctile = city_tile(pcity);
2245
2246 if (tile_continent(ctile) == continent) {
2248
2249 if (this_dist < min_dist) {
2251 nearest = pcity;
2252 }
2253 }
2255
2256 if (nearest != nullptr) {
2258 if (alive && same_pos(unit_tile(punit), nearest->tile)) {
2260 }
2261 }
2262
2263 return alive;
2264}
2265
2266/**********************************************************************/
2272static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
2273 struct unit *punit)
2274{
2275 struct caravan_parameter parameter;
2276 struct caravan_result result;
2277 const struct city *homecity;
2278 const struct city *dest = nullptr;
2279 struct unit_ai *unit_data;
2281 bool expect_boats = pclass->adv.ferry_types > 0;
2282 /* TODO: Will pplayer have a boat for punit in a reasonable time? */
2283 bool help_wonder = FALSE;
2284 bool required_boat = FALSE;
2285 bool request_boat = FALSE;
2287 const struct civ_map *nmap = &(wld.map);
2288
2290
2294 /* We only want units that can establish trade, enter marketplace or
2295 * help build wonders */
2296 return;
2297 }
2298
2300
2301 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) task %s to (%d,%d)",
2306
2307 homecity = game_city_by_number(punit->homecity);
2308 if (homecity == nullptr && unit_data->task == AIUNIT_TRADE) {
2310 return;
2311 }
2312 homecity = game_city_by_number(punit->homecity);
2313 if (homecity == nullptr) {
2314 return;
2315 }
2316 }
2317
2318 if ((unit_data->task == AIUNIT_TRADE
2319 || unit_data->task == AIUNIT_WONDER)) {
2320 /* We are moving to our destination */
2321 /* We check to see if our current goal is feasible */
2323 struct goods_type *pgood = unit_current_goods(punit, homecity);
2324
2325 if ((city_dest == nullptr)
2327 || (unit_data->task == AIUNIT_TRADE
2328 && !(can_cities_trade(homecity, city_dest)
2330 pgood->replace_priority)))
2331 || (unit_data->task == AIUNIT_WONDER
2332 /* Helping the (new) production is illegal. */
2334 || (unit_data->task == AIUNIT_TRADE
2339 punit, city_dest)))
2340 || (unit_data->task == AIUNIT_WONDER
2343 punit, city_dest))) {
2344 /* Destination invalid! */
2345 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2346 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) destination invalid!",
2349 } else {
2350 /* Destination valid. Are we tired of waiting for a boat? */
2353 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2354 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) unit tired of waiting!",
2358 } else {
2359 dest = city_dest;
2360 help_wonder = (unit_data->task == AIUNIT_WONDER) ? TRUE : FALSE;
2361 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2363 }
2364 }
2365 }
2366
2367 if (unit_data->task == AIUNIT_NONE) {
2368 if (homecity == nullptr) {
2369 /* FIXME: We shouldn't bother in getting homecity for
2370 * caravan that will then be used for wonder building. */
2372 return;
2373 }
2374 homecity = game_city_by_number(punit->homecity);
2375 if (homecity == nullptr) {
2376 return;
2377 }
2378 }
2379
2381 /* Make more trade with allies than other peaceful nations
2382 * by considering only allies 50% of the time.
2383 * (the other 50% allies are still considered, but so are other nations) */
2384 if (fc_rand(2)) {
2385 /* Be optimistic about development of relations with no-contact and
2386 * cease-fire nations. */
2387 parameter.allow_foreign_trade = FTL_NONWAR;
2388 } else {
2389 parameter.allow_foreign_trade = FTL_ALLIED;
2390 }
2391
2394 parameter.callback_data = punit;
2395 }
2397 parameter.ignore_transit_time = TRUE;
2398 }
2401 parameter.ignore_transit_time = FALSE;
2402 }
2403 caravan_find_best_destination(nmap, punit, &parameter, &result,
2404 !has_handicap(pplayer, H_MAP));
2405 if (result.dest != nullptr) {
2406 /* We did find a new destination for the unit */
2407 dest = result.dest;
2408 help_wonder = result.help_wonder;
2409 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2410 request_boat = required_boat;
2412 (help_wonder) ? AIUNIT_WONDER : AIUNIT_TRADE,
2413 dest->tile);
2414 } else {
2415 dest = nullptr;
2416 }
2417 }
2418
2419 if (required_boat && !expect_boats) {
2420 /* Would require boat, but can't have them. Render destination invalid. */
2421 dest = nullptr;
2422 }
2423
2424 if (dest != nullptr) {
2425 dai_caravan_goto(ait, pplayer, punit, dest, help_wonder,
2426 required_boat, request_boat);
2427 return; /* That may have clobbered the unit */
2428 } else {
2429 /* We have nowhere to go! */
2430 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d), nothing to do!",
2434 /* Should we become a defensive unit? */
2435 }
2436}
2437
2438/**********************************************************************/
2443 struct unit *punit)
2444{
2445 struct player *pplayer = unit_owner(punit);
2446 struct city *pcity = tile_city(unit_tile(punit));
2447 struct city *safe = nullptr;
2448 const struct unit_type *punittype = unit_type_get(punit);
2449 bool no_recovery;
2450
2452
2453 if (pcity != nullptr) {
2454 /* Rest in the city until the hitpoints are recovered, but attempt
2455 * to protect city from attack (and be opportunistic too)*/
2458 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "recovering hit points.");
2459 } else {
2460 return; /* We died heroically defending our city */
2461 }
2462 } else {
2463 /* Goto to nearest city to recover hit points */
2464 /* Just before, check to see if we can occupy an undefended enemy city */
2467 return; /* Oops, we died */
2468 }
2469
2470 /* Find a city to stay and go there */
2472 if (safe) {
2473 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "going to %s to recover",
2475 if (!dai_unit_goto(ait, punit, safe->tile)) {
2476 log_base(LOGLEVEL_RECOVERY, "died trying to hide and recover");
2477 return;
2478 }
2479 } else {
2480 /* Oops */
2481 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "didn't find a city to recover in!");
2482 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2483 dai_military_attack(ait, pplayer, punit);
2484 return;
2485 }
2486 }
2487
2488 /* Is the unit still damaged? If true, and could recover hit points, do so.
2489 * Don't wait if would be losing hitpoints that way! */
2491 if (punit->hp == punittype->hp) {
2492 no_recovery = TRUE;
2493 } else {
2495
2496 if (gain < 0) {
2497 no_recovery = TRUE;
2498 } else if (gain == 0 && !punit->moved) {
2499 /* Isn't gaining even though has not moved. */
2500 no_recovery = TRUE;
2501 }
2502 }
2503 if (no_recovery) {
2504 /* We are ready to go out and kick ass again */
2505 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "ready to kick ass again!");
2506 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2507 return;
2508 } else {
2509 def_ai_unit_data(punit, ait)->done = TRUE; /* Sit tight */
2510 }
2511}
2512
2513/**********************************************************************/
2517void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap,
2518 struct player *pplayer, struct unit *punit)
2519{
2520 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2521 int id = punit->id;
2522
2524
2525 /* "Escorting" aircraft should not do anything. They are activated
2526 * by their transport or charge. We do _NOT_ set them to 'done'
2527 * since they may need be activated once our charge moves. */
2528 if (unit_data->task == AIUNIT_ESCORT
2530 return;
2531 }
2532
2535 && has_handicap(pplayer, H_AWAY)) {
2536 /* Don't move sentried or fortified units controlled by a player
2537 * in away mode. */
2538 unit_data->done = TRUE;
2539 return;
2540 }
2541
2542 /* Since military units re-evaluate their actions every turn,
2543 we must make sure that previously reserved ferry is freed. */
2545
2547 /* Try hunting with this unit */
2548 if (dai_hunter_qualify(pplayer, punit)) {
2549 int result, sanity = punit->id;
2550
2551 UNIT_LOG(LOGLEVEL_HUNT, punit, "is qualified as hunter");
2552 result = dai_hunter_manage(ait, pplayer, punit);
2553 if (game_unit_by_number(sanity) == nullptr) {
2555 return; /* Died */
2556 }
2557 if (result == -1) {
2558 (void) dai_hunter_manage(ait, pplayer, punit); /* More carnage */
2560 return;
2561 } else if (result >= 1) {
2563 return; /* Done moving */
2564 } else if (unit_data->task == AIUNIT_HUNTER) {
2565 /* This should be very rare */
2566 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2567 }
2568 } else if (unit_data->task == AIUNIT_HUNTER) {
2569 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2570 }
2572
2573 /* Do we have a specific job for this unit? If not, we default
2574 * to attack. */
2575 dai_military_findjob(ait, nmap, pplayer, punit);
2576
2577 switch (unit_data->task) {
2578 case AIUNIT_AUTO_WORKER:
2579 case AIUNIT_BUILD_CITY:
2580 fc_assert(FALSE); /* This is not the place for this role */
2581 break;
2582 case AIUNIT_DEFEND_HOME:
2584 dai_military_defend(ait, pplayer, punit);
2586 break;
2587 case AIUNIT_ATTACK:
2588 case AIUNIT_NONE:
2590 dai_military_attack(ait, pplayer, punit);
2592 break;
2593 case AIUNIT_ESCORT:
2595 dai_military_bodyguard(ait, pplayer, punit);
2597 break;
2598 case AIUNIT_EXPLORE:
2599 switch (manage_auto_explorer(punit)) {
2600 case MR_DEATH:
2601 /* Don't use punit! */
2602 return;
2603 case MR_OK:
2604 UNIT_LOG(LOG_DEBUG, punit, "more exploring");
2605 break;
2606 default:
2607 UNIT_LOG(LOG_DEBUG, punit, "no more exploring either");
2608 break;
2609 };
2610 def_ai_unit_data(punit, ait)->done = (punit->moves_left <= 0);
2611 break;
2612 case AIUNIT_RECOVER:
2616 break;
2617 case AIUNIT_HUNTER:
2618 fc_assert(FALSE); /* Dealt with above */
2619 break;
2620 default:
2622 }
2623
2624 /* If we are still alive, either sentry or fortify. */
2625 if ((punit = game_unit_by_number(id))) {
2627 struct city *pcity = tile_city(unit_tile(punit));
2628
2629 if (unit_list_find(unit_tile(punit)->units,
2630 unit_data->ferryboat)) {
2632 } else if (pcity || punit->activity == ACTIVITY_IDLE) {
2633 /* We do not need to fortify in cities - we fortify and sentry
2634 * according to home defense setup, for easy debugging. */
2635 if (!pcity || unit_data->task == AIUNIT_DEFEND_HOME) {
2640 }
2641 } else {
2643 }
2644 }
2645 }
2646}
2647
2648/**********************************************************************/
2651static void dai_manage_settler(struct ai_type *ait, struct player *pplayer,
2652 struct unit *punit)
2653{
2654 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2655
2657 unit_data->done = TRUE; /* We will manage this unit later... ugh */
2658 /* If BUILD_CITY must remain BUILD_CITY, otherwise turn into autoworker */
2659 if (unit_data->task == AIUNIT_NONE) {
2661 }
2662}
2663
2664/**********************************************************************/
2672void dai_manage_unit(struct ai_type *ait, struct player *pplayer,
2673 struct unit *punit)
2674{
2675 struct unit_ai *unit_data;
2676 struct unit *bodyguard = aiguard_guard_of(ait, punit);
2677 bool is_ferry = FALSE;
2678 const struct unit_type *ptype;
2679 const struct civ_map *nmap = &(wld.map);
2680
2682
2683 /* Don't manage the unit if it is under human orders. */
2684 if (unit_has_orders(punit)) {
2686
2687 UNIT_LOG(LOG_VERBOSE, punit, "is under human orders, aborting AI control.");
2688 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2689 unit_data->done = TRUE;
2690
2691 return;
2692 }
2693
2694 /* Check if we have lost our bodyguard. If we never had one, all
2695 * fine. If we had one and lost it, ask for a new one. */
2696 if (!bodyguard && aiguard_has_guard(ait, punit)) {
2697 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "lost bodyguard, asking for new");
2699 }
2700
2702
2703 if (punit->moves_left <= 0) {
2704 /* Can do nothing */
2705 unit_data->done = TRUE;
2706
2707 return;
2708 }
2709
2710 is_ferry = dai_is_ferry(punit, ait);
2711
2713
2716 dai_manage_diplomat(ait, pplayer, punit);
2718
2719 return;
2720 } else if (ptype->adv.worker
2722 dai_manage_settler(ait, pplayer, punit);
2723
2724 return;
2729 dai_manage_caravan(ait, pplayer, punit);
2731
2732 return;
2734 dai_manage_barbarian_leader(ait, pplayer, punit);
2735
2736 return;
2739 dai_manage_paratrooper(ait, pplayer, punit);
2740
2741 return;
2742 } else if (is_ferry && unit_data->task != AIUNIT_HUNTER) {
2744 dai_manage_ferryboat(ait, pplayer, punit);
2746
2747 return;
2748 } else if (utype_fuel(ptype)
2749 && unit_data->task != AIUNIT_ESCORT) {
2751 dai_manage_airunit(ait, pplayer, punit);
2753
2754 return;
2755 } else if (is_losing_hp(punit)) {
2756 /* This unit is losing hitpoints over time */
2757
2758 /* TODO: We can try using air-unit code for helicopters, just
2759 * pretend they have fuel = HP / 3 or something. */
2760 unit_data->done = TRUE; /* we did our best, which was ...
2761 nothing */
2762 return;
2763 } else if (!is_special_unit(punit)) {
2765 UNIT_LOG(LOG_DEBUG, punit, "recruit unit for the military");
2766 dai_manage_military(ait, nmap, pplayer, punit);
2768
2769 return;
2770 } else {
2771 /* what else could this be? -- Syela */
2772 switch (manage_auto_explorer(punit)) {
2773 case MR_DEATH:
2774 /* don't use punit! */
2775 break;
2776 case MR_OK:
2777 UNIT_LOG(LOG_DEBUG, punit, "now exploring");
2778 break;
2779 default:
2780 UNIT_LOG(LOG_DEBUG, punit, "fell through all unit tasks, defending");
2782 dai_military_defend(ait, pplayer, punit);
2783 break;
2784 };
2785
2786 return;
2787 }
2788}
2789
2790/**********************************************************************/
2796static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
2797{
2798 city_list_iterate(pplayer->cities, pcity) {
2799 /* The idea here is that we should never keep more than two
2800 * units in permanent defense. */
2801 int total_defense = 0;
2803 bool emergency = FALSE;
2804 int count = 0;
2809 int entertainers = 0;
2810 bool enough = FALSE;
2811
2814 entertainers += pcity->specialists[sp];
2815 }
2817
2818 martless_unhappy += entertainers; /* We want to use martial law instead
2819 * of entertainers. */
2820
2821 while (!enough
2824 && martless_unhappy > mart_each * count))) {
2825 int best_want = 0;
2826 struct unit *best = nullptr;
2827 bool defense_needed = total_defense <= total_attack; /* Defense or martial */
2828
2830 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2831
2832 if ((unit_data->task == AIUNIT_NONE || emergency)
2833 && unit_data->task != AIUNIT_DEFEND_HOME
2834 && unit_owner(punit) == pplayer) {
2835 int want = assess_defense_unit(ait, pcity, punit, FALSE);
2836
2837 if (want > best_want) {
2838 best_want = want;
2839 best = punit;
2840 }
2841 }
2843
2844 if (best == nullptr) {
2845 if (defense_needed) {
2846 /* Ooops - try to grab any unit as defender! */
2847 if (emergency) {
2848 CITY_LOG(LOG_DEBUG, pcity, "Not defended properly");
2849 break;
2850 }
2851 emergency = TRUE;
2852 } else {
2853 break;
2854 }
2855 } else {
2856 const struct unit_type *btype = unit_type_get(best);
2857
2858 if ((martless_unhappy < mart_each * count
2859 || count >= mart_max || mart_each <= 0)
2860 && ((count >= 2
2861 && btype->attack_strength > btype->defense_strength)
2862 || (count >= 4
2863 && btype->attack_strength == btype->defense_strength))) {
2864 /* In this case attack would be better defense than fortifying
2865 * to city. */
2866 enough = TRUE;
2867 } else {
2868 int loglevel = pcity->server.debug ? LOG_AI_TEST : LOG_DEBUG;
2869
2871 UNIT_LOG(loglevel, best, "Defending city");
2873 count++;
2874 }
2875 }
2876 }
2877 CITY_LOG(LOG_DEBUG, pcity, "Evaluating defense: %d defense, %d incoming"
2878 ", %d defenders (out of %d)", total_defense, total_attack, count,
2881}
2882
2883/**********************************************************************/
2891void dai_manage_units(struct ai_type *ait, struct player *pplayer)
2892{
2894 dai_airlift(ait, pplayer);
2896
2897 /* Clear previous orders, if desirable, here. */
2898 unit_list_iterate(pplayer->units, punit) {
2899 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2900
2901 unit_data->done = FALSE;
2902 if (unit_data->task == AIUNIT_DEFEND_HOME) {
2903 dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
2904 }
2906
2907 /* Find and set city defenders first - figure out which units are
2908 * allowed to leave home. */
2909 dai_set_defenders(ait, pplayer);
2910
2912 if ((!unit_transported(punit)
2913 || unit_owner(unit_transport_get(punit)) != pplayer)
2914 && !def_ai_unit_data(punit, ait)->done) {
2915 /* Though it is usually the passenger who drives the transport,
2916 * the transporter is responsible for managing its passengers. */
2917 dai_manage_unit(ait, pplayer, punit);
2918 }
2920}
2921
2922/**********************************************************************/
2928 const struct city *pcity)
2929{
2930 const struct impr_type *impr_req = nullptr;
2931 const struct req_context context = {
2933 .city = pcity,
2934 .tile = city_tile(pcity),
2935 .unittype = putype,
2936 };
2937
2938 requirement_vector_iterate(&putype->build_reqs, preq) {
2939 if (is_req_active(&context, nullptr, preq, RPT_CERTAIN)) {
2940 /* Already there. */
2941 continue;
2942 }
2944 city_owner(pcity), pcity)) {
2945 /* The unit type can't be built at all. */
2946 return nullptr;
2947 }
2948 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2949 /* This is (one of) the building(s) required. */
2950 impr_req = preq->source.value.building;
2951 }
2953
2954 return impr_req;
2955}
2956
2957/**********************************************************************/
2962 const struct unit_type *base)
2963{
2964 /* This is the real function: */
2965 do {
2966 base = base->obsoleted_by;
2967 if (base == test) {
2968 return TRUE;
2969 }
2970 } while (base);
2971 return FALSE;
2972}
2973
2974/**********************************************************************/
2978static void dai_manage_barbarian_leader(struct ai_type *ait,
2979 struct player *pplayer,
2980 struct unit *leader)
2981{
2983 struct pf_parameter parameter;
2984 struct pf_map *pfm;
2985 struct pf_reverse_map *pfrm;
2986 struct unit *worst_danger;
2987 int move_cost, best_move_cost;
2988 int body_guards;
2989 bool alive = TRUE;
2990 const struct civ_map *nmap = &(wld.map);
2991
2993
2994 if (leader->moves_left == 0
2996 && 1 < unit_list_size(leader_tile->units))) {
2998
2999 return;
3000 }
3001
3002 if (is_boss_of_boat(ait, leader)) {
3003 /* We are in charge. Of course, since we are the leader...
3004 * But maybe somebody more militaristic should lead our ship to battle! */
3005
3006 /* First release boat from leaders lead */
3008
3012 && warrior->moves_left > 0) {
3013 /* This seems like a good warrior to lead us in to conquest! */
3014 dai_manage_unit(ait, pplayer, warrior);
3015
3016 /* If we reached our destination, ferryboat already called
3017 * ai_manage_unit() for leader. So no need to continue here.
3018 * Leader might even be dead.
3019 * If this return is removed, surrounding unit_list_iterate()
3020 * has to be replaced with unit_list_iterate_safe()*/
3021 return;
3022 }
3024 }
3025
3026 /* If we are not in charge of the boat, continue as if we
3027 * were not in a boat - we may want to leave the ship now. */
3028
3029 /* Check the total number of units able to protect our leader. */
3030 body_guards = 0;
3031 unit_list_iterate(pplayer->units, punit) {
3034 body_guards++;
3035 }
3037
3038 if (0 < body_guards) {
3039 pft_fill_unit_parameter(&parameter, nmap, leader);
3040 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3041 pfm = pf_map_new(&parameter);
3042
3043 /* Find the closest body guard.
3044 * FIXME: Maybe choose the strongest too? */
3045 pf_map_tiles_iterate(pfm, ptile, FALSE) {
3046 unit_list_iterate(ptile->units, punit) {
3047 if (unit_owner(punit) == pplayer
3050 struct pf_path *path = pf_map_path(pfm, ptile);
3051
3052 adv_follow_path(leader, path, ptile);
3053 pf_path_destroy(path);
3055
3056 return;
3057 }
3060
3062 }
3063
3064 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader needs to flee");
3065
3066 /* Check for units we could fear. */
3067 pfrm = pf_reverse_map_new(nmap, pplayer, leader_tile, 3,
3068 !has_handicap(pplayer, H_MAP));
3069 worst_danger = nullptr;
3071
3072 players_iterate(other_player) {
3073 if (other_player == pplayer) {
3074 continue;
3075 }
3076
3077 unit_list_iterate(other_player->units, punit) {
3079 if (PF_IMPOSSIBLE_MC != move_cost && move_cost < best_move_cost) {
3080 best_move_cost = move_cost;
3082 }
3085
3087
3088 if (worst_danger == nullptr) {
3090 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: no close enemy.");
3091 return;
3092 }
3093
3095 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3096 pfm = pf_map_new(&parameter);
3098
3099 /* Try to escape. */
3100 do {
3102
3103 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: moves left: %d.",
3104 leader->moves_left);
3105
3108 continue;
3109 }
3110
3111 move_cost = pf_map_move_cost(pfm, near_tile);
3112 if (PF_IMPOSSIBLE_MC != move_cost
3113 && move_cost > best_move_cost) {
3115 "Barbarian leader: safest is (%d, %d), safeness %d",
3117 best_move_cost = move_cost;
3119 }
3121
3122 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: fleeing to (%d, %d).",
3126 "Barbarian leader: reached the safest position.");
3129 return;
3130 }
3131
3133 if (alive) {
3135 /* Didn't move. No point to retry. */
3137 return;
3138 }
3140 }
3141 } while (alive && 0 < leader->moves_left);
3142
3144}
3145
3146/**********************************************************************/
3152void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile,
3153 struct unit *punit,
3154 enum override_bool *result)
3155{
3156 int a = 0, d, db;
3157 struct player *pplayer = unit_owner(punit);
3158 struct city *pcity = tile_city(ptile);
3159 int extras_bonus = 0;
3160
3161 if (is_human(pplayer)) {
3162 /* Use advisors code for humans. */
3163 return;
3164 }
3165
3166 if (pcity != nullptr && pplayers_allied(city_owner(pcity), pplayer)
3167 && !is_non_allied_unit_tile(ptile, pplayer,
3169 /* We will be safe in a friendly city */
3170 *result = OVERRIDE_FALSE;
3171 return;
3172 }
3173
3174 /* Calculate how well we can defend at (x,y) */
3175 db = 10 + tile_terrain(ptile)->defense_bonus / 10;
3177
3178 db += (db * extras_bonus) / 100;
3180
3181 adjc_iterate(&(wld.map), ptile, ptile1) {
3182 if (has_handicap(pplayer, H_FOG)
3183 && !map_is_known_and_seen(ptile1, pplayer, V_MAIN)) {
3184 /* We cannot see danger at (ptile1) => assume there is none */
3185 continue;
3186 }
3187 unit_list_iterate(ptile1->units, enemy) {
3188 if (pplayers_at_war(unit_owner(enemy), pplayer)
3189 && (unit_attack_unit_at_tile_result(enemy, nullptr, punit, ptile)
3190 == ATT_OK)
3191 && (unit_attack_units_at_tile_result(enemy, nullptr, ptile)
3192 == ATT_OK)) {
3194 if ((a * a * 10) >= d) {
3195 /* The enemies combined strength is too big! */
3196 *result = OVERRIDE_TRUE;
3197 return;
3198 }
3199 }
3202
3203 *result = OVERRIDE_FALSE;
3204}
3205
3206/**********************************************************************/
3209static void update_simple_ai_types(void)
3210{
3211 int i = 0;
3212
3215
3218 && !(pclass->adv.land_move == MOVE_NONE
3221 && punittype->transport_capacity < 8) {
3223 i++;
3224 }
3226
3227 simple_ai_types[i] = nullptr;
3228}
3229
3230/**********************************************************************/
3234{
3235 /* TODO: Remove the simple_ai_types cache or merge it with a general ai
3236 * cache; see the comment to struct unit_type *simple_ai_types at
3237 * the beginning of this file. */
3239
3241 struct unit_type_ai *utai = fc_malloc(sizeof(*utai));
3242
3243 utai->low_firepower = FALSE;
3244 utai->ferry = FALSE;
3245 utai->missile_platform = FALSE;
3246 utai->carries_occupiers = FALSE;
3247 utai->potential_charges = unit_type_list_new();
3248
3251
3254
3255 /* Confirm firepower */
3257 if (pbonus->type == CBONUS_LOW_FIREPOWER) {
3259 if (utype_has_flag(penemy, pbonus->flag)) {
3260 struct unit_type_ai *utai = utype_ai_data(penemy, ait);
3261
3262 utai->low_firepower = TRUE;
3263 }
3265 }
3267
3268 /* Consider potential cargo */
3269 if (punittype->transport_capacity > 0) {
3270 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3271
3274
3277 utai->missile_platform = TRUE;
3278 } else if (pclass->adv.sea_move != MOVE_NONE
3279 && pcargo->adv.land_move != MOVE_NONE) {
3280 if (pcargo->adv.sea_move != MOVE_FULL) {
3281 utai->ferry = TRUE;
3282 } else {
3283 if (0 != utype_fuel(pctype)) {
3284 utai->ferry = TRUE;
3285 }
3286 }
3287 }
3288
3290 utai->carries_occupiers = TRUE;
3291 }
3292 }
3294 }
3295
3296 /* Consider potential charges */
3299
3300 if (0 < utype_fuel(punittype)
3301 && (0 == utype_fuel(pcharge)
3303 continue;
3304 }
3305
3306 unit_class_list_iterate(pclass->cache.subset_movers, chgcls) {
3307 if (chgcls == utype_class(pcharge)) {
3309 }
3311
3313 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3314 unit_type_list_append(utai->potential_charges, pcharge);
3315 }
3316
3319}
3320
3321/**********************************************************************/
3325{
3327 struct unit_type_ai *utai = utype_ai_data(ptype, ait);
3328
3329 if (utai == nullptr) {
3330 continue;
3331 }
3332 utype_set_ai_data(ptype, ait, nullptr);
3333
3334 unit_type_list_destroy(utai->potential_charges);
3335 free(utai);
3337}
3338
3339/**********************************************************************/
3342void dai_unit_init(struct ai_type *ait, struct unit *punit)
3343{
3344 /* Make sure that contents of unit_ai structure are correctly initialized,
3345 * if you ever allocate it by some other mean than fc_calloc() */
3346 struct unit_ai *unit_data = fc_calloc(1, sizeof(struct unit_ai));
3347
3348 unit_data->done = FALSE;
3349 unit_data->cur_pos = nullptr;
3350 unit_data->prev_pos = nullptr;
3351 unit_data->target = 0;
3352 BV_CLR_ALL(unit_data->hunted);
3353 unit_data->ferryboat = 0;
3354 unit_data->passenger = 0;
3355 unit_data->bodyguard = 0;
3356 unit_data->charge = 0;
3357
3359}
3360
3361/**********************************************************************/
3364void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
3365{
3366 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3367
3368 fc_assert_ret(unit_data != nullptr);
3369
3370 BV_CLR_ALL(unit_data->hunted);
3371}
3372
3373/**********************************************************************/
3376void dai_unit_close(struct ai_type *ait, struct unit *punit)
3377{
3378 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3379
3380 fc_assert_ret(unit_data != nullptr);
3381
3384
3385 if (unit_data != nullptr) {
3386 unit_set_ai_data(punit, ait, nullptr);
3388 }
3389}
3390
3391/**********************************************************************/
3394void dai_unit_save(struct ai_type *ait, const char *aitstr,
3395 struct section_file *file,
3396 const struct unit *punit, const char *unitstr)
3397{
3398 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3399
3400 secfile_insert_int(file, unit_data->passenger, "%s.%spassenger",
3401 unitstr, aitstr);
3402 secfile_insert_int(file, unit_data->ferryboat, "%s.%sferryboat",
3403 unitstr, aitstr);
3404 secfile_insert_int(file, unit_data->charge, "%s.%scharge",
3405 unitstr, aitstr);
3406 secfile_insert_int(file, unit_data->bodyguard, "%s.%sbodyguard",
3407 unitstr, aitstr);
3408}
3409
3410/**********************************************************************/
3413void dai_unit_load(struct ai_type *ait, const char *aitstr,
3414 const struct section_file *file,
3415 struct unit *punit, const char *unitstr)
3416{
3417 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3418
3419 unit_data->passenger
3420 = secfile_lookup_int_default(file, 0, "%s.%spassenger",
3421 unitstr, aitstr);
3422 unit_data->ferryboat
3423 = secfile_lookup_int_default(file, 0, "%s.%sferryboat",
3424 unitstr, aitstr);
3425 unit_data->charge
3426 = secfile_lookup_int_default(file, 0, "%s.%scharge",
3427 unitstr, aitstr);
3428 unit_data->bodyguard
3429 = secfile_lookup_int_default(file, 0, "%s.%sbodyguard",
3430 unitstr, aitstr);
3431}
3432
3438
3439/**********************************************************************/
3442static bool role_unit_cb(struct unit_type *ptype, void *data)
3443{
3444 struct role_unit_cb_data *cb_data = (struct role_unit_cb_data *)data;
3446 const struct civ_map *nmap = &(wld.map);
3447
3448 if ((cb_data->tc == TC_LAND && pclass->adv.land_move == MOVE_NONE)
3449 || (cb_data->tc == TC_OCEAN && pclass->adv.sea_move == MOVE_NONE)) {
3450 return FALSE;
3451 }
3452
3453 if (cb_data->build_city == nullptr
3454 || can_city_build_unit_now(nmap, cb_data->build_city, ptype)) {
3455 return TRUE;
3456 }
3457
3458 return FALSE;
3459}
3460
3461/**********************************************************************/
3465 enum terrain_class tc)
3466{
3467 struct role_unit_cb_data cb_data = { .build_city = pcity, .tc = tc };
3468
3470}
3471
3472/**********************************************************************/
3475bool dai_unit_can_strike_my_unit(const struct unit *attacker,
3476 const struct unit *defender)
3477{
3478 struct pf_parameter parameter;
3479 struct pf_map *pfm;
3480 const struct tile *ptarget = unit_tile(defender);
3481 int max_move_cost = attacker->moves_left;
3482 bool able_to_strike = FALSE;
3483 const struct civ_map *nmap = &(wld.map);
3484
3485 pft_fill_unit_parameter(&parameter, nmap, attacker);
3486 parameter.omniscience = !has_handicap(unit_owner(defender), H_MAP);
3487 pfm = pf_map_new(&parameter);
3488
3489 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
3490 if (move_cost > max_move_cost) {
3491 break;
3492 }
3493
3494 if (ptile == ptarget) {
3496 break;
3497 }
3499
3501
3502 return able_to_strike;
3503}
3504
3505/**********************************************************************/
3508void dai_switch_to_explore(struct ai_type *ait, struct unit *punit,
3509 struct tile *target, enum override_bool *allow)
3510{
3511 struct unit_ai *udata = def_ai_unit_data(punit, ait);
3512
3513 if (udata->task != AIUNIT_NONE && udata->task != AIUNIT_EXPLORE) {
3515
3516 return;
3517 }
3518}
bool is_action_enabled_unit_on_stack(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit, const struct tile *target_tile)
Definition actions.c:3257
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:3092
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:1860
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:804
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:947
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
@ CITIZEN_ANGRY
Definition city.h:269
@ CITIZEN_UNHAPPY
Definition city.h:268
#define city_owner(_pcity_)
Definition city.h:563
#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:76
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:42
static struct unit_ai * def_ai_unit_data(const struct unit *punit, struct ai_type *deftype)
Definition daiplayer.h:48
int stack_cost(struct unit *pattacker, struct unit *pdefender)
Definition daitools.c:1283
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:1419
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:3394
static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:855
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:2235
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:2000
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:703
void dai_units_ruleset_init(struct ai_type *ait)
Definition daiunit.c:3233
#define LOG_CARAVAN
Definition daiunit.c:87
static int unit_att_rating_now(const struct unit *punit)
Definition daiunit.c:258
static void dai_military_attack_barbarian(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1695
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:1065
static void invasion_funct(struct ai_type *ait, struct unit *punit, bool dest, int radius, int which)
Definition daiunit.c:1023
static void update_simple_ai_types(void)
Definition daiunit.c:3209
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Definition daiunit.c:2927
void dai_switch_to_explore(struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
Definition daiunit.c:3508
void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2517
static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:1913
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:3475
bool is_on_unit_upgrade_path(const struct unit_type *test, const struct unit_type *base)
Definition daiunit.c:2961
static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:636
static void single_invader(struct ai_city *city_data, const struct unit_type *utype, int which)
Definition daiunit.c:995
#define LOG_CARAVAN3
Definition daiunit.c:89
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2891
static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2272
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:1144
static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2796
void dai_manage_unit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2672
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:718
static bool role_unit_cb(struct unit_type *ptype, void *data)
Definition daiunit.c:3442
static void dai_military_attack(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1758
void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3364
static void dai_manage_settler(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2651
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:2131
static int dai_rampage_want(struct unit *punit, struct tile *ptile)
Definition daiunit.c:456
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:837
struct city * find_nearest_safe_city(struct unit *punit)
Definition daiunit.c:1644
static void dai_manage_barbarian_leader(struct ai_type *ait, struct player *pplayer, struct unit *leader)
Definition daiunit.c:2978
static void dai_manage_hitpoint_recovery(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2442
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:524
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:3342
void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
Definition daiunit.c:3152
bool uclass_need_trans_between(struct unit_class *pclass, struct tile *ctile, struct tile *ptile)
Definition daiunit.c:1961
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:3413
static void caravan_optimize_callback(const struct caravan_result *result, void *data)
Definition daiunit.c:2112
#define LOGLEVEL_RECOVERY
Definition daiunit.c:86
void dai_unit_close(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3376
void dai_units_ruleset_close(struct ai_type *ait)
Definition daiunit.c:3324
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Definition daiunit.c:3464
static void dai_military_defend(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:949
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:603
static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer, struct unit *punit)
Definition daiunit.c:2190
#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:3044
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2966
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:74
bool ai_fuzzy(const struct player *pplayer, bool normal_decision)
Definition difficulty.c:339
int get_city_bonus(const struct city *pcity, enum effect_type effect_type)
Definition effects.c:842
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, const struct action *paction, enum effect_type effect_type)
Definition effects.c:1031
bool unit_can_enter_hut(const struct unit *punit, const struct tile *ptile)
Definition extras.c:720
float adv_want
Definition fc_types.h:1048
@ RPT_CERTAIN
Definition fc_types.h:515
@ AUT_AUTO_WORKER
Definition fc_types.h:230
#define ADV_WANT_PRINTF
Definition fc_types.h:1049
@ O_LUXURY
Definition fc_types.h:102
signed short Continent_id
Definition fc_types.h:232
override_bool
Definition fc_types.h:95
@ OVERRIDE_TRUE
Definition fc_types.h:95
@ OVERRIDE_FALSE
Definition fc_types.h:95
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
struct unit * game_unit_by_number(int id)
Definition game.c:116
struct city * game_city_by_number(int id)
Definition game.c:107
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:1067
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:1076
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:364
#define iterate_outward_end
Definition map.h:368
#define adjc_iterate_end
Definition map.h:430
static int index_to_map_pos_y(int mindex)
Definition map.h:763
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:388
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:425
#define square_iterate_end
Definition map.h:391
static int index_to_map_pos_x(int mindex)
Definition map.h:750
#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:901
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:480
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:1237
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1396
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1417
#define players_iterate_end
Definition player.h:542
#define players_iterate(_pplayer)
Definition player.h:537
static bool is_barbarian(const struct player *pplayer)
Definition player.h:491
#define is_human(plr)
Definition player.h:231
#define fc_rand(_size)
Definition rand.h:56
int secfile_lookup_int_default(const struct section_file *secfile, int def, const char *path,...)
#define secfile_insert_int(secfile, value, path,...)
bool is_req_active(const struct req_context *context, const struct req_context *other_context, const struct requirement *req, const enum req_problem_type prob_type)
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Definition specialist.c: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:3435
struct city * build_city
Definition daiunit.c:3436
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:616
#define is_ocean_tile(ptile)
Definition terrain.h:196
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:2354
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2474
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2292
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:2545
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2247
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2745
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:2964
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:914
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:2529
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:407
#define unit_cargo_iterate_end
Definition unit.h:590
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:587
#define CHECK_UNIT(punit)
Definition unit.h:273
#define unit_owner(_pu)
Definition unit.h:406
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.h:443
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unithand.c:6707
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:3349
bool unit_server_side_agent_set(struct player *pplayer, struct unit *punit, enum server_side_agent agent)
Definition unithand.c:6605
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:2262
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2279
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:2325
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2530
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:2771
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:2779
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:847
#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:948
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:940
#define unit_type_iterate(_p)
Definition unittype.h:863
#define unit_type_list_iterate_end
Definition unittype.h:950
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:870
#define unit_class_list_iterate_end
Definition unittype.h:942