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/**********************************************************************/
457static int dai_rampage_want(struct unit *punit, struct tile *ptile)
458{
459 struct player *pplayer = unit_owner(punit);
460 struct unit *pdef;
461 struct civ_map *nmap = &(wld.map);
462
464
465 if (can_unit_attack_tile(punit, NULL, ptile)
466 && (pdef = get_defender(nmap, punit, ptile, NULL))
467 /* Action enablers might prevent attacking */
469 punit, ptile)) {
470 /* See description of kill_desire() about these variables. */
474
475 attack *= attack;
476
477 /* If the victim is in the city/fortress, we correct the benefit
478 * with our health because there could be reprisal attacks. We
479 * shouldn't send already injured units to useless suicide.
480 * Note that we do not specially encourage attacks against
481 * cities: rampage is a hit-n-run operation. */
482 if (!is_stack_vulnerable(ptile)
483 && unit_list_size(ptile->units) > 1) {
485 }
486
487 /* If we have non-zero attack rating... */
488 if (attack > 0 && is_my_turn(punit, pdef)) {
491
492 /* No need to amortize, our operation takes one turn. */
493 UNIT_LOG(LOG_DEBUG, punit, "Rampage: Desire %d to kill %s(%d,%d)",
494 desire,
497
498 return MAX(0, desire);
499 }
500 } else if (0 == unit_list_size(ptile->units)) {
501 /* No defender. */
502 struct city *pcity = tile_city(ptile);
503
504 /* ...and free foreign city waiting for us. Who would resist! */
505 if (NULL != pcity
506 && pplayers_at_war(pplayer, city_owner(pcity))
509 }
510
511 /* ...or tiny pleasant hut here! */
512 /* FIXME: unhardcode and variate the desire to enter a hut. */
513 if (unit_can_enter_hut(punit, ptile) && !is_barbarian(pplayer)
514 && is_native_tile(unit_type_get(punit), ptile)) {
515 return -RAMPAGE_HUT_OR_BETTER;
516 }
517 }
518
519 return 0;
520}
521
522/**********************************************************************/
525static struct pf_path *find_rampage_target(struct unit *punit,
526 int thresh_adj, int thresh_move)
527{
528 struct pf_map *tgt_map;
529 struct pf_path *path = NULL;
530 struct pf_parameter parameter;
531 /* Coordinates of the best target (initialize to silence compiler) */
532 struct tile *ptile = unit_tile(punit);
533 /* Want of the best target */
534 int max_want = 0;
535 struct player *pplayer = unit_owner(punit);
536 const struct civ_map *nmap = &(wld.map);
537
539 parameter.omniscience = !has_handicap(pplayer, H_MAP);
540 /* When trying to find rampage targets we ignore risks such as
541 * enemy units because we are looking for trouble!
542 * Hence no call ai_avoid_risks()
543 */
544
545 tgt_map = pf_map_new(&parameter);
547 int want;
548 bool move_needed;
549 int thresh;
550
551 if (move_cost > punit->moves_left) {
552 /* This is too far */
553 break;
554 }
555
556 if (has_handicap(pplayer, H_TARGETS)
557 && !map_is_known_and_seen(iter_tile, pplayer, V_MAIN)) {
558 /* The target is under fog of war */
559 continue;
560 }
561
563
564 /* Negative want means move needed even though the tiles are adjacent */
566 || want < 0);
567 /* Select the relevant threshold */
569 want = (want < 0 ? -want : want);
570
571 if (want > max_want && want > thresh) {
572 /* The new want exceeds both the previous maximum
573 * and the relevant threshold, so it's worth recording */
574 max_want = want;
575 ptile = iter_tile;
576 }
578
579 if (max_want > 0) {
580 /* We found something */
581 path = pf_map_path(tgt_map, ptile);
582 fc_assert(path != NULL);
583 }
584
586
587 return path;
588}
589
590/**********************************************************************/
605 int thresh_move)
606{
607 int count = punit->moves_left + 1; /* break any infinite loops */
608 struct pf_path *path = NULL;
609
612
614 /* This teaches the AI about the dangers inherent in occupychance. */
616
617 while (count-- > 0 && punit->moves_left > 0
619 if (!adv_unit_execute_path(punit, path)) {
620 /* Died */
621 count = -1;
622 }
623 pf_path_destroy(path);
624 path = NULL;
625 }
626
627 fc_assert(NULL == path);
628
630 return (count >= 0);
631}
632
633/**********************************************************************/
637static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer,
638 struct unit *punit)
639{
640 struct unit *aunit = aiguard_charge_unit(ait, punit);
641 struct city *acity = aiguard_charge_city(ait, punit);
642 struct tile *ptile;
643
645 CHECK_GUARD(ait, punit);
646
647 if (aunit && unit_owner(aunit) == unit_owner(punit)) {
648 /* protect a unit */
649 if (aunit->goto_tile != NULL) {
650 /* Our charge is going somewhere: maybe we should meet them there */
651 /* FIXME: This probably isn't the best algorithm for this. */
653 int me2goal = real_map_distance(unit_tile(punit), aunit->goto_tile);
654 int them2goal = real_map_distance(unit_tile(aunit), aunit->goto_tile);
655
656 if (me2goal < me2them
660 ptile = aunit->goto_tile;
661 } else {
662 ptile = unit_tile(aunit);
663 }
664 } else {
665 ptile = unit_tile(aunit);
666 }
667 } else if (acity && city_owner(acity) == unit_owner(punit)) {
668 /* protect a city */
669 ptile = acity->tile;
670 } else {
671 /* should be impossible */
672 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "we lost our charge");
674 return;
675 }
676
677 if (same_pos(unit_tile(punit), ptile)) {
678 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "at RV");
679 } else {
680 if (goto_is_sane(punit, ptile)) {
681 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "meeting charge");
682 if (!dai_gothere(ait, pplayer, punit, ptile)) {
683 /* We died */
684 return;
685 }
686 } else {
687 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "can not meet charge");
689 }
690 }
691 /* We might have stopped because of an enemy nearby.
692 * Perhaps we can kill it.*/
695 && same_pos(unit_tile(punit), ptile)) {
696 def_ai_unit_data(punit, ait)->done = TRUE; /* Stay with charge */
697 }
698}
699
700/**********************************************************************/
703static bool unit_role_defender(const struct unit_type *punittype)
704{
706}
707
708/**********************************************************************/
718adv_want look_for_charge(struct ai_type *ait, const struct civ_map *nmap,
719 struct player *pplayer, struct unit *punit,
720 struct unit **aunit, struct city **acity)
721{
722 struct pf_parameter parameter;
723 struct pf_map *pfm;
724 struct city *pcity;
725 struct ai_city *data, *best_data = NULL;
727 int def, best_def = -1;
728 /* Arbitrary: 3 turns. */
729 const int max_move_cost = 3 * unit_move_rate(punit);
730
731 *aunit = NULL;
732 *acity = NULL;
733
734 if (0 == toughness) {
735 /* useless */
736 return 0;
737 }
738
739 pft_fill_unit_parameter(&parameter, nmap, punit);
740 parameter.omniscience = !has_handicap(pplayer, H_MAP);
741 pfm = pf_map_new(&parameter);
742
743 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
744 if (move_cost > max_move_cost) {
745 /* Consider too far. */
746 break;
747 }
748
749 pcity = tile_city(ptile);
750
751 /* Consider unit bodyguard. */
752 unit_list_iterate(ptile->units, buddy) {
753 const struct unit_type *ptype = unit_type_get(punit);
754 const struct unit_type *buddy_type = unit_type_get(buddy);
755
756 /* TODO: allied unit bodyguard? */
758 || unit_owner(buddy) != pplayer
759 || !aiguard_wanted(ait, buddy)
765
766 continue;
767 }
768
770 if (0 >= def) {
771 continue;
772 }
773
775 /* Reduce want based on move cost. We can't do this for
776 * transports since they move around all the time, leading
777 * to hilarious flip-flops. */
778 def >>= move_cost / (2 * unit_move_rate(punit));
779 }
780 if (def > best_def) {
781 *aunit = buddy;
782 *acity = NULL;
783 best_def = def;
784 }
786
787 /* City bodyguard. TODO: allied city bodyguard? */
788 if (ai_fuzzy(pplayer, TRUE)
789 && NULL != pcity
790 && city_owner(pcity) == pplayer
791 && (data = def_ai_city_data(pcity, ait))
792 && 0 < data->urgency) {
793 if (NULL != best_data
794 && (0 < best_data->grave_danger
795 || best_data->urgency > data->urgency
796 || ((best_data->danger > data->danger
797 || AIUNIT_DEFEND_HOME == def_ai_unit_data(punit, ait)->task)
798 && 0 == data->grave_danger))) {
799 /* Chances are we'll be between cities when we are needed the most!
800 * Resuming pf_map_move_costs_iterate()... */
801 continue;
802 }
803 def = (data->danger - assess_defense_quadratic(ait, pcity));
804 if (def <= 0) {
805 continue;
806 }
807 /* Reduce want based on move cost. */
808 def >>= move_cost / (2 * unit_move_rate(punit));
809 if (def > best_def && ai_fuzzy(pplayer, TRUE)) {
810 *acity = pcity;
811 *aunit = NULL;
812 best_def = def;
813 best_data = data;
814 }
815 }
817
819
820 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "%s(), best_def=%d, type=%s (%d, %d)",
823 : (NULL != *aunit ? unit_rule_name(*aunit) : "")),
825 : (NULL != *aunit ?
828 : (NULL != *aunit ?
830
831 return ((best_def * 100) / toughness);
832}
833
834/**********************************************************************/
838 const struct unit_type *followee,
839 struct ai_type *ait)
840{
841 struct unit_type_ai *utai = utype_ai_data(follower, ait);
842
843 unit_type_list_iterate(utai->potential_charges, pcharge) {
844 if (pcharge == followee) {
845 return TRUE;
846 }
848
849 return FALSE;
850}
851
852/**********************************************************************/
855static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap,
856 struct player *pplayer, struct unit *punit)
857{
858 const struct unit_type *punittype = unit_type_get(punit);
859 struct unit_ai *unit_data;
860
862
863 /* Keep barbarians aggressive and primitive */
864 if (is_barbarian(pplayer)) {
866 && is_land_barbarian(pplayer)) {
867 /* Land barbarians pillage */
869 }
871
872 return;
873 }
874
876
877 /* If I am a bodyguard, check whether I can do my job. */
878 if (unit_data->task == AIUNIT_ESCORT
879 || unit_data->task == AIUNIT_DEFEND_HOME) {
881 }
882
883 if (aiguard_has_charge(ait, punit)
884 && unit_data->task == AIUNIT_ESCORT) {
885 struct unit *aunit = aiguard_charge_unit(ait, punit);
886 struct city *acity = aiguard_charge_city(ait, punit);
887 struct ai_city *city_data = NULL;
888
889 if (acity != NULL) {
891 }
892
893 /* Check if the city we are on our way to rescue is still in danger,
894 * or the unit we should protect is still alive... */
895 if ((aunit && (aiguard_has_guard(ait, aunit) || aiguard_wanted(ait, aunit))
898 && city_data->urgency != 0
899 && city_data->danger > assess_defense_quadratic(ait, acity))) {
900 return; /* Yep! */
901 } else {
902 dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL); /* Nope! */
903 }
904 }
905
906 /* Is the unit badly damaged? */
907 if ((unit_data->task == AIUNIT_RECOVER
908 && punit->hp < punittype->hp)
909 || punit->hp < punittype->hp * 0.25) { /* WAG */
910 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "set to hp recovery");
912 return;
913 }
914
917 /* This is a defending unit that doesn't need to stay put.
918 * It needs to defend something, but not necessarily where it's at.
919 * Therefore, it will consider becoming a bodyguard. -- Syela */
920 struct city *acity;
921 struct unit *aunit;
922
923 look_for_charge(ait, nmap, pplayer, punit, &aunit, &acity);
924 if (acity) {
927 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend city");
928 } else if (aunit) {
931 BODYGUARD_LOG(ait, LOG_DEBUG, punit, "going to defend unit");
932 }
933 }
935}
936
937/**********************************************************************/
946static void dai_military_defend(struct ai_type *ait, struct player *pplayer,
947 struct unit *punit)
948{
949 struct city *pcity = aiguard_charge_city(ait, punit);
950
952
953 if (!pcity || city_owner(pcity) != pplayer) {
954 pcity = tile_city(unit_tile(punit));
955 /* Do not stay defending an allied city forever */
957 }
958
959 if (!pcity) {
960 /* Try to find a place to rest. Sitting duck out in the wilderness
961 * is generally a bad idea, since we protect no cities that way, and
962 * it looks silly. */
963 pcity = find_closest_city(unit_tile(punit), NULL, pplayer,
966 }
967
968 if (!pcity) {
970 }
971
974 /* ... we survived */
975 if (pcity) {
976 UNIT_LOG(LOG_DEBUG, punit, "go to defend %s", city_name_get(pcity));
977 if (same_pos(unit_tile(punit), pcity->tile)) {
978 UNIT_LOG(LOG_DEBUG, punit, "go defend successful");
980 } else {
981 (void) dai_gothere(ait, pplayer, punit, pcity->tile);
982 }
983 } else {
984 UNIT_LOG(LOG_VERBOSE, punit, "defending nothing...?");
985 }
986 }
987}
988
989/**********************************************************************/
992static void single_invader(struct ai_city *city_data,
993 const struct unit_type *utype,
994 int which)
995{
996 int attacks;
997
1000 attacks = 1;
1001 } else {
1002 attacks = utype->move_rate;
1003 }
1004 city_data->invasion.attack += attacks;
1005 if (which == INVASION_OCCUPY) {
1006 city_data->invasion.occupy++;
1007 }
1008}
1009
1010/**********************************************************************/
1020static void invasion_funct(struct ai_type *ait, struct unit *punit,
1021 bool dest, int radius, int which)
1022{
1023 struct tile *ptile;
1024 struct player *pplayer = unit_owner(punit);
1025
1027
1028 if (dest) {
1029 ptile = punit->goto_tile;
1030 } else {
1031 ptile = unit_tile(punit);
1032 }
1033
1034 square_iterate(&(wld.map), ptile, radius, tile1) {
1035 struct city *pcity = tile_city(tile1);
1036
1037 if (pcity
1038 && POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, city_owner(pcity))
1039 && (dest || !has_defense(pcity))) {
1040 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1041
1042 /* Unit itself */
1044
1045 /* Cargo */
1046 unit_cargo_iterate(punit, cargo) {
1047 const struct unit_type *utype = unit_type_get(cargo);
1048
1049 if (IS_ATTACKER(utype)) {
1053 }
1055 }
1057}
1058
1059/**********************************************************************/
1062bool find_beachhead(const struct player *pplayer, struct pf_map *ferry_map,
1063 struct tile *dest_tile,
1064 const struct unit_type *cargo_type,
1065 struct tile **ferry_dest, struct tile **beachhead_tile)
1066{
1067 if (NULL == tile_city(dest_tile)
1069 /* Unit can directly go to 'dest_tile'. */
1070 struct tile *best_tile = NULL;
1072
1073 if (NULL != beachhead_tile) {
1074 *beachhead_tile = dest_tile;
1075 }
1076
1077 adjc_iterate(&(wld.map), dest_tile, ptile) {
1079 if (cost != PF_IMPOSSIBLE_MC
1080 && (NULL == best_tile || cost < best_cost)) {
1081 best_tile = ptile;
1082 best_cost = cost;
1083 }
1085
1086 if (NULL != ferry_dest) {
1088 }
1089
1090 return (PF_IMPOSSIBLE_MC != best_cost);
1091 } else {
1092 /* We need to find a beach around 'dest_tile'. */
1093 struct tile *best_tile = NULL, *best_beach = NULL;
1096
1097 tile_list_append(checked_tiles, dest_tile);
1098 adjc_iterate(&(wld.map), dest_tile, beach) {
1100 /* Can land there. */
1101 adjc_iterate(&(wld.map), beach, ptile) {
1102 if (!tile_list_search(checked_tiles, ptile)
1103 && !is_non_allied_unit_tile(ptile, pplayer)) {
1106 if (cost != PF_IMPOSSIBLE_MC
1107 && (NULL == best_tile || cost < best_cost)) {
1108 best_beach = beach;
1109 best_tile = ptile;
1110 best_cost = cost;
1111 }
1112 }
1114 }
1116
1118
1119 if (NULL != beachhead_tile) {
1121 }
1122 if (NULL != ferry_dest) {
1124 }
1125 return (PF_IMPOSSIBLE_MC != best_cost);
1126 }
1127}
1128
1129/**********************************************************************/
1137adv_want find_something_to_kill(struct ai_type *ait, struct player *pplayer,
1138 struct unit *punit,
1139 struct tile **pdest_tile, struct pf_path **ppath,
1140 struct pf_map **pferrymap,
1141 struct unit **pferryboat,
1142 const struct unit_type **pboattype, int *pmove_time)
1143{
1144 const int attack_value = adv_unit_att_rating(punit); /* basic attack. */
1145 struct pf_parameter parameter;
1146 struct pf_map *punit_map, *ferry_map;
1147 struct pf_position pos;
1149 const struct unit_type *punit_type = unit_type_get(punit);
1150 struct tile *punit_tile = unit_tile(punit);
1151 /* Type of our boat (a future one if ferryboat == NULL). */
1152 const struct unit_type *boattype = NULL;
1153 struct unit *ferryboat = NULL;
1154 struct city *pcity;
1155 struct ai_city *acity_data;
1156 int bcost, bcost_bal; /* Build cost of the attacker (+adjustments). */
1157 bool handicap = has_handicap(pplayer, H_TARGETS);
1158 bool unhap = FALSE; /* Do we make unhappy citizen. */
1159 bool harbor = FALSE; /* Do we have access to sea? */
1160 bool go_by_boat; /* Whether we need a boat or not. */
1161 int vulnerability; /* Enemy defense rating. */
1162 adv_want benefit; /* Benefit from killing the target. */
1163 struct unit *pdefender; /* Enemy city defender. */
1164 int move_time; /* Turns needed to target. */
1165 int reserves;
1166 int attack; /* Our total attack value with reinforcements. */
1167 int victim_count; /* Number of enemies there. */
1168 int needferry; /* Cost of building a ferry boat. */
1169 /* This is a kluge, because if we don't set x and y with !punit->id,
1170 * p_a_w isn't called, and we end up not wanting ironclads and therefore
1171 * never learning steam engine, even though ironclads would be very
1172 * useful. -- Syela */
1173 adv_want bk = 0;
1174 adv_want want; /* Want (amortized) of the operation. */
1175 adv_want best = 0; /* Best of all wants. */
1176 struct tile *goto_dest_tile = NULL;
1177 bool can_occupy;
1178 struct civ_map *nmap = &(wld.map);
1179
1180 /* Very preliminary checks. */
1182 if (NULL != pferrymap) {
1183 *pferrymap = NULL;
1184 }
1185 if (NULL != pferryboat) {
1186 *pferryboat = NULL;
1187 }
1188 if (NULL != pboattype) {
1189 *pboattype = NULL;
1190 }
1191 if (NULL != pmove_time) {
1192 *pmove_time = 0;
1193 }
1194 if (NULL != ppath) {
1195 *ppath = NULL;
1196 }
1197
1198 if (0 == attack_value) {
1199 /* A very poor attacker... probably low on HP. */
1200 return 0;
1201 }
1202
1204
1205
1206 /*** Part 1: Calculate targets ***/
1207
1208 /* This horrible piece of code attempts to calculate the attractiveness of
1209 * enemy cities as targets for our units, by checking how many units are
1210 * going towards it or are near it already. */
1211
1212 /* Reset enemy cities data. */
1214 /* See comment below in next usage of POTENTIALLY_HOSTILE_PLAYER. */
1215 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1216 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1217 continue;
1218 }
1219 city_list_iterate(aplayer->cities, acity) {
1220 struct ai_city *city_data = def_ai_city_data(acity, ait);
1221
1223 &city_data->attack,
1224 &city_data->bcost);
1225 city_data->invasion.attack = 0;
1226 city_data->invasion.occupy = 0;
1229
1230 /* Second, calculate in units on their way there, and mark targets for
1231 * invasion */
1232 unit_list_iterate(pplayer->units, aunit) {
1233 const struct unit_type *atype;
1234
1235 if (aunit == punit) {
1236 continue;
1237 }
1238
1240
1241 /* Dealing with invasion stuff */
1242 if (IS_ATTACKER(atype)) {
1243 if (aunit->activity == ACTIVITY_GOTO) {
1244 invasion_funct(ait, aunit, TRUE, 0,
1247 if ((pcity = tile_city(aunit->goto_tile))) {
1248 struct ai_city *city_data = def_ai_city_data(pcity, ait);
1249
1252 }
1253 }
1257 } else if (def_ai_unit_data(aunit, ait)->passenger != 0
1259 /* It's a transport with reinforcements */
1260 if (aunit->activity == ACTIVITY_GOTO) {
1262 }
1264 }
1266 /* end horrible initialization subroutine */
1267
1268
1269 /*** Part 2: Now pick one target ***/
1270
1271 /* We first iterate through all cities, then all units, looking
1272 * for a viable target. We also try to gang up on the target
1273 * to avoid spreading out attacks too widely to be inefficient.
1274 */
1275
1276 pcity = tile_city(punit_tile);
1277 if (NULL != pcity && (0 == punit->id || pcity->id == punit->homecity)) {
1278 /* I would have thought unhappiness should be taken into account
1279 * irrespectfully the city in which it will surface... -- GB */
1281 }
1282
1285
1287 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1288 punit_map = pf_map_new(&parameter);
1289
1290 if (MOVE_NONE == punit_class->adv.sea_move) {
1291 /* We need boat to move over sea. */
1292 ferryboat = unit_transport_get(punit);
1293
1294 /* First check if we can use the boat we are currently loaded to. */
1295 if (ferryboat == NULL || !is_boat_free(ait, ferryboat, punit, 0)) {
1296 /* No, we cannot control current boat */
1297 ferryboat = NULL;
1298 }
1299
1300 if (NULL == ferryboat) {
1301 /* Try to find new boat */
1302 ferryboat = player_unit_by_number(pplayer,
1303 aiferry_find_boat(ait, punit, 1, NULL));
1304 }
1305
1307 harbor = TRUE;
1308 }
1309 }
1310
1311 if (NULL != ferryboat) {
1312 boattype = unit_type_get(ferryboat);
1313 pft_fill_unit_overlap_param(&parameter, nmap, ferryboat);
1314 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1315 ferry_map = pf_map_new(&parameter);
1316 } else {
1318 if (NULL == boattype) {
1319 /* We pretend that we can have the simplest boat to stimulate tech. */
1321 }
1322 if (NULL != boattype && harbor) {
1323 /* Let's simulate a boat at 'punit' position. */
1325 punit_tile, pplayer);
1326 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1327 ferry_map = pf_map_new(&parameter);
1328 } else {
1329 ferry_map = NULL;
1330 }
1331 }
1332
1334
1336 /* For the virtual unit case, which is when we are called to evaluate
1337 * which units to build, we want to calculate in danger and which
1338 * players we want to make war with in the future. We do _not_ want
1339 * to do this when actually making attacks. */
1340 if ((punit->id == 0 && !POTENTIALLY_HOSTILE_PLAYER(ait, pplayer, aplayer))
1341 || (punit->id != 0 && !pplayers_at_war(pplayer, aplayer))) {
1342 continue; /* Not an enemy. */
1343 }
1344
1345 city_list_iterate(aplayer->cities, acity) {
1346 struct tile *atile = city_tile(acity);
1347
1350 /* Can't attack this city. It is on land. */
1351 continue;
1352 }
1353
1354 if (handicap && !map_is_known(atile, pplayer)) {
1355 /* Can't see it */
1356 continue;
1357 }
1358
1360 go_by_boat = FALSE;
1361 move_time = pos.turn;
1362 } else if (NULL == ferry_map) {
1363 continue; /* Impossible to handle. */
1364 } else {
1365 struct tile *dest, *beach;
1366
1367 if (!find_beachhead(pplayer, ferry_map, atile, punit_type,
1368 &dest, &beach)) {
1369 continue; /* Impossible to go by boat. */
1370 }
1371 if (!pf_map_position(ferry_map, dest, &pos)) {
1373 continue;
1374 }
1375 move_time = pos.turn; /* Sailing time. */
1376 if (dest != beach) {
1377 move_time++; /* Land time. */
1378 }
1379 if (NULL != ferryboat && unit_tile(ferryboat) != punit_tile) {
1380 if (pf_map_position(punit_map, unit_tile(ferryboat), &pos)) {
1381 move_time += pos.turn; /* Time to reach the boat. */
1382 } else {
1383 continue; /* Cannot reach the boat. */
1384 }
1385 }
1386 go_by_boat = TRUE;
1387 }
1388
1393 } else {
1394 pdefender = NULL;
1395 vulnerability = 0;
1396 benefit = 0;
1397 }
1398
1399 if (1 < move_time) {
1401
1402 if (def_type) {
1406 if (v > vulnerability) {
1407 /* They can build a better defender! */
1408 vulnerability = v;
1410 }
1411 }
1412 }
1413
1415
1416 reserves = (acity_data->invasion.attack
1417 - unit_list_size(acity->tile->units));
1418
1419 if (punit->id == 0) {
1420 /* Real unit would add to reserves once built. */
1423 reserves++;
1424 } else {
1425 reserves += punit_type->move_rate;
1426 }
1427 }
1428
1429 if (0 < reserves && (can_occupy
1430 || 0 < acity_data->invasion.occupy)) {
1431 /* There are units able to occupy the city after all defenders
1432 * are killed! */
1433 benefit += acity_data->worth * reserves / 5;
1434 }
1435
1436 attack = attack_value + acity_data->attack;
1437 attack *= attack;
1438 /* Avoiding handling upkeep aggregation this way -- Syela */
1439
1440 /* AI was not sending enough reinforcements to totally wipe out a city
1441 * and conquer it in one turn.
1442 * This variable enables total carnage. -- Syela */
1444
1445 if (!can_occupy && NULL == pdefender) {
1446 /* Nothing there to bash and we can't occupy!
1447 * Not having this check caused warships yoyoing */
1448 want = 0;
1449 } else if (10 < move_time) {
1450 /* Too far! */
1451 want = 0;
1452 } else if (can_occupy && 0 == acity_data->invasion.occupy
1453 && (0 < acity_data->invasion.attack
1454 || victim_count == 0)) {
1455 /* Units able to occupy really needed there! */
1456 want = bcost * SHIELD_WEIGHTING;
1457 } else {
1458 want = kill_desire(benefit, attack, bcost + acity_data->bcost,
1460 }
1463 /* Build_cost of ferry. */
1464 needferry = (go_by_boat && NULL == ferryboat
1466 /* FIXME: add time to build the ferry? */
1468 want, MAX(1, move_time),
1470
1471 /* BEGIN STEAM-ENGINES-ARE-OUR-FRIENDS KLUGE. */
1472 if (0 >= want && 0 == punit->id && 0 >= best) {
1476 MAX(1, move_time),
1478
1479 if (bk_e > bk) {
1480 *pdest_tile = atile;
1481 if (NULL != pferrymap) {
1483 }
1484 if (NULL != pferryboat) {
1485 *pferryboat = go_by_boat ? ferryboat : NULL;
1486 }
1487 if (NULL != pboattype) {
1489 }
1490 if (NULL != pmove_time) {
1492 }
1493 goto_dest_tile = (go_by_boat && NULL != ferryboat
1494 ? unit_tile(ferryboat) : atile);
1495 bk = bk_e;
1496 }
1497 }
1498 /* END STEAM-ENGINES KLUGE */
1499
1500 if (0 != punit->id
1501 && NULL != ferryboat
1502 && punit_class->adv.sea_move == MOVE_NONE) {
1504 "%s(): with boat %s@(%d, %d) -> %s@(%d, %d)"
1505 " (go_by_boat = %d, move_time = %d, want = " ADV_WANT_PRINTF
1506 ", best = " ADV_WANT_PRINTF ")",
1507 __FUNCTION__, unit_rule_name(ferryboat),
1508 TILE_XY(unit_tile(ferryboat)), city_name_get(acity),
1509 TILE_XY(atile), go_by_boat, move_time, want, best);
1510 }
1511
1512 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1513 /* Yes, we like this target */
1514 best = want;
1515 *pdest_tile = atile;
1516 if (NULL != pferrymap) {
1518 }
1519 if (NULL != pferryboat) {
1520 *pferryboat = go_by_boat ? ferryboat : NULL;
1521 }
1522 if (NULL != pboattype) {
1524 }
1525 if (NULL != pmove_time) {
1527 }
1528 goto_dest_tile = (go_by_boat && NULL != ferryboat
1529 ? unit_tile(ferryboat) : atile);
1530 }
1532
1534 /* I'm not sure the following code is good but it seems to be adequate.
1535 * I am deliberately not adding ferryboat code to the unit_list_iterate.
1536 * -- Syela */
1538 struct tile *atile = unit_tile(aunit);
1539
1540 if (NULL != tile_city(atile)) {
1541 /* already dealt with it. */
1542 continue;
1543 }
1544
1545 if (handicap && !map_is_known(atile, pplayer)) {
1546 /* Can't see the target. */
1547 continue;
1548 }
1549
1551 && 0 == punit->id) {
1552 /* We will not build units just to chase caravans and
1553 * ambassadors. */
1554 continue;
1555 }
1556
1557 /* We have to assume the attack is diplomatically ok.
1558 * We cannot use can_player_attack_tile, because we might not
1559 * be at war with aplayer yet */
1561 || aunit != get_defender(nmap, punit, atile, NULL)) {
1562 /* We cannot attack it, or it is not the main defender. */
1563 continue;
1564 }
1565
1567 /* Cannot reach it. */
1568 continue;
1569 }
1570
1573
1574 move_time = pos.turn;
1575 if (10 < move_time) {
1576 /* Too far. */
1577 want = 0;
1578 } else {
1579 want = kill_desire(benefit, attack, bcost, vulnerability, 1);
1580 /* Take into account maintenance of the unit. */
1581 /* FIXME: Depends on the government. */
1582 want -= move_time * SHIELD_WEIGHTING;
1583 /* Take into account unhappiness
1584 * (costs 2 luxuries to compensate). */
1585 want -= (unhap ? 2 * move_time * TRADE_WEIGHTING : 0);
1586 }
1588 want, MAX(1, move_time), bcost_bal);
1589 if (want > best && ai_fuzzy(pplayer, TRUE)) {
1590 best = want;
1591 *pdest_tile = atile;
1592 if (NULL != pferrymap) {
1593 *pferrymap = NULL;
1594 }
1595 if (NULL != pferryboat) {
1596 *pferryboat = NULL;
1597 }
1598 if (NULL != pboattype) {
1599 *pboattype = NULL;
1600 }
1601 if (NULL != pmove_time) {
1603 }
1605 }
1608
1609 if (NULL != ppath) {
1612 }
1613
1615 if (NULL != ferry_map
1616 && (NULL == pferrymap || *pferrymap != ferry_map)) {
1618 }
1619
1621
1622 return best;
1623}
1624
1625/**********************************************************************/
1634{
1635 struct pf_parameter parameter;
1636 struct pf_map *pfm;
1637 struct player *pplayer = unit_owner(punit);
1638 struct city *pcity, *best_city = NULL;
1639 int best = FC_INFINITY, cur;
1640 const struct civ_map *nmap = &(wld.map);
1641
1642 pft_fill_unit_parameter(&parameter, nmap, punit);
1643 parameter.omniscience = !has_handicap(pplayer, H_MAP);
1644 pfm = pf_map_new(&parameter);
1645
1646 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
1647 if (move_cost > best) {
1648 /* We already found a better city. No need to continue. */
1649 break;
1650 }
1651
1652 pcity = tile_city(ptile);
1653 if (NULL == pcity || !pplayers_allied(pplayer, city_owner(pcity))) {
1654 continue;
1655 }
1656
1657 /* Score based on the move cost. */
1658 cur = move_cost;
1659
1660 /* Note the unit owner may be different from the city owner. */
1661 if (0 == get_unittype_bonus(unit_owner(punit), ptile,
1663 EFT_HP_REGEN)) {
1664 /* If we cannot regen fast our hit points here, let's make some
1665 * penalty. */
1666 cur *= 3;
1667 }
1668
1669 if (cur < best) {
1670 best_city = pcity;
1671 best = cur;
1672 }
1674
1676 return best_city;
1677}
1678
1679/**********************************************************************/
1685 struct player *pplayer,
1686 struct unit *punit)
1687{
1688 struct city *pc;
1689 bool only_continent = TRUE;
1690
1691 if (unit_transported(punit)) {
1692 /* If we are in transport, we can go to any continent.
1693 * Actually, we are not currently in a continent where to stay. */
1695 }
1696
1697 if ((pc = find_closest_city(unit_tile(punit), NULL, pplayer, FALSE,
1700 UNIT_LOG(LOG_DEBUG, punit, "Barbarian heading to conquer %s",
1701 city_name_get(pc));
1702 (void) dai_gothere(ait, pplayer, punit, pc->tile);
1703 } else {
1704 struct unit *ferry = NULL;
1705
1706 if (unit_transported(punit)) {
1707 ferry = unit_transport_get(punit);
1708
1709 /* We already are in a boat so it needs no
1710 * free capacity */
1711 if (!is_boat_free(ait, ferry, punit, 0)) {
1712 /* We cannot control our ferry. */
1713 ferry = NULL;
1714 }
1715 } else {
1716 /* We are not in a boat yet. Search for one. */
1718 if (is_boat_free(ait, aunit, punit, 1)
1720 ferry = aunit;
1721 break;
1722 }
1724 }
1725
1726 if (ferry) {
1727 UNIT_LOG(LOG_DEBUG, punit, "Barbarian sailing to conquer %s",
1728 city_name_get(pc));
1729 (void) aiferry_goto_amphibious(ait, ferry, punit, pc->tile);
1730 } else {
1731 /* This is not an error. Somebody else might be in charge
1732 * of the ferry. */
1733 UNIT_LOG(LOG_DEBUG, punit, "unable to find barbarian ferry");
1734 }
1735 }
1736 } else {
1737 UNIT_LOG(LOG_DEBUG, punit, "Barbarian find no target city");
1738 }
1739}
1740
1741/**********************************************************************/
1747static void dai_military_attack(struct ai_type *ait, struct player *pplayer,
1748 struct unit *punit)
1749{
1750 struct tile *dest_tile;
1751 int id = punit->id;
1752 int ct = 10;
1753 struct city *pcity = NULL;
1754
1756
1757 /* Barbarians pillage, and might keep on doing that so they sometimes
1758 * even finish it. */
1759 if (punit->activity == ACTIVITY_PILLAGE && is_barbarian(pplayer)
1760 && fc_rand(2) == 1) {
1761 return;
1762 }
1763
1764 /* First find easy nearby enemies, anything better than pillage goes.
1765 * NB: We do not need to repeat dai_military_rampage, it is repeats itself
1766 * until it runs out of targets. */
1767 /* FIXME: 1. dai_military_rampage never checks if it should defend newly
1768 * conquered cities.
1769 * FIXME: 2. would be more convenient if it returned FALSE if we run out
1770 * of moves too.*/
1772 return; /* we died */
1773 }
1774
1775 if (punit->moves_left <= 0) {
1776 return;
1777 }
1778
1779 /* Main attack loop */
1780 do {
1781 struct tile *start_tile = unit_tile(punit);
1782 struct pf_path *path;
1783 struct unit *ferryboat;
1784
1785 /* Then find enemies the hard way */
1786 find_something_to_kill(ait, pplayer, punit, &dest_tile, &path,
1787 NULL, &ferryboat, NULL, NULL);
1788 if (!same_pos(unit_tile(punit), dest_tile)) {
1789 if (!is_tiles_adjacent(unit_tile(punit), dest_tile)
1790 || !can_unit_attack_tile(punit, NULL, dest_tile)) {
1791
1792 /* Adjacent and can't attack usually means we are not marines
1793 * and on a ferry. This fixes the problem (usually). */
1794 UNIT_LOG(LOG_DEBUG, punit, "mil att gothere -> (%d, %d)",
1795 TILE_XY(dest_tile));
1796
1797 /* Set ACTIVITY_GOTO more permanently than just inside
1798 * adv_follow_path(). This way other units will know we're
1799 * on our way even if we don't reach target yet. */
1800 punit->goto_tile = dest_tile;
1802 if (NULL != path && !adv_follow_path(punit, path, dest_tile)) {
1803 /* Died. */
1804 pf_path_destroy(path);
1805 return;
1806 }
1807 if (NULL != ferryboat) {
1808 /* Need a boat. */
1809 aiferry_gobyboat(ait, pplayer, punit, dest_tile, FALSE);
1810 pf_path_destroy(path);
1811 return;
1812 }
1813 if (0 >= punit->moves_left) {
1814 /* No moves left. */
1815 pf_path_destroy(path);
1816 return;
1817 }
1818
1819 /* Either we're adjacent or we sitting on the tile. We might be
1820 * sitting on the tile if the enemy that _was_ sitting there
1821 * attacked us and died _and_ we had enough movement to get there */
1822 if (same_pos(unit_tile(punit), dest_tile)) {
1823 UNIT_LOG(LOG_DEBUG, punit, "mil att made it -> (%d, %d)",
1824 TILE_XY(dest_tile));
1825 pf_path_destroy(path);
1826 break;
1827 }
1828 }
1829
1830 if (is_tiles_adjacent(unit_tile(punit), dest_tile)) {
1831 /* Close combat. fstk sometimes want us to attack an adjacent
1832 * enemy that rampage wouldn't */
1833 UNIT_LOG(LOG_DEBUG, punit, "mil att bash -> (%d, %d)",
1834 TILE_XY(dest_tile));
1835 if (!dai_unit_attack(ait, punit, dest_tile)) {
1836 /* Died */
1837 pf_path_destroy(path);
1838 return;
1839 }
1840 } else if (!same_pos(start_tile, unit_tile(punit))) {
1841 /* Got stuck. Possibly because of adjacency to an
1842 * enemy unit. Perhaps we are in luck and are now next to a
1843 * tempting target? Let's find out... */
1846 pf_path_destroy(path);
1847 return;
1848 }
1849
1850 } else {
1851 /* FIXME: This happens a bit too often! */
1852 UNIT_LOG(LOG_DEBUG, punit, "fstk didn't find us a worthy target!");
1853 /* No worthy enemies found, so abort loop */
1854 ct = 0;
1855 }
1856 pf_path_destroy(path);
1857
1858 ct--; /* infinite loops from railroads must be stopped */
1859 } while (punit->moves_left > 0 && ct > 0);
1860
1861 /* Cleanup phase */
1862 if (punit->moves_left == 0) {
1863 return;
1864 }
1866 if (pcity != NULL
1867 && (dai_is_ferry(punit, ait)
1868 || punit->hp < unit_type_get(punit)->hp * 0.50)) { /* WAG */
1869 /* Go somewhere safe */
1870 UNIT_LOG(LOG_DEBUG, punit, "heading to nearest safe house.");
1871 (void) dai_unit_goto(ait, punit, pcity->tile);
1872 } else if (!is_barbarian(pplayer)) {
1873 /* Nothing else to do, so try exploring. */
1874 switch (manage_auto_explorer(punit)) {
1875 case MR_DEATH:
1876 /* don't use punit! */
1877 return;
1878 case MR_OK:
1879 UNIT_LOG(LOG_DEBUG, punit, "nothing else to do, so exploring");
1880 break;
1881 default:
1882 UNIT_LOG(LOG_DEBUG, punit, "nothing to do - no more exploring either");
1883 break;
1884 };
1885 } else {
1886 /* You can still have some moves left here, but barbarians should
1887 not sit helplessly, but advance towards nearest known enemy city */
1888 UNIT_LOG(LOG_DEBUG, punit, "attack: barbarian");
1889 dai_military_attack_barbarian(ait, pplayer, punit);
1890 }
1891 if ((punit = game_unit_by_number(id)) && punit->moves_left > 0) {
1892 UNIT_LOG(LOG_DEBUG, punit, "attack: giving up unit to defense");
1893 dai_military_defend(ait, pplayer, punit);
1894 }
1895}
1896
1897/**********************************************************************/
1901static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
1902{
1903 bool alive = TRUE;
1904 int ferryboat = 0;
1905 struct pf_path *path_to_ferry = NULL;
1906
1907 UNIT_LOG(LOG_CARAVAN, punit, "requesting a boat!");
1908 ferryboat = aiferry_find_boat(ait, punit, 1, &path_to_ferry);
1909 /* going to meet the boat */
1910 if ((ferryboat <= 0)) {
1912 "in find_boat_for_unit cannot find any boats.");
1913 /* if we are undefended on the country side go to a city */
1915 if (current_city == NULL) {
1917 if (city_near != NULL) {
1919 }
1920 }
1921 } else {
1922 if (path_to_ferry != NULL) {
1924 /* Died. */
1927 alive = FALSE;
1928 } else {
1931 alive = TRUE;
1932 }
1933 }
1934 }
1935 return alive;
1936}
1937
1938/**********************************************************************/
1947 struct tile *ctile, struct tile *ptile)
1948{
1949 /* We usually have Inaccessible terrain, so not testing MOVE_FULL == */
1950 bool lm = MOVE_NONE != pclass->adv.land_move,
1951 sm = MOVE_NONE != pclass->adv.sea_move;
1952 struct civ_map *pmap = &(wld.map);
1953
1954 if (lm && sm) {
1955 return FALSE;
1956 }
1957
1958 /* We could use adjc_iterate() but likely often tiles are on the same
1959 * continent and it will be more time to find where they connect */
1962
1963 if (is_ocean_tile(atile) ? sm : lm) {
1964 iterate_outward(pmap, ptile, 1, btile) {
1965 if (tile_continent(btile) == acont) {
1966 return FALSE;
1967 }
1969 }
1971
1972 if (is_tiles_adjacent(ctile, ptile)) {
1973 return FALSE;
1974 }
1975
1976 return TRUE;
1977}
1978
1979/**********************************************************************/
1985static void dai_caravan_goto(struct ai_type *ait, struct player *pplayer,
1986 struct unit *punit,
1987 const struct city *dest_city,
1988 bool help_wonder,
1989 bool required_boat, bool request_boat)
1990{
1991 bool alive = TRUE;
1992 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
1993 const struct civ_map *nmap = &(wld.map);
1994
1996
1997 /* if we're not there yet, and we can move, move... */
1998 if (!same_pos(dest_city->tile, unit_tile(punit))
1999 && punit->moves_left != 0) {
2000 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) task %s going to %s in %s %s",
2004 help_wonder ? "help a wonder" : "trade", city_name_get(dest_city),
2005 required_boat ? "with a boat" : "");
2006 if (required_boat) {
2007 /* to trade with boat */
2008 if (request_boat) {
2009 /* Try to find new boat */
2011 } else {
2012 /* if we are not being transported then ask for a boat again */
2013 alive = TRUE;
2016 unit_tile(punit), dest_city->tile)) {
2018 }
2019 }
2020 if (alive) {
2021 /* FIXME: sometimes we get FALSE here just because
2022 * a trireme that we've boarded can't go over an ocean. */
2023 alive = dai_gothere(ait, pplayer, punit, dest_city->tile);
2024 }
2025 } else {
2026 /* to trade without boat */
2028 }
2029 }
2030
2031 /* if moving didn't kill us, and we got to the destination, handle it. */
2032 if (alive && real_map_distance(dest_city->tile, unit_tile(punit)) <= 1) {
2033 /* release the boat! */
2034 if (unit_transported(punit)) {
2036 }
2038 punit, dest_city)) {
2039 /*
2040 * We really don't want to just drop all caravans in immediately.
2041 * Instead, we want to aggregate enough caravans to build instantly.
2042 * -AJS, 990704
2043 */
2044 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) helps build wonder in %s",
2047 punit->id,
2050 unit_do_action(pplayer, punit->id, dest_city->id,
2051 0, "", ACTION_HELP_WONDER);
2053 punit, dest_city)) {
2054 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) creates trade route in %s",
2057 punit->id,
2060 unit_do_action(pplayer, punit->id, dest_city->id,
2061 0, "", ACTION_TRADE_ROUTE);
2063 punit, dest_city)) {
2064 /* Get the one time bonus. */
2065 log_base(LOG_CARAVAN, "%s %s[%d](%d,%d) enters marketplace of %s",
2068 punit->id,
2071 unit_do_action(pplayer, punit->id, dest_city->id,
2072 0, "", ACTION_MARKETPLACE);
2073 } else {
2074 enum log_level level = LOG_NORMAL;
2075
2076 if (help_wonder) {
2077 /* A Caravan ordered to help build wonder may arrive after
2078 * enough shields to build the wonder is produced. */
2080 }
2081
2083 "%s %s[%d](%d,%d) unable to trade with %s",
2086 punit->id,
2089 }
2090 }
2091}
2092
2093/**********************************************************************/
2097static void caravan_optimize_callback(const struct caravan_result *result,
2098 void *data)
2099{
2100 const struct unit *caravan = data;
2101
2102 log_base(LOG_CARAVAN3, "%s %s[%d](%d,%d) %s: %s %s worth %g",
2104 unit_rule_name(caravan),
2105 caravan->id,
2106 TILE_XY(unit_tile(caravan)),
2107 city_name_get(result->src),
2108 result->help_wonder ? "wonder in" : "trade to",
2109 city_name_get(result->dest),
2110 result->value);
2111}
2112
2113/**********************************************************************/
2117 struct unit *punit)
2118{
2119 struct tile *src = NULL, *dest = NULL, *src_home_city = NULL;
2120 struct city *phome_city = NULL;
2121 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2122
2123 if ((unit_data->task != AIUNIT_NONE)) {
2124 src = unit_tile(punit);
2126 if (phome_city != NULL) {
2128 }
2129 dest = punit->goto_tile;
2130
2131 if (src == NULL || dest == NULL) {
2132 return FALSE;
2133 }
2134
2135 /* If we have a home continent, and are not there.
2136 * (FIXME: well, why?)
2137 * (I guess because home continent is which we were supposed to leave,
2138 * not the target continent) */
2139 if (src_home_city != NULL
2141 return FALSE;
2142 }
2143
2144 if (!goto_is_sane(punit, dest)) {
2145 if (unit_transported(punit)) {
2146 /* If we're being transported */
2147 return FALSE;
2148 }
2149 if ((punit->birth_turn + 15 < game.info.turn)) {
2150 /* We are tired of waiting */
2152
2153 if (ferrys <= 0) {
2154 /* There are no ferrys available... give up */
2155 return TRUE;
2156 } else {
2157 if (punit->birth_turn + 20 < game.info.turn) {
2158 /* We are fed up! */
2159 return TRUE;
2160 }
2161 }
2162 }
2163 }
2164 }
2165
2166 return FALSE;
2167}
2168
2169/**********************************************************************/
2176 struct unit *punit)
2177{
2178 struct city *pcity = game_city_by_number(punit->homecity);
2179 Continent_id continent;
2180
2181 fc_assert(pcity != NULL);
2182
2183 if (unit_class_get(punit)->adv.ferry_types <= 0) {
2184 /* There is just no possible transporters. */
2185 return FALSE;
2186 }
2187 continent = tile_continent(pcity->tile);
2188
2189 /* Look for proper destination city at different continent. */
2190 city_list_iterate(pplayer->cities, acity) {
2191 if (can_cities_trade(pcity, acity)) {
2192 if (tile_continent(acity->tile) != continent) {
2193 return TRUE;
2194 }
2195 }
2197
2199 if (aplayer == pplayer || !aplayer->is_alive) {
2200 continue;
2201 }
2202 if (pplayers_allied(pplayer, aplayer)) {
2203 city_list_iterate(aplayer->cities, acity) {
2204 if (can_cities_trade(pcity, acity)) {
2205 if (tile_continent(acity->tile) != continent) {
2206 return TRUE;
2207 }
2208 }
2211 }
2212
2213 return FALSE;
2214}
2215
2216/**********************************************************************/
2220static bool search_homecity_for_caravan(struct ai_type *ait, struct unit *punit)
2221{
2222 struct city *nearest = NULL;
2223 int min_dist = FC_INFINITY;
2224 struct tile *current_loc = unit_tile(punit);
2226 bool alive = TRUE;
2227
2229 struct tile *ctile = city_tile(pcity);
2230
2231 if (tile_continent(ctile) == continent) {
2233
2234 if (this_dist < min_dist) {
2236 nearest = pcity;
2237 }
2238 }
2240
2241 if (nearest != NULL) {
2243 if (alive && same_pos(unit_tile(punit), nearest->tile)) {
2245 }
2246 }
2247
2248 return alive;
2249}
2250
2251/**********************************************************************/
2257static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer,
2258 struct unit *punit)
2259{
2260 struct caravan_parameter parameter;
2261 struct caravan_result result;
2262 const struct city *homecity;
2263 const struct city *dest = NULL;
2264 struct unit_ai *unit_data;
2266 bool expect_boats = pclass->adv.ferry_types > 0;
2267 /* TODO: will pplayer have a boat for punit in a reasonable time? */
2268 bool help_wonder = FALSE;
2269 bool required_boat = FALSE;
2270 bool request_boat = FALSE;
2272 const struct civ_map *nmap = &(wld.map);
2273
2275
2279 /* we only want units that can establish trade, enter marketplace or
2280 * help build wonders */
2281 return;
2282 }
2283
2285
2286 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) task %s to (%d,%d)",
2291
2292 homecity = game_city_by_number(punit->homecity);
2293 if (homecity == NULL && unit_data->task == AIUNIT_TRADE) {
2295 return;
2296 }
2297 homecity = game_city_by_number(punit->homecity);
2298 if (homecity == NULL) {
2299 return;
2300 }
2301 }
2302
2303 if ((unit_data->task == AIUNIT_TRADE
2304 || unit_data->task == AIUNIT_WONDER)) {
2305 /* we are moving to our destination */
2306 /* we check to see if our current goal is feasible */
2308
2309 if ((city_dest == NULL)
2311 || (unit_data->task == AIUNIT_TRADE
2312 && !(can_cities_trade(homecity, city_dest)
2313 && can_establish_trade_route(homecity, city_dest)))
2314 || (unit_data->task == AIUNIT_WONDER
2315 /* Helping the (new) production is illegal. */
2317 || (unit_data->task == AIUNIT_TRADE
2322 punit, city_dest)))
2323 || (unit_data->task == AIUNIT_WONDER
2326 punit, city_dest))) {
2327 /* destination invalid! */
2329 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) destination invalid!",
2332 } else {
2333 /* destination valid, are we tired of waiting for a boat? */
2337 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d) unit tired of waiting!",
2341 } else {
2342 dest = city_dest;
2343 help_wonder = (unit_data->task == AIUNIT_WONDER) ? TRUE : FALSE;
2344 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2346 }
2347 }
2348 }
2349
2350 if (unit_data->task == AIUNIT_NONE) {
2351 if (homecity == NULL) {
2352 /* FIXME: We shouldn't bother in getting homecity for
2353 * caravan that will then be used for wonder building. */
2355 return;
2356 }
2357 homecity = game_city_by_number(punit->homecity);
2358 if (homecity == NULL) {
2359 return;
2360 }
2361 }
2362
2364 /* Make more trade with allies than other peaceful nations
2365 * by considering only allies 50% of the time.
2366 * (the other 50% allies are still considered, but so are other nations) */
2367 if (fc_rand(2)) {
2368 /* Be optimistic about development of relations with no-contact and
2369 * cease-fire nations. */
2370 parameter.allow_foreign_trade = FTL_NONWAR;
2371 } else {
2372 parameter.allow_foreign_trade = FTL_ALLIED;
2373 }
2374
2377 parameter.callback_data = punit;
2378 }
2380 parameter.ignore_transit_time = TRUE;
2381 }
2384 parameter.ignore_transit_time = FALSE;
2385 }
2386 caravan_find_best_destination(nmap, punit, &parameter, &result,
2387 !has_handicap(pplayer, H_MAP));
2388 if (result.dest != NULL) {
2389 /* We did find a new destination for the unit */
2390 dest = result.dest;
2391 help_wonder = result.help_wonder;
2392 required_boat = uclass_need_trans_between(pclass, unit_tile(punit), dest->tile);
2393 request_boat = required_boat;
2395 (help_wonder) ? AIUNIT_WONDER : AIUNIT_TRADE,
2396 dest->tile);
2397 } else {
2398 dest = NULL;
2399 }
2400 }
2401
2402 if (required_boat && !expect_boats) {
2403 /* Would require boat, but can't have them. Render destination invalid. */
2404 dest = NULL;
2405 }
2406
2407 if (dest != NULL) {
2408 dai_caravan_goto(ait, pplayer, punit, dest, help_wonder,
2409 required_boat, request_boat);
2410 return; /* that may have clobbered the unit */
2411 } else {
2412 /* We have nowhere to go! */
2413 log_base(LOG_CARAVAN2, "%s %s[%d](%d,%d), nothing to do!",
2417 /* Should we become a defensive unit? */
2418 }
2419}
2420
2421/**********************************************************************/
2426 struct unit *punit)
2427{
2428 struct player *pplayer = unit_owner(punit);
2429 struct city *pcity = tile_city(unit_tile(punit));
2430 struct city *safe = NULL;
2431 const struct unit_type *punittype = unit_type_get(punit);
2432 bool no_recovery;
2433
2435
2436 if (pcity != NULL) {
2437 /* Rest in the city until the hitpoints are recovered, but attempt
2438 * to protect city from attack (and be opportunistic too)*/
2441 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "recovering hit points.");
2442 } else {
2443 return; /* We died heroically defending our city */
2444 }
2445 } else {
2446 /* Goto to nearest city to recover hit points */
2447 /* Just before, check to see if we can occupy an undefended enemy city */
2450 return; /* Oops, we died */
2451 }
2452
2453 /* Find a city to stay and go there */
2455 if (safe) {
2456 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "going to %s to recover",
2458 if (!dai_unit_goto(ait, punit, safe->tile)) {
2459 log_base(LOGLEVEL_RECOVERY, "died trying to hide and recover");
2460 return;
2461 }
2462 } else {
2463 /* Oops */
2464 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "didn't find a city to recover in!");
2466 dai_military_attack(ait, pplayer, punit);
2467 return;
2468 }
2469 }
2470
2471 /* Is the unit still damaged? If true, and could recover hit points, do so.
2472 * Don't wait if would be losing hitpoints that way! */
2474 if (punit->hp == punittype->hp) {
2475 no_recovery = TRUE;
2476 } else {
2478
2479 if (gain < 0) {
2480 no_recovery = TRUE;
2481 } else if (gain == 0 && !punit->moved) {
2482 /* Isn't gaining even though has not moved. */
2483 no_recovery = TRUE;
2484 }
2485 }
2486 if (no_recovery) {
2487 /* We are ready to go out and kick ass again */
2488 UNIT_LOG(LOGLEVEL_RECOVERY, punit, "ready to kick ass again!");
2490 return;
2491 } else {
2492 def_ai_unit_data(punit, ait)->done = TRUE; /* Sit tight */
2493 }
2494}
2495
2496/**********************************************************************/
2500void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap,
2501 struct player *pplayer, struct unit *punit)
2502{
2503 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2504 int id = punit->id;
2505
2507
2508 /* "Escorting" aircraft should not do anything. They are activated
2509 * by their transport or charge. We do _NOT_ set them to 'done'
2510 * since they may need be activated once our charge moves. */
2511 if (unit_data->task == AIUNIT_ESCORT
2513 return;
2514 }
2515
2518 && has_handicap(pplayer, H_AWAY)) {
2519 /* Don't move sentried or fortified units controlled by a player
2520 * in away mode. */
2521 unit_data->done = TRUE;
2522 return;
2523 }
2524
2525 /* Since military units re-evaluate their actions every turn,
2526 we must make sure that previously reserved ferry is freed. */
2528
2530 /* Try hunting with this unit */
2531 if (dai_hunter_qualify(pplayer, punit)) {
2532 int result, sanity = punit->id;
2533
2534 UNIT_LOG(LOGLEVEL_HUNT, punit, "is qualified as hunter");
2535 result = dai_hunter_manage(ait, pplayer, punit);
2538 return; /* died */
2539 }
2540 if (result == -1) {
2541 (void) dai_hunter_manage(ait, pplayer, punit); /* More carnage */
2543 return;
2544 } else if (result >= 1) {
2546 return; /* Done moving */
2547 } else if (unit_data->task == AIUNIT_HUNTER) {
2548 /* This should be very rare */
2550 }
2551 } else if (unit_data->task == AIUNIT_HUNTER) {
2553 }
2555
2556 /* Do we have a specific job for this unit? If not, we default
2557 * to attack. */
2558 dai_military_findjob(ait, nmap, pplayer, punit);
2559
2560 switch (unit_data->task) {
2562 case AIUNIT_BUILD_CITY:
2563 fc_assert(FALSE); /* This is not the place for this role */
2564 break;
2565 case AIUNIT_DEFEND_HOME:
2567 dai_military_defend(ait, pplayer, punit);
2569 break;
2570 case AIUNIT_ATTACK:
2571 case AIUNIT_NONE:
2573 dai_military_attack(ait, pplayer, punit);
2575 break;
2576 case AIUNIT_ESCORT:
2578 dai_military_bodyguard(ait, pplayer, punit);
2580 break;
2581 case AIUNIT_EXPLORE:
2582 switch (manage_auto_explorer(punit)) {
2583 case MR_DEATH:
2584 /* don't use punit! */
2585 return;
2586 case MR_OK:
2587 UNIT_LOG(LOG_DEBUG, punit, "more exploring");
2588 break;
2589 default:
2590 UNIT_LOG(LOG_DEBUG, punit, "no more exploring either");
2591 break;
2592 };
2593 def_ai_unit_data(punit, ait)->done = (punit->moves_left <= 0);
2594 break;
2595 case AIUNIT_RECOVER:
2599 break;
2600 case AIUNIT_HUNTER:
2601 fc_assert(FALSE); /* dealt with above */
2602 break;
2603 default:
2605 }
2606
2607 /* If we are still alive, either sentry or fortify. */
2608 if ((punit = game_unit_by_number(id))) {
2610 struct city *pcity = tile_city(unit_tile(punit));
2611
2612 if (unit_list_find(unit_tile(punit)->units,
2613 unit_data->ferryboat)) {
2615 } else if (pcity || punit->activity == ACTIVITY_IDLE) {
2616 /* We do not need to fortify in cities - we fortify and sentry
2617 * according to home defense setup, for easy debugging. */
2618 if (!pcity || unit_data->task == AIUNIT_DEFEND_HOME) {
2622 }
2623 } else {
2625 }
2626 }
2627 }
2628}
2629
2630/**********************************************************************/
2633static void dai_manage_settler(struct ai_type *ait, struct player *pplayer,
2634 struct unit *punit)
2635{
2636 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2637
2639 unit_data->done = TRUE; /* we will manage this unit later... ugh */
2640 /* if BUILD_CITY must remain BUILD_CITY, otherwise turn into autosettler */
2641 if (unit_data->task == AIUNIT_NONE) {
2643 }
2644 return;
2645}
2646
2647/**********************************************************************/
2655void dai_manage_unit(struct ai_type *ait, struct player *pplayer,
2656 struct unit *punit)
2657{
2658 struct unit_ai *unit_data;
2659 struct unit *bodyguard = aiguard_guard_of(ait, punit);
2660 bool is_ferry = FALSE;
2661 const struct unit_type *ptype;
2662 const struct civ_map *nmap = &(wld.map);
2663
2665
2666 /* Don't manage the unit if it is under human orders. */
2667 if (unit_has_orders(punit)) {
2669
2670 UNIT_LOG(LOG_VERBOSE, punit, "is under human orders, aborting AI control.");
2672 unit_data->done = TRUE;
2673 return;
2674 }
2675
2676 /* Check if we have lost our bodyguard. If we never had one, all
2677 * fine. If we had one and lost it, ask for a new one. */
2678 if (!bodyguard && aiguard_has_guard(ait, punit)) {
2679 UNIT_LOG(LOGLEVEL_BODYGUARD, punit, "lost bodyguard, asking for new");
2681 }
2682
2684
2685 if (punit->moves_left <= 0) {
2686 /* Can do nothing */
2687 unit_data->done = TRUE;
2688 return;
2689 }
2690
2691 is_ferry = dai_is_ferry(punit, ait);
2692
2694
2697 dai_manage_diplomat(ait, pplayer, punit);
2699 return;
2700 } else if (ptype->adv.worker
2702 dai_manage_settler(ait, pplayer, punit);
2703 return;
2708 dai_manage_caravan(ait, pplayer, punit);
2710 return;
2712 dai_manage_barbarian_leader(ait, pplayer, punit);
2713 return;
2716 dai_manage_paratrooper(ait, pplayer, punit);
2717 return;
2718 } else if (is_ferry && unit_data->task != AIUNIT_HUNTER) {
2720 dai_manage_ferryboat(ait, pplayer, punit);
2722 return;
2723 } else if (utype_fuel(ptype)
2724 && unit_data->task != AIUNIT_ESCORT) {
2726 dai_manage_airunit(ait, pplayer, punit);
2728 return;
2729 } else if (is_losing_hp(punit)) {
2730 /* This unit is losing hitpoints over time */
2731
2732 /* TODO: We can try using air-unit code for helicopters, just
2733 * pretend they have fuel = HP / 3 or something. */
2734 unit_data->done = TRUE; /* we did our best, which was ...
2735 nothing */
2736 return;
2737 } else if (!is_special_unit(punit)) {
2739 UNIT_LOG(LOG_DEBUG, punit, "recruit unit for the military");
2740 dai_manage_military(ait, nmap, pplayer, punit);
2742 return;
2743 } else {
2744 /* what else could this be? -- Syela */
2745 switch (manage_auto_explorer(punit)) {
2746 case MR_DEATH:
2747 /* don't use punit! */
2748 break;
2749 case MR_OK:
2750 UNIT_LOG(LOG_DEBUG, punit, "now exploring");
2751 break;
2752 default:
2753 UNIT_LOG(LOG_DEBUG, punit, "fell through all unit tasks, defending");
2755 dai_military_defend(ait, pplayer, punit);
2756 break;
2757 };
2758 return;
2759 }
2760}
2761
2762/**********************************************************************/
2768static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
2769{
2770 city_list_iterate(pplayer->cities, pcity) {
2771 /* The idea here is that we should never keep more than two
2772 * units in permanent defense. */
2773 int total_defense = 0;
2774 int total_attack = def_ai_city_data(pcity, ait)->danger;
2775 bool emergency = FALSE;
2776 int count = 0;
2780 + pcity->feel[CITIZEN_ANGRY][FEELING_NATIONALITY];
2781 int entertainers = 0;
2782 bool enough = FALSE;
2783
2785 if (get_specialist_output(pcity, sp, O_LUXURY) > 0) {
2786 entertainers += pcity->specialists[sp];
2787 }
2789
2790 martless_unhappy += entertainers; /* We want to use martial law instead
2791 * of entertainers. */
2792
2793 while (!enough
2796 && martless_unhappy > mart_each * count))) {
2797 int best_want = 0;
2798 struct unit *best = NULL;
2799 bool defense_needed = total_defense <= total_attack; /* Defense or martial */
2800
2801 unit_list_iterate(pcity->tile->units, punit) {
2802 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2803
2804 if ((unit_data->task == AIUNIT_NONE || emergency)
2805 && unit_data->task != AIUNIT_DEFEND_HOME
2806 && unit_owner(punit) == pplayer) {
2807 int want = assess_defense_unit(ait, pcity, punit, FALSE);
2808
2809 if (want > best_want) {
2810 best_want = want;
2811 best = punit;
2812 }
2813 }
2815
2816 if (best == NULL) {
2817 if (defense_needed) {
2818 /* Ooops - try to grab any unit as defender! */
2819 if (emergency) {
2820 CITY_LOG(LOG_DEBUG, pcity, "Not defended properly");
2821 break;
2822 }
2823 emergency = TRUE;
2824 } else {
2825 break;
2826 }
2827 } else {
2828 const struct unit_type *btype = unit_type_get(best);
2829
2830 if ((martless_unhappy < mart_each * count
2831 || count >= mart_max || mart_each <= 0)
2832 && ((count >= 2
2833 && btype->attack_strength > btype->defense_strength)
2834 || (count >= 4
2835 && btype->attack_strength == btype->defense_strength))) {
2836 /* In this case attack would be better defense than fortifying
2837 * to city. */
2838 enough = TRUE;
2839 } else {
2840 int loglevel = pcity->server.debug ? LOG_AI_TEST : LOG_DEBUG;
2841
2843 UNIT_LOG(loglevel, best, "Defending city");
2844 dai_unit_new_task(ait, best, AIUNIT_DEFEND_HOME, pcity->tile);
2845 count++;
2846 }
2847 }
2848 }
2849 CITY_LOG(LOG_DEBUG, pcity, "Evaluating defense: %d defense, %d incoming"
2850 ", %d defenders (out of %d)", total_defense, total_attack, count,
2851 unit_list_size(pcity->tile->units));
2853}
2854
2855/**********************************************************************/
2863void dai_manage_units(struct ai_type *ait, struct player *pplayer)
2864{
2866 dai_airlift(ait, pplayer);
2868
2869 /* Clear previous orders, if desirable, here. */
2870 unit_list_iterate(pplayer->units, punit) {
2871 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
2872
2873 unit_data->done = FALSE;
2874 if (unit_data->task == AIUNIT_DEFEND_HOME) {
2876 }
2878
2879 /* Find and set city defenders first - figure out which units are
2880 * allowed to leave home. */
2881 dai_set_defenders(ait, pplayer);
2882
2885 && !def_ai_unit_data(punit, ait)->done) {
2886 /* Though it is usually the passenger who drives the transport,
2887 * the transporter is responsible for managing its passengers. */
2888 dai_manage_unit(ait, pplayer, punit);
2889 }
2891}
2892
2893/**********************************************************************/
2899 const struct city *pcity)
2900{
2901 const struct impr_type *impr_req = NULL;
2902 const struct req_context context = {
2903 .player = city_owner(pcity),
2904 .city = pcity,
2905 .tile = city_tile(pcity),
2906 .unittype = putype,
2907 };
2908
2909 requirement_vector_iterate(&putype->build_reqs, preq) {
2911 /* Already there. */
2912 continue;
2913 }
2915 city_owner(pcity), pcity)) {
2916 /* The unit type can't be built at all. */
2917 return NULL;
2918 }
2919 if (VUT_IMPROVEMENT == preq->source.kind && preq->present) {
2920 /* This is (one of) the building(s) required. */
2921 impr_req = preq->source.value.building;
2922 }
2924
2925 return impr_req;
2926}
2927
2928/**********************************************************************/
2933 const struct unit_type *base)
2934{
2935 /* This is the real function: */
2936 do {
2937 base = base->obsoleted_by;
2938 if (base == test) {
2939 return TRUE;
2940 }
2941 } while (base);
2942 return FALSE;
2943}
2944
2945/**********************************************************************/
2950static void dai_manage_barbarian_leader(struct ai_type *ait,
2951 struct player *pplayer,
2952 struct unit *leader)
2953{
2955 struct pf_parameter parameter;
2956 struct pf_map *pfm;
2957 struct pf_reverse_map *pfrm;
2958 struct unit *worst_danger;
2959 int move_cost, best_move_cost;
2960 int body_guards;
2961 bool alive = TRUE;
2962 const struct civ_map *nmap = &(wld.map);
2963
2965
2966 if (leader->moves_left == 0
2968 && 1 < unit_list_size(leader_tile->units))) {
2970
2971 return;
2972 }
2973
2974 if (is_boss_of_boat(ait, leader)) {
2975 /* We are in charge. Of course, since we are the leader...
2976 * But maybe somebody more militaristic should lead our ship to battle! */
2977
2978 /* First release boat from leaders lead */
2980
2984 && warrior->moves_left > 0) {
2985 /* This seems like a good warrior to lead us in to conquest! */
2986 dai_manage_unit(ait, pplayer, warrior);
2987
2988 /* If we reached our destination, ferryboat already called
2989 * ai_manage_unit() for leader. So no need to continue here.
2990 * Leader might even be dead.
2991 * If this return is removed, surrounding unit_list_iterate()
2992 * has to be replaced with unit_list_iterate_safe()*/
2993 return;
2994 }
2996 }
2997
2998 /* If we are not in charge of the boat, continue as if we
2999 * were not in a boat - we may want to leave the ship now. */
3000
3001 /* Check the total number of units able to protect our leader. */
3002 body_guards = 0;
3003 unit_list_iterate(pplayer->units, punit) {
3006 body_guards++;
3007 }
3009
3010 if (0 < body_guards) {
3011 pft_fill_unit_parameter(&parameter, nmap, leader);
3012 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3013 pfm = pf_map_new(&parameter);
3014
3015 /* Find the closest body guard.
3016 * FIXME: maybe choose the strongest too? */
3017 pf_map_tiles_iterate(pfm, ptile, FALSE) {
3018 unit_list_iterate(ptile->units, punit) {
3019 if (unit_owner(punit) == pplayer
3022 struct pf_path *path = pf_map_path(pfm, ptile);
3023
3024 adv_follow_path(leader, path, ptile);
3025 pf_path_destroy(path);
3027 return;
3028 }
3031
3033 }
3034
3035 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader needs to flee");
3036
3037 /* Check for units we could fear. */
3038 pfrm = pf_reverse_map_new(nmap, pplayer, leader_tile, 3,
3039 !has_handicap(pplayer, H_MAP));
3042
3043 players_iterate(other_player) {
3044 if (other_player == pplayer) {
3045 continue;
3046 }
3047
3048 unit_list_iterate(other_player->units, punit) {
3050 if (PF_IMPOSSIBLE_MC != move_cost && move_cost < best_move_cost) {
3051 best_move_cost = move_cost;
3053 }
3056
3058
3059 if (NULL == worst_danger) {
3061 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: no close enemy.");
3062 return;
3063 }
3064
3066 parameter.omniscience = !has_handicap(pplayer, H_MAP);
3067 pfm = pf_map_new(&parameter);
3069
3070 /* Try to escape. */
3071 do {
3073
3074 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: moves left: %d.",
3075 leader->moves_left);
3076
3079 continue;
3080 }
3081
3082 move_cost = pf_map_move_cost(pfm, near_tile);
3083 if (PF_IMPOSSIBLE_MC != move_cost
3084 && move_cost > best_move_cost) {
3086 "Barbarian leader: safest is (%d, %d), safeness %d",
3088 best_move_cost = move_cost;
3090 }
3092
3093 UNIT_LOG(LOG_DEBUG, leader, "Barbarian leader: fleeing to (%d, %d).",
3097 "Barbarian leader: reached the safest position.");
3100 return;
3101 }
3102
3104 if (alive) {
3106 /* Didn't move. No point to retry. */
3108 return;
3109 }
3111 }
3112 } while (alive && 0 < leader->moves_left);
3113
3115}
3116
3117/**********************************************************************/
3123void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile,
3124 struct unit *punit,
3125 enum override_bool *result)
3126{
3127 int a = 0, d, db;
3128 struct player *pplayer = unit_owner(punit);
3129 struct city *pcity = tile_city(ptile);
3130 int extras_bonus = 0;
3131
3132 if (is_human(pplayer)) {
3133 /* Use advisors code for humans. */
3134 return;
3135 }
3136
3137 if (pcity && pplayers_allied(city_owner(pcity), unit_owner(punit))
3138 && !is_non_allied_unit_tile(ptile, pplayer)) {
3139 /* We will be safe in a friendly city */
3140 *result = OVERRIDE_FALSE;
3141 return;
3142 }
3143
3144 /* Calculate how well we can defend at (x,y) */
3145 db = 10 + tile_terrain(ptile)->defense_bonus / 10;
3147
3148 db += (db * extras_bonus) / 100;
3150
3151 adjc_iterate(&(wld.map), ptile, ptile1) {
3152 if (has_handicap(pplayer, H_FOG)
3154 /* We cannot see danger at (ptile1) => assume there is none */
3155 continue;
3156 }
3157 unit_list_iterate(ptile1->units, enemy) {
3160 == ATT_OK)
3162 == ATT_OK)) {
3164 if ((a * a * 10) >= d) {
3165 /* The enemies combined strength is too big! */
3166 *result = OVERRIDE_TRUE;
3167 return;
3168 }
3169 }
3172
3173 *result = OVERRIDE_FALSE;
3174}
3175
3176/**********************************************************************/
3179static void update_simple_ai_types(void)
3180{
3181 int i = 0;
3182
3185
3188 && !(pclass->adv.land_move == MOVE_NONE
3191 && punittype->transport_capacity < 8) {
3193 i++;
3194 }
3196
3198}
3199
3200/**********************************************************************/
3204{
3205 /* TODO: remove the simple_ai_types cache or merge it with a general ai
3206 * cache; see the comment to struct unit_type *simple_ai_types at
3207 * the beginning of this file. */
3209
3211 struct unit_type_ai *utai = fc_malloc(sizeof(*utai));
3212
3213 utai->low_firepower = FALSE;
3214 utai->ferry = FALSE;
3215 utai->missile_platform = FALSE;
3216 utai->carries_occupiers = FALSE;
3217 utai->potential_charges = unit_type_list_new();
3218
3221
3224
3225 /* Confirm firepower */
3227 if (pbonus->type == CBONUS_LOW_FIREPOWER) {
3229 if (utype_has_flag(penemy, pbonus->flag)) {
3230 struct unit_type_ai *utai = utype_ai_data(penemy, ait);
3231
3232 utai->low_firepower = TRUE;
3233 }
3235 }
3237
3238 /* Consider potential cargo */
3239 if (punittype->transport_capacity > 0) {
3240 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3241
3244
3247 utai->missile_platform = TRUE;
3248 } else if (pclass->adv.sea_move != MOVE_NONE
3249 && pcargo->adv.land_move != MOVE_NONE) {
3250 if (pcargo->adv.sea_move != MOVE_FULL) {
3251 utai->ferry = TRUE;
3252 } else {
3253 if (0 != utype_fuel(pctype)) {
3254 utai->ferry = TRUE;
3255 }
3256 }
3257 }
3258
3260 utai->carries_occupiers = TRUE;
3261 }
3262 }
3264 }
3265
3266 /* Consider potential charges */
3269
3270 if (0 < utype_fuel(punittype)
3271 && (0 == utype_fuel(pcharge)
3273 continue;
3274 }
3275
3276 unit_class_list_iterate(pclass->cache.subset_movers, chgcls) {
3277 if (chgcls == utype_class(pcharge)) {
3279 }
3281
3283 struct unit_type_ai *utai = utype_ai_data(punittype, ait);
3284 unit_type_list_append(utai->potential_charges, pcharge);
3285 }
3286
3289}
3290
3291/**********************************************************************/
3295{
3297 struct unit_type_ai *utai = utype_ai_data(ptype, ait);
3298
3299 if (utai == NULL) {
3300 continue;
3301 }
3303
3304 unit_type_list_destroy(utai->potential_charges);
3305 free(utai);
3307}
3308
3309/**********************************************************************/
3312void dai_unit_init(struct ai_type *ait, struct unit *punit)
3313{
3314 /* Make sure that contents of unit_ai structure are correctly initialized,
3315 * if you ever allocate it by some other mean than fc_calloc() */
3316 struct unit_ai *unit_data = fc_calloc(1, sizeof(struct unit_ai));
3317
3318 unit_data->done = FALSE;
3319 unit_data->cur_pos = NULL;
3320 unit_data->prev_pos = NULL;
3321 unit_data->target = 0;
3322 BV_CLR_ALL(unit_data->hunted);
3323 unit_data->ferryboat = 0;
3324 unit_data->passenger = 0;
3325 unit_data->bodyguard = 0;
3326 unit_data->charge = 0;
3327
3329}
3330
3331/**********************************************************************/
3334void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
3335{
3336 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3337
3339
3340 BV_CLR_ALL(unit_data->hunted);
3341}
3342
3343/**********************************************************************/
3346void dai_unit_close(struct ai_type *ait, struct unit *punit)
3347{
3348 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3349
3351
3354
3355 if (unit_data != NULL) {
3358 }
3359}
3360
3361/**********************************************************************/
3364void dai_unit_save(struct ai_type *ait, const char *aitstr,
3365 struct section_file *file,
3366 const struct unit *punit, const char *unitstr)
3367{
3368 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3369
3370 secfile_insert_int(file, unit_data->passenger, "%s.%spassenger",
3371 unitstr, aitstr);
3372 secfile_insert_int(file, unit_data->ferryboat, "%s.%sferryboat",
3373 unitstr, aitstr);
3374 secfile_insert_int(file, unit_data->charge, "%s.%scharge",
3375 unitstr, aitstr);
3376 secfile_insert_int(file, unit_data->bodyguard, "%s.%sbodyguard",
3377 unitstr, aitstr);
3378}
3379
3380/**********************************************************************/
3383void dai_unit_load(struct ai_type *ait, const char *aitstr,
3384 const struct section_file *file,
3385 struct unit *punit, const char *unitstr)
3386{
3387 struct unit_ai *unit_data = def_ai_unit_data(punit, ait);
3388
3389 unit_data->passenger
3390 = secfile_lookup_int_default(file, 0, "%s.%spassenger",
3391 unitstr, aitstr);
3392 unit_data->ferryboat
3393 = secfile_lookup_int_default(file, 0, "%s.%sferryboat",
3394 unitstr, aitstr);
3395 unit_data->charge
3396 = secfile_lookup_int_default(file, 0, "%s.%scharge",
3397 unitstr, aitstr);
3398 unit_data->bodyguard
3399 = secfile_lookup_int_default(file, 0, "%s.%sbodyguard",
3400 unitstr, aitstr);
3401}
3402
3408
3409/**********************************************************************/
3412static bool role_unit_cb(struct unit_type *ptype, void *data)
3413{
3414 struct role_unit_cb_data *cb_data = (struct role_unit_cb_data *)data;
3416 const struct civ_map *nmap = &(wld.map);
3417
3418 if ((cb_data->tc == TC_LAND && pclass->adv.land_move == MOVE_NONE)
3419 || (cb_data->tc == TC_OCEAN && pclass->adv.sea_move == MOVE_NONE)) {
3420 return FALSE;
3421 }
3422
3423 if (cb_data->build_city == NULL
3424 || can_city_build_unit_now(nmap, cb_data->build_city, ptype)) {
3425 return TRUE;
3426 }
3427
3428 return FALSE;
3429}
3430
3431/**********************************************************************/
3435 enum terrain_class tc)
3436{
3437 struct role_unit_cb_data cb_data = { .build_city = pcity, .tc = tc };
3438
3440}
3441
3442/**********************************************************************/
3445bool dai_unit_can_strike_my_unit(const struct unit *attacker,
3446 const struct unit *defender)
3447{
3448 struct pf_parameter parameter;
3449 struct pf_map *pfm;
3450 const struct tile *ptarget = unit_tile(defender);
3451 int max_move_cost = attacker->moves_left;
3452 bool able_to_strike = FALSE;
3453 const struct civ_map *nmap = &(wld.map);
3454
3455 pft_fill_unit_parameter(&parameter, nmap, attacker);
3456 parameter.omniscience = !has_handicap(unit_owner(defender), H_MAP);
3457 pfm = pf_map_new(&parameter);
3458
3459 pf_map_move_costs_iterate(pfm, ptile, move_cost, FALSE) {
3460 if (move_cost > max_move_cost) {
3461 break;
3462 }
3463
3464 if (ptile == ptarget) {
3466 break;
3467 }
3469
3471
3472 return able_to_strike;
3473}
3474
3475/**********************************************************************/
3478void dai_switch_to_explore(struct ai_type *ait, struct unit *punit,
3479 struct tile *target, enum override_bool *allow)
3480{
3481 struct unit_ai *udata = def_ai_unit_data(punit, ait);
3482
3483 if (udata->task != AIUNIT_NONE && udata->task != AIUNIT_EXPLORE) {
3485
3486 return;
3487 }
3488}
bool is_action_enabled_unit_on_stack(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit, const struct tile *target_tile)
Definition actions.c:4006
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 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:1282
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:1417
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:3364
static void dai_military_findjob(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:855
static bool has_defense(struct city *pcity)
Definition daiunit.c: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:2220
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:1985
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:703
void dai_units_ruleset_init(struct ai_type *ait)
Definition daiunit.c:3203
#define LOG_CARAVAN
Definition daiunit.c:88
static int unit_att_rating_now(const struct unit *punit)
Definition daiunit.c:259
static void dai_military_attack_barbarian(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1684
static void invasion_funct(struct ai_type *ait, struct unit *punit, bool dest, int radius, int which)
Definition daiunit.c:1020
static void update_simple_ai_types(void)
Definition daiunit.c:3179
const struct impr_type * utype_needs_improvement(const struct unit_type *putype, const struct city *pcity)
Definition daiunit.c:2898
void dai_switch_to_explore(struct ai_type *ait, struct unit *punit, struct tile *target, enum override_bool *allow)
Definition daiunit.c:3478
void dai_manage_military(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2500
static bool dai_find_boat_for_unit(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:1901
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:3445
bool is_on_unit_upgrade_path(const struct unit_type *test, const struct unit_type *base)
Definition daiunit.c:2932
static void dai_military_bodyguard(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:637
static void single_invader(struct ai_city *city_data, const struct unit_type *utype, int which)
Definition daiunit.c:992
#define LOG_CARAVAN3
Definition daiunit.c:90
void dai_manage_units(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2863
static void dai_manage_caravan(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2257
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:1137
static void dai_set_defenders(struct ai_type *ait, struct player *pplayer)
Definition daiunit.c:2768
void dai_manage_unit(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2655
adv_want look_for_charge(struct ai_type *ait, const struct civ_map *nmap, struct player *pplayer, struct unit *punit, struct unit **aunit, struct city **acity)
Definition daiunit.c:718
static bool role_unit_cb(struct unit_type *ptype, void *data)
Definition daiunit.c:3412
static void dai_military_attack(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:1747
void dai_unit_turn_end(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3334
static void dai_manage_settler(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:2633
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:2116
static int dai_rampage_want(struct unit *punit, struct tile *ptile)
Definition daiunit.c:457
bool dai_can_unit_type_follow_unit_type(const struct unit_type *follower, const struct unit_type *followee, struct ai_type *ait)
Definition daiunit.c:837
struct city * find_nearest_safe_city(struct unit *punit)
Definition daiunit.c:1633
static void dai_manage_barbarian_leader(struct ai_type *ait, struct player *pplayer, struct unit *leader)
Definition daiunit.c:2950
static void dai_manage_hitpoint_recovery(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:2425
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:525
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:3312
void dai_consider_tile_dangerous(struct ai_type *ait, struct tile *ptile, struct unit *punit, enum override_bool *result)
Definition daiunit.c:3123
bool uclass_need_trans_between(struct unit_class *pclass, struct tile *ctile, struct tile *ptile)
Definition daiunit.c:1946
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:3383
static void caravan_optimize_callback(const struct caravan_result *result, void *data)
Definition daiunit.c:2097
#define LOGLEVEL_RECOVERY
Definition daiunit.c:87
void dai_unit_close(struct ai_type *ait, struct unit *punit)
Definition daiunit.c:3346
void dai_units_ruleset_close(struct ai_type *ait)
Definition daiunit.c:3294
struct unit_type * dai_role_utype_for_terrain_class(struct city *pcity, int role, enum terrain_class tc)
Definition daiunit.c:3434
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:1062
static void dai_military_defend(struct ai_type *ait, struct player *pplayer, struct unit *punit)
Definition daiunit.c:946
bool dai_military_rampage(struct unit *punit, int thresh_adj, int thresh_move)
Definition daiunit.c:604
static bool dai_caravan_can_trade_cities_diff_cont(struct player *pplayer, struct unit *punit)
Definition daiunit.c:2175
#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:931
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:940
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:630
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:654
#define iterate_outward(nmap, start_tile, max_dist, itr_tile)
Definition map.h:367
#define iterate_outward_end
Definition map.h:371
#define adjc_iterate_end
Definition map.h:433
static int index_to_map_pos_y(int mindex)
Definition map.h: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:3405
struct city * build_city
Definition daiunit.c:3406
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