Freeciv-3.2
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 "autosettlers.h"
63
64/* ai */
65#include "difficulty.h"
66#include "handicaps.h"
67
68/* ai/default */
69#include "aiair.h"
70#include "aidiplomat.h"
71#include "aiferry.h"
72#include "aiguard.h"
73#include "aihand.h"
74#include "aihunt.h"
75#include "aiparatrooper.h"
76#include "aitools.h"
77#include "daicity.h"
78#include "daidata.h"
79#include "daieffects.h"
80#include "dailog.h"
81#include "daimilitary.h"
82#include "daiplayer.h"
83
84#include "daiunit.h"
85
86
87#define LOGLEVEL_RECOVERY LOG_DEBUG
88#define LOG_CARAVAN LOG_DEBUG
89#define LOG_CARAVAN2 LOG_DEBUG
90#define LOG_CARAVAN3 LOG_DEBUG
91
92static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit);
93static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer,
94 struct unit *punit);
95static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
96 struct unit *punit);
97static void dai_manage_barbarian_leader(struct ai_type *ait,
98 struct player *pplayer,
99 struct unit *leader);
100
101static void dai_military_findjob(struct ai_type *ait,
102 struct player *pplayer, struct unit *punit);
103static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
104 struct unit *punit);
105static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
106 struct unit *punit);
107
108static bool unit_role_defender(const struct unit_type *punittype);
109static int unit_def_rating_squared(const struct unit *punit,
110 const struct unit *pdef);
111
112/*
113 * Cached values. Updated by update_simple_ai_types.
114 *
115 * This a hack to enable emulation of old loops previously hardwired
116 * as
117 * for (i = U_WARRIORS; i <= U_BATTLESHIP; i++)
118 *
119 * (Could probably just adjust the loops themselves fairly simply,
120 * but this is safer for regression testing.)
121 *
122 * Not dealing with planes yet.
123 *
124 * Terminated by NULL.
125 */
127
128/**********************************************************************/
138static struct city *find_neediest_airlift_city(struct ai_type *ait,
139 const struct player *pplayer)
140{
141 struct city *neediest_city = NULL;
142 int most_danger = 0;
143 int most_urgent = 0;
144
145 city_list_iterate(pplayer->cities, pcity) {
146 struct ai_city *city_data = def_ai_city_data(pcity, ait);
147
148 if (pcity->airlift) {
149 if (city_data->urgency > most_urgent) {
150 most_urgent = city_data->urgency;
151 neediest_city = pcity;
152 } else if (0 == most_urgent /* urgency trumps danger */
153 && city_data->danger > most_danger) {
154 most_danger = city_data->danger;
155 neediest_city = pcity;
156 }
157 }
159
160 return neediest_city;
161}
162
163/**********************************************************************/
169static void dai_airlift(struct ai_type *ait, struct player *pplayer)
170{
171 struct city *most_needed;
172 int comparison;
173 struct unit *transported;
174
175 do {
177 comparison = 0;
178 transported = NULL;
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, NULL, ptile)
465 && (pdef = get_defender(nmap, punit, ptile, NULL))) {
466 /* See description of kill_desire() about these variables. */
470
471 attack *= attack;
472
473 /* If the victim is in the city/fortress, we correct the benefit
474 * with our health because there could be reprisal attacks. We
475 * shouldn't send already injured units to useless suicide.
476 * Note that we do not specially encourage attacks against
477 * cities: rampage is a hit-n-run operation. */
478 if (!is_stack_vulnerable(ptile)
479 && unit_list_size(ptile->units) > 1) {
481 }
482
483 /* If we have non-zero attack rating... */
484 if (attack > 0 && is_my_turn(punit, pdef)) {
487
488 /* No need to amortize, our operation takes one turn. */
489 UNIT_LOG(LOG_DEBUG, punit, "Rampage: Desire %d to kill %s(%d,%d)",
490 desire,
493
494 return MAX(0, desire);
495 }
496 } else if (0 == unit_list_size(ptile->units)) {
497 /* No defender. */
498 struct city *pcity = tile_city(ptile);
499
500 /* ...and free foreign city waiting for us. Who would resist! */
501 if (NULL != pcity
502 && pplayers_at_war(pplayer, city_owner(pcity))
505 }
506
507 /* ...or tiny pleasant hut here! */
508 /* FIXME: unhardcode and variate the desire to enter a hut. */
509 if (unit_can_enter_hut(punit, ptile) && !is_barbarian(pplayer)
510 && is_native_tile(unit_type_get(punit), ptile)) {
511 return -RAMPAGE_HUT_OR_BETTER;
512 }
513 }
514
515 return 0;
516}
517
518/**********************************************************************/
521static struct pf_path *find_rampage_target(struct unit *punit,
522 int thresh_adj, int thresh_move)
523{
524 struct pf_map *tgt_map;
525 struct pf_path *path = NULL;
526 struct pf_parameter parameter;
527 /* Coordinates of the best target (initialize to silence compiler) */
528 struct tile *ptile = unit_tile(punit);
529 /* Want of the best target */
530 int max_want = 0;
531 struct player *pplayer = unit_owner(punit);
532 const struct civ_map *nmap = &(wld.map);
533
535 parameter.omniscience = !has_handicap(pplayer, H_MAP);
536 /* When trying to find rampage targets we ignore risks such as
537 * enemy units because we are looking for trouble!
538 * Hence no call ai_avoid_risks()
539 */
540
541 tgt_map = pf_map_new(&parameter);
543 int want;
544 bool move_needed;
545 int thresh;
546
547 if (move_cost > punit->moves_left) {
548 /* This is too far */
549 break;
550 }
551
552 if (has_handicap(pplayer, H_TARGETS)
553 && !map_is_known_and_seen(iter_tile, pplayer, V_MAIN)) {
554 /* The target is under fog of war */
555 continue;
556 }
557
559
560 /* Negative want means move needed even though the tiles are adjacent */
562 || want < 0);
563 /* Select the relevant threshold */
565 want = (want < 0 ? -want : want);
566
567 if (want > max_want && want > thresh) {
568 /* The new want exceeds both the previous maximum
569 * and the relevant threshold, so it's worth recording */
570 max_want = want;
571 ptile = iter_tile;
572 }
574
575 if (max_want > 0) {
576 /* We found something */
577 path = pf_map_path(tgt_map, ptile);
578 fc_assert(path != NULL);
579 }
580
582
583 return path;
584}
585
586/**********************************************************************/
601 int thresh_move)
602{
603 int count = punit->moves_left + 1; /* break any infinite loops */
604 struct pf_path *path = NULL;
605
608
610 /* This teaches the AI about the dangers inherent in occupychance. */
612
613 while (count-- > 0 && punit->moves_left > 0
615 if (!adv_unit_execute_path(punit, path)) {
616 /* Died */
617 count = -1;
618 }
619 pf_path_destroy(path);
620 path = NULL;
621 }
622
623 fc_assert(NULL == path);
624
626 return (count >= 0);
627}
628
629/**********************************************************************/
633static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer,
634 struct unit *punit)
635{
636 struct unit *aunit = aiguard_charge_unit(ait, punit);
637 struct city *acity = aiguard_charge_city(ait, punit);
638 struct tile *ptile;
639
641 CHECK_GUARD(ait, punit);
642
643 if (aunit && unit_owner(aunit) == unit_owner(punit)) {
644 /* protect a unit */
645 if (aunit->goto_tile != NULL) {
646 /* Our charge is going somewhere: maybe we should meet them there */
647 /* FIXME: This probably isn't the best algorithm for this. */
649 int me2goal = real_map_distance(unit_tile(punit), aunit->goto_tile);
650 int them2goal = real_map_distance(unit_tile(aunit), aunit->goto_tile);
651
652 if (me2goal < me2them
656 ptile = aunit->goto_tile;
657 } else {
658 ptile = unit_tile(aunit);
659 }
660 } else {
661 ptile = unit_tile(aunit);
662 }
663 } else if (acity && city_owner(acity) == unit_owner(punit)) {
664 /* protect a city */
665 ptile = acity->tile;
666 } else {
667 /* should be impossible */
668 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "we lost our charge");
670 return;
671 }
672
673 if (same_pos(unit_tile(punit), ptile)) {
674 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "at RV");
675 } else {
676 if (goto_is_sane(punit, ptile)) {
677 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "meeting charge");
678 if (!dai_gothere(ait, pplayer, punit, ptile)) {
679 /* We died */
680 return;
681 }
682 } else {
683 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "can not meet charge");
685 }
686 }
687 /* We might have stopped because of an enemy nearby.
688 * Perhaps we can kill it.*/
691 && same_pos(unit_tile(punit), ptile)) {
692 def_ai_unit_data(punit, ait)->done = TRUE; /* Stay with charge */
693 }
694}
695
696/**********************************************************************/
699static bool unit_role_defender(const struct unit_type *punittype)
700{
702}
703
704/**********************************************************************/
714adv_want look_for_charge(struct ai_type *ait, struct player *pplayer,
715 struct unit *punit,
716 struct unit **aunit, struct city **acity)
717{
718 struct pf_parameter parameter;
719 struct pf_map *pfm;
720 struct city *pcity;
721 struct ai_city *data, *best_data = NULL;
723 int def, best_def = -1;
724 /* Arbitrary: 3 turns. */
725 const int max_move_cost = 3 * unit_move_rate(punit);
726 const struct civ_map *nmap = &(wld.map);
727
728 *aunit = NULL;
729 *acity = NULL;
730
731 if (0 == toughness) {
732 /* useless */
733 return 0;
734 }
735
736 pft_fill_unit_parameter(&parameter, nmap, punit);
737 parameter.omniscience = !has_handicap(pplayer, H_MAP);
738 pfm = pf_map_new(&parameter);
739
740 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
741 if (move_cost > max_move_cost) {
742 /* Consider too far. */
743 break;
744 }
745
746 pcity = tile_city(ptile);
747
748 /* Consider unit bodyguard. */
749 unit_list_iterate(ptile->units, buddy) {
750 const struct unit_type *ptype = unit_type_get(punit);
751 const struct unit_type *buddy_type = unit_type_get(buddy);
752
753 /* TODO: allied unit bodyguard? */
755 || unit_owner(buddy) != pplayer
756 || !aiguard_wanted(ait, buddy)
762
763 continue;
764 }
765
767 if (0 >= def) {
768 continue;
769 }
770
772 /* Reduce want based on move cost. We can't do this for
773 * transports since they move around all the time, leading
774 * to hillarious flip-flops. */
775 def >>= move_cost / (2 * unit_move_rate(punit));
776 }
777 if (def > best_def) {
778 *aunit = buddy;
779 *acity = NULL;
780 best_def = def;
781 }
783
784 /* City bodyguard. TODO: allied city bodyguard? */
785 if (ai_fuzzy(pplayer, TRUE)
786 && NULL != pcity
787 && city_owner(pcity) == pplayer
788 && (data = def_ai_city_data(pcity, ait))
789 && 0 < data->urgency) {
790 if (NULL != best_data
791 && (0 < best_data->grave_danger
792 || best_data->urgency > data->urgency
793 || ((best_data->danger > data->danger
794 || AIUNIT_DEFEND_HOME == def_ai_unit_data(punit, ait)->task)
795 && 0 == data->grave_danger))) {
796 /* Chances are we'll be between cities when we are needed the most!
797 * Resuming pf_map_move_costs_iterate()... */
798 continue;
799 }
800 def = (data->danger - assess_defense_quadratic(ait, pcity));
801 if (def <= 0) {
802 continue;
803 }
804 /* Reduce want based on move cost. */
805 def >>= move_cost / (2 * unit_move_rate(punit));
806 if (def > best_def && ai_fuzzy(pplayer, TRUE)) {
807 *acity = pcity;
808 *aunit = NULL;
809 best_def = def;
810 best_data = data;
811 }
812 }
814
816
817 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "%s(), best_def=%d, type=%s (%d, %d)",
820 : (NULL != *aunit ? unit_rule_name(*aunit) : "")),
822 : (NULL != *aunit ?
825 : (NULL != *aunit ?
827
828 return ((best_def * 100) / toughness);
829}
830
831/**********************************************************************/
835 const struct unit_type *followee,
836 struct ai_type *ait)
837{
838 struct unit_type_ai *utai = utype_ai_data(follower, ait);
839
840 unit_type_list_iterate(utai->potential_charges, pcharge) {
841 if (pcharge == followee) {
842 return TRUE;
843 }
845
846 return FALSE;
847}
848
849/**********************************************************************/
852static void dai_military_findjob(struct ai_type *ait,
853 struct player *pplayer, struct unit *punit)
854{
855 const struct unit_type *punittype = unit_type_get(punit);
856 struct unit_ai *unit_data;
857 const struct civ_map *nmap = &(wld.map);
858
860
861 /* Keep barbarians aggressive and primitive */
862 if (is_barbarian(pplayer)) {
864 && is_land_barbarian(pplayer)) {
865 /* Land barbarians pillage */
867 }
869
870 return;
871 }
872
874
875 /* If I am a bodyguard, check whether I can do my job. */
876 if (unit_data->task == AIUNIT_ESCORT
877 || unit_data->task == AIUNIT_DEFEND_HOME) {
879 }
880
881 if (aiguard_has_charge(ait, punit)
882 && unit_data->task == AIUNIT_ESCORT) {
883 struct unit *aunit = aiguard_charge_unit(ait, punit);
884 struct city *acity = aiguard_charge_city(ait, punit);
885 struct ai_city *city_data = NULL;
886
887 if (acity != NULL) {
889 }
890
891 /* Check if the city we are on our way to rescue is still in danger,
892 * or the unit we should protect is still alive... */
893 if ((aunit && (aiguard_has_guard(ait, aunit) || aiguard_wanted(ait, aunit))
896 && city_data->urgency != 0
897 && city_data->danger > assess_defense_quadratic(ait, acity))) {
898 return; /* Yep! */
899 } else {
900 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL); /* Nope! */
901 }
902 }
903
904 /* Is the unit badly damaged? */
905 if ((unit_data->task == AIUNIT_RECOVER
906 && punit->hp < punittype->hp)
907 || punit->hp < punittype->hp * 0.25) { /* WAG */
908 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "set to hp recovery");
910 return;
911 }
912
915 /* This is a defending unit that doesn't need to stay put.
916 * It needs to defend something, but not necessarily where it's at.
917 * Therefore, it will consider becoming a bodyguard. -- Syela */
918 struct city *acity;
919 struct unit *aunit;
920
921 look_for_charge(ait, pplayer, punit, &aunit, &acity);
922 if (acity) {
925 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend city");
926 } else if (aunit) {
929 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend unit");
930 }
931 }
933}
934
935/**********************************************************************/
944static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
945 struct unit *punit)
946{
947 struct city *pcity = aiguard_charge_city(ait, punit);
948
950
951 if (!pcity || city_owner(pcity) != pplayer) {
952 pcity = tile_city(unit_tile(punit));
953 /* Do not stay defending an allied city forever */
955 }
956
957 if (!pcity) {
958 /* Try to find a place to rest. Sitting duck out in the wilderness
959 * is generally a bad idea, since we protect no cities that way, and
960 * it looks silly. */
961 pcity = find_closest_city(unit_tile(punit), NULL, pplayer,
964 }
965
966 if (!pcity) {
968 }
969
972 /* ... we survived */
973 if (pcity) {
974 UNIT_LOG(LOG_DEBUG, punit, "go to defend %s", city_name_get(pcity));
975 if (same_pos(unit_tile(punit), pcity->tile)) {
976 UNIT_LOG(LOG_DEBUG, punit, "go defend successful");
978 } else {
979 (void) dai_gothere(ait, pplayer, punit, pcity->tile);
980 }
981 } else {
982 UNIT_LOG(LOG_VERBOSE, punit, "defending nothing...?");
983 }
984 }
985}
986
987/**********************************************************************/
990static void single_invader(struct ai_city *city_data,
991 const struct unit_type *utype,
992 int which)
993{
994 int attacks;
995
998 attacks = 1;
999 } else {
1000 attacks = utype->move_rate;
1001 }
1002 city_data->invasion.attack += attacks;
1003 if (which == INVASION_OCCUPY) {
1004 city_data->invasion.occupy++;
1005 }
1006}
1007
1008/**********************************************************************/
1018static void invasion_funct(struct ai_type *ait, struct unit *punit,
1019 bool dest, int radius, int which)
1020{
1021 struct tile *ptile;
1022 struct player *pplayer = unit_owner(punit);
1023
1025
1026 if (dest) {
1027 ptile = punit->goto_tile;
1028 } else {
1029 ptile = unit_tile(punit);
1030 }
1031
1032 square_iterate(&(wld.map), ptile, radius, tile1) {
1033 struct city *pcity = tile_city(tile1);
1034
1035 if (pcity
1036 && POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, city_owner(pcity))
1037 && (dest || !has_defense(pcity))) {
1038 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1039
1040 /* Unit itself */
1042
1043 /* Cargo */
1044 unit_cargo_iterate(punit, cargo) {
1045 const struct unit_type *utype = unit_type_get(cargo);
1046
1047 if (IS_ATTACKER(utype)) {
1051 }
1053 }
1055}
1056
1057/**********************************************************************/
1060bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map,
1061 struct tile *dest_tile,
1062 const struct unit_type *cargo_type,
1063 struct tile **ferry_dest, struct tile **beachhead_tile)
1064{
1065 if (NULL == tile_city(dest_tile)
1067 /* Unit can directly go to 'dest_tile'. */
1068 struct tile *best_tile = NULL;
1070
1071 if (NULL != beachhead_tile) {
1072 *beachhead_tile = dest_tile;
1073 }
1074
1075 adjc_iterate(&(wld.map), dest_tile, ptile) {
1077 if (cost != PF_IMPOSSIBLE_MC
1078 && (NULL == best_tile || cost < best_cost)) {
1079 best_tile = ptile;
1080 best_cost = cost;
1081 }
1083
1084 if (NULL != ferry_dest) {
1086 }
1087
1088 return (PF_IMPOSSIBLE_MC != best_cost);
1089 } else {
1090 /* We need to find a beach around 'dest_tile'. */
1091 struct tile *best_tile = NULL, *best_beach = NULL;
1094
1095 tile_list_append(checked_tiles, dest_tile);
1096 adjc_iterate(&(wld.map), dest_tile, beach) {
1098 /* Can land there. */
1099 adjc_iterate(&(wld.map), beach, ptile) {
1100 if (!tile_list_search(checked_tiles, ptile)
1101 && !is_non_allied_unit_tile(ptile, pplayer)) {
1104 if (cost != PF_IMPOSSIBLE_MC
1105 && (NULL == best_tile || cost < best_cost)) {
1106 best_beach = beach;
1107 best_tile = ptile;
1108 best_cost = cost;
1109 }
1110 }
1112 }
1114
1116
1117 if (NULL != beachhead_tile) {
1119 }
1120 if (NULL != ferry_dest) {
1122 }
1123 return (PF_IMPOSSIBLE_MC != best_cost);
1124 }
1125}
1126
1127/**********************************************************************/
1135adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
1136 struct unit *punit,
1137 struct tile **pdest_tile, struct pf_path **ppath,
1138 struct pf_map **pferrymap,
1139 struct unit **pferryboat,
1140 const struct unit_type **pboattype, int *pmove_time)
1141{
1142 const int attack_value = adv_unit_att_rating(punit); /* basic attack. */
1143 struct pf_parameter parameter;
1144 struct pf_map *punit_map, *ferry_map;
1145 struct pf_position pos;
1147 const struct unit_type *punit_type = unit_type_get(punit);
1148 struct tile *punit_tile = unit_tile(punit);
1149 /* Type of our boat (a future one if ferryboat == NULL). */
1150 const struct unit_type *boattype = NULL;
1151 struct unit *ferryboat = NULL;
1152 struct city *pcity;
1153 struct ai_city *acity_data;
1154 int bcost, bcost_bal; /* Build cost of the attacker (+adjustments). */
1155 bool handicap = has_handicap(pplayer, H_TARGETS);
1156 bool unhap = FALSE; /* Do we make unhappy citizen. */
1157 bool harbor = FALSE; /* Do we have access to sea? */
1158 bool go_by_boat; /* Whether we need a boat or not. */
1159 int vulnerability; /* Enemy defense rating. */
1160 adv_want benefit; /* Benefit from killing the target. */
1161 struct unit *pdefender; /* Enemy city defender. */
1162 int move_time; /* Turns needed to target. */
1163 int reserves;
1164 int attack; /* Our total attack value with reinforcements. */
1165 int victim_count; /* Number of enemies there. */
1166 int needferry; /* Cost of building a ferry boat. */
1167 /* This is a kluge, because if we don't set x and y with !punit->id,
1168 * p_a_w isn't called, and we end up not wanting ironclads and therefore
1169 * never learning steam engine, even though ironclads would be very
1170 * useful. -- Syela */
1171 adv_want bk = 0;
1172 adv_want want; /* Want (amortized) of the operaton. */
1173 adv_want best = 0; /* Best of all wants. */
1174 struct tile *goto_dest_tile = NULL;
1175 bool can_occupy;
1176 struct civ_map *nmap = &(wld.map);
1177
1178 /* Very preliminary checks. */
1180 if (NULL != pferrymap) {
1181 *pferrymap = NULL;
1182 }
1183 if (NULL != pferryboat) {
1184 *pferryboat = NULL;
1185 }
1186 if (NULL != pboattype) {
1187 *pboattype = NULL;
1188 }
1189 if (NULL != pmove_time) {
1190 *pmove_time = 0;
1191 }
1192 if (NULL != ppath) {
1193 *ppath = NULL;
1194 }
1195
1196 if (0 == attack_value) {
1197 /* A very poor attacker... probably low on HP. */
1198 return 0;
1199 }
1200
1202
1203
1204 /*** Part 1: Calculate targets ***/
1205
1206 /* This horrible piece of code attempts to calculate the attractiveness of
1207 * enemy cities as targets for our units, by checking how many units are
1208 * going towards it or are near it already. */
1209
1210 /* Reset enemy cities data. */
1212 /* See comment below in next usage of POTENTIALLY_HOSTILE_PLAYER. */
1213 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1214 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1215 continue;
1216 }
1217 city_list_iterate(aplayer->cities, acity) {
1218 struct ai_city *city_data = def_ai_city_data(acity, ait);
1219
1221 &city_data->attack,
1222 &city_data->bcost);
1223 city_data->invasion.attack = 0;
1224 city_data->invasion.occupy = 0;
1227
1228 /* Second, calculate in units on their way there, and mark targets for
1229 * invasion */
1230 unit_list_iterate(pplayer->units, aunit) {
1231 const struct unit_type *atype;
1232
1233 if (aunit == punit) {
1234 continue;
1235 }
1236
1238
1239 /* Dealing with invasion stuff */
1240 if (IS_ATTACKER(atype)) {
1241 if (aunit->activity == ACTIVITY_GOTO) {
1242 invasion_funct(ait, aunit, TRUE, 0,
1245 if ((pcity = tile_city(aunit->goto_tile))) {
1246 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1247
1250 }
1251 }
1255 } else if (def_ai_unit_data(aunit, ait)->passenger != 0
1257 /* It's a transport with reinforcements */
1258 if (aunit->activity == ACTIVITY_GOTO) {
1260 }
1262 }
1264 /* end horrible initialization subroutine */
1265
1266
1267 /*** Part 2: Now pick one target ***/
1268
1269 /* We first iterate through all cities, then all units, looking
1270 * for a viable target. We also try to gang up on the target
1271 * to avoid spreading out attacks too widely to be inefficient.
1272 */
1273
1274 pcity = tile_city(punit_tile);
1275 if (NULL != pcity && (0 == punit->id || pcity->id == punit->homecity)) {
1276 /* I would have thought unhappiness should be taken into account
1277 * irrespectfully the city in which it will surface... -- GB */
1279 }
1280
1283
1285 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1286 punit_map = pf_map_new(&parameter);
1287
1288 if (MOVE_NONE == punit_class->adv.sea_move) {
1289 /* We need boat to move over sea. */
1290 ferryboat = unit_transport_get(punit);
1291
1292 /* First check if we can use the boat we are currently loaded to. */
1293 if (ferryboat == NULL || !is_boat_free(ait, ferryboat, punit, 0)) {
1294 /* No, we cannot control current boat */
1295 ferryboat = NULL;
1296 }
1297
1298 if (NULL == ferryboat) {
1299 /* Try to find new boat */
1300 ferryboat = player_unit_by_number(pplayer,
1301 aiferry_find_boat(ait, punit, 1, NULL));
1302 }
1303
1305 harbor = TRUE;
1306 }
1307 }
1308
1309 if (NULL != ferryboat) {
1310 boattype = unit_type_get(ferryboat);
1311 pft_fill_unit_overlap_param(&parameter, nmap, ferryboat);
1312 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1313 ferry_map = pf_map_new(&parameter);
1314 } else {
1316 if (NULL == boattype) {
1317 /* We pretend that we can have the simplest boat to stimulate tech. */
1319 }
1320 if (NULL != boattype && harbor) {
1321 /* Let's simulate a boat at 'punit' position. */
1323 punit_tile, pplayer);
1324 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1325 ferry_map = pf_map_new(&parameter);
1326 } else {
1327 ferry_map = NULL;
1328 }
1329 }
1330
1332
1334 /* For the virtual unit case, which is when we are called to evaluate
1335 * which units to build, we want to calculate in danger and which
1336 * players we want to make war with in the future. We do _not_ want
1337 * to do this when actually making attacks. */
1338 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1339 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1340 continue; /* Not an enemy. */
1341 }
1342
1343 city_list_iterate(aplayer->cities, acity) {
1344 struct tile *atile = city_tile(acity);
1345
1348 /* Can't attack this city. It is on land. */
1349 continue;
1350 }
1351
1352 if (handicap && !map_is_known(atile, pplayer)) {
1353 /* Can't see it */
1354 continue;
1355 }
1356
1358 go_by_boat = FALSE;
1359 move_time = pos.turn;
1360 } else if (NULL == ferry_map) {
1361 continue; /* Impossible to handle. */
1362 } else {
1363 struct tile *dest, *beach;
1364
1365 if (!find_beachhead(pplayer, ferry_map, atile, punit_type,
1366 &dest, &beach)) {
1367 continue; /* Impossible to go by boat. */
1368 }
1369 if (!pf_map_position(ferry_map, dest, &pos)) {
1371 continue;
1372 }
1373 move_time = pos.turn; /* Sailing time. */
1374 if (dest != beach) {
1375 move_time++; /* Land time. */
1376 }
1377 if (NULL != ferryboat && unit_tile(ferryboat) != punit_tile) {
1378 if (pf_map_position(punit_map, unit_tile(ferryboat), &pos)) {
1379 move_time += pos.turn; /* Time to reach the boat. */
1380 } else {
1381 continue; /* Cannot reach the boat. */
1382 }
1383 }
1384 go_by_boat = TRUE;
1385 }
1386
1391 } else {
1392 pdefender = NULL;
1393 vulnerability = 0;
1394 benefit = 0;
1395 }
1396
1397 if (1 < move_time) {
1399
1400 if (def_type) {
1404 if (v > vulnerability) {
1405 /* They can build a better defender! */
1406 vulnerability = v;
1408 }
1409 }
1410 }
1411
1413
1414 reserves = (acity_data->invasion.attack
1415 - unit_list_size(acity->tile->units));
1416
1417 if (punit->id == 0) {
1418 /* Real unit would add to reserves once built. */
1421 reserves++;
1422 } else {
1423 reserves += punit_type->move_rate;
1424 }
1425 }
1426
1427 if (0 < reserves && (can_occupy
1428 || 0 < acity_data->invasion.occupy)) {
1429 /* There are units able to occupy the city after all defenders
1430 * are killed! */
1431 benefit += acity_data->worth * reserves / 5;
1432 }
1433
1434 attack = attack_value + acity_data->attack;
1435 attack *= attack;
1436 /* Avoiding handling upkeep aggregation this way -- Syela */
1437
1438 /* AI was not sending enough reinforcements to totally wipe out a city
1439 * and conquer it in one turn.
1440 * This variable enables total carnage. -- Syela */
1442
1443 if (!can_occupy && NULL == pdefender) {
1444 /* Nothing there to bash and we can't occupy!
1445 * Not having this check caused warships yoyoing */
1446 want = 0;
1447 } else if (10 < move_time) {
1448 /* Too far! */
1449 want = 0;
1450 } else if (can_occupy && 0 == acity_data->invasion.occupy
1451 && (0 < acity_data->invasion.attack
1452 || victim_count == 0)) {
1453 /* Units able to occupy really needed there! */
1454 want = bcost * SHIELD_WEIGHTING;
1455 } else {
1456 want = kill_desire(benefit, attack, bcost + acity_data->bcost,
1458 }
1461 /* Build_cost of ferry. */
1462 needferry = (go_by_boat && NULL == ferryboat
1464 /* FIXME: add time to build the ferry? */
1466 want, MAX(1, move_time),
1468
1469 /* BEGIN STEAM-ENGINES-ARE-OUR-FRIENDS KLUGE. */
1470 if (0 >= want && 0 == punit->id && 0 >= best) {
1474 MAX(1, move_time),
1476
1477 if (bk_e > bk) {
1478 *pdest_tile = atile;
1479 if (NULL != pferrymap) {
1481 }
1482 if (NULL != pferryboat) {
1483 *pferryboat = go_by_boat ? ferryboat : NULL;
1484 }
1485 if (NULL != pboattype) {
1487 }
1488 if (NULL != pmove_time) {
1490 }
1491 goto_dest_tile = (go_by_boat && NULL != ferryboat
1492 ? unit_tile(ferryboat) : atile);
1493 bk = bk_e;
1494 }
1495 }
1496 /* END STEAM-ENGINES KLUGE */
1497
1498 if (0 != punit->id
1499 && NULL != ferryboat
1500 && punit_class->adv.sea_move == MOVE_NONE) {
1502 "%s(): with boat %s@(%d, %d) -> %s@(%d, %d)"
1503 " (go_by_boat = %d, move_time = %d, want = " ADV_WANT_PRINTF
1504 ", best = " ADV_WANT_PRINTF ")",
1505 __FUNCTION__, unit_rule_name(ferryboat),
1506 TILE_XY(unit_tile(ferryboat)), city_name_get(acity),
1507 TILE_XY(atile), go_by_boat, move_time, want, best);
1508 }
1509
1510 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1511 /* Yes, we like this target */
1512 best = want;
1513 *pdest_tile = atile;
1514 if (NULL != pferrymap) {
1516 }
1517 if (NULL != pferryboat) {
1518 *pferryboat = go_by_boat ? ferryboat : NULL;
1519 }
1520 if (NULL != pboattype) {
1522 }
1523 if (NULL != pmove_time) {
1525 }
1526 goto_dest_tile = (go_by_boat && NULL != ferryboat
1527 ? unit_tile(ferryboat) : atile);
1528 }
1530
1532 /* I'm not sure the following code is good but it seems to be adequate.
1533 * I am deliberately not adding ferryboat code to the unit_list_iterate.
1534 * -- Syela */
1536 struct tile *atile = unit_tile(aunit);
1537
1538 if (NULL != tile_city(atile)) {
1539 /* already dealt with it. */
1540 continue;
1541 }
1542
1543 if (handicap && !map_is_known(atile, pplayer)) {
1544 /* Can't see the target. */
1545 continue;
1546 }
1547
1549 && 0 == punit->id) {
1550 /* We will not build units just to chase caravans and
1551 * ambassadors. */
1552 continue;
1553 }
1554
1555 /* We have to assume the attack is diplomatically ok.
1556 * We cannot use can_player_attack_tile, because we might not
1557 * be at war with aplayer yet */
1559 || aunit != get_defender(nmap, punit, atile, NULL)) {
1560 /* We cannot attack it, or it is not the main defender. */
1561 continue;
1562 }
1563
1565 /* Cannot reach it. */
1566 continue;
1567 }
1568
1571
1572 move_time = pos.turn;
1573 if (10 < move_time) {
1574 /* Too far. */
1575 want = 0;
1576 } else {
1577 want = kill_desire(benefit, attack, bcost, vulnerability, 1);
1578 /* Take into account maintainance of the unit. */
1579 /* FIXME: Depends on the government. */
1580 want -= move_time * SHIELD_WEIGHTING;
1581 /* Take into account unhappiness
1582 * (costs 2 luxuries to compensate). */
1583 want -= (unhap ? 2 * move_time * TRADE_WEIGHTING : 0);
1584 }
1586 want, MAX(1, move_time), bcost_bal);
1587 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1588 best = want;
1589 *pdest_tile = atile;
1590 if (NULL != pferrymap) {
1591 *pferrymap = NULL;
1592 }
1593 if (NULL != pferryboat) {
1594 *pferryboat = NULL;
1595 }
1596 if (NULL != pboattype) {
1597 *pboattype = NULL;
1598 }
1599 if (NULL != pmove_time) {
1601 }
1603 }
1606
1607 if (NULL != ppath) {
1610 }
1611
1613 if (NULL != ferry_map
1614 && (NULL == pferrymap || *pferrymap != ferry_map)) {
1616 }
1617
1619
1620 return best;
1621}
1622
1623/**********************************************************************/
1632{
1633 struct pf_parameter parameter;
1634 struct pf_map *pfm;
1635 struct player *pplayer = unit_owner(punit);
1636 struct city *pcity, *best_city = NULL;
1637 int best = FC_INFINITY, cur;
1638 const struct civ_map *nmap = &(wld.map);
1639
1640 pft_fill_unit_parameter(&parameter, nmap, punit);
1641 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1642 pfm = pf_map_new(&parameter);
1643
1644 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
1645 if (move_cost > best) {
1646 /* We already found a better city. No need to continue. */
1647 break;
1648 }
1649
1650 pcity = tile_city(ptile);
1651 if (NULL == pcity || !pplayers_allied(pplayer, city_owner(pcity))) {
1652 continue;
1653 }
1654
1655 /* Score based on the move cost. */
1656 cur = move_cost;
1657
1658 /* Note the unit owner may be different from the city owner. */
1659 if (0 == get_unittype_bonus(unit_owner(punit), ptile,
1661 EFT_HP_REGEN)) {
1662 /* If we cannot regen fast our hit points here, let's make some
1663 * penalty. */
1664 cur *= 3;
1665 }
1666
1667 if (cur < best) {
1668 best_city = pcity;
1669 best = cur;
1670 }
1672
1674 return best_city;
1675}
1676
1677/**********************************************************************/
1683 struct player *pplayer,
1684 struct unit *punit)
1685{
1686 struct city *pc;
1687 bool only_continent = TRUE;
1688
1689 if (unit_transported(punit)) {
1690 /* If we are in transport, we can go to any continent.
1691 * Actually, we are not currently in a continent where to stay. */
1693 }
1694
1695 if ((pc = find_closest_city(unit_tile(punit), NULL, pplayer, FALSE,
1698 UNIT_LOG(LOG_DEBUG, punit, "Barbarian heading to conquer %s",
1699 city_name_get(pc));
1700 (void) dai_gothere(ait, pplayer, punit, pc->tile);
1701 } else {
1702 struct unit *ferry = NULL;
1703
1704 if (unit_transported(punit)) {
1705 ferry = unit_transport_get(punit);
1706
1707 /* We already are in a boat so it needs no
1708 * free capacity */
1709 if (!is_boat_free(ait, ferry, punit, 0)) {
1710 /* We cannot control our ferry. */
1711 ferry = NULL;
1712 }
1713 } else {
1714 /* We are not in a boat yet. Search for one. */
1716 if (is_boat_free(ait, aunit, punit, 1)
1718 ferry = aunit;
1719 break;
1720 }
1722 }
1723
1724 if (ferry) {
1725 UNIT_LOG(LOG_DEBUG, punit, "Barbarian sailing to conquer %s",
1726 city_name_get(pc));
1727 (void) aiferry_goto_amphibious(ait, ferry, punit, pc->tile);
1728 } else {
1729 /* This is not an error. Somebody else might be in charge
1730 * of the ferry. */
1731 UNIT_LOG(LOG_DEBUG, punit, "unable to find barbarian ferry");
1732 }
1733 }
1734 } else {
1735 UNIT_LOG(LOG_DEBUG, punit, "Barbarian find no target city");
1736 }
1737}
1738
1739/**********************************************************************/
1745static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
1746 struct unit *punit)
1747{
1748 struct tile *dest_tile;
1749 int id = punit->id;
1750 int ct = 10;
1751 struct city *pcity = NULL;
1752
1754
1755 /* Barbarians pillage, and might keep on doing that so they sometimes
1756 * even finish it. */
1757 if (punit->activity == ACTIVITY_PILLAGE && is_barbarian(pplayer)
1758 && fc_rand(2) == 1) {
1759 return;
1760 }
1761
1762 /* First find easy nearby enemies, anything better than pillage goes.
1763 * NB: We do not need to repeat dai_military_rampage, it is repeats itself
1764 * until it runs out of targets. */
1765 /* FIXME: 1. dai_military_rampage never checks if it should defend newly
1766 * conquered cities.
1767 * FIXME: 2. would be more convenient if it returned FALSE if we run out
1768 * of moves too.*/
1770 return; /* we died */
1771 }
1772
1773 if (punit->moves_left <= 0) {
1774 return;
1775 }
1776
1777 /* Main attack loop */
1778 do {
1779 struct tile *start_tile = unit_tile(punit);
1780 struct pf_path *path;
1781 struct unit *ferryboat;
1782
1783 /* Then find enemies the hard way */
1784 find_something_to_kill(ait, pplayer, punit, &dest_tile, &path,
1785 NULL, &ferryboat, NULL, NULL);
1786 if (!same_pos(unit_tile(punit), dest_tile)) {
1787 if (!is_tiles_adjacent(unit_tile(punit), dest_tile)
1788 || !can_unit_attack_tile(punit, NULL, dest_tile)) {
1789
1790 /* Adjacent and can't attack usually means we are not marines
1791 * and on a ferry. This fixes the problem (usually). */
1792 UNIT_LOG(LOG_DEBUG, punit, "mil att gothere -> (%d, %d)",
1793 TILE_XY(dest_tile));
1794
1795 /* Set ACTIVITY_GOTO more permanently than just inside
1796 * adv_follow_path(). This way other units will know we're
1797 * on our way even if we don't reach target yet. */
1798 punit->goto_tile = dest_tile;
1800 if (NULL != path && !adv_follow_path(punit, path, dest_tile)) {
1801 /* Died. */
1802 pf_path_destroy(path);
1803 return;
1804 }
1805 if (NULL != ferryboat) {
1806 /* Need a boat. */
1807 aiferry_gobyboat(ait, pplayer, punit, dest_tile, FALSE);
1808 pf_path_destroy(path);
1809 return;
1810 }
1811 if (0 >= punit->moves_left) {
1812 /* No moves left. */
1813 pf_path_destroy(path);
1814 return;
1815 }
1816
1817 /* Either we're adjacent or we sitting on the tile. We might be
1818 * sitting on the tile if the enemy that _was_ sitting there
1819 * attacked us and died _and_ we had enough movement to get there */
1820 if (same_pos(unit_tile(punit), dest_tile)) {
1821 UNIT_LOG(LOG_DEBUG, punit, "mil att made it -> (%d, %d)",
1822 TILE_XY(dest_tile));
1823 pf_path_destroy(path);
1824 break;
1825 }
1826 }
1827
1828 if (is_tiles_adjacent(unit_tile(punit), dest_tile)) {
1829 /* Close combat. fstk sometimes want us to attack an adjacent
1830 * enemy that rampage wouldn't */
1831 UNIT_LOG(LOG_DEBUG, punit, "mil att bash -> (%d, %d)",
1832 TILE_XY(dest_tile));
1833 if (!dai_unit_attack(ait, punit, dest_tile)) {
1834 /* Died */
1835 pf_path_destroy(path);
1836 return;
1837 }
1838 } else if (!same_pos(start_tile, unit_tile(punit))) {
1839 /* Got stuck. Possibly because of adjacency to an
1840 * enemy unit. Perhaps we are in luck and are now next to a
1841 * tempting target? Let's find out... */
1844 pf_path_destroy(path);
1845 return;
1846 }
1847
1848 } else {
1849 /* FIXME: This happens a bit too often! */
1850 UNIT_LOG(LOG_DEBUG, punit, "fstk didn't find us a worthy target!");
1851 /* No worthy enemies found, so abort loop */
1852 ct = 0;
1853 }
1854 pf_path_destroy(path);
1855
1856 ct--; /* infinite loops from railroads must be stopped */
1857 } while (punit->moves_left > 0 && ct > 0);
1858
1859 /* Cleanup phase */
1860 if (punit->moves_left == 0) {
1861 return;
1862 }
1864 if (pcity != NULL
1865 && (dai_is_ferry(punit, ait)
1866 || punit->hp < unit_type_get(punit)->hp * 0.50)) { /* WAG */
1867 /* Go somewhere safe */
1868 UNIT_LOG(LOG_DEBUG, punit, "heading to nearest safe house.");
1869 (void) dai_unit_goto(ait, punit, pcity->tile);
1870 } else if (!is_barbarian(pplayer)) {
1871 /* Nothing else to do, so try exploring. */
1872 switch (manage_auto_explorer(punit)) {
1873 case MR_DEATH:
1874 /* don't use punit! */
1875 return;
1876 case MR_OK:
1877 UNIT_LOG(LOG_DEBUG, punit, "nothing else to do, so exploring");
1878 break;
1879 default:
1880 UNIT_LOG(LOG_DEBUG, punit, "nothing to do - no more exploring either");
1881 break;
1882 };
1883 } else {
1884 /* You can still have some moves left here, but barbarians should
1885 not sit helplessly, but advance towards nearest known enemy city */
1886 UNIT_LOG(LOG_DEBUG, punit, "attack: barbarian");
1887 dai_military_attack_barbarian(ait, pplayer, punit);
1888 }
1889 if ((punit = game_unit_by_number(id)) && punit->moves_left > 0) {
1890 UNIT_LOG(LOG_DEBUG, punit, "attack: giving up unit to defense");
1891 dai_military_defend(ait, pplayer, punit);
1892 }
1893}
1894
1895/**********************************************************************/
1899static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
1900{
1901 bool alive = TRUE;
1902 int ferryboat = 0;
1903 struct pf_path *path_to_ferry = NULL;
1904
1905 UNIT_LOG(LOG_CARAVAN, punit, "requesting a boat!");
1906 ferryboat = aiferry_find_boat(ait, punit, 1, &path_to_ferry);
1907 /* going to meet the boat */
1908 if ((ferryboat <= 0)) {
1910 "in find_boat_for_unit cannot find any boats.");
1911 /* if we are undefended on the country side go to a city */
1913 if (current_city == NULL) {
1915 if (city_near != NULL) {
1917 }
1918 }
1919 } else {
1920 if (path_to_ferry != NULL) {
1922 /* Died. */
1925 alive = FALSE;
1926 } else {
1929 alive = TRUE;
1930 }
1931 }
1932 }
1933 return alive;
1934}
1935
1936/**********************************************************************/
1945 struct tile *ctile, struct tile *ptile)
1946{
1947 /* We usually have Inaccessible terrain, so not testing MOVE_FULL == */
1948 bool lm = MOVE_NONE != pclass->adv.land_move,
1949 sm = MOVE_NONE != pclass->adv.sea_move;
1950 struct civ_map *pmap = &(wld.map);
1951
1952 if (lm && sm) {
1953 return FALSE;
1954 }
1955
1956 /* We could use adjc_iterate() but likely often tiles are on the same
1957 * continent and it will be more time to find where they connect */
1960
1961 if (is_ocean_tile(atile) ? sm : lm) {
1962 iterate_outward(pmap, ptile, 1, btile) {
1963 if (tile_continent(btile) == acont) {
1964 return FALSE;
1965 }
1967 }
1969
1970 if (is_tiles_adjacent(ctile, ptile)) {
1971 return FALSE;
1972 }
1973
1974 return TRUE;
1975}
1976
1977/**********************************************************************/
1983static void dai_caravan_goto(struct ai_type *ait, struct player *pplayer,
1984 struct unit *punit,
1985 const struct city *dest_city,
1986 bool help_wonder,
1987 bool required_boat, bool request_boat)
1988{
1989 bool alive = TRUE;
1990 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
1991 const struct civ_map *nmap = &(wld.map);
1992
1994
1995 /* if we're not there yet, and we can move, move... */
1996 if (!same_pos(dest_city->tile, unit_tile(punit))
1997 && punit->moves_left != 0) {
1998 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) task %s going to %s in %s %s",
2002 help_wonder ? "help a wonder" : "trade", city_name_get(dest_city),
2003 required_boat ? "with a boat" : "");
2004 if (required_boat) {
2005 /* to trade with boat */
2006 if (request_boat) {
2007 /* Try to find new boat */
2009 } else {
2010 /* if we are not being transported then ask for a boat again */
2011 alive = TRUE;
2014 unit_tile(punit), dest_city->tile)) {
2016 }
2017 }
2018 if (alive) {
2019 /* FIXME: sometimes we get FALSE here just because
2020 * a trireme that we've boarded can't go over an ocean. */
2021 alive = dai_gothere(ait, pplayer, punit, dest_city->tile);
2022 }
2023 } else {
2024 /* to trade without boat */
2026 }
2027 }
2028
2029 /* if moving didn't kill us, and we got to the destination, handle it. */
2030 if (alive && real_map_distance(dest_city->tile, unit_tile(punit)) <= 1) {
2031 /* release the boat! */
2032 if (unit_transported(punit)) {
2034 }
2036 punit, dest_city)) {
2037 /*
2038 * We really don't want to just drop all caravans in immediately.
2039 * Instead, we want to aggregate enough caravans to build instantly.
2040 * -AJS, 990704
2041 */
2042 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) helps build wonder in %s",
2045 punit->id,
2048 unit_do_action(pplayer, punit->id, dest_city->id,
2049 0, "", ACTION_HELP_WONDER);
2051 punit, dest_city)) {
2052 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) creates trade route in %s",
2055 punit->id,
2058 unit_do_action(pplayer, punit->id, dest_city->id,
2059 0, "", ACTION_TRADE_ROUTE);
2061 punit, dest_city)) {
2062 /* Get the one time bonus. */
2063 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) enters marketplace of %s",
2066 punit->id,
2069 unit_do_action(pplayer, punit->id, dest_city->id,
2070 0, "", ACTION_MARKETPLACE);
2071 } else {
2072 enum log_level level = LOG_NORMAL;
2073
2074 if (help_wonder) {
2075 /* A Caravan ordered to help build wonder may arrive after
2076 * enough shields to build the wonder is produced. */
2078 }
2079
2081 "%s %s[%d](%d,%d) unable to trade with %s",
2084 punit->id,
2087 }
2088 }
2089}
2090
2091/**********************************************************************/
2095static void caravan_optimize_callback(const struct caravan_result *result,
2096 void *data)
2097{
2098 const struct unit *caravan = data;
2099
2100 log_base(LOG_CARAVAN3, "%s %s[%d](%d,%d) %s: %s %s worth %g",
2102 unit_rule_name(caravan),
2103 caravan->id,
2104 TILE_XY(unit_tile(caravan)),
2105 city_name_get(result->src),
2106 result->help_wonder ? "wonder in" : "trade to",
2107 city_name_get(result->dest),
2108 result->value);
2109}
2110
2111/**********************************************************************/
2115 struct unit *punit)
2116{
2117 struct tile *src = NULL, *dest = NULL, *src_home_city = NULL;
2118 struct city *phome_city = NULL;
2119 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2120
2121 if ((unit_data->task != AIUNIT_NONE)) {
2122 src = unit_tile(punit);
2124 if (phome_city != NULL) {
2126 }
2127 dest = punit->goto_tile;
2128
2129 if (src == NULL || dest == NULL) {
2130 return FALSE;
2131 }
2132
2133 /* If we have a home continent, and are not there.
2134 * (FIXME: well, why?)
2135 * (I guess because home continent is which we were supposed to leave,
2136 * not the target continent) */
2137 if (src_home_city != NULL
2139 return FALSE;
2140 }
2141
2142 if (!goto_is_sane(punit, dest)) {
2143 if (unit_transported(punit)) {
2144 /* If we're being transported */
2145 return FALSE;
2146 }
2147 if ((punit->birth_turn + 15 < game.info.turn)) {
2148 /* We are tired of waiting */
2150
2151 if (ferrys <= 0) {
2152 /* There are no ferrys available... give up */
2153 return TRUE;
2154 } else {
2155 if (punit->birth_turn + 20 < game.info.turn) {
2156 /* We are fed up! */
2157 return TRUE;
2158 }
2159 }
2160 }
2161 }
2162 }
2163
2164 return FALSE;
2165}
2166
2167/**********************************************************************/
2174 struct unit *punit)
2175{
2176 struct city *pcity = game_city_by_number(punit->homecity);
2177 Continent_id continent;
2178
2179 fc_assert(pcity != NULL);
2180
2181 if (unit_class_get(punit)->adv.ferry_types <= 0) {
2182 /* There is just no possible transporters. */
2183 return FALSE;
2184 }
2185 continent = tile_continent(pcity->tile);
2186
2187 /* Look for proper destination city at different continent. */
2188 city_list_iterate(pplayer->cities, acity) {
2189 if (can_cities_trade(pcity, acity)) {
2190 if (tile_continent(acity->tile) != continent) {
2191 return TRUE;
2192 }
2193 }
2195
2197 if (aplayer == pplayer || !aplayer->is_alive) {
2198 continue;
2199 }
2200 if (pplayers_allied(pplayer, aplayer)) {
2201 city_list_iterate(aplayer->cities, acity) {
2202 if (can_cities_trade(pcity, acity)) {
2203 if (tile_continent(acity->tile) != continent) {
2204 return TRUE;
2205 }
2206 }
2209 }
2210
2211 return FALSE;
2212}
2213
2214/**********************************************************************/
2218static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
2219{
2220 struct city *nearest = NULL;
2221 int min_dist = FC_INFINITY;
2222 struct tile *current_loc = unit_tile(punit);
2224 bool alive = TRUE;
2225
2227 struct tile *ctile = city_tile(pcity);
2228
2229 if (tile_continent(ctile) == continent) {
2231
2232 if (this_dist < min_dist) {
2234 nearest = pcity;
2235 }
2236 }
2238
2239 if (nearest != NULL) {
2241 if (alive && same_pos(unit_tile(punit), nearest->tile)) {
2243 }
2244 }
2245
2246 return alive;
2247}
2248
2249/**********************************************************************/
2255static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
2256 struct unit *punit)
2257{
2258 struct caravan_parameter parameter;
2259 struct caravan_result result;
2260 const struct city *homecity;
2261 const struct city *dest = NULL;
2262 struct unit_ai *unit_data;
2264 bool expect_boats = pclass->adv.ferry_types > 0;
2265 /* TODO: will pplayer have a boat for punit in a reasonable time? */
2266 bool help_wonder = FALSE;
2267 bool required_boat = FALSE;
2268 bool request_boat = FALSE;
2270 const struct civ_map *nmap = &(wld.map);
2271
2273
2277 /* we only want units that can establish trade, enter marketplace or
2278 * help build wonders */
2279 return;
2280 }
2281
2283
2284 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) task %s to (%d,%d)",
2289
2290 homecity = game_city_by_number(punit->homecity);
2291 if (homecity == NULL && unit_data->task == AIUNIT_TRADE) {
2293 return;
2294 }
2295 homecity = game_city_by_number(punit->homecity);
2296 if (homecity == NULL) {
2297 return;
2298 }
2299 }
2300
2301 if ((unit_data->task == AIUNIT_TRADE
2302 || unit_data->task == AIUNIT_WONDER)) {
2303 /* we are moving to our destination */
2304 /* we check to see if our current goal is feasible */
2306
2307 if ((city_dest == NULL)
2309 || (unit_data->task == AIUNIT_TRADE
2310 && !(can_cities_trade(homecity, city_dest)
2311 && can_establish_trade_route(homecity, city_dest)))
2312 || (unit_data->task == AIUNIT_WONDER
2313 /* Helping the (new) production is illegal. */
2315 || (unit_data->task == AIUNIT_TRADE
2320 punit, city_dest)))
2321 || (unit_data->task == AIUNIT_WONDER
2324 punit, city_dest))) {
2325 /* destination invalid! */
2327 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) destination invalid!",
2330 } else {
2331 /* destination valid, are we tired of waiting for a boat? */
2335 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) unit tired of waiting!",
2339 } else {
2340 dest = city_dest;
2341 help_wonder = (unit_data->task == AIUNIT_WONDER) ? TRUE : FALSE;
2342 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2344 }
2345 }
2346 }
2347
2348 if (unit_data->task == AIUNIT_NONE) {
2349 if (homecity == NULL) {
2350 /* FIXME: We shouldn't bother in getting homecity for
2351 * caravan that will then be used for wonder building. */
2353 return;
2354 }
2355 homecity = game_city_by_number(punit->homecity);
2356 if (homecity == NULL) {
2357 return;
2358 }
2359 }
2360
2362 /* Make more trade with allies than other peaceful nations
2363 * by considering only allies 50% of the time.
2364 * (the other 50% allies are still considered, but so are other nations) */
2365 if (fc_rand(2)) {
2366 /* Be optimistic about development of relations with no-contact and
2367 * cease-fire nations. */
2368 parameter.allow_foreign_trade = FTL_NONWAR;
2369 } else {
2370 parameter.allow_foreign_trade = FTL_ALLIED;
2371 }
2372
2375 parameter.callback_data = punit;
2376 }
2378 parameter.ignore_transit_time = TRUE;
2379 }
2382 parameter.ignore_transit_time = FALSE;
2383 }
2384 caravan_find_best_destination(nmap, punit, &parameter, &result,
2385 !has_handicap(pplayer, H_MAP));
2386 if (result.dest != NULL) {
2387 /* We did find a new destination for the unit */
2388 dest = result.dest;
2389 help_wonder = result.help_wonder;
2390 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2391 request_boat = required_boat;
2393 (help_wonder) ? AIUNIT_WONDER : AIUNIT_TRADE,
2394 dest->tile);
2395 } else {
2396 dest = NULL;
2397 }
2398 }
2399
2400 if (required_boat && !expect_boats) {
2401 /* Would require boat, but can't have them. Render destination invalid. */
2402 dest = NULL;
2403 }
2404
2405 if (dest != NULL) {
2406 dai_caravan_goto(ait, pplayer, punit, dest, help_wonder,
2407 required_boat, request_boat);
2408 return; /* that may have clobbered the unit */
2409 } else {
2410 /* We have nowhere to go! */
2411 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d), nothing to do!",
2415 /* Should we become a defensive unit? */
2416 }
2417}
2418
2419/**********************************************************************/
2424 struct unit *punit)
2425{
2426 struct player *pplayer = unit_owner(punit);
2427 struct city *pcity = tile_city(unit_tile(punit));
2428 struct city *safe = NULL;
2429 const struct unit_type *punittype = unit_type_get(punit);
2430 bool no_recovery;
2431
2433
2434 if (pcity != NULL) {
2435 /* Rest in the city until the hitpoints are recovered, but attempt
2436 * to protect city from attack (and be opportunistic too)*/
2439 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "recovering hit points.");
2440 } else {
2441 return; /* We died heroically defending our city */
2442 }
2443 } else {
2444 /* Goto to nearest city to recover hit points */
2445 /* Just before, check to see if we can occupy an undefended enemy city */
2448 return; /* Oops, we died */
2449 }
2450
2451 /* Find a city to stay and go there */
2453 if (safe) {
2454 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "going to %s to recover",
2456 if (!dai_unit_goto(ait, punit, safe->tile)) {
2457 log_base(LOGLEVEL_RECOVERY, "died trying to hide and recover");
2458 return;
2459 }
2460 } else {
2461 /* Oops */
2462 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "didn't find a city to recover in!");
2464 dai_military_attack(ait, pplayer, punit);
2465 return;
2466 }
2467 }
2468
2469 /* Is the unit still damaged? If true, and could recover hit points, do so.
2470 * Don't wait if would be losing hitpoints that way! */
2472 if (punit->hp == punittype->hp) {
2473 no_recovery = TRUE;
2474 } else {
2476
2477 if (gain < 0) {
2478 no_recovery = TRUE;
2479 } else if (gain == 0 && !punit->moved) {
2480 /* Isn't gaining even though has not moved. */
2481 no_recovery = TRUE;
2482 }
2483 }
2484 if (no_recovery) {
2485 /* We are ready to go out and kick ass again */
2486 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "ready to kick ass again!");
2488 return;
2489 } else {
2490 def_ai_unit_data(punit, ait)->done = TRUE; /* Sit tight */
2491 }
2492}
2493
2494/**********************************************************************/
2498void dai_manage_military(struct ai_type *ait, struct player *pplayer,
2499 struct unit *punit)
2500{
2501 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2502 int id = punit->id;
2503
2505
2506 /* "Escorting" aircraft should not do anything. They are activated
2507 * by their transport or charge. We do _NOT_ set them to 'done'
2508 * since they may need be activated once our charge moves. */
2509 if (unit_data->task == AIUNIT_ESCORT
2511 return;
2512 }
2513
2516 && has_handicap(pplayer, H_AWAY)) {
2517 /* Don't move sentried or fortified units controlled by a player
2518 * in away mode. */
2519 unit_data->done = TRUE;
2520 return;
2521 }
2522
2523 /* Since military units re-evaluate their actions every turn,
2524 we must make sure that previously reserved ferry is freed. */
2526
2528 /* Try hunting with this unit */
2529 if (dai_hunter_qualify(pplayer, punit)) {
2530 int result, sanity = punit->id;
2531
2532 UNIT_LOG(LOGLEVEL_HUNT, punit, "is qualified as hunter");
2533 result = dai_hunter_manage(ait, pplayer, punit);
2536 return; /* died */
2537 }
2538 if (result == -1) {
2539 (void) dai_hunter_manage(ait, pplayer, punit); /* More carnage */
2541 return;
2542 } else if (result >= 1) {
2544 return; /* Done moving */
2545 } else if (unit_data->task == AIUNIT_HUNTER) {
2546 /* This should be very rare */
2548 }
2549 } else if (unit_data->task == AIUNIT_HUNTER) {
2551 }
2553
2554 /* Do we have a specific job for this unit? If not, we default
2555 * to attack. */
2556 dai_military_findjob(ait, pplayer, punit);
2557
2558 switch (unit_data->task) {
2560 case AIUNIT_BUILD_CITY:
2561 fc_assert(FALSE); /* This is not the place for this role */
2562 break;
2563 case AIUNIT_DEFEND_HOME:
2565 dai_military_defend(ait, pplayer, punit);
2567 break;
2568 case AIUNIT_ATTACK:
2569 case AIUNIT_NONE:
2571 dai_military_attack(ait, pplayer, punit);
2573 break;
2574 case AIUNIT_ESCORT:
2576 dai_military_bodyguard(ait, pplayer, punit);
2578 break;
2579 case AIUNIT_EXPLORE:
2580 switch (manage_auto_explorer(punit)) {
2581 case MR_DEATH:
2582 /* don't use punit! */
2583 return;
2584 case MR_OK:
2585 UNIT_LOG(LOG_DEBUG, punit, "more exploring");
2586 break;
2587 default:
2588 UNIT_LOG(LOG_DEBUG, punit, "no more exploring either");
2589 break;
2590 };
2591 def_ai_unit_data(punit, ait)->done = (punit->moves_left <= 0);
2592 break;
2593 case AIUNIT_RECOVER:
2597 break;
2598 case AIUNIT_HUNTER:
2599 fc_assert(FALSE); /* dealt with above */
2600 break;
2601 default:
2603 }
2604
2605 /* If we are still alive, either sentry or fortify. */
2606 if ((punit = game_unit_by_number(id))) {
2608 struct city *pcity = tile_city(unit_tile(punit));
2609
2610 if (unit_list_find(unit_tile(punit)->units,
2611 unit_data->ferryboat)) {
2613 } else if (pcity || punit->activity == ACTIVITY_IDLE) {
2614 /* We do not need to fortify in cities - we fortify and sentry
2615 * according to home defense setup, for easy debugging. */
2616 if (!pcity || unit_data->task == AIUNIT_DEFEND_HOME) {
2620 }
2621 } else {
2623 }
2624 }
2625 }
2626}
2627
2628/**********************************************************************/
2631static void dai_manage_settler(struct ai_type *ait, struct player *pplayer,
2632 struct unit *punit)
2633{
2634 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2635
2637 unit_data->done = TRUE; /* we will manage this unit later... ugh */
2638 /* if BUILD_CITY must remain BUILD_CITY, otherwise turn into autosettler */
2639 if (unit_data->task == AIUNIT_NONE) {
2641 }
2642 return;
2643}
2644
2645/**********************************************************************/
2653void dai_manage_unit(struct ai_type *ait, struct player *pplayer,
2654 struct unit *punit)
2655{
2656 struct unit_ai *unit_data;
2657 struct unit *bodyguard = aiguard_guard_of(ait, punit);
2658 bool is_ferry = FALSE;
2659
2661
2662 /* Don't manage the unit if it is under human orders. */
2663 if (unit_has_orders(punit)) {
2665
2666 UNIT_LOG(LOG_VERBOSE, punit, "is under human orders, aborting AI control.");
2668 unit_data->done = TRUE;
2669 return;
2670 }
2671
2672 /* Check if we have lost our bodyguard. If we never had one, all
2673 * fine. If we had one and lost it, ask for a new one. */
2674 if (!bodyguard && aiguard_has_guard(ait, punit)) {
2675 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "lost bodyguard, asking for new");
2677 }
2678
2680
2681 if (punit->moves_left <= 0) {
2682 /* Can do nothing */
2683 unit_data->done = TRUE;
2684 return;
2685 }
2686
2687 is_ferry = dai_is_ferry(punit, ait);
2688
2691 dai_manage_diplomat(ait, pplayer, punit);
2693 return;
2696 dai_manage_settler(ait, pplayer, punit);
2697 return;
2702 dai_manage_caravan(ait, pplayer, punit);
2704 return;
2706 dai_manage_barbarian_leader(ait, pplayer, punit);
2707 return;
2710 dai_manage_paratrooper(ait, pplayer, punit);
2711 return;
2712 } else if (is_ferry && unit_data->task != AIUNIT_HUNTER) {
2714 dai_manage_ferryboat(ait, pplayer, punit);
2716 return;
2717 } else if (utype_fuel(unit_type_get(punit))
2718 && unit_data->task != AIUNIT_ESCORT) {
2720 dai_manage_airunit(ait, pplayer, punit);
2722 return;
2723 } else if (is_losing_hp(punit)) {
2724 /* This unit is losing hitpoints over time */
2725
2726 /* TODO: We can try using air-unit code for helicopters, just
2727 * pretend they have fuel = HP / 3 or something. */
2728 unit_data->done = TRUE; /* we did our best, which was ...
2729 nothing */
2730 return;
2731 } else if (!is_special_unit(punit)) {
2733 UNIT_LOG(LOG_DEBUG, punit, "recruit unit for the military");
2734 dai_manage_military(ait, pplayer, punit);
2736 return;
2737 } else {
2738 /* what else could this be? -- Syela */
2739 switch (manage_auto_explorer(punit)) {
2740 case MR_DEATH:
2741 /* don't use punit! */
2742 break;
2743 case MR_OK:
2744 UNIT_LOG(LOG_DEBUG, punit, "now exploring");
2745 break;
2746 default:
2747 UNIT_LOG(LOG_DEBUG, punit, "fell through all unit tasks, defending");
2749 dai_military_defend(ait, pplayer, punit);
2750 break;
2751 };
2752 return;
2753 }
2754}
2755
2756/**********************************************************************/
2762static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
2763{
2764 city_list_iterate(pplayer->cities, pcity) {
2765 /* The idea here is that we should never keep more than two
2766 * units in permanent defense. */
2767 int total_defense = 0;
2768 int total_attack = def_ai_city_data(pcity, ait)->danger;
2769 bool emergency = FALSE;
2770 int count = 0;
2774 + pcity->feel[CITIZEN_ANGRY][FEELING_NATIONALITY];
2775 int entertainers = 0;
2776 bool enough = FALSE;
2777
2779 if (get_specialist_output(pcity, sp, O_LUXURY) > 0) {
2780 entertainers += pcity->specialists[sp];
2781 }
2783
2784 martless_unhappy += entertainers; /* We want to use martial law instead
2785 * of entertainers. */
2786
2787 while (!enough
2790 && martless_unhappy > mart_each * count))) {
2791 int best_want = 0;
2792 struct unit *best = NULL;
2793 bool defense_needed = total_defense <= total_attack; /* Defense or martial */
2794
2795 unit_list_iterate(pcity->tile->units, punit) {
2796 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2797
2798 if ((unit_data->task == AIUNIT_NONE || emergency)
2799 && unit_data->task != AIUNIT_DEFEND_HOME
2800 && unit_owner(punit) == pplayer) {
2801 int want = assess_defense_unit(ait, pcity, punit, FALSE);
2802
2803 if (want > best_want) {
2804 best_want = want;
2805 best = punit;
2806 }
2807 }
2809
2810 if (best == NULL) {
2811 if (defense_needed) {
2812 /* Ooops - try to grab any unit as defender! */
2813 if (emergency) {
2814 CITY_LOG(LOG_DEBUG, pcity, "Not defended properly");
2815 break;
2816 }
2817 emergency = TRUE;
2818 } else {
2819 break;
2820 }
2821 } else {
2822 const struct unit_type *btype = unit_type_get(best);
2823
2824 if ((martless_unhappy < mart_each * count
2825 || count >= mart_max || mart_each <= 0)
2826 && ((count >= 2
2827 && btype->attack_strength > btype->defense_strength)
2828 || (count >= 4
2829 && btype->attack_strength == btype->defense_strength))) {
2830 /* In this case attack would be better defense than fortifying
2831 * to city. */
2832 enough = TRUE;
2833 } else {
2834 int loglevel = pcity->server.debug ? LOG_AI_TEST : LOG_DEBUG;
2835
2837 UNIT_LOG(loglevel, best, "Defending city");
2838 dai_unit_new_task(ait, best, AIUNIT_DEFEND_HOME, pcity->tile);
2839 count++;
2840 }
2841 }
2842 }
2843 CITY_LOG(LOG_DEBUG, pcity, "Evaluating defense: %d defense, %d incoming"
2844 ", %d defenders (out of %d)", total_defense, total_attack, count,
2845 unit_list_size(pcity->tile->units));
2847}
2848
2849/**********************************************************************/
2857void dai_manage_units(struct ai_type *ait, struct player *pplayer)
2858{
2860 dai_airlift(ait, pplayer);
2862
2863 /* Clear previous orders, if desirable, here. */
2864 unit_list_iterate(pplayer->units, punit) {
2865 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2866
2867 unit_data->done = FALSE;
2868 if (unit_data->task == AIUNIT_DEFEND_HOME) {
2870 }
2872
2873 /* Find and set city defenders first - figure out which units are
2874 * allowed to leave home. */
2875 dai_set_defenders(ait, pplayer);
2876
2879 && !def_ai_unit_data(punit, ait)->done) {
2880 /* Though it is usually the passenger who drives the transport,
2881 * the transporter is responsible for managing its passengers. */
2882 dai_manage_unit(ait, pplayer, punit);
2883 }
2885}
2886
2887/**********************************************************************/
2893 const struct city *pcity)
2894{
2895 const struct impr_type *impr_req = NULL;
2896 const struct req_context context = {
2897 .player = city_owner(pcity),
2898 .city = pcity,
2899 .tile = city_tile(pcity),
2900 .unittype = putype,
2901 };
2902
2903 requirement_vector_iterate(&putype->build_reqs, preq) {
2905 /* Already there. */
2906 continue;
2907 }
2909 city_owner(pcity), pcity)) {
2910 /* The unit type can't be built at all. */
2911 return NULL;
2912 }
2913 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2914 /* This is (one of) the building(s) required. */
2915 impr_req = preq->source.value.building;
2916 }
2918
2919 return impr_req;
2920}
2921
2922/**********************************************************************/
2927 const struct unit_type *base)
2928{
2929 /* This is the real function: */
2930 do {
2931 base = base->obsoleted_by;
2932 if (base == test) {
2933 return TRUE;
2934 }
2935 } while (base);
2936 return FALSE;
2937}
2938
2939/**********************************************************************/
2944static void dai_manage_barbarian_leader(struct ai_type *ait,
2945 struct player *pplayer,
2946 struct unit *leader)
2947{
2949 struct pf_parameter parameter;
2950 struct pf_map *pfm;
2951 struct pf_reverse_map *pfrm;
2952 struct unit *worst_danger;
2953 int move_cost, best_move_cost;
2954 int body_guards;
2955 bool alive = TRUE;
2956 const struct civ_map *nmap = &(wld.map);
2957
2959
2960 if (leader->moves_left == 0
2962 && 1 < unit_list_size(leader_tile->units))) {
2964
2965 return;
2966 }
2967
2968 if (is_boss_of_boat(ait, leader)) {
2969 /* We are in charge. Of course, since we are the leader...
2970 * But maybe somebody more militaristic should lead our ship to battle! */
2971
2972 /* First release boat from leaders lead */
2974
2978 && warrior->moves_left > 0) {
2979 /* This seems like a good warrior to lead us in to conquest! */
2980 dai_manage_unit(ait, pplayer, warrior);
2981
2982 /* If we reached our destination, ferryboat already called
2983 * ai_manage_unit() for leader. So no need to continue here.
2984 * Leader might even be dead.
2985 * If this return is removed, surronding unit_list_iterate()
2986 * has to be replaced with unit_list_iterate_safe()*/
2987 return;
2988 }
2990 }
2991
2992 /* If we are not in charge of the boat, continue as if we
2993 * were not in a boat - we may want to leave the ship now. */
2994
2995 /* Check the total number of units able to protect our leader. */
2996 body_guards = 0;
2997 unit_list_iterate(pplayer->units, punit) {
3000 body_guards++;
3001 }
3003
3004 if (0 < body_guards) {
3005 pft_fill_unit_parameter(&parameter, nmap, leader);
3006 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3007 pfm = pf_map_new(&parameter);
3008
3009 /* Find the closest body guard.
3010 * FIXME: maybe choose the strongest too? */
3011 pf_map_tiles_iterate(pfm, ptile, FALSE) {
3012 unit_list_iterate(ptile->units, punit) {
3013 if (unit_owner(punit) == pplayer
3016 struct pf_path *path = pf_map_path(pfm, ptile);
3017
3018 adv_follow_path(leader, path, ptile);
3019 pf_path_destroy(path);
3021 return;
3022 }
3025
3027 }
3028
3029 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader needs to flee");
3030
3031 /* Check for units we could fear. */
3032 pfrm = pf_reverse_map_new(pplayer, leader_tile, 3,
3033 !has_handicap(pplayer, H_MAP), &(wld.map));
3036
3037 players_iterate(other_player) {
3038 if (other_player == pplayer) {
3039 continue;
3040 }
3041
3042 unit_list_iterate(other_player->units, punit) {
3044 if (PF_IMPOSSIBLE_MC != move_cost && move_cost < best_move_cost) {
3045 best_move_cost = move_cost;
3047 }
3050
3052
3053 if (NULL == worst_danger) {
3055 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: no close enemy.");
3056 return;
3057 }
3058
3060 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3061 pfm = pf_map_new(&parameter);
3063
3064 /* Try to escape. */
3065 do {
3067
3068 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: moves left: %d.",
3069 leader->moves_left);
3070
3073 continue;
3074 }
3075
3076 move_cost = pf_map_move_cost(pfm, near_tile);
3077 if (PF_IMPOSSIBLE_MC != move_cost
3078 && move_cost > best_move_cost) {
3080 "Barbarian leader: safest is (%d, %d), safeness %d",
3082 best_move_cost = move_cost;
3084 }
3086
3087 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: fleeing to (%d, %d).",
3091 "Barbarian leader: reached the safest position.");
3094 return;
3095 }
3096
3098 if (alive) {
3100 /* Didn't move. No point to retry. */
3102 return;
3103 }
3105 }
3106 } while (alive && 0 < leader->moves_left);
3107
3109}
3110
3111/**********************************************************************/
3117void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile,
3118 struct unit *punit,
3119 enum override_bool *result)
3120{
3121 int a = 0, d, db;
3122 struct player *pplayer = unit_owner(punit);
3123 struct city *pcity = tile_city(ptile);
3124 int extras_bonus = 0;
3125
3126 if (is_human(pplayer)) {
3127 /* Use advisors code for humans. */
3128 return;
3129 }
3130
3131 if (pcity && pplayers_allied(city_owner(pcity), unit_owner(punit))
3132 && !is_non_allied_unit_tile(ptile, pplayer)) {
3133 /* We will be safe in a friendly city */
3134 *result = OVERRIDE_FALSE;
3135 return;
3136 }
3137
3138 /* Calculate how well we can defend at (x,y) */
3139 db = 10 + tile_terrain(ptile)->defense_bonus / 10;
3141
3142 db += (db * extras_bonus) / 100;
3144
3145 adjc_iterate(&(wld.map), ptile, ptile1) {
3146 if (has_handicap(pplayer, H_FOG)
3148 /* We cannot see danger at (ptile1) => assume there is none */
3149 continue;
3150 }
3151 unit_list_iterate(ptile1->units, enemy) {
3154 == ATT_OK)
3156 == ATT_OK)) {
3158 if ((a * a * 10) >= d) {
3159 /* The enemies combined strength is too big! */
3160 *result = OVERRIDE_TRUE;
3161 return;
3162 }
3163 }
3166
3167 *result = OVERRIDE_FALSE;
3168}
3169
3170/**********************************************************************/
3173static void update_simple_ai_types(void)
3174{
3175 int i = 0;
3176
3179
3182 && !(pclass->adv.land_move == MOVE_NONE
3185 && punittype->transport_capacity < 8) {
3187 i++;
3188 }
3190
3192}
3193
3194/**********************************************************************/
3198{
3199 /* TODO: remove the simple_ai_types cache or merge it with a general ai
3200 * cache; see the comment to struct unit_type *simple_ai_types at
3201 * the beginning of this file. */
3203
3205 struct unit_type_ai *utai = fc_malloc(sizeof(*utai));
3206
3207 utai->low_firepower = FALSE;
3208 utai->ferry = FALSE;
3209 utai->missile_platform = FALSE;
3210 utai->carries_occupiers = FALSE;
3211 utai->potential_charges = unit_type_list_new();
3212
3215
3218
3219 /* Confirm firepower */
3221 if (pbonus->type == CBONUS_LOW_FIREPOWER) {
3223 if (utype_has_flag(penemy, pbonus->flag)) {
3224 struct unit_type_ai *utai = utype_ai_data(penemy, ait);
3225
3226 utai->low_firepower = TRUE;
3227 }
3229 }
3231
3232 /* Consider potential cargo */
3233 if (punittype->transport_capacity > 0) {
3234 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3235
3238
3241 utai->missile_platform = TRUE;
3242 } else if (pclass->adv.sea_move != MOVE_NONE
3243 && pcargo->adv.land_move != MOVE_NONE) {
3244 if (pcargo->adv.sea_move != MOVE_FULL) {
3245 utai->ferry = TRUE;
3246 } else {
3247 if (0 != utype_fuel(pctype)) {
3248 utai->ferry = TRUE;
3249 }
3250 }
3251 }
3252
3254 utai->carries_occupiers = TRUE;
3255 }
3256 }
3258 }
3259
3260 /* Consider potential charges */
3263
3264 if (0 < utype_fuel(punittype)
3265 && (0 == utype_fuel(pcharge)
3267 continue;
3268 }
3269
3270 unit_class_list_iterate(pclass->cache.subset_movers, chgcls) {
3271 if (chgcls == utype_class(pcharge)) {
3273 }
3275
3277 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3278 unit_type_list_append(utai->potential_charges, pcharge);
3279 }
3280
3283}
3284
3285/**********************************************************************/
3289{
3291 struct unit_type_ai *utai = utype_ai_data(ptype, ait);
3292
3293 if (utai == NULL) {
3294 continue;
3295 }
3297
3298 unit_type_list_destroy(utai->potential_charges);
3299 free(utai);
3301}
3302
3303/**********************************************************************/
3306void dai_unit_init(struct ai_type *ait, struct unit *punit)
3307{
3308 /* Make sure that contents of unit_ai structure are correctly initialized,
3309 * if you ever allocate it by some other mean than fc_calloc() */
3310 struct unit_ai *unit_data = fc_calloc(1, sizeof(struct unit_ai));
3311
3312 unit_data->done = FALSE;
3313 unit_data->cur_pos = NULL;
3314 unit_data->prev_pos = NULL;
3315 unit_data->target = 0;
3316 BV_CLR_ALL(unit_data->hunted);
3317 unit_data->ferryboat = 0;
3318 unit_data->passenger = 0;
3319 unit_data->bodyguard = 0;
3320 unit_data->charge = 0;
3321
3323}
3324
3325/**********************************************************************/
3328void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
3329{
3330 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3331
3333
3334 BV_CLR_ALL(unit_data->hunted);
3335}
3336
3337/**********************************************************************/
3340void dai_unit_close(struct ai_type *ait, struct unit *punit)
3341{
3342 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3343
3345
3348
3349 if (unit_data != NULL) {
3352 }
3353}
3354
3355/**********************************************************************/
3358void dai_unit_save(struct ai_type *ait, const char *aitstr,
3359 struct section_file *file,
3360 const struct unit *punit, const char *unitstr)
3361{
3362 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3363
3364 secfile_insert_int(file, unit_data->passenger, "%s.%spassenger",
3365 unitstr, aitstr);
3366 secfile_insert_int(file, unit_data->ferryboat, "%s.%sferryboat",
3367 unitstr, aitstr);
3368 secfile_insert_int(file, unit_data->charge, "%s.%scharge",
3369 unitstr, aitstr);
3370 secfile_insert_int(file, unit_data->bodyguard, "%s.%sbodyguard",
3371 unitstr, aitstr);
3372}
3373
3374/**********************************************************************/
3377void dai_unit_load(struct ai_type *ait, const char *aitstr,
3378 const struct section_file *file,
3379 struct unit *punit, const char *unitstr)
3380{
3381 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3382
3383 unit_data->passenger
3384 = secfile_lookup_int_default(file, 0, "%s.%spassenger",
3385 unitstr, aitstr);
3386 unit_data->ferryboat
3387 = secfile_lookup_int_default(file, 0, "%s.%sferryboat",
3388 unitstr, aitstr);
3389 unit_data->charge
3390 = secfile_lookup_int_default(file, 0, "%s.%scharge",
3391 unitstr, aitstr);
3392 unit_data->bodyguard
3393 = secfile_lookup_int_default(file, 0, "%s.%sbodyguard",
3394 unitstr, aitstr);
3395}
3396
3402
3403/**********************************************************************/
3406static bool role_unit_cb(struct unit_type *ptype, void *data)
3407{
3408 struct role_unit_cb_data *cb_data = (struct role_unit_cb_data *)data;
3410
3411 if ((cb_data->tc == TC_LAND && pclass->adv.land_move == MOVE_NONE)
3412 || (cb_data->tc == TC_OCEAN && pclass->adv.sea_move == MOVE_NONE)) {
3413 return FALSE;
3414 }
3415
3416 if (cb_data->build_city == NULL
3417 || can_city_build_unit_now(cb_data->build_city, ptype)) {
3418 return TRUE;
3419 }
3420
3421 return FALSE;
3422}
3423
3424/**********************************************************************/
3428 enum terrain_class tc)
3429{
3430 struct role_unit_cb_data cb_data = { .build_city = pcity, .tc = tc };
3431
3433}
3434
3435/**********************************************************************/
3438bool dai_unit_can_strike_my_unit(const struct unit *attacker,
3439 const struct unit *defender)
3440{
3441 struct pf_parameter parameter;
3442 struct pf_map *pfm;
3443 const struct tile *ptarget = unit_tile(defender);
3444 int max_move_cost = attacker->moves_left;
3445 bool able_to_strike = FALSE;
3446 const struct civ_map *nmap = &(wld.map);
3447
3448 pft_fill_unit_parameter(&parameter, nmap, attacker);
3449 parameter.omniscience = !has_handicap(unit_owner(defender), H_MAP);
3450 pfm = pf_map_new(&parameter);
3451
3452 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
3453 if (move_cost > max_move_cost) {
3454 break;
3455 }
3456
3457 if (ptile == ptarget) {
3459 break;
3460 }
3462
3464
3465 return able_to_strike;
3466}
3467
3468/**********************************************************************/
3471void dai_switch_to_explore(struct ai_type *ait, struct unit *punit,
3472 struct tile *target, enum override_bool *allow)
3473{
3474 struct unit_ai *udata = def_ai_unit_data(punit, ait);
3475
3476 if (udata->task != AIUNIT_NONE && udata->task != AIUNIT_EXPLORE) {
3478
3479 return;
3480 }
3481}
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:3832
static struct action * action_by_number(action_id act_id)
Definition actions.h:633
#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:410
int adv_unit_def_rating_basic(const struct unit *punit)
Definition advgoto.c:401
int adv_unittype_att_rating(const struct unit_type *punittype, int veteran, int moves_left, int hp)
Definition advgoto.c:380
int adv_unit_att_rating(const struct unit *punit)
Definition advgoto.c:391
int adv_could_unit_move_to_tile(struct unit *punit, struct tile *dest_tile)
Definition advgoto.c:354
bool adv_unit_execute_path(struct unit *punit, struct pf_path *path)
Definition advgoto.c:82
#define POWER_DIVIDER
Definition advtools.h:32
void dai_manage_airunit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition aiair.c:486
void dai_manage_diplomat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition aidiplomat.c:722
bool aiferry_gobyboat(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile, bool with_bodyguard)
Definition aiferry.c:764
int aiferry_find_boat(struct ai_type *ait, struct unit *punit, int cap, struct pf_path **path)
Definition aiferry.c:495
void dai_manage_ferryboat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition aiferry.c:1113
bool is_boat_free(struct ai_type *ait, struct unit *boat, struct unit *punit, int cap)
Definition aiferry.c:438
bool aiferry_goto_amphibious(struct ai_type *ait, struct unit *ferry, struct unit *passenger, struct tile *ptile)
Definition aiferry.c:730
bool is_boss_of_boat(struct ai_type *ait, struct unit *punit)
Definition aiferry.c:471
int aiferry_avail_boats(struct ai_type *ait, struct player *pplayer)
Definition aiferry.c:353
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Definition aiferry.c:159
void aiferry_clear_boat(struct ai_type *ait, struct unit *punit)
Definition aiferry.c:251
struct city * aiguard_charge_city(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:289
void aiguard_clear_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:117
void aiguard_request_guard(struct ai_type *ait, struct unit *punit)
Definition aiguard.c:227
bool aiguard_has_guard(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:259
void aiguard_assign_guard_unit(struct ai_type *ait, struct unit *charge, struct unit *guard)
Definition aiguard.c:173
bool aiguard_wanted(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:241
void aiguard_assign_guard_city(struct ai_type *ait, struct city *charge, struct unit *guard)
Definition aiguard.c:196
struct unit * aiguard_charge_unit(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:279
void aiguard_clear_guard(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:146
struct unit * aiguard_guard_of(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:269
bool aiguard_has_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:250
void aiguard_update_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:299
#define CHECK_GUARD(ait, guard)
Definition aiguard.h:21
bool dai_hunter_qualify(struct player *pplayer, struct unit *punit)
Definition aihunt.c:288
int dai_hunter_manage(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition aihunt.c:436
void dai_manage_paratrooper(struct ai_type *ait, struct player *pplayer, struct unit *punit)
int stack_cost(struct unit *pattacker, struct unit *pdefender)
Definition aitools.c:1280
bool goto_is_sane(struct unit *punit, struct tile *ptile)
Definition aitools.c:428
bool dai_assess_military_unhappiness(struct city *pcity)
Definition aitools.c:1415
bool dai_unit_attack(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Definition aitools.c:816
bool dai_unit_goto(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Definition aitools.c:604
void dai_unit_new_task(struct ai_type *ait, struct unit *punit, enum ai_unit_task task, struct tile *ptile)
Definition aitools.c:642
bool dai_unit_make_homecity(struct unit *punit, struct city *pcity)
Definition aitools.c:754
const char * dai_unit_task_rule_name(const enum ai_unit_task task)
Definition aitools.c:77
adv_want military_amortize(struct player *pplayer, struct city *pcity, adv_want value, int delay, int build_cost)
Definition aitools.c:118
bool dai_gothere(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile)
Definition aitools.c:243
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:95
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:748
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:1852
const char * city_name_get(const struct city *pcity)
Definition city.c:1128
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:803
bool can_city_build_unit_now(const struct city *pcity, const struct unit_type *punittype)
Definition city.c:945
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
@ CITIZEN_ANGRY
Definition city.h:271
@ CITIZEN_UNHAPPY
Definition city.h:270
#define city_owner(_pcity_)
Definition city.h:563
#define city_list_iterate_end
Definition city.h:510
@ FEELING_NATIONALITY
Definition city.h:282
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:851
double unit_win_chance(const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender, const struct action *paction)
Definition combat.c:478
bool is_stack_vulnerable(const struct tile *ptile)
Definition combat.c:987
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:710
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:256
int get_total_defense_power(const struct unit *attacker, const struct unit *defender)
Definition combat.c:765
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:122
struct unit * get_defender(const struct civ_map *nmap, const struct unit *attacker, const struct tile *ptile, const struct action *paction)
Definition combat.c:834
bool can_unit_attack_tile(const struct unit *punit, const struct action *paction, const struct tile *dest_tile)
Definition combat.c:311
int base_get_defense_power(const struct unit *punit)
Definition combat.c:569
@ ATT_OK
Definition combat.h:35
char * incite_cost
Definition comments.c:74
bool dai_can_requirement_be_met_in_city(const struct requirement *preq, const struct player *pplayer, const struct city *pcity)
Definition daieffects.c:782
#define BODYGUARD_LOG(ait, loglevel, punit, msg,...)
Definition dailog.h:69
struct unit_type * dai_choose_defender_versus(struct city *pcity, struct unit *attacker)
Definition daimilitary.c:90
int assess_defense_quadratic(struct ai_type *ait, struct city *pcity)
int assess_defense_unit(struct ai_type *ait, struct city *pcity, struct unit *punit, bool igwall)
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
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:3358
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
void dai_manage_military(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2498
static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2218
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:1983
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:699
void dai_units_ruleset_init(struct ai_type *ait)
Definition daiunit.c:3197
#define LOG_CARAVAN
Definition daiunit.c:88
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:1682
static void invasion_funct(struct ai_type *ait, struct unit *punit, bool dest, int radius, int which)
Definition daiunit.c:1018
static void update_simple_ai_types(void)
Definition daiunit.c:3173
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Definition daiunit.c:2892
void dai_switch_to_explore(struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
Definition daiunit.c:3471
static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:1899
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:126
static void dai_airlift(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:169
int build_cost_balanced(const struct unit_type *punittype)
Definition daiunit.c:249
bool dai_unit_can_strike_my_unit(const struct unit *attacker, const struct unit *defender)
Definition daiunit.c:3438
bool is_on_unit_upgrade_path(const struct unit_type *test, const struct unit_type *base)
Definition daiunit.c:2926
static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:633
static void single_invader(struct ai_city *city_data, const struct unit_type *utype, int which)
Definition daiunit.c:990
#define LOG_CARAVAN3
Definition daiunit.c:90
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2857
adv_want look_for_charge(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity)
Definition daiunit.c:714
static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2255
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:1135
static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2762
void dai_manage_unit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2653
static bool role_unit_cb(struct unit_type *ptype, void *data)
Definition daiunit.c:3406
static void dai_military_attack(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1745
void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3328
static void dai_manage_settler(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2631
static int avg_benefit(int benefit, int loss, double chance)
Definition daiunit.c:365
#define LOG_CARAVAN2
Definition daiunit.c:89
static struct city * find_neediest_airlift_city(struct ai_type *ait, const struct player *pplayer)
Definition daiunit.c:138
static int unit_def_rating(const struct unit *attacker, const struct unit *defender)
Definition daiunit.c:277
static bool dai_is_unit_tired_waiting_boat(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2114
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:834
struct city * find_nearest_safe_city(struct unit *punit)
Definition daiunit.c:1631
static void dai_manage_barbarian_leader(struct ai_type *ait, struct player *pplayer, struct unit *leader)
Definition daiunit.c:2944
static void dai_manage_hitpoint_recovery(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2423
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:521
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:3306
void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
Definition daiunit.c:3117
static void dai_military_findjob(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:852
bool uclass_need_trans_between(struct unit_class *pclass, struct tile *ctile, struct tile *ptile)
Definition daiunit.c:1944
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:3377
static void caravan_optimize_callback(const struct caravan_result *result, void *data)
Definition daiunit.c:2095
#define LOGLEVEL_RECOVERY
Definition daiunit.c:87
void dai_unit_close(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3340
void dai_units_ruleset_close(struct ai_type *ait)
Definition daiunit.c:3288
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Definition daiunit.c:3427
bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map, struct tile *dest_tile, const struct unit_type *cargo_type, struct tile **ferry_dest, struct tile **beachhead_tile)
Definition daiunit.c:1060
static void dai_military_defend(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:944
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:600
static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer, struct unit *punit)
Definition daiunit.c:2173
#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_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_AUTO_SETTLER
Definition daiunit.h:27
@ 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:3009
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2931
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 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:846
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:1035
bool unit_can_enter_hut(const struct unit *punit, const struct tile *ptile)
Definition extras.c:712
float adv_want
Definition fc_types.h:1355
@ RPT_CERTAIN
Definition fc_types.h:704
@ AUT_AUTO_SETTLER
Definition fc_types.h:373
#define ADV_WANT_PRINTF
Definition fc_types.h:1356
@ O_LUXURY
Definition fc_types.h:101
signed short Continent_id
Definition fc_types.h:375
override_bool
Definition fc_types.h:94
@ OVERRIDE_TRUE
Definition fc_types.h:94
@ OVERRIDE_FALSE
Definition fc_types.h:94
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:191
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_do_output_for_level(level)
Definition log.h:89
#define log_base(level, message,...)
Definition log.h:94
log_level
Definition log.h:28
@ LOG_DEBUG
Definition log.h:34
@ LOG_NORMAL
Definition log.h:32
@ LOG_VERBOSE
Definition log.h:33
bool is_tiles_adjacent(const struct tile *tile0, const struct tile *tile1)
Definition map.c:931
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:940
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:630
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:654
#define iterate_outward(nmap, start_tile, max_dist, itr_tile)
Definition map.h:367
#define iterate_outward_end
Definition map.h:371
#define adjc_iterate_end
Definition map.h:433
static int index_to_map_pos_y(int mindex)
Definition map.h:724
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:391
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:428
#define square_iterate_end
Definition map.h:394
static int index_to_map_pos_x(int mindex)
Definition map.h:715
#define pmap(_tile)
bool map_is_known(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:894
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:920
#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:319
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:331
int unit_move_rate(const struct unit *punit)
Definition movement.c:90
bool can_attack_from_non_native(const struct unit_type *utype)
Definition movement.c:227
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:491
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:868
bool can_attack_non_native(const struct unit_type *utype)
Definition movement.c:214
#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:84
@ MR_OK
Definition movement.h:35
@ MR_DEATH
Definition movement.h:36
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
struct nation_type * nation_of_unit(const struct unit *punit)
Definition nation.c:463
void pf_path_destroy(struct pf_path *path)
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
struct pf_path * pf_map_path(struct pf_map *pfm, struct tile *ptile)
struct pf_reverse_map * pf_reverse_map_new(const struct player *pplayer, struct tile *target_tile, int max_turns, bool omniscient, const struct civ_map *nmap)
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)
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:840
void pft_fill_unit_attack_param(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:949
void pft_fill_unit_overlap_param(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:891
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:876
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1229
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1405
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
static bool is_barbarian(const struct player *pplayer)
Definition player.h:489
#define is_human(plr)
Definition player.h:229
#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 player *other_player, const struct requirement *req, const enum req_problem_type prob_type)
#define requirement_vector_iterate_end
#define requirement_vector_iterate(req_vec, preq)
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
int get_specialist_output(const struct city *pcity, Specialist_type_id sp, Output_type_id otype)
Definition specialist.c:217
#define specialist_type_iterate_end
Definition specialist.h:79
#define specialist_type_iterate(sp)
Definition specialist.h:73
#define CITY_LOG(loglevel, pcity, msg,...)
Definition srv_log.h:83
#define LOGLEVEL_HUNT
Definition srv_log.h:35
#define UNIT_LOG(loglevel, punit, msg,...)
Definition srv_log.h:98
#define LOG_AI_TEST
Definition srv_log.h:38
@ AIT_RECOVER
Definition srv_log.h:68
@ AIT_FSTK
Definition srv_log.h:54
@ AIT_CARAVAN
Definition srv_log.h:56
@ AIT_RAMPAGE
Definition srv_log.h:71
@ AIT_ATTACK
Definition srv_log.h:66
@ AIT_HUNTER
Definition srv_log.h:57
@ AIT_BODYGUARD
Definition srv_log.h:69
@ AIT_DEFENDERS
Definition srv_log.h:55
@ AIT_AIRUNIT
Definition srv_log.h:60
@ AIT_FERRY
Definition srv_log.h:70
@ AIT_MILITARY
Definition srv_log.h:67
@ AIT_DIPLOMAT
Definition srv_log.h:59
@ AIT_AIRLIFT
Definition srv_log.h:58
@ TIMER_STOP
Definition srv_log.h:76
@ TIMER_START
Definition srv_log.h:76
#define TIMING_LOG(timer, activity)
Definition srv_log.h:125
#define LOGLEVEL_BODYGUARD
Definition srv_log.h:30
unsigned int danger
Definition daicity.h:47
int bcost
Definition daicity.h:44
Definition ai.h:50
void(* callback)(const struct caravan_result *result, void *data)
Definition caravan.h:124
enum foreign_trade_limit allow_foreign_trade
Definition caravan.h:99
bool ignore_transit_time
Definition caravan.h:108
void * callback_data
Definition caravan.h:125
const struct city * src
Definition caravan.h:52
const struct city * dest
Definition caravan.h:53
bool help_wonder
Definition caravan.h:57
double value
Definition caravan.h:56
Definition city.h:320
int id
Definition city.h:326
struct adv_city * adv
Definition city.h:452
struct tile * tile
Definition city.h:322
struct packet_game_info info
Definition game.h:89
int occupychance
Definition game.h:170
struct civ_game::@31::@35 server
struct city_list * cities
Definition player.h:279
struct unit_list * units
Definition player.h:280
bool is_alive
Definition player.h:266
const struct player * player
enum terrain_class tc
Definition daiunit.c:3399
struct city * build_city
Definition daiunit.c:3400
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:558
struct veteran_system * veteran
Definition unittype.h:544
int move_rate
Definition unittype.h:517
Definition unit.h:138
enum unit_activity activity
Definition unit.h:157
int moves_left
Definition unit.h:150
int id
Definition unit.h:145
bool moved
Definition unit.h:173
int hp
Definition unit.h:151
struct tile * tile
Definition unit.h:140
struct unit_adv * adv
Definition unit.h:236
int homecity
Definition unit.h:146
int birth_turn
Definition unit.h:207
struct tile * goto_tile
Definition unit.h:155
int veteran
Definition unit.h:152
struct player * owner
Definition unit.h:143
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:612
#define is_ocean_tile(ptile)
Definition terrain.h:303
int tile_extras_defense_bonus(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:233
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:88
#define tile_terrain(_tile)
Definition tile.h:110
#define TILE_XY(ptile)
Definition tile.h:43
#define tile_continent(_tile)
Definition tile.h:92
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
bool can_establish_trade_route(const struct city *pc1, const struct city *pc2)
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2286
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2366
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2224
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2437
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2179
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2637
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:361
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:371
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:299
bool unit_can_airlift_to(const struct unit *punit, const struct city *pdest_city)
Definition unit.c:186
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:865
bool is_special_unit(const struct unit *punit)
Definition unit.c:352
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2421
bool is_guard_unit(const struct unit *punit)
Definition unit.c:342
bool unit_has_orders(const struct unit *punit)
Definition unit.c:205
#define unit_tile(_pu)
Definition unit.h:390
#define unit_cargo_iterate_end
Definition unit.h:565
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:562
#define CHECK_UNIT(punit)
Definition unit.h:270
#define unit_owner(_pu)
Definition unit.h:389
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.h:425
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Definition unithand.c:6468
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:3291
bool unit_server_side_agent_set(struct player *pplayer, struct unit *punit, enum server_side_agent agent)
Definition unithand.c:6368
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:1191
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
struct unit_type * role_units_iterate_backwards(int role, role_unit_callback cb, void *data)
Definition unittype.c:2236
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2253
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1587
bool utype_has_role(const struct unit_type *punittype, int role)
Definition unittype.c:199
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1468
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1484
struct unit_type * best_role_unit_for_player(const struct player *pplayer, int role)
Definition unittype.c:2297
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2498
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:387
bool unit_has_type_role(const struct unit *punit, enum unit_role_id role)
Definition unittype.c:208
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:190
bool unit_can_take_over(const struct unit *punit)
Definition unittype.c:264
void * utype_ai_data(const struct unit_type *ptype, const struct ai_type *ai)
Definition unittype.c:2725
bool utype_can_take_over(const struct unit_type *punittype)
Definition unittype.c:276
void utype_set_ai_data(struct unit_type *ptype, const struct ai_type *ai, void *data)
Definition unittype.c:2733
int utype_build_shield_cost(const struct city *pcity, const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1438
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:443
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:371
#define utype_class(_t_)
Definition unittype.h:749
#define utype_fuel(ptype)
Definition unittype.h:836
#define combat_bonus_list_iterate_end
Definition unittype.h:482
#define combat_bonus_list_iterate(bonuslist, pbonus)
Definition unittype.h:480
@ MOVE_FULL
Definition unittype.h:144
@ MOVE_NONE
Definition unittype.h:144
#define unit_type_list_iterate(utype_list, ptype)
Definition unittype.h:937
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:617
#define unit_class_list_iterate(uclass_list, pclass)
Definition unittype.h:929
#define unit_type_iterate(_p)
Definition unittype.h:852
#define unit_type_list_iterate_end
Definition unittype.h:939
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:859
#define unit_class_list_iterate_end
Definition unittype.h:931