Freeciv-3.2
Loading...
Searching...
No Matches
unit.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/* utility */
19#include "astring.h"
20#include "bitvector.h"
21#include "fcintl.h"
22#include "mem.h"
23#include "shared.h"
24#include "support.h"
25
26/* common */
27#include "ai.h"
28#include "actions.h"
29#include "base.h"
30#include "city.h"
31#include "game.h"
32#include "log.h"
33#include "map.h"
34#include "movement.h"
35#include "packets.h"
36#include "player.h"
37#include "road.h"
38#include "tech.h"
39#include "traderoutes.h"
40#include "unitlist.h"
41
42#include "unit.h"
43
48
54#define CARGO_ITER(iter) ((struct cargo_iter *) (iter))
55
56/**********************************************************************/
60 const struct unit_order *order2)
61{
62 return order1->order == order2->order
63 && order1->activity == order2->activity
64 && order1->target == order2->target
65 && order1->sub_target == order2->sub_target
66 && order1->action == order2->action
67 && order1->dir == order2->dir;
68}
69
70/**********************************************************************/
86 const struct player *restriction,
87 const struct unit *punit,
88 const struct city *pdest_city)
89{
90 const struct city *psrc_city = tile_city(unit_tile(punit));
91 const struct player *punit_owner;
92 const struct tile *dst_tile = NULL;
93 const struct unit_type *putype = unit_type_get(punit);
95
96 if (0 == punit->moves_left
98 /* No moves left. */
99 return AR_NO_MOVES;
100 }
101
103 return AR_WRONG_UNITTYPE;
104 }
105
107 /* Units with occupants can't be airlifted currently. */
108 return AR_OCCUPIED;
109 }
110
111 if (NULL == psrc_city) {
112 /* No city there. */
113 return AR_NOT_IN_CITY;
114 }
115
116 if (psrc_city == pdest_city) {
117 /* Airlifting to our current position doesn't make sense. */
118 return AR_BAD_DST_CITY;
119 }
120
121 if (NULL != pdest_city) {
123
124 if (NULL != restriction
127 /* Can't exist at the destination tile. */
128 return AR_BAD_DST_CITY;
129 }
130 }
131
133
134 /* Check validity of both source and destination before checking capacity,
135 * to avoid misleadingly optimistic returns. */
136
140 /* Not allowed to airlift from this source. */
141 return AR_BAD_SRC_CITY;
142 }
143
148 /* Not allowed to airlift to this destination. */
149 return AR_BAD_DST_CITY;
150 }
151
152 /* Check airlift capacities */
155 /* We know for sure whether or not src can airlift this turn. */
156 if (0 >= psrc_city->airlift
158 /* The source cannot airlift for this turn (maybe already airlifted
159 * or no airport).
160 * See also do_airline() in server/unittools.h. */
161 return AR_SRC_NO_FLIGHTS;
162 } /* else, there is capacity; continue to other checks */
163 } else {
164 /* We don't have access to the 'airlift' field. Assume it's OK; can
165 * only find out for sure by trying it. */
167 }
168 }
169
172 if (0 >= pdest_city->airlift
174 /* The destination cannot support airlifted units for this turn
175 * (maybe already airlifted or no airport).
176 * See also do_airline() in server/unittools.h. */
177 return AR_DST_NO_FLIGHTS;
178 } /* else continue */
179 } else {
181 }
182 }
183
184 return ok_result;
185}
186
187/**********************************************************************/
195 const struct unit *punit,
196 const struct city *pdest_city)
197{
198 if (is_server()) {
201 } else {
203 pdest_city));
204 }
205}
206
207/**********************************************************************/
210bool unit_has_orders(const struct unit *punit)
211{
212 return punit->has_orders;
213}
214
215/**********************************************************************/
222int unit_shield_value(const struct unit *punit,
223 const struct unit_type *punittype,
224 const struct action *paction)
225{
226 int value;
227
228 bool has_unit;
229 const struct player *act_player;
230
231 has_unit = punit != NULL;
232
233 if (has_unit && punittype == NULL) {
235 }
236
240
242 /* TODO: determine if tile and city should be where the unit currently is
243 * located or the target city. Those two may differ. Wait for ruleset
244 * author feed back. */
245
247 value += ((value
249 &(const struct req_context) {
250 .player = act_player,
251 .unit = punit,
252 .unittype = punittype,
253 .action = paction,
254 },
255 NULL,
257 / 100);
258
259 return value;
260}
261
262/**********************************************************************/
267 const struct unit *punit)
268{
269 struct city *pcity = tile_city(unit_tile(punit));
270
271 if (pcity == NULL) {
272 /* No city to help at this tile. */
273 return FALSE;
274 }
275
277 /* This unit can never do help wonder. */
278 return FALSE;
279 }
280
281 /* Evaluate all action enablers for extra accuracy. */
282 /* TODO: Is it worth it? */
285 pcity));
286}
287
288/**********************************************************************/
301
302/**********************************************************************/
306{
308}
309
310/**********************************************************************/
320
321/**********************************************************************/
324bool is_martial_law_unit(const struct unit *punit)
325{
327}
328
329/**********************************************************************/
332bool is_occupying_unit(const struct unit *punit)
333{
335}
336
337/**********************************************************************/
341{
343}
344
345/**********************************************************************/
348bool is_guard_unit(const struct unit *punit)
349{
351}
352
353/**********************************************************************/
358bool is_special_unit(const struct unit *punit)
359{
361}
362
363/**********************************************************************/
367bool unit_can_do_action(const struct unit *punit,
368 const action_id act_id)
369{
370 return utype_can_do_action(unit_type_get(punit), act_id);
371}
372
373/**********************************************************************/
378 enum action_result result)
379{
381}
382
383/**********************************************************************/
392
393/**********************************************************************/
397 const struct player *pplayer,
398 const struct tile *ptile, bool omniscient)
399{
400 square_iterate(nmap, ptile, 2, ptile1) {
402 if ((omniscient
403 || can_player_see_unit(pplayer, punit))
404 && pplayers_at_war(pplayer, unit_owner(punit))
409 unit_class_get(punit), ptile)))) {
410 return TRUE;
411 }
414
415 return FALSE;
416}
417
418/**********************************************************************/
422bool is_field_unit(const struct unit *punit)
423{
425}
426
427/**********************************************************************/
434bool is_hiding_unit(const struct unit *punit)
435{
437
438 if (vl == V_INVIS || vl == V_SUBSURFACE) {
439 return TRUE;
440 }
441
442 if (unit_transported(punit)) {
444 if (vl == V_INVIS || vl == V_SUBSURFACE) {
445 return TRUE;
446 }
447 }
448
449 return FALSE;
450}
451
452/**********************************************************************/
457 const struct unit *punit)
458{
459 struct city *tgt_city;
460
461 if ((tgt_city = tile_city(unit_tile(punit)))) {
464 } else {
467 }
468}
469
470/**********************************************************************/
474 const struct unit *punit,
475 const struct city *pcity)
476{
477 if (pcity == NULL) {
478 /* Can't change home city to a non existing city. */
479 return FALSE;
480 }
481
483 pcity));
484}
485
486/**********************************************************************/
490 const struct unit *punit)
491{
493}
494
495/**********************************************************************/
503int get_activity_rate(const struct unit *punit)
504{
505 const struct veteran_level *vlevel;
506 const struct unit_type *ptype;
507 int move_rate;
508
510
514
515 /* The speed of the settler depends on its base move_rate, not on
516 * the number of moves actually remaining or the adjusted move rate.
517 * This means sea formers won't have their activity rate increased by
518 * Magellan's, and it means injured units work just as fast as
519 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
521
522 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
523 return ACTIVITY_FACTOR
524 * (float)vlevel->power_fact / 100
526}
527
528/**********************************************************************/
535{
536 /* This logic is also coded in client/goto.c. */
537 if (punit->moves_left > 0) {
538 return get_activity_rate(punit);
539 } else {
540 return 0;
541 }
542}
543
544/**********************************************************************/
551 enum unit_activity activity,
552 const struct tile *ptile,
553 struct extra_type *tgt)
554{
555 /* FIXME: This is just an approximation since we don't account for
556 * get_activity_rate_this_turn. */
558 int points_needed;
559
560 fc_assert(tgt != NULL || !is_targeted_activity(activity));
561
562 points_needed = tile_activity_time(activity, ptile, tgt);
563
564 if (points_needed >= 0 && speed > 0) {
565 return (points_needed - 1) / speed + 1; /* Round up */
566 } else {
567 return FC_INFINITY;
568 }
569}
570
571/**********************************************************************/
575{
576 switch (activity) {
577 case ACTIVITY_PILLAGE:
578 case ACTIVITY_BASE:
581 case ACTIVITY_MINE:
582 case ACTIVITY_CLEAN:
583 return TRUE;
584 case ACTIVITY_IDLE:
586 case ACTIVITY_SENTRY:
587 case ACTIVITY_GOTO:
588 case ACTIVITY_EXPLORE:
591 case ACTIVITY_PLANT:
593 case ACTIVITY_CONVERT:
594 return FALSE;
595 /* These shouldn't be kicking around internally. */
596 case ACTIVITY_LAST:
597 break;
598 }
599
601
602 return FALSE;
603}
604
605/**********************************************************************/
615{
616 return unit_type_get(punit)->adv.worker;
617}
618
619/**********************************************************************/
622const char *get_activity_text(enum unit_activity activity)
623{
624 /* The switch statement has just the activities listed with no "default"
625 * handling. This enables the compiler to detect missing entries
626 * automatically, and still handles everything correctly. */
627 switch (activity) {
628 case ACTIVITY_IDLE:
629 return _("Idle");
630 case ACTIVITY_CLEAN:
631 return _("Clean");
632 case ACTIVITY_MINE:
633 /* TRANS: Activity name, verb in English */
634 return Q_("?act:Mine");
635 case ACTIVITY_PLANT:
636 /* TRANS: Activity name, verb in English */
637 return _("Plant");
639 return _("Irrigate");
641 return _("Cultivate");
643 return _("Fortifying");
645 return _("Fortified");
646 case ACTIVITY_SENTRY:
647 return _("Sentry");
648 case ACTIVITY_PILLAGE:
649 return _("Pillage");
650 case ACTIVITY_GOTO:
651 return _("Goto");
652 case ACTIVITY_EXPLORE:
653 return _("Explore");
655 return _("Transform");
656 case ACTIVITY_BASE:
657 return _("Base");
659 return _("Road");
660 case ACTIVITY_CONVERT:
661 return _("Convert");
662 case ACTIVITY_LAST:
663 break;
664 }
665
667 return _("Unknown");
668}
669
670/**********************************************************************/
677 const struct unit *ptrans)
678{
679 /* Make sure this transporter can carry this type of unit. */
681 return FALSE;
682 }
683
684 /* Make sure there's room in the transporter. */
687 return FALSE;
688 }
689
690 /* Check iff this is a valid transport. */
692 return FALSE;
693 }
694
695 /* Check transport depth. */
698 return FALSE;
699 }
700
701 return TRUE;
702}
703
704/**********************************************************************/
708bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
709{
710 if (!pcargo || !ptrans || pcargo == ptrans) {
711 return FALSE;
712 }
713
714 /* Double-check ownership of the units: you can load into an allied unit
715 * (of course only allied units can be on the same tile). */
717 return FALSE;
718 }
719
720 /* Un-embarkable transport must be in city or base to load cargo. */
724 return FALSE;
725 }
726
728}
729
730/**********************************************************************/
733bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
734{
735 /* This function needs to check EVERYTHING. */
736
737 /* Check positions of the units. Of course you can't load a unit onto
738 * a transporter on a different tile... */
740 return FALSE;
741 }
742
743 /* Cannot load if cargo is already loaded onto something else. */
745 return FALSE;
746 }
747
749}
750
751/**********************************************************************/
760bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
761{
762 if (!pcargo || !ptrans) {
763 return FALSE;
764 }
765
766 /* Make sure the unit's transporter exists and is known. */
768 return FALSE;
769 }
770
771 /* Un-disembarkable transport must be in city or base to unload cargo. */
775 return FALSE;
776 }
777
778 return TRUE;
779}
780
781/**********************************************************************/
808
809/**********************************************************************/
814bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
815{
818 return TRUE;
819 }
821
822 return FALSE;
823}
824
825/**********************************************************************/
845
846/**********************************************************************/
850 struct unit *punit)
851{
852 enum unit_activity current = punit->activity;
853 struct extra_type *target = punit->activity_target;
854 enum unit_activity current2 =
855 (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
856 bool result;
857
860
862
863 punit->activity = current;
864 punit->activity_target = target;
865
866 return result;
867}
868
869/**********************************************************************/
877 const struct unit *punit,
878 enum unit_activity activity)
879{
880 struct extra_type *target = NULL;
881
882 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
883 * being able to find out whether an activity is in general possible.
884 * Find one for them, but when they come to do the activity, they will
885 * have to determine the target themselves */
886 {
887 struct tile *ptile = unit_tile(punit);
888
889 if (activity == ACTIVITY_IRRIGATE) {
890 target = next_extra_for_tile(ptile,
893 punit);
894 if (NULL == target) {
895 return FALSE; /* No more irrigation extras available. */
896 }
897 } else if (activity == ACTIVITY_MINE) {
898 target = next_extra_for_tile(ptile,
899 EC_MINE,
901 punit);
902 if (NULL == target) {
903 return FALSE; /* No more mine extras available. */
904 }
905 }
906 }
907
908 return can_unit_do_activity_targeted(nmap, punit, activity, target);
909}
910
911/**********************************************************************/
916 const struct unit *punit,
917 enum unit_activity activity,
918 struct extra_type *target)
919{
920 return can_unit_do_activity_targeted_at(nmap, punit, activity, target,
922}
923
924/**********************************************************************/
929 const struct unit *punit,
930 enum unit_activity activity,
931 struct extra_type *target,
932 const struct tile *ptile)
933{
934 /* Check that no build activity conflicting with one already in progress
935 * gets executed. */
936 /* FIXME: Should check also the cases where one of the activities is terrain
937 * change that destroys the target of the other activity */
938 if (target != NULL && is_build_activity(activity)) {
939 if (tile_is_placing(ptile)) {
940 return FALSE;
941 }
942
943 unit_list_iterate(ptile->units, tunit) {
944 if (is_build_activity(tunit->activity)
945 && !can_extras_coexist(target, tunit->activity_target)) {
946 return FALSE;
947 }
949 }
950
951#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
952{ \
953 switch (action_get_target_kind(paction)) { \
954 case ATK_TILE: \
955 return is_action_enabled_unit_on_tile(nmap, paction->id, \
956 punit, ptile, target); \
957 case ATK_EXTRAS: \
958 return is_action_enabled_unit_on_extras(nmap, paction->id, \
959 punit, ptile, target); \
960 case ATK_CITY: \
961 case ATK_UNIT: \
962 case ATK_UNITS: \
963 case ATK_SELF: \
964 return FALSE; \
965 case ATK_COUNT: \
966 break; /* Handle outside switch */ \
967 } \
968 fc_assert(action_target_kind_is_valid( \
969 action_get_target_kind(paction))); \
970 return FALSE; \
971 }
972
973 switch (activity) {
974 case ACTIVITY_IDLE:
975 case ACTIVITY_GOTO:
976 return TRUE;
977
978 case ACTIVITY_CLEAN:
979 /* The call below doesn't support actor tile speculation. */
980 fc_assert_msg(unit_tile(punit) == ptile,
981 "Please use action_speculate_unit_on_tile()");
983 punit, ptile, target);
984
985 case ACTIVITY_MINE:
986 /* The call below doesn't support actor tile speculation. */
987 fc_assert_msg(unit_tile(punit) == ptile,
988 "Please use action_speculate_unit_on_tile()");
990 ptile, target);
991
992 case ACTIVITY_PLANT:
993 /* The call below doesn't support actor tile speculation. */
994 fc_assert_msg(unit_tile(punit) == ptile,
995 "Please use action_speculate_unit_on_tile()");
997 punit, ptile, NULL);
998
1000 /* The call below doesn't support actor tile speculation. */
1001 fc_assert_msg(unit_tile(punit) == ptile,
1002 "Please use action_speculate_unit_on_tile()");
1004 ptile, target);
1005
1006 case ACTIVITY_CULTIVATE:
1007 /* The call below doesn't support actor tile speculation. */
1008 fc_assert_msg(unit_tile(punit) == ptile,
1009 "Please use action_speculate_unit_on_tile()");
1011 punit, ptile, NULL);
1012
1014 /* The call below doesn't support actor tile speculation. */
1015 fc_assert_msg(unit_tile(punit) == ptile,
1016 "Please use action_speculate_unit_on_self()");
1018 punit);
1019
1020 case ACTIVITY_FORTIFIED:
1021 return FALSE;
1022
1023 case ACTIVITY_BASE:
1024 /* The call below doesn't support actor tile speculation. */
1025 fc_assert_msg(unit_tile(punit) == ptile,
1026 "Please use action_speculate_unit_on_tile()");
1028 punit, ptile, target);
1029
1030 case ACTIVITY_GEN_ROAD:
1031 /* The call below doesn't support actor tile speculation. */
1032 fc_assert_msg(unit_tile(punit) == ptile,
1033 "Please use action_speculate_unit_on_tile()");
1035 punit, ptile, target);
1036
1037 case ACTIVITY_SENTRY:
1039 && !unit_transported(punit)) {
1040 /* Don't let units sentry on tiles they will die on. */
1041 return FALSE;
1042 }
1043 return TRUE;
1044
1045 case ACTIVITY_PILLAGE:
1046 /* The call below doesn't support actor tile speculation. */
1047 fc_assert_msg(unit_tile(punit) == ptile,
1048 "Please use action_speculate_unit_on_tile()");
1050
1051 case ACTIVITY_EXPLORE:
1052 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1053
1054 case ACTIVITY_TRANSFORM:
1055 /* The call below doesn't support actor tile speculation. */
1056 fc_assert_msg(unit_tile(punit) == ptile,
1057 "Please use action_speculate_unit_on_tile()");
1059 punit, ptile, NULL);
1060
1061 case ACTIVITY_CONVERT:
1062 /* The call below doesn't support actor tile speculation. */
1063 fc_assert_msg(unit_tile(punit) == ptile,
1064 "Please use action_speculate_unit_on_self()");
1066
1067 case ACTIVITY_LAST:
1068 break;
1069 }
1070
1071 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1072 activity);
1073 return FALSE;
1074
1075#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1076}
1077
1078/**********************************************************************/
1083{
1085 punit->activity_count = 0;
1087 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1088 /* No longer done. */
1090 }
1091}
1092
1093/**********************************************************************/
1109
1110/**********************************************************************/
1127
1128/**********************************************************************/
1132 const struct tile *ptile)
1133{
1134 unit_list_iterate(ptile->units, punit) {
1135 if (punit->activity == activity) {
1136 return TRUE;
1137 }
1139 return FALSE;
1140}
1141
1142/**********************************************************************/
1147{
1149
1151 unit_list_iterate(ptile->units, punit) {
1154 }
1156
1157 return tgt_ret;
1158}
1159
1160/**********************************************************************/
1163void unit_activity_astr(const struct unit *punit, struct astring *astr)
1164{
1165 if (!punit || !astr) {
1166 return;
1167 }
1168
1169 switch (punit->activity) {
1170 case ACTIVITY_IDLE:
1172 int rate, f;
1173
1175 f = ((punit->fuel) - 1);
1176
1177 /* Add in two parts as move_points_text() returns ptr to static
1178 * End result: "Moves: (fuel)moves_left" */
1179 astr_add_line(astr, "%s: (%s)", _("Moves"),
1181 astr_add(astr, "%s",
1183 } else {
1184 astr_add_line(astr, "%s: %s", _("Moves"),
1186 }
1187 return;
1188 case ACTIVITY_CLEAN:
1189 case ACTIVITY_TRANSFORM:
1191 case ACTIVITY_FORTIFIED:
1192 case ACTIVITY_SENTRY:
1193 case ACTIVITY_GOTO:
1194 case ACTIVITY_EXPLORE:
1195 case ACTIVITY_CONVERT:
1196 case ACTIVITY_CULTIVATE:
1197 case ACTIVITY_PLANT:
1199 return;
1200 case ACTIVITY_MINE:
1201 case ACTIVITY_IRRIGATE:
1202 if (punit->activity_target == NULL) {
1204 } else {
1205 astr_add_line(astr, "Building %s",
1207 }
1208 return;
1209 case ACTIVITY_PILLAGE:
1210 if (punit->activity_target != NULL) {
1213 } else {
1215 }
1216 return;
1217 case ACTIVITY_BASE:
1220 return;
1221 case ACTIVITY_GEN_ROAD:
1224 return;
1225 case ACTIVITY_LAST:
1226 break;
1227 }
1228
1229 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1231}
1232
1233/**********************************************************************/
1239void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1240{
1241 if (!punit || !astr) {
1242 return;
1243 }
1244
1245 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1247 punit->upkeep[O_GOLD]);
1248}
1249
1250/**********************************************************************/
1253struct player *unit_nationality(const struct unit *punit)
1254{
1256 return punit->nationality;
1257}
1258
1259/**********************************************************************/
1263void unit_tile_set(struct unit *punit, struct tile *ptile)
1264{
1266 punit->tile = ptile;
1267}
1268
1269/**********************************************************************/
1276struct unit *tile_allied_unit(const struct tile *ptile,
1277 const struct player *pplayer)
1278{
1279 struct unit *punit = NULL;
1280
1281 unit_list_iterate(ptile->units, cunit) {
1282 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1283 punit = cunit;
1284 } else {
1285 return NULL;
1286 }
1287 }
1289
1290 return punit;
1291}
1292
1293/**********************************************************************/
1299struct unit *tile_enemy_unit(const struct tile *ptile,
1300 const struct player *pplayer)
1301{
1302 unit_list_iterate(ptile->units, punit) {
1303 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1304 return punit;
1305 }
1307
1308 return NULL;
1309}
1310
1311/**********************************************************************/
1314struct unit *tile_non_allied_unit(const struct tile *ptile,
1315 const struct player *pplayer)
1316{
1317 unit_list_iterate(ptile->units, punit) {
1318 if (!pplayers_allied(unit_owner(punit), pplayer)) {
1319 return punit;
1320 }
1321 }
1323
1324 return NULL;
1325}
1326
1327/**********************************************************************/
1331struct unit *tile_other_players_unit(const struct tile *ptile,
1332 const struct player *pplayer)
1333{
1334 unit_list_iterate(ptile->units, punit) {
1335 if (unit_owner(punit) != pplayer) {
1336 return punit;
1337 }
1339
1340 return NULL;
1341}
1342
1343/**********************************************************************/
1347struct unit *tile_non_attack_unit(const struct tile *ptile,
1348 const struct player *pplayer)
1349{
1350 unit_list_iterate(ptile->units, punit) {
1351 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1352 return punit;
1353 }
1354 }
1356
1357 return NULL;
1358}
1359
1360/**********************************************************************/
1368struct unit *unit_occupies_tile(const struct tile *ptile,
1369 const struct player *pplayer)
1370{
1371 unit_list_iterate(ptile->units, punit) {
1372 if (!is_occupying_unit(punit)) {
1373 continue;
1374 }
1375
1377 continue;
1378 }
1379
1380 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1381 return punit;
1382 }
1384
1385 return NULL;
1386}
1387
1388/**********************************************************************/
1395bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1396 const struct civ_map *zmap)
1397{
1399
1400 square_iterate(zmap, ptile0, 1, ptile) {
1401 struct terrain *pterrain;
1402 struct city *pcity;
1403
1404 pterrain = tile_terrain(ptile);
1405 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1406 continue;
1407 }
1408
1409 pcity = tile_non_allied_city(ptile, pplayer);
1410 if (pcity != NULL) {
1411 if (unit_list_size(ptile->units) > 0) {
1412 /* Occupied enemy city, it doesn't matter if units inside have
1413 * UTYF_NOZOC or not. */
1414 return FALSE;
1415 }
1416 } else {
1417 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1419 if (tile_has_extra(ptile, pextra)) {
1420 return FALSE;
1421 }
1423 }
1424
1425 unit_list_iterate(ptile->units, punit) {
1426 if (!pplayers_allied(unit_owner(punit), pplayer)
1429 bool hidden = FALSE;
1430
1431 /* We do NOT check the possibility that player is allied with an extra owner,
1432 * and should thus see inside the extra.
1433 * This is to avoid the situation where having an alliance with third player
1434 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1435 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1436 if (tile_has_extra(ptile, pextra)) {
1437 hidden = TRUE;
1438 break;
1439 }
1441
1442 if (!hidden) {
1443 return FALSE;
1444 }
1445 }
1447 }
1449
1450 return TRUE;
1451}
1452
1453/**********************************************************************/
1464bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1465 const struct civ_map *zmap)
1466{
1468
1469 square_iterate(zmap, ptile0, 1, ptile) {
1470 struct terrain *pterrain;
1471 struct city *pcity;
1472
1473 pterrain = tile_terrain(ptile);
1474 if (T_UNKNOWN == pterrain
1475 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1476 continue;
1477 }
1478
1479 pcity = tile_non_allied_city(ptile, pplayer);
1480 if (pcity != NULL) {
1481 if (pcity->client.occupied
1482 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1483 /* Occupied enemy city, it doesn't matter if units inside have
1484 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1485 return FALSE;
1486 }
1487 } else {
1488 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1490 if (tile_has_extra(ptile, pextra)) {
1491 return FALSE;
1492 }
1494 }
1495
1496 unit_list_iterate(ptile->units, punit) {
1498 && !pplayers_allied(unit_owner(punit), pplayer)
1500 return FALSE;
1501 }
1503 }
1505
1506 return TRUE;
1507}
1508
1509/**********************************************************************/
1517
1518/**********************************************************************/
1527 const struct unit *punit)
1528{
1529 const struct tile *ptile = unit_tile(punit);
1531
1532 if (!is_attack_unit(punit)) {
1533 return FALSE;
1534 }
1535 if (tile_city(ptile)) {
1536 return FALSE;
1537 }
1539 switch (game.info.happyborders) {
1540 case HB_DISABLED:
1541 break;
1542 case HB_NATIONAL:
1543 if (tile_owner(ptile) == unit_owner(punit)) {
1544 return FALSE;
1545 }
1546 break;
1547 case HB_ALLIANCE:
1549 return FALSE;
1550 }
1551 break;
1552 }
1553 }
1554
1557 if (max_friendliness_range >= 0) {
1559 }
1560
1561 return TRUE;
1562}
1563
1564/**********************************************************************/
1568{
1569 switch (activity) {
1570 case ACTIVITY_MINE:
1571 case ACTIVITY_IRRIGATE:
1572 case ACTIVITY_BASE:
1573 case ACTIVITY_GEN_ROAD:
1574 return TRUE;
1575 default:
1576 return FALSE;
1577 }
1578}
1579
1580/**********************************************************************/
1584{
1585 switch (activity) {
1586 case ACTIVITY_PILLAGE:
1587 case ACTIVITY_CLEAN:
1588 return TRUE;
1589 default:
1590 return FALSE;
1591 }
1592}
1593
1594/**********************************************************************/
1598{
1599 switch (activity) {
1600 case ACTIVITY_CULTIVATE:
1601 case ACTIVITY_PLANT:
1602 case ACTIVITY_TRANSFORM:
1603 return TRUE;
1604 default:
1605 return FALSE;
1606 }
1607}
1608
1609/**********************************************************************/
1613{
1614 return is_build_activity(activity)
1615 || is_clean_activity(activity)
1616 || is_terrain_change_activity(activity);
1617}
1618
1619/**********************************************************************/
1623{
1624 return is_build_activity(activity)
1625 || is_clean_activity(activity);
1626}
1627
1628/**********************************************************************/
1632struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1633 const struct unit_type *punittype,
1634 int veteran_level)
1635{
1636 /* Make sure that contents of unit structure are correctly initialized,
1637 * if you ever allocate it by some other mean than fc_calloc() */
1638 struct unit *punit = fc_calloc(1, sizeof(*punit));
1639 int max_vet_lvl;
1640
1641 /* It does not register the unit so the id is set to 0. */
1643
1644 fc_assert_ret_val(NULL != punittype, NULL); /* No untyped units! */
1646
1647 fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned units! */
1648 punit->owner = pplayer;
1649 punit->nationality = pplayer;
1650
1651 punit->refcount = 1;
1653
1654 if (pcity) {
1655 unit_tile_set(punit, pcity->tile);
1656 punit->homecity = pcity->id;
1657 } else {
1660 }
1661
1662 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1663 punit->goto_tile = NULL;
1666 /* A unit new and fresh ... */
1670 /* Random moves units start with zero movement as their first movement
1671 * will be only after their moves have been reset in the beginning of
1672 * the next turn. */
1673 punit->moves_left = 0;
1674 } else {
1676 }
1677 punit->moved = FALSE;
1678
1682
1685
1686 punit->carrying = NULL;
1687
1691
1694
1695 punit->stay = FALSE;
1696
1699
1700 if (is_server()) {
1702
1704
1706
1708 O_LAST * sizeof(*punit->server.upkeep_paid));
1709
1710 punit->server.ord_map = 0;
1711 punit->server.ord_city = 0;
1712
1713 punit->server.vision = NULL; /* No vision. */
1715 /* Must be an invalid turn number, and an invalid previous turn
1716 * number. */
1717 punit->server.action_turn = -2;
1718 /* punit->server.moving = NULL; set by fc_calloc(). */
1719
1720 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1721
1722 CALL_FUNC_EACH_AI(unit_alloc, punit);
1723 } else {
1728 }
1729
1730 return punit;
1731}
1732
1733/**********************************************************************/
1738{
1740
1741 /* Unload unit if transported. */
1744
1745 /* Check for transported units. Use direct access to the list. */
1746 if (unit_list_size(punit->transporting) != 0) {
1747 /* Unload all units. */
1751 }
1753
1754 if (punit->transporting) {
1756 }
1757
1758 CALL_FUNC_EACH_AI(unit_free, punit);
1759
1760 if (is_server() && punit->server.adv) {
1762 } else {
1765 }
1766 }
1767
1768 if (--punit->refcount <= 0) {
1769 FC_FREE(punit);
1770 }
1771}
1772
1773/**********************************************************************/
1778{
1779 if (punit->has_orders) {
1780 punit->goto_tile = NULL;
1782 punit->orders.list = NULL;
1783 }
1784 punit->orders.length = 0;
1786}
1787
1788/**********************************************************************/
1792{
1794
1795 return unit_list_size(ptrans->transporting);
1796}
1797
1798/**********************************************************************/
1801static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1802 const struct tile *ptile,
1803 bool (*unit_load_test)
1804 (const struct unit *pc,
1805 const struct unit *pt))
1806{
1807 struct unit *best_trans = NULL;
1808 struct {
1811 } cur, best = { FALSE };
1812
1813 unit_list_iterate(ptile->units, ptrans) {
1814 if (!unit_load_test(pcargo, ptrans)) {
1815 continue;
1816 } else if (best_trans == NULL) {
1818 }
1819
1820 /* Gather data from transport stack in a single pass, for use in
1821 * various conditions below. */
1823 cur.outermost_moves_left = ptrans->moves_left;
1824 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1827 cur.has_orders = TRUE;
1828 }
1829 cur.outermost_moves_left = ptranstrans->moves_left;
1830 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1832
1833 /* Criteria for deciding the 'best' transport to load onto.
1834 * The following tests are applied in order; earlier ones have
1835 * lexicographically greater significance than later ones. */
1836
1837 /* Transports which have orders, or are on transports with orders,
1838 * are less preferable to transport stacks without orders (to
1839 * avoid loading on units that are just passing through). */
1840 if (best_trans != ptrans) {
1841 if (!cur.has_orders && best.has_orders) {
1843 } else if (cur.has_orders && !best.has_orders) {
1844 continue;
1845 }
1846 }
1847
1848 /* Else, transports which are idle are preferable (giving players
1849 * some control over loading) -- this does not check transports
1850 * of transports. */
1851 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1852 if (best_trans != ptrans) {
1853 if (cur.is_idle && !best.is_idle) {
1855 } else if (!cur.is_idle && best.is_idle) {
1856 continue;
1857 }
1858 }
1859
1860 /* Else, transports from which the cargo could unload at any time
1861 * are preferable to those where the cargo can only disembark in
1862 * cities/bases. */
1863 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1865 if (best_trans != ptrans) {
1866 if (cur.can_freely_unload && !best.can_freely_unload) {
1868 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1869 continue;
1870 }
1871 }
1872
1873 /* Else, transports which are less deeply nested are preferable. */
1875 if (best_trans != ptrans) {
1876 if (cur.depth < best.depth) {
1878 } else if (cur.depth > best.depth) {
1879 continue;
1880 }
1881 }
1882
1883 /* Else, transport stacks where the outermost transport has more
1884 * moves left are preferable (on the assumption that it's the
1885 * outermost transport that's about to move). */
1886 if (best_trans != ptrans) {
1887 if (cur.outermost_moves_left > best.outermost_moves_left) {
1889 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1890 continue;
1891 }
1892 }
1893
1894 /* All other things being equal, as a tie-breaker, compare the total
1895 * moves left (this turn) and move rate (future turns) for the whole
1896 * stack, to take into account total potential movement for both
1897 * short and long journeys (we don't know which the cargo intends to
1898 * make). Doesn't try to account for whether transports can unload,
1899 * etc. */
1900 if (best_trans != ptrans) {
1901 if (cur.total_moves > best.total_moves) {
1903 } else {
1904 continue;
1905 }
1906 }
1907
1909 best = cur;
1911
1912 return best_trans;
1913}
1914
1915/**********************************************************************/
1924
1925/**********************************************************************/
1931 const struct tile *ptile)
1932{
1934}
1935
1936/**********************************************************************/
1941 const struct unit *punit)
1942{
1944 return FALSE;
1945 }
1946
1949 return FALSE;
1950 }
1952
1953 return TRUE;
1954}
1955
1956/**********************************************************************/
1965 const struct unit *punit,
1966 const struct unit_type *to_unittype)
1967{
1969
1971 return UU_NOT_ENOUGH_ROOM;
1972 }
1973
1974 if (punit->transporter != NULL) {
1978 }
1980 /* The new unit type can't survive on this terrain. */
1981 return UU_NOT_TERRAIN;
1982 }
1983
1984 return UU_OK;
1985}
1986
1987/**********************************************************************/
1997 const struct unit *punit,
1998 bool is_free)
1999{
2000 struct player *pplayer = unit_owner(punit);
2001 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2003 struct city *pcity;
2004 int cost;
2005
2006 if (!to_unittype) {
2007 return UU_NO_UNITTYPE;
2008 }
2009
2011 /* TODO: There may be other activities that the upgraded unit is not
2012 allowed to do, which we could also test.
2013 -
2014 If convert were legal for new unit_type we could allow, but then it
2015 has a 'head start' getting activity time from the old conversion. */
2016 return UU_NOT_ACTIVITY;
2017 }
2018
2019 if (!is_free) {
2021 if (pplayer->economic.gold < cost) {
2022 return UU_NO_MONEY;
2023 }
2024
2025 pcity = tile_city(unit_tile(punit));
2026 if (!pcity) {
2027 return UU_NOT_IN_CITY;
2028 }
2029 if (city_owner(pcity) != pplayer) {
2030 /* TODO: Should upgrades in allied cities be possible? */
2031 return UU_NOT_CITY_OWNER;
2032 }
2033 }
2034
2035 /* TODO: Allow transported units to be reassigned. Check here
2036 * and make changes to upgrade_unit. */
2038}
2039
2040/**********************************************************************/
2043bool unit_can_convert(const struct civ_map *nmap,
2044 const struct unit *punit)
2045{
2046 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2047
2048 if (tgt == NULL) {
2049 return FALSE;
2050 }
2051
2052 return UU_OK == unit_transform_result(nmap, punit, tgt);
2053}
2054
2055/**********************************************************************/
2060 const struct unit *punit,
2061 char *buf, size_t bufsz)
2062{
2063 struct player *pplayer = unit_owner(punit);
2065 int upgrade_cost;
2066 const struct unit_type *from_unittype = unit_type_get(punit);
2067 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2069 char tbuf[MAX_LEN_MSG];
2070
2071 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2072 "Treasury contains %d gold.",
2073 pplayer->economic.gold),
2074 pplayer->economic.gold);
2075
2076 switch (result) {
2077 case UU_OK:
2079 /* This message is targeted toward the GUI callers. */
2080 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2081 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2082 "Upgrade %s to %s for %d gold?\n%s",
2083 upgrade_cost),
2087 break;
2088 case UU_NO_UNITTYPE:
2090 _("Sorry, cannot upgrade %s (yet)."),
2092 break;
2093 case UU_NO_MONEY:
2095 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2096 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2097 "Upgrading %s to %s costs %d gold.\n%s",
2098 upgrade_cost),
2102 break;
2103 case UU_NOT_IN_CITY:
2104 case UU_NOT_CITY_OWNER:
2106 _("You can only upgrade units in your cities."));
2107 break;
2108 case UU_NOT_ENOUGH_ROOM:
2110 _("Upgrading this %s would strand units it transports."),
2112 break;
2113 case UU_NOT_TERRAIN:
2115 _("Upgrading this %s would result in a %s which can not "
2116 "survive at this place."),
2119 break;
2122 _("Upgrading this %s would result in a %s which its "
2123 "current transport, %s, could not transport."),
2127 break;
2128 case UU_NOT_ACTIVITY:
2130 _("Cannot upgrade %s while doing '%s'."),
2133 break;
2134 }
2135
2136 return result;
2137}
2138
2139/**********************************************************************/
2144 const struct unit *punit)
2145{
2146 int mpco;
2147
2149 &(const struct req_context) {
2150 .player = unit_owner(punit),
2151 .city = unit_tile(punit)
2153 : NULL,
2154 .tile = unit_tile(punit),
2155 .unit = punit,
2156 .unittype = unit_type_get(punit),
2157 .action = paction,
2158 },
2159 NULL,
2161
2163
2164 return mpco;
2165}
2166
2167/**********************************************************************/
2170int hp_gain_coord(const struct unit *punit)
2171{
2172 int hp = 0;
2173 const int base = unit_type_get(punit)->hp;
2174 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2175
2176 /* Includes barracks (100%), fortress (25%), etc. */
2178
2179 /* Minimum HP after regen effects applied. */
2180 hp = MAX(hp, min);
2181
2182 /* Regen2 effects that apply after there's at least Min HP */
2183 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2184
2185 return MAX(hp, 0);
2186}
2187
2188/**********************************************************************/
2192{
2193 const struct unit_type *utype = unit_type_get(punit);
2195 struct city *pcity = tile_city(unit_tile(punit));
2196 int gain;
2197
2198 if (!punit->moved) {
2200 } else {
2201 gain = 0;
2202 }
2203
2205
2206 if (!punit->homecity && 0 < game.server.killunhomed
2208 /* Hit point loss of units without homecity; at least 1 hp! */
2209 /* Gameloss units are immune to this effect. */
2210 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2211
2212 if (gain > hp_loss) {
2213 gain = -1;
2214 } else {
2215 gain -= hp_loss;
2216 }
2217 }
2218
2219 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2220 && !unit_transported(punit)) {
2221 gain -= utype->hp * pclass->hp_loss_pct / 100;
2222 }
2223
2224 if (punit->hp + gain > utype->hp) {
2225 gain = utype->hp - punit->hp;
2226 } else if (punit->hp + gain < 0) {
2227 gain = -punit->hp;
2228 }
2229
2230 return gain;
2231}
2232
2233/**********************************************************************/
2236bool is_losing_hp(const struct unit *punit)
2237{
2238 const struct unit_type *punittype = unit_type_get(punit);
2239
2241 < (punittype->hp *
2242 utype_class(punittype)->hp_loss_pct / 100);
2243}
2244
2245/**********************************************************************/
2248bool unit_type_is_losing_hp(const struct player *pplayer,
2249 const struct unit_type *punittype)
2250{
2251 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2253 < (punittype->hp *
2254 utype_class(punittype)->hp_loss_pct / 100);
2255}
2256
2257/**********************************************************************/
2261bool unit_is_alive(int id)
2262{
2263 /* Check if unit exist in game */
2264 if (game_unit_by_number(id)) {
2265 return TRUE;
2266 }
2267
2268 return FALSE;
2269}
2270
2271/**********************************************************************/
2278bool unit_is_virtual(const struct unit *punit)
2279{
2280 if (!punit) {
2281 return FALSE;
2282 }
2283
2284 return punit != game_unit_by_number(punit->id);
2285}
2286
2287/**********************************************************************/
2290void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2291{
2292 return punit->server.ais[ai_type_number(ai)];
2293}
2294
2295/**********************************************************************/
2298void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2299 void *data)
2300{
2301 punit->server.ais[ai_type_number(ai)] = data;
2302}
2303
2304/**********************************************************************/
2313{
2314 int cost, default_hp, dist = 0;
2315 struct tile *ptile = unit_tile(punit);
2316
2318
2320 cost = unit_owner(punit)->economic.gold + game.info.base_bribe_cost;
2321
2322 /* Consider the distance to the capital. */
2325 if (is_capital(capital)) {
2326 int tmp = map_distance(capital->tile, ptile);
2327
2328 if (tmp < dist) {
2329 dist = tmp;
2330 }
2331 }
2333
2334 cost /= dist + 2;
2335
2336 /* Consider the build cost. */
2338
2339 /* Rule set specific cost modification */
2340 cost += (cost
2342 &(const struct req_context) {
2343 .player = unit_owner(punit),
2344 .city = game_city_by_number(
2346 ),
2347 .tile = ptile,
2348 .unit = punit,
2349 .unittype = unit_type_get(punit),
2350 },
2351 briber,
2353 / 100;
2354
2355 /* Veterans are not cheap. */
2356 {
2357 const struct veteran_level *vlevel
2359
2361 cost = cost * vlevel->power_fact / 100;
2362 if (unit_type_get(punit)->move_rate > 0) {
2363 cost += cost * vlevel->move_bonus / unit_type_get(punit)->move_rate;
2364 } else {
2365 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2366 }
2367 }
2368
2369 /* Cost now contains the basic bribe cost. We now reduce it by:
2370 * bribecost = cost/2 + cost/2 * damage/hp
2371 * = cost/2 * (1 + damage/hp) */
2372 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2373}
2374
2375/**********************************************************************/
2378bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2379{
2382
2384
2385 if (force || can_unit_load(pcargo, ptrans)) {
2386 pcargo->transporter = ptrans;
2387 unit_list_append(ptrans->transporting, pcargo);
2388
2389 return TRUE;
2390 }
2391
2392 return FALSE;
2393}
2394
2395/**********************************************************************/
2399{
2400 struct unit *ptrans;
2401
2403
2404 if (!unit_transported(pcargo)) {
2405 /* 'pcargo' is not transported. */
2406 return FALSE;
2407 }
2408
2409 /* Get the transporter; must not be defined on the client! */
2411 if (ptrans) {
2412 /* 'pcargo' and 'ptrans' should be on the same tile. */
2414
2415#ifndef FREECIV_NDEBUG
2416 bool success =
2417#endif
2418 unit_list_remove(ptrans->transporting, pcargo);
2419
2420 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2422 }
2423
2424 /* For the server (also safe for the client). */
2425 pcargo->transporter = NULL;
2426
2427 return TRUE;
2428}
2429
2430/**********************************************************************/
2433bool unit_transported(const struct unit *pcargo)
2434{
2436
2437 /* The unit is transported if a transporter unit is set or, (for the client)
2438 * if the transported_by field is set. */
2439 if (is_server()) {
2441 } else {
2443 }
2444}
2445
2446/**********************************************************************/
2449struct unit *unit_transport_get(const struct unit *pcargo)
2450{
2452
2453 return pcargo->transporter;
2454}
2455
2456/**********************************************************************/
2459struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2460{
2462 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2463
2464 return ptrans->transporting;
2465}
2466
2467/**********************************************************************/
2470static inline bool
2478
2479/**********************************************************************/
2488 const struct unit *ptrans)
2489{
2490 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2491
2492 /* Check 'pcargo' against 'ptrans'. */
2494 return FALSE;
2495 }
2496
2497 /* Check 'pcargo' against 'ptrans' parents. */
2500 return FALSE;
2501 }
2503
2504 /* Check cargo children... */
2507
2508 /* ...against 'ptrans'. */
2510 return FALSE;
2511 }
2512
2513 /* ...and against 'ptrans' parents. */
2516 return FALSE;
2517 }
2520
2521 return TRUE;
2522}
2523
2524/**********************************************************************/
2528bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2529{
2531 if (ptrans == plevel) {
2532 return TRUE;
2533 }
2535 return FALSE;
2536}
2537
2538/**********************************************************************/
2541int unit_cargo_depth(const struct unit *ptrans)
2542{
2543 struct cargo_iter iter;
2544 struct iterator *it;
2545 int depth = 0;
2546
2547 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2548 iterator_next(it)) {
2549 if (iter.depth > depth) {
2550 depth = iter.depth;
2551 }
2552 }
2553 return depth;
2554}
2555
2556/**********************************************************************/
2560{
2561 int level = 0;
2562
2564 level++;
2566 return level;
2567}
2568
2569/**********************************************************************/
2573{
2574 return sizeof(struct cargo_iter);
2575}
2576
2577/**********************************************************************/
2580static void *cargo_iter_get(const struct iterator *it)
2581{
2582 const struct cargo_iter *iter = CARGO_ITER(it);
2583
2584 return unit_list_link_data(iter->links[iter->depth - 1]);
2585}
2586
2587/**********************************************************************/
2590static void cargo_iter_next(struct iterator *it)
2591{
2592 struct cargo_iter *iter = CARGO_ITER(it);
2593 const struct unit_list_link *piter;
2594 const struct unit_list_link *pnext;
2595
2596 /* Variant 1: unit has cargo. */
2598 if (NULL != pnext) {
2599 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2600 iter->links[iter->depth++] = pnext;
2601 return;
2602 }
2603
2604 fc_assert(iter->depth > 0);
2605
2606 while (iter->depth > 0) {
2607 piter = iter->links[iter->depth - 1];
2608
2609 /* Variant 2: there are other cargo units at same level. */
2611 if (NULL != pnext) {
2612 iter->links[iter->depth - 1] = pnext;
2613 return;
2614 }
2615
2616 /* Variant 3: return to previous level, and do same tests. */
2617 iter->depth--;
2618 }
2619}
2620
2621/**********************************************************************/
2624static bool cargo_iter_valid(const struct iterator *it)
2625{
2626 return (0 < CARGO_ITER(it)->depth);
2627}
2628
2629/**********************************************************************/
2633 const struct unit *ptrans)
2634{
2635 struct iterator *it = ITERATOR(iter);
2636
2637 it->get = cargo_iter_get;
2638 it->next = cargo_iter_next;
2639 it->valid = cargo_iter_valid;
2641 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2642
2643 return it;
2644}
2645
2646/**********************************************************************/
2649bool unit_is_cityfounder(const struct unit *punit)
2650{
2652}
2653
2654/**********************************************************************/
2658 int length, const struct unit_order *orders)
2659{
2660 int i;
2661
2662 for (i = 0; i < length; i++) {
2663 struct action *paction;
2664 struct extra_type *pextra;
2665
2666 if (orders[i].order > ORDER_LAST) {
2667 log_error("invalid order %d at index %d", orders[i].order, i);
2668 return FALSE;
2669 }
2670 switch (orders[i].order) {
2671 case ORDER_MOVE:
2672 case ORDER_ACTION_MOVE:
2673 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2674 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2675 return FALSE;
2676 }
2677 break;
2678 case ORDER_ACTIVITY:
2679 switch (orders[i].activity) {
2680 case ACTIVITY_SENTRY:
2681 if (i != length - 1) {
2682 /* Only allowed as the last order. */
2683 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2684 i);
2685 return FALSE;
2686 }
2687 break;
2688 /* Replaced by action orders */
2689 case ACTIVITY_BASE:
2690 case ACTIVITY_GEN_ROAD:
2691 case ACTIVITY_CLEAN:
2692 case ACTIVITY_PILLAGE:
2693 case ACTIVITY_MINE:
2694 case ACTIVITY_IRRIGATE:
2695 case ACTIVITY_PLANT:
2696 case ACTIVITY_CULTIVATE:
2697 case ACTIVITY_TRANSFORM:
2698 case ACTIVITY_CONVERT:
2700 log_error("at index %d, use action rather than activity %d.",
2701 i, orders[i].activity);
2702 return FALSE;
2703 /* Not supported. */
2704 case ACTIVITY_EXPLORE:
2705 case ACTIVITY_IDLE:
2706 /* Not set from the client. */
2707 case ACTIVITY_GOTO:
2708 case ACTIVITY_FORTIFIED:
2709 /* Unused. */
2710 case ACTIVITY_LAST:
2711 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2712 return FALSE;
2713 }
2714
2715 break;
2717 if (!action_id_exists(orders[i].action)) {
2718 /* Non-existent action. */
2719 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2720 return FALSE;
2721 }
2722
2723 paction = action_by_number(orders[i].action);
2724
2725 /* Validate main target. */
2726 if (index_to_tile(nmap, orders[i].target) == NULL) {
2727 log_error("at index %d, invalid tile target %d for the action %d.",
2728 i, orders[i].target, orders[i].action);
2729 return FALSE;
2730 }
2731
2732 if (orders[i].dir != DIR8_ORIGIN) {
2733 log_error("at index %d, the action %d sets the outdated target"
2734 " specification dir.",
2735 i, orders[i].action);
2736 }
2737
2738 /* Validate sub target. */
2739 switch (action_id_get_sub_target_kind(orders[i].action)) {
2740 case ASTK_BUILDING:
2741 /* Sub target is a building. */
2742 if (!improvement_by_number(orders[i].sub_target)) {
2743 /* Sub target is invalid. */
2744 log_error("at index %d, cannot do %s without a target.", i,
2745 action_id_rule_name(orders[i].action));
2746 return FALSE;
2747 }
2748 break;
2749 case ASTK_TECH:
2750 /* Sub target is a technology. */
2751 if (orders[i].sub_target == A_NONE
2752 || (!valid_advance_by_number(orders[i].sub_target)
2753 && orders[i].sub_target != A_FUTURE)) {
2754 /* Target tech is invalid. */
2755 log_error("at index %d, cannot do %s without a target.", i,
2756 action_id_rule_name(orders[i].action));
2757 return FALSE;
2758 }
2759 break;
2760 case ASTK_EXTRA:
2762 /* Sub target is an extra. */
2763 pextra = (!(orders[i].sub_target == NO_TARGET
2764 || (orders[i].sub_target < 0
2765 || (orders[i].sub_target
2767 ? extra_by_number(orders[i].sub_target) : NULL);
2768 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2769 if (pextra == NULL) {
2770 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2771 /* Target extra is invalid. */
2772 log_error("at index %d, cannot do %s without a target.", i,
2773 action_id_rule_name(orders[i].action));
2774 return FALSE;
2775 }
2776 } else {
2777 if (!actres_removes_extra(paction->result, pextra)
2778 && !actres_creates_extra(paction->result, pextra)) {
2779 /* Target extra is irrelevant for the action. */
2780 log_error("at index %d, cannot do %s to %s.", i,
2781 action_id_rule_name(orders[i].action),
2782 extra_rule_name(pextra));
2783 return FALSE;
2784 }
2785 }
2786 break;
2787 case ASTK_NONE:
2788 /* No validation required. */
2789 break;
2790 /* Invalid action? */
2791 case ASTK_COUNT:
2794 FALSE,
2795 "Bad action %d in order number %d.", orders[i].action, i);
2796 }
2797
2798 /* Some action orders are sane only in the last order. */
2799 if (i != length - 1) {
2800 /* If the unit is dead, */
2802 /* or if Freeciv has no idea where the unit will end up after it
2803 * has performed this action, */
2806 /* or if the unit will end up standing still, */
2808 /* than having this action in the middle of a unit's orders is
2809 * probably wrong. */
2810 log_error("action %d is not allowed at index %d.",
2811 orders[i].action, i);
2812 return FALSE;
2813 }
2814 }
2815
2816 /* Don't validate that the target tile really contains a target or
2817 * that the actor player's map think the target tile has one.
2818 * The player may target something from their player map that isn't
2819 * there any more, a target they think is there even if their player
2820 * map doesn't have it, or even a target they assume will be there
2821 * when the unit reaches the target tile.
2822 *
2823 * With that said: The client should probably at least have an
2824 * option to only aim city targeted actions at cities. */
2825
2826 break;
2827 case ORDER_FULL_MP:
2828 break;
2829 case ORDER_LAST:
2830 /* An invalid order. This is handled above. */
2831 break;
2832 }
2833 }
2834
2835 return TRUE;
2836}
2837
2838/**********************************************************************/
2843 int length,
2844 const struct unit_order *orders)
2845{
2846 struct unit_order *unit_orders;
2847
2848 if (!unit_order_list_is_sane(nmap, length, orders)) {
2849 return NULL;
2850 }
2851
2852 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2853 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2854
2855 return unit_orders;
2856}
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5821
struct act_prob action_prob_vs_tile(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:5342
const char * action_id_rule_name(action_id act_id)
Definition actions.c:2000
bool is_action_enabled_unit_on_tile(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:4082
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
bool is_action_enabled_unit_on_unit(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit, const struct unit *target_unit)
Definition actions.c:3918
struct act_prob action_prob_vs_city(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct city *target_city)
Definition actions.c:4984
bool action_maybe_possible_actor_unit(const struct civ_map *nmap, const action_id act_id, const struct unit *actor_unit)
Definition actions.c:6237
bool action_id_exists(const action_id act_id)
Definition actions.c:1829
bool is_action_enabled_unit_on_self(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit)
Definition actions.c:4231
struct act_prob action_prob_vs_unit(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct unit *target_unit)
Definition actions.c:5067
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:481
#define action_id_get_sub_target_kind(act_id)
Definition actions.h:656
static struct action * action_by_number(action_id act_id)
Definition actions.h:635
#define action_has_result(_act_, _res_)
Definition actions.h:431
#define action_by_result_iterate_end
Definition actions.h:485
#define action_id(_act_)
Definition actions.h:661
bool actres_removes_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:803
bool actres_creates_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:782
int ai_type_number(const struct ai_type *ai)
Definition ai.c:278
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:387
void astr_add_line(struct astring *astr, const char *format,...)
Definition astring.c:299
void astr_add(struct astring *astr, const char *format,...)
Definition astring.c:287
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
#define BV_SET(bv, bit)
Definition bitvector.h:81
bool is_capital(const struct city *pcity)
Definition city.c:1579
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2066
bool is_unit_near_a_friendly_city(const struct civ_map *nmap, const struct unit *punit, int distance)
Definition city.c:2081
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
#define city_owner(_pcity_)
Definition city.h:563
#define city_list_iterate_end
Definition city.h:510
char * incite_cost
Definition comments.c:75
#define MAX_LEN_MSG
Definition conn_types.h:37
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
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct player *other_player, enum effect_type effect_type)
Definition effects.c:748
int get_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Definition effects.c:1070
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
struct extra_type * next_extra_for_tile(const struct tile *ptile, enum extra_cause cause, const struct player *pplayer, const struct unit *punit)
Definition extras.c:765
struct player * extra_owner(const struct tile *ptile)
Definition extras.c:1114
struct extra_type_list * extra_type_list_of_zoccers(void)
Definition extras.c:267
static struct extra_type_list * zoccers
Definition extras.c:39
struct extra_type * extra_by_number(int id)
Definition extras.c:183
bool can_extras_coexist(const struct extra_type *pextra1, const struct extra_type *pextra2)
Definition extras.c:1003
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:203
const char * extra_name_translation(const struct extra_type *pextra)
Definition extras.c:194
#define extra_type_list_iterate(extralist, pextra)
Definition extras.h:165
#define extra_index(_e_)
Definition extras.h:183
#define extra_type_list_iterate_end
Definition extras.h:167
static bool is_server(void)
#define NO_TARGET
Definition fc_types.h:354
enum unit_activity Activity_type_id
Definition fc_types.h:379
#define DIR8_ORIGIN
Definition fc_types.h:457
int action_id
Definition fc_types.h:389
@ HB_ALLIANCE
Definition fc_types.h:1292
@ HB_DISABLED
Definition fc_types.h:1290
@ HB_NATIONAL
Definition fc_types.h:1291
@ O_SHIELD
Definition fc_types.h:101
@ O_FOOD
Definition fc_types.h:101
@ O_GOLD
Definition fc_types.h:101
@ O_LAST
Definition fc_types.h:101
@ BORDERS_DISABLED
Definition fc_types.h:1037
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:92
#define Q_(String)
Definition fcintl.h:70
#define PL_(String1, String2, n)
Definition fcintl.h:71
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:62
struct unit * game_unit_by_number(int id)
Definition game.c:116
struct city * game_city_by_number(int id)
Definition game.c:107
#define GAME_UNIT_BRIBE_DIST_MAX
Definition game.h:759
#define GAME_TRANSPORT_MAX_RECURSIVE
Definition game.h:762
static const int bufsz
Definition helpdlg.c:70
struct impr_type * improvement_by_number(const Impr_type_id id)
static bool iterator_valid(const struct iterator *it)
Definition iterator.h:61
#define ITERATOR(p)
Definition iterator.h:37
static void iterator_next(struct iterator *it)
Definition iterator.h:42
#define fc_assert_msg(condition, message,...)
Definition log.h:181
#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_error(message,...)
Definition log.h:103
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition log.h:208
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:456
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:940
bool map_untrusted_dir_is_valid(enum direction8 dir)
Definition map.c:1273
enum direction8 rand_direction(void)
Definition map.c:1929
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:654
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:391
#define square_iterate_end
Definition map.h:394
#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
const char * move_points_text(int mp, bool reduce)
Definition movement.c:1047
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Definition movement.c:279
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:363
bool could_exist_in_city(const struct civ_map *nmap, const struct player *pov_player, const struct unit_type *utype, const struct city *pcity)
Definition movement.c:310
bool can_unit_transport(const struct unit *transporter, const struct unit *transported)
Definition movement.c:887
int unit_move_rate(const struct unit *punit)
Definition movement.c:90
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:523
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:497
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
struct city_list * cities
Definition packhand.c:119
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Definition player.c:1104
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
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1459
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MIN(x, y)
Definition shared.h:55
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
Definition ai.h:50
struct iterator vtable
Definition unit.c:50
int depth
Definition unit.c:52
const struct unit_list_link * links[GAME_TRANSPORT_MAX_RECURSIVE]
Definition unit.c:51
Definition city.h:320
int id
Definition city.h:326
bool occupied
Definition city.h:460
struct tile * tile
Definition city.h:322
struct city::@17::@20 client
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
int killunhomed
Definition game.h:158
struct civ_game::@31::@35 server
bool ruledit_disabled
Definition extras.h:91
bool(* valid)(const struct iterator *it)
Definition iterator.h:34
void *(* get)(const struct iterator *it)
Definition iterator.h:33
void(* next)(struct iterator *it)
Definition iterator.h:32
enum borders_mode borders
enum happyborders_type happyborders
enum airlifting_style airlifting_style
bool airlift_from_always_enabled
bool airlift_to_always_enabled
struct player_economic economic
Definition player.h:282
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
int sub_target
Definition unit.h:98
int transport_capacity
Definition unittype.h:523
struct unit_type::@87 adv
bool worker
Definition unittype.h:575
int move_rate
Definition unittype.h:517
enum vision_layer vlayer
Definition unittype.h:569
const struct unit_type * converted_to
Definition unittype.h:530
Definition unit.h:138
time_t action_timestamp
Definition unit.h:245
int length
Definition unit.h:195
int upkeep[O_LAST]
Definition unit.h:148
bool has_orders
Definition unit.h:193
struct unit::@80 orders
enum action_decision action_decision_want
Definition unit.h:202
int battlegroup
Definition unit.h:191
enum unit_activity activity
Definition unit.h:157
int moves_left
Definition unit.h:150
int refcount
Definition unit.h:141
struct unit::@81::@83 client
int id
Definition unit.h:145
struct unit_list * transporting
Definition unit.h:184
int ord_city
Definition unit.h:242
bool moved
Definition unit.h:173
int ord_map
Definition unit.h:241
bool debug
Definition unit.h:234
struct vision * vision
Definition unit.h:244
int hp
Definition unit.h:151
int fuel
Definition unit.h:153
struct extra_type * changed_from_target
Definition unit.h:170
int current_form_turn
Definition unit.h:208
bool stay
Definition unit.h:205
bool colored
Definition unit.h:222
enum direction8 facing
Definition unit.h:142
struct unit::@81::@84 server
struct tile * tile
Definition unit.h:140
struct extra_type * activity_target
Definition unit.h:164
struct act_prob * act_prob_cache
Definition unit.h:228
int activity_count
Definition unit.h:162
struct unit_order * list
Definition unit.h:198
enum unit_activity changed_from
Definition unit.h:168
struct unit_adv * adv
Definition unit.h:236
struct player * nationality
Definition unit.h:144
int transported_by
Definition unit.h:216
void(* removal_callback)(struct unit *punit)
Definition unit.h:253
void * ais[FREECIV_AI_MOD_LAST]
Definition unit.h:237
int action_turn
Definition unit.h:246
int homecity
Definition unit.h:146
int upkeep_paid[O_LAST]
Definition unit.h:256
bool paradropped
Definition unit.h:174
bool done_moving
Definition unit.h:181
struct unit * transporter
Definition unit.h:183
int birth_turn
Definition unit.h:207
struct goods_type * carrying
Definition unit.h:186
struct tile * goto_tile
Definition unit.h:155
struct tile * action_decision_tile
Definition unit.h:203
const struct unit_type * utype
Definition unit.h:139
int veteran
Definition unit.h:152
int changed_from_count
Definition unit.h:169
struct player * owner
Definition unit.h:143
bool dying
Definition unit.h:250
enum unit_focus_status focus_status
Definition unit.h:214
enum server_side_agent ssa_controller
Definition unit.h:172
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
struct advance * valid_advance_by_number(const Tech_type_id id)
Definition tech.c:176
#define A_FUTURE
Definition tech.h:46
#define A_NONE
Definition tech.h:43
#define T_UNKNOWN
Definition terrain.h:57
#define terrain_has_flag(terr, flag)
Definition terrain.h:283
bool tile_is_placing(const struct tile *ptile)
Definition tile.c:1129
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Definition tile.c:418
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:324
int tile_has_not_aggressive_extra_for_unit(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:196
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:392
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
@ TILE_KNOWN_UNSEEN
Definition tile.h:37
#define ACTIVITY_FACTOR
Definition tile.h:169
#define tile_terrain(_tile)
Definition tile.h:114
#define tile_has_extra(ptile, pextra)
Definition tile.h:151
#define tile_owner(_tile)
Definition tile.h:96
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
bool unit_can_do_action_sub_result(const struct unit *punit, enum action_sub_result sub_result)
Definition unit.c:387
size_t cargo_iter_sizeof(void)
Definition unit.c:2572
bool can_unit_do_activity_targeted_at(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, struct extra_type *target, const struct tile *ptile)
Definition unit.c:928
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1612
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2842
enum unit_upgrade_result unit_transform_result(const struct civ_map *nmap, const struct unit *punit, const struct unit_type *to_unittype)
Definition unit.c:1964
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1512
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2248
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1597
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1314
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:222
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1791
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2590
bool can_unit_change_homecity_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pcity)
Definition unit.c:473
void free_unit_orders(struct unit *punit)
Definition unit.c:1777
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2298
bool unit_is_alive(int id)
Definition unit.c:2261
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:292
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:332
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:434
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:534
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2143
int get_turns_for_activity_at(const struct unit *punit, enum unit_activity activity, const struct tile *ptile, struct extra_type *tgt)
Definition unit.c:550
bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1464
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2378
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1930
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2632
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2059
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2559
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1096
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2236
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1622
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:733
bool unit_can_add_or_build_city(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:456
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1347
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2170
bool can_unit_change_homecity(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:489
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2580
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1253
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1526
int get_activity_rate(const struct unit *punit)
Definition unit.c:503
bool can_unit_deboard_or_be_unloaded(const struct civ_map *nmap, const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:785
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2487
bool is_attack_unit(const struct unit *punit)
Definition unit.c:313
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:194
const Activity_type_id tile_changing_activities[]
Definition unit.c:44
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2449
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2398
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1567
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2528
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2191
enum unit_airlift_result test_unit_can_airlift_to(const struct civ_map *nmap, const struct player *restriction, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:85
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:849
int unit_bribe_cost(struct unit *punit, struct player *briber)
Definition unit.c:2312
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1239
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:708
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2649
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1131
bool can_unit_do_autosettlers(const struct unit *punit)
Definition unit.c:614
bool is_field_unit(const struct unit *punit)
Definition unit.c:422
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2624
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
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:324
void set_unit_activity_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type *new_target)
Definition unit.c:1113
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1632
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2541
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Definition unit.c:2471
#define CARGO_ITER(iter)
Definition unit.c:54
bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:814
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2657
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:622
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1146
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:305
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1395
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:340
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1583
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:830
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1163
bool unit_can_help_build_wonder_here(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:266
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:876
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1331
bool is_special_unit(const struct unit *punit)
Definition unit.c:358
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1737
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1299
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1263
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:1996
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2433
bool are_unit_orders_equal(const struct unit_order *order1, const struct unit_order *order2)
Definition unit.c:59
bool unit_is_virtual(const struct unit *punit)
Definition unit.c:2278
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1081
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:396
bool is_guard_unit(const struct unit *punit)
Definition unit.c:348
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:760
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1368
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2459
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1940
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2290
bool unit_has_orders(const struct unit *punit)
Definition unit.c:210
struct unit * tile_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1276
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1920
static struct unit * base_transporter_for_unit(const struct unit *pcargo, const struct tile *ptile, bool(*unit_load_test)(const struct unit *pc, const struct unit *pt))
Definition unit.c:1801
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2043
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:574
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:676
bool can_unit_do_activity_targeted(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, struct extra_type *target)
Definition unit.c:915
#define unit_tile(_pu)
Definition unit.h:397
#define unit_transports_iterate_end
Definition unit.h:563
#define unit_cargo_iterate_end
Definition unit.h:573
@ FOCUS_AVAIL
Definition unit.h:53
#define BATTLEGROUP_NONE
Definition unit.h:190
unit_orders
Definition unit.h:37
@ ORDER_ACTION_MOVE
Definition unit.h:45
@ ORDER_ACTIVITY
Definition unit.h:41
@ ORDER_FULL_MP
Definition unit.h:43
@ ORDER_MOVE
Definition unit.h:39
@ ORDER_LAST
Definition unit.h:49
@ ORDER_PERFORM_ACTION
Definition unit.h:47
#define unit_transported_server(_pcargo_)
Definition unit.h:541
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:570
#define unit_owner(_pu)
Definition unit.h:396
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:559
unit_upgrade_result
Definition unit.h:60
@ UU_NO_MONEY
Definition unit.h:63
@ UU_NOT_IN_CITY
Definition unit.h:64
@ UU_NO_UNITTYPE
Definition unit.h:62
@ UU_NOT_TERRAIN
Definition unit.h:67
@ UU_UNSUITABLE_TRANSPORT
Definition unit.h:68
@ UU_NOT_CITY_OWNER
Definition unit.h:65
@ UU_NOT_ENOUGH_ROOM
Definition unit.h:66
@ UU_OK
Definition unit.h:61
@ UU_NOT_ACTIVITY
Definition unit.h:69
#define unit_transported_client(_pcargo_)
Definition unit.h:544
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.h:432
unit_airlift_result
Definition unit.h:72
@ AR_SRC_NO_FLIGHTS
Definition unit.h:84
@ AR_OK_SRC_UNKNOWN
Definition unit.h:75
@ AR_OK_DST_UNKNOWN
Definition unit.h:76
@ AR_NO_MOVES
Definition unit.h:78
@ AR_BAD_DST_CITY
Definition unit.h:83
@ AR_NOT_IN_CITY
Definition unit.h:81
@ AR_OCCUPIED
Definition unit.h:80
@ AR_OK
Definition unit.h:74
@ AR_DST_NO_FLIGHTS
Definition unit.h:85
@ AR_WRONG_UNITTYPE
Definition unit.h:79
@ AR_BAD_SRC_CITY
Definition unit.h:82
#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
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
int utype_pays_mp_for_action_base(const struct action *paction, const struct unit_type *putype)
Definition unittype.c:1381
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:300
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1569
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1249
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2965
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1587
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1468
bool utype_may_act_move_frags(const struct unit_type *punit_type, const action_id act_id, const int move_fragments)
Definition unittype.c:1058
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1484
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1703
bool utype_can_freely_load(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:288
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2625
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
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Definition unittype.c:2597
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:190
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1219
bool utype_can_do_action_sub_result(const struct unit_type *putype, enum action_sub_result sub_result)
Definition unittype.c:408
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1288
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:443
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1560
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:371
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1731
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:766
#define utype_class(_t_)
Definition unittype.h:749
#define utype_fuel(ptype)
Definition unittype.h:839
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:617