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(nmap, 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 const struct civ_map *nmap = &(wld.map);
265
266 if (pcity == NULL) {
267 /* No city to help at this tile. */
268 return FALSE;
269 }
270
271 if (!utype_can_do_action(unit_type_get(punit), ACTION_HELP_WONDER)) {
272 /* This unit can never do help wonder. */
273 return FALSE;
274 }
275
276 /* Evaluate all action enablers for extra accuracy. */
277 /* TODO: Is it worth it? */
278 return action_prob_possible(action_prob_vs_city(nmap, punit,
279 ACTION_HELP_WONDER,
280 pcity));
281}
282
283/**********************************************************************/
287 bool unit_can_est_trade_route_here(const struct unit *punit)
288{
289 struct city *phomecity, *pdestcity;
290
291 return (utype_can_do_action(unit_type_get(punit), ACTION_TRADE_ROUTE)
292 && (pdestcity = tile_city(unit_tile(punit)))
293 && (phomecity = game_city_by_number(punit->homecity))
294 && can_cities_trade(phomecity, pdestcity));
295}
296
297/**********************************************************************/
300 int get_transporter_capacity(const struct unit *punit)
301{
302 return unit_type_get(punit)->transport_capacity;
303}
304
305/**********************************************************************/
308 bool is_attack_unit(const struct unit *punit)
309{
310 return ((unit_can_do_action_result(punit, ACTRES_ATTACK)
311 || unit_can_do_action_result(punit, ACTRES_BOMBARD)
312 || unit_can_do_action_result(punit, ACTRES_WIPE_UNITS))
313 && unit_type_get(punit)->attack_strength > 0);
314}
315
316/**********************************************************************/
319 bool is_martial_law_unit(const struct unit *punit)
320{
321 return !unit_has_type_flag(punit, UTYF_CIVILIAN);
322}
323
324/**********************************************************************/
327 bool is_occupying_unit(const struct unit *punit)
328{
329 return !unit_has_type_flag(punit, UTYF_CIVILIAN);
330}
331
332/**********************************************************************/
335 bool is_enter_borders_unit(const struct unit *punit)
336{
337 return unit_has_type_flag(punit, UTYF_CIVILIAN);
338}
339
340/**********************************************************************/
343 bool is_guard_unit(const struct unit *punit)
344{
345 return !unit_has_type_flag(punit, UTYF_CIVILIAN);
346}
347
348/**********************************************************************/
353 bool is_special_unit(const struct unit *punit)
354{
355 return unit_has_type_flag(punit, UTYF_CIVILIAN);
356}
357
358/**********************************************************************/
366 bool is_flagless_to_player(const struct unit *punit,
367 const struct player *pplayer)
368{
369 if (unit_owner(punit) != pplayer
370 && unit_has_type_flag(punit, UTYF_FLAGLESS)) {
371 if (pplayer == nullptr) {
372 /* Global observer always sees the flags */
373 return FALSE;
374 }
375
376 return TRUE;
377 }
378
379 return FALSE;
380}
381
382/**********************************************************************/
386 bool unit_can_do_action(const struct unit *punit,
387 const action_id act_id)
388{
389 return utype_can_do_action(unit_type_get(punit), act_id);
390}
391
392/**********************************************************************/
396 bool unit_can_do_action_result(const struct unit *punit,
397 enum action_result result)
398{
399 return utype_can_do_action_result(unit_type_get(punit), result);
400}
401
402/**********************************************************************/
406 bool unit_can_do_action_sub_result(const struct unit *punit,
407 enum action_sub_result sub_result)
408{
409 return utype_can_do_action_sub_result(unit_type_get(punit), sub_result);
410}
411
412/**********************************************************************/
415 bool is_square_threatened(const struct civ_map *nmap,
416 const struct player *pplayer,
417 const struct tile *ptile, bool omniscient)
418{
419 square_iterate(nmap, ptile, 2, ptile1) {
420 unit_list_iterate(ptile1->units, punit) {
421 if ((omniscient
422 || can_player_see_unit(pplayer, punit))
423 && pplayers_at_war(pplayer, unit_owner(punit))
424 && utype_acts_hostile(unit_type_get(punit))
425 && (is_native_tile(unit_type_get(punit), ptile)
426 || (can_attack_non_native(unit_type_get(punit))
427 && is_native_near_tile(nmap,
428 unit_class_get(punit), ptile)))) {
429 return TRUE;
430 }
431 } unit_list_iterate_end;
432 } square_iterate_end;
433
434 return FALSE;
435}
436
437/**********************************************************************/
441 bool is_field_unit(const struct unit *punit)
442{
443 return unit_has_type_flag(punit, UTYF_FIELDUNIT);
444}
445
446/**********************************************************************/
453 bool is_hiding_unit(const struct unit *punit)
454{
455 enum vision_layer vl = unit_type_get(punit)->vlayer;
456
457 if (vl == V_INVIS || vl == V_SUBSURFACE) {
458 return TRUE;
459 }
460
461 if (unit_transported(punit)) {
462 vl = unit_type_get(unit_transport_get(punit))->vlayer;
463 if (vl == V_INVIS || vl == V_SUBSURFACE) {
464 return TRUE;
465 }
466 }
467
468 return FALSE;
469}
470
471/**********************************************************************/
475 bool unit_can_add_or_build_city(const struct unit *punit)
476{
477 struct city *tgt_city;
478 const struct civ_map *nmap = &(wld.map);
479
480 if ((tgt_city = tile_city(unit_tile(punit)))) {
481 return action_prob_possible(action_prob_vs_city(nmap, punit,
482 ACTION_JOIN_CITY, tgt_city));
483 } else {
484 return action_prob_possible(action_prob_vs_tile(punit,
485 ACTION_FOUND_CITY, unit_tile(punit), NULL));
486 }
487}
488
489/**********************************************************************/
492 bool can_unit_change_homecity_to(const struct unit *punit,
493 const struct city *pcity)
494{
495 const struct civ_map *nmap = &(wld.map);
496
497 if (pcity == NULL) {
498 /* Can't change home city to a non existing city. */
499 return FALSE;
500 }
501
502 return action_prob_possible(action_prob_vs_city(nmap, punit, ACTION_HOME_CITY,
503 pcity));
504}
505
506/**********************************************************************/
509 bool can_unit_change_homecity(const struct unit *punit)
510{
511 return can_unit_change_homecity_to(punit, tile_city(unit_tile(punit)));
512}
513
514/**********************************************************************/
522 int get_activity_rate(const struct unit *punit)
523{
524 const struct veteran_level *vlevel;
525
526 fc_assert_ret_val(punit != NULL, 0);
527
528 vlevel = utype_veteran_level(unit_type_get(punit), punit->veteran);
529 fc_assert_ret_val(vlevel != NULL, 0);
530
531 /* The speed of the settler depends on its base move_rate, not on
532 * the number of moves actually remaining or the adjusted move rate.
533 * This means sea formers won't have their activity rate increased by
534 * Magellan's, and it means injured units work just as fast as
535 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
536 int move_rate = unit_type_get(punit)->move_rate;
537
538 /* All settler actions are multiplied by ACTIVITY_FACTOR. */
539 return ACTIVITY_FACTOR
540 * (float)vlevel->power_fact / 100
541 * move_rate / SINGLE_MOVE;
542}
543
544/**********************************************************************/
550 int get_activity_rate_this_turn(const struct unit *punit)
551{
552 /* This logic is also coded in client/goto.c. */
553 if (punit->moves_left > 0) {
554 return get_activity_rate(punit);
555 } else {
556 return 0;
557 }
558}
559
560/**********************************************************************/
566 int get_turns_for_activity_at(const struct unit *punit,
567 enum unit_activity activity,
568 const struct tile *ptile,
569 struct extra_type *tgt)
570{
571 /* FIXME: This is just an approximation since we don't account for
572 * get_activity_rate_this_turn. */
573 int speed = get_activity_rate(punit);
574 int points_needed;
575
576 fc_assert(tgt != NULL || !is_targeted_activity(activity));
577
578 points_needed = tile_activity_time(activity, ptile, tgt);
579
580 if (points_needed >= 0 && speed > 0) {
581 return (points_needed - 1) / speed + 1; /* Round up */
582 } else {
583 return FC_INFINITY;
584 }
585}
586
587/**********************************************************************/
590 bool activity_requires_target(enum unit_activity activity)
591{
592 switch (activity) {
593 case ACTIVITY_PILLAGE:
594 case ACTIVITY_BASE:
595 case ACTIVITY_GEN_ROAD:
596 case ACTIVITY_IRRIGATE:
597 case ACTIVITY_MINE:
598 case ACTIVITY_CLEAN:
599 return TRUE;
600 case ACTIVITY_IDLE:
601 case ACTIVITY_FORTIFIED:
602 case ACTIVITY_SENTRY:
603 case ACTIVITY_GOTO:
604 case ACTIVITY_EXPLORE:
605 case ACTIVITY_TRANSFORM:
606 case ACTIVITY_CULTIVATE:
607 case ACTIVITY_PLANT:
608 case ACTIVITY_FORTIFYING:
609 case ACTIVITY_CONVERT:
610 return FALSE;
611 /* These shouldn't be kicking around internally. */
612 case ACTIVITY_LAST:
613 break;
614 }
615
616 fc_assert(FALSE);
617
618 return FALSE;
619}
620
621/**********************************************************************/
630 bool can_unit_do_autoworker(const struct unit *punit)
631{
632 return unit_type_get(punit)->adv.worker;
633}
634
635/**********************************************************************/
638 const char *get_activity_text(enum unit_activity activity)
639{
640 /* The switch statement has just the activities listed with no "default"
641 * handling. This enables the compiler to detect missing entries
642 * automatically, and still handles everything correctly. */
643 switch (activity) {
644 case ACTIVITY_IDLE:
645 return _("Idle");
646 case ACTIVITY_CLEAN:
647 return _("Clean");
648 case ACTIVITY_MINE:
649 /* TRANS: Activity name, verb in English */
650 return Q_("?act:Mine");
651 case ACTIVITY_PLANT:
652 /* TRANS: Activity name, verb in English */
653 return _("Plant");
654 case ACTIVITY_IRRIGATE:
655 return _("Irrigate");
656 case ACTIVITY_CULTIVATE:
657 return _("Cultivate");
658 case ACTIVITY_FORTIFYING:
659 return _("Fortifying");
660 case ACTIVITY_FORTIFIED:
661 return _("Fortified");
662 case ACTIVITY_SENTRY:
663 return _("Sentry");
664 case ACTIVITY_PILLAGE:
665 return _("Pillage");
666 case ACTIVITY_GOTO:
667 return _("Goto");
668 case ACTIVITY_EXPLORE:
669 return _("Explore");
670 case ACTIVITY_TRANSFORM:
671 return _("Transform");
672 case ACTIVITY_BASE:
673 return _("Base");
674 case ACTIVITY_GEN_ROAD:
675 return _("Road");
676 case ACTIVITY_CONVERT:
677 return _("Convert");
678 case ACTIVITY_LAST:
679 break;
680 }
681
682 fc_assert(FALSE);
683 return _("Unknown");
684}
685
686/**********************************************************************/
692 bool could_unit_be_in_transport(const struct unit *pcargo,
693 const struct unit *ptrans)
694{
695 /* Make sure this transporter can carry this type of unit. */
696 if (!can_unit_transport(ptrans, pcargo)) {
697 return FALSE;
698 }
699
700 /* Make sure there's room in the transporter. */
701 if (get_transporter_occupancy(ptrans)
702 >= get_transporter_capacity(ptrans)) {
703 return FALSE;
704 }
705
706 /* Check iff this is a valid transport. */
707 if (!unit_transport_check(pcargo, ptrans)) {
708 return FALSE;
709 }
710
711 /* Check transport depth. */
712 if (GAME_TRANSPORT_MAX_RECURSIVE
713 < 1 + unit_transport_depth(ptrans) + unit_cargo_depth(pcargo)) {
714 return FALSE;
715 }
716
717 return TRUE;
718}
719
720/**********************************************************************/
724 bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
725{
726 if (!pcargo || !ptrans || pcargo == ptrans) {
727 return FALSE;
728 }
729
730 /* Double-check ownership of the units: you can load into an allied unit
731 * (of course only allied units can be on the same tile). */
732 if (!pplayers_allied(unit_owner(pcargo), unit_owner(ptrans))) {
733 return FALSE;
734 }
735
736 /* Un-embarkable transport must be in city or base to load cargo. */
737 if (!utype_can_freely_load(unit_type_get(pcargo), unit_type_get(ptrans))
738 && !tile_city(unit_tile(ptrans))
739 && !tile_has_native_base(unit_tile(ptrans), unit_type_get(ptrans))) {
740 return FALSE;
741 }
742
743 return could_unit_be_in_transport(pcargo, ptrans);
744}
745
746/**********************************************************************/
749 bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
750{
751 /* This function needs to check EVERYTHING. */
752
753 /* Check positions of the units. Of course you can't load a unit onto
754 * a transporter on a different tile... */
755 if (!same_pos(unit_tile(pcargo), unit_tile(ptrans))) {
756 return FALSE;
757 }
758
759 /* Cannot load if cargo is already loaded onto something else. */
760 if (unit_transported(pcargo)) {
761 return FALSE;
762 }
763
764 return could_unit_load(pcargo, ptrans);
765}
766
767/**********************************************************************/
776 bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
777{
778 if (!pcargo || !ptrans) {
779 return FALSE;
780 }
781
782 /* Make sure the unit's transporter exists and is known. */
783 if (unit_transport_get(pcargo) != ptrans) {
784 return FALSE;
785 }
786
787 /* Un-disembarkable transport must be in city or base to unload cargo. */
788 if (!utype_can_freely_unload(unit_type_get(pcargo), unit_type_get(ptrans))
789 && !tile_city(unit_tile(ptrans))
790 && !tile_has_native_base(unit_tile(ptrans), unit_type_get(ptrans))) {
791 return FALSE;
792 }
793
794 return TRUE;
795}
796
797/**********************************************************************/
801 bool can_unit_deboard_or_be_unloaded(const struct unit *pcargo,
802 const struct unit *ptrans)
803{
804 const struct civ_map *nmap = &(wld.map);
805
806 if (!pcargo || !ptrans) {
807 return FALSE;
808 }
809
810 fc_assert_ret_val(unit_transport_get(pcargo) == ptrans, FALSE);
811
812 if (is_server()) {
813 return (is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_DEBOARD,
814 pcargo, ptrans)
815 || is_action_enabled_unit_on_unit(nmap, ACTION_TRANSPORT_UNLOAD,
816 ptrans, pcargo));
817 } else {
818 return (action_prob_possible(
819 action_prob_vs_unit(nmap, pcargo, ACTION_TRANSPORT_DEBOARD, ptrans))
820 || action_prob_possible(
821 action_prob_vs_unit(nmap, ptrans, ACTION_TRANSPORT_UNLOAD,
822 pcargo)));
823 }
824}
825
826/**********************************************************************/
831 bool can_unit_teleport(const struct civ_map *nmap, const struct unit *punit)
832{
833 action_by_result_iterate(paction, ACTRES_TELEPORT) {
834 if (action_maybe_possible_actor_unit(nmap, action_id(paction), punit)) {
835 return TRUE;
836 }
837 } action_by_result_iterate_end;
838
839 return FALSE;
840}
841
842/**********************************************************************/
847 bool can_unit_paradrop(const struct civ_map *nmap, const struct unit *punit)
848{
849 action_by_result_iterate(paction, ACTRES_PARADROP) {
850 if (action_maybe_possible_actor_unit(nmap, action_id(paction), punit)) {
851 return TRUE;
852 }
853 } action_by_result_iterate_end;
854 action_by_result_iterate(paction, ACTRES_PARADROP_CONQUER) {
855 if (action_maybe_possible_actor_unit(nmap, action_id(paction), punit)) {
856 return TRUE;
857 }
858 } action_by_result_iterate_end;
859
860 return FALSE;
861}
862
863/**********************************************************************/
866 bool can_unit_continue_current_activity(const struct civ_map *nmap,
867 struct unit *punit)
868{
869 enum unit_activity current = punit->activity;
870 struct extra_type *target = punit->activity_target;
871 enum unit_activity current2 =
872 (current == ACTIVITY_FORTIFIED) ? ACTIVITY_FORTIFYING : current;
873 bool result;
874
875 punit->activity = ACTIVITY_IDLE;
876 punit->activity_target = NULL;
877
878 result = can_unit_do_activity_targeted(nmap, punit, current2, target);
879
880 punit->activity = current;
881 punit->activity_target = target;
882
883 return result;
884}
885
886/**********************************************************************/
893 bool can_unit_do_activity(const struct civ_map *nmap,
894 const struct unit *punit,
895 enum unit_activity activity)
896{
897 struct extra_type *target = NULL;
898
899 /* FIXME: Lots of callers (usually client real_menus_update()) rely on
900 * being able to find out whether an activity is in general possible.
901 * Find one for them, but when they come to do the activity, they will
902 * have to determine the target themselves */
903 {
904 struct tile *ptile = unit_tile(punit);
905
906 if (activity == ACTIVITY_IRRIGATE) {
907 target = next_extra_for_tile(ptile,
908 EC_IRRIGATION,
909 unit_owner(punit),
910 punit);
911 if (NULL == target) {
912 return FALSE; /* No more irrigation extras available. */
913 }
914 } else if (activity == ACTIVITY_MINE) {
915 target = next_extra_for_tile(ptile,
916 EC_MINE,
917 unit_owner(punit),
918 punit);
919 if (NULL == target) {
920 return FALSE; /* No more mine extras available. */
921 }
922 }
923 }
924
925 return can_unit_do_activity_targeted(nmap, punit, activity, target);
926}
927
928/**********************************************************************/
932 bool can_unit_do_activity_targeted(const struct civ_map *nmap,
933 const struct unit *punit,
934 enum unit_activity activity,
935 struct extra_type *target)
936{
937 return can_unit_do_activity_targeted_at(nmap, punit, activity, target,
938 unit_tile(punit));
939}
940
941/**********************************************************************/
945 bool can_unit_do_activity_targeted_at(const struct civ_map *nmap,
946 const struct unit *punit,
947 enum unit_activity activity,
948 struct extra_type *target,
949 const struct tile *ptile)
950{
951 /* Check that no build activity conflicting with one already in progress
952 * gets executed. */
953 /* FIXME: Should check also the cases where one of the activities is terrain
954 * change that destroys the target of the other activity */
955 if (target != NULL && is_build_activity(activity)) {
956 if (tile_is_placing(ptile)) {
957 return FALSE;
958 }
959
960 unit_list_iterate(ptile->units, tunit) {
961 if (is_build_activity(tunit->activity)
962 && !can_extras_coexist(target, tunit->activity_target)) {
963 return FALSE;
964 }
965 } unit_list_iterate_end;
966 }
967
968#define RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(paction) \
969{ \
970 switch (action_get_target_kind(paction)) { \
971 case ATK_TILE: \
972 return is_action_enabled_unit_on_tile(nmap, paction->id, \
973 punit, ptile, target); \
974 case ATK_EXTRAS: \
975 return is_action_enabled_unit_on_extras(nmap, paction->id, \
976 punit, ptile, target); \
977 case ATK_CITY: \
978 case ATK_UNIT: \
979 case ATK_UNITS: \
980 case ATK_SELF: \
981 return FALSE; \
982 case ATK_COUNT: \
983 break; /* Handle outside switch */ \
984 } \
985 fc_assert(action_target_kind_is_valid( \
986 action_get_target_kind(paction))); \
987 return FALSE; \
988 }
989
990 switch (activity) {
991 case ACTIVITY_IDLE:
992 case ACTIVITY_GOTO:
993 return TRUE;
994
995 case ACTIVITY_CLEAN:
996 /* The call below doesn't support actor tile speculation. */
997 fc_assert_msg(unit_tile(punit) == ptile,
998 "Please use action_speculate_unit_on_tile()");
999 return is_action_enabled_unit_on_tile(nmap, ACTION_CLEAN,
1000 punit, ptile, target);
1001
1002 case ACTIVITY_MINE:
1003 /* The call below doesn't support actor tile speculation. */
1004 fc_assert_msg(unit_tile(punit) == ptile,
1005 "Please use action_speculate_unit_on_tile()");
1006 return is_action_enabled_unit_on_tile(nmap, ACTION_MINE, punit,
1007 ptile, target);
1008
1009 case ACTIVITY_PLANT:
1010 /* The call below doesn't support actor tile speculation. */
1011 fc_assert_msg(unit_tile(punit) == ptile,
1012 "Please use action_speculate_unit_on_tile()");
1013 return is_action_enabled_unit_on_tile(nmap, ACTION_PLANT,
1014 punit, ptile, NULL);
1015
1016 case ACTIVITY_IRRIGATE:
1017 /* The call below doesn't support actor tile speculation. */
1018 fc_assert_msg(unit_tile(punit) == ptile,
1019 "Please use action_speculate_unit_on_tile()");
1020 return is_action_enabled_unit_on_tile(nmap, ACTION_IRRIGATE, punit,
1021 ptile, target);
1022
1023 case ACTIVITY_CULTIVATE:
1024 /* The call below doesn't support actor tile speculation. */
1025 fc_assert_msg(unit_tile(punit) == ptile,
1026 "Please use action_speculate_unit_on_tile()");
1027 return is_action_enabled_unit_on_tile(nmap, ACTION_CULTIVATE,
1028 punit, ptile, NULL);
1029
1030 case ACTIVITY_FORTIFYING:
1031 /* The call below doesn't support actor tile speculation. */
1032 fc_assert_msg(unit_tile(punit) == ptile,
1033 "Please use action_speculate_unit_on_self()");
1034 return is_action_enabled_unit_on_self(nmap, ACTION_FORTIFY,
1035 punit);
1036
1037 case ACTIVITY_FORTIFIED:
1038 return FALSE;
1039
1040 case ACTIVITY_BASE:
1041 /* The call below doesn't support actor tile speculation. */
1042 fc_assert_msg(unit_tile(punit) == ptile,
1043 "Please use action_speculate_unit_on_tile()");
1044 return is_action_enabled_unit_on_tile(nmap, ACTION_BASE,
1045 punit, ptile, target);
1046
1047 case ACTIVITY_GEN_ROAD:
1048 /* The call below doesn't support actor tile speculation. */
1049 fc_assert_msg(unit_tile(punit) == ptile,
1050 "Please use action_speculate_unit_on_tile()");
1051 return is_action_enabled_unit_on_tile(nmap, ACTION_ROAD,
1052 punit, ptile, target);
1053
1054 case ACTIVITY_SENTRY:
1055 if (!can_unit_survive_at_tile(nmap, punit, unit_tile(punit))
1056 && !unit_transported(punit)) {
1057 /* Don't let units sentry on tiles they will die on. */
1058 return FALSE;
1059 }
1060 return TRUE;
1061
1062 case ACTIVITY_PILLAGE:
1063 /* The call below doesn't support actor tile speculation. */
1064 fc_assert_msg(unit_tile(punit) == ptile,
1065 "Please use action_speculate_unit_on_tile()");
1066 RETURN_IS_ACTIVITY_ENABLED_UNIT_ON(action_by_number(ACTION_PILLAGE));
1067
1068 case ACTIVITY_EXPLORE:
1069 return (!unit_type_get(punit)->fuel && !is_losing_hp(punit));
1070
1071 case ACTIVITY_TRANSFORM:
1072 /* The call below doesn't support actor tile speculation. */
1073 fc_assert_msg(unit_tile(punit) == ptile,
1074 "Please use action_speculate_unit_on_tile()");
1075 return is_action_enabled_unit_on_tile(nmap, ACTION_TRANSFORM_TERRAIN,
1076 punit, ptile, NULL);
1077
1078 case ACTIVITY_CONVERT:
1079 /* The call below doesn't support actor tile speculation. */
1080 fc_assert_msg(unit_tile(punit) == ptile,
1081 "Please use action_speculate_unit_on_self()");
1082 return is_action_enabled_unit_on_self(nmap, ACTION_CONVERT, punit);
1083
1084 case ACTIVITY_LAST:
1085 break;
1086 }
1087
1088 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1089 activity);
1090 return FALSE;
1091
1092#undef RETURN_IS_ACTIVITY_ENABLED_UNIT_ON
1093}
1094
1095/**********************************************************************/
1098 static void set_unit_activity_internal(struct unit *punit,
1099 enum unit_activity new_activity,
1100 enum gen_action trigger_action)
1101{
1102 punit->activity = new_activity;
1103 punit->action = trigger_action;
1104 punit->activity_count = 0;
1105 punit->activity_target = NULL;
1106 if (new_activity == ACTIVITY_IDLE && punit->moves_left > 0) {
1107 /* No longer done. */
1108 punit->done_moving = FALSE;
1109 }
1110}
1111
1112/**********************************************************************/
1115 void set_unit_activity(struct unit *punit, enum unit_activity new_activity,
1116 enum gen_action trigger_action)
1117{
1118 fc_assert_ret(!activity_requires_target(new_activity));
1119
1120 if (new_activity == ACTIVITY_FORTIFYING
1121 && punit->changed_from == ACTIVITY_FORTIFIED) {
1122 new_activity = ACTIVITY_FORTIFIED;
1123 }
1124 set_unit_activity_internal(punit, new_activity, trigger_action);
1125 if (new_activity == punit->changed_from) {
1126 punit->activity_count = punit->changed_from_count;
1127 }
1128}
1129
1130/**********************************************************************/
1133 void set_unit_activity_targeted(struct unit *punit,
1134 enum unit_activity new_activity,
1135 struct extra_type *new_target,
1136 enum gen_action trigger_action)
1137{
1138 fc_assert_ret(activity_requires_target(new_activity)
1139 || new_target == NULL);
1140
1141 set_unit_activity_internal(punit, new_activity, trigger_action);
1142 punit->activity_target = new_target;
1143 if (new_activity == punit->changed_from
1144 && new_target == punit->changed_from_target) {
1145 punit->activity_count = punit->changed_from_count;
1146 }
1147}
1148
1149/**********************************************************************/
1152 bool is_unit_activity_on_tile(enum unit_activity activity,
1153 const struct tile *ptile)
1154{
1155 unit_list_iterate(ptile->units, punit) {
1156 if (punit->activity == activity) {
1157 return TRUE;
1158 }
1159 } unit_list_iterate_end;
1160
1161 return FALSE;
1162}
1163
1164/**********************************************************************/
1168 bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
1169{
1170 bv_extras tgt_ret;
1171
1172 BV_CLR_ALL(tgt_ret);
1173 unit_list_iterate(ptile->units, punit) {
1174 if (punit->activity == ACTIVITY_PILLAGE) {
1175 BV_SET(tgt_ret, extra_index(punit->activity_target));
1176 }
1177 } unit_list_iterate_end;
1178
1179 return tgt_ret;
1180}
1181
1182/**********************************************************************/
1185 void unit_activity_astr(const struct unit *punit, struct astring *astr)
1186{
1187 if (!punit || !astr) {
1188 return;
1189 }
1190
1191 switch (punit->activity) {
1192 case ACTIVITY_IDLE:
1193 if (utype_fuel(unit_type_get(punit))) {
1194 int rate, f;
1195
1196 rate = unit_type_get(punit)->move_rate;
1197 f = ((punit->fuel) - 1);
1198
1199 /* Add in two parts as move_points_text() returns ptr to static
1200 * End result: "Moves: (fuel)moves_left" */
1201 astr_add_line(astr, "%s: (%s)", _("Moves"),
1202 move_points_text((rate * f) + punit->moves_left, FALSE));
1203 astr_add(astr, "%s",
1204 move_points_text(punit->moves_left, FALSE));
1205 } else {
1206 astr_add_line(astr, "%s: %s", _("Moves"),
1207 move_points_text(punit->moves_left, FALSE));
1208 }
1209 return;
1210 case ACTIVITY_CLEAN:
1211 case ACTIVITY_TRANSFORM:
1212 case ACTIVITY_FORTIFYING:
1213 case ACTIVITY_FORTIFIED:
1214 case ACTIVITY_SENTRY:
1215 case ACTIVITY_GOTO:
1216 case ACTIVITY_EXPLORE:
1217 case ACTIVITY_CONVERT:
1218 case ACTIVITY_CULTIVATE:
1219 case ACTIVITY_PLANT:
1220 astr_add_line(astr, "%s", get_activity_text(punit->activity));
1221 return;
1222 case ACTIVITY_MINE:
1223 case ACTIVITY_IRRIGATE:
1224 if (punit->activity_target == NULL) {
1225 astr_add_line(astr, "%s", get_activity_text(punit->activity));
1226 } else {
1227 astr_add_line(astr, "Building %s",
1228 extra_name_translation(punit->activity_target));
1229 }
1230 return;
1231 case ACTIVITY_PILLAGE:
1232 if (punit->activity_target != NULL) {
1233 astr_add_line(astr, "%s: %s", get_activity_text(punit->activity),
1234 extra_name_translation(punit->activity_target));
1235 } else {
1236 astr_add_line(astr, "%s", get_activity_text(punit->activity));
1237 }
1238 return;
1239 case ACTIVITY_BASE:
1240 astr_add_line(astr, "%s: %s", get_activity_text(punit->activity),
1241 extra_name_translation(punit->activity_target));
1242 return;
1243 case ACTIVITY_GEN_ROAD:
1244 astr_add_line(astr, "%s: %s", get_activity_text(punit->activity),
1245 extra_name_translation(punit->activity_target));
1246 return;
1247 case ACTIVITY_LAST:
1248 break;
1249 }
1250
1251 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1252 punit->activity, unit_rule_name(punit), punit->id, __FUNCTION__);
1253}
1254
1255/**********************************************************************/
1261 void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
1262{
1263 if (!punit || !astr) {
1264 return;
1265 }
1266
1267 astr_add_line(astr, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1268 punit->upkeep[O_FOOD], punit->upkeep[O_SHIELD],
1269 punit->upkeep[O_GOLD]);
1270}
1271
1272/**********************************************************************/
1275 struct player *unit_nationality(const struct unit *punit)
1276{
1277 fc_assert_ret_val(NULL != punit, NULL);
1278 return punit->nationality;
1279}
1280
1281/**********************************************************************/
1285 void unit_tile_set(struct unit *punit, struct tile *ptile)
1286{
1287 fc_assert_ret(NULL != punit);
1288 punit->tile = ptile;
1289}
1290
1291/**********************************************************************/
1298 struct unit *tile_allied_unit(const struct tile *ptile,
1299 const struct player *pplayer)
1300{
1301 struct unit *punit = NULL;
1302
1303 unit_list_iterate(ptile->units, cunit) {
1304 if (pplayers_allied(pplayer, unit_owner(cunit))) {
1305 punit = cunit;
1306 } else {
1307 return NULL;
1308 }
1309 }
1310 unit_list_iterate_end;
1311
1312 return punit;
1313}
1314
1315/**********************************************************************/
1321 struct unit *tile_enemy_unit(const struct tile *ptile,
1322 const struct player *pplayer)
1323{
1324 unit_list_iterate(ptile->units, punit) {
1325 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1326 return punit;
1327 }
1328 } unit_list_iterate_end;
1329
1330 return NULL;
1331}
1332
1333/**********************************************************************/
1336 struct unit *tile_non_allied_unit(const struct tile *ptile,
1337 const struct player *pplayer,
1338 bool everyone_non_allied)
1339{
1340 unit_list_iterate(ptile->units, punit) {
1341 struct player *owner = unit_owner(punit);
1342
1343 if (everyone_non_allied && owner != pplayer) {
1344 return punit;
1345 }
1346
1347 if (!pplayers_allied(owner, pplayer)
1348 || is_flagless_to_player(punit, pplayer)) {
1349 return punit;
1350 }
1351 }
1352 unit_list_iterate_end;
1353
1354 return NULL;
1355}
1356
1357/**********************************************************************/
1361 struct unit *tile_other_players_unit(const struct tile *ptile,
1362 const struct player *pplayer)
1363{
1364 unit_list_iterate(ptile->units, punit) {
1365 if (unit_owner(punit) != pplayer) {
1366 return punit;
1367 }
1368 } unit_list_iterate_end;
1369
1370 return NULL;
1371}
1372
1373/**********************************************************************/
1377 struct unit *tile_non_attack_unit(const struct tile *ptile,
1378 const struct player *pplayer)
1379{
1380 unit_list_iterate(ptile->units, punit) {
1381 if (pplayers_non_attack(unit_owner(punit), pplayer)) {
1382 return punit;
1383 }
1384 }
1385 unit_list_iterate_end;
1386
1387 return NULL;
1388}
1389
1390/**********************************************************************/
1398 struct unit *unit_occupies_tile(const struct tile *ptile,
1399 const struct player *pplayer)
1400{
1401 unit_list_iterate(ptile->units, punit) {
1402 if (!is_occupying_unit(punit)) {
1403 continue;
1404 }
1405
1406 if (uclass_has_flag(unit_class_get(punit), UCF_DOESNT_OCCUPY_TILE)) {
1407 continue;
1408 }
1409
1410 if (pplayers_at_war(unit_owner(punit), pplayer)) {
1411 return punit;
1412 }
1413 } unit_list_iterate_end;
1414
1415 return NULL;
1416}
1417
1418/**********************************************************************/
1425 bool is_plr_zoc_srv(const struct player *pplayer, const struct tile *ptile0,
1426 const struct civ_map *zmap)
1427{
1428 struct extra_type_list *zoccers = extra_type_list_of_zoccers();
1429
1430 square_iterate(zmap, ptile0, 1, ptile) {
1431 struct terrain *pterrain;
1432 struct city *pcity;
1433
1434 pterrain = tile_terrain(ptile);
1435 if (terrain_has_flag(pterrain, TER_NO_ZOC)) {
1436 continue;
1437 }
1438
1439 pcity = tile_non_allied_city(ptile, pplayer);
1440 if (pcity != NULL) {
1441 if (unit_list_size(ptile->units) > 0) {
1442 /* Occupied enemy city, it doesn't matter if units inside have
1443 * UTYF_NOZOC or not. */
1444 return FALSE;
1445 }
1446 } else {
1447 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1448 extra_type_list_iterate(zoccers, pextra) {
1449 if (tile_has_extra(ptile, pextra)) {
1450 return FALSE;
1451 }
1452 } extra_type_list_iterate_end;
1453 }
1454
1455 unit_list_iterate(ptile->units, punit) {
1456 if (!pplayers_allied(unit_owner(punit), pplayer)
1457 && !unit_has_type_flag(punit, UTYF_NOZOC)
1458 && !unit_transported_server(punit)) {
1459 bool hidden = FALSE;
1460
1461 /* We do NOT check the possibility that player is allied with an extra owner,
1462 * and should thus see inside the extra.
1463 * This is to avoid the situation where having an alliance with third player
1464 * suddenly causes ZoC from a unit that would not cause it without the alliance. */
1465 extra_type_list_iterate(unit_class_get(punit)->cache.hiding_extras, pextra) {
1466 if (tile_has_extra(ptile, pextra)) {
1467 hidden = TRUE;
1468 break;
1469 }
1470 } extra_type_list_iterate_end;
1471
1472 if (!hidden) {
1473 return FALSE;
1474 }
1475 }
1476 } unit_list_iterate_end;
1477 }
1478 } square_iterate_end;
1479
1480 return TRUE;
1481}
1482
1483/**********************************************************************/
1494 bool is_plr_zoc_client(const struct player *pplayer, const struct tile *ptile0,
1495 const struct civ_map *zmap)
1496{
1497 struct extra_type_list *zoccers = extra_type_list_of_zoccers();
1498
1499 square_iterate(zmap, ptile0, 1, ptile) {
1500 struct terrain *pterrain;
1501 struct city *pcity;
1502
1503 pterrain = tile_terrain(ptile);
1504 if (T_UNKNOWN == pterrain
1505 || terrain_has_flag(pterrain, TER_NO_ZOC)) {
1506 continue;
1507 }
1508
1509 pcity = tile_non_allied_city(ptile, pplayer);
1510 if (pcity != NULL) {
1511 if (pcity->client.occupied
1512 || TILE_KNOWN_UNSEEN == tile_get_known(ptile, pplayer)) {
1513 /* Occupied enemy city, it doesn't matter if units inside have
1514 * UTYF_NOZOC or not. Fogged city is assumed to be occupied. */
1515 return FALSE;
1516 }
1517 } else {
1518 if (!pplayers_allied(extra_owner(ptile), pplayer)) {
1519 extra_type_list_iterate(zoccers, pextra) {
1520 if (tile_has_extra(ptile, pextra)) {
1521 return FALSE;
1522 }
1523 } extra_type_list_iterate_end;
1524 }
1525
1526 unit_list_iterate(ptile->units, punit) {
1527 if (!unit_transported_client(punit)
1528 && !pplayers_allied(unit_owner(punit), pplayer)
1529 && !unit_has_type_flag(punit, UTYF_NOZOC)) {
1530 return FALSE;
1531 }
1532 } unit_list_iterate_end;
1533 }
1534 } square_iterate_end;
1535
1536 return TRUE;
1537}
1538
1539/**********************************************************************/
1542 bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
1543{
1544 return (!uclass_has_flag(utype_class(punittype), UCF_ZOC)
1545 || utype_has_flag(punittype, UTYF_IGZOC));
1546}
1547
1548/**********************************************************************/
1556 bool unit_being_aggressive(const struct unit *punit)
1557{
1558 const struct tile *ptile = unit_tile(punit);
1559 int max_friendliness_range;
1560 const struct civ_map *nmap = &(wld.map);
1561
1562 if (!is_attack_unit(punit)) {
1563 return FALSE;
1564 }
1565 if (tile_city(ptile)) {
1566 return FALSE;
1567 }
1568 if (BORDERS_DISABLED != game.info.borders) {
1569 switch (game.info.happyborders) {
1570 case HB_DISABLED:
1571 break;
1572 case HB_NATIONAL:
1573 if (tile_owner(ptile) == unit_owner(punit)) {
1574 return FALSE;
1575 }
1576 break;
1577 case HB_ALLIANCE:
1578 if (pplayers_allied(tile_owner(ptile), unit_owner(punit))) {
1579 return FALSE;
1580 }
1581 break;
1582 }
1583 }
1584
1585 max_friendliness_range = tile_has_not_aggressive_extra_for_unit(ptile,
1586 unit_type_get(punit));
1587 if (max_friendliness_range >= 0) {
1588 return !is_unit_near_a_friendly_city(nmap, punit, max_friendliness_range);
1589 }
1590
1591 return TRUE;
1592}
1593
1594/**********************************************************************/
1597 bool is_build_activity(enum unit_activity activity)
1598{
1599 switch (activity) {
1600 case ACTIVITY_MINE:
1601 case ACTIVITY_IRRIGATE:
1602 case ACTIVITY_BASE:
1603 case ACTIVITY_GEN_ROAD:
1604 return TRUE;
1605 default:
1606 return FALSE;
1607 }
1608}
1609
1610/**********************************************************************/
1613 bool is_clean_activity(enum unit_activity activity)
1614{
1615 switch (activity) {
1616 case ACTIVITY_PILLAGE:
1617 case ACTIVITY_CLEAN:
1618 return TRUE;
1619 default:
1620 return FALSE;
1621 }
1622}
1623
1624/**********************************************************************/
1627 bool is_terrain_change_activity(enum unit_activity activity)
1628{
1629 switch (activity) {
1630 case ACTIVITY_CULTIVATE:
1631 case ACTIVITY_PLANT:
1632 case ACTIVITY_TRANSFORM:
1633 return TRUE;
1634 default:
1635 return FALSE;
1636 }
1637}
1638
1639/**********************************************************************/
1642 bool is_tile_activity(enum unit_activity activity)
1643{
1644 return is_build_activity(activity)
1645 || is_clean_activity(activity)
1646 || is_terrain_change_activity(activity);
1647}
1648
1649/**********************************************************************/
1652 bool is_targeted_activity(enum unit_activity activity)
1653{
1654 return is_build_activity(activity)
1655 || is_clean_activity(activity);
1656}
1657
1658/**********************************************************************/
1662 struct unit *unit_virtual_create(struct player *pplayer, struct city *pcity,
1663 const struct unit_type *punittype,
1664 int veteran_level)
1665{
1666 /* Make sure that contents of unit structure are correctly initialized,
1667 * if you ever allocate it by some other mean than fc_calloc() */
1668 struct unit *punit = fc_calloc(1, sizeof(*punit));
1669 int max_vet_lvl;
1670
1671 /* It does not register the unit so the id is set to 0. */
1672 punit->id = IDENTITY_NUMBER_ZERO;
1673
1674 fc_assert_ret_val(punittype != nullptr, nullptr); /* No untyped units! */
1675 punit->utype = punittype;
1676
1677 fc_assert_ret_val(!is_server() || pplayer != nullptr, nullptr); /* No unowned units! */
1678 punit->owner = pplayer;
1679 punit->nationality = pplayer;
1680
1681 punit->refcount = 1;
1682 punit->facing = rand_direction();
1683
1684 if (pcity != nullptr) {
1685 unit_tile_set(punit, pcity->tile);
1686 punit->homecity = pcity->id;
1687 } else {
1688 unit_tile_set(punit, nullptr);
1689 punit->homecity = IDENTITY_NUMBER_ZERO;
1690 }
1691
1692 memset(punit->upkeep, 0, O_LAST * sizeof(*punit->upkeep));
1693 punit->goto_tile = nullptr;
1694 max_vet_lvl = utype_veteran_levels(punittype) - 1;
1695 punit->veteran = MIN(veteran_level, max_vet_lvl);
1696 /* A unit new and fresh ... */
1697 punit->fuel = utype_fuel(unit_type_get(punit));
1698 punit->hp = unit_type_get(punit)->hp;
1699 if (utype_has_flag(punittype, UTYF_RANDOM_MOVEMENT)) {
1700 /* Random moves units start with zero movement as their first movement
1701 * will be only after their moves have been reset in the beginning of
1702 * the next turn. */
1703 punit->moves_left = 0;
1704 } else {
1705 punit->moves_left = unit_move_rate(punit);
1706 }
1707 punit->moved = FALSE;
1708
1709 punit->ssa_controller = SSA_NONE;
1710 punit->paradropped = FALSE;
1711 punit->done_moving = FALSE;
1712
1713 punit->transporter = nullptr;
1714 punit->transporting = unit_list_new();
1715
1716 punit->carrying = nullptr;
1717
1718 set_unit_activity(punit, ACTIVITY_IDLE, ACTION_NONE);
1719 punit->battlegroup = BATTLEGROUP_NONE;
1720 punit->has_orders = FALSE;
1721
1722 punit->action_decision_want = ACT_DEC_NOTHING;
1723 punit->action_decision_tile = nullptr;
1724
1725 punit->stay = FALSE;
1726
1727 punit->birth_turn = game.info.turn;
1728 punit->current_form_turn = game.info.turn;
1729
1730 if (is_server()) {
1731 punit->server.debug = FALSE;
1732
1733 punit->server.dying = FALSE;
1734
1735 punit->server.removal_callback = nullptr;
1736
1737 memset(punit->server.upkeep_paid, 0,
1738 O_LAST * sizeof(*punit->server.upkeep_paid));
1739
1740 punit->server.ord_map = 0;
1741 punit->server.ord_city = 0;
1742
1743 punit->server.vision = nullptr; /* No vision. */
1744 punit->server.action_timestamp = 0;
1745 /* Must be an invalid turn number, and an invalid previous turn
1746 * number. */
1747 punit->server.action_turn = -2;
1748 /* punit->server.moving = NULL; set by fc_calloc(). */
1749
1750 punit->server.adv = fc_calloc(1, sizeof(*punit->server.adv));
1751
1752 CALL_FUNC_EACH_AI(unit_alloc, punit);
1753 } else {
1754 punit->client.focus_status = FOCUS_AVAIL;
1755 punit->client.transported_by = -1;
1756 punit->client.colored = FALSE;
1757 punit->client.act_prob_cache = nullptr;
1758 }
1759
1760 return punit;
1761}
1762
1763/**********************************************************************/
1767 void unit_virtual_destroy(struct unit *punit)
1768{
1769 free_unit_orders(punit);
1770
1771 /* Unload unit if transported. */
1772 unit_transport_unload(punit);
1773 fc_assert(!unit_transported(punit));
1774
1775 /* Check for transported units. Use direct access to the list. */
1776 if (unit_list_size(punit->transporting) != 0) {
1777 /* Unload all units. */
1778 unit_list_iterate_safe(punit->transporting, pcargo) {
1779 unit_transport_unload(pcargo);
1780 } unit_list_iterate_safe_end;
1781 }
1782 fc_assert(unit_list_size(punit->transporting) == 0);
1783
1784 if (punit->transporting) {
1785 unit_list_destroy(punit->transporting);
1786 }
1787
1788 CALL_FUNC_EACH_AI(unit_free, punit);
1789
1790 if (is_server() && punit->server.adv) {
1791 FC_FREE(punit->server.adv);
1792 } else {
1793 if (punit->client.act_prob_cache) {
1794 FC_FREE(punit->client.act_prob_cache);
1795 }
1796 }
1797
1798 if (--punit->refcount <= 0) {
1799 FC_FREE(punit);
1800 }
1801}
1802
1803/**********************************************************************/
1807 void free_unit_orders(struct unit *punit)
1808{
1809 if (punit->has_orders) {
1810 punit->goto_tile = NULL;
1811 free(punit->orders.list);
1812 punit->orders.list = NULL;
1813 }
1814 punit->orders.length = 0;
1815 punit->has_orders = FALSE;
1816}
1817
1818/**********************************************************************/
1821 int get_transporter_occupancy(const struct unit *ptrans)
1822{
1823 fc_assert_ret_val(ptrans, -1);
1824
1825 return unit_list_size(ptrans->transporting);
1826}
1827
1828/**********************************************************************/
1831 static struct unit *base_transporter_for_unit(const struct unit *pcargo,
1832 const struct tile *ptile,
1833 bool (*unit_load_test)
1834 (const struct unit *pc,
1835 const struct unit *pt))
1836{
1837 struct unit *best_trans = NULL;
1838 struct {
1839 bool has_orders, is_idle, can_freely_unload;
1840 int depth, outermost_moves_left, total_moves;
1841 } cur, best = { FALSE };
1842
1843 unit_list_iterate(ptile->units, ptrans) {
1844 if (!unit_load_test(pcargo, ptrans)) {
1845 continue;
1846 } else if (best_trans == NULL) {
1847 best_trans = ptrans;
1848 }
1849
1850 /* Gather data from transport stack in a single pass, for use in
1851 * various conditions below. */
1852 cur.has_orders = unit_has_orders(ptrans);
1853 cur.outermost_moves_left = ptrans->moves_left;
1854 cur.total_moves = ptrans->moves_left + unit_move_rate(ptrans);
1855 unit_transports_iterate(ptrans, ptranstrans) {
1856 if (unit_has_orders(ptranstrans)) {
1857 cur.has_orders = TRUE;
1858 }
1859 cur.outermost_moves_left = ptranstrans->moves_left;
1860 cur.total_moves += ptranstrans->moves_left + unit_move_rate(ptranstrans);
1861 } unit_transports_iterate_end;
1862
1863 /* Criteria for deciding the 'best' transport to load onto.
1864 * The following tests are applied in order; earlier ones have
1865 * lexicographically greater significance than later ones. */
1866
1867 /* Transports which have orders, or are on transports with orders,
1868 * are less preferable to transport stacks without orders (to
1869 * avoid loading on units that are just passing through). */
1870 if (best_trans != ptrans) {
1871 if (!cur.has_orders && best.has_orders) {
1872 best_trans = ptrans;
1873 } else if (cur.has_orders && !best.has_orders) {
1874 continue;
1875 }
1876 }
1877
1878 /* Else, transports which are idle are preferable (giving players
1879 * some control over loading) -- this does not check transports
1880 * of transports. */
1881 cur.is_idle = (ptrans->activity == ACTIVITY_IDLE);
1882 if (best_trans != ptrans) {
1883 if (cur.is_idle && !best.is_idle) {
1884 best_trans = ptrans;
1885 } else if (!cur.is_idle && best.is_idle) {
1886 continue;
1887 }
1888 }
1889
1890 /* Else, transports from which the cargo could unload at any time
1891 * are preferable to those where the cargo can only disembark in
1892 * cities/bases. */
1893 cur.can_freely_unload = utype_can_freely_unload(unit_type_get(pcargo),
1894 unit_type_get(ptrans));
1895 if (best_trans != ptrans) {
1896 if (cur.can_freely_unload && !best.can_freely_unload) {
1897 best_trans = ptrans;
1898 } else if (!cur.can_freely_unload && best.can_freely_unload) {
1899 continue;
1900 }
1901 }
1902
1903 /* Else, transports which are less deeply nested are preferable. */
1904 cur.depth = unit_transport_depth(ptrans);
1905 if (best_trans != ptrans) {
1906 if (cur.depth < best.depth) {
1907 best_trans = ptrans;
1908 } else if (cur.depth > best.depth) {
1909 continue;
1910 }
1911 }
1912
1913 /* Else, transport stacks where the outermost transport has more
1914 * moves left are preferable (on the assumption that it's the
1915 * outermost transport that's about to move). */
1916 if (best_trans != ptrans) {
1917 if (cur.outermost_moves_left > best.outermost_moves_left) {
1918 best_trans = ptrans;
1919 } else if (cur.outermost_moves_left < best.outermost_moves_left) {
1920 continue;
1921 }
1922 }
1923
1924 /* All other things being equal, as a tie-breaker, compare the total
1925 * moves left (this turn) and move rate (future turns) for the whole
1926 * stack, to take into account total potential movement for both
1927 * short and long journeys (we don't know which the cargo intends to
1928 * make). Doesn't try to account for whether transports can unload,
1929 * etc. */
1930 if (best_trans != ptrans) {
1931 if (cur.total_moves > best.total_moves) {
1932 best_trans = ptrans;
1933 } else {
1934 continue;
1935 }
1936 }
1937
1938 fc_assert(best_trans == ptrans);
1939 best = cur;
1940 } unit_list_iterate_end;
1941
1942 return best_trans;
1943}
1944
1945/**********************************************************************/
1950 struct unit *transporter_for_unit(const struct unit *pcargo)
1951{
1952 return base_transporter_for_unit(pcargo, unit_tile(pcargo), can_unit_load);
1953}
1954
1955/**********************************************************************/
1960 struct unit *transporter_for_unit_at(const struct unit *pcargo,
1961 const struct tile *ptile)
1962{
1963 return base_transporter_for_unit(pcargo, ptile, could_unit_load);
1964}
1965
1966/**********************************************************************/
1970 static bool can_type_transport_units_cargo(const struct unit_type *utype,
1971 const struct unit *punit)
1972{
1973 if (get_transporter_occupancy(punit) > utype->transport_capacity) {
1974 return FALSE;
1975 }
1976
1977 unit_list_iterate(punit->transporting, pcargo) {
1978 if (!can_unit_type_transport(utype, unit_class_get(pcargo))) {
1979 return FALSE;
1980 }
1981 } unit_list_iterate_end;
1982
1983 return TRUE;
1984}
1985
1986/**********************************************************************/
1993enum unit_upgrade_result
1994unit_transform_result(const struct civ_map *nmap,
1995 const struct unit *punit,
1996 const struct unit_type *to_unittype)
1997{
1998 fc_assert_ret_val(NULL != to_unittype, UU_NO_UNITTYPE);
1999
2000 if (!can_type_transport_units_cargo(to_unittype, punit)) {
2001 return UU_NOT_ENOUGH_ROOM;
2002 }
2003
2004 if (punit->transporter != NULL) {
2005 if (!can_unit_type_transport(unit_type_get(punit->transporter),
2006 utype_class(to_unittype))) {
2007 return UU_UNSUITABLE_TRANSPORT;
2008 }
2009 } else if (!can_exist_at_tile(nmap, to_unittype, unit_tile(punit))) {
2010 /* The new unit type can't survive on this terrain. */
2011 return UU_NOT_TERRAIN;
2012 }
2013
2014 return UU_OK;
2015}
2016
2017/**********************************************************************/
2026 enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap,
2027 const struct unit *punit,
2028 bool is_free)
2029{
2030 struct player *pplayer = unit_owner(punit);
2031 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2032 unit_type_get(punit));
2033 struct city *pcity;
2034 int cost;
2035
2036 if (!to_unittype) {
2037 return UU_NO_UNITTYPE;
2038 }
2039
2040 if (punit->activity == ACTIVITY_CONVERT) {
2041 /* TODO: There may be other activities that the upgraded unit is not
2042 allowed to do, which we could also test.
2043 -
2044 If convert were legal for new unit_type we could allow, but then it
2045 has a 'head start' getting activity time from the old conversion. */
2046 return UU_NOT_ACTIVITY;
2047 }
2048
2049 if (!is_free) {
2050 cost = unit_upgrade_price(pplayer, unit_type_get(punit), to_unittype);
2051 if (pplayer->economic.gold < cost) {
2052 return UU_NO_MONEY;
2053 }
2054
2055 pcity = tile_city(unit_tile(punit));
2056 if (!pcity) {
2057 return UU_NOT_IN_CITY;
2058 }
2059 if (city_owner(pcity) != pplayer) {
2060 /* TODO: Should upgrades in allied cities be possible? */
2061 return UU_NOT_CITY_OWNER;
2062 }
2063 }
2064
2065 /* TODO: Allow transported units to be reassigned. Check here
2066 * and make changes to upgrade_unit. */
2067 return unit_transform_result(nmap, punit, to_unittype);
2068}
2069
2070/**********************************************************************/
2073 bool unit_can_convert(const struct civ_map *nmap,
2074 const struct unit *punit)
2075{
2076 const struct unit_type *tgt = unit_type_get(punit)->converted_to;
2077
2078 if (tgt == NULL) {
2079 return FALSE;
2080 }
2081
2082 return UU_OK == unit_transform_result(nmap, punit, tgt);
2083}
2084
2085/**********************************************************************/
2089 enum unit_upgrade_result unit_upgrade_info(const struct civ_map *nmap,
2090 const struct unit *punit,
2091 char *buf, size_t bufsz)
2092{
2093 struct player *pplayer = unit_owner(punit);
2094 enum unit_upgrade_result result = unit_upgrade_test(nmap, punit, FALSE);
2095 int upgrade_cost;
2096 const struct unit_type *from_unittype = unit_type_get(punit);
2097 const struct unit_type *to_unittype = can_upgrade_unittype(pplayer,
2098 unit_type_get(punit));
2099 char tbuf[MAX_LEN_MSG];
2100
2101 fc_snprintf(tbuf, ARRAY_SIZE(tbuf), PL_("Treasury contains %d gold.",
2102 "Treasury contains %d gold.",
2103 pplayer->economic.gold),
2104 pplayer->economic.gold);
2105
2106 switch (result) {
2107 case UU_OK:
2108 upgrade_cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
2109 /* This message is targeted toward the GUI callers. */
2110 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2111 fc_snprintf(buf, bufsz, PL_("Upgrade %s to %s for %d gold?\n%s",
2112 "Upgrade %s to %s for %d gold?\n%s",
2113 upgrade_cost),
2114 utype_name_translation(from_unittype),
2115 utype_name_translation(to_unittype),
2116 upgrade_cost, tbuf);
2117 break;
2118 case UU_NO_UNITTYPE:
2119 fc_snprintf(buf, bufsz,
2120 _("Sorry, cannot upgrade %s (yet)."),
2121 utype_name_translation(from_unittype));
2122 break;
2123 case UU_NO_MONEY:
2124 upgrade_cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
2125 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2126 fc_snprintf(buf, bufsz, PL_("Upgrading %s to %s costs %d gold.\n%s",
2127 "Upgrading %s to %s costs %d gold.\n%s",
2128 upgrade_cost),
2129 utype_name_translation(from_unittype),
2130 utype_name_translation(to_unittype),
2131 upgrade_cost, tbuf);
2132 break;
2133 case UU_NOT_IN_CITY:
2134 case UU_NOT_CITY_OWNER:
2135 fc_snprintf(buf, bufsz,
2136 _("You can only upgrade units in your cities."));
2137 break;
2138 case UU_NOT_ENOUGH_ROOM:
2139 fc_snprintf(buf, bufsz,
2140 _("Upgrading this %s would strand units it transports."),
2141 utype_name_translation(from_unittype));
2142 break;
2143 case UU_NOT_TERRAIN:
2144 fc_snprintf(buf, bufsz,
2145 _("Upgrading this %s would result in a %s which can not "
2146 "survive at this place."),
2147 utype_name_translation(from_unittype),
2148 utype_name_translation(to_unittype));
2149 break;
2150 case UU_UNSUITABLE_TRANSPORT:
2151 fc_snprintf(buf, bufsz,
2152 _("Upgrading this %s would result in a %s which its "
2153 "current transport, %s, could not transport."),
2154 utype_name_translation(from_unittype),
2155 utype_name_translation(to_unittype),
2156 unit_name_translation(punit->transporter));
2157 break;
2158 case UU_NOT_ACTIVITY:
2159 fc_snprintf(buf, bufsz,
2160 _("Cannot upgrade %s while doing '%s'."),
2161 utype_name_translation(from_unittype),
2162 unit_activity_name(punit->activity));
2163 break;
2164 }
2165
2166 return result;
2167}
2168
2169/**********************************************************************/
2173 int unit_pays_mp_for_action(const struct action *paction,
2174 const struct unit *punit)
2175{
2176 int mpco;
2177
2178 mpco = get_target_bonus_effects(NULL,
2179 &(const struct req_context) {
2180 .player = unit_owner(punit),
2181 .city = unit_tile(punit)
2182 ? tile_city(unit_tile(punit))
2183 : NULL,
2184 .tile = unit_tile(punit),
2185 .unit = punit,
2186 .unittype = unit_type_get(punit),
2187 .action = paction,
2188 },
2189 NULL,
2190 EFT_ACTION_SUCCESS_MOVE_COST);
2191
2192 mpco += utype_pays_mp_for_action_base(paction, unit_type_get(punit));
2193
2194 return mpco;
2195}
2196
2197/**********************************************************************/
2200 int hp_gain_coord(const struct unit *punit)
2201{
2202 int hp = 0;
2203 const int base = unit_type_get(punit)->hp;
2204 int min = base * get_unit_bonus(punit, EFT_MIN_HP_PCT) / 100;
2205
2206 /* Includes barracks (100%), fortress (25%), etc. */
2207 hp += base * get_unit_bonus(punit, EFT_HP_REGEN) / 100;
2208
2209 /* Minimum HP after regen effects applied. */
2210 hp = MAX(hp, min);
2211
2212 /* Regen2 effects that apply after there's at least Min HP */
2213 hp += ceil(base / 10) * get_unit_bonus(punit, EFT_HP_REGEN_2) / 10;
2214
2215 return MAX(hp, 0);
2216}
2217
2218/**********************************************************************/
2221 int unit_gain_hitpoints(const struct unit *punit)
2222{
2223 const struct unit_type *utype = unit_type_get(punit);
2224 struct unit_class *pclass = unit_class_get(punit);
2225 struct city *pcity = tile_city(unit_tile(punit));
2226 int gain;
2227
2228 if (!punit->moved) {
2229 gain = hp_gain_coord(punit);
2230 } else {
2231 gain = 0;
2232 }
2233
2234 gain += get_unit_bonus(punit, EFT_UNIT_RECOVER);
2235
2236 if (!punit->homecity && 0 < game.server.killunhomed
2237 && !unit_has_type_flag(punit, UTYF_GAMELOSS)) {
2238 /* Hit point loss of units without homecity; at least 1 hp! */
2239 /* Gameloss units are immune to this effect. */
2240 int hp_loss = MAX(utype->hp * game.server.killunhomed / 100, 1);
2241
2242 if (gain > hp_loss) {
2243 gain = -1;
2244 } else {
2245 gain -= hp_loss;
2246 }
2247 }
2248
2249 if (pcity == NULL && !tile_has_native_base(unit_tile(punit), utype)
2250 && !unit_transported(punit)) {
2251 gain -= utype->hp * pclass->hp_loss_pct / 100;
2252 }
2253
2254 if (punit->hp + gain > utype->hp) {
2255 gain = utype->hp - punit->hp;
2256 } else if (punit->hp + gain < 0) {
2257 gain = -punit->hp;
2258 }
2259
2260 return gain;
2261}
2262
2263/**********************************************************************/
2266 bool is_losing_hp(const struct unit *punit)
2267{
2268 const struct unit_type *punittype = unit_type_get(punit);
2269
2270 return get_unit_bonus(punit, EFT_UNIT_RECOVER)
2271 < (punittype->hp *
2272 utype_class(punittype)->hp_loss_pct / 100);
2273}
2274
2275/**********************************************************************/
2278 bool unit_type_is_losing_hp(const struct player *pplayer,
2279 const struct unit_type *punittype)
2280{
2281 return get_unittype_bonus(pplayer, NULL, punittype, NULL,
2282 EFT_UNIT_RECOVER)
2283 < (punittype->hp *
2284 utype_class(punittype)->hp_loss_pct / 100);
2285}
2286
2287/**********************************************************************/
2291 bool unit_is_alive(int id)
2292{
2293 /* Check if unit exist in game */
2294 if (game_unit_by_number(id)) {
2295 return TRUE;
2296 }
2297
2298 return FALSE;
2299}
2300
2301/**********************************************************************/
2308 bool unit_is_virtual(const struct unit *punit)
2309{
2310 if (!punit) {
2311 return FALSE;
2312 }
2313
2314 return punit != game_unit_by_number(punit->id);
2315}
2316
2317/**********************************************************************/
2320 void *unit_ai_data(const struct unit *punit, const struct ai_type *ai)
2321{
2322 return punit->server.ais[ai_type_number(ai)];
2323}
2324
2325/**********************************************************************/
2328 void unit_set_ai_data(struct unit *punit, const struct ai_type *ai,
2329 void *data)
2330{
2331 punit->server.ais[ai_type_number(ai)] = data;
2332}
2333
2334/**********************************************************************/
2345 int unit_bribe_cost(const struct unit *punit, const struct player *briber,
2346 const struct unit *briber_unit)
2347{
2348 int cost, default_hp;
2349 struct tile *ptile = unit_tile(punit);
2350 struct player *owner = unit_owner(punit);
2351 const struct unit_type *ptype = unit_type_get(punit);
2352
2353 fc_assert_ret_val(punit != NULL, 0);
2354
2355 default_hp = ptype->hp;
2356
2357 cost = game.info.base_bribe_cost;
2358
2359 if (owner != nullptr) {
2360 int dist = 0;
2361
2362 cost += owner->economic.gold;
2363
2364 /* Consider the distance to the capital. */
2365 dist = GAME_UNIT_BRIBE_DIST_MAX;
2366 city_list_iterate(owner->cities, capital) {
2367 if (is_capital(capital)) {
2368 int tmp = map_distance(capital->tile, ptile);
2369
2370 if (tmp < dist) {
2371 dist = tmp;
2372 }
2373 }
2374 } city_list_iterate_end;
2375
2376 cost /= dist + 2;
2377 }
2378
2379 /* Consider the build cost. */
2380 cost *= unit_build_shield_cost_base(punit) / 10.0;
2381
2382 /* Rule set specific cost modification */
2383 cost += (cost * get_target_bonus_effects(NULL,
2384 &(const struct req_context) {
2385 .player = owner,
2386 .city = game_city_by_number(punit->homecity),
2387 .tile = ptile,
2388 .unit = punit,
2389 .unittype = ptype,
2390 },
2391 &(const struct req_context) {
2392 .player = briber,
2393 .unit = briber_unit,
2394 .unittype = briber_unit ? unit_type_get(briber_unit)
2395 : nullptr,
2396 .tile = briber_unit ? unit_tile(briber_unit)
2397 : nullptr,
2398 .city = briber_unit
2399 ? game_city_by_number(briber_unit->homecity)
2400 : nullptr,
2401 },
2402 EFT_UNIT_BRIBE_COST_PCT))
2403 / 100;
2404
2405 /* Veterans are not cheap. */
2406 {
2407 const struct veteran_level *vlevel
2408 = utype_veteran_level(ptype, punit->veteran);
2409
2410 fc_assert_ret_val(vlevel != NULL, 0);
2411 cost = cost * vlevel->power_fact / 100;
2412 if (ptype->move_rate > 0) {
2413 cost += cost * vlevel->move_bonus / ptype->move_rate;
2414 } else {
2415 cost += cost * vlevel->move_bonus / SINGLE_MOVE;
2416 }
2417 }
2418
2419 /* Cost now contains the basic bribe cost. We now reduce it by:
2420 * bribecost = cost / 2 + cost / 2 * damage / hp
2421 * = cost / 2 * (1 + damage / hp) */
2422 return ((float)cost / 2 * (1.0 + (float)punit->hp / default_hp));
2423}
2424
2425/**********************************************************************/
2428 bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
2429{
2430 fc_assert_ret_val(ptrans != NULL, FALSE);
2431 fc_assert_ret_val(pcargo != NULL, FALSE);
2432
2433 fc_assert_ret_val(!unit_list_search(ptrans->transporting, pcargo), FALSE);
2434
2435 if (force || can_unit_load(pcargo, ptrans)) {
2436 pcargo->transporter = ptrans;
2437 unit_list_append(ptrans->transporting, pcargo);
2438
2439 return TRUE;
2440 }
2441
2442 return FALSE;
2443}
2444
2445/**********************************************************************/
2448 bool unit_transport_unload(struct unit *pcargo)
2449{
2450 struct unit *ptrans;
2451
2452 fc_assert_ret_val(pcargo != NULL, FALSE);
2453
2454 if (!unit_transported(pcargo)) {
2455 /* 'pcargo' is not transported. */
2456 return FALSE;
2457 }
2458
2459 /* Get the transporter; must not be defined on the client! */
2460 ptrans = unit_transport_get(pcargo);
2461 if (ptrans) {
2462 /* 'pcargo' and 'ptrans' should be on the same tile. */
2463 fc_assert(same_pos(unit_tile(pcargo), unit_tile(ptrans)));
2464
2465#ifndef FREECIV_NDEBUG
2466 bool success =
2467#endif
2468 unit_list_remove(ptrans->transporting, pcargo);
2469
2470 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2471 fc_assert(success);
2472 }
2473
2474 /* For the server (also safe for the client). */
2475 pcargo->transporter = NULL;
2476
2477 return TRUE;
2478}
2479
2480/**********************************************************************/
2483 bool unit_transported(const struct unit *pcargo)
2484{
2485 fc_assert_ret_val(pcargo != NULL, FALSE);
2486
2487 /* The unit is transported if a transporter unit is set or, (for the client)
2488 * if the transported_by field is set. */
2489 if (is_server()) {
2490 return unit_transported_server(pcargo);
2491 } else {
2492 return unit_transported_client(pcargo);
2493 }
2494}
2495
2496/**********************************************************************/
2499 struct unit *unit_transport_get(const struct unit *pcargo)
2500{
2501 fc_assert_ret_val(pcargo != NULL, NULL);
2502
2503 return pcargo->transporter;
2504}
2505
2506/**********************************************************************/
2509 struct unit_list *unit_transport_cargo(const struct unit *ptrans)
2510{
2511 fc_assert_ret_val(ptrans != NULL, NULL);
2512 fc_assert_ret_val(ptrans->transporting != NULL, NULL);
2513
2514 return ptrans->transporting;
2515}
2516
2517/**********************************************************************/
2520static inline bool
2521unit_transport_check_one(const struct unit_type *cargo_utype,
2522 const struct unit_type *trans_utype)
2523{
2524 return (trans_utype != cargo_utype
2525 && !can_unit_type_transport(cargo_utype,
2526 utype_class(trans_utype)));
2527}
2528
2529/**********************************************************************/
2537 bool unit_transport_check(const struct unit *pcargo,
2538 const struct unit *ptrans)
2539{
2540 const struct unit_type *cargo_utype = unit_type_get(pcargo);
2541
2542 /* Check 'pcargo' against 'ptrans'. */
2543 if (!unit_transport_check_one(cargo_utype, unit_type_get(ptrans))) {
2544 return FALSE;
2545 }
2546
2547 /* Check 'pcargo' against 'ptrans' parents. */
2548 unit_transports_iterate(ptrans, pparent) {
2549 if (!unit_transport_check_one(cargo_utype, unit_type_get(pparent))) {
2550 return FALSE;
2551 }
2552 } unit_transports_iterate_end;
2553
2554 /* Check cargo children... */
2555 unit_cargo_iterate(pcargo, pchild) {
2556 cargo_utype = unit_type_get(pchild);
2557
2558 /* ...against 'ptrans'. */
2559 if (!unit_transport_check_one(cargo_utype, unit_type_get(ptrans))) {
2560 return FALSE;
2561 }
2562
2563 /* ...and against 'ptrans' parents. */
2564 unit_transports_iterate(ptrans, pparent) {
2565 if (!unit_transport_check_one(cargo_utype, unit_type_get(pparent))) {
2566 return FALSE;
2567 }
2568 } unit_transports_iterate_end;
2569 } unit_cargo_iterate_end;
2570
2571 return TRUE;
2572}
2573
2574/**********************************************************************/
2578 bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
2579{
2580 unit_transports_iterate(pcargo, plevel) {
2581 if (ptrans == plevel) {
2582 return TRUE;
2583 }
2584 } unit_transports_iterate_end;
2585 return FALSE;
2586}
2587
2588/**********************************************************************/
2591 int unit_cargo_depth(const struct unit *ptrans)
2592{
2593 struct cargo_iter iter;
2594 struct iterator *it;
2595 int depth = 0;
2596
2597 for (it = cargo_iter_init(&iter, ptrans); iterator_valid(it);
2598 iterator_next(it)) {
2599 if (iter.depth > depth) {
2600 depth = iter.depth;
2601 }
2602 }
2603 return depth;
2604}
2605
2606/**********************************************************************/
2609 int unit_transport_depth(const struct unit *pcargo)
2610{
2611 int level = 0;
2612
2613 unit_transports_iterate(pcargo, plevel) {
2614 level++;
2615 } unit_transports_iterate_end;
2616 return level;
2617}
2618
2619/**********************************************************************/
2622 size_t cargo_iter_sizeof(void)
2623{
2624 return sizeof(struct cargo_iter);
2625}
2626
2627/**********************************************************************/
2630 static void *cargo_iter_get(const struct iterator *it)
2631{
2632 const struct cargo_iter *iter = CARGO_ITER(it);
2633
2634 return unit_list_link_data(iter->links[iter->depth - 1]);
2635}
2636
2637/**********************************************************************/
2640 static void cargo_iter_next(struct iterator *it)
2641{
2642 struct cargo_iter *iter = CARGO_ITER(it);
2643 const struct unit_list_link *piter;
2644 const struct unit_list_link *pnext;
2645
2646 /* Variant 1: unit has cargo. */
2647 pnext = unit_list_head(unit_transport_cargo(cargo_iter_get(it)));
2648 if (NULL != pnext) {
2649 fc_assert(iter->depth < ARRAY_SIZE(iter->links));
2650 iter->links[iter->depth++] = pnext;
2651 return;
2652 }
2653
2654 fc_assert(iter->depth > 0);
2655
2656 while (iter->depth > 0) {
2657 piter = iter->links[iter->depth - 1];
2658
2659 /* Variant 2: there are other cargo units at same level. */
2660 pnext = unit_list_link_next(piter);
2661 if (NULL != pnext) {
2662 iter->links[iter->depth - 1] = pnext;
2663 return;
2664 }
2665
2666 /* Variant 3: return to previous level, and do same tests. */
2667 iter->depth--;
2668 }
2669}
2670
2671/**********************************************************************/
2674 static bool cargo_iter_valid(const struct iterator *it)
2675{
2676 return (0 < CARGO_ITER(it)->depth);
2677}
2678
2679/**********************************************************************/
2682 struct iterator *cargo_iter_init(struct cargo_iter *iter,
2683 const struct unit *ptrans)
2684{
2685 struct iterator *it = ITERATOR(iter);
2686
2687 it->get = cargo_iter_get;
2688 it->next = cargo_iter_next;
2689 it->valid = cargo_iter_valid;
2690 iter->links[0] = unit_list_head(unit_transport_cargo(ptrans));
2691 iter->depth = (NULL != iter->links[0] ? 1 : 0);
2692
2693 return it;
2694}
2695
2696/**********************************************************************/
2699 bool unit_is_cityfounder(const struct unit *punit)
2700{
2701 return utype_is_cityfounder(unit_type_get(punit));
2702}
2703
2704/**********************************************************************/
2707 bool unit_order_list_is_sane(const struct civ_map *nmap,
2708 int length, const struct unit_order *orders)
2709{
2710 int i;
2711
2712 for (i = 0; i < length; i++) {
2713 struct action *paction;
2714 struct extra_type *pextra;
2715
2716 if (orders[i].order > ORDER_LAST) {
2717 log_error("invalid order %d at index %d", orders[i].order, i);
2718 return FALSE;
2719 }
2720 switch (orders[i].order) {
2721 case ORDER_MOVE:
2722 case ORDER_ACTION_MOVE:
2723 if (!map_untrusted_dir_is_valid(orders[i].dir)) {
2724 log_error("in order %d, invalid move direction %d.", i, orders[i].dir);
2725 return FALSE;
2726 }
2727 break;
2728 case ORDER_ACTIVITY:
2729 switch (orders[i].activity) {
2730 case ACTIVITY_SENTRY:
2731 if (i != length - 1) {
2732 /* Only allowed as the last order. */
2733 log_error("activity %d is not allowed at index %d.", orders[i].activity,
2734 i);
2735 return FALSE;
2736 }
2737 break;
2738 /* Replaced by action orders */
2739 case ACTIVITY_BASE:
2740 case ACTIVITY_GEN_ROAD:
2741 case ACTIVITY_CLEAN:
2742 case ACTIVITY_PILLAGE:
2743 case ACTIVITY_MINE:
2744 case ACTIVITY_IRRIGATE:
2745 case ACTIVITY_PLANT:
2746 case ACTIVITY_CULTIVATE:
2747 case ACTIVITY_TRANSFORM:
2748 case ACTIVITY_CONVERT:
2749 case ACTIVITY_FORTIFYING:
2750 log_error("at index %d, use action rather than activity %d.",
2751 i, orders[i].activity);
2752 return FALSE;
2753 /* Not supported. */
2754 case ACTIVITY_EXPLORE:
2755 case ACTIVITY_IDLE:
2756 /* Not set from the client. */
2757 case ACTIVITY_GOTO:
2758 case ACTIVITY_FORTIFIED:
2759 /* Unused. */
2760 case ACTIVITY_LAST:
2761 log_error("at index %d, unsupported activity %d.", i, orders[i].activity);
2762 return FALSE;
2763 }
2764
2765 break;
2766 case ORDER_PERFORM_ACTION:
2767 if (!action_id_exists(orders[i].action)) {
2768 /* Non-existent action. */
2769 log_error("at index %d, the action %d doesn't exist.", i, orders[i].action);
2770 return FALSE;
2771 }
2772
2773 paction = action_by_number(orders[i].action);
2774
2775 /* Validate main target. */
2776 if (index_to_tile(nmap, orders[i].target) == NULL) {
2777 log_error("at index %d, invalid tile target %d for the action %d.",
2778 i, orders[i].target, orders[i].action);
2779 return FALSE;
2780 }
2781
2782 if (orders[i].dir != DIR8_ORIGIN) {
2783 log_error("at index %d, the action %d sets the outdated target"
2784 " specification dir.",
2785 i, orders[i].action);
2786 }
2787
2788 /* Validate sub target. */
2789 switch (action_id_get_sub_target_kind(orders[i].action)) {
2790 case ASTK_BUILDING:
2791 /* Sub target is a building. */
2792 if (!improvement_by_number(orders[i].sub_target)) {
2793 /* Sub target is invalid. */
2794 log_error("at index %d, cannot do %s without a target.", i,
2795 action_id_rule_name(orders[i].action));
2796 return FALSE;
2797 }
2798 break;
2799 case ASTK_TECH:
2800 /* Sub target is a technology. */
2801 if (orders[i].sub_target == A_NONE
2802 || (!valid_advance_by_number(orders[i].sub_target)
2803 && orders[i].sub_target != A_FUTURE)) {
2804 /* Target tech is invalid. */
2805 log_error("at index %d, cannot do %s without a target.", i,
2806 action_id_rule_name(orders[i].action));
2807 return FALSE;
2808 }
2809 break;
2810 case ASTK_EXTRA:
2811 case ASTK_EXTRA_NOT_THERE:
2812 /* Sub target is an extra. */
2813 pextra = (!(orders[i].sub_target == NO_TARGET
2814 || (orders[i].sub_target < 0
2815 || (orders[i].sub_target
2816 >= game.control.num_extra_types)))
2817 ? extra_by_number(orders[i].sub_target) : NULL);
2818 fc_assert(pextra == NULL || !(pextra->ruledit_disabled));
2819 if (pextra == NULL) {
2820 if (paction->target_complexity != ACT_TGT_COMPL_FLEXIBLE) {
2821 /* Target extra is invalid. */
2822 log_error("at index %d, cannot do %s without a target.", i,
2823 action_id_rule_name(orders[i].action));
2824 return FALSE;
2825 }
2826 } else {
2827 if (!actres_removes_extra(paction->result, pextra)
2828 && !actres_creates_extra(paction->result, pextra)) {
2829 /* Target extra is irrelevant for the action. */
2830 log_error("at index %d, cannot do %s to %s.", i,
2831 action_id_rule_name(orders[i].action),
2832 extra_rule_name(pextra));
2833 return FALSE;
2834 }
2835 }
2836 break;
2837 case ASTK_NONE:
2838 /* No validation required. */
2839 break;
2840 /* Invalid action? */
2841 case ASTK_COUNT:
2842 fc_assert_ret_val_msg(
2843 action_id_get_sub_target_kind(orders[i].action) != ASTK_COUNT,
2844 FALSE,
2845 "Bad action %d in order number %d.", orders[i].action, i);
2846 }
2847
2848 /* Some action orders are sane only in the last order. */
2849 if (i != length - 1) {
2850 /* If the unit is dead, */
2851 if (utype_is_consumed_by_action(paction, NULL)
2852 /* or if Freeciv has no idea where the unit will end up after it
2853 * has performed this action, */
2854 || !(utype_is_unmoved_by_action(paction, NULL)
2855 || utype_is_moved_to_tgt_by_action(paction, NULL))
2856 /* or if the unit will end up standing still, */
2857 || action_has_result(paction, ACTRES_FORTIFY)) {
2858 /* than having this action in the middle of a unit's orders is
2859 * probably wrong. */
2860 log_error("action %d is not allowed at index %d.",
2861 orders[i].action, i);
2862 return FALSE;
2863 }
2864 }
2865
2866 /* Don't validate that the target tile really contains a target or
2867 * that the actor player's map think the target tile has one.
2868 * The player may target something from their player map that isn't
2869 * there any more, a target they think is there even if their player
2870 * map doesn't have it, or even a target they assume will be there
2871 * when the unit reaches the target tile.
2872 *
2873 * With that said: The client should probably at least have an
2874 * option to only aim city targeted actions at cities. */
2875
2876 break;
2877 case ORDER_FULL_MP:
2878 break;
2879 case ORDER_LAST:
2880 /* An invalid order. This is handled above. */
2881 break;
2882 }
2883 }
2884
2885 return TRUE;
2886}
2887
2888/**********************************************************************/
2892 struct unit_order *create_unit_orders(const struct civ_map *nmap,
2893 int length,
2894 const struct unit_order *orders)
2895{
2896 struct unit_order *unit_orders;
2897
2898 if (!unit_order_list_is_sane(nmap, length, orders)) {
2899 return NULL;
2900 }
2901
2902 unit_orders = fc_malloc(length * sizeof(*(unit_orders)));
2903 memcpy(unit_orders, orders, length * sizeof(*(unit_orders)));
2904
2905 return unit_orders;
2906}
2907
2908/**********************************************************************/
2911 enum gen_action activity_default_action(enum unit_activity act)
2912{
2913 enum gen_action act_act[ACTIVITY_LAST] = {
2914 ACTION_NONE, // ACTIVITY_IDLE
2915 ACTION_CULTIVATE, // ACTIVITY_CULTIVATE
2916 ACTION_MINE, // ACTIVITY_MINE
2917 ACTION_IRRIGATE, // ACTIVITY_IRRIGATE
2918 ACTION_FORTIFY, // ACTIVITY_FORTIFIED
2919 ACTION_NONE, // ACTIVITY_SENTRY
2920 ACTION_PILLAGE, // ACTIVITY_PILLAGE
2921 ACTION_NONE, // ACTIVITY_GOTO
2922 ACTION_NONE, // ACTIVITY_EXPLORE
2923 ACTION_TRANSFORM_TERRAIN, // ACTIVITY_TRANSFORM
2924 ACTION_FORTIFY, // ACTIVITY_FORTIFYING
2925 ACTION_CLEAN, // ACTIVITY_CLEAN
2926 ACTION_BASE, // ACTIVITY_BASE
2927 ACTION_ROAD, // ACTIVITY_GEN_ROAD
2928 ACTION_CONVERT, // ACTIVITY_CONVERT
2929 ACTION_PLANT // ACTIVITY_PLANT
2930 };
2931
2932 return act_act[act];
2933}
action_prob_possible
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5044
action_id_rule_name
const char * action_id_rule_name(action_id act_id)
Definition actions.c:1214
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:4562
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:3293
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:3052
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:3129
action_prob_vs_city
struct act_prob action_prob_vs_city(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct city *target_city)
Definition actions.c:4203
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:5467
action_id_exists
bool action_id_exists(const action_id act_id)
Definition actions.c:1043
is_action_enabled_unit_on_self
bool is_action_enabled_unit_on_self(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit)
Definition actions.c:3442
action_prob_vs_unit
struct act_prob action_prob_vs_unit(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct unit *target_unit)
Definition actions.c:4286
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:1575
tile_non_allied_city
struct city * tile_non_allied_city(const struct tile *ptile, const struct player *pplayer)
Definition city.c:2062
is_unit_near_a_friendly_city
bool is_unit_near_a_friendly_city(const struct civ_map *nmap, const struct unit *punit, int distance)
Definition city.c:2077
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:2938
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_unit_bonus
int get_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Definition effects.c:1066
get_target_bonus_effects
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct req_context *other_context, enum effect_type effect_type)
Definition effects.c:744
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:1031
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:779
extra_owner
struct player * extra_owner(const struct tile *ptile)
Definition extras.c:1128
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:1017
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:459
action_id
int action_id
Definition fc_types.h:393
HB_ALLIANCE
@ HB_ALLIANCE
Definition fc_types.h:1273
HB_DISABLED
@ HB_DISABLED
Definition fc_types.h:1271
HB_NATIONAL
@ HB_NATIONAL
Definition fc_types.h:1272
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:1017
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:61
wld
struct world wld
Definition game.c:62
game_unit_by_number
struct unit * game_unit_by_number(int id)
Definition game.c:115
game_city_by_number
struct city * game_city_by_number(int id)
Definition game.c:106
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:226
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:133
packet_game_info::happyborders
enum happyborders_type happyborders
Definition packets_gen.h:181
packet_game_info::airlifting_style
enum airlifting_style airlifting_style
Definition packets_gen.h:122
packet_game_info::airlift_from_always_enabled
bool airlift_from_always_enabled
Definition packets_gen.h:123
packet_game_info::turn
int turn
Definition packets_gen.h:245
packet_game_info::base_bribe_cost
int base_bribe_cost
Definition packets_gen.h:134
packet_game_info::airlift_to_always_enabled
bool airlift_to_always_enabled
Definition packets_gen.h:124
packet_ruleset_control::num_extra_types
int num_extra_types
Definition packets_gen.h:1442
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:406
cargo_iter_sizeof
size_t cargo_iter_sizeof(void)
Definition unit.c:2622
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:945
is_tile_activity
bool is_tile_activity(enum unit_activity activity)
Definition unit.c:1642
create_unit_orders
struct unit_order * create_unit_orders(const struct civ_map *nmap, int length, const struct unit_order *orders)
Definition unit.c:2892
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:1994
unit_being_aggressive
bool unit_being_aggressive(const struct unit *punit)
Definition unit.c:1556
unit_type_really_ignores_zoc
bool unit_type_really_ignores_zoc(const struct unit_type *punittype)
Definition unit.c:1542
unit_type_is_losing_hp
bool unit_type_is_losing_hp(const struct player *pplayer, const struct unit_type *punittype)
Definition unit.c:2278
is_terrain_change_activity
bool is_terrain_change_activity(enum unit_activity activity)
Definition unit.c:1627
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:1821
cargo_iter_next
static void cargo_iter_next(struct iterator *it)
Definition unit.c:2640
free_unit_orders
void free_unit_orders(struct unit *punit)
Definition unit.c:1807
can_unit_change_homecity
bool can_unit_change_homecity(const struct unit *punit)
Definition unit.c:509
unit_set_ai_data
void unit_set_ai_data(struct unit *punit, const struct ai_type *ai, void *data)
Definition unit.c:2328
unit_is_alive
bool unit_is_alive(int id)
Definition unit.c:2291
unit_can_est_trade_route_here
bool unit_can_est_trade_route_here(const struct unit *punit)
Definition unit.c:287
is_occupying_unit
bool is_occupying_unit(const struct unit *punit)
Definition unit.c:327
is_hiding_unit
bool is_hiding_unit(const struct unit *punit)
Definition unit.c:453
get_activity_rate_this_turn
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:550
unit_pays_mp_for_action
int unit_pays_mp_for_action(const struct action *paction, const struct unit *punit)
Definition unit.c:2173
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:566
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:1494
unit_transport_load
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2428
unit_can_add_or_build_city
bool unit_can_add_or_build_city(const struct unit *punit)
Definition unit.c:475
transporter_for_unit_at
struct unit * transporter_for_unit_at(const struct unit *pcargo, const struct tile *ptile)
Definition unit.c:1960
cargo_iter_init
struct iterator * cargo_iter_init(struct cargo_iter *iter, const struct unit *ptrans)
Definition unit.c:2682
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:2089
unit_transport_depth
int unit_transport_depth(const struct unit *pcargo)
Definition unit.c:2609
is_losing_hp
bool is_losing_hp(const struct unit *punit)
Definition unit.c:2266
is_targeted_activity
bool is_targeted_activity(enum unit_activity activity)
Definition unit.c:1652
can_unit_deboard_or_be_unloaded
bool can_unit_deboard_or_be_unloaded(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:801
can_unit_change_homecity_to
bool can_unit_change_homecity_to(const struct unit *punit, const struct city *pcity)
Definition unit.c:492
can_unit_load
bool can_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:749
tile_non_attack_unit
struct unit * tile_non_attack_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1377
hp_gain_coord
int hp_gain_coord(const struct unit *punit)
Definition unit.c:2200
cargo_iter_get
static void * cargo_iter_get(const struct iterator *it)
Definition unit.c:2630
unit_nationality
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1275
get_activity_rate
int get_activity_rate(const struct unit *punit)
Definition unit.c:522
unit_transport_check
bool unit_transport_check(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2537
is_attack_unit
bool is_attack_unit(const struct unit *punit)
Definition unit.c:308
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:2499
unit_transport_unload
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2448
is_build_activity
bool is_build_activity(enum unit_activity activity)
Definition unit.c:1597
unit_contained_in
bool unit_contained_in(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:2578
unit_gain_hitpoints
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2221
unit_bribe_cost
int unit_bribe_cost(const struct unit *punit, const struct player *briber, const struct unit *briber_unit)
Definition unit.c:2345
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:866
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:1098
unit_upkeep_astr
void unit_upkeep_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1261
could_unit_load
bool could_unit_load(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:724
unit_is_cityfounder
bool unit_is_cityfounder(const struct unit *punit)
Definition unit.c:2699
is_unit_activity_on_tile
bool is_unit_activity_on_tile(enum unit_activity activity, const struct tile *ptile)
Definition unit.c:1152
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:441
cargo_iter_valid
static bool cargo_iter_valid(const struct iterator *it)
Definition unit.c:2674
unit_can_do_action
bool unit_can_do_action(const struct unit *punit, const action_id act_id)
Definition unit.c:386
unit_can_do_action_result
bool unit_can_do_action_result(const struct unit *punit, enum action_result result)
Definition unit.c:396
activity_default_action
enum gen_action activity_default_action(enum unit_activity act)
Definition unit.c:2911
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:319
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:1662
unit_cargo_depth
int unit_cargo_depth(const struct unit *ptrans)
Definition unit.c:2591
can_unit_do_autoworker
bool can_unit_do_autoworker(const struct unit *punit)
Definition unit.c:630
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:2521
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:831
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:2707
get_activity_text
const char * get_activity_text(enum unit_activity activity)
Definition unit.c:638
get_unit_tile_pillage_set
bv_extras get_unit_tile_pillage_set(const struct tile *ptile)
Definition unit.c:1168
get_transporter_capacity
int get_transporter_capacity(const struct unit *punit)
Definition unit.c:300
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:1425
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:1133
is_enter_borders_unit
bool is_enter_borders_unit(const struct unit *punit)
Definition unit.c:335
is_clean_activity
bool is_clean_activity(enum unit_activity activity)
Definition unit.c:1613
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:847
unit_activity_astr
void unit_activity_astr(const struct unit *punit, struct astring *astr)
Definition unit.c:1185
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:893
tile_other_players_unit
struct unit * tile_other_players_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1361
is_special_unit
bool is_special_unit(const struct unit *punit)
Definition unit.c:353
unit_virtual_destroy
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1767
tile_enemy_unit
struct unit * tile_enemy_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1321
unit_tile_set
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1285
set_unit_activity
void set_unit_activity(struct unit *punit, enum unit_activity new_activity, enum gen_action trigger_action)
Definition unit.c:1115
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:2026
unit_transported
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2483
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:2308
is_flagless_to_player
bool is_flagless_to_player(const struct unit *punit, const struct player *pplayer)
Definition unit.c:366
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:415
is_guard_unit
bool is_guard_unit(const struct unit *punit)
Definition unit.c:343
can_unit_unload
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:776
unit_occupies_tile
struct unit * unit_occupies_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1398
unit_transport_cargo
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2509
can_type_transport_units_cargo
static bool can_type_transport_units_cargo(const struct unit_type *utype, const struct unit *punit)
Definition unit.c:1970
unit_ai_data
void * unit_ai_data(const struct unit *punit, const struct ai_type *ai)
Definition unit.c:2320
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:1298
transporter_for_unit
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1950
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:1831
unit_can_convert
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2073
activity_requires_target
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:590
could_unit_be_in_transport
bool could_unit_be_in_transport(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:692
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:1336
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:932
unit.h
unit_tile
#define unit_tile(_pu)
Definition unit.h:397
unit_transports_iterate_end
#define unit_transports_iterate_end
Definition unit.h:565
unit_cargo_iterate_end
#define unit_cargo_iterate_end
Definition unit.h:575
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:543
unit_cargo_iterate
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:572
unit_owner
#define unit_owner(_pu)
Definition unit.h:396
unit_transports_iterate
#define unit_transports_iterate(_pcargo, _ptrans)
Definition unit.h:561
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:546
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