Freeciv-3.4
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 "specialist.h"
39#include "tech.h"
40#include "traderoutes.h"
41#include "unitlist.h"
42
43#include "unit.h"
44
49
55#define CARGO_ITER(iter) ((struct cargo_iter *) (iter))
56
57/**********************************************************************/
61 const struct unit_order *order2)
62{
63 return order1->order == order2->order
64 && order1->activity == order2->activity
65 && order1->target == order2->target
66 && order1->sub_target == order2->sub_target
67 && order1->action == order2->action
68 && order1->dir == order2->dir;
69}
70
71/**********************************************************************/
87 const struct player *restriction,
88 const struct unit *punit,
89 const struct city *pdest_city)
90{
91 const struct city *psrc_city = tile_city(unit_tile(punit));
92 const struct player *punit_owner;
93 const struct tile *dst_tile = nullptr;
94 const struct unit_type *putype = unit_type_get(punit);
97
98 if (0 == punit->moves_left
100 /* No moves left. */
101 return AR_NO_MOVES;
102 }
103
105 return AR_WRONG_UNITTYPE;
106 }
107
109 /* Units with occupants can't be airlifted currently. */
110 return AR_OCCUPIED;
111 }
112
113 if (nullptr == psrc_city) {
114 /* No city there. */
115 return AR_NOT_IN_CITY;
116 }
117
118 if (psrc_city == pdest_city) {
119 /* Airlifting to our current position doesn't make sense. */
120 return AR_BAD_DST_CITY;
121 }
122
123 if (nullptr != pdest_city) {
125
126 if (nullptr != restriction
129 /* Can't exist at the destination tile. */
130 return AR_BAD_DST_CITY;
131 }
132 }
133
135
136 /* Check validity of both source and destination before checking capacity,
137 * to avoid misleadingly optimistic returns. */
138
142 /* Not allowed to airlift from this source. */
143 return AR_BAD_SRC_CITY;
144 }
145
146 /* Check diplomatic possibility of the destination */
147 if (nullptr != pdest_city) {
151 || flagless
153 /* Not allowed to airlift to this destination. */
154 return AR_BAD_DST_CITY;
155 }
156 } else if (flagless
158 /* Foreign units block airlifting to this destination */
159 return AR_BAD_DST_CITY;
160 }
161 }
162
163 /* Check airlift capacities */
165 if (nullptr == restriction || city_owner(psrc_city) == restriction) {
166 /* We know for sure whether or not src can airlift this turn. */
167 if (0 >= psrc_city->airlift
169 /* The source cannot airlift for this turn (maybe already airlifted
170 * or no airport).
171 * See also do_airline() in server/unittools.h. */
172 return AR_SRC_NO_FLIGHTS;
173 } /* else, there is capacity; continue to other checks */
174 } else {
175 /* We don't have access to the 'airlift' field. Assume it's OK; can
176 * only find out for sure by trying it. */
178 }
179 }
180
181 if (nullptr != pdest_city && !game.info.airlift_to_always_enabled) {
182 if (nullptr == restriction || city_owner(pdest_city) == restriction) {
183 if (0 >= pdest_city->airlift
185 /* The destination cannot support airlifted units for this turn
186 * (maybe already airlifted or no airport).
187 * See also do_airline() in server/unittools.h. */
188 return AR_DST_NO_FLIGHTS;
189 } /* else continue */
190 } else {
192 }
193 }
194
195 return ok_result;
196}
197
198/**********************************************************************/
206 const struct unit *punit,
207 const struct city *pdest_city)
208{
209 if (is_server()) {
212 } else {
214 pdest_city));
215 }
216}
217
218/**********************************************************************/
221bool unit_has_orders(const struct unit *punit)
222{
223 return punit->has_orders;
224}
225
226/**********************************************************************/
233int unit_shield_value(const struct unit *punit,
234 const struct unit_type *punittype,
235 const struct action *paction)
236{
237 int value;
238
239 bool has_unit;
240 const struct player *act_player;
241
242 has_unit = punit != NULL;
243
244 if (has_unit && punittype == NULL) {
246 }
247
251
253 /* TODO: determine if tile and city should be where the unit currently is
254 * located or the target city. Those two may differ. Wait for ruleset
255 * author feed back. */
256
258 value += ((value
260 &(const struct req_context) {
261 .player = act_player,
262 .unit = punit,
263 .unittype = punittype,
264 .action = paction,
265 },
266 NULL,
268 / 100);
269
270 return value;
271}
272
273/**********************************************************************/
278 const struct unit *punit)
279{
280 struct city *pcity = tile_city(unit_tile(punit));
281
282 if (pcity == NULL) {
283 /* No city to help at this tile. */
284 return FALSE;
285 }
286
288 /* This unit can never do help wonder. */
289 return FALSE;
290 }
291
292 /* Evaluate all action enablers for extra accuracy. */
293 /* TODO: Is it worth it? */
296 pcity));
297}
298
299/**********************************************************************/
312
313/**********************************************************************/
317{
319}
320
321/**********************************************************************/
331
332/**********************************************************************/
335bool is_martial_law_unit(const struct unit *punit)
336{
338}
339
340/**********************************************************************/
343bool is_occupying_unit(const struct unit *punit)
344{
346}
347
348/**********************************************************************/
352{
354}
355
356/**********************************************************************/
359bool is_guard_unit(const struct unit *punit)
360{
362}
363
364/**********************************************************************/
369bool is_special_unit(const struct unit *punit)
370{
372}
373
374/**********************************************************************/
383 const struct player *pplayer)
384{
385 if (unit_owner(punit) != pplayer
387 if (pplayer == nullptr) {
388 /* Global observer always sees the flags */
389 return FALSE;
390 }
391
392 return TRUE;
393 }
394
395 return FALSE;
396}
397
398/**********************************************************************/
402bool unit_can_do_action(const struct unit *punit,
403 const action_id act_id)
404{
405 return utype_can_do_action(unit_type_get(punit), act_id);
406}
407
408/**********************************************************************/
413 enum action_result result)
414{
416}
417
418/**********************************************************************/
427
428/**********************************************************************/
432 const struct player *pplayer,
433 const struct tile *ptile, bool omniscient)
434{
435 square_iterate(nmap, ptile, 2, ptile1) {
437 if ((omniscient
438 || can_player_see_unit(pplayer, punit))
439 && pplayers_at_war(pplayer, unit_owner(punit))
444 unit_class_get(punit), ptile)))) {
445 return TRUE;
446 }
449
450 return FALSE;
451}
452
453/**********************************************************************/
457bool is_field_unit(const struct unit *punit)
458{
460}
461
462/**********************************************************************/
469bool is_hiding_unit(const struct unit *punit)
470{
472
473 if (vl == V_INVIS || vl == V_SUBSURFACE) {
474 return TRUE;
475 }
476
477 if (unit_transported(punit)) {
479 if (vl == V_INVIS || vl == V_SUBSURFACE) {
480 return TRUE;
481 }
482 }
483
484 return FALSE;
485}
486
487/**********************************************************************/
492 const struct unit *punit)
493{
494 struct city *tgt_city;
495
496 if ((tgt_city = tile_city(unit_tile(punit)))) {
499 } else {
502 }
503}
504
505/**********************************************************************/
509 const struct unit *punit,
510 const struct city *pcity)
511{
512 if (pcity == NULL) {
513 /* Can't change home city to a non existing city. */
514 return FALSE;
515 }
516
518 pcity));
519}
520
521/**********************************************************************/
525 const struct unit *punit)
526{
528}
529
530/**********************************************************************/
538int get_activity_rate(const struct unit *punit)
539{
540 const struct veteran_level *vlevel;
541 const struct unit_type *ptype;
542 int move_rate;
543
545
549
550 /* The speed of the settler depends on its base move_rate, not on
551 * the number of moves actually remaining or the adjusted move rate.
552 * This means sea formers won't have their activity rate increased by
553 * Magellan's, and it means injured units work just as fast as
554 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
555 move_rate = ptype->move_rate;
556
557 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
558 return ACTIVITY_FACTOR
559 * (float)vlevel->power_fact / 100
561}
562
563/**********************************************************************/
570{
571 /* This logic is also coded in client/goto.c. */
572 if (punit->moves_left > 0) {
573 return get_activity_rate(punit);
574 } else {
575 return 0;
576 }
577}
578
579/**********************************************************************/
586 enum unit_activity activity,
587 const struct tile *ptile,
588 struct extra_type *tgt)
589{
590 /* FIXME: This is just an approximation since we don't account for
591 * get_activity_rate_this_turn. */
593 int points_needed;
594
595 fc_assert(tgt != NULL || !is_targeted_activity(activity));
596
597 points_needed = tile_activity_time(activity, ptile, tgt);
598
599 if (points_needed >= 0 && speed > 0) {
600 return (points_needed - 1) / speed + 1; /* Round up */
601 } else {
602 return FC_INFINITY;
603 }
604}
605
606/**********************************************************************/
610{
611 switch (activity) {
612 case ACTIVITY_PILLAGE:
613 case ACTIVITY_BASE:
616 case ACTIVITY_MINE:
617 case ACTIVITY_CLEAN:
618 return TRUE;
619 case ACTIVITY_IDLE:
621 case ACTIVITY_SENTRY:
622 case ACTIVITY_GOTO:
623 case ACTIVITY_EXPLORE:
626 case ACTIVITY_PLANT:
628 case ACTIVITY_CONVERT:
629 return FALSE;
630 /* These shouldn't be kicking around internally. */
631 case ACTIVITY_LAST:
632 break;
633 }
634
636
637 return FALSE;
638}
639
640/**********************************************************************/
650{
651 return unit_type_get(punit)->adv.worker;
652}
653
654/**********************************************************************/
657const char *get_activity_text(enum unit_activity activity)
658{
659 /* The switch statement has just the activities listed with no "default"
660 * handling. This enables the compiler to detect missing entries
661 * automatically, and still handles everything correctly. */
662 switch (activity) {
663 case ACTIVITY_IDLE:
664 return _("Idle");
665 case ACTIVITY_CLEAN:
666 return _("Clean");
667 case ACTIVITY_MINE:
668 /* TRANS: Activity name, verb in English */
669 return Q_("?act:Mine");
670 case ACTIVITY_PLANT:
671 /* TRANS: Activity name, verb in English */
672 return _("Plant");
674 return _("Irrigate");
676 return _("Cultivate");
678 return _("Fortifying");
680 return _("Fortified");
681 case ACTIVITY_SENTRY:
682 return _("Sentry");
683 case ACTIVITY_PILLAGE:
684 return _("Pillage");
685 case ACTIVITY_GOTO:
686 return _("Goto");
687 case ACTIVITY_EXPLORE:
688 return _("Explore");
690 return _("Transform");
691 case ACTIVITY_BASE:
692 return _("Base");
694 return _("Road");
695 case ACTIVITY_CONVERT:
696 return _("Convert");
697 case ACTIVITY_LAST:
698 break;
699 }
700
702 return _("Unknown");
703}
704
705/**********************************************************************/
712 const struct unit *ptrans)
713{
714 /* Make sure this transporter can carry this type of unit. */
716 return FALSE;
717 }
718
719 /* Make sure there's room in the transporter. */
722 return FALSE;
723 }
724
725 /* Check iff this is a valid transport. */
727 return FALSE;
728 }
729
730 /* Check transport depth. */
733 return FALSE;
734 }
735
736 return TRUE;
737}
738
739/**********************************************************************/
743bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
744{
745 if (!pcargo || !ptrans || pcargo == ptrans) {
746 return FALSE;
747 }
748
749 /* Double-check ownership of the units: you can load into an allied unit
750 * (of course only allied units can be on the same tile). */
752 return FALSE;
753 }
754
755 /* Un-embarkable transport must be in city or base to load cargo. */
759 return FALSE;
760 }
761
763}
764
765/**********************************************************************/
768bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
769{
770 /* This function needs to check EVERYTHING. */
771
772 /* Check positions of the units. Of course you can't load a unit onto
773 * a transporter on a different tile... */
775 return FALSE;
776 }
777
778 /* Cannot load if cargo is already loaded onto something else. */
780 return FALSE;
781 }
782
784}
785
786/**********************************************************************/
795bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
796{
797 if (!pcargo || !ptrans) {
798 return FALSE;
799 }
800
801 /* Make sure the unit's transporter exists and is known. */
803 return FALSE;
804 }
805
806 /* Un-disembarkable transport must be in city or base to unload cargo. */
810 return FALSE;
811 }
812
813 return TRUE;
814}
815
816/**********************************************************************/
843
844/**********************************************************************/
849bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
850{
853 return TRUE;
854 }
856
857 return FALSE;
858}
859
860/**********************************************************************/
880
881/**********************************************************************/
885 struct unit *punit)
886{
887 enum unit_activity current = punit->activity;
888 struct extra_type *target = punit->activity_target;
890 = (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
892 bool result;
893
896
898 target);
899
900 punit->activity = current;
901 punit->activity_target = target;
903
904 return result;
905}
906
907/**********************************************************************/
915 const struct unit *punit,
916 enum unit_activity activity,
917 enum gen_action action)
918{
919 struct extra_type *target = NULL;
920
921 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
922 * being able to find out whether an activity is in general possible.
923 * Find one for them, but when they come to do the activity, they will
924 * have to determine the target themselves */
925 {
926 struct tile *ptile = unit_tile(punit);
927
928 if (activity == ACTIVITY_IRRIGATE) {
929 target = next_extra_for_tile(ptile,
932 punit);
933 if (NULL == target) {
934 return FALSE; /* No more irrigation extras available. */
935 }
936 } else if (activity == ACTIVITY_MINE) {
937 target = next_extra_for_tile(ptile,
938 EC_MINE,
940 punit);
941 if (NULL == target) {
942 return FALSE; /* No more mine extras available. */
943 }
944 }
945 }
946
948 target);
949}
950
951/**********************************************************************/
956 const struct unit *punit,
957 enum unit_activity activity,
958 enum gen_action action,
959 struct extra_type *target)
960{
962 target, unit_tile(punit));
963}
964
965/**********************************************************************/
970 const struct unit *punit,
971 enum unit_activity activity,
972 enum gen_action action,
973 struct extra_type *target,
974 const struct tile *ptile)
975{
976 /* Check that no build activity conflicting with one already in progress
977 * gets executed. */
978 /* FIXME: Should check also the cases where one of the activities is terrain
979 * change that destroys the target of the other activity */
980 if (target != NULL && is_build_activity(activity)) {
981 if (tile_is_placing(ptile)) {
982 return FALSE;
983 }
984
985 unit_list_iterate(ptile->units, tunit) {
986 if (is_build_activity(tunit->activity)
987 && !can_extras_coexist(target, tunit->activity_target)) {
988 return FALSE;
989 }
991 }
992
993#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
994{ \
995 switch (action_get_target_kind(paction)) { \
996 case ATK_TILE: \
997 return is_action_enabled_unit_on_tile(nmap, paction->id, \
998 punit, ptile, target); \
999 case ATK_EXTRAS: \
1000 return is_action_enabled_unit_on_extras(nmap, paction->id, \
1001 punit, ptile, target); \
1002 case ATK_CITY: \
1003 case ATK_UNIT: \
1004 case ATK_STACK: \
1005 case ATK_SELF: \
1006 return FALSE; \
1007 case ATK_COUNT: \
1008 break; /* Handle outside switch */ \
1009 } \
1010 fc_assert(action_target_kind_is_valid( \
1011 action_get_target_kind(paction))); \
1012 return FALSE; \
1013 }
1014
1015 switch (activity) {
1016 case ACTIVITY_IDLE:
1017 case ACTIVITY_GOTO:
1018 return TRUE;
1019
1020 case ACTIVITY_CLEAN:
1021 /* The call below doesn't support actor tile speculation. */
1022 fc_assert_msg(unit_tile(punit) == ptile,
1023 "Please use action_speculate_unit_on_tile()");
1025 punit, ptile, target);
1026
1027 case ACTIVITY_MINE:
1028 /* The call below doesn't support actor tile speculation. */
1029 fc_assert_msg(unit_tile(punit) == ptile,
1030 "Please use action_speculate_unit_on_tile()");
1032 ptile, target);
1033
1034 case ACTIVITY_PLANT:
1035 /* The call below doesn't support actor tile speculation. */
1036 fc_assert_msg(unit_tile(punit) == ptile,
1037 "Please use action_speculate_unit_on_tile()");
1039 punit, ptile, NULL);
1040
1041 case ACTIVITY_IRRIGATE:
1042 /* The call below doesn't support actor tile speculation. */
1043 fc_assert_msg(unit_tile(punit) == ptile,
1044 "Please use action_speculate_unit_on_tile()");
1046 ptile, target);
1047
1048 case ACTIVITY_CULTIVATE:
1049 /* The call below doesn't support actor tile speculation. */
1050 fc_assert_msg(unit_tile(punit) == ptile,
1051 "Please use action_speculate_unit_on_tile()");
1053 punit, ptile, NULL);
1054
1056 /* The call below doesn't support actor tile speculation. */
1057 fc_assert_msg(unit_tile(punit) == ptile,
1058 "Please use action_speculate_unit_on_self()");
1060 punit);
1061
1062 case ACTIVITY_FORTIFIED:
1063 return FALSE;
1064
1065 case ACTIVITY_BASE:
1066 /* The call below doesn't support actor tile speculation. */
1067 fc_assert_msg(unit_tile(punit) == ptile,
1068 "Please use action_speculate_unit_on_tile()");
1070 punit, ptile, target);
1071
1072 case ACTIVITY_GEN_ROAD:
1073 /* The call below doesn't support actor tile speculation. */
1074 fc_assert_msg(unit_tile(punit) == ptile,
1075 "Please use action_speculate_unit_on_tile()");
1077 punit, ptile, target);
1078
1079 case ACTIVITY_SENTRY:
1081 && !unit_transported(punit)) {
1082 /* Don't let units sentry on tiles they will die on. */
1083 return FALSE;
1084 }
1085 return TRUE;
1086
1087 case ACTIVITY_PILLAGE:
1088 /* The call below doesn't support actor tile speculation. */
1089 fc_assert_msg(unit_tile(punit) == ptile,
1090 "Please use action_speculate_unit_on_tile()");
1092
1093 case ACTIVITY_EXPLORE:
1094 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1095
1096 case ACTIVITY_TRANSFORM:
1097 /* The call below doesn't support actor tile speculation. */
1098 fc_assert_msg(unit_tile(punit) == ptile,
1099 "Please use action_speculate_unit_on_tile()");
1101 punit, ptile, NULL);
1102
1103 case ACTIVITY_CONVERT:
1104 /* The call below doesn't support actor tile speculation. */
1105 fc_assert_msg(unit_tile(punit) == ptile,
1106 "Please use action_speculate_unit_on_self()");
1108
1109 case ACTIVITY_LAST:
1110 break;
1111 }
1112
1113 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1114 activity);
1115
1116 return FALSE;
1117
1118#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1119}
1120
1121/**********************************************************************/
1127{
1130 punit->activity_count = 0;
1132 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1133 /* No longer done. */
1135 }
1136}
1137
1138/**********************************************************************/
1155
1156/**********************************************************************/
1174
1175/**********************************************************************/
1179 const struct tile *ptile)
1180{
1181 unit_list_iterate(ptile->units, punit) {
1182 if (punit->activity == activity) {
1183 return TRUE;
1184 }
1186
1187 return FALSE;
1188}
1189
1190/**********************************************************************/
1195{
1197
1199 unit_list_iterate(ptile->units, punit) {
1202 }
1204
1205 return tgt_ret;
1206}
1207
1208/**********************************************************************/
1211void unit_activity_astr(const struct unit *punit, struct astring *astr)
1212{
1213 if (!punit || !astr) {
1214 return;
1215 }
1216
1217 switch (punit->activity) {
1218 case ACTIVITY_IDLE:
1220 int rate, f;
1221
1223 f = ((punit->fuel) - 1);
1224
1225 /* Add in two parts as move_points_text() returns ptr to static
1226 * End result: "Moves: (fuel)moves_left" */
1227 astr_add_line(astr, "%s: (%s)", _("Moves"),
1229 astr_add(astr, "%s",
1231 } else {
1232 astr_add_line(astr, "%s: %s", _("Moves"),
1234 }
1235 return;
1236 case ACTIVITY_CLEAN:
1237 case ACTIVITY_TRANSFORM:
1239 case ACTIVITY_FORTIFIED:
1240 case ACTIVITY_SENTRY:
1241 case ACTIVITY_GOTO:
1242 case ACTIVITY_EXPLORE:
1243 case ACTIVITY_CONVERT:
1244 case ACTIVITY_CULTIVATE:
1245 case ACTIVITY_PLANT:
1247 return;
1248 case ACTIVITY_MINE:
1249 case ACTIVITY_IRRIGATE:
1250 if (punit->activity_target == NULL) {
1252 } else {
1253 astr_add_line(astr, "Building %s",
1255 }
1256 return;
1257 case ACTIVITY_PILLAGE:
1258 if (punit->activity_target != NULL) {
1261 } else {
1263 }
1264 return;
1265 case ACTIVITY_BASE:
1268 return;
1269 case ACTIVITY_GEN_ROAD:
1272 return;
1273 case ACTIVITY_LAST:
1274 break;
1275 }
1276
1277 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1279}
1280
1281/**********************************************************************/
1287void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1288{
1289 if (!punit || !astr) {
1290 return;
1291 }
1292
1293 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1295 punit->upkeep[O_GOLD]);
1296}
1297
1298/**********************************************************************/
1301struct player *unit_nationality(const struct unit *punit)
1302{
1304 return punit->nationality;
1305}
1306
1307/**********************************************************************/
1311void unit_tile_set(struct unit *punit, struct tile *ptile)
1312{
1314 punit->tile = ptile;
1315}
1316
1317/**********************************************************************/
1324struct unit *tile_allied_unit(const struct tile *ptile,
1325 const struct player *pplayer)
1326{
1327 struct unit *punit = NULL;
1328
1329 unit_list_iterate(ptile->units, cunit) {
1330 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1331 punit = cunit;
1332 } else {
1333 return NULL;
1334 }
1335 }
1337
1338 return punit;
1339}
1340
1341/**********************************************************************/
1347struct unit *tile_enemy_unit(const struct tile *ptile,
1348 const struct player *pplayer)
1349{
1350 unit_list_iterate(ptile->units, punit) {
1351 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1352 return punit;
1353 }
1355
1356 return NULL;
1357}
1358
1359/**********************************************************************/
1362struct unit *tile_non_allied_unit(const struct tile *ptile,
1363 const struct player *pplayer,
1365{
1366 unit_list_iterate(ptile->units, punit) {
1367 struct player *owner = unit_owner(punit);
1368
1369 if (everyone_non_allied && owner != pplayer) {
1370 return punit;
1371 }
1372
1373 if (!pplayers_allied(owner, pplayer)
1374 || is_flagless_to_player(punit, pplayer)) {
1375 return punit;
1376 }
1377 }
1379
1380 return NULL;
1381}
1382
1383/**********************************************************************/
1387struct unit *tile_other_players_unit(const struct tile *ptile,
1388 const struct player *pplayer)
1389{
1390 unit_list_iterate(ptile->units, punit) {
1391 if (unit_owner(punit) != pplayer) {
1392 return punit;
1393 }
1395
1396 return NULL;
1397}
1398
1399/**********************************************************************/
1403struct unit *tile_non_attack_unit(const struct tile *ptile,
1404 const struct player *pplayer)
1405{
1406 unit_list_iterate(ptile->units, punit) {
1407 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1408 return punit;
1409 }
1410 }
1412
1413 return NULL;
1414}
1415
1416/**********************************************************************/
1424struct unit *unit_occupies_tile(const struct tile *ptile,
1425 const struct player *pplayer)
1426{
1427 unit_list_iterate(ptile->units, punit) {
1428 if (!is_occupying_unit(punit)) {
1429 continue;
1430 }
1431
1433 continue;
1434 }
1435
1436 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1437 return punit;
1438 }
1440
1441 return NULL;
1442}
1443
1444/**********************************************************************/
1451bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1452 const struct civ_map *zmap)
1453{
1455
1456 square_iterate(zmap, ptile0, 1, ptile) {
1457 struct terrain *pterrain;
1458 struct city *pcity;
1459
1460 pterrain = tile_terrain(ptile);
1461 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1462 continue;
1463 }
1464
1465 pcity = tile_non_allied_city(ptile, pplayer);
1466 if (pcity != NULL) {
1467 if (unit_list_size(ptile->units) > 0) {
1468 /* Occupied enemy city, it doesn't matter if units inside have
1469 * UTYF_NOZOC or not. */
1470 return FALSE;
1471 }
1472 } else {
1473 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1475 if (tile_has_extra(ptile, pextra)) {
1476 return FALSE;
1477 }
1479 }
1480
1481 unit_list_iterate(ptile->units, punit) {
1482 if (!pplayers_allied(unit_owner(punit), pplayer)
1485 bool hidden = FALSE;
1486
1487 /* We do NOT check the possibility that player is allied with an extra owner,
1488 * and should thus see inside the extra.
1489 * This is to avoid the situation where having an alliance with third player
1490 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1491 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1492 if (tile_has_extra(ptile, pextra)) {
1493 hidden = TRUE;
1494 break;
1495 }
1497
1498 if (!hidden) {
1499 return FALSE;
1500 }
1501 }
1503 }
1505
1506 return TRUE;
1507}
1508
1509/**********************************************************************/
1520bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1521 const struct civ_map *zmap)
1522{
1524
1525 square_iterate(zmap, ptile0, 1, ptile) {
1526 struct terrain *pterrain;
1527 struct city *pcity;
1528
1529 pterrain = tile_terrain(ptile);
1530 if (T_UNKNOWN == pterrain
1531 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1532 continue;
1533 }
1534
1535 pcity = tile_non_allied_city(ptile, pplayer);
1536 if (pcity != NULL) {
1537 if (pcity->client.occupied
1538 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1539 /* Occupied enemy city, it doesn't matter if units inside have
1540 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1541 return FALSE;
1542 }
1543 } else {
1544 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1546 if (tile_has_extra(ptile, pextra)) {
1547 return FALSE;
1548 }
1550 }
1551
1552 unit_list_iterate(ptile->units, punit) {
1554 && !pplayers_allied(unit_owner(punit), pplayer)
1556 return FALSE;
1557 }
1559 }
1561
1562 return TRUE;
1563}
1564
1565/**********************************************************************/
1573
1574/**********************************************************************/
1583 const struct unit *punit)
1584{
1585 const struct tile *ptile = unit_tile(punit);
1587
1588 if (!is_attack_unit(punit)) {
1589 return FALSE;
1590 }
1591 if (tile_city(ptile)) {
1592 return FALSE;
1593 }
1595 switch (game.info.happyborders) {
1596 case HB_DISABLED:
1597 break;
1598 case HB_NATIONAL:
1599 if (tile_owner(ptile) == unit_owner(punit)) {
1600 return FALSE;
1601 }
1602 break;
1603 case HB_ALLIANCE:
1605 return FALSE;
1606 }
1607 break;
1608 }
1609 }
1610
1613 if (max_friendliness_range >= 0) {
1615 }
1616
1617 return TRUE;
1618}
1619
1620/**********************************************************************/
1624{
1625 switch (activity) {
1626 case ACTIVITY_MINE:
1627 case ACTIVITY_IRRIGATE:
1628 case ACTIVITY_BASE:
1629 case ACTIVITY_GEN_ROAD:
1630 return TRUE;
1631 default:
1632 return FALSE;
1633 }
1634}
1635
1636/**********************************************************************/
1640{
1641 switch (activity) {
1642 case ACTIVITY_PILLAGE:
1643 case ACTIVITY_CLEAN:
1644 return TRUE;
1645 default:
1646 return FALSE;
1647 }
1648}
1649
1650/**********************************************************************/
1654{
1655 switch (activity) {
1656 case ACTIVITY_CULTIVATE:
1657 case ACTIVITY_PLANT:
1658 case ACTIVITY_TRANSFORM:
1659 return TRUE;
1660 default:
1661 return FALSE;
1662 }
1663}
1664
1665/**********************************************************************/
1669{
1670 return is_build_activity(activity)
1671 || is_clean_activity(activity)
1672 || is_terrain_change_activity(activity);
1673}
1674
1675/**********************************************************************/
1679{
1680 return is_build_activity(activity)
1681 || is_clean_activity(activity);
1682}
1683
1684/**********************************************************************/
1688struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1689 const struct unit_type *punittype,
1690 int veteran_level)
1691{
1692 /* Make sure that contents of unit structure are correctly initialized,
1693 * if you ever allocate it by some other mean than fc_calloc() */
1694 struct unit *punit = fc_calloc(1, sizeof(*punit));
1695 int max_vet_lvl;
1696
1697 /* It does not register the unit so the id is set to 0. */
1699
1700 fc_assert_ret_val(punittype != nullptr, nullptr); /* No untyped units! */
1702
1703 fc_assert_ret_val(!is_server() || pplayer != nullptr, nullptr); /* No unowned units! */
1704 punit->owner = pplayer;
1705 punit->nationality = pplayer;
1706
1707 punit->refcount = 1;
1709
1710 if (pcity != nullptr) {
1712 punit->homecity = pcity->id;
1713 } else {
1714 unit_tile_set(punit, nullptr);
1716 }
1717
1718 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1719 punit->goto_tile = nullptr;
1722 /* A unit new and fresh ... */
1726 /* Random moves units start with zero movement as their first movement
1727 * will be only after their moves have been reset in the beginning of
1728 * the next turn. */
1729 punit->moves_left = 0;
1730 } else {
1732 }
1733 punit->moved = FALSE;
1734
1738
1739 punit->transporter = nullptr;
1741
1742 punit->carrying = nullptr;
1743
1747
1749 punit->action_decision_tile = nullptr;
1750
1751 punit->stay = FALSE;
1752
1755
1756 if (is_server()) {
1758
1760
1761 punit->server.removal_callback = nullptr;
1762
1764 O_LAST * sizeof(*punit->server.upkeep_paid));
1765
1766 punit->server.ord_map = 0;
1767 punit->server.ord_city = 0;
1768
1769 punit->server.vision = nullptr; /* No vision. */
1771 /* Must be an invalid turn number, and an invalid previous turn
1772 * number. */
1773 punit->server.action_turn = -2;
1774 /* punit->server.moving = NULL; set by fc_calloc(). */
1775
1776 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1777
1778 CALL_FUNC_EACH_AI(unit_alloc, punit);
1779 } else {
1783 punit->client.act_prob_cache = nullptr;
1784 }
1785
1786 return punit;
1787}
1788
1789/**********************************************************************/
1794{
1796
1797 /* Unload unit if transported. */
1800
1801 /* Check for transported units. Use direct access to the list. */
1802 if (unit_list_size(punit->transporting) != 0) {
1803 /* Unload all units. */
1807 }
1809
1810 if (punit->transporting) {
1812 }
1813
1814 CALL_FUNC_EACH_AI(unit_free, punit);
1815
1816 if (is_server() && punit->server.adv) {
1818 } else {
1821 }
1822 }
1823
1824 if (--punit->refcount <= 0) {
1825 FC_FREE(punit);
1826 }
1827}
1828
1829/**********************************************************************/
1834{
1835 if (punit->has_orders) {
1836 punit->goto_tile = NULL;
1838 punit->orders.list = NULL;
1839 }
1840 punit->orders.length = 0;
1842}
1843
1844/**********************************************************************/
1848{
1850
1851 return unit_list_size(ptrans->transporting);
1852}
1853
1854/**********************************************************************/
1857static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1858 const struct tile *ptile,
1859 bool (*unit_load_test)
1860 (const struct unit *pc,
1861 const struct unit *pt))
1862{
1863 struct unit *best_trans = NULL;
1864 struct {
1867 } cur, best = { FALSE };
1868
1869 unit_list_iterate(ptile->units, ptrans) {
1870 if (!unit_load_test(pcargo, ptrans)) {
1871 continue;
1872 } else if (best_trans == NULL) {
1874 }
1875
1876 /* Gather data from transport stack in a single pass, for use in
1877 * various conditions below. */
1879 cur.outermost_moves_left = ptrans->moves_left;
1880 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1883 cur.has_orders = TRUE;
1884 }
1885 cur.outermost_moves_left = ptranstrans->moves_left;
1886 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1888
1889 /* Criteria for deciding the 'best' transport to load onto.
1890 * The following tests are applied in order; earlier ones have
1891 * lexicographically greater significance than later ones. */
1892
1893 /* Transports which have orders, or are on transports with orders,
1894 * are less preferable to transport stacks without orders (to
1895 * avoid loading on units that are just passing through). */
1896 if (best_trans != ptrans) {
1897 if (!cur.has_orders && best.has_orders) {
1899 } else if (cur.has_orders && !best.has_orders) {
1900 continue;
1901 }
1902 }
1903
1904 /* Else, transports which are idle are preferable (giving players
1905 * some control over loading) -- this does not check transports
1906 * of transports. */
1907 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1908 if (best_trans != ptrans) {
1909 if (cur.is_idle && !best.is_idle) {
1911 } else if (!cur.is_idle && best.is_idle) {
1912 continue;
1913 }
1914 }
1915
1916 /* Else, transports from which the cargo could unload at any time
1917 * are preferable to those where the cargo can only disembark in
1918 * cities/bases. */
1919 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1921 if (best_trans != ptrans) {
1922 if (cur.can_freely_unload && !best.can_freely_unload) {
1924 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1925 continue;
1926 }
1927 }
1928
1929 /* Else, transports which are less deeply nested are preferable. */
1931 if (best_trans != ptrans) {
1932 if (cur.depth < best.depth) {
1934 } else if (cur.depth > best.depth) {
1935 continue;
1936 }
1937 }
1938
1939 /* Else, transport stacks where the outermost transport has more
1940 * moves left are preferable (on the assumption that it's the
1941 * outermost transport that's about to move). */
1942 if (best_trans != ptrans) {
1943 if (cur.outermost_moves_left > best.outermost_moves_left) {
1945 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1946 continue;
1947 }
1948 }
1949
1950 /* All other things being equal, as a tie-breaker, compare the total
1951 * moves left (this turn) and move rate (future turns) for the whole
1952 * stack, to take into account total potential movement for both
1953 * short and long journeys (we don't know which the cargo intends to
1954 * make). Doesn't try to account for whether transports can unload,
1955 * etc. */
1956 if (best_trans != ptrans) {
1957 if (cur.total_moves > best.total_moves) {
1959 } else {
1960 continue;
1961 }
1962 }
1963
1965 best = cur;
1967
1968 return best_trans;
1969}
1970
1971/**********************************************************************/
1980
1981/**********************************************************************/
1987 const struct tile *ptile)
1988{
1990}
1991
1992/**********************************************************************/
1997 const struct unit *punit)
1998{
2000 return FALSE;
2001 }
2002
2005 return FALSE;
2006 }
2008
2009 return TRUE;
2010}
2011
2012/**********************************************************************/
2021 const struct unit *punit,
2022 const struct unit_type *to_unittype)
2023{
2025
2027 return UU_NOT_ENOUGH_ROOM;
2028 }
2029
2030 if (punit->transporter != NULL) {
2034 }
2036 /* The new unit type can't survive on this terrain. */
2037 return UU_NOT_TERRAIN;
2038 }
2039
2040 return UU_OK;
2041}
2042
2043/**********************************************************************/
2053 const struct unit *punit,
2054 bool is_free)
2055{
2056 struct player *pplayer = unit_owner(punit);
2057 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2059 struct city *pcity;
2060 int cost;
2061
2062 if (!to_unittype) {
2063 return UU_NO_UNITTYPE;
2064 }
2065
2067 /* TODO: There may be other activities that the upgraded unit is not
2068 allowed to do, which we could also test.
2069 -
2070 If convert were legal for new unit_type we could allow, but then it
2071 has a 'head start' getting activity time from the old conversion. */
2072 return UU_NOT_ACTIVITY;
2073 }
2074
2075 if (!is_free) {
2077 if (pplayer->economic.gold < cost) {
2078 return UU_NO_MONEY;
2079 }
2080
2082 if (!pcity) {
2083 return UU_NOT_IN_CITY;
2084 }
2085 if (city_owner(pcity) != pplayer) {
2086 /* TODO: Should upgrades in allied cities be possible? */
2087 return UU_NOT_CITY_OWNER;
2088 }
2089 }
2090
2091 /* TODO: Allow transported units to be reassigned. Check here
2092 * and make changes to upgrade_unit. */
2094}
2095
2096/**********************************************************************/
2099bool unit_can_convert(const struct civ_map *nmap,
2100 const struct unit *punit)
2101{
2102 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2103
2104 if (tgt == NULL) {
2105 return FALSE;
2106 }
2107
2108 return UU_OK == unit_transform_result(nmap, punit, tgt);
2109}
2110
2111/**********************************************************************/
2116 const struct unit *punit,
2117 char *buf, size_t bufsz)
2118{
2119 struct player *pplayer = unit_owner(punit);
2121 int upgrade_cost;
2122 const struct unit_type *from_unittype = unit_type_get(punit);
2123 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2125 char tbuf[MAX_LEN_MSG];
2126
2127 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2128 "Treasury contains %d gold.",
2129 pplayer->economic.gold),
2130 pplayer->economic.gold);
2131
2132 switch (result) {
2133 case UU_OK:
2135 /* This message is targeted toward the GUI callers. */
2136 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2137 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2138 "Upgrade %s to %s for %d gold?\n%s",
2139 upgrade_cost),
2143 break;
2144 case UU_NO_UNITTYPE:
2146 _("Sorry, cannot upgrade %s (yet)."),
2148 break;
2149 case UU_NO_MONEY:
2151 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2152 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2153 "Upgrading %s to %s costs %d gold.\n%s",
2154 upgrade_cost),
2158 break;
2159 case UU_NOT_IN_CITY:
2160 case UU_NOT_CITY_OWNER:
2162 _("You can only upgrade units in your cities."));
2163 break;
2164 case UU_NOT_ENOUGH_ROOM:
2166 _("Upgrading this %s would strand units it transports."),
2168 break;
2169 case UU_NOT_TERRAIN:
2171 _("Upgrading this %s would result in a %s which can not "
2172 "survive at this place."),
2175 break;
2178 _("Upgrading this %s would result in a %s which its "
2179 "current transport, %s, could not transport."),
2183 break;
2184 case UU_NOT_ACTIVITY:
2186 _("Cannot upgrade %s while doing '%s'."),
2189 break;
2190 }
2191
2192 return result;
2193}
2194
2195/**********************************************************************/
2200 const struct unit *punit)
2201{
2202 int mpco;
2203
2205 &(const struct req_context) {
2206 .player = unit_owner(punit),
2207 .city = unit_tile(punit)
2209 : NULL,
2210 .tile = unit_tile(punit),
2211 .unit = punit,
2212 .unittype = unit_type_get(punit),
2213 .action = paction,
2214 },
2215 NULL,
2217
2219
2220 return mpco;
2221}
2222
2223/**********************************************************************/
2226int hp_gain_coord(const struct unit *punit)
2227{
2228 int hp = 0;
2229 const int base = unit_type_get(punit)->hp;
2230 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2231
2232 /* Includes barracks (100%), fortress (25%), etc. */
2234
2235 /* Minimum HP after regen effects applied. */
2236 hp = MAX(hp, min);
2237
2238 /* Regen2 effects that apply after there's at least Min HP */
2239 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2240
2241 return MAX(hp, 0);
2242}
2243
2244/**********************************************************************/
2248{
2249 const struct unit_type *utype = unit_type_get(punit);
2251 struct city *pcity = tile_city(unit_tile(punit));
2252 int gain;
2253
2254 if (!punit->moved) {
2256 } else {
2257 gain = 0;
2258 }
2259
2261
2262 if (!punit->homecity && 0 < game.server.killunhomed
2264 /* Hit point loss of units without homecity; at least 1 hp! */
2265 /* Gameloss units are immune to this effect. */
2266 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2267
2268 if (gain > hp_loss) {
2269 gain = -1;
2270 } else {
2271 gain -= hp_loss;
2272 }
2273 }
2274
2275 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2276 && !unit_transported(punit)) {
2277 gain -= utype->hp * pclass->hp_loss_pct / 100;
2278 }
2279
2280 if (punit->hp + gain > utype->hp) {
2281 gain = utype->hp - punit->hp;
2282 } else if (punit->hp + gain < 0) {
2283 gain = -punit->hp;
2284 }
2285
2286 return gain;
2287}
2288
2289/**********************************************************************/
2292bool is_losing_hp(const struct unit *punit)
2293{
2294 const struct unit_type *punittype = unit_type_get(punit);
2295
2297 < (punittype->hp *
2298 utype_class(punittype)->hp_loss_pct / 100);
2299}
2300
2301/**********************************************************************/
2304bool unit_type_is_losing_hp(const struct player *pplayer,
2305 const struct unit_type *punittype)
2306{
2307 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2309 < (punittype->hp *
2310 utype_class(punittype)->hp_loss_pct / 100);
2311}
2312
2313/**********************************************************************/
2317bool unit_is_alive(int id)
2318{
2319 /* Check if unit exist in game */
2320 if (game_unit_by_number(id)) {
2321 return TRUE;
2322 }
2323
2324 return FALSE;
2325}
2326
2327/**********************************************************************/
2334bool unit_is_virtual(const struct unit *punit)
2335{
2336 if (!punit) {
2337 return FALSE;
2338 }
2339
2340 return punit != game_unit_by_number(punit->id);
2341}
2342
2343/**********************************************************************/
2346void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2347{
2348 return punit->server.ais[ai_type_number(ai)];
2349}
2350
2351/**********************************************************************/
2354void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2355 void *data)
2356{
2357 punit->server.ais[ai_type_number(ai)] = data;
2358}
2359
2360/**********************************************************************/
2371int unit_bribe_cost(const struct unit *punit, const struct player *briber,
2372 const struct unit *briber_unit)
2373{
2374 int cost, default_hp;
2375 struct tile *ptile = unit_tile(punit);
2376 struct player *owner = unit_owner(punit);
2377 const struct unit_type *ptype = unit_type_get(punit);
2378
2380
2381 default_hp = ptype->hp;
2382
2384
2385 if (owner != nullptr) {
2386 int dist = 0;
2387
2388 cost += owner->economic.gold;
2389
2390 /* Consider the distance to the capital. */
2392 city_list_iterate(owner->cities, capital) {
2393 if (is_capital(capital)) {
2394 int tmp = map_distance(capital->tile, ptile);
2395
2396 if (tmp < dist) {
2397 dist = tmp;
2398 }
2399 }
2401
2402 cost /= dist + 2;
2403 }
2404
2405 /* Consider the build cost. */
2407
2408 /* Rule set specific cost modification */
2410 &(const struct req_context) {
2411 .player = owner,
2413 .tile = ptile,
2414 .unit = punit,
2415 .unittype = ptype,
2416 },
2417 &(const struct req_context) {
2418 .player = briber,
2419 .unit = briber_unit,
2421 : nullptr,
2423 : nullptr,
2424 .city = briber_unit
2425 ? game_city_by_number(briber_unit->homecity)
2426 : nullptr,
2427 },
2429 / 100;
2430
2431 /* Veterans are not cheap. */
2432 {
2433 const struct veteran_level *vlevel
2435
2437 cost = cost * vlevel->power_fact / 100;
2438 if (ptype->move_rate > 0) {
2439 cost += cost * vlevel->move_bonus / ptype->move_rate;
2440 } else {
2441 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2442 }
2443 }
2444
2445 /* Cost now contains the basic bribe cost. We now reduce it by:
2446 * bribecost = cost / 2 + cost / 2 * damage / hp
2447 * = cost / 2 * (1 + damage / hp) */
2448 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2449}
2450
2451/**********************************************************************/
2459int stack_bribe_cost(const struct tile *ptile, const struct player *briber,
2460 const struct unit *briber_unit)
2461{
2462 int bribe_cost = 0;
2463
2467
2468 return bribe_cost;
2469}
2470
2471/**********************************************************************/
2474bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2475{
2478
2480
2481 if (force || can_unit_load(pcargo, ptrans)) {
2482 pcargo->transporter = ptrans;
2483 unit_list_append(ptrans->transporting, pcargo);
2484
2485 return TRUE;
2486 }
2487
2488 return FALSE;
2489}
2490
2491/**********************************************************************/
2495{
2496 struct unit *ptrans;
2497
2499
2500 if (!unit_transported(pcargo)) {
2501 /* 'pcargo' is not transported. */
2502 return FALSE;
2503 }
2504
2505 /* Get the transporter; must not be defined on the client! */
2507 if (ptrans) {
2508 /* 'pcargo' and 'ptrans' should be on the same tile. */
2510
2511#ifndef FREECIV_NDEBUG
2512 bool success =
2513#endif
2514 unit_list_remove(ptrans->transporting, pcargo);
2515
2516 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2518 }
2519
2520 /* For the server (also safe for the client). */
2521 pcargo->transporter = NULL;
2522
2523 return TRUE;
2524}
2525
2526/**********************************************************************/
2529bool unit_transported(const struct unit *pcargo)
2530{
2532
2533 /* The unit is transported if a transporter unit is set or, (for the client)
2534 * if the transported_by field is set. */
2535 if (is_server()) {
2537 } else {
2539 }
2540}
2541
2542/**********************************************************************/
2545struct unit *unit_transport_get(const struct unit *pcargo)
2546{
2548
2549 return pcargo->transporter;
2550}
2551
2552/**********************************************************************/
2555struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2556{
2558 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2559
2560 return ptrans->transporting;
2561}
2562
2563/**********************************************************************/
2566static inline bool
2574
2575/**********************************************************************/
2584 const struct unit *ptrans)
2585{
2586 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2587
2588 /* Check 'pcargo' against 'ptrans'. */
2590 return FALSE;
2591 }
2592
2593 /* Check 'pcargo' against 'ptrans' parents. */
2596 return FALSE;
2597 }
2599
2600 /* Check cargo children... */
2603
2604 /* ...against 'ptrans'. */
2606 return FALSE;
2607 }
2608
2609 /* ...and against 'ptrans' parents. */
2612 return FALSE;
2613 }
2616
2617 return TRUE;
2618}
2619
2620/**********************************************************************/
2624bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2625{
2627 if (ptrans == plevel) {
2628 return TRUE;
2629 }
2631 return FALSE;
2632}
2633
2634/**********************************************************************/
2637int unit_cargo_depth(const struct unit *ptrans)
2638{
2639 struct cargo_iter iter;
2640 struct iterator *it;
2641 int depth = 0;
2642
2643 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2644 iterator_next(it)) {
2645 if (iter.depth > depth) {
2646 depth = iter.depth;
2647 }
2648 }
2649 return depth;
2650}
2651
2652/**********************************************************************/
2656{
2657 int level = 0;
2658
2660 level++;
2662 return level;
2663}
2664
2665/**********************************************************************/
2669{
2670 return sizeof(struct cargo_iter);
2671}
2672
2673/**********************************************************************/
2676static void *cargo_iter_get(const struct iterator *it)
2677{
2678 const struct cargo_iter *iter = CARGO_ITER(it);
2679
2680 return unit_list_link_data(iter->links[iter->depth - 1]);
2681}
2682
2683/**********************************************************************/
2686static void cargo_iter_next(struct iterator *it)
2687{
2688 struct cargo_iter *iter = CARGO_ITER(it);
2689 const struct unit_list_link *piter;
2690 const struct unit_list_link *pnext;
2691
2692 /* Variant 1: unit has cargo. */
2694 if (NULL != pnext) {
2695 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2696 iter->links[iter->depth++] = pnext;
2697 return;
2698 }
2699
2700 fc_assert(iter->depth > 0);
2701
2702 while (iter->depth > 0) {
2703 piter = iter->links[iter->depth - 1];
2704
2705 /* Variant 2: there are other cargo units at same level. */
2707 if (NULL != pnext) {
2708 iter->links[iter->depth - 1] = pnext;
2709 return;
2710 }
2711
2712 /* Variant 3: return to previous level, and do same tests. */
2713 iter->depth--;
2714 }
2715}
2716
2717/**********************************************************************/
2720static bool cargo_iter_valid(const struct iterator *it)
2721{
2722 return (0 < CARGO_ITER(it)->depth);
2723}
2724
2725/**********************************************************************/
2729 const struct unit *ptrans)
2730{
2731 struct iterator *it = ITERATOR(iter);
2732
2733 it->get = cargo_iter_get;
2734 it->next = cargo_iter_next;
2735 it->valid = cargo_iter_valid;
2737 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2738
2739 return it;
2740}
2741
2742/**********************************************************************/
2745bool unit_is_cityfounder(const struct unit *punit)
2746{
2748}
2749
2750/**********************************************************************/
2754 int length, const struct unit_order *orders)
2755{
2756 int i;
2757
2758 for (i = 0; i < length; i++) {
2759 struct action *paction;
2760 struct extra_type *pextra;
2761
2762 if (orders[i].order > ORDER_LAST) {
2763 log_error("invalid order %d at index %d", orders[i].order, i);
2764 return FALSE;
2765 }
2766 switch (orders[i].order) {
2767 case ORDER_MOVE:
2768 case ORDER_ACTION_MOVE:
2769 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2770 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2771 return FALSE;
2772 }
2773 break;
2774 case ORDER_ACTIVITY:
2775 switch (orders[i].activity) {
2776 case ACTIVITY_SENTRY:
2777 if (i != length - 1) {
2778 /* Only allowed as the last order. */
2779 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2780 i);
2781 return FALSE;
2782 }
2783 break;
2784 /* Replaced by action orders */
2785 case ACTIVITY_BASE:
2786 case ACTIVITY_GEN_ROAD:
2787 case ACTIVITY_CLEAN:
2788 case ACTIVITY_PILLAGE:
2789 case ACTIVITY_MINE:
2790 case ACTIVITY_IRRIGATE:
2791 case ACTIVITY_PLANT:
2792 case ACTIVITY_CULTIVATE:
2793 case ACTIVITY_TRANSFORM:
2794 case ACTIVITY_CONVERT:
2796 log_error("at index %d, use action rather than activity %d.",
2797 i, orders[i].activity);
2798 return FALSE;
2799 /* Not supported. */
2800 case ACTIVITY_EXPLORE:
2801 case ACTIVITY_IDLE:
2802 /* Not set from the client. */
2803 case ACTIVITY_GOTO:
2804 case ACTIVITY_FORTIFIED:
2805 /* Unused. */
2806 case ACTIVITY_LAST:
2807 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2808 return FALSE;
2809 }
2810
2811 break;
2813 if (!action_id_exists(orders[i].action)) {
2814 /* Non-existent action. */
2815 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2816 return FALSE;
2817 }
2818
2819 paction = action_by_number(orders[i].action);
2820
2821 /* Validate main target. */
2822 if (index_to_tile(nmap, orders[i].target) == NULL) {
2823 log_error("at index %d, invalid tile target %d for the action %d.",
2824 i, orders[i].target, orders[i].action);
2825 return FALSE;
2826 }
2827
2828 if (orders[i].dir != DIR8_ORIGIN) {
2829 log_error("at index %d, the action %d sets the outdated target"
2830 " specification dir.",
2831 i, orders[i].action);
2832 }
2833
2834 /* Validate sub target. */
2835 switch (action_id_get_sub_target_kind(orders[i].action)) {
2836 case ASTK_BUILDING:
2837 /* Sub target is a building. */
2838 if (!improvement_by_number(orders[i].sub_target)) {
2839 /* Sub target is invalid. */
2840 log_error("at index %d, cannot do %s without a target.", i,
2841 action_id_rule_name(orders[i].action));
2842 return FALSE;
2843 }
2844 break;
2845 case ASTK_TECH:
2846 /* Sub target is a technology. */
2847 if (orders[i].sub_target == A_NONE
2848 || (!valid_advance_by_number(orders[i].sub_target)
2849 && orders[i].sub_target != A_FUTURE)) {
2850 /* Target tech is invalid. */
2851 log_error("at index %d, cannot do %s without a target.", i,
2852 action_id_rule_name(orders[i].action));
2853 return FALSE;
2854 }
2855 break;
2856 case ASTK_EXTRA:
2858 /* Sub target is an extra. */
2859 pextra = (!(orders[i].sub_target == NO_TARGET
2860 || (orders[i].sub_target < 0
2861 || (orders[i].sub_target
2863 ? extra_by_number(orders[i].sub_target) : NULL);
2864 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2865 if (pextra == NULL) {
2866 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2867 /* Target extra is invalid. */
2868 log_error("at index %d, cannot do %s without a target.", i,
2869 action_id_rule_name(orders[i].action));
2870 return FALSE;
2871 }
2872 } else {
2873 if (!actres_removes_extra(paction->result, pextra)
2874 && !actres_creates_extra(paction->result, pextra)) {
2875 /* Target extra is irrelevant for the action. */
2876 log_error("at index %d, cannot do %s to %s.", i,
2877 action_id_rule_name(orders[i].action),
2878 extra_rule_name(pextra));
2879 return FALSE;
2880 }
2881 }
2882 break;
2883 case ASTK_SPECIALIST:
2884 if (!specialist_by_number(orders[i].sub_target)) {
2885 log_error("at index %d, cannot do %s without a target.", i,
2886 action_id_rule_name(orders[i].action));
2887 return FALSE;
2888 }
2889 break;
2890 case ASTK_NONE:
2891 /* No validation required. */
2892 break;
2893 /* Invalid action? */
2894 case ASTK_COUNT:
2897 FALSE,
2898 "Bad action %d in order number %d.", orders[i].action, i);
2899 }
2900
2901 /* Some action orders are sane only in the last order. */
2902 if (i != length - 1) {
2903 /* If the unit is dead, */
2905 /* or if Freeciv has no idea where the unit will end up after it
2906 * has performed this action, */
2909 /* or if the unit will end up standing still, */
2911 /* than having this action in the middle of a unit's orders is
2912 * probably wrong. */
2913 log_error("action %d is not allowed at index %d.",
2914 orders[i].action, i);
2915 return FALSE;
2916 }
2917 }
2918
2919 /* Don't validate that the target tile really contains a target or
2920 * that the actor player's map think the target tile has one.
2921 * The player may target something from their player map that isn't
2922 * there any more, a target they think is there even if their player
2923 * map doesn't have it, or even a target they assume will be there
2924 * when the unit reaches the target tile.
2925 *
2926 * With that said: The client should probably at least have an
2927 * option to only aim city targeted actions at cities. */
2928
2929 break;
2930 case ORDER_FULL_MP:
2931 break;
2932 case ORDER_LAST:
2933 /* An invalid order. This is handled above. */
2934 break;
2935 }
2936 }
2937
2938 return TRUE;
2939}
2940
2941/**********************************************************************/
2946 int length,
2947 const struct unit_order *orders)
2948{
2949 struct unit_order *unit_orders;
2950
2951 if (!unit_order_list_is_sane(nmap, length, orders)) {
2952 return NULL;
2953 }
2954
2955 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2956 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2957
2958 return unit_orders;
2959}
2960
2961/**********************************************************************/
2965{
2967 ACTION_NONE, // ACTIVITY_IDLE
2968 ACTION_CULTIVATE, // ACTIVITY_CULTIVATE
2969 ACTION_MINE, // ACTIVITY_MINE
2970 ACTION_IRRIGATE, // ACTIVITY_IRRIGATE
2971 ACTION_FORTIFY, // ACTIVITY_FORTIFIED
2972 ACTION_NONE, // ACTIVITY_SENTRY
2973 ACTION_PILLAGE, // ACTIVITY_PILLAGE
2974 ACTION_NONE, // ACTIVITY_GOTO
2975 ACTION_NONE, // ACTIVITY_EXPLORE
2976 ACTION_TRANSFORM_TERRAIN, // ACTIVITY_TRANSFORM
2977 ACTION_FORTIFY, // ACTIVITY_FORTIFYING
2978 ACTION_CLEAN, // ACTIVITY_CLEAN
2979 ACTION_BASE, // ACTIVITY_BASE
2980 ACTION_ROAD, // ACTIVITY_GEN_ROAD
2981 ACTION_CONVERT, // ACTIVITY_CONVERT
2982 ACTION_PLANT // ACTIVITY_PLANT
2983 };
2984
2985 return act_act[act];
2986}
2987
2988/**********************************************************************/
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5306
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:4827
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1260
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:3333
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:3092
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:3169
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:4469
bool action_maybe_possible_actor_unit(const struct civ_map *nmap, const action_id act_id, const struct unit *actor_unit)
Definition actions.c:5729
bool action_id_exists(const action_id act_id)
Definition actions.c:1089
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:3482
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:4552
#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:811
bool actres_creates_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:790
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: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:76
#define MAX_LEN_MSG
Definition conn_types.h:37
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2966
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:214
enum unit_activity Activity_type_id
Definition fc_types.h:239
#define DIR8_ORIGIN
Definition fc_types.h:315
int action_id
Definition fc_types.h:249
@ HB_ALLIANCE
Definition fc_types.h:986
@ HB_DISABLED
Definition fc_types.h:984
@ HB_NATIONAL
Definition fc_types.h:985
@ O_SHIELD
Definition fc_types.h:102
@ O_FOOD
Definition fc_types.h:102
@ O_GOLD
Definition fc_types.h:102
@ O_LAST
Definition fc_types.h:102
@ BORDERS_DISABLED
Definition fc_types.h:730
enum output_type_id Output_type_id
Definition fc_types.h:238
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:93
#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: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:1112
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1396
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1417
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1471
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
struct specialist * specialist_by_number(const Specialist_type_id id)
Definition specialist.c:110
Definition ai.h:50
struct iterator vtable
Definition unit.c:51
int depth
Definition unit.c:53
const struct unit_list_link * links[GAME_TRANSPORT_MAX_RECURSIVE]
Definition unit.c:52
Definition city.h:318
struct tile * tile
Definition city.h:320
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:530
bool worker
Definition unittype.h:582
int move_rate
Definition unittype.h:524
enum vision_layer vlayer
Definition unittype.h:576
struct unit_type::@90 adv
const struct unit_type * converted_to
Definition unittype.h:537
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:1132
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Definition tile.c:419
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:325
int tile_has_not_aggressive_extra_for_unit(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:197
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:393
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:422
size_t cargo_iter_sizeof(void)
Definition unit.c:2668
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1668
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2945
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:2020
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1568
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2304
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1653
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:233
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1847
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2686
bool can_unit_change_homecity_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pcity)
Definition unit.c:508
void free_unit_orders(struct unit *punit)
Definition unit.c:1833
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2354
bool unit_is_alive(int id)
Definition unit.c:2317
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:303
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:343
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:469
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:569
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2199
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:585
int unit_upkeep_cost(const struct unit *punit, Output_type_id otype)
Definition unit.c:2994
bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1520
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2474
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1986
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2728
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2115
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2655
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2292
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1678
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:768
bool unit_can_add_or_build_city(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:491
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1403
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2226
bool can_unit_change_homecity(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:524
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2676
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1301
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1582
int get_activity_rate(const struct unit *punit)
Definition unit.c:538
bool can_unit_deboard_or_be_unloaded(const struct civ_map *nmap, const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:820
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2583
bool is_attack_unit(const struct unit *punit)
Definition unit.c:324
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:205
const Activity_type_id tile_changing_activities[]
Definition unit.c:45
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2545
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2494
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1623
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2624
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2247
int unit_bribe_cost(const struct unit *punit, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2371
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:86
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:884
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1124
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1287
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:743
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2745
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1178
bool is_field_unit(const struct unit *punit)
Definition unit.c:457
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2720
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:402
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:412
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2964
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:335
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:914
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1688
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2637
bool can_unit_do_autoworker(const struct unit *punit)
Definition unit.c:649
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Definition unit.c:2567
#define CARGO_ITER(iter)
Definition unit.c:55
bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:849
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2753
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:657
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1194
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:316
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1451
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:1159
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:351
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1639
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:865
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1211
bool unit_can_help_build_wonder_here(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:277
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1387
bool is_special_unit(const struct unit *punit)
Definition unit.c:369
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1793
int stack_bribe_cost(const struct tile *ptile, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2459
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1347
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1311
void set_unit_activity(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1141
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:2052
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2529
bool are_unit_orders_equal(const struct unit_order *order1, const struct unit_order *order2)
Definition unit.c:60
bool unit_is_virtual(const struct unit *punit)
Definition unit.c:2334
bool is_flagless_to_player(const struct unit *punit, const struct player *pplayer)
Definition unit.c:382
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:431
bool is_guard_unit(const struct unit *punit)
Definition unit.c:359
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:795
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1424
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2555
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:969
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1996
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2346
bool unit_has_orders(const struct unit *punit)
Definition unit.c:221
struct unit * tile_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1324
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1976
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:1857
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2099
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:609
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:711
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.c:1362
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:955
#define unit_tile(_pu)
Definition unit.h:407
#define unit_transports_iterate_end
Definition unit.h:580
#define unit_cargo_iterate_end
Definition unit.h:590
@ 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:558
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:587
#define unit_owner(_pu)
Definition unit.h:406
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:576
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:561
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:126
int utype_pays_mp_for_action_base(const struct action *paction, const struct unit_type *putype)
Definition unittype.c:1406
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:325
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1595
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1274
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2997
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1613
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1493
bool utype_may_act_move_frags(const struct unit_type *punit_type, const action_id act_id, const int move_fragments)
Definition unittype.c:1083
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1509
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1729
bool utype_can_freely_load(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:313
int utype_upkeep_cost(const struct unit_type *ut, const struct unit *punit, struct player *pplayer, Output_type_id otype)
Definition unittype.c:136
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2657
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2530
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:412
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Definition unittype.c:2629
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:215
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1244
bool utype_can_do_action_sub_result(const struct unit_type *putype, enum action_sub_result sub_result)
Definition unittype.c:433
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1313
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:468
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1586
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:396
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1757
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:773
#define utype_class(_t_)
Definition unittype.h:756
#define utype_fuel(ptype)
Definition unittype.h:847
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624