Freeciv-3.3: /fast/freeciv/nightly/main/common/unit.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
unit.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "astring.h"
20#include "bitvector.h"
21#include "fcintl.h"
22#include "mem.h"
23#include "shared.h"
24#include "support.h"
25
26/* common */
27#include "ai.h"
28#include "actions.h"
29#include "base.h"
30#include "city.h"
31#include "game.h"
32#include "log.h"
33#include "map.h"
34#include "movement.h"
35#include "packets.h"
36#include "player.h"
37#include "road.h"
38#include "tech.h"
39#include "traderoutes.h"
40#include "unitlist.h"
41
42#include "unit.h"
43
44 const Activity_type_id tile_changing_activities[] =
45 { ACTIVITY_PILLAGE, ACTIVITY_GEN_ROAD, ACTIVITY_IRRIGATE, ACTIVITY_MINE,
46 ACTIVITY_BASE, ACTIVITY_CULTIVATE, ACTIVITY_PLANT, ACTIVITY_TRANSFORM,
47 ACTIVITY_CLEAN, ACTIVITY_LAST };
48
49 struct cargo_iter {
50 struct iterator vtable;
51 const struct unit_list_link *links[GAME_TRANSPORT_MAX_RECURSIVE];
52 int depth;
53};
54#define CARGO_ITER(iter) ((struct cargo_iter *) (iter))
55
56/**********************************************************************/
59 bool are_unit_orders_equal(const struct unit_order *order1,
60 const struct unit_order *order2)
61{
62 return order1->order == order2->order
63 && order1->activity == order2->activity
64 && order1->target == order2->target
65 && order1->sub_target == order2->sub_target
66 && order1->action == order2->action
67 && order1->dir == order2->dir;
68}
69
70/**********************************************************************/
80enum unit_airlift_result
81 test_unit_can_airlift_to(const struct civ_map *nmap,
82 const struct player *restriction,
83 const struct unit *punit,
84 const struct city *pdest_city)
85{
86 const struct city *psrc_city = tile_city(unit_tile(punit));
87 const struct player *punit_owner;
88 enum unit_airlift_result ok_result = AR_OK;
89
90 if (0 == punit->moves_left
91 && !utype_may_act_move_frags(unit_type_get(punit),
92 ACTION_AIRLIFT, 0)) {
93 /* No moves left. */
94 return AR_NO_MOVES;
95 }
96
97 if (!unit_can_do_action(punit, ACTION_AIRLIFT)) {
98 return AR_WRONG_UNITTYPE;
99 }
100
101 if (0 < get_transporter_occupancy(punit)) {
102 /* Units with occupants can't be airlifted currently. */
103 return AR_OCCUPIED;
104 }
105
106 if (NULL == psrc_city) {
107 /* No city there. */
108 return AR_NOT_IN_CITY;
109 }
110
111 if (psrc_city == pdest_city) {
112 /* Airlifting to our current position doesn't make sense. */
113 return AR_BAD_DST_CITY;
114 }
115
116 if (pdest_city
117 && (NULL == restriction
118 || (tile_get_known(city_tile(pdest_city), restriction)
119 == TILE_KNOWN_SEEN))
120 && !can_unit_exist_at_tile(nmap, punit, city_tile(pdest_city))) {
121 /* Can't exist at the destination tile. */
122 return AR_BAD_DST_CITY;
123 }
124
125 punit_owner = unit_owner(punit);
126
127 /* Check validity of both source and destination before checking capacity,
128 * to avoid misleadingly optimistic returns. */
129
130 if (punit_owner != city_owner(psrc_city)
131 && !(game.info.airlifting_style & AIRLIFTING_ALLIED_SRC
132 && pplayers_allied(punit_owner, city_owner(psrc_city)))) {
133 /* Not allowed to airlift from this source. */
134 return AR_BAD_SRC_CITY;
135 }
136
137 if (pdest_city
138 && punit_owner != city_owner(pdest_city)
139 && !(game.info.airlifting_style & AIRLIFTING_ALLIED_DEST
140 && pplayers_allied(punit_owner, city_owner(pdest_city)))) {
141 /* Not allowed to airlift to this destination. */
142 return AR_BAD_DST_CITY;
143 }
144
145 if (NULL == restriction || city_owner(psrc_city) == restriction) {
146 /* We know for sure whether or not src can airlift this turn. */
147 if (0 >= psrc_city->airlift
148 && (!(game.info.airlifting_style & AIRLIFTING_UNLIMITED_SRC)
149 || !game.info.airlift_from_always_enabled)) {
150 /* The source cannot airlift for this turn (maybe already airlifted
151 * or no airport).
152 * See also do_airline() in server/unittools.h. */
153 return AR_SRC_NO_FLIGHTS;
154 } /* else, there is capacity; continue to other checks */
155 } else {
156 /* We don't have access to the 'airlift' field. Assume it's OK; can
157 * only find out for sure by trying it. */
158 ok_result = AR_OK_SRC_UNKNOWN;
159 }
160
161 if (pdest_city) {
162 if (NULL == restriction || city_owner(pdest_city) == restriction) {
163 if (0 >= pdest_city->airlift
164 && (!(game.info.airlifting_style & AIRLIFTING_UNLIMITED_DEST)
165 || !game.info.airlift_to_always_enabled)) {
166 /* The destination cannot support airlifted units for this turn
167 * (maybe already airlifted or no airport).
168 * See also do_airline() in server/unittools.h. */
169 return AR_DST_NO_FLIGHTS;
170 } /* else continue */
171 } else {
172 ok_result = AR_OK_DST_UNKNOWN;
173 }
174 }
175
176 return ok_result;
177}
178
179/**********************************************************************/
186 bool unit_can_airlift_to(const struct unit *punit,
187 const struct city *pdest_city)
188{
189 const struct civ_map *nmap = &(wld.map);
190
191 fc_assert_ret_val(pdest_city, FALSE);
192
193 if (is_server()) {
194 return is_action_enabled_unit_on_city(nmap, ACTION_AIRLIFT,
195 punit, pdest_city);
196 } else {
197 return action_prob_possible(action_prob_vs_city(punit, ACTION_AIRLIFT,
198 pdest_city));
199 }
200}
201
202/**********************************************************************/
205 bool unit_has_orders(const struct unit *punit)
206{
207 return punit->has_orders;
208}
209
210/**********************************************************************/
217 int unit_shield_value(const struct unit *punit,
218 const struct unit_type *punittype,
219 const struct action *paction)
220{
221 int value;
222
223 bool has_unit;
224 const struct player *act_player;
225
226 has_unit = punit != NULL;
227
228 if (has_unit && punittype == NULL) {
229 punittype = unit_type_get(punit);
230 }
231
232 fc_assert_ret_val(punittype != NULL, 0);
233 fc_assert(punit == NULL || unit_type_get(punit) == punittype);
234 fc_assert_ret_val(paction != NULL, 0);
235
236 act_player = has_unit ? unit_owner(punit) : NULL;
237 /* TODO: determine if tile and city should be where the unit currently is
238 * located or the target city. Those two may differ. Wait for ruleset
239 * author feed back. */
240
241 value = utype_build_shield_cost_base(punittype);
242 value += ((value
243 * get_target_bonus_effects(NULL,
244 &(const struct req_context) {
245 .player = act_player,
246 .unit = punit,
247 .unittype = punittype,
248 .action = paction,
249 },
250 NULL,
251 EFT_UNIT_SHIELD_VALUE_PCT))
252 / 100);
253
254 return value;
255}
256
257/**********************************************************************/
261 bool unit_can_help_build_wonder_here(const struct unit *punit)
262{
263 struct city *pcity = tile_city(unit_tile(punit));
264
265 if (!pcity) {
266 /* No city to help at this tile. */
267 return FALSE;
268 }
269
270 if (!utype_can_do_action(unit_type_get(punit), ACTION_HELP_WONDER)) {
271 /* This unit can never do help wonder. */
272 return FALSE;
273 }
274
275 /* Evaluate all action enablers for extra accuracy. */
276 /* TODO: Is it worth it? */
277 return action_prob_possible(action_prob_vs_city(punit,
278 ACTION_HELP_WONDER,
279 pcity));
280}
281
282/**********************************************************************/
286 bool unit_can_est_trade_route_here(const struct unit *punit)
287{
288 struct city *phomecity, *pdestcity;
289
290 return (utype_can_do_action(unit_type_get(punit), ACTION_TRADE_ROUTE)
291 && (pdestcity = tile_city(unit_tile(punit)))
292 && (phomecity = game_city_by_number(punit->homecity))
293 && can_cities_trade(phomecity, pdestcity));
294}
295
296/**********************************************************************/
299 int get_transporter_capacity(const struct unit *punit)
300{
301 return unit_type_get(punit)->transport_capacity;
302}
303
304/**********************************************************************/
307 bool is_attack_unit(const struct unit *punit)
308{
309 return ((unit_can_do_action_result(punit, ACTRES_ATTACK)
310 || unit_can_do_action_result(punit, ACTRES_BOMBARD)
311 || unit_can_do_action_result(punit, ACTRES_WIPE_UNITS))
312 && unit_type_get(punit)->attack_strength > 0);
313}
314
315/**********************************************************************/
318 bool is_martial_law_unit(const struct unit *punit)
319{
320 return !unit_has_type_flag(punit, UTYF_CIVILIAN);
321}
322
323/**********************************************************************/
326 bool is_occupying_unit(const struct unit *punit)
327{
328 return !unit_has_type_flag(punit, UTYF_CIVILIAN);
329}
330
331/**********************************************************************/
334 bool is_enter_borders_unit(const struct unit *punit)
335{
336 return unit_has_type_flag(punit, UTYF_CIVILIAN);
337}
338
339/**********************************************************************/
342 bool is_guard_unit(const struct unit *punit)
343{
344 return !unit_has_type_flag(punit, UTYF_CIVILIAN);
345}
346
347/**********************************************************************/
352 bool is_special_unit(const struct unit *punit)
353{
354 return unit_has_type_flag(punit, UTYF_CIVILIAN);
355}
356
357/**********************************************************************/
365 bool is_flagless_to_player(const struct unit *punit,
366 const struct player *pplayer)
367{
368 if (unit_owner(punit) != pplayer
369 && unit_has_type_flag(punit, UTYF_FLAGLESS)) {
370 if (pplayer == nullptr) {
371 /* Global observer always sees the flags */
372 return FALSE;
373 }
374
375 return TRUE;
376 }
377
378 return FALSE;
379}
380
381/**********************************************************************/
385 bool unit_can_do_action(const struct unit *punit,
386 const action_id act_id)
387{
388 return utype_can_do_action(unit_type_get(punit), act_id);
389}
390
391/**********************************************************************/
395 bool unit_can_do_action_result(const struct unit *punit,
396 enum action_result result)
397{
398 return utype_can_do_action_result(unit_type_get(punit), result);
399}
400
401/**********************************************************************/
405 bool unit_can_do_action_sub_result(const struct unit *punit,
406 enum action_sub_result sub_result)
407{
408 return utype_can_do_action_sub_result(unit_type_get(punit), sub_result);
409}
410
411/**********************************************************************/
414 bool is_square_threatened(const struct civ_map *nmap,
415 const struct player *pplayer,
416 const struct tile *ptile, bool omniscient)
417{
418 square_iterate(nmap, ptile, 2, ptile1) {
419 unit_list_iterate(ptile1->units, punit) {
420 if ((omniscient
421 || can_player_see_unit(pplayer, punit))
422 && pplayers_at_war(pplayer, unit_owner(punit))
423 && utype_acts_hostile(unit_type_get(punit))
424 && (is_native_tile(unit_type_get(punit), ptile)
425 || (can_attack_non_native(unit_type_get(punit))
426 && is_native_near_tile(nmap,
427 unit_class_get(punit), ptile)))) {
428 return TRUE;
429 }
430 } unit_list_iterate_end;
431 } square_iterate_end;
432
433 return FALSE;
434}
435
436/**********************************************************************/
440 bool is_field_unit(const struct unit *punit)
441{
442 return unit_has_type_flag(punit, UTYF_FIELDUNIT);
443}
444
445/**********************************************************************/
452 bool is_hiding_unit(const struct unit *punit)
453{
454 enum vision_layer vl = unit_type_get(punit)->vlayer;
455
456 if (vl == V_INVIS || vl == V_SUBSURFACE) {
457 return TRUE;
458 }
459
460 if (unit_transported(punit)) {
461 vl = unit_type_get(unit_transport_get(punit))->vlayer;
462 if (vl == V_INVIS || vl == V_SUBSURFACE) {
463 return TRUE;
464 }
465 }
466
467 return FALSE;
468}
469
470/**********************************************************************/
474 bool unit_can_add_or_build_city(const struct unit *punit)
475{
476 struct city *tgt_city;
477
478 if ((tgt_city = tile_city(unit_tile(punit)))) {
479 return action_prob_possible(action_prob_vs_city(punit,
480 ACTION_JOIN_CITY, tgt_city));
481 } else {
482 return action_prob_possible(action_prob_vs_tile(punit,
483 ACTION_FOUND_CITY, unit_tile(punit), NULL));
484 }
485}
486
487/**********************************************************************/
490 bool can_unit_change_homecity_to(const struct unit *punit,
491 const struct city *pcity)
492{
493 if (pcity == NULL) {
494 /* Can't change home city to a non existing city. */
495 return FALSE;
496 }
497
498 return action_prob_possible(action_prob_vs_city(punit, ACTION_HOME_CITY,
499 pcity));
500}
501
502/**********************************************************************/
505 bool can_unit_change_homecity(const struct unit *punit)
506{
507 return can_unit_change_homecity_to(punit, tile_city(unit_tile(punit)));
508}
509
510/**********************************************************************/
518 int get_activity_rate(const struct unit *punit)
519{
520 const struct veteran_level *vlevel;
521
522 fc_assert_ret_val(punit != NULL, 0);
523
524 vlevel = utype_veteran_level(unit_type_get(punit), punit->veteran);
525 fc_assert_ret_val(vlevel != NULL, 0);
526
527 /* The speed of the settler depends on its base move_rate, not on
528 * the number of moves actually remaining or the adjusted move rate.
529 * This means sea formers won't have their activity rate increased by
530 * Magellan's, and it means injured units work just as fast as
531 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
532 int move_rate = unit_type_get(punit)->move_rate;
533
534 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
535 return ACTIVITY_FACTOR
536 * (float)vlevel->power_fact / 100
537 * move_rate / SINGLE_MOVE;
538}
539
540/**********************************************************************/
546 int get_activity_rate_this_turn(const struct unit *punit)
547{
548 /* This logic is also coded in client/goto.c. */
549 if (punit->moves_left > 0) {
550 return get_activity_rate(punit);
551 } else {
552 return 0;
553 }
554}
555
556/**********************************************************************/
562 int get_turns_for_activity_at(const struct unit *punit,
563 enum unit_activity activity,
564 const struct tile *ptile,
565 struct extra_type *tgt)
566{
567 /* FIXME: This is just an approximation since we don't account for
568 * get_activity_rate_this_turn. */
569 int speed = get_activity_rate(punit);
570 int points_needed;
571
572 fc_assert(tgt != NULL || !is_targeted_activity(activity));
573
574 points_needed = tile_activity_time(activity, ptile, tgt);
575
576 if (points_needed >= 0 && speed > 0) {
577 return (points_needed - 1) / speed + 1; /* Round up */
578 } else {
579 return FC_INFINITY;
580 }
581}
582
583/**********************************************************************/
586 bool activity_requires_target(enum unit_activity activity)
587{
588 switch (activity) {
589 case ACTIVITY_PILLAGE:
590 case ACTIVITY_BASE:
591 case ACTIVITY_GEN_ROAD:
592 case ACTIVITY_IRRIGATE:
593 case ACTIVITY_MINE:
594 case ACTIVITY_CLEAN:
595 return TRUE;
596 case ACTIVITY_IDLE:
597 case ACTIVITY_FORTIFIED:
598 case ACTIVITY_SENTRY:
599 case ACTIVITY_GOTO:
600 case ACTIVITY_EXPLORE:
601 case ACTIVITY_TRANSFORM:
602 case ACTIVITY_CULTIVATE:
603 case ACTIVITY_PLANT:
604 case ACTIVITY_FORTIFYING:
605 case ACTIVITY_CONVERT:
606 return FALSE;
607 /* These shouldn't be kicking around internally. */
608 case ACTIVITY_LAST:
609 break;
610 }
611
612 fc_assert(FALSE);
613
614 return FALSE;
615}
616
617/**********************************************************************/
626 bool can_unit_do_autoworker(const struct unit *punit)
627{
628 return unit_type_get(punit)->adv.worker;
629}
630
631/**********************************************************************/
634 const char *get_activity_text(enum unit_activity activity)
635{
636 /* The switch statement has just the activities listed with no "default"
637 * handling. This enables the compiler to detect missing entries
638 * automatically, and still handles everything correctly. */
639 switch (activity) {
640 case ACTIVITY_IDLE:
641 return _("Idle");
642 case ACTIVITY_CLEAN:
643 return _("Clean");
644 case ACTIVITY_MINE:
645 /* TRANS: Activity name, verb in English */
646 return Q_("?act:Mine");
647 case ACTIVITY_PLANT:
648 /* TRANS: Activity name, verb in English */
649 return _("Plant");
650 case ACTIVITY_IRRIGATE:
651 return _("Irrigate");
652 case ACTIVITY_CULTIVATE:
653 return _("Cultivate");
654 case ACTIVITY_FORTIFYING:
655 return _("Fortifying");
656 case ACTIVITY_FORTIFIED:
657 return _("Fortified");
658 case ACTIVITY_SENTRY:
659 return _("Sentry");
660 case ACTIVITY_PILLAGE:
661 return _("Pillage");
662 case ACTIVITY_GOTO:
663 return _("Goto");
664 case ACTIVITY_EXPLORE:
665 return _("Explore");
666 case ACTIVITY_TRANSFORM:
667 return _("Transform");
668 case ACTIVITY_BASE:
669 return _("Base");
670 case ACTIVITY_GEN_ROAD:
671 return _("Road");
672 case ACTIVITY_CONVERT:
673 return _("Convert");
674 case ACTIVITY_LAST:
675 break;
676 }
677
678 fc_assert(FALSE);
679 return _("Unknown");
680}
681
682/**********************************************************************/
688 bool could_unit_be_in_transport(const struct unit *pcargo,
689 const struct unit *ptrans)
690{
691 /* Make sure this transporter can carry this type of unit. */
692 if (!can_unit_transport(ptrans, pcargo)) {
693 return FALSE;
694 }
695
696 /* Make sure there's room in the transporter. */
697 if (get_transporter_occupancy(ptrans)
698 >= get_transporter_capacity(ptrans)) {
699 return FALSE;
700 }
701
702 /* Check iff this is a valid transport. */
703 if (!unit_transport_check(pcargo, ptrans)) {
704 return FALSE;
705 }
706
707 /* Check transport depth. */
708 if (GAME_TRANSPORT_MAX_RECURSIVE
709 < 1 + unit_transport_depth(ptrans) + unit_cargo_depth(pcargo)) {
710 return FALSE;
711 }
712
713 return TRUE;
714}
715
716/**********************************************************************/
720 bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
721{
722 if (!pcargo || !ptrans || pcargo == ptrans) {
723 return FALSE;
724 }
725
726 /* Double-check ownership of the units: you can load into an allied unit
727 * (of course only allied units can be on the same tile). */
728 if (!pplayers_allied(unit_owner(pcargo), unit_owner(ptrans))) {
729 return FALSE;
730 }
731
732 /* Un-embarkable transport must be in city or base to load cargo. */
733 if (!utype_can_freely_load(unit_type_get(pcargo), unit_type_get(ptrans))
734 && !tile_city(unit_tile(ptrans))
735 && !tile_has_native_base(unit_tile(ptrans), unit_type_get(ptrans))) {
736 return FALSE;
737 }
738
739 return could_unit_be_in_transport(pcargo, ptrans);
740}
741
742/**********************************************************************/
745 bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
746{
747 /* This function needs to check EVERYTHING. */
748
749 /* Check positions of the units. Of course you can't load a unit onto
750 * a transporter on a different tile... */
751 if (!same_pos(unit_tile(pcargo), unit_tile(ptrans))) {
752 return FALSE;
753 }
754
755 /* Cannot load if cargo is already loaded onto something else. */
756 if (unit_transported(pcargo)) {
757 return FALSE;
758 }
759
760 return could_unit_load(pcargo, ptrans);
761}
762
763/**********************************************************************/
772 bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
773{
774 if (!pcargo || !ptrans) {
775 return FALSE;
776 }
777
778 /* Make sure the unit's transporter exists and is known. */
779 if (unit_transport_get(pcargo) != ptrans) {
780 return FALSE;
781 }
782
783 /* Un-disembarkable transport must be in city or base to unload cargo. */
784 if (!utype_can_freely_unload(unit_type_get(pcargo), unit_type_get(ptrans))
785 && !tile_city(unit_tile(ptrans))
786 && !tile_has_native_base(unit_tile(ptrans), unit_type_get(ptrans))) {
787 return FALSE;
788 }
789
790 return TRUE;
791}
792
793/**********************************************************************/
797 bool can_unit_deboard_or_be_unloaded(const struct unit *pcargo,
798 const struct unit *ptrans)
799{
800 const struct civ_map *nmap = &(wld.map);
801
802 if (!pcargo || !ptrans) {
803 return FALSE;
804 }
805
806 fc_assert_ret_val(unit_transport_get(pcargo) == ptrans, FALSE);
807
808 if (is_server()) {
809 return (is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_DEBOARD,
810 pcargo, ptrans)
811 || is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_UNLOAD,
812 ptrans, pcargo));
813 } else {
814 return (action_prob_possible(
815 action_prob_vs_unit(pcargo, ACTION_TRANSPORT_DEBOARD, ptrans))
816 || action_prob_possible(
817 action_prob_vs_unit(ptrans, ACTION_TRANSPORT_UNLOAD,
818 pcargo)));
819 }
820}
821
822/**********************************************************************/
827 bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
828{
829 action_by_result_iterate(paction, ACTRES_TELEPORT) {
830 if (action_maybe_possible_actor_unit(nmap, action_id(paction), punit)) {
831 return TRUE;
832 }
833 } action_by_result_iterate_end;
834
835 return FALSE;
836}
837
838/**********************************************************************/
843 bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
844{
845 action_by_result_iterate(paction, ACTRES_PARADROP) {
846 if (action_maybe_possible_actor_unit(nmap, action_id(paction), punit)) {
847 return TRUE;
848 }
849 } action_by_result_iterate_end;
850 action_by_result_iterate(paction, ACTRES_PARADROP_CONQUER) {
851 if (action_maybe_possible_actor_unit(nmap, action_id(paction), punit)) {
852 return TRUE;
853 }
854 } action_by_result_iterate_end;
855
856 return FALSE;
857}
858
859/**********************************************************************/
862 bool can_unit_continue_current_activity(const struct civ_map *nmap,
863 struct unit *punit)
864{
865 enum unit_activity current = punit->activity;
866 struct extra_type *target = punit->activity_target;
867 enum unit_activity current2 =
868 (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
869 bool result;
870
871 punit->activity = ACTIVITY_IDLE;
872 punit->activity_target = NULL;
873
874 result = can_unit_do_activity_targeted(nmap, punit, current2, target);
875
876 punit->activity = current;
877 punit->activity_target = target;
878
879 return result;
880}
881
882/**********************************************************************/
889 bool can_unit_do_activity(const struct civ_map *nmap,
890 const struct unit *punit,
891 enum unit_activity activity)
892{
893 struct extra_type *target = NULL;
894
895 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
896 * being able to find out whether an activity is in general possible.
897 * Find one for them, but when they come to do the activity, they will
898 * have to determine the target themselves */
899 {
900 struct tile *ptile = unit_tile(punit);
901
902 if (activity == ACTIVITY_IRRIGATE) {
903 target = next_extra_for_tile(ptile,
904 EC_IRRIGATION,
905 unit_owner(punit),
906 punit);
907 if (NULL == target) {
908 return FALSE; /* No more irrigation extras available. */
909 }
910 } else if (activity == ACTIVITY_MINE) {
911 target = next_extra_for_tile(ptile,
912 EC_MINE,
913 unit_owner(punit),
914 punit);
915 if (NULL == target) {
916 return FALSE; /* No more mine extras available. */
917 }
918 }
919 }
920
921 return can_unit_do_activity_targeted(nmap, punit, activity, target);
922}
923
924/**********************************************************************/
928 bool can_unit_do_activity_targeted(const struct civ_map *nmap,
929 const struct unit *punit,
930 enum unit_activity activity,
931 struct extra_type *target)
932{
933 return can_unit_do_activity_targeted_at(nmap, punit, activity, target,
934 unit_tile(punit));
935}
936
937/**********************************************************************/
941 bool can_unit_do_activity_targeted_at(const struct civ_map *nmap,
942 const struct unit *punit,
943 enum unit_activity activity,
944 struct extra_type *target,
945 const struct tile *ptile)
946{
947 /* Check that no build activity conflicting with one already in progress
948 * gets executed. */
949 /* FIXME: Should check also the cases where one of the activities is terrain
950 * change that destroys the target of the other activity */
951 if (target != NULL && is_build_activity(activity)) {
952 if (tile_is_placing(ptile)) {
953 return FALSE;
954 }
955
956 unit_list_iterate(ptile->units, tunit) {
957 if (is_build_activity(tunit->activity)
958 && !can_extras_coexist(target, tunit->activity_target)) {
959 return FALSE;
960 }
961 } unit_list_iterate_end;
962 }
963
964#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
965{ \
966 switch (action_get_target_kind(paction)) { \
967 case ATK_TILE: \
968 return is_action_enabled_unit_on_tile(nmap, paction->id, \
969 punit, ptile, target); \
970 case ATK_EXTRAS: \
971 return is_action_enabled_unit_on_extras(nmap, paction->id, \
972 punit, ptile, target); \
973 case ATK_CITY: \
974 case ATK_UNIT: \
975 case ATK_UNITS: \
976 case ATK_SELF: \
977 return FALSE; \
978 case ATK_COUNT: \
979 break; /* Handle outside switch */ \
980 } \
981 fc_assert(action_target_kind_is_valid( \
982 action_get_target_kind(paction))); \
983 return FALSE; \
984 }
985
986 switch (activity) {
987 case ACTIVITY_IDLE:
988 case ACTIVITY_GOTO:
989 return TRUE;
990
991 case ACTIVITY_CLEAN:
992 /* The call below doesn't support actor tile speculation. */
993 fc_assert_msg(unit_tile(punit) == ptile,
994 "Please use action_speculate_unit_on_tile()");
995 return is_action_enabled_unit_on_tile(nmap, ACTION_CLEAN,
996 punit, ptile, target);
997
998 case ACTIVITY_MINE:
999 /* The call below doesn't support actor tile speculation. */
1000 fc_assert_msg(unit_tile(punit) == ptile,
1001 "Please use action_speculate_unit_on_tile()");
1002 return is_action_enabled_unit_on_tile(nmap, ACTION_MINE, punit,
1003 ptile, target);
1004
1005 case ACTIVITY_PLANT:
1006 /* The call below doesn't support actor tile speculation. */
1007 fc_assert_msg(unit_tile(punit) == ptile,
1008 "Please use action_speculate_unit_on_tile()");
1009 return is_action_enabled_unit_on_tile(nmap, ACTION_PLANT,
1010 punit, ptile, NULL);
1011
1012 case ACTIVITY_IRRIGATE:
1013 /* The call below doesn't support actor tile speculation. */
1014 fc_assert_msg(unit_tile(punit) == ptile,
1015 "Please use action_speculate_unit_on_tile()");
1016 return is_action_enabled_unit_on_tile(nmap, ACTION_IRRIGATE, punit,
1017 ptile, target);
1018
1019 case ACTIVITY_CULTIVATE:
1020 /* The call below doesn't support actor tile speculation. */
1021 fc_assert_msg(unit_tile(punit) == ptile,
1022 "Please use action_speculate_unit_on_tile()");
1023 return is_action_enabled_unit_on_tile(nmap, ACTION_CULTIVATE,
1024 punit, ptile, NULL);
1025
1026 case ACTIVITY_FORTIFYING:
1027 /* The call below doesn't support actor tile speculation. */
1028 fc_assert_msg(unit_tile(punit) == ptile,
1029 "Please use action_speculate_unit_on_self()");
1030 return is_action_enabled_unit_on_self(ACTION_FORTIFY,
1031 punit);
1032
1033 case ACTIVITY_FORTIFIED:
1034 return FALSE;
1035
1036 case ACTIVITY_BASE:
1037 /* The call below doesn't support actor tile speculation. */
1038 fc_assert_msg(unit_tile(punit) == ptile,
1039 "Please use action_speculate_unit_on_tile()");
1040 return is_action_enabled_unit_on_tile(nmap, ACTION_BASE,
1041 punit, ptile, target);
1042
1043 case ACTIVITY_GEN_ROAD:
1044 /* The call below doesn't support actor tile speculation. */
1045 fc_assert_msg(unit_tile(punit) == ptile,
1046 "Please use action_speculate_unit_on_tile()");
1047 return is_action_enabled_unit_on_tile(nmap, ACTION_ROAD,
1048 punit, ptile, target);
1049
1050 case ACTIVITY_SENTRY:
1051 if (!can_unit_survive_at_tile(nmap, punit, unit_tile(punit))
1052 && !unit_transported(punit)) {
1053 /* Don't let units sentry on tiles they will die on. */
1054 return FALSE;
1055 }
1056 return TRUE;
1057
1058 case ACTIVITY_PILLAGE:
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_ACTIVITY_ENABLED_UNIT_ON(action_by_number(ACTION_PILLAGE));
1063
1064 case ACTIVITY_EXPLORE:
1065 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1066
1067 case ACTIVITY_TRANSFORM:
1068 /* The call below doesn't support actor tile speculation. */
1069 fc_assert_msg(unit_tile(punit) == ptile,
1070 "Please use action_speculate_unit_on_tile()");
1071 return is_action_enabled_unit_on_tile(nmap, ACTION_TRANSFORM_TERRAIN,
1072 punit, ptile, NULL);
1073
1074 case ACTIVITY_CONVERT:
1075 /* The call below doesn't support actor tile speculation. */
1076 fc_assert_msg(unit_tile(punit) == ptile,
1077 "Please use action_speculate_unit_on_self()");
1078 return is_action_enabled_unit_on_self(ACTION_CONVERT, punit);
1079
1080 case ACTIVITY_LAST:
1081 break;
1082 }
1083
1084 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1085 activity);
1086 return FALSE;
1087
1088#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1089}
1090
1091/**********************************************************************/
1094 static void set_unit_activity_internal(struct unit *punit,
1095 enum unit_activity new_activity,
1096 enum gen_action trigger_action)
1097{
1098 punit->activity = new_activity;
1099 punit->action = trigger_action;
1100 punit->activity_count = 0;
1101 punit->activity_target = NULL;
1102 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1103 /* No longer done. */
1104 punit->done_moving = FALSE;
1105 }
1106}
1107
1108/**********************************************************************/
1111 void set_unit_activity(struct unit *punit, enum unit_activity new_activity,
1112 enum gen_action trigger_action)
1113{
1114 fc_assert_ret(!activity_requires_target(new_activity));
1115
1116 if (new_activity == ACTIVITY_FORTIFYING
1117 && punit->changed_from == ACTIVITY_FORTIFIED) {
1118 new_activity = ACTIVITY_FORTIFIED;
1119 }
1120 set_unit_activity_internal(punit, new_activity, trigger_action);
1121 if (new_activity == punit->changed_from) {
1122 punit->activity_count = punit->changed_from_count;
1123 }
1124}
1125
1126/**********************************************************************/
1129 void set_unit_activity_targeted(struct unit *punit,
1130 enum unit_activity new_activity,
1131 struct extra_type *new_target,
1132 enum gen_action trigger_action)
1133{
1134 fc_assert_ret(activity_requires_target(new_activity)
1135 || new_target == NULL);
1136
1137 set_unit_activity_internal(punit, new_activity, trigger_action);
1138 punit->activity_target = new_target;
1139 if (new_activity == punit->changed_from
1140 && new_target == punit->changed_from_target) {
1141 punit->activity_count = punit->changed_from_count;
1142 }
1143}
1144
1145/**********************************************************************/
1148 bool is_unit_activity_on_tile(enum unit_activity activity,
1149 const struct tile *ptile)
1150{
1151 unit_list_iterate(ptile->units, punit) {
1152 if (punit->activity == activity) {
1153 return TRUE;
1154 }
1155 } unit_list_iterate_end;
1156
1157 return FALSE;
1158}
1159
1160/**********************************************************************/
1164 bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
1165{
1166 bv_extras tgt_ret;
1167
1168 BV_CLR_ALL(tgt_ret);
1169 unit_list_iterate(ptile->units, punit) {
1170 if (punit->activity == ACTIVITY_PILLAGE) {
1171 BV_SET(tgt_ret, extra_index(punit->activity_target));
1172 }
1173 } unit_list_iterate_end;
1174
1175 return tgt_ret;
1176}
1177
1178/**********************************************************************/
1181 void unit_activity_astr(const struct unit *punit, struct astring *astr)
1182{
1183 if (!punit || !astr) {
1184 return;
1185 }
1186
1187 switch (punit->activity) {
1188 case ACTIVITY_IDLE:
1189 if (utype_fuel(unit_type_get(punit))) {
1190 int rate, f;
1191
1192 rate = unit_type_get(punit)->move_rate;
1193 f = ((punit->fuel) - 1);
1194
1195 /* Add in two parts as move_points_text() returns ptr to static
1196 * End result: "Moves: (fuel)moves_left" */
1197 astr_add_line(astr, "%s: (%s)", _("Moves"),
1198 move_points_text((rate * f) + punit->moves_left, FALSE));
1199 astr_add(astr, "%s",
1200 move_points_text(punit->moves_left, FALSE));
1201 } else {
1202 astr_add_line(astr, "%s: %s", _("Moves"),
1203 move_points_text(punit->moves_left, FALSE));
1204 }
1205 return;
1206 case ACTIVITY_CLEAN:
1207 case ACTIVITY_TRANSFORM:
1208 case ACTIVITY_FORTIFYING:
1209 case ACTIVITY_FORTIFIED:
1210 case ACTIVITY_SENTRY:
1211 case ACTIVITY_GOTO:
1212 case ACTIVITY_EXPLORE:
1213 case ACTIVITY_CONVERT:
1214 case ACTIVITY_CULTIVATE:
1215 case ACTIVITY_PLANT:
1216 astr_add_line(astr, "%s", get_activity_text(punit->activity));
1217 return;
1218 case ACTIVITY_MINE:
1219 case ACTIVITY_IRRIGATE:
1220 if (punit->activity_target == NULL) {
1221 astr_add_line(astr, "%s", get_activity_text(punit->activity));
1222 } else {
1223 astr_add_line(astr, "Building %s",
1224 extra_name_translation(punit->activity_target));
1225 }
1226 return;
1227 case ACTIVITY_PILLAGE:
1228 if (punit->activity_target != NULL) {
1229 astr_add_line(astr, "%s: %s", get_activity_text(punit->activity),
1230 extra_name_translation(punit->activity_target));
1231 } else {
1232 astr_add_line(astr, "%s", get_activity_text(punit->activity));
1233 }
1234 return;
1235 case ACTIVITY_BASE:
1236 astr_add_line(astr, "%s: %s", get_activity_text(punit->activity),
1237 extra_name_translation(punit->activity_target));
1238 return;
1239 case ACTIVITY_GEN_ROAD:
1240 astr_add_line(astr, "%s: %s", get_activity_text(punit->activity),
1241 extra_name_translation(punit->activity_target));
1242 return;
1243 case ACTIVITY_LAST:
1244 break;
1245 }
1246
1247 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1248 punit->activity, unit_rule_name(punit), punit->id, __FUNCTION__);
1249}
1250
1251/**********************************************************************/
1257 void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1258{
1259 if (!punit || !astr) {
1260 return;
1261 }
1262
1263 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1264 punit->upkeep[O_FOOD], punit->upkeep[O_SHIELD],
1265 punit->upkeep[O_GOLD]);
1266}
1267
1268/**********************************************************************/
1271 struct player *unit_nationality(const struct unit *punit)
1272{
1273 fc_assert_ret_val(NULL != punit, NULL);
1274 return punit->nationality;
1275}
1276
1277/**********************************************************************/
1281 void unit_tile_set(struct unit *punit, struct tile *ptile)
1282{
1283 fc_assert_ret(NULL != punit);
1284 punit->tile = ptile;
1285}
1286
1287/**********************************************************************/
1294 struct unit *tile_allied_unit(const struct tile *ptile,
1295 const struct player *pplayer)
1296{
1297 struct unit *punit = NULL;
1298
1299 unit_list_iterate(ptile->units, cunit) {
1300 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1301 punit = cunit;
1302 } else {
1303 return NULL;
1304 }
1305 }
1306 unit_list_iterate_end;
1307
1308 return punit;
1309}
1310
1311/**********************************************************************/
1317 struct unit *tile_enemy_unit(const struct tile *ptile,
1318 const struct player *pplayer)
1319{
1320 unit_list_iterate(ptile->units, punit) {
1321 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1322 return punit;
1323 }
1324 } unit_list_iterate_end;
1325
1326 return NULL;
1327}
1328
1329/**********************************************************************/
1332 struct unit *tile_non_allied_unit(const struct tile *ptile,
1333 const struct player *pplayer,
1334 bool everyone_non_allied)
1335{
1336 unit_list_iterate(ptile->units, punit) {
1337 struct player *owner = unit_owner(punit);
1338
1339 if (everyone_non_allied && owner != pplayer) {
1340 return punit;
1341 }
1342
1343 if (!pplayers_allied(owner, pplayer)
1344 || is_flagless_to_player(punit, pplayer)) {
1345 return punit;
1346 }
1347 }
1348 unit_list_iterate_end;
1349
1350 return NULL;
1351}
1352
1353/**********************************************************************/
1357 struct unit *tile_other_players_unit(const struct tile *ptile,
1358 const struct player *pplayer)
1359{
1360 unit_list_iterate(ptile->units, punit) {
1361 if (unit_owner(punit) != pplayer) {
1362 return punit;
1363 }
1364 } unit_list_iterate_end;
1365
1366 return NULL;
1367}
1368
1369/**********************************************************************/
1373 struct unit *tile_non_attack_unit(const struct tile *ptile,
1374 const struct player *pplayer)
1375{
1376 unit_list_iterate(ptile->units, punit) {
1377 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1378 return punit;
1379 }
1380 }
1381 unit_list_iterate_end;
1382
1383 return NULL;
1384}
1385
1386/**********************************************************************/
1394 struct unit *unit_occupies_tile(const struct tile *ptile,
1395 const struct player *pplayer)
1396{
1397 unit_list_iterate(ptile->units, punit) {
1398 if (!is_occupying_unit(punit)) {
1399 continue;
1400 }
1401
1402 if (uclass_has_flag(unit_class_get(punit), UCF_DOESNT_OCCUPY_TILE)) {
1403 continue;
1404 }
1405
1406 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1407 return punit;
1408 }
1409 } unit_list_iterate_end;
1410
1411 return NULL;
1412}
1413
1414/**********************************************************************/
1421 bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1422 const struct civ_map *zmap)
1423{
1424 struct extra_type_list *zoccers = extra_type_list_of_zoccers();
1425
1426 square_iterate(zmap, ptile0, 1, ptile) {
1427 struct terrain *pterrain;
1428 struct city *pcity;
1429
1430 pterrain = tile_terrain(ptile);
1431 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1432 continue;
1433 }
1434
1435 pcity = tile_non_allied_city(ptile, pplayer);
1436 if (pcity != NULL) {
1437 if (unit_list_size(ptile->units) > 0) {
1438 /* Occupied enemy city, it doesn't matter if units inside have
1439 * UTYF_NOZOC or not. */
1440 return FALSE;
1441 }
1442 } else {
1443 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1444 extra_type_list_iterate(zoccers, pextra) {
1445 if (tile_has_extra(ptile, pextra)) {
1446 return FALSE;
1447 }
1448 } extra_type_list_iterate_end;
1449 }
1450
1451 unit_list_iterate(ptile->units, punit) {
1452 if (!pplayers_allied(unit_owner(punit), pplayer)
1453 && !unit_has_type_flag(punit, UTYF_NOZOC)
1454 && !unit_transported_server(punit)) {
1455 bool hidden = FALSE;
1456
1457 /* We do NOT check the possibility that player is allied with an extra owner,
1458 * and should thus see inside the extra.
1459 * This is to avoid the situation where having an alliance with third player
1460 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1461 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1462 if (tile_has_extra(ptile, pextra)) {
1463 hidden = TRUE;
1464 break;
1465 }
1466 } extra_type_list_iterate_end;
1467
1468 if (!hidden) {
1469 return FALSE;
1470 }
1471 }
1472 } unit_list_iterate_end;
1473 }
1474 } square_iterate_end;
1475
1476 return TRUE;
1477}
1478
1479/**********************************************************************/
1490 bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1491 const struct civ_map *zmap)
1492{
1493 struct extra_type_list *zoccers = extra_type_list_of_zoccers();
1494
1495 square_iterate(zmap, ptile0, 1, ptile) {
1496 struct terrain *pterrain;
1497 struct city *pcity;
1498
1499 pterrain = tile_terrain(ptile);
1500 if (T_UNKNOWN == pterrain
1501 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1502 continue;
1503 }
1504
1505 pcity = tile_non_allied_city(ptile, pplayer);
1506 if (pcity != NULL) {
1507 if (pcity->client.occupied
1508 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1509 /* Occupied enemy city, it doesn't matter if units inside have
1510 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1511 return FALSE;
1512 }
1513 } else {
1514 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1515 extra_type_list_iterate(zoccers, pextra) {
1516 if (tile_has_extra(ptile, pextra)) {
1517 return FALSE;
1518 }
1519 } extra_type_list_iterate_end;
1520 }
1521
1522 unit_list_iterate(ptile->units, punit) {
1523 if (!unit_transported_client(punit)
1524 && !pplayers_allied(unit_owner(punit), pplayer)
1525 && !unit_has_type_flag(punit, UTYF_NOZOC)) {
1526 return FALSE;
1527 }
1528 } unit_list_iterate_end;
1529 }
1530 } square_iterate_end;
1531
1532 return TRUE;
1533}
1534
1535/**********************************************************************/
1538 bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
1539{
1540 return (!uclass_has_flag(utype_class(punittype), UCF_ZOC)
1541 || utype_has_flag(punittype, UTYF_IGZOC));
1542}
1543
1544/**********************************************************************/
1552 bool unit_being_aggressive(const struct unit *punit)
1553{
1554 const struct tile *ptile = unit_tile(punit);
1555 int max_friendliness_range;
1556
1557 if (!is_attack_unit(punit)) {
1558 return FALSE;
1559 }
1560 if (tile_city(ptile)) {
1561 return FALSE;
1562 }
1563 if (BORDERS_DISABLED != game.info.borders) {
1564 switch (game.info.happyborders) {
1565 case HB_DISABLED:
1566 break;
1567 case HB_NATIONAL:
1568 if (tile_owner(ptile) == unit_owner(punit)) {
1569 return FALSE;
1570 }
1571 break;
1572 case HB_ALLIANCE:
1573 if (pplayers_allied(tile_owner(ptile), unit_owner(punit))) {
1574 return FALSE;
1575 }
1576 break;
1577 }
1578 }
1579
1580 max_friendliness_range = tile_has_not_aggressive_extra_for_unit(ptile,
1581 unit_type_get(punit));
1582 if (max_friendliness_range >= 0) {
1583 return !is_unit_near_a_friendly_city(punit, max_friendliness_range);
1584 }
1585
1586 return TRUE;
1587}
1588
1589/**********************************************************************/
1592 bool is_build_activity(enum unit_activity activity)
1593{
1594 switch (activity) {
1595 case ACTIVITY_MINE:
1596 case ACTIVITY_IRRIGATE:
1597 case ACTIVITY_BASE:
1598 case ACTIVITY_GEN_ROAD:
1599 return TRUE;
1600 default:
1601 return FALSE;
1602 }
1603}
1604
1605/**********************************************************************/
1608 bool is_clean_activity(enum unit_activity activity)
1609{
1610 switch (activity) {
1611 case ACTIVITY_PILLAGE:
1612 case ACTIVITY_CLEAN:
1613 return TRUE;
1614 default:
1615 return FALSE;
1616 }
1617}
1618
1619/**********************************************************************/
1622 bool is_terrain_change_activity(enum unit_activity activity)
1623{
1624 switch (activity) {
1625 case ACTIVITY_CULTIVATE:
1626 case ACTIVITY_PLANT:
1627 case ACTIVITY_TRANSFORM:
1628 return TRUE;
1629 default:
1630 return FALSE;
1631 }
1632}
1633
1634/**********************************************************************/
1637 bool is_tile_activity(enum unit_activity activity)
1638{
1639 return is_build_activity(activity)
1640 || is_clean_activity(activity)
1641 || is_terrain_change_activity(activity);
1642}
1643
1644/**********************************************************************/
1647 bool is_targeted_activity(enum unit_activity activity)
1648{
1649 return is_build_activity(activity)
1650 || is_clean_activity(activity);
1651}
1652
1653/**********************************************************************/
1657 struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1658 const struct unit_type *punittype,
1659 int veteran_level)
1660{
1661 /* Make sure that contents of unit structure are correctly initialized,
1662 * if you ever allocate it by some other mean than fc_calloc() */
1663 struct unit *punit = fc_calloc(1, sizeof(*punit));
1664 int max_vet_lvl;
1665
1666 /* It does not register the unit so the id is set to 0. */
1667 punit->id = IDENTITY_NUMBER_ZERO;
1668
1669 fc_assert_ret_val(punittype != nullptr, nullptr); /* No untyped units! */
1670 punit->utype = punittype;
1671
1672 fc_assert_ret_val(!is_server() || pplayer != nullptr, nullptr); /* No unowned units! */
1673 punit->owner = pplayer;
1674 punit->nationality = pplayer;
1675
1676 punit->refcount = 1;
1677 punit->facing = rand_direction();
1678
1679 if (pcity != nullptr) {
1680 unit_tile_set(punit, pcity->tile);
1681 punit->homecity = pcity->id;
1682 } else {
1683 unit_tile_set(punit, nullptr);
1684 punit->homecity = IDENTITY_NUMBER_ZERO;
1685 }
1686
1687 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1688 punit->goto_tile = nullptr;
1689 max_vet_lvl = utype_veteran_levels(punittype) - 1;
1690 punit->veteran = MIN(veteran_level, max_vet_lvl);
1691 /* A unit new and fresh ... */
1692 punit->fuel = utype_fuel(unit_type_get(punit));
1693 punit->hp = unit_type_get(punit)->hp;
1694 if (utype_has_flag(punittype, UTYF_RANDOM_MOVEMENT)) {
1695 /* Random moves units start with zero movement as their first movement
1696 * will be only after their moves have been reset in the beginning of
1697 * the next turn. */
1698 punit->moves_left = 0;
1699 } else {
1700 punit->moves_left = unit_move_rate(punit);
1701 }
1702 punit->moved = FALSE;
1703
1704 punit->ssa_controller = SSA_NONE;
1705 punit->paradropped = FALSE;
1706 punit->done_moving = FALSE;
1707
1708 punit->transporter = nullptr;
1709 punit->transporting = unit_list_new();
1710
1711 punit->carrying = nullptr;
1712
1713 set_unit_activity(punit, ACTIVITY_IDLE, ACTION_NONE);
1714 punit->battlegroup = BATTLEGROUP_NONE;
1715 punit->has_orders = FALSE;
1716
1717 punit->action_decision_want = ACT_DEC_NOTHING;
1718 punit->action_decision_tile = nullptr;
1719
1720 punit->stay = FALSE;
1721
1722 punit->birth_turn = game.info.turn;
1723 punit->current_form_turn = game.info.turn;
1724
1725 if (is_server()) {
1726 punit->server.debug = FALSE;
1727
1728 punit->server.dying = FALSE;
1729
1730 punit->server.removal_callback = nullptr;
1731
1732 memset(punit->server.upkeep_paid, 0,
1733 O_LAST * sizeof(*punit->server.upkeep_paid));
1734
1735 punit->server.ord_map = 0;
1736 punit->server.ord_city = 0;
1737
1738 punit->server.vision = nullptr; /* No vision. */
1739 punit->server.action_timestamp = 0;
1740 /* Must be an invalid turn number, and an invalid previous turn
1741 * number. */
1742 punit->server.action_turn = -2;
1743 /* punit->server.moving = NULL; set by fc_calloc(). */
1744
1745 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1746
1747 CALL_FUNC_EACH_AI(unit_alloc, punit);
1748 } else {
1749 punit->client.focus_status = FOCUS_AVAIL;
1750 punit->client.transported_by = -1;
1751 punit->client.colored = FALSE;
1752 punit->client.act_prob_cache = nullptr;
1753 }
1754
1755 return punit;
1756}
1757
1758/**********************************************************************/
1762 void unit_virtual_destroy(struct unit *punit)
1763{
1764 free_unit_orders(punit);
1765
1766 /* Unload unit if transported. */
1767 unit_transport_unload(punit);
1768 fc_assert(!unit_transported(punit));
1769
1770 /* Check for transported units. Use direct access to the list. */
1771 if (unit_list_size(punit->transporting) != 0) {
1772 /* Unload all units. */
1773 unit_list_iterate_safe(punit->transporting, pcargo) {
1774 unit_transport_unload(pcargo);
1775 } unit_list_iterate_safe_end;
1776 }
1777 fc_assert(unit_list_size(punit->transporting) == 0);
1778
1779 if (punit->transporting) {
1780 unit_list_destroy(punit->transporting);
1781 }
1782
1783 CALL_FUNC_EACH_AI(unit_free, punit);
1784
1785 if (is_server() && punit->server.adv) {
1786 FC_FREE(punit->server.adv);
1787 } else {
1788 if (punit->client.act_prob_cache) {
1789 FC_FREE(punit->client.act_prob_cache);
1790 }
1791 }
1792
1793 if (--punit->refcount <= 0) {
1794 FC_FREE(punit);
1795 }
1796}
1797
1798/**********************************************************************/
1802 void free_unit_orders(struct unit *punit)
1803{
1804 if (punit->has_orders) {
1805 punit->goto_tile = NULL;
1806 free(punit->orders.list);
1807 punit->orders.list = NULL;
1808 }
1809 punit->orders.length = 0;
1810 punit->has_orders = FALSE;
1811}
1812
1813/**********************************************************************/
1816 int get_transporter_occupancy(const struct unit *ptrans)
1817{
1818 fc_assert_ret_val(ptrans, -1);
1819
1820 return unit_list_size(ptrans->transporting);
1821}
1822
1823/**********************************************************************/
1826 static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1827 const struct tile *ptile,
1828 bool (*unit_load_test)
1829 (const struct unit *pc,
1830 const struct unit *pt))
1831{
1832 struct unit *best_trans = NULL;
1833 struct {
1834 bool has_orders, is_idle, can_freely_unload;
1835 int depth, outermost_moves_left, total_moves;
1836 } cur, best = { FALSE };
1837
1838 unit_list_iterate(ptile->units, ptrans) {
1839 if (!unit_load_test(pcargo, ptrans)) {
1840 continue;
1841 } else if (best_trans == NULL) {
1842 best_trans = ptrans;
1843 }
1844
1845 /* Gather data from transport stack in a single pass, for use in
1846 * various conditions below. */
1847 cur.has_orders = unit_has_orders(ptrans);
1848 cur.outermost_moves_left = ptrans->moves_left;
1849 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1850 unit_transports_iterate(ptrans, ptranstrans) {
1851 if (unit_has_orders(ptranstrans)) {
1852 cur.has_orders = TRUE;
1853 }
1854 cur.outermost_moves_left = ptranstrans->moves_left;
1855 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1856 } unit_transports_iterate_end;
1857
1858 /* Criteria for deciding the 'best' transport to load onto.
1859 * The following tests are applied in order; earlier ones have
1860 * lexicographically greater significance than later ones. */
1861
1862 /* Transports which have orders, or are on transports with orders,
1863 * are less preferable to transport stacks without orders (to
1864 * avoid loading on units that are just passing through). */
1865 if (best_trans != ptrans) {
1866 if (!cur.has_orders && best.has_orders) {
1867 best_trans = ptrans;
1868 } else if (cur.has_orders && !best.has_orders) {
1869 continue;
1870 }
1871 }
1872
1873 /* Else, transports which are idle are preferable (giving players
1874 * some control over loading) -- this does not check transports
1875 * of transports. */
1876 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1877 if (best_trans != ptrans) {
1878 if (cur.is_idle && !best.is_idle) {
1879 best_trans = ptrans;
1880 } else if (!cur.is_idle && best.is_idle) {
1881 continue;
1882 }
1883 }
1884
1885 /* Else, transports from which the cargo could unload at any time
1886 * are preferable to those where the cargo can only disembark in
1887 * cities/bases. */
1888 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1889 unit_type_get(ptrans));
1890 if (best_trans != ptrans) {
1891 if (cur.can_freely_unload && !best.can_freely_unload) {
1892 best_trans = ptrans;
1893 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1894 continue;
1895 }
1896 }
1897
1898 /* Else, transports which are less deeply nested are preferable. */
1899 cur.depth = unit_transport_depth(ptrans);
1900 if (best_trans != ptrans) {
1901 if (cur.depth < best.depth) {
1902 best_trans = ptrans;
1903 } else if (cur.depth > best.depth) {
1904 continue;
1905 }
1906 }
1907
1908 /* Else, transport stacks where the outermost transport has more
1909 * moves left are preferable (on the assumption that it's the
1910 * outermost transport that's about to move). */
1911 if (best_trans != ptrans) {
1912 if (cur.outermost_moves_left > best.outermost_moves_left) {
1913 best_trans = ptrans;
1914 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1915 continue;
1916 }
1917 }
1918
1919 /* All other things being equal, as a tie-breaker, compare the total
1920 * moves left (this turn) and move rate (future turns) for the whole
1921 * stack, to take into account total potential movement for both
1922 * short and long journeys (we don't know which the cargo intends to
1923 * make). Doesn't try to account for whether transports can unload,
1924 * etc. */
1925 if (best_trans != ptrans) {
1926 if (cur.total_moves > best.total_moves) {
1927 best_trans = ptrans;
1928 } else {
1929 continue;
1930 }
1931 }
1932
1933 fc_assert(best_trans == ptrans);
1934 best = cur;
1935 } unit_list_iterate_end;
1936
1937 return best_trans;
1938}
1939
1940/**********************************************************************/
1945 struct unit *transporter_for_unit(const struct unit *pcargo)
1946{
1947 return base_transporter_for_unit(pcargo, unit_tile(pcargo), can_unit_load);
1948}
1949
1950/**********************************************************************/
1955 struct unit *transporter_for_unit_at(const struct unit *pcargo,
1956 const struct tile *ptile)
1957{
1958 return base_transporter_for_unit(pcargo, ptile, could_unit_load);
1959}
1960
1961/**********************************************************************/
1965 static bool can_type_transport_units_cargo(const struct unit_type *utype,
1966 const struct unit *punit)
1967{
1968 if (get_transporter_occupancy(punit) > utype->transport_capacity) {
1969 return FALSE;
1970 }
1971
1972 unit_list_iterate(punit->transporting, pcargo) {
1973 if (!can_unit_type_transport(utype, unit_class_get(pcargo))) {
1974 return FALSE;
1975 }
1976 } unit_list_iterate_end;
1977
1978 return TRUE;
1979}
1980
1981/**********************************************************************/
1988enum unit_upgrade_result
1989unit_transform_result(const struct civ_map *nmap,
1990 const struct unit *punit,
1991 const struct unit_type *to_unittype)
1992{
1993 fc_assert_ret_val(NULL != to_unittype, UU_NO_UNITTYPE);
1994
1995 if (!can_type_transport_units_cargo(to_unittype, punit)) {
1996 return UU_NOT_ENOUGH_ROOM;
1997 }
1998
1999 if (punit->transporter != NULL) {
2000 if (!can_unit_type_transport(unit_type_get(punit->transporter),
2001 utype_class(to_unittype))) {
2002 return UU_UNSUITABLE_TRANSPORT;
2003 }
2004 } else if (!can_exist_at_tile(nmap, to_unittype, unit_tile(punit))) {
2005 /* The new unit type can't survive on this terrain. */
2006 return UU_NOT_TERRAIN;
2007 }
2008
2009 return UU_OK;
2010}
2011
2012/**********************************************************************/
2021 enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap,
2022 const struct unit *punit,
2023 bool is_free)
2024{
2025 struct player *pplayer = unit_owner(punit);
2026 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2027 unit_type_get(punit));
2028 struct city *pcity;
2029 int cost;
2030
2031 if (!to_unittype) {
2032 return UU_NO_UNITTYPE;
2033 }
2034
2035 if (punit->activity == ACTIVITY_CONVERT) {
2036 /* TODO: There may be other activities that the upgraded unit is not
2037 allowed to do, which we could also test.
2038 -
2039 If convert were legal for new unit_type we could allow, but then it
2040 has a 'head start' getting activity time from the old conversion. */
2041 return UU_NOT_ACTIVITY;
2042 }
2043
2044 if (!is_free) {
2045 cost = unit_upgrade_price(pplayer, unit_type_get(punit), to_unittype);
2046 if (pplayer->economic.gold < cost) {
2047 return UU_NO_MONEY;
2048 }
2049
2050 pcity = tile_city(unit_tile(punit));
2051 if (!pcity) {
2052 return UU_NOT_IN_CITY;
2053 }
2054 if (city_owner(pcity) != pplayer) {
2055 /* TODO: Should upgrades in allied cities be possible? */
2056 return UU_NOT_CITY_OWNER;
2057 }
2058 }
2059
2060 /* TODO: Allow transported units to be reassigned. Check here
2061 * and make changes to upgrade_unit. */
2062 return unit_transform_result(nmap, punit, to_unittype);
2063}
2064
2065/**********************************************************************/
2068 bool unit_can_convert(const struct civ_map *nmap,
2069 const struct unit *punit)
2070{
2071 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2072
2073 if (tgt == NULL) {
2074 return FALSE;
2075 }
2076
2077 return UU_OK == unit_transform_result(nmap, punit, tgt);
2078}
2079
2080/**********************************************************************/
2084 enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap,
2085 const struct unit *punit,
2086 char *buf, size_t bufsz)
2087{
2088 struct player *pplayer = unit_owner(punit);
2089 enum unit_upgrade_result result = unit_upgrade_test(nmap, punit, FALSE);
2090 int upgrade_cost;
2091 const struct unit_type *from_unittype = unit_type_get(punit);
2092 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2093 unit_type_get(punit));
2094 char tbuf[MAX_LEN_MSG];
2095
2096 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2097 "Treasury contains %d gold.",
2098 pplayer->economic.gold),
2099 pplayer->economic.gold);
2100
2101 switch (result) {
2102 case UU_OK:
2103 upgrade_cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
2104 /* This message is targeted toward the GUI callers. */
2105 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2106 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2107 "Upgrade %s to %s for %d gold?\n%s",
2108 upgrade_cost),
2109 utype_name_translation(from_unittype),
2110 utype_name_translation(to_unittype),
2111 upgrade_cost, tbuf);
2112 break;
2113 case UU_NO_UNITTYPE:
2114 fc_snprintf(buf, bufsz,
2115 _("Sorry, cannot upgrade %s (yet)."),
2116 utype_name_translation(from_unittype));
2117 break;
2118 case UU_NO_MONEY:
2119 upgrade_cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
2120 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2121 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2122 "Upgrading %s to %s costs %d gold.\n%s",
2123 upgrade_cost),
2124 utype_name_translation(from_unittype),
2125 utype_name_translation(to_unittype),
2126 upgrade_cost, tbuf);
2127 break;
2128 case UU_NOT_IN_CITY:
2129 case UU_NOT_CITY_OWNER:
2130 fc_snprintf(buf, bufsz,
2131 _("You can only upgrade units in your cities."));
2132 break;
2133 case UU_NOT_ENOUGH_ROOM:
2134 fc_snprintf(buf, bufsz,
2135 _("Upgrading this %s would strand units it transports."),
2136 utype_name_translation(from_unittype));
2137 break;
2138 case UU_NOT_TERRAIN:
2139 fc_snprintf(buf, bufsz,
2140 _("Upgrading this %s would result in a %s which can not "
2141 "survive at this place."),
2142 utype_name_translation(from_unittype),
2143 utype_name_translation(to_unittype));
2144 break;
2145 case UU_UNSUITABLE_TRANSPORT:
2146 fc_snprintf(buf, bufsz,
2147 _("Upgrading this %s would result in a %s which its "
2148 "current transport, %s, could not transport."),
2149 utype_name_translation(from_unittype),
2150 utype_name_translation(to_unittype),
2151 unit_name_translation(punit->transporter));
2152 break;
2153 case UU_NOT_ACTIVITY:
2154 fc_snprintf(buf, bufsz,
2155 _("Cannot upgrade %s while doing '%s'."),
2156 utype_name_translation(from_unittype),
2157 unit_activity_name(punit->activity));
2158 break;
2159 }
2160
2161 return result;
2162}
2163
2164/**********************************************************************/
2168 int unit_pays_mp_for_action(const struct action *paction,
2169 const struct unit *punit)
2170{
2171 int mpco;
2172
2173 mpco = get_target_bonus_effects(NULL,
2174 &(const struct req_context) {
2175 .player = unit_owner(punit),
2176 .city = unit_tile(punit)
2177 ? tile_city(unit_tile(punit))
2178 : NULL,
2179 .tile = unit_tile(punit),
2180 .unit = punit,
2181 .unittype = unit_type_get(punit),
2182 .action = paction,
2183 },
2184 NULL,
2185 EFT_ACTION_SUCCESS_MOVE_COST);
2186
2187 mpco += utype_pays_mp_for_action_base(paction, unit_type_get(punit));
2188
2189 return mpco;
2190}
2191
2192/**********************************************************************/
2195 int hp_gain_coord(const struct unit *punit)
2196{
2197 int hp = 0;
2198 const int base = unit_type_get(punit)->hp;
2199 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2200
2201 /* Includes barracks (100%), fortress (25%), etc. */
2202 hp += base * get_unit_bonus(punit, EFT_HP_REGEN) / 100;
2203
2204 /* Minimum HP after regen effects applied. */
2205 hp = MAX(hp, min);
2206
2207 /* Regen2 effects that apply after there's at least Min HP */
2208 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2209
2210 return MAX(hp, 0);
2211}
2212
2213/**********************************************************************/
2216 int unit_gain_hitpoints(const struct unit *punit)
2217{
2218 const struct unit_type *utype = unit_type_get(punit);
2219 struct unit_class *pclass = unit_class_get(punit);
2220 struct city *pcity = tile_city(unit_tile(punit));
2221 int gain;
2222
2223 if (!punit->moved) {
2224 gain = hp_gain_coord(punit);
2225 } else {
2226 gain = 0;
2227 }
2228
2229 gain += get_unit_bonus(punit, EFT_UNIT_RECOVER);
2230
2231 if (!punit->homecity && 0 < game.server.killunhomed
2232 && !unit_has_type_flag(punit, UTYF_GAMELOSS)) {
2233 /* Hit point loss of units without homecity; at least 1 hp! */
2234 /* Gameloss units are immune to this effect. */
2235 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2236
2237 if (gain > hp_loss) {
2238 gain = -1;
2239 } else {
2240 gain -= hp_loss;
2241 }
2242 }
2243
2244 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2245 && !unit_transported(punit)) {
2246 gain -= utype->hp * pclass->hp_loss_pct / 100;
2247 }
2248
2249 if (punit->hp + gain > utype->hp) {
2250 gain = utype->hp - punit->hp;
2251 } else if (punit->hp + gain < 0) {
2252 gain = -punit->hp;
2253 }
2254
2255 return gain;
2256}
2257
2258/**********************************************************************/
2261 bool is_losing_hp(const struct unit *punit)
2262{
2263 const struct unit_type *punittype = unit_type_get(punit);
2264
2265 return get_unit_bonus(punit, EFT_UNIT_RECOVER)
2266 < (punittype->hp *
2267 utype_class(punittype)->hp_loss_pct / 100);
2268}
2269
2270/**********************************************************************/
2273 bool unit_type_is_losing_hp(const struct player *pplayer,
2274 const struct unit_type *punittype)
2275{
2276 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2277 EFT_UNIT_RECOVER)
2278 < (punittype->hp *
2279 utype_class(punittype)->hp_loss_pct / 100);
2280}
2281
2282/**********************************************************************/
2286 bool unit_is_alive(int id)
2287{
2288 /* Check if unit exist in game */
2289 if (game_unit_by_number(id)) {
2290 return TRUE;
2291 }
2292
2293 return FALSE;
2294}
2295
2296/**********************************************************************/
2303 bool unit_is_virtual(const struct unit *punit)
2304{
2305 if (!punit) {
2306 return FALSE;
2307 }
2308
2309 return punit != game_unit_by_number(punit->id);
2310}
2311
2312/**********************************************************************/
2315 void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2316{
2317 return punit->server.ais[ai_type_number(ai)];
2318}
2319
2320/**********************************************************************/
2323 void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2324 void *data)
2325{
2326 punit->server.ais[ai_type_number(ai)] = data;
2327}
2328
2329/**********************************************************************/
2339 int unit_bribe_cost(struct unit *punit, struct player *briber)
2340{
2341 int cost, default_hp;
2342 struct tile *ptile = unit_tile(punit);
2343 struct player *owner = unit_owner(punit);
2344 const struct unit_type *ptype = unit_type_get(punit);
2345
2346 fc_assert_ret_val(punit != NULL, 0);
2347
2348 default_hp = ptype->hp;
2349
2350 cost = game.info.base_bribe_cost;
2351
2352 if (owner != nullptr) {
2353 int dist = 0;
2354
2355 cost += owner->economic.gold;
2356
2357 /* Consider the distance to the capital. */
2358 dist = GAME_UNIT_BRIBE_DIST_MAX;
2359 city_list_iterate(owner->cities, capital) {
2360 if (is_capital(capital)) {
2361 int tmp = map_distance(capital->tile, ptile);
2362
2363 if (tmp < dist) {
2364 dist = tmp;
2365 }
2366 }
2367 } city_list_iterate_end;
2368
2369 cost /= dist + 2;
2370 }
2371
2372 /* Consider the build cost. */
2373 cost *= unit_build_shield_cost_base(punit) / 10.0;
2374
2375 /* Rule set specific cost modification */
2376 cost += (cost
2377 * get_target_bonus_effects(NULL,
2378 &(const struct req_context) {
2379 .player = owner,
2380 .city = game_city_by_number(
2381 punit->homecity
2382 ),
2383 .tile = ptile,
2384 .unit = punit,
2385 .unittype = ptype,
2386 },
2387 briber,
2388 EFT_UNIT_BRIBE_COST_PCT))
2389 / 100;
2390
2391 /* Veterans are not cheap. */
2392 {
2393 const struct veteran_level *vlevel
2394 = utype_veteran_level(ptype, punit->veteran);
2395
2396 fc_assert_ret_val(vlevel != NULL, 0);
2397 cost = cost * vlevel->power_fact / 100;
2398 if (ptype->move_rate > 0) {
2399 cost += cost * vlevel->move_bonus / ptype->move_rate;
2400 } else {
2401 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2402 }
2403 }
2404
2405 /* Cost now contains the basic bribe cost. We now reduce it by:
2406 * bribecost = cost / 2 + cost / 2 * damage / hp
2407 * = cost / 2 * (1 + damage / hp) */
2408 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2409}
2410
2411/**********************************************************************/
2414 bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2415{
2416 fc_assert_ret_val(ptrans != NULL, FALSE);
2417 fc_assert_ret_val(pcargo != NULL, FALSE);
2418
2419 fc_assert_ret_val(!unit_list_search(ptrans->transporting, pcargo), FALSE);
2420
2421 if (force || can_unit_load(pcargo, ptrans)) {
2422 pcargo->transporter = ptrans;
2423 unit_list_append(ptrans->transporting, pcargo);
2424
2425 return TRUE;
2426 }
2427
2428 return FALSE;
2429}
2430
2431/**********************************************************************/
2434 bool unit_transport_unload(struct unit *pcargo)
2435{
2436 struct unit *ptrans;
2437
2438 fc_assert_ret_val(pcargo != NULL, FALSE);
2439
2440 if (!unit_transported(pcargo)) {
2441 /* 'pcargo' is not transported. */
2442 return FALSE;
2443 }
2444
2445 /* Get the transporter; must not be defined on the client! */
2446 ptrans = unit_transport_get(pcargo);
2447 if (ptrans) {
2448 /* 'pcargo' and 'ptrans' should be on the same tile. */
2449 fc_assert(same_pos(unit_tile(pcargo), unit_tile(ptrans)));
2450
2451#ifndef FREECIV_NDEBUG
2452 bool success =
2453#endif
2454 unit_list_remove(ptrans->transporting, pcargo);
2455
2456 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2457 fc_assert(success);
2458 }
2459
2460 /* For the server (also safe for the client). */
2461 pcargo->transporter = NULL;
2462
2463 return TRUE;
2464}
2465
2466/**********************************************************************/
2469 bool unit_transported(const struct unit *pcargo)
2470{
2471 fc_assert_ret_val(pcargo != NULL, FALSE);
2472
2473 /* The unit is transported if a transporter unit is set or, (for the client)
2474 * if the transported_by field is set. */
2475 if (is_server()) {
2476 return unit_transported_server(pcargo);
2477 } else {
2478 return unit_transported_client(pcargo);
2479 }
2480}
2481
2482/**********************************************************************/
2485 struct unit *unit_transport_get(const struct unit *pcargo)
2486{
2487 fc_assert_ret_val(pcargo != NULL, NULL);
2488
2489 return pcargo->transporter;
2490}
2491
2492/**********************************************************************/
2495 struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2496{
2497 fc_assert_ret_val(ptrans != NULL, NULL);
2498 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2499
2500 return ptrans->transporting;
2501}
2502
2503/**********************************************************************/
2506static inline bool
2507unit_transport_check_one(const struct unit_type *cargo_utype,
2508 const struct unit_type *trans_utype)
2509{
2510 return (trans_utype != cargo_utype
2511 && !can_unit_type_transport(cargo_utype,
2512 utype_class(trans_utype)));
2513}
2514
2515/**********************************************************************/
2523 bool unit_transport_check(const struct unit *pcargo,
2524 const struct unit *ptrans)
2525{
2526 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2527
2528 /* Check 'pcargo' against 'ptrans'. */
2529 if (!unit_transport_check_one(cargo_utype, unit_type_get(ptrans))) {
2530 return FALSE;
2531 }
2532
2533 /* Check 'pcargo' against 'ptrans' parents. */
2534 unit_transports_iterate(ptrans, pparent) {
2535 if (!unit_transport_check_one(cargo_utype, unit_type_get(pparent))) {
2536 return FALSE;
2537 }
2538 } unit_transports_iterate_end;
2539
2540 /* Check cargo children... */
2541 unit_cargo_iterate(pcargo, pchild) {
2542 cargo_utype = unit_type_get(pchild);
2543
2544 /* ...against 'ptrans'. */
2545 if (!unit_transport_check_one(cargo_utype, unit_type_get(ptrans))) {
2546 return FALSE;
2547 }
2548
2549 /* ...and against 'ptrans' parents. */
2550 unit_transports_iterate(ptrans, pparent) {
2551 if (!unit_transport_check_one(cargo_utype, unit_type_get(pparent))) {
2552 return FALSE;
2553 }
2554 } unit_transports_iterate_end;
2555 } unit_cargo_iterate_end;
2556
2557 return TRUE;
2558}
2559
2560/**********************************************************************/
2564 bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2565{
2566 unit_transports_iterate(pcargo, plevel) {
2567 if (ptrans == plevel) {
2568 return TRUE;
2569 }
2570 } unit_transports_iterate_end;
2571 return FALSE;
2572}
2573
2574/**********************************************************************/
2577 int unit_cargo_depth(const struct unit *ptrans)
2578{
2579 struct cargo_iter iter;
2580 struct iterator *it;
2581 int depth = 0;
2582
2583 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2584 iterator_next(it)) {
2585 if (iter.depth > depth) {
2586 depth = iter.depth;
2587 }
2588 }
2589 return depth;
2590}
2591
2592/**********************************************************************/
2595 int unit_transport_depth(const struct unit *pcargo)
2596{
2597 int level = 0;
2598
2599 unit_transports_iterate(pcargo, plevel) {
2600 level++;
2601 } unit_transports_iterate_end;
2602 return level;
2603}
2604
2605/**********************************************************************/
2608 size_t cargo_iter_sizeof(void)
2609{
2610 return sizeof(struct cargo_iter);
2611}
2612
2613/**********************************************************************/
2616 static void *cargo_iter_get(const struct iterator *it)
2617{
2618 const struct cargo_iter *iter = CARGO_ITER(it);
2619
2620 return unit_list_link_data(iter->links[iter->depth - 1]);
2621}
2622
2623/**********************************************************************/
2626 static void cargo_iter_next(struct iterator *it)
2627{
2628 struct cargo_iter *iter = CARGO_ITER(it);
2629 const struct unit_list_link *piter;
2630 const struct unit_list_link *pnext;
2631
2632 /* Variant 1: unit has cargo. */
2633 pnext = unit_list_head(unit_transport_cargo(cargo_iter_get(it)));
2634 if (NULL != pnext) {
2635 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2636 iter->links[iter->depth++] = pnext;
2637 return;
2638 }
2639
2640 fc_assert(iter->depth > 0);
2641
2642 while (iter->depth > 0) {
2643 piter = iter->links[iter->depth - 1];
2644
2645 /* Variant 2: there are other cargo units at same level. */
2646 pnext = unit_list_link_next(piter);
2647 if (NULL != pnext) {
2648 iter->links[iter->depth - 1] = pnext;
2649 return;
2650 }
2651
2652 /* Variant 3: return to previous level, and do same tests. */
2653 iter->depth--;
2654 }
2655}
2656
2657/**********************************************************************/
2660 static bool cargo_iter_valid(const struct iterator *it)
2661{
2662 return (0 < CARGO_ITER(it)->depth);
2663}
2664
2665/**********************************************************************/
2668 struct iterator *cargo_iter_init(struct cargo_iter *iter,
2669 const struct unit *ptrans)
2670{
2671 struct iterator *it = ITERATOR(iter);
2672
2673 it->get = cargo_iter_get;
2674 it->next = cargo_iter_next;
2675 it->valid = cargo_iter_valid;
2676 iter->links[0] = unit_list_head(unit_transport_cargo(ptrans));
2677 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2678
2679 return it;
2680}
2681
2682/**********************************************************************/
2685 bool unit_is_cityfounder(const struct unit *punit)
2686{
2687 return utype_is_cityfounder(unit_type_get(punit));
2688}
2689
2690/**********************************************************************/
2693 bool unit_order_list_is_sane(const struct civ_map *nmap,
2694 int length, const struct unit_order *orders)
2695{
2696 int i;
2697
2698 for (i = 0; i < length; i++) {
2699 struct action *paction;
2700 struct extra_type *pextra;
2701
2702 if (orders[i].order > ORDER_LAST) {
2703 log_error("invalid order %d at index %d", orders[i].order, i);
2704 return FALSE;
2705 }
2706 switch (orders[i].order) {
2707 case ORDER_MOVE:
2708 case ORDER_ACTION_MOVE:
2709 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2710 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2711 return FALSE;
2712 }
2713 break;
2714 case ORDER_ACTIVITY:
2715 switch (orders[i].activity) {
2716 case ACTIVITY_SENTRY:
2717 if (i != length - 1) {
2718 /* Only allowed as the last order. */
2719 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2720 i);
2721 return FALSE;
2722 }
2723 break;
2724 /* Replaced by action orders */
2725 case ACTIVITY_BASE:
2726 case ACTIVITY_GEN_ROAD:
2727 case ACTIVITY_CLEAN:
2728 case ACTIVITY_PILLAGE:
2729 case ACTIVITY_MINE:
2730 case ACTIVITY_IRRIGATE:
2731 case ACTIVITY_PLANT:
2732 case ACTIVITY_CULTIVATE:
2733 case ACTIVITY_TRANSFORM:
2734 case ACTIVITY_CONVERT:
2735 case ACTIVITY_FORTIFYING:
2736 log_error("at index %d, use action rather than activity %d.",
2737 i, orders[i].activity);
2738 return FALSE;
2739 /* Not supported. */
2740 case ACTIVITY_EXPLORE:
2741 case ACTIVITY_IDLE:
2742 /* Not set from the client. */
2743 case ACTIVITY_GOTO:
2744 case ACTIVITY_FORTIFIED:
2745 /* Unused. */
2746 case ACTIVITY_LAST:
2747 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2748 return FALSE;
2749 }
2750
2751 break;
2752 case ORDER_PERFORM_ACTION:
2753 if (!action_id_exists(orders[i].action)) {
2754 /* Non-existent action. */
2755 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2756 return FALSE;
2757 }
2758
2759 paction = action_by_number(orders[i].action);
2760
2761 /* Validate main target. */
2762 if (index_to_tile(nmap, orders[i].target) == NULL) {
2763 log_error("at index %d, invalid tile target %d for the action %d.",
2764 i, orders[i].target, orders[i].action);
2765 return FALSE;
2766 }
2767
2768 if (orders[i].dir != DIR8_ORIGIN) {
2769 log_error("at index %d, the action %d sets the outdated target"
2770 " specification dir.",
2771 i, orders[i].action);
2772 }
2773
2774 /* Validate sub target. */
2775 switch (action_id_get_sub_target_kind(orders[i].action)) {
2776 case ASTK_BUILDING:
2777 /* Sub target is a building. */
2778 if (!improvement_by_number(orders[i].sub_target)) {
2779 /* Sub target is invalid. */
2780 log_error("at index %d, cannot do %s without a target.", i,
2781 action_id_rule_name(orders[i].action));
2782 return FALSE;
2783 }
2784 break;
2785 case ASTK_TECH:
2786 /* Sub target is a technology. */
2787 if (orders[i].sub_target == A_NONE
2788 || (!valid_advance_by_number(orders[i].sub_target)
2789 && orders[i].sub_target != A_FUTURE)) {
2790 /* Target tech is invalid. */
2791 log_error("at index %d, cannot do %s without a target.", i,
2792 action_id_rule_name(orders[i].action));
2793 return FALSE;
2794 }
2795 break;
2796 case ASTK_EXTRA:
2797 case ASTK_EXTRA_NOT_THERE:
2798 /* Sub target is an extra. */
2799 pextra = (!(orders[i].sub_target == NO_TARGET
2800 || (orders[i].sub_target < 0
2801 || (orders[i].sub_target
2802 >= game.control.num_extra_types)))
2803 ? extra_by_number(orders[i].sub_target) : NULL);
2804 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2805 if (pextra == NULL) {
2806 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2807 /* Target extra is invalid. */
2808 log_error("at index %d, cannot do %s without a target.", i,
2809 action_id_rule_name(orders[i].action));
2810 return FALSE;
2811 }
2812 } else {
2813 if (!actres_removes_extra(paction->result, pextra)
2814 && !actres_creates_extra(paction->result, pextra)) {
2815 /* Target extra is irrelevant for the action. */
2816 log_error("at index %d, cannot do %s to %s.", i,
2817 action_id_rule_name(orders[i].action),
2818 extra_rule_name(pextra));
2819 return FALSE;
2820 }
2821 }
2822 break;
2823 case ASTK_NONE:
2824 /* No validation required. */
2825 break;
2826 /* Invalid action? */
2827 case ASTK_COUNT:
2828 fc_assert_ret_val_msg(
2829 action_id_get_sub_target_kind(orders[i].action) != ASTK_COUNT,
2830 FALSE,
2831 "Bad action %d in order number %d.", orders[i].action, i);
2832 }
2833
2834 /* Some action orders are sane only in the last order. */
2835 if (i != length - 1) {
2836 /* If the unit is dead, */
2837 if (utype_is_consumed_by_action(paction, NULL)
2838 /* or if Freeciv has no idea where the unit will end up after it
2839 * has performed this action, */
2840 || !(utype_is_unmoved_by_action(paction, NULL)
2841 || utype_is_moved_to_tgt_by_action(paction, NULL))
2842 /* or if the unit will end up standing still, */
2843 || action_has_result(paction, ACTRES_FORTIFY)) {
2844 /* than having this action in the middle of a unit's orders is
2845 * probably wrong. */
2846 log_error("action %d is not allowed at index %d.",
2847 orders[i].action, i);
2848 return FALSE;
2849 }
2850 }
2851
2852 /* Don't validate that the target tile really contains a target or
2853 * that the actor player's map think the target tile has one.
2854 * The player may target something from their player map that isn't
2855 * there any more, a target they think is there even if their player
2856 * map doesn't have it, or even a target they assume will be there
2857 * when the unit reaches the target tile.
2858 *
2859 * With that said: The client should probably at least have an
2860 * option to only aim city targeted actions at cities. */
2861
2862 break;
2863 case ORDER_FULL_MP:
2864 break;
2865 case ORDER_LAST:
2866 /* An invalid order. This is handled above. */
2867 break;
2868 }
2869 }
2870
2871 return TRUE;
2872}
2873
2874/**********************************************************************/
2878 struct unit_order *create_unit_orders(const struct civ_map *nmap,
2879 int length,
2880 const struct unit_order *orders)
2881{
2882 struct unit_order *unit_orders;
2883
2884 if (!unit_order_list_is_sane(nmap, length, orders)) {
2885 return NULL;
2886 }
2887
2888 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2889 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2890
2891 return unit_orders;
2892}
2893
2894/**********************************************************************/
2897 enum gen_action activity_default_action(enum unit_activity act)
2898{
2899 enum gen_action act_act[ACTIVITY_LAST] = {
2900 ACTION_NONE, // ACTIVITY_IDLE
2901 ACTION_CULTIVATE, // ACTIVITY_CULTIVATE
2902 ACTION_MINE, // ACTIVITY_MINE
2903 ACTION_IRRIGATE, // ACTIVITY_IRRIGATE
2904 ACTION_FORTIFY, // ACTIVITY_FORTIFIED
2905 ACTION_NONE, // ACTIVITY_SENTRY
2906 ACTION_PILLAGE, // ACTIVITY_PILLAGE
2907 ACTION_NONE, // ACTIVITY_GOTO
2908 ACTION_NONE, // ACTIVITY_EXPLORE
2909 ACTION_TRANSFORM_TERRAIN, // ACTIVITY_TRANSFORM
2910 ACTION_FORTIFY, // ACTIVITY_FORTIFYING
2911 ACTION_CLEAN, // ACTIVITY_CLEAN
2912 ACTION_BASE, // ACTIVITY_BASE
2913 ACTION_ROAD, // ACTIVITY_GEN_ROAD
2914 ACTION_CONVERT, // ACTIVITY_CONVERT
2915 ACTION_PLANT // ACTIVITY_PLANT
2916 };
2917
2918 return act_act[act];
2919}
action_prob_possible
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5034
is_action_enabled_unit_on_self
bool is_action_enabled_unit_on_self(const action_id wanted_action, const struct unit *actor_unit)
Definition actions.c:3434
action_prob_vs_city
struct act_prob action_prob_vs_city(const struct unit *actor_unit, const action_id act_id, const struct city *target_city)
Definition actions.c:4192
action_prob_vs_unit
struct act_prob action_prob_vs_unit(const struct unit *actor_unit, const action_id act_id, const struct unit *target_unit)
Definition actions.c:4276
action_id_rule_name
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1204
action_prob_vs_tile
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:4553
is_action_enabled_unit_on_tile
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:3285
is_action_enabled_unit_on_city
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:3044
is_action_enabled_unit_on_unit
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:3121
action_maybe_possible_actor_unit
bool action_maybe_possible_actor_unit(const struct civ_map *nmap, const action_id act_id, const struct unit *actor_unit)
Definition actions.c:5450
action_id_exists
bool action_id_exists(const action_id act_id)
Definition actions.c:1033
actions.h
action_by_result_iterate
#define action_by_result_iterate(_paction_, _result_)
Definition actions.h:236
action_id_get_sub_target_kind
#define action_id_get_sub_target_kind(act_id)
Definition actions.h:411
action_by_number
static struct action * action_by_number(action_id act_id)
Definition actions.h:390
action_has_result
#define action_has_result(_act_, _res_)
Definition actions.h:175
action_by_result_iterate_end
#define action_by_result_iterate_end
Definition actions.h:240
action_id
#define action_id(_act_)
Definition actions.h:416
ACTION_NONE
#define ACTION_NONE
Definition actions.h:55
actres_removes_extra
bool actres_removes_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:806
actres_creates_extra
bool actres_creates_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:785
ai_type_number
int ai_type_number(const struct ai_type *ai)
Definition ai.c:278
ai.h
CALL_FUNC_EACH_AI
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:387
astr_add_line
void astr_add_line(struct astring *astr, const char *format,...)
Definition astring.c:283
astr_add
void astr_add(struct astring *astr, const char *format,...)
Definition astring.c:271
astring.h
base.h
bitvector.h
BV_CLR_ALL
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
BV_SET
#define BV_SET(bv, bit)
Definition bitvector.h:81
is_capital
bool is_capital(const struct city *pcity)
Definition city.c:1571
tile_non_allied_city
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2058
is_unit_near_a_friendly_city
bool is_unit_near_a_friendly_city(const struct unit *punit, int distance)
Definition city.c:2073
city.h
city_list_iterate
#define city_list_iterate(citylist, pcity)
Definition city.h:508
city_tile
#define city_tile(_pcity_)
Definition city.h:564
city_owner
#define city_owner(_pcity_)
Definition city.h:563
city_list_iterate_end
#define city_list_iterate_end
Definition city.h:510
incite_cost
char * incite_cost
Definition comments.c:74
MAX_LEN_MSG
#define MAX_LEN_MSG
Definition conn_types.h:37
base
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:2937
punit
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:74
cost
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit int cost
Definition dialogs_g.h:74
get_target_bonus_effects
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct player *other_player, enum effect_type effect_type)
Definition effects.c:748
get_unit_bonus
int get_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Definition effects.c:1070
get_unittype_bonus
int get_unittype_bonus(const struct player *pplayer, const struct tile *ptile, const struct unit_type *punittype, const struct action *paction, enum effect_type effect_type)
Definition effects.c:1035
next_extra_for_tile
struct extra_type * next_extra_for_tile(const struct tile *ptile, enum extra_cause cause, const struct player *pplayer, const struct unit *punit)
Definition extras.c:765
extra_owner
struct player * extra_owner(const struct tile *ptile)
Definition extras.c:1114
extra_type_list_of_zoccers
struct extra_type_list * extra_type_list_of_zoccers(void)
Definition extras.c:267
zoccers
static struct extra_type_list * zoccers
Definition extras.c:39
extra_by_number
struct extra_type * extra_by_number(int id)
Definition extras.c:183
can_extras_coexist
bool can_extras_coexist(const struct extra_type *pextra1, const struct extra_type *pextra2)
Definition extras.c:1003
extra_rule_name
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:203
extra_name_translation
const char * extra_name_translation(const struct extra_type *pextra)
Definition extras.c:194
extra_type_list_iterate
#define extra_type_list_iterate(extralist, pextra)
Definition extras.h:165
extra_index
#define extra_index(_e_)
Definition extras.h:183
extra_type_list_iterate_end
#define extra_type_list_iterate_end
Definition extras.h:167
is_server
static bool is_server(void)
Definition fc_interface.h:69
NO_TARGET
#define NO_TARGET
Definition fc_types.h:358
Activity_type_id
enum unit_activity Activity_type_id
Definition fc_types.h:383
DIR8_ORIGIN
#define DIR8_ORIGIN
Definition fc_types.h:461
action_id
int action_id
Definition fc_types.h:393
HB_ALLIANCE
@ HB_ALLIANCE
Definition fc_types.h:1303
HB_DISABLED
@ HB_DISABLED
Definition fc_types.h:1301
HB_NATIONAL
@ HB_NATIONAL
Definition fc_types.h:1302
O_SHIELD
@ O_SHIELD
Definition fc_types.h:101
O_FOOD
@ O_FOOD
Definition fc_types.h:101
O_GOLD
@ O_GOLD
Definition fc_types.h:101
O_LAST
@ O_LAST
Definition fc_types.h:101
BORDERS_DISABLED
@ BORDERS_DISABLED
Definition fc_types.h:1047
IDENTITY_NUMBER_ZERO
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:92
fcintl.h
Q_
#define Q_(String)
Definition fcintl.h:70
PL_
#define PL_(String1, String2, n)
Definition fcintl.h:71
_
#define _(String)
Definition fcintl.h:67
game
struct civ_game game
Definition game.c:62
wld
struct world wld
Definition game.c:63
game_unit_by_number
struct unit * game_unit_by_number(int id)
Definition game.c:116
game_city_by_number
struct city * game_city_by_number(int id)
Definition game.c:107
game.h
GAME_UNIT_BRIBE_DIST_MAX
#define GAME_UNIT_BRIBE_DIST_MAX
Definition game.h:752
GAME_TRANSPORT_MAX_RECURSIVE
#define GAME_TRANSPORT_MAX_RECURSIVE
Definition game.h:755
owner
struct city * owner
Definition citydlg.c:220
bufsz
static const int bufsz
Definition helpdlg.c:70
improvement_by_number
struct impr_type * improvement_by_number(const Impr_type_id id)
Definition improvement.c:194
iterator_valid
static bool iterator_valid(const struct iterator *it)
Definition iterator.h:61
ITERATOR
#define ITERATOR(p)
Definition iterator.h:37
iterator_next
static void iterator_next(struct iterator *it)
Definition iterator.h:42
log.h
fc_assert_msg
#define fc_assert_msg(condition, message,...)
Definition log.h:181
fc_assert_ret
#define fc_assert_ret(condition)
Definition log.h:191
fc_assert
#define fc_assert(condition)
Definition log.h:176
fc_assert_ret_val
#define fc_assert_ret_val(condition, val)
Definition log.h:194
log_error
#define log_error(message,...)
Definition log.h:103
fc_assert_ret_val_msg
#define fc_assert_ret_val_msg(condition, val, message,...)
Definition log.h:208
index_to_tile
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:462
same_pos
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:950
map_untrusted_dir_is_valid
bool map_untrusted_dir_is_valid(enum direction8 dir)
Definition map.c:1288
rand_direction
enum direction8 rand_direction(void)
Definition map.c:1958
map_distance
int map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:660
map.h
square_iterate
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:377
square_iterate_end
#define square_iterate_end
Definition map.h:380
mem.h
fc_calloc
#define fc_calloc(n, esz)
Definition mem.h:38
FC_FREE
#define FC_FREE(ptr)
Definition mem.h:41
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
move_points_text
const char * move_points_text(int mp, bool reduce)
Definition movement.c:1016
can_exist_at_tile
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Definition movement.c:289
can_unit_exist_at_tile
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:318
is_native_tile
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:330
can_unit_transport
bool can_unit_transport(const struct unit *transporter, const struct unit *transported)
Definition movement.c:856
unit_move_rate
int unit_move_rate(const struct unit *punit)
Definition movement.c:89
can_unit_survive_at_tile
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:490
is_native_near_tile
bool is_native_near_tile(const struct civ_map *nmap, const struct unit_class *uclass, const struct tile *ptile)
Definition movement.c:464
can_unit_type_transport
bool can_unit_type_transport(const struct unit_type *transporter, const struct unit_class *transported)
Definition movement.c:869
can_attack_non_native
bool can_attack_non_native(const struct unit_type *utype)
Definition movement.c:213
movement.h
SINGLE_MOVE
#define SINGLE_MOVE
Definition movement.h:26
packets.h
can_player_see_unit
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Definition player.c:1104
pplayers_at_war
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
pplayers_allied
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1409
pplayers_non_attack
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1463
player.h
road.h
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
shared.h
ARRAY_SIZE
#define ARRAY_SIZE(x)
Definition shared.h:85
MIN
#define MIN(x, y)
Definition shared.h:55
FC_INFINITY
#define FC_INFINITY
Definition shared.h:36
MAX
#define MAX(x, y)
Definition shared.h:54
action
Definition actions.h:106
ai_type
Definition ai.h:50
astring
Definition astring.h:36
cargo_iter
Definition unit.c:49
cargo_iter::vtable
struct iterator vtable
Definition unit.c:50
cargo_iter::depth
int depth
Definition unit.c:52
cargo_iter::links
const struct unit_list_link * links[GAME_TRANSPORT_MAX_RECURSIVE]
Definition unit.c:51
city
Definition city.h:320
city::id
int id
Definition city.h:326
city::occupied
bool occupied
Definition city.h:460
city::tile
struct tile * tile
Definition city.h:322
city::client
struct city::@17::@20 client
civ_game::control
struct packet_ruleset_control control
Definition game.h:83
civ_game::info
struct packet_game_info info
Definition game.h:89
civ_game::killunhomed
int killunhomed
Definition game.h:154
civ_game::server
struct civ_game::@31::@35 server
civ_map
Definition map_types.h:71
extra_type
Definition extras.h:88
extra_type::ruledit_disabled
bool ruledit_disabled
Definition extras.h:91
iterator
Definition iterator.h:31
iterator::valid
bool(* valid)(const struct iterator *it)
Definition iterator.h:34
iterator::get
void *(* get)(const struct iterator *it)
Definition iterator.h:33
iterator::next
void(* next)(struct iterator *it)
Definition iterator.h:32
packet_game_info::borders
enum borders_mode borders
Definition packets_gen.h:135
packet_game_info::happyborders
enum happyborders_type happyborders
Definition packets_gen.h:183
packet_game_info::airlifting_style
enum airlifting_style airlifting_style
Definition packets_gen.h:124
packet_game_info::airlift_from_always_enabled
bool airlift_from_always_enabled
Definition packets_gen.h:125
packet_game_info::turn
int turn
Definition packets_gen.h:247
packet_game_info::base_bribe_cost
int base_bribe_cost
Definition packets_gen.h:136
packet_game_info::airlift_to_always_enabled
bool airlift_to_always_enabled
Definition packets_gen.h:126
packet_ruleset_control::num_extra_types
int num_extra_types
Definition packets_gen.h:1463
player_economic::gold
int gold
Definition player.h:63
player
Definition player.h:249
player::economic
struct player_economic economic
Definition player.h:284
req_context
Definition requirements.h:100
terrain
Definition terrain.h:75
tile
Definition tile.h:50
tile::units
struct unit_list * units
Definition tile.h:58
unit_class
Definition unittype.h:149
unit_order
Definition unit.h:93
unit_order::sub_target
int sub_target
Definition unit.h:99
unit_type
Definition unittype.h:507
unit_type::transport_capacity
int transport_capacity
Definition unittype.h:530
unit_type::adv
struct unit_type::@87 adv
unit_type::worker
bool worker
Definition unittype.h:582
unit_type::move_rate
int move_rate
Definition unittype.h:524
unit_type::vlayer
enum vision_layer vlayer
Definition unittype.h:576
unit_type::hp
int hp
Definition unittype.h:531
unit_type::converted_to
const struct unit_type * converted_to
Definition unittype.h:537
unit
Definition unit.h:140
unit::action_timestamp
time_t action_timestamp
Definition unit.h:248
unit::length
int length
Definition unit.h:198
unit::upkeep
int upkeep[O_LAST]
Definition unit.h:150
unit::has_orders
bool has_orders
Definition unit.h:196
unit::orders
struct unit::@80 orders
unit::action_decision_want
enum action_decision action_decision_want
Definition unit.h:205
unit::battlegroup
int battlegroup
Definition unit.h:194
unit::activity
enum unit_activity activity
Definition unit.h:159
unit::moves_left
int moves_left
Definition unit.h:152
unit::refcount
int refcount
Definition unit.h:143
unit::client
struct unit::@81::@83 client
unit::id
int id
Definition unit.h:147
unit::action
enum gen_action action
Definition unit.h:160
unit::transporting
struct unit_list * transporting
Definition unit.h:187
unit::ord_city
int ord_city
Definition unit.h:245
unit::moved
bool moved
Definition unit.h:176
unit::ord_map
int ord_map
Definition unit.h:244
unit::debug
bool debug
Definition unit.h:237
unit::vision
struct vision * vision
Definition unit.h:247
unit::hp
int hp
Definition unit.h:153
unit::fuel
int fuel
Definition unit.h:155
unit::changed_from_target
struct extra_type * changed_from_target
Definition unit.h:173
unit::current_form_turn
int current_form_turn
Definition unit.h:211
unit::stay
bool stay
Definition unit.h:208
unit::colored
bool colored
Definition unit.h:225
unit::facing
enum direction8 facing
Definition unit.h:144
unit::server
struct unit::@81::@84 server
unit::tile
struct tile * tile
Definition unit.h:142
unit::activity_target
struct extra_type * activity_target
Definition unit.h:167
unit::act_prob_cache
struct act_prob * act_prob_cache
Definition unit.h:231
unit::activity_count
int activity_count
Definition unit.h:165
unit::list
struct unit_order * list
Definition unit.h:201
unit::changed_from
enum unit_activity changed_from
Definition unit.h:171
unit::adv
struct unit_adv * adv
Definition unit.h:239
unit::nationality
struct player * nationality
Definition unit.h:146
unit::transported_by
int transported_by
Definition unit.h:219
unit::removal_callback
void(* removal_callback)(struct unit *punit)
Definition unit.h:256
unit::ais
void * ais[FREECIV_AI_MOD_LAST]
Definition unit.h:240
unit::action_turn
int action_turn
Definition unit.h:249
unit::homecity
int homecity
Definition unit.h:148
unit::upkeep_paid
int upkeep_paid[O_LAST]
Definition unit.h:259
unit::paradropped
bool paradropped
Definition unit.h:177
unit::done_moving
bool done_moving
Definition unit.h:184
unit::transporter
struct unit * transporter
Definition unit.h:186
unit::birth_turn
int birth_turn
Definition unit.h:210
unit::carrying
struct goods_type * carrying
Definition unit.h:189
unit::goto_tile
struct tile * goto_tile
Definition unit.h:157
unit::action_decision_tile
struct tile * action_decision_tile
Definition unit.h:206
unit::utype
const struct unit_type * utype
Definition unit.h:141
unit::veteran
int veteran
Definition unit.h:154
unit::changed_from_count
int changed_from_count
Definition unit.h:172
unit::owner
struct player * owner
Definition unit.h:145
unit::dying
bool dying
Definition unit.h:253
unit::focus_status
enum unit_focus_status focus_status
Definition unit.h:217
unit::ssa_controller
enum server_side_agent ssa_controller
Definition unit.h:175
veteran_level
Definition unittype.h:493
world::map
struct civ_map map
Definition world_object.h:38
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
support.h
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47
valid_advance_by_number
struct advance * valid_advance_by_number(const Tech_type_id id)
Definition tech.c:176
tech.h
A_FUTURE
#define A_FUTURE
Definition tech.h:46
A_NONE
#define A_NONE
Definition tech.h:43
T_UNKNOWN
#define T_UNKNOWN
Definition terrain.h:62
terrain_has_flag
#define terrain_has_flag(terr, flag)
Definition terrain.h:176
tile_is_placing
bool tile_is_placing(const struct tile *ptile)
Definition tile.c:1131
tile_activity_time
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Definition tile.c:418
tile_has_native_base
bool tile_has_native_base(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:324
tile_has_not_aggressive_extra_for_unit
int tile_has_not_aggressive_extra_for_unit(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:196
tile_get_known
enum known_type tile_get_known(const struct tile *ptile, const struct player *pplayer)
Definition tile.c:392
tile_city
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
TILE_KNOWN_UNSEEN
@ TILE_KNOWN_UNSEEN
Definition tile.h:37
TILE_KNOWN_SEEN
@ TILE_KNOWN_SEEN
Definition tile.h:38
ACTIVITY_FACTOR
#define ACTIVITY_FACTOR
Definition tile.h:166
tile_terrain
#define tile_terrain(_tile)
Definition tile.h:111
tile_has_extra
#define tile_has_extra(ptile, pextra)
Definition tile.h:148
tile_owner
#define tile_owner(_tile)
Definition tile.h:97
can_cities_trade
bool can_cities_trade(const struct city *pc1, const struct city *pc2)
Definition traderoutes.c:214
traderoutes.h
unit_can_do_action_sub_result
bool unit_can_do_action_sub_result(const struct unit *punit, enum action_sub_result sub_result)
Definition unit.c:405
cargo_iter_sizeof
size_t cargo_iter_sizeof(void)
Definition unit.c:2608
can_unit_do_activity_targeted_at
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:941
is_tile_activity
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1637
create_unit_orders
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2878
unit_transform_result
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:1989
unit_being_aggressive
bool unit_being_aggressive(const struct unit *punit)
Definition unit.c:1552
unit_type_really_ignores_zoc
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1538
unit_type_is_losing_hp
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2273
is_terrain_change_activity
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1622
unit_shield_value
int unit_shield_value(const struct unit *punit, const struct unit_type *punittype, const struct action *paction)
Definition unit.c:217
get_transporter_occupancy
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1816
cargo_iter_next
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2626
free_unit_orders
void free_unit_orders(struct unit *punit)
Definition unit.c:1802
can_unit_change_homecity
bool can_unit_change_homecity(const struct unit *punit)
Definition unit.c:505
unit_set_ai_data
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2323
unit_is_alive
bool unit_is_alive(int id)
Definition unit.c:2286
unit_can_est_trade_route_here
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:286
is_occupying_unit
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:326
is_hiding_unit
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:452
get_activity_rate_this_turn
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:546
unit_pays_mp_for_action
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2168
get_turns_for_activity_at
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:562
is_plr_zoc_client
bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1490
unit_transport_load
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2414
unit_can_add_or_build_city
bool unit_can_add_or_build_city(const struct unit *punit)
Definition unit.c:474
transporter_for_unit_at
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1955
cargo_iter_init
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2668
unit_upgrade_info
enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap, const struct unit *punit, char *buf, size_t bufsz)
Definition unit.c:2084
unit_transport_depth
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2595
is_losing_hp
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2261
is_targeted_activity
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1647
can_unit_deboard_or_be_unloaded
bool can_unit_deboard_or_be_unloaded(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:797
can_unit_change_homecity_to
bool can_unit_change_homecity_to(const struct unit *punit, const struct city *pcity)
Definition unit.c:490
can_unit_load
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:745
tile_non_attack_unit
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1373
hp_gain_coord
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2195
cargo_iter_get
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2616
unit_nationality
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1271
get_activity_rate
int get_activity_rate(const struct unit *punit)
Definition unit.c:518
unit_transport_check
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2523
is_attack_unit
bool is_attack_unit(const struct unit *punit)
Definition unit.c:307
tile_changing_activities
const Activity_type_id tile_changing_activities[]
Definition unit.c:44
unit_transport_get
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2485
unit_transport_unload
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2434
is_build_activity
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1592
unit_contained_in
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2564
unit_gain_hitpoints
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2216
test_unit_can_airlift_to
enum unit_airlift_result test_unit_can_airlift_to(const struct civ_map *nmap, const struct player *restriction, const struct unit *punit, const struct city *pdest_city)
Definition unit.c:81
can_unit_continue_current_activity
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:862
unit_bribe_cost
int unit_bribe_cost(struct unit *punit, struct player *briber)
Definition unit.c:2339
set_unit_activity_internal
static void set_unit_activity_internal(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1094
unit_upkeep_astr
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1257
could_unit_load
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:720
unit_is_cityfounder
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2685
is_unit_activity_on_tile
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1148
unit_can_help_build_wonder_here
bool unit_can_help_build_wonder_here(const struct unit *punit)
Definition unit.c:261
is_field_unit
bool is_field_unit(const struct unit *punit)
Definition unit.c:440
cargo_iter_valid
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2660
unit_can_do_action
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:385
unit_can_do_action_result
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:395
activity_default_action
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2897
RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction)
is_martial_law_unit
bool is_martial_law_unit(const struct unit *punit)
Definition unit.c:318
unit_virtual_create
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1657
unit_cargo_depth
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2577
can_unit_do_autoworker
bool can_unit_do_autoworker(const struct unit *punit)
Definition unit.c:626
unit_transport_check_one
static bool unit_transport_check_one(const struct unit_type *cargo_utype, const struct unit_type *trans_utype)
Definition unit.c:2507
CARGO_ITER
#define CARGO_ITER(iter)
Definition unit.c:54
can_unit_teleport
bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:827
unit_order_list_is_sane
bool unit_order_list_is_sane(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2693
get_activity_text
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:634
get_unit_tile_pillage_set
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1164
get_transporter_capacity
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:299
is_plr_zoc_srv
bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0, const struct civ_map *zmap)
Definition unit.c:1421
set_unit_activity_targeted
void set_unit_activity_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type *new_target, enum gen_action trigger_action)
Definition unit.c:1129
is_enter_borders_unit
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:334
is_clean_activity
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1608
unit_can_airlift_to
bool unit_can_airlift_to(const struct unit *punit, const struct city *pdest_city)
Definition unit.c:186
can_unit_paradrop
bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:843
unit_activity_astr
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1181
can_unit_do_activity
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:889
tile_other_players_unit
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1357
is_special_unit
bool is_special_unit(const struct unit *punit)
Definition unit.c:352
unit_virtual_destroy
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1762
tile_enemy_unit
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1317
unit_tile_set
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1281
set_unit_activity
void set_unit_activity(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1111
unit_upgrade_test
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:2021
unit_transported
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2469
are_unit_orders_equal
bool are_unit_orders_equal(const struct unit_order *order1, const struct unit_order *order2)
Definition unit.c:59
unit_is_virtual
bool unit_is_virtual(const struct unit *punit)
Definition unit.c:2303
is_flagless_to_player
bool is_flagless_to_player(const struct unit *punit, const struct player *pplayer)
Definition unit.c:365
is_square_threatened
bool is_square_threatened(const struct civ_map *nmap, const struct player *pplayer, const struct tile *ptile, bool omniscient)
Definition unit.c:414
is_guard_unit
bool is_guard_unit(const struct unit *punit)
Definition unit.c:342
can_unit_unload
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:772
unit_occupies_tile
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1394
unit_transport_cargo
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2495
can_type_transport_units_cargo
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1965
unit_ai_data
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2315
unit_has_orders
bool unit_has_orders(const struct unit *punit)
Definition unit.c:205
tile_allied_unit
struct unit * tile_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1294
transporter_for_unit
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1945
base_transporter_for_unit
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:1826
unit_can_convert
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2068
activity_requires_target
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:586
could_unit_be_in_transport
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:688
tile_non_allied_unit
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer, bool everyone_non_allied)
Definition unit.c:1332
can_unit_do_activity_targeted
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:928
unit.h
unit_tile
#define unit_tile(_pu)
Definition unit.h:397
unit_transports_iterate_end
#define unit_transports_iterate_end
Definition unit.h:564
unit_cargo_iterate_end
#define unit_cargo_iterate_end
Definition unit.h:574
FOCUS_AVAIL
@ FOCUS_AVAIL
Definition unit.h:54
BATTLEGROUP_NONE
#define BATTLEGROUP_NONE
Definition unit.h:193
unit_orders
unit_orders
Definition unit.h:38
ORDER_ACTION_MOVE
@ ORDER_ACTION_MOVE
Definition unit.h:46
ORDER_ACTIVITY
@ ORDER_ACTIVITY
Definition unit.h:42
ORDER_FULL_MP
@ ORDER_FULL_MP
Definition unit.h:44
ORDER_MOVE
@ ORDER_MOVE
Definition unit.h:40
ORDER_LAST
@ ORDER_LAST
Definition unit.h:50
ORDER_PERFORM_ACTION
@ ORDER_PERFORM_ACTION
Definition unit.h:48
unit_transported_server
#define unit_transported_server(_pcargo_)
Definition unit.h:542
unit_cargo_iterate
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:571
unit_owner
#define unit_owner(_pu)
Definition unit.h:396
unit_transports_iterate
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:560
unit_upgrade_result
unit_upgrade_result
Definition unit.h:61
UU_NO_MONEY
@ UU_NO_MONEY
Definition unit.h:64
UU_NOT_IN_CITY
@ UU_NOT_IN_CITY
Definition unit.h:65
UU_NO_UNITTYPE
@ UU_NO_UNITTYPE
Definition unit.h:63
UU_NOT_TERRAIN
@ UU_NOT_TERRAIN
Definition unit.h:68
UU_UNSUITABLE_TRANSPORT
@ UU_UNSUITABLE_TRANSPORT
Definition unit.h:69
UU_NOT_CITY_OWNER
@ UU_NOT_CITY_OWNER
Definition unit.h:66
UU_NOT_ENOUGH_ROOM
@ UU_NOT_ENOUGH_ROOM
Definition unit.h:67
UU_OK
@ UU_OK
Definition unit.h:62
UU_NOT_ACTIVITY
@ UU_NOT_ACTIVITY
Definition unit.h:70
unit_transported_client
#define unit_transported_client(_pcargo_)
Definition unit.h:545
unit_airlift_result
unit_airlift_result
Definition unit.h:73
AR_SRC_NO_FLIGHTS
@ AR_SRC_NO_FLIGHTS
Definition unit.h:85
AR_OK_SRC_UNKNOWN
@ AR_OK_SRC_UNKNOWN
Definition unit.h:76
AR_OK_DST_UNKNOWN
@ AR_OK_DST_UNKNOWN
Definition unit.h:77
AR_NO_MOVES
@ AR_NO_MOVES
Definition unit.h:79
AR_BAD_DST_CITY
@ AR_BAD_DST_CITY
Definition unit.h:84
AR_NOT_IN_CITY
@ AR_NOT_IN_CITY
Definition unit.h:82
AR_OCCUPIED
@ AR_OCCUPIED
Definition unit.h:81
AR_OK
@ AR_OK
Definition unit.h:75
AR_DST_NO_FLIGHTS
@ AR_DST_NO_FLIGHTS
Definition unit.h:86
AR_WRONG_UNITTYPE
@ AR_WRONG_UNITTYPE
Definition unit.h:80
AR_BAD_SRC_CITY
@ AR_BAD_SRC_CITY
Definition unit.h:83
unitlist.h
unit_list_iterate
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
unit_list_iterate_safe
#define unit_list_iterate_safe(unitlist, _unit)
Definition unitlist.h:39
unit_list_iterate_end
#define unit_list_iterate_end
Definition unitlist.h:33
unit_list_iterate_safe_end
#define unit_list_iterate_safe_end
Definition unitlist.h:61
unit_type_get
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
utype_pays_mp_for_action_base
int utype_pays_mp_for_action_base(const struct action *paction, const struct unit_type *putype)
Definition unittype.c:1381
utype_can_freely_unload
bool utype_can_freely_unload(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:300
unit_name_translation
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1569
utype_is_moved_to_tgt_by_action
bool utype_is_moved_to_tgt_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1249
utype_is_cityfounder
bool utype_is_cityfounder(const struct unit_type *utype)
Definition unittype.c:2951
unit_rule_name
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1587
utype_build_shield_cost_base
int utype_build_shield_cost_base(const struct unit_type *punittype)
Definition unittype.c:1468
utype_may_act_move_frags
bool utype_may_act_move_frags(const struct unit_type *punit_type, const action_id act_id, const int move_fragments)
Definition unittype.c:1058
unit_build_shield_cost_base
int unit_build_shield_cost_base(const struct unit *punit)
Definition unittype.c:1484
can_upgrade_unittype
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1703
utype_can_freely_load
bool utype_can_freely_load(const struct unit_type *pcargotype, const struct unit_type *ptranstype)
Definition unittype.c:288
utype_veteran_levels
int utype_veteran_levels(const struct unit_type *punittype)
Definition unittype.c:2613
unit_class_get
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2498
utype_can_do_action_result
bool utype_can_do_action_result(const struct unit_type *putype, enum action_result result)
Definition unittype.c:387
utype_veteran_level
const struct veteran_level * utype_veteran_level(const struct unit_type *punittype, int level)
Definition unittype.c:2583
unit_has_type_flag
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:190
utype_is_consumed_by_action
bool utype_is_consumed_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1219
utype_can_do_action_sub_result
bool utype_can_do_action_sub_result(const struct unit_type *putype, enum action_sub_result sub_result)
Definition unittype.c:408
utype_is_unmoved_by_action
bool utype_is_unmoved_by_action(const struct action *paction, const struct unit_type *utype)
Definition unittype.c:1288
utype_acts_hostile
bool utype_acts_hostile(const struct unit_type *putype)
Definition unittype.c:443
utype_name_translation
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1560
utype_can_do_action
bool utype_can_do_action(const struct unit_type *putype, const action_id act_id)
Definition unittype.c:371
unit_upgrade_price
int unit_upgrade_price(const struct player *pplayer, const struct unit_type *from, const struct unit_type *to)
Definition unittype.c:1731
uclass_has_flag
static bool uclass_has_flag(const struct unit_class *punitclass, enum unit_class_flag_id flag)
Definition unittype.h:773
utype_class
#define utype_class(_t_)
Definition unittype.h:756
utype_fuel
#define utype_fuel(ptype)
Definition unittype.h:843
utype_has_flag
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:624