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)
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 ***********************************************************************/
15 #include <fc_config.h>
20 #include "bitvector.h"
38 #include "traderoutes.h"
43 static bool is_real_activity(enum unit_activity activity
);
45 Activity_type_id real_activities
[ACTIVITY_LAST
];
48 struct iterator vtable
;
49 const struct unit_list_link
*links
[GAME_TRANSPORT_MAX_RECURSIVE
];
52 #define CARGO_ITER(iter) ((struct cargo_iter *) (iter))
54 /**************************************************************************
61 **************************************************************************/
63 /**************************************************************************
64 Whether a diplomat can move to a particular tile and perform a
65 particular action there.
66 **************************************************************************/
67 bool diplomat_can_do_action(const struct unit
*pdiplomat
,
68 enum diplomat_actions action
,
69 const struct tile
*ptile
)
71 if (!is_diplomat_action_available(pdiplomat
, action
, ptile
)) {
75 if (!is_tiles_adjacent(unit_tile(pdiplomat
), ptile
)
76 && !same_pos(unit_tile(pdiplomat
), ptile
)) {
80 if(pdiplomat
->moves_left
== 0)
86 /**************************************************************************
87 Whether a diplomat can perform a particular action at a particular
88 tile. This does _not_ check whether the diplomat can move there.
89 If the action is DIPLOMAT_ANY_ACTION, checks whether there is any
90 action the diplomat can perform at the tile.
91 **************************************************************************/
92 bool is_diplomat_action_available(const struct unit
*pdiplomat
,
93 enum diplomat_actions action
,
94 const struct tile
*ptile
)
96 struct city
*pcity
=tile_city(ptile
);
98 if (action
!= DIPLOMAT_MOVE
99 && !can_unit_exist_at_tile(pdiplomat
, unit_tile(pdiplomat
))) {
104 if (city_owner(pcity
) != unit_owner(pdiplomat
)
105 && real_map_distance(unit_tile(pdiplomat
), pcity
->tile
) <= 1) {
106 if(action
==DIPLOMAT_SABOTAGE
)
107 return pplayers_at_war(unit_owner(pdiplomat
), city_owner(pcity
));
108 if(action
==DIPLOMAT_MOVE
)
109 return pplayers_allied(unit_owner(pdiplomat
), city_owner(pcity
));
110 if (action
== DIPLOMAT_EMBASSY
111 && !get_player_bonus(city_owner(pcity
), EFT_NO_DIPLOMACY
)
112 && !player_has_real_embassy(unit_owner(pdiplomat
),
113 city_owner(pcity
))) {
116 if(action
== SPY_POISON
117 && city_size_get(pcity
) > 1
118 && unit_has_type_flag(pdiplomat
, UTYF_SPY
)) {
119 return pplayers_at_war(unit_owner(pdiplomat
), city_owner(pcity
));
121 if(action
==DIPLOMAT_INVESTIGATE
)
123 if (action
== DIPLOMAT_STEAL
&& !is_barbarian(city_owner(pcity
))) {
126 if(action
==DIPLOMAT_INCITE
)
127 return !pplayers_allied(city_owner(pcity
), unit_owner(pdiplomat
));
128 if(action
==DIPLOMAT_ANY_ACTION
)
131 } else { /* Action against a unit at a tile */
132 /* If it is made possible to do action against allied units
133 unit_move_handling() should be changed so that pdefender
134 is also set to allied units */
137 if ((action
== SPY_SABOTAGE_UNIT
|| action
== DIPLOMAT_ANY_ACTION
)
138 && unit_list_size(ptile
->units
) == 1
139 && unit_has_type_flag(pdiplomat
, UTYF_SPY
)) {
140 punit
= unit_list_get(ptile
->units
, 0);
141 if (pplayers_at_war(unit_owner(pdiplomat
), unit_owner(punit
))) {
146 if ((action
== DIPLOMAT_BRIBE
|| action
== DIPLOMAT_ANY_ACTION
)
147 && unit_list_size(ptile
->units
) == 1) {
148 punit
= unit_list_get(ptile
->units
, 0);
149 if (!pplayers_allied(unit_owner(punit
), unit_owner(pdiplomat
))) {
157 /****************************************************************************
158 Determines if punit can be airlifted to dest_city now! So punit needs
160 If pdest_city is NULL, just indicate whether it's possible for the unit
161 to be airlifted at all from its current position.
162 The 'restriction' parameter specifies which player's knowledge this is
163 based on -- one player can't see whether another's cities are currently
164 able to airlift. (Clients other than global observers should only call
165 this with a non-NULL 'restriction'.)
166 ****************************************************************************/
167 enum unit_airlift_result
168 test_unit_can_airlift_to(const struct player
*restriction
,
169 const struct unit
*punit
,
170 const struct city
*pdest_city
)
172 const struct city
*psrc_city
= tile_city(unit_tile(punit
));
173 const struct player
*punit_owner
;
174 enum unit_airlift_result ok_result
= AR_OK
;
176 if (0 == punit
->moves_left
) {
181 if (!is_ground_unit(punit
)) {
182 /* Only land units can be airlifted currently. */
183 return AR_WRONG_UNITTYPE
;
186 if (0 < get_transporter_occupancy(punit
)) {
187 /* Units with occupants can't be airlifted currently. */
191 if (NULL
== psrc_city
) {
193 return AR_NOT_IN_CITY
;
196 if (psrc_city
== pdest_city
) {
197 /* Airlifting to our current position doesn't make sense. */
198 return AR_BAD_DST_CITY
;
201 punit_owner
= unit_owner(punit
);
203 /* Check validity of both source and destination before checking capacity,
204 * to avoid misleadingly optimistic returns. */
206 if (punit_owner
!= city_owner(psrc_city
)
207 && !(game
.info
.airlifting_style
& AIRLIFTING_ALLIED_SRC
208 && pplayers_allied(punit_owner
, city_owner(psrc_city
)))) {
209 /* Not allowed to airlift from this source. */
210 return AR_BAD_SRC_CITY
;
214 punit_owner
!= city_owner(pdest_city
)
215 && !(game
.info
.airlifting_style
& AIRLIFTING_ALLIED_DEST
216 && pplayers_allied(punit_owner
, city_owner(pdest_city
)))) {
217 /* Not allowed to airlift to this destination. */
218 return AR_BAD_DST_CITY
;
221 if (NULL
== restriction
|| city_owner(psrc_city
) == restriction
) {
222 /* We know for sure whether or not src can airlift this turn. */
223 if (0 >= psrc_city
->airlift
) {
224 /* The source cannot airlift for this turn (maybe already airlifted
227 * Note that (game.info.airlifting_style & AIRLIFTING_UNLIMITED_SRC)
228 * is not handled here because it always needs an airport to airlift.
229 * See also do_airline() in server/unittools.h. */
230 return AR_SRC_NO_FLIGHTS
;
231 } /* else, there is capacity; continue to other checks */
233 /* We don't have access to the 'airlift' field. Assume it's OK; can
234 * only find out for sure by trying it. */
235 ok_result
= AR_OK_SRC_UNKNOWN
;
239 if (NULL
== restriction
|| city_owner(pdest_city
) == restriction
) {
240 if (0 >= pdest_city
->airlift
241 && !(game
.info
.airlifting_style
& AIRLIFTING_UNLIMITED_DEST
)) {
242 /* The destination cannot support airlifted units for this turn
243 * (maybe already airlifed or no airport).
244 * See also do_airline() in server/unittools.h. */
245 return AR_DST_NO_FLIGHTS
;
246 } /* else continue */
248 ok_result
= AR_OK_DST_UNKNOWN
;
255 /****************************************************************************
256 Encapsulates whether a return from test_unit_can_airlift_to() should be
257 treated as a successful result.
258 ****************************************************************************/
259 bool is_successful_airlift_result(enum unit_airlift_result result
)
263 case AR_OK_SRC_UNKNOWN
:
264 case AR_OK_DST_UNKNOWN
:
266 default: /* everything else is failure */
271 /****************************************************************************
272 Determines if punit can be airlifted to dest_city now! So punit needs
274 On the server this gives correct information; on the client it errs on the
275 side of saying airlifting is possible even if it's not certain given
277 ****************************************************************************/
278 bool unit_can_airlift_to(const struct unit
*punit
,
279 const struct city
*pdest_city
)
281 /* FIXME: really we want client_player(), not unit_owner(). */
282 struct player
*restriction
= is_server() ? NULL
: unit_owner(punit
);
283 fc_assert_ret_val(pdest_city
, FALSE
);
284 return is_successful_airlift_result(
285 test_unit_can_airlift_to(restriction
, punit
, pdest_city
));
288 /****************************************************************************
289 Return TRUE iff the unit is following client-side orders.
290 ****************************************************************************/
291 bool unit_has_orders(const struct unit
*punit
)
293 return punit
->has_orders
;
296 /**************************************************************************
297 Return TRUE iff this unit can be disbanded at the given city to get full
298 shields for building a wonder.
299 **************************************************************************/
300 bool unit_can_help_build_wonder(const struct unit
*punit
,
301 const struct city
*pcity
)
303 if (!is_tiles_adjacent(unit_tile(punit
), pcity
->tile
)
304 && !same_pos(unit_tile(punit
), pcity
->tile
)) {
308 return (unit_has_type_flag(punit
, UTYF_HELP_WONDER
)
309 && unit_owner(punit
) == city_owner(pcity
)
310 && VUT_IMPROVEMENT
== pcity
->production
.kind
311 && is_wonder(pcity
->production
.value
.building
)
312 && (pcity
->shield_stock
313 < impr_build_shield_cost(pcity
->production
.value
.building
)));
317 /**************************************************************************
318 Return TRUE iff this unit can be disbanded at its current position to
319 get full shields for building a wonder.
320 **************************************************************************/
321 bool unit_can_help_build_wonder_here(const struct unit
*punit
)
323 struct city
*pcity
= tile_city(unit_tile(punit
));
325 return pcity
&& unit_can_help_build_wonder(punit
, pcity
);
329 /**************************************************************************
330 Return TRUE iff this unit can be disbanded at its current location to
331 provide a trade route from the homecity to the target city.
332 **************************************************************************/
333 bool unit_can_est_trade_route_here(const struct unit
*punit
)
335 struct city
*phomecity
, *pdestcity
;
337 return (unit_has_type_flag(punit
, UTYF_TRADE_ROUTE
)
338 && (pdestcity
= tile_city(unit_tile(punit
)))
339 && (phomecity
= game_city_by_number(punit
->homecity
))
340 && can_cities_trade(phomecity
, pdestcity
));
343 /**************************************************************************
344 Return the number of units the transporter can hold (or 0).
345 **************************************************************************/
346 int get_transporter_capacity(const struct unit
*punit
)
348 return unit_type(punit
)->transport_capacity
;
351 /**************************************************************************
352 Is the unit capable of attacking?
353 **************************************************************************/
354 bool is_attack_unit(const struct unit
*punit
)
356 return (unit_type(punit
)->attack_strength
> 0);
359 /**************************************************************************
360 Military units are capable of enforcing martial law. Military ground
361 and heli units can occupy empty cities -- see unit_can_take_over(punit).
362 Some military units, like the Galleon, have no attack strength.
363 **************************************************************************/
364 bool is_military_unit(const struct unit
*punit
)
366 return !unit_has_type_flag(punit
, UTYF_CIVILIAN
);
369 /**************************************************************************
370 Return TRUE iff this unit is a diplomat (spy) unit. Diplomatic units
371 can do diplomatic actions (not to be confused with diplomacy).
372 **************************************************************************/
373 bool is_diplomat_unit(const struct unit
*punit
)
375 return (unit_has_type_flag(punit
, UTYF_DIPLOMAT
));
378 /**************************************************************************
379 Return TRUE iff this tile is threatened from any unit within 2 tiles.
380 **************************************************************************/
381 bool is_square_threatened(const struct player
*pplayer
,
382 const struct tile
*ptile
)
384 square_iterate(ptile
, 2, ptile1
) {
385 unit_list_iterate(ptile1
->units
, punit
) {
386 if (((pplayer
->ai_controlled
387 && !ai_handicap(pplayer
, H_FOG
))
388 || can_player_see_unit(pplayer
, punit
))
389 && pplayers_at_war(pplayer
, unit_owner(punit
))
390 && (is_diplomat_unit(punit
)
391 || (is_military_unit(punit
) && is_attack_unit(punit
)))
392 && (is_native_tile(unit_type(punit
), ptile
)
393 || (can_attack_non_native(unit_type(punit
))
394 && is_native_near_tile(unit_class(punit
), ptile
)))) {
397 } unit_list_iterate_end
;
398 } square_iterate_end
;
403 /**************************************************************************
404 This checks the "field unit" flag on the unit. Field units cause
405 unhappiness (under certain governments) even when they aren't abroad.
406 **************************************************************************/
407 bool is_field_unit(const struct unit
*punit
)
409 return unit_has_type_flag(punit
, UTYF_FIELDUNIT
);
413 /**************************************************************************
414 Is the unit one that is invisible on the map. A unit is invisible if
415 it has the UTYF_PARTIAL_INVIS flag or if it transported by a unit with
417 **************************************************************************/
418 bool is_hiding_unit(const struct unit
*punit
)
420 return (unit_has_type_flag(punit
, UTYF_PARTIAL_INVIS
)
421 || (unit_transported(punit
)
422 && unit_has_type_flag(unit_transport_get(punit
),
423 UTYF_PARTIAL_INVIS
)));
426 /**************************************************************************
427 Return TRUE iff an attack from this unit would kill a citizen in a city
428 (city walls protect against this).
429 **************************************************************************/
430 bool kills_citizen_after_attack(const struct unit
*punit
)
432 return game
.info
.killcitizen
433 && uclass_has_flag(unit_class(punit
), UCF_KILLCITIZEN
);
436 /****************************************************************************
437 Return TRUE iff this unit may be disbanded to add its pop_cost to a
438 city at its current location.
439 ****************************************************************************/
440 bool unit_can_add_to_city(const struct unit
*punit
)
442 return (UAB_ADD_OK
== unit_add_or_build_city_test(punit
));
445 /****************************************************************************
446 Return TRUE iff this unit is capable of building a new city at its
448 ****************************************************************************/
449 bool unit_can_build_city(const struct unit
*punit
)
451 return (UAB_BUILD_OK
== unit_add_or_build_city_test(punit
));
454 /****************************************************************************
455 Return TRUE iff this unit can add to a current city or build a new city
456 at its current location.
457 ****************************************************************************/
458 bool unit_can_add_or_build_city(const struct unit
*punit
)
460 enum unit_add_build_city_result res
= unit_add_or_build_city_test(punit
);
462 return (UAB_BUILD_OK
== res
|| UAB_ADD_OK
== res
);
465 /****************************************************************************
466 See if the unit can add to an existing city or build a new city at
467 its current location, and return a 'result' value telling what is
469 ****************************************************************************/
470 enum unit_add_build_city_result
471 unit_add_or_build_city_test(const struct unit
*punit
)
473 struct tile
*ptile
= unit_tile(punit
);
474 struct city
*pcity
= tile_city(ptile
);
475 bool is_build
= unit_has_type_flag(punit
, UTYF_CITIES
);
476 bool is_add
= unit_has_type_flag(punit
, UTYF_ADD_TO_CITY
);
479 /* Test if we can build. */
482 return UAB_NOT_BUILD_UNIT
;
484 if (punit
->moves_left
== 0) {
485 return UAB_NO_MOVES_BUILD
;
487 switch (city_build_here_test(ptile
, punit
)) {
490 case CB_BAD_CITY_TERRAIN
:
491 return UAB_BAD_CITY_TERRAIN
;
492 case CB_BAD_UNIT_TERRAIN
:
493 return UAB_BAD_UNIT_TERRAIN
;
495 return UAB_BAD_BORDERS
;
497 return UAB_NO_MIN_DIST
;
499 log_error("%s(): Internal error.", __FUNCTION__
);
500 return UAB_NO_MOVES_BUILD
; /* Returns something prohibitive. */
503 /* Test if we can add. */
505 return UAB_NOT_ADDABLE_UNIT
;
507 if (punit
->moves_left
== 0) {
508 return UAB_NO_MOVES_ADD
;
511 fc_assert(unit_pop_value(punit
) > 0);
512 new_pop
= city_size_get(pcity
) + unit_pop_value(punit
);
514 if (new_pop
> game
.info
.add_to_size_limit
) {
517 if (city_owner(pcity
) != unit_owner(punit
)) {
518 return UAB_NOT_OWNER
;
520 if (!city_can_grow_to(pcity
, new_pop
)) {
526 /**************************************************************************
527 Return TRUE iff the unit can change homecity to the given city.
528 **************************************************************************/
529 bool can_unit_change_homecity_to(const struct unit
*punit
,
530 const struct city
*pcity
)
532 struct city
*acity
= tile_city(unit_tile(punit
));
534 /* Requirements to change homecity:
536 * 1. Homeless units can't change homecity (this is a feature since
537 * being homeless is a big benefit).
538 * 2. The unit must be inside the city it is rehoming to.
539 * 3. Of course you can only have your own cities as homecity.
540 * 4. You can't rehome to the current homecity. */
541 return (punit
&& pcity
542 && punit
->homecity
> 0
544 && city_owner(acity
) == unit_owner(punit
)
545 && punit
->homecity
!= acity
->id
);
548 /**************************************************************************
549 Return TRUE iff the unit can change homecity at its current location.
550 **************************************************************************/
551 bool can_unit_change_homecity(const struct unit
*punit
)
553 return can_unit_change_homecity_to(punit
, tile_city(unit_tile(punit
)));
556 /**************************************************************************
557 Returns the speed of a unit doing an activity. This depends on the
558 veteran level and the base move_rate of the unit (regardless of HP or
559 effects). Usually this is just used for settlers but the value is also
560 used for military units doing fortify/pillage activities.
562 The speed is multiplied by ACTIVITY_COUNT.
563 **************************************************************************/
564 int get_activity_rate(const struct unit
*punit
)
566 const struct veteran_level
*vlevel
;
568 fc_assert_ret_val(punit
!= NULL
, 0);
570 vlevel
= utype_veteran_level(unit_type(punit
), punit
->veteran
);
571 fc_assert_ret_val(vlevel
!= NULL
, 0);
573 /* The speed of the settler depends on its base move_rate, not on
574 * the number of moves actually remaining or the adjusted move rate.
575 * This means sea formers won't have their activity rate increased by
576 * Magellan's, and it means injured units work just as fast as
577 * uninjured ones. Note the value is never less than SINGLE_MOVE. */
578 int move_rate
= unit_type(punit
)->move_rate
;
580 /* All settler actions are multiplied by ACTIVITY_COUNT. */
581 return ACTIVITY_FACTOR
582 * (float)vlevel
->power_fact
/ 100
583 * move_rate
/ SINGLE_MOVE
;
586 /**************************************************************************
587 Returns the amount of work a unit does (will do) on an activity this
588 turn. Units that have no MP do no work.
590 The speed is multiplied by ACTIVITY_COUNT.
591 **************************************************************************/
592 int get_activity_rate_this_turn(const struct unit
*punit
)
594 /* This logic is also coded in client/goto.c. */
595 if (punit
->moves_left
> 0) {
596 return get_activity_rate(punit
);
602 /**************************************************************************
603 Return the estimated number of turns for the worker unit to start and
604 complete the activity at the given location. This assumes no other
605 worker units are helping out, and doesn't take account of any work
606 already done by this unit.
607 **************************************************************************/
608 int get_turns_for_activity_at(const struct unit
*punit
,
609 enum unit_activity activity
,
610 const struct tile
*ptile
)
612 /* FIXME: This is just an approximation since we don't account for
613 * get_activity_rate_this_turn. */
614 int speed
= get_activity_rate(punit
);
615 int time
= tile_activity_time(activity
, ptile
);
617 if (time
>= 0 && speed
>= 0) {
618 return (time
- 1) / speed
+ 1; /* round up */
624 /**************************************************************************
625 Return the estimated number of turns for the worker unit to start and
626 complete the road at the given location. This assumes no other
627 worker units are helping out, and doesn't take account of any work
628 already done by this unit.
629 **************************************************************************/
630 int get_turns_for_road_at(const struct unit
*punit
,
631 const struct road_type
*proad
,
632 const struct tile
*ptile
)
634 /* FIXME: This is just an approximation since we don't account for
635 * get_activity_rate_this_turn. */
636 int speed
= get_activity_rate(punit
);
637 int time
= tile_activity_road_time(ptile
, road_number(proad
));
639 if (time
>= 0 && speed
>= 0) {
640 return (time
- 1) / speed
+ 1; /* round up */
646 /**************************************************************************
647 Return the estimated number of turns for the worker unit to start and
648 complete the base road at the given location. This assumes no other
649 worker units are helping out, and doesn't take account of any work
650 already done by this unit.
651 **************************************************************************/
652 int get_turns_for_base_at(const struct unit
*punit
,
653 const struct base_type
*pbase
,
654 const struct tile
*ptile
)
656 /* FIXME: This is just an approximation since we don't account for
657 * get_activity_rate_this_turn. */
658 int speed
= get_activity_rate(punit
);
659 int time
= tile_activity_base_time(ptile
, base_number(pbase
));
661 if (time
>= 0 && speed
>= 0) {
662 return (time
- 1) / speed
+ 1; /* round up */
668 /**************************************************************************
669 Return TRUE if activity requires some sort of target to be specified.
670 **************************************************************************/
671 bool activity_requires_target(enum unit_activity activity
)
674 case ACTIVITY_PILLAGE
:
676 case ACTIVITY_GEN_ROAD
:
679 case ACTIVITY_POLLUTION
:
681 case ACTIVITY_IRRIGATE
:
682 case ACTIVITY_FORTIFIED
:
683 case ACTIVITY_SENTRY
:
685 case ACTIVITY_EXPLORE
:
686 case ACTIVITY_TRANSFORM
:
687 case ACTIVITY_FORTIFYING
:
688 case ACTIVITY_FALLOUT
:
689 case ACTIVITY_CONVERT
:
691 /* These shouldn't be kicking around internally. */
692 case ACTIVITY_FORTRESS
:
693 case ACTIVITY_AIRBASE
:
694 case ACTIVITY_PATROL_UNUSED
:
696 fc_assert_ret_val(FALSE
, FALSE
);
700 /**************************************************************************
701 Return whether the unit can be put in auto-settler mode.
703 NOTE: we used to have "auto" mode including autosettlers and auto-attack.
704 This was bad because the two were indestinguishable even though they
705 are very different. Now auto-attack is done differently so we just have
706 auto-settlers. If any new auto modes are introduced they should be
708 **************************************************************************/
709 bool can_unit_do_autosettlers(const struct unit
*punit
)
711 return unit_has_type_flag(punit
, UTYF_SETTLERS
);
714 /**************************************************************************
715 Setup array of real activities
716 **************************************************************************/
717 void setup_real_activities_array(void)
719 Activity_type_id act
;
722 for (act
= 0; act
< ACTIVITY_LAST
; act
++) {
723 if (is_real_activity(act
)) {
724 real_activities
[i
++] = act
;
728 real_activities
[i
] = ACTIVITY_LAST
;
731 /**************************************************************************
732 Return if given activity really is in game. For savegame compatibility
733 activity enum cannot be reordered and there is holes in it.
734 **************************************************************************/
735 static bool is_real_activity(enum unit_activity activity
)
737 /* ACTIVITY_FORTRESS, ACTIVITY_AIRBASE, ACTIVITY_OLD_ROAD, and
738 * ACTIVITY_OLD_RAILROAD are deprecated */
739 return (0 <= activity
&& activity
< ACTIVITY_LAST
)
740 && activity
!= ACTIVITY_FORTRESS
741 && activity
!= ACTIVITY_AIRBASE
742 && activity
!= ACTIVITY_OLD_ROAD
743 && activity
!= ACTIVITY_OLD_RAILROAD
744 && activity
!= ACTIVITY_UNKNOWN
745 && activity
!= ACTIVITY_PATROL_UNUSED
;
748 /**************************************************************************
749 Return the name of the activity in a static buffer.
750 **************************************************************************/
751 const char *get_activity_text(enum unit_activity activity
)
753 /* The switch statement has just the activities listed with no "default"
754 * handling. This enables the compiler to detect missing entries
755 * automatically, and still handles everything correctly. */
759 case ACTIVITY_POLLUTION
:
760 return _("Pollution");
763 case ACTIVITY_IRRIGATE
:
764 return _("Irrigation");
765 case ACTIVITY_FORTIFYING
:
766 return _("Fortifying");
767 case ACTIVITY_FORTIFIED
:
768 return _("Fortified");
769 case ACTIVITY_SENTRY
:
771 case ACTIVITY_PILLAGE
:
775 case ACTIVITY_EXPLORE
:
777 case ACTIVITY_TRANSFORM
:
778 return _("Transform");
779 case ACTIVITY_FALLOUT
:
783 case ACTIVITY_GEN_ROAD
:
785 case ACTIVITY_CONVERT
:
787 case ACTIVITY_OLD_ROAD
:
788 case ACTIVITY_OLD_RAILROAD
:
789 case ACTIVITY_FORTRESS
:
790 case ACTIVITY_AIRBASE
:
791 case ACTIVITY_UNKNOWN
:
792 case ACTIVITY_PATROL_UNUSED
:
801 /****************************************************************************
802 Return TRUE iff the given unit could be loaded into the transporter
804 ****************************************************************************/
805 bool could_unit_load(const struct unit
*pcargo
, const struct unit
*ptrans
)
807 if (!pcargo
|| !ptrans
|| pcargo
== ptrans
) {
811 /* Double-check ownership of the units: you can load into an allied unit
812 * (of course only allied units can be on the same tile). */
813 if (!pplayers_allied(unit_owner(pcargo
), unit_owner(ptrans
))) {
817 /* Make sure this transporter can carry this type of unit. */
818 if (!can_unit_transport(ptrans
, pcargo
)) {
822 /* Un-embarkable transport must be in city or base to load cargo. */
823 if (!utype_can_freely_load(unit_type(pcargo
), unit_type(ptrans
))
824 && !tile_city(unit_tile(ptrans
))
825 && !tile_has_native_base(unit_tile(ptrans
), unit_type(ptrans
))) {
829 /* Make sure there's room in the transporter. */
830 if (get_transporter_occupancy(ptrans
)
831 >= get_transporter_capacity(ptrans
)) {
835 /* Check iff this is a valid transport. */
836 if (!unit_transport_check(pcargo
, ptrans
)) {
840 /* Check transport depth. */
841 if (GAME_TRANSPORT_MAX_RECURSIVE
842 < 1 + unit_transport_depth(ptrans
) + unit_cargo_depth(pcargo
)) {
849 /****************************************************************************
850 Return TRUE iff the given unit can be loaded into the transporter.
851 ****************************************************************************/
852 bool can_unit_load(const struct unit
*pcargo
, const struct unit
*ptrans
)
854 /* This function needs to check EVERYTHING. */
856 /* Check positions of the units. Of course you can't load a unit onto
857 * a transporter on a different tile... */
858 if (!same_pos(unit_tile(pcargo
), unit_tile(ptrans
))) {
862 /* Cannot load if cargo is already loaded onto something else. */
863 if (unit_transported(pcargo
)) {
867 return could_unit_load(pcargo
, ptrans
);
870 /****************************************************************************
871 Return TRUE iff the given unit can be unloaded from its current
874 This function checks everything *except* the legality of the position
875 after the unloading. The caller may also want to call
876 can_unit_exist_at_tile() to check this, unless the unit is unloading and
877 moving at the same time.
878 ****************************************************************************/
879 bool can_unit_unload(const struct unit
*pcargo
, const struct unit
*ptrans
)
881 if (!pcargo
|| !ptrans
) {
885 /* Make sure the unit's transporter exists and is known. */
886 if (unit_transport_get(pcargo
) != ptrans
) {
890 /* Un-disembarkable transport must be in city or base to unload cargo. */
891 if (!utype_can_freely_unload(unit_type(pcargo
), unit_type(ptrans
))
892 && !tile_city(unit_tile(ptrans
))
893 && !tile_has_native_base(unit_tile(ptrans
), unit_type(ptrans
))) {
900 /**************************************************************************
901 Return whether the unit can be paradropped - that is, if the unit is in
902 a friendly city or on an airbase special, has enough movepoints left, and
903 has not paradropped yet this turn.
904 **************************************************************************/
905 bool can_unit_paradrop(const struct unit
*punit
)
907 struct unit_type
*utype
;
909 if (!unit_has_type_flag(punit
, UTYF_PARATROOPERS
))
912 if(punit
->paradropped
)
915 utype
= unit_type(punit
);
917 if(punit
->moves_left
< utype
->paratroopers_mr_req
)
920 if (tile_has_base_flag(unit_tile(punit
), BF_PARADROP_FROM
)) {
921 /* Paradrop has to be possible from non-native base.
922 * Paratroopers are "Land" units, but they can paradrom from Airbase. */
926 if (!tile_city(unit_tile(punit
))) {
933 /**************************************************************************
934 Return whether the unit can bombard.
935 Basically if it is a bombarder, isn't being transported, and hasn't
937 **************************************************************************/
938 bool can_unit_bombard(const struct unit
*punit
)
940 if (!unit_has_type_flag(punit
, UTYF_BOMBARDER
)) {
944 if (unit_transported(punit
)) {
951 /**************************************************************************
952 Compare if action targets are identical
953 **************************************************************************/
954 bool cmp_act_tgt(struct act_tgt
*act1
, struct act_tgt
*act2
)
956 if (act1
->type
!= act2
->type
) {
960 switch (act1
->type
) {
962 return act1
->obj
.spe
== act2
->obj
.spe
;
964 return act1
->obj
.base
== act2
->obj
.base
;
966 return act1
->obj
.road
== act2
->obj
.road
;
973 /**************************************************************************
974 Check if the unit's current activity is actually legal.
975 **************************************************************************/
976 bool can_unit_continue_current_activity(struct unit
*punit
)
978 enum unit_activity current
= punit
->activity
;
979 struct act_tgt target
= punit
->activity_target
;
980 enum unit_activity current2
=
981 (current
== ACTIVITY_FORTIFIED
) ? ACTIVITY_FORTIFYING
: current
;
984 punit
->activity
= ACTIVITY_IDLE
;
985 punit
->activity_target
.type
= ATT_SPECIAL
;
986 punit
->activity_target
.obj
.spe
= S_LAST
;
988 result
= can_unit_do_activity_targeted(punit
, current2
, &target
);
990 punit
->activity
= current
;
991 punit
->activity_target
= target
;
996 /**************************************************************************
997 Return TRUE iff the unit can do the given untargeted activity at its
1000 Note that some activities must be targeted; see
1001 can_unit_do_activity_targeted.
1002 **************************************************************************/
1003 bool can_unit_do_activity(const struct unit
*punit
,
1004 enum unit_activity activity
)
1006 struct act_tgt target
= { .type
= ATT_SPECIAL
, .obj
.spe
= S_LAST
};
1008 return can_unit_do_activity_targeted(punit
, activity
, &target
);
1011 /**************************************************************************
1012 Return TRUE iff the unit can do the given base building activity at its
1014 **************************************************************************/
1015 bool can_unit_do_activity_base(const struct unit
*punit
,
1018 struct act_tgt target
= { .type
= ATT_BASE
, .obj
.base
= base
};
1020 return can_unit_do_activity_targeted(punit
, ACTIVITY_BASE
, &target
);
1023 /**************************************************************************
1024 Return TRUE iff the unit can do the given road building activity at its
1026 **************************************************************************/
1027 bool can_unit_do_activity_road(const struct unit
*punit
,
1030 struct act_tgt target
= { .type
= ATT_ROAD
, .obj
.road
= road
};
1032 return can_unit_do_activity_targeted(punit
, ACTIVITY_GEN_ROAD
, &target
);
1035 /**************************************************************************
1036 Return whether the unit can do the targeted activity at its current
1038 **************************************************************************/
1039 bool can_unit_do_activity_targeted(const struct unit
*punit
,
1040 enum unit_activity activity
,
1041 struct act_tgt
*target
)
1043 return can_unit_do_activity_targeted_at(punit
, activity
, target
,
1047 /**************************************************************************
1048 Return TRUE if the unit can do the targeted activity at the given
1051 Note that if you make changes here you should also change the code for
1052 autosettlers in server/settler.c. The code there does not use this
1053 function as it would be a major CPU hog.
1054 **************************************************************************/
1055 bool can_unit_do_activity_targeted_at(const struct unit
*punit
,
1056 enum unit_activity activity
,
1057 struct act_tgt
*target
,
1058 const struct tile
*ptile
)
1060 struct player
*pplayer
= unit_owner(punit
);
1061 struct terrain
*pterrain
= tile_terrain(ptile
);
1062 struct unit_class
*pclass
= unit_class(punit
);
1069 case ACTIVITY_POLLUTION
:
1070 return (unit_has_type_flag(punit
, UTYF_SETTLERS
)
1071 && tile_has_special(ptile
, S_POLLUTION
));
1073 case ACTIVITY_FALLOUT
:
1074 return (unit_has_type_flag(punit
, UTYF_SETTLERS
)
1075 && tile_has_special(ptile
, S_FALLOUT
));
1078 /* Don't allow it if someone else is irrigating this tile.
1079 * *Do* allow it if they're transforming - the mine may survive */
1080 if (unit_has_type_flag(punit
, UTYF_SETTLERS
)
1081 && ((pterrain
== pterrain
->mining_result
1082 && !tile_has_special(ptile
, S_MINE
)
1083 && get_tile_bonus(ptile
, punit
, EFT_MINING_POSSIBLE
) > 0)
1084 || (pterrain
!= pterrain
->mining_result
1085 && pterrain
->mining_result
!= T_NONE
1086 && get_tile_bonus(ptile
, punit
, EFT_MINING_TF_POSSIBLE
) > 0
1087 && (!is_ocean(pterrain
)
1088 || is_ocean(pterrain
->mining_result
)
1089 || can_reclaim_ocean(ptile
))
1090 && (is_ocean(pterrain
)
1091 || !is_ocean(pterrain
->mining_result
)
1092 || can_channel_land(ptile
))
1093 && (!terrain_has_flag(pterrain
->mining_result
, TER_NO_CITIES
)
1094 || !tile_city(ptile
))))) {
1095 unit_list_iterate(ptile
->units
, tunit
) {
1096 if (tunit
->activity
== ACTIVITY_IRRIGATE
) {
1099 } unit_list_iterate_end
;
1105 case ACTIVITY_IRRIGATE
:
1106 /* Don't allow it if someone else is mining this tile.
1107 * *Do* allow it if they're transforming - the irrigation may survive */
1108 if (unit_has_type_flag(punit
, UTYF_SETTLERS
)
1109 && (!tile_has_special(ptile
, S_IRRIGATION
)
1110 || (!tile_has_special(ptile
, S_FARMLAND
)
1111 && player_knows_techs_with_flag(pplayer
, TF_FARMLAND
)))
1112 && ((pterrain
== pterrain
->irrigation_result
1113 && can_be_irrigated(ptile
, punit
))
1114 || (pterrain
!= pterrain
->irrigation_result
1115 && pterrain
->irrigation_result
!= T_NONE
1116 && get_tile_bonus(ptile
, punit
, EFT_IRRIG_TF_POSSIBLE
) > 0
1117 && (!is_ocean(pterrain
)
1118 || is_ocean(pterrain
->irrigation_result
)
1119 || can_reclaim_ocean(ptile
))
1120 && (is_ocean(pterrain
)
1121 || !is_ocean(pterrain
->irrigation_result
)
1122 || can_channel_land(ptile
))
1123 && (!terrain_has_flag(pterrain
->irrigation_result
, TER_NO_CITIES
)
1124 || !tile_city(ptile
))))) {
1125 unit_list_iterate(ptile
->units
, tunit
) {
1126 if (tunit
->activity
== ACTIVITY_MINE
) {
1129 } unit_list_iterate_end
;
1135 case ACTIVITY_FORTIFYING
:
1136 return (uclass_has_flag(pclass
, UCF_CAN_FORTIFY
)
1137 && punit
->activity
!= ACTIVITY_FORTIFIED
1138 && !unit_has_type_flag(punit
, UTYF_SETTLERS
)
1139 && (!is_ocean(pterrain
) || tile_city(ptile
)));
1141 case ACTIVITY_FORTIFIED
:
1145 return can_build_base(punit
, base_by_number(target
->obj
.base
), ptile
);
1147 case ACTIVITY_GEN_ROAD
:
1148 return can_build_road(road_by_number(target
->obj
.road
), punit
, ptile
);
1150 case ACTIVITY_SENTRY
:
1151 if (!can_unit_survive_at_tile(punit
, unit_tile(punit
))
1152 && !unit_transported(punit
)) {
1153 /* Don't let units sentry on tiles they will die on. */
1158 case ACTIVITY_PILLAGE
:
1160 if (uclass_has_flag(pclass
, UCF_CAN_PILLAGE
)) {
1161 bv_special pspresent
= get_tile_infrastructure_set(ptile
, NULL
);
1162 bv_bases bspresent
= get_tile_pillageable_base_set(ptile
, NULL
);
1163 bv_roads rspresent
= get_tile_pillageable_road_set(ptile
, NULL
);
1164 bv_special psworking
= get_unit_tile_pillage_set(ptile
);
1165 bv_bases bsworking
= get_unit_tile_pillage_base_set(ptile
);
1166 bv_roads rsworking
= get_unit_tile_pillage_road_set(ptile
);
1167 bv_special pspossible
;
1168 bv_bases bspossible
;
1169 bv_roads rspossible
;
1171 BV_CLR_ALL(pspossible
);
1172 tile_special_type_iterate(spe
) {
1173 /* Only one unit can pillage a given improvement at a time */
1174 if (BV_ISSET(pspresent
, spe
) && !BV_ISSET(psworking
, spe
)) {
1175 BV_SET(pspossible
, spe
);
1177 } tile_special_type_iterate_end
;
1178 tile_special_type_iterate(spe
) {
1179 enum tile_special_type prereq
= get_infrastructure_prereq(spe
);
1180 /* If an improvement is present, we can't pillage its prerequisite */
1181 /* (FIXME: Could in principle allow simultaneous pillaging of
1182 * an improvement and its prerequisite, but this would require care
1183 * to ensure that the unit pillaging the topmost improvement
1184 * finished first.) */
1185 if (prereq
!= S_LAST
&& BV_ISSET(pspresent
, spe
)) {
1186 BV_CLR(pspossible
, prereq
);
1188 } tile_special_type_iterate_end
;
1190 BV_CLR_ALL(bspossible
);
1191 base_type_iterate(bp
) {
1192 struct city
*pcity
= tile_city(ptile
);
1193 bool cannot_pillage
= FALSE
;
1195 /* Cannot pillage BF_ALWAYS_ON_CITY_CENTER bases from city center */
1196 if (pcity
!= NULL
) {
1197 if (base_has_flag(bp
, BF_ALWAYS_ON_CITY_CENTER
)) {
1198 cannot_pillage
= TRUE
;
1199 } else if (base_has_flag(bp
, BF_AUTO_ON_CITY_CENTER
)) {
1200 struct tile
*vtile
= tile_virtual_new(ptile
);
1202 /* Would base get rebuilt if removed */
1203 tile_remove_base(vtile
, bp
);
1204 if (player_can_build_base(bp
, city_owner(pcity
), vtile
)) {
1205 /* No need to worry about conflicting bases - base would had
1206 * not been here if conflicting one is. */
1207 cannot_pillage
= TRUE
;
1210 tile_virtual_destroy(vtile
);
1214 if (!cannot_pillage
) {
1215 Base_type_id b
= base_index(bp
);
1217 if (BV_ISSET(bspresent
, b
) && !BV_ISSET(bsworking
, b
)) {
1218 BV_SET(bspossible
, b
);
1221 } base_type_iterate_end
;
1223 BV_CLR_ALL(rspossible
);
1224 road_type_iterate(pr
) {
1225 struct city
*pcity
= tile_city(ptile
);
1226 bool cannot_pillage
= FALSE
;
1228 /* Cannot pillage RF_ALWAYS_ON_CITY_CENTER roads from city center */
1229 if (pcity
!= NULL
) {
1230 if (road_has_flag(pr
, RF_ALWAYS_ON_CITY_CENTER
)) {
1231 cannot_pillage
= TRUE
;
1232 } else if (road_has_flag(pr
, RF_AUTO_ON_CITY_CENTER
)) {
1233 struct tile
*vtile
= tile_virtual_new(ptile
);
1235 /* Would road get rebuilt if removed */
1236 tile_remove_road(vtile
, pr
);
1237 if (player_can_build_road(pr
, city_owner(pcity
), vtile
)) {
1238 /* No need to worry about conflicting roads - road would had
1239 * not been here if conflicting one is. */
1240 cannot_pillage
= TRUE
;
1243 tile_virtual_destroy(vtile
);
1247 if (!cannot_pillage
) {
1248 Road_type_id r
= road_index(pr
);
1250 if (BV_ISSET(rspresent
, r
) && !BV_ISSET(rsworking
, r
)) {
1251 BV_SET(rspossible
, r
);
1254 } road_type_iterate_end
;
1256 if (!BV_ISSET_ANY(pspossible
)
1257 && !BV_ISSET_ANY(bspossible
)
1258 && !BV_ISSET_ANY(rspossible
)) {
1259 /* Nothing available to pillage */
1263 if (target
->type
== ATT_SPECIAL
&& target
->obj
.spe
== S_LAST
) {
1264 /* Undirected pillaging. If we've got this far, then there's
1265 * *something* we can pillage; work out what when we come to it */
1268 if (!game
.info
.pillage_select
) {
1269 /* Hobson's choice (this case mostly exists for old clients) */
1270 /* Needs to match what unit_activity_assign_target chooses */
1272 bool found
= get_preferred_pillage(&tgt
, pspossible
, bspossible
, rspossible
);
1274 fc_assert_ret_val(found
, FALSE
);
1276 if (!cmp_act_tgt(&tgt
, target
)) {
1277 /* Only one target allowed, which wasn't the requested one */
1281 if (target
->type
== ATT_SPECIAL
) {
1282 return BV_ISSET(pspossible
, target
->obj
.spe
);
1283 } else if (target
->type
== ATT_BASE
) {
1284 return BV_ISSET(bspossible
, target
->obj
.base
);
1286 fc_assert(target
->type
== ATT_ROAD
);
1287 return BV_ISSET(rspossible
, target
->obj
.road
);
1291 /* Unit is not a type that can pillage at all */
1296 case ACTIVITY_EXPLORE
:
1297 return (!unit_type(punit
)->fuel
&& !is_losing_hp(punit
));
1299 case ACTIVITY_TRANSFORM
:
1300 return (pterrain
->transform_result
!= T_NONE
1301 && pterrain
!= pterrain
->transform_result
1302 && (!is_ocean(pterrain
)
1303 || is_ocean(pterrain
->transform_result
)
1304 || can_reclaim_ocean(ptile
))
1305 && (is_ocean(pterrain
)
1306 || !is_ocean(pterrain
->transform_result
)
1307 || can_channel_land(ptile
))
1308 && (!terrain_has_flag(pterrain
->transform_result
, TER_NO_CITIES
)
1309 || !(tile_city(ptile
)))
1310 && get_tile_bonus(ptile
, punit
, EFT_TRANSFORM_POSSIBLE
) > 0);
1312 case ACTIVITY_CONVERT
:
1313 return unit_can_convert(punit
);
1315 case ACTIVITY_OLD_ROAD
:
1316 case ACTIVITY_OLD_RAILROAD
:
1317 case ACTIVITY_FORTRESS
:
1318 case ACTIVITY_AIRBASE
:
1319 case ACTIVITY_PATROL_UNUSED
:
1321 case ACTIVITY_UNKNOWN
:
1324 log_error("can_unit_do_activity_targeted_at() unknown activity %d",
1329 /**************************************************************************
1330 Assign a new task to a unit. Doesn't account for changed_from.
1331 **************************************************************************/
1332 static void set_unit_activity_internal(struct unit
*punit
,
1333 enum unit_activity new_activity
)
1335 fc_assert_ret(new_activity
!= ACTIVITY_FORTRESS
1336 && new_activity
!= ACTIVITY_AIRBASE
);
1338 punit
->activity
= new_activity
;
1339 punit
->activity_count
= 0;
1340 punit
->activity_target
.type
= ATT_SPECIAL
;
1341 punit
->activity_target
.obj
.spe
= S_LAST
;
1342 if (new_activity
== ACTIVITY_IDLE
&& punit
->moves_left
> 0) {
1343 /* No longer done. */
1344 punit
->done_moving
= FALSE
;
1348 /**************************************************************************
1349 assign a new untargeted task to a unit.
1350 **************************************************************************/
1351 void set_unit_activity(struct unit
*punit
, enum unit_activity new_activity
)
1353 fc_assert_ret(!activity_requires_target(new_activity
));
1355 if (new_activity
== ACTIVITY_FORTIFYING
1356 && punit
->changed_from
== ACTIVITY_FORTIFIED
) {
1357 new_activity
= ACTIVITY_FORTIFIED
;
1359 set_unit_activity_internal(punit
, new_activity
);
1360 if (new_activity
== punit
->changed_from
) {
1361 punit
->activity_count
= punit
->changed_from_count
;
1365 /**************************************************************************
1366 assign a new targeted task to a unit.
1367 **************************************************************************/
1368 void set_unit_activity_targeted(struct unit
*punit
,
1369 enum unit_activity new_activity
,
1370 struct act_tgt
*new_target
)
1372 fc_assert_ret(activity_requires_target(new_activity
));
1374 set_unit_activity_internal(punit
, new_activity
);
1375 punit
->activity_target
= *new_target
;
1376 if (new_activity
== punit
->changed_from
1377 && cmp_act_tgt(new_target
, &punit
->changed_from_target
)) {
1378 punit
->activity_count
= punit
->changed_from_count
;
1382 /**************************************************************************
1383 Assign a new base building task to unit
1384 **************************************************************************/
1385 void set_unit_activity_base(struct unit
*punit
,
1388 set_unit_activity_internal(punit
, ACTIVITY_BASE
);
1389 punit
->activity_target
.type
= ATT_BASE
;
1390 punit
->activity_target
.obj
.base
= base
;
1391 if (ACTIVITY_BASE
== punit
->changed_from
1392 && cmp_act_tgt(&punit
->activity_target
, &punit
->changed_from_target
)) {
1393 punit
->activity_count
= punit
->changed_from_count
;
1397 /**************************************************************************
1398 Assign a new road building task to unit
1399 **************************************************************************/
1400 void set_unit_activity_road(struct unit
*punit
,
1403 set_unit_activity_internal(punit
, ACTIVITY_GEN_ROAD
);
1404 punit
->activity_target
.type
= ATT_ROAD
;
1405 punit
->activity_target
.obj
.road
= road
;
1406 if (ACTIVITY_GEN_ROAD
== punit
->changed_from
1407 && cmp_act_tgt(&punit
->activity_target
, &punit
->changed_from_target
)) {
1408 punit
->activity_count
= punit
->changed_from_count
;
1412 /**************************************************************************
1413 Return whether any units on the tile are doing this activity.
1414 **************************************************************************/
1415 bool is_unit_activity_on_tile(enum unit_activity activity
,
1416 const struct tile
*ptile
)
1418 unit_list_iterate(ptile
->units
, punit
) {
1419 if (punit
->activity
== activity
) {
1422 } unit_list_iterate_end
;
1426 /****************************************************************************
1427 Return a mask of the specials which are actively (currently) being
1428 pillaged on the given tile.
1429 ****************************************************************************/
1430 bv_special
get_unit_tile_pillage_set(const struct tile
*ptile
)
1434 BV_CLR_ALL(tgt_ret
);
1435 unit_list_iterate(ptile
->units
, punit
) {
1436 if (punit
->activity
== ACTIVITY_PILLAGE
1437 && punit
->activity_target
.type
== ATT_SPECIAL
) {
1438 fc_assert_action(punit
->activity_target
.obj
.spe
< S_LAST
, continue);
1439 BV_SET(tgt_ret
, punit
->activity_target
.obj
.spe
);
1441 } unit_list_iterate_end
;
1446 /****************************************************************************
1447 Return a mask of the bases which are actively (currently) being
1448 pillaged on the given tile.
1449 ****************************************************************************/
1450 bv_bases
get_unit_tile_pillage_base_set(const struct tile
*ptile
)
1454 BV_CLR_ALL(tgt_ret
);
1455 unit_list_iterate(ptile
->units
, punit
) {
1456 if (punit
->activity
== ACTIVITY_PILLAGE
1457 && punit
->activity_target
.type
== ATT_BASE
) {
1458 fc_assert(punit
->activity_target
.obj
.base
< base_count());
1459 BV_SET(tgt_ret
, punit
->activity_target
.obj
.base
);
1461 } unit_list_iterate_end
;
1466 /****************************************************************************
1467 Return a mask of the roads which are actively (currently) being
1468 pillaged on the given tile.
1469 ****************************************************************************/
1470 bv_roads
get_unit_tile_pillage_road_set(const struct tile
*ptile
)
1474 BV_CLR_ALL(tgt_ret
);
1475 unit_list_iterate(ptile
->units
, punit
) {
1476 if (punit
->activity
== ACTIVITY_PILLAGE
1477 && punit
->activity_target
.type
== ATT_ROAD
) {
1478 fc_assert(punit
->activity_target
.obj
.road
< road_count());
1479 BV_SET(tgt_ret
, punit
->activity_target
.obj
.road
);
1481 } unit_list_iterate_end
;
1486 /**************************************************************************
1487 Return text describing the unit's current activity as a static string.
1489 FIXME: Convert all callers of this function to unit_activity_astr()
1490 because this function is not re-entrant.
1491 **************************************************************************/
1492 const char *unit_activity_text(const struct unit
*punit
) {
1493 static struct astring str
= ASTRING_INIT
;
1496 unit_activity_astr(punit
, &str
);
1498 return astr_str(&str
);
1501 /**************************************************************************
1502 Append text describing the unit's current activity to the given astring.
1503 **************************************************************************/
1504 void unit_activity_astr(const struct unit
*punit
, struct astring
*astr
)
1506 if (!punit
|| !astr
) {
1510 switch (punit
->activity
) {
1512 if (utype_fuel(unit_type(punit
))) {
1514 rate
= unit_type(punit
)->move_rate
;
1515 f
= ((punit
->fuel
) - 1);
1517 /* Add in two parts as move_points_text() returns ptr to static
1518 * End result: "Moves: (fuel)moves_left" */
1519 astr_add_line(astr
, "%s: (%s)", _("Moves"),
1520 move_points_text((rate
* f
) + punit
->moves_left
, FALSE
));
1521 astr_add(astr
, "%s",
1522 move_points_text(punit
->moves_left
, FALSE
));
1524 astr_add_line(astr
, "%s: %s", _("Moves"),
1525 move_points_text(punit
->moves_left
, FALSE
));
1528 case ACTIVITY_POLLUTION
:
1529 case ACTIVITY_FALLOUT
:
1530 case ACTIVITY_OLD_ROAD
:
1531 case ACTIVITY_OLD_RAILROAD
:
1533 case ACTIVITY_IRRIGATE
:
1534 case ACTIVITY_TRANSFORM
:
1535 case ACTIVITY_FORTIFYING
:
1536 case ACTIVITY_FORTIFIED
:
1537 case ACTIVITY_AIRBASE
:
1538 case ACTIVITY_FORTRESS
:
1539 case ACTIVITY_SENTRY
:
1541 case ACTIVITY_EXPLORE
:
1542 case ACTIVITY_CONVERT
:
1543 astr_add_line(astr
, "%s", get_activity_text(punit
->activity
));
1545 case ACTIVITY_PILLAGE
:
1546 switch (punit
->activity_target
.type
) {
1548 if (punit
->activity_target
.obj
.spe
== S_LAST
) {
1549 astr_add_line(astr
, "%s", get_activity_text(punit
->activity
));
1558 BV_SET(pset
, punit
->activity_target
.obj
.spe
);
1559 astr_add_line(astr
, "%s: %s", get_activity_text(punit
->activity
),
1560 get_infrastructure_text(pset
, bases
, roads
));
1572 BV_SET(bases
, punit
->activity_target
.obj
.base
);
1573 astr_add_line(astr
, "%s: %s", get_activity_text(punit
->activity
),
1574 get_infrastructure_text(pset
, bases
, roads
));
1586 BV_SET(roads
, punit
->activity_target
.obj
.road
);
1587 astr_add_line(astr
, "%s: %s", get_activity_text(punit
->activity
),
1588 get_infrastructure_text(pset
, bases
, roads
));
1595 struct base_type
*pbase
;
1596 pbase
= base_by_number(punit
->activity_target
.obj
.base
);
1597 astr_add_line(astr
, "%s: %s", get_activity_text(punit
->activity
),
1598 base_name_translation(pbase
));
1601 case ACTIVITY_GEN_ROAD
:
1603 struct road_type
*proad
;
1604 proad
= road_by_number(punit
->activity_target
.obj
.road
);
1605 astr_add_line(astr
, "%s: %s", get_activity_text(punit
->activity
),
1606 road_name_translation(proad
));
1609 case ACTIVITY_UNKNOWN
:
1610 case ACTIVITY_PATROL_UNUSED
:
1615 log_error("Unknown unit activity %d for %s (nb %d) in %s()",
1616 punit
->activity
, unit_rule_name(punit
), punit
->id
, __FUNCTION__
);
1619 /**************************************************************************
1620 Append a line of text describing the unit's upkeep to the astring.
1622 NB: In the client it is assumed that this information is only available
1623 for units owned by the client's player; the caller must check this.
1624 **************************************************************************/
1625 void unit_upkeep_astr(const struct unit
*punit
, struct astring
*astr
)
1627 if (!punit
|| !astr
) {
1631 astr_add_line(astr
, "%s %d/%d/%d", _("Food/Shield/Gold:"),
1632 punit
->upkeep
[O_FOOD
], punit
->upkeep
[O_SHIELD
],
1633 punit
->upkeep
[O_GOLD
]);
1636 /**************************************************************************
1637 Return the nationality of the unit.
1638 **************************************************************************/
1639 struct player
*unit_nationality(const struct unit
*punit
)
1641 fc_assert_ret_val(NULL
!= punit
, NULL
);
1642 return punit
->nationality
;
1645 /*****************************************************************************
1646 Set the tile location of the unit. Tile can be NULL (for transported units.
1647 *****************************************************************************/
1648 void unit_tile_set(struct unit
*punit
, struct tile
*ptile
)
1650 fc_assert_ret(NULL
!= punit
);
1651 punit
->tile
= ptile
;
1654 /**************************************************************************
1655 Returns true if the tile contains an allied unit and only allied units.
1656 (ie, if your nation A is allied with B, and B is allied with C, a tile
1657 containing units from B and C will return false)
1658 **************************************************************************/
1659 struct unit
*is_allied_unit_tile(const struct tile
*ptile
,
1660 const struct player
*pplayer
)
1662 struct unit
*punit
= NULL
;
1664 unit_list_iterate(ptile
->units
, cunit
) {
1665 if (pplayers_allied(pplayer
, unit_owner(cunit
)))
1670 unit_list_iterate_end
;
1675 /****************************************************************************
1676 Is there an enemy unit on this tile? Returns the unit or NULL if none.
1678 This function is likely to fail if used at the client because the client
1679 doesn't see all units. (Maybe it should be moved into the server code.)
1680 ****************************************************************************/
1681 struct unit
*is_enemy_unit_tile(const struct tile
*ptile
,
1682 const struct player
*pplayer
)
1684 unit_list_iterate(ptile
->units
, punit
) {
1685 if (pplayers_at_war(unit_owner(punit
), pplayer
))
1687 } unit_list_iterate_end
;
1692 /**************************************************************************
1693 is there an non-allied unit on this tile?
1694 **************************************************************************/
1695 struct unit
*is_non_allied_unit_tile(const struct tile
*ptile
,
1696 const struct player
*pplayer
)
1698 unit_list_iterate(ptile
->units
, punit
) {
1699 if (!pplayers_allied(unit_owner(punit
), pplayer
))
1702 unit_list_iterate_end
;
1707 /**************************************************************************
1708 is there an unit we have peace or ceasefire with on this tile?
1709 **************************************************************************/
1710 struct unit
*is_non_attack_unit_tile(const struct tile
*ptile
,
1711 const struct player
*pplayer
)
1713 unit_list_iterate(ptile
->units
, punit
) {
1714 if (pplayers_non_attack(unit_owner(punit
), pplayer
))
1717 unit_list_iterate_end
;
1722 /****************************************************************************
1723 Is there an occupying unit on this tile?
1725 Intended for both client and server; assumes that hiding units are not
1726 sent to client. First check tile for known and seen.
1728 called by city_can_work_tile().
1729 ****************************************************************************/
1730 struct unit
*unit_occupies_tile(const struct tile
*ptile
,
1731 const struct player
*pplayer
)
1733 unit_list_iterate(ptile
->units
, punit
) {
1734 if (!is_military_unit(punit
)) {
1738 if (uclass_has_flag(unit_class(punit
), UCF_DOESNT_OCCUPY_TILE
)) {
1742 if (pplayers_at_war(unit_owner(punit
), pplayer
)) {
1745 } unit_list_iterate_end
;
1750 /**************************************************************************
1751 Is this square controlled by the pplayer?
1753 Here "is_my_zoc" means essentially a square which is *not* adjacent to an
1754 enemy unit on a land tile.
1756 Note this function only makes sense for ground units.
1758 Since this function is also used in the client, it has to deal with some
1759 client-specific features, like FoW and the fact that the client cannot
1760 see units inside enemy cities.
1761 **************************************************************************/
1762 bool is_my_zoc(const struct player
*pplayer
, const struct tile
*ptile0
)
1764 square_iterate(ptile0
, 1, ptile
) {
1765 if (is_ocean_tile(ptile
)) {
1768 if (is_non_allied_unit_tile(ptile
, pplayer
)) {
1769 /* Note: in the client, the above function will return NULL
1770 * if there is a city there, even if the city is occupied */
1775 struct city
*pcity
= is_non_allied_city_tile(ptile
, pplayer
);
1778 && (pcity
->client
.occupied
1779 || TILE_KNOWN_UNSEEN
== tile_get_known(ptile
, pplayer
))) {
1780 /* If the city is fogged, we assume it's occupied */
1784 } square_iterate_end
;
1789 /**************************************************************************
1790 Takes into account unit class flag UCF_ZOC as well as IGZOC
1791 **************************************************************************/
1792 bool unit_type_really_ignores_zoc(const struct unit_type
*punittype
)
1794 return (!uclass_has_flag(utype_class(punittype
), UCF_ZOC
)
1795 || utype_has_flag(punittype
, UTYF_IGZOC
));
1798 /**************************************************************************
1799 An "aggressive" unit is a unit which may cause unhappiness
1800 under a Republic or Democracy.
1801 A unit is *not* aggressive if one or more of following is true:
1802 - zero attack strength
1804 - ground unit inside a fortress within 3 squares of a friendly city
1805 **************************************************************************/
1806 bool unit_being_aggressive(const struct unit
*punit
)
1808 if (!is_attack_unit(punit
)) {
1811 if (tile_city(unit_tile(punit
))) {
1814 if (BORDERS_DISABLED
!= game
.info
.borders
1815 && game
.info
.happyborders
1816 && tile_owner(unit_tile(punit
)) == unit_owner(punit
)) {
1819 if (tile_has_base_flag_for_unit(unit_tile(punit
),
1821 BF_NOT_AGGRESSIVE
)) {
1822 return !is_unit_near_a_friendly_city (punit
);
1828 /**************************************************************************
1829 Returns true if given activity is some kind of building/cleaning.
1830 **************************************************************************/
1831 bool is_build_or_clean_activity(enum unit_activity activity
)
1834 case ACTIVITY_POLLUTION
:
1836 case ACTIVITY_IRRIGATE
:
1837 case ACTIVITY_TRANSFORM
:
1838 case ACTIVITY_FALLOUT
:
1846 /**************************************************************************
1847 Create a virtual unit skeleton. pcity can be NULL, but then you need
1848 to set tile and homecity yourself.
1849 **************************************************************************/
1850 struct unit
*unit_virtual_create(struct player
*pplayer
, struct city
*pcity
,
1851 struct unit_type
*punittype
,
1854 /* Make sure that contents of unit structure are correctly initialized,
1855 * if you ever allocate it by some other mean than fc_calloc() */
1856 struct unit
*punit
= fc_calloc(1, sizeof(*punit
));
1859 /* It does not register the unit so the id is set to 0. */
1860 punit
->id
= IDENTITY_NUMBER_ZERO
;
1862 fc_assert_ret_val(NULL
!= punittype
, NULL
); /* No untyped units! */
1863 punit
->utype
= punittype
;
1865 fc_assert_ret_val(NULL
!= pplayer
, NULL
); /* No unowned units! */
1866 punit
->owner
= pplayer
;
1867 punit
->nationality
= pplayer
;
1869 punit
->facing
= rand_direction();
1872 unit_tile_set(punit
, pcity
->tile
);
1873 punit
->homecity
= pcity
->id
;
1875 unit_tile_set(punit
, NULL
);
1876 punit
->homecity
= IDENTITY_NUMBER_ZERO
;
1879 memset(punit
->upkeep
, 0, O_LAST
* sizeof(*punit
->upkeep
));
1880 punit
->goto_tile
= NULL
;
1881 max_vet_lvl
= utype_veteran_levels(punittype
) - 1;
1882 punit
->veteran
= MIN(veteran_level
, max_vet_lvl
);
1883 /* A unit new and fresh ... */
1884 punit
->fuel
= utype_fuel(unit_type(punit
));
1885 punit
->hp
= unit_type(punit
)->hp
;
1886 punit
->moves_left
= unit_move_rate(punit
);
1887 punit
->moved
= FALSE
;
1889 punit
->ai_controlled
= FALSE
;
1890 punit
->paradropped
= FALSE
;
1891 punit
->done_moving
= FALSE
;
1893 punit
->transporter
= NULL
;
1894 punit
->transporting
= unit_list_new();
1896 set_unit_activity(punit
, ACTIVITY_IDLE
);
1897 punit
->battlegroup
= BATTLEGROUP_NONE
;
1898 punit
->has_orders
= FALSE
;
1901 punit
->server
.debug
= FALSE
;
1902 punit
->server
.birth_turn
= game
.info
.turn
;
1904 punit
->server
.ord_map
= 0;
1905 punit
->server
.ord_city
= 0;
1907 punit
->server
.vision
= NULL
; /* No vision. */
1908 punit
->server
.action_timestamp
= 0;
1909 /* Must be an invalid turn number, and an invalid previous turn
1911 punit
->server
.action_turn
= -2;
1912 /* punit->server.moving = NULL; set by fc_calloc(). */
1914 punit
->server
.adv
= fc_calloc(1, sizeof(*punit
->server
.adv
));
1916 CALL_FUNC_EACH_AI(unit_alloc
, punit
);
1918 punit
->client
.focus_status
= FOCUS_AVAIL
;
1919 punit
->client
.transported_by
= -1;
1920 punit
->client
.colored
= FALSE
;
1926 /**************************************************************************
1927 Free the memory used by virtual unit. By the time this function is
1928 called, you should already have unregistered it everywhere.
1929 **************************************************************************/
1930 void unit_virtual_destroy(struct unit
*punit
)
1932 free_unit_orders(punit
);
1934 /* Unload unit if transported. */
1935 unit_transport_unload(punit
);
1936 fc_assert(!unit_transported(punit
));
1938 /* Check for transported units. Use direct access to the list. */
1939 if (unit_list_size(punit
->transporting
) != 0) {
1940 /* Unload all units. */
1941 unit_list_iterate_safe(punit
->transporting
, pcargo
) {
1942 unit_transport_unload(pcargo
);
1943 } unit_list_iterate_safe_end
;
1945 fc_assert(unit_list_size(punit
->transporting
) == 0);
1947 if (punit
->transporting
) {
1948 unit_list_destroy(punit
->transporting
);
1951 CALL_FUNC_EACH_AI(unit_free
, punit
);
1953 if (is_server() && punit
->server
.adv
) {
1954 FC_FREE(punit
->server
.adv
);
1960 /**************************************************************************
1961 Free and reset the unit's goto route (punit->pgr). Only used by the
1963 **************************************************************************/
1964 void free_unit_orders(struct unit
*punit
)
1966 if (punit
->has_orders
) {
1967 punit
->goto_tile
= NULL
;
1968 free(punit
->orders
.list
);
1969 punit
->orders
.list
= NULL
;
1971 punit
->has_orders
= FALSE
;
1974 /****************************************************************************
1975 Return how many units are in the transport.
1976 ****************************************************************************/
1977 int get_transporter_occupancy(const struct unit
*ptrans
)
1979 fc_assert_ret_val(ptrans
, -1);
1981 return unit_list_size(ptrans
->transporting
);
1984 /****************************************************************************
1985 Find the best transporter at the given location for the unit. See also
1986 transport_from_tile() to test if a transport might be suitable for
1988 ****************************************************************************/
1989 struct unit
*transporter_for_unit(const struct unit
*pcargo
)
1991 struct unit_list
*tile_units
= unit_tile(pcargo
)->units
;
1992 struct unit
*best_trans
= NULL
;
1994 bool has_orders
, is_idle
, can_freely_unload
;
1995 int depth
, outermost_moves_left
, total_moves
;
1996 } cur
, best
= { FALSE
};
1998 unit_list_iterate(tile_units
, ptrans
) {
1999 if (!can_unit_load(pcargo
, ptrans
)) {
2001 } else if (best_trans
== NULL
) {
2002 best_trans
= ptrans
;
2005 /* Gather data from transport stack in a single pass, for use in
2006 * various conditions below. */
2007 cur
.has_orders
= unit_has_orders(ptrans
);
2008 cur
.outermost_moves_left
= ptrans
->moves_left
;
2009 cur
.total_moves
= ptrans
->moves_left
+ unit_move_rate(ptrans
);
2011 const struct unit
*ptranstrans
= unit_transport_get(ptrans
);
2013 while (NULL
!= ptranstrans
) {
2014 if (unit_has_orders(ptranstrans
)) {
2015 cur
.has_orders
= TRUE
;
2017 cur
.outermost_moves_left
= ptranstrans
->moves_left
;
2018 cur
.total_moves
+= ptranstrans
->moves_left
2019 + unit_move_rate(ptranstrans
);
2020 ptranstrans
= unit_transport_get(ptranstrans
);
2024 /* Criteria for deciding the 'best' transport to load onto.
2025 * The following tests are applied in order; earlier ones have
2026 * lexicographically greater significance than later ones. */
2028 /* Transports which have orders, or are on transports with orders,
2029 * are less preferable to transport stacks without orders (to
2030 * avoid loading on units that are just passing through). */
2031 if (best_trans
!= ptrans
) {
2032 if (!cur
.has_orders
&& best
.has_orders
) {
2033 best_trans
= ptrans
;
2034 } else if (cur
.has_orders
&& !best
.has_orders
) {
2039 /* Else, transports which are idle are preferable (giving players
2040 * some control over loading) -- this does not check transports
2042 cur
.is_idle
= (ptrans
->activity
== ACTIVITY_IDLE
);
2043 if (best_trans
!= ptrans
) {
2044 if (cur
.is_idle
&& !best
.is_idle
) {
2045 best_trans
= ptrans
;
2046 } else if (!cur
.is_idle
&& best
.is_idle
) {
2051 /* Else, transports from which the cargo could unload at any time
2052 * are preferable to those where the cargo can only disembark in
2054 cur
.can_freely_unload
= utype_can_freely_unload(unit_type(pcargo
),
2056 if (best_trans
!= ptrans
) {
2057 if (cur
.can_freely_unload
&& !best
.can_freely_unload
) {
2058 best_trans
= ptrans
;
2059 } else if (!cur
.can_freely_unload
&& best
.can_freely_unload
) {
2064 /* Else, transports which are less deeply nested are preferable. */
2065 cur
.depth
= unit_transport_depth(ptrans
);
2066 if (best_trans
!= ptrans
) {
2067 if (cur
.depth
< best
.depth
) {
2068 best_trans
= ptrans
;
2069 } else if (cur
.depth
> best
.depth
) {
2074 /* Else, transport stacks where the outermost transport has more
2075 * moves left are preferable (on the assumption that it's the
2076 * outermost transport that's about to move). */
2077 if (best_trans
!= ptrans
) {
2078 if (cur
.outermost_moves_left
> best
.outermost_moves_left
) {
2079 best_trans
= ptrans
;
2080 } else if (cur
.outermost_moves_left
< best
.outermost_moves_left
) {
2085 /* All other things being equal, as a tie-breaker, compare the total
2086 * moves left (this turn) and move rate (future turns) for the whole
2087 * stack, to take into account total potential movement for both
2088 * short and long journeys (we don't know which the cargo intends to
2089 * make). Doesn't try to account for whether transports can unload,
2091 if (best_trans
!= ptrans
) {
2092 if (cur
.total_moves
> best
.total_moves
) {
2093 best_trans
= ptrans
;
2099 fc_assert(best_trans
== ptrans
);
2101 } unit_list_iterate_end
;
2106 /****************************************************************************
2107 Check if unit of given type would be able to transport all of transport's
2109 ****************************************************************************/
2110 static bool can_type_transport_units_cargo(const struct unit_type
*utype
,
2111 const struct unit
*punit
)
2113 if (get_transporter_occupancy(punit
) > utype
->transport_capacity
) {
2117 unit_list_iterate(punit
->transporting
, pcargo
) {
2118 if (!can_unit_type_transport(utype
, unit_class(pcargo
))) {
2121 } unit_list_iterate_end
;
2126 /****************************************************************************
2127 Tests if the unit could be updated. Returns UU_OK if is this is
2130 is_free should be set if the unit upgrade is "free" (e.g., Leonardo's).
2131 Otherwise money is needed and the unit must be in an owned city.
2133 Note that this function is strongly tied to unittools.c:upgrade_unit().
2134 ****************************************************************************/
2135 enum unit_upgrade_result
unit_upgrade_test(const struct unit
*punit
,
2138 struct player
*pplayer
= unit_owner(punit
);
2139 struct unit_type
*to_unittype
= can_upgrade_unittype(pplayer
, unit_type(punit
));
2144 return UU_NO_UNITTYPE
;
2148 cost
= unit_upgrade_price(pplayer
, unit_type(punit
), to_unittype
);
2149 if (pplayer
->economic
.gold
< cost
) {
2153 pcity
= tile_city(unit_tile(punit
));
2155 return UU_NOT_IN_CITY
;
2157 if (city_owner(pcity
) != pplayer
) {
2158 /* TODO: should upgrades in allied cities be possible? */
2159 return UU_NOT_CITY_OWNER
;
2163 if (!can_type_transport_units_cargo(to_unittype
, punit
)) {
2164 /* TODO: allow transported units to be reassigned. Check here
2165 * and make changes to upgrade_unit. */
2166 return UU_NOT_ENOUGH_ROOM
;
2169 if (!can_exist_at_tile(to_unittype
, unit_tile(punit
))) {
2170 /* The new unit type can't survive on this terrain. */
2171 return UU_NOT_TERRAIN
;
2177 /**************************************************************************
2178 Tests if unit can be converted to another type.
2179 **************************************************************************/
2180 bool unit_can_convert(const struct unit
*punit
)
2182 struct unit_type
*tgt
= unit_type(punit
)->converted_to
;
2188 if (!can_type_transport_units_cargo(tgt
, punit
)) {
2192 if (!can_exist_at_tile(tgt
, unit_tile(punit
))) {
2199 /**************************************************************************
2200 Find the result of trying to upgrade the unit, and a message that
2201 most callers can use directly.
2202 **************************************************************************/
2203 enum unit_upgrade_result
unit_upgrade_info(const struct unit
*punit
,
2204 char *buf
, size_t bufsz
)
2206 struct player
*pplayer
= unit_owner(punit
);
2207 enum unit_upgrade_result result
= unit_upgrade_test(punit
, FALSE
);
2209 struct unit_type
*from_unittype
= unit_type(punit
);
2210 struct unit_type
*to_unittype
= can_upgrade_unittype(pplayer
,
2212 char tbuf
[MAX_LEN_MSG
];
2214 fc_snprintf(tbuf
, ARRAY_SIZE(tbuf
), PL_("Treasury contains %d gold.",
2215 "Treasury contains %d gold.",
2216 pplayer
->economic
.gold
),
2217 pplayer
->economic
.gold
);
2221 upgrade_cost
= unit_upgrade_price(pplayer
, from_unittype
, to_unittype
);
2222 /* This message is targeted toward the GUI callers. */
2223 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2224 fc_snprintf(buf
, bufsz
, PL_("Upgrade %s to %s for %d gold?\n%s",
2225 "Upgrade %s to %s for %d gold?\n%s",
2227 utype_name_translation(from_unittype
),
2228 utype_name_translation(to_unittype
),
2229 upgrade_cost
, tbuf
);
2231 case UU_NO_UNITTYPE
:
2232 fc_snprintf(buf
, bufsz
,
2233 _("Sorry, cannot upgrade %s (yet)."),
2234 utype_name_translation(from_unittype
));
2237 upgrade_cost
= unit_upgrade_price(pplayer
, from_unittype
, to_unittype
);
2238 /* TRANS: Last %s is pre-pluralised "Treasury contains %d gold." */
2239 fc_snprintf(buf
, bufsz
, PL_("Upgrading %s to %s costs %d gold.\n%s",
2240 "Upgrading %s to %s costs %d gold.\n%s",
2242 utype_name_translation(from_unittype
),
2243 utype_name_translation(to_unittype
),
2244 upgrade_cost
, tbuf
);
2246 case UU_NOT_IN_CITY
:
2247 case UU_NOT_CITY_OWNER
:
2248 fc_snprintf(buf
, bufsz
,
2249 _("You can only upgrade units in your cities."));
2251 case UU_NOT_ENOUGH_ROOM
:
2252 fc_snprintf(buf
, bufsz
,
2253 _("Upgrading this %s would strand units it transports."),
2254 utype_name_translation(from_unittype
));
2256 case UU_NOT_TERRAIN
:
2257 fc_snprintf(buf
, bufsz
,
2258 _("Upgrading this %s would result in a %s which can not "
2259 "survive at this place."),
2260 utype_name_translation(from_unittype
),
2261 utype_name_translation(to_unittype
));
2268 /**************************************************************************
2269 Does unit lose hitpoints each turn?
2270 **************************************************************************/
2271 bool is_losing_hp(const struct unit
*punit
)
2273 struct unit_type
*punittype
= unit_type(punit
);
2275 return get_unit_bonus(punit
, EFT_UNIT_RECOVER
)
2277 utype_class(punittype
)->hp_loss_pct
/ 100);
2280 /**************************************************************************
2281 Does unit lose hitpoints each turn?
2282 **************************************************************************/
2283 bool unit_type_is_losing_hp(const struct player
*pplayer
,
2284 const struct unit_type
*punittype
)
2286 return get_unittype_bonus(pplayer
, NULL
, punittype
, EFT_UNIT_RECOVER
)
2288 utype_class(punittype
)->hp_loss_pct
/ 100);
2291 /**************************************************************************
2292 Check if unit with given id is still alive. Use this before using
2293 old unit pointers when unit might have dead.
2294 **************************************************************************/
2295 bool unit_alive(int id
)
2297 /* Check if unit exist in game */
2298 if (game_unit_by_number(id
)) {
2305 /**************************************************************************
2306 Return TRUE if this is a valid unit pointer but does not correspond to
2307 any unit that exists in the game.
2309 NB: A return value of FALSE implies that either the pointer is NULL or
2310 that the unit exists in the game.
2311 **************************************************************************/
2312 bool unit_is_virtual(const struct unit
*punit
)
2318 return punit
!= game_unit_by_number(punit
->id
);
2321 /**************************************************************************
2322 Return pointer to ai data of given unit and ai type.
2323 **************************************************************************/
2324 void *unit_ai_data(const struct unit
*punit
, const struct ai_type
*ai
)
2326 return punit
->server
.ais
[ai_type_number(ai
)];
2329 /**************************************************************************
2330 Attach ai data to unit
2331 **************************************************************************/
2332 void unit_set_ai_data(struct unit
*punit
, const struct ai_type
*ai
,
2335 punit
->server
.ais
[ai_type_number(ai
)] = data
;
2338 /*****************************************************************************
2339 Calculate how expensive it is to bribe the unit. The cost depends on the
2340 distance to the capital, the owner's treasury, and the build cost of the
2341 unit. For a damaged unit the price is reduced. For a veteran unit, it is
2344 The bribe cost for settlers are halved.
2345 **************************************************************************/
2346 int unit_bribe_cost(struct unit
*punit
)
2348 int cost
, default_hp
, dist
= 0;
2349 struct city
*capital
;
2351 fc_assert_ret_val(punit
!= NULL
, 0);
2353 default_hp
= unit_type(punit
)->hp
;
2354 cost
= unit_owner(punit
)->economic
.gold
+ game
.info
.base_bribe_cost
;
2355 capital
= player_capital(unit_owner(punit
));
2357 /* Consider the distance to the capital. */
2358 if (capital
!= NULL
) {
2359 dist
= MIN(GAME_UNIT_BRIBE_DIST_MAX
,
2360 map_distance(capital
->tile
, unit_tile(punit
)));
2362 dist
= GAME_UNIT_BRIBE_DIST_MAX
;
2366 /* Consider the build cost. */
2367 cost
*= unit_build_shield_cost(punit
) / 10;
2369 /* FIXME: This is a weird one - should be replaced. */
2370 if (unit_has_type_flag(punit
, UTYF_CITIES
)) {
2374 /* Veterans are not cheap. */
2376 const struct veteran_level
*vlevel
2377 = utype_veteran_level(unit_type(punit
), punit
->veteran
);
2378 fc_assert_ret_val(vlevel
!= NULL
, 0);
2379 cost
= cost
* vlevel
->power_fact
/ 100;
2380 if (unit_type(punit
)->move_rate
> 0) {
2381 cost
+= cost
* vlevel
->move_bonus
/ unit_type(punit
)->move_rate
;
2383 cost
+= cost
* vlevel
->move_bonus
/ SINGLE_MOVE
;
2387 /* Cost now contains the basic bribe cost. We now reduce it by:
2388 * bribecost = cost/2 + cost/2 * damage/hp
2389 * = cost/2 * (1 + damage/hp) */
2390 return ((float)cost
/ 2 * (1.0 + punit
->hp
/ default_hp
));
2393 /*****************************************************************************
2394 Load pcargo onto ptrans. Returns TRUE on success.
2395 *****************************************************************************/
2396 bool unit_transport_load(struct unit
*pcargo
, struct unit
*ptrans
, bool force
)
2398 fc_assert_ret_val(ptrans
!= NULL
, FALSE
);
2399 fc_assert_ret_val(pcargo
!= NULL
, FALSE
);
2401 fc_assert_ret_val(!unit_list_search(ptrans
->transporting
, pcargo
), FALSE
);
2403 if (force
|| can_unit_load(pcargo
, ptrans
)) {
2404 pcargo
->transporter
= ptrans
;
2405 unit_list_append(ptrans
->transporting
, pcargo
);
2413 /*****************************************************************************
2414 Unload pcargo from ptrans. Returns TRUE on success.
2415 *****************************************************************************/
2416 bool unit_transport_unload(struct unit
*pcargo
)
2418 struct unit
*ptrans
;
2420 fc_assert_ret_val(pcargo
!= NULL
, FALSE
);
2422 if (!unit_transported(pcargo
)) {
2423 /* 'pcargo' is not transported. */
2427 /* Get the transporter; must not be defined on the client! */
2428 ptrans
= unit_transport_get(pcargo
);
2432 /* 'pcargo' and 'ptrans' should be on the same tile. */
2433 fc_assert(same_pos(unit_tile(pcargo
), unit_tile(ptrans
)));
2434 /* It is an error if 'pcargo' can not be removed from the 'ptrans'. */
2435 success
= unit_list_remove(ptrans
->transporting
, pcargo
);
2439 /* For the server (also safe for the client). */
2440 pcargo
->transporter
= NULL
;
2445 /*****************************************************************************
2446 Returns TRUE iff the unit is transported.
2447 *****************************************************************************/
2448 bool unit_transported(const struct unit
*pcargo
)
2450 fc_assert_ret_val(pcargo
!= NULL
, FALSE
);
2452 /* The unit is transported if a transporter unit is set or, (for the client)
2453 * if the transported_by field is set. */
2454 if (pcargo
->transporter
!= NULL
2455 || (!is_server() && pcargo
->client
.transported_by
!= -1)) {
2462 /*****************************************************************************
2463 Returns the transporter of the unit or NULL if it is not transported.
2464 *****************************************************************************/
2465 struct unit
*unit_transport_get(const struct unit
*pcargo
)
2467 fc_assert_ret_val(pcargo
!= NULL
, NULL
);
2469 return pcargo
->transporter
;
2472 /*****************************************************************************
2473 Returns the list of cargo units.
2474 *****************************************************************************/
2475 struct unit_list
*unit_transport_cargo(const struct unit
*ptrans
)
2477 fc_assert_ret_val(ptrans
!= NULL
, NULL
);
2478 fc_assert_ret_val(ptrans
->transporting
!= NULL
, NULL
);
2480 return ptrans
->transporting
;
2483 /****************************************************************************
2484 Helper for unit_transport_check().
2485 ****************************************************************************/
2487 unit_transport_check_one(const struct unit_type
*cargo_utype
,
2488 const struct unit_type
*trans_utype
)
2490 return (trans_utype
!= cargo_utype
2491 && !can_unit_type_transport(cargo_utype
,
2492 utype_class(trans_utype
)));
2495 /****************************************************************************
2496 Returns whether 'pcargo' in 'ptrans' is a valid transport. Note that
2497 'pcargo' can already be (but doesn't need) loaded into 'ptrans'.
2499 It may fail if one of the cargo unit has the same type of one of the
2500 transporter unit or if one of the cargo unit can transport one of
2502 ****************************************************************************/
2503 bool unit_transport_check(const struct unit
*pcargo
,
2504 const struct unit
*ptrans
)
2506 const struct unit_type
*cargo_utype
= unit_type(pcargo
);
2508 /* Check 'pcargo' against 'ptrans'. */
2509 if (!unit_transport_check_one(cargo_utype
, unit_type(ptrans
))) {
2513 /* Check 'pcargo' against 'ptrans' parents. */
2514 unit_transports_iterate(ptrans
, pparent
) {
2515 if (!unit_transport_check_one(cargo_utype
, unit_type(pparent
))) {
2518 } unit_transports_iterate_end
;
2520 /* Check cargo children... */
2521 unit_cargo_iterate(pcargo
, pchild
) {
2522 cargo_utype
= unit_type(pchild
);
2524 /* ...against 'ptrans'. */
2525 if (!unit_transport_check_one(cargo_utype
, unit_type(ptrans
))) {
2529 /* ...and against 'ptrans' parents. */
2530 unit_transports_iterate(ptrans
, pparent
) {
2531 if (!unit_transport_check_one(cargo_utype
, unit_type(pparent
))) {
2534 } unit_transports_iterate_end
;
2535 } unit_cargo_iterate_end
;
2540 /****************************************************************************
2541 Returns whether 'pcargo' is transported by 'ptrans', either directly
2543 ****************************************************************************/
2544 bool unit_contained_in(const struct unit
*pcargo
, const struct unit
*ptrans
)
2546 unit_transports_iterate(pcargo
, plevel
) {
2547 if (ptrans
== plevel
) {
2550 } unit_transports_iterate_end
;
2554 /****************************************************************************
2555 Returns the number of unit cargo layers within transport 'ptrans'.
2556 ****************************************************************************/
2557 int unit_cargo_depth(const struct unit
*ptrans
)
2559 struct cargo_iter iter
;
2560 struct iterator
*it
;
2563 for (it
= cargo_iter_init(&iter
, ptrans
); iterator_valid(it
);
2564 iterator_next(it
)) {
2565 if (iter
.depth
> depth
) {
2572 /****************************************************************************
2573 Returns the number of unit transport layers which carry unit 'pcargo'.
2574 ****************************************************************************/
2575 int unit_transport_depth(const struct unit
*pcargo
)
2579 unit_transports_iterate(pcargo
, plevel
) {
2581 } unit_transports_iterate_end
;
2585 /****************************************************************************
2586 Returns the size of the unit cargo iterator.
2587 ****************************************************************************/
2588 size_t cargo_iter_sizeof(void)
2590 return sizeof(struct cargo_iter
);
2593 /****************************************************************************
2594 Get the unit of the cargo iterator.
2595 ****************************************************************************/
2596 static void *cargo_iter_get(const struct iterator
*it
)
2598 const struct cargo_iter
*iter
= CARGO_ITER(it
);
2600 return unit_list_link_data(iter
->links
[iter
->depth
- 1]);
2603 /****************************************************************************
2604 Try to find next unit for the cargo iterator.
2605 ****************************************************************************/
2606 static void cargo_iter_next(struct iterator
*it
)
2608 struct cargo_iter
*iter
= CARGO_ITER(it
);
2609 const struct unit_list_link
*piter
= iter
->links
[iter
->depth
- 1];
2610 const struct unit_list_link
*pnext
;
2612 /* Variant 1: unit has cargo. */
2613 pnext
= unit_list_head(unit_transport_cargo(unit_list_link_data(piter
)));
2614 if (NULL
!= pnext
) {
2615 fc_assert(iter
->depth
< ARRAY_SIZE(iter
->links
));
2616 iter
->links
[iter
->depth
++] = pnext
;
2621 /* Variant 2: there are other cargo units at same level. */
2622 pnext
= unit_list_link_next(piter
);
2623 if (NULL
!= pnext
) {
2624 iter
->links
[iter
->depth
- 1] = pnext
;
2628 /* Variant 3: return to previous level, and do same tests. */
2629 piter
= iter
->links
[iter
->depth
-- - 2];
2630 } while (0 < iter
->depth
);
2633 /****************************************************************************
2634 Return whether the iterator is still valid.
2635 ****************************************************************************/
2636 static bool cargo_iter_valid(const struct iterator
*it
)
2638 return (0 < CARGO_ITER(it
)->depth
);
2641 /****************************************************************************
2642 Initialize the cargo iterator.
2643 ****************************************************************************/
2644 struct iterator
*cargo_iter_init(struct cargo_iter
*iter
,
2645 const struct unit
*ptrans
)
2647 struct iterator
*it
= ITERATOR(iter
);
2649 it
->get
= cargo_iter_get
;
2650 it
->next
= cargo_iter_next
;
2651 it
->valid
= cargo_iter_valid
;
2652 iter
->links
[0] = unit_list_head(unit_transport_cargo(ptrans
));
2653 iter
->depth
= (NULL
!= iter
->links
[0] ? 1 : 0);