Freeciv-3.4
Loading...
Searching...
No Matches
unit.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "astring.h"
20#include "bitvector.h"
21#include "fcintl.h"
22#include "mem.h"
23#include "shared.h"
24#include "support.h"
25
26/* common */
27#include "ai.h"
28#include "actions.h"
29#include "base.h"
30#include "city.h"
31#include "game.h"
32#include "log.h"
33#include "map.h"
34#include "movement.h"
35#include "packets.h"
36#include "player.h"
37#include "road.h"
38#include "specialist.h"
39#include "tech.h"
40#include "traderoutes.h"
41#include "unitlist.h"
42
43#include "unit.h"
44
49
55#define CARGO_ITER(iter) ((struct cargo_iter *) (iter))
56
57/**********************************************************************/
61 const struct unit_order *order2)
62{
63 return order1->order == order2->order
64 && order1->activity == order2->activity
65 && order1->target == order2->target
66 && order1->sub_target == order2->sub_target
67 && order1->action == order2->action
68 && order1->dir == order2->dir;
69}
70
71/**********************************************************************/
87 const struct player *restriction,
88 const struct unit *punit,
89 const struct city *pdest_city)
90{
91 const struct city *psrc_city = tile_city(unit_tile(punit));
92 const struct player *punit_owner;
93 const struct tile *dst_tile = nullptr;
94 const struct unit_type *putype = unit_type_get(punit);
97
98 if (0 == punit->moves_left
100 /* No moves left. */
101 return AR_NO_MOVES;
102 }
103
105 return AR_WRONG_UNITTYPE;
106 }
107
109 /* Units with occupants can't be airlifted currently. */
110 return AR_OCCUPIED;
111 }
112
113 if (nullptr == psrc_city) {
114 /* No city there. */
115 return AR_NOT_IN_CITY;
116 }
117
118 if (psrc_city == pdest_city) {
119 /* Airlifting to our current position doesn't make sense. */
120 return AR_BAD_DST_CITY;
121 }
122
123 if (nullptr != pdest_city) {
125
126 if (nullptr != restriction
129 /* Can't exist at the destination tile. */
130 return AR_BAD_DST_CITY;
131 }
132 }
133
135
136 /* Check validity of both source and destination before checking capacity,
137 * to avoid misleadingly optimistic returns. */
138
142 /* Not allowed to airlift from this source. */
143 return AR_BAD_SRC_CITY;
144 }
145
146 /* Check diplomatic possibility of the destination */
147 if (nullptr != pdest_city) {
151 || flagless
153 /* Not allowed to airlift to this destination. */
154 return AR_BAD_DST_CITY;
155 }
156 } else if (flagless
158 /* Foreign units block airlifting to this destination */
159 return AR_BAD_DST_CITY;
160 }
161 }
162
163 /* Check airlift capacities */
165 if (nullptr == restriction || city_owner(psrc_city) == restriction) {
166 /* We know for sure whether or not src can airlift this turn. */
167 if (0 >= psrc_city->airlift
169 /* The source cannot airlift for this turn (maybe already airlifted
170 * or no airport).
171 * See also do_airline() in server/unittools.h. */
172 return AR_SRC_NO_FLIGHTS;
173 } /* else, there is capacity; continue to other checks */
174 } else {
175 /* We don't have access to the 'airlift' field. Assume it's OK; can
176 * only find out for sure by trying it. */
178 }
179 }
180
181 if (nullptr != pdest_city && !game.info.airlift_to_always_enabled) {
182 if (nullptr == restriction || city_owner(pdest_city) == restriction) {
183 if (0 >= pdest_city->airlift
185 /* The destination cannot support airlifted units for this turn
186 * (maybe already airlifted or no airport).
187 * See also do_airline() in server/unittools.h. */
188 return AR_DST_NO_FLIGHTS;
189 } /* else continue */
190 } else {
192 }
193 }
194
195 return ok_result;
196}
197
198/**********************************************************************/
206 const struct unit *punit,
207 const struct city *pdest_city)
208{
209 if (is_server()) {
212 } else {
214 pdest_city));
215 }
216}
217
218/**********************************************************************/
221bool unit_has_orders(const struct unit *punit)
222{
223 return punit->has_orders;
224}
225
226/**********************************************************************/
233int unit_shield_value(const struct unit *punit,
234 const struct unit_type *punittype,
235 const struct action *paction)
236{
237 int value;
238
239 bool has_unit;
240 const struct player *act_player;
241
242 has_unit = (punit != nullptr);
243
244 if (has_unit && punittype == nullptr) {
246 }
247
248 fc_assert_ret_val(punittype != nullptr, 0);
249 fc_assert(punit == nullptr || unit_type_get(punit) == punittype);
250 fc_assert_ret_val(paction != nullptr, 0);
251
252 act_player = has_unit ? unit_owner(punit) : nullptr;
253 /* TODO: determine if tile and city should be where the unit currently is
254 * located or the target city. Those two may differ. Wait for ruleset
255 * author feed back. */
256
258 value += ((value
259 * get_target_bonus_effects(nullptr,
260 &(const struct req_context) {
261 .player = act_player,
262 .unit = punit,
263 .unittype = punittype,
264 .action = paction,
265 },
266 nullptr,
268 / 100);
269
270 return value;
271}
272
273/**********************************************************************/
278 const struct unit *punit)
279{
280 struct city *pcity = tile_city(unit_tile(punit));
281
282 if (pcity == nullptr) {
283 /* No city to help at this tile. */
284 return FALSE;
285 }
286
288 /* This unit can never do help wonder. */
289 return FALSE;
290 }
291
292 /* Evaluate all action enablers for extra accuracy. */
293 /* TODO: Is it worth it? */
296 pcity));
297}
298
299/**********************************************************************/
312
313/**********************************************************************/
317{
319}
320
321/**********************************************************************/
331
332/**********************************************************************/
335bool is_martial_law_unit(const struct unit *punit)
336{
338}
339
340/**********************************************************************/
343bool is_occupying_unit(const struct unit *punit)
344{
346}
347
348/**********************************************************************/
352{
354}
355
356/**********************************************************************/
359bool is_guard_unit(const struct unit *punit)
360{
362}
363
364/**********************************************************************/
369bool is_special_unit(const struct unit *punit)
370{
372}
373
374/**********************************************************************/
383 const struct player *pplayer)
384{
385 if (unit_owner(punit) != pplayer
387 if (pplayer == nullptr) {
388 /* Global observer always sees the flags */
389 return FALSE;
390 }
391
392 return TRUE;
393 }
394
395 return FALSE;
396}
397
398/**********************************************************************/
402bool unit_can_do_action(const struct unit *punit,
403 const action_id act_id)
404{
405 return utype_can_do_action(unit_type_get(punit), act_id);
406}
407
408/**********************************************************************/
413 enum action_result result)
414{
416}
417
418/**********************************************************************/
427
428/**********************************************************************/
432 const struct player *pplayer,
433 const struct tile *ptile, bool omniscient)
434{
435 square_iterate(nmap, ptile, 2, ptile1) {
437 if ((omniscient
438 || can_player_see_unit(pplayer, punit))
439 && pplayers_at_war(pplayer, unit_owner(punit))
444 unit_class_get(punit), ptile)))) {
445 return TRUE;
446 }
449
450 return FALSE;
451}
452
453/**********************************************************************/
457bool is_field_unit(const struct unit *punit)
458{
460}
461
462/**********************************************************************/
469bool is_hiding_unit(const struct unit *punit)
470{
472
473 if (vl == V_INVIS || vl == V_SUBSURFACE) {
474 return TRUE;
475 }
476
477 if (unit_transported(punit)) {
479 if (vl == V_INVIS || vl == V_SUBSURFACE) {
480 return TRUE;
481 }
482 }
483
484 return FALSE;
485}
486
487/**********************************************************************/
492 const struct unit *punit)
493{
494 struct city *tgt_city;
495
496 if ((tgt_city = tile_city(unit_tile(punit)))) {
499 } else {
501 ACTION_FOUND_CITY, unit_tile(punit), nullptr));
502 }
503}
504
505/**********************************************************************/
509 const struct unit *punit,
510 const struct city *pcity)
511{
512 if (pcity == nullptr) {
513 /* Can't change home city to a non existing city. */
514 return FALSE;
515 }
516
518 pcity));
519}
520
521/**********************************************************************/
525 const struct unit *punit)
526{
528}
529
530/**********************************************************************/
538int get_activity_rate(const struct unit *punit)
539{
540 const struct veteran_level *vlevel;
541 const struct unit_type *ptype;
542 int move_rate;
543
546 fc_assert_ret_val(vlevel != nullptr, 0);
547
548 /* The speed of the settler depends on its base move_rate, not on
549 * the number of moves actually remaining or the adjusted move rate.
550 * This means sea formers won't have their activity rate increased by
551 * Magellan's, and it means injured units work just as fast as
552 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
553 move_rate = ptype->move_rate;
554
555 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
556 return ACTIVITY_FACTOR
557 * (float)vlevel->power_fact / 100
559}
560
561/**********************************************************************/
568{
569 /* This logic is also coded in client/goto.c. */
570 if (punit->moves_left > 0) {
571 return get_activity_rate(punit);
572 } else {
573 return 0;
574 }
575}
576
577/**********************************************************************/
584 enum unit_activity activity,
585 const struct tile *ptile,
586 struct extra_type *tgt)
587{
588 /* FIXME: This is just an approximation since we don't account for
589 * get_activity_rate_this_turn. */
591 int points_needed;
592
593 fc_assert(tgt != nullptr || !is_targeted_activity(activity));
594
595 points_needed = tile_activity_time(activity, ptile, tgt);
596
597 if (points_needed >= 0 && speed > 0) {
598 return (points_needed - 1) / speed + 1; /* Round up */
599 } else {
600 return FC_INFINITY;
601 }
602}
603
604/**********************************************************************/
608{
609 switch (activity) {
610 case ACTIVITY_PILLAGE:
611 case ACTIVITY_BASE:
614 case ACTIVITY_MINE:
615 case ACTIVITY_CLEAN:
616 return TRUE;
617 case ACTIVITY_IDLE:
619 case ACTIVITY_SENTRY:
620 case ACTIVITY_GOTO:
621 case ACTIVITY_EXPLORE:
624 case ACTIVITY_PLANT:
626 case ACTIVITY_CONVERT:
627 return FALSE;
628 /* These shouldn't be kicking around internally. */
629 case ACTIVITY_LAST:
630 break;
631 }
632
634
635 return FALSE;
636}
637
638/**********************************************************************/
648{
649 return unit_type_get(punit)->adv.worker;
650}
651
652/**********************************************************************/
655const char *get_activity_text(enum unit_activity activity)
656{
657 /* The switch statement has just the activities listed with no "default"
658 * handling. This enables the compiler to detect missing entries
659 * automatically, and still handles everything correctly. */
660 switch (activity) {
661 case ACTIVITY_IDLE:
662 return _("Idle");
663 case ACTIVITY_CLEAN:
664 return _("Clean");
665 case ACTIVITY_MINE:
666 /* TRANS: Activity name, verb in English */
667 return Q_("?act:Mine");
668 case ACTIVITY_PLANT:
669 /* TRANS: Activity name, verb in English */
670 return _("Plant");
672 return _("Irrigate");
674 return _("Cultivate");
676 return _("Fortifying");
678 return _("Fortified");
679 case ACTIVITY_SENTRY:
680 return _("Sentry");
681 case ACTIVITY_PILLAGE:
682 return _("Pillage");
683 case ACTIVITY_GOTO:
684 return _("Goto");
685 case ACTIVITY_EXPLORE:
686 return _("Explore");
688 return _("Transform");
689 case ACTIVITY_BASE:
690 return _("Base");
692 return _("Road");
693 case ACTIVITY_CONVERT:
694 return _("Convert");
695 case ACTIVITY_LAST:
696 break;
697 }
698
700 return _("Unknown");
701}
702
703/**********************************************************************/
710 const struct unit *ptrans)
711{
712 /* Make sure this transporter can carry this type of unit. */
714 return FALSE;
715 }
716
717 /* Make sure there's room in the transporter. */
720 return FALSE;
721 }
722
723 /* Check iff this is a valid transport. */
725 return FALSE;
726 }
727
728 /* Check transport depth. */
731 return FALSE;
732 }
733
734 return TRUE;
735}
736
737/**********************************************************************/
741bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
742{
743 if (!pcargo || !ptrans || pcargo == ptrans) {
744 return FALSE;
745 }
746
747 /* Double-check ownership of the units: you can load into an allied unit
748 * (of course only allied units can be on the same tile). */
750 return FALSE;
751 }
752
753 /* Un-embarkable transport must be in city or base to load cargo. */
757 return FALSE;
758 }
759
761}
762
763/**********************************************************************/
766bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
767{
768 /* This function needs to check EVERYTHING. */
769
770 /* Check positions of the units. Of course you can't load a unit onto
771 * a transporter on a different tile... */
773 return FALSE;
774 }
775
776 /* Cannot load if cargo is already loaded onto something else. */
778 return FALSE;
779 }
780
782}
783
784/**********************************************************************/
793bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
794{
795 if (!pcargo || !ptrans) {
796 return FALSE;
797 }
798
799 /* Make sure the unit's transporter exists and is known. */
801 return FALSE;
802 }
803
804 /* Un-disembarkable transport must be in city or base to unload cargo. */
808 return FALSE;
809 }
810
811 return TRUE;
812}
813
814/**********************************************************************/
841
842/**********************************************************************/
847bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
848{
851 return TRUE;
852 }
854
855 return FALSE;
856}
857
858/**********************************************************************/
878
879/**********************************************************************/
883 struct unit *punit)
884{
885 enum unit_activity current = punit->activity;
886 struct extra_type *target = punit->activity_target;
888 = (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
890 bool result;
891
893 punit->activity_target = nullptr;
894
896 target);
897
898 punit->activity = current;
899 punit->activity_target = target;
901
902 return result;
903}
904
905/**********************************************************************/
913 const struct unit *punit,
914 enum unit_activity activity,
915 enum gen_action action)
916{
917 struct extra_type *target = nullptr;
918
919 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
920 * being able to find out whether an activity is in general possible.
921 * Find one for them, but when they come to do the activity, they will
922 * have to determine the target themselves */
923 {
924 struct tile *ptile = unit_tile(punit);
925
926 if (activity == ACTIVITY_IRRIGATE) {
927 target = next_extra_for_tile(ptile,
930 punit);
931 if (target == nullptr) {
932 return FALSE; /* No more irrigation extras available. */
933 }
934 } else if (activity == ACTIVITY_MINE) {
935 target = next_extra_for_tile(ptile,
936 EC_MINE,
938 punit);
939 if (target == nullptr) {
940 return FALSE; /* No more mine extras available. */
941 }
942 }
943 }
944
946 target);
947}
948
949/**********************************************************************/
954 const struct unit *punit,
955 enum unit_activity activity,
956 enum gen_action action,
957 struct extra_type *target)
958{
960 target, unit_tile(punit));
961}
962
963/**********************************************************************/
968 const struct unit *punit,
969 enum unit_activity activity,
970 enum gen_action action,
971 struct extra_type *target,
972 const struct tile *ptile)
973{
974 /* Check that no build activity conflicting with one already in progress
975 * gets executed. */
976 /* FIXME: Should check also the cases where one of the activities is terrain
977 * change that destroys the target of the other activity */
978 if (target != nullptr && is_build_activity(activity)) {
979 if (tile_is_placing(ptile)) {
980 return FALSE;
981 }
982
983 unit_list_iterate(ptile->units, tunit) {
984 if (is_build_activity(tunit->activity)
985 && !can_extras_coexist(target, tunit->activity_target)) {
986 return FALSE;
987 }
989 }
990
991#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
992{ \
993 switch (action_get_target_kind(paction)) { \
994 case ATK_TILE: \
995 return is_action_enabled_unit_on_tile(nmap, paction->id, \
996 punit, ptile, target); \
997 case ATK_EXTRAS: \
998 return is_action_enabled_unit_on_extras(nmap, paction->id, \
999 punit, ptile, target); \
1000 case ATK_CITY: \
1001 case ATK_UNIT: \
1002 case ATK_STACK: \
1003 case ATK_SELF: \
1004 return FALSE; \
1005 case ATK_COUNT: \
1006 break; /* Handle outside switch */ \
1007 } \
1008 fc_assert(action_target_kind_is_valid( \
1009 action_get_target_kind(paction))); \
1010 return FALSE; \
1011 }
1012
1013 switch (activity) {
1014 case ACTIVITY_IDLE:
1015 case ACTIVITY_GOTO:
1016 return TRUE;
1017
1018 case ACTIVITY_CLEAN:
1019 /* The call below doesn't support actor tile speculation. */
1020 fc_assert_msg(unit_tile(punit) == ptile,
1021 "Please use action_speculate_unit_on_tile()");
1023 punit, ptile, target);
1024
1025 case ACTIVITY_MINE:
1026 /* The call below doesn't support actor tile speculation. */
1027 fc_assert_msg(unit_tile(punit) == ptile,
1028 "Please use action_speculate_unit_on_tile()");
1030 ptile, target);
1031
1032 case ACTIVITY_PLANT:
1033 /* The call below doesn't support actor tile speculation. */
1034 fc_assert_msg(unit_tile(punit) == ptile,
1035 "Please use action_speculate_unit_on_tile()");
1037 punit, ptile, nullptr);
1038
1039 case ACTIVITY_IRRIGATE:
1040 /* The call below doesn't support actor tile speculation. */
1041 fc_assert_msg(unit_tile(punit) == ptile,
1042 "Please use action_speculate_unit_on_tile()");
1044 ptile, target);
1045
1046 case ACTIVITY_CULTIVATE:
1047 /* The call below doesn't support actor tile speculation. */
1048 fc_assert_msg(unit_tile(punit) == ptile,
1049 "Please use action_speculate_unit_on_tile()");
1051 punit, ptile, nullptr);
1052
1054 /* The call below doesn't support actor tile speculation. */
1055 fc_assert_msg(unit_tile(punit) == ptile,
1056 "Please use action_speculate_unit_on_self()");
1058 punit);
1059
1060 case ACTIVITY_FORTIFIED:
1061 return FALSE;
1062
1063 case ACTIVITY_BASE:
1064 /* The call below doesn't support actor tile speculation. */
1065 fc_assert_msg(unit_tile(punit) == ptile,
1066 "Please use action_speculate_unit_on_tile()");
1068 punit, ptile, target);
1069
1070 case ACTIVITY_GEN_ROAD:
1071 /* The call below doesn't support actor tile speculation. */
1072 fc_assert_msg(unit_tile(punit) == ptile,
1073 "Please use action_speculate_unit_on_tile()");
1075 punit, ptile, target);
1076
1077 case ACTIVITY_SENTRY:
1079 && !unit_transported(punit)) {
1080 /* Don't let units sentry on tiles they will die on. */
1081 return FALSE;
1082 }
1083 return TRUE;
1084
1085 case ACTIVITY_PILLAGE:
1086 /* The call below doesn't support actor tile speculation. */
1087 fc_assert_msg(unit_tile(punit) == ptile,
1088 "Please use action_speculate_unit_on_tile()");
1090
1091 case ACTIVITY_EXPLORE:
1092 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1093
1094 case ACTIVITY_TRANSFORM:
1095 /* The call below doesn't support actor tile speculation. */
1096 fc_assert_msg(unit_tile(punit) == ptile,
1097 "Please use action_speculate_unit_on_tile()");
1099 punit, ptile, nullptr);
1100
1101 case ACTIVITY_CONVERT:
1102 /* The call below doesn't support actor tile speculation. */
1103 fc_assert_msg(unit_tile(punit) == ptile,
1104 "Please use action_speculate_unit_on_self()");
1106
1107 case ACTIVITY_LAST:
1108 break;
1109 }
1110
1111 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1112 activity);
1113
1114 return FALSE;
1115
1116#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1117}
1118
1119/**********************************************************************/
1125{
1128 punit->activity_count = 0;
1129 punit->activity_target = nullptr;
1130 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1131 /* No longer done. */
1133 }
1134}
1135
1136/**********************************************************************/
1153
1154/**********************************************************************/
1172
1173/**********************************************************************/
1177 const struct tile *ptile)
1178{
1179 unit_list_iterate(ptile->units, punit) {
1180 if (punit->activity == activity) {
1181 return TRUE;
1182 }
1184
1185 return FALSE;
1186}
1187
1188/**********************************************************************/
1193{
1195
1197 unit_list_iterate(ptile->units, punit) {
1200 }
1202
1203 return tgt_ret;
1204}
1205
1206/**********************************************************************/
1209void unit_activity_astr(const struct unit *punit, struct astring *astr)
1210{
1211 if (!punit || !astr) {
1212 return;
1213 }
1214
1215 switch (punit->activity) {
1216 case ACTIVITY_IDLE:
1218 int rate, f;
1219
1221 f = ((punit->fuel) - 1);
1222
1223 /* Add in two parts as move_points_text() returns ptr to static
1224 * End result: "Moves: (fuel)moves_left" */
1225 astr_add_line(astr, "%s: (%s)", _("Moves"),
1227 astr_add(astr, "%s",
1229 } else {
1230 astr_add_line(astr, "%s: %s", _("Moves"),
1232 }
1233 return;
1234 case ACTIVITY_CLEAN:
1235 case ACTIVITY_TRANSFORM:
1237 case ACTIVITY_FORTIFIED:
1238 case ACTIVITY_SENTRY:
1239 case ACTIVITY_GOTO:
1240 case ACTIVITY_EXPLORE:
1241 case ACTIVITY_CONVERT:
1242 case ACTIVITY_CULTIVATE:
1243 case ACTIVITY_PLANT:
1245 return;
1246 case ACTIVITY_MINE:
1247 case ACTIVITY_IRRIGATE:
1248 if (punit->activity_target == nullptr) {
1250 } else {
1251 astr_add_line(astr, "Building %s",
1253 }
1254 return;
1255 case ACTIVITY_PILLAGE:
1256 if (punit->activity_target != nullptr) {
1259 } else {
1261 }
1262 return;
1263 case ACTIVITY_BASE:
1266 return;
1267 case ACTIVITY_GEN_ROAD:
1270 return;
1271 case ACTIVITY_LAST:
1272 break;
1273 }
1274
1275 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1277}
1278
1279/**********************************************************************/
1285void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1286{
1287 if (!punit || !astr) {
1288 return;
1289 }
1290
1291 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1293 punit->upkeep[O_GOLD]);
1294}
1295
1296/**********************************************************************/
1299struct player *unit_nationality(const struct unit *punit)
1300{
1301 return punit->nationality;
1302}
1303
1304/**********************************************************************/
1308void unit_tile_set(struct unit *punit, struct tile *ptile)
1309{
1310 punit->tile = ptile;
1311}
1312
1313/**********************************************************************/
1320struct unit *tile_allied_unit(const struct tile *ptile,
1321 const struct player *pplayer)
1322{
1323 struct unit *punit = nullptr;
1324
1325 unit_list_iterate(ptile->units, cunit) {
1326 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1327 punit = cunit;
1328 } else {
1329 return nullptr;
1330 }
1331 }
1333
1334 return punit;
1335}
1336
1337/**********************************************************************/
1343struct unit *tile_enemy_unit(const struct tile *ptile,
1344 const struct player *pplayer)
1345{
1346 unit_list_iterate(ptile->units, punit) {
1347 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1348 return punit;
1349 }
1351
1352 return nullptr;
1353}
1354
1355/**********************************************************************/
1358struct unit *tile_non_allied_unit(const struct tile *ptile,
1359 const struct player *pplayer,
1361{
1362 unit_list_iterate(ptile->units, punit) {
1363 struct player *owner = unit_owner(punit);
1364
1365 if (everyone_non_allied && owner != pplayer) {
1366 return punit;
1367 }
1368
1369 if (!pplayers_allied(owner, pplayer)
1370 || is_flagless_to_player(punit, pplayer)) {
1371 return punit;
1372 }
1374
1375 return nullptr;
1376}
1377
1378/**********************************************************************/
1382struct unit *tile_other_players_unit(const struct tile *ptile,
1383 const struct player *pplayer)
1384{
1385 unit_list_iterate(ptile->units, punit) {
1386 if (unit_owner(punit) != pplayer) {
1387 return punit;
1388 }
1390
1391 return nullptr;
1392}
1393
1394/**********************************************************************/
1398struct unit *tile_non_attack_unit(const struct tile *ptile,
1399 const struct player *pplayer)
1400{
1401 unit_list_iterate(ptile->units, punit) {
1402 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1403 return punit;
1404 }
1406
1407 return nullptr;
1408}
1409
1410/**********************************************************************/
1418struct unit *unit_occupies_tile(const struct tile *ptile,
1419 const struct player *pplayer)
1420{
1421 unit_list_iterate(ptile->units, punit) {
1422 if (!is_occupying_unit(punit)) {
1423 continue;
1424 }
1425
1427 continue;
1428 }
1429
1430 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1431 return punit;
1432 }
1434
1435 return nullptr;
1436}
1437
1438/**********************************************************************/
1445bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1446 const struct civ_map *zmap)
1447{
1449
1450 square_iterate(zmap, ptile0, 1, ptile) {
1451 struct terrain *pterrain;
1452 struct city *pcity;
1453
1454 pterrain = tile_terrain(ptile);
1455 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1456 continue;
1457 }
1458
1459 pcity = tile_non_allied_city(ptile, pplayer);
1460 if (pcity != nullptr) {
1461 if (unit_list_size(ptile->units) > 0) {
1462 /* Occupied enemy city, it doesn't matter if units inside have
1463 * UTYF_NOZOC or not. */
1464 return FALSE;
1465 }
1466 } else {
1467 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1469 if (tile_has_extra(ptile, pextra)) {
1470 return FALSE;
1471 }
1473 }
1474
1475 unit_list_iterate(ptile->units, punit) {
1476 if (!pplayers_allied(unit_owner(punit), pplayer)
1479 bool hidden = FALSE;
1480
1481 /* We do NOT check the possibility that player is allied with an extra owner,
1482 * and should thus see inside the extra.
1483 * This is to avoid the situation where having an alliance with third player
1484 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1485 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1486 if (tile_has_extra(ptile, pextra)) {
1487 hidden = TRUE;
1488 break;
1489 }
1491
1492 if (!hidden) {
1493 return FALSE;
1494 }
1495 }
1497 }
1499
1500 return TRUE;
1501}
1502
1503/**********************************************************************/
1514bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1515 const struct civ_map *zmap)
1516{
1518
1519 square_iterate(zmap, ptile0, 1, ptile) {
1520 struct terrain *pterrain;
1521 struct city *pcity;
1522
1523 pterrain = tile_terrain(ptile);
1524 if (T_UNKNOWN == pterrain
1525 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1526 continue;
1527 }
1528
1529 pcity = tile_non_allied_city(ptile, pplayer);
1530 if (pcity != nullptr) {
1531 if (pcity->client.occupied
1532 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1533 /* Occupied enemy city, it doesn't matter if units inside have
1534 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1535 return FALSE;
1536 }
1537 } else {
1538 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1540 if (tile_has_extra(ptile, pextra)) {
1541 return FALSE;
1542 }
1544 }
1545
1546 unit_list_iterate(ptile->units, punit) {
1548 && !pplayers_allied(unit_owner(punit), pplayer)
1550 return FALSE;
1551 }
1553 }
1555
1556 return TRUE;
1557}
1558
1559/**********************************************************************/
1567
1568/**********************************************************************/
1577 const struct unit *punit)
1578{
1579 const struct tile *ptile = unit_tile(punit);
1581
1582 if (!is_attack_unit(punit)) {
1583 return FALSE;
1584 }
1585 if (tile_city(ptile)) {
1586 return FALSE;
1587 }
1589 switch (game.info.happyborders) {
1590 case HB_DISABLED:
1591 break;
1592 case HB_NATIONAL:
1593 if (tile_owner(ptile) == unit_owner(punit)) {
1594 return FALSE;
1595 }
1596 break;
1597 case HB_ALLIANCE:
1599 return FALSE;
1600 }
1601 break;
1602 }
1603 }
1604
1607 if (max_friendliness_range >= 0) {
1609 }
1610
1611 return TRUE;
1612}
1613
1614/**********************************************************************/
1618{
1619 switch (activity) {
1620 case ACTIVITY_MINE:
1621 case ACTIVITY_IRRIGATE:
1622 case ACTIVITY_BASE:
1623 case ACTIVITY_GEN_ROAD:
1624 return TRUE;
1625 default:
1626 return FALSE;
1627 }
1628}
1629
1630/**********************************************************************/
1634{
1635 switch (activity) {
1636 case ACTIVITY_PILLAGE:
1637 case ACTIVITY_CLEAN:
1638 return TRUE;
1639 default:
1640 return FALSE;
1641 }
1642}
1643
1644/**********************************************************************/
1648{
1649 switch (activity) {
1650 case ACTIVITY_CULTIVATE:
1651 case ACTIVITY_PLANT:
1652 case ACTIVITY_TRANSFORM:
1653 return TRUE;
1654 default:
1655 return FALSE;
1656 }
1657}
1658
1659/**********************************************************************/
1663{
1664 return is_build_activity(activity)
1665 || is_clean_activity(activity)
1666 || is_terrain_change_activity(activity);
1667}
1668
1669/**********************************************************************/
1673{
1674 return is_build_activity(activity)
1675 || is_clean_activity(activity);
1676}
1677
1678/**********************************************************************/
1682struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1683 const struct unit_type *punittype,
1684 int veteran_level)
1685{
1686 /* Make sure that contents of unit structure are correctly initialized,
1687 * if you ever allocate it by some other mean than fc_calloc() */
1688 struct unit *punit = fc_calloc(1, sizeof(*punit));
1689 int max_vet_lvl;
1690
1691 /* It does not register the unit so the id is set to 0. */
1693
1695
1696 fc_assert_ret_val(!is_server() || pplayer != nullptr, nullptr); /* No unowned units! */
1697 punit->owner = pplayer;
1698 punit->nationality = pplayer;
1699
1700 punit->refcount = 1;
1702
1703 if (pcity != nullptr) {
1705 punit->homecity = pcity->id;
1706 } else {
1707 unit_tile_set(punit, nullptr);
1709 }
1710
1711 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1712 punit->goto_tile = nullptr;
1715 /* A unit new and fresh ... */
1719 /* Random moves units start with zero movement as their first movement
1720 * will be only after their moves have been reset in the beginning of
1721 * the next turn. */
1722 punit->moves_left = 0;
1723 } else {
1725 }
1726 punit->moved = FALSE;
1727
1731
1732 punit->transporter = nullptr;
1734
1735 punit->carrying = nullptr;
1736
1740
1742 punit->action_decision_tile = nullptr;
1743
1744 punit->stay = FALSE;
1745
1748
1749 if (is_server()) {
1751
1753
1754 punit->server.removal_callback = nullptr;
1755
1757 O_LAST * sizeof(*punit->server.upkeep_paid));
1758
1759 punit->server.ord_map = 0;
1760 punit->server.ord_city = 0;
1761
1762 punit->server.vision = nullptr; /* No vision. */
1764 /* Must be an invalid turn number, and an invalid previous turn
1765 * number. */
1766 punit->server.action_turn = -2;
1767 /* punit->server.moving = nullptr; set by fc_calloc(). */
1768
1769 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1770
1771 CALL_FUNC_EACH_AI(unit_alloc, punit);
1772 } else {
1776 punit->client.act_prob_cache = nullptr;
1777 }
1778
1779 return punit;
1780}
1781
1782/**********************************************************************/
1787{
1789
1790 /* Unload unit if transported. */
1793
1794 /* Check for transported units. Use direct access to the list. */
1795 if (unit_list_size(punit->transporting) != 0) {
1796 /* Unload all units. */
1800 }
1802
1803 if (punit->transporting) {
1805 }
1806
1807 CALL_FUNC_EACH_AI(unit_free, punit);
1808
1809 if (is_server() && punit->server.adv) {
1811 } else {
1814 }
1815 }
1816
1817 if (--punit->refcount <= 0) {
1818 FC_FREE(punit);
1819 }
1820}
1821
1822/**********************************************************************/
1827{
1828 if (punit->has_orders) {
1829 punit->goto_tile = nullptr;
1831 punit->orders.list = nullptr;
1832 }
1833 punit->orders.length = 0;
1835}
1836
1837/**********************************************************************/
1841{
1842 return unit_list_size(ptrans->transporting);
1843}
1844
1845/**********************************************************************/
1848static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1849 const struct tile *ptile,
1850 bool (*unit_load_test)
1851 (const struct unit *pc,
1852 const struct unit *pt))
1853{
1854 struct unit *best_trans = nullptr;
1855 struct {
1858 } cur, best = { FALSE };
1859
1860 unit_list_iterate(ptile->units, ptrans) {
1861 if (!unit_load_test(pcargo, ptrans)) {
1862 continue;
1863 } else if (best_trans == nullptr) {
1865 }
1866
1867 /* Gather data from transport stack in a single pass, for use in
1868 * various conditions below. */
1870 cur.outermost_moves_left = ptrans->moves_left;
1871 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1874 cur.has_orders = TRUE;
1875 }
1876 cur.outermost_moves_left = ptranstrans->moves_left;
1877 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1879
1880 /* Criteria for deciding the 'best' transport to load onto.
1881 * The following tests are applied in order; earlier ones have
1882 * lexicographically greater significance than later ones. */
1883
1884 /* Transports which have orders, or are on transports with orders,
1885 * are less preferable to transport stacks without orders (to
1886 * avoid loading on units that are just passing through). */
1887 if (best_trans != ptrans) {
1888 if (!cur.has_orders && best.has_orders) {
1890 } else if (cur.has_orders && !best.has_orders) {
1891 continue;
1892 }
1893 }
1894
1895 /* Else, transports which are idle are preferable (giving players
1896 * some control over loading) -- this does not check transports
1897 * of transports. */
1898 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1899 if (best_trans != ptrans) {
1900 if (cur.is_idle && !best.is_idle) {
1902 } else if (!cur.is_idle && best.is_idle) {
1903 continue;
1904 }
1905 }
1906
1907 /* Else, transports from which the cargo could unload at any time
1908 * are preferable to those where the cargo can only disembark in
1909 * cities/bases. */
1910 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1912 if (best_trans != ptrans) {
1913 if (cur.can_freely_unload && !best.can_freely_unload) {
1915 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1916 continue;
1917 }
1918 }
1919
1920 /* Else, transports which are less deeply nested are preferable. */
1922 if (best_trans != ptrans) {
1923 if (cur.depth < best.depth) {
1925 } else if (cur.depth > best.depth) {
1926 continue;
1927 }
1928 }
1929
1930 /* Else, transport stacks where the outermost transport has more
1931 * moves left are preferable (on the assumption that it's the
1932 * outermost transport that's about to move). */
1933 if (best_trans != ptrans) {
1934 if (cur.outermost_moves_left > best.outermost_moves_left) {
1936 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1937 continue;
1938 }
1939 }
1940
1941 /* All other things being equal, as a tie-breaker, compare the total
1942 * moves left (this turn) and move rate (future turns) for the whole
1943 * stack, to take into account total potential movement for both
1944 * short and long journeys (we don't know which the cargo intends to
1945 * make). Doesn't try to account for whether transports can unload,
1946 * etc. */
1947 if (best_trans != ptrans) {
1948 if (cur.total_moves > best.total_moves) {
1950 } else {
1951 continue;
1952 }
1953 }
1954
1956 best = cur;
1958
1959 return best_trans;
1960}
1961
1962/**********************************************************************/
1971
1972/**********************************************************************/
1978 const struct tile *ptile)
1979{
1981}
1982
1983/**********************************************************************/
1988 const struct unit *punit)
1989{
1991 return FALSE;
1992 }
1993
1996 return FALSE;
1997 }
1999
2000 return TRUE;
2001}
2002
2003/**********************************************************************/
2012 const struct unit *punit,
2013 const struct unit_type *to_unittype)
2014{
2016 return UU_NOT_ENOUGH_ROOM;
2017 }
2018
2019 if (punit->transporter != nullptr) {
2023 }
2025 /* The new unit type can't survive on this terrain. */
2026 return UU_NOT_TERRAIN;
2027 }
2028
2029 return UU_OK;
2030}
2031
2032/**********************************************************************/
2042 const struct unit *punit,
2043 bool is_free)
2044{
2045 struct player *pplayer = unit_owner(punit);
2046 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2048 struct city *pcity;
2049 int cost;
2050
2051 if (!to_unittype) {
2052 return UU_NO_UNITTYPE;
2053 }
2054
2056 /* TODO: There may be other activities that the upgraded unit is not
2057 allowed to do, which we could also test.
2058 -
2059 If convert were legal for new unit_type we could allow, but then it
2060 has a 'head start' getting activity time from the old conversion. */
2061 return UU_NOT_ACTIVITY;
2062 }
2063
2064 if (!is_free) {
2066 if (pplayer->economic.gold < cost) {
2067 return UU_NO_MONEY;
2068 }
2069
2071 if (!pcity) {
2072 return UU_NOT_IN_CITY;
2073 }
2074 if (city_owner(pcity) != pplayer) {
2075 /* TODO: Should upgrades in allied cities be possible? */
2076 return UU_NOT_CITY_OWNER;
2077 }
2078 }
2079
2080 /* TODO: Allow transported units to be reassigned. Check here
2081 * and make changes to upgrade_unit. */
2083}
2084
2085/**********************************************************************/
2088bool unit_can_convert(const struct civ_map *nmap,
2089 const struct unit *punit)
2090{
2091 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2092
2093 if (tgt == nullptr) {
2094 return FALSE;
2095 }
2096
2097 return UU_OK == unit_transform_result(nmap, punit, tgt);
2098}
2099
2100/**********************************************************************/
2105 const struct unit *punit,
2106 char *buf, size_t bufsz)
2107{
2108 struct player *pplayer = unit_owner(punit);
2110 int upgrade_cost;
2111 const struct unit_type *from_unittype = unit_type_get(punit);
2112 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2114 char tbuf[MAX_LEN_MSG];
2115
2116 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2117 "Treasury contains %d gold.",
2118 pplayer->economic.gold),
2119 pplayer->economic.gold);
2120
2121 switch (result) {
2122 case UU_OK:
2124 /* This message is targeted toward the GUI callers. */
2125 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2126 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2127 "Upgrade %s to %s for %d gold?\n%s",
2128 upgrade_cost),
2132 break;
2133 case UU_NO_UNITTYPE:
2135 _("Sorry, cannot upgrade %s (yet)."),
2137 break;
2138 case UU_NO_MONEY:
2140 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2141 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2142 "Upgrading %s to %s costs %d gold.\n%s",
2143 upgrade_cost),
2147 break;
2148 case UU_NOT_IN_CITY:
2149 case UU_NOT_CITY_OWNER:
2151 _("You can only upgrade units in your cities."));
2152 break;
2153 case UU_NOT_ENOUGH_ROOM:
2155 _("Upgrading this %s would strand units it transports."),
2157 break;
2158 case UU_NOT_TERRAIN:
2160 _("Upgrading this %s would result in a %s which can not "
2161 "survive at this place."),
2164 break;
2167 _("Upgrading this %s would result in a %s which its "
2168 "current transport, %s, could not transport."),
2172 break;
2173 case UU_NOT_ACTIVITY:
2175 _("Cannot upgrade %s while doing '%s'."),
2178 break;
2179 }
2180
2181 return result;
2182}
2183
2184/**********************************************************************/
2189 const struct unit *punit)
2190{
2191 int mpco;
2192
2194 &(const struct req_context) {
2195 .player = unit_owner(punit),
2196 .city = unit_tile(punit)
2198 : nullptr,
2199 .tile = unit_tile(punit),
2200 .unit = punit,
2201 .unittype = unit_type_get(punit),
2202 .action = paction,
2203 },
2204 nullptr,
2206
2208
2209 return mpco;
2210}
2211
2212/**********************************************************************/
2215int hp_gain_coord(const struct unit *punit)
2216{
2217 int hp = 0;
2218 const int base = unit_type_get(punit)->hp;
2219 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2220
2221 /* Includes barracks (100%), fortress (25%), etc. */
2223
2224 /* Minimum HP after regen effects applied. */
2225 hp = MAX(hp, min);
2226
2227 /* Regen2 effects that apply after there's at least Min HP */
2228 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2229
2230 return MAX(hp, 0);
2231}
2232
2233/**********************************************************************/
2237{
2238 const struct unit_type *utype = unit_type_get(punit);
2240 struct city *pcity = tile_city(unit_tile(punit));
2241 int gain;
2242
2243 if (!punit->moved) {
2245 } else {
2246 gain = 0;
2247 }
2248
2250
2251 if (!punit->homecity && 0 < game.server.killunhomed
2253 /* Hit point loss of units without homecity; at least 1 hp! */
2254 /* Gameloss units are immune to this effect. */
2255 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2256
2257 if (gain > hp_loss) {
2258 gain = -1;
2259 } else {
2260 gain -= hp_loss;
2261 }
2262 }
2263
2264 if (pcity == nullptr && !tile_has_native_base(unit_tile(punit), utype)
2265 && !unit_transported(punit)) {
2266 gain -= utype->hp * pclass->hp_loss_pct / 100;
2267 }
2268
2269 if (punit->hp + gain > utype->hp) {
2270 gain = utype->hp - punit->hp;
2271 } else if (punit->hp + gain < 0) {
2272 gain = -punit->hp;
2273 }
2274
2275 return gain;
2276}
2277
2278/**********************************************************************/
2281bool is_losing_hp(const struct unit *punit)
2282{
2283 const struct unit_type *punittype = unit_type_get(punit);
2284
2286 < (punittype->hp *
2287 utype_class(punittype)->hp_loss_pct / 100);
2288}
2289
2290/**********************************************************************/
2293bool unit_type_is_losing_hp(const struct player *pplayer,
2294 const struct unit_type *punittype)
2295{
2296 return get_unittype_bonus(pplayer, nullptr, punittype, nullptr,
2298 < (punittype->hp *
2299 utype_class(punittype)->hp_loss_pct / 100);
2300}
2301
2302/**********************************************************************/
2306bool unit_is_alive(int id)
2307{
2308 /* Check if unit exist in game */
2309 if (game_unit_by_number(id)) {
2310 return TRUE;
2311 }
2312
2313 return FALSE;
2314}
2315
2316/**********************************************************************/
2323bool unit_is_virtual(const struct unit *punit)
2324{
2325 if (!punit) {
2326 return FALSE;
2327 }
2328
2329 return punit != game_unit_by_number(punit->id);
2330}
2331
2332/**********************************************************************/
2335void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2336{
2337 return punit->server.ais[ai_type_number(ai)];
2338}
2339
2340/**********************************************************************/
2343void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2344 void *data)
2345{
2346 punit->server.ais[ai_type_number(ai)] = data;
2347}
2348
2349/**********************************************************************/
2360int unit_bribe_cost(const struct unit *punit, const struct player *briber,
2361 const struct unit *briber_unit)
2362{
2363 int cost, default_hp;
2364 struct tile *ptile = unit_tile(punit);
2365 struct player *owner = unit_owner(punit);
2366 const struct unit_type *ptype = unit_type_get(punit);
2367
2368 default_hp = ptype->hp;
2369
2371
2372 if (owner != nullptr) {
2373 int dist = 0;
2374
2375 cost += owner->economic.gold;
2376
2377 /* Consider the distance to the capital. */
2379 city_list_iterate(owner->cities, capital) {
2380 if (is_capital(capital)) {
2381 int tmp = map_distance(capital->tile, ptile);
2382
2383 if (tmp < dist) {
2384 dist = tmp;
2385 }
2386 }
2388
2389 cost /= dist + 2;
2390 }
2391
2392 /* Consider the build cost. */
2394
2395 /* Rule set specific cost modification */
2396 cost += (cost * get_target_bonus_effects(nullptr,
2397 &(const struct req_context) {
2398 .player = owner,
2400 .tile = ptile,
2401 .unit = punit,
2402 .unittype = ptype,
2403 },
2404 &(const struct req_context) {
2405 .player = briber,
2406 .unit = briber_unit,
2408 : nullptr,
2410 : nullptr,
2411 .city = briber_unit
2412 ? game_city_by_number(briber_unit->homecity)
2413 : nullptr,
2414 },
2416 / 100;
2417
2418 /* Veterans are not cheap. */
2419 {
2420 const struct veteran_level *vlevel
2422
2423 fc_assert_ret_val(vlevel != nullptr, 0);
2424 cost = cost * vlevel->power_fact / 100;
2425 if (ptype->move_rate > 0) {
2426 cost += cost * vlevel->move_bonus / ptype->move_rate;
2427 } else {
2428 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2429 }
2430 }
2431
2432 /* Cost now contains the basic bribe cost. We now reduce it by:
2433 * bribecost = cost / 2 + cost / 2 * damage / hp
2434 * = cost / 2 * (1 + damage / hp) */
2435 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2436}
2437
2438/**********************************************************************/
2446int stack_bribe_cost(const struct tile *ptile, const struct player *briber,
2447 const struct unit *briber_unit)
2448{
2449 int bribe_cost = 0;
2450
2454
2455 return bribe_cost;
2456}
2457
2458/**********************************************************************/
2461bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2462{
2464
2465 if (force || can_unit_load(pcargo, ptrans)) {
2466 pcargo->transporter = ptrans;
2467 unit_list_append(ptrans->transporting, pcargo);
2468
2469 return TRUE;
2470 }
2471
2472 return FALSE;
2473}
2474
2475/**********************************************************************/
2479{
2480 struct unit *ptrans;
2481
2482 if (!unit_transported(pcargo)) {
2483 /* 'pcargo' is not transported. */
2484 return FALSE;
2485 }
2486
2487 /* Get the transporter; must not be defined on the client! */
2489 if (ptrans) {
2490 /* 'pcargo' and 'ptrans' should be on the same tile. */
2492
2493#ifndef FREECIV_NDEBUG
2494 bool success =
2495#endif
2496 unit_list_remove(ptrans->transporting, pcargo);
2497
2498 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2500 }
2501
2502 /* For the server (also safe for the client). */
2503 pcargo->transporter = nullptr;
2504
2505 return TRUE;
2506}
2507
2508/**********************************************************************/
2511bool unit_transported(const struct unit *pcargo)
2512{
2513 /* The unit is transported if a transporter unit is set or, (for the client)
2514 * if the transported_by field is set. */
2515 if (is_server()) {
2517 } else {
2519 }
2520}
2521
2522/**********************************************************************/
2525struct unit *unit_transport_get(const struct unit *pcargo)
2526{
2527 return pcargo->transporter;
2528}
2529
2530/**********************************************************************/
2533struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2534{
2535 fc_assert_ret_val(ptrans->transporting != nullptr, nullptr);
2536
2537 return ptrans->transporting;
2538}
2539
2540/**********************************************************************/
2543static inline bool
2551
2552/**********************************************************************/
2561 const struct unit *ptrans)
2562{
2563 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2564
2565 /* Check 'pcargo' against 'ptrans'. */
2567 return FALSE;
2568 }
2569
2570 /* Check 'pcargo' against 'ptrans' parents. */
2573 return FALSE;
2574 }
2576
2577 /* Check cargo children... */
2580
2581 /* ...against 'ptrans'. */
2583 return FALSE;
2584 }
2585
2586 /* ...and against 'ptrans' parents. */
2589 return FALSE;
2590 }
2593
2594 return TRUE;
2595}
2596
2597/**********************************************************************/
2601bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2602{
2604 if (ptrans == plevel) {
2605 return TRUE;
2606 }
2608 return FALSE;
2609}
2610
2611/**********************************************************************/
2614int unit_cargo_depth(const struct unit *ptrans)
2615{
2616 struct cargo_iter iter;
2617 struct iterator *it;
2618 int depth = 0;
2619
2620 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2621 iterator_next(it)) {
2622 if (iter.depth > depth) {
2623 depth = iter.depth;
2624 }
2625 }
2626 return depth;
2627}
2628
2629/**********************************************************************/
2633{
2634 int level = 0;
2635
2637 level++;
2639 return level;
2640}
2641
2642/**********************************************************************/
2646{
2647 return sizeof(struct cargo_iter);
2648}
2649
2650/**********************************************************************/
2653static void *cargo_iter_get(const struct iterator *it)
2654{
2655 const struct cargo_iter *iter = CARGO_ITER(it);
2656
2657 return unit_list_link_data(iter->links[iter->depth - 1]);
2658}
2659
2660/**********************************************************************/
2663static void cargo_iter_next(struct iterator *it)
2664{
2665 struct cargo_iter *iter = CARGO_ITER(it);
2666 const struct unit_list_link *piter;
2667 const struct unit_list_link *pnext;
2668
2669 /* Variant 1: unit has cargo. */
2671 if (pnext != nullptr) {
2672 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2673 iter->links[iter->depth++] = pnext;
2674 return;
2675 }
2676
2677 fc_assert(iter->depth > 0);
2678
2679 while (iter->depth > 0) {
2680 piter = iter->links[iter->depth - 1];
2681
2682 /* Variant 2: there are other cargo units at same level. */
2684 if (pnext != nullptr) {
2685 iter->links[iter->depth - 1] = pnext;
2686 return;
2687 }
2688
2689 /* Variant 3: return to previous level, and do same tests. */
2690 iter->depth--;
2691 }
2692}
2693
2694/**********************************************************************/
2697static bool cargo_iter_valid(const struct iterator *it)
2698{
2699 return (0 < CARGO_ITER(it)->depth);
2700}
2701
2702/**********************************************************************/
2706 const struct unit *ptrans)
2707{
2708 struct iterator *it = ITERATOR(iter);
2709
2710 it->get = cargo_iter_get;
2711 it->next = cargo_iter_next;
2712 it->valid = cargo_iter_valid;
2714 iter->depth = (iter->links[0] != nullptr ? 1 : 0);
2715
2716 return it;
2717}
2718
2719/**********************************************************************/
2722bool unit_is_cityfounder(const struct unit *punit)
2723{
2725}
2726
2727/**********************************************************************/
2731 int length, const struct unit_order *orders)
2732{
2733 int i;
2734
2735 for (i = 0; i < length; i++) {
2736 struct action *paction;
2737 struct extra_type *pextra;
2738
2739 if (orders[i].order > ORDER_LAST) {
2740 log_error("invalid order %d at index %d", orders[i].order, i);
2741 return FALSE;
2742 }
2743 switch (orders[i].order) {
2744 case ORDER_MOVE:
2745 case ORDER_ACTION_MOVE:
2746 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2747 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2748 return FALSE;
2749 }
2750 break;
2751 case ORDER_ACTIVITY:
2752 switch (orders[i].activity) {
2753 case ACTIVITY_SENTRY:
2754 if (i != length - 1) {
2755 /* Only allowed as the last order. */
2756 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2757 i);
2758 return FALSE;
2759 }
2760 break;
2761 /* Replaced by action orders */
2762 case ACTIVITY_BASE:
2763 case ACTIVITY_GEN_ROAD:
2764 case ACTIVITY_CLEAN:
2765 case ACTIVITY_PILLAGE:
2766 case ACTIVITY_MINE:
2767 case ACTIVITY_IRRIGATE:
2768 case ACTIVITY_PLANT:
2769 case ACTIVITY_CULTIVATE:
2770 case ACTIVITY_TRANSFORM:
2771 case ACTIVITY_CONVERT:
2773 log_error("at index %d, use action rather than activity %d.",
2774 i, orders[i].activity);
2775 return FALSE;
2776 /* Not supported. */
2777 case ACTIVITY_EXPLORE:
2778 case ACTIVITY_IDLE:
2779 /* Not set from the client. */
2780 case ACTIVITY_GOTO:
2781 case ACTIVITY_FORTIFIED:
2782 /* Unused. */
2783 case ACTIVITY_LAST:
2784 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2785 return FALSE;
2786 }
2787
2788 break;
2790 if (!action_id_exists(orders[i].action)) {
2791 /* Non-existent action. */
2792 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2793 return FALSE;
2794 }
2795
2796 paction = action_by_number(orders[i].action);
2797
2798 /* Validate main target. */
2799 if (index_to_tile(nmap, orders[i].target) == nullptr) {
2800 log_error("at index %d, invalid tile target %d for the action %d.",
2801 i, orders[i].target, orders[i].action);
2802 return FALSE;
2803 }
2804
2805 if (orders[i].dir != DIR8_ORIGIN) {
2806 log_error("at index %d, the action %d sets the outdated target"
2807 " specification dir.",
2808 i, orders[i].action);
2809 }
2810
2811 /* Validate sub target. */
2812 switch (action_id_get_sub_target_kind(orders[i].action)) {
2813 case ASTK_BUILDING:
2814 /* Sub target is a building. */
2815 if (!improvement_by_number(orders[i].sub_target)) {
2816 /* Sub target is invalid. */
2817 log_error("at index %d, cannot do %s without a target.", i,
2818 action_id_rule_name(orders[i].action));
2819 return FALSE;
2820 }
2821 break;
2822 case ASTK_TECH:
2823 /* Sub target is a technology. */
2824 if (orders[i].sub_target == A_NONE
2825 || (!valid_advance_by_number(orders[i].sub_target)
2826 && orders[i].sub_target != A_FUTURE)) {
2827 /* Target tech is invalid. */
2828 log_error("at index %d, cannot do %s without a target.", i,
2829 action_id_rule_name(orders[i].action));
2830 return FALSE;
2831 }
2832 break;
2833 case ASTK_EXTRA:
2835 /* Sub target is an extra. */
2836 pextra = (!(orders[i].sub_target == NO_TARGET
2837 || (orders[i].sub_target < 0
2838 || (orders[i].sub_target
2840 ? extra_by_number(orders[i].sub_target) : nullptr);
2841 fc_assert(pextra == nullptr || !(pextra->ruledit_disabled));
2842
2843 if (pextra == nullptr) {
2844 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2845 /* Target extra is invalid. */
2846 log_error("at index %d, cannot do %s without a target.", i,
2847 action_id_rule_name(orders[i].action));
2848 return FALSE;
2849 }
2850 } else {
2851 if (!actres_removes_extra(paction->result, pextra)
2852 && !actres_creates_extra(paction->result, pextra)) {
2853 /* Target extra is irrelevant for the action. */
2854 log_error("at index %d, cannot do %s to %s.", i,
2855 action_id_rule_name(orders[i].action),
2856 extra_rule_name(pextra));
2857 return FALSE;
2858 }
2859 }
2860 break;
2861 case ASTK_SPECIALIST:
2862 if (!specialist_by_number(orders[i].sub_target)) {
2863 log_error("at index %d, cannot do %s without a target.", i,
2864 action_id_rule_name(orders[i].action));
2865 return FALSE;
2866 }
2867 break;
2868 case ASTK_NONE:
2869 /* No validation required. */
2870 break;
2871 /* Invalid action? */
2872 case ASTK_COUNT:
2875 FALSE,
2876 "Bad action %d in order number %d.", orders[i].action, i);
2877 }
2878
2879 /* Some action orders are sane only in the last order. */
2880 if (i != length - 1) {
2881 /* If the unit is dead, */
2883 /* or if Freeciv has no idea where the unit will end up after it
2884 * has performed this action, */
2885 || !(utype_is_unmoved_by_action(paction, nullptr)
2887 /* or if the unit will end up standing still, */
2889 /* than having this action in the middle of a unit's orders is
2890 * probably wrong. */
2891 log_error("action %d is not allowed at index %d.",
2892 orders[i].action, i);
2893 return FALSE;
2894 }
2895 }
2896
2897 /* Don't validate that the target tile really contains a target or
2898 * that the actor player's map think the target tile has one.
2899 * The player may target something from their player map that isn't
2900 * there any more, a target they think is there even if their player
2901 * map doesn't have it, or even a target they assume will be there
2902 * when the unit reaches the target tile.
2903 *
2904 * With that said: The client should probably at least have an
2905 * option to only aim city targeted actions at cities. */
2906
2907 break;
2908 case ORDER_FULL_MP:
2909 break;
2910 case ORDER_LAST:
2911 /* An invalid order. This is handled above. */
2912 break;
2913 }
2914 }
2915
2916 return TRUE;
2917}
2918
2919/**********************************************************************/
2924 int length,
2925 const struct unit_order *orders)
2926{
2927 struct unit_order *unit_orders;
2928
2929 if (!unit_order_list_is_sane(nmap, length, orders)) {
2930 return nullptr;
2931 }
2932
2933 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2934 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2935
2936 return unit_orders;
2937}
2938
2939/**********************************************************************/
2943{
2945 ACTION_NONE, // ACTIVITY_IDLE
2946 ACTION_CULTIVATE, // ACTIVITY_CULTIVATE
2947 ACTION_MINE, // ACTIVITY_MINE
2948 ACTION_IRRIGATE, // ACTIVITY_IRRIGATE
2949 ACTION_FORTIFY, // ACTIVITY_FORTIFIED
2950 ACTION_NONE, // ACTIVITY_SENTRY
2951 ACTION_PILLAGE, // ACTIVITY_PILLAGE
2952 ACTION_NONE, // ACTIVITY_GOTO
2953 ACTION_NONE, // ACTIVITY_EXPLORE
2954 ACTION_TRANSFORM_TERRAIN, // ACTIVITY_TRANSFORM
2955 ACTION_FORTIFY, // ACTIVITY_FORTIFYING
2956 ACTION_CLEAN, // ACTIVITY_CLEAN
2957 ACTION_BASE, // ACTIVITY_BASE
2958 ACTION_ROAD, // ACTIVITY_GEN_ROAD
2959 ACTION_CONVERT, // ACTIVITY_CONVERT
2960 ACTION_PLANT // ACTIVITY_PLANT
2961 };
2962
2963 return act_act[act];
2964}
2965
2966/**********************************************************************/
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5317
struct act_prob action_prob_vs_tile(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:4838
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1260
bool is_action_enabled_unit_on_tile(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:3344
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:3103
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:3180
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:4480
bool action_maybe_possible_actor_unit(const struct civ_map *nmap, const action_id act_id, const struct unit *actor_unit)
Definition actions.c:5740
bool action_id_exists(const action_id act_id)
Definition actions.c:1089
bool is_action_enabled_unit_on_self(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit)
Definition actions.c:3493
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:4563
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:245
#define action_id_get_sub_target_kind(act_id)
Definition actions.h:421
static struct action * action_by_number(action_id act_id)
Definition actions.h:400
#define action_has_result(_act_, _res_)
Definition actions.h:184
#define action_by_result_iterate_end
Definition actions.h:249
#define action_id(_act_)
Definition actions.h:426
#define ACTION_NONE
Definition actions.h:59
bool actres_removes_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:811
bool actres_creates_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:790
int ai_type_number(const struct ai_type *ai)
Definition ai.c:278
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:390
void astr_add_line(struct astring *astr, const char *format,...)
Definition astring.c:283
void astr_add(struct astring *astr, const char *format,...)
Definition astring.c:271
#define BV_CLR_ALL(bv)
Definition bitvector.h:103
#define BV_SET(bv, bit)
Definition bitvector.h:89
bool is_capital(const struct city *pcity)
Definition city.c:1620
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2107
bool is_unit_near_a_friendly_city(const struct civ_map *nmap, const struct unit *punit, int distance)
Definition city.c:2122
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:565
#define city_owner(_pcity_)
Definition city.h:564
#define city_list_iterate_end
Definition city.h:510
char * incite_cost
Definition comments.c:77
#define MAX_LEN_MSG
Definition conn_types.h:37
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2977
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int const struct action *paction struct unit struct city * pcity
Definition dialogs_g.h:78
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:74
int get_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Definition effects.c:1071
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:1036
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:215
enum unit_activity Activity_type_id
Definition fc_types.h:240
#define DIR8_ORIGIN
Definition fc_types.h:316
int action_id
Definition fc_types.h:250
@ HB_ALLIANCE
Definition fc_types.h:988
@ HB_DISABLED
Definition fc_types.h:986
@ HB_NATIONAL
Definition fc_types.h:987
@ O_SHIELD
Definition fc_types.h:103
@ O_FOOD
Definition fc_types.h:103
@ O_GOLD
Definition fc_types.h:103
@ O_LAST
Definition fc_types.h:103
@ BORDERS_DISABLED
Definition fc_types.h:732
enum output_type_id Output_type_id
Definition fc_types.h:239
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:94
#define Q_(String)
Definition fcintl.h:70
#define PL_(String1, String2, n)
Definition fcintl.h:71
#define _(String)
Definition fcintl.h:67
struct civ_game game
Definition game.c:62
struct unit * game_unit_by_number(int id)
Definition game.c:115
struct city * game_city_by_number(int id)
Definition game.c:106
#define GAME_UNIT_BRIBE_DIST_MAX
Definition game.h:764
#define GAME_TRANSPORT_MAX_RECURSIVE
Definition game.h:767
struct city * owner
Definition citydlg.c:226
static const int bufsz
Definition helpdlg.c:70
struct impr_type * improvement_by_number(const Impr_type_id id)
static bool iterator_valid(const struct iterator *it)
Definition iterator.h:61
#define ITERATOR(p)
Definition iterator.h:37
static void iterator_next(struct iterator *it)
Definition iterator.h:42
#define fc_assert_msg(condition, message,...)
Definition log.h:182
#define fc_assert_ret(condition)
Definition log.h:192
#define fc_assert(condition)
Definition log.h:177
#define fc_assert_ret_val(condition, val)
Definition log.h:195
#define log_error(message,...)
Definition log.h:104
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition log.h:209
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:471
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:1085
bool map_untrusted_dir_is_valid(enum direction8 dir)
Definition map.c:1423
enum direction8 rand_direction(void)
Definition map.c:2093
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:699
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:397
#define square_iterate_end
Definition map.h:400
#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:1066
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Definition movement.c:278
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:362
bool could_exist_in_city(const struct civ_map *nmap, const struct player *pov_player, const struct unit_type *utype, const struct city *pcity)
Definition movement.c:309
bool can_unit_transport(const struct unit *transporter, const struct unit *transported)
Definition movement.c:906
int unit_move_rate(const struct unit *punit)
Definition movement.c:89
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:522
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:496
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:919
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:1095
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1376
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1397
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1451
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MIN(x, y)
Definition shared.h:55
#define FC_INFINITY
Definition shared.h:36
#define MAX(x, y)
Definition shared.h:54
struct specialist * specialist_by_number(const Specialist_type_id id)
Definition specialist.c:110
Definition ai.h:50
struct iterator vtable
Definition unit.c:51
int depth
Definition unit.c:53
const struct unit_list_link * links[GAME_TRANSPORT_MAX_RECURSIVE]
Definition unit.c:52
Definition city.h:318
struct tile * tile
Definition city.h:320
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
int killunhomed
Definition game.h:161
struct civ_game::@32::@36 server
bool ruledit_disabled
Definition extras.h:91
bool(* valid)(const struct iterator *it)
Definition iterator.h:34
void *(* get)(const struct iterator *it)
Definition iterator.h:33
void(* next)(struct iterator *it)
Definition iterator.h:32
enum borders_mode borders
enum happyborders_type happyborders
enum airlifting_style airlifting_style
bool airlift_from_always_enabled
bool airlift_to_always_enabled
struct player_economic economic
Definition player.h:284
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
int sub_target
Definition unit.h:99
int transport_capacity
Definition unittype.h:530
bool worker
Definition unittype.h:582
int move_rate
Definition unittype.h:524
enum vision_layer vlayer
Definition unittype.h:576
struct unit_type::@90 adv
const struct unit_type * converted_to
Definition unittype.h:537
Definition unit.h:140
time_t action_timestamp
Definition unit.h:248
int length
Definition unit.h:198
int upkeep[O_LAST]
Definition unit.h:150
bool has_orders
Definition unit.h:196
bool occupied
Definition unit.h:222
enum action_decision action_decision_want
Definition unit.h:205
int battlegroup
Definition unit.h:194
enum unit_activity activity
Definition unit.h:159
int moves_left
Definition unit.h:152
int refcount
Definition unit.h:143
int id
Definition unit.h:147
enum gen_action action
Definition unit.h:160
struct unit_list * transporting
Definition unit.h:187
int ord_city
Definition unit.h:245
struct unit::@83 orders
bool moved
Definition unit.h:176
int ord_map
Definition unit.h:244
bool debug
Definition unit.h:237
struct vision * vision
Definition unit.h:247
int hp
Definition unit.h:153
int fuel
Definition unit.h:155
struct extra_type * changed_from_target
Definition unit.h:173
int current_form_turn
Definition unit.h:211
bool stay
Definition unit.h:208
bool colored
Definition unit.h:225
enum direction8 facing
Definition unit.h:144
struct unit::@84::@87 server
struct tile * tile
Definition unit.h:142
struct unit::@84::@86 client
struct extra_type * activity_target
Definition unit.h:167
struct act_prob * act_prob_cache
Definition unit.h:231
int activity_count
Definition unit.h:165
struct unit_order * list
Definition unit.h:201
enum unit_activity changed_from
Definition unit.h:171
struct unit_adv * adv
Definition unit.h:239
struct player * nationality
Definition unit.h:146
int transported_by
Definition unit.h:219
void(* removal_callback)(struct unit *punit)
Definition unit.h:256
void * ais[FREECIV_AI_MOD_LAST]
Definition unit.h:240
int action_turn
Definition unit.h:249
int homecity
Definition unit.h:148
int upkeep_paid[O_LAST]
Definition unit.h:259
bool paradropped
Definition unit.h:177
bool done_moving
Definition unit.h:184
struct unit * transporter
Definition unit.h:186
int birth_turn
Definition unit.h:210
struct goods_type * carrying
Definition unit.h:189
struct tile * goto_tile
Definition unit.h:157
struct tile * action_decision_tile
Definition unit.h:206
const struct unit_type * utype
Definition unit.h:141
int veteran
Definition unit.h:154
int changed_from_count
Definition unit.h:172
struct player * owner
Definition unit.h:145
bool dying
Definition unit.h:253
enum unit_focus_status focus_status
Definition unit.h:217
enum server_side_agent ssa_controller
Definition unit.h:175
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
struct advance * valid_advance_by_number(const Tech_type_id id)
Definition tech.c:181
#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:177
bool tile_is_placing(const struct tile *ptile)
Definition tile.c:1132
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Definition tile.c:419
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:325
int tile_has_not_aggressive_extra_for_unit(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:197
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:393
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
@ TILE_KNOWN_UNSEEN
Definition tile.h:37
#define ACTIVITY_FACTOR
Definition tile.h:170
#define tile_terrain(_tile)
Definition tile.h:115
#define tile_has_extra(ptile, pextra)
Definition tile.h:152
#define tile_owner(_tile)
Definition tile.h:97
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
bool unit_can_do_action_sub_result(const struct unit *punit, enum action_sub_result sub_result)
Definition unit.c:422
size_t cargo_iter_sizeof(void)
Definition unit.c:2645
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1662
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2923
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:2011
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1562
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2293
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1647
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:233
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1840
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2663
bool can_unit_change_homecity_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pcity)
Definition unit.c:508
void free_unit_orders(struct unit *punit)
Definition unit.c:1826
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2343
bool unit_is_alive(int id)
Definition unit.c:2306
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:303
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:343
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:469
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:567
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2188
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:583
int unit_upkeep_cost(const struct unit *punit, Output_type_id otype)
Definition unit.c:2972
bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1514
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2461
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1977
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2705
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2104
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2632
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2281
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1672
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:766
bool unit_can_add_or_build_city(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:491
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1398
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2215
bool can_unit_change_homecity(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:524
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2653
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1299
bool unit_being_aggressive(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:1576
int get_activity_rate(const struct unit *punit)
Definition unit.c:538
bool can_unit_deboard_or_be_unloaded(const struct civ_map *nmap, const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:818
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2560
bool is_attack_unit(const struct unit *punit)
Definition unit.c:324
bool unit_can_airlift_to(const struct civ_map *nmap, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:205
const Activity_type_id tile_changing_activities[]
Definition unit.c:45
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2525
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2478
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1617
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2601
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2236
int unit_bribe_cost(const struct unit *punit, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2360
enum unit_airlift_result test_unit_can_airlift_to(const struct civ_map *nmap, const struct player *restriction, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:86
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:882
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1122
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1285
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:741
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2722
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1176
bool is_field_unit(const struct unit *punit)
Definition unit.c:457
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2697
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:402
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:412
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2942
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:335
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, enum gen_action action)
Definition unit.c:912
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1682
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2614
bool can_unit_do_autoworker(const struct unit *punit)
Definition unit.c:647
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Definition unit.c:2544
#define CARGO_ITER(iter)
Definition unit.c:55
bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:847
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2730
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:655
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1192
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:316
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1445
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:1157
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:351
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1633
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:863
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1209
bool unit_can_help_build_wonder_here(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:277
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1382
bool is_special_unit(const struct unit *punit)
Definition unit.c:369
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1786
int stack_bribe_cost(const struct tile *ptile, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2446
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1343
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1308
void set_unit_activity(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1139
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:2041
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2511
bool are_unit_orders_equal(const struct unit_order *order1, const struct unit_order *order2)
Definition unit.c:60
bool unit_is_virtual(const struct unit *punit)
Definition unit.c:2323
bool is_flagless_to_player(const struct unit *punit, const struct player *pplayer)
Definition unit.c:382
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:431
bool is_guard_unit(const struct unit *punit)
Definition unit.c:359
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:793
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1418
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2533
bool can_unit_do_activity_targeted_at(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, enum gen_action action, struct extra_type *target, const struct tile *ptile)
Definition unit.c:967
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1987
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2335
bool unit_has_orders(const struct unit *punit)
Definition unit.c:221
struct unit * tile_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1320
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1967
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:1848
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2088
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:607
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:709
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.c:1358
bool can_unit_do_activity_targeted(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, enum gen_action action, struct extra_type *target)
Definition unit.c:953
#define unit_tile(_pu)
Definition unit.h:408
#define unit_transports_iterate_end
Definition unit.h:592
#define unit_cargo_iterate_end
Definition unit.h:602
@ 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:568
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:599
#define unit_owner(_pu)
Definition unit.h:407
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:588
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:571
unit_airlift_result
Definition unit.h:73
@ AR_SRC_NO_FLIGHTS
Definition unit.h:85
@ AR_OK_SRC_UNKNOWN
Definition unit.h:76
@ AR_OK_DST_UNKNOWN
Definition unit.h:77
@ AR_NO_MOVES
Definition unit.h:79
@ AR_BAD_DST_CITY
Definition unit.h:84
@ AR_NOT_IN_CITY
Definition unit.h:82
@ AR_OCCUPIED
Definition unit.h:81
@ AR_OK
Definition unit.h:75
@ AR_DST_NO_FLIGHTS
Definition unit.h:86
@ AR_WRONG_UNITTYPE
Definition unit.h:80
@ AR_BAD_SRC_CITY
Definition unit.h:83
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.h:446
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_safe(unitlist, _unit)
Definition unitlist.h:39
#define unit_list_iterate_end
Definition unitlist.h:33
#define unit_list_iterate_safe_end
Definition unitlist.h:61
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:126
int utype_pays_mp_for_action_base(const struct action *paction, const struct unit_type *putype)
Definition unittype.c:1406
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:325
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1595
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1274
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2997
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1613
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1493
bool utype_may_act_move_frags(const struct unit_type *punit_type, const action_id act_id, const int move_fragments)
Definition unittype.c:1083
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1509
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1729
bool utype_can_freely_load(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:313
int utype_upkeep_cost(const struct unit_type *ut, const struct unit *punit, struct player *pplayer, Output_type_id otype)
Definition unittype.c:136
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2657
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2530
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:412
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Definition unittype.c:2629
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:215
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1244
bool utype_can_do_action_sub_result(const struct unit_type *putype, enum action_sub_result sub_result)
Definition unittype.c:433
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1313
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:468
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1586
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:396
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1757
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:773
#define utype_class(_t_)
Definition unittype.h:756
#define utype_fuel(ptype)
Definition unittype.h:847
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624