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/**********************************************************************/
82 const struct player *restriction,
83 const struct unit *punit,
84 const struct city *pdest_city)
85{
86 const struct city *psrc_city = tile_city(unit_tile(punit));
87 const struct player *punit_owner;
89
90 if (0 == punit->moves_left
92 ACTION_AIRLIFT, 0)) {
93 /* No moves left. */
94 return AR_NO_MOVES;
95 }
96
98 return AR_WRONG_UNITTYPE;
99 }
100
102 /* Units with occupants can't be airlifted currently. */
103 return AR_OCCUPIED;
104 }
105
106 if (NULL == psrc_city) {
107 /* No city there. */
108 return AR_NOT_IN_CITY;
109 }
110
111 if (psrc_city == pdest_city) {
112 /* Airlifting to our current position doesn't make sense. */
113 return AR_BAD_DST_CITY;
114 }
115
116 if (pdest_city
117 && (NULL == restriction
119 == TILE_KNOWN_SEEN))
121 /* Can't exist at the destination tile. */
122 return AR_BAD_DST_CITY;
123 }
124
126
127 /* Check validity of both source and destination before checking capacity,
128 * to avoid misleadingly optimistic returns. */
129
133 /* Not allowed to airlift from this source. */
134 return AR_BAD_SRC_CITY;
135 }
136
137 if (pdest_city
141 /* Not allowed to airlift to this destination. */
142 return AR_BAD_DST_CITY;
143 }
144
146 /* We know for sure whether or not src can airlift this turn. */
147 if (0 >= psrc_city->airlift
150 /* The source cannot airlift for this turn (maybe already airlifted
151 * or no airport).
152 * See also do_airline() in server/unittools.h. */
153 return AR_SRC_NO_FLIGHTS;
154 } /* else, there is capacity; continue to other checks */
155 } else {
156 /* We don't have access to the 'airlift' field. Assume it's OK; can
157 * only find out for sure by trying it. */
159 }
160
161 if (pdest_city) {
163 if (0 >= pdest_city->airlift
166 /* The destination cannot support airlifted units for this turn
167 * (maybe already airlifted or no airport).
168 * See also do_airline() in server/unittools.h. */
169 return AR_DST_NO_FLIGHTS;
170 } /* else continue */
171 } else {
173 }
174 }
175
176 return ok_result;
177}
178
179/**********************************************************************/
187 const struct unit *punit,
188 const struct city *pdest_city)
189{
190 if (is_server()) {
193 } else {
195 pdest_city));
196 }
197}
198
199/**********************************************************************/
202bool unit_has_orders(const struct unit *punit)
203{
204 return punit->has_orders;
205}
206
207/**********************************************************************/
214int unit_shield_value(const struct unit *punit,
215 const struct unit_type *punittype,
216 const struct action *paction)
217{
218 int value;
219
220 bool has_unit;
221 const struct player *act_player;
222
223 has_unit = punit != NULL;
224
225 if (has_unit && punittype == NULL) {
227 }
228
232
234 /* TODO: determine if tile and city should be where the unit currently is
235 * located or the target city. Those two may differ. Wait for ruleset
236 * author feed back. */
237
239 value += ((value
241 &(const struct req_context) {
242 .player = act_player,
243 .unit = punit,
244 .unittype = punittype,
245 .action = paction,
246 },
247 NULL,
249 / 100);
250
251 return value;
252}
253
254/**********************************************************************/
259 const struct unit *punit)
260{
261 struct city *pcity = tile_city(unit_tile(punit));
262
263 if (pcity == NULL) {
264 /* No city to help at this tile. */
265 return FALSE;
266 }
267
269 /* This unit can never do help wonder. */
270 return FALSE;
271 }
272
273 /* Evaluate all action enablers for extra accuracy. */
274 /* TODO: Is it worth it? */
277 pcity));
278}
279
280/**********************************************************************/
293
294/**********************************************************************/
298{
300}
301
302/**********************************************************************/
312
313/**********************************************************************/
316bool is_martial_law_unit(const struct unit *punit)
317{
319}
320
321/**********************************************************************/
324bool is_occupying_unit(const struct unit *punit)
325{
327}
328
329/**********************************************************************/
333{
335}
336
337/**********************************************************************/
340bool is_guard_unit(const struct unit *punit)
341{
343}
344
345/**********************************************************************/
350bool is_special_unit(const struct unit *punit)
351{
353}
354
355/**********************************************************************/
359bool unit_can_do_action(const struct unit *punit,
360 const action_id act_id)
361{
362 return utype_can_do_action(unit_type_get(punit), act_id);
363}
364
365/**********************************************************************/
370 enum action_result result)
371{
373}
374
375/**********************************************************************/
384
385/**********************************************************************/
389 const struct player *pplayer,
390 const struct tile *ptile, bool omniscient)
391{
392 square_iterate(nmap, ptile, 2, ptile1) {
394 if ((omniscient
395 || can_player_see_unit(pplayer, punit))
396 && pplayers_at_war(pplayer, unit_owner(punit))
401 unit_class_get(punit), ptile)))) {
402 return TRUE;
403 }
406
407 return FALSE;
408}
409
410/**********************************************************************/
414bool is_field_unit(const struct unit *punit)
415{
417}
418
419/**********************************************************************/
426bool is_hiding_unit(const struct unit *punit)
427{
429
430 if (vl == V_INVIS || vl == V_SUBSURFACE) {
431 return TRUE;
432 }
433
434 if (unit_transported(punit)) {
436 if (vl == V_INVIS || vl == V_SUBSURFACE) {
437 return TRUE;
438 }
439 }
440
441 return FALSE;
442}
443
444/**********************************************************************/
449 const struct unit *punit)
450{
451 struct city *tgt_city;
452
453 if ((tgt_city = tile_city(unit_tile(punit)))) {
456 } else {
459 }
460}
461
462/**********************************************************************/
466 const struct unit *punit,
467 const struct city *pcity)
468{
469 if (pcity == NULL) {
470 /* Can't change home city to a non existing city. */
471 return FALSE;
472 }
473
475 pcity));
476}
477
478/**********************************************************************/
482 const struct unit *punit)
483{
485}
486
487/**********************************************************************/
495int get_activity_rate(const struct unit *punit)
496{
497 const struct veteran_level *vlevel;
498 const struct unit_type *ptype;
499 int move_rate;
500
502
506
507 /* The speed of the settler depends on its base move_rate, not on
508 * the number of moves actually remaining or the adjusted move rate.
509 * This means sea formers won't have their activity rate increased by
510 * Magellan's, and it means injured units work just as fast as
511 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
513
514 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
515 return ACTIVITY_FACTOR
516 * (float)vlevel->power_fact / 100
518}
519
520/**********************************************************************/
527{
528 /* This logic is also coded in client/goto.c. */
529 if (punit->moves_left > 0) {
530 return get_activity_rate(punit);
531 } else {
532 return 0;
533 }
534}
535
536/**********************************************************************/
543 enum unit_activity activity,
544 const struct tile *ptile,
545 struct extra_type *tgt)
546{
547 /* FIXME: This is just an approximation since we don't account for
548 * get_activity_rate_this_turn. */
550 int points_needed;
551
552 fc_assert(tgt != NULL || !is_targeted_activity(activity));
553
554 points_needed = tile_activity_time(activity, ptile, tgt);
555
556 if (points_needed >= 0 && speed > 0) {
557 return (points_needed - 1) / speed + 1; /* Round up */
558 } else {
559 return FC_INFINITY;
560 }
561}
562
563/**********************************************************************/
567{
568 switch (activity) {
569 case ACTIVITY_PILLAGE:
570 case ACTIVITY_BASE:
573 case ACTIVITY_MINE:
574 case ACTIVITY_CLEAN:
575 return TRUE;
576 case ACTIVITY_IDLE:
578 case ACTIVITY_SENTRY:
579 case ACTIVITY_GOTO:
580 case ACTIVITY_EXPLORE:
583 case ACTIVITY_PLANT:
585 case ACTIVITY_CONVERT:
586 return FALSE;
587 /* These shouldn't be kicking around internally. */
588 case ACTIVITY_LAST:
589 break;
590 }
591
593
594 return FALSE;
595}
596
597/**********************************************************************/
607{
608 return unit_type_get(punit)->adv.worker;
609}
610
611/**********************************************************************/
614const char *get_activity_text(enum unit_activity activity)
615{
616 /* The switch statement has just the activities listed with no "default"
617 * handling. This enables the compiler to detect missing entries
618 * automatically, and still handles everything correctly. */
619 switch (activity) {
620 case ACTIVITY_IDLE:
621 return _("Idle");
622 case ACTIVITY_CLEAN:
623 return _("Clean");
624 case ACTIVITY_MINE:
625 /* TRANS: Activity name, verb in English */
626 return Q_("?act:Mine");
627 case ACTIVITY_PLANT:
628 /* TRANS: Activity name, verb in English */
629 return _("Plant");
631 return _("Irrigate");
633 return _("Cultivate");
635 return _("Fortifying");
637 return _("Fortified");
638 case ACTIVITY_SENTRY:
639 return _("Sentry");
640 case ACTIVITY_PILLAGE:
641 return _("Pillage");
642 case ACTIVITY_GOTO:
643 return _("Goto");
644 case ACTIVITY_EXPLORE:
645 return _("Explore");
647 return _("Transform");
648 case ACTIVITY_BASE:
649 return _("Base");
651 return _("Road");
652 case ACTIVITY_CONVERT:
653 return _("Convert");
654 case ACTIVITY_LAST:
655 break;
656 }
657
659 return _("Unknown");
660}
661
662/**********************************************************************/
669 const struct unit *ptrans)
670{
671 /* Make sure this transporter can carry this type of unit. */
673 return FALSE;
674 }
675
676 /* Make sure there's room in the transporter. */
679 return FALSE;
680 }
681
682 /* Check iff this is a valid transport. */
684 return FALSE;
685 }
686
687 /* Check transport depth. */
690 return FALSE;
691 }
692
693 return TRUE;
694}
695
696/**********************************************************************/
700bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
701{
702 if (!pcargo || !ptrans || pcargo == ptrans) {
703 return FALSE;
704 }
705
706 /* Double-check ownership of the units: you can load into an allied unit
707 * (of course only allied units can be on the same tile). */
709 return FALSE;
710 }
711
712 /* Un-embarkable transport must be in city or base to load cargo. */
716 return FALSE;
717 }
718
720}
721
722/**********************************************************************/
725bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
726{
727 /* This function needs to check EVERYTHING. */
728
729 /* Check positions of the units. Of course you can't load a unit onto
730 * a transporter on a different tile... */
732 return FALSE;
733 }
734
735 /* Cannot load if cargo is already loaded onto something else. */
737 return FALSE;
738 }
739
741}
742
743/**********************************************************************/
752bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
753{
754 if (!pcargo || !ptrans) {
755 return FALSE;
756 }
757
758 /* Make sure the unit's transporter exists and is known. */
760 return FALSE;
761 }
762
763 /* Un-disembarkable transport must be in city or base to unload cargo. */
767 return FALSE;
768 }
769
770 return TRUE;
771}
772
773/**********************************************************************/
800
801/**********************************************************************/
806bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
807{
810 return TRUE;
811 }
813
814 return FALSE;
815}
816
817/**********************************************************************/
837
838/**********************************************************************/
842 struct unit *punit)
843{
844 enum unit_activity current = punit->activity;
845 struct extra_type *target = punit->activity_target;
846 enum unit_activity current2 =
847 (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
848 bool result;
849
852
854
855 punit->activity = current;
856 punit->activity_target = target;
857
858 return result;
859}
860
861/**********************************************************************/
869 const struct unit *punit,
870 enum unit_activity activity)
871{
872 struct extra_type *target = NULL;
873
874 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
875 * being able to find out whether an activity is in general possible.
876 * Find one for them, but when they come to do the activity, they will
877 * have to determine the target themselves */
878 {
879 struct tile *ptile = unit_tile(punit);
880
881 if (activity == ACTIVITY_IRRIGATE) {
882 target = next_extra_for_tile(ptile,
885 punit);
886 if (NULL == target) {
887 return FALSE; /* No more irrigation extras available. */
888 }
889 } else if (activity == ACTIVITY_MINE) {
890 target = next_extra_for_tile(ptile,
891 EC_MINE,
893 punit);
894 if (NULL == target) {
895 return FALSE; /* No more mine extras available. */
896 }
897 }
898 }
899
900 return can_unit_do_activity_targeted(nmap, punit, activity, target);
901}
902
903/**********************************************************************/
908 const struct unit *punit,
909 enum unit_activity activity,
910 struct extra_type *target)
911{
912 return can_unit_do_activity_targeted_at(nmap, punit, activity, target,
914}
915
916/**********************************************************************/
921 const struct unit *punit,
922 enum unit_activity activity,
923 struct extra_type *target,
924 const struct tile *ptile)
925{
926 /* Check that no build activity conflicting with one already in progress
927 * gets executed. */
928 /* FIXME: Should check also the cases where one of the activities is terrain
929 * change that destroys the target of the other activity */
930 if (target != NULL && is_build_activity(activity)) {
931 if (tile_is_placing(ptile)) {
932 return FALSE;
933 }
934
935 unit_list_iterate(ptile->units, tunit) {
936 if (is_build_activity(tunit->activity)
937 && !can_extras_coexist(target, tunit->activity_target)) {
938 return FALSE;
939 }
941 }
942
943#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
944{ \
945 switch (action_get_target_kind(paction)) { \
946 case ATK_TILE: \
947 return is_action_enabled_unit_on_tile(nmap, paction->id, \
948 punit, ptile, target); \
949 case ATK_EXTRAS: \
950 return is_action_enabled_unit_on_extras(nmap, paction->id, \
951 punit, ptile, target); \
952 case ATK_CITY: \
953 case ATK_UNIT: \
954 case ATK_UNITS: \
955 case ATK_SELF: \
956 return FALSE; \
957 case ATK_COUNT: \
958 break; /* Handle outside switch */ \
959 } \
960 fc_assert(action_target_kind_is_valid( \
961 action_get_target_kind(paction))); \
962 return FALSE; \
963 }
964
965 switch (activity) {
966 case ACTIVITY_IDLE:
967 case ACTIVITY_GOTO:
968 return TRUE;
969
970 case ACTIVITY_CLEAN:
971 /* The call below doesn't support actor tile speculation. */
972 fc_assert_msg(unit_tile(punit) == ptile,
973 "Please use action_speculate_unit_on_tile()");
975 punit, ptile, target);
976
977 case ACTIVITY_MINE:
978 /* The call below doesn't support actor tile speculation. */
979 fc_assert_msg(unit_tile(punit) == ptile,
980 "Please use action_speculate_unit_on_tile()");
982 ptile, target);
983
984 case ACTIVITY_PLANT:
985 /* The call below doesn't support actor tile speculation. */
986 fc_assert_msg(unit_tile(punit) == ptile,
987 "Please use action_speculate_unit_on_tile()");
989 punit, ptile, NULL);
990
992 /* The call below doesn't support actor tile speculation. */
993 fc_assert_msg(unit_tile(punit) == ptile,
994 "Please use action_speculate_unit_on_tile()");
996 ptile, target);
997
999 /* The call below doesn't support actor tile speculation. */
1000 fc_assert_msg(unit_tile(punit) == ptile,
1001 "Please use action_speculate_unit_on_tile()");
1003 punit, ptile, NULL);
1004
1006 /* The call below doesn't support actor tile speculation. */
1007 fc_assert_msg(unit_tile(punit) == ptile,
1008 "Please use action_speculate_unit_on_self()");
1010 punit);
1011
1012 case ACTIVITY_FORTIFIED:
1013 return FALSE;
1014
1015 case ACTIVITY_BASE:
1016 /* The call below doesn't support actor tile speculation. */
1017 fc_assert_msg(unit_tile(punit) == ptile,
1018 "Please use action_speculate_unit_on_tile()");
1020 punit, ptile, target);
1021
1022 case ACTIVITY_GEN_ROAD:
1023 /* The call below doesn't support actor tile speculation. */
1024 fc_assert_msg(unit_tile(punit) == ptile,
1025 "Please use action_speculate_unit_on_tile()");
1027 punit, ptile, target);
1028
1029 case ACTIVITY_SENTRY:
1031 && !unit_transported(punit)) {
1032 /* Don't let units sentry on tiles they will die on. */
1033 return FALSE;
1034 }
1035 return TRUE;
1036
1037 case ACTIVITY_PILLAGE:
1038 /* The call below doesn't support actor tile speculation. */
1039 fc_assert_msg(unit_tile(punit) == ptile,
1040 "Please use action_speculate_unit_on_tile()");
1042
1043 case ACTIVITY_EXPLORE:
1044 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1045
1046 case ACTIVITY_TRANSFORM:
1047 /* The call below doesn't support actor tile speculation. */
1048 fc_assert_msg(unit_tile(punit) == ptile,
1049 "Please use action_speculate_unit_on_tile()");
1051 punit, ptile, NULL);
1052
1053 case ACTIVITY_CONVERT:
1054 /* The call below doesn't support actor tile speculation. */
1055 fc_assert_msg(unit_tile(punit) == ptile,
1056 "Please use action_speculate_unit_on_self()");
1058
1059 case ACTIVITY_LAST:
1060 break;
1061 }
1062
1063 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1064 activity);
1065 return FALSE;
1066
1067#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1068}
1069
1070/**********************************************************************/
1075{
1077 punit->activity_count = 0;
1079 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1080 /* No longer done. */
1082 }
1083}
1084
1085/**********************************************************************/
1101
1102/**********************************************************************/
1119
1120/**********************************************************************/
1124 const struct tile *ptile)
1125{
1126 unit_list_iterate(ptile->units, punit) {
1127 if (punit->activity == activity) {
1128 return TRUE;
1129 }
1131 return FALSE;
1132}
1133
1134/**********************************************************************/
1139{
1141
1143 unit_list_iterate(ptile->units, punit) {
1146 }
1148
1149 return tgt_ret;
1150}
1151
1152/**********************************************************************/
1155void unit_activity_astr(const struct unit *punit, struct astring *astr)
1156{
1157 if (!punit || !astr) {
1158 return;
1159 }
1160
1161 switch (punit->activity) {
1162 case ACTIVITY_IDLE:
1164 int rate, f;
1165
1167 f = ((punit->fuel) - 1);
1168
1169 /* Add in two parts as move_points_text() returns ptr to static
1170 * End result: "Moves: (fuel)moves_left" */
1171 astr_add_line(astr, "%s: (%s)", _("Moves"),
1173 astr_add(astr, "%s",
1175 } else {
1176 astr_add_line(astr, "%s: %s", _("Moves"),
1178 }
1179 return;
1180 case ACTIVITY_CLEAN:
1181 case ACTIVITY_TRANSFORM:
1183 case ACTIVITY_FORTIFIED:
1184 case ACTIVITY_SENTRY:
1185 case ACTIVITY_GOTO:
1186 case ACTIVITY_EXPLORE:
1187 case ACTIVITY_CONVERT:
1188 case ACTIVITY_CULTIVATE:
1189 case ACTIVITY_PLANT:
1191 return;
1192 case ACTIVITY_MINE:
1193 case ACTIVITY_IRRIGATE:
1194 if (punit->activity_target == NULL) {
1196 } else {
1197 astr_add_line(astr, "Building %s",
1199 }
1200 return;
1201 case ACTIVITY_PILLAGE:
1202 if (punit->activity_target != NULL) {
1205 } else {
1207 }
1208 return;
1209 case ACTIVITY_BASE:
1212 return;
1213 case ACTIVITY_GEN_ROAD:
1216 return;
1217 case ACTIVITY_LAST:
1218 break;
1219 }
1220
1221 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1223}
1224
1225/**********************************************************************/
1231void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1232{
1233 if (!punit || !astr) {
1234 return;
1235 }
1236
1237 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1239 punit->upkeep[O_GOLD]);
1240}
1241
1242/**********************************************************************/
1245struct player *unit_nationality(const struct unit *punit)
1246{
1248 return punit->nationality;
1249}
1250
1251/**********************************************************************/
1255void unit_tile_set(struct unit *punit, struct tile *ptile)
1256{
1258 punit->tile = ptile;
1259}
1260
1261/**********************************************************************/
1268struct unit *tile_allied_unit(const struct tile *ptile,
1269 const struct player *pplayer)
1270{
1271 struct unit *punit = NULL;
1272
1273 unit_list_iterate(ptile->units, cunit) {
1274 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1275 punit = cunit;
1276 } else {
1277 return NULL;
1278 }
1279 }
1281
1282 return punit;
1283}
1284
1285/**********************************************************************/
1291struct unit *tile_enemy_unit(const struct tile *ptile,
1292 const struct player *pplayer)
1293{
1294 unit_list_iterate(ptile->units, punit) {
1295 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1296 return punit;
1297 }
1299
1300 return NULL;
1301}
1302
1303/**********************************************************************/
1306struct unit *tile_non_allied_unit(const struct tile *ptile,
1307 const struct player *pplayer)
1308{
1309 unit_list_iterate(ptile->units, punit) {
1310 if (!pplayers_allied(unit_owner(punit), pplayer)) {
1311 return punit;
1312 }
1313 }
1315
1316 return NULL;
1317}
1318
1319/**********************************************************************/
1323struct unit *tile_other_players_unit(const struct tile *ptile,
1324 const struct player *pplayer)
1325{
1326 unit_list_iterate(ptile->units, punit) {
1327 if (unit_owner(punit) != pplayer) {
1328 return punit;
1329 }
1331
1332 return NULL;
1333}
1334
1335/**********************************************************************/
1339struct unit *tile_non_attack_unit(const struct tile *ptile,
1340 const struct player *pplayer)
1341{
1342 unit_list_iterate(ptile->units, punit) {
1343 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1344 return punit;
1345 }
1346 }
1348
1349 return NULL;
1350}
1351
1352/**********************************************************************/
1360struct unit *unit_occupies_tile(const struct tile *ptile,
1361 const struct player *pplayer)
1362{
1363 unit_list_iterate(ptile->units, punit) {
1364 if (!is_occupying_unit(punit)) {
1365 continue;
1366 }
1367
1369 continue;
1370 }
1371
1372 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1373 return punit;
1374 }
1376
1377 return NULL;
1378}
1379
1380/**********************************************************************/
1387bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1388 const struct civ_map *zmap)
1389{
1391
1392 square_iterate(zmap, ptile0, 1, ptile) {
1393 struct terrain *pterrain;
1394 struct city *pcity;
1395
1396 pterrain = tile_terrain(ptile);
1397 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1398 continue;
1399 }
1400
1401 pcity = tile_non_allied_city(ptile, pplayer);
1402 if (pcity != NULL) {
1403 if (unit_list_size(ptile->units) > 0) {
1404 /* Occupied enemy city, it doesn't matter if units inside have
1405 * UTYF_NOZOC or not. */
1406 return FALSE;
1407 }
1408 } else {
1409 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1411 if (tile_has_extra(ptile, pextra)) {
1412 return FALSE;
1413 }
1415 }
1416
1417 unit_list_iterate(ptile->units, punit) {
1418 if (!pplayers_allied(unit_owner(punit), pplayer)
1421 bool hidden = FALSE;
1422
1423 /* We do NOT check the possibility that player is allied with an extra owner,
1424 * and should thus see inside the extra.
1425 * This is to avoid the situation where having an alliance with third player
1426 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1427 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1428 if (tile_has_extra(ptile, pextra)) {
1429 hidden = TRUE;
1430 break;
1431 }
1433
1434 if (!hidden) {
1435 return FALSE;
1436 }
1437 }
1439 }
1441
1442 return TRUE;
1443}
1444
1445/**********************************************************************/
1456bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1457 const struct civ_map *zmap)
1458{
1460
1461 square_iterate(zmap, ptile0, 1, ptile) {
1462 struct terrain *pterrain;
1463 struct city *pcity;
1464
1465 pterrain = tile_terrain(ptile);
1466 if (T_UNKNOWN == pterrain
1467 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1468 continue;
1469 }
1470
1471 pcity = tile_non_allied_city(ptile, pplayer);
1472 if (pcity != NULL) {
1473 if (pcity->client.occupied
1474 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1475 /* Occupied enemy city, it doesn't matter if units inside have
1476 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1477 return FALSE;
1478 }
1479 } else {
1480 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1482 if (tile_has_extra(ptile, pextra)) {
1483 return FALSE;
1484 }
1486 }
1487
1488 unit_list_iterate(ptile->units, punit) {
1490 && !pplayers_allied(unit_owner(punit), pplayer)
1492 return FALSE;
1493 }
1495 }
1497
1498 return TRUE;
1499}
1500
1501/**********************************************************************/
1509
1510/**********************************************************************/
1519 const struct unit *punit)
1520{
1521 const struct tile *ptile = unit_tile(punit);
1523
1524 if (!is_attack_unit(punit)) {
1525 return FALSE;
1526 }
1527 if (tile_city(ptile)) {
1528 return FALSE;
1529 }
1531 switch (game.info.happyborders) {
1532 case HB_DISABLED:
1533 break;
1534 case HB_NATIONAL:
1535 if (tile_owner(ptile) == unit_owner(punit)) {
1536 return FALSE;
1537 }
1538 break;
1539 case HB_ALLIANCE:
1541 return FALSE;
1542 }
1543 break;
1544 }
1545 }
1546
1549 if (max_friendliness_range >= 0) {
1551 }
1552
1553 return TRUE;
1554}
1555
1556/**********************************************************************/
1560{
1561 switch (activity) {
1562 case ACTIVITY_MINE:
1563 case ACTIVITY_IRRIGATE:
1564 case ACTIVITY_BASE:
1565 case ACTIVITY_GEN_ROAD:
1566 return TRUE;
1567 default:
1568 return FALSE;
1569 }
1570}
1571
1572/**********************************************************************/
1576{
1577 switch (activity) {
1578 case ACTIVITY_PILLAGE:
1579 case ACTIVITY_CLEAN:
1580 return TRUE;
1581 default:
1582 return FALSE;
1583 }
1584}
1585
1586/**********************************************************************/
1590{
1591 switch (activity) {
1592 case ACTIVITY_CULTIVATE:
1593 case ACTIVITY_PLANT:
1594 case ACTIVITY_TRANSFORM:
1595 return TRUE;
1596 default:
1597 return FALSE;
1598 }
1599}
1600
1601/**********************************************************************/
1605{
1606 return is_build_activity(activity)
1607 || is_clean_activity(activity)
1608 || is_terrain_change_activity(activity);
1609}
1610
1611/**********************************************************************/
1615{
1616 return is_build_activity(activity)
1617 || is_clean_activity(activity);
1618}
1619
1620/**********************************************************************/
1624struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1625 const struct unit_type *punittype,
1626 int veteran_level)
1627{
1628 /* Make sure that contents of unit structure are correctly initialized,
1629 * if you ever allocate it by some other mean than fc_calloc() */
1630 struct unit *punit = fc_calloc(1, sizeof(*punit));
1631 int max_vet_lvl;
1632
1633 /* It does not register the unit so the id is set to 0. */
1635
1636 fc_assert_ret_val(NULL != punittype, NULL); /* No untyped units! */
1638
1639 fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned units! */
1640 punit->owner = pplayer;
1641 punit->nationality = pplayer;
1642
1643 punit->refcount = 1;
1645
1646 if (pcity) {
1647 unit_tile_set(punit, pcity->tile);
1648 punit->homecity = pcity->id;
1649 } else {
1652 }
1653
1654 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1655 punit->goto_tile = NULL;
1658 /* A unit new and fresh ... */
1662 /* Random moves units start with zero movement as their first movement
1663 * will be only after their moves have been reset in the beginning of
1664 * the next turn. */
1665 punit->moves_left = 0;
1666 } else {
1668 }
1669 punit->moved = FALSE;
1670
1674
1677
1678 punit->carrying = NULL;
1679
1683
1686
1687 punit->stay = FALSE;
1688
1691
1692 if (is_server()) {
1694
1696
1698
1700 O_LAST * sizeof(*punit->server.upkeep_paid));
1701
1702 punit->server.ord_map = 0;
1703 punit->server.ord_city = 0;
1704
1705 punit->server.vision = NULL; /* No vision. */
1707 /* Must be an invalid turn number, and an invalid previous turn
1708 * number. */
1709 punit->server.action_turn = -2;
1710 /* punit->server.moving = NULL; set by fc_calloc(). */
1711
1712 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1713
1714 CALL_FUNC_EACH_AI(unit_alloc, punit);
1715 } else {
1720 }
1721
1722 return punit;
1723}
1724
1725/**********************************************************************/
1730{
1732
1733 /* Unload unit if transported. */
1736
1737 /* Check for transported units. Use direct access to the list. */
1738 if (unit_list_size(punit->transporting) != 0) {
1739 /* Unload all units. */
1743 }
1745
1746 if (punit->transporting) {
1748 }
1749
1750 CALL_FUNC_EACH_AI(unit_free, punit);
1751
1752 if (is_server() && punit->server.adv) {
1754 } else {
1757 }
1758 }
1759
1760 if (--punit->refcount <= 0) {
1761 FC_FREE(punit);
1762 }
1763}
1764
1765/**********************************************************************/
1770{
1771 if (punit->has_orders) {
1772 punit->goto_tile = NULL;
1774 punit->orders.list = NULL;
1775 }
1776 punit->orders.length = 0;
1778}
1779
1780/**********************************************************************/
1784{
1786
1787 return unit_list_size(ptrans->transporting);
1788}
1789
1790/**********************************************************************/
1793static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1794 const struct tile *ptile,
1795 bool (*unit_load_test)
1796 (const struct unit *pc,
1797 const struct unit *pt))
1798{
1799 struct unit *best_trans = NULL;
1800 struct {
1803 } cur, best = { FALSE };
1804
1805 unit_list_iterate(ptile->units, ptrans) {
1806 if (!unit_load_test(pcargo, ptrans)) {
1807 continue;
1808 } else if (best_trans == NULL) {
1810 }
1811
1812 /* Gather data from transport stack in a single pass, for use in
1813 * various conditions below. */
1815 cur.outermost_moves_left = ptrans->moves_left;
1816 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1819 cur.has_orders = TRUE;
1820 }
1821 cur.outermost_moves_left = ptranstrans->moves_left;
1822 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1824
1825 /* Criteria for deciding the 'best' transport to load onto.
1826 * The following tests are applied in order; earlier ones have
1827 * lexicographically greater significance than later ones. */
1828
1829 /* Transports which have orders, or are on transports with orders,
1830 * are less preferable to transport stacks without orders (to
1831 * avoid loading on units that are just passing through). */
1832 if (best_trans != ptrans) {
1833 if (!cur.has_orders && best.has_orders) {
1835 } else if (cur.has_orders && !best.has_orders) {
1836 continue;
1837 }
1838 }
1839
1840 /* Else, transports which are idle are preferable (giving players
1841 * some control over loading) -- this does not check transports
1842 * of transports. */
1843 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1844 if (best_trans != ptrans) {
1845 if (cur.is_idle && !best.is_idle) {
1847 } else if (!cur.is_idle && best.is_idle) {
1848 continue;
1849 }
1850 }
1851
1852 /* Else, transports from which the cargo could unload at any time
1853 * are preferable to those where the cargo can only disembark in
1854 * cities/bases. */
1855 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1857 if (best_trans != ptrans) {
1858 if (cur.can_freely_unload && !best.can_freely_unload) {
1860 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1861 continue;
1862 }
1863 }
1864
1865 /* Else, transports which are less deeply nested are preferable. */
1867 if (best_trans != ptrans) {
1868 if (cur.depth < best.depth) {
1870 } else if (cur.depth > best.depth) {
1871 continue;
1872 }
1873 }
1874
1875 /* Else, transport stacks where the outermost transport has more
1876 * moves left are preferable (on the assumption that it's the
1877 * outermost transport that's about to move). */
1878 if (best_trans != ptrans) {
1879 if (cur.outermost_moves_left > best.outermost_moves_left) {
1881 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1882 continue;
1883 }
1884 }
1885
1886 /* All other things being equal, as a tie-breaker, compare the total
1887 * moves left (this turn) and move rate (future turns) for the whole
1888 * stack, to take into account total potential movement for both
1889 * short and long journeys (we don't know which the cargo intends to
1890 * make). Doesn't try to account for whether transports can unload,
1891 * etc. */
1892 if (best_trans != ptrans) {
1893 if (cur.total_moves > best.total_moves) {
1895 } else {
1896 continue;
1897 }
1898 }
1899
1901 best = cur;
1903
1904 return best_trans;
1905}
1906
1907/**********************************************************************/
1916
1917/**********************************************************************/
1923 const struct tile *ptile)
1924{
1926}
1927
1928/**********************************************************************/
1933 const struct unit *punit)
1934{
1936 return FALSE;
1937 }
1938
1941 return FALSE;
1942 }
1944
1945 return TRUE;
1946}
1947
1948/**********************************************************************/
1957 const struct unit *punit,
1958 const struct unit_type *to_unittype)
1959{
1961
1963 return UU_NOT_ENOUGH_ROOM;
1964 }
1965
1966 if (punit->transporter != NULL) {
1970 }
1972 /* The new unit type can't survive on this terrain. */
1973 return UU_NOT_TERRAIN;
1974 }
1975
1976 return UU_OK;
1977}
1978
1979/**********************************************************************/
1989 const struct unit *punit,
1990 bool is_free)
1991{
1992 struct player *pplayer = unit_owner(punit);
1993 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
1995 struct city *pcity;
1996 int cost;
1997
1998 if (!to_unittype) {
1999 return UU_NO_UNITTYPE;
2000 }
2001
2003 /* TODO: There may be other activities that the upgraded unit is not
2004 allowed to do, which we could also test.
2005 -
2006 If convert were legal for new unit_type we could allow, but then it
2007 has a 'head start' getting activity time from the old conversion. */
2008 return UU_NOT_ACTIVITY;
2009 }
2010
2011 if (!is_free) {
2013 if (pplayer->economic.gold < cost) {
2014 return UU_NO_MONEY;
2015 }
2016
2017 pcity = tile_city(unit_tile(punit));
2018 if (!pcity) {
2019 return UU_NOT_IN_CITY;
2020 }
2021 if (city_owner(pcity) != pplayer) {
2022 /* TODO: Should upgrades in allied cities be possible? */
2023 return UU_NOT_CITY_OWNER;
2024 }
2025 }
2026
2027 /* TODO: Allow transported units to be reassigned. Check here
2028 * and make changes to upgrade_unit. */
2030}
2031
2032/**********************************************************************/
2035bool unit_can_convert(const struct civ_map *nmap,
2036 const struct unit *punit)
2037{
2038 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2039
2040 if (tgt == NULL) {
2041 return FALSE;
2042 }
2043
2044 return UU_OK == unit_transform_result(nmap, punit, tgt);
2045}
2046
2047/**********************************************************************/
2052 const struct unit *punit,
2053 char *buf, size_t bufsz)
2054{
2055 struct player *pplayer = unit_owner(punit);
2057 int upgrade_cost;
2058 const struct unit_type *from_unittype = unit_type_get(punit);
2059 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2061 char tbuf[MAX_LEN_MSG];
2062
2063 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2064 "Treasury contains %d gold.",
2065 pplayer->economic.gold),
2066 pplayer->economic.gold);
2067
2068 switch (result) {
2069 case UU_OK:
2071 /* This message is targeted toward the GUI callers. */
2072 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2073 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2074 "Upgrade %s to %s for %d gold?\n%s",
2075 upgrade_cost),
2079 break;
2080 case UU_NO_UNITTYPE:
2082 _("Sorry, cannot upgrade %s (yet)."),
2084 break;
2085 case UU_NO_MONEY:
2087 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2088 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2089 "Upgrading %s to %s costs %d gold.\n%s",
2090 upgrade_cost),
2094 break;
2095 case UU_NOT_IN_CITY:
2096 case UU_NOT_CITY_OWNER:
2098 _("You can only upgrade units in your cities."));
2099 break;
2100 case UU_NOT_ENOUGH_ROOM:
2102 _("Upgrading this %s would strand units it transports."),
2104 break;
2105 case UU_NOT_TERRAIN:
2107 _("Upgrading this %s would result in a %s which can not "
2108 "survive at this place."),
2111 break;
2114 _("Upgrading this %s would result in a %s which its "
2115 "current transport, %s, could not transport."),
2119 break;
2120 case UU_NOT_ACTIVITY:
2122 _("Cannot upgrade %s while doing '%s'."),
2125 break;
2126 }
2127
2128 return result;
2129}
2130
2131/**********************************************************************/
2136 const struct unit *punit)
2137{
2138 int mpco;
2139
2141 &(const struct req_context) {
2142 .player = unit_owner(punit),
2143 .city = unit_tile(punit)
2145 : NULL,
2146 .tile = unit_tile(punit),
2147 .unit = punit,
2148 .unittype = unit_type_get(punit),
2149 .action = paction,
2150 },
2151 NULL,
2153
2155
2156 return mpco;
2157}
2158
2159/**********************************************************************/
2162int hp_gain_coord(const struct unit *punit)
2163{
2164 int hp = 0;
2165 const int base = unit_type_get(punit)->hp;
2166 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2167
2168 /* Includes barracks (100%), fortress (25%), etc. */
2170
2171 /* Minimum HP after regen effects applied. */
2172 hp = MAX(hp, min);
2173
2174 /* Regen2 effects that apply after there's at least Min HP */
2175 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2176
2177 return MAX(hp, 0);
2178}
2179
2180/**********************************************************************/
2184{
2185 const struct unit_type *utype = unit_type_get(punit);
2187 struct city *pcity = tile_city(unit_tile(punit));
2188 int gain;
2189
2190 if (!punit->moved) {
2192 } else {
2193 gain = 0;
2194 }
2195
2197
2198 if (!punit->homecity && 0 < game.server.killunhomed
2200 /* Hit point loss of units without homecity; at least 1 hp! */
2201 /* Gameloss units are immune to this effect. */
2202 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2203
2204 if (gain > hp_loss) {
2205 gain = -1;
2206 } else {
2207 gain -= hp_loss;
2208 }
2209 }
2210
2211 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2212 && !unit_transported(punit)) {
2213 gain -= utype->hp * pclass->hp_loss_pct / 100;
2214 }
2215
2216 if (punit->hp + gain > utype->hp) {
2217 gain = utype->hp - punit->hp;
2218 } else if (punit->hp + gain < 0) {
2219 gain = -punit->hp;
2220 }
2221
2222 return gain;
2223}
2224
2225/**********************************************************************/
2228bool is_losing_hp(const struct unit *punit)
2229{
2230 const struct unit_type *punittype = unit_type_get(punit);
2231
2233 < (punittype->hp *
2234 utype_class(punittype)->hp_loss_pct / 100);
2235}
2236
2237/**********************************************************************/
2240bool unit_type_is_losing_hp(const struct player *pplayer,
2241 const struct unit_type *punittype)
2242{
2243 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2245 < (punittype->hp *
2246 utype_class(punittype)->hp_loss_pct / 100);
2247}
2248
2249/**********************************************************************/
2253bool unit_is_alive(int id)
2254{
2255 /* Check if unit exist in game */
2256 if (game_unit_by_number(id)) {
2257 return TRUE;
2258 }
2259
2260 return FALSE;
2261}
2262
2263/**********************************************************************/
2270bool unit_is_virtual(const struct unit *punit)
2271{
2272 if (!punit) {
2273 return FALSE;
2274 }
2275
2276 return punit != game_unit_by_number(punit->id);
2277}
2278
2279/**********************************************************************/
2282void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2283{
2284 return punit->server.ais[ai_type_number(ai)];
2285}
2286
2287/**********************************************************************/
2290void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2291 void *data)
2292{
2293 punit->server.ais[ai_type_number(ai)] = data;
2294}
2295
2296/**********************************************************************/
2305{
2306 int cost, default_hp, dist = 0;
2307 struct tile *ptile = unit_tile(punit);
2308
2310
2312 cost = unit_owner(punit)->economic.gold + game.info.base_bribe_cost;
2313
2314 /* Consider the distance to the capital. */
2317 if (is_capital(capital)) {
2318 int tmp = map_distance(capital->tile, ptile);
2319
2320 if (tmp < dist) {
2321 dist = tmp;
2322 }
2323 }
2325
2326 cost /= dist + 2;
2327
2328 /* Consider the build cost. */
2330
2331 /* Rule set specific cost modification */
2332 cost += (cost
2334 &(const struct req_context) {
2335 .player = unit_owner(punit),
2336 .city = game_city_by_number(
2338 ),
2339 .tile = ptile,
2340 .unit = punit,
2341 .unittype = unit_type_get(punit),
2342 },
2343 briber,
2345 / 100;
2346
2347 /* Veterans are not cheap. */
2348 {
2349 const struct veteran_level *vlevel
2351
2353 cost = cost * vlevel->power_fact / 100;
2354 if (unit_type_get(punit)->move_rate > 0) {
2355 cost += cost * vlevel->move_bonus / unit_type_get(punit)->move_rate;
2356 } else {
2357 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2358 }
2359 }
2360
2361 /* Cost now contains the basic bribe cost. We now reduce it by:
2362 * bribecost = cost/2 + cost/2 * damage/hp
2363 * = cost/2 * (1 + damage/hp) */
2364 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2365}
2366
2367/**********************************************************************/
2370bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2371{
2374
2376
2377 if (force || can_unit_load(pcargo, ptrans)) {
2378 pcargo->transporter = ptrans;
2379 unit_list_append(ptrans->transporting, pcargo);
2380
2381 return TRUE;
2382 }
2383
2384 return FALSE;
2385}
2386
2387/**********************************************************************/
2391{
2392 struct unit *ptrans;
2393
2395
2396 if (!unit_transported(pcargo)) {
2397 /* 'pcargo' is not transported. */
2398 return FALSE;
2399 }
2400
2401 /* Get the transporter; must not be defined on the client! */
2403 if (ptrans) {
2404 /* 'pcargo' and 'ptrans' should be on the same tile. */
2406
2407#ifndef FREECIV_NDEBUG
2408 bool success =
2409#endif
2410 unit_list_remove(ptrans->transporting, pcargo);
2411
2412 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2414 }
2415
2416 /* For the server (also safe for the client). */
2417 pcargo->transporter = NULL;
2418
2419 return TRUE;
2420}
2421
2422/**********************************************************************/
2425bool unit_transported(const struct unit *pcargo)
2426{
2428
2429 /* The unit is transported if a transporter unit is set or, (for the client)
2430 * if the transported_by field is set. */
2431 if (is_server()) {
2433 } else {
2435 }
2436}
2437
2438/**********************************************************************/
2441struct unit *unit_transport_get(const struct unit *pcargo)
2442{
2444
2445 return pcargo->transporter;
2446}
2447
2448/**********************************************************************/
2451struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2452{
2454 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2455
2456 return ptrans->transporting;
2457}
2458
2459/**********************************************************************/
2462static inline bool
2470
2471/**********************************************************************/
2480 const struct unit *ptrans)
2481{
2482 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2483
2484 /* Check 'pcargo' against 'ptrans'. */
2486 return FALSE;
2487 }
2488
2489 /* Check 'pcargo' against 'ptrans' parents. */
2492 return FALSE;
2493 }
2495
2496 /* Check cargo children... */
2499
2500 /* ...against 'ptrans'. */
2502 return FALSE;
2503 }
2504
2505 /* ...and against 'ptrans' parents. */
2508 return FALSE;
2509 }
2512
2513 return TRUE;
2514}
2515
2516/**********************************************************************/
2520bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2521{
2523 if (ptrans == plevel) {
2524 return TRUE;
2525 }
2527 return FALSE;
2528}
2529
2530/**********************************************************************/
2533int unit_cargo_depth(const struct unit *ptrans)
2534{
2535 struct cargo_iter iter;
2536 struct iterator *it;
2537 int depth = 0;
2538
2539 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2540 iterator_next(it)) {
2541 if (iter.depth > depth) {
2542 depth = iter.depth;
2543 }
2544 }
2545 return depth;
2546}
2547
2548/**********************************************************************/
2552{
2553 int level = 0;
2554
2556 level++;
2558 return level;
2559}
2560
2561/**********************************************************************/
2565{
2566 return sizeof(struct cargo_iter);
2567}
2568
2569/**********************************************************************/
2572static void *cargo_iter_get(const struct iterator *it)
2573{
2574 const struct cargo_iter *iter = CARGO_ITER(it);
2575
2576 return unit_list_link_data(iter->links[iter->depth - 1]);
2577}
2578
2579/**********************************************************************/
2582static void cargo_iter_next(struct iterator *it)
2583{
2584 struct cargo_iter *iter = CARGO_ITER(it);
2585 const struct unit_list_link *piter;
2586 const struct unit_list_link *pnext;
2587
2588 /* Variant 1: unit has cargo. */
2590 if (NULL != pnext) {
2591 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2592 iter->links[iter->depth++] = pnext;
2593 return;
2594 }
2595
2596 fc_assert(iter->depth > 0);
2597
2598 while (iter->depth > 0) {
2599 piter = iter->links[iter->depth - 1];
2600
2601 /* Variant 2: there are other cargo units at same level. */
2603 if (NULL != pnext) {
2604 iter->links[iter->depth - 1] = pnext;
2605 return;
2606 }
2607
2608 /* Variant 3: return to previous level, and do same tests. */
2609 iter->depth--;
2610 }
2611}
2612
2613/**********************************************************************/
2616static bool cargo_iter_valid(const struct iterator *it)
2617{
2618 return (0 < CARGO_ITER(it)->depth);
2619}
2620
2621/**********************************************************************/
2625 const struct unit *ptrans)
2626{
2627 struct iterator *it = ITERATOR(iter);
2628
2629 it->get = cargo_iter_get;
2630 it->next = cargo_iter_next;
2631 it->valid = cargo_iter_valid;
2633 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2634
2635 return it;
2636}
2637
2638/**********************************************************************/
2641bool unit_is_cityfounder(const struct unit *punit)
2642{
2644}
2645
2646/**********************************************************************/
2650 int length, const struct unit_order *orders)
2651{
2652 int i;
2653
2654 for (i = 0; i < length; i++) {
2655 struct action *paction;
2656 struct extra_type *pextra;
2657
2658 if (orders[i].order > ORDER_LAST) {
2659 log_error("invalid order %d at index %d", orders[i].order, i);
2660 return FALSE;
2661 }
2662 switch (orders[i].order) {
2663 case ORDER_MOVE:
2664 case ORDER_ACTION_MOVE:
2665 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2666 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2667 return FALSE;
2668 }
2669 break;
2670 case ORDER_ACTIVITY:
2671 switch (orders[i].activity) {
2672 case ACTIVITY_SENTRY:
2673 if (i != length - 1) {
2674 /* Only allowed as the last order. */
2675 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2676 i);
2677 return FALSE;
2678 }
2679 break;
2680 /* Replaced by action orders */
2681 case ACTIVITY_BASE:
2682 case ACTIVITY_GEN_ROAD:
2683 case ACTIVITY_CLEAN:
2684 case ACTIVITY_PILLAGE:
2685 case ACTIVITY_MINE:
2686 case ACTIVITY_IRRIGATE:
2687 case ACTIVITY_PLANT:
2688 case ACTIVITY_CULTIVATE:
2689 case ACTIVITY_TRANSFORM:
2690 case ACTIVITY_CONVERT:
2692 log_error("at index %d, use action rather than activity %d.",
2693 i, orders[i].activity);
2694 return FALSE;
2695 /* Not supported. */
2696 case ACTIVITY_EXPLORE:
2697 case ACTIVITY_IDLE:
2698 /* Not set from the client. */
2699 case ACTIVITY_GOTO:
2700 case ACTIVITY_FORTIFIED:
2701 /* Unused. */
2702 case ACTIVITY_LAST:
2703 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2704 return FALSE;
2705 }
2706
2707 break;
2709 if (!action_id_exists(orders[i].action)) {
2710 /* Non-existent action. */
2711 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2712 return FALSE;
2713 }
2714
2715 paction = action_by_number(orders[i].action);
2716
2717 /* Validate main target. */
2718 if (index_to_tile(nmap, orders[i].target) == NULL) {
2719 log_error("at index %d, invalid tile target %d for the action %d.",
2720 i, orders[i].target, orders[i].action);
2721 return FALSE;
2722 }
2723
2724 if (orders[i].dir != DIR8_ORIGIN) {
2725 log_error("at index %d, the action %d sets the outdated target"
2726 " specification dir.",
2727 i, orders[i].action);
2728 }
2729
2730 /* Validate sub target. */
2731 switch (action_id_get_sub_target_kind(orders[i].action)) {
2732 case ASTK_BUILDING:
2733 /* Sub target is a building. */
2734 if (!improvement_by_number(orders[i].sub_target)) {
2735 /* Sub target is invalid. */
2736 log_error("at index %d, cannot do %s without a target.", i,
2737 action_id_rule_name(orders[i].action));
2738 return FALSE;
2739 }
2740 break;
2741 case ASTK_TECH:
2742 /* Sub target is a technology. */
2743 if (orders[i].sub_target == A_NONE
2744 || (!valid_advance_by_number(orders[i].sub_target)
2745 && orders[i].sub_target != A_FUTURE)) {
2746 /* Target tech is invalid. */
2747 log_error("at index %d, cannot do %s without a target.", i,
2748 action_id_rule_name(orders[i].action));
2749 return FALSE;
2750 }
2751 break;
2752 case ASTK_EXTRA:
2754 /* Sub target is an extra. */
2755 pextra = (!(orders[i].sub_target == NO_TARGET
2756 || (orders[i].sub_target < 0
2757 || (orders[i].sub_target
2759 ? extra_by_number(orders[i].sub_target) : NULL);
2760 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2761 if (pextra == NULL) {
2762 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2763 /* Target extra is invalid. */
2764 log_error("at index %d, cannot do %s without a target.", i,
2765 action_id_rule_name(orders[i].action));
2766 return FALSE;
2767 }
2768 } else {
2769 if (!actres_removes_extra(paction->result, pextra)
2770 && !actres_creates_extra(paction->result, pextra)) {
2771 /* Target extra is irrelevant for the action. */
2772 log_error("at index %d, cannot do %s to %s.", i,
2773 action_id_rule_name(orders[i].action),
2774 extra_rule_name(pextra));
2775 return FALSE;
2776 }
2777 }
2778 break;
2779 case ASTK_NONE:
2780 /* No validation required. */
2781 break;
2782 /* Invalid action? */
2783 case ASTK_COUNT:
2786 FALSE,
2787 "Bad action %d in order number %d.", orders[i].action, i);
2788 }
2789
2790 /* Some action orders are sane only in the last order. */
2791 if (i != length - 1) {
2792 /* If the unit is dead, */
2794 /* or if Freeciv has no idea where the unit will end up after it
2795 * has performed this action, */
2798 /* or if the unit will end up standing still, */
2800 /* than having this action in the middle of a unit's orders is
2801 * probably wrong. */
2802 log_error("action %d is not allowed at index %d.",
2803 orders[i].action, i);
2804 return FALSE;
2805 }
2806 }
2807
2808 /* Don't validate that the target tile really contains a target or
2809 * that the actor player's map think the target tile has one.
2810 * The player may target something from their player map that isn't
2811 * there any more, a target they think is there even if their player
2812 * map doesn't have it, or even a target they assume will be there
2813 * when the unit reaches the target tile.
2814 *
2815 * With that said: The client should probably at least have an
2816 * option to only aim city targeted actions at cities. */
2817
2818 break;
2819 case ORDER_FULL_MP:
2820 break;
2821 case ORDER_LAST:
2822 /* An invalid order. This is handled above. */
2823 break;
2824 }
2825 }
2826
2827 return TRUE;
2828}
2829
2830/**********************************************************************/
2835 int length,
2836 const struct unit_order *orders)
2837{
2838 struct unit_order *unit_orders;
2839
2840 if (!unit_order_list_is_sane(nmap, length, orders)) {
2841 return NULL;
2842 }
2843
2844 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2845 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2846
2847 return unit_orders;
2848}
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:2931
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:74
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:1015
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Definition movement.c:290
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:319
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:331
bool can_unit_transport(const struct unit *transporter, const struct unit *transported)
Definition movement.c:855
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:491
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:465
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:868
bool can_attack_non_native(const struct unit_type *utype)
Definition movement.c:214
#define SINGLE_MOVE
Definition movement.h:26
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
@ TILE_KNOWN_SEEN
Definition tile.h:38
#define ACTIVITY_FACTOR
Definition tile.h:165
#define tile_terrain(_tile)
Definition tile.h:110
#define tile_has_extra(ptile, pextra)
Definition tile.h:147
#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:379
size_t cargo_iter_sizeof(void)
Definition unit.c:2564
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:920
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1604
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2834
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:1956
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1504
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2240
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1589
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1306
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:214
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1783
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2582
bool can_unit_change_homecity_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pcity)
Definition unit.c:465
void free_unit_orders(struct unit *punit)
Definition unit.c:1769
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2290
bool unit_is_alive(int id)
Definition unit.c:2253
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:284
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:324
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:426
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:526
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2135
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:542
bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1456
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2370
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1922
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2624
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2051
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2551
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1088
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2228
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1614
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:725
bool unit_can_add_or_build_city(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:448
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1339
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2162
bool can_unit_change_homecity(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:481
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2572
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1245
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1518
int get_activity_rate(const struct unit *punit)
Definition unit.c:495
bool can_unit_deboard_or_be_unloaded(const struct civ_map *nmap, const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:777
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2479
bool is_attack_unit(const struct unit *punit)
Definition unit.c:305
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:186
const Activity_type_id tile_changing_activities[]
Definition unit.c:44
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2441
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2390
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1559
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2520
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2183
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:81
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:841
int unit_bribe_cost(struct unit *punit, struct player *briber)
Definition unit.c:2304
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1231
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:700
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2641
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1123
bool can_unit_do_autosettlers(const struct unit *punit)
Definition unit.c:606
bool is_field_unit(const struct unit *punit)
Definition unit.c:414
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2616
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:359
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:369
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:316
void set_unit_activity_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type *new_target)
Definition unit.c:1105
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1624
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2533
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Definition unit.c:2463
#define CARGO_ITER(iter)
Definition unit.c:54
bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:806
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2649
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:614
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1138
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:297
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1387
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:332
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1575
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:822
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1155
bool unit_can_help_build_wonder_here(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:258
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:868
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1323
bool is_special_unit(const struct unit *punit)
Definition unit.c:350
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1729
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1291
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1255
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:1988
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2425
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:2270
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1073
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:388
bool is_guard_unit(const struct unit *punit)
Definition unit.c:340
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:752
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1360
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2451
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1932
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2282
bool unit_has_orders(const struct unit *punit)
Definition unit.c:202
struct unit * tile_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1268
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1912
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:1793
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2035
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:566
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:668
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:907
#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
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