Freeciv-3.1
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
45
47 { ACTIVITY_PILLAGE, ACTIVITY_GEN_ROAD, ACTIVITY_IRRIGATE, ACTIVITY_MINE,
48 ACTIVITY_BASE, ACTIVITY_CULTIVATE, ACTIVITY_PLANT, ACTIVITY_TRANSFORM,
49 ACTIVITY_POLLUTION, ACTIVITY_FALLOUT, ACTIVITY_LAST };
50
51struct cargo_iter {
53 const struct unit_list_link *links[GAME_TRANSPORT_MAX_RECURSIVE];
54 int depth;
55};
56#define CARGO_ITER(iter) ((struct cargo_iter *) (iter))
57
58/**********************************************************************/
61bool are_unit_orders_equal(const struct unit_order *order1,
62 const struct unit_order *order2)
63{
64 return order1->order == order2->order
65 && order1->activity == order2->activity
66 && order1->target == order2->target
67 && order1->sub_target == order2->sub_target
68 && order1->action == order2->action
69 && order1->dir == order2->dir;
70}
71
72/**********************************************************************/
84 const struct player *restriction,
85 const struct unit *punit,
86 const struct city *pdest_city)
87{
88 const struct city *psrc_city = tile_city(unit_tile(punit));
89 const struct player *punit_owner;
90 enum unit_airlift_result ok_result = AR_OK;
91
92 if (0 == punit->moves_left
94 ACTION_AIRLIFT, 0)) {
95 /* No moves left. */
96 return AR_NO_MOVES;
97 }
98
99 if (!unit_can_do_action(punit, ACTION_AIRLIFT)) {
100 return AR_WRONG_UNITTYPE;
101 }
102
104 /* Units with occupants can't be airlifted currently. */
105 return AR_OCCUPIED;
106 }
107
108 if (NULL == psrc_city) {
109 /* No city there. */
110 return AR_NOT_IN_CITY;
111 }
112
113 if (psrc_city == pdest_city) {
114 /* Airlifting to our current position doesn't make sense. */
115 return AR_BAD_DST_CITY;
116 }
117
118 if (pdest_city
119 && (NULL == restriction
120 || (tile_get_known(city_tile(pdest_city), restriction)
121 == TILE_KNOWN_SEEN))
122 && !can_unit_exist_at_tile(nmap, punit, city_tile(pdest_city))) {
123 /* Can't exist at the destination tile. */
124 return AR_BAD_DST_CITY;
125 }
126
127 punit_owner = unit_owner(punit);
128
129 /* Check validity of both source and destination before checking capacity,
130 * to avoid misleadingly optimistic returns. */
131
132 if (punit_owner != city_owner(psrc_city)
133 && !(game.info.airlifting_style & AIRLIFTING_ALLIED_SRC
134 && pplayers_allied(punit_owner, city_owner(psrc_city)))) {
135 /* Not allowed to airlift from this source. */
136 return AR_BAD_SRC_CITY;
137 }
138
139 if (pdest_city
140 && punit_owner != city_owner(pdest_city)
141 && !(game.info.airlifting_style & AIRLIFTING_ALLIED_DEST
142 && pplayers_allied(punit_owner, city_owner(pdest_city)))) {
143 /* Not allowed to airlift to this destination. */
144 return AR_BAD_DST_CITY;
145 }
146
147 if (NULL == restriction || city_owner(psrc_city) == restriction) {
148 /* We know for sure whether or not src can airlift this turn. */
149 if (0 >= psrc_city->airlift
150 && (!(game.info.airlifting_style & AIRLIFTING_UNLIMITED_SRC)
152 /* The source cannot airlift for this turn (maybe already airlifted
153 * or no airport).
154 * See also do_airline() in server/unittools.h. */
155 return AR_SRC_NO_FLIGHTS;
156 } /* else, there is capacity; continue to other checks */
157 } else {
158 /* We don't have access to the 'airlift' field. Assume it's OK; can
159 * only find out for sure by trying it. */
160 ok_result = AR_OK_SRC_UNKNOWN;
161 }
162
163 if (pdest_city) {
164 if (NULL == restriction || city_owner(pdest_city) == restriction) {
165 if (0 >= pdest_city->airlift
166 && (!(game.info.airlifting_style & AIRLIFTING_UNLIMITED_DEST)
168 /* The destination cannot support airlifted units for this turn
169 * (maybe already airlifted or no airport).
170 * See also do_airline() in server/unittools.h. */
171 return AR_DST_NO_FLIGHTS;
172 } /* else continue */
173 } else {
174 ok_result = AR_OK_DST_UNKNOWN;
175 }
176 }
177
178 return ok_result;
179}
180
181/**********************************************************************/
188bool unit_can_airlift_to(const struct unit *punit,
189 const struct city *pdest_city)
190{
191 const struct civ_map *nmap = &(wld.map);
192
193 fc_assert_ret_val(pdest_city, FALSE);
194
195 if (is_server()) {
196 return is_action_enabled_unit_on_city(nmap, ACTION_AIRLIFT,
197 punit, pdest_city);
198 } else {
199 return action_prob_possible(action_prob_vs_city(nmap, punit, ACTION_AIRLIFT,
200 pdest_city));
201 }
202}
203
204/**********************************************************************/
207bool unit_has_orders(const struct unit *punit)
208{
209 return punit->has_orders;
210}
211
212/**********************************************************************/
219int unit_shield_value(const struct unit *punit,
220 const struct unit_type *punittype,
221 const struct action *paction)
222{
223 int value;
224
225 bool has_unit;
226 const struct player *act_player;
227
228 has_unit = punit != NULL;
229
230 if (has_unit && punittype == NULL) {
231 punittype = unit_type_get(punit);
232 }
233
234 fc_assert_ret_val(punittype != NULL, 0);
235 fc_assert(punit == NULL || unit_type_get(punit) == punittype);
236 fc_assert_ret_val(paction != NULL, 0);
237
238 act_player = has_unit ? unit_owner(punit) : NULL;
239 /* TODO: determine if tile and city should be where the unit currently is
240 * located or the target city. Those two may differ. Wait for ruleset
241 * author feed back. */
242
243 value = utype_build_shield_cost_base(punittype);
244 value += ((value
246 &(const struct req_context) {
247 .player = act_player,
248 .unit = punit,
249 .unittype = punittype,
250 .action = paction,
251 },
252 NULL,
253 EFT_UNIT_SHIELD_VALUE_PCT))
254 / 100);
255
256 return value;
257}
258
259/**********************************************************************/
264{
265 struct city *pcity = tile_city(unit_tile(punit));
266 const struct civ_map *nmap = &(wld.map);
267
268 if (pcity == NULL) {
269 /* No city to help at this tile. */
270 return FALSE;
271 }
272
273 if (!utype_can_do_action(unit_type_get(punit), ACTION_HELP_WONDER)) {
274 /* This unit can never do help wonder. */
275 return FALSE;
276 }
277
278 /* Evaluate all action enablers for extra accuracy. */
279 /* TODO: Is it worth it? */
281 ACTION_HELP_WONDER,
282 pcity));
283}
284
285/**********************************************************************/
290{
291 struct city *phomecity, *pdestcity;
292
293 return (utype_can_do_action(unit_type_get(punit), ACTION_TRADE_ROUTE)
294 && (pdestcity = tile_city(unit_tile(punit)))
295 && (phomecity = game_city_by_number(punit->homecity))
296 && can_cities_trade(phomecity, pdestcity));
297}
298
299/**********************************************************************/
303{
305}
306
307/**********************************************************************/
310bool is_attack_unit(const struct unit *punit)
311{
312 return ((unit_can_do_action_result(punit, ACTRES_ATTACK)
313 || unit_can_do_action_result(punit, ACTRES_BOMBARD))
314 && unit_type_get(punit)->attack_strength > 0);
315}
316
317/**********************************************************************/
322bool is_military_unit(const struct unit *punit)
323{
324 return !unit_has_type_flag(punit, UTYF_CIVILIAN);
325}
326
327/**********************************************************************/
331bool unit_can_do_action(const struct unit *punit,
332 const action_id act_id)
333{
334 return utype_can_do_action(unit_type_get(punit), act_id);
335}
336
337/**********************************************************************/
342 enum action_result result)
343{
345}
346
347/**********************************************************************/
352 enum action_sub_result sub_result)
353{
355}
356
357/**********************************************************************/
360bool is_square_threatened(const struct civ_map *nmap,
361 const struct player *pplayer,
362 const struct tile *ptile, bool omniscient)
363{
364 square_iterate(nmap, ptile, 2, ptile1) {
365 unit_list_iterate(ptile1->units, punit) {
366 if ((omniscient
367 || can_player_see_unit(pplayer, punit))
368 && pplayers_at_war(pplayer, unit_owner(punit))
372 && is_native_near_tile(nmap,
373 unit_class_get(punit), ptile)))) {
374 return TRUE;
375 }
378
379 return FALSE;
380}
381
382/**********************************************************************/
386bool is_field_unit(const struct unit *punit)
387{
388 return unit_has_type_flag(punit, UTYF_FIELDUNIT);
389}
390
391/**********************************************************************/
398bool is_hiding_unit(const struct unit *punit)
399{
400 enum vision_layer vl = unit_type_get(punit)->vlayer;
401
402 if (vl == V_INVIS || vl == V_SUBSURFACE) {
403 return TRUE;
404 }
405
406 if (unit_transported(punit)) {
408 if (vl == V_INVIS || vl == V_SUBSURFACE) {
409 return TRUE;
410 }
411 }
412
413 return FALSE;
414}
415
416/**********************************************************************/
421{
422 return game.info.killcitizen
423 && uclass_has_flag(unit_class_get(punit), UCF_KILLCITIZEN);
424}
425
426/**********************************************************************/
431{
432 struct city *tgt_city;
433 const struct civ_map *nmap = &(wld.map);
434
435 if ((tgt_city = tile_city(unit_tile(punit)))) {
437 ACTION_JOIN_CITY, tgt_city));
438 } else {
440 ACTION_FOUND_CITY, unit_tile(punit), NULL));
441 }
442}
443
444/**********************************************************************/
448 const struct city *pcity)
449{
450 const struct civ_map *nmap = &(wld.map);
451
452 if (pcity == NULL) {
453 /* Can't change home city to a non existing city. */
454 return FALSE;
455 }
456
457 return action_prob_possible(action_prob_vs_city(nmap, punit, ACTION_HOME_CITY,
458 pcity));
459}
460
461/**********************************************************************/
468
469/**********************************************************************/
477int get_activity_rate(const struct unit *punit)
478{
479 const struct veteran_level *vlevel;
480
481 fc_assert_ret_val(punit != NULL, 0);
482
484 fc_assert_ret_val(vlevel != NULL, 0);
485
486 /* The speed of the settler depends on its base move_rate, not on
487 * the number of moves actually remaining or the adjusted move rate.
488 * This means sea formers won't have their activity rate increased by
489 * Magellan's, and it means injured units work just as fast as
490 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
491 int move_rate = unit_type_get(punit)->move_rate;
492
493 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
494 return ACTIVITY_FACTOR
495 * (float)vlevel->power_fact / 100
496 * move_rate / SINGLE_MOVE;
497}
498
499/**********************************************************************/
506{
507 /* This logic is also coded in client/goto.c. */
508 if (punit->moves_left > 0) {
509 return get_activity_rate(punit);
510 } else {
511 return 0;
512 }
513}
514
515/**********************************************************************/
522 enum unit_activity activity,
523 const struct tile *ptile,
524 struct extra_type *tgt)
525{
526 /* FIXME: This is just an approximation since we don't account for
527 * get_activity_rate_this_turn. */
528 int speed = get_activity_rate(punit);
529 int points_needed = tile_activity_time(activity, ptile, tgt);
530
531 if (points_needed >= 0 && speed > 0) {
532 return (points_needed - 1) / speed + 1; /* round up */
533 } else {
534 return FC_INFINITY;
535 }
536}
537
538/**********************************************************************/
541bool activity_requires_target(enum unit_activity activity)
542{
543 switch (activity) {
544 case ACTIVITY_PILLAGE:
545 case ACTIVITY_BASE:
546 case ACTIVITY_GEN_ROAD:
547 case ACTIVITY_IRRIGATE:
548 case ACTIVITY_MINE:
549 case ACTIVITY_POLLUTION:
550 case ACTIVITY_FALLOUT:
551 return TRUE;
552 case ACTIVITY_IDLE:
553 case ACTIVITY_FORTIFIED:
554 case ACTIVITY_SENTRY:
555 case ACTIVITY_GOTO:
556 case ACTIVITY_EXPLORE:
557 case ACTIVITY_TRANSFORM:
558 case ACTIVITY_CULTIVATE:
559 case ACTIVITY_PLANT:
560 case ACTIVITY_FORTIFYING:
561 case ACTIVITY_CONVERT:
562 return FALSE;
563 /* These shouldn't be kicking around internally. */
564 case ACTIVITY_FORTRESS:
565 case ACTIVITY_AIRBASE:
566 case ACTIVITY_PATROL_UNUSED:
567 default:
569 }
570
571 return FALSE;
572}
573
574/**********************************************************************/
584{
585 return unit_type_get(punit)->adv.worker;
586}
587
588/**********************************************************************/
592{
594 int i = 0;
595
596 for (act = 0; act < ACTIVITY_LAST; act++) {
597 if (is_real_activity(act)) {
598 real_activities[i++] = act;
599 }
600 }
601
602 real_activities[i] = ACTIVITY_LAST;
603}
604
605/**********************************************************************/
609bool is_real_activity(enum unit_activity activity)
610{
611 /* ACTIVITY_FORTRESS, ACTIVITY_AIRBASE, ACTIVITY_OLD_ROAD, and
612 * ACTIVITY_OLD_RAILROAD are deprecated */
613 return (activity < ACTIVITY_LAST)
614 && activity != ACTIVITY_FORTRESS
615 && activity != ACTIVITY_AIRBASE
616 && activity != ACTIVITY_OLD_ROAD
617 && activity != ACTIVITY_OLD_RAILROAD
618 && activity != ACTIVITY_UNKNOWN
619 && activity != ACTIVITY_PATROL_UNUSED;
620}
621
622/**********************************************************************/
625const char *get_activity_text(enum unit_activity activity)
626{
627 /* The switch statement has just the activities listed with no "default"
628 * handling. This enables the compiler to detect missing entries
629 * automatically, and still handles everything correctly. */
630 switch (activity) {
631 case ACTIVITY_IDLE:
632 return _("Idle");
633 case ACTIVITY_POLLUTION:
634 return _("Pollution");
635 case ACTIVITY_MINE:
636 /* TRANS: Activity name, verb in English */
637 return Q_("?act:Mine");
638 case ACTIVITY_PLANT:
639 /* TRANS: Activity name, verb in English */
640 return _("Plant");
641 case ACTIVITY_IRRIGATE:
642 return _("Irrigate");
643 case ACTIVITY_CULTIVATE:
644 return _("Cultivate");
645 case ACTIVITY_FORTIFYING:
646 return _("Fortifying");
647 case ACTIVITY_FORTIFIED:
648 return _("Fortified");
649 case ACTIVITY_SENTRY:
650 return _("Sentry");
651 case ACTIVITY_PILLAGE:
652 return _("Pillage");
653 case ACTIVITY_GOTO:
654 return _("Goto");
655 case ACTIVITY_EXPLORE:
656 return _("Explore");
657 case ACTIVITY_TRANSFORM:
658 return _("Transform");
659 case ACTIVITY_FALLOUT:
660 return _("Fallout");
661 case ACTIVITY_BASE:
662 return _("Base");
663 case ACTIVITY_GEN_ROAD:
664 return _("Road");
665 case ACTIVITY_CONVERT:
666 return _("Convert");
667 case ACTIVITY_OLD_ROAD:
668 case ACTIVITY_OLD_RAILROAD:
669 case ACTIVITY_FORTRESS:
670 case ACTIVITY_AIRBASE:
671 case ACTIVITY_UNKNOWN:
672 case ACTIVITY_PATROL_UNUSED:
673 case ACTIVITY_LAST:
674 break;
675 }
676
678 return _("Unknown");
679}
680
681/**********************************************************************/
687bool could_unit_be_in_transport(const struct unit *pcargo,
688 const struct unit *ptrans)
689{
690 /* Make sure this transporter can carry this type of unit. */
691 if (!can_unit_transport(ptrans, pcargo)) {
692 return FALSE;
693 }
694
695 /* Make sure there's room in the transporter. */
696 if (get_transporter_occupancy(ptrans)
697 >= get_transporter_capacity(ptrans)) {
698 return FALSE;
699 }
700
701 /* Check iff this is a valid transport. */
702 if (!unit_transport_check(pcargo, ptrans)) {
703 return FALSE;
704 }
705
706 /* Check transport depth. */
708 < 1 + unit_transport_depth(ptrans) + unit_cargo_depth(pcargo)) {
709 return FALSE;
710 }
711
712 return TRUE;
713}
714
715/**********************************************************************/
719bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
720{
721 if (!pcargo || !ptrans || pcargo == ptrans) {
722 return FALSE;
723 }
724
725 /* Double-check ownership of the units: you can load into an allied unit
726 * (of course only allied units can be on the same tile). */
727 if (!pplayers_allied(unit_owner(pcargo), unit_owner(ptrans))) {
728 return FALSE;
729 }
730
731 /* Un-embarkable transport must be in city or base to load cargo. */
733 && !tile_city(unit_tile(ptrans))
734 && !tile_has_native_base(unit_tile(ptrans), unit_type_get(ptrans))) {
735 return FALSE;
736 }
737
738 return could_unit_be_in_transport(pcargo, ptrans);
739}
740
741/**********************************************************************/
744bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
745{
746 /* This function needs to check EVERYTHING. */
747
748 /* Check positions of the units. Of course you can't load a unit onto
749 * a transporter on a different tile... */
750 if (!same_pos(unit_tile(pcargo), unit_tile(ptrans))) {
751 return FALSE;
752 }
753
754 /* Cannot load if cargo is already loaded onto something else. */
755 if (unit_transported(pcargo)) {
756 return FALSE;
757 }
758
759 return could_unit_load(pcargo, ptrans);
760}
761
762/**********************************************************************/
771bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
772{
773 if (!pcargo || !ptrans) {
774 return FALSE;
775 }
776
777 /* Make sure the unit's transporter exists and is known. */
778 if (unit_transport_get(pcargo) != ptrans) {
779 return FALSE;
780 }
781
782 /* Un-disembarkable transport must be in city or base to unload cargo. */
784 && !tile_city(unit_tile(ptrans))
785 && !tile_has_native_base(unit_tile(ptrans), unit_type_get(ptrans))) {
786 return FALSE;
787 }
788
789 return TRUE;
790}
791
792/**********************************************************************/
796bool can_unit_alight_or_be_unloaded(const struct unit *pcargo,
797 const struct unit *ptrans)
798{
799 const struct civ_map *nmap = &(wld.map);
800
801 if (!pcargo || !ptrans) {
802 return FALSE;
803 }
804
805 fc_assert_ret_val(unit_transport_get(pcargo) == ptrans, FALSE);
806
807 if (is_server()) {
808 return (is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_ALIGHT,
809 pcargo, ptrans)
810 || is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_UNLOAD,
811 ptrans, pcargo));
812 } else {
813 return (action_prob_possible(
814 action_prob_vs_unit(nmap, pcargo, ACTION_TRANSPORT_ALIGHT, ptrans))
816 action_prob_vs_unit(nmap, ptrans, ACTION_TRANSPORT_UNLOAD,
817 pcargo)));
818 }
819}
820
821
822/**********************************************************************/
827bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
828{
829 action_by_result_iterate(paction, ACTRES_PARADROP) {
830 if (action_maybe_possible_actor_unit(nmap, paction->id, punit)) {
831 return TRUE;
832 }
834 action_by_result_iterate(paction, ACTRES_PARADROP_CONQUER) {
835 if (action_maybe_possible_actor_unit(nmap, paction->id, punit)) {
836 return TRUE;
837 }
839
840 return FALSE;
841}
842
843/**********************************************************************/
847 struct unit *punit)
848{
849 enum unit_activity current = punit->activity;
850 struct extra_type *target = punit->activity_target;
851 enum unit_activity current2 =
852 (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
853 bool result;
854
855 punit->activity = ACTIVITY_IDLE;
856 punit->activity_target = NULL;
857
858 result = can_unit_do_activity_targeted(nmap, punit, current2, target);
859
860 punit->activity = current;
861 punit->activity_target = target;
862
863 return result;
864}
865
866/**********************************************************************/
873bool can_unit_do_activity(const struct civ_map *nmap,
874 const struct unit *punit,
875 enum unit_activity activity)
876{
877 struct extra_type *target = NULL;
878
879 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
880 * being able to find out whether an activity is in general possible.
881 * Find one for them, but when they come to do the activity, they will
882 * have to determine the target themselves */
883 {
884 struct tile *ptile = unit_tile(punit);
885
886 if (activity == ACTIVITY_IRRIGATE) {
887 target = next_extra_for_tile(ptile,
888 EC_IRRIGATION,
890 punit);
891 if (NULL == target) {
892 return FALSE; /* No more irrigation extras available. */
893 }
894 } else if (activity == ACTIVITY_MINE) {
895 target = next_extra_for_tile(ptile,
896 EC_MINE,
898 punit);
899 if (NULL == target) {
900 return FALSE; /* No more mine extras available. */
901 }
902 }
903 }
904
905 return can_unit_do_activity_targeted(nmap, punit, activity, target);
906}
907
908/**********************************************************************/
913 const struct unit *punit,
914 enum unit_activity activity,
915 struct extra_type *target)
916{
917 return can_unit_do_activity_targeted_at(nmap, punit, activity, target,
919}
920
921/**********************************************************************/
926 const struct unit *punit,
927 enum unit_activity activity,
928 struct extra_type *target,
929 const struct tile *ptile)
930{
931 /* Check that no build activity conflicting with one already in progress
932 * gets executed. */
933 /* FIXME: Should check also the cases where one of the activities is terrain
934 * change that destroys the target of the other activity */
935 if (target != NULL && is_build_activity(activity)) {
936 if (tile_is_placing(ptile)) {
937 return FALSE;
938 }
939
940 unit_list_iterate(ptile->units, tunit) {
941 if (is_build_activity(tunit->activity)
942 && !can_extras_coexist(target, tunit->activity_target)) {
943 return FALSE;
944 }
946 }
947
948#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
949{ \
950 switch (action_get_target_kind(paction)) { \
951 case ATK_TILE: \
952 return is_action_enabled_unit_on_tile(nmap, paction->id, \
953 punit, ptile, target); \
954 case ATK_EXTRAS: \
955 return is_action_enabled_unit_on_extras(nmap, paction->id, \
956 punit, ptile, target); \
957 case ATK_CITY: \
958 case ATK_UNIT: \
959 case ATK_UNITS: \
960 case ATK_SELF: \
961 return FALSE; \
962 case ATK_COUNT: \
963 break; /* Handle outside switch */ \
964 } \
965 fc_assert(action_target_kind_is_valid( \
966 action_get_target_kind(paction))); \
967 return FALSE; \
968 }
969
970 switch (activity) {
971 case ACTIVITY_IDLE:
972 case ACTIVITY_GOTO:
973 return TRUE;
974
975 case ACTIVITY_POLLUTION:
976 /* The call below doesn't support actor tile speculation. */
977 fc_assert_msg(unit_tile(punit) == ptile,
978 "Please use action_speculate_unit_on_tile()");
979 return is_action_enabled_unit_on_tile(nmap, ACTION_CLEAN_POLLUTION,
980 punit, ptile, target);
981
982 case ACTIVITY_FALLOUT:
983 /* The call below doesn't support actor tile speculation. */
984 fc_assert_msg(unit_tile(punit) == ptile,
985 "Please use action_speculate_unit_on_tile()");
986 return is_action_enabled_unit_on_tile(nmap, ACTION_CLEAN_FALLOUT,
987 punit, ptile, target);
988
989 case ACTIVITY_MINE:
990 /* The call below doesn't support actor tile speculation. */
991 fc_assert_msg(unit_tile(punit) == ptile,
992 "Please use action_speculate_unit_on_tile()");
993 return is_action_enabled_unit_on_tile(nmap, ACTION_MINE, punit,
994 ptile, target);
995
996 case ACTIVITY_PLANT:
997 /* The call below doesn't support actor tile speculation. */
998 fc_assert_msg(unit_tile(punit) == ptile,
999 "Please use action_speculate_unit_on_tile()");
1000 return is_action_enabled_unit_on_tile(nmap, ACTION_PLANT,
1001 punit, ptile, NULL);
1002
1003 case ACTIVITY_IRRIGATE:
1004 /* The call below doesn't support actor tile speculation. */
1005 fc_assert_msg(unit_tile(punit) == ptile,
1006 "Please use action_speculate_unit_on_tile()");
1007 return is_action_enabled_unit_on_tile(nmap, ACTION_IRRIGATE, punit,
1008 ptile, target);
1009
1010 case ACTIVITY_CULTIVATE:
1011 /* The call below doesn't support actor tile speculation. */
1012 fc_assert_msg(unit_tile(punit) == ptile,
1013 "Please use action_speculate_unit_on_tile()");
1014 return is_action_enabled_unit_on_tile(nmap, ACTION_CULTIVATE,
1015 punit, ptile, NULL);
1016
1017 case ACTIVITY_FORTIFYING:
1018 /* The call below doesn't support actor tile speculation. */
1019 fc_assert_msg(unit_tile(punit) == ptile,
1020 "Please use action_speculate_unit_on_self()");
1021 return is_action_enabled_unit_on_self(nmap, ACTION_FORTIFY,
1022 punit);
1023
1024 case ACTIVITY_FORTIFIED:
1025 return FALSE;
1026
1027 case ACTIVITY_BASE:
1028 /* The call below doesn't support actor tile speculation. */
1029 fc_assert_msg(unit_tile(punit) == ptile,
1030 "Please use action_speculate_unit_on_tile()");
1031 return is_action_enabled_unit_on_tile(nmap, ACTION_BASE,
1032 punit, ptile, target);
1033
1034 case ACTIVITY_GEN_ROAD:
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()");
1038 return is_action_enabled_unit_on_tile(nmap, ACTION_ROAD,
1039 punit, ptile, target);
1040
1041 case ACTIVITY_SENTRY:
1043 && !unit_transported(punit)) {
1044 /* Don't let units sentry on tiles they will die on. */
1045 return FALSE;
1046 }
1047 return TRUE;
1048
1049 case ACTIVITY_PILLAGE:
1050 /* The call below doesn't support actor tile speculation. */
1051 fc_assert_msg(unit_tile(punit) == ptile,
1052 "Please use action_speculate_unit_on_tile()");
1054
1055 case ACTIVITY_EXPLORE:
1056 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1057
1058 case ACTIVITY_TRANSFORM:
1059 /* The call below doesn't support actor tile speculation. */
1060 fc_assert_msg(unit_tile(punit) == ptile,
1061 "Please use action_speculate_unit_on_tile()");
1062 return is_action_enabled_unit_on_tile(nmap, ACTION_TRANSFORM_TERRAIN,
1063 punit, ptile, NULL);
1064
1065 case ACTIVITY_CONVERT:
1066 /* The call below doesn't support actor tile speculation. */
1067 fc_assert_msg(unit_tile(punit) == ptile,
1068 "Please use action_speculate_unit_on_self()");
1069 return is_action_enabled_unit_on_self(nmap, ACTION_CONVERT, punit);
1070
1071 case ACTIVITY_OLD_ROAD:
1072 case ACTIVITY_OLD_RAILROAD:
1073 case ACTIVITY_FORTRESS:
1074 case ACTIVITY_AIRBASE:
1075 case ACTIVITY_PATROL_UNUSED:
1076 case ACTIVITY_LAST:
1077 case ACTIVITY_UNKNOWN:
1078 break;
1079 }
1080 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1081 activity);
1082 return FALSE;
1083
1084#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1085}
1086
1087/**********************************************************************/
1091 enum unit_activity new_activity)
1092{
1093 fc_assert_ret(new_activity != ACTIVITY_FORTRESS
1094 && new_activity != ACTIVITY_AIRBASE);
1095
1096 punit->activity = new_activity;
1097 punit->activity_count = 0;
1098 punit->activity_target = NULL;
1099 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1100 /* No longer done. */
1102 }
1103}
1104
1105/**********************************************************************/
1108void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
1109{
1111
1112 if (new_activity == ACTIVITY_FORTIFYING
1113 && punit->changed_from == ACTIVITY_FORTIFIED) {
1114 new_activity = ACTIVITY_FORTIFIED;
1115 }
1116 set_unit_activity_internal(punit, new_activity);
1117 if (new_activity == punit->changed_from) {
1119 }
1120}
1121
1122/**********************************************************************/
1126 enum unit_activity new_activity,
1127 struct extra_type *new_target)
1128{
1130 || new_target == NULL);
1131
1132 set_unit_activity_internal(punit, new_activity);
1133 punit->activity_target = new_target;
1134 if (new_activity == punit->changed_from
1135 && new_target == punit->changed_from_target) {
1137 }
1138}
1139
1140/**********************************************************************/
1143bool is_unit_activity_on_tile(enum unit_activity activity,
1144 const struct tile *ptile)
1145{
1146 unit_list_iterate(ptile->units, punit) {
1147 if (punit->activity == activity) {
1148 return TRUE;
1149 }
1151 return FALSE;
1152}
1153
1154/**********************************************************************/
1158bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
1159{
1160 bv_extras tgt_ret;
1161
1162 BV_CLR_ALL(tgt_ret);
1163 unit_list_iterate(ptile->units, punit) {
1164 if (punit->activity == ACTIVITY_PILLAGE) {
1166 }
1168
1169 return tgt_ret;
1170}
1171
1172/**********************************************************************/
1175void unit_activity_astr(const struct unit *punit, struct astring *astr)
1176{
1177 if (!punit || !astr) {
1178 return;
1179 }
1180
1181 switch (punit->activity) {
1182 case ACTIVITY_IDLE:
1184 int rate, f;
1185
1186 rate = unit_type_get(punit)->move_rate;
1187 f = ((punit->fuel) - 1);
1188
1189 /* Add in two parts as move_points_text() returns ptr to static
1190 * End result: "Moves: (fuel)moves_left" */
1191 astr_add_line(astr, "%s: (%s)", _("Moves"),
1192 move_points_text((rate * f) + punit->moves_left, FALSE));
1193 astr_add(astr, "%s",
1195 } else {
1196 astr_add_line(astr, "%s: %s", _("Moves"),
1198 }
1199 return;
1200 case ACTIVITY_POLLUTION:
1201 case ACTIVITY_FALLOUT:
1202 case ACTIVITY_OLD_ROAD:
1203 case ACTIVITY_OLD_RAILROAD:
1204 case ACTIVITY_TRANSFORM:
1205 case ACTIVITY_FORTIFYING:
1206 case ACTIVITY_FORTIFIED:
1207 case ACTIVITY_AIRBASE:
1208 case ACTIVITY_FORTRESS:
1209 case ACTIVITY_SENTRY:
1210 case ACTIVITY_GOTO:
1211 case ACTIVITY_EXPLORE:
1212 case ACTIVITY_CONVERT:
1213 case ACTIVITY_CULTIVATE:
1214 case ACTIVITY_PLANT:
1216 return;
1217 case ACTIVITY_MINE:
1218 case ACTIVITY_IRRIGATE:
1219 if (punit->activity_target == NULL) {
1221 } else {
1222 astr_add_line(astr, "Building %s",
1224 }
1225 return;
1226 case ACTIVITY_PILLAGE:
1227 if (punit->activity_target != NULL) {
1230 } else {
1232 }
1233 return;
1234 case ACTIVITY_BASE:
1237 return;
1238 case ACTIVITY_GEN_ROAD:
1241 return;
1242 case ACTIVITY_UNKNOWN:
1243 case ACTIVITY_PATROL_UNUSED:
1244 case ACTIVITY_LAST:
1245 break;
1246 }
1247
1248 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1249 punit->activity, unit_rule_name(punit), punit->id, __FUNCTION__);
1250}
1251
1252/**********************************************************************/
1258void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1259{
1260 if (!punit || !astr) {
1261 return;
1262 }
1263
1264 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1266 punit->upkeep[O_GOLD]);
1267}
1268
1269/**********************************************************************/
1272struct player *unit_nationality(const struct unit *punit)
1273{
1274 fc_assert_ret_val(NULL != punit, NULL);
1275 return punit->nationality;
1276}
1277
1278/**********************************************************************/
1282void unit_tile_set(struct unit *punit, struct tile *ptile)
1283{
1284 fc_assert_ret(NULL != punit);
1285 punit->tile = ptile;
1286}
1287
1288/**********************************************************************/
1295struct unit *tile_allied_unit(const struct tile *ptile,
1296 const struct player *pplayer)
1297{
1298 struct unit *punit = NULL;
1299
1300 unit_list_iterate(ptile->units, cunit) {
1301 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1302 punit = cunit;
1303 } else {
1304 return NULL;
1305 }
1306 }
1308
1309 return punit;
1310}
1311
1312/**********************************************************************/
1318struct unit *tile_enemy_unit(const struct tile *ptile,
1319 const struct player *pplayer)
1320{
1321 unit_list_iterate(ptile->units, punit) {
1322 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1323 return punit;
1324 }
1326
1327 return NULL;
1328}
1329
1330/**********************************************************************/
1333struct unit *tile_non_allied_unit(const struct tile *ptile,
1334 const struct player *pplayer)
1335{
1336 unit_list_iterate(ptile->units, punit) {
1337 if (!pplayers_allied(unit_owner(punit), pplayer)) {
1338 return punit;
1339 }
1340 }
1342
1343 return NULL;
1344}
1345
1346/**********************************************************************/
1350struct unit *tile_other_players_unit(const struct tile *ptile,
1351 const struct player *pplayer)
1352{
1353 unit_list_iterate(ptile->units, punit) {
1354 if (unit_owner(punit) != pplayer) {
1355 return punit;
1356 }
1358
1359 return NULL;
1360}
1361
1362/**********************************************************************/
1366struct unit *tile_non_attack_unit(const struct tile *ptile,
1367 const struct player *pplayer)
1368{
1369 unit_list_iterate(ptile->units, punit) {
1370 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1371 return punit;
1372 }
1373 }
1375
1376 return NULL;
1377}
1378
1379/**********************************************************************/
1387struct unit *unit_occupies_tile(const struct tile *ptile,
1388 const struct player *pplayer)
1389{
1390 unit_list_iterate(ptile->units, punit) {
1391 if (!is_military_unit(punit)) {
1392 continue;
1393 }
1394
1395 if (uclass_has_flag(unit_class_get(punit), UCF_DOESNT_OCCUPY_TILE)) {
1396 continue;
1397 }
1398
1399 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1400 return punit;
1401 }
1403
1404 return NULL;
1405}
1406
1407/**********************************************************************/
1414bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1415 const struct civ_map *zmap)
1416{
1417 square_iterate(zmap, ptile0, 1, ptile) {
1418 struct terrain *pterrain;
1419 struct city *pcity;
1420
1421 pterrain = tile_terrain(ptile);
1422 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1423 continue;
1424 }
1425
1426 pcity = tile_non_allied_city(ptile, pplayer);
1427 if (pcity != NULL) {
1428 if (unit_list_size(ptile->units) > 0) {
1429 /* Occupied enemy city, it doesn't matter if units inside have
1430 * UTYF_NOZOC or not. */
1431 return FALSE;
1432 }
1433 } else {
1434 unit_list_iterate(ptile->units, punit) {
1435 if (!pplayers_allied(unit_owner(punit), pplayer)
1436 && !unit_has_type_flag(punit, UTYF_NOZOC)
1438 bool hidden = FALSE;
1439
1440 /* We do NOT check the possibility that player is allied with an extra owner,
1441 * and should thus see inside the extra.
1442 * This is to avoid the situation where having an alliance with third player
1443 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1445 if (tile_has_extra(ptile, pextra)) {
1446 hidden = TRUE;
1447 break;
1448 }
1450
1451 if (!hidden) {
1452 return FALSE;
1453 }
1454 }
1456 }
1458
1459 return TRUE;
1460}
1461
1462/**********************************************************************/
1473bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1474 const struct civ_map *zmap)
1475{
1476 square_iterate(zmap, ptile0, 1, ptile) {
1477 struct terrain *pterrain;
1478 struct city *pcity;
1479
1480 pterrain = tile_terrain(ptile);
1481 if (T_UNKNOWN == pterrain
1482 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1483 continue;
1484 }
1485
1486 pcity = tile_non_allied_city(ptile, pplayer);
1487 if (pcity != NULL) {
1488 if (pcity->client.occupied
1489 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1490 /* Occupied enemy city, it doesn't matter if units inside have
1491 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1492 return FALSE;
1493 }
1494 } else {
1495 unit_list_iterate(ptile->units, punit) {
1497 && !pplayers_allied(unit_owner(punit), pplayer)
1498 && !unit_has_type_flag(punit, UTYF_NOZOC)) {
1499 return FALSE;
1500 }
1502 }
1504
1505 return TRUE;
1506}
1507
1508/**********************************************************************/
1511bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
1512{
1513 return (!uclass_has_flag(utype_class(punittype), UCF_ZOC)
1514 || utype_has_flag(punittype, UTYF_IGZOC));
1515}
1516
1517/**********************************************************************/
1526{
1527 const struct civ_map *nmap = &(wld.map);
1528
1529 if (!is_attack_unit(punit)) {
1530 return FALSE;
1531 }
1532 if (tile_city(unit_tile(punit))) {
1533 return FALSE;
1534 }
1536 switch (game.info.happyborders) {
1537 case HB_DISABLED:
1538 break;
1539 case HB_NATIONAL:
1541 return FALSE;
1542 }
1543 break;
1544 case HB_ALLIANCE:
1546 return FALSE;
1547 }
1548 break;
1549 }
1550 }
1552 unit_type_get(punit))) {
1553 return !is_unit_near_a_friendly_city(nmap, punit);
1554 }
1555
1556 return TRUE;
1557}
1558
1559/**********************************************************************/
1562bool is_build_activity(enum unit_activity activity)
1563{
1564 switch (activity) {
1565 case ACTIVITY_MINE:
1566 case ACTIVITY_IRRIGATE:
1567 case ACTIVITY_BASE:
1568 case ACTIVITY_GEN_ROAD:
1569 return TRUE;
1570 default:
1571 return FALSE;
1572 }
1573}
1574
1575/**********************************************************************/
1578bool is_clean_activity(enum unit_activity activity)
1579{
1580 switch (activity) {
1581 case ACTIVITY_PILLAGE:
1582 case ACTIVITY_POLLUTION:
1583 case ACTIVITY_FALLOUT:
1584 return TRUE;
1585 default:
1586 return FALSE;
1587 }
1588}
1589
1590/**********************************************************************/
1593bool is_terrain_change_activity(enum unit_activity activity)
1594{
1595 switch (activity) {
1596 case ACTIVITY_CULTIVATE:
1597 case ACTIVITY_PLANT:
1598 case ACTIVITY_TRANSFORM:
1599 return TRUE;
1600 default:
1601 return FALSE;
1602 }
1603}
1604
1605/**********************************************************************/
1608bool is_tile_activity(enum unit_activity activity)
1609{
1610 return is_build_activity(activity)
1611 || is_clean_activity(activity)
1612 || is_terrain_change_activity(activity);
1613}
1614
1615/**********************************************************************/
1619struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1620 const struct unit_type *punittype,
1621 int veteran_level)
1622{
1623 /* Make sure that contents of unit structure are correctly initialized,
1624 * if you ever allocate it by some other mean than fc_calloc() */
1625 struct unit *punit = fc_calloc(1, sizeof(*punit));
1626 int max_vet_lvl;
1627
1628 /* It does not register the unit so the id is set to 0. */
1630
1631 fc_assert_ret_val(NULL != punittype, NULL); /* No untyped units! */
1632 punit->utype = punittype;
1633
1634 fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned units! */
1635 punit->owner = pplayer;
1636 punit->nationality = pplayer;
1637
1638 punit->refcount = 1;
1640
1641 if (pcity) {
1642 unit_tile_set(punit, pcity->tile);
1643 punit->homecity = pcity->id;
1644 } else {
1645 unit_tile_set(punit, NULL);
1647 }
1648
1649 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1650 punit->goto_tile = NULL;
1651 max_vet_lvl = utype_veteran_levels(punittype) - 1;
1652 punit->veteran = MIN(veteran_level, max_vet_lvl);
1653 /* A unit new and fresh ... */
1657 punit->moved = FALSE;
1658
1659 punit->ssa_controller = SSA_NONE;
1662
1663 punit->transporter = NULL;
1664 punit->transporting = unit_list_new();
1665
1666 punit->carrying = NULL;
1667
1668 set_unit_activity(punit, ACTIVITY_IDLE);
1671
1672 punit->action_decision_want = ACT_DEC_NOTHING;
1674
1675 punit->stay = FALSE;
1676
1677 if (is_server()) {
1680
1682
1684
1685 memset(punit->server.upkeep_paid, 0,
1686 O_LAST * sizeof(*punit->server.upkeep_paid));
1687
1688 punit->server.ord_map = 0;
1689 punit->server.ord_city = 0;
1690
1691 punit->server.vision = NULL; /* No vision. */
1693 /* Must be an invalid turn number, and an invalid previous turn
1694 * number. */
1695 punit->server.action_turn = -2;
1696 /* punit->server.moving = NULL; set by fc_calloc(). */
1697
1698 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1699
1700 CALL_FUNC_EACH_AI(unit_alloc, punit);
1701 } else {
1705 punit->client.act_prob_cache = NULL;
1706 }
1707
1708 return punit;
1709}
1710
1711/**********************************************************************/
1716{
1718
1719 /* Unload unit if transported. */
1722
1723 /* Check for transported units. Use direct access to the list. */
1724 if (unit_list_size(punit->transporting) != 0) {
1725 /* Unload all units. */
1727 unit_transport_unload(pcargo);
1729 }
1730 fc_assert(unit_list_size(punit->transporting) == 0);
1731
1732 if (punit->transporting) {
1733 unit_list_destroy(punit->transporting);
1734 }
1735
1736 CALL_FUNC_EACH_AI(unit_free, punit);
1737
1738 if (is_server() && punit->server.adv) {
1740 } else {
1743 }
1744 }
1745
1746 if (--punit->refcount <= 0) {
1747 FC_FREE(punit);
1748 }
1749}
1750
1751/**********************************************************************/
1756{
1757 if (punit->has_orders) {
1758 punit->goto_tile = NULL;
1759 free(punit->orders.list);
1760 punit->orders.list = NULL;
1761 }
1762 punit->orders.length = 0;
1764}
1765
1766/**********************************************************************/
1769int get_transporter_occupancy(const struct unit *ptrans)
1770{
1771 fc_assert_ret_val(ptrans, -1);
1772
1773 return unit_list_size(ptrans->transporting);
1774}
1775
1776/**********************************************************************/
1779static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1780 const struct tile *ptile,
1781 bool (*unit_load_test)
1782 (const struct unit *pc,
1783 const struct unit *pt))
1784{
1785 struct unit *best_trans = NULL;
1786 struct {
1787 bool has_orders, is_idle, can_freely_unload;
1788 int depth, outermost_moves_left, total_moves;
1789 } cur, best = { FALSE };
1790
1791 unit_list_iterate(ptile->units, ptrans) {
1792 if (!unit_load_test(pcargo, ptrans)) {
1793 continue;
1794 } else if (best_trans == NULL) {
1795 best_trans = ptrans;
1796 }
1797
1798 /* Gather data from transport stack in a single pass, for use in
1799 * various conditions below. */
1800 cur.has_orders = unit_has_orders(ptrans);
1801 cur.outermost_moves_left = ptrans->moves_left;
1802 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1803 unit_transports_iterate(ptrans, ptranstrans) {
1804 if (unit_has_orders(ptranstrans)) {
1805 cur.has_orders = TRUE;
1806 }
1807 cur.outermost_moves_left = ptranstrans->moves_left;
1808 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1810
1811 /* Criteria for deciding the 'best' transport to load onto.
1812 * The following tests are applied in order; earlier ones have
1813 * lexicographically greater significance than later ones. */
1814
1815 /* Transports which have orders, or are on transports with orders,
1816 * are less preferable to transport stacks without orders (to
1817 * avoid loading on units that are just passing through). */
1818 if (best_trans != ptrans) {
1819 if (!cur.has_orders && best.has_orders) {
1820 best_trans = ptrans;
1821 } else if (cur.has_orders && !best.has_orders) {
1822 continue;
1823 }
1824 }
1825
1826 /* Else, transports which are idle are preferable (giving players
1827 * some control over loading) -- this does not check transports
1828 * of transports. */
1829 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1830 if (best_trans != ptrans) {
1831 if (cur.is_idle && !best.is_idle) {
1832 best_trans = ptrans;
1833 } else if (!cur.is_idle && best.is_idle) {
1834 continue;
1835 }
1836 }
1837
1838 /* Else, transports from which the cargo could unload at any time
1839 * are preferable to those where the cargo can only disembark in
1840 * cities/bases. */
1841 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1842 unit_type_get(ptrans));
1843 if (best_trans != ptrans) {
1844 if (cur.can_freely_unload && !best.can_freely_unload) {
1845 best_trans = ptrans;
1846 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1847 continue;
1848 }
1849 }
1850
1851 /* Else, transports which are less deeply nested are preferable. */
1852 cur.depth = unit_transport_depth(ptrans);
1853 if (best_trans != ptrans) {
1854 if (cur.depth < best.depth) {
1855 best_trans = ptrans;
1856 } else if (cur.depth > best.depth) {
1857 continue;
1858 }
1859 }
1860
1861 /* Else, transport stacks where the outermost transport has more
1862 * moves left are preferable (on the assumption that it's the
1863 * outermost transport that's about to move). */
1864 if (best_trans != ptrans) {
1865 if (cur.outermost_moves_left > best.outermost_moves_left) {
1866 best_trans = ptrans;
1867 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1868 continue;
1869 }
1870 }
1871
1872 /* All other things being equal, as a tie-breaker, compare the total
1873 * moves left (this turn) and move rate (future turns) for the whole
1874 * stack, to take into account total potential movement for both
1875 * short and long journeys (we don't know which the cargo intends to
1876 * make). Doesn't try to account for whether transports can unload,
1877 * etc. */
1878 if (best_trans != ptrans) {
1879 if (cur.total_moves > best.total_moves) {
1880 best_trans = ptrans;
1881 } else {
1882 continue;
1883 }
1884 }
1885
1886 fc_assert(best_trans == ptrans);
1887 best = cur;
1889
1890 return best_trans;
1891}
1892
1893/**********************************************************************/
1898struct unit *transporter_for_unit(const struct unit *pcargo)
1899{
1900 return base_transporter_for_unit(pcargo, unit_tile(pcargo), can_unit_load);
1901}
1902
1903/**********************************************************************/
1908struct unit *transporter_for_unit_at(const struct unit *pcargo,
1909 const struct tile *ptile)
1910{
1911 return base_transporter_for_unit(pcargo, ptile, could_unit_load);
1912}
1913
1914/**********************************************************************/
1919 const struct unit *punit)
1920{
1922 return FALSE;
1923 }
1924
1927 return FALSE;
1928 }
1930
1931 return TRUE;
1932}
1933
1934/**********************************************************************/
1943 const struct unit *punit,
1944 const struct unit_type *to_unittype)
1945{
1946 fc_assert_ret_val(NULL != to_unittype, UU_NO_UNITTYPE);
1947
1948 if (!can_type_transport_units_cargo(to_unittype, punit)) {
1949 return UU_NOT_ENOUGH_ROOM;
1950 }
1951
1952 if (punit->transporter != NULL) {
1954 utype_class(to_unittype))) {
1956 }
1957 } else if (!can_exist_at_tile(nmap, to_unittype, unit_tile(punit))) {
1958 /* The new unit type can't survive on this terrain. */
1959 return UU_NOT_TERRAIN;
1960 }
1961
1962 return UU_OK;
1963}
1964
1965/**********************************************************************/
1975 const struct unit *punit,
1976 bool is_free)
1977{
1978 struct player *pplayer = unit_owner(punit);
1979 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
1981 struct city *pcity;
1982 int cost;
1983
1984 if (!to_unittype) {
1985 return UU_NO_UNITTYPE;
1986 }
1987
1988 if (punit->activity == ACTIVITY_CONVERT) {
1989 /* TODO: There may be other activities that the upgraded unit is not
1990 allowed to do, which we could also test.
1991 -
1992 If convert were legal for new unit_type we could allow, but then it
1993 has a 'head start' getting activity time from the old conversion. */
1994 return UU_NOT_ACTIVITY;
1995 }
1996
1997 if (!is_free) {
1998 cost = unit_upgrade_price(pplayer, unit_type_get(punit), to_unittype);
1999 if (pplayer->economic.gold < cost) {
2000 return UU_NO_MONEY;
2001 }
2002
2003 pcity = tile_city(unit_tile(punit));
2004 if (!pcity) {
2005 return UU_NOT_IN_CITY;
2006 }
2007 if (city_owner(pcity) != pplayer) {
2008 /* TODO: Should upgrades in allied cities be possible? */
2009 return UU_NOT_CITY_OWNER;
2010 }
2011 }
2012
2013 /* TODO: Allow transported units to be reassigned. Check here
2014 * and make changes to upgrade_unit. */
2015 return unit_transform_result(nmap, punit, to_unittype);
2016}
2017
2018/**********************************************************************/
2021bool unit_can_convert(const struct civ_map *nmap,
2022 const struct unit *punit)
2023{
2024 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2025
2026 if (tgt == NULL) {
2027 return FALSE;
2028 }
2029
2030 return UU_OK == unit_transform_result(nmap, punit, tgt);
2031}
2032
2033/**********************************************************************/
2038 const struct unit *punit,
2039 char *buf, size_t bufsz)
2040{
2041 struct player *pplayer = unit_owner(punit);
2042 enum unit_upgrade_result result = unit_upgrade_test(nmap, punit, FALSE);
2043 int upgrade_cost;
2044 const struct unit_type *from_unittype = unit_type_get(punit);
2045 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2047 char tbuf[MAX_LEN_MSG];
2048
2049 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2050 "Treasury contains %d gold.",
2051 pplayer->economic.gold),
2052 pplayer->economic.gold);
2053
2054 switch (result) {
2055 case UU_OK:
2056 upgrade_cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
2057 /* This message is targeted toward the GUI callers. */
2058 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2059 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2060 "Upgrade %s to %s for %d gold?\n%s",
2061 upgrade_cost),
2062 utype_name_translation(from_unittype),
2063 utype_name_translation(to_unittype),
2064 upgrade_cost, tbuf);
2065 break;
2066 case UU_NO_UNITTYPE:
2067 fc_snprintf(buf, bufsz,
2068 _("Sorry, cannot upgrade %s (yet)."),
2069 utype_name_translation(from_unittype));
2070 break;
2071 case UU_NO_MONEY:
2072 upgrade_cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
2073 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2074 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2075 "Upgrading %s to %s costs %d gold.\n%s",
2076 upgrade_cost),
2077 utype_name_translation(from_unittype),
2078 utype_name_translation(to_unittype),
2079 upgrade_cost, tbuf);
2080 break;
2081 case UU_NOT_IN_CITY:
2082 case UU_NOT_CITY_OWNER:
2083 fc_snprintf(buf, bufsz,
2084 _("You can only upgrade units in your cities."));
2085 break;
2086 case UU_NOT_ENOUGH_ROOM:
2087 fc_snprintf(buf, bufsz,
2088 _("Upgrading this %s would strand units it transports."),
2089 utype_name_translation(from_unittype));
2090 break;
2091 case UU_NOT_TERRAIN:
2092 fc_snprintf(buf, bufsz,
2093 _("Upgrading this %s would result in a %s which can not "
2094 "survive at this place."),
2095 utype_name_translation(from_unittype),
2096 utype_name_translation(to_unittype));
2097 break;
2099 fc_snprintf(buf, bufsz,
2100 _("Upgrading this %s would result in a %s which its "
2101 "current transport, %s, could not transport."),
2102 utype_name_translation(from_unittype),
2103 utype_name_translation(to_unittype),
2105 break;
2106 case UU_NOT_ACTIVITY:
2107 fc_snprintf(buf, bufsz,
2108 _("Cannot upgrade %s while doing '%s'."),
2109 utype_name_translation(from_unittype),
2110 unit_activity_name(punit->activity));
2111 break;
2112 }
2113
2114 return result;
2115}
2116
2117/**********************************************************************/
2121int unit_pays_mp_for_action(const struct action *paction,
2122 const struct unit *punit)
2123{
2124 int mpco;
2125
2126 mpco = get_target_bonus_effects(NULL,
2127 &(const struct req_context) {
2128 .player = unit_owner(punit),
2129 .city = unit_tile(punit)
2131 : NULL,
2132 .tile = unit_tile(punit),
2133 .unit = punit,
2134 .unittype = unit_type_get(punit),
2135 .action = paction,
2136 },
2137 NULL,
2138 EFT_ACTION_SUCCESS_MOVE_COST);
2139
2141
2142 return mpco;
2143}
2144
2145/**********************************************************************/
2148int hp_gain_coord(const struct unit *punit)
2149{
2150 int hp = 0;
2151 const int base = unit_type_get(punit)->hp;
2152 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2153
2154 /* Includes barracks (100%), fortress (25%), etc. */
2155 hp += base * get_unit_bonus(punit, EFT_HP_REGEN) / 100;
2156
2157 /* Minimum HP after regen effects applied. */
2158 hp = MAX(hp, min);
2159
2160 /* Regen2 effects that apply after there's at least Min HP */
2161 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2162
2163 return MAX(hp, 0);
2164}
2165
2166/**********************************************************************/
2170{
2171 const struct unit_type *utype = unit_type_get(punit);
2172 struct unit_class *pclass = unit_class_get(punit);
2173 struct city *pcity = tile_city(unit_tile(punit));
2174 int gain;
2175
2176 if (!punit->moved) {
2177 gain = hp_gain_coord(punit);
2178 } else {
2179 gain = 0;
2180 }
2181
2182 gain += get_unit_bonus(punit, EFT_UNIT_RECOVER);
2183
2184 if (!punit->homecity && 0 < game.server.killunhomed
2185 && !unit_has_type_flag(punit, UTYF_GAMELOSS)) {
2186 /* Hit point loss of units without homecity; at least 1 hp! */
2187 /* Gameloss units are immune to this effect. */
2188 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2189
2190 if (gain > hp_loss) {
2191 gain = -1;
2192 } else {
2193 gain -= hp_loss;
2194 }
2195 }
2196
2197 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2198 && !unit_transported(punit)) {
2199 gain -= utype->hp * pclass->hp_loss_pct / 100;
2200 }
2201
2202 if (punit->hp + gain > utype->hp) {
2203 gain = utype->hp - punit->hp;
2204 } else if (punit->hp + gain < 0) {
2205 gain = -punit->hp;
2206 }
2207
2208 return gain;
2209}
2210
2211/**********************************************************************/
2214bool is_losing_hp(const struct unit *punit)
2215{
2216 const struct unit_type *punittype = unit_type_get(punit);
2217
2218 return get_unit_bonus(punit, EFT_UNIT_RECOVER)
2219 < (punittype->hp *
2220 utype_class(punittype)->hp_loss_pct / 100);
2221}
2222
2223/**********************************************************************/
2226bool unit_type_is_losing_hp(const struct player *pplayer,
2227 const struct unit_type *punittype)
2228{
2229 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2230 EFT_UNIT_RECOVER)
2231 < (punittype->hp *
2232 utype_class(punittype)->hp_loss_pct / 100);
2233}
2234
2235/**********************************************************************/
2239bool unit_is_alive(int id)
2240{
2241 /* Check if unit exist in game */
2242 if (game_unit_by_number(id)) {
2243 return TRUE;
2244 }
2245
2246 return FALSE;
2247}
2248
2249/**********************************************************************/
2256bool unit_is_virtual(const struct unit *punit)
2257{
2258 if (!punit) {
2259 return FALSE;
2260 }
2261
2262 return punit != game_unit_by_number(punit->id);
2263}
2264
2265/**********************************************************************/
2268void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2269{
2270 return punit->server.ais[ai_type_number(ai)];
2271}
2272
2273/**********************************************************************/
2276void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2277 void *data)
2278{
2279 punit->server.ais[ai_type_number(ai)] = data;
2280}
2281
2282/**********************************************************************/
2290int unit_bribe_cost(struct unit *punit, struct player *briber)
2291{
2292 int cost, default_hp, dist = 0;
2293 struct tile *ptile = unit_tile(punit);
2294
2295 fc_assert_ret_val(punit != NULL, 0);
2296
2297 default_hp = unit_type_get(punit)->hp;
2298 cost = unit_owner(punit)->economic.gold + game.info.base_bribe_cost;
2299
2300 /* Consider the distance to the capital. */
2303 if (is_capital(capital)) {
2304 int tmp = map_distance(capital->tile, ptile);
2305
2306 if (tmp < dist) {
2307 dist = tmp;
2308 }
2309 }
2311
2312 cost /= dist + 2;
2313
2314 /* Consider the build cost. */
2316
2317 /* Rule set specific cost modification */
2318 cost += (cost
2320 &(const struct req_context) {
2321 .player = unit_owner(punit),
2322 .city = game_city_by_number(
2324 ),
2325 .tile = ptile,
2326 .unit = punit,
2327 .unittype = unit_type_get(punit),
2328 },
2329 briber,
2330 EFT_UNIT_BRIBE_COST_PCT))
2331 / 100;
2332
2333 /* Veterans are not cheap. */
2334 {
2335 const struct veteran_level *vlevel
2337
2338 fc_assert_ret_val(vlevel != NULL, 0);
2339 cost = cost * vlevel->power_fact / 100;
2340 if (unit_type_get(punit)->move_rate > 0) {
2342 } else {
2343 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2344 }
2345 }
2346
2347 /* Cost now contains the basic bribe cost. We now reduce it by:
2348 * bribecost = cost/2 + cost/2 * damage/hp
2349 * = cost/2 * (1 + damage/hp) */
2350 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2351}
2352
2353/**********************************************************************/
2356bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2357{
2358 fc_assert_ret_val(ptrans != NULL, FALSE);
2359 fc_assert_ret_val(pcargo != NULL, FALSE);
2360
2361 fc_assert_ret_val(!unit_list_search(ptrans->transporting, pcargo), FALSE);
2362
2363 if (force || can_unit_load(pcargo, ptrans)) {
2364 pcargo->transporter = ptrans;
2365 unit_list_append(ptrans->transporting, pcargo);
2366
2367 return TRUE;
2368 }
2369
2370 return FALSE;
2371}
2372
2373/**********************************************************************/
2376bool unit_transport_unload(struct unit *pcargo)
2377{
2378 struct unit *ptrans;
2379
2380 fc_assert_ret_val(pcargo != NULL, FALSE);
2381
2382 if (!unit_transported(pcargo)) {
2383 /* 'pcargo' is not transported. */
2384 return FALSE;
2385 }
2386
2387 /* Get the transporter; must not be defined on the client! */
2388 ptrans = unit_transport_get(pcargo);
2389 if (ptrans) {
2390 /* 'pcargo' and 'ptrans' should be on the same tile. */
2391 fc_assert(same_pos(unit_tile(pcargo), unit_tile(ptrans)));
2392
2393#ifndef FREECIV_NDEBUG
2394 bool success =
2395#endif
2396 unit_list_remove(ptrans->transporting, pcargo);
2397
2398 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2399 fc_assert(success);
2400 }
2401
2402 /* For the server (also safe for the client). */
2403 pcargo->transporter = NULL;
2404
2405 return TRUE;
2406}
2407
2408/**********************************************************************/
2411bool unit_transported(const struct unit *pcargo)
2412{
2413 fc_assert_ret_val(pcargo != NULL, FALSE);
2414
2415 /* The unit is transported if a transporter unit is set or, (for the client)
2416 * if the transported_by field is set. */
2417 if (is_server()) {
2418 return unit_transported_server(pcargo);
2419 } else {
2420 return unit_transported_client(pcargo);
2421 }
2422}
2423
2424/**********************************************************************/
2427struct unit *unit_transport_get(const struct unit *pcargo)
2428{
2429 fc_assert_ret_val(pcargo != NULL, NULL);
2430
2431 return pcargo->transporter;
2432}
2433
2434/**********************************************************************/
2437struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2438{
2439 fc_assert_ret_val(ptrans != NULL, NULL);
2440 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2441
2442 return ptrans->transporting;
2443}
2444
2445/**********************************************************************/
2448static inline bool
2449unit_transport_check_one(const struct unit_type *cargo_utype,
2450 const struct unit_type *trans_utype)
2451{
2452 return (trans_utype != cargo_utype
2453 && !can_unit_type_transport(cargo_utype,
2454 utype_class(trans_utype)));
2455}
2456
2457/**********************************************************************/
2465bool unit_transport_check(const struct unit *pcargo,
2466 const struct unit *ptrans)
2467{
2468 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2469
2470 /* Check 'pcargo' against 'ptrans'. */
2471 if (!unit_transport_check_one(cargo_utype, unit_type_get(ptrans))) {
2472 return FALSE;
2473 }
2474
2475 /* Check 'pcargo' against 'ptrans' parents. */
2476 unit_transports_iterate(ptrans, pparent) {
2477 if (!unit_transport_check_one(cargo_utype, unit_type_get(pparent))) {
2478 return FALSE;
2479 }
2481
2482 /* Check cargo children... */
2483 unit_cargo_iterate(pcargo, pchild) {
2484 cargo_utype = unit_type_get(pchild);
2485
2486 /* ...against 'ptrans'. */
2487 if (!unit_transport_check_one(cargo_utype, unit_type_get(ptrans))) {
2488 return FALSE;
2489 }
2490
2491 /* ...and against 'ptrans' parents. */
2492 unit_transports_iterate(ptrans, pparent) {
2493 if (!unit_transport_check_one(cargo_utype, unit_type_get(pparent))) {
2494 return FALSE;
2495 }
2498
2499 return TRUE;
2500}
2501
2502/**********************************************************************/
2506bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2507{
2508 unit_transports_iterate(pcargo, plevel) {
2509 if (ptrans == plevel) {
2510 return TRUE;
2511 }
2513 return FALSE;
2514}
2515
2516/**********************************************************************/
2519int unit_cargo_depth(const struct unit *ptrans)
2520{
2521 struct cargo_iter iter;
2522 struct iterator *it;
2523 int depth = 0;
2524
2525 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2526 iterator_next(it)) {
2527 if (iter.depth > depth) {
2528 depth = iter.depth;
2529 }
2530 }
2531 return depth;
2532}
2533
2534/**********************************************************************/
2537int unit_transport_depth(const struct unit *pcargo)
2538{
2539 int level = 0;
2540
2541 unit_transports_iterate(pcargo, plevel) {
2542 level++;
2544 return level;
2545}
2546
2547/**********************************************************************/
2551{
2552 return sizeof(struct cargo_iter);
2553}
2554
2555/**********************************************************************/
2558static void *cargo_iter_get(const struct iterator *it)
2559{
2560 const struct cargo_iter *iter = CARGO_ITER(it);
2561
2562 return unit_list_link_data(iter->links[iter->depth - 1]);
2563}
2564
2565/**********************************************************************/
2568static void cargo_iter_next(struct iterator *it)
2569{
2570 struct cargo_iter *iter = CARGO_ITER(it);
2571 const struct unit_list_link *piter;
2572 const struct unit_list_link *pnext;
2573
2574 /* Variant 1: unit has cargo. */
2575 pnext = unit_list_head(unit_transport_cargo(cargo_iter_get(it)));
2576 if (NULL != pnext) {
2577 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2578 iter->links[iter->depth++] = pnext;
2579 return;
2580 }
2581
2582 while (iter->depth > 0) {
2583 piter = iter->links[iter->depth - 1];
2584
2585 /* Variant 2: there are other cargo units at same level. */
2586 pnext = unit_list_link_next(piter);
2587 if (NULL != pnext) {
2588 iter->links[iter->depth - 1] = pnext;
2589 return;
2590 }
2591
2592 /* Variant 3: return to previous level, and do same tests. */
2593 iter->depth--;
2594 }
2595}
2596
2597/**********************************************************************/
2600static bool cargo_iter_valid(const struct iterator *it)
2601{
2602 return (0 < CARGO_ITER(it)->depth);
2603}
2604
2605/**********************************************************************/
2609 const struct unit *ptrans)
2610{
2611 struct iterator *it = ITERATOR(iter);
2612
2613 it->get = cargo_iter_get;
2614 it->next = cargo_iter_next;
2615 it->valid = cargo_iter_valid;
2616 iter->links[0] = unit_list_head(unit_transport_cargo(ptrans));
2617 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2618
2619 return it;
2620}
2621
2622/**********************************************************************/
2625bool unit_is_cityfounder(const struct unit *punit)
2626{
2628}
2629
2630
2631/**********************************************************************/
2634bool unit_order_list_is_sane(const struct civ_map *nmap,
2635 int length, const struct unit_order *orders)
2636{
2637 int i;
2638
2639 for (i = 0; i < length; i++) {
2640 struct action *paction;
2641 struct extra_type *pextra;
2642
2643 if (orders[i].order > ORDER_LAST) {
2644 log_error("invalid order %d at index %d", orders[i].order, i);
2645 return FALSE;
2646 }
2647 switch (orders[i].order) {
2648 case ORDER_MOVE:
2649 case ORDER_ACTION_MOVE:
2650 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2651 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2652 return FALSE;
2653 }
2654 break;
2655 case ORDER_ACTIVITY:
2656 switch (orders[i].activity) {
2657 case ACTIVITY_SENTRY:
2658 if (i != length - 1) {
2659 /* Only allowed as the last order. */
2660 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2661 i);
2662 return FALSE;
2663 }
2664 break;
2665 /* Replaced by action orders */
2666 case ACTIVITY_BASE:
2667 case ACTIVITY_GEN_ROAD:
2668 case ACTIVITY_FALLOUT:
2669 case ACTIVITY_POLLUTION:
2670 case ACTIVITY_PILLAGE:
2671 case ACTIVITY_MINE:
2672 case ACTIVITY_IRRIGATE:
2673 case ACTIVITY_PLANT:
2674 case ACTIVITY_CULTIVATE:
2675 case ACTIVITY_TRANSFORM:
2676 case ACTIVITY_CONVERT:
2677 case ACTIVITY_FORTIFYING:
2678 log_error("at index %d, use action rather than activity %d.",
2679 i, orders[i].activity);
2680 return FALSE;
2681 /* Not supported. */
2682 case ACTIVITY_EXPLORE:
2683 case ACTIVITY_IDLE:
2684 /* Not set from the client. */
2685 case ACTIVITY_GOTO:
2686 case ACTIVITY_FORTIFIED:
2687 /* Compatiblity, used in savegames. */
2688 case ACTIVITY_OLD_ROAD:
2689 case ACTIVITY_OLD_RAILROAD:
2690 case ACTIVITY_FORTRESS:
2691 case ACTIVITY_AIRBASE:
2692 /* Unused. */
2693 case ACTIVITY_PATROL_UNUSED:
2694 case ACTIVITY_LAST:
2695 case ACTIVITY_UNKNOWN:
2696 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2697 return FALSE;
2698 }
2699
2700 break;
2702 if (!action_id_exists(orders[i].action)) {
2703 /* Non-existent action. */
2704 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2705 return FALSE;
2706 }
2707
2708 paction = action_by_number(orders[i].action);
2709
2710 /* Validate main target. */
2711 if (index_to_tile(nmap, orders[i].target) == NULL) {
2712 log_error("at index %d, invalid tile target %d for the action %d.",
2713 i, orders[i].target, orders[i].action);
2714 return FALSE;
2715 }
2716
2717 if (orders[i].dir != DIR8_ORIGIN) {
2718 log_error("at index %d, the action %d sets the outdated target"
2719 " specification dir.",
2720 i, orders[i].action);
2721 }
2722
2723 /* Validate sub target. */
2724 switch (action_id_get_sub_target_kind(orders[i].action)) {
2725 case ASTK_BUILDING:
2726 /* Sub target is a building. */
2727 if (!improvement_by_number(orders[i].sub_target)) {
2728 /* Sub target is invalid. */
2729 log_error("at index %d, cannot do %s without a target.", i,
2730 action_id_rule_name(orders[i].action));
2731 return FALSE;
2732 }
2733 break;
2734 case ASTK_TECH:
2735 /* Sub target is a technology. */
2736 if (orders[i].sub_target == A_NONE
2737 || (!valid_advance_by_number(orders[i].sub_target)
2738 && orders[i].sub_target != A_FUTURE)) {
2739 /* Target tech is invalid. */
2740 log_error("at index %d, cannot do %s without a target.", i,
2741 action_id_rule_name(orders[i].action));
2742 return FALSE;
2743 }
2744 break;
2745 case ASTK_EXTRA:
2746 case ASTK_EXTRA_NOT_THERE:
2747 /* Sub target is an extra. */
2748 pextra = (!(orders[i].sub_target == NO_TARGET
2749 || (orders[i].sub_target < 0
2750 || (orders[i].sub_target
2752 ? extra_by_number(orders[i].sub_target) : NULL);
2753 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2754 if (pextra == NULL) {
2755 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2756 /* Target extra is invalid. */
2757 log_error("at index %d, cannot do %s without a target.", i,
2758 action_id_rule_name(orders[i].action));
2759 return FALSE;
2760 }
2761 } else {
2762 if (!(action_removes_extra(paction, pextra)
2763 || action_creates_extra(paction, pextra))) {
2764 /* Target extra is irrelevant for the action. */
2765 log_error("at index %d, cannot do %s to %s.", i,
2766 action_id_rule_name(orders[i].action),
2767 extra_rule_name(pextra));
2768 return FALSE;
2769 }
2770 }
2771 break;
2772 case ASTK_NONE:
2773 /* No validation required. */
2774 break;
2775 /* Invalid action? */
2776 case ASTK_COUNT:
2778 action_id_get_sub_target_kind(orders[i].action) != ASTK_COUNT,
2779 FALSE,
2780 "Bad action %d in order number %d.", orders[i].action, i);
2781 }
2782
2783 /* Some action orders are sane only in the last order. */
2784 if (i != length - 1) {
2785 /* If the unit is dead, */
2786 if (utype_is_consumed_by_action(paction, NULL)
2787 /* or if Freeciv has no idea where the unit will end up after it
2788 * has performed this action, */
2789 || !(utype_is_unmoved_by_action(paction, NULL)
2790 || utype_is_moved_to_tgt_by_action(paction, NULL))
2791 /* or if the unit will end up standing still, */
2792 || action_has_result(paction, ACTRES_FORTIFY)) {
2793 /* than having this action in the middle of a unit's orders is
2794 * probably wrong. */
2795 log_error("action %d is not allowed at index %d.",
2796 orders[i].action, i);
2797 return FALSE;
2798 }
2799 }
2800
2801 /* Don't validate that the target tile really contains a target or
2802 * that the actor player's map think the target tile has one.
2803 * The player may target something from their player map that isn't
2804 * there any more, a target they think is there even if their player
2805 * map doesn't have it, or even a target they assume will be there
2806 * when the unit reaches the target tile.
2807 *
2808 * With that said: The client should probably at least have an
2809 * option to only aim city targeted actions at cities. */
2810
2811 break;
2812 case ORDER_FULL_MP:
2813 break;
2814 case ORDER_LAST:
2815 /* An invalid order. This is handled above. */
2816 break;
2817 }
2818 }
2819
2820 return TRUE;
2821}
2822
2823/**********************************************************************/
2827struct unit_order *create_unit_orders(const struct civ_map *nmap,
2828 int length,
2829 const struct unit_order *orders)
2830{
2831 struct unit_order *unit_orders;
2832
2833 if (!unit_order_list_is_sane(nmap, length, orders)) {
2834 return NULL;
2835 }
2836
2837 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2838 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2839
2840 return unit_orders;
2841}
bool action_removes_extra(const struct action *paction, const struct extra_type *pextra)
Definition actions.c:2312
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:6707
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1899
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:6225
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:4996
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:4755
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:4832
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:5870
bool action_maybe_possible_actor_unit(const struct civ_map *nmap, const action_id act_id, const struct unit *actor_unit)
Definition actions.c:7169
bool action_id_exists(const action_id act_id)
Definition actions.c:1697
bool action_creates_extra(const struct action *paction, const struct extra_type *pextra)
Definition actions.c:2224
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:5145
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:5953
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:488
#define action_id_get_sub_target_kind(act_id)
Definition actions.h:659
static struct action * action_by_number(action_id act_id)
Definition actions.h:638
#define action_has_result(_act_, _res_)
Definition actions.h:448
#define action_by_result_iterate_end
Definition actions.h:492
int ai_type_number(const struct ai_type *ai)
Definition ai.c:272
#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:1548
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2035
bool is_unit_near_a_friendly_city(const struct civ_map *nmap, const struct unit *punit)
Definition city.c:2051
#define city_list_iterate(citylist, pcity)
Definition city.h:488
#define city_tile(_pcity_)
Definition city.h:544
#define city_owner(_pcity_)
Definition city.h:543
#define city_list_iterate_end
Definition city.h:490
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2839
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:73
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:73
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:691
int get_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Definition effects.c:992
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:957
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:740
struct extra_type * extra_by_number(int id)
Definition extras.c:175
bool can_extras_coexist(const struct extra_type *pextra1, const struct extra_type *pextra2)
Definition extras.c:952
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:195
const char * extra_name_translation(const struct extra_type *pextra)
Definition extras.c:186
#define extra_type_list_iterate(extralist, pextra)
Definition extras.h:159
#define extra_index(_e_)
Definition extras.h:177
#define extra_type_list_iterate_end
Definition extras.h:161
static bool is_server(void)
#define NO_TARGET
Definition fc_types.h:324
enum unit_activity Activity_type_id
Definition fc_types.h:349
#define DIR8_ORIGIN
Definition fc_types.h:427
int action_id
Definition fc_types.h:359
@ HB_ALLIANCE
Definition fc_types.h:1144
@ HB_DISABLED
Definition fc_types.h:1142
@ HB_NATIONAL
Definition fc_types.h:1143
@ O_SHIELD
Definition fc_types.h:91
@ O_FOOD
Definition fc_types.h:91
@ O_GOLD
Definition fc_types.h:91
@ O_LAST
Definition fc_types.h:91
@ BORDERS_DISABLED
Definition fc_types.h:891
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:82
#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:57
struct world wld
Definition game.c:58
struct unit * game_unit_by_number(int id)
Definition game.c:111
struct city * game_city_by_number(int id)
Definition game.c:102
#define GAME_UNIT_BRIBE_DIST_MAX
Definition game.h:727
#define GAME_TRANSPORT_MAX_RECURSIVE
Definition game.h:730
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:454
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:938
bool map_untrusted_dir_is_valid(enum direction8 dir)
Definition map.c:1271
enum direction8 rand_direction(void)
Definition map.c:1732
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:652
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:385
#define square_iterate_end
Definition map.h:388
#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:973
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Definition movement.c:275
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:304
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:316
bool can_unit_transport(const struct unit *transporter, const struct unit *transported)
Definition movement.c:813
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:476
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:450
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:826
bool can_attack_non_native(const struct unit_type *utype)
Definition movement.c:202
#define SINGLE_MOVE
Definition movement.h:24
#define MAX_LEN_MSG
Definition packets.h:43
struct city_list * cities
Definition packhand.c:117
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Definition player.c:1084
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1364
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1381
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1435
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:183
#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
enum act_tgt_compl target_complexity
Definition actions.h:390
Definition ai.h:50
struct iterator vtable
Definition unit.c:52
int depth
Definition unit.c:54
const struct unit_list_link * links[GAME_TRANSPORT_MAX_RECURSIVE]
Definition unit.c:53
Definition city.h:309
int id
Definition city.h:315
int airlift
Definition city.h:365
bool occupied
Definition city.h:443
struct tile * tile
Definition city.h:311
struct city::@17::@20 client
struct civ_game::@30::@34 server
struct packet_ruleset_control control
Definition game.h:83
struct packet_game_info info
Definition game.h:89
int killunhomed
Definition game.h:153
bool ruledit_disabled
Definition extras.h:88
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:49
struct unit_list * units
Definition tile.h:57
int hp_loss_pct
Definition unittype.h:142
struct extra_type_list * hiding_extras
Definition unittype.h:159
enum unit_activity activity
Definition unit.h:94
enum unit_orders order
Definition unit.h:93
int action
Definition unit.h:100
enum direction8 dir
Definition unit.h:102
int target
Definition unit.h:97
int sub_target
Definition unit.h:98
int transport_capacity
Definition unittype.h:504
struct unit_type::@87 adv
bool worker
Definition unittype.h:556
int move_rate
Definition unittype.h:497
enum vision_layer vlayer
Definition unittype.h:550
const struct unit_type * converted_to
Definition unittype.h:511
Definition unit.h:138
time_t action_timestamp
Definition unit.h:243
int length
Definition unit.h:195
int upkeep[O_LAST]
Definition unit.h:148
bool has_orders
Definition unit.h:193
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
int id
Definition unit.h:145
struct unit_list * transporting
Definition unit.h:184
int ord_city
Definition unit.h:240
bool moved
Definition unit.h:173
int ord_map
Definition unit.h:239
bool debug
Definition unit.h:231
struct vision * vision
Definition unit.h:242
int hp
Definition unit.h:151
struct unit::@80::@82 client
struct unit::@79 orders
int fuel
Definition unit.h:153
struct extra_type * changed_from_target
Definition unit.h:170
bool stay
Definition unit.h:205
bool colored
Definition unit.h:219
enum direction8 facing
Definition unit.h:142
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:225
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:233
struct player * nationality
Definition unit.h:144
int transported_by
Definition unit.h:213
void(* removal_callback)(struct unit *punit)
Definition unit.h:251
void * ais[FREECIV_AI_MOD_LAST]
Definition unit.h:234
int action_turn
Definition unit.h:244
struct unit::@80::@83 server
int homecity
Definition unit.h:146
int upkeep_paid[O_LAST]
Definition unit.h:254
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:235
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:248
enum unit_focus_status focus_status
Definition unit.h:211
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:969
#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:269
bool tile_has_not_aggressive_extra_for_unit(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:193
bool tile_is_placing(const struct tile *ptile)
Definition tile.c:1114
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Definition tile.c:412
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:318
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:386
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
@ TILE_KNOWN_UNSEEN
Definition tile.h:36
@ TILE_KNOWN_SEEN
Definition tile.h:37
#define ACTIVITY_FACTOR
Definition tile.h:164
#define tile_terrain(_tile)
Definition tile.h:109
#define tile_has_extra(ptile, pextra)
Definition tile.h:146
#define tile_owner(_tile)
Definition tile.h:95
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:351
size_t cargo_iter_sizeof(void)
Definition unit.c:2550
Activity_type_id real_activities[ACTIVITY_LAST]
Definition unit.c:44
bool is_real_activity(enum unit_activity activity)
Definition unit.c:609
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:925
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1608
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2827
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:1942
bool unit_being_aggressive(const struct unit *punit)
Definition unit.c:1525
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1511
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2226
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1593
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1333
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:219
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1769
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2568
void free_unit_orders(struct unit *punit)
Definition unit.c:1755
bool can_unit_change_homecity(const struct unit *punit)
Definition unit.c:464
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2276
bool unit_is_alive(int id)
Definition unit.c:2239
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:289
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:398
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:505
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2121
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:521
bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1473
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2356
bool unit_can_add_or_build_city(const struct unit *punit)
Definition unit.c:430
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1908
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2608
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2037
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2537
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1108
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2214
bool can_unit_change_homecity_to(const struct unit *punit, const struct city *pcity)
Definition unit.c:447
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:744
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1366
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2148
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2558
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1272
int get_activity_rate(const struct unit *punit)
Definition unit.c:477
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2465
bool is_attack_unit(const struct unit *punit)
Definition unit.c:310
const Activity_type_id tile_changing_activities[]
Definition unit.c:46
void setup_real_activities_array(void)
Definition unit.c:591
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2427
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2376
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1562
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2506
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2169
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:83
bool is_military_unit(const struct unit *punit)
Definition unit.c:322
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:846
int unit_bribe_cost(struct unit *punit, struct player *briber)
Definition unit.c:2290
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1258
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:719
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2625
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1143
bool can_unit_do_autosettlers(const struct unit *punit)
Definition unit.c:583
bool unit_can_help_build_wonder_here(const struct unit *punit)
Definition unit.c:263
bool can_unit_alight_or_be_unloaded(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:796
bool is_field_unit(const struct unit *punit)
Definition unit.c:386
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2600
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:331
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:341
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
void set_unit_activity_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type *new_target)
Definition unit.c:1125
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1619
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2519
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Definition unit.c:2449
#define CARGO_ITER(iter)
Definition unit.c:56
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2634
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:625
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1158
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:302
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1414
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1578
bool unit_can_airlift_to(const struct unit *punit, const struct city *pdest_city)
Definition unit.c:188
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:827
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1175
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:873
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1350
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1715
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1318
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1282
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:1974
bool kills_citizen_after_attack(const struct unit *punit)
Definition unit.c:420
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2411
bool are_unit_orders_equal(const struct unit_order *order1, const struct unit_order *order2)
Definition unit.c:61
bool unit_is_virtual(const struct unit *punit)
Definition unit.c:2256
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1090
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:360
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:771
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1387
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2437
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1918
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2268
bool unit_has_orders(const struct unit *punit)
Definition unit.c:207
struct unit * tile_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1295
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1898
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:1779
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2021
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:541
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:687
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:912
#define unit_tile(_pu)
Definition unit.h:388
#define unit_transports_iterate_end
Definition unit.h:552
#define unit_cargo_iterate_end
Definition unit.h:562
@ 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:530
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:559
#define unit_owner(_pu)
Definition unit.h:387
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:548
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:533
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:1433
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:294
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1621
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1301
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2967
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1639
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1520
bool utype_may_act_move_frags(const struct unit_type *punit_type, const action_id act_id, const int move_fragments)
Definition unittype.c:1110
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1536
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1755
bool utype_can_freely_load(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:282
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2661
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2546
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:459
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Definition unittype.c:2631
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:184
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1271
bool utype_can_do_action_sub_result(const struct unit_type *putype, enum action_sub_result sub_result)
Definition unittype.c:480
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1340
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:515
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1612
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:443
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1783
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:753
#define utype_class(_t_)
Definition unittype.h:736
#define utype_fuel(ptype)
Definition unittype.h:822
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:604