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>
19 #include <math.h> /* ceil */
27 #include "string_vector.h"
34 #include "government.h"
42 #define MAX_UNIT_ROLES L_LAST + ACTION_COUNT
44 static struct unit_type unit_types
[U_LAST
];
45 static struct unit_class unit_classes
[UCL_LAST
];
46 /* the unit_types and unit_classes arrays are now setup in:
47 server/ruleset.c (for the server)
48 client/packhand.c (for the client)
51 static struct user_flag user_type_flags
[MAX_NUM_USER_UNIT_FLAGS
];
53 /**************************************************************************
54 Return the first item of unit_types.
55 **************************************************************************/
56 struct unit_type
*unit_type_array_first(void)
58 if (game
.control
.num_unit_types
> 0) {
64 /**************************************************************************
65 Return the last item of unit_types.
66 **************************************************************************/
67 const struct unit_type
*unit_type_array_last(void)
69 if (game
.control
.num_unit_types
> 0) {
70 return &unit_types
[game
.control
.num_unit_types
- 1];
75 /**************************************************************************
76 Return the number of unit types.
77 **************************************************************************/
78 Unit_type_id
utype_count(void)
80 return game
.control
.num_unit_types
;
83 /**************************************************************************
84 Return the unit type index.
86 Currently same as utype_number(), paired with utype_count()
87 indicates use as an array index.
88 **************************************************************************/
89 Unit_type_id
utype_index(const struct unit_type
*punittype
)
91 fc_assert_ret_val(punittype
, -1);
92 return punittype
- unit_types
;
95 /**************************************************************************
96 Return the unit type index.
97 **************************************************************************/
98 Unit_type_id
utype_number(const struct unit_type
*punittype
)
100 fc_assert_ret_val(punittype
, -1);
101 return punittype
->item_number
;
104 /**************************************************************************
105 Return a pointer for the unit type struct for the given unit type id.
107 This function returns NULL for invalid unit pointers (some callers
109 **************************************************************************/
110 struct unit_type
*utype_by_number(const Unit_type_id id
)
112 if (id
< 0 || id
>= game
.control
.num_unit_types
) {
115 return &unit_types
[id
];
118 /**************************************************************************
119 Return the unit type for this unit.
120 **************************************************************************/
121 struct unit_type
*unit_type_get(const struct unit
*punit
)
123 fc_assert_ret_val(NULL
!= punit
, NULL
);
128 /**************************************************************************
129 Returns the upkeep of a unit of this type under the given government.
130 **************************************************************************/
131 int utype_upkeep_cost(const struct unit_type
*ut
, struct player
*pplayer
,
132 Output_type_id otype
)
134 int val
= ut
->upkeep
[otype
], gold_upkeep_factor
;
136 if (BV_ISSET(ut
->flags
, UTYF_FANATIC
)
137 && get_player_bonus(pplayer
, EFT_FANATICS
) > 0) {
138 /* Special case: fanatics have no upkeep under fanaticism. */
142 /* switch shield upkeep to gold upkeep if
143 - the effect 'EFT_SHIELD2GOLD_FACTOR' is non-zero (it gives the
144 conversion factor in percent) and
145 - the unit has the corresponding flag set (UTYF_SHIELD2GOLD)
146 FIXME: Should the ai know about this? */
147 if (utype_has_flag(ut
, UTYF_SHIELD2GOLD
)
148 && (otype
== O_GOLD
|| otype
== O_SHIELD
)) {
149 gold_upkeep_factor
= get_player_bonus(pplayer
, EFT_SHIELD2GOLD_FACTOR
);
150 if (gold_upkeep_factor
> 0) {
153 val
= ceil((0.01 * gold_upkeep_factor
) * ut
->upkeep
[O_SHIELD
]);
159 fc_assert(otype
== O_GOLD
|| otype
== O_SHIELD
);
165 val
*= get_player_output_bonus(pplayer
, get_output_type(otype
),
170 /**************************************************************************
171 Return the "happy cost" (the number of citizens who are discontented)
173 **************************************************************************/
174 int utype_happy_cost(const struct unit_type
*ut
,
175 const struct player
*pplayer
)
177 return ut
->happy_cost
* get_player_bonus(pplayer
, EFT_UNHAPPY_FACTOR
);
180 /**************************************************************************
181 Return whether the unit has the given flag.
182 **************************************************************************/
183 bool unit_has_type_flag(const struct unit
*punit
, enum unit_type_flag_id flag
)
185 return utype_has_flag(unit_type_get(punit
), flag
);
188 /**************************************************************************
189 Return whether the given unit type has the role. Roles are like
190 flags but have no meaning except to the AI.
191 **************************************************************************/
192 bool utype_has_role(const struct unit_type
*punittype
, int role
)
194 fc_assert_ret_val(role
>= L_FIRST
&& role
< L_LAST
, FALSE
);
195 return BV_ISSET(punittype
->roles
, role
- L_FIRST
);
198 /**************************************************************************
199 Return whether the unit has the given role.
200 **************************************************************************/
201 bool unit_has_type_role(const struct unit
*punit
, enum unit_role_id role
)
203 return utype_has_role(unit_type_get(punit
), role
);
206 /****************************************************************************
207 Return whether the unit can take over enemy cities.
208 ****************************************************************************/
209 bool unit_can_take_over(const struct unit
*punit
)
211 return unit_owner(punit
)->ai_common
.barbarian_type
!= ANIMAL_BARBARIAN
212 && utype_can_take_over(unit_type_get(punit
));
215 /****************************************************************************
216 Return whether the unit type can take over enemy cities.
217 ****************************************************************************/
218 bool utype_can_take_over(const struct unit_type
*punittype
)
220 return (uclass_has_flag(utype_class(punittype
), UCF_CAN_OCCUPY_CITY
)
221 && !utype_has_flag(punittype
, UTYF_CIVILIAN
));
224 /****************************************************************************
225 Return TRUE iff the given cargo type has no restrictions on when it can
226 load onto the given transporter.
227 (Does not check that cargo is valid for transport!)
228 ****************************************************************************/
229 bool utype_can_freely_load(const struct unit_type
*pcargotype
,
230 const struct unit_type
*ptranstype
)
232 return BV_ISSET(pcargotype
->embarks
,
233 uclass_index(utype_class(ptranstype
)));
236 /****************************************************************************
237 Return TRUE iff the given cargo type has no restrictions on when it can
238 unload from the given transporter.
239 (Does not check that cargo is valid for transport!)
240 ****************************************************************************/
241 bool utype_can_freely_unload(const struct unit_type
*pcargotype
,
242 const struct unit_type
*ptranstype
)
244 return BV_ISSET(pcargotype
->disembarks
,
245 uclass_index(utype_class(ptranstype
)));
248 /* Fake action representing any hostile action. */
249 #define ACTION_HOSTILE ACTION_COUNT + 1
251 /* Number of real and fake actions. */
252 #define ACTION_AND_FAKES ACTION_HOSTILE + 1
254 /* Cache of what generalized (ruleset defined) action enabler controlled
255 * actions units of each type can perform. Checking if any action can be
256 * done at all is done via the fake action ACTION_ANY. If any hostile
257 * action can be performed is done via ACTION_HOSTILE. */
258 static bv_unit_types unit_can_act_cache
[ACTION_AND_FAKES
];
260 /**************************************************************************
261 Cache what generalized (ruleset defined) action enabler controlled
262 actions a unit of the given type can perform.
263 **************************************************************************/
264 static void unit_can_act_cache_set(struct unit_type
*putype
)
266 /* Clear old values. */
267 action_iterate(act_id
) {
268 BV_CLR(unit_can_act_cache
[act_id
], utype_index(putype
));
269 } action_iterate_end
;
270 BV_CLR(unit_can_act_cache
[ACTION_ANY
], utype_index(putype
));
271 BV_CLR(unit_can_act_cache
[ACTION_HOSTILE
], utype_index(putype
));
273 /* See if the unit type can do an action controlled by generalized action
275 action_enablers_iterate(enabler
) {
276 if (requirement_fulfilled_by_unit_type(putype
,
277 &(enabler
->actor_reqs
))
278 && action_get_actor_kind(enabler
->action
) == AAK_UNIT
) {
279 log_debug("act_cache: %s can %s",
280 utype_rule_name(putype
), gen_action_name(enabler
->action
));
281 BV_SET(unit_can_act_cache
[enabler
->action
], utype_index(putype
));
282 BV_SET(unit_can_act_cache
[ACTION_ANY
], utype_index(putype
));
283 if (action_is_hostile(enabler
->action
)) {
284 BV_SET(unit_can_act_cache
[ACTION_HOSTILE
], utype_index(putype
));
287 } action_enablers_iterate_end
;
290 /**************************************************************************
291 Return TRUE iff units of this type can do actions controlled by
292 generalized (ruleset defined) action enablers.
293 **************************************************************************/
294 bool utype_may_act_at_all(const struct unit_type
*putype
)
296 return utype_can_do_action(putype
, ACTION_ANY
);
299 /**************************************************************************
300 Return TRUE iff units of the given type can do the specified generalized
301 (ruleset defined) action enabler controlled action.
302 **************************************************************************/
303 bool utype_can_do_action(const struct unit_type
*putype
,
306 return BV_ISSET(unit_can_act_cache
[action_id
], utype_index(putype
));
309 /**************************************************************************
310 Return TRUE iff the unit type can perform the action corresponding to
312 **************************************************************************/
313 static bool utype_can_do_action_role(const struct unit_type
*putype
,
316 return utype_can_do_action(putype
, role
- L_LAST
);
319 /**************************************************************************
320 Return TRUE iff units of this type can do hostile actions controlled by
321 generalized (ruleset defined) action enablers.
322 **************************************************************************/
323 bool utype_acts_hostile(const struct unit_type
*putype
)
325 return utype_can_do_action(putype
, ACTION_HOSTILE
);
328 /* Cache if any action at all may be possible when the actor unit's state
330 * bit 0 to USP_COUNT - 1: Possible when the corresponding property is TRUE
331 * bit USP_COUNT to ((USP_COUNT * 2) - 1): Possible when the corresponding
334 BV_DEFINE(bv_ustate_act_cache
, USP_COUNT
* 2);
336 /* Caches for each unit type */
337 static bv_ustate_act_cache ustate_act_cache
[U_LAST
][ACTION_AND_FAKES
];
338 static bv_diplrel_all_reqs dipl_rel_action_cache
[U_LAST
][ACTION_AND_FAKES
];
340 /**************************************************************************
341 Cache if any action may be possible for a unit of the type putype for
342 each unit state property. Since a unit state property could be ignored
343 both present and !present must be checked.
344 **************************************************************************/
345 static void unit_state_action_cache_set(struct unit_type
*putype
)
347 struct requirement req
;
348 int uidx
= utype_index(putype
);
350 /* The unit is not yet known to be allowed to perform any actions no
351 * matter what its unit state is. */
352 action_iterate(action_id
) {
353 BV_CLR_ALL(ustate_act_cache
[uidx
][action_id
]);
354 } action_iterate_end
;
355 BV_CLR_ALL(ustate_act_cache
[uidx
][ACTION_ANY
]);
356 BV_CLR_ALL(ustate_act_cache
[uidx
][ACTION_HOSTILE
]);
358 if (!utype_may_act_at_all(putype
)) {
359 /* Not an actor unit. */
363 /* Common for every situation */
364 req
.range
= REQ_RANGE_LOCAL
;
365 req
.survives
= FALSE
;
366 req
.source
.kind
= VUT_UNITSTATE
;
368 for (req
.source
.value
.unit_state
= ustate_prop_begin();
369 req
.source
.value
.unit_state
!= ustate_prop_end();
370 req
.source
.value
.unit_state
= ustate_prop_next(
371 req
.source
.value
.unit_state
)) {
373 /* No action will ever be possible in a specific unit state if the
374 * opposite unit state is required in all action enablers.
375 * No unit state except present and !present of the same property
376 * implies or conflicts with another so the tests can be simple. */
377 action_enablers_iterate(enabler
) {
378 if (requirement_fulfilled_by_unit_type(putype
,
379 &(enabler
->actor_reqs
))
380 && action_get_actor_kind(enabler
->action
) == AAK_UNIT
) {
381 /* Not required to be absent, so OK if present */
383 if (!is_req_in_vec(&req
, &(enabler
->actor_reqs
))) {
384 BV_SET(ustate_act_cache
[utype_index(putype
)][ACTION_ANY
],
385 requirement_unit_state_ereq(req
.source
.value
.unit_state
,
387 BV_SET(ustate_act_cache
[utype_index(putype
)][enabler
->action
],
388 requirement_unit_state_ereq(req
.source
.value
.unit_state
,
392 /* Not required to be present, so OK if absent */
394 if (!is_req_in_vec(&req
, &(enabler
->actor_reqs
))) {
395 BV_SET(ustate_act_cache
[utype_index(putype
)][ACTION_ANY
],
396 requirement_unit_state_ereq(req
.source
.value
.unit_state
,
398 BV_SET(ustate_act_cache
[utype_index(putype
)][enabler
->action
],
399 requirement_unit_state_ereq(req
.source
.value
.unit_state
,
403 } action_enablers_iterate_end
;
407 /**************************************************************************
408 Cache what actions may be possible for a unit of the type putype for
409 each local DiplRel variation. Since a diplomatic relationship could be
410 ignored both present and !present must be checked.
412 Note: since can_unit_act_when_local_diplrel_is() only supports querying
413 the local range no values for the other ranges are set.
414 **************************************************************************/
415 static void local_dipl_rel_action_cache_set(struct unit_type
*putype
)
417 struct requirement req
;
418 int putype_id
= utype_index(putype
);
420 /* The unit is not yet known to be allowed to perform any actions no
421 * matter what the diplomatic state is. */
422 action_iterate(action_id
) {
423 BV_CLR_ALL(dipl_rel_action_cache
[putype_id
][action_id
]);
424 } action_iterate_end
;
425 BV_CLR_ALL(dipl_rel_action_cache
[putype_id
][ACTION_ANY
]);
426 BV_CLR_ALL(dipl_rel_action_cache
[putype_id
][ACTION_HOSTILE
]);
428 if (!utype_may_act_at_all(putype
)) {
429 /* Not an actor unit. */
433 /* Common for every situation */
434 req
.range
= REQ_RANGE_LOCAL
;
435 req
.survives
= FALSE
;
436 req
.source
.kind
= VUT_DIPLREL
;
438 /* DiplRel starts with diplstate_type and ends with diplrel_other */
439 for (req
.source
.value
.diplrel
= diplstate_type_begin();
440 req
.source
.value
.diplrel
!= DRO_LAST
;
441 req
.source
.value
.diplrel
++) {
443 /* No action will ever be possible in a specific diplomatic relation if
444 * its presence contradicts all action enablers.
445 * Everything was set to false above. It is therefore OK to only change
446 * the cache when units can do an action given a certain diplomatic
447 * relationship property value. */
448 action_enablers_iterate(enabler
) {
449 if (requirement_fulfilled_by_unit_type(putype
,
450 &(enabler
->actor_reqs
))
451 && action_get_actor_kind(enabler
->action
) == AAK_UNIT
) {
453 if (!does_req_contradicts_reqs(&req
, &(enabler
->actor_reqs
))) {
454 BV_SET(dipl_rel_action_cache
[putype_id
][enabler
->action
],
455 requirement_diplrel_ereq(req
.source
.value
.diplrel
,
456 REQ_RANGE_LOCAL
, TRUE
));
457 BV_SET(dipl_rel_action_cache
[putype_id
][ACTION_HOSTILE
],
458 requirement_diplrel_ereq(req
.source
.value
.diplrel
,
459 REQ_RANGE_LOCAL
, TRUE
));
460 BV_SET(dipl_rel_action_cache
[putype_id
][ACTION_ANY
],
461 requirement_diplrel_ereq(req
.source
.value
.diplrel
,
462 REQ_RANGE_LOCAL
, TRUE
));
466 if (!does_req_contradicts_reqs(&req
, &(enabler
->actor_reqs
))) {
467 BV_SET(dipl_rel_action_cache
[putype_id
][enabler
->action
],
468 requirement_diplrel_ereq(req
.source
.value
.diplrel
,
469 REQ_RANGE_LOCAL
, FALSE
));
470 BV_SET(dipl_rel_action_cache
[putype_id
][ACTION_HOSTILE
],
471 requirement_diplrel_ereq(req
.source
.value
.diplrel
,
472 REQ_RANGE_LOCAL
, FALSE
));
473 BV_SET(dipl_rel_action_cache
[putype_id
][ACTION_ANY
],
474 requirement_diplrel_ereq(req
.source
.value
.diplrel
,
475 REQ_RANGE_LOCAL
, FALSE
));
478 } action_enablers_iterate_end
;
487 #define MOVES_LEFT_INFINITY -1
489 /**************************************************************************
490 Get the legal range of move fragments left of the specified requirement
492 **************************************************************************/
493 static struct range
*moves_left_range(struct requirement_vector
*reqs
)
495 struct range
*prange
= fc_malloc(sizeof(prange
));
498 prange
->max
= MOVES_LEFT_INFINITY
;
500 requirement_vector_iterate(reqs
, preq
) {
501 if (preq
->source
.kind
== VUT_MINMOVES
) {
503 prange
->min
= preq
->source
.value
.minmoves
;
505 prange
->max
= preq
->source
.value
.minmoves
;
508 } requirement_vector_iterate_end
;
513 /**************************************************************************
514 Cache if any action may be possible for a unit of the type putype given
515 the property tested for. Since a it could be ignored both present and
516 !present must be checked.
517 **************************************************************************/
518 void unit_type_action_cache_set(struct unit_type
*ptype
)
520 unit_can_act_cache_set(ptype
);
521 unit_state_action_cache_set(ptype
);
522 local_dipl_rel_action_cache_set(ptype
);
525 /**************************************************************************
526 Cache what unit types may be allowed do what actions, both at all and
527 when certain properties are true.
528 **************************************************************************/
529 void unit_type_action_cache_init(void)
531 unit_type_iterate(u
) {
532 unit_type_action_cache_set(u
);
533 } unit_type_iterate_end
;
536 /**************************************************************************
537 Return TRUE iff there exists an (action enabler controlled) action that a
538 unit of the type punit_type can perform while its unit state property
539 prop has the value is_there.
540 **************************************************************************/
541 bool can_unit_act_when_ustate_is(const struct unit_type
*punit_type
,
542 const enum ustate_prop prop
,
545 return utype_can_do_act_when_ustate(punit_type
, ACTION_ANY
, prop
, is_there
);
548 /**************************************************************************
549 Return TRUE iff the unit type can do the specified (action enabler
550 controlled) action while its unit state property prop has the value
552 **************************************************************************/
553 bool utype_can_do_act_when_ustate(const struct unit_type
*punit_type
,
555 const enum ustate_prop prop
,
558 return BV_ISSET(ustate_act_cache
[utype_index(punit_type
)][action_id
],
559 requirement_unit_state_ereq(prop
, is_there
));
562 /**************************************************************************
563 Return TRUE iff the given (action enabler controlled) action can be
564 performed by a unit of the given type while the given property of its
565 owner's diplomatic relationship to the target's owner has the given
568 Note: since this only supports the local range no information for other
569 ranges are stored in dipl_rel_action_cache.
570 **************************************************************************/
571 bool can_utype_do_act_if_tgt_diplrel(const struct unit_type
*punit_type
,
576 int utype_id
= utype_index(punit_type
);
578 return BV_ISSET(dipl_rel_action_cache
[utype_id
][action_id
],
579 requirement_diplrel_ereq(prop
, REQ_RANGE_LOCAL
, is_there
));
582 /**************************************************************************
583 Return TRUE iff the given (action enabler controlled) action may be
584 performed by a unit of the given type that has the given number of move
587 Note: Values aren't cached. If a performance critical user appears it
588 would be a good idea to cache the (merged) ranges of move fragments
589 where a unit of the given type can perform the specified action.
590 **************************************************************************/
591 bool utype_may_act_move_frags(struct unit_type
*punit_type
,
593 const int move_fragments
)
595 struct range
*ml_range
;
597 fc_assert(action_id_is_valid(action_id
) || action_id
== ACTION_ANY
);
599 if (!utype_may_act_at_all(punit_type
)) {
600 /* Not an actor unit. */
604 if (action_id
== ACTION_ANY
) {
605 /* Any action is OK. */
606 action_iterate(alt_act
) {
607 if (utype_may_act_move_frags(punit_type
, alt_act
,
609 /* It only has to be true for one action. */
612 } action_iterate_end
;
614 /* No action enabled. */
618 if (action_get_actor_kind(action_id
) != AAK_UNIT
) {
619 /* This action isn't performed by any unit at all so this unit type
624 action_enabler_list_iterate(action_enablers_for_action(action_id
),
626 if (!requirement_fulfilled_by_unit_type(punit_type
,
627 &(enabler
->actor_reqs
))) {
628 /* This action enabler isn't for this unit type at all. */
632 ml_range
= moves_left_range(&(enabler
->actor_reqs
));
633 if (ml_range
->min
<= move_fragments
634 && (ml_range
->max
== MOVES_LEFT_INFINITY
635 || ml_range
->max
> move_fragments
)) {
636 /* The number of move fragments is in range of the action enabler. */
644 } action_enabler_list_iterate_end
;
649 /****************************************************************************
650 Returns the number of shields it takes to build this unit type.
651 ****************************************************************************/
652 int utype_build_shield_cost(const struct unit_type
*punittype
)
654 return MAX(punittype
->build_cost
* game
.info
.shieldbox
/ 100, 1);
657 /****************************************************************************
658 Returns the number of shields it takes to build this unit.
659 ****************************************************************************/
660 int unit_build_shield_cost(const struct unit
*punit
)
662 return utype_build_shield_cost(unit_type_get(punit
));
665 /****************************************************************************
666 Returns the amount of gold it takes to rush this unit.
667 ****************************************************************************/
668 int utype_buy_gold_cost(const struct unit_type
*punittype
,
669 int shields_in_stock
)
672 const int missing
= utype_build_shield_cost(punittype
) - shields_in_stock
;
675 cost
= 2 * missing
+ (missing
* missing
) / 20;
677 if (shields_in_stock
== 0) {
683 /****************************************************************************
684 Returns the number of shields received when this unit type is disbanded.
685 ****************************************************************************/
686 int utype_disband_shields(const struct unit_type
*punittype
)
688 return utype_build_shield_cost(punittype
) / 2;
691 /****************************************************************************
692 Returns the number of shields received when this unit is disbanded.
693 ****************************************************************************/
694 int unit_disband_shields(const struct unit
*punit
)
696 return utype_disband_shields(unit_type_get(punit
));
699 /**************************************************************************
700 How much city shrinks when it builds unit of this type.
701 **************************************************************************/
702 int utype_pop_value(const struct unit_type
*punittype
)
704 return (punittype
->pop_cost
);
707 /**************************************************************************
708 How much population is put to building this unit.
709 **************************************************************************/
710 int unit_pop_value(const struct unit
*punit
)
712 return utype_pop_value(unit_type_get(punit
));
715 /**************************************************************************
716 Return move type of the unit type
717 **************************************************************************/
718 enum unit_move_type
utype_move_type(const struct unit_type
*punittype
)
720 return utype_class(punittype
)->move_type
;
723 /**************************************************************************
724 Return the (translated) name of the unit type.
725 You don't have to free the return pointer.
726 **************************************************************************/
727 const char *utype_name_translation(const struct unit_type
*punittype
)
729 return name_translation_get(&punittype
->name
);
732 /**************************************************************************
733 Return the (translated) name of the unit.
734 You don't have to free the return pointer.
735 **************************************************************************/
736 const char *unit_name_translation(const struct unit
*punit
)
738 return utype_name_translation(unit_type_get(punit
));
741 /**************************************************************************
742 Return the (untranslated) rule name of the unit type.
743 You don't have to free the return pointer.
744 **************************************************************************/
745 const char *utype_rule_name(const struct unit_type
*punittype
)
747 return rule_name_get(&punittype
->name
);
750 /**************************************************************************
751 Return the (untranslated) rule name of the unit.
752 You don't have to free the return pointer.
753 **************************************************************************/
754 const char *unit_rule_name(const struct unit
*punit
)
756 return utype_rule_name(unit_type_get(punit
));
759 /**************************************************************************
760 Return string describing unit type values.
761 String is static buffer that gets reused when function is called again.
762 **************************************************************************/
763 const char *utype_values_string(const struct unit_type
*punittype
)
765 static char buffer
[256];
767 /* Print in two parts as move_points_text() returns a static buffer */
768 fc_snprintf(buffer
, sizeof(buffer
), "%d/%d/%s",
769 punittype
->attack_strength
,
770 punittype
->defense_strength
,
771 move_points_text(punittype
->move_rate
, TRUE
));
772 if (utype_fuel(punittype
)) {
773 cat_snprintf(buffer
, sizeof(buffer
), "(%s)",
774 move_points_text(punittype
->move_rate
* utype_fuel(punittype
),
780 /**************************************************************************
781 Return string with translated unit name and list of its values.
782 String is static buffer that gets reused when function is called again.
783 **************************************************************************/
784 const char *utype_values_translation(const struct unit_type
*punittype
)
786 static char buffer
[256];
788 fc_snprintf(buffer
, sizeof(buffer
),
790 utype_name_translation(punittype
),
791 utype_values_string(punittype
));
795 /**************************************************************************
796 Return the (translated) name of the unit class.
797 You don't have to free the return pointer.
798 **************************************************************************/
799 const char *uclass_name_translation(const struct unit_class
*pclass
)
801 return name_translation_get(&pclass
->name
);
804 /**************************************************************************
805 Return the (untranslated) rule name of the unit class.
806 You don't have to free the return pointer.
807 **************************************************************************/
808 const char *uclass_rule_name(const struct unit_class
*pclass
)
810 return rule_name_get(&pclass
->name
);
813 /****************************************************************************
814 Return a string with all the names of units with this flag. If "alts" is
815 set, separate with "or", otherwise "and". Return FALSE if no unit with
817 ****************************************************************************/
818 bool role_units_translations(struct astring
*astr
, int flag
, bool alts
)
820 const int count
= num_role_units(flag
);
824 astr_set(astr
, _("%s or similar units"),
825 utype_name_translation(get_role_unit(flag
, 0)));
827 astr_set(astr
, _("%s and similar units"),
828 utype_name_translation(get_role_unit(flag
, 0)));
831 } else if (0 < count
) {
832 const char *vec
[count
];
835 for (i
= 0; i
< count
; i
++) {
836 vec
[i
] = utype_name_translation(get_role_unit(flag
, i
));
840 astr_build_or_list(astr
, vec
, count
);
842 astr_build_and_list(astr
, vec
, count
);
849 /**************************************************************************
850 Return whether this player can upgrade this unit type (to any other
851 unit type). Returns NULL if no upgrade is possible.
852 **************************************************************************/
853 struct unit_type
*can_upgrade_unittype(const struct player
*pplayer
,
854 struct unit_type
*punittype
)
856 struct unit_type
*upgrade
= punittype
;
857 struct unit_type
*best_upgrade
= NULL
;
859 /* For some reason this used to check
860 * can_player_build_unit_direct() for the unittype
861 * we're updating from.
862 * That check is now removed as it prevented legal updates
863 * of units player had acquired via bribing, capturing,
864 * diplomatic treaties, or lua script. */
866 while ((upgrade
= upgrade
->obsoleted_by
) != U_NOT_OBSOLETED
) {
867 if (can_player_build_unit_direct(pplayer
, upgrade
)) {
868 best_upgrade
= upgrade
;
875 /**************************************************************************
876 Return the cost (gold) of upgrading a single unit of the specified type
877 to the new type. This price could (but currently does not) depend on
878 other attributes (like nation or government type) of the player the unit
880 **************************************************************************/
881 int unit_upgrade_price(const struct player
*pplayer
,
882 const struct unit_type
*from
,
883 const struct unit_type
*to
)
885 int base_cost
= utype_buy_gold_cost(to
, utype_disband_shields(from
));
888 * (100 + get_player_bonus(pplayer
, EFT_UPGRADE_PRICE_PCT
))
892 /**************************************************************************
893 Returns the unit type that has the given (translated) name.
894 Returns NULL if none match.
895 **************************************************************************/
896 struct unit_type
*unit_type_by_translated_name(const char *name
)
898 unit_type_iterate(punittype
) {
899 if (0 == strcmp(utype_name_translation(punittype
), name
)) {
902 } unit_type_iterate_end
;
907 /**************************************************************************
908 Returns the unit type that has the given (untranslated) rule name.
909 Returns NULL if none match.
910 **************************************************************************/
911 struct unit_type
*unit_type_by_rule_name(const char *name
)
913 const char *qname
= Qn_(name
);
915 unit_type_iterate(punittype
) {
916 if (0 == fc_strcasecmp(utype_rule_name(punittype
), qname
)) {
919 } unit_type_iterate_end
;
924 /**************************************************************************
925 Returns the unit class that has the given (untranslated) rule name.
926 Returns NULL if none match.
927 **************************************************************************/
928 struct unit_class
*unit_class_by_rule_name(const char *s
)
930 const char *qs
= Qn_(s
);
932 unit_class_iterate(pclass
) {
933 if (0 == fc_strcasecmp(uclass_rule_name(pclass
), qs
)) {
936 } unit_class_iterate_end
;
941 /**************************************************************************
942 Initialize user unit type flags.
943 **************************************************************************/
944 void user_unit_type_flags_init(void)
948 for (i
= 0; i
< MAX_NUM_USER_UNIT_FLAGS
; i
++) {
949 user_flag_init(&user_type_flags
[i
]);
953 /**************************************************************************
954 Sets user defined name for unit flag.
955 **************************************************************************/
956 void set_user_unit_type_flag_name(enum unit_type_flag_id id
, const char *name
,
959 int ufid
= id
- UTYF_USER_FLAG_1
;
961 fc_assert_ret(id
>= UTYF_USER_FLAG_1
&& id
<= UTYF_LAST_USER_FLAG
);
963 if (user_type_flags
[ufid
].name
!= NULL
) {
964 FC_FREE(user_type_flags
[ufid
].name
);
965 user_type_flags
[ufid
].name
= NULL
;
968 if (name
&& name
[0] != '\0') {
969 user_type_flags
[ufid
].name
= fc_strdup(name
);
972 if (user_type_flags
[ufid
].helptxt
!= NULL
) {
973 free(user_type_flags
[ufid
].helptxt
);
974 user_type_flags
[ufid
].helptxt
= NULL
;
977 if (helptxt
&& helptxt
[0] != '\0') {
978 user_type_flags
[ufid
].helptxt
= fc_strdup(helptxt
);
982 /**************************************************************************
983 Unit type flag name callback, called from specenum code.
984 **************************************************************************/
985 const char *unit_type_flag_id_name_cb(enum unit_type_flag_id flag
)
987 if (flag
< UTYF_USER_FLAG_1
|| flag
> UTYF_LAST_USER_FLAG
) {
991 return user_type_flags
[flag
- UTYF_USER_FLAG_1
].name
;
994 /**************************************************************************
995 Return the (untranslated) helptxt of the user unit flag.
996 **************************************************************************/
997 const char *unit_type_flag_helptxt(enum unit_type_flag_id id
)
999 fc_assert(id
>= UTYF_USER_FLAG_1
&& id
<= UTYF_LAST_USER_FLAG
);
1001 return user_type_flags
[id
- UTYF_USER_FLAG_1
].helptxt
;
1004 /**************************************************************************
1005 Returns TRUE iff the unit type is unique and the player already has one.
1006 **************************************************************************/
1007 bool utype_player_already_has_this_unique(const struct player
*pplayer
,
1008 const struct unit_type
*putype
)
1010 if (!utype_has_flag(putype
, UTYF_UNIQUE
)) {
1011 /* This isn't a unique unit type. */
1015 unit_list_iterate(pplayer
->units
, existing_unit
) {
1016 if (putype
== unit_type_get(existing_unit
)) {
1017 /* FIXME: This could be slow if we have lots of units. We could
1018 * consider keeping an array of unittypes updated with this info
1023 } unit_list_iterate_end
;
1025 /* The player doesn't already have one. */
1029 /**************************************************************************
1030 Whether player can build given unit somewhere,
1031 ignoring whether unit is obsolete and assuming the
1032 player has a coastal city.
1033 **************************************************************************/
1034 bool can_player_build_unit_direct(const struct player
*p
,
1035 const struct unit_type
*punittype
)
1037 fc_assert_ret_val(NULL
!= punittype
, FALSE
);
1040 && !utype_has_role(punittype
, L_BARBARIAN_BUILD
)
1041 && !utype_has_role(punittype
, L_BARBARIAN_BUILD_TECH
)) {
1042 /* Barbarians can build only role units */
1046 if (utype_has_flag(punittype
, UTYF_NUCLEAR
)
1047 && get_player_bonus(p
, EFT_ENABLE_NUKE
) <= 0) {
1050 if (utype_has_flag(punittype
, UTYF_NOBUILD
)) {
1054 if (utype_has_flag(punittype
, UTYF_BARBARIAN_ONLY
)
1055 && !is_barbarian(p
)) {
1056 /* Unit can be built by barbarians only and this player is
1061 if (utype_has_flag(punittype
, UTYF_NEWCITY_GAMES_ONLY
)
1062 && game
.scenario
.prevent_new_cities
) {
1063 /* Unit is usable only in games where founding of new cities is allowed. */
1067 if (punittype
->need_government
1068 && punittype
->need_government
!= government_of_player(p
)) {
1071 if (research_invention_state(research_get(p
),
1072 advance_number(punittype
->require_advance
))
1074 if (!is_barbarian(p
)) {
1075 /* Normal players can never build units without knowing tech
1079 if (!utype_has_role(punittype
, L_BARBARIAN_BUILD
)) {
1080 /* Even barbarian cannot build this unit without tech */
1082 /* Unit has to have L_BARBARIAN_BUILD_TECH role
1083 * In the beginning of this function we checked that
1084 * barbarian player tries to build only role
1085 * L_BARBARIAN_BUILD or L_BARBARIAN_BUILD_TECH units. */
1086 fc_assert_ret_val(utype_has_role(punittype
, L_BARBARIAN_BUILD_TECH
),
1089 /* Client does not know all the advances other players have
1090 * got. So following gives wrong answer in the client.
1091 * This is called at the client when received create_city
1092 * packet for a barbarian city. City initialization tries
1093 * to find L_FIRSTBUILD unit. */
1095 if (!game
.info
.global_advances
[advance_index(punittype
->require_advance
)]) {
1096 /* Nobody knows required tech */
1102 if (utype_player_already_has_this_unique(p
, punittype
)) {
1103 /* A player can only have one unit of each unique unit type. */
1107 /* If the unit has a building requirement, we check to see if the player
1108 * can build that building. Note that individual cities may not have
1109 * that building, so they still may not be able to build the unit. */
1110 if (punittype
->need_improvement
) {
1111 if (is_great_wonder(punittype
->need_improvement
)
1112 && (great_wonder_is_built(punittype
->need_improvement
)
1113 || great_wonder_is_destroyed(punittype
->need_improvement
))) {
1114 /* It's already built great wonder */
1115 if (great_wonder_owner(punittype
->need_improvement
) != p
) {
1116 /* Not owned by this player. Either destroyed or owned by somebody
1121 if (!can_player_build_improvement_direct(p
,
1122 punittype
->need_improvement
)) {
1131 /**************************************************************************
1132 Whether player can build given unit somewhere;
1133 returns FALSE if unit is obsolete.
1134 **************************************************************************/
1135 bool can_player_build_unit_now(const struct player
*p
,
1136 const struct unit_type
*punittype
)
1138 if (!can_player_build_unit_direct(p
, punittype
)) {
1141 while ((punittype
= punittype
->obsoleted_by
) != U_NOT_OBSOLETED
) {
1142 if (can_player_build_unit_direct(p
, punittype
)) {
1149 /**************************************************************************
1150 Whether player can _eventually_ build given unit somewhere -- ie,
1151 returns TRUE if unit is available with current tech OR will be available
1152 with future tech. Returns FALSE if unit is obsolete.
1153 **************************************************************************/
1154 bool can_player_build_unit_later(const struct player
*p
,
1155 const struct unit_type
*punittype
)
1157 fc_assert_ret_val(NULL
!= punittype
, FALSE
);
1158 if (utype_has_flag(punittype
, UTYF_NOBUILD
)) {
1161 while ((punittype
= punittype
->obsoleted_by
) != U_NOT_OBSOLETED
) {
1162 if (can_player_build_unit_direct(p
, punittype
)) {
1169 /**************************************************************************
1170 The following functions use static variables so we can quickly look up
1171 which unit types have given flag or role.
1172 For these functions flags and roles are considered to be in the same "space",
1173 and any "role" argument can also be a "flag".
1174 Unit order is in terms of the order in the units ruleset.
1175 **************************************************************************/
1176 static bool first_init
= TRUE
;
1177 static int n_with_role
[MAX_UNIT_ROLES
];
1178 static struct unit_type
**with_role
[MAX_UNIT_ROLES
];
1180 /**************************************************************************
1181 Do the real work for role_unit_precalcs, for one role (or flag), given by i.
1182 **************************************************************************/
1183 static void precalc_one(int i
,
1184 bool (*func_has
)(const struct unit_type
*, int))
1189 fc_assert(n_with_role
[i
] == 0);
1190 unit_type_iterate(u
) {
1191 if (func_has(u
, i
)) {
1194 } unit_type_iterate_end
;
1196 if(n_with_role
[i
] > 0) {
1197 with_role
[i
] = fc_malloc(n_with_role
[i
] * sizeof(*with_role
[i
]));
1199 unit_type_iterate(u
) {
1200 if (func_has(u
, i
)) {
1201 with_role
[i
][j
++] = u
;
1203 } unit_type_iterate_end
;
1204 fc_assert(j
== n_with_role
[i
]);
1208 /****************************************************************************
1209 Free memory allocated by role_unit_precalcs().
1210 ****************************************************************************/
1211 void role_unit_precalcs_free(void)
1215 for (i
= 0; i
< MAX_UNIT_ROLES
; i
++) {
1217 with_role
[i
] = NULL
;
1222 /****************************************************************************
1223 Initialize; it is safe to call this multiple times (e.g., if units have
1224 changed due to rulesets in client).
1225 ****************************************************************************/
1226 void role_unit_precalcs(void)
1231 for (i
= 0; i
< MAX_UNIT_ROLES
; i
++) {
1232 with_role
[i
] = NULL
;
1236 role_unit_precalcs_free();
1239 for (i
= 0; i
<= UTYF_LAST_USER_FLAG
; i
++) {
1240 precalc_one(i
, utype_has_flag
);
1242 for (i
= L_FIRST
; i
< L_LAST
; i
++) {
1243 precalc_one(i
, utype_has_role
);
1245 for (i
= L_LAST
; i
< MAX_UNIT_ROLES
; i
++) {
1246 precalc_one(i
, utype_can_do_action_role
);
1251 /**************************************************************************
1252 How many unit types have specified role/flag.
1253 **************************************************************************/
1254 int num_role_units(int role
)
1256 fc_assert_ret_val((role
>= 0 && role
<= UTYF_LAST_USER_FLAG
)
1257 || (role
>= L_FIRST
&& role
< L_LAST
)
1258 || (role
>= L_LAST
&& role
< MAX_UNIT_ROLES
), -1);
1259 fc_assert_ret_val(!first_init
, -1);
1260 return n_with_role
[role
];
1263 /**************************************************************************
1264 Iterate over all the role units and feed them to callback.
1265 Once callback returns TRUE, no further units are feeded to it and
1266 we return the unit that caused callback to return TRUE
1267 **************************************************************************/
1268 struct unit_type
*role_units_iterate(int role
, role_unit_callback cb
, void *data
)
1272 for (i
= 0; i
< n_with_role
[role
]; i
++) {
1273 if (cb(with_role
[role
][i
], data
)) {
1274 return with_role
[role
][i
];
1281 /**************************************************************************
1282 Iterate over all the role units and feed them to callback, starting
1284 Once callback returns TRUE, no further units are feeded to it and
1285 we return the unit that caused callback to return TRUE
1286 **************************************************************************/
1287 struct unit_type
*role_units_iterate_backwards(int role
, role_unit_callback cb
, void *data
)
1291 for (i
= n_with_role
[role
] - 1; i
>= 0; i
--) {
1292 if (cb(with_role
[role
][i
], data
)) {
1293 return with_role
[role
][i
];
1300 /**************************************************************************
1301 Return index-th unit with specified role/flag.
1302 Index -1 means (n-1), ie last one.
1303 **************************************************************************/
1304 struct unit_type
*get_role_unit(int role
, int role_index
)
1306 fc_assert_ret_val((role
>= 0 && role
<= UTYF_LAST_USER_FLAG
)
1307 || (role
>= L_FIRST
&& role
< L_LAST
)
1308 || (role
>= L_LAST
&& role
< MAX_UNIT_ROLES
), NULL
);
1309 fc_assert_ret_val(!first_init
, NULL
);
1310 if (role_index
== -1) {
1311 role_index
= n_with_role
[role
] - 1;
1313 fc_assert_ret_val(role_index
>= 0 && role_index
< n_with_role
[role
], NULL
);
1315 return with_role
[role
][role_index
];
1318 /**************************************************************************
1319 Return "best" unit this city can build, with given role/flag.
1320 Returns NULL if none match. "Best" means highest unit type id.
1321 **************************************************************************/
1322 struct unit_type
*best_role_unit(const struct city
*pcity
, int role
)
1324 struct unit_type
*u
;
1327 fc_assert_ret_val((role
>= 0 && role
<= UTYF_LAST_USER_FLAG
)
1328 || (role
>= L_FIRST
&& role
< L_LAST
)
1329 || (role
>= L_LAST
&& role
< MAX_UNIT_ROLES
), NULL
);
1330 fc_assert_ret_val(!first_init
, NULL
);
1332 for(j
=n_with_role
[role
]-1; j
>=0; j
--) {
1333 u
= with_role
[role
][j
];
1334 if ((1 != utype_fuel(u
) || uclass_has_flag(utype_class(u
), UCF_MISSILE
))
1335 && can_city_build_unit_now(pcity
, u
)) {
1336 /* Allow fuel==1 units when pathfinding can handle them. */
1343 /**************************************************************************
1344 Return "best" unit the player can build, with given role/flag.
1345 Returns NULL if none match. "Best" means highest unit type id.
1347 TODO: Cache the result per player?
1348 **************************************************************************/
1349 struct unit_type
*best_role_unit_for_player(const struct player
*pplayer
,
1354 fc_assert_ret_val((role
>= 0 && role
<= UTYF_LAST_USER_FLAG
)
1355 || (role
>= L_FIRST
&& role
< L_LAST
)
1356 || (role
>= L_LAST
&& role
< MAX_UNIT_ROLES
), NULL
);
1357 fc_assert_ret_val(!first_init
, NULL
);
1359 for(j
= n_with_role
[role
]-1; j
>= 0; j
--) {
1360 struct unit_type
*utype
= with_role
[role
][j
];
1362 if (can_player_build_unit_now(pplayer
, utype
)) {
1370 /**************************************************************************
1371 Return first unit the player can build, with given role/flag.
1372 Returns NULL if none match. Used eg when placing starting units.
1373 **************************************************************************/
1374 struct unit_type
*first_role_unit_for_player(const struct player
*pplayer
,
1379 fc_assert_ret_val((role
>= 0 && role
<= UTYF_LAST_USER_FLAG
)
1380 || (role
>= L_FIRST
&& role
< L_LAST
)
1381 || (role
>= L_LAST
&& role
< MAX_UNIT_ROLES
), NULL
);
1382 fc_assert_ret_val(!first_init
, NULL
);
1384 for(j
= 0; j
< n_with_role
[role
]; j
++) {
1385 struct unit_type
*utype
= with_role
[role
][j
];
1387 if (can_player_build_unit_now(pplayer
, utype
)) {
1395 /****************************************************************************
1396 Inialize unit-type structures.
1397 ****************************************************************************/
1398 void unit_types_init(void)
1402 /* Can't use unit_type_iterate or utype_by_number here because
1403 * num_unit_types isn't known yet. */
1404 for (i
= 0; i
< ARRAY_SIZE(unit_types
); i
++) {
1405 unit_types
[i
].item_number
= i
;
1406 unit_types
[i
].helptext
= NULL
;
1407 unit_types
[i
].veteran
= NULL
;
1408 unit_types
[i
].bonuses
= combat_bonus_list_new();
1409 unit_types
[i
].disabled
= FALSE
;
1413 /**************************************************************************
1414 Frees the memory associated with this unit type.
1415 **************************************************************************/
1416 static void unit_type_free(struct unit_type
*punittype
)
1418 if (NULL
!= punittype
->helptext
) {
1419 strvec_destroy(punittype
->helptext
);
1420 punittype
->helptext
= NULL
;
1423 veteran_system_destroy(punittype
->veteran
);
1424 combat_bonus_list_iterate(punittype
->bonuses
, pbonus
) {
1426 } combat_bonus_list_iterate_end
;
1427 combat_bonus_list_destroy(punittype
->bonuses
);
1430 /***************************************************************
1431 Frees the memory associated with all unit types.
1432 ***************************************************************/
1433 void unit_types_free(void)
1437 /* Can't use unit_type_iterate or utype_by_number here because
1438 * we want to free what unit_types_init() has allocated. */
1439 for (i
= 0; i
< ARRAY_SIZE(unit_types
); i
++) {
1440 unit_type_free(unit_types
+ i
);
1444 /***************************************************************
1445 Frees the memory associated with all unit type flags
1446 ***************************************************************/
1447 void unit_type_flags_free(void)
1451 for (i
= 0; i
< MAX_NUM_USER_UNIT_FLAGS
; i
++) {
1452 user_flag_free(&user_type_flags
[i
]);
1456 /**************************************************************************
1457 Return the first item of unit_classes.
1458 **************************************************************************/
1459 struct unit_class
*unit_class_array_first(void)
1461 if (game
.control
.num_unit_classes
> 0) {
1462 return unit_classes
;
1467 /**************************************************************************
1468 Return the last item of unit_classes.
1469 **************************************************************************/
1470 const struct unit_class
*unit_class_array_last(void)
1472 if (game
.control
.num_unit_classes
> 0) {
1473 return &unit_classes
[game
.control
.num_unit_classes
- 1];
1478 /**************************************************************************
1479 Return the unit_class count.
1480 **************************************************************************/
1481 Unit_Class_id
uclass_count(void)
1483 return game
.control
.num_unit_classes
;
1486 #ifndef uclass_index
1487 /**************************************************************************
1488 Return the unit_class index.
1490 Currently same as uclass_number(), paired with uclass_count()
1491 indicates use as an array index.
1492 **************************************************************************/
1493 Unit_Class_id
uclass_index(const struct unit_class
*pclass
)
1495 fc_assert_ret_val(pclass
, -1);
1496 return pclass
- unit_classes
;
1498 #endif /* uclass_index */
1500 /**************************************************************************
1501 Return the unit_class index.
1502 **************************************************************************/
1503 Unit_Class_id
uclass_number(const struct unit_class
*pclass
)
1505 fc_assert_ret_val(pclass
, -1);
1506 return pclass
->item_number
;
1509 /****************************************************************************
1510 Returns unit class pointer for an ID value.
1511 ****************************************************************************/
1512 struct unit_class
*uclass_by_number(const Unit_Class_id id
)
1514 if (id
< 0 || id
>= game
.control
.num_unit_classes
) {
1517 return &unit_classes
[id
];
1521 /***************************************************************
1522 Returns unit class pointer for a unit type.
1523 ***************************************************************/
1524 struct unit_class
*utype_class(const struct unit_type
*punittype
)
1526 fc_assert(NULL
!= punittype
->uclass
);
1527 return punittype
->uclass
;
1529 #endif /* utype_class */
1531 /***************************************************************
1532 Returns unit class pointer for a unit.
1533 ***************************************************************/
1534 struct unit_class
*unit_class_get(const struct unit
*punit
)
1536 return utype_class(unit_type_get(punit
));
1539 /****************************************************************************
1540 Initialize unit_class structures.
1541 ****************************************************************************/
1542 void unit_classes_init(void)
1546 /* Can't use unit_class_iterate or uclass_by_number here because
1547 * num_unit_classes isn't known yet. */
1548 for (i
= 0; i
< ARRAY_SIZE(unit_classes
); i
++) {
1549 unit_classes
[i
].item_number
= i
;
1550 unit_classes
[i
].cache
.refuel_bases
= NULL
;
1551 unit_classes
[i
].cache
.native_tile_extras
= NULL
;
1552 unit_classes
[i
].cache
.bonus_roads
= NULL
;
1553 unit_classes
[i
].cache
.subset_movers
= NULL
;
1554 unit_classes
[i
].helptext
= NULL
;
1558 /****************************************************************************
1559 Free resources allocated for unit classes.
1560 ****************************************************************************/
1561 void unit_classes_free(void)
1565 for (i
= 0; i
< ARRAY_SIZE(unit_classes
); i
++) {
1566 if (unit_classes
[i
].cache
.refuel_bases
!= NULL
) {
1567 extra_type_list_destroy(unit_classes
[i
].cache
.refuel_bases
);
1568 unit_classes
[i
].cache
.refuel_bases
= NULL
;
1570 if (unit_classes
[i
].cache
.native_tile_extras
!= NULL
) {
1571 extra_type_list_destroy(unit_classes
[i
].cache
.native_tile_extras
);
1572 unit_classes
[i
].cache
.native_tile_extras
= NULL
;
1574 if (unit_classes
[i
].cache
.bonus_roads
!= NULL
) {
1575 extra_type_list_destroy(unit_classes
[i
].cache
.bonus_roads
);
1576 unit_classes
[i
].cache
.bonus_roads
= NULL
;
1578 if (unit_classes
[i
].cache
.subset_movers
!= NULL
) {
1579 unit_class_list_destroy(unit_classes
[i
].cache
.subset_movers
);
1581 if (unit_classes
[i
].helptext
!= NULL
) {
1582 strvec_destroy(unit_classes
[i
].helptext
);
1583 unit_classes
[i
].helptext
= NULL
;
1588 /****************************************************************************
1589 Return veteran system used for this unit type.
1590 ****************************************************************************/
1591 const struct veteran_system
*
1592 utype_veteran_system(const struct unit_type
*punittype
)
1594 fc_assert_ret_val(punittype
!= NULL
, NULL
);
1596 if (punittype
->veteran
) {
1597 return punittype
->veteran
;
1600 fc_assert_ret_val(game
.veteran
!= NULL
, NULL
);
1601 return game
.veteran
;
1604 /****************************************************************************
1605 Return veteran level properties of given unit in given veterancy level.
1606 ****************************************************************************/
1607 const struct veteran_level
*
1608 utype_veteran_level(const struct unit_type
*punittype
, int level
)
1610 const struct veteran_system
*vsystem
= utype_veteran_system(punittype
);
1612 fc_assert_ret_val(vsystem
!= NULL
, NULL
);
1613 fc_assert_ret_val(vsystem
->definitions
!= NULL
, NULL
);
1614 fc_assert_ret_val(vsystem
->levels
> level
, NULL
);
1616 return (vsystem
->definitions
+ level
);
1619 /****************************************************************************
1620 Return translated name of the given veteran level.
1621 NULL if this unit type doesn't have different veteran levels.
1622 ****************************************************************************/
1623 const char *utype_veteran_name_translation(const struct unit_type
*punittype
,
1626 if (utype_veteran_levels(punittype
) <= 1) {
1629 const struct veteran_level
*vlvl
= utype_veteran_level(punittype
, level
);
1631 return name_translation_get(&vlvl
->name
);
1635 /****************************************************************************
1636 Return veteran levels of the given unit type.
1637 ****************************************************************************/
1638 int utype_veteran_levels(const struct unit_type
*punittype
)
1640 const struct veteran_system
*vsystem
= utype_veteran_system(punittype
);
1642 fc_assert_ret_val(vsystem
!= NULL
, 1);
1644 return vsystem
->levels
;
1647 /****************************************************************************
1648 Return whether this unit type's veteran system, if any, confers a power
1649 factor bonus at any level (it could just add extra moves).
1650 ****************************************************************************/
1651 bool utype_veteran_has_power_bonus(const struct unit_type
*punittype
)
1653 int i
, initial_power_fact
= utype_veteran_level(punittype
, 0)->power_fact
;
1654 for (i
= 1; i
< utype_veteran_levels(punittype
); i
++) {
1655 if (utype_veteran_level(punittype
, i
)->power_fact
> initial_power_fact
) {
1662 /****************************************************************************
1663 Allocate new veteran system structure with given veteran level count.
1664 ****************************************************************************/
1665 struct veteran_system
*veteran_system_new(int count
)
1667 struct veteran_system
*vsystem
;
1669 /* There must be at least one level. */
1670 fc_assert_ret_val(count
> 0, NULL
);
1672 vsystem
= fc_calloc(1, sizeof(*vsystem
));
1673 vsystem
->levels
= count
;
1674 vsystem
->definitions
= fc_calloc(vsystem
->levels
,
1675 sizeof(*vsystem
->definitions
));
1680 /****************************************************************************
1682 ****************************************************************************/
1683 void veteran_system_destroy(struct veteran_system
*vsystem
)
1686 if (vsystem
->definitions
) {
1687 free(vsystem
->definitions
);
1693 /****************************************************************************
1694 Fill veteran level in given veteran system with given information.
1695 ****************************************************************************/
1696 void veteran_system_definition(struct veteran_system
*vsystem
, int level
,
1697 const char *vlist_name
, int vlist_power
,
1698 int vlist_move
, int vlist_raise
,
1701 struct veteran_level
*vlevel
;
1703 fc_assert_ret(vsystem
!= NULL
);
1704 fc_assert_ret(vsystem
->levels
> level
);
1706 vlevel
= vsystem
->definitions
+ level
;
1708 names_set(&vlevel
->name
, NULL
, vlist_name
, NULL
);
1709 vlevel
->power_fact
= vlist_power
;
1710 vlevel
->move_bonus
= vlist_move
;
1711 vlevel
->raise_chance
= vlist_raise
;
1712 vlevel
->work_raise_chance
= vlist_wraise
;
1715 /**************************************************************************
1716 Return pointer to ai data of given unit type and ai type.
1717 **************************************************************************/
1718 void *utype_ai_data(const struct unit_type
*ptype
, const struct ai_type
*ai
)
1720 return ptype
->ais
[ai_type_number(ai
)];
1723 /**************************************************************************
1724 Attach ai data to unit type
1725 **************************************************************************/
1726 void utype_set_ai_data(struct unit_type
*ptype
, const struct ai_type
*ai
,
1729 ptype
->ais
[ai_type_number(ai
)] = data
;
1732 /****************************************************************************
1733 Set caches for unit class.
1734 ****************************************************************************/
1735 void set_unit_class_caches(struct unit_class
*pclass
)
1737 pclass
->cache
.refuel_bases
= extra_type_list_new();
1738 pclass
->cache
.native_tile_extras
= extra_type_list_new();
1739 pclass
->cache
.bonus_roads
= extra_type_list_new();
1740 pclass
->cache
.subset_movers
= unit_class_list_new();
1742 extra_type_iterate(pextra
) {
1743 if (is_native_extra_to_uclass(pextra
, pclass
)) {
1744 struct road_type
*proad
= extra_road_get(pextra
);
1746 if (extra_has_flag(pextra
, EF_REFUEL
)) {
1747 extra_type_list_append(pclass
->cache
.refuel_bases
, pextra
);
1749 if (extra_has_flag(pextra
, EF_NATIVE_TILE
)) {
1750 extra_type_list_append(pclass
->cache
.native_tile_extras
, pextra
);
1752 if (proad
!= NULL
&& road_provides_move_bonus(proad
)) {
1753 extra_type_list_append(pclass
->cache
.bonus_roads
, pextra
);
1756 } extra_type_iterate_end
;
1758 unit_class_iterate(pcharge
) {
1759 bool subset_mover
= TRUE
;
1761 terrain_type_iterate(pterrain
) {
1762 if (BV_ISSET(pterrain
->native_to
, uclass_index(pcharge
))
1763 && !BV_ISSET(pterrain
->native_to
, uclass_index(pclass
))) {
1764 subset_mover
= FALSE
;
1766 } terrain_type_iterate_end
;
1769 extra_type_iterate(pextra
) {
1770 if (is_native_extra_to_uclass(pextra
, pcharge
)
1771 && !is_native_extra_to_uclass(pextra
, pclass
)) {
1772 subset_mover
= FALSE
;
1774 } extra_type_list_iterate_end
;
1778 unit_class_list_append(pclass
->cache
.subset_movers
, pcharge
);
1780 } unit_class_iterate_end
;
1783 /****************************************************************************
1784 Set caches for unit types.
1785 ****************************************************************************/
1786 void set_unit_type_caches(struct unit_type
*ptype
)
1788 ptype
->cache
.max_defense_mp
= -FC_INFINITY
;
1790 unit_type_iterate(utype
) {
1791 int idx
= utype_index(utype
);
1793 ptype
->cache
.defense_mp_bonuses
[idx
] = combat_bonus_against(ptype
->bonuses
, utype
,
1794 CBONUS_DEFENSE_MULTIPLIER
);
1795 if (ptype
->cache
.defense_mp_bonuses
[idx
] > ptype
->cache
.max_defense_mp
) {
1796 ptype
->cache
.max_defense_mp
= ptype
->cache
.defense_mp_bonuses
[idx
];
1798 } unit_type_iterate_end
;
1801 /**************************************************************************
1802 What move types nativity of this extra will give?
1803 **************************************************************************/
1804 static enum unit_move_type
move_type_from_extra(struct extra_type
*pextra
,
1805 struct unit_class
*puc
)
1807 bool land_allowed
= TRUE
;
1808 bool sea_allowed
= TRUE
;
1810 if (!extra_has_flag(pextra
, EF_NATIVE_TILE
)) {
1811 return unit_move_type_invalid();
1813 if (!is_native_extra_to_uclass(pextra
, puc
)) {
1814 return unit_move_type_invalid();
1817 if (is_extra_caused_by(pextra
, EC_ROAD
)
1818 && road_has_flag(extra_road_get(pextra
), RF_RIVER
)) {
1819 /* Natural rivers are created to land only */
1820 sea_allowed
= FALSE
;
1823 requirement_vector_iterate(&pextra
->reqs
, preq
) {
1824 if (preq
->source
.kind
== VUT_TERRAINCLASS
) {
1825 if (!preq
->present
) {
1826 if (preq
->source
.value
.terrainclass
== TC_LAND
) {
1827 land_allowed
= FALSE
;
1828 } else if (preq
->source
.value
.terrainclass
== TC_OCEAN
) {
1829 sea_allowed
= FALSE
;
1832 if (preq
->source
.value
.terrainclass
== TC_LAND
) {
1833 sea_allowed
= FALSE
;
1834 } else if (preq
->source
.value
.terrainclass
== TC_OCEAN
) {
1835 land_allowed
= FALSE
;
1838 } else if (preq
->source
.kind
== VUT_TERRAIN
) {
1839 if (!preq
->present
) {
1840 if (preq
->source
.value
.terrain
->tclass
== TC_LAND
) {
1841 land_allowed
= FALSE
;
1842 } else if (preq
->source
.value
.terrain
->tclass
== TC_OCEAN
) {
1843 sea_allowed
= FALSE
;
1846 if (preq
->source
.value
.terrain
->tclass
== TC_LAND
) {
1847 sea_allowed
= FALSE
;
1848 } else if (preq
->source
.value
.terrain
->tclass
== TC_OCEAN
) {
1849 land_allowed
= FALSE
;
1853 } requirement_vector_iterate_end
;
1855 if (land_allowed
&& sea_allowed
) {
1858 if (land_allowed
&& !sea_allowed
) {
1861 if (!land_allowed
&& sea_allowed
) {
1865 return unit_move_type_invalid();
1868 /****************************************************************************
1869 Set move_type for unit class.
1870 ****************************************************************************/
1871 void set_unit_move_type(struct unit_class
*puclass
)
1873 bool land_moving
= FALSE
;
1874 bool sea_moving
= FALSE
;
1876 extra_type_iterate(pextra
) {
1877 enum unit_move_type eut
= move_type_from_extra(pextra
, puclass
);
1879 if (eut
== UMT_BOTH
) {
1882 } else if (eut
== UMT_LAND
) {
1884 } else if (eut
== UMT_SEA
) {
1887 } extra_type_iterate_end
;
1889 terrain_type_iterate(pterrain
) {
1890 if (is_native_to_class(puclass
, pterrain
, NULL
)) {
1891 if (is_ocean(pterrain
)) {
1897 } terrain_type_iterate_end
;
1899 if (land_moving
&& sea_moving
) {
1900 puclass
->move_type
= UMT_BOTH
;
1901 } else if (sea_moving
) {
1902 puclass
->move_type
= UMT_SEA
;
1904 /* If unit has no native terrains, it is considered land moving */
1905 puclass
->move_type
= UMT_LAND
;
1909 /****************************************************************************
1911 ****************************************************************************/
1912 bool utype_is_cityfounder(struct unit_type
*utype
)
1914 if (game
.scenario
.prevent_new_cities
) {
1915 /* No unit is allowed to found new cities */
1919 return utype_has_flag(utype
, UTYF_CITIES
);