Freeciv-3.2
Loading...
Searching...
No Matches
unit.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "astring.h"
20#include "bitvector.h"
21#include "fcintl.h"
22#include "mem.h"
23#include "shared.h"
24#include "support.h"
25
26/* common */
27#include "ai.h"
28#include "actions.h"
29#include "base.h"
30#include "city.h"
31#include "game.h"
32#include "log.h"
33#include "map.h"
34#include "movement.h"
35#include "packets.h"
36#include "player.h"
37#include "road.h"
38#include "tech.h"
39#include "traderoutes.h"
40#include "unitlist.h"
41
42#include "unit.h"
43
48
54#define CARGO_ITER(iter) ((struct cargo_iter *) (iter))
55
56/**********************************************************************/
60 const struct unit_order *order2)
61{
62 return order1->order == order2->order
63 && order1->activity == order2->activity
64 && order1->target == order2->target
65 && order1->sub_target == order2->sub_target
66 && order1->action == order2->action
67 && order1->dir == order2->dir;
68}
69
70/**********************************************************************/
82 const struct player *restriction,
83 const struct unit *punit,
84 const struct city *pdest_city)
85{
86 const struct city *psrc_city = tile_city(unit_tile(punit));
87 const struct player *punit_owner;
89
90 if (0 == punit->moves_left
92 ACTION_AIRLIFT, 0)) {
93 /* No moves left. */
94 return AR_NO_MOVES;
95 }
96
98 return AR_WRONG_UNITTYPE;
99 }
100
102 /* Units with occupants can't be airlifted currently. */
103 return AR_OCCUPIED;
104 }
105
106 if (NULL == psrc_city) {
107 /* No city there. */
108 return AR_NOT_IN_CITY;
109 }
110
111 if (psrc_city == pdest_city) {
112 /* Airlifting to our current position doesn't make sense. */
113 return AR_BAD_DST_CITY;
114 }
115
116 if (pdest_city
117 && (NULL == restriction
119 == TILE_KNOWN_SEEN))
121 /* Can't exist at the destination tile. */
122 return AR_BAD_DST_CITY;
123 }
124
126
127 /* Check validity of both source and destination before checking capacity,
128 * to avoid misleadingly optimistic returns. */
129
133 /* Not allowed to airlift from this source. */
134 return AR_BAD_SRC_CITY;
135 }
136
137 if (pdest_city
141 /* Not allowed to airlift to this destination. */
142 return AR_BAD_DST_CITY;
143 }
144
146 /* We know for sure whether or not src can airlift this turn. */
147 if (0 >= psrc_city->airlift
150 /* The source cannot airlift for this turn (maybe already airlifted
151 * or no airport).
152 * See also do_airline() in server/unittools.h. */
153 return AR_SRC_NO_FLIGHTS;
154 } /* else, there is capacity; continue to other checks */
155 } else {
156 /* We don't have access to the 'airlift' field. Assume it's OK; can
157 * only find out for sure by trying it. */
159 }
160
161 if (pdest_city) {
163 if (0 >= pdest_city->airlift
166 /* The destination cannot support airlifted units for this turn
167 * (maybe already airlifted or no airport).
168 * See also do_airline() in server/unittools.h. */
169 return AR_DST_NO_FLIGHTS;
170 } /* else continue */
171 } else {
173 }
174 }
175
176 return ok_result;
177}
178
179/**********************************************************************/
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
265 if (!pcity) {
266 /* No city to help at this tile. */
267 return FALSE;
268 }
269
271 /* This unit can never do help wonder. */
272 return FALSE;
273 }
274
275 /* Evaluate all action enablers for extra accuracy. */
276 /* TODO: Is it worth it? */
279 pcity));
280}
281
282/**********************************************************************/
295
296/**********************************************************************/
300{
302}
303
304/**********************************************************************/
314
315/**********************************************************************/
318bool is_martial_law_unit(const struct unit *punit)
319{
321}
322
323/**********************************************************************/
326bool is_occupying_unit(const struct unit *punit)
327{
329}
330
331/**********************************************************************/
335{
337}
338
339/**********************************************************************/
342bool is_guard_unit(const struct unit *punit)
343{
345}
346
347/**********************************************************************/
352bool is_special_unit(const struct unit *punit)
353{
355}
356
357/**********************************************************************/
361bool unit_can_do_action(const struct unit *punit,
362 const action_id act_id)
363{
364 return utype_can_do_action(unit_type_get(punit), act_id);
365}
366
367/**********************************************************************/
372 enum action_result result)
373{
375}
376
377/**********************************************************************/
386
387/**********************************************************************/
391 const struct player *pplayer,
392 const struct tile *ptile, bool omniscient)
393{
394 square_iterate(nmap, ptile, 2, ptile1) {
396 if ((omniscient
397 || can_player_see_unit(pplayer, punit))
398 && pplayers_at_war(pplayer, unit_owner(punit))
403 unit_class_get(punit), ptile)))) {
404 return TRUE;
405 }
408
409 return FALSE;
410}
411
412/**********************************************************************/
416bool is_field_unit(const struct unit *punit)
417{
419}
420
421/**********************************************************************/
428bool is_hiding_unit(const struct unit *punit)
429{
431
432 if (vl == V_INVIS || vl == V_SUBSURFACE) {
433 return TRUE;
434 }
435
436 if (unit_transported(punit)) {
438 if (vl == V_INVIS || vl == V_SUBSURFACE) {
439 return TRUE;
440 }
441 }
442
443 return FALSE;
444}
445
446/**********************************************************************/
462
463/**********************************************************************/
467 const struct city *pcity)
468{
469 if (pcity == NULL) {
470 /* Can't change home city to a non existing city. */
471 return FALSE;
472 }
473
475 pcity));
476}
477
478/**********************************************************************/
485
486/**********************************************************************/
494int get_activity_rate(const struct unit *punit)
495{
496 const struct veteran_level *vlevel;
497
499
502
503 /* The speed of the settler depends on its base move_rate, not on
504 * the number of moves actually remaining or the adjusted move rate.
505 * This means sea formers won't have their activity rate increased by
506 * Magellan's, and it means injured units work just as fast as
507 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
508 int move_rate = unit_type_get(punit)->move_rate;
509
510 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
511 return ACTIVITY_FACTOR
512 * (float)vlevel->power_fact / 100
513 * move_rate / SINGLE_MOVE;
514}
515
516/**********************************************************************/
523{
524 /* This logic is also coded in client/goto.c. */
525 if (punit->moves_left > 0) {
526 return get_activity_rate(punit);
527 } else {
528 return 0;
529 }
530}
531
532/**********************************************************************/
539 enum unit_activity activity,
540 const struct tile *ptile,
541 struct extra_type *tgt)
542{
543 /* FIXME: This is just an approximation since we don't account for
544 * get_activity_rate_this_turn. */
546 int points_needed;
547
548 fc_assert(tgt != NULL || !is_targeted_activity(activity));
549
550 points_needed = tile_activity_time(activity, ptile, tgt);
551
552 if (points_needed >= 0 && speed > 0) {
553 return (points_needed - 1) / speed + 1; /* Round up */
554 } else {
555 return FC_INFINITY;
556 }
557}
558
559/**********************************************************************/
563{
564 switch (activity) {
565 case ACTIVITY_PILLAGE:
566 case ACTIVITY_BASE:
569 case ACTIVITY_MINE:
570 case ACTIVITY_CLEAN:
571 return TRUE;
572 case ACTIVITY_IDLE:
574 case ACTIVITY_SENTRY:
575 case ACTIVITY_GOTO:
576 case ACTIVITY_EXPLORE:
579 case ACTIVITY_PLANT:
581 case ACTIVITY_CONVERT:
582 return FALSE;
583 /* These shouldn't be kicking around internally. */
584 case ACTIVITY_LAST:
585 break;
586 }
587
589
590 return FALSE;
591}
592
593/**********************************************************************/
603{
604 return unit_type_get(punit)->adv.worker;
605}
606
607/**********************************************************************/
610const char *get_activity_text(enum unit_activity activity)
611{
612 /* The switch statement has just the activities listed with no "default"
613 * handling. This enables the compiler to detect missing entries
614 * automatically, and still handles everything correctly. */
615 switch (activity) {
616 case ACTIVITY_IDLE:
617 return _("Idle");
618 case ACTIVITY_CLEAN:
619 return _("Clean");
620 case ACTIVITY_MINE:
621 /* TRANS: Activity name, verb in English */
622 return Q_("?act:Mine");
623 case ACTIVITY_PLANT:
624 /* TRANS: Activity name, verb in English */
625 return _("Plant");
627 return _("Irrigate");
629 return _("Cultivate");
631 return _("Fortifying");
633 return _("Fortified");
634 case ACTIVITY_SENTRY:
635 return _("Sentry");
636 case ACTIVITY_PILLAGE:
637 return _("Pillage");
638 case ACTIVITY_GOTO:
639 return _("Goto");
640 case ACTIVITY_EXPLORE:
641 return _("Explore");
643 return _("Transform");
644 case ACTIVITY_BASE:
645 return _("Base");
647 return _("Road");
648 case ACTIVITY_CONVERT:
649 return _("Convert");
650 case ACTIVITY_LAST:
651 break;
652 }
653
655 return _("Unknown");
656}
657
658/**********************************************************************/
665 const struct unit *ptrans)
666{
667 /* Make sure this transporter can carry this type of unit. */
669 return FALSE;
670 }
671
672 /* Make sure there's room in the transporter. */
675 return FALSE;
676 }
677
678 /* Check iff this is a valid transport. */
680 return FALSE;
681 }
682
683 /* Check transport depth. */
686 return FALSE;
687 }
688
689 return TRUE;
690}
691
692/**********************************************************************/
696bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
697{
698 if (!pcargo || !ptrans || pcargo == ptrans) {
699 return FALSE;
700 }
701
702 /* Double-check ownership of the units: you can load into an allied unit
703 * (of course only allied units can be on the same tile). */
705 return FALSE;
706 }
707
708 /* Un-embarkable transport must be in city or base to load cargo. */
712 return FALSE;
713 }
714
716}
717
718/**********************************************************************/
721bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
722{
723 /* This function needs to check EVERYTHING. */
724
725 /* Check positions of the units. Of course you can't load a unit onto
726 * a transporter on a different tile... */
728 return FALSE;
729 }
730
731 /* Cannot load if cargo is already loaded onto something else. */
733 return FALSE;
734 }
735
737}
738
739/**********************************************************************/
748bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
749{
750 if (!pcargo || !ptrans) {
751 return FALSE;
752 }
753
754 /* Make sure the unit's transporter exists and is known. */
756 return FALSE;
757 }
758
759 /* Un-disembarkable transport must be in city or base to unload cargo. */
763 return FALSE;
764 }
765
766 return TRUE;
767}
768
769/**********************************************************************/
797
798/**********************************************************************/
803bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
804{
807 return TRUE;
808 }
810
811 return FALSE;
812}
813
814/**********************************************************************/
834
835/**********************************************************************/
839 struct unit *punit)
840{
841 enum unit_activity current = punit->activity;
842 struct extra_type *target = punit->activity_target;
843 enum unit_activity current2 =
844 (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
845 bool result;
846
849
851
852 punit->activity = current;
853 punit->activity_target = target;
854
855 return result;
856}
857
858/**********************************************************************/
866 const struct unit *punit,
867 enum unit_activity activity)
868{
869 struct extra_type *target = NULL;
870
871 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
872 * being able to find out whether an activity is in general possible.
873 * Find one for them, but when they come to do the activity, they will
874 * have to determine the target themselves */
875 {
876 struct tile *ptile = unit_tile(punit);
877
878 if (activity == ACTIVITY_IRRIGATE) {
879 target = next_extra_for_tile(ptile,
882 punit);
883 if (NULL == target) {
884 return FALSE; /* No more irrigation extras available. */
885 }
886 } else if (activity == ACTIVITY_MINE) {
887 target = next_extra_for_tile(ptile,
888 EC_MINE,
890 punit);
891 if (NULL == target) {
892 return FALSE; /* No more mine extras available. */
893 }
894 }
895 }
896
897 return can_unit_do_activity_targeted(nmap, punit, activity, target);
898}
899
900/**********************************************************************/
905 const struct unit *punit,
906 enum unit_activity activity,
907 struct extra_type *target)
908{
909 return can_unit_do_activity_targeted_at(nmap, punit, activity, target,
911}
912
913/**********************************************************************/
918 const struct unit *punit,
919 enum unit_activity activity,
920 struct extra_type *target,
921 const struct tile *ptile)
922{
923 /* Check that no build activity conflicting with one already in progress
924 * gets executed. */
925 /* FIXME: Should check also the cases where one of the activities is terrain
926 * change that destroys the target of the other activity */
927 if (target != NULL && is_build_activity(activity)) {
928 if (tile_is_placing(ptile)) {
929 return FALSE;
930 }
931
932 unit_list_iterate(ptile->units, tunit) {
933 if (is_build_activity(tunit->activity)
934 && !can_extras_coexist(target, tunit->activity_target)) {
935 return FALSE;
936 }
938 }
939
940#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
941{ \
942 switch (action_get_target_kind(paction)) { \
943 case ATK_TILE: \
944 return is_action_enabled_unit_on_tile(nmap, paction->id, \
945 punit, ptile, target); \
946 case ATK_EXTRAS: \
947 return is_action_enabled_unit_on_extras(paction->id, \
948 punit, ptile, target); \
949 case ATK_CITY: \
950 case ATK_UNIT: \
951 case ATK_UNITS: \
952 case ATK_SELF: \
953 return FALSE; \
954 case ATK_COUNT: \
955 break; /* Handle outside switch */ \
956 } \
957 fc_assert(action_target_kind_is_valid( \
958 action_get_target_kind(paction))); \
959 return FALSE; \
960 }
961
962 switch (activity) {
963 case ACTIVITY_IDLE:
964 case ACTIVITY_GOTO:
965 return TRUE;
966
967 case ACTIVITY_CLEAN:
968 /* The call below doesn't support actor tile speculation. */
969 fc_assert_msg(unit_tile(punit) == ptile,
970 "Please use action_speculate_unit_on_tile()");
972 punit, ptile, target);
973
974 case ACTIVITY_MINE:
975 /* The call below doesn't support actor tile speculation. */
976 fc_assert_msg(unit_tile(punit) == ptile,
977 "Please use action_speculate_unit_on_tile()");
979 ptile, target);
980
981 case ACTIVITY_PLANT:
982 /* The call below doesn't support actor tile speculation. */
983 fc_assert_msg(unit_tile(punit) == ptile,
984 "Please use action_speculate_unit_on_tile()");
986 punit, ptile, NULL);
987
989 /* The call below doesn't support actor tile speculation. */
990 fc_assert_msg(unit_tile(punit) == ptile,
991 "Please use action_speculate_unit_on_tile()");
993 ptile, target);
994
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, NULL);
1001
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_self()");
1007 punit);
1008
1009 case ACTIVITY_FORTIFIED:
1010 return FALSE;
1011
1012 case ACTIVITY_BASE:
1013 /* The call below doesn't support actor tile speculation. */
1014 fc_assert_msg(unit_tile(punit) == ptile,
1015 "Please use action_speculate_unit_on_tile()");
1017 punit, ptile, target);
1018
1019 case ACTIVITY_GEN_ROAD:
1020 /* The call below doesn't support actor tile speculation. */
1021 fc_assert_msg(unit_tile(punit) == ptile,
1022 "Please use action_speculate_unit_on_tile()");
1024 punit, ptile, target);
1025
1026 case ACTIVITY_SENTRY:
1028 && !unit_transported(punit)) {
1029 /* Don't let units sentry on tiles they will die on. */
1030 return FALSE;
1031 }
1032 return TRUE;
1033
1034 case ACTIVITY_PILLAGE:
1035 /* The call below doesn't support actor tile speculation. */
1036 fc_assert_msg(unit_tile(punit) == ptile,
1037 "Please use action_speculate_unit_on_tile()");
1039
1040 case ACTIVITY_EXPLORE:
1041 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1042
1043 case ACTIVITY_TRANSFORM:
1044 /* The call below doesn't support actor tile speculation. */
1045 fc_assert_msg(unit_tile(punit) == ptile,
1046 "Please use action_speculate_unit_on_tile()");
1048 punit, ptile, NULL);
1049
1050 case ACTIVITY_CONVERT:
1051 /* The call below doesn't support actor tile speculation. */
1052 fc_assert_msg(unit_tile(punit) == ptile,
1053 "Please use action_speculate_unit_on_self()");
1055
1056 case ACTIVITY_LAST:
1057 break;
1058 }
1059
1060 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1061 activity);
1062 return FALSE;
1063
1064#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1065}
1066
1067/**********************************************************************/
1072{
1074 punit->activity_count = 0;
1076 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1077 /* No longer done. */
1079 }
1080}
1081
1082/**********************************************************************/
1098
1099/**********************************************************************/
1116
1117/**********************************************************************/
1121 const struct tile *ptile)
1122{
1123 unit_list_iterate(ptile->units, punit) {
1124 if (punit->activity == activity) {
1125 return TRUE;
1126 }
1128 return FALSE;
1129}
1130
1131/**********************************************************************/
1136{
1138
1140 unit_list_iterate(ptile->units, punit) {
1143 }
1145
1146 return tgt_ret;
1147}
1148
1149/**********************************************************************/
1152void unit_activity_astr(const struct unit *punit, struct astring *astr)
1153{
1154 if (!punit || !astr) {
1155 return;
1156 }
1157
1158 switch (punit->activity) {
1159 case ACTIVITY_IDLE:
1161 int rate, f;
1162
1164 f = ((punit->fuel) - 1);
1165
1166 /* Add in two parts as move_points_text() returns ptr to static
1167 * End result: "Moves: (fuel)moves_left" */
1168 astr_add_line(astr, "%s: (%s)", _("Moves"),
1170 astr_add(astr, "%s",
1172 } else {
1173 astr_add_line(astr, "%s: %s", _("Moves"),
1175 }
1176 return;
1177 case ACTIVITY_CLEAN:
1178 case ACTIVITY_TRANSFORM:
1180 case ACTIVITY_FORTIFIED:
1181 case ACTIVITY_SENTRY:
1182 case ACTIVITY_GOTO:
1183 case ACTIVITY_EXPLORE:
1184 case ACTIVITY_CONVERT:
1185 case ACTIVITY_CULTIVATE:
1186 case ACTIVITY_PLANT:
1188 return;
1189 case ACTIVITY_MINE:
1190 case ACTIVITY_IRRIGATE:
1191 if (punit->activity_target == NULL) {
1193 } else {
1194 astr_add_line(astr, "Building %s",
1196 }
1197 return;
1198 case ACTIVITY_PILLAGE:
1199 if (punit->activity_target != NULL) {
1202 } else {
1204 }
1205 return;
1206 case ACTIVITY_BASE:
1209 return;
1210 case ACTIVITY_GEN_ROAD:
1213 return;
1214 case ACTIVITY_LAST:
1215 break;
1216 }
1217
1218 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1220}
1221
1222/**********************************************************************/
1228void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1229{
1230 if (!punit || !astr) {
1231 return;
1232 }
1233
1234 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1236 punit->upkeep[O_GOLD]);
1237}
1238
1239/**********************************************************************/
1242struct player *unit_nationality(const struct unit *punit)
1243{
1245 return punit->nationality;
1246}
1247
1248/**********************************************************************/
1252void unit_tile_set(struct unit *punit, struct tile *ptile)
1253{
1255 punit->tile = ptile;
1256}
1257
1258/**********************************************************************/
1265struct unit *tile_allied_unit(const struct tile *ptile,
1266 const struct player *pplayer)
1267{
1268 struct unit *punit = NULL;
1269
1270 unit_list_iterate(ptile->units, cunit) {
1271 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1272 punit = cunit;
1273 } else {
1274 return NULL;
1275 }
1276 }
1278
1279 return punit;
1280}
1281
1282/**********************************************************************/
1288struct unit *tile_enemy_unit(const struct tile *ptile,
1289 const struct player *pplayer)
1290{
1291 unit_list_iterate(ptile->units, punit) {
1292 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1293 return punit;
1294 }
1296
1297 return NULL;
1298}
1299
1300/**********************************************************************/
1303struct unit *tile_non_allied_unit(const struct tile *ptile,
1304 const struct player *pplayer)
1305{
1306 unit_list_iterate(ptile->units, punit) {
1307 if (!pplayers_allied(unit_owner(punit), pplayer)) {
1308 return punit;
1309 }
1310 }
1312
1313 return NULL;
1314}
1315
1316/**********************************************************************/
1320struct unit *tile_other_players_unit(const struct tile *ptile,
1321 const struct player *pplayer)
1322{
1323 unit_list_iterate(ptile->units, punit) {
1324 if (unit_owner(punit) != pplayer) {
1325 return punit;
1326 }
1328
1329 return NULL;
1330}
1331
1332/**********************************************************************/
1336struct unit *tile_non_attack_unit(const struct tile *ptile,
1337 const struct player *pplayer)
1338{
1339 unit_list_iterate(ptile->units, punit) {
1340 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1341 return punit;
1342 }
1343 }
1345
1346 return NULL;
1347}
1348
1349/**********************************************************************/
1357struct unit *unit_occupies_tile(const struct tile *ptile,
1358 const struct player *pplayer)
1359{
1360 unit_list_iterate(ptile->units, punit) {
1361 if (!is_occupying_unit(punit)) {
1362 continue;
1363 }
1364
1366 continue;
1367 }
1368
1369 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1370 return punit;
1371 }
1373
1374 return NULL;
1375}
1376
1377/**********************************************************************/
1384bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1385 const struct civ_map *zmap)
1386{
1388
1389 square_iterate(zmap, ptile0, 1, ptile) {
1390 struct terrain *pterrain;
1391 struct city *pcity;
1392
1393 pterrain = tile_terrain(ptile);
1394 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1395 continue;
1396 }
1397
1398 pcity = tile_non_allied_city(ptile, pplayer);
1399 if (pcity != NULL) {
1400 if (unit_list_size(ptile->units) > 0) {
1401 /* Occupied enemy city, it doesn't matter if units inside have
1402 * UTYF_NOZOC or not. */
1403 return FALSE;
1404 }
1405 } else {
1406 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1408 if (tile_has_extra(ptile, pextra)) {
1409 return FALSE;
1410 }
1412 }
1413
1414 unit_list_iterate(ptile->units, punit) {
1415 if (!pplayers_allied(unit_owner(punit), pplayer)
1418 bool hidden = FALSE;
1419
1420 /* We do NOT check the possibility that player is allied with an extra owner,
1421 * and should thus see inside the extra.
1422 * This is to avoid the situation where having an alliance with third player
1423 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1424 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1425 if (tile_has_extra(ptile, pextra)) {
1426 hidden = TRUE;
1427 break;
1428 }
1430
1431 if (!hidden) {
1432 return FALSE;
1433 }
1434 }
1436 }
1438
1439 return TRUE;
1440}
1441
1442/**********************************************************************/
1453bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1454 const struct civ_map *zmap)
1455{
1457
1458 square_iterate(zmap, ptile0, 1, ptile) {
1459 struct terrain *pterrain;
1460 struct city *pcity;
1461
1462 pterrain = tile_terrain(ptile);
1463 if (T_UNKNOWN == pterrain
1464 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1465 continue;
1466 }
1467
1468 pcity = tile_non_allied_city(ptile, pplayer);
1469 if (pcity != NULL) {
1470 if (pcity->client.occupied
1471 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1472 /* Occupied enemy city, it doesn't matter if units inside have
1473 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1474 return FALSE;
1475 }
1476 } else {
1477 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1479 if (tile_has_extra(ptile, pextra)) {
1480 return FALSE;
1481 }
1483 }
1484
1485 unit_list_iterate(ptile->units, punit) {
1487 && !pplayers_allied(unit_owner(punit), pplayer)
1489 return FALSE;
1490 }
1492 }
1494
1495 return TRUE;
1496}
1497
1498/**********************************************************************/
1506
1507/**********************************************************************/
1516{
1517 const struct tile *ptile = unit_tile(punit);
1519
1520 if (!is_attack_unit(punit)) {
1521 return FALSE;
1522 }
1523 if (tile_city(ptile)) {
1524 return FALSE;
1525 }
1527 switch (game.info.happyborders) {
1528 case HB_DISABLED:
1529 break;
1530 case HB_NATIONAL:
1531 if (tile_owner(ptile) == unit_owner(punit)) {
1532 return FALSE;
1533 }
1534 break;
1535 case HB_ALLIANCE:
1537 return FALSE;
1538 }
1539 break;
1540 }
1541 }
1542
1545 if (max_friendliness_range >= 0) {
1547 }
1548
1549 return TRUE;
1550}
1551
1552/**********************************************************************/
1556{
1557 switch (activity) {
1558 case ACTIVITY_MINE:
1559 case ACTIVITY_IRRIGATE:
1560 case ACTIVITY_BASE:
1561 case ACTIVITY_GEN_ROAD:
1562 return TRUE;
1563 default:
1564 return FALSE;
1565 }
1566}
1567
1568/**********************************************************************/
1572{
1573 switch (activity) {
1574 case ACTIVITY_PILLAGE:
1575 case ACTIVITY_CLEAN:
1576 return TRUE;
1577 default:
1578 return FALSE;
1579 }
1580}
1581
1582/**********************************************************************/
1586{
1587 switch (activity) {
1588 case ACTIVITY_CULTIVATE:
1589 case ACTIVITY_PLANT:
1590 case ACTIVITY_TRANSFORM:
1591 return TRUE;
1592 default:
1593 return FALSE;
1594 }
1595}
1596
1597/**********************************************************************/
1601{
1602 return is_build_activity(activity)
1603 || is_clean_activity(activity)
1604 || is_terrain_change_activity(activity);
1605}
1606
1607/**********************************************************************/
1611{
1612 return is_build_activity(activity)
1613 || is_clean_activity(activity);
1614}
1615
1616/**********************************************************************/
1620struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1621 const struct unit_type *punittype,
1622 int veteran_level)
1623{
1624 /* Make sure that contents of unit structure are correctly initialized,
1625 * if you ever allocate it by some other mean than fc_calloc() */
1626 struct unit *punit = fc_calloc(1, sizeof(*punit));
1627 int max_vet_lvl;
1628
1629 /* It does not register the unit so the id is set to 0. */
1631
1632 fc_assert_ret_val(NULL != punittype, NULL); /* No untyped units! */
1634
1635 fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned units! */
1636 punit->owner = pplayer;
1637 punit->nationality = pplayer;
1638
1639 punit->refcount = 1;
1641
1642 if (pcity) {
1643 unit_tile_set(punit, pcity->tile);
1644 punit->homecity = pcity->id;
1645 } else {
1648 }
1649
1650 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1651 punit->goto_tile = NULL;
1654 /* A unit new and fresh ... */
1658 /* Random moves units start with zero movement as their first movement
1659 * will be only after their moves have been reset in the beginning of
1660 * the next turn. */
1661 punit->moves_left = 0;
1662 } else {
1664 }
1665 punit->moved = FALSE;
1666
1670
1673
1674 punit->carrying = NULL;
1675
1679
1682
1683 punit->stay = FALSE;
1684
1687
1688 if (is_server()) {
1690
1692
1694
1696 O_LAST * sizeof(*punit->server.upkeep_paid));
1697
1698 punit->server.ord_map = 0;
1699 punit->server.ord_city = 0;
1700
1701 punit->server.vision = NULL; /* No vision. */
1703 /* Must be an invalid turn number, and an invalid previous turn
1704 * number. */
1705 punit->server.action_turn = -2;
1706 /* punit->server.moving = NULL; set by fc_calloc(). */
1707
1708 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1709
1710 CALL_FUNC_EACH_AI(unit_alloc, punit);
1711 } else {
1716 }
1717
1718 return punit;
1719}
1720
1721/**********************************************************************/
1726{
1728
1729 /* Unload unit if transported. */
1732
1733 /* Check for transported units. Use direct access to the list. */
1734 if (unit_list_size(punit->transporting) != 0) {
1735 /* Unload all units. */
1739 }
1741
1742 if (punit->transporting) {
1744 }
1745
1746 CALL_FUNC_EACH_AI(unit_free, punit);
1747
1748 if (is_server() && punit->server.adv) {
1750 } else {
1753 }
1754 }
1755
1756 if (--punit->refcount <= 0) {
1757 FC_FREE(punit);
1758 }
1759}
1760
1761/**********************************************************************/
1766{
1767 if (punit->has_orders) {
1768 punit->goto_tile = NULL;
1770 punit->orders.list = NULL;
1771 }
1772 punit->orders.length = 0;
1774}
1775
1776/**********************************************************************/
1780{
1782
1783 return unit_list_size(ptrans->transporting);
1784}
1785
1786/**********************************************************************/
1789static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1790 const struct tile *ptile,
1791 bool (*unit_load_test)
1792 (const struct unit *pc,
1793 const struct unit *pt))
1794{
1795 struct unit *best_trans = NULL;
1796 struct {
1799 } cur, best = { FALSE };
1800
1801 unit_list_iterate(ptile->units, ptrans) {
1802 if (!unit_load_test(pcargo, ptrans)) {
1803 continue;
1804 } else if (best_trans == NULL) {
1806 }
1807
1808 /* Gather data from transport stack in a single pass, for use in
1809 * various conditions below. */
1811 cur.outermost_moves_left = ptrans->moves_left;
1812 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1815 cur.has_orders = TRUE;
1816 }
1817 cur.outermost_moves_left = ptranstrans->moves_left;
1818 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1820
1821 /* Criteria for deciding the 'best' transport to load onto.
1822 * The following tests are applied in order; earlier ones have
1823 * lexicographically greater significance than later ones. */
1824
1825 /* Transports which have orders, or are on transports with orders,
1826 * are less preferable to transport stacks without orders (to
1827 * avoid loading on units that are just passing through). */
1828 if (best_trans != ptrans) {
1829 if (!cur.has_orders && best.has_orders) {
1831 } else if (cur.has_orders && !best.has_orders) {
1832 continue;
1833 }
1834 }
1835
1836 /* Else, transports which are idle are preferable (giving players
1837 * some control over loading) -- this does not check transports
1838 * of transports. */
1839 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1840 if (best_trans != ptrans) {
1841 if (cur.is_idle && !best.is_idle) {
1843 } else if (!cur.is_idle && best.is_idle) {
1844 continue;
1845 }
1846 }
1847
1848 /* Else, transports from which the cargo could unload at any time
1849 * are preferable to those where the cargo can only disembark in
1850 * cities/bases. */
1851 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1853 if (best_trans != ptrans) {
1854 if (cur.can_freely_unload && !best.can_freely_unload) {
1856 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1857 continue;
1858 }
1859 }
1860
1861 /* Else, transports which are less deeply nested are preferable. */
1863 if (best_trans != ptrans) {
1864 if (cur.depth < best.depth) {
1866 } else if (cur.depth > best.depth) {
1867 continue;
1868 }
1869 }
1870
1871 /* Else, transport stacks where the outermost transport has more
1872 * moves left are preferable (on the assumption that it's the
1873 * outermost transport that's about to move). */
1874 if (best_trans != ptrans) {
1875 if (cur.outermost_moves_left > best.outermost_moves_left) {
1877 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1878 continue;
1879 }
1880 }
1881
1882 /* All other things being equal, as a tie-breaker, compare the total
1883 * moves left (this turn) and move rate (future turns) for the whole
1884 * stack, to take into account total potential movement for both
1885 * short and long journeys (we don't know which the cargo intends to
1886 * make). Doesn't try to account for whether transports can unload,
1887 * etc. */
1888 if (best_trans != ptrans) {
1889 if (cur.total_moves > best.total_moves) {
1891 } else {
1892 continue;
1893 }
1894 }
1895
1897 best = cur;
1899
1900 return best_trans;
1901}
1902
1903/**********************************************************************/
1912
1913/**********************************************************************/
1919 const struct tile *ptile)
1920{
1922}
1923
1924/**********************************************************************/
1929 const struct unit *punit)
1930{
1932 return FALSE;
1933 }
1934
1937 return FALSE;
1938 }
1940
1941 return TRUE;
1942}
1943
1944/**********************************************************************/
1953 const struct unit *punit,
1954 const struct unit_type *to_unittype)
1955{
1957
1959 return UU_NOT_ENOUGH_ROOM;
1960 }
1961
1962 if (punit->transporter != NULL) {
1966 }
1968 /* The new unit type can't survive on this terrain. */
1969 return UU_NOT_TERRAIN;
1970 }
1971
1972 return UU_OK;
1973}
1974
1975/**********************************************************************/
1985 const struct unit *punit,
1986 bool is_free)
1987{
1988 struct player *pplayer = unit_owner(punit);
1989 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
1991 struct city *pcity;
1992 int cost;
1993
1994 if (!to_unittype) {
1995 return UU_NO_UNITTYPE;
1996 }
1997
1999 /* TODO: There may be other activities that the upgraded unit is not
2000 allowed to do, which we could also test.
2001 -
2002 If convert were legal for new unit_type we could allow, but then it
2003 has a 'head start' getting activity time from the old conversion. */
2004 return UU_NOT_ACTIVITY;
2005 }
2006
2007 if (!is_free) {
2009 if (pplayer->economic.gold < cost) {
2010 return UU_NO_MONEY;
2011 }
2012
2013 pcity = tile_city(unit_tile(punit));
2014 if (!pcity) {
2015 return UU_NOT_IN_CITY;
2016 }
2017 if (city_owner(pcity) != pplayer) {
2018 /* TODO: Should upgrades in allied cities be possible? */
2019 return UU_NOT_CITY_OWNER;
2020 }
2021 }
2022
2023 /* TODO: Allow transported units to be reassigned. Check here
2024 * and make changes to upgrade_unit. */
2026}
2027
2028/**********************************************************************/
2031bool unit_can_convert(const struct civ_map *nmap,
2032 const struct unit *punit)
2033{
2034 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2035
2036 if (tgt == NULL) {
2037 return FALSE;
2038 }
2039
2040 return UU_OK == unit_transform_result(nmap, punit, tgt);
2041}
2042
2043/**********************************************************************/
2048 const struct unit *punit,
2049 char *buf, size_t bufsz)
2050{
2051 struct player *pplayer = unit_owner(punit);
2053 int upgrade_cost;
2054 const struct unit_type *from_unittype = unit_type_get(punit);
2055 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2057 char tbuf[MAX_LEN_MSG];
2058
2059 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2060 "Treasury contains %d gold.",
2061 pplayer->economic.gold),
2062 pplayer->economic.gold);
2063
2064 switch (result) {
2065 case UU_OK:
2067 /* This message is targeted toward the GUI callers. */
2068 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2069 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2070 "Upgrade %s to %s for %d gold?\n%s",
2071 upgrade_cost),
2075 break;
2076 case UU_NO_UNITTYPE:
2078 _("Sorry, cannot upgrade %s (yet)."),
2080 break;
2081 case UU_NO_MONEY:
2083 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2084 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2085 "Upgrading %s to %s costs %d gold.\n%s",
2086 upgrade_cost),
2090 break;
2091 case UU_NOT_IN_CITY:
2092 case UU_NOT_CITY_OWNER:
2094 _("You can only upgrade units in your cities."));
2095 break;
2096 case UU_NOT_ENOUGH_ROOM:
2098 _("Upgrading this %s would strand units it transports."),
2100 break;
2101 case UU_NOT_TERRAIN:
2103 _("Upgrading this %s would result in a %s which can not "
2104 "survive at this place."),
2107 break;
2110 _("Upgrading this %s would result in a %s which its "
2111 "current transport, %s, could not transport."),
2115 break;
2116 case UU_NOT_ACTIVITY:
2118 _("Cannot upgrade %s while doing '%s'."),
2121 break;
2122 }
2123
2124 return result;
2125}
2126
2127/**********************************************************************/
2132 const struct unit *punit)
2133{
2134 int mpco;
2135
2137 &(const struct req_context) {
2138 .player = unit_owner(punit),
2139 .city = unit_tile(punit)
2141 : NULL,
2142 .tile = unit_tile(punit),
2143 .unit = punit,
2144 .unittype = unit_type_get(punit),
2145 .action = paction,
2146 },
2147 NULL,
2149
2151
2152 return mpco;
2153}
2154
2155/**********************************************************************/
2158int hp_gain_coord(const struct unit *punit)
2159{
2160 int hp = 0;
2161 const int base = unit_type_get(punit)->hp;
2162 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2163
2164 /* Includes barracks (100%), fortress (25%), etc. */
2166
2167 /* Minimum HP after regen effects applied. */
2168 hp = MAX(hp, min);
2169
2170 /* Regen2 effects that apply after there's at least Min HP */
2171 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2172
2173 return MAX(hp, 0);
2174}
2175
2176/**********************************************************************/
2180{
2181 const struct unit_type *utype = unit_type_get(punit);
2183 struct city *pcity = tile_city(unit_tile(punit));
2184 int gain;
2185
2186 if (!punit->moved) {
2188 } else {
2189 gain = 0;
2190 }
2191
2193
2194 if (!punit->homecity && 0 < game.server.killunhomed
2196 /* Hit point loss of units without homecity; at least 1 hp! */
2197 /* Gameloss units are immune to this effect. */
2198 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2199
2200 if (gain > hp_loss) {
2201 gain = -1;
2202 } else {
2203 gain -= hp_loss;
2204 }
2205 }
2206
2207 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2208 && !unit_transported(punit)) {
2209 gain -= utype->hp * pclass->hp_loss_pct / 100;
2210 }
2211
2212 if (punit->hp + gain > utype->hp) {
2213 gain = utype->hp - punit->hp;
2214 } else if (punit->hp + gain < 0) {
2215 gain = -punit->hp;
2216 }
2217
2218 return gain;
2219}
2220
2221/**********************************************************************/
2224bool is_losing_hp(const struct unit *punit)
2225{
2226 const struct unit_type *punittype = unit_type_get(punit);
2227
2229 < (punittype->hp *
2230 utype_class(punittype)->hp_loss_pct / 100);
2231}
2232
2233/**********************************************************************/
2236bool unit_type_is_losing_hp(const struct player *pplayer,
2237 const struct unit_type *punittype)
2238{
2239 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2241 < (punittype->hp *
2242 utype_class(punittype)->hp_loss_pct / 100);
2243}
2244
2245/**********************************************************************/
2249bool unit_is_alive(int id)
2250{
2251 /* Check if unit exist in game */
2252 if (game_unit_by_number(id)) {
2253 return TRUE;
2254 }
2255
2256 return FALSE;
2257}
2258
2259/**********************************************************************/
2266bool unit_is_virtual(const struct unit *punit)
2267{
2268 if (!punit) {
2269 return FALSE;
2270 }
2271
2272 return punit != game_unit_by_number(punit->id);
2273}
2274
2275/**********************************************************************/
2278void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2279{
2280 return punit->server.ais[ai_type_number(ai)];
2281}
2282
2283/**********************************************************************/
2286void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2287 void *data)
2288{
2289 punit->server.ais[ai_type_number(ai)] = data;
2290}
2291
2292/**********************************************************************/
2301{
2302 int cost, default_hp, dist = 0;
2303 struct tile *ptile = unit_tile(punit);
2304
2306
2308 cost = unit_owner(punit)->economic.gold + game.info.base_bribe_cost;
2309
2310 /* Consider the distance to the capital. */
2313 if (is_capital(capital)) {
2314 int tmp = map_distance(capital->tile, ptile);
2315
2316 if (tmp < dist) {
2317 dist = tmp;
2318 }
2319 }
2321
2322 cost /= dist + 2;
2323
2324 /* Consider the build cost. */
2326
2327 /* Rule set specific cost modification */
2328 cost += (cost
2330 &(const struct req_context) {
2331 .player = unit_owner(punit),
2332 .city = game_city_by_number(
2334 ),
2335 .tile = ptile,
2336 .unit = punit,
2337 .unittype = unit_type_get(punit),
2338 },
2339 briber,
2341 / 100;
2342
2343 /* Veterans are not cheap. */
2344 {
2345 const struct veteran_level *vlevel
2347
2349 cost = cost * vlevel->power_fact / 100;
2350 if (unit_type_get(punit)->move_rate > 0) {
2351 cost += cost * vlevel->move_bonus / unit_type_get(punit)->move_rate;
2352 } else {
2353 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2354 }
2355 }
2356
2357 /* Cost now contains the basic bribe cost. We now reduce it by:
2358 * bribecost = cost/2 + cost/2 * damage/hp
2359 * = cost/2 * (1 + damage/hp) */
2360 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2361}
2362
2363/**********************************************************************/
2366bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2367{
2370
2372
2373 if (force || can_unit_load(pcargo, ptrans)) {
2374 pcargo->transporter = ptrans;
2375 unit_list_append(ptrans->transporting, pcargo);
2376
2377 return TRUE;
2378 }
2379
2380 return FALSE;
2381}
2382
2383/**********************************************************************/
2387{
2388 struct unit *ptrans;
2389
2391
2392 if (!unit_transported(pcargo)) {
2393 /* 'pcargo' is not transported. */
2394 return FALSE;
2395 }
2396
2397 /* Get the transporter; must not be defined on the client! */
2399 if (ptrans) {
2400 /* 'pcargo' and 'ptrans' should be on the same tile. */
2402
2403#ifndef FREECIV_NDEBUG
2404 bool success =
2405#endif
2406 unit_list_remove(ptrans->transporting, pcargo);
2407
2408 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2410 }
2411
2412 /* For the server (also safe for the client). */
2413 pcargo->transporter = NULL;
2414
2415 return TRUE;
2416}
2417
2418/**********************************************************************/
2421bool unit_transported(const struct unit *pcargo)
2422{
2424
2425 /* The unit is transported if a transporter unit is set or, (for the client)
2426 * if the transported_by field is set. */
2427 if (is_server()) {
2429 } else {
2431 }
2432}
2433
2434/**********************************************************************/
2437struct unit *unit_transport_get(const struct unit *pcargo)
2438{
2440
2441 return pcargo->transporter;
2442}
2443
2444/**********************************************************************/
2447struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2448{
2450 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2451
2452 return ptrans->transporting;
2453}
2454
2455/**********************************************************************/
2458static inline bool
2466
2467/**********************************************************************/
2476 const struct unit *ptrans)
2477{
2478 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2479
2480 /* Check 'pcargo' against 'ptrans'. */
2482 return FALSE;
2483 }
2484
2485 /* Check 'pcargo' against 'ptrans' parents. */
2488 return FALSE;
2489 }
2491
2492 /* Check cargo children... */
2495
2496 /* ...against 'ptrans'. */
2498 return FALSE;
2499 }
2500
2501 /* ...and against 'ptrans' parents. */
2504 return FALSE;
2505 }
2508
2509 return TRUE;
2510}
2511
2512/**********************************************************************/
2516bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2517{
2519 if (ptrans == plevel) {
2520 return TRUE;
2521 }
2523 return FALSE;
2524}
2525
2526/**********************************************************************/
2529int unit_cargo_depth(const struct unit *ptrans)
2530{
2531 struct cargo_iter iter;
2532 struct iterator *it;
2533 int depth = 0;
2534
2535 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2536 iterator_next(it)) {
2537 if (iter.depth > depth) {
2538 depth = iter.depth;
2539 }
2540 }
2541 return depth;
2542}
2543
2544/**********************************************************************/
2548{
2549 int level = 0;
2550
2552 level++;
2554 return level;
2555}
2556
2557/**********************************************************************/
2561{
2562 return sizeof(struct cargo_iter);
2563}
2564
2565/**********************************************************************/
2568static void *cargo_iter_get(const struct iterator *it)
2569{
2570 const struct cargo_iter *iter = CARGO_ITER(it);
2571
2572 return unit_list_link_data(iter->links[iter->depth - 1]);
2573}
2574
2575/**********************************************************************/
2578static void cargo_iter_next(struct iterator *it)
2579{
2580 struct cargo_iter *iter = CARGO_ITER(it);
2581 const struct unit_list_link *piter;
2582 const struct unit_list_link *pnext;
2583
2584 /* Variant 1: unit has cargo. */
2586 if (NULL != pnext) {
2587 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2588 iter->links[iter->depth++] = pnext;
2589 return;
2590 }
2591
2592 fc_assert(iter->depth > 0);
2593
2594 while (iter->depth > 0) {
2595 piter = iter->links[iter->depth - 1];
2596
2597 /* Variant 2: there are other cargo units at same level. */
2599 if (NULL != pnext) {
2600 iter->links[iter->depth - 1] = pnext;
2601 return;
2602 }
2603
2604 /* Variant 3: return to previous level, and do same tests. */
2605 iter->depth--;
2606 }
2607}
2608
2609/**********************************************************************/
2612static bool cargo_iter_valid(const struct iterator *it)
2613{
2614 return (0 < CARGO_ITER(it)->depth);
2615}
2616
2617/**********************************************************************/
2621 const struct unit *ptrans)
2622{
2623 struct iterator *it = ITERATOR(iter);
2624
2625 it->get = cargo_iter_get;
2626 it->next = cargo_iter_next;
2627 it->valid = cargo_iter_valid;
2629 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2630
2631 return it;
2632}
2633
2634/**********************************************************************/
2637bool unit_is_cityfounder(const struct unit *punit)
2638{
2640}
2641
2642/**********************************************************************/
2646 int length, const struct unit_order *orders)
2647{
2648 int i;
2649
2650 for (i = 0; i < length; i++) {
2651 struct action *paction;
2652 struct extra_type *pextra;
2653
2654 if (orders[i].order > ORDER_LAST) {
2655 log_error("invalid order %d at index %d", orders[i].order, i);
2656 return FALSE;
2657 }
2658 switch (orders[i].order) {
2659 case ORDER_MOVE:
2660 case ORDER_ACTION_MOVE:
2661 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2662 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2663 return FALSE;
2664 }
2665 break;
2666 case ORDER_ACTIVITY:
2667 switch (orders[i].activity) {
2668 case ACTIVITY_SENTRY:
2669 if (i != length - 1) {
2670 /* Only allowed as the last order. */
2671 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2672 i);
2673 return FALSE;
2674 }
2675 break;
2676 /* Replaced by action orders */
2677 case ACTIVITY_BASE:
2678 case ACTIVITY_GEN_ROAD:
2679 case ACTIVITY_CLEAN:
2680 case ACTIVITY_PILLAGE:
2681 case ACTIVITY_MINE:
2682 case ACTIVITY_IRRIGATE:
2683 case ACTIVITY_PLANT:
2684 case ACTIVITY_CULTIVATE:
2685 case ACTIVITY_TRANSFORM:
2686 case ACTIVITY_CONVERT:
2688 log_error("at index %d, use action rather than activity %d.",
2689 i, orders[i].activity);
2690 return FALSE;
2691 /* Not supported. */
2692 case ACTIVITY_EXPLORE:
2693 case ACTIVITY_IDLE:
2694 /* Not set from the client. */
2695 case ACTIVITY_GOTO:
2696 case ACTIVITY_FORTIFIED:
2697 /* Unused. */
2698 case ACTIVITY_LAST:
2699 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2700 return FALSE;
2701 }
2702
2703 break;
2705 if (!action_id_exists(orders[i].action)) {
2706 /* Non-existent action. */
2707 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2708 return FALSE;
2709 }
2710
2711 paction = action_by_number(orders[i].action);
2712
2713 /* Validate main target. */
2714 if (index_to_tile(nmap, orders[i].target) == NULL) {
2715 log_error("at index %d, invalid tile target %d for the action %d.",
2716 i, orders[i].target, orders[i].action);
2717 return FALSE;
2718 }
2719
2720 if (orders[i].dir != DIR8_ORIGIN) {
2721 log_error("at index %d, the action %d sets the outdated target"
2722 " specification dir.",
2723 i, orders[i].action);
2724 }
2725
2726 /* Validate sub target. */
2727 switch (action_id_get_sub_target_kind(orders[i].action)) {
2728 case ASTK_BUILDING:
2729 /* Sub target is a building. */
2730 if (!improvement_by_number(orders[i].sub_target)) {
2731 /* Sub target is invalid. */
2732 log_error("at index %d, cannot do %s without a target.", i,
2733 action_id_rule_name(orders[i].action));
2734 return FALSE;
2735 }
2736 break;
2737 case ASTK_TECH:
2738 /* Sub target is a technology. */
2739 if (orders[i].sub_target == A_NONE
2740 || (!valid_advance_by_number(orders[i].sub_target)
2741 && orders[i].sub_target != A_FUTURE)) {
2742 /* Target tech is invalid. */
2743 log_error("at index %d, cannot do %s without a target.", i,
2744 action_id_rule_name(orders[i].action));
2745 return FALSE;
2746 }
2747 break;
2748 case ASTK_EXTRA:
2750 /* Sub target is an extra. */
2751 pextra = (!(orders[i].sub_target == NO_TARGET
2752 || (orders[i].sub_target < 0
2753 || (orders[i].sub_target
2755 ? extra_by_number(orders[i].sub_target) : NULL);
2756 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2757 if (pextra == NULL) {
2758 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2759 /* Target extra is invalid. */
2760 log_error("at index %d, cannot do %s without a target.", i,
2761 action_id_rule_name(orders[i].action));
2762 return FALSE;
2763 }
2764 } else {
2765 if (!actres_removes_extra(paction->result, pextra)
2766 && !actres_creates_extra(paction->result, pextra)) {
2767 /* Target extra is irrelevant for the action. */
2768 log_error("at index %d, cannot do %s to %s.", i,
2769 action_id_rule_name(orders[i].action),
2770 extra_rule_name(pextra));
2771 return FALSE;
2772 }
2773 }
2774 break;
2775 case ASTK_NONE:
2776 /* No validation required. */
2777 break;
2778 /* Invalid action? */
2779 case ASTK_COUNT:
2782 FALSE,
2783 "Bad action %d in order number %d.", orders[i].action, i);
2784 }
2785
2786 /* Some action orders are sane only in the last order. */
2787 if (i != length - 1) {
2788 /* If the unit is dead, */
2790 /* or if Freeciv has no idea where the unit will end up after it
2791 * has performed this action, */
2794 /* or if the unit will end up standing still, */
2796 /* than having this action in the middle of a unit's orders is
2797 * probably wrong. */
2798 log_error("action %d is not allowed at index %d.",
2799 orders[i].action, i);
2800 return FALSE;
2801 }
2802 }
2803
2804 /* Don't validate that the target tile really contains a target or
2805 * that the actor player's map think the target tile has one.
2806 * The player may target something from their player map that isn't
2807 * there any more, a target they think is there even if their player
2808 * map doesn't have it, or even a target they assume will be there
2809 * when the unit reaches the target tile.
2810 *
2811 * With that said: The client should probably at least have an
2812 * option to only aim city targeted actions at cities. */
2813
2814 break;
2815 case ORDER_FULL_MP:
2816 break;
2817 case ORDER_LAST:
2818 /* An invalid order. This is handled above. */
2819 break;
2820 }
2821 }
2822
2823 return TRUE;
2824}
2825
2826/**********************************************************************/
2831 int length,
2832 const struct unit_order *orders)
2833{
2834 struct unit_order *unit_orders;
2835
2836 if (!unit_order_list_is_sane(nmap, length, orders)) {
2837 return NULL;
2838 }
2839
2840 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2841 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2842
2843 return unit_orders;
2844}
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5823
bool is_action_enabled_unit_on_self(const action_id wanted_action, const struct unit *actor_unit)
Definition actions.c:4223
struct act_prob action_prob_vs_city(const struct unit *actor_unit, const action_id act_id, const struct city *target_city)
Definition actions.c:4981
struct act_prob action_prob_vs_unit(const struct unit *actor_unit, const action_id act_id, const struct unit *target_unit)
Definition actions.c:5065
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1991
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:5342
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:4073
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:3832
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:3909
bool action_maybe_possible_actor_unit(const struct civ_map *nmap, const action_id act_id, const struct unit *actor_unit)
Definition actions.c:6239
bool action_id_exists(const action_id act_id)
Definition actions.c:1820
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:479
#define action_id_get_sub_target_kind(act_id)
Definition actions.h:654
static struct action * action_by_number(action_id act_id)
Definition actions.h:633
#define action_has_result(_act_, _res_)
Definition actions.h:429
#define action_by_result_iterate_end
Definition actions.h:483
#define action_id(_act_)
Definition actions.h:659
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:384
void astr_add_line(struct astring *astr, const char *format,...)
Definition astring.c:299
void astr_add(struct astring *astr, const char *format,...)
Definition astring.c:287
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
#define BV_SET(bv, bit)
Definition bitvector.h:81
bool is_capital(const struct city *pcity)
Definition city.c:1571
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2058
bool is_unit_near_a_friendly_city(const struct unit *punit, int distance)
Definition city.c:2073
#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:2931
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:74
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct player *other_player, enum effect_type effect_type)
Definition effects.c:748
int get_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Definition effects.c:1070
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, const struct action *paction, enum effect_type effect_type)
Definition effects.c:1035
struct extra_type * next_extra_for_tile(const struct tile *ptile, enum extra_cause cause, const struct player *pplayer, const struct unit *punit)
Definition extras.c:765
struct player * extra_owner(const struct tile *ptile)
Definition extras.c:1114
struct extra_type_list * extra_type_list_of_zoccers(void)
Definition extras.c:267
static struct extra_type_list * zoccers
Definition extras.c:39
struct extra_type * extra_by_number(int id)
Definition extras.c:183
bool can_extras_coexist(const struct extra_type *pextra1, const struct extra_type *pextra2)
Definition extras.c:1003
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:203
const char * extra_name_translation(const struct extra_type *pextra)
Definition extras.c:194
#define extra_type_list_iterate(extralist, pextra)
Definition extras.h:165
#define extra_index(_e_)
Definition extras.h:183
#define extra_type_list_iterate_end
Definition extras.h:167
static bool is_server(void)
#define NO_TARGET
Definition fc_types.h:357
enum unit_activity Activity_type_id
Definition fc_types.h:382
#define DIR8_ORIGIN
Definition fc_types.h:460
int action_id
Definition fc_types.h:392
@ HB_ALLIANCE
Definition fc_types.h:1293
@ HB_DISABLED
Definition fc_types.h:1291
@ HB_NATIONAL
Definition fc_types.h:1292
@ 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:1038
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:92
#define Q_(String)
Definition fcintl.h:70
#define PL_(String1, String2, n)
Definition fcintl.h:71
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:62
struct world wld
Definition game.c:63
struct unit * game_unit_by_number(int id)
Definition game.c:116
struct city * game_city_by_number(int id)
Definition game.c:107
#define GAME_UNIT_BRIBE_DIST_MAX
Definition game.h:751
#define GAME_TRANSPORT_MAX_RECURSIVE
Definition game.h:754
static const int bufsz
Definition helpdlg.c:70
struct impr_type * improvement_by_number(const Impr_type_id id)
static bool iterator_valid(const struct iterator *it)
Definition iterator.h:61
#define ITERATOR(p)
Definition iterator.h:37
static void iterator_next(struct iterator *it)
Definition iterator.h:42
#define fc_assert_msg(condition, message,...)
Definition log.h:181
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define log_error(message,...)
Definition log.h:103
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition log.h:208
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:456
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:940
bool map_untrusted_dir_is_valid(enum direction8 dir)
Definition map.c:1273
enum direction8 rand_direction(void)
Definition map.c:1929
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:654
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:391
#define square_iterate_end
Definition map.h:394
#define fc_calloc(n, esz)
Definition mem.h:38
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_malloc(sz)
Definition mem.h:34
const char * move_points_text(int mp, bool reduce)
Definition movement.c:1015
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Definition movement.c:290
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:319
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:331
bool can_unit_transport(const struct unit *transporter, const struct unit *transported)
Definition movement.c:855
int unit_move_rate(const struct unit *punit)
Definition movement.c:90
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:491
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:465
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:868
bool can_attack_non_native(const struct unit_type *utype)
Definition movement.c:214
#define SINGLE_MOVE
Definition movement.h:26
struct city_list * cities
Definition packhand.c:119
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Definition player.c:1104
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1405
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1459
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MIN(x, y)
Definition shared.h:55
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
Definition ai.h:50
struct iterator vtable
Definition unit.c:50
int depth
Definition unit.c:52
const struct unit_list_link * links[GAME_TRANSPORT_MAX_RECURSIVE]
Definition unit.c:51
Definition city.h:320
int id
Definition city.h:326
bool occupied
Definition city.h:460
struct tile * tile
Definition city.h:322
struct city::@17::@20 client
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
int killunhomed
Definition game.h: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:282
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
int sub_target
Definition unit.h:98
int transport_capacity
Definition unittype.h:523
struct unit_type::@87 adv
bool worker
Definition unittype.h:575
int move_rate
Definition unittype.h:517
enum vision_layer vlayer
Definition unittype.h:569
const struct unit_type * converted_to
Definition unittype.h:530
Definition unit.h:138
time_t action_timestamp
Definition unit.h:245
int length
Definition unit.h:195
int upkeep[O_LAST]
Definition unit.h:148
bool has_orders
Definition unit.h:193
struct unit::@80 orders
enum action_decision action_decision_want
Definition unit.h:202
int battlegroup
Definition unit.h:191
enum unit_activity activity
Definition unit.h:157
int moves_left
Definition unit.h:150
int refcount
Definition unit.h:141
struct unit::@81::@83 client
int id
Definition unit.h:145
struct unit_list * transporting
Definition unit.h:184
int ord_city
Definition unit.h:242
bool moved
Definition unit.h:173
int ord_map
Definition unit.h:241
bool debug
Definition unit.h:234
struct vision * vision
Definition unit.h:244
int hp
Definition unit.h:151
int fuel
Definition unit.h:153
struct extra_type * changed_from_target
Definition unit.h:170
int current_form_turn
Definition unit.h:208
bool stay
Definition unit.h:205
bool colored
Definition unit.h:222
enum direction8 facing
Definition unit.h:142
struct unit::@81::@84 server
struct tile * tile
Definition unit.h:140
struct extra_type * activity_target
Definition unit.h:164
struct act_prob * act_prob_cache
Definition unit.h:228
int activity_count
Definition unit.h:162
struct unit_order * list
Definition unit.h:198
enum unit_activity changed_from
Definition unit.h:168
struct unit_adv * adv
Definition unit.h:236
struct player * nationality
Definition unit.h:144
int transported_by
Definition unit.h:216
void(* removal_callback)(struct unit *punit)
Definition unit.h:253
void * ais[FREECIV_AI_MOD_LAST]
Definition unit.h:237
int action_turn
Definition unit.h:246
int homecity
Definition unit.h:146
int upkeep_paid[O_LAST]
Definition unit.h:256
bool paradropped
Definition unit.h:174
bool done_moving
Definition unit.h:181
struct unit * transporter
Definition unit.h:183
int birth_turn
Definition unit.h:207
struct goods_type * carrying
Definition unit.h:186
struct tile * goto_tile
Definition unit.h:155
struct tile * action_decision_tile
Definition unit.h:203
const struct unit_type * utype
Definition unit.h:139
int veteran
Definition unit.h:152
int changed_from_count
Definition unit.h:169
struct player * owner
Definition unit.h:143
bool dying
Definition unit.h:250
enum unit_focus_status focus_status
Definition unit.h:214
enum server_side_agent ssa_controller
Definition unit.h:172
struct civ_map map
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
struct advance * valid_advance_by_number(const Tech_type_id id)
Definition tech.c:176
#define A_FUTURE
Definition tech.h:46
#define A_NONE
Definition tech.h:43
#define T_UNKNOWN
Definition terrain.h:57
#define terrain_has_flag(terr, flag)
Definition terrain.h:283
bool tile_is_placing(const struct tile *ptile)
Definition tile.c:1129
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Definition tile.c:418
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:324
int tile_has_not_aggressive_extra_for_unit(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:196
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:392
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
@ TILE_KNOWN_UNSEEN
Definition tile.h:37
@ TILE_KNOWN_SEEN
Definition tile.h:38
#define ACTIVITY_FACTOR
Definition tile.h:165
#define tile_terrain(_tile)
Definition tile.h:110
#define tile_has_extra(ptile, pextra)
Definition tile.h:147
#define tile_owner(_tile)
Definition tile.h:96
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
bool unit_can_do_action_sub_result(const struct unit *punit, enum action_sub_result sub_result)
Definition unit.c:381
size_t cargo_iter_sizeof(void)
Definition unit.c:2560
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:917
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1600
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2830
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:1952
bool unit_being_aggressive(const struct unit *punit)
Definition unit.c:1515
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1501
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2236
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1585
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1303
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:1779
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2578
void free_unit_orders(struct unit *punit)
Definition unit.c:1765
bool can_unit_change_homecity(const struct unit *punit)
Definition unit.c:481
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2286
bool unit_is_alive(int id)
Definition unit.c:2249
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:286
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:326
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:428
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:522
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2131
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:538
bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1453
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2366
bool unit_can_add_or_build_city(const struct unit *punit)
Definition unit.c:450
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1918
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2620
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2047
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2547
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1085
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2224
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1610
bool can_unit_deboard_or_be_unloaded(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:773
bool can_unit_change_homecity_to(const struct unit *punit, const struct city *pcity)
Definition unit.c:466
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:721
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1336
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2158
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2568
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1242
int get_activity_rate(const struct unit *punit)
Definition unit.c:494
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2475
bool is_attack_unit(const struct unit *punit)
Definition unit.c:307
const Activity_type_id tile_changing_activities[]
Definition unit.c:44
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2437
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2386
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1555
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2516
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2179
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:838
int unit_bribe_cost(struct unit *punit, struct player *briber)
Definition unit.c:2300
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1228
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:696
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2637
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1120
bool can_unit_do_autosettlers(const struct unit *punit)
Definition unit.c:602
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:416
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2612
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:361
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:371
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:318
void set_unit_activity_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type *new_target)
Definition unit.c:1102
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1620
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2529
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Definition unit.c:2459
#define CARGO_ITER(iter)
Definition unit.c:54
bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:803
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2645
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:610
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1135
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:299
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1384
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:334
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1571
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:819
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1152
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:865
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1320
bool is_special_unit(const struct unit *punit)
Definition unit.c:352
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1725
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1288
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1252
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:1984
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2421
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:2266
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1070
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:390
bool is_guard_unit(const struct unit *punit)
Definition unit.c:342
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:748
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1357
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2447
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1928
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2278
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:1265
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1908
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:1789
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2031
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:562
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:664
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:904
#define unit_tile(_pu)
Definition unit.h:390
#define unit_transports_iterate_end
Definition unit.h:555
#define unit_cargo_iterate_end
Definition unit.h:565
@ FOCUS_AVAIL
Definition unit.h:53
#define BATTLEGROUP_NONE
Definition unit.h:190
unit_orders
Definition unit.h:37
@ ORDER_ACTION_MOVE
Definition unit.h:45
@ ORDER_ACTIVITY
Definition unit.h:41
@ ORDER_FULL_MP
Definition unit.h:43
@ ORDER_MOVE
Definition unit.h:39
@ ORDER_LAST
Definition unit.h:49
@ ORDER_PERFORM_ACTION
Definition unit.h:47
#define unit_transported_server(_pcargo_)
Definition unit.h:533
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:562
#define unit_owner(_pu)
Definition unit.h:389
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:551
unit_upgrade_result
Definition unit.h:60
@ UU_NO_MONEY
Definition unit.h:63
@ UU_NOT_IN_CITY
Definition unit.h:64
@ UU_NO_UNITTYPE
Definition unit.h:62
@ UU_NOT_TERRAIN
Definition unit.h:67
@ UU_UNSUITABLE_TRANSPORT
Definition unit.h:68
@ UU_NOT_CITY_OWNER
Definition unit.h:65
@ UU_NOT_ENOUGH_ROOM
Definition unit.h:66
@ UU_OK
Definition unit.h:61
@ UU_NOT_ACTIVITY
Definition unit.h:69
#define unit_transported_client(_pcargo_)
Definition unit.h:536
unit_airlift_result
Definition unit.h:72
@ AR_SRC_NO_FLIGHTS
Definition unit.h:84
@ AR_OK_SRC_UNKNOWN
Definition unit.h:75
@ AR_OK_DST_UNKNOWN
Definition unit.h:76
@ AR_NO_MOVES
Definition unit.h:78
@ AR_BAD_DST_CITY
Definition unit.h:83
@ AR_NOT_IN_CITY
Definition unit.h:81
@ AR_OCCUPIED
Definition unit.h:80
@ AR_OK
Definition unit.h:74
@ AR_DST_NO_FLIGHTS
Definition unit.h:85
@ AR_WRONG_UNITTYPE
Definition unit.h:79
@ AR_BAD_SRC_CITY
Definition unit.h:82
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_safe(unitlist, _unit)
Definition unitlist.h:39
#define unit_list_iterate_end
Definition unitlist.h:33
#define unit_list_iterate_safe_end
Definition unitlist.h:61
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
int utype_pays_mp_for_action_base(const struct action *paction, const struct unit_type *putype)
Definition unittype.c:1381
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:300
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1569
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1249
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c: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:766
#define utype_class(_t_)
Definition unittype.h:749
#define utype_fuel(ptype)
Definition unittype.h:836
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:617