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/**********************************************************************/
82 const struct player *restriction,
83 const struct unit *punit,
84 const struct city *pdest_city)
85{
86 const struct city *psrc_city = tile_city(unit_tile(punit));
87 const struct player *punit_owner;
89
90 if (0 == punit->moves_left
92 ACTION_AIRLIFT, 0)) {
93 /* No moves left. */
94 return AR_NO_MOVES;
95 }
96
98 return AR_WRONG_UNITTYPE;
99 }
100
102 /* Units with occupants can't be airlifted currently. */
103 return AR_OCCUPIED;
104 }
105
106 if (NULL == psrc_city) {
107 /* No city there. */
108 return AR_NOT_IN_CITY;
109 }
110
111 if (psrc_city == pdest_city) {
112 /* Airlifting to our current position doesn't make sense. */
113 return AR_BAD_DST_CITY;
114 }
115
116 if (pdest_city
117 && (NULL == restriction
119 == TILE_KNOWN_SEEN))
121 /* Can't exist at the destination tile. */
122 return AR_BAD_DST_CITY;
123 }
124
126
127 /* Check validity of both source and destination before checking capacity,
128 * to avoid misleadingly optimistic returns. */
129
133 /* Not allowed to airlift from this source. */
134 return AR_BAD_SRC_CITY;
135 }
136
137 if (pdest_city
141 /* Not allowed to airlift to this destination. */
142 return AR_BAD_DST_CITY;
143 }
144
146 /* We know for sure whether or not src can airlift this turn. */
147 if (0 >= psrc_city->airlift
150 /* The source cannot airlift for this turn (maybe already airlifted
151 * or no airport).
152 * See also do_airline() in server/unittools.h. */
153 return AR_SRC_NO_FLIGHTS;
154 } /* else, there is capacity; continue to other checks */
155 } else {
156 /* We don't have access to the 'airlift' field. Assume it's OK; can
157 * only find out for sure by trying it. */
159 }
160
161 if (pdest_city) {
163 if (0 >= pdest_city->airlift
166 /* The destination cannot support airlifted units for this turn
167 * (maybe already airlifted or no airport).
168 * See also do_airline() in server/unittools.h. */
169 return AR_DST_NO_FLIGHTS;
170 } /* else continue */
171 } else {
173 }
174 }
175
176 return ok_result;
177}
178
179/**********************************************************************/
187 const struct unit *punit,
188 const struct city *pdest_city)
189{
190 if (is_server()) {
193 } else {
195 pdest_city));
196 }
197}
198
199/**********************************************************************/
202bool unit_has_orders(const struct unit *punit)
203{
204 return punit->has_orders;
205}
206
207/**********************************************************************/
214int unit_shield_value(const struct unit *punit,
215 const struct unit_type *punittype,
216 const struct action *paction)
217{
218 int value;
219
220 bool has_unit;
221 const struct player *act_player;
222
223 has_unit = punit != NULL;
224
225 if (has_unit && punittype == NULL) {
227 }
228
232
234 /* TODO: determine if tile and city should be where the unit currently is
235 * located or the target city. Those two may differ. Wait for ruleset
236 * author feed back. */
237
239 value += ((value
241 &(const struct req_context) {
242 .player = act_player,
243 .unit = punit,
244 .unittype = punittype,
245 .action = paction,
246 },
247 NULL,
249 / 100);
250
251 return value;
252}
253
254/**********************************************************************/
259 const struct unit *punit)
260{
261 struct city *pcity = tile_city(unit_tile(punit));
262
263 if (pcity == NULL) {
264 /* No city to help at this tile. */
265 return FALSE;
266 }
267
269 /* This unit can never do help wonder. */
270 return FALSE;
271 }
272
273 /* Evaluate all action enablers for extra accuracy. */
274 /* TODO: Is it worth it? */
277 pcity));
278}
279
280/**********************************************************************/
293
294/**********************************************************************/
298{
300}
301
302/**********************************************************************/
312
313/**********************************************************************/
316bool is_martial_law_unit(const struct unit *punit)
317{
319}
320
321/**********************************************************************/
324bool is_occupying_unit(const struct unit *punit)
325{
327}
328
329/**********************************************************************/
333{
335}
336
337/**********************************************************************/
340bool is_guard_unit(const struct unit *punit)
341{
343}
344
345/**********************************************************************/
350bool is_special_unit(const struct unit *punit)
351{
353}
354
355/**********************************************************************/
364 const struct player *pplayer)
365{
366 if (unit_owner(punit) != pplayer
368 if (pplayer == nullptr) {
369 /* Global observer always sees the flags */
370 return FALSE;
371 }
372
373 return TRUE;
374 }
375
376 return FALSE;
377}
378
379/**********************************************************************/
383bool unit_can_do_action(const struct unit *punit,
384 const action_id act_id)
385{
386 return utype_can_do_action(unit_type_get(punit), act_id);
387}
388
389/**********************************************************************/
394 enum action_result result)
395{
397}
398
399/**********************************************************************/
408
409/**********************************************************************/
413 const struct player *pplayer,
414 const struct tile *ptile, bool omniscient)
415{
416 square_iterate(nmap, ptile, 2, ptile1) {
418 if ((omniscient
419 || can_player_see_unit(pplayer, punit))
420 && pplayers_at_war(pplayer, unit_owner(punit))
425 unit_class_get(punit), ptile)))) {
426 return TRUE;
427 }
430
431 return FALSE;
432}
433
434/**********************************************************************/
438bool is_field_unit(const struct unit *punit)
439{
441}
442
443/**********************************************************************/
450bool is_hiding_unit(const struct unit *punit)
451{
453
454 if (vl == V_INVIS || vl == V_SUBSURFACE) {
455 return TRUE;
456 }
457
458 if (unit_transported(punit)) {
460 if (vl == V_INVIS || vl == V_SUBSURFACE) {
461 return TRUE;
462 }
463 }
464
465 return FALSE;
466}
467
468/**********************************************************************/
473 const struct unit *punit)
474{
475 struct city *tgt_city;
476
477 if ((tgt_city = tile_city(unit_tile(punit)))) {
480 } else {
483 }
484}
485
486/**********************************************************************/
490 const struct unit *punit,
491 const struct city *pcity)
492{
493 if (pcity == NULL) {
494 /* Can't change home city to a non existing city. */
495 return FALSE;
496 }
497
499 pcity));
500}
501
502/**********************************************************************/
506 const struct unit *punit)
507{
509}
510
511/**********************************************************************/
519int get_activity_rate(const struct unit *punit)
520{
521 const struct veteran_level *vlevel;
522 const struct unit_type *ptype;
523 int move_rate;
524
526
530
531 /* The speed of the settler depends on its base move_rate, not on
532 * the number of moves actually remaining or the adjusted move rate.
533 * This means sea formers won't have their activity rate increased by
534 * Magellan's, and it means injured units work just as fast as
535 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
536 move_rate = ptype->move_rate;
537
538 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
539 return ACTIVITY_FACTOR
540 * (float)vlevel->power_fact / 100
542}
543
544/**********************************************************************/
551{
552 /* This logic is also coded in client/goto.c. */
553 if (punit->moves_left > 0) {
554 return get_activity_rate(punit);
555 } else {
556 return 0;
557 }
558}
559
560/**********************************************************************/
567 enum unit_activity activity,
568 const struct tile *ptile,
569 struct extra_type *tgt)
570{
571 /* FIXME: This is just an approximation since we don't account for
572 * get_activity_rate_this_turn. */
574 int points_needed;
575
576 fc_assert(tgt != NULL || !is_targeted_activity(activity));
577
578 points_needed = tile_activity_time(activity, ptile, tgt);
579
580 if (points_needed >= 0 && speed > 0) {
581 return (points_needed - 1) / speed + 1; /* Round up */
582 } else {
583 return FC_INFINITY;
584 }
585}
586
587/**********************************************************************/
591{
592 switch (activity) {
593 case ACTIVITY_PILLAGE:
594 case ACTIVITY_BASE:
597 case ACTIVITY_MINE:
598 case ACTIVITY_CLEAN:
599 return TRUE;
600 case ACTIVITY_IDLE:
602 case ACTIVITY_SENTRY:
603 case ACTIVITY_GOTO:
604 case ACTIVITY_EXPLORE:
607 case ACTIVITY_PLANT:
609 case ACTIVITY_CONVERT:
610 return FALSE;
611 /* These shouldn't be kicking around internally. */
612 case ACTIVITY_LAST:
613 break;
614 }
615
617
618 return FALSE;
619}
620
621/**********************************************************************/
631{
632 return unit_type_get(punit)->adv.worker;
633}
634
635/**********************************************************************/
638const char *get_activity_text(enum unit_activity activity)
639{
640 /* The switch statement has just the activities listed with no "default"
641 * handling. This enables the compiler to detect missing entries
642 * automatically, and still handles everything correctly. */
643 switch (activity) {
644 case ACTIVITY_IDLE:
645 return _("Idle");
646 case ACTIVITY_CLEAN:
647 return _("Clean");
648 case ACTIVITY_MINE:
649 /* TRANS: Activity name, verb in English */
650 return Q_("?act:Mine");
651 case ACTIVITY_PLANT:
652 /* TRANS: Activity name, verb in English */
653 return _("Plant");
655 return _("Irrigate");
657 return _("Cultivate");
659 return _("Fortifying");
661 return _("Fortified");
662 case ACTIVITY_SENTRY:
663 return _("Sentry");
664 case ACTIVITY_PILLAGE:
665 return _("Pillage");
666 case ACTIVITY_GOTO:
667 return _("Goto");
668 case ACTIVITY_EXPLORE:
669 return _("Explore");
671 return _("Transform");
672 case ACTIVITY_BASE:
673 return _("Base");
675 return _("Road");
676 case ACTIVITY_CONVERT:
677 return _("Convert");
678 case ACTIVITY_LAST:
679 break;
680 }
681
683 return _("Unknown");
684}
685
686/**********************************************************************/
693 const struct unit *ptrans)
694{
695 /* Make sure this transporter can carry this type of unit. */
697 return FALSE;
698 }
699
700 /* Make sure there's room in the transporter. */
703 return FALSE;
704 }
705
706 /* Check iff this is a valid transport. */
708 return FALSE;
709 }
710
711 /* Check transport depth. */
714 return FALSE;
715 }
716
717 return TRUE;
718}
719
720/**********************************************************************/
724bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
725{
726 if (!pcargo || !ptrans || pcargo == ptrans) {
727 return FALSE;
728 }
729
730 /* Double-check ownership of the units: you can load into an allied unit
731 * (of course only allied units can be on the same tile). */
733 return FALSE;
734 }
735
736 /* Un-embarkable transport must be in city or base to load cargo. */
740 return FALSE;
741 }
742
744}
745
746/**********************************************************************/
749bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
750{
751 /* This function needs to check EVERYTHING. */
752
753 /* Check positions of the units. Of course you can't load a unit onto
754 * a transporter on a different tile... */
756 return FALSE;
757 }
758
759 /* Cannot load if cargo is already loaded onto something else. */
761 return FALSE;
762 }
763
765}
766
767/**********************************************************************/
776bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
777{
778 if (!pcargo || !ptrans) {
779 return FALSE;
780 }
781
782 /* Make sure the unit's transporter exists and is known. */
784 return FALSE;
785 }
786
787 /* Un-disembarkable transport must be in city or base to unload cargo. */
791 return FALSE;
792 }
793
794 return TRUE;
795}
796
797/**********************************************************************/
824
825/**********************************************************************/
830bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
831{
834 return TRUE;
835 }
837
838 return FALSE;
839}
840
841/**********************************************************************/
861
862/**********************************************************************/
866 struct unit *punit)
867{
868 enum unit_activity current = punit->activity;
869 struct extra_type *target = punit->activity_target;
870 enum unit_activity current2 =
871 (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
872 bool result;
873
876
878
879 punit->activity = current;
880 punit->activity_target = target;
881
882 return result;
883}
884
885/**********************************************************************/
893 const struct unit *punit,
894 enum unit_activity activity)
895{
896 struct extra_type *target = NULL;
897
898 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
899 * being able to find out whether an activity is in general possible.
900 * Find one for them, but when they come to do the activity, they will
901 * have to determine the target themselves */
902 {
903 struct tile *ptile = unit_tile(punit);
904
905 if (activity == ACTIVITY_IRRIGATE) {
906 target = next_extra_for_tile(ptile,
909 punit);
910 if (NULL == target) {
911 return FALSE; /* No more irrigation extras available. */
912 }
913 } else if (activity == ACTIVITY_MINE) {
914 target = next_extra_for_tile(ptile,
915 EC_MINE,
917 punit);
918 if (NULL == target) {
919 return FALSE; /* No more mine extras available. */
920 }
921 }
922 }
923
924 return can_unit_do_activity_targeted(nmap, punit, activity, target);
925}
926
927/**********************************************************************/
932 const struct unit *punit,
933 enum unit_activity activity,
934 struct extra_type *target)
935{
936 return can_unit_do_activity_targeted_at(nmap, punit, activity, target,
938}
939
940/**********************************************************************/
945 const struct unit *punit,
946 enum unit_activity activity,
947 struct extra_type *target,
948 const struct tile *ptile)
949{
950 /* Check that no build activity conflicting with one already in progress
951 * gets executed. */
952 /* FIXME: Should check also the cases where one of the activities is terrain
953 * change that destroys the target of the other activity */
954 if (target != NULL && is_build_activity(activity)) {
955 if (tile_is_placing(ptile)) {
956 return FALSE;
957 }
958
959 unit_list_iterate(ptile->units, tunit) {
960 if (is_build_activity(tunit->activity)
961 && !can_extras_coexist(target, tunit->activity_target)) {
962 return FALSE;
963 }
965 }
966
967#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
968{ \
969 switch (action_get_target_kind(paction)) { \
970 case ATK_TILE: \
971 return is_action_enabled_unit_on_tile(nmap, paction->id, \
972 punit, ptile, target); \
973 case ATK_EXTRAS: \
974 return is_action_enabled_unit_on_extras(nmap, paction->id, \
975 punit, ptile, target); \
976 case ATK_CITY: \
977 case ATK_UNIT: \
978 case ATK_STACK: \
979 case ATK_SELF: \
980 return FALSE; \
981 case ATK_COUNT: \
982 break; /* Handle outside switch */ \
983 } \
984 fc_assert(action_target_kind_is_valid( \
985 action_get_target_kind(paction))); \
986 return FALSE; \
987 }
988
989 switch (activity) {
990 case ACTIVITY_IDLE:
991 case ACTIVITY_GOTO:
992 return TRUE;
993
994 case ACTIVITY_CLEAN:
995 /* The call below doesn't support actor tile speculation. */
996 fc_assert_msg(unit_tile(punit) == ptile,
997 "Please use action_speculate_unit_on_tile()");
999 punit, ptile, target);
1000
1001 case ACTIVITY_MINE:
1002 /* The call below doesn't support actor tile speculation. */
1003 fc_assert_msg(unit_tile(punit) == ptile,
1004 "Please use action_speculate_unit_on_tile()");
1006 ptile, target);
1007
1008 case ACTIVITY_PLANT:
1009 /* The call below doesn't support actor tile speculation. */
1010 fc_assert_msg(unit_tile(punit) == ptile,
1011 "Please use action_speculate_unit_on_tile()");
1013 punit, ptile, NULL);
1014
1015 case ACTIVITY_IRRIGATE:
1016 /* The call below doesn't support actor tile speculation. */
1017 fc_assert_msg(unit_tile(punit) == ptile,
1018 "Please use action_speculate_unit_on_tile()");
1020 ptile, target);
1021
1022 case ACTIVITY_CULTIVATE:
1023 /* The call below doesn't support actor tile speculation. */
1024 fc_assert_msg(unit_tile(punit) == ptile,
1025 "Please use action_speculate_unit_on_tile()");
1027 punit, ptile, NULL);
1028
1030 /* The call below doesn't support actor tile speculation. */
1031 fc_assert_msg(unit_tile(punit) == ptile,
1032 "Please use action_speculate_unit_on_self()");
1034 punit);
1035
1036 case ACTIVITY_FORTIFIED:
1037 return FALSE;
1038
1039 case ACTIVITY_BASE:
1040 /* The call below doesn't support actor tile speculation. */
1041 fc_assert_msg(unit_tile(punit) == ptile,
1042 "Please use action_speculate_unit_on_tile()");
1044 punit, ptile, target);
1045
1046 case ACTIVITY_GEN_ROAD:
1047 /* The call below doesn't support actor tile speculation. */
1048 fc_assert_msg(unit_tile(punit) == ptile,
1049 "Please use action_speculate_unit_on_tile()");
1051 punit, ptile, target);
1052
1053 case ACTIVITY_SENTRY:
1055 && !unit_transported(punit)) {
1056 /* Don't let units sentry on tiles they will die on. */
1057 return FALSE;
1058 }
1059 return TRUE;
1060
1061 case ACTIVITY_PILLAGE:
1062 /* The call below doesn't support actor tile speculation. */
1063 fc_assert_msg(unit_tile(punit) == ptile,
1064 "Please use action_speculate_unit_on_tile()");
1066
1067 case ACTIVITY_EXPLORE:
1068 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1069
1070 case ACTIVITY_TRANSFORM:
1071 /* The call below doesn't support actor tile speculation. */
1072 fc_assert_msg(unit_tile(punit) == ptile,
1073 "Please use action_speculate_unit_on_tile()");
1075 punit, ptile, NULL);
1076
1077 case ACTIVITY_CONVERT:
1078 /* The call below doesn't support actor tile speculation. */
1079 fc_assert_msg(unit_tile(punit) == ptile,
1080 "Please use action_speculate_unit_on_self()");
1082
1083 case ACTIVITY_LAST:
1084 break;
1085 }
1086
1087 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1088 activity);
1089 return FALSE;
1090
1091#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1092}
1093
1094/**********************************************************************/
1100{
1103 punit->activity_count = 0;
1105 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1106 /* No longer done. */
1108 }
1109}
1110
1111/**********************************************************************/
1128
1129/**********************************************************************/
1147
1148/**********************************************************************/
1152 const struct tile *ptile)
1153{
1154 unit_list_iterate(ptile->units, punit) {
1155 if (punit->activity == activity) {
1156 return TRUE;
1157 }
1159
1160 return FALSE;
1161}
1162
1163/**********************************************************************/
1168{
1170
1172 unit_list_iterate(ptile->units, punit) {
1175 }
1177
1178 return tgt_ret;
1179}
1180
1181/**********************************************************************/
1184void unit_activity_astr(const struct unit *punit, struct astring *astr)
1185{
1186 if (!punit || !astr) {
1187 return;
1188 }
1189
1190 switch (punit->activity) {
1191 case ACTIVITY_IDLE:
1193 int rate, f;
1194
1196 f = ((punit->fuel) - 1);
1197
1198 /* Add in two parts as move_points_text() returns ptr to static
1199 * End result: "Moves: (fuel)moves_left" */
1200 astr_add_line(astr, "%s: (%s)", _("Moves"),
1202 astr_add(astr, "%s",
1204 } else {
1205 astr_add_line(astr, "%s: %s", _("Moves"),
1207 }
1208 return;
1209 case ACTIVITY_CLEAN:
1210 case ACTIVITY_TRANSFORM:
1212 case ACTIVITY_FORTIFIED:
1213 case ACTIVITY_SENTRY:
1214 case ACTIVITY_GOTO:
1215 case ACTIVITY_EXPLORE:
1216 case ACTIVITY_CONVERT:
1217 case ACTIVITY_CULTIVATE:
1218 case ACTIVITY_PLANT:
1220 return;
1221 case ACTIVITY_MINE:
1222 case ACTIVITY_IRRIGATE:
1223 if (punit->activity_target == NULL) {
1225 } else {
1226 astr_add_line(astr, "Building %s",
1228 }
1229 return;
1230 case ACTIVITY_PILLAGE:
1231 if (punit->activity_target != NULL) {
1234 } else {
1236 }
1237 return;
1238 case ACTIVITY_BASE:
1241 return;
1242 case ACTIVITY_GEN_ROAD:
1245 return;
1246 case ACTIVITY_LAST:
1247 break;
1248 }
1249
1250 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1252}
1253
1254/**********************************************************************/
1260void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1261{
1262 if (!punit || !astr) {
1263 return;
1264 }
1265
1266 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1268 punit->upkeep[O_GOLD]);
1269}
1270
1271/**********************************************************************/
1274struct player *unit_nationality(const struct unit *punit)
1275{
1277 return punit->nationality;
1278}
1279
1280/**********************************************************************/
1284void unit_tile_set(struct unit *punit, struct tile *ptile)
1285{
1287 punit->tile = ptile;
1288}
1289
1290/**********************************************************************/
1297struct unit *tile_allied_unit(const struct tile *ptile,
1298 const struct player *pplayer)
1299{
1300 struct unit *punit = NULL;
1301
1302 unit_list_iterate(ptile->units, cunit) {
1303 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1304 punit = cunit;
1305 } else {
1306 return NULL;
1307 }
1308 }
1310
1311 return punit;
1312}
1313
1314/**********************************************************************/
1320struct unit *tile_enemy_unit(const struct tile *ptile,
1321 const struct player *pplayer)
1322{
1323 unit_list_iterate(ptile->units, punit) {
1324 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1325 return punit;
1326 }
1328
1329 return NULL;
1330}
1331
1332/**********************************************************************/
1335struct unit *tile_non_allied_unit(const struct tile *ptile,
1336 const struct player *pplayer,
1338{
1339 unit_list_iterate(ptile->units, punit) {
1340 struct player *owner = unit_owner(punit);
1341
1342 if (everyone_non_allied && owner != pplayer) {
1343 return punit;
1344 }
1345
1346 if (!pplayers_allied(owner, pplayer)
1347 || is_flagless_to_player(punit, pplayer)) {
1348 return punit;
1349 }
1350 }
1352
1353 return NULL;
1354}
1355
1356/**********************************************************************/
1360struct unit *tile_other_players_unit(const struct tile *ptile,
1361 const struct player *pplayer)
1362{
1363 unit_list_iterate(ptile->units, punit) {
1364 if (unit_owner(punit) != pplayer) {
1365 return punit;
1366 }
1368
1369 return NULL;
1370}
1371
1372/**********************************************************************/
1376struct unit *tile_non_attack_unit(const struct tile *ptile,
1377 const struct player *pplayer)
1378{
1379 unit_list_iterate(ptile->units, punit) {
1380 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1381 return punit;
1382 }
1383 }
1385
1386 return NULL;
1387}
1388
1389/**********************************************************************/
1397struct unit *unit_occupies_tile(const struct tile *ptile,
1398 const struct player *pplayer)
1399{
1400 unit_list_iterate(ptile->units, punit) {
1401 if (!is_occupying_unit(punit)) {
1402 continue;
1403 }
1404
1406 continue;
1407 }
1408
1409 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1410 return punit;
1411 }
1413
1414 return NULL;
1415}
1416
1417/**********************************************************************/
1424bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1425 const struct civ_map *zmap)
1426{
1428
1429 square_iterate(zmap, ptile0, 1, ptile) {
1430 struct terrain *pterrain;
1431 struct city *pcity;
1432
1433 pterrain = tile_terrain(ptile);
1434 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1435 continue;
1436 }
1437
1438 pcity = tile_non_allied_city(ptile, pplayer);
1439 if (pcity != NULL) {
1440 if (unit_list_size(ptile->units) > 0) {
1441 /* Occupied enemy city, it doesn't matter if units inside have
1442 * UTYF_NOZOC or not. */
1443 return FALSE;
1444 }
1445 } else {
1446 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1448 if (tile_has_extra(ptile, pextra)) {
1449 return FALSE;
1450 }
1452 }
1453
1454 unit_list_iterate(ptile->units, punit) {
1455 if (!pplayers_allied(unit_owner(punit), pplayer)
1458 bool hidden = FALSE;
1459
1460 /* We do NOT check the possibility that player is allied with an extra owner,
1461 * and should thus see inside the extra.
1462 * This is to avoid the situation where having an alliance with third player
1463 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1464 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1465 if (tile_has_extra(ptile, pextra)) {
1466 hidden = TRUE;
1467 break;
1468 }
1470
1471 if (!hidden) {
1472 return FALSE;
1473 }
1474 }
1476 }
1478
1479 return TRUE;
1480}
1481
1482/**********************************************************************/
1493bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1494 const struct civ_map *zmap)
1495{
1497
1498 square_iterate(zmap, ptile0, 1, ptile) {
1499 struct terrain *pterrain;
1500 struct city *pcity;
1501
1502 pterrain = tile_terrain(ptile);
1503 if (T_UNKNOWN == pterrain
1504 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1505 continue;
1506 }
1507
1508 pcity = tile_non_allied_city(ptile, pplayer);
1509 if (pcity != NULL) {
1510 if (pcity->client.occupied
1511 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1512 /* Occupied enemy city, it doesn't matter if units inside have
1513 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1514 return FALSE;
1515 }
1516 } else {
1517 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1519 if (tile_has_extra(ptile, pextra)) {
1520 return FALSE;
1521 }
1523 }
1524
1525 unit_list_iterate(ptile->units, punit) {
1527 && !pplayers_allied(unit_owner(punit), pplayer)
1529 return FALSE;
1530 }
1532 }
1534
1535 return TRUE;
1536}
1537
1538/**********************************************************************/
1546
1547/**********************************************************************/
1556 const struct unit *punit)
1557{
1558 const struct tile *ptile = unit_tile(punit);
1560
1561 if (!is_attack_unit(punit)) {
1562 return FALSE;
1563 }
1564 if (tile_city(ptile)) {
1565 return FALSE;
1566 }
1568 switch (game.info.happyborders) {
1569 case HB_DISABLED:
1570 break;
1571 case HB_NATIONAL:
1572 if (tile_owner(ptile) == unit_owner(punit)) {
1573 return FALSE;
1574 }
1575 break;
1576 case HB_ALLIANCE:
1578 return FALSE;
1579 }
1580 break;
1581 }
1582 }
1583
1586 if (max_friendliness_range >= 0) {
1588 }
1589
1590 return TRUE;
1591}
1592
1593/**********************************************************************/
1597{
1598 switch (activity) {
1599 case ACTIVITY_MINE:
1600 case ACTIVITY_IRRIGATE:
1601 case ACTIVITY_BASE:
1602 case ACTIVITY_GEN_ROAD:
1603 return TRUE;
1604 default:
1605 return FALSE;
1606 }
1607}
1608
1609/**********************************************************************/
1613{
1614 switch (activity) {
1615 case ACTIVITY_PILLAGE:
1616 case ACTIVITY_CLEAN:
1617 return TRUE;
1618 default:
1619 return FALSE;
1620 }
1621}
1622
1623/**********************************************************************/
1627{
1628 switch (activity) {
1629 case ACTIVITY_CULTIVATE:
1630 case ACTIVITY_PLANT:
1631 case ACTIVITY_TRANSFORM:
1632 return TRUE;
1633 default:
1634 return FALSE;
1635 }
1636}
1637
1638/**********************************************************************/
1642{
1643 return is_build_activity(activity)
1644 || is_clean_activity(activity)
1645 || is_terrain_change_activity(activity);
1646}
1647
1648/**********************************************************************/
1652{
1653 return is_build_activity(activity)
1654 || is_clean_activity(activity);
1655}
1656
1657/**********************************************************************/
1661struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1662 const struct unit_type *punittype,
1663 int veteran_level)
1664{
1665 /* Make sure that contents of unit structure are correctly initialized,
1666 * if you ever allocate it by some other mean than fc_calloc() */
1667 struct unit *punit = fc_calloc(1, sizeof(*punit));
1668 int max_vet_lvl;
1669
1670 /* It does not register the unit so the id is set to 0. */
1672
1673 fc_assert_ret_val(punittype != nullptr, nullptr); /* No untyped units! */
1675
1676 fc_assert_ret_val(!is_server() || pplayer != nullptr, nullptr); /* No unowned units! */
1677 punit->owner = pplayer;
1678 punit->nationality = pplayer;
1679
1680 punit->refcount = 1;
1682
1683 if (pcity != nullptr) {
1685 punit->homecity = pcity->id;
1686 } else {
1687 unit_tile_set(punit, nullptr);
1689 }
1690
1691 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1692 punit->goto_tile = nullptr;
1695 /* A unit new and fresh ... */
1699 /* Random moves units start with zero movement as their first movement
1700 * will be only after their moves have been reset in the beginning of
1701 * the next turn. */
1702 punit->moves_left = 0;
1703 } else {
1705 }
1706 punit->moved = FALSE;
1707
1711
1712 punit->transporter = nullptr;
1714
1715 punit->carrying = nullptr;
1716
1720
1722 punit->action_decision_tile = nullptr;
1723
1724 punit->stay = FALSE;
1725
1728
1729 if (is_server()) {
1731
1733
1734 punit->server.removal_callback = nullptr;
1735
1737 O_LAST * sizeof(*punit->server.upkeep_paid));
1738
1739 punit->server.ord_map = 0;
1740 punit->server.ord_city = 0;
1741
1742 punit->server.vision = nullptr; /* No vision. */
1744 /* Must be an invalid turn number, and an invalid previous turn
1745 * number. */
1746 punit->server.action_turn = -2;
1747 /* punit->server.moving = NULL; set by fc_calloc(). */
1748
1749 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1750
1751 CALL_FUNC_EACH_AI(unit_alloc, punit);
1752 } else {
1756 punit->client.act_prob_cache = nullptr;
1757 }
1758
1759 return punit;
1760}
1761
1762/**********************************************************************/
1767{
1769
1770 /* Unload unit if transported. */
1773
1774 /* Check for transported units. Use direct access to the list. */
1775 if (unit_list_size(punit->transporting) != 0) {
1776 /* Unload all units. */
1780 }
1782
1783 if (punit->transporting) {
1785 }
1786
1787 CALL_FUNC_EACH_AI(unit_free, punit);
1788
1789 if (is_server() && punit->server.adv) {
1791 } else {
1794 }
1795 }
1796
1797 if (--punit->refcount <= 0) {
1798 FC_FREE(punit);
1799 }
1800}
1801
1802/**********************************************************************/
1807{
1808 if (punit->has_orders) {
1809 punit->goto_tile = NULL;
1811 punit->orders.list = NULL;
1812 }
1813 punit->orders.length = 0;
1815}
1816
1817/**********************************************************************/
1821{
1823
1824 return unit_list_size(ptrans->transporting);
1825}
1826
1827/**********************************************************************/
1830static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1831 const struct tile *ptile,
1832 bool (*unit_load_test)
1833 (const struct unit *pc,
1834 const struct unit *pt))
1835{
1836 struct unit *best_trans = NULL;
1837 struct {
1840 } cur, best = { FALSE };
1841
1842 unit_list_iterate(ptile->units, ptrans) {
1843 if (!unit_load_test(pcargo, ptrans)) {
1844 continue;
1845 } else if (best_trans == NULL) {
1847 }
1848
1849 /* Gather data from transport stack in a single pass, for use in
1850 * various conditions below. */
1852 cur.outermost_moves_left = ptrans->moves_left;
1853 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1856 cur.has_orders = TRUE;
1857 }
1858 cur.outermost_moves_left = ptranstrans->moves_left;
1859 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1861
1862 /* Criteria for deciding the 'best' transport to load onto.
1863 * The following tests are applied in order; earlier ones have
1864 * lexicographically greater significance than later ones. */
1865
1866 /* Transports which have orders, or are on transports with orders,
1867 * are less preferable to transport stacks without orders (to
1868 * avoid loading on units that are just passing through). */
1869 if (best_trans != ptrans) {
1870 if (!cur.has_orders && best.has_orders) {
1872 } else if (cur.has_orders && !best.has_orders) {
1873 continue;
1874 }
1875 }
1876
1877 /* Else, transports which are idle are preferable (giving players
1878 * some control over loading) -- this does not check transports
1879 * of transports. */
1880 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1881 if (best_trans != ptrans) {
1882 if (cur.is_idle && !best.is_idle) {
1884 } else if (!cur.is_idle && best.is_idle) {
1885 continue;
1886 }
1887 }
1888
1889 /* Else, transports from which the cargo could unload at any time
1890 * are preferable to those where the cargo can only disembark in
1891 * cities/bases. */
1892 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1894 if (best_trans != ptrans) {
1895 if (cur.can_freely_unload && !best.can_freely_unload) {
1897 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1898 continue;
1899 }
1900 }
1901
1902 /* Else, transports which are less deeply nested are preferable. */
1904 if (best_trans != ptrans) {
1905 if (cur.depth < best.depth) {
1907 } else if (cur.depth > best.depth) {
1908 continue;
1909 }
1910 }
1911
1912 /* Else, transport stacks where the outermost transport has more
1913 * moves left are preferable (on the assumption that it's the
1914 * outermost transport that's about to move). */
1915 if (best_trans != ptrans) {
1916 if (cur.outermost_moves_left > best.outermost_moves_left) {
1918 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1919 continue;
1920 }
1921 }
1922
1923 /* All other things being equal, as a tie-breaker, compare the total
1924 * moves left (this turn) and move rate (future turns) for the whole
1925 * stack, to take into account total potential movement for both
1926 * short and long journeys (we don't know which the cargo intends to
1927 * make). Doesn't try to account for whether transports can unload,
1928 * etc. */
1929 if (best_trans != ptrans) {
1930 if (cur.total_moves > best.total_moves) {
1932 } else {
1933 continue;
1934 }
1935 }
1936
1938 best = cur;
1940
1941 return best_trans;
1942}
1943
1944/**********************************************************************/
1953
1954/**********************************************************************/
1960 const struct tile *ptile)
1961{
1963}
1964
1965/**********************************************************************/
1970 const struct unit *punit)
1971{
1973 return FALSE;
1974 }
1975
1978 return FALSE;
1979 }
1981
1982 return TRUE;
1983}
1984
1985/**********************************************************************/
1994 const struct unit *punit,
1995 const struct unit_type *to_unittype)
1996{
1998
2000 return UU_NOT_ENOUGH_ROOM;
2001 }
2002
2003 if (punit->transporter != NULL) {
2007 }
2009 /* The new unit type can't survive on this terrain. */
2010 return UU_NOT_TERRAIN;
2011 }
2012
2013 return UU_OK;
2014}
2015
2016/**********************************************************************/
2026 const struct unit *punit,
2027 bool is_free)
2028{
2029 struct player *pplayer = unit_owner(punit);
2030 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2032 struct city *pcity;
2033 int cost;
2034
2035 if (!to_unittype) {
2036 return UU_NO_UNITTYPE;
2037 }
2038
2040 /* TODO: There may be other activities that the upgraded unit is not
2041 allowed to do, which we could also test.
2042 -
2043 If convert were legal for new unit_type we could allow, but then it
2044 has a 'head start' getting activity time from the old conversion. */
2045 return UU_NOT_ACTIVITY;
2046 }
2047
2048 if (!is_free) {
2050 if (pplayer->economic.gold < cost) {
2051 return UU_NO_MONEY;
2052 }
2053
2055 if (!pcity) {
2056 return UU_NOT_IN_CITY;
2057 }
2058 if (city_owner(pcity) != pplayer) {
2059 /* TODO: Should upgrades in allied cities be possible? */
2060 return UU_NOT_CITY_OWNER;
2061 }
2062 }
2063
2064 /* TODO: Allow transported units to be reassigned. Check here
2065 * and make changes to upgrade_unit. */
2067}
2068
2069/**********************************************************************/
2072bool unit_can_convert(const struct civ_map *nmap,
2073 const struct unit *punit)
2074{
2075 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2076
2077 if (tgt == NULL) {
2078 return FALSE;
2079 }
2080
2081 return UU_OK == unit_transform_result(nmap, punit, tgt);
2082}
2083
2084/**********************************************************************/
2089 const struct unit *punit,
2090 char *buf, size_t bufsz)
2091{
2092 struct player *pplayer = unit_owner(punit);
2094 int upgrade_cost;
2095 const struct unit_type *from_unittype = unit_type_get(punit);
2096 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2098 char tbuf[MAX_LEN_MSG];
2099
2100 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2101 "Treasury contains %d gold.",
2102 pplayer->economic.gold),
2103 pplayer->economic.gold);
2104
2105 switch (result) {
2106 case UU_OK:
2108 /* This message is targeted toward the GUI callers. */
2109 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2110 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2111 "Upgrade %s to %s for %d gold?\n%s",
2112 upgrade_cost),
2116 break;
2117 case UU_NO_UNITTYPE:
2119 _("Sorry, cannot upgrade %s (yet)."),
2121 break;
2122 case UU_NO_MONEY:
2124 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2125 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2126 "Upgrading %s to %s costs %d gold.\n%s",
2127 upgrade_cost),
2131 break;
2132 case UU_NOT_IN_CITY:
2133 case UU_NOT_CITY_OWNER:
2135 _("You can only upgrade units in your cities."));
2136 break;
2137 case UU_NOT_ENOUGH_ROOM:
2139 _("Upgrading this %s would strand units it transports."),
2141 break;
2142 case UU_NOT_TERRAIN:
2144 _("Upgrading this %s would result in a %s which can not "
2145 "survive at this place."),
2148 break;
2151 _("Upgrading this %s would result in a %s which its "
2152 "current transport, %s, could not transport."),
2156 break;
2157 case UU_NOT_ACTIVITY:
2159 _("Cannot upgrade %s while doing '%s'."),
2162 break;
2163 }
2164
2165 return result;
2166}
2167
2168/**********************************************************************/
2173 const struct unit *punit)
2174{
2175 int mpco;
2176
2178 &(const struct req_context) {
2179 .player = unit_owner(punit),
2180 .city = unit_tile(punit)
2182 : NULL,
2183 .tile = unit_tile(punit),
2184 .unit = punit,
2185 .unittype = unit_type_get(punit),
2186 .action = paction,
2187 },
2188 NULL,
2190
2192
2193 return mpco;
2194}
2195
2196/**********************************************************************/
2199int hp_gain_coord(const struct unit *punit)
2200{
2201 int hp = 0;
2202 const int base = unit_type_get(punit)->hp;
2203 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2204
2205 /* Includes barracks (100%), fortress (25%), etc. */
2207
2208 /* Minimum HP after regen effects applied. */
2209 hp = MAX(hp, min);
2210
2211 /* Regen2 effects that apply after there's at least Min HP */
2212 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2213
2214 return MAX(hp, 0);
2215}
2216
2217/**********************************************************************/
2221{
2222 const struct unit_type *utype = unit_type_get(punit);
2224 struct city *pcity = tile_city(unit_tile(punit));
2225 int gain;
2226
2227 if (!punit->moved) {
2229 } else {
2230 gain = 0;
2231 }
2232
2234
2235 if (!punit->homecity && 0 < game.server.killunhomed
2237 /* Hit point loss of units without homecity; at least 1 hp! */
2238 /* Gameloss units are immune to this effect. */
2239 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2240
2241 if (gain > hp_loss) {
2242 gain = -1;
2243 } else {
2244 gain -= hp_loss;
2245 }
2246 }
2247
2248 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2249 && !unit_transported(punit)) {
2250 gain -= utype->hp * pclass->hp_loss_pct / 100;
2251 }
2252
2253 if (punit->hp + gain > utype->hp) {
2254 gain = utype->hp - punit->hp;
2255 } else if (punit->hp + gain < 0) {
2256 gain = -punit->hp;
2257 }
2258
2259 return gain;
2260}
2261
2262/**********************************************************************/
2265bool is_losing_hp(const struct unit *punit)
2266{
2267 const struct unit_type *punittype = unit_type_get(punit);
2268
2270 < (punittype->hp *
2271 utype_class(punittype)->hp_loss_pct / 100);
2272}
2273
2274/**********************************************************************/
2277bool unit_type_is_losing_hp(const struct player *pplayer,
2278 const struct unit_type *punittype)
2279{
2280 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2282 < (punittype->hp *
2283 utype_class(punittype)->hp_loss_pct / 100);
2284}
2285
2286/**********************************************************************/
2290bool unit_is_alive(int id)
2291{
2292 /* Check if unit exist in game */
2293 if (game_unit_by_number(id)) {
2294 return TRUE;
2295 }
2296
2297 return FALSE;
2298}
2299
2300/**********************************************************************/
2307bool unit_is_virtual(const struct unit *punit)
2308{
2309 if (!punit) {
2310 return FALSE;
2311 }
2312
2313 return punit != game_unit_by_number(punit->id);
2314}
2315
2316/**********************************************************************/
2319void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2320{
2321 return punit->server.ais[ai_type_number(ai)];
2322}
2323
2324/**********************************************************************/
2327void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2328 void *data)
2329{
2330 punit->server.ais[ai_type_number(ai)] = data;
2331}
2332
2333/**********************************************************************/
2344int unit_bribe_cost(const struct unit *punit, const struct player *briber,
2345 const struct unit *briber_unit)
2346{
2347 int cost, default_hp;
2348 struct tile *ptile = unit_tile(punit);
2349 struct player *owner = unit_owner(punit);
2350 const struct unit_type *ptype = unit_type_get(punit);
2351
2353
2354 default_hp = ptype->hp;
2355
2357
2358 if (owner != nullptr) {
2359 int dist = 0;
2360
2361 cost += owner->economic.gold;
2362
2363 /* Consider the distance to the capital. */
2365 city_list_iterate(owner->cities, capital) {
2366 if (is_capital(capital)) {
2367 int tmp = map_distance(capital->tile, ptile);
2368
2369 if (tmp < dist) {
2370 dist = tmp;
2371 }
2372 }
2374
2375 cost /= dist + 2;
2376 }
2377
2378 /* Consider the build cost. */
2380
2381 /* Rule set specific cost modification */
2383 &(const struct req_context) {
2384 .player = owner,
2386 .tile = ptile,
2387 .unit = punit,
2388 .unittype = ptype,
2389 },
2390 &(const struct req_context) {
2391 .player = briber,
2392 .unit = briber_unit,
2394 : nullptr,
2396 : nullptr,
2397 .city = briber_unit
2398 ? game_city_by_number(briber_unit->homecity)
2399 : nullptr,
2400 },
2402 / 100;
2403
2404 /* Veterans are not cheap. */
2405 {
2406 const struct veteran_level *vlevel
2408
2410 cost = cost * vlevel->power_fact / 100;
2411 if (ptype->move_rate > 0) {
2412 cost += cost * vlevel->move_bonus / ptype->move_rate;
2413 } else {
2414 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2415 }
2416 }
2417
2418 /* Cost now contains the basic bribe cost. We now reduce it by:
2419 * bribecost = cost / 2 + cost / 2 * damage / hp
2420 * = cost / 2 * (1 + damage / hp) */
2421 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2422}
2423
2424/**********************************************************************/
2432int stack_bribe_cost(const struct tile *ptile, const struct player *briber,
2433 const struct unit *briber_unit)
2434{
2435 int bribe_cost = 0;
2436
2440
2441 return bribe_cost;
2442}
2443
2444/**********************************************************************/
2447bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2448{
2451
2453
2454 if (force || can_unit_load(pcargo, ptrans)) {
2455 pcargo->transporter = ptrans;
2456 unit_list_append(ptrans->transporting, pcargo);
2457
2458 return TRUE;
2459 }
2460
2461 return FALSE;
2462}
2463
2464/**********************************************************************/
2468{
2469 struct unit *ptrans;
2470
2472
2473 if (!unit_transported(pcargo)) {
2474 /* 'pcargo' is not transported. */
2475 return FALSE;
2476 }
2477
2478 /* Get the transporter; must not be defined on the client! */
2480 if (ptrans) {
2481 /* 'pcargo' and 'ptrans' should be on the same tile. */
2483
2484#ifndef FREECIV_NDEBUG
2485 bool success =
2486#endif
2487 unit_list_remove(ptrans->transporting, pcargo);
2488
2489 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2491 }
2492
2493 /* For the server (also safe for the client). */
2494 pcargo->transporter = NULL;
2495
2496 return TRUE;
2497}
2498
2499/**********************************************************************/
2502bool unit_transported(const struct unit *pcargo)
2503{
2505
2506 /* The unit is transported if a transporter unit is set or, (for the client)
2507 * if the transported_by field is set. */
2508 if (is_server()) {
2510 } else {
2512 }
2513}
2514
2515/**********************************************************************/
2518struct unit *unit_transport_get(const struct unit *pcargo)
2519{
2521
2522 return pcargo->transporter;
2523}
2524
2525/**********************************************************************/
2528struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2529{
2531 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2532
2533 return ptrans->transporting;
2534}
2535
2536/**********************************************************************/
2539static inline bool
2547
2548/**********************************************************************/
2557 const struct unit *ptrans)
2558{
2559 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2560
2561 /* Check 'pcargo' against 'ptrans'. */
2563 return FALSE;
2564 }
2565
2566 /* Check 'pcargo' against 'ptrans' parents. */
2569 return FALSE;
2570 }
2572
2573 /* Check cargo children... */
2576
2577 /* ...against 'ptrans'. */
2579 return FALSE;
2580 }
2581
2582 /* ...and against 'ptrans' parents. */
2585 return FALSE;
2586 }
2589
2590 return TRUE;
2591}
2592
2593/**********************************************************************/
2597bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2598{
2600 if (ptrans == plevel) {
2601 return TRUE;
2602 }
2604 return FALSE;
2605}
2606
2607/**********************************************************************/
2610int unit_cargo_depth(const struct unit *ptrans)
2611{
2612 struct cargo_iter iter;
2613 struct iterator *it;
2614 int depth = 0;
2615
2616 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2617 iterator_next(it)) {
2618 if (iter.depth > depth) {
2619 depth = iter.depth;
2620 }
2621 }
2622 return depth;
2623}
2624
2625/**********************************************************************/
2629{
2630 int level = 0;
2631
2633 level++;
2635 return level;
2636}
2637
2638/**********************************************************************/
2642{
2643 return sizeof(struct cargo_iter);
2644}
2645
2646/**********************************************************************/
2649static void *cargo_iter_get(const struct iterator *it)
2650{
2651 const struct cargo_iter *iter = CARGO_ITER(it);
2652
2653 return unit_list_link_data(iter->links[iter->depth - 1]);
2654}
2655
2656/**********************************************************************/
2659static void cargo_iter_next(struct iterator *it)
2660{
2661 struct cargo_iter *iter = CARGO_ITER(it);
2662 const struct unit_list_link *piter;
2663 const struct unit_list_link *pnext;
2664
2665 /* Variant 1: unit has cargo. */
2667 if (NULL != pnext) {
2668 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2669 iter->links[iter->depth++] = pnext;
2670 return;
2671 }
2672
2673 fc_assert(iter->depth > 0);
2674
2675 while (iter->depth > 0) {
2676 piter = iter->links[iter->depth - 1];
2677
2678 /* Variant 2: there are other cargo units at same level. */
2680 if (NULL != pnext) {
2681 iter->links[iter->depth - 1] = pnext;
2682 return;
2683 }
2684
2685 /* Variant 3: return to previous level, and do same tests. */
2686 iter->depth--;
2687 }
2688}
2689
2690/**********************************************************************/
2693static bool cargo_iter_valid(const struct iterator *it)
2694{
2695 return (0 < CARGO_ITER(it)->depth);
2696}
2697
2698/**********************************************************************/
2702 const struct unit *ptrans)
2703{
2704 struct iterator *it = ITERATOR(iter);
2705
2706 it->get = cargo_iter_get;
2707 it->next = cargo_iter_next;
2708 it->valid = cargo_iter_valid;
2710 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2711
2712 return it;
2713}
2714
2715/**********************************************************************/
2718bool unit_is_cityfounder(const struct unit *punit)
2719{
2721}
2722
2723/**********************************************************************/
2727 int length, const struct unit_order *orders)
2728{
2729 int i;
2730
2731 for (i = 0; i < length; i++) {
2732 struct action *paction;
2733 struct extra_type *pextra;
2734
2735 if (orders[i].order > ORDER_LAST) {
2736 log_error("invalid order %d at index %d", orders[i].order, i);
2737 return FALSE;
2738 }
2739 switch (orders[i].order) {
2740 case ORDER_MOVE:
2741 case ORDER_ACTION_MOVE:
2742 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2743 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2744 return FALSE;
2745 }
2746 break;
2747 case ORDER_ACTIVITY:
2748 switch (orders[i].activity) {
2749 case ACTIVITY_SENTRY:
2750 if (i != length - 1) {
2751 /* Only allowed as the last order. */
2752 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2753 i);
2754 return FALSE;
2755 }
2756 break;
2757 /* Replaced by action orders */
2758 case ACTIVITY_BASE:
2759 case ACTIVITY_GEN_ROAD:
2760 case ACTIVITY_CLEAN:
2761 case ACTIVITY_PILLAGE:
2762 case ACTIVITY_MINE:
2763 case ACTIVITY_IRRIGATE:
2764 case ACTIVITY_PLANT:
2765 case ACTIVITY_CULTIVATE:
2766 case ACTIVITY_TRANSFORM:
2767 case ACTIVITY_CONVERT:
2769 log_error("at index %d, use action rather than activity %d.",
2770 i, orders[i].activity);
2771 return FALSE;
2772 /* Not supported. */
2773 case ACTIVITY_EXPLORE:
2774 case ACTIVITY_IDLE:
2775 /* Not set from the client. */
2776 case ACTIVITY_GOTO:
2777 case ACTIVITY_FORTIFIED:
2778 /* Unused. */
2779 case ACTIVITY_LAST:
2780 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2781 return FALSE;
2782 }
2783
2784 break;
2786 if (!action_id_exists(orders[i].action)) {
2787 /* Non-existent action. */
2788 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2789 return FALSE;
2790 }
2791
2792 paction = action_by_number(orders[i].action);
2793
2794 /* Validate main target. */
2795 if (index_to_tile(nmap, orders[i].target) == NULL) {
2796 log_error("at index %d, invalid tile target %d for the action %d.",
2797 i, orders[i].target, orders[i].action);
2798 return FALSE;
2799 }
2800
2801 if (orders[i].dir != DIR8_ORIGIN) {
2802 log_error("at index %d, the action %d sets the outdated target"
2803 " specification dir.",
2804 i, orders[i].action);
2805 }
2806
2807 /* Validate sub target. */
2808 switch (action_id_get_sub_target_kind(orders[i].action)) {
2809 case ASTK_BUILDING:
2810 /* Sub target is a building. */
2811 if (!improvement_by_number(orders[i].sub_target)) {
2812 /* Sub target is invalid. */
2813 log_error("at index %d, cannot do %s without a target.", i,
2814 action_id_rule_name(orders[i].action));
2815 return FALSE;
2816 }
2817 break;
2818 case ASTK_TECH:
2819 /* Sub target is a technology. */
2820 if (orders[i].sub_target == A_NONE
2821 || (!valid_advance_by_number(orders[i].sub_target)
2822 && orders[i].sub_target != A_FUTURE)) {
2823 /* Target tech is invalid. */
2824 log_error("at index %d, cannot do %s without a target.", i,
2825 action_id_rule_name(orders[i].action));
2826 return FALSE;
2827 }
2828 break;
2829 case ASTK_EXTRA:
2831 /* Sub target is an extra. */
2832 pextra = (!(orders[i].sub_target == NO_TARGET
2833 || (orders[i].sub_target < 0
2834 || (orders[i].sub_target
2836 ? extra_by_number(orders[i].sub_target) : NULL);
2837 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2838 if (pextra == NULL) {
2839 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2840 /* Target extra is invalid. */
2841 log_error("at index %d, cannot do %s without a target.", i,
2842 action_id_rule_name(orders[i].action));
2843 return FALSE;
2844 }
2845 } else {
2846 if (!actres_removes_extra(paction->result, pextra)
2847 && !actres_creates_extra(paction->result, pextra)) {
2848 /* Target extra is irrelevant for the action. */
2849 log_error("at index %d, cannot do %s to %s.", i,
2850 action_id_rule_name(orders[i].action),
2851 extra_rule_name(pextra));
2852 return FALSE;
2853 }
2854 }
2855 break;
2856 case ASTK_NONE:
2857 /* No validation required. */
2858 break;
2859 /* Invalid action? */
2860 case ASTK_COUNT:
2863 FALSE,
2864 "Bad action %d in order number %d.", orders[i].action, i);
2865 }
2866
2867 /* Some action orders are sane only in the last order. */
2868 if (i != length - 1) {
2869 /* If the unit is dead, */
2871 /* or if Freeciv has no idea where the unit will end up after it
2872 * has performed this action, */
2875 /* or if the unit will end up standing still, */
2877 /* than having this action in the middle of a unit's orders is
2878 * probably wrong. */
2879 log_error("action %d is not allowed at index %d.",
2880 orders[i].action, i);
2881 return FALSE;
2882 }
2883 }
2884
2885 /* Don't validate that the target tile really contains a target or
2886 * that the actor player's map think the target tile has one.
2887 * The player may target something from their player map that isn't
2888 * there any more, a target they think is there even if their player
2889 * map doesn't have it, or even a target they assume will be there
2890 * when the unit reaches the target tile.
2891 *
2892 * With that said: The client should probably at least have an
2893 * option to only aim city targeted actions at cities. */
2894
2895 break;
2896 case ORDER_FULL_MP:
2897 break;
2898 case ORDER_LAST:
2899 /* An invalid order. This is handled above. */
2900 break;
2901 }
2902 }
2903
2904 return TRUE;
2905}
2906
2907/**********************************************************************/
2912 int length,
2913 const struct unit_order *orders)
2914{
2915 struct unit_order *unit_orders;
2916
2917 if (!unit_order_list_is_sane(nmap, length, orders)) {
2918 return NULL;
2919 }
2920
2921 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2922 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2923
2924 return unit_orders;
2925}
2926
2927/**********************************************************************/
2931{
2933 ACTION_NONE, // ACTIVITY_IDLE
2934 ACTION_CULTIVATE, // ACTIVITY_CULTIVATE
2935 ACTION_MINE, // ACTIVITY_MINE
2936 ACTION_IRRIGATE, // ACTIVITY_IRRIGATE
2937 ACTION_FORTIFY, // ACTIVITY_FORTIFIED
2938 ACTION_NONE, // ACTIVITY_SENTRY
2939 ACTION_PILLAGE, // ACTIVITY_PILLAGE
2940 ACTION_NONE, // ACTIVITY_GOTO
2941 ACTION_NONE, // ACTIVITY_EXPLORE
2942 ACTION_TRANSFORM_TERRAIN, // ACTIVITY_TRANSFORM
2943 ACTION_FORTIFY, // ACTIVITY_FORTIFYING
2944 ACTION_CLEAN, // ACTIVITY_CLEAN
2945 ACTION_BASE, // ACTIVITY_BASE
2946 ACTION_ROAD, // ACTIVITY_GEN_ROAD
2947 ACTION_CONVERT, // ACTIVITY_CONVERT
2948 ACTION_PLANT // ACTIVITY_PLANT
2949 };
2950
2951 return act_act[act];
2952}
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5091
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:4612
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:3332
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:3091
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:3168
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:4254
bool action_maybe_possible_actor_unit(const struct civ_map *nmap, const action_id act_id, const struct unit *actor_unit)
Definition actions.c:5514
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:3481
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:4337
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:241
#define action_id_get_sub_target_kind(act_id)
Definition actions.h:417
static struct action * action_by_number(action_id act_id)
Definition actions.h:396
#define action_has_result(_act_, _res_)
Definition actions.h:180
#define action_by_result_iterate_end
Definition actions.h:245
#define action_id(_act_)
Definition actions.h:422
#define ACTION_NONE
Definition actions.h:55
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:2956
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:1016
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Definition movement.c:289
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:318
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:330
bool can_unit_transport(const struct unit *transporter, const struct unit *transported)
Definition movement.c:856
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:490
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:464
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:869
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: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: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
@ TILE_KNOWN_SEEN
Definition tile.h:38
#define ACTIVITY_FACTOR
Definition tile.h:166
#define tile_terrain(_tile)
Definition tile.h:111
#define tile_has_extra(ptile, pextra)
Definition tile.h:148
#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:403
size_t cargo_iter_sizeof(void)
Definition unit.c:2641
bool can_unit_do_activity_targeted_at(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, struct extra_type *target, const struct tile *ptile)
Definition unit.c:944
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1641
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2911
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:1993
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1541
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2277
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1626
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:214
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1820
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2659
bool can_unit_change_homecity_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pcity)
Definition unit.c:489
void free_unit_orders(struct unit *punit)
Definition unit.c:1806
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2327
bool unit_is_alive(int id)
Definition unit.c:2290
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:284
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:324
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:450
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:550
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2172
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:566
bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1493
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2447
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1959
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2701
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2088
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2628
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2265
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1651
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:749
bool unit_can_add_or_build_city(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:472
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1376
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2199
bool can_unit_change_homecity(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:505
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2649
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1274
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1555
int get_activity_rate(const struct unit *punit)
Definition unit.c:519
bool can_unit_deboard_or_be_unloaded(const struct civ_map *nmap, const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:801
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2556
bool is_attack_unit(const struct unit *punit)
Definition unit.c:305
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:186
const Activity_type_id tile_changing_activities[]
Definition unit.c:44
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2518
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2467
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1596
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2597
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2220
int unit_bribe_cost(const struct unit *punit, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2344
enum unit_airlift_result test_unit_can_airlift_to(const struct civ_map *nmap, const struct player *restriction, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:81
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:865
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1097
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1260
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:724
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2718
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1151
bool is_field_unit(const struct unit *punit)
Definition unit.c:438
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2693
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:383
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:393
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2930
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:316
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1661
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2610
bool can_unit_do_autoworker(const struct unit *punit)
Definition unit.c:630
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Definition unit.c:2540
#define CARGO_ITER(iter)
Definition unit.c:54
bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:830
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2726
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:638
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1167
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:297
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1424
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:1132
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:332
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1612
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:846
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1184
bool unit_can_help_build_wonder_here(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:258
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:892
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1360
bool is_special_unit(const struct unit *punit)
Definition unit.c:350
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1766
int stack_bribe_cost(const struct tile *ptile, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2432
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1320
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1284
void set_unit_activity(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1114
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:2025
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2502
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:2307
bool is_flagless_to_player(const struct unit *punit, const struct player *pplayer)
Definition unit.c:363
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:412
bool is_guard_unit(const struct unit *punit)
Definition unit.c:340
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:776
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1397
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2528
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1969
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2319
bool unit_has_orders(const struct unit *punit)
Definition unit.c:202
struct unit * tile_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1297
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1949
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:1830
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2072
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:590
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:692
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.c:1335
bool can_unit_do_activity_targeted(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, struct extra_type *target)
Definition unit.c:931
#define unit_tile(_pu)
Definition unit.h:404
#define unit_transports_iterate_end
Definition unit.h:575
#define unit_cargo_iterate_end
Definition unit.h:585
@ 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:553
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:582
#define unit_owner(_pu)
Definition unit.h:403
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:571
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:556
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
#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:1387
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:306
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1575
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1255
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2971
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1593
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1474
bool utype_may_act_move_frags(const struct unit_type *punit_type, const action_id act_id, const int move_fragments)
Definition unittype.c:1064
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1490
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1709
bool utype_can_freely_load(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:294
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2631
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2505
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:393
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Definition unittype.c:2603
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:196
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1225
bool utype_can_do_action_sub_result(const struct unit_type *putype, enum action_sub_result sub_result)
Definition unittype.c:414
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1294
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:449
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1566
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:377
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1737
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:846
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624