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/**********************************************************************/
186bool unit_can_airlift_to(const struct unit *punit,
187 const struct city *pdest_city)
188{
189 const struct civ_map *nmap = &(wld.map);
190
192
193 if (is_server()) {
196 } else {
198 pdest_city));
199 }
200}
201
202/**********************************************************************/
205bool unit_has_orders(const struct unit *punit)
206{
207 return punit->has_orders;
208}
209
210/**********************************************************************/
217int unit_shield_value(const struct unit *punit,
218 const struct unit_type *punittype,
219 const struct action *paction)
220{
221 int value;
222
223 bool has_unit;
224 const struct player *act_player;
225
226 has_unit = punit != NULL;
227
228 if (has_unit && punittype == NULL) {
230 }
231
235
237 /* TODO: determine if tile and city should be where the unit currently is
238 * located or the target city. Those two may differ. Wait for ruleset
239 * author feed back. */
240
242 value += ((value
244 &(const struct req_context) {
245 .player = act_player,
246 .unit = punit,
247 .unittype = punittype,
248 .action = paction,
249 },
250 NULL,
252 / 100);
253
254 return value;
255}
256
257/**********************************************************************/
262{
263 struct city *pcity = tile_city(unit_tile(punit));
264 const struct civ_map *nmap = &(wld.map);
265
266 if (pcity == NULL) {
267 /* No city to help at this tile. */
268 return FALSE;
269 }
270
272 /* This unit can never do help wonder. */
273 return FALSE;
274 }
275
276 /* Evaluate all action enablers for extra accuracy. */
277 /* TODO: Is it worth it? */
280 pcity));
281}
282
283/**********************************************************************/
296
297/**********************************************************************/
301{
303}
304
305/**********************************************************************/
315
316/**********************************************************************/
319bool is_martial_law_unit(const struct unit *punit)
320{
322}
323
324/**********************************************************************/
327bool is_occupying_unit(const struct unit *punit)
328{
330}
331
332/**********************************************************************/
336{
338}
339
340/**********************************************************************/
343bool is_guard_unit(const struct unit *punit)
344{
346}
347
348/**********************************************************************/
353bool is_special_unit(const struct unit *punit)
354{
356}
357
358/**********************************************************************/
367 const struct player *pplayer)
368{
369 if (unit_owner(punit) != pplayer
371 if (pplayer == nullptr) {
372 /* Global observer always sees the flags */
373 return FALSE;
374 }
375
376 return TRUE;
377 }
378
379 return FALSE;
380}
381
382/**********************************************************************/
386bool unit_can_do_action(const struct unit *punit,
387 const action_id act_id)
388{
389 return utype_can_do_action(unit_type_get(punit), act_id);
390}
391
392/**********************************************************************/
397 enum action_result result)
398{
400}
401
402/**********************************************************************/
411
412/**********************************************************************/
416 const struct player *pplayer,
417 const struct tile *ptile, bool omniscient)
418{
419 square_iterate(nmap, ptile, 2, ptile1) {
421 if ((omniscient
422 || can_player_see_unit(pplayer, punit))
423 && pplayers_at_war(pplayer, unit_owner(punit))
428 unit_class_get(punit), ptile)))) {
429 return TRUE;
430 }
433
434 return FALSE;
435}
436
437/**********************************************************************/
441bool is_field_unit(const struct unit *punit)
442{
444}
445
446/**********************************************************************/
453bool is_hiding_unit(const struct unit *punit)
454{
456
457 if (vl == V_INVIS || vl == V_SUBSURFACE) {
458 return TRUE;
459 }
460
461 if (unit_transported(punit)) {
463 if (vl == V_INVIS || vl == V_SUBSURFACE) {
464 return TRUE;
465 }
466 }
467
468 return FALSE;
469}
470
471/**********************************************************************/
476{
477 struct city *tgt_city;
478 const struct civ_map *nmap = &(wld.map);
479
480 if ((tgt_city = tile_city(unit_tile(punit)))) {
483 } else {
486 }
487}
488
489/**********************************************************************/
493 const struct city *pcity)
494{
495 const struct civ_map *nmap = &(wld.map);
496
497 if (pcity == NULL) {
498 /* Can't change home city to a non existing city. */
499 return FALSE;
500 }
501
503 pcity));
504}
505
506/**********************************************************************/
513
514/**********************************************************************/
522int get_activity_rate(const struct unit *punit)
523{
524 const struct veteran_level *vlevel;
525
527
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 int move_rate = unit_type_get(punit)->move_rate;
537
538 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
539 return ACTIVITY_FACTOR
540 * (float)vlevel->power_fact / 100
541 * move_rate / SINGLE_MOVE;
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/**********************************************************************/
825
826/**********************************************************************/
831bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
832{
835 return TRUE;
836 }
838
839 return FALSE;
840}
841
842/**********************************************************************/
862
863/**********************************************************************/
867 struct unit *punit)
868{
869 enum unit_activity current = punit->activity;
870 struct extra_type *target = punit->activity_target;
871 enum unit_activity current2 =
872 (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
873 bool result;
874
877
879
880 punit->activity = current;
881 punit->activity_target = target;
882
883 return result;
884}
885
886/**********************************************************************/
894 const struct unit *punit,
895 enum unit_activity activity)
896{
897 struct extra_type *target = NULL;
898
899 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
900 * being able to find out whether an activity is in general possible.
901 * Find one for them, but when they come to do the activity, they will
902 * have to determine the target themselves */
903 {
904 struct tile *ptile = unit_tile(punit);
905
906 if (activity == ACTIVITY_IRRIGATE) {
907 target = next_extra_for_tile(ptile,
910 punit);
911 if (NULL == target) {
912 return FALSE; /* No more irrigation extras available. */
913 }
914 } else if (activity == ACTIVITY_MINE) {
915 target = next_extra_for_tile(ptile,
916 EC_MINE,
918 punit);
919 if (NULL == target) {
920 return FALSE; /* No more mine extras available. */
921 }
922 }
923 }
924
925 return can_unit_do_activity_targeted(nmap, punit, activity, target);
926}
927
928/**********************************************************************/
933 const struct unit *punit,
934 enum unit_activity activity,
935 struct extra_type *target)
936{
937 return can_unit_do_activity_targeted_at(nmap, punit, activity, target,
939}
940
941/**********************************************************************/
946 const struct unit *punit,
947 enum unit_activity activity,
948 struct extra_type *target,
949 const struct tile *ptile)
950{
951 /* Check that no build activity conflicting with one already in progress
952 * gets executed. */
953 /* FIXME: Should check also the cases where one of the activities is terrain
954 * change that destroys the target of the other activity */
955 if (target != NULL && is_build_activity(activity)) {
956 if (tile_is_placing(ptile)) {
957 return FALSE;
958 }
959
960 unit_list_iterate(ptile->units, tunit) {
961 if (is_build_activity(tunit->activity)
962 && !can_extras_coexist(target, tunit->activity_target)) {
963 return FALSE;
964 }
966 }
967
968#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
969{ \
970 switch (action_get_target_kind(paction)) { \
971 case ATK_TILE: \
972 return is_action_enabled_unit_on_tile(nmap, paction->id, \
973 punit, ptile, target); \
974 case ATK_EXTRAS: \
975 return is_action_enabled_unit_on_extras(nmap, paction->id, \
976 punit, ptile, target); \
977 case ATK_CITY: \
978 case ATK_UNIT: \
979 case ATK_UNITS: \
980 case ATK_SELF: \
981 return FALSE; \
982 case ATK_COUNT: \
983 break; /* Handle outside switch */ \
984 } \
985 fc_assert(action_target_kind_is_valid( \
986 action_get_target_kind(paction))); \
987 return FALSE; \
988 }
989
990 switch (activity) {
991 case ACTIVITY_IDLE:
992 case ACTIVITY_GOTO:
993 return TRUE;
994
995 case ACTIVITY_CLEAN:
996 /* The call below doesn't support actor tile speculation. */
997 fc_assert_msg(unit_tile(punit) == ptile,
998 "Please use action_speculate_unit_on_tile()");
1000 punit, ptile, target);
1001
1002 case ACTIVITY_MINE:
1003 /* The call below doesn't support actor tile speculation. */
1004 fc_assert_msg(unit_tile(punit) == ptile,
1005 "Please use action_speculate_unit_on_tile()");
1007 ptile, target);
1008
1009 case ACTIVITY_PLANT:
1010 /* The call below doesn't support actor tile speculation. */
1011 fc_assert_msg(unit_tile(punit) == ptile,
1012 "Please use action_speculate_unit_on_tile()");
1014 punit, ptile, NULL);
1015
1016 case ACTIVITY_IRRIGATE:
1017 /* The call below doesn't support actor tile speculation. */
1018 fc_assert_msg(unit_tile(punit) == ptile,
1019 "Please use action_speculate_unit_on_tile()");
1021 ptile, target);
1022
1023 case ACTIVITY_CULTIVATE:
1024 /* The call below doesn't support actor tile speculation. */
1025 fc_assert_msg(unit_tile(punit) == ptile,
1026 "Please use action_speculate_unit_on_tile()");
1028 punit, ptile, NULL);
1029
1031 /* The call below doesn't support actor tile speculation. */
1032 fc_assert_msg(unit_tile(punit) == ptile,
1033 "Please use action_speculate_unit_on_self()");
1035 punit);
1036
1037 case ACTIVITY_FORTIFIED:
1038 return FALSE;
1039
1040 case ACTIVITY_BASE:
1041 /* The call below doesn't support actor tile speculation. */
1042 fc_assert_msg(unit_tile(punit) == ptile,
1043 "Please use action_speculate_unit_on_tile()");
1045 punit, ptile, target);
1046
1047 case ACTIVITY_GEN_ROAD:
1048 /* The call below doesn't support actor tile speculation. */
1049 fc_assert_msg(unit_tile(punit) == ptile,
1050 "Please use action_speculate_unit_on_tile()");
1052 punit, ptile, target);
1053
1054 case ACTIVITY_SENTRY:
1056 && !unit_transported(punit)) {
1057 /* Don't let units sentry on tiles they will die on. */
1058 return FALSE;
1059 }
1060 return TRUE;
1061
1062 case ACTIVITY_PILLAGE:
1063 /* The call below doesn't support actor tile speculation. */
1064 fc_assert_msg(unit_tile(punit) == ptile,
1065 "Please use action_speculate_unit_on_tile()");
1067
1068 case ACTIVITY_EXPLORE:
1069 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1070
1071 case ACTIVITY_TRANSFORM:
1072 /* The call below doesn't support actor tile speculation. */
1073 fc_assert_msg(unit_tile(punit) == ptile,
1074 "Please use action_speculate_unit_on_tile()");
1076 punit, ptile, NULL);
1077
1078 case ACTIVITY_CONVERT:
1079 /* The call below doesn't support actor tile speculation. */
1080 fc_assert_msg(unit_tile(punit) == ptile,
1081 "Please use action_speculate_unit_on_self()");
1083
1084 case ACTIVITY_LAST:
1085 break;
1086 }
1087
1088 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1089 activity);
1090 return FALSE;
1091
1092#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1093}
1094
1095/**********************************************************************/
1101{
1104 punit->activity_count = 0;
1106 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1107 /* No longer done. */
1109 }
1110}
1111
1112/**********************************************************************/
1129
1130/**********************************************************************/
1148
1149/**********************************************************************/
1153 const struct tile *ptile)
1154{
1155 unit_list_iterate(ptile->units, punit) {
1156 if (punit->activity == activity) {
1157 return TRUE;
1158 }
1160
1161 return FALSE;
1162}
1163
1164/**********************************************************************/
1169{
1171
1173 unit_list_iterate(ptile->units, punit) {
1176 }
1178
1179 return tgt_ret;
1180}
1181
1182/**********************************************************************/
1185void unit_activity_astr(const struct unit *punit, struct astring *astr)
1186{
1187 if (!punit || !astr) {
1188 return;
1189 }
1190
1191 switch (punit->activity) {
1192 case ACTIVITY_IDLE:
1194 int rate, f;
1195
1197 f = ((punit->fuel) - 1);
1198
1199 /* Add in two parts as move_points_text() returns ptr to static
1200 * End result: "Moves: (fuel)moves_left" */
1201 astr_add_line(astr, "%s: (%s)", _("Moves"),
1203 astr_add(astr, "%s",
1205 } else {
1206 astr_add_line(astr, "%s: %s", _("Moves"),
1208 }
1209 return;
1210 case ACTIVITY_CLEAN:
1211 case ACTIVITY_TRANSFORM:
1213 case ACTIVITY_FORTIFIED:
1214 case ACTIVITY_SENTRY:
1215 case ACTIVITY_GOTO:
1216 case ACTIVITY_EXPLORE:
1217 case ACTIVITY_CONVERT:
1218 case ACTIVITY_CULTIVATE:
1219 case ACTIVITY_PLANT:
1221 return;
1222 case ACTIVITY_MINE:
1223 case ACTIVITY_IRRIGATE:
1224 if (punit->activity_target == NULL) {
1226 } else {
1227 astr_add_line(astr, "Building %s",
1229 }
1230 return;
1231 case ACTIVITY_PILLAGE:
1232 if (punit->activity_target != NULL) {
1235 } else {
1237 }
1238 return;
1239 case ACTIVITY_BASE:
1242 return;
1243 case ACTIVITY_GEN_ROAD:
1246 return;
1247 case ACTIVITY_LAST:
1248 break;
1249 }
1250
1251 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1253}
1254
1255/**********************************************************************/
1261void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1262{
1263 if (!punit || !astr) {
1264 return;
1265 }
1266
1267 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1269 punit->upkeep[O_GOLD]);
1270}
1271
1272/**********************************************************************/
1275struct player *unit_nationality(const struct unit *punit)
1276{
1278 return punit->nationality;
1279}
1280
1281/**********************************************************************/
1285void unit_tile_set(struct unit *punit, struct tile *ptile)
1286{
1288 punit->tile = ptile;
1289}
1290
1291/**********************************************************************/
1298struct unit *tile_allied_unit(const struct tile *ptile,
1299 const struct player *pplayer)
1300{
1301 struct unit *punit = NULL;
1302
1303 unit_list_iterate(ptile->units, cunit) {
1304 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1305 punit = cunit;
1306 } else {
1307 return NULL;
1308 }
1309 }
1311
1312 return punit;
1313}
1314
1315/**********************************************************************/
1321struct unit *tile_enemy_unit(const struct tile *ptile,
1322 const struct player *pplayer)
1323{
1324 unit_list_iterate(ptile->units, punit) {
1325 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1326 return punit;
1327 }
1329
1330 return NULL;
1331}
1332
1333/**********************************************************************/
1336struct unit *tile_non_allied_unit(const struct tile *ptile,
1337 const struct player *pplayer,
1339{
1340 unit_list_iterate(ptile->units, punit) {
1341 struct player *owner = unit_owner(punit);
1342
1343 if (everyone_non_allied && owner != pplayer) {
1344 return punit;
1345 }
1346
1347 if (!pplayers_allied(owner, pplayer)
1348 || is_flagless_to_player(punit, pplayer)) {
1349 return punit;
1350 }
1351 }
1353
1354 return NULL;
1355}
1356
1357/**********************************************************************/
1361struct unit *tile_other_players_unit(const struct tile *ptile,
1362 const struct player *pplayer)
1363{
1364 unit_list_iterate(ptile->units, punit) {
1365 if (unit_owner(punit) != pplayer) {
1366 return punit;
1367 }
1369
1370 return NULL;
1371}
1372
1373/**********************************************************************/
1377struct unit *tile_non_attack_unit(const struct tile *ptile,
1378 const struct player *pplayer)
1379{
1380 unit_list_iterate(ptile->units, punit) {
1381 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1382 return punit;
1383 }
1384 }
1386
1387 return NULL;
1388}
1389
1390/**********************************************************************/
1398struct unit *unit_occupies_tile(const struct tile *ptile,
1399 const struct player *pplayer)
1400{
1401 unit_list_iterate(ptile->units, punit) {
1402 if (!is_occupying_unit(punit)) {
1403 continue;
1404 }
1405
1407 continue;
1408 }
1409
1410 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1411 return punit;
1412 }
1414
1415 return NULL;
1416}
1417
1418/**********************************************************************/
1425bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1426 const struct civ_map *zmap)
1427{
1429
1430 square_iterate(zmap, ptile0, 1, ptile) {
1431 struct terrain *pterrain;
1432 struct city *pcity;
1433
1434 pterrain = tile_terrain(ptile);
1435 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1436 continue;
1437 }
1438
1439 pcity = tile_non_allied_city(ptile, pplayer);
1440 if (pcity != NULL) {
1441 if (unit_list_size(ptile->units) > 0) {
1442 /* Occupied enemy city, it doesn't matter if units inside have
1443 * UTYF_NOZOC or not. */
1444 return FALSE;
1445 }
1446 } else {
1447 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1449 if (tile_has_extra(ptile, pextra)) {
1450 return FALSE;
1451 }
1453 }
1454
1455 unit_list_iterate(ptile->units, punit) {
1456 if (!pplayers_allied(unit_owner(punit), pplayer)
1459 bool hidden = FALSE;
1460
1461 /* We do NOT check the possibility that player is allied with an extra owner,
1462 * and should thus see inside the extra.
1463 * This is to avoid the situation where having an alliance with third player
1464 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1465 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1466 if (tile_has_extra(ptile, pextra)) {
1467 hidden = TRUE;
1468 break;
1469 }
1471
1472 if (!hidden) {
1473 return FALSE;
1474 }
1475 }
1477 }
1479
1480 return TRUE;
1481}
1482
1483/**********************************************************************/
1494bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1495 const struct civ_map *zmap)
1496{
1498
1499 square_iterate(zmap, ptile0, 1, ptile) {
1500 struct terrain *pterrain;
1501 struct city *pcity;
1502
1503 pterrain = tile_terrain(ptile);
1504 if (T_UNKNOWN == pterrain
1505 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1506 continue;
1507 }
1508
1509 pcity = tile_non_allied_city(ptile, pplayer);
1510 if (pcity != NULL) {
1511 if (pcity->client.occupied
1512 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1513 /* Occupied enemy city, it doesn't matter if units inside have
1514 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1515 return FALSE;
1516 }
1517 } else {
1518 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1520 if (tile_has_extra(ptile, pextra)) {
1521 return FALSE;
1522 }
1524 }
1525
1526 unit_list_iterate(ptile->units, punit) {
1528 && !pplayers_allied(unit_owner(punit), pplayer)
1530 return FALSE;
1531 }
1533 }
1535
1536 return TRUE;
1537}
1538
1539/**********************************************************************/
1547
1548/**********************************************************************/
1557{
1558 const struct tile *ptile = unit_tile(punit);
1560 const struct civ_map *nmap = &(wld.map);
1561
1562 if (!is_attack_unit(punit)) {
1563 return FALSE;
1564 }
1565 if (tile_city(ptile)) {
1566 return FALSE;
1567 }
1569 switch (game.info.happyborders) {
1570 case HB_DISABLED:
1571 break;
1572 case HB_NATIONAL:
1573 if (tile_owner(ptile) == unit_owner(punit)) {
1574 return FALSE;
1575 }
1576 break;
1577 case HB_ALLIANCE:
1579 return FALSE;
1580 }
1581 break;
1582 }
1583 }
1584
1587 if (max_friendliness_range >= 0) {
1589 }
1590
1591 return TRUE;
1592}
1593
1594/**********************************************************************/
1598{
1599 switch (activity) {
1600 case ACTIVITY_MINE:
1601 case ACTIVITY_IRRIGATE:
1602 case ACTIVITY_BASE:
1603 case ACTIVITY_GEN_ROAD:
1604 return TRUE;
1605 default:
1606 return FALSE;
1607 }
1608}
1609
1610/**********************************************************************/
1614{
1615 switch (activity) {
1616 case ACTIVITY_PILLAGE:
1617 case ACTIVITY_CLEAN:
1618 return TRUE;
1619 default:
1620 return FALSE;
1621 }
1622}
1623
1624/**********************************************************************/
1628{
1629 switch (activity) {
1630 case ACTIVITY_CULTIVATE:
1631 case ACTIVITY_PLANT:
1632 case ACTIVITY_TRANSFORM:
1633 return TRUE;
1634 default:
1635 return FALSE;
1636 }
1637}
1638
1639/**********************************************************************/
1643{
1644 return is_build_activity(activity)
1645 || is_clean_activity(activity)
1646 || is_terrain_change_activity(activity);
1647}
1648
1649/**********************************************************************/
1653{
1654 return is_build_activity(activity)
1655 || is_clean_activity(activity);
1656}
1657
1658/**********************************************************************/
1662struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1663 const struct unit_type *punittype,
1664 int veteran_level)
1665{
1666 /* Make sure that contents of unit structure are correctly initialized,
1667 * if you ever allocate it by some other mean than fc_calloc() */
1668 struct unit *punit = fc_calloc(1, sizeof(*punit));
1669 int max_vet_lvl;
1670
1671 /* It does not register the unit so the id is set to 0. */
1673
1674 fc_assert_ret_val(punittype != nullptr, nullptr); /* No untyped units! */
1676
1677 fc_assert_ret_val(!is_server() || pplayer != nullptr, nullptr); /* No unowned units! */
1678 punit->owner = pplayer;
1679 punit->nationality = pplayer;
1680
1681 punit->refcount = 1;
1683
1684 if (pcity != nullptr) {
1685 unit_tile_set(punit, pcity->tile);
1686 punit->homecity = pcity->id;
1687 } else {
1688 unit_tile_set(punit, nullptr);
1690 }
1691
1692 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1693 punit->goto_tile = nullptr;
1696 /* A unit new and fresh ... */
1700 /* Random moves units start with zero movement as their first movement
1701 * will be only after their moves have been reset in the beginning of
1702 * the next turn. */
1703 punit->moves_left = 0;
1704 } else {
1706 }
1707 punit->moved = FALSE;
1708
1712
1713 punit->transporter = nullptr;
1715
1716 punit->carrying = nullptr;
1717
1721
1723 punit->action_decision_tile = nullptr;
1724
1725 punit->stay = FALSE;
1726
1729
1730 if (is_server()) {
1732
1734
1735 punit->server.removal_callback = nullptr;
1736
1738 O_LAST * sizeof(*punit->server.upkeep_paid));
1739
1740 punit->server.ord_map = 0;
1741 punit->server.ord_city = 0;
1742
1743 punit->server.vision = nullptr; /* No vision. */
1745 /* Must be an invalid turn number, and an invalid previous turn
1746 * number. */
1747 punit->server.action_turn = -2;
1748 /* punit->server.moving = NULL; set by fc_calloc(). */
1749
1750 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1751
1752 CALL_FUNC_EACH_AI(unit_alloc, punit);
1753 } else {
1757 punit->client.act_prob_cache = nullptr;
1758 }
1759
1760 return punit;
1761}
1762
1763/**********************************************************************/
1768{
1770
1771 /* Unload unit if transported. */
1774
1775 /* Check for transported units. Use direct access to the list. */
1776 if (unit_list_size(punit->transporting) != 0) {
1777 /* Unload all units. */
1781 }
1783
1784 if (punit->transporting) {
1786 }
1787
1788 CALL_FUNC_EACH_AI(unit_free, punit);
1789
1790 if (is_server() && punit->server.adv) {
1792 } else {
1795 }
1796 }
1797
1798 if (--punit->refcount <= 0) {
1799 FC_FREE(punit);
1800 }
1801}
1802
1803/**********************************************************************/
1808{
1809 if (punit->has_orders) {
1810 punit->goto_tile = NULL;
1812 punit->orders.list = NULL;
1813 }
1814 punit->orders.length = 0;
1816}
1817
1818/**********************************************************************/
1822{
1824
1825 return unit_list_size(ptrans->transporting);
1826}
1827
1828/**********************************************************************/
1831static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1832 const struct tile *ptile,
1833 bool (*unit_load_test)
1834 (const struct unit *pc,
1835 const struct unit *pt))
1836{
1837 struct unit *best_trans = NULL;
1838 struct {
1841 } cur, best = { FALSE };
1842
1843 unit_list_iterate(ptile->units, ptrans) {
1844 if (!unit_load_test(pcargo, ptrans)) {
1845 continue;
1846 } else if (best_trans == NULL) {
1848 }
1849
1850 /* Gather data from transport stack in a single pass, for use in
1851 * various conditions below. */
1853 cur.outermost_moves_left = ptrans->moves_left;
1854 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1857 cur.has_orders = TRUE;
1858 }
1859 cur.outermost_moves_left = ptranstrans->moves_left;
1860 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1862
1863 /* Criteria for deciding the 'best' transport to load onto.
1864 * The following tests are applied in order; earlier ones have
1865 * lexicographically greater significance than later ones. */
1866
1867 /* Transports which have orders, or are on transports with orders,
1868 * are less preferable to transport stacks without orders (to
1869 * avoid loading on units that are just passing through). */
1870 if (best_trans != ptrans) {
1871 if (!cur.has_orders && best.has_orders) {
1873 } else if (cur.has_orders && !best.has_orders) {
1874 continue;
1875 }
1876 }
1877
1878 /* Else, transports which are idle are preferable (giving players
1879 * some control over loading) -- this does not check transports
1880 * of transports. */
1881 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1882 if (best_trans != ptrans) {
1883 if (cur.is_idle && !best.is_idle) {
1885 } else if (!cur.is_idle && best.is_idle) {
1886 continue;
1887 }
1888 }
1889
1890 /* Else, transports from which the cargo could unload at any time
1891 * are preferable to those where the cargo can only disembark in
1892 * cities/bases. */
1893 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1895 if (best_trans != ptrans) {
1896 if (cur.can_freely_unload && !best.can_freely_unload) {
1898 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1899 continue;
1900 }
1901 }
1902
1903 /* Else, transports which are less deeply nested are preferable. */
1905 if (best_trans != ptrans) {
1906 if (cur.depth < best.depth) {
1908 } else if (cur.depth > best.depth) {
1909 continue;
1910 }
1911 }
1912
1913 /* Else, transport stacks where the outermost transport has more
1914 * moves left are preferable (on the assumption that it's the
1915 * outermost transport that's about to move). */
1916 if (best_trans != ptrans) {
1917 if (cur.outermost_moves_left > best.outermost_moves_left) {
1919 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1920 continue;
1921 }
1922 }
1923
1924 /* All other things being equal, as a tie-breaker, compare the total
1925 * moves left (this turn) and move rate (future turns) for the whole
1926 * stack, to take into account total potential movement for both
1927 * short and long journeys (we don't know which the cargo intends to
1928 * make). Doesn't try to account for whether transports can unload,
1929 * etc. */
1930 if (best_trans != ptrans) {
1931 if (cur.total_moves > best.total_moves) {
1933 } else {
1934 continue;
1935 }
1936 }
1937
1939 best = cur;
1941
1942 return best_trans;
1943}
1944
1945/**********************************************************************/
1954
1955/**********************************************************************/
1961 const struct tile *ptile)
1962{
1964}
1965
1966/**********************************************************************/
1971 const struct unit *punit)
1972{
1974 return FALSE;
1975 }
1976
1979 return FALSE;
1980 }
1982
1983 return TRUE;
1984}
1985
1986/**********************************************************************/
1995 const struct unit *punit,
1996 const struct unit_type *to_unittype)
1997{
1999
2001 return UU_NOT_ENOUGH_ROOM;
2002 }
2003
2004 if (punit->transporter != NULL) {
2008 }
2010 /* The new unit type can't survive on this terrain. */
2011 return UU_NOT_TERRAIN;
2012 }
2013
2014 return UU_OK;
2015}
2016
2017/**********************************************************************/
2027 const struct unit *punit,
2028 bool is_free)
2029{
2030 struct player *pplayer = unit_owner(punit);
2031 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2033 struct city *pcity;
2034 int cost;
2035
2036 if (!to_unittype) {
2037 return UU_NO_UNITTYPE;
2038 }
2039
2041 /* TODO: There may be other activities that the upgraded unit is not
2042 allowed to do, which we could also test.
2043 -
2044 If convert were legal for new unit_type we could allow, but then it
2045 has a 'head start' getting activity time from the old conversion. */
2046 return UU_NOT_ACTIVITY;
2047 }
2048
2049 if (!is_free) {
2051 if (pplayer->economic.gold < cost) {
2052 return UU_NO_MONEY;
2053 }
2054
2055 pcity = tile_city(unit_tile(punit));
2056 if (!pcity) {
2057 return UU_NOT_IN_CITY;
2058 }
2059 if (city_owner(pcity) != pplayer) {
2060 /* TODO: Should upgrades in allied cities be possible? */
2061 return UU_NOT_CITY_OWNER;
2062 }
2063 }
2064
2065 /* TODO: Allow transported units to be reassigned. Check here
2066 * and make changes to upgrade_unit. */
2068}
2069
2070/**********************************************************************/
2073bool unit_can_convert(const struct civ_map *nmap,
2074 const struct unit *punit)
2075{
2076 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2077
2078 if (tgt == NULL) {
2079 return FALSE;
2080 }
2081
2082 return UU_OK == unit_transform_result(nmap, punit, tgt);
2083}
2084
2085/**********************************************************************/
2090 const struct unit *punit,
2091 char *buf, size_t bufsz)
2092{
2093 struct player *pplayer = unit_owner(punit);
2095 int upgrade_cost;
2096 const struct unit_type *from_unittype = unit_type_get(punit);
2097 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2099 char tbuf[MAX_LEN_MSG];
2100
2101 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2102 "Treasury contains %d gold.",
2103 pplayer->economic.gold),
2104 pplayer->economic.gold);
2105
2106 switch (result) {
2107 case UU_OK:
2109 /* This message is targeted toward the GUI callers. */
2110 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2111 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2112 "Upgrade %s to %s for %d gold?\n%s",
2113 upgrade_cost),
2117 break;
2118 case UU_NO_UNITTYPE:
2120 _("Sorry, cannot upgrade %s (yet)."),
2122 break;
2123 case UU_NO_MONEY:
2125 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2126 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2127 "Upgrading %s to %s costs %d gold.\n%s",
2128 upgrade_cost),
2132 break;
2133 case UU_NOT_IN_CITY:
2134 case UU_NOT_CITY_OWNER:
2136 _("You can only upgrade units in your cities."));
2137 break;
2138 case UU_NOT_ENOUGH_ROOM:
2140 _("Upgrading this %s would strand units it transports."),
2142 break;
2143 case UU_NOT_TERRAIN:
2145 _("Upgrading this %s would result in a %s which can not "
2146 "survive at this place."),
2149 break;
2152 _("Upgrading this %s would result in a %s which its "
2153 "current transport, %s, could not transport."),
2157 break;
2158 case UU_NOT_ACTIVITY:
2160 _("Cannot upgrade %s while doing '%s'."),
2163 break;
2164 }
2165
2166 return result;
2167}
2168
2169/**********************************************************************/
2174 const struct unit *punit)
2175{
2176 int mpco;
2177
2179 &(const struct req_context) {
2180 .player = unit_owner(punit),
2181 .city = unit_tile(punit)
2183 : NULL,
2184 .tile = unit_tile(punit),
2185 .unit = punit,
2186 .unittype = unit_type_get(punit),
2187 .action = paction,
2188 },
2189 NULL,
2191
2193
2194 return mpco;
2195}
2196
2197/**********************************************************************/
2200int hp_gain_coord(const struct unit *punit)
2201{
2202 int hp = 0;
2203 const int base = unit_type_get(punit)->hp;
2204 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2205
2206 /* Includes barracks (100%), fortress (25%), etc. */
2208
2209 /* Minimum HP after regen effects applied. */
2210 hp = MAX(hp, min);
2211
2212 /* Regen2 effects that apply after there's at least Min HP */
2213 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2214
2215 return MAX(hp, 0);
2216}
2217
2218/**********************************************************************/
2222{
2223 const struct unit_type *utype = unit_type_get(punit);
2225 struct city *pcity = tile_city(unit_tile(punit));
2226 int gain;
2227
2228 if (!punit->moved) {
2230 } else {
2231 gain = 0;
2232 }
2233
2235
2236 if (!punit->homecity && 0 < game.server.killunhomed
2238 /* Hit point loss of units without homecity; at least 1 hp! */
2239 /* Gameloss units are immune to this effect. */
2240 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2241
2242 if (gain > hp_loss) {
2243 gain = -1;
2244 } else {
2245 gain -= hp_loss;
2246 }
2247 }
2248
2249 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2250 && !unit_transported(punit)) {
2251 gain -= utype->hp * pclass->hp_loss_pct / 100;
2252 }
2253
2254 if (punit->hp + gain > utype->hp) {
2255 gain = utype->hp - punit->hp;
2256 } else if (punit->hp + gain < 0) {
2257 gain = -punit->hp;
2258 }
2259
2260 return gain;
2261}
2262
2263/**********************************************************************/
2266bool is_losing_hp(const struct unit *punit)
2267{
2268 const struct unit_type *punittype = unit_type_get(punit);
2269
2271 < (punittype->hp *
2272 utype_class(punittype)->hp_loss_pct / 100);
2273}
2274
2275/**********************************************************************/
2278bool unit_type_is_losing_hp(const struct player *pplayer,
2279 const struct unit_type *punittype)
2280{
2281 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2283 < (punittype->hp *
2284 utype_class(punittype)->hp_loss_pct / 100);
2285}
2286
2287/**********************************************************************/
2291bool unit_is_alive(int id)
2292{
2293 /* Check if unit exist in game */
2294 if (game_unit_by_number(id)) {
2295 return TRUE;
2296 }
2297
2298 return FALSE;
2299}
2300
2301/**********************************************************************/
2308bool unit_is_virtual(const struct unit *punit)
2309{
2310 if (!punit) {
2311 return FALSE;
2312 }
2313
2314 return punit != game_unit_by_number(punit->id);
2315}
2316
2317/**********************************************************************/
2320void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2321{
2322 return punit->server.ais[ai_type_number(ai)];
2323}
2324
2325/**********************************************************************/
2328void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2329 void *data)
2330{
2331 punit->server.ais[ai_type_number(ai)] = data;
2332}
2333
2334/**********************************************************************/
2345int unit_bribe_cost(const struct unit *punit, const struct player *briber,
2346 const struct unit *briber_unit)
2347{
2348 int cost, default_hp;
2349 struct tile *ptile = unit_tile(punit);
2350 struct player *owner = unit_owner(punit);
2351 const struct unit_type *ptype = unit_type_get(punit);
2352
2354
2355 default_hp = ptype->hp;
2356
2358
2359 if (owner != nullptr) {
2360 int dist = 0;
2361
2362 cost += owner->economic.gold;
2363
2364 /* Consider the distance to the capital. */
2366 city_list_iterate(owner->cities, capital) {
2367 if (is_capital(capital)) {
2368 int tmp = map_distance(capital->tile, ptile);
2369
2370 if (tmp < dist) {
2371 dist = tmp;
2372 }
2373 }
2375
2376 cost /= dist + 2;
2377 }
2378
2379 /* Consider the build cost. */
2381
2382 /* Rule set specific cost modification */
2384 &(const struct req_context) {
2385 .player = owner,
2387 .tile = ptile,
2388 .unit = punit,
2389 .unittype = ptype,
2390 },
2391 &(const struct req_context) {
2392 .player = briber,
2393 .unit = briber_unit,
2395 : nullptr,
2397 : nullptr,
2398 .city = briber_unit
2399 ? game_city_by_number(briber_unit->homecity)
2400 : nullptr,
2401 },
2403 / 100;
2404
2405 /* Veterans are not cheap. */
2406 {
2407 const struct veteran_level *vlevel
2409
2411 cost = cost * vlevel->power_fact / 100;
2412 if (ptype->move_rate > 0) {
2413 cost += cost * vlevel->move_bonus / ptype->move_rate;
2414 } else {
2415 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2416 }
2417 }
2418
2419 /* Cost now contains the basic bribe cost. We now reduce it by:
2420 * bribecost = cost / 2 + cost / 2 * damage / hp
2421 * = cost / 2 * (1 + damage / hp) */
2422 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2423}
2424
2425/**********************************************************************/
2428bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2429{
2432
2434
2435 if (force || can_unit_load(pcargo, ptrans)) {
2436 pcargo->transporter = ptrans;
2437 unit_list_append(ptrans->transporting, pcargo);
2438
2439 return TRUE;
2440 }
2441
2442 return FALSE;
2443}
2444
2445/**********************************************************************/
2449{
2450 struct unit *ptrans;
2451
2453
2454 if (!unit_transported(pcargo)) {
2455 /* 'pcargo' is not transported. */
2456 return FALSE;
2457 }
2458
2459 /* Get the transporter; must not be defined on the client! */
2461 if (ptrans) {
2462 /* 'pcargo' and 'ptrans' should be on the same tile. */
2464
2465#ifndef FREECIV_NDEBUG
2466 bool success =
2467#endif
2468 unit_list_remove(ptrans->transporting, pcargo);
2469
2470 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2472 }
2473
2474 /* For the server (also safe for the client). */
2475 pcargo->transporter = NULL;
2476
2477 return TRUE;
2478}
2479
2480/**********************************************************************/
2483bool unit_transported(const struct unit *pcargo)
2484{
2486
2487 /* The unit is transported if a transporter unit is set or, (for the client)
2488 * if the transported_by field is set. */
2489 if (is_server()) {
2491 } else {
2493 }
2494}
2495
2496/**********************************************************************/
2499struct unit *unit_transport_get(const struct unit *pcargo)
2500{
2502
2503 return pcargo->transporter;
2504}
2505
2506/**********************************************************************/
2509struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2510{
2512 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2513
2514 return ptrans->transporting;
2515}
2516
2517/**********************************************************************/
2520static inline bool
2528
2529/**********************************************************************/
2538 const struct unit *ptrans)
2539{
2540 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2541
2542 /* Check 'pcargo' against 'ptrans'. */
2544 return FALSE;
2545 }
2546
2547 /* Check 'pcargo' against 'ptrans' parents. */
2550 return FALSE;
2551 }
2553
2554 /* Check cargo children... */
2557
2558 /* ...against 'ptrans'. */
2560 return FALSE;
2561 }
2562
2563 /* ...and against 'ptrans' parents. */
2566 return FALSE;
2567 }
2570
2571 return TRUE;
2572}
2573
2574/**********************************************************************/
2578bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2579{
2581 if (ptrans == plevel) {
2582 return TRUE;
2583 }
2585 return FALSE;
2586}
2587
2588/**********************************************************************/
2591int unit_cargo_depth(const struct unit *ptrans)
2592{
2593 struct cargo_iter iter;
2594 struct iterator *it;
2595 int depth = 0;
2596
2597 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2598 iterator_next(it)) {
2599 if (iter.depth > depth) {
2600 depth = iter.depth;
2601 }
2602 }
2603 return depth;
2604}
2605
2606/**********************************************************************/
2610{
2611 int level = 0;
2612
2614 level++;
2616 return level;
2617}
2618
2619/**********************************************************************/
2623{
2624 return sizeof(struct cargo_iter);
2625}
2626
2627/**********************************************************************/
2630static void *cargo_iter_get(const struct iterator *it)
2631{
2632 const struct cargo_iter *iter = CARGO_ITER(it);
2633
2634 return unit_list_link_data(iter->links[iter->depth - 1]);
2635}
2636
2637/**********************************************************************/
2640static void cargo_iter_next(struct iterator *it)
2641{
2642 struct cargo_iter *iter = CARGO_ITER(it);
2643 const struct unit_list_link *piter;
2644 const struct unit_list_link *pnext;
2645
2646 /* Variant 1: unit has cargo. */
2648 if (NULL != pnext) {
2649 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2650 iter->links[iter->depth++] = pnext;
2651 return;
2652 }
2653
2654 fc_assert(iter->depth > 0);
2655
2656 while (iter->depth > 0) {
2657 piter = iter->links[iter->depth - 1];
2658
2659 /* Variant 2: there are other cargo units at same level. */
2661 if (NULL != pnext) {
2662 iter->links[iter->depth - 1] = pnext;
2663 return;
2664 }
2665
2666 /* Variant 3: return to previous level, and do same tests. */
2667 iter->depth--;
2668 }
2669}
2670
2671/**********************************************************************/
2674static bool cargo_iter_valid(const struct iterator *it)
2675{
2676 return (0 < CARGO_ITER(it)->depth);
2677}
2678
2679/**********************************************************************/
2683 const struct unit *ptrans)
2684{
2685 struct iterator *it = ITERATOR(iter);
2686
2687 it->get = cargo_iter_get;
2688 it->next = cargo_iter_next;
2689 it->valid = cargo_iter_valid;
2691 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2692
2693 return it;
2694}
2695
2696/**********************************************************************/
2699bool unit_is_cityfounder(const struct unit *punit)
2700{
2702}
2703
2704/**********************************************************************/
2708 int length, const struct unit_order *orders)
2709{
2710 int i;
2711
2712 for (i = 0; i < length; i++) {
2713 struct action *paction;
2714 struct extra_type *pextra;
2715
2716 if (orders[i].order > ORDER_LAST) {
2717 log_error("invalid order %d at index %d", orders[i].order, i);
2718 return FALSE;
2719 }
2720 switch (orders[i].order) {
2721 case ORDER_MOVE:
2722 case ORDER_ACTION_MOVE:
2723 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2724 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2725 return FALSE;
2726 }
2727 break;
2728 case ORDER_ACTIVITY:
2729 switch (orders[i].activity) {
2730 case ACTIVITY_SENTRY:
2731 if (i != length - 1) {
2732 /* Only allowed as the last order. */
2733 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2734 i);
2735 return FALSE;
2736 }
2737 break;
2738 /* Replaced by action orders */
2739 case ACTIVITY_BASE:
2740 case ACTIVITY_GEN_ROAD:
2741 case ACTIVITY_CLEAN:
2742 case ACTIVITY_PILLAGE:
2743 case ACTIVITY_MINE:
2744 case ACTIVITY_IRRIGATE:
2745 case ACTIVITY_PLANT:
2746 case ACTIVITY_CULTIVATE:
2747 case ACTIVITY_TRANSFORM:
2748 case ACTIVITY_CONVERT:
2750 log_error("at index %d, use action rather than activity %d.",
2751 i, orders[i].activity);
2752 return FALSE;
2753 /* Not supported. */
2754 case ACTIVITY_EXPLORE:
2755 case ACTIVITY_IDLE:
2756 /* Not set from the client. */
2757 case ACTIVITY_GOTO:
2758 case ACTIVITY_FORTIFIED:
2759 /* Unused. */
2760 case ACTIVITY_LAST:
2761 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2762 return FALSE;
2763 }
2764
2765 break;
2767 if (!action_id_exists(orders[i].action)) {
2768 /* Non-existent action. */
2769 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2770 return FALSE;
2771 }
2772
2773 paction = action_by_number(orders[i].action);
2774
2775 /* Validate main target. */
2776 if (index_to_tile(nmap, orders[i].target) == NULL) {
2777 log_error("at index %d, invalid tile target %d for the action %d.",
2778 i, orders[i].target, orders[i].action);
2779 return FALSE;
2780 }
2781
2782 if (orders[i].dir != DIR8_ORIGIN) {
2783 log_error("at index %d, the action %d sets the outdated target"
2784 " specification dir.",
2785 i, orders[i].action);
2786 }
2787
2788 /* Validate sub target. */
2789 switch (action_id_get_sub_target_kind(orders[i].action)) {
2790 case ASTK_BUILDING:
2791 /* Sub target is a building. */
2792 if (!improvement_by_number(orders[i].sub_target)) {
2793 /* Sub target is invalid. */
2794 log_error("at index %d, cannot do %s without a target.", i,
2795 action_id_rule_name(orders[i].action));
2796 return FALSE;
2797 }
2798 break;
2799 case ASTK_TECH:
2800 /* Sub target is a technology. */
2801 if (orders[i].sub_target == A_NONE
2802 || (!valid_advance_by_number(orders[i].sub_target)
2803 && orders[i].sub_target != A_FUTURE)) {
2804 /* Target tech is invalid. */
2805 log_error("at index %d, cannot do %s without a target.", i,
2806 action_id_rule_name(orders[i].action));
2807 return FALSE;
2808 }
2809 break;
2810 case ASTK_EXTRA:
2812 /* Sub target is an extra. */
2813 pextra = (!(orders[i].sub_target == NO_TARGET
2814 || (orders[i].sub_target < 0
2815 || (orders[i].sub_target
2817 ? extra_by_number(orders[i].sub_target) : NULL);
2818 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2819 if (pextra == NULL) {
2820 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2821 /* Target extra is invalid. */
2822 log_error("at index %d, cannot do %s without a target.", i,
2823 action_id_rule_name(orders[i].action));
2824 return FALSE;
2825 }
2826 } else {
2827 if (!actres_removes_extra(paction->result, pextra)
2828 && !actres_creates_extra(paction->result, pextra)) {
2829 /* Target extra is irrelevant for the action. */
2830 log_error("at index %d, cannot do %s to %s.", i,
2831 action_id_rule_name(orders[i].action),
2832 extra_rule_name(pextra));
2833 return FALSE;
2834 }
2835 }
2836 break;
2837 case ASTK_NONE:
2838 /* No validation required. */
2839 break;
2840 /* Invalid action? */
2841 case ASTK_COUNT:
2844 FALSE,
2845 "Bad action %d in order number %d.", orders[i].action, i);
2846 }
2847
2848 /* Some action orders are sane only in the last order. */
2849 if (i != length - 1) {
2850 /* If the unit is dead, */
2852 /* or if Freeciv has no idea where the unit will end up after it
2853 * has performed this action, */
2856 /* or if the unit will end up standing still, */
2858 /* than having this action in the middle of a unit's orders is
2859 * probably wrong. */
2860 log_error("action %d is not allowed at index %d.",
2861 orders[i].action, i);
2862 return FALSE;
2863 }
2864 }
2865
2866 /* Don't validate that the target tile really contains a target or
2867 * that the actor player's map think the target tile has one.
2868 * The player may target something from their player map that isn't
2869 * there any more, a target they think is there even if their player
2870 * map doesn't have it, or even a target they assume will be there
2871 * when the unit reaches the target tile.
2872 *
2873 * With that said: The client should probably at least have an
2874 * option to only aim city targeted actions at cities. */
2875
2876 break;
2877 case ORDER_FULL_MP:
2878 break;
2879 case ORDER_LAST:
2880 /* An invalid order. This is handled above. */
2881 break;
2882 }
2883 }
2884
2885 return TRUE;
2886}
2887
2888/**********************************************************************/
2893 int length,
2894 const struct unit_order *orders)
2895{
2896 struct unit_order *unit_orders;
2897
2898 if (!unit_order_list_is_sane(nmap, length, orders)) {
2899 return NULL;
2900 }
2901
2902 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2903 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2904
2905 return unit_orders;
2906}
2907
2908/**********************************************************************/
2912{
2914 ACTION_NONE, // ACTIVITY_IDLE
2915 ACTION_CULTIVATE, // ACTIVITY_CULTIVATE
2916 ACTION_MINE, // ACTIVITY_MINE
2917 ACTION_IRRIGATE, // ACTIVITY_IRRIGATE
2918 ACTION_FORTIFY, // ACTIVITY_FORTIFIED
2919 ACTION_NONE, // ACTIVITY_SENTRY
2920 ACTION_PILLAGE, // ACTIVITY_PILLAGE
2921 ACTION_NONE, // ACTIVITY_GOTO
2922 ACTION_NONE, // ACTIVITY_EXPLORE
2923 ACTION_TRANSFORM_TERRAIN, // ACTIVITY_TRANSFORM
2924 ACTION_FORTIFY, // ACTIVITY_FORTIFYING
2925 ACTION_CLEAN, // ACTIVITY_CLEAN
2926 ACTION_BASE, // ACTIVITY_BASE
2927 ACTION_ROAD, // ACTIVITY_GEN_ROAD
2928 ACTION_CONVERT, // ACTIVITY_CONVERT
2929 ACTION_PLANT // ACTIVITY_PLANT
2930 };
2931
2932 return act_act[act];
2933}
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5044
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1214
struct act_prob action_prob_vs_tile(const struct unit *actor_unit, const action_id act_id, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:4562
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:3293
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:3052
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:3129
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:4203
bool action_maybe_possible_actor_unit(const struct civ_map *nmap, const action_id act_id, const struct unit *actor_unit)
Definition actions.c:5467
bool action_id_exists(const action_id act_id)
Definition actions.c:1043
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:3442
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:4286
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:236
#define action_id_get_sub_target_kind(act_id)
Definition actions.h:411
static struct action * action_by_number(action_id act_id)
Definition actions.h:390
#define action_has_result(_act_, _res_)
Definition actions.h:175
#define action_by_result_iterate_end
Definition actions.h:240
#define action_id(_act_)
Definition actions.h:416
#define ACTION_NONE
Definition actions.h:55
bool actres_removes_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:806
bool actres_creates_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:785
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:95
#define BV_SET(bv, bit)
Definition bitvector.h:81
bool is_capital(const struct city *pcity)
Definition city.c:1575
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2062
bool is_unit_near_a_friendly_city(const struct civ_map *nmap, const struct unit *punit, int distance)
Definition city.c:2077
#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:74
#define MAX_LEN_MSG
Definition conn_types.h:37
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2938
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:74
int get_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:358
enum unit_activity Activity_type_id
Definition fc_types.h:383
#define DIR8_ORIGIN
Definition fc_types.h:459
int action_id
Definition fc_types.h:393
@ HB_ALLIANCE
Definition fc_types.h:1273
@ HB_DISABLED
Definition fc_types.h:1271
@ HB_NATIONAL
Definition fc_types.h:1272
@ 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:1017
#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 world wld
Definition game.c:62
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:752
#define GAME_TRANSPORT_MAX_RECURSIVE
Definition game.h:755
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:181
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_error(message,...)
Definition log.h:103
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition log.h:208
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:462
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:950
bool map_untrusted_dir_is_valid(enum direction8 dir)
Definition map.c:1288
enum direction8 rand_direction(void)
Definition map.c:1958
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:660
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:377
#define square_iterate_end
Definition map.h:380
#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:320
int id
Definition city.h:326
bool occupied
Definition city.h:460
struct tile * tile
Definition city.h:322
struct city::@17::@20 client
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
int killunhomed
Definition game.h:154
struct civ_game::@31::@35 server
bool ruledit_disabled
Definition extras.h:91
bool(* valid)(const struct iterator *it)
Definition iterator.h:34
void *(* get)(const struct iterator *it)
Definition iterator.h:33
void(* next)(struct iterator *it)
Definition iterator.h:32
enum borders_mode borders
enum happyborders_type happyborders
enum airlifting_style airlifting_style
bool airlift_from_always_enabled
bool airlift_to_always_enabled
struct player_economic economic
Definition player.h: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
struct unit_type::@87 adv
bool worker
Definition unittype.h:582
int move_rate
Definition unittype.h:524
enum vision_layer vlayer
Definition unittype.h:576
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
struct unit::@80 orders
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
struct unit::@81::@83 client
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
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::@81::@84 server
struct tile * tile
Definition unit.h:142
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
struct civ_map map
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:406
size_t cargo_iter_sizeof(void)
Definition unit.c:2622
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:945
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1642
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2892
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:1994
bool unit_being_aggressive(const struct unit *punit)
Definition unit.c:1556
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1542
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2278
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1627
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:217
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1821
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2640
void free_unit_orders(struct unit *punit)
Definition unit.c:1807
bool can_unit_change_homecity(const struct unit *punit)
Definition unit.c:509
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2328
bool unit_is_alive(int id)
Definition unit.c:2291
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:287
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:327
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:453
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:2173
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:1494
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2428
bool unit_can_add_or_build_city(const struct unit *punit)
Definition unit.c:475
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1960
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2682
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2089
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2609
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2266
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1652
bool can_unit_deboard_or_be_unloaded(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:801
bool can_unit_change_homecity_to(const struct unit *punit, const struct city *pcity)
Definition unit.c:492
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:749
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1377
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2200
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2630
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1275
int get_activity_rate(const struct unit *punit)
Definition unit.c:522
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2537
bool is_attack_unit(const struct unit *punit)
Definition unit.c:308
const Activity_type_id tile_changing_activities[]
Definition unit.c:44
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2499
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2448
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1597
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2578
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2221
int unit_bribe_cost(const struct unit *punit, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2345
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:866
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1098
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1261
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:2699
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1152
bool unit_can_help_build_wonder_here(const struct unit *punit)
Definition unit.c:261
bool is_field_unit(const struct unit *punit)
Definition unit.c:441
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2674
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:386
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:396
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2911
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:319
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1662
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2591
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:2521
#define CARGO_ITER(iter)
Definition unit.c:54
bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:831
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2707
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:1168
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:300
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1425
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:1133
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:335
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1613
bool unit_can_airlift_to(const struct unit *punit, const struct city *pdest_city)
Definition unit.c:186
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:847
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1185
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:893
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1361
bool is_special_unit(const struct unit *punit)
Definition unit.c:353
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1767
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1321
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1285
void set_unit_activity(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1115
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:2026
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2483
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:2308
bool is_flagless_to_player(const struct unit *punit, const struct player *pplayer)
Definition unit.c:366
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:415
bool is_guard_unit(const struct unit *punit)
Definition unit.c:343
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:1398
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2509
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1970
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2320
bool unit_has_orders(const struct unit *punit)
Definition unit.c:205
struct unit * tile_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1298
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1950
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:1831
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2073
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:1336
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:932
#define unit_tile(_pu)
Definition unit.h:397
#define unit_transports_iterate_end
Definition unit.h:565
#define unit_cargo_iterate_end
Definition unit.h:575
@ 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:543
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:572
#define unit_owner(_pu)
Definition unit.h:396
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:561
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:546
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:1381
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:300
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1569
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1249
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2951
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1587
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1468
bool utype_may_act_move_frags(const struct unit_type *punit_type, const action_id act_id, const int move_fragments)
Definition unittype.c:1058
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1484
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1703
bool utype_can_freely_load(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:288
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2613
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2498
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:387
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Definition unittype.c:2583
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:190
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1219
bool utype_can_do_action_sub_result(const struct unit_type *putype, enum action_sub_result sub_result)
Definition unittype.c:408
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1288
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:443
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1560
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:371
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1731
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:773
#define utype_class(_t_)
Definition unittype.h:756
#define utype_fuel(ptype)
Definition unittype.h:843
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624