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, const struct civ_map *nmap,
102 struct player *pplayer, struct unit *punit);
103static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
104 struct unit *punit);
105static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
106 struct unit *punit);
107
108static bool unit_role_defender(const struct unit_type *punittype);
109static int unit_def_rating_squared(const struct unit *punit,
110 const struct unit *pdef);
111
112/*
113 * Cached values. Updated by update_simple_ai_types.
114 *
115 * This a hack to enable emulation of old loops previously hardwired
116 * as
117 * for (i = U_WARRIORS; i <= U_BATTLESHIP; i++)
118 *
119 * (Could probably just adjust the loops themselves fairly simply,
120 * but this is safer for regression testing.)
121 *
122 * Not dealing with planes yet.
123 *
124 * Terminated by NULL.
125 */
127
128/**********************************************************************/
138static struct city *find_neediest_airlift_city(struct ai_type *ait,
139 const struct player *pplayer)
140{
141 struct city *neediest_city = NULL;
142 int most_danger = 0;
143 int most_urgent = 0;
144
145 city_list_iterate(pplayer->cities, pcity) {
146 struct ai_city *city_data = def_ai_city_data(pcity, ait);
147
148 if (pcity->airlift) {
149 if (city_data->urgency > most_urgent) {
150 most_urgent = city_data->urgency;
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 const struct civ_map *nmap = &(wld.map);
175
176 do {
178 comparison = 0;
179 transported = NULL;
180
181 if (!most_needed) {
182 return;
183 }
184
185 unit_list_iterate(pplayer->units, punit) {
186 struct tile *ptile = (unit_tile(punit));
187 struct city *pcity = tile_city(ptile);
188
189 if (pcity) {
190 struct ai_city *city_data = def_ai_city_data(pcity, ait);
191 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
192 const struct unit_type *ptype = unit_type_get(punit);
193
194 if (city_data->urgency == 0
197 && DEFENSE_POWER(ptype) > 2
198 && (unit_data->task == AIUNIT_NONE
199 || unit_data->task == AIUNIT_DEFEND_HOME)
200 && IS_ATTACKER(ptype)) {
201 comparison = city_data->danger;
202 transported = punit;
203 }
204 }
206 if (!transported) {
207 return;
208 }
209 UNIT_LOG(LOG_DEBUG, transported, "airlifted to defend %s",
211 unit_do_action(pplayer, transported->id, most_needed->id,
212 0, "", ACTION_AIRLIFT);
213 } while (TRUE);
214}
215
216/**********************************************************************/
222static bool has_defense(struct city *pcity)
223{
224 struct tile *ptile = city_tile(pcity);
225
226 unit_list_iterate(ptile->units, punit) {
228 && punit->hp != 0) {
230
231 if (pclass->non_native_def_pct > 0
232 || is_native_tile_to_class(pclass, ptile)) {
233 return TRUE;
234 }
235 }
236 }
238
239 return FALSE;
240}
241
242/**********************************************************************/
251{
252 return 2 * utype_build_shield_cost_base(punittype) * punittype->attack_strength /
253 (punittype->attack_strength + punittype->defense_strength);
254}
255
256/**********************************************************************/
259static int unit_att_rating_now(const struct unit *punit)
260{
263}
264
265/**********************************************************************/
268static int unit_att_rating_squared(const struct unit *punit)
269{
270 int v = adv_unit_att_rating(punit);
271
272 return v * v;
273}
274
275/**********************************************************************/
278static int unit_def_rating(const struct unit *attacker,
279 const struct unit *defender)
280{
281 const struct unit_type *def_type = unit_type_get(defender);
282
283 return (get_total_defense_power(attacker, defender)
284 * (attacker->id != 0 ? defender->hp : def_type->hp)
285 * def_type->firepower / POWER_DIVIDER);
286}
287
288/**********************************************************************/
291static int unit_def_rating_squared(const struct unit *attacker,
292 const struct unit *defender)
293{
294 int v = unit_def_rating(attacker, defender);
295
296 return v * v;
297}
298
299/**********************************************************************/
305 const struct unit_type *def_type,
306 struct player *def_player,
307 struct tile *ptile, bool fortified,
308 int veteran)
309{
310 struct civ_map *nmap = &(wld.map);
312 ptile, fortified, veteran)
313 * def_type->hp * def_type->firepower / POWER_DIVIDER;
314
315 return v * v;
316}
317
318/**********************************************************************/
343 int victim_count)
344{
346
347 /* attractiveness danger */
350
351 return desire;
352}
353
354/**********************************************************************/
366static int avg_benefit(int benefit, int loss, double chance)
367{
368 return (int)(((benefit + loss) * chance - loss) * SHIELD_WEIGHTING);
369}
370
371/**********************************************************************/
376 struct tile *ptile0,
377 int *value, int *cost)
378{
379 *cost = 0;
380 *value = 0;
381 square_iterate(&(wld.map), ptile0, 1, ptile) {
382 unit_list_iterate(ptile->units, aunit) {
383 if (aunit != punit
385 int val = adv_unit_att_rating(aunit);
386
387 if (val != 0) {
388 *value += val;
390 }
391 }
394}
395
396/**********************************************************************/
400static bool is_my_turn(struct unit *punit, struct unit *pdef)
401{
402 int val = unit_att_rating_now(punit);
403 int cur, d;
404 const struct unit_type *def_type = unit_type_get(pdef);
405 struct tile *def_tile = unit_tile(pdef);
406 struct civ_map *nmap = &(wld.map);
407
409
410 square_iterate(nmap, def_tile, 1, ptile) {
411 unit_list_iterate(ptile->units, aunit) {
412 if (aunit == punit || unit_owner(aunit) != unit_owner(punit)) {
413 continue;
414 }
416 != ATT_OK)
418 pdef, def_tile)
419 != ATT_OK)) {
420 continue;
421 }
424 FALSE, 0);
425 if (d == 0) {
426 return TRUE; /* Thanks, Markus -- Syela */
427 }
431 FALSE, 0) / d;
432 if (cur > val && ai_fuzzy(unit_owner(punit), TRUE)) {
433 return FALSE;
434 }
437
438 return TRUE;
439}
440
441/**********************************************************************/
446 struct unit *punit,
447 struct tile *ptile)
448{
449 enum gen_action selected;
450
452 punit, ptile))
453 == ACTION_NONE
455 punit, ptile))
456 == ACTION_NONE
458 punit, ptile))
459 == ACTION_NONE
461 punit, ptile))
462 == ACTION_NONE) {
463 return ACTION_NONE;
464 }
465
466 return selected;
467}
468
469/**********************************************************************/
485static int dai_rampage_want(struct unit *punit, struct tile *ptile)
486{
487 struct player *pplayer = unit_owner(punit);
488 struct unit *pdef;
489 struct civ_map *nmap = &(wld.map);
490
492
493 if (can_unit_attack_tile(punit, NULL, ptile)
494 && (pdef = get_defender(nmap, punit, ptile, NULL))
495 /* Action enablers might prevent attacking */
497 /* See description of kill_desire() about these variables. */
501
502 attack *= attack;
503
504 /* If the victim is in the city/fortress, we correct the benefit
505 * with our health because there could be reprisal attacks. We
506 * shouldn't send already injured units to useless suicide.
507 * Note that we do not specially encourage attacks against
508 * cities: rampage is a hit-n-run operation. */
509 if (!is_stack_vulnerable(ptile)
510 && unit_list_size(ptile->units) > 1) {
512 }
513
514 /* If we have non-zero attack rating... */
515 if (attack > 0 && is_my_turn(punit, pdef)) {
518
519 /* No need to amortize, our operation takes one turn. */
520 UNIT_LOG(LOG_DEBUG, punit, "Rampage: Desire %d to kill %s(%d,%d)",
521 desire,
524
525 return MAX(0, desire);
526 }
527 } else if (0 == unit_list_size(ptile->units)) {
528 /* No defender. */
529 struct city *pcity = tile_city(ptile);
530
531 /* ...and free foreign city waiting for us. Who would resist! */
532 if (NULL != pcity
533 && pplayers_at_war(pplayer, city_owner(pcity))
536 }
537
538 /* ...or tiny pleasant hut here! */
539 /* FIXME: unhardcode and variate the desire to enter a hut. */
540 if (unit_can_enter_hut(punit, ptile) && !is_barbarian(pplayer)
541 && is_native_tile(unit_type_get(punit), ptile)) {
542 return -RAMPAGE_HUT_OR_BETTER;
543 }
544 }
545
546 return 0;
547}
548
549/**********************************************************************/
552static struct pf_path *find_rampage_target(struct unit *punit,
553 int thresh_adj, int thresh_move)
554{
555 struct pf_map *tgt_map;
556 struct pf_path *path = NULL;
557 struct pf_parameter parameter;
558 /* Coordinates of the best target (initialize to silence compiler) */
559 struct tile *ptile = unit_tile(punit);
560 /* Want of the best target */
561 int max_want = 0;
562 struct player *pplayer = unit_owner(punit);
563 const struct civ_map *nmap = &(wld.map);
564
566 parameter.omniscience = !has_handicap(pplayer, H_MAP);
567 /* When trying to find rampage targets we ignore risks such as
568 * enemy units because we are looking for trouble!
569 * Hence no call ai_avoid_risks()
570 */
571
572 tgt_map = pf_map_new(&parameter);
574 int want;
575 bool move_needed;
576 int thresh;
577
578 if (move_cost > punit->moves_left) {
579 /* This is too far */
580 break;
581 }
582
583 if (has_handicap(pplayer, H_TARGETS)
584 && !map_is_known_and_seen(iter_tile, pplayer, V_MAIN)) {
585 /* The target is under fog of war */
586 continue;
587 }
588
590
591 /* Negative want means move needed even though the tiles are adjacent */
593 || want < 0);
594 /* Select the relevant threshold */
596 want = (want < 0 ? -want : want);
597
598 if (want > max_want && want > thresh) {
599 /* The new want exceeds both the previous maximum
600 * and the relevant threshold, so it's worth recording */
601 max_want = want;
602 ptile = iter_tile;
603 }
605
606 if (max_want > 0) {
607 /* We found something */
608 path = pf_map_path(tgt_map, ptile);
609 fc_assert(path != NULL);
610 }
611
613
614 return path;
615}
616
617/**********************************************************************/
632 int thresh_move)
633{
634 int count = punit->moves_left + 1; /* break any infinite loops */
635 struct pf_path *path = NULL;
636
639
641 /* This teaches the AI about the dangers inherent in occupychance. */
643
644 while (count-- > 0 && punit->moves_left > 0
646 if (!adv_unit_execute_path(punit, path)) {
647 /* Died */
648 count = -1;
649 }
650 pf_path_destroy(path);
651 path = NULL;
652 }
653
654 fc_assert(NULL == path);
655
657 return (count >= 0);
658}
659
660/**********************************************************************/
664static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer,
665 struct unit *punit)
666{
667 struct unit *aunit = aiguard_charge_unit(ait, punit);
668 struct city *acity = aiguard_charge_city(ait, punit);
669 struct tile *ptile;
670
672 CHECK_GUARD(ait, punit);
673
674 if (aunit && unit_owner(aunit) == unit_owner(punit)) {
675 /* protect a unit */
676 if (aunit->goto_tile != NULL) {
677 /* Our charge is going somewhere: maybe we should meet them there */
678 /* FIXME: This probably isn't the best algorithm for this. */
680 int me2goal = real_map_distance(unit_tile(punit), aunit->goto_tile);
681 int them2goal = real_map_distance(unit_tile(aunit), aunit->goto_tile);
682
683 if (me2goal < me2them
687 ptile = aunit->goto_tile;
688 } else {
689 ptile = unit_tile(aunit);
690 }
691 } else {
692 ptile = unit_tile(aunit);
693 }
694 } else if (acity && city_owner(acity) == unit_owner(punit)) {
695 /* protect a city */
696 ptile = acity->tile;
697 } else {
698 /* should be impossible */
699 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "we lost our charge");
701 return;
702 }
703
704 if (same_pos(unit_tile(punit), ptile)) {
705 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "at RV");
706 } else {
707 if (goto_is_sane(punit, ptile)) {
708 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "meeting charge");
709 if (!dai_gothere(ait, pplayer, punit, ptile)) {
710 /* We died */
711 return;
712 }
713 } else {
714 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "can not meet charge");
716 }
717 }
718 /* We might have stopped because of an enemy nearby.
719 * Perhaps we can kill it.*/
722 && same_pos(unit_tile(punit), ptile)) {
723 def_ai_unit_data(punit, ait)->done = TRUE; /* Stay with charge */
724 }
725}
726
727/**********************************************************************/
730static bool unit_role_defender(const struct unit_type *punittype)
731{
733}
734
735/**********************************************************************/
745adv_want look_for_charge(struct ai_type *ait, const struct civ_map *nmap,
746 struct player *pplayer, struct unit *punit,
747 struct unit **aunit, struct city **acity)
748{
749 struct pf_parameter parameter;
750 struct pf_map *pfm;
751 struct city *pcity;
752 struct ai_city *data, *best_data = NULL;
754 int def, best_def = -1;
755 /* Arbitrary: 3 turns. */
756 const int max_move_cost = 3 * unit_move_rate(punit);
757
758 *aunit = NULL;
759 *acity = NULL;
760
761 if (0 == toughness) {
762 /* useless */
763 return 0;
764 }
765
766 pft_fill_unit_parameter(&parameter, nmap, punit);
767 parameter.omniscience = !has_handicap(pplayer, H_MAP);
768 pfm = pf_map_new(&parameter);
769
770 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
771 if (move_cost > max_move_cost) {
772 /* Consider too far. */
773 break;
774 }
775
776 pcity = tile_city(ptile);
777
778 /* Consider unit bodyguard. */
779 unit_list_iterate(ptile->units, buddy) {
780 const struct unit_type *ptype = unit_type_get(punit);
781 const struct unit_type *buddy_type = unit_type_get(buddy);
782
783 /* TODO: allied unit bodyguard? */
785 || unit_owner(buddy) != pplayer
786 || !aiguard_wanted(ait, buddy)
792
793 continue;
794 }
795
797 if (0 >= def) {
798 continue;
799 }
800
802 /* Reduce want based on move cost. We can't do this for
803 * transports since they move around all the time, leading
804 * to hilarious flip-flops. */
805 def >>= move_cost / (2 * unit_move_rate(punit));
806 }
807 if (def > best_def) {
808 *aunit = buddy;
809 *acity = NULL;
810 best_def = def;
811 }
813
814 /* City bodyguard. TODO: allied city bodyguard? */
815 if (ai_fuzzy(pplayer, TRUE)
816 && NULL != pcity
817 && city_owner(pcity) == pplayer
818 && (data = def_ai_city_data(pcity, ait))
819 && 0 < data->urgency) {
820 if (NULL != best_data
821 && (0 < best_data->grave_danger
822 || best_data->urgency > data->urgency
823 || ((best_data->danger > data->danger
824 || AIUNIT_DEFEND_HOME == def_ai_unit_data(punit, ait)->task)
825 && 0 == data->grave_danger))) {
826 /* Chances are we'll be between cities when we are needed the most!
827 * Resuming pf_map_move_costs_iterate()... */
828 continue;
829 }
830 def = (data->danger - assess_defense_quadratic(ait, pcity));
831 if (def <= 0) {
832 continue;
833 }
834 /* Reduce want based on move cost. */
835 def >>= move_cost / (2 * unit_move_rate(punit));
836 if (def > best_def && ai_fuzzy(pplayer, TRUE)) {
837 *acity = pcity;
838 *aunit = NULL;
839 best_def = def;
840 best_data = data;
841 }
842 }
844
846
847 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "%s(), best_def=%d, type=%s (%d, %d)",
850 : (NULL != *aunit ? unit_rule_name(*aunit) : "")),
852 : (NULL != *aunit ?
855 : (NULL != *aunit ?
857
858 return ((best_def * 100) / toughness);
859}
860
861/**********************************************************************/
865 const struct unit_type *followee,
866 struct ai_type *ait)
867{
868 struct unit_type_ai *utai = utype_ai_data(follower, ait);
869
870 unit_type_list_iterate(utai->potential_charges, pcharge) {
871 if (pcharge == followee) {
872 return TRUE;
873 }
875
876 return FALSE;
877}
878
879/**********************************************************************/
882static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap,
883 struct player *pplayer, struct unit *punit)
884{
885 const struct unit_type *punittype = unit_type_get(punit);
886 struct unit_ai *unit_data;
887
889
890 /* Keep barbarians aggressive and primitive */
891 if (is_barbarian(pplayer)) {
893 && is_land_barbarian(pplayer)) {
894 /* Land barbarians pillage */
896 }
898
899 return;
900 }
901
903
904 /* If I am a bodyguard, check whether I can do my job. */
905 if (unit_data->task == AIUNIT_ESCORT
906 || unit_data->task == AIUNIT_DEFEND_HOME) {
908 }
909
910 if (aiguard_has_charge(ait, punit)
911 && unit_data->task == AIUNIT_ESCORT) {
912 struct unit *aunit = aiguard_charge_unit(ait, punit);
913 struct city *acity = aiguard_charge_city(ait, punit);
914 struct ai_city *city_data = NULL;
915
916 if (acity != NULL) {
918 }
919
920 /* Check if the city we are on our way to rescue is still in danger,
921 * or the unit we should protect is still alive... */
922 if ((aunit && (aiguard_has_guard(ait, aunit) || aiguard_wanted(ait, aunit))
925 && city_data->urgency != 0
926 && city_data->danger > assess_defense_quadratic(ait, acity))) {
927 return; /* Yep! */
928 } else {
929 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL); /* Nope! */
930 }
931 }
932
933 /* Is the unit badly damaged? */
934 if ((unit_data->task == AIUNIT_RECOVER
935 && punit->hp < punittype->hp)
936 || punit->hp < punittype->hp * 0.25) { /* WAG */
937 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "set to hp recovery");
939 return;
940 }
941
944 /* This is a defending unit that doesn't need to stay put.
945 * It needs to defend something, but not necessarily where it's at.
946 * Therefore, it will consider becoming a bodyguard. -- Syela */
947 struct city *acity;
948 struct unit *aunit;
949
950 look_for_charge(ait, nmap, pplayer, punit, &aunit, &acity);
951 if (acity) {
954 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend city");
955 } else if (aunit) {
958 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend unit");
959 }
960 }
962}
963
964/**********************************************************************/
973static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
974 struct unit *punit)
975{
976 struct city *pcity = aiguard_charge_city(ait, punit);
977
979
980 if (!pcity || city_owner(pcity) != pplayer) {
981 pcity = tile_city(unit_tile(punit));
982 /* Do not stay defending an allied city forever */
984 }
985
986 if (!pcity) {
987 /* Try to find a place to rest. Sitting duck out in the wilderness
988 * is generally a bad idea, since we protect no cities that way, and
989 * it looks silly. */
990 pcity = find_closest_city(unit_tile(punit), NULL, pplayer,
993 }
994
995 if (!pcity) {
997 }
998
1001 /* ... we survived */
1002 if (pcity) {
1003 UNIT_LOG(LOG_DEBUG, punit, "go to defend %s", city_name_get(pcity));
1004 if (same_pos(unit_tile(punit), pcity->tile)) {
1005 UNIT_LOG(LOG_DEBUG, punit, "go defend successful");
1007 } else {
1008 (void) dai_gothere(ait, pplayer, punit, pcity->tile);
1009 }
1010 } else {
1011 UNIT_LOG(LOG_VERBOSE, punit, "defending nothing...?");
1012 }
1013 }
1014}
1015
1016/**********************************************************************/
1020 const struct unit_type *utype,
1021 int which)
1022{
1023 int attacks;
1024
1025 if (utype_action_takes_all_mp(utype,
1027 attacks = 1;
1028 } else {
1029 attacks = utype->move_rate;
1030 }
1031 city_data->invasion.attack += attacks;
1032 if (which == INVASION_OCCUPY) {
1033 city_data->invasion.occupy++;
1034 }
1035}
1036
1037/**********************************************************************/
1047static bool invasion_funct(struct ai_type *ait, struct unit *punit,
1048 bool dest, int radius, int which)
1049{
1050 struct tile *ptile;
1051 struct player *pplayer = unit_owner(punit);
1052
1054
1055 if (dest) {
1057
1058 ptile = punit->goto_tile;
1059 } else {
1060 ptile = unit_tile(punit);
1061 }
1062
1063 square_iterate(&(wld.map), ptile, radius, tile1) {
1064 struct city *pcity = tile_city(tile1);
1065
1066 if (pcity
1067 && POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, city_owner(pcity))
1068 && (dest || !has_defense(pcity))) {
1069 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1070
1071 /* Unit itself */
1073
1074 /* Cargo */
1075 unit_cargo_iterate(punit, cargo) {
1076 const struct unit_type *utype = unit_type_get(cargo);
1077
1078 if (IS_ATTACKER(utype)) {
1082 }
1084 }
1086
1087 return TRUE;
1088}
1089
1090/**********************************************************************/
1093bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map,
1094 struct tile *dest_tile,
1095 const struct unit_type *cargo_type,
1096 struct tile **ferry_dest, struct tile **beachhead_tile)
1097{
1098 if (NULL == tile_city(dest_tile)
1100 /* Unit can directly go to 'dest_tile'. */
1101 struct tile *best_tile = NULL;
1103
1104 if (NULL != beachhead_tile) {
1105 *beachhead_tile = dest_tile;
1106 }
1107
1108 adjc_iterate(&(wld.map), dest_tile, ptile) {
1110 if (cost != PF_IMPOSSIBLE_MC
1111 && (NULL == best_tile || cost < best_cost)) {
1112 best_tile = ptile;
1113 best_cost = cost;
1114 }
1116
1117 if (NULL != ferry_dest) {
1119 }
1120
1121 return (PF_IMPOSSIBLE_MC != best_cost);
1122 } else {
1123 /* We need to find a beach around 'dest_tile'. */
1124 struct tile *best_tile = NULL, *best_beach = NULL;
1127
1128 tile_list_append(checked_tiles, dest_tile);
1129 adjc_iterate(&(wld.map), dest_tile, beach) {
1131 /* Can land there. */
1132 adjc_iterate(&(wld.map), beach, ptile) {
1133 if (!tile_list_search(checked_tiles, ptile)
1134 && !is_non_allied_unit_tile(ptile, pplayer)) {
1137 if (cost != PF_IMPOSSIBLE_MC
1138 && (NULL == best_tile || cost < best_cost)) {
1139 best_beach = beach;
1140 best_tile = ptile;
1141 best_cost = cost;
1142 }
1143 }
1145 }
1147
1149
1150 if (NULL != beachhead_tile) {
1152 }
1153 if (NULL != ferry_dest) {
1155 }
1156 return (PF_IMPOSSIBLE_MC != best_cost);
1157 }
1158}
1159
1160/**********************************************************************/
1168adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
1169 struct unit *punit,
1170 struct tile **pdest_tile, struct pf_path **ppath,
1171 struct pf_map **pferrymap,
1172 struct unit **pferryboat,
1173 const struct unit_type **pboattype, int *pmove_time)
1174{
1175 const int attack_value = adv_unit_att_rating(punit); /* basic attack. */
1176 struct pf_parameter parameter;
1177 struct pf_map *punit_map, *ferry_map;
1178 struct pf_position pos;
1180 const struct unit_type *punit_type = unit_type_get(punit);
1181 struct tile *punit_tile = unit_tile(punit);
1182 /* Type of our boat (a future one if ferryboat == NULL). */
1183 const struct unit_type *boattype = NULL;
1184 struct unit *ferryboat = NULL;
1185 struct city *pcity;
1186 struct ai_city *acity_data;
1187 int bcost, bcost_bal; /* Build cost of the attacker (+adjustments). */
1188 bool handicap = has_handicap(pplayer, H_TARGETS);
1189 bool unhap = FALSE; /* Do we make unhappy citizen. */
1190 bool harbor = FALSE; /* Do we have access to sea? */
1191 bool go_by_boat; /* Whether we need a boat or not. */
1192 int vulnerability; /* Enemy defense rating. */
1193 adv_want benefit; /* Benefit from killing the target. */
1194 struct unit *pdefender; /* Enemy city defender. */
1195 int move_time; /* Turns needed to target. */
1196 int reserves;
1197 int attack; /* Our total attack value with reinforcements. */
1198 int victim_count; /* Number of enemies there. */
1199 int needferry; /* Cost of building a ferry boat. */
1200 /* This is a kluge, because if we don't set x and y with !punit->id,
1201 * p_a_w isn't called, and we end up not wanting ironclads and therefore
1202 * never learning steam engine, even though ironclads would be very
1203 * useful. -- Syela */
1204 adv_want bk = 0;
1205 adv_want want; /* Want (amortized) of the operation. */
1206 adv_want best = 0; /* Best of all wants. */
1207 struct tile *goto_dest_tile = NULL;
1208 bool can_occupy;
1209 struct civ_map *nmap = &(wld.map);
1210
1211 /* Very preliminary checks. */
1213 if (NULL != pferrymap) {
1214 *pferrymap = NULL;
1215 }
1216 if (NULL != pferryboat) {
1217 *pferryboat = NULL;
1218 }
1219 if (NULL != pboattype) {
1220 *pboattype = NULL;
1221 }
1222 if (NULL != pmove_time) {
1223 *pmove_time = 0;
1224 }
1225 if (NULL != ppath) {
1226 *ppath = NULL;
1227 }
1228
1229 if (0 == attack_value) {
1230 /* A very poor attacker... probably low on HP. */
1231 return 0;
1232 }
1233
1235
1236
1237 /*** Part 1: Calculate targets ***/
1238
1239 /* This horrible piece of code attempts to calculate the attractiveness of
1240 * enemy cities as targets for our units, by checking how many units are
1241 * going towards it or are near it already. */
1242
1243 /* Reset enemy cities data. */
1245 /* See comment below in next usage of POTENTIALLY_HOSTILE_PLAYER. */
1246 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1247 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1248 continue;
1249 }
1250 city_list_iterate(aplayer->cities, acity) {
1251 struct ai_city *city_data = def_ai_city_data(acity, ait);
1252
1254 &city_data->attack,
1255 &city_data->bcost);
1256 city_data->invasion.attack = 0;
1257 city_data->invasion.occupy = 0;
1260
1261 /* Second, calculate in units on their way there, and mark targets for
1262 * invasion */
1263 unit_list_iterate(pplayer->units, aunit) {
1264 const struct unit_type *atype;
1265
1266 if (aunit == punit) {
1267 continue;
1268 }
1269
1271
1272 /* Dealing with invasion stuff */
1273 if (IS_ATTACKER(atype)) {
1274 if (aunit->activity == ACTIVITY_GOTO) {
1275 if (invasion_funct(ait, aunit, TRUE, 0,
1278 && (pcity = tile_city(aunit->goto_tile))) {
1279 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1280
1283 }
1284 }
1288 } else if (def_ai_unit_data(aunit, ait)->passenger != 0
1290 /* It's a transport with reinforcements */
1291 if (aunit->activity == ACTIVITY_GOTO) {
1293 }
1295 }
1297 /* end horrible initialization subroutine */
1298
1299
1300 /*** Part 2: Now pick one target ***/
1301
1302 /* We first iterate through all cities, then all units, looking
1303 * for a viable target. We also try to gang up on the target
1304 * to avoid spreading out attacks too widely to be inefficient.
1305 */
1306
1307 pcity = tile_city(punit_tile);
1308 if (NULL != pcity && (0 == punit->id || pcity->id == punit->homecity)) {
1309 /* I would have thought unhappiness should be taken into account
1310 * irrespectfully the city in which it will surface... -- GB */
1312 }
1313
1316
1318 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1319 punit_map = pf_map_new(&parameter);
1320
1321 if (MOVE_NONE == punit_class->adv.sea_move) {
1322 /* We need boat to move over sea. */
1323 ferryboat = unit_transport_get(punit);
1324
1325 /* First check if we can use the boat we are currently loaded to. */
1326 if (ferryboat == NULL || !is_boat_free(ait, ferryboat, punit, 0)) {
1327 /* No, we cannot control current boat */
1328 ferryboat = NULL;
1329 }
1330
1331 if (NULL == ferryboat) {
1332 /* Try to find new boat */
1333 ferryboat = player_unit_by_number(pplayer,
1334 aiferry_find_boat(ait, punit, 1, NULL));
1335 }
1336
1338 harbor = TRUE;
1339 }
1340 }
1341
1342 if (NULL != ferryboat) {
1343 boattype = unit_type_get(ferryboat);
1344 pft_fill_unit_overlap_param(&parameter, nmap, ferryboat);
1345 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1346 ferry_map = pf_map_new(&parameter);
1347 } else {
1349 if (NULL == boattype) {
1350 /* We pretend that we can have the simplest boat to stimulate tech. */
1352 }
1353 if (NULL != boattype && harbor) {
1354 /* Let's simulate a boat at 'punit' position. */
1356 punit_tile, pplayer);
1357 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1358 ferry_map = pf_map_new(&parameter);
1359 } else {
1360 ferry_map = NULL;
1361 }
1362 }
1363
1365
1367 /* For the virtual unit case, which is when we are called to evaluate
1368 * which units to build, we want to calculate in danger and which
1369 * players we want to make war with in the future. We do _not_ want
1370 * to do this when actually making attacks. */
1371 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1372 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1373 continue; /* Not an enemy. */
1374 }
1375
1376 city_list_iterate(aplayer->cities, acity) {
1377 struct tile *atile = city_tile(acity);
1378
1381 /* Can't attack this city. It is on land. */
1382 continue;
1383 }
1384
1385 if (handicap && !map_is_known(atile, pplayer)) {
1386 /* Can't see it */
1387 continue;
1388 }
1389
1391 go_by_boat = FALSE;
1392 move_time = pos.turn;
1393 } else if (NULL == ferry_map) {
1394 continue; /* Impossible to handle. */
1395 } else {
1396 struct tile *dest, *beach;
1397
1398 if (!find_beachhead(pplayer, ferry_map, atile, punit_type,
1399 &dest, &beach)) {
1400 continue; /* Impossible to go by boat. */
1401 }
1402 if (!pf_map_position(ferry_map, dest, &pos)) {
1404 continue;
1405 }
1406 move_time = pos.turn; /* Sailing time. */
1407 if (dest != beach) {
1408 move_time++; /* Land time. */
1409 }
1410 if (NULL != ferryboat && unit_tile(ferryboat) != punit_tile) {
1411 if (pf_map_position(punit_map, unit_tile(ferryboat), &pos)) {
1412 move_time += pos.turn; /* Time to reach the boat. */
1413 } else {
1414 continue; /* Cannot reach the boat. */
1415 }
1416 }
1417 go_by_boat = TRUE;
1418 }
1419
1424 } else {
1425 pdefender = NULL;
1426 vulnerability = 0;
1427 benefit = 0;
1428 }
1429
1430 if (1 < move_time) {
1432
1433 if (def_type) {
1437 if (v > vulnerability) {
1438 /* They can build a better defender! */
1439 vulnerability = v;
1441 }
1442 }
1443 }
1444
1446
1447 reserves = (acity_data->invasion.attack
1448 - unit_list_size(acity->tile->units));
1449
1450 if (punit->id == 0) {
1451 /* Real unit would add to reserves once built. */
1454 reserves++;
1455 } else {
1456 reserves += punit_type->move_rate;
1457 }
1458 }
1459
1460 if (0 < reserves && (can_occupy
1461 || 0 < acity_data->invasion.occupy)) {
1462 /* There are units able to occupy the city after all defenders
1463 * are killed! */
1464 benefit += acity_data->worth * reserves / 5;
1465 }
1466
1467 attack = attack_value + acity_data->attack;
1468 attack *= attack;
1469 /* Avoiding handling upkeep aggregation this way -- Syela */
1470
1471 /* AI was not sending enough reinforcements to totally wipe out a city
1472 * and conquer it in one turn.
1473 * This variable enables total carnage. -- Syela */
1475
1476 if (!can_occupy && NULL == pdefender) {
1477 /* Nothing there to bash and we can't occupy!
1478 * Not having this check caused warships yoyoing */
1479 want = 0;
1480 } else if (10 < move_time) {
1481 /* Too far! */
1482 want = 0;
1483 } else if (can_occupy && 0 == acity_data->invasion.occupy
1484 && (0 < acity_data->invasion.attack
1485 || victim_count == 0)) {
1486 /* Units able to occupy really needed there! */
1487 want = bcost * SHIELD_WEIGHTING;
1488 } else {
1489 want = kill_desire(benefit, attack, bcost + acity_data->bcost,
1491 }
1494 /* Build_cost of ferry. */
1495 needferry = (go_by_boat && NULL == ferryboat
1497 /* FIXME: add time to build the ferry? */
1499 want, MAX(1, move_time),
1501
1502 /* BEGIN STEAM-ENGINES-ARE-OUR-FRIENDS KLUGE. */
1503 if (0 >= want && 0 == punit->id && 0 >= best) {
1507 MAX(1, move_time),
1509
1510 if (bk_e > bk) {
1511 *pdest_tile = atile;
1512 if (NULL != pferrymap) {
1514 }
1515 if (NULL != pferryboat) {
1516 *pferryboat = go_by_boat ? ferryboat : NULL;
1517 }
1518 if (NULL != pboattype) {
1520 }
1521 if (NULL != pmove_time) {
1523 }
1524 goto_dest_tile = (go_by_boat && NULL != ferryboat
1525 ? unit_tile(ferryboat) : atile);
1526 bk = bk_e;
1527 }
1528 }
1529 /* END STEAM-ENGINES KLUGE */
1530
1531 if (0 != punit->id
1532 && NULL != ferryboat
1533 && punit_class->adv.sea_move == MOVE_NONE) {
1535 "%s(): with boat %s@(%d, %d) -> %s@(%d, %d)"
1536 " (go_by_boat = %d, move_time = %d, want = " ADV_WANT_PRINTF
1537 ", best = " ADV_WANT_PRINTF ")",
1538 __FUNCTION__, unit_rule_name(ferryboat),
1539 TILE_XY(unit_tile(ferryboat)), city_name_get(acity),
1540 TILE_XY(atile), go_by_boat, move_time, want, best);
1541 }
1542
1543 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1544 /* Yes, we like this target */
1545 best = want;
1546 *pdest_tile = atile;
1547 if (NULL != pferrymap) {
1549 }
1550 if (NULL != pferryboat) {
1551 *pferryboat = go_by_boat ? ferryboat : NULL;
1552 }
1553 if (NULL != pboattype) {
1555 }
1556 if (NULL != pmove_time) {
1558 }
1559 goto_dest_tile = (go_by_boat && NULL != ferryboat
1560 ? unit_tile(ferryboat) : atile);
1561 }
1563
1565 /* I'm not sure the following code is good but it seems to be adequate.
1566 * I am deliberately not adding ferryboat code to the unit_list_iterate.
1567 * -- Syela */
1569 struct tile *atile = unit_tile(aunit);
1570
1571 if (NULL != tile_city(atile)) {
1572 /* already dealt with it. */
1573 continue;
1574 }
1575
1576 if (handicap && !map_is_known(atile, pplayer)) {
1577 /* Can't see the target. */
1578 continue;
1579 }
1580
1582 && 0 == punit->id) {
1583 /* We will not build units just to chase caravans and
1584 * ambassadors. */
1585 continue;
1586 }
1587
1588 /* We have to assume the attack is diplomatically ok.
1589 * We cannot use can_player_attack_tile, because we might not
1590 * be at war with aplayer yet */
1592 || aunit != get_defender(nmap, punit, atile, NULL)) {
1593 /* We cannot attack it, or it is not the main defender. */
1594 continue;
1595 }
1596
1598 /* Cannot reach it. */
1599 continue;
1600 }
1601
1604
1605 move_time = pos.turn;
1606 if (10 < move_time) {
1607 /* Too far. */
1608 want = 0;
1609 } else {
1610 want = kill_desire(benefit, attack, bcost, vulnerability, 1);
1611 /* Take into account maintenance of the unit. */
1612 /* FIXME: Depends on the government. */
1613 want -= move_time * SHIELD_WEIGHTING;
1614 /* Take into account unhappiness
1615 * (costs 2 luxuries to compensate). */
1616 want -= (unhap ? 2 * move_time * TRADE_WEIGHTING : 0);
1617 }
1619 want, MAX(1, move_time), bcost_bal);
1620 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1621 best = want;
1622 *pdest_tile = atile;
1623 if (NULL != pferrymap) {
1624 *pferrymap = NULL;
1625 }
1626 if (NULL != pferryboat) {
1627 *pferryboat = NULL;
1628 }
1629 if (NULL != pboattype) {
1630 *pboattype = NULL;
1631 }
1632 if (NULL != pmove_time) {
1634 }
1636 }
1639
1640 if (NULL != ppath) {
1643 }
1644
1646 if (NULL != ferry_map
1647 && (NULL == pferrymap || *pferrymap != ferry_map)) {
1649 }
1650
1652
1653 return best;
1654}
1655
1656/**********************************************************************/
1665{
1666 struct pf_parameter parameter;
1667 struct pf_map *pfm;
1668 struct player *pplayer = unit_owner(punit);
1669 struct city *pcity, *best_city = NULL;
1670 int best = FC_INFINITY, cur;
1671 const struct civ_map *nmap = &(wld.map);
1672
1673 pft_fill_unit_parameter(&parameter, nmap, punit);
1674 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1675 pfm = pf_map_new(&parameter);
1676
1677 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
1678 if (move_cost > best) {
1679 /* We already found a better city. No need to continue. */
1680 break;
1681 }
1682
1683 pcity = tile_city(ptile);
1684 if (NULL == pcity || !pplayers_allied(pplayer, city_owner(pcity))) {
1685 continue;
1686 }
1687
1688 /* Score based on the move cost. */
1689 cur = move_cost;
1690
1691 /* Note the unit owner may be different from the city owner. */
1692 if (0 == get_unittype_bonus(unit_owner(punit), ptile,
1694 EFT_HP_REGEN)) {
1695 /* If we cannot regen fast our hit points here, let's make some
1696 * penalty. */
1697 cur *= 3;
1698 }
1699
1700 if (cur < best) {
1701 best_city = pcity;
1702 best = cur;
1703 }
1705
1707 return best_city;
1708}
1709
1710/**********************************************************************/
1716 struct player *pplayer,
1717 struct unit *punit)
1718{
1719 struct city *pc;
1720 bool only_continent = TRUE;
1721
1722 if (unit_transported(punit)) {
1723 /* If we are in transport, we can go to any continent.
1724 * Actually, we are not currently in a continent where to stay. */
1726 }
1727
1728 if ((pc = find_closest_city(unit_tile(punit), NULL, pplayer, FALSE,
1731 UNIT_LOG(LOG_DEBUG, punit, "Barbarian heading to conquer %s",
1732 city_name_get(pc));
1733 (void) dai_gothere(ait, pplayer, punit, pc->tile);
1734 } else {
1735 struct unit *ferry = NULL;
1736
1737 if (unit_transported(punit)) {
1738 ferry = unit_transport_get(punit);
1739
1740 /* We already are in a boat so it needs no
1741 * free capacity */
1742 if (!is_boat_free(ait, ferry, punit, 0)) {
1743 /* We cannot control our ferry. */
1744 ferry = NULL;
1745 }
1746 } else {
1747 /* We are not in a boat yet. Search for one. */
1749 if (is_boat_free(ait, aunit, punit, 1)
1751 ferry = aunit;
1752 break;
1753 }
1755 }
1756
1757 if (ferry) {
1758 UNIT_LOG(LOG_DEBUG, punit, "Barbarian sailing to conquer %s",
1759 city_name_get(pc));
1760 (void) aiferry_goto_amphibious(ait, ferry, punit, pc->tile);
1761 } else {
1762 /* This is not an error. Somebody else might be in charge
1763 * of the ferry. */
1764 UNIT_LOG(LOG_DEBUG, punit, "unable to find barbarian ferry");
1765 }
1766 }
1767 } else {
1768 UNIT_LOG(LOG_DEBUG, punit, "Barbarian find no target city");
1769 }
1770}
1771
1772/**********************************************************************/
1778static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
1779 struct unit *punit)
1780{
1781 struct tile *dest_tile;
1782 int id = punit->id;
1783 int ct = 10;
1784 struct city *pcity = NULL;
1785
1787
1788 /* Barbarians pillage, and might keep on doing that so they sometimes
1789 * even finish it. */
1790 if (punit->activity == ACTIVITY_PILLAGE && is_barbarian(pplayer)
1791 && fc_rand(2) == 1) {
1792 return;
1793 }
1794
1795 /* First find easy nearby enemies, anything better than pillage goes.
1796 * NB: We do not need to repeat dai_military_rampage, it is repeats itself
1797 * until it runs out of targets. */
1798 /* FIXME: 1. dai_military_rampage never checks if it should defend newly
1799 * conquered cities.
1800 * FIXME: 2. would be more convenient if it returned FALSE if we run out
1801 * of moves too.*/
1803 return; /* we died */
1804 }
1805
1806 if (punit->moves_left <= 0) {
1807 return;
1808 }
1809
1810 /* Main attack loop */
1811 do {
1812 struct tile *start_tile = unit_tile(punit);
1813 struct pf_path *path;
1814 struct unit *ferryboat;
1815
1816 /* Then find enemies the hard way */
1817 find_something_to_kill(ait, pplayer, punit, &dest_tile, &path,
1818 NULL, &ferryboat, NULL, NULL);
1819 if (!same_pos(unit_tile(punit), dest_tile)) {
1820 if (!is_tiles_adjacent(unit_tile(punit), dest_tile)
1821 || !can_unit_attack_tile(punit, NULL, dest_tile)) {
1822
1823 /* Adjacent and can't attack usually means we are not marines
1824 * and on a ferry. This fixes the problem (usually). */
1825 UNIT_LOG(LOG_DEBUG, punit, "mil att gothere -> (%d, %d)",
1826 TILE_XY(dest_tile));
1827
1828 /* Set ACTIVITY_GOTO more permanently than just inside
1829 * adv_follow_path(). This way other units will know we're
1830 * on our way even if we don't reach target yet. */
1831 punit->goto_tile = dest_tile;
1833 if (NULL != path && !adv_follow_path(punit, path, dest_tile)) {
1834 /* Died. */
1835 pf_path_destroy(path);
1836 return;
1837 }
1838 if (NULL != ferryboat) {
1839 /* Need a boat. */
1840 aiferry_gobyboat(ait, pplayer, punit, dest_tile, FALSE);
1841 pf_path_destroy(path);
1842 return;
1843 }
1844 if (0 >= punit->moves_left) {
1845 /* No moves left. */
1846 pf_path_destroy(path);
1847 return;
1848 }
1849
1850 /* Either we're adjacent or we sitting on the tile. We might be
1851 * sitting on the tile if the enemy that _was_ sitting there
1852 * attacked us and died _and_ we had enough movement to get there */
1853 if (same_pos(unit_tile(punit), dest_tile)) {
1854 UNIT_LOG(LOG_DEBUG, punit, "mil att made it -> (%d, %d)",
1855 TILE_XY(dest_tile));
1856 pf_path_destroy(path);
1857 break;
1858 }
1859 }
1860
1861 if (is_tiles_adjacent(unit_tile(punit), dest_tile)) {
1862 /* Close combat. fstk sometimes want us to attack an adjacent
1863 * enemy that rampage wouldn't */
1864 UNIT_LOG(LOG_DEBUG, punit, "mil att bash -> (%d, %d)",
1865 TILE_XY(dest_tile));
1866 if (!dai_unit_attack(ait, punit, dest_tile)) {
1867 /* Died */
1868 pf_path_destroy(path);
1869 return;
1870 }
1871 } else if (!same_pos(start_tile, unit_tile(punit))) {
1872 /* Got stuck. Possibly because of adjacency to an
1873 * enemy unit. Perhaps we are in luck and are now next to a
1874 * tempting target? Let's find out... */
1877 pf_path_destroy(path);
1878 return;
1879 }
1880
1881 } else {
1882 /* FIXME: This happens a bit too often! */
1883 UNIT_LOG(LOG_DEBUG, punit, "fstk didn't find us a worthy target!");
1884 /* No worthy enemies found, so abort loop */
1885 ct = 0;
1886 }
1887 pf_path_destroy(path);
1888
1889 ct--; /* infinite loops from railroads must be stopped */
1890 } while (punit->moves_left > 0 && ct > 0);
1891
1892 /* Cleanup phase */
1893 if (punit->moves_left == 0) {
1894 return;
1895 }
1897 if (pcity != NULL
1898 && (dai_is_ferry(punit, ait)
1899 || punit->hp < unit_type_get(punit)->hp * 0.50)) { /* WAG */
1900 /* Go somewhere safe */
1901 UNIT_LOG(LOG_DEBUG, punit, "heading to nearest safe house.");
1902 (void) dai_unit_goto(ait, punit, pcity->tile);
1903 } else if (!is_barbarian(pplayer)) {
1904 /* Nothing else to do, so try exploring. */
1905 switch (manage_auto_explorer(punit)) {
1906 case MR_DEATH:
1907 /* don't use punit! */
1908 return;
1909 case MR_OK:
1910 UNIT_LOG(LOG_DEBUG, punit, "nothing else to do, so exploring");
1911 break;
1912 default:
1913 UNIT_LOG(LOG_DEBUG, punit, "nothing to do - no more exploring either");
1914 break;
1915 };
1916 } else {
1917 /* You can still have some moves left here, but barbarians should
1918 not sit helplessly, but advance towards nearest known enemy city */
1919 UNIT_LOG(LOG_DEBUG, punit, "attack: barbarian");
1920 dai_military_attack_barbarian(ait, pplayer, punit);
1921 }
1922 if ((punit = game_unit_by_number(id)) && punit->moves_left > 0) {
1923 UNIT_LOG(LOG_DEBUG, punit, "attack: giving up unit to defense");
1924 dai_military_defend(ait, pplayer, punit);
1925 }
1926}
1927
1928/**********************************************************************/
1932static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
1933{
1934 bool alive = TRUE;
1935 int ferryboat = 0;
1936 struct pf_path *path_to_ferry = NULL;
1937
1938 UNIT_LOG(LOG_CARAVAN, punit, "requesting a boat!");
1939 ferryboat = aiferry_find_boat(ait, punit, 1, &path_to_ferry);
1940 /* going to meet the boat */
1941 if ((ferryboat <= 0)) {
1943 "in find_boat_for_unit cannot find any boats.");
1944 /* if we are undefended on the country side go to a city */
1946 if (current_city == NULL) {
1948 if (city_near != NULL) {
1950 }
1951 }
1952 } else {
1953 if (path_to_ferry != NULL) {
1955 /* Died. */
1958 alive = FALSE;
1959 } else {
1962 alive = TRUE;
1963 }
1964 }
1965 }
1966 return alive;
1967}
1968
1969/**********************************************************************/
1978 struct tile *ctile, struct tile *ptile)
1979{
1980 /* We usually have Inaccessible terrain, so not testing MOVE_FULL == */
1981 bool lm = MOVE_NONE != pclass->adv.land_move,
1982 sm = MOVE_NONE != pclass->adv.sea_move;
1983 struct civ_map *pmap = &(wld.map);
1984
1985 if (lm && sm) {
1986 return FALSE;
1987 }
1988
1989 /* We could use adjc_iterate() but likely often tiles are on the same
1990 * continent and it will be more time to find where they connect */
1993
1994 if (is_ocean_tile(atile) ? sm : lm) {
1995 iterate_outward(pmap, ptile, 1, btile) {
1996 if (tile_continent(btile) == acont) {
1997 return FALSE;
1998 }
2000 }
2002
2003 if (is_tiles_adjacent(ctile, ptile)) {
2004 return FALSE;
2005 }
2006
2007 return TRUE;
2008}
2009
2010/**********************************************************************/
2016static void dai_caravan_goto(struct ai_type *ait, struct player *pplayer,
2017 struct unit *punit,
2018 const struct city *dest_city,
2019 bool help_wonder,
2020 bool required_boat, bool request_boat)
2021{
2022 bool alive = TRUE;
2023 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2024 const struct civ_map *nmap = &(wld.map);
2025
2027
2028 /* if we're not there yet, and we can move, move... */
2029 if (!same_pos(dest_city->tile, unit_tile(punit))
2030 && punit->moves_left != 0) {
2031 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) task %s going to %s in %s %s",
2035 help_wonder ? "help a wonder" : "trade", city_name_get(dest_city),
2036 required_boat ? "with a boat" : "");
2037 if (required_boat) {
2038 /* to trade with boat */
2039 if (request_boat) {
2040 /* Try to find new boat */
2042 } else {
2043 /* if we are not being transported then ask for a boat again */
2044 alive = TRUE;
2047 unit_tile(punit), dest_city->tile)) {
2049 }
2050 }
2051 if (alive) {
2052 /* FIXME: sometimes we get FALSE here just because
2053 * a trireme that we've boarded can't go over an ocean. */
2054 alive = dai_gothere(ait, pplayer, punit, dest_city->tile);
2055 }
2056 } else {
2057 /* to trade without boat */
2059 }
2060 }
2061
2062 /* if moving didn't kill us, and we got to the destination, handle it. */
2063 if (alive && real_map_distance(dest_city->tile, unit_tile(punit)) <= 1) {
2064 /* release the boat! */
2065 if (unit_transported(punit)) {
2067 }
2069 punit, dest_city)) {
2070 /*
2071 * We really don't want to just drop all caravans in immediately.
2072 * Instead, we want to aggregate enough caravans to build instantly.
2073 * -AJS, 990704
2074 */
2075 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) helps build wonder in %s",
2078 punit->id,
2081 unit_do_action(pplayer, punit->id, dest_city->id,
2082 0, "", ACTION_HELP_WONDER);
2084 punit, dest_city)) {
2085 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) creates trade route in %s",
2088 punit->id,
2091 unit_do_action(pplayer, punit->id, dest_city->id,
2092 0, "", ACTION_TRADE_ROUTE);
2094 punit, dest_city)) {
2095 /* Get the one time bonus. */
2096 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) enters marketplace of %s",
2099 punit->id,
2102 unit_do_action(pplayer, punit->id, dest_city->id,
2103 0, "", ACTION_MARKETPLACE);
2104 } else {
2105 enum log_level level = LOG_NORMAL;
2106
2107 if (help_wonder) {
2108 /* A Caravan ordered to help build wonder may arrive after
2109 * enough shields to build the wonder is produced. */
2111 }
2112
2114 "%s %s[%d](%d,%d) unable to trade with %s",
2117 punit->id,
2120 }
2121 }
2122}
2123
2124/**********************************************************************/
2128static void caravan_optimize_callback(const struct caravan_result *result,
2129 void *data)
2130{
2131 const struct unit *caravan = data;
2132
2133 log_base(LOG_CARAVAN3, "%s %s[%d](%d,%d) %s: %s %s worth %g",
2135 unit_rule_name(caravan),
2136 caravan->id,
2137 TILE_XY(unit_tile(caravan)),
2138 city_name_get(result->src),
2139 result->help_wonder ? "wonder in" : "trade to",
2140 city_name_get(result->dest),
2141 result->value);
2142}
2143
2144/**********************************************************************/
2148 struct unit *punit)
2149{
2150 struct tile *src = NULL, *dest = NULL, *src_home_city = NULL;
2151 struct city *phome_city = NULL;
2152 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2153
2154 if ((unit_data->task != AIUNIT_NONE)) {
2155 src = unit_tile(punit);
2157 if (phome_city != NULL) {
2159 }
2160 dest = punit->goto_tile;
2161
2162 if (src == NULL || dest == NULL) {
2163 return FALSE;
2164 }
2165
2166 /* If we have a home continent, and are not there.
2167 * (FIXME: well, why?)
2168 * (I guess because home continent is which we were supposed to leave,
2169 * not the target continent) */
2170 if (src_home_city != NULL
2172 return FALSE;
2173 }
2174
2175 if (!goto_is_sane(punit, dest)) {
2176 if (unit_transported(punit)) {
2177 /* If we're being transported */
2178 return FALSE;
2179 }
2180 if ((punit->birth_turn + 15 < game.info.turn)) {
2181 /* We are tired of waiting */
2183
2184 if (ferrys <= 0) {
2185 /* There are no ferrys available... give up */
2186 return TRUE;
2187 } else {
2188 if (punit->birth_turn + 20 < game.info.turn) {
2189 /* We are fed up! */
2190 return TRUE;
2191 }
2192 }
2193 }
2194 }
2195 }
2196
2197 return FALSE;
2198}
2199
2200/**********************************************************************/
2207 struct unit *punit)
2208{
2209 struct city *pcity = game_city_by_number(punit->homecity);
2210 Continent_id continent;
2211
2212 fc_assert(pcity != NULL);
2213
2214 if (unit_class_get(punit)->adv.ferry_types <= 0) {
2215 /* There is just no possible transporters. */
2216 return FALSE;
2217 }
2218 continent = tile_continent(pcity->tile);
2219
2220 /* Look for proper destination city at different continent. */
2221 city_list_iterate(pplayer->cities, acity) {
2222 if (can_cities_trade(pcity, acity)) {
2223 if (tile_continent(acity->tile) != continent) {
2224 return TRUE;
2225 }
2226 }
2228
2230 if (aplayer == pplayer || !aplayer->is_alive) {
2231 continue;
2232 }
2233 if (pplayers_allied(pplayer, aplayer)) {
2234 city_list_iterate(aplayer->cities, acity) {
2235 if (can_cities_trade(pcity, acity)) {
2236 if (tile_continent(acity->tile) != continent) {
2237 return TRUE;
2238 }
2239 }
2242 }
2243
2244 return FALSE;
2245}
2246
2247/**********************************************************************/
2251static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
2252{
2253 struct city *nearest = NULL;
2254 int min_dist = FC_INFINITY;
2255 struct tile *current_loc = unit_tile(punit);
2257 bool alive = TRUE;
2258
2260 struct tile *ctile = city_tile(pcity);
2261
2262 if (tile_continent(ctile) == continent) {
2264
2265 if (this_dist < min_dist) {
2267 nearest = pcity;
2268 }
2269 }
2271
2272 if (nearest != NULL) {
2274 if (alive && same_pos(unit_tile(punit), nearest->tile)) {
2276 }
2277 }
2278
2279 return alive;
2280}
2281
2282/**********************************************************************/
2288static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
2289 struct unit *punit)
2290{
2291 struct caravan_parameter parameter;
2292 struct caravan_result result;
2293 const struct city *homecity;
2294 const struct city *dest = NULL;
2295 struct unit_ai *unit_data;
2297 bool expect_boats = pclass->adv.ferry_types > 0;
2298 /* TODO: will pplayer have a boat for punit in a reasonable time? */
2299 bool help_wonder = FALSE;
2300 bool required_boat = FALSE;
2301 bool request_boat = FALSE;
2303 const struct civ_map *nmap = &(wld.map);
2304
2306
2310 /* we only want units that can establish trade, enter marketplace or
2311 * help build wonders */
2312 return;
2313 }
2314
2316
2317 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) task %s to (%d,%d)",
2322
2323 homecity = game_city_by_number(punit->homecity);
2324 if (homecity == NULL && unit_data->task == AIUNIT_TRADE) {
2326 return;
2327 }
2328 homecity = game_city_by_number(punit->homecity);
2329 if (homecity == NULL) {
2330 return;
2331 }
2332 }
2333
2334 if ((unit_data->task == AIUNIT_TRADE
2335 || unit_data->task == AIUNIT_WONDER)) {
2336 /* we are moving to our destination */
2337 /* we check to see if our current goal is feasible */
2339
2340 if ((city_dest == NULL)
2342 || (unit_data->task == AIUNIT_TRADE
2343 && !(can_cities_trade(homecity, city_dest)
2344 && can_establish_trade_route(homecity, city_dest)))
2345 || (unit_data->task == AIUNIT_WONDER
2346 /* Helping the (new) production is illegal. */
2348 || (unit_data->task == AIUNIT_TRADE
2353 punit, city_dest)))
2354 || (unit_data->task == AIUNIT_WONDER
2357 punit, city_dest))) {
2358 /* destination invalid! */
2360 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) destination invalid!",
2363 } else {
2364 /* destination valid, are we tired of waiting for a boat? */
2368 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) unit tired of waiting!",
2372 } else {
2373 dest = city_dest;
2374 help_wonder = (unit_data->task == AIUNIT_WONDER) ? TRUE : FALSE;
2375 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2377 }
2378 }
2379 }
2380
2381 if (unit_data->task == AIUNIT_NONE) {
2382 if (homecity == NULL) {
2383 /* FIXME: We shouldn't bother in getting homecity for
2384 * caravan that will then be used for wonder building. */
2386 return;
2387 }
2388 homecity = game_city_by_number(punit->homecity);
2389 if (homecity == NULL) {
2390 return;
2391 }
2392 }
2393
2395 /* Make more trade with allies than other peaceful nations
2396 * by considering only allies 50% of the time.
2397 * (the other 50% allies are still considered, but so are other nations) */
2398 if (fc_rand(2)) {
2399 /* Be optimistic about development of relations with no-contact and
2400 * cease-fire nations. */
2401 parameter.allow_foreign_trade = FTL_NONWAR;
2402 } else {
2403 parameter.allow_foreign_trade = FTL_ALLIED;
2404 }
2405
2408 parameter.callback_data = punit;
2409 }
2411 parameter.ignore_transit_time = TRUE;
2412 }
2415 parameter.ignore_transit_time = FALSE;
2416 }
2417 caravan_find_best_destination(nmap, punit, &parameter, &result,
2418 !has_handicap(pplayer, H_MAP));
2419 if (result.dest != NULL) {
2420 /* We did find a new destination for the unit */
2421 dest = result.dest;
2422 help_wonder = result.help_wonder;
2423 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2424 request_boat = required_boat;
2426 (help_wonder) ? AIUNIT_WONDER : AIUNIT_TRADE,
2427 dest->tile);
2428 } else {
2429 dest = NULL;
2430 }
2431 }
2432
2433 if (required_boat && !expect_boats) {
2434 /* Would require boat, but can't have them. Render destination invalid. */
2435 dest = NULL;
2436 }
2437
2438 if (dest != NULL) {
2439 dai_caravan_goto(ait, pplayer, punit, dest, help_wonder,
2440 required_boat, request_boat);
2441 return; /* that may have clobbered the unit */
2442 } else {
2443 /* We have nowhere to go! */
2444 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d), nothing to do!",
2448 /* Should we become a defensive unit? */
2449 }
2450}
2451
2452/**********************************************************************/
2457 struct unit *punit)
2458{
2459 struct player *pplayer = unit_owner(punit);
2460 struct city *pcity = tile_city(unit_tile(punit));
2461 struct city *safe = NULL;
2462 const struct unit_type *punittype = unit_type_get(punit);
2463 bool no_recovery;
2464
2466
2467 if (pcity != NULL) {
2468 /* Rest in the city until the hitpoints are recovered, but attempt
2469 * to protect city from attack (and be opportunistic too)*/
2472 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "recovering hit points.");
2473 } else {
2474 return; /* We died heroically defending our city */
2475 }
2476 } else {
2477 /* Goto to nearest city to recover hit points */
2478 /* Just before, check to see if we can occupy an undefended enemy city */
2481 return; /* Oops, we died */
2482 }
2483
2484 /* Find a city to stay and go there */
2486 if (safe) {
2487 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "going to %s to recover",
2489 if (!dai_unit_goto(ait, punit, safe->tile)) {
2490 log_base(LOGLEVEL_RECOVERY, "died trying to hide and recover");
2491 return;
2492 }
2493 } else {
2494 /* Oops */
2495 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "didn't find a city to recover in!");
2497 dai_military_attack(ait, pplayer, punit);
2498 return;
2499 }
2500 }
2501
2502 /* Is the unit still damaged? If true, and could recover hit points, do so.
2503 * Don't wait if would be losing hitpoints that way! */
2505 if (punit->hp == punittype->hp) {
2506 no_recovery = TRUE;
2507 } else {
2509
2510 if (gain < 0) {
2511 no_recovery = TRUE;
2512 } else if (gain == 0 && !punit->moved) {
2513 /* Isn't gaining even though has not moved. */
2514 no_recovery = TRUE;
2515 }
2516 }
2517 if (no_recovery) {
2518 /* We are ready to go out and kick ass again */
2519 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "ready to kick ass again!");
2521 return;
2522 } else {
2523 def_ai_unit_data(punit, ait)->done = TRUE; /* Sit tight */
2524 }
2525}
2526
2527/**********************************************************************/
2531void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap,
2532 struct player *pplayer, struct unit *punit)
2533{
2534 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2535 int id = punit->id;
2536
2538
2539 /* "Escorting" aircraft should not do anything. They are activated
2540 * by their transport or charge. We do _NOT_ set them to 'done'
2541 * since they may need be activated once our charge moves. */
2542 if (unit_data->task == AIUNIT_ESCORT
2544 return;
2545 }
2546
2549 && has_handicap(pplayer, H_AWAY)) {
2550 /* Don't move sentried or fortified units controlled by a player
2551 * in away mode. */
2552 unit_data->done = TRUE;
2553 return;
2554 }
2555
2556 /* Since military units re-evaluate their actions every turn,
2557 we must make sure that previously reserved ferry is freed. */
2559
2561 /* Try hunting with this unit */
2562 if (dai_hunter_qualify(pplayer, punit)) {
2563 int result, sanity = punit->id;
2564
2565 UNIT_LOG(LOGLEVEL_HUNT, punit, "is qualified as hunter");
2566 result = dai_hunter_manage(ait, pplayer, punit);
2569 return; /* died */
2570 }
2571 if (result == -1) {
2572 (void) dai_hunter_manage(ait, pplayer, punit); /* More carnage */
2574 return;
2575 } else if (result >= 1) {
2577 return; /* Done moving */
2578 } else if (unit_data->task == AIUNIT_HUNTER) {
2579 /* This should be very rare */
2581 }
2582 } else if (unit_data->task == AIUNIT_HUNTER) {
2584 }
2586
2587 /* Do we have a specific job for this unit? If not, we default
2588 * to attack. */
2589 dai_military_findjob(ait, nmap, pplayer, punit);
2590
2591 switch (unit_data->task) {
2593 case AIUNIT_BUILD_CITY:
2594 fc_assert(FALSE); /* This is not the place for this role */
2595 break;
2596 case AIUNIT_DEFEND_HOME:
2598 dai_military_defend(ait, pplayer, punit);
2600 break;
2601 case AIUNIT_ATTACK:
2602 case AIUNIT_NONE:
2604 dai_military_attack(ait, pplayer, punit);
2606 break;
2607 case AIUNIT_ESCORT:
2609 dai_military_bodyguard(ait, pplayer, punit);
2611 break;
2612 case AIUNIT_EXPLORE:
2613 switch (manage_auto_explorer(punit)) {
2614 case MR_DEATH:
2615 /* don't use punit! */
2616 return;
2617 case MR_OK:
2618 UNIT_LOG(LOG_DEBUG, punit, "more exploring");
2619 break;
2620 default:
2621 UNIT_LOG(LOG_DEBUG, punit, "no more exploring either");
2622 break;
2623 };
2624 def_ai_unit_data(punit, ait)->done = (punit->moves_left <= 0);
2625 break;
2626 case AIUNIT_RECOVER:
2630 break;
2631 case AIUNIT_HUNTER:
2632 fc_assert(FALSE); /* dealt with above */
2633 break;
2634 default:
2636 }
2637
2638 /* If we are still alive, either sentry or fortify. */
2639 if ((punit = game_unit_by_number(id))) {
2641 struct city *pcity = tile_city(unit_tile(punit));
2642
2643 if (unit_list_find(unit_tile(punit)->units,
2644 unit_data->ferryboat)) {
2646 } else if (pcity || punit->activity == ACTIVITY_IDLE) {
2647 /* We do not need to fortify in cities - we fortify and sentry
2648 * according to home defense setup, for easy debugging. */
2649 if (!pcity || unit_data->task == AIUNIT_DEFEND_HOME) {
2653 }
2654 } else {
2656 }
2657 }
2658 }
2659}
2660
2661/**********************************************************************/
2664static void dai_manage_settler(struct ai_type *ait, struct player *pplayer,
2665 struct unit *punit)
2666{
2667 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2668
2670 unit_data->done = TRUE; /* we will manage this unit later... ugh */
2671 /* if BUILD_CITY must remain BUILD_CITY, otherwise turn into autosettler */
2672 if (unit_data->task == AIUNIT_NONE) {
2674 }
2675 return;
2676}
2677
2678/**********************************************************************/
2686void dai_manage_unit(struct ai_type *ait, struct player *pplayer,
2687 struct unit *punit)
2688{
2689 struct unit_ai *unit_data;
2690 struct unit *bodyguard = aiguard_guard_of(ait, punit);
2691 bool is_ferry = FALSE;
2692 const struct unit_type *ptype;
2693 const struct civ_map *nmap = &(wld.map);
2694
2696
2697 /* Don't manage the unit if it is under human orders. */
2698 if (unit_has_orders(punit)) {
2700
2701 UNIT_LOG(LOG_VERBOSE, punit, "is under human orders, aborting AI control.");
2703 unit_data->done = TRUE;
2704 return;
2705 }
2706
2707 /* Check if we have lost our bodyguard. If we never had one, all
2708 * fine. If we had one and lost it, ask for a new one. */
2709 if (!bodyguard && aiguard_has_guard(ait, punit)) {
2710 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "lost bodyguard, asking for new");
2712 }
2713
2715
2716 if (punit->moves_left <= 0) {
2717 /* Can do nothing */
2718 unit_data->done = TRUE;
2719 return;
2720 }
2721
2722 is_ferry = dai_is_ferry(punit, ait);
2723
2725
2728 dai_manage_diplomat(ait, pplayer, punit);
2730 return;
2731 } else if (ptype->adv.worker
2733 dai_manage_settler(ait, pplayer, punit);
2734 return;
2739 dai_manage_caravan(ait, pplayer, punit);
2741 return;
2743 dai_manage_barbarian_leader(ait, pplayer, punit);
2744 return;
2747 dai_manage_paratrooper(ait, pplayer, punit);
2748 return;
2749 } else if (is_ferry && unit_data->task != AIUNIT_HUNTER) {
2751 dai_manage_ferryboat(ait, pplayer, punit);
2753 return;
2754 } else if (utype_fuel(ptype)
2755 && unit_data->task != AIUNIT_ESCORT) {
2757 dai_manage_airunit(ait, pplayer, punit);
2759 return;
2760 } else if (is_losing_hp(punit)) {
2761 /* This unit is losing hitpoints over time */
2762
2763 /* TODO: We can try using air-unit code for helicopters, just
2764 * pretend they have fuel = HP / 3 or something. */
2765 unit_data->done = TRUE; /* we did our best, which was ...
2766 nothing */
2767 return;
2768 } else if (!is_special_unit(punit)) {
2770 UNIT_LOG(LOG_DEBUG, punit, "recruit unit for the military");
2771 dai_manage_military(ait, nmap, pplayer, punit);
2773 return;
2774 } else {
2775 /* what else could this be? -- Syela */
2776 switch (manage_auto_explorer(punit)) {
2777 case MR_DEATH:
2778 /* don't use punit! */
2779 break;
2780 case MR_OK:
2781 UNIT_LOG(LOG_DEBUG, punit, "now exploring");
2782 break;
2783 default:
2784 UNIT_LOG(LOG_DEBUG, punit, "fell through all unit tasks, defending");
2786 dai_military_defend(ait, pplayer, punit);
2787 break;
2788 };
2789 return;
2790 }
2791}
2792
2793/**********************************************************************/
2799static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
2800{
2801 city_list_iterate(pplayer->cities, pcity) {
2802 /* The idea here is that we should never keep more than two
2803 * units in permanent defense. */
2804 int total_defense = 0;
2805 int total_attack = def_ai_city_data(pcity, ait)->danger;
2806 bool emergency = FALSE;
2807 int count = 0;
2811 + pcity->feel[CITIZEN_ANGRY][FEELING_NATIONALITY];
2812 int entertainers = 0;
2813 bool enough = FALSE;
2814
2816 if (get_specialist_output(pcity, sp, O_LUXURY) > 0) {
2817 entertainers += pcity->specialists[sp];
2818 }
2820
2821 martless_unhappy += entertainers; /* We want to use martial law instead
2822 * of entertainers. */
2823
2824 while (!enough
2827 && martless_unhappy > mart_each * count))) {
2828 int best_want = 0;
2829 struct unit *best = NULL;
2830 bool defense_needed = total_defense <= total_attack; /* Defense or martial */
2831
2832 unit_list_iterate(pcity->tile->units, punit) {
2833 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2834
2835 if ((unit_data->task == AIUNIT_NONE || emergency)
2836 && unit_data->task != AIUNIT_DEFEND_HOME
2837 && unit_owner(punit) == pplayer) {
2838 int want = assess_defense_unit(ait, pcity, punit, FALSE);
2839
2840 if (want > best_want) {
2841 best_want = want;
2842 best = punit;
2843 }
2844 }
2846
2847 if (best == NULL) {
2848 if (defense_needed) {
2849 /* Ooops - try to grab any unit as defender! */
2850 if (emergency) {
2851 CITY_LOG(LOG_DEBUG, pcity, "Not defended properly");
2852 break;
2853 }
2854 emergency = TRUE;
2855 } else {
2856 break;
2857 }
2858 } else {
2859 const struct unit_type *btype = unit_type_get(best);
2860
2861 if ((martless_unhappy < mart_each * count
2862 || count >= mart_max || mart_each <= 0)
2863 && ((count >= 2
2864 && btype->attack_strength > btype->defense_strength)
2865 || (count >= 4
2866 && btype->attack_strength == btype->defense_strength))) {
2867 /* In this case attack would be better defense than fortifying
2868 * to city. */
2869 enough = TRUE;
2870 } else {
2871 int loglevel = pcity->server.debug ? LOG_AI_TEST : LOG_DEBUG;
2872
2874 UNIT_LOG(loglevel, best, "Defending city");
2875 dai_unit_new_task(ait, best, AIUNIT_DEFEND_HOME, pcity->tile);
2876 count++;
2877 }
2878 }
2879 }
2880 CITY_LOG(LOG_DEBUG, pcity, "Evaluating defense: %d defense, %d incoming"
2881 ", %d defenders (out of %d)", total_defense, total_attack, count,
2882 unit_list_size(pcity->tile->units));
2884}
2885
2886/**********************************************************************/
2894void dai_manage_units(struct ai_type *ait, struct player *pplayer)
2895{
2897 dai_airlift(ait, pplayer);
2899
2900 /* Clear previous orders, if desirable, here. */
2901 unit_list_iterate(pplayer->units, punit) {
2902 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2903
2904 unit_data->done = FALSE;
2905 if (unit_data->task == AIUNIT_DEFEND_HOME) {
2907 }
2909
2910 /* Find and set city defenders first - figure out which units are
2911 * allowed to leave home. */
2912 dai_set_defenders(ait, pplayer);
2913
2916 && !def_ai_unit_data(punit, ait)->done) {
2917 /* Though it is usually the passenger who drives the transport,
2918 * the transporter is responsible for managing its passengers. */
2919 dai_manage_unit(ait, pplayer, punit);
2920 }
2922}
2923
2924/**********************************************************************/
2930 const struct city *pcity)
2931{
2932 const struct impr_type *impr_req = NULL;
2933 const struct req_context context = {
2934 .player = city_owner(pcity),
2935 .city = pcity,
2936 .tile = city_tile(pcity),
2937 .unittype = putype,
2938 };
2939
2940 requirement_vector_iterate(&putype->build_reqs, preq) {
2942 /* Already there. */
2943 continue;
2944 }
2946 city_owner(pcity), pcity)) {
2947 /* The unit type can't be built at all. */
2948 return NULL;
2949 }
2950 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2951 /* This is (one of) the building(s) required. */
2952 impr_req = preq->source.value.building;
2953 }
2955
2956 return impr_req;
2957}
2958
2959/**********************************************************************/
2964 const struct unit_type *base)
2965{
2966 /* This is the real function: */
2967 do {
2968 base = base->obsoleted_by;
2969 if (base == test) {
2970 return TRUE;
2971 }
2972 } while (base);
2973 return FALSE;
2974}
2975
2976/**********************************************************************/
2981static void dai_manage_barbarian_leader(struct ai_type *ait,
2982 struct player *pplayer,
2983 struct unit *leader)
2984{
2986 struct pf_parameter parameter;
2987 struct pf_map *pfm;
2988 struct pf_reverse_map *pfrm;
2989 struct unit *worst_danger;
2990 int move_cost, best_move_cost;
2991 int body_guards;
2992 bool alive = TRUE;
2993 const struct civ_map *nmap = &(wld.map);
2994
2996
2997 if (leader->moves_left == 0
2999 && 1 < unit_list_size(leader_tile->units))) {
3001
3002 return;
3003 }
3004
3005 if (is_boss_of_boat(ait, leader)) {
3006 /* We are in charge. Of course, since we are the leader...
3007 * But maybe somebody more militaristic should lead our ship to battle! */
3008
3009 /* First release boat from leaders lead */
3011
3015 && warrior->moves_left > 0) {
3016 /* This seems like a good warrior to lead us in to conquest! */
3017 dai_manage_unit(ait, pplayer, warrior);
3018
3019 /* If we reached our destination, ferryboat already called
3020 * ai_manage_unit() for leader. So no need to continue here.
3021 * Leader might even be dead.
3022 * If this return is removed, surrounding unit_list_iterate()
3023 * has to be replaced with unit_list_iterate_safe()*/
3024 return;
3025 }
3027 }
3028
3029 /* If we are not in charge of the boat, continue as if we
3030 * were not in a boat - we may want to leave the ship now. */
3031
3032 /* Check the total number of units able to protect our leader. */
3033 body_guards = 0;
3034 unit_list_iterate(pplayer->units, punit) {
3037 body_guards++;
3038 }
3040
3041 if (0 < body_guards) {
3042 pft_fill_unit_parameter(&parameter, nmap, leader);
3043 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3044 pfm = pf_map_new(&parameter);
3045
3046 /* Find the closest body guard.
3047 * FIXME: maybe choose the strongest too? */
3048 pf_map_tiles_iterate(pfm, ptile, FALSE) {
3049 unit_list_iterate(ptile->units, punit) {
3050 if (unit_owner(punit) == pplayer
3053 struct pf_path *path = pf_map_path(pfm, ptile);
3054
3055 adv_follow_path(leader, path, ptile);
3056 pf_path_destroy(path);
3058 return;
3059 }
3062
3064 }
3065
3066 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader needs to flee");
3067
3068 /* Check for units we could fear. */
3069 pfrm = pf_reverse_map_new(nmap, pplayer, leader_tile, 3,
3070 !has_handicap(pplayer, H_MAP));
3073
3074 players_iterate(other_player) {
3075 if (other_player == pplayer) {
3076 continue;
3077 }
3078
3079 unit_list_iterate(other_player->units, punit) {
3081 if (PF_IMPOSSIBLE_MC != move_cost && move_cost < best_move_cost) {
3082 best_move_cost = move_cost;
3084 }
3087
3089
3090 if (NULL == worst_danger) {
3092 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: no close enemy.");
3093 return;
3094 }
3095
3097 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3098 pfm = pf_map_new(&parameter);
3100
3101 /* Try to escape. */
3102 do {
3104
3105 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: moves left: %d.",
3106 leader->moves_left);
3107
3110 continue;
3111 }
3112
3113 move_cost = pf_map_move_cost(pfm, near_tile);
3114 if (PF_IMPOSSIBLE_MC != move_cost
3115 && move_cost > best_move_cost) {
3117 "Barbarian leader: safest is (%d, %d), safeness %d",
3119 best_move_cost = move_cost;
3121 }
3123
3124 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: fleeing to (%d, %d).",
3128 "Barbarian leader: reached the safest position.");
3131 return;
3132 }
3133
3135 if (alive) {
3137 /* Didn't move. No point to retry. */
3139 return;
3140 }
3142 }
3143 } while (alive && 0 < leader->moves_left);
3144
3146}
3147
3148/**********************************************************************/
3154void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile,
3155 struct unit *punit,
3156 enum override_bool *result)
3157{
3158 int a = 0, d, db;
3159 struct player *pplayer = unit_owner(punit);
3160 struct city *pcity = tile_city(ptile);
3161 int extras_bonus = 0;
3162
3163 if (is_human(pplayer)) {
3164 /* Use advisors code for humans. */
3165 return;
3166 }
3167
3168 if (pcity && pplayers_allied(city_owner(pcity), unit_owner(punit))
3169 && !is_non_allied_unit_tile(ptile, pplayer)) {
3170 /* We will be safe in a friendly city */
3171 *result = OVERRIDE_FALSE;
3172 return;
3173 }
3174
3175 /* Calculate how well we can defend at (x,y) */
3176 db = 10 + tile_terrain(ptile)->defense_bonus / 10;
3178
3179 db += (db * extras_bonus) / 100;
3181
3182 adjc_iterate(&(wld.map), ptile, ptile1) {
3183 if (has_handicap(pplayer, H_FOG)
3185 /* We cannot see danger at (ptile1) => assume there is none */
3186 continue;
3187 }
3188 unit_list_iterate(ptile1->units, enemy) {
3191 == ATT_OK)
3193 == ATT_OK)) {
3195 if ((a * a * 10) >= d) {
3196 /* The enemies combined strength is too big! */
3197 *result = OVERRIDE_TRUE;
3198 return;
3199 }
3200 }
3203
3204 *result = OVERRIDE_FALSE;
3205}
3206
3207/**********************************************************************/
3210static void update_simple_ai_types(void)
3211{
3212 int i = 0;
3213
3216
3219 && !(pclass->adv.land_move == MOVE_NONE
3222 && punittype->transport_capacity < 8) {
3224 i++;
3225 }
3227
3229}
3230
3231/**********************************************************************/
3235{
3236 /* TODO: remove the simple_ai_types cache or merge it with a general ai
3237 * cache; see the comment to struct unit_type *simple_ai_types at
3238 * the beginning of this file. */
3240
3242 struct unit_type_ai *utai = fc_malloc(sizeof(*utai));
3243
3244 utai->low_firepower = FALSE;
3245 utai->ferry = FALSE;
3246 utai->missile_platform = FALSE;
3247 utai->carries_occupiers = FALSE;
3248 utai->potential_charges = unit_type_list_new();
3249
3252
3255
3256 /* Confirm firepower */
3258 if (pbonus->type == CBONUS_LOW_FIREPOWER) {
3260 if (utype_has_flag(penemy, pbonus->flag)) {
3261 struct unit_type_ai *utai = utype_ai_data(penemy, ait);
3262
3263 utai->low_firepower = TRUE;
3264 }
3266 }
3268
3269 /* Consider potential cargo */
3270 if (punittype->transport_capacity > 0) {
3271 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3272
3275
3278 utai->missile_platform = TRUE;
3279 } else if (pclass->adv.sea_move != MOVE_NONE
3280 && pcargo->adv.land_move != MOVE_NONE) {
3281 if (pcargo->adv.sea_move != MOVE_FULL) {
3282 utai->ferry = TRUE;
3283 } else {
3284 if (0 != utype_fuel(pctype)) {
3285 utai->ferry = TRUE;
3286 }
3287 }
3288 }
3289
3291 utai->carries_occupiers = TRUE;
3292 }
3293 }
3295 }
3296
3297 /* Consider potential charges */
3300
3301 if (0 < utype_fuel(punittype)
3302 && (0 == utype_fuel(pcharge)
3304 continue;
3305 }
3306
3307 unit_class_list_iterate(pclass->cache.subset_movers, chgcls) {
3308 if (chgcls == utype_class(pcharge)) {
3310 }
3312
3314 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3315 unit_type_list_append(utai->potential_charges, pcharge);
3316 }
3317
3320}
3321
3322/**********************************************************************/
3326{
3328 struct unit_type_ai *utai = utype_ai_data(ptype, ait);
3329
3330 if (utai == NULL) {
3331 continue;
3332 }
3334
3335 unit_type_list_destroy(utai->potential_charges);
3336 free(utai);
3338}
3339
3340/**********************************************************************/
3343void dai_unit_init(struct ai_type *ait, struct unit *punit)
3344{
3345 /* Make sure that contents of unit_ai structure are correctly initialized,
3346 * if you ever allocate it by some other mean than fc_calloc() */
3347 struct unit_ai *unit_data = fc_calloc(1, sizeof(struct unit_ai));
3348
3349 unit_data->done = FALSE;
3350 unit_data->cur_pos = NULL;
3351 unit_data->prev_pos = NULL;
3352 unit_data->target = 0;
3353 BV_CLR_ALL(unit_data->hunted);
3354 unit_data->ferryboat = 0;
3355 unit_data->passenger = 0;
3356 unit_data->bodyguard = 0;
3357 unit_data->charge = 0;
3358
3360}
3361
3362/**********************************************************************/
3365void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
3366{
3367 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3368
3370
3371 BV_CLR_ALL(unit_data->hunted);
3372}
3373
3374/**********************************************************************/
3377void dai_unit_close(struct ai_type *ait, struct unit *punit)
3378{
3379 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3380
3382
3385
3386 if (unit_data != NULL) {
3389 }
3390}
3391
3392/**********************************************************************/
3395void dai_unit_save(struct ai_type *ait, const char *aitstr,
3396 struct section_file *file,
3397 const struct unit *punit, const char *unitstr)
3398{
3399 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3400
3401 secfile_insert_int(file, unit_data->passenger, "%s.%spassenger",
3402 unitstr, aitstr);
3403 secfile_insert_int(file, unit_data->ferryboat, "%s.%sferryboat",
3404 unitstr, aitstr);
3405 secfile_insert_int(file, unit_data->charge, "%s.%scharge",
3406 unitstr, aitstr);
3407 secfile_insert_int(file, unit_data->bodyguard, "%s.%sbodyguard",
3408 unitstr, aitstr);
3409}
3410
3411/**********************************************************************/
3414void dai_unit_load(struct ai_type *ait, const char *aitstr,
3415 const struct section_file *file,
3416 struct unit *punit, const char *unitstr)
3417{
3418 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3419
3420 unit_data->passenger
3421 = secfile_lookup_int_default(file, 0, "%s.%spassenger",
3422 unitstr, aitstr);
3423 unit_data->ferryboat
3424 = secfile_lookup_int_default(file, 0, "%s.%sferryboat",
3425 unitstr, aitstr);
3426 unit_data->charge
3427 = secfile_lookup_int_default(file, 0, "%s.%scharge",
3428 unitstr, aitstr);
3429 unit_data->bodyguard
3430 = secfile_lookup_int_default(file, 0, "%s.%sbodyguard",
3431 unitstr, aitstr);
3432}
3433
3439
3440/**********************************************************************/
3443static bool role_unit_cb(struct unit_type *ptype, void *data)
3444{
3445 struct role_unit_cb_data *cb_data = (struct role_unit_cb_data *)data;
3447 const struct civ_map *nmap = &(wld.map);
3448
3449 if ((cb_data->tc == TC_LAND && pclass->adv.land_move == MOVE_NONE)
3450 || (cb_data->tc == TC_OCEAN && pclass->adv.sea_move == MOVE_NONE)) {
3451 return FALSE;
3452 }
3453
3454 if (cb_data->build_city == NULL
3455 || can_city_build_unit_now(nmap, cb_data->build_city, ptype)) {
3456 return TRUE;
3457 }
3458
3459 return FALSE;
3460}
3461
3462/**********************************************************************/
3466 enum terrain_class tc)
3467{
3468 struct role_unit_cb_data cb_data = { .build_city = pcity, .tc = tc };
3469
3471}
3472
3473/**********************************************************************/
3476bool dai_unit_can_strike_my_unit(const struct unit *attacker,
3477 const struct unit *defender)
3478{
3479 struct pf_parameter parameter;
3480 struct pf_map *pfm;
3481 const struct tile *ptarget = unit_tile(defender);
3482 int max_move_cost = attacker->moves_left;
3483 bool able_to_strike = FALSE;
3484 const struct civ_map *nmap = &(wld.map);
3485
3486 pft_fill_unit_parameter(&parameter, nmap, attacker);
3487 parameter.omniscience = !has_handicap(unit_owner(defender), H_MAP);
3488 pfm = pf_map_new(&parameter);
3489
3490 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
3491 if (move_cost > max_move_cost) {
3492 break;
3493 }
3494
3495 if (ptile == ptarget) {
3497 break;
3498 }
3500
3502
3503 return able_to_strike;
3504}
3505
3506/**********************************************************************/
3509void dai_switch_to_explore(struct ai_type *ait, struct unit *punit,
3510 struct tile *target, enum override_bool *allow)
3511{
3512 struct unit_ai *udata = def_ai_unit_data(punit, ait);
3513
3514 if (udata->task != AIUNIT_NONE && udata->task != AIUNIT_EXPLORE) {
3516
3517 return;
3518 }
3519}
enum gen_action select_actres_action_unit_on_stack(struct civ_map *nmap, enum action_result actres, struct unit *punit, struct tile *ptile)
Definition actions.c:8156
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:3841
static struct action * action_by_number(action_id act_id)
Definition actions.h:635
#define ACTION_NONE
Definition actions.h:311
#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:489
void dai_manage_diplomat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition aidiplomat.c:723
bool aiferry_gobyboat(struct ai_type *ait, struct player *pplayer, struct unit *punit, struct tile *dest_tile, bool with_bodyguard)
Definition aiferry.c:764
int aiferry_find_boat(struct ai_type *ait, struct unit *punit, int cap, struct pf_path **path)
Definition aiferry.c:495
void dai_manage_ferryboat(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition aiferry.c:1113
bool is_boat_free(struct ai_type *ait, struct unit *boat, struct unit *punit, int cap)
Definition aiferry.c:438
bool aiferry_goto_amphibious(struct ai_type *ait, struct unit *ferry, struct unit *passenger, struct tile *ptile)
Definition aiferry.c:730
bool is_boss_of_boat(struct ai_type *ait, struct unit *punit)
Definition aiferry.c:471
int aiferry_avail_boats(struct ai_type *ait, struct player *pplayer)
Definition aiferry.c:353
bool dai_is_ferry(struct unit *pferry, struct ai_type *ait)
Definition aiferry.c:159
void aiferry_clear_boat(struct ai_type *ait, struct unit *punit)
Definition aiferry.c:251
struct city * aiguard_charge_city(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:289
void aiguard_clear_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:117
void aiguard_request_guard(struct ai_type *ait, struct unit *punit)
Definition aiguard.c:227
bool aiguard_has_guard(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:259
void aiguard_assign_guard_unit(struct ai_type *ait, struct unit *charge, struct unit *guard)
Definition aiguard.c:173
bool aiguard_wanted(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:241
void aiguard_assign_guard_city(struct ai_type *ait, struct city *charge, struct unit *guard)
Definition aiguard.c:196
struct unit * aiguard_charge_unit(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:279
void aiguard_clear_guard(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:146
struct unit * aiguard_guard_of(struct ai_type *ait, struct unit *charge)
Definition aiguard.c:269
bool aiguard_has_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:250
void aiguard_update_charge(struct ai_type *ait, struct unit *guard)
Definition aiguard.c:299
#define CHECK_GUARD(ait, guard)
Definition aiguard.h:21
bool dai_hunter_qualify(struct player *pplayer, struct unit *punit)
Definition aihunt.c:290
int dai_hunter_manage(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition aihunt.c:438
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:1307
bool goto_is_sane(struct unit *punit, struct tile *ptile)
Definition aitools.c:430
bool dai_unit_attack(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Definition aitools.c:818
bool dai_unit_goto(struct ai_type *ait, struct unit *punit, struct tile *ptile)
Definition aitools.c:606
void dai_unit_new_task(struct ai_type *ait, struct unit *punit, enum ai_unit_task task, struct tile *ptile)
Definition aitools.c:644
bool dai_assess_military_unhappiness(const struct civ_map *nmap, struct city *pcity)
Definition aitools.c:1442
bool dai_unit_make_homecity(struct unit *punit, struct city *pcity)
Definition aitools.c:756
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:245
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:752
void caravan_parameter_init_from_unit(struct caravan_parameter *parameter, const struct unit *caravan)
Definition caravan.c:53
@ FTL_ALLIED
Definition caravan.h:31
@ FTL_NATIONAL_ONLY
Definition caravan.h:30
@ FTL_NONWAR
Definition caravan.h:33
bool city_production_gets_caravan_shields(const struct universal *tgt)
Definition city.c:1860
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
int city_production_unit_veteran_level(struct city *pcity, const struct unit_type *punittype)
Definition city.c:804
bool can_city_build_unit_now(const struct civ_map *nmap, const struct city *pcity, const struct unit_type *punittype)
Definition city.c:947
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
@ CITIZEN_ANGRY
Definition city.h: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:856
double unit_win_chance(const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender, const struct action *paction)
Definition combat.c:478
bool is_stack_vulnerable(const struct tile *ptile)
Definition combat.c:989
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:712
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:767
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:836
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:75
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:91
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:3395
static bool invasion_funct(struct ai_type *ait, struct unit *punit, bool dest, int radius, int which)
Definition daiunit.c:1047
static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:882
static bool has_defense(struct city *pcity)
Definition daiunit.c:222
adv_want kill_desire(adv_want benefit, int attack, int loss, int vuln, int victim_count)
Definition daiunit.c:342
static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2251
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:2016
int unittype_def_rating_squared(const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
Definition daiunit.c:304
static bool unit_role_defender(const struct unit_type *punittype)
Definition daiunit.c:730
void dai_units_ruleset_init(struct ai_type *ait)
Definition daiunit.c:3234
#define LOG_CARAVAN
Definition daiunit.c:88
static int unit_att_rating_now(const struct unit *punit)
Definition daiunit.c:259
enum gen_action dai_select_tile_attack_action(struct civ_map *nmap, struct unit *punit, struct tile *ptile)
Definition daiunit.c:445
static void dai_military_attack_barbarian(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1715
static void update_simple_ai_types(void)
Definition daiunit.c:3210
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Definition daiunit.c:2929
void dai_switch_to_explore(struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
Definition daiunit.c:3509
void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2531
static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:1932
static void reinforcements_cost_and_value(struct unit *punit, struct tile *ptile0, int *value, int *cost)
Definition daiunit.c:375
struct unit_type * simple_ai_types[U_LAST]
Definition daiunit.c:126
static void dai_airlift(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:169
int build_cost_balanced(const struct unit_type *punittype)
Definition daiunit.c:250
bool dai_unit_can_strike_my_unit(const struct unit *attacker, const struct unit *defender)
Definition daiunit.c:3476
bool is_on_unit_upgrade_path(const struct unit_type *test, const struct unit_type *base)
Definition daiunit.c:2963
static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:664
static void single_invader(struct ai_city *city_data, const struct unit_type *utype, int which)
Definition daiunit.c:1019
#define LOG_CARAVAN3
Definition daiunit.c:90
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2894
static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2288
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:1168
static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2799
void dai_manage_unit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2686
adv_want look_for_charge(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity)
Definition daiunit.c:745
static bool role_unit_cb(struct unit_type *ptype, void *data)
Definition daiunit.c:3443
static void dai_military_attack(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1778
void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3365
static void dai_manage_settler(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2664
static int avg_benefit(int benefit, int loss, double chance)
Definition daiunit.c:366
#define LOG_CARAVAN2
Definition daiunit.c:89
static struct city * find_neediest_airlift_city(struct ai_type *ait, const struct player *pplayer)
Definition daiunit.c:138
static int unit_def_rating(const struct unit *attacker, const struct unit *defender)
Definition daiunit.c:278
static bool dai_is_unit_tired_waiting_boat(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2147
static int dai_rampage_want(struct unit *punit, struct tile *ptile)
Definition daiunit.c:485
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:864
struct city * find_nearest_safe_city(struct unit *punit)
Definition daiunit.c:1664
static void dai_manage_barbarian_leader(struct ai_type *ait, struct player *pplayer, struct unit *leader)
Definition daiunit.c:2981
static void dai_manage_hitpoint_recovery(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2456
static int unit_att_rating_squared(const struct unit *punit)
Definition daiunit.c:268
static struct pf_path * find_rampage_target(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:552
static bool is_my_turn(struct unit *punit, struct unit *pdef)
Definition daiunit.c:400
static int unit_def_rating_squared(const struct unit *punit, const struct unit *pdef)
Definition daiunit.c:291
void dai_unit_init(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3343
void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
Definition daiunit.c:3154
bool uclass_need_trans_between(struct unit_class *pclass, struct tile *ctile, struct tile *ptile)
Definition daiunit.c:1977
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:3414
static void caravan_optimize_callback(const struct caravan_result *result, void *data)
Definition daiunit.c:2128
#define LOGLEVEL_RECOVERY
Definition daiunit.c:87
void dai_unit_close(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3377
void dai_units_ruleset_close(struct ai_type *ait)
Definition daiunit.c:3325
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Definition daiunit.c:3465
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:1093
static void dai_military_defend(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:973
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:631
static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer, struct unit *punit)
Definition daiunit.c:2206
#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:3018
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2940
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:1354
@ RPT_CERTAIN
Definition fc_types.h:701
@ AUT_AUTO_SETTLER
Definition fc_types.h:370
#define ADV_WANT_PRINTF
Definition fc_types.h:1355
@ O_LUXURY
Definition fc_types.h:101
signed short Continent_id
Definition fc_types.h:372
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:940
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:949
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:735
#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:722
#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:351
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:363
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:523
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:900
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:88
@ MR_OK
Definition movement.h:35
@ MR_DEATH
Definition movement.h:36
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
struct nation_type * nation_of_unit(const struct unit *punit)
Definition nation.c:463
void pf_path_destroy(struct pf_path *path)
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
struct pf_path * pf_map_path(struct pf_map *pfm, struct tile *ptile)
bool pf_map_position(struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
void pf_reverse_map_destroy(struct pf_reverse_map *pfrm)
int pf_reverse_map_unit_move_cost(struct pf_reverse_map *pfrm, const struct unit *punit)
void pf_map_destroy(struct pf_map *pfm)
struct pf_reverse_map * pf_reverse_map_new(const struct civ_map *nmap, const struct player *pplayer, struct tile *target_tile, int max_turns, bool omniscient)
int pf_map_move_cost(struct pf_map *pfm, struct tile *ptile)
#define PF_IMPOSSIBLE_MC
#define pf_map_move_costs_iterate_end
#define pf_map_move_costs_iterate(ARG_pfm, NAME_tile, NAME_cost, COND_from_start)
#define pf_map_tiles_iterate(ARG_pfm, NAME_tile, COND_from_start)
#define pf_map_tiles_iterate_end
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c: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:174
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:3436
struct city * build_city
Definition daiunit.c:3437
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:114
#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:2298
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2378
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2236
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:194
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2449
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2191
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2649
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:367
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:377
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:305
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:876
bool is_special_unit(const struct unit *punit)
Definition unit.c:358
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2433
bool is_guard_unit(const struct unit *punit)
Definition unit.c:348
bool unit_has_orders(const struct unit *punit)
Definition unit.c:210
#define unit_tile(_pu)
Definition unit.h:397
#define unit_cargo_iterate_end
Definition unit.h:573
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:570
#define CHECK_UNIT(punit)
Definition unit.h:270
#define unit_owner(_pu)
Definition unit.h:396
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.h:432
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Definition unithand.c:6471
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:3293
bool unit_server_side_agent_set(struct player *pplayer, struct unit *punit, enum server_side_agent agent)
Definition unithand.c:6370
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:2298
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2499
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_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:2739
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:2747
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:839
#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:940
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:932
#define unit_type_iterate(_p)
Definition unittype.h:855
#define unit_type_list_iterate_end
Definition unittype.h:942
#define U_LAST
Definition unittype.h:40
#define unit_type_iterate_end
Definition unittype.h:862
#define unit_class_list_iterate_end
Definition unittype.h:934