Freeciv-3.3
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 = nullptr;
93 const struct unit_type *putype = unit_type_get(punit);
96
97 if (0 == punit->moves_left
99 /* No moves left. */
100 return AR_NO_MOVES;
101 }
102
104 return AR_WRONG_UNITTYPE;
105 }
106
108 /* Units with occupants can't be airlifted currently. */
109 return AR_OCCUPIED;
110 }
111
112 if (nullptr == psrc_city) {
113 /* No city there. */
114 return AR_NOT_IN_CITY;
115 }
116
117 if (psrc_city == pdest_city) {
118 /* Airlifting to our current position doesn't make sense. */
119 return AR_BAD_DST_CITY;
120 }
121
122 if (nullptr != pdest_city) {
124
125 if (nullptr != restriction
128 /* Can't exist at the destination tile. */
129 return AR_BAD_DST_CITY;
130 }
131 }
132
134
135 /* Check validity of both source and destination before checking capacity,
136 * to avoid misleadingly optimistic returns. */
137
141 /* Not allowed to airlift from this source. */
142 return AR_BAD_SRC_CITY;
143 }
144
145 /* Check diplomatic possibility of the destination */
146 if (nullptr != pdest_city) {
150 || flagless
152 /* Not allowed to airlift to this destination. */
153 return AR_BAD_DST_CITY;
154 }
155 } else if (flagless
157 /* Foreign units block airlifting to this destination */
158 return AR_BAD_DST_CITY;
159 }
160 }
161
162 /* Check airlift capacities */
164 if (nullptr == restriction || city_owner(psrc_city) == restriction) {
165 /* We know for sure whether or not src can airlift this turn. */
166 if (0 >= psrc_city->airlift
168 /* The source cannot airlift for this turn (maybe already airlifted
169 * or no airport).
170 * See also do_airline() in server/unittools.h. */
171 return AR_SRC_NO_FLIGHTS;
172 } /* else, there is capacity; continue to other checks */
173 } else {
174 /* We don't have access to the 'airlift' field. Assume it's OK; can
175 * only find out for sure by trying it. */
177 }
178 }
179
180 if (nullptr != pdest_city && !game.info.airlift_to_always_enabled) {
181 if (nullptr == restriction || city_owner(pdest_city) == restriction) {
182 if (0 >= pdest_city->airlift
184 /* The destination cannot support airlifted units for this turn
185 * (maybe already airlifted or no airport).
186 * See also do_airline() in server/unittools.h. */
187 return AR_DST_NO_FLIGHTS;
188 } /* else continue */
189 } else {
191 }
192 }
193
194 return ok_result;
195}
196
197/**********************************************************************/
205 const struct unit *punit,
206 const struct city *pdest_city)
207{
208 if (is_server()) {
211 } else {
213 pdest_city));
214 }
215}
216
217/**********************************************************************/
220bool unit_has_orders(const struct unit *punit)
221{
222 return punit->has_orders;
223}
224
225/**********************************************************************/
232int unit_shield_value(const struct unit *punit,
233 const struct unit_type *punittype,
234 const struct action *paction)
235{
236 int value;
237
238 bool has_unit;
239 const struct player *act_player;
240
241 has_unit = punit != NULL;
242
243 if (has_unit && punittype == NULL) {
245 }
246
250
252 /* TODO: determine if tile and city should be where the unit currently is
253 * located or the target city. Those two may differ. Wait for ruleset
254 * author feed back. */
255
257 value += ((value
259 &(const struct req_context) {
260 .player = act_player,
261 .unit = punit,
262 .unittype = punittype,
263 .action = paction,
264 },
265 NULL,
267 / 100);
268
269 return value;
270}
271
272/**********************************************************************/
277 const struct unit *punit)
278{
279 struct city *pcity = tile_city(unit_tile(punit));
280
281 if (pcity == NULL) {
282 /* No city to help at this tile. */
283 return FALSE;
284 }
285
287 /* This unit can never do help wonder. */
288 return FALSE;
289 }
290
291 /* Evaluate all action enablers for extra accuracy. */
292 /* TODO: Is it worth it? */
295 pcity));
296}
297
298/**********************************************************************/
311
312/**********************************************************************/
316{
318}
319
320/**********************************************************************/
330
331/**********************************************************************/
334bool is_martial_law_unit(const struct unit *punit)
335{
337}
338
339/**********************************************************************/
342bool is_occupying_unit(const struct unit *punit)
343{
345}
346
347/**********************************************************************/
351{
353}
354
355/**********************************************************************/
358bool is_guard_unit(const struct unit *punit)
359{
361}
362
363/**********************************************************************/
368bool is_special_unit(const struct unit *punit)
369{
371}
372
373/**********************************************************************/
382 const struct player *pplayer)
383{
384 if (unit_owner(punit) != pplayer
386 if (pplayer == nullptr) {
387 /* Global observer always sees the flags */
388 return FALSE;
389 }
390
391 return TRUE;
392 }
393
394 return FALSE;
395}
396
397/**********************************************************************/
401bool unit_can_do_action(const struct unit *punit,
402 const action_id act_id)
403{
404 return utype_can_do_action(unit_type_get(punit), act_id);
405}
406
407/**********************************************************************/
412 enum action_result result)
413{
415}
416
417/**********************************************************************/
426
427/**********************************************************************/
431 const struct player *pplayer,
432 const struct tile *ptile, bool omniscient)
433{
434 square_iterate(nmap, ptile, 2, ptile1) {
436 if ((omniscient
437 || can_player_see_unit(pplayer, punit))
438 && pplayers_at_war(pplayer, unit_owner(punit))
443 unit_class_get(punit), ptile)))) {
444 return TRUE;
445 }
448
449 return FALSE;
450}
451
452/**********************************************************************/
456bool is_field_unit(const struct unit *punit)
457{
459}
460
461/**********************************************************************/
468bool is_hiding_unit(const struct unit *punit)
469{
471
472 if (vl == V_INVIS || vl == V_SUBSURFACE) {
473 return TRUE;
474 }
475
476 if (unit_transported(punit)) {
478 if (vl == V_INVIS || vl == V_SUBSURFACE) {
479 return TRUE;
480 }
481 }
482
483 return FALSE;
484}
485
486/**********************************************************************/
491 const struct unit *punit)
492{
493 struct city *tgt_city;
494
495 if ((tgt_city = tile_city(unit_tile(punit)))) {
498 } else {
501 }
502}
503
504/**********************************************************************/
508 const struct unit *punit,
509 const struct city *pcity)
510{
511 if (pcity == NULL) {
512 /* Can't change home city to a non existing city. */
513 return FALSE;
514 }
515
517 pcity));
518}
519
520/**********************************************************************/
524 const struct unit *punit)
525{
527}
528
529/**********************************************************************/
537int get_activity_rate(const struct unit *punit)
538{
539 const struct veteran_level *vlevel;
540 const struct unit_type *ptype;
541 int move_rate;
542
544
548
549 /* The speed of the settler depends on its base move_rate, not on
550 * the number of moves actually remaining or the adjusted move rate.
551 * This means sea formers won't have their activity rate increased by
552 * Magellan's, and it means injured units work just as fast as
553 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
554 move_rate = ptype->move_rate;
555
556 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
557 return ACTIVITY_FACTOR
558 * (float)vlevel->power_fact / 100
560}
561
562/**********************************************************************/
569{
570 /* This logic is also coded in client/goto.c. */
571 if (punit->moves_left > 0) {
572 return get_activity_rate(punit);
573 } else {
574 return 0;
575 }
576}
577
578/**********************************************************************/
585 enum unit_activity activity,
586 const struct tile *ptile,
587 struct extra_type *tgt)
588{
589 /* FIXME: This is just an approximation since we don't account for
590 * get_activity_rate_this_turn. */
592 int points_needed;
593
594 fc_assert(tgt != NULL || !is_targeted_activity(activity));
595
596 points_needed = tile_activity_time(activity, ptile, tgt);
597
598 if (points_needed >= 0 && speed > 0) {
599 return (points_needed - 1) / speed + 1; /* Round up */
600 } else {
601 return FC_INFINITY;
602 }
603}
604
605/**********************************************************************/
609{
610 switch (activity) {
611 case ACTIVITY_PILLAGE:
612 case ACTIVITY_BASE:
615 case ACTIVITY_MINE:
616 case ACTIVITY_CLEAN:
617 return TRUE;
618 case ACTIVITY_IDLE:
620 case ACTIVITY_SENTRY:
621 case ACTIVITY_GOTO:
622 case ACTIVITY_EXPLORE:
625 case ACTIVITY_PLANT:
627 case ACTIVITY_CONVERT:
628 return FALSE;
629 /* These shouldn't be kicking around internally. */
630 case ACTIVITY_LAST:
631 break;
632 }
633
635
636 return FALSE;
637}
638
639/**********************************************************************/
649{
650 return unit_type_get(punit)->adv.worker;
651}
652
653/**********************************************************************/
656const char *get_activity_text(enum unit_activity activity)
657{
658 /* The switch statement has just the activities listed with no "default"
659 * handling. This enables the compiler to detect missing entries
660 * automatically, and still handles everything correctly. */
661 switch (activity) {
662 case ACTIVITY_IDLE:
663 return _("Idle");
664 case ACTIVITY_CLEAN:
665 return _("Clean");
666 case ACTIVITY_MINE:
667 /* TRANS: Activity name, verb in English */
668 return Q_("?act:Mine");
669 case ACTIVITY_PLANT:
670 /* TRANS: Activity name, verb in English */
671 return _("Plant");
673 return _("Irrigate");
675 return _("Cultivate");
677 return _("Fortifying");
679 return _("Fortified");
680 case ACTIVITY_SENTRY:
681 return _("Sentry");
682 case ACTIVITY_PILLAGE:
683 return _("Pillage");
684 case ACTIVITY_GOTO:
685 return _("Goto");
686 case ACTIVITY_EXPLORE:
687 return _("Explore");
689 return _("Transform");
690 case ACTIVITY_BASE:
691 return _("Base");
693 return _("Road");
694 case ACTIVITY_CONVERT:
695 return _("Convert");
696 case ACTIVITY_LAST:
697 break;
698 }
699
701 return _("Unknown");
702}
703
704/**********************************************************************/
711 const struct unit *ptrans)
712{
713 /* Make sure this transporter can carry this type of unit. */
715 return FALSE;
716 }
717
718 /* Make sure there's room in the transporter. */
721 return FALSE;
722 }
723
724 /* Check iff this is a valid transport. */
726 return FALSE;
727 }
728
729 /* Check transport depth. */
732 return FALSE;
733 }
734
735 return TRUE;
736}
737
738/**********************************************************************/
742bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
743{
744 if (!pcargo || !ptrans || pcargo == ptrans) {
745 return FALSE;
746 }
747
748 /* Double-check ownership of the units: you can load into an allied unit
749 * (of course only allied units can be on the same tile). */
751 return FALSE;
752 }
753
754 /* Un-embarkable transport must be in city or base to load cargo. */
758 return FALSE;
759 }
760
762}
763
764/**********************************************************************/
767bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
768{
769 /* This function needs to check EVERYTHING. */
770
771 /* Check positions of the units. Of course you can't load a unit onto
772 * a transporter on a different tile... */
774 return FALSE;
775 }
776
777 /* Cannot load if cargo is already loaded onto something else. */
779 return FALSE;
780 }
781
783}
784
785/**********************************************************************/
794bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
795{
796 if (!pcargo || !ptrans) {
797 return FALSE;
798 }
799
800 /* Make sure the unit's transporter exists and is known. */
802 return FALSE;
803 }
804
805 /* Un-disembarkable transport must be in city or base to unload cargo. */
809 return FALSE;
810 }
811
812 return TRUE;
813}
814
815/**********************************************************************/
842
843/**********************************************************************/
848bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
849{
852 return TRUE;
853 }
855
856 return FALSE;
857}
858
859/**********************************************************************/
879
880/**********************************************************************/
884 struct unit *punit)
885{
886 enum unit_activity current = punit->activity;
887 struct extra_type *target = punit->activity_target;
888 enum unit_activity current2 =
889 (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
891 bool result;
892
895
897 target);
898
899 punit->activity = current;
900 punit->activity_target = target;
902
903 return result;
904}
905
906/**********************************************************************/
914 const struct unit *punit,
915 enum unit_activity activity,
916 enum gen_action action)
917{
918 struct extra_type *target = NULL;
919
920 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
921 * being able to find out whether an activity is in general possible.
922 * Find one for them, but when they come to do the activity, they will
923 * have to determine the target themselves */
924 {
925 struct tile *ptile = unit_tile(punit);
926
927 if (activity == ACTIVITY_IRRIGATE) {
928 target = next_extra_for_tile(ptile,
931 punit);
932 if (NULL == target) {
933 return FALSE; /* No more irrigation extras available. */
934 }
935 } else if (activity == ACTIVITY_MINE) {
936 target = next_extra_for_tile(ptile,
937 EC_MINE,
939 punit);
940 if (NULL == target) {
941 return FALSE; /* No more mine extras available. */
942 }
943 }
944 }
945
947 target);
948}
949
950/**********************************************************************/
955 const struct unit *punit,
956 enum unit_activity activity,
957 enum gen_action action,
958 struct extra_type *target)
959{
961 target, unit_tile(punit));
962}
963
964/**********************************************************************/
969 const struct unit *punit,
970 enum unit_activity activity,
971 enum gen_action action,
972 struct extra_type *target,
973 const struct tile *ptile)
974{
975 /* Check that no build activity conflicting with one already in progress
976 * gets executed. */
977 /* FIXME: Should check also the cases where one of the activities is terrain
978 * change that destroys the target of the other activity */
979 if (target != NULL && is_build_activity(activity)) {
980 if (tile_is_placing(ptile)) {
981 return FALSE;
982 }
983
984 unit_list_iterate(ptile->units, tunit) {
985 if (is_build_activity(tunit->activity)
986 && !can_extras_coexist(target, tunit->activity_target)) {
987 return FALSE;
988 }
990 }
991
992#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
993{ \
994 switch (action_get_target_kind(paction)) { \
995 case ATK_TILE: \
996 return is_action_enabled_unit_on_tile(nmap, paction->id, \
997 punit, ptile, target); \
998 case ATK_EXTRAS: \
999 return is_action_enabled_unit_on_extras(nmap, paction->id, \
1000 punit, ptile, target); \
1001 case ATK_CITY: \
1002 case ATK_UNIT: \
1003 case ATK_STACK: \
1004 case ATK_SELF: \
1005 return FALSE; \
1006 case ATK_COUNT: \
1007 break; /* Handle outside switch */ \
1008 } \
1009 fc_assert(action_target_kind_is_valid( \
1010 action_get_target_kind(paction))); \
1011 return FALSE; \
1012 }
1013
1014 switch (activity) {
1015 case ACTIVITY_IDLE:
1016 case ACTIVITY_GOTO:
1017 return TRUE;
1018
1019 case ACTIVITY_CLEAN:
1020 /* The call below doesn't support actor tile speculation. */
1021 fc_assert_msg(unit_tile(punit) == ptile,
1022 "Please use action_speculate_unit_on_tile()");
1024 punit, ptile, target);
1025
1026 case ACTIVITY_MINE:
1027 /* The call below doesn't support actor tile speculation. */
1028 fc_assert_msg(unit_tile(punit) == ptile,
1029 "Please use action_speculate_unit_on_tile()");
1031 ptile, target);
1032
1033 case ACTIVITY_PLANT:
1034 /* The call below doesn't support actor tile speculation. */
1035 fc_assert_msg(unit_tile(punit) == ptile,
1036 "Please use action_speculate_unit_on_tile()");
1038 punit, ptile, NULL);
1039
1040 case ACTIVITY_IRRIGATE:
1041 /* The call below doesn't support actor tile speculation. */
1042 fc_assert_msg(unit_tile(punit) == ptile,
1043 "Please use action_speculate_unit_on_tile()");
1045 ptile, target);
1046
1047 case ACTIVITY_CULTIVATE:
1048 /* The call below doesn't support actor tile speculation. */
1049 fc_assert_msg(unit_tile(punit) == ptile,
1050 "Please use action_speculate_unit_on_tile()");
1052 punit, ptile, NULL);
1053
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_self()");
1059 punit);
1060
1061 case ACTIVITY_FORTIFIED:
1062 return FALSE;
1063
1064 case ACTIVITY_BASE:
1065 /* The call below doesn't support actor tile speculation. */
1066 fc_assert_msg(unit_tile(punit) == ptile,
1067 "Please use action_speculate_unit_on_tile()");
1069 punit, ptile, target);
1070
1071 case ACTIVITY_GEN_ROAD:
1072 /* The call below doesn't support actor tile speculation. */
1073 fc_assert_msg(unit_tile(punit) == ptile,
1074 "Please use action_speculate_unit_on_tile()");
1076 punit, ptile, target);
1077
1078 case ACTIVITY_SENTRY:
1080 && !unit_transported(punit)) {
1081 /* Don't let units sentry on tiles they will die on. */
1082 return FALSE;
1083 }
1084 return TRUE;
1085
1086 case ACTIVITY_PILLAGE:
1087 /* The call below doesn't support actor tile speculation. */
1088 fc_assert_msg(unit_tile(punit) == ptile,
1089 "Please use action_speculate_unit_on_tile()");
1091
1092 case ACTIVITY_EXPLORE:
1093 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1094
1095 case ACTIVITY_TRANSFORM:
1096 /* The call below doesn't support actor tile speculation. */
1097 fc_assert_msg(unit_tile(punit) == ptile,
1098 "Please use action_speculate_unit_on_tile()");
1100 punit, ptile, NULL);
1101
1102 case ACTIVITY_CONVERT:
1103 /* The call below doesn't support actor tile speculation. */
1104 fc_assert_msg(unit_tile(punit) == ptile,
1105 "Please use action_speculate_unit_on_self()");
1107
1108 case ACTIVITY_LAST:
1109 break;
1110 }
1111
1112 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1113 activity);
1114
1115 return FALSE;
1116
1117#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1118}
1119
1120/**********************************************************************/
1126{
1129 punit->activity_count = 0;
1131 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1132 /* No longer done. */
1134 }
1135}
1136
1137/**********************************************************************/
1154
1155/**********************************************************************/
1173
1174/**********************************************************************/
1178 const struct tile *ptile)
1179{
1180 unit_list_iterate(ptile->units, punit) {
1181 if (punit->activity == activity) {
1182 return TRUE;
1183 }
1185
1186 return FALSE;
1187}
1188
1189/**********************************************************************/
1194{
1196
1198 unit_list_iterate(ptile->units, punit) {
1201 }
1203
1204 return tgt_ret;
1205}
1206
1207/**********************************************************************/
1210void unit_activity_astr(const struct unit *punit, struct astring *astr)
1211{
1212 if (!punit || !astr) {
1213 return;
1214 }
1215
1216 switch (punit->activity) {
1217 case ACTIVITY_IDLE:
1219 int rate, f;
1220
1222 f = ((punit->fuel) - 1);
1223
1224 /* Add in two parts as move_points_text() returns ptr to static
1225 * End result: "Moves: (fuel)moves_left" */
1226 astr_add_line(astr, "%s: (%s)", _("Moves"),
1228 astr_add(astr, "%s",
1230 } else {
1231 astr_add_line(astr, "%s: %s", _("Moves"),
1233 }
1234 return;
1235 case ACTIVITY_CLEAN:
1236 case ACTIVITY_TRANSFORM:
1238 case ACTIVITY_FORTIFIED:
1239 case ACTIVITY_SENTRY:
1240 case ACTIVITY_GOTO:
1241 case ACTIVITY_EXPLORE:
1242 case ACTIVITY_CONVERT:
1243 case ACTIVITY_CULTIVATE:
1244 case ACTIVITY_PLANT:
1246 return;
1247 case ACTIVITY_MINE:
1248 case ACTIVITY_IRRIGATE:
1249 if (punit->activity_target == NULL) {
1251 } else {
1252 astr_add_line(astr, "Building %s",
1254 }
1255 return;
1256 case ACTIVITY_PILLAGE:
1257 if (punit->activity_target != NULL) {
1260 } else {
1262 }
1263 return;
1264 case ACTIVITY_BASE:
1267 return;
1268 case ACTIVITY_GEN_ROAD:
1271 return;
1272 case ACTIVITY_LAST:
1273 break;
1274 }
1275
1276 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1278}
1279
1280/**********************************************************************/
1286void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1287{
1288 if (!punit || !astr) {
1289 return;
1290 }
1291
1292 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1294 punit->upkeep[O_GOLD]);
1295}
1296
1297/**********************************************************************/
1300struct player *unit_nationality(const struct unit *punit)
1301{
1303 return punit->nationality;
1304}
1305
1306/**********************************************************************/
1310void unit_tile_set(struct unit *punit, struct tile *ptile)
1311{
1313 punit->tile = ptile;
1314}
1315
1316/**********************************************************************/
1323struct unit *tile_allied_unit(const struct tile *ptile,
1324 const struct player *pplayer)
1325{
1326 struct unit *punit = NULL;
1327
1328 unit_list_iterate(ptile->units, cunit) {
1329 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1330 punit = cunit;
1331 } else {
1332 return NULL;
1333 }
1334 }
1336
1337 return punit;
1338}
1339
1340/**********************************************************************/
1346struct unit *tile_enemy_unit(const struct tile *ptile,
1347 const struct player *pplayer)
1348{
1349 unit_list_iterate(ptile->units, punit) {
1350 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1351 return punit;
1352 }
1354
1355 return NULL;
1356}
1357
1358/**********************************************************************/
1361struct unit *tile_non_allied_unit(const struct tile *ptile,
1362 const struct player *pplayer,
1364{
1365 unit_list_iterate(ptile->units, punit) {
1366 struct player *owner = unit_owner(punit);
1367
1368 if (everyone_non_allied && owner != pplayer) {
1369 return punit;
1370 }
1371
1372 if (!pplayers_allied(owner, pplayer)
1373 || is_flagless_to_player(punit, pplayer)) {
1374 return punit;
1375 }
1376 }
1378
1379 return NULL;
1380}
1381
1382/**********************************************************************/
1386struct unit *tile_other_players_unit(const struct tile *ptile,
1387 const struct player *pplayer)
1388{
1389 unit_list_iterate(ptile->units, punit) {
1390 if (unit_owner(punit) != pplayer) {
1391 return punit;
1392 }
1394
1395 return NULL;
1396}
1397
1398/**********************************************************************/
1402struct unit *tile_non_attack_unit(const struct tile *ptile,
1403 const struct player *pplayer)
1404{
1405 unit_list_iterate(ptile->units, punit) {
1406 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1407 return punit;
1408 }
1409 }
1411
1412 return NULL;
1413}
1414
1415/**********************************************************************/
1423struct unit *unit_occupies_tile(const struct tile *ptile,
1424 const struct player *pplayer)
1425{
1426 unit_list_iterate(ptile->units, punit) {
1427 if (!is_occupying_unit(punit)) {
1428 continue;
1429 }
1430
1432 continue;
1433 }
1434
1435 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1436 return punit;
1437 }
1439
1440 return NULL;
1441}
1442
1443/**********************************************************************/
1450bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1451 const struct civ_map *zmap)
1452{
1454
1455 square_iterate(zmap, ptile0, 1, ptile) {
1456 struct terrain *pterrain;
1457 struct city *pcity;
1458
1459 pterrain = tile_terrain(ptile);
1460 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1461 continue;
1462 }
1463
1464 pcity = tile_non_allied_city(ptile, pplayer);
1465 if (pcity != NULL) {
1466 if (unit_list_size(ptile->units) > 0) {
1467 /* Occupied enemy city, it doesn't matter if units inside have
1468 * UTYF_NOZOC or not. */
1469 return FALSE;
1470 }
1471 } else {
1472 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1474 if (tile_has_extra(ptile, pextra)) {
1475 return FALSE;
1476 }
1478 }
1479
1480 unit_list_iterate(ptile->units, punit) {
1481 if (!pplayers_allied(unit_owner(punit), pplayer)
1484 bool hidden = FALSE;
1485
1486 /* We do NOT check the possibility that player is allied with an extra owner,
1487 * and should thus see inside the extra.
1488 * This is to avoid the situation where having an alliance with third player
1489 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1490 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1491 if (tile_has_extra(ptile, pextra)) {
1492 hidden = TRUE;
1493 break;
1494 }
1496
1497 if (!hidden) {
1498 return FALSE;
1499 }
1500 }
1502 }
1504
1505 return TRUE;
1506}
1507
1508/**********************************************************************/
1519bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1520 const struct civ_map *zmap)
1521{
1523
1524 square_iterate(zmap, ptile0, 1, ptile) {
1525 struct terrain *pterrain;
1526 struct city *pcity;
1527
1528 pterrain = tile_terrain(ptile);
1529 if (T_UNKNOWN == pterrain
1530 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1531 continue;
1532 }
1533
1534 pcity = tile_non_allied_city(ptile, pplayer);
1535 if (pcity != NULL) {
1536 if (pcity->client.occupied
1537 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1538 /* Occupied enemy city, it doesn't matter if units inside have
1539 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1540 return FALSE;
1541 }
1542 } else {
1543 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1545 if (tile_has_extra(ptile, pextra)) {
1546 return FALSE;
1547 }
1549 }
1550
1551 unit_list_iterate(ptile->units, punit) {
1553 && !pplayers_allied(unit_owner(punit), pplayer)
1555 return FALSE;
1556 }
1558 }
1560
1561 return TRUE;
1562}
1563
1564/**********************************************************************/
1572
1573/**********************************************************************/
1582 const struct unit *punit)
1583{
1584 const struct tile *ptile = unit_tile(punit);
1586
1587 if (!is_attack_unit(punit)) {
1588 return FALSE;
1589 }
1590 if (tile_city(ptile)) {
1591 return FALSE;
1592 }
1594 switch (game.info.happyborders) {
1595 case HB_DISABLED:
1596 break;
1597 case HB_NATIONAL:
1598 if (tile_owner(ptile) == unit_owner(punit)) {
1599 return FALSE;
1600 }
1601 break;
1602 case HB_ALLIANCE:
1604 return FALSE;
1605 }
1606 break;
1607 }
1608 }
1609
1612 if (max_friendliness_range >= 0) {
1614 }
1615
1616 return TRUE;
1617}
1618
1619/**********************************************************************/
1623{
1624 switch (activity) {
1625 case ACTIVITY_MINE:
1626 case ACTIVITY_IRRIGATE:
1627 case ACTIVITY_BASE:
1628 case ACTIVITY_GEN_ROAD:
1629 return TRUE;
1630 default:
1631 return FALSE;
1632 }
1633}
1634
1635/**********************************************************************/
1639{
1640 switch (activity) {
1641 case ACTIVITY_PILLAGE:
1642 case ACTIVITY_CLEAN:
1643 return TRUE;
1644 default:
1645 return FALSE;
1646 }
1647}
1648
1649/**********************************************************************/
1653{
1654 switch (activity) {
1655 case ACTIVITY_CULTIVATE:
1656 case ACTIVITY_PLANT:
1657 case ACTIVITY_TRANSFORM:
1658 return TRUE;
1659 default:
1660 return FALSE;
1661 }
1662}
1663
1664/**********************************************************************/
1668{
1669 return is_build_activity(activity)
1670 || is_clean_activity(activity)
1671 || is_terrain_change_activity(activity);
1672}
1673
1674/**********************************************************************/
1678{
1679 return is_build_activity(activity)
1680 || is_clean_activity(activity);
1681}
1682
1683/**********************************************************************/
1687struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1688 const struct unit_type *punittype,
1689 int veteran_level)
1690{
1691 /* Make sure that contents of unit structure are correctly initialized,
1692 * if you ever allocate it by some other mean than fc_calloc() */
1693 struct unit *punit = fc_calloc(1, sizeof(*punit));
1694 int max_vet_lvl;
1695
1696 /* It does not register the unit so the id is set to 0. */
1698
1699 fc_assert_ret_val(punittype != nullptr, nullptr); /* No untyped units! */
1701
1702 fc_assert_ret_val(!is_server() || pplayer != nullptr, nullptr); /* No unowned units! */
1703 punit->owner = pplayer;
1704 punit->nationality = pplayer;
1705
1706 punit->refcount = 1;
1708
1709 if (pcity != nullptr) {
1711 punit->homecity = pcity->id;
1712 } else {
1713 unit_tile_set(punit, nullptr);
1715 }
1716
1717 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1718 punit->goto_tile = nullptr;
1721 /* A unit new and fresh ... */
1725 /* Random moves units start with zero movement as their first movement
1726 * will be only after their moves have been reset in the beginning of
1727 * the next turn. */
1728 punit->moves_left = 0;
1729 } else {
1731 }
1732 punit->moved = FALSE;
1733
1737
1738 punit->transporter = nullptr;
1740
1741 punit->carrying = nullptr;
1742
1746
1748 punit->action_decision_tile = nullptr;
1749
1750 punit->stay = FALSE;
1751
1754
1755 if (is_server()) {
1757
1759
1760 punit->server.removal_callback = nullptr;
1761
1763 O_LAST * sizeof(*punit->server.upkeep_paid));
1764
1765 punit->server.ord_map = 0;
1766 punit->server.ord_city = 0;
1767
1768 punit->server.vision = nullptr; /* No vision. */
1770 /* Must be an invalid turn number, and an invalid previous turn
1771 * number. */
1772 punit->server.action_turn = -2;
1773 /* punit->server.moving = NULL; set by fc_calloc(). */
1774
1775 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1776
1777 CALL_FUNC_EACH_AI(unit_alloc, punit);
1778 } else {
1782 punit->client.act_prob_cache = nullptr;
1783 }
1784
1785 return punit;
1786}
1787
1788/**********************************************************************/
1793{
1795
1796 /* Unload unit if transported. */
1799
1800 /* Check for transported units. Use direct access to the list. */
1801 if (unit_list_size(punit->transporting) != 0) {
1802 /* Unload all units. */
1806 }
1808
1809 if (punit->transporting) {
1811 }
1812
1813 CALL_FUNC_EACH_AI(unit_free, punit);
1814
1815 if (is_server() && punit->server.adv) {
1817 } else {
1820 }
1821 }
1822
1823 if (--punit->refcount <= 0) {
1824 FC_FREE(punit);
1825 }
1826}
1827
1828/**********************************************************************/
1833{
1834 if (punit->has_orders) {
1835 punit->goto_tile = NULL;
1837 punit->orders.list = NULL;
1838 }
1839 punit->orders.length = 0;
1841}
1842
1843/**********************************************************************/
1847{
1849
1850 return unit_list_size(ptrans->transporting);
1851}
1852
1853/**********************************************************************/
1856static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1857 const struct tile *ptile,
1858 bool (*unit_load_test)
1859 (const struct unit *pc,
1860 const struct unit *pt))
1861{
1862 struct unit *best_trans = NULL;
1863 struct {
1866 } cur, best = { FALSE };
1867
1868 unit_list_iterate(ptile->units, ptrans) {
1869 if (!unit_load_test(pcargo, ptrans)) {
1870 continue;
1871 } else if (best_trans == NULL) {
1873 }
1874
1875 /* Gather data from transport stack in a single pass, for use in
1876 * various conditions below. */
1878 cur.outermost_moves_left = ptrans->moves_left;
1879 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1882 cur.has_orders = TRUE;
1883 }
1884 cur.outermost_moves_left = ptranstrans->moves_left;
1885 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1887
1888 /* Criteria for deciding the 'best' transport to load onto.
1889 * The following tests are applied in order; earlier ones have
1890 * lexicographically greater significance than later ones. */
1891
1892 /* Transports which have orders, or are on transports with orders,
1893 * are less preferable to transport stacks without orders (to
1894 * avoid loading on units that are just passing through). */
1895 if (best_trans != ptrans) {
1896 if (!cur.has_orders && best.has_orders) {
1898 } else if (cur.has_orders && !best.has_orders) {
1899 continue;
1900 }
1901 }
1902
1903 /* Else, transports which are idle are preferable (giving players
1904 * some control over loading) -- this does not check transports
1905 * of transports. */
1906 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1907 if (best_trans != ptrans) {
1908 if (cur.is_idle && !best.is_idle) {
1910 } else if (!cur.is_idle && best.is_idle) {
1911 continue;
1912 }
1913 }
1914
1915 /* Else, transports from which the cargo could unload at any time
1916 * are preferable to those where the cargo can only disembark in
1917 * cities/bases. */
1918 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1920 if (best_trans != ptrans) {
1921 if (cur.can_freely_unload && !best.can_freely_unload) {
1923 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1924 continue;
1925 }
1926 }
1927
1928 /* Else, transports which are less deeply nested are preferable. */
1930 if (best_trans != ptrans) {
1931 if (cur.depth < best.depth) {
1933 } else if (cur.depth > best.depth) {
1934 continue;
1935 }
1936 }
1937
1938 /* Else, transport stacks where the outermost transport has more
1939 * moves left are preferable (on the assumption that it's the
1940 * outermost transport that's about to move). */
1941 if (best_trans != ptrans) {
1942 if (cur.outermost_moves_left > best.outermost_moves_left) {
1944 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1945 continue;
1946 }
1947 }
1948
1949 /* All other things being equal, as a tie-breaker, compare the total
1950 * moves left (this turn) and move rate (future turns) for the whole
1951 * stack, to take into account total potential movement for both
1952 * short and long journeys (we don't know which the cargo intends to
1953 * make). Doesn't try to account for whether transports can unload,
1954 * etc. */
1955 if (best_trans != ptrans) {
1956 if (cur.total_moves > best.total_moves) {
1958 } else {
1959 continue;
1960 }
1961 }
1962
1964 best = cur;
1966
1967 return best_trans;
1968}
1969
1970/**********************************************************************/
1979
1980/**********************************************************************/
1986 const struct tile *ptile)
1987{
1989}
1990
1991/**********************************************************************/
1996 const struct unit *punit)
1997{
1999 return FALSE;
2000 }
2001
2004 return FALSE;
2005 }
2007
2008 return TRUE;
2009}
2010
2011/**********************************************************************/
2020 const struct unit *punit,
2021 const struct unit_type *to_unittype)
2022{
2024
2026 return UU_NOT_ENOUGH_ROOM;
2027 }
2028
2029 if (punit->transporter != NULL) {
2033 }
2035 /* The new unit type can't survive on this terrain. */
2036 return UU_NOT_TERRAIN;
2037 }
2038
2039 return UU_OK;
2040}
2041
2042/**********************************************************************/
2052 const struct unit *punit,
2053 bool is_free)
2054{
2055 struct player *pplayer = unit_owner(punit);
2056 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2058 struct city *pcity;
2059 int cost;
2060
2061 if (!to_unittype) {
2062 return UU_NO_UNITTYPE;
2063 }
2064
2066 /* TODO: There may be other activities that the upgraded unit is not
2067 allowed to do, which we could also test.
2068 -
2069 If convert were legal for new unit_type we could allow, but then it
2070 has a 'head start' getting activity time from the old conversion. */
2071 return UU_NOT_ACTIVITY;
2072 }
2073
2074 if (!is_free) {
2076 if (pplayer->economic.gold < cost) {
2077 return UU_NO_MONEY;
2078 }
2079
2081 if (!pcity) {
2082 return UU_NOT_IN_CITY;
2083 }
2084 if (city_owner(pcity) != pplayer) {
2085 /* TODO: Should upgrades in allied cities be possible? */
2086 return UU_NOT_CITY_OWNER;
2087 }
2088 }
2089
2090 /* TODO: Allow transported units to be reassigned. Check here
2091 * and make changes to upgrade_unit. */
2093}
2094
2095/**********************************************************************/
2098bool unit_can_convert(const struct civ_map *nmap,
2099 const struct unit *punit)
2100{
2101 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2102
2103 if (tgt == NULL) {
2104 return FALSE;
2105 }
2106
2107 return UU_OK == unit_transform_result(nmap, punit, tgt);
2108}
2109
2110/**********************************************************************/
2115 const struct unit *punit,
2116 char *buf, size_t bufsz)
2117{
2118 struct player *pplayer = unit_owner(punit);
2120 int upgrade_cost;
2121 const struct unit_type *from_unittype = unit_type_get(punit);
2122 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2124 char tbuf[MAX_LEN_MSG];
2125
2126 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2127 "Treasury contains %d gold.",
2128 pplayer->economic.gold),
2129 pplayer->economic.gold);
2130
2131 switch (result) {
2132 case UU_OK:
2134 /* This message is targeted toward the GUI callers. */
2135 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2136 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2137 "Upgrade %s to %s for %d gold?\n%s",
2138 upgrade_cost),
2142 break;
2143 case UU_NO_UNITTYPE:
2145 _("Sorry, cannot upgrade %s (yet)."),
2147 break;
2148 case UU_NO_MONEY:
2150 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2151 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2152 "Upgrading %s to %s costs %d gold.\n%s",
2153 upgrade_cost),
2157 break;
2158 case UU_NOT_IN_CITY:
2159 case UU_NOT_CITY_OWNER:
2161 _("You can only upgrade units in your cities."));
2162 break;
2163 case UU_NOT_ENOUGH_ROOM:
2165 _("Upgrading this %s would strand units it transports."),
2167 break;
2168 case UU_NOT_TERRAIN:
2170 _("Upgrading this %s would result in a %s which can not "
2171 "survive at this place."),
2174 break;
2177 _("Upgrading this %s would result in a %s which its "
2178 "current transport, %s, could not transport."),
2182 break;
2183 case UU_NOT_ACTIVITY:
2185 _("Cannot upgrade %s while doing '%s'."),
2188 break;
2189 }
2190
2191 return result;
2192}
2193
2194/**********************************************************************/
2199 const struct unit *punit)
2200{
2201 int mpco;
2202
2204 &(const struct req_context) {
2205 .player = unit_owner(punit),
2206 .city = unit_tile(punit)
2208 : NULL,
2209 .tile = unit_tile(punit),
2210 .unit = punit,
2211 .unittype = unit_type_get(punit),
2212 .action = paction,
2213 },
2214 NULL,
2216
2218
2219 return mpco;
2220}
2221
2222/**********************************************************************/
2225int hp_gain_coord(const struct unit *punit)
2226{
2227 int hp = 0;
2228 const int base = unit_type_get(punit)->hp;
2229 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2230
2231 /* Includes barracks (100%), fortress (25%), etc. */
2233
2234 /* Minimum HP after regen effects applied. */
2235 hp = MAX(hp, min);
2236
2237 /* Regen2 effects that apply after there's at least Min HP */
2238 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2239
2240 return MAX(hp, 0);
2241}
2242
2243/**********************************************************************/
2247{
2248 const struct unit_type *utype = unit_type_get(punit);
2250 struct city *pcity = tile_city(unit_tile(punit));
2251 int gain;
2252
2253 if (!punit->moved) {
2255 } else {
2256 gain = 0;
2257 }
2258
2260
2261 if (!punit->homecity && 0 < game.server.killunhomed
2263 /* Hit point loss of units without homecity; at least 1 hp! */
2264 /* Gameloss units are immune to this effect. */
2265 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2266
2267 if (gain > hp_loss) {
2268 gain = -1;
2269 } else {
2270 gain -= hp_loss;
2271 }
2272 }
2273
2274 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2275 && !unit_transported(punit)) {
2276 gain -= utype->hp * pclass->hp_loss_pct / 100;
2277 }
2278
2279 if (punit->hp + gain > utype->hp) {
2280 gain = utype->hp - punit->hp;
2281 } else if (punit->hp + gain < 0) {
2282 gain = -punit->hp;
2283 }
2284
2285 return gain;
2286}
2287
2288/**********************************************************************/
2291bool is_losing_hp(const struct unit *punit)
2292{
2293 const struct unit_type *punittype = unit_type_get(punit);
2294
2296 < (punittype->hp *
2297 utype_class(punittype)->hp_loss_pct / 100);
2298}
2299
2300/**********************************************************************/
2303bool unit_type_is_losing_hp(const struct player *pplayer,
2304 const struct unit_type *punittype)
2305{
2306 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2308 < (punittype->hp *
2309 utype_class(punittype)->hp_loss_pct / 100);
2310}
2311
2312/**********************************************************************/
2316bool unit_is_alive(int id)
2317{
2318 /* Check if unit exist in game */
2319 if (game_unit_by_number(id)) {
2320 return TRUE;
2321 }
2322
2323 return FALSE;
2324}
2325
2326/**********************************************************************/
2333bool unit_is_virtual(const struct unit *punit)
2334{
2335 if (!punit) {
2336 return FALSE;
2337 }
2338
2339 return punit != game_unit_by_number(punit->id);
2340}
2341
2342/**********************************************************************/
2345void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2346{
2347 return punit->server.ais[ai_type_number(ai)];
2348}
2349
2350/**********************************************************************/
2353void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2354 void *data)
2355{
2356 punit->server.ais[ai_type_number(ai)] = data;
2357}
2358
2359/**********************************************************************/
2370int unit_bribe_cost(const struct unit *punit, const struct player *briber,
2371 const struct unit *briber_unit)
2372{
2373 int cost, default_hp;
2374 struct tile *ptile = unit_tile(punit);
2375 struct player *owner = unit_owner(punit);
2376 const struct unit_type *ptype = unit_type_get(punit);
2377
2379
2380 default_hp = ptype->hp;
2381
2383
2384 if (owner != nullptr) {
2385 int dist = 0;
2386
2387 cost += owner->economic.gold;
2388
2389 /* Consider the distance to the capital. */
2391 city_list_iterate(owner->cities, capital) {
2392 if (is_capital(capital)) {
2393 int tmp = map_distance(capital->tile, ptile);
2394
2395 if (tmp < dist) {
2396 dist = tmp;
2397 }
2398 }
2400
2401 cost /= dist + 2;
2402 }
2403
2404 /* Consider the build cost. */
2406
2407 /* Rule set specific cost modification */
2409 &(const struct req_context) {
2410 .player = owner,
2412 .tile = ptile,
2413 .unit = punit,
2414 .unittype = ptype,
2415 },
2416 &(const struct req_context) {
2417 .player = briber,
2418 .unit = briber_unit,
2420 : nullptr,
2422 : nullptr,
2423 .city = briber_unit
2424 ? game_city_by_number(briber_unit->homecity)
2425 : nullptr,
2426 },
2428 / 100;
2429
2430 /* Veterans are not cheap. */
2431 {
2432 const struct veteran_level *vlevel
2434
2436 cost = cost * vlevel->power_fact / 100;
2437 if (ptype->move_rate > 0) {
2438 cost += cost * vlevel->move_bonus / ptype->move_rate;
2439 } else {
2440 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2441 }
2442 }
2443
2444 /* Cost now contains the basic bribe cost. We now reduce it by:
2445 * bribecost = cost / 2 + cost / 2 * damage / hp
2446 * = cost / 2 * (1 + damage / hp) */
2447 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2448}
2449
2450/**********************************************************************/
2458int stack_bribe_cost(const struct tile *ptile, const struct player *briber,
2459 const struct unit *briber_unit)
2460{
2461 int bribe_cost = 0;
2462
2466
2467 return bribe_cost;
2468}
2469
2470/**********************************************************************/
2473bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2474{
2477
2479
2480 if (force || can_unit_load(pcargo, ptrans)) {
2481 pcargo->transporter = ptrans;
2482 unit_list_append(ptrans->transporting, pcargo);
2483
2484 return TRUE;
2485 }
2486
2487 return FALSE;
2488}
2489
2490/**********************************************************************/
2494{
2495 struct unit *ptrans;
2496
2498
2499 if (!unit_transported(pcargo)) {
2500 /* 'pcargo' is not transported. */
2501 return FALSE;
2502 }
2503
2504 /* Get the transporter; must not be defined on the client! */
2506 if (ptrans) {
2507 /* 'pcargo' and 'ptrans' should be on the same tile. */
2509
2510#ifndef FREECIV_NDEBUG
2511 bool success =
2512#endif
2513 unit_list_remove(ptrans->transporting, pcargo);
2514
2515 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2517 }
2518
2519 /* For the server (also safe for the client). */
2520 pcargo->transporter = NULL;
2521
2522 return TRUE;
2523}
2524
2525/**********************************************************************/
2528bool unit_transported(const struct unit *pcargo)
2529{
2531
2532 /* The unit is transported if a transporter unit is set or, (for the client)
2533 * if the transported_by field is set. */
2534 if (is_server()) {
2536 } else {
2538 }
2539}
2540
2541/**********************************************************************/
2544struct unit *unit_transport_get(const struct unit *pcargo)
2545{
2547
2548 return pcargo->transporter;
2549}
2550
2551/**********************************************************************/
2554struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2555{
2557 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2558
2559 return ptrans->transporting;
2560}
2561
2562/**********************************************************************/
2565static inline bool
2573
2574/**********************************************************************/
2583 const struct unit *ptrans)
2584{
2585 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2586
2587 /* Check 'pcargo' against 'ptrans'. */
2589 return FALSE;
2590 }
2591
2592 /* Check 'pcargo' against 'ptrans' parents. */
2595 return FALSE;
2596 }
2598
2599 /* Check cargo children... */
2602
2603 /* ...against 'ptrans'. */
2605 return FALSE;
2606 }
2607
2608 /* ...and against 'ptrans' parents. */
2611 return FALSE;
2612 }
2615
2616 return TRUE;
2617}
2618
2619/**********************************************************************/
2623bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2624{
2626 if (ptrans == plevel) {
2627 return TRUE;
2628 }
2630 return FALSE;
2631}
2632
2633/**********************************************************************/
2636int unit_cargo_depth(const struct unit *ptrans)
2637{
2638 struct cargo_iter iter;
2639 struct iterator *it;
2640 int depth = 0;
2641
2642 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2643 iterator_next(it)) {
2644 if (iter.depth > depth) {
2645 depth = iter.depth;
2646 }
2647 }
2648 return depth;
2649}
2650
2651/**********************************************************************/
2655{
2656 int level = 0;
2657
2659 level++;
2661 return level;
2662}
2663
2664/**********************************************************************/
2668{
2669 return sizeof(struct cargo_iter);
2670}
2671
2672/**********************************************************************/
2675static void *cargo_iter_get(const struct iterator *it)
2676{
2677 const struct cargo_iter *iter = CARGO_ITER(it);
2678
2679 return unit_list_link_data(iter->links[iter->depth - 1]);
2680}
2681
2682/**********************************************************************/
2685static void cargo_iter_next(struct iterator *it)
2686{
2687 struct cargo_iter *iter = CARGO_ITER(it);
2688 const struct unit_list_link *piter;
2689 const struct unit_list_link *pnext;
2690
2691 /* Variant 1: unit has cargo. */
2693 if (NULL != pnext) {
2694 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2695 iter->links[iter->depth++] = pnext;
2696 return;
2697 }
2698
2699 fc_assert(iter->depth > 0);
2700
2701 while (iter->depth > 0) {
2702 piter = iter->links[iter->depth - 1];
2703
2704 /* Variant 2: there are other cargo units at same level. */
2706 if (NULL != pnext) {
2707 iter->links[iter->depth - 1] = pnext;
2708 return;
2709 }
2710
2711 /* Variant 3: return to previous level, and do same tests. */
2712 iter->depth--;
2713 }
2714}
2715
2716/**********************************************************************/
2719static bool cargo_iter_valid(const struct iterator *it)
2720{
2721 return (0 < CARGO_ITER(it)->depth);
2722}
2723
2724/**********************************************************************/
2728 const struct unit *ptrans)
2729{
2730 struct iterator *it = ITERATOR(iter);
2731
2732 it->get = cargo_iter_get;
2733 it->next = cargo_iter_next;
2734 it->valid = cargo_iter_valid;
2736 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2737
2738 return it;
2739}
2740
2741/**********************************************************************/
2744bool unit_is_cityfounder(const struct unit *punit)
2745{
2747}
2748
2749/**********************************************************************/
2753 int length, const struct unit_order *orders)
2754{
2755 int i;
2756
2757 for (i = 0; i < length; i++) {
2758 struct action *paction;
2759 struct extra_type *pextra;
2760
2761 if (orders[i].order > ORDER_LAST) {
2762 log_error("invalid order %d at index %d", orders[i].order, i);
2763 return FALSE;
2764 }
2765 switch (orders[i].order) {
2766 case ORDER_MOVE:
2767 case ORDER_ACTION_MOVE:
2768 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2769 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2770 return FALSE;
2771 }
2772 break;
2773 case ORDER_ACTIVITY:
2774 switch (orders[i].activity) {
2775 case ACTIVITY_SENTRY:
2776 if (i != length - 1) {
2777 /* Only allowed as the last order. */
2778 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2779 i);
2780 return FALSE;
2781 }
2782 break;
2783 /* Replaced by action orders */
2784 case ACTIVITY_BASE:
2785 case ACTIVITY_GEN_ROAD:
2786 case ACTIVITY_CLEAN:
2787 case ACTIVITY_PILLAGE:
2788 case ACTIVITY_MINE:
2789 case ACTIVITY_IRRIGATE:
2790 case ACTIVITY_PLANT:
2791 case ACTIVITY_CULTIVATE:
2792 case ACTIVITY_TRANSFORM:
2793 case ACTIVITY_CONVERT:
2795 log_error("at index %d, use action rather than activity %d.",
2796 i, orders[i].activity);
2797 return FALSE;
2798 /* Not supported. */
2799 case ACTIVITY_EXPLORE:
2800 case ACTIVITY_IDLE:
2801 /* Not set from the client. */
2802 case ACTIVITY_GOTO:
2803 case ACTIVITY_FORTIFIED:
2804 /* Unused. */
2805 case ACTIVITY_LAST:
2806 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2807 return FALSE;
2808 }
2809
2810 break;
2812 if (!action_id_exists(orders[i].action)) {
2813 /* Non-existent action. */
2814 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2815 return FALSE;
2816 }
2817
2818 paction = action_by_number(orders[i].action);
2819
2820 /* Validate main target. */
2821 if (index_to_tile(nmap, orders[i].target) == NULL) {
2822 log_error("at index %d, invalid tile target %d for the action %d.",
2823 i, orders[i].target, orders[i].action);
2824 return FALSE;
2825 }
2826
2827 if (orders[i].dir != DIR8_ORIGIN) {
2828 log_error("at index %d, the action %d sets the outdated target"
2829 " specification dir.",
2830 i, orders[i].action);
2831 }
2832
2833 /* Validate sub target. */
2834 switch (action_id_get_sub_target_kind(orders[i].action)) {
2835 case ASTK_BUILDING:
2836 /* Sub target is a building. */
2837 if (!improvement_by_number(orders[i].sub_target)) {
2838 /* Sub target is invalid. */
2839 log_error("at index %d, cannot do %s without a target.", i,
2840 action_id_rule_name(orders[i].action));
2841 return FALSE;
2842 }
2843 break;
2844 case ASTK_TECH:
2845 /* Sub target is a technology. */
2846 if (orders[i].sub_target == A_NONE
2847 || (!valid_advance_by_number(orders[i].sub_target)
2848 && orders[i].sub_target != A_FUTURE)) {
2849 /* Target tech is invalid. */
2850 log_error("at index %d, cannot do %s without a target.", i,
2851 action_id_rule_name(orders[i].action));
2852 return FALSE;
2853 }
2854 break;
2855 case ASTK_EXTRA:
2857 /* Sub target is an extra. */
2858 pextra = (!(orders[i].sub_target == NO_TARGET
2859 || (orders[i].sub_target < 0
2860 || (orders[i].sub_target
2862 ? extra_by_number(orders[i].sub_target) : NULL);
2863 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2864 if (pextra == NULL) {
2865 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2866 /* Target extra is invalid. */
2867 log_error("at index %d, cannot do %s without a target.", i,
2868 action_id_rule_name(orders[i].action));
2869 return FALSE;
2870 }
2871 } else {
2872 if (!actres_removes_extra(paction->result, pextra)
2873 && !actres_creates_extra(paction->result, pextra)) {
2874 /* Target extra is irrelevant for the action. */
2875 log_error("at index %d, cannot do %s to %s.", i,
2876 action_id_rule_name(orders[i].action),
2877 extra_rule_name(pextra));
2878 return FALSE;
2879 }
2880 }
2881 break;
2882 case ASTK_NONE:
2883 /* No validation required. */
2884 break;
2885 /* Invalid action? */
2886 case ASTK_COUNT:
2889 FALSE,
2890 "Bad action %d in order number %d.", orders[i].action, i);
2891 }
2892
2893 /* Some action orders are sane only in the last order. */
2894 if (i != length - 1) {
2895 /* If the unit is dead, */
2897 /* or if Freeciv has no idea where the unit will end up after it
2898 * has performed this action, */
2901 /* or if the unit will end up standing still, */
2903 /* than having this action in the middle of a unit's orders is
2904 * probably wrong. */
2905 log_error("action %d is not allowed at index %d.",
2906 orders[i].action, i);
2907 return FALSE;
2908 }
2909 }
2910
2911 /* Don't validate that the target tile really contains a target or
2912 * that the actor player's map think the target tile has one.
2913 * The player may target something from their player map that isn't
2914 * there any more, a target they think is there even if their player
2915 * map doesn't have it, or even a target they assume will be there
2916 * when the unit reaches the target tile.
2917 *
2918 * With that said: The client should probably at least have an
2919 * option to only aim city targeted actions at cities. */
2920
2921 break;
2922 case ORDER_FULL_MP:
2923 break;
2924 case ORDER_LAST:
2925 /* An invalid order. This is handled above. */
2926 break;
2927 }
2928 }
2929
2930 return TRUE;
2931}
2932
2933/**********************************************************************/
2938 int length,
2939 const struct unit_order *orders)
2940{
2941 struct unit_order *unit_orders;
2942
2943 if (!unit_order_list_is_sane(nmap, length, orders)) {
2944 return NULL;
2945 }
2946
2947 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2948 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2949
2950 return unit_orders;
2951}
2952
2953/**********************************************************************/
2957{
2959 ACTION_NONE, // ACTIVITY_IDLE
2960 ACTION_CULTIVATE, // ACTIVITY_CULTIVATE
2961 ACTION_MINE, // ACTIVITY_MINE
2962 ACTION_IRRIGATE, // ACTIVITY_IRRIGATE
2963 ACTION_FORTIFY, // ACTIVITY_FORTIFIED
2964 ACTION_NONE, // ACTIVITY_SENTRY
2965 ACTION_PILLAGE, // ACTIVITY_PILLAGE
2966 ACTION_NONE, // ACTIVITY_GOTO
2967 ACTION_NONE, // ACTIVITY_EXPLORE
2968 ACTION_TRANSFORM_TERRAIN, // ACTIVITY_TRANSFORM
2969 ACTION_FORTIFY, // ACTIVITY_FORTIFYING
2970 ACTION_CLEAN, // ACTIVITY_CLEAN
2971 ACTION_BASE, // ACTIVITY_BASE
2972 ACTION_ROAD, // ACTIVITY_GEN_ROAD
2973 ACTION_CONVERT, // ACTIVITY_CONVERT
2974 ACTION_PLANT // ACTIVITY_PLANT
2975 };
2976
2977 return act_act[act];
2978}
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5068
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:4589
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1239
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:3309
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:3068
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:3145
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:4231
bool action_maybe_possible_actor_unit(const struct civ_map *nmap, const action_id act_id, const struct unit *actor_unit)
Definition actions.c:5491
bool action_id_exists(const action_id act_id)
Definition actions.c:1068
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:3458
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:4314
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:245
#define action_id_get_sub_target_kind(act_id)
Definition actions.h:421
static struct action * action_by_number(action_id act_id)
Definition actions.h:400
#define action_has_result(_act_, _res_)
Definition actions.h:184
#define action_by_result_iterate_end
Definition actions.h:249
#define action_id(_act_)
Definition actions.h:426
#define ACTION_NONE
Definition actions.h:59
bool actres_removes_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:810
bool actres_creates_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:789
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:283
void astr_add(struct astring *astr, const char *format,...)
Definition astring.c:271
#define BV_CLR_ALL(bv)
Definition bitvector.h:103
#define BV_SET(bv, bit)
Definition bitvector.h:89
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:505
#define city_tile(_pcity_)
Definition city.h:561
#define city_owner(_pcity_)
Definition city.h:560
#define city_list_iterate_end
Definition city.h:507
char * incite_cost
Definition comments.c:76
#define MAX_LEN_MSG
Definition conn_types.h:37
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2965
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 const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
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_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Definition effects.c:1066
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct req_context *other_context, enum effect_type effect_type)
Definition effects.c:744
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:1031
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:779
struct player * extra_owner(const struct tile *ptile)
Definition extras.c:1128
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:1017
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:213
enum unit_activity Activity_type_id
Definition fc_types.h:238
#define DIR8_ORIGIN
Definition fc_types.h:314
int action_id
Definition fc_types.h:248
@ HB_ALLIANCE
Definition fc_types.h:1001
@ HB_DISABLED
Definition fc_types.h:999
@ HB_NATIONAL
Definition fc_types.h:1000
@ 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:745
#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:61
struct unit * game_unit_by_number(int id)
Definition game.c:115
struct city * game_city_by_number(int id)
Definition game.c:106
#define GAME_UNIT_BRIBE_DIST_MAX
Definition game.h:764
#define GAME_TRANSPORT_MAX_RECURSIVE
Definition game.h:767
struct city * owner
Definition citydlg.c:226
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:182
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_error(message,...)
Definition log.h:104
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition log.h:209
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:471
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:1076
bool map_untrusted_dir_is_valid(enum direction8 dir)
Definition map.c:1414
enum direction8 rand_direction(void)
Definition map.c:2084
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:699
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:388
#define square_iterate_end
Definition map.h:391
#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:1048
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Definition movement.c:278
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:362
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:309
bool can_unit_transport(const struct unit *transporter, const struct unit *transported)
Definition movement.c:888
int unit_move_rate(const struct unit *punit)
Definition movement.c:89
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:522
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:496
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:901
bool can_attack_non_native(const struct unit_type *utype)
Definition movement.c:213
#define SINGLE_MOVE
Definition movement.h:26
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:1409
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1463
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:317
struct tile * tile
Definition city.h:319
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
int killunhomed
Definition game.h:161
struct civ_game::@32::@36 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:284
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
int sub_target
Definition unit.h:99
int transport_capacity
Definition unittype.h:528
bool worker
Definition unittype.h:580
int move_rate
Definition unittype.h:522
enum vision_layer vlayer
Definition unittype.h:574
struct unit_type::@90 adv
const struct unit_type * converted_to
Definition unittype.h:535
Definition unit.h:140
time_t action_timestamp
Definition unit.h:248
int length
Definition unit.h:198
int upkeep[O_LAST]
Definition unit.h:150
bool has_orders
Definition unit.h:196
bool occupied
Definition unit.h:222
enum action_decision action_decision_want
Definition unit.h:205
int battlegroup
Definition unit.h:194
enum unit_activity activity
Definition unit.h:159
int moves_left
Definition unit.h:152
int refcount
Definition unit.h:143
int id
Definition unit.h:147
enum gen_action action
Definition unit.h:160
struct unit_list * transporting
Definition unit.h:187
int ord_city
Definition unit.h:245
struct unit::@83 orders
bool moved
Definition unit.h:176
int ord_map
Definition unit.h:244
bool debug
Definition unit.h:237
struct vision * vision
Definition unit.h:247
int hp
Definition unit.h:153
int fuel
Definition unit.h:155
struct extra_type * changed_from_target
Definition unit.h:173
int current_form_turn
Definition unit.h:211
bool stay
Definition unit.h:208
bool colored
Definition unit.h:225
enum direction8 facing
Definition unit.h:144
struct unit::@84::@87 server
struct tile * tile
Definition unit.h:142
struct unit::@84::@86 client
struct extra_type * activity_target
Definition unit.h:167
struct act_prob * act_prob_cache
Definition unit.h:231
int activity_count
Definition unit.h:165
struct unit_order * list
Definition unit.h:201
enum unit_activity changed_from
Definition unit.h:171
struct unit_adv * adv
Definition unit.h:239
struct player * nationality
Definition unit.h:146
int transported_by
Definition unit.h:219
void(* removal_callback)(struct unit *punit)
Definition unit.h:256
void * ais[FREECIV_AI_MOD_LAST]
Definition unit.h:240
int action_turn
Definition unit.h:249
int homecity
Definition unit.h:148
int upkeep_paid[O_LAST]
Definition unit.h:259
bool paradropped
Definition unit.h:177
bool done_moving
Definition unit.h:184
struct unit * transporter
Definition unit.h:186
int birth_turn
Definition unit.h:210
struct goods_type * carrying
Definition unit.h:189
struct tile * goto_tile
Definition unit.h:157
struct tile * action_decision_tile
Definition unit.h:206
const struct unit_type * utype
Definition unit.h:141
int veteran
Definition unit.h:154
int changed_from_count
Definition unit.h:172
struct player * owner
Definition unit.h:145
bool dying
Definition unit.h:253
enum unit_focus_status focus_status
Definition unit.h:217
enum server_side_agent ssa_controller
Definition unit.h:175
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
#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:62
#define terrain_has_flag(terr, flag)
Definition terrain.h:176
bool tile_is_placing(const struct tile *ptile)
Definition tile.c:1131
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:170
#define tile_terrain(_tile)
Definition tile.h:115
#define tile_has_extra(ptile, pextra)
Definition tile.h:152
#define tile_owner(_tile)
Definition tile.h:97
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:421
size_t cargo_iter_sizeof(void)
Definition unit.c:2667
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1667
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2937
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:2019
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1567
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2303
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1652
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:232
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1846
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2685
bool can_unit_change_homecity_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pcity)
Definition unit.c:507
void free_unit_orders(struct unit *punit)
Definition unit.c:1832
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2353
bool unit_is_alive(int id)
Definition unit.c:2316
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:302
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:342
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:468
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:568
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2198
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:584
bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1519
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2473
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1985
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2727
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2114
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2654
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2291
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1677
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:767
bool unit_can_add_or_build_city(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:490
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1402
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2225
bool can_unit_change_homecity(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:523
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2675
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1300
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1581
int get_activity_rate(const struct unit *punit)
Definition unit.c:537
bool can_unit_deboard_or_be_unloaded(const struct civ_map *nmap, const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:819
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2582
bool is_attack_unit(const struct unit *punit)
Definition unit.c:323
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:204
const Activity_type_id tile_changing_activities[]
Definition unit.c:44
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2544
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2493
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1622
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2623
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2246
int unit_bribe_cost(const struct unit *punit, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2370
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:883
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1123
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1286
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:742
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2744
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1177
bool is_field_unit(const struct unit *punit)
Definition unit.c:456
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2719
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:401
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:411
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2956
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:334
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, enum gen_action action)
Definition unit.c:913
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1687
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2636
bool can_unit_do_autoworker(const struct unit *punit)
Definition unit.c:648
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Definition unit.c:2566
#define CARGO_ITER(iter)
Definition unit.c:54
bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:848
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2752
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:656
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1193
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:315
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1450
void set_unit_activity_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type *new_target, enum gen_action trigger_action)
Definition unit.c:1158
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:350
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1638
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:864
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1210
bool unit_can_help_build_wonder_here(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:276
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1386
bool is_special_unit(const struct unit *punit)
Definition unit.c:368
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1792
int stack_bribe_cost(const struct tile *ptile, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2458
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1346
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1310
void set_unit_activity(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1140
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:2051
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2528
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:2333
bool is_flagless_to_player(const struct unit *punit, const struct player *pplayer)
Definition unit.c:381
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:430
bool is_guard_unit(const struct unit *punit)
Definition unit.c:358
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:794
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1423
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2554
bool can_unit_do_activity_targeted_at(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, enum gen_action action, struct extra_type *target, const struct tile *ptile)
Definition unit.c:968
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1995
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2345
bool unit_has_orders(const struct unit *punit)
Definition unit.c:220
struct unit * tile_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1323
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1975
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:1856
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2098
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:608
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:710
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.c:1361
bool can_unit_do_activity_targeted(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, enum gen_action action, struct extra_type *target)
Definition unit.c:954
#define unit_tile(_pu)
Definition unit.h:407
#define unit_transports_iterate_end
Definition unit.h:578
#define unit_cargo_iterate_end
Definition unit.h:588
@ FOCUS_AVAIL
Definition unit.h:54
#define BATTLEGROUP_NONE
Definition unit.h:193
unit_orders
Definition unit.h:38
@ ORDER_ACTION_MOVE
Definition unit.h:46
@ ORDER_ACTIVITY
Definition unit.h:42
@ ORDER_FULL_MP
Definition unit.h:44
@ ORDER_MOVE
Definition unit.h:40
@ ORDER_LAST
Definition unit.h:50
@ ORDER_PERFORM_ACTION
Definition unit.h:48
#define unit_transported_server(_pcargo_)
Definition unit.h:556
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:585
#define unit_owner(_pu)
Definition unit.h:406
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:574
unit_upgrade_result
Definition unit.h:61
@ UU_NO_MONEY
Definition unit.h:64
@ UU_NOT_IN_CITY
Definition unit.h:65
@ UU_NO_UNITTYPE
Definition unit.h:63
@ UU_NOT_TERRAIN
Definition unit.h:68
@ UU_UNSUITABLE_TRANSPORT
Definition unit.h:69
@ UU_NOT_CITY_OWNER
Definition unit.h:66
@ UU_NOT_ENOUGH_ROOM
Definition unit.h:67
@ UU_OK
Definition unit.h:62
@ UU_NOT_ACTIVITY
Definition unit.h:70
#define unit_transported_client(_pcargo_)
Definition unit.h:559
unit_airlift_result
Definition unit.h:73
@ AR_SRC_NO_FLIGHTS
Definition unit.h:85
@ AR_OK_SRC_UNKNOWN
Definition unit.h:76
@ AR_OK_DST_UNKNOWN
Definition unit.h:77
@ AR_NO_MOVES
Definition unit.h:79
@ AR_BAD_DST_CITY
Definition unit.h:84
@ AR_NOT_IN_CITY
Definition unit.h:82
@ AR_OCCUPIED
Definition unit.h:81
@ AR_OK
Definition unit.h:75
@ AR_DST_NO_FLIGHTS
Definition unit.h:86
@ AR_WRONG_UNITTYPE
Definition unit.h:80
@ AR_BAD_SRC_CITY
Definition unit.h:83
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.h:443
#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:1385
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:304
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1573
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1253
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2969
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1591
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1472
bool utype_may_act_move_frags(const struct unit_type *punit_type, const action_id act_id, const int move_fragments)
Definition unittype.c:1062
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1488
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1707
bool utype_can_freely_load(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:292
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2629
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2503
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:391
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Definition unittype.c:2601
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:194
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1223
bool utype_can_do_action_sub_result(const struct unit_type *putype, enum action_sub_result sub_result)
Definition unittype.c:412
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1292
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:447
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1564
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:375
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1735
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:771
#define utype_class(_t_)
Definition unittype.h:754
#define utype_fuel(ptype)
Definition unittype.h:844
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:622