1 /***********************************************************************
2 Freeciv - Copyright (C) 1996-2013 - Freeciv Development Team
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>
18 #include <math.h> /* ceil, floor */
27 #include "fc_interface.h"
35 /* Custom data type for obligatory hard action requirements. */
36 struct obligatory_req
{
37 /* A requirement that contradicts the obligatory hard requirement. */
38 struct requirement contradiction
;
40 /* Is the obligatory hard requirement in the action enabler's target
41 * requirement vector? If FALSE it is in its actor requirement vector. */
44 /* The error message to show when the hard obligatory requirement is
45 * missing. Must be there. */
46 const char *error_msg
;
49 #define SPECVEC_TAG obligatory_req
50 #define SPECVEC_TYPE struct obligatory_req
52 #define obligatory_req_vector_iterate(obreq_vec, pobreq) \
53 TYPED_VECTOR_ITERATE(struct obligatory_req, obreq_vec, pobreq)
54 #define obligatory_req_vector_iterate_end VECTOR_ITERATE_END
56 /* Values used to interpret action probabilities.
58 * Action probabilities are sent over the network. A change in a value here
59 * will therefore change the network protocol.
61 * A change in a value here should also update the action probability
62 * format documentation in fc_types.h */
63 /* The lowest possible probability value (0%) */
64 #define ACTPROB_VAL_MIN 0
65 /* The highest possible probability value (100%) */
66 #define ACTPROB_VAL_MAX 200
67 /* A probability increase of 1% corresponds to this increase. */
68 #define ACTPROB_VAL_1_PCT (ACTPROB_VAL_MAX / 100)
69 /* Action probability doesn't apply when min is this. */
70 #define ACTPROB_VAL_NA 253
71 /* Action probability unsupported when min is this. */
72 #define ACTPROB_VAL_NOT_IMPL 254
74 static struct action
*actions
[MAX_NUM_ACTIONS
];
75 struct action_auto_perf auto_perfs
[MAX_NUM_ACTION_AUTO_PERFORMERS
];
76 static bool actions_initialized
= FALSE
;
78 static struct action_enabler_list
*action_enablers_by_action
[MAX_NUM_ACTIONS
];
80 /* Hard requirements relates to action result. */
81 static struct obligatory_req_vector obligatory_hard_reqs
[ACTION_COUNT
];
83 static struct action
*action_new(enum gen_action id
,
84 enum action_target_kind target_kind
,
85 bool hostile
, bool requires_details
,
87 bool unitwaittime_controlled
,
88 const int min_distance
,
89 const int max_distance
);
91 static bool is_enabler_active(const struct action_enabler
*enabler
,
92 const struct player
*actor_player
,
93 const struct city
*actor_city
,
94 const struct impr_type
*actor_building
,
95 const struct tile
*actor_tile
,
96 const struct unit
*actor_unit
,
97 const struct unit_type
*actor_unittype
,
98 const struct output_type
*actor_output
,
99 const struct specialist
*actor_specialist
,
100 const struct player
*target_player
,
101 const struct city
*target_city
,
102 const struct impr_type
*target_building
,
103 const struct tile
*target_tile
,
104 const struct unit
*target_unit
,
105 const struct unit_type
*target_unittype
,
106 const struct output_type
*target_output
,
107 const struct specialist
*target_specialist
);
110 action_prob_is_signal(const struct act_prob probability
);
112 action_prob_not_relevant(const struct act_prob probability
);
114 action_prob_not_impl(const struct act_prob probability
);
116 /* Make sure that an action distance can target the whole map. */
117 FC_STATIC_ASSERT(MAP_DISTANCE_MAX
<= ACTION_DISTANCE_LAST_NON_SIGNAL
,
118 action_range_can_not_cover_the_whole_map
);
120 /**************************************************************************
121 Register an obligatory hard requirement for the actions it applies to.
123 The vararg parameter is a list of action ids it applies to terminated
125 **************************************************************************/
126 static void oblig_hard_req_register(struct requirement contradiction
,
128 const char *error_message
,
131 struct obligatory_req oreq
;
135 /* A non null action message is used to indicate that an obligatory hard
136 * requirement is missing. */
137 fc_assert_ret(error_message
);
139 /* Pack the obligatory hard requirement. */
140 oreq
.contradiction
= contradiction
;
141 oreq
.is_target
= is_target
;
142 oreq
.error_msg
= error_message
;
144 /* Add the obligatory hard requirement to each action it applies to. */
145 va_start(args
, error_message
);
147 while (ACTION_NONE
!= (act
= va_arg(args
, enum gen_action
))) {
148 /* Any invalid action result should terminate the loop before this
150 fc_assert_ret_msg(gen_action_is_valid(act
), "Invalid action id %d", act
);
152 obligatory_req_vector_append(&obligatory_hard_reqs
[act
], oreq
);
158 /**************************************************************************
159 Hard code the obligatory hard requirements that don't depend on the rest
160 of the ruleset. They are sorted by requirement to make it easy to read,
161 modify and explain them.
162 **************************************************************************/
163 static void hard_code_oblig_hard_reqs(void)
165 /* Why this is a hard requirement: There is currently no point in
166 * allowing the listed actions against domestic targets.
167 * (Possible counter argument: crazy hack involving the Lua
168 * callback action_started_callback() to use an action to do
170 /* TODO: Unhardcode as a part of false flag operation support. */
171 oblig_hard_req_register(req_from_values(VUT_DIPLREL
, REQ_RANGE_LOCAL
,
172 FALSE
, FALSE
, TRUE
, DRO_FOREIGN
),
174 "All action enablers for %s must require a "
176 ACTION_ESTABLISH_EMBASSY
,
177 ACTION_SPY_INVESTIGATE_CITY
,
178 ACTION_SPY_STEAL_GOLD
,
180 ACTION_SPY_STEAL_TECH
,
181 ACTION_SPY_TARGETED_STEAL_TECH
,
182 ACTION_SPY_INCITE_CITY
,
183 ACTION_SPY_BRIBE_UNIT
,
184 ACTION_CAPTURE_UNITS
,
188 /* Why this is a hard requirement: there is a hard requirement that
189 * the actor player is at war with the owner of any city on the
190 * target tile. It can't move to the ruleset as long as Bombard is
191 * targeted at unit stacks only. Having the same requirement
192 * against each unit in the stack as against any city at the tile
193 * ensures compatibility with any future solution that allows the
194 * requirement against any city on the target tile to move to the
196 oblig_hard_req_register(req_from_values(VUT_DIPLREL
, REQ_RANGE_LOCAL
,
197 FALSE
, FALSE
, TRUE
, DS_WAR
),
199 "All action enablers for %s must require a "
200 "target the actor is at war with.",
201 ACTION_BOMBARD
, ACTION_NONE
);
203 /* Why this is a hard requirement: Keep the old rules. Need to work
204 * out corner cases. */
205 oblig_hard_req_register(req_from_values(VUT_DIPLREL
, REQ_RANGE_LOCAL
,
206 FALSE
, TRUE
, TRUE
, DRO_FOREIGN
),
208 "All action enablers for %s must require a "
210 ACTION_UPGRADE_UNIT
, ACTION_NONE
);
212 /* Why this is a hard requirement: Preserve semantics of NoHome
213 * flag. Need to replace other uses in game engine before this can
214 * be demoted to a regular unit flag. */
215 oblig_hard_req_register(req_from_values(VUT_UTFLAG
, REQ_RANGE_LOCAL
,
216 FALSE
, TRUE
, TRUE
, UTYF_NOHOME
),
218 "All action enablers for %s must require that "
219 "the actor doesn't have the NoHome utype flag.",
220 ACTION_HOME_CITY
, ACTION_NONE
);
222 /* Why this is a hard requirement: Preserve semantics of NonMil
223 * flag. Need to replace other uses in game engine before this can
224 * be demoted to a regular unit flag. */
225 oblig_hard_req_register(req_from_values(VUT_UTFLAG
, REQ_RANGE_LOCAL
,
226 FALSE
, TRUE
, TRUE
, UTYF_CIVILIAN
),
228 "All action enablers for %s must require that "
229 "the actor doesn't have the NonMil utype flag.",
230 ACTION_ATTACK
, ACTION_CONQUER_CITY
, ACTION_NONE
);
232 /* Why this is a hard requirement: Preserve semantics of
233 * CanOccupyCity unit class flag. */
234 oblig_hard_req_register(req_from_values(VUT_UCFLAG
, REQ_RANGE_LOCAL
,
236 UCF_CAN_OCCUPY_CITY
),
238 "All action enablers for %s must require that "
239 "the actor has the CanOccupyCity uclass flag.",
240 ACTION_CONQUER_CITY
, ACTION_NONE
);
242 /* Why this is a hard requirement: Consistency with ACTION_ATTACK.
243 * Assumed by other locations in the Freeciv code. Examples:
244 * unit_move_to_tile_test() and unit_conquer_city(). */
245 oblig_hard_req_register(req_from_values(VUT_DIPLREL
, REQ_RANGE_LOCAL
,
246 FALSE
, FALSE
, TRUE
, DS_WAR
),
248 "All action enablers for %s must require that "
249 "the actor is at war with the target.",
250 ACTION_CONQUER_CITY
, ACTION_NONE
);
252 /* Why this is a hard requirement: a unit must move into a city to
254 oblig_hard_req_register(req_from_values(VUT_MINMOVES
, REQ_RANGE_LOCAL
,
255 FALSE
, FALSE
, TRUE
, 1),
257 "All action enablers for %s must require that "
258 "the actor has a movement point left.",
259 ACTION_CONQUER_CITY
, ACTION_NONE
);
261 /* Why this is a hard requirement: this eliminates the need to
262 * check if units transported by the actor unit can exist at the
263 * same tile as all the units in the occupied city.
265 * This makes an implicit rule explicit:
266 * 1. A unit must move into a city to conquer it.
267 * 2. It can't move into the city if the tile contains a non allied
268 * unit (see unit_move_to_tile_test()).
269 * 3. A city could, at the time this rule was made explicit, only
270 * contain units allied to its owner.
271 * 3. A player could, at the time this rule was made explicit, not
272 * be allied to a player that is at war with another ally.
273 * 4. A player could, at the time this rule was made explicit, only
274 * conquer a city belonging to someone he was at war with.
275 * Conclusion: the conquered city had to be empty.
277 oblig_hard_req_register(req_from_values(VUT_MAXTILEUNITS
, REQ_RANGE_LOCAL
,
278 FALSE
, FALSE
, TRUE
, 0),
280 "All action enablers for %s must require that "
281 "the target city is empty.",
282 ACTION_CONQUER_CITY
, ACTION_NONE
);
284 /* Why this is a hard requirement: Assumed in the code. Corner case
285 * where diplomacy prevents a transported unit to go to the target
286 * tile. The paradrop code doesn't check if transported units can
287 * coexist with the target tile city and units. */
288 oblig_hard_req_register(req_from_values(VUT_UNITSTATE
, REQ_RANGE_LOCAL
,
292 "All action enablers for %s must require that "
293 "the actor isn't transporting another unit.",
294 ACTION_PARADROP
, ACTION_AIRLIFT
, ACTION_NONE
);
297 /**************************************************************************
298 Hard code the obligatory hard requirements that needs access to the
299 ruleset before they can be generated.
300 **************************************************************************/
301 static void hard_code_oblig_hard_reqs_ruleset(void)
303 /* Why this is a hard requirement: the "animal can't conquer a city"
304 * rule. Assumed in unit_can_take_over(). */
305 nations_iterate(pnation
) {
306 if (nation_barbarian_type(pnation
) == ANIMAL_BARBARIAN
) {
307 oblig_hard_req_register(req_from_values(VUT_NATION
, REQ_RANGE_PLAYER
,
309 nation_number(pnation
)),
311 "All action enablers for %s must require a "
312 "non animal player actor.",
313 ACTION_CONQUER_CITY
, ACTION_NONE
);
315 } nations_iterate_end
;
318 /**************************************************************************
319 Hard code the actions.
320 **************************************************************************/
321 static void hard_code_actions(void)
323 actions
[ACTION_SPY_POISON
] = action_new(ACTION_SPY_POISON
, ATK_CITY
,
324 TRUE
, FALSE
, FALSE
, TRUE
,
326 actions
[ACTION_SPY_SABOTAGE_UNIT
] =
327 action_new(ACTION_SPY_SABOTAGE_UNIT
, ATK_UNIT
,
328 TRUE
, FALSE
, FALSE
, TRUE
,
330 actions
[ACTION_SPY_BRIBE_UNIT
] =
331 action_new(ACTION_SPY_BRIBE_UNIT
, ATK_UNIT
,
332 TRUE
, FALSE
, FALSE
, TRUE
,
334 actions
[ACTION_SPY_SABOTAGE_CITY
] =
335 action_new(ACTION_SPY_SABOTAGE_CITY
, ATK_CITY
,
336 TRUE
, FALSE
, FALSE
, TRUE
,
338 actions
[ACTION_SPY_TARGETED_SABOTAGE_CITY
] =
339 action_new(ACTION_SPY_TARGETED_SABOTAGE_CITY
, ATK_CITY
,
340 TRUE
, TRUE
, FALSE
, TRUE
,
342 actions
[ACTION_SPY_INCITE_CITY
] =
343 action_new(ACTION_SPY_INCITE_CITY
, ATK_CITY
,
344 TRUE
, FALSE
, FALSE
, TRUE
,
346 actions
[ACTION_ESTABLISH_EMBASSY
] =
347 action_new(ACTION_ESTABLISH_EMBASSY
, ATK_CITY
,
348 FALSE
, FALSE
, FALSE
, TRUE
,
350 actions
[ACTION_SPY_STEAL_TECH
] =
351 action_new(ACTION_SPY_STEAL_TECH
, ATK_CITY
,
352 TRUE
, FALSE
, FALSE
, TRUE
,
354 actions
[ACTION_SPY_TARGETED_STEAL_TECH
] =
355 action_new(ACTION_SPY_TARGETED_STEAL_TECH
, ATK_CITY
,
356 TRUE
, TRUE
, FALSE
, TRUE
,
358 actions
[ACTION_SPY_INVESTIGATE_CITY
] =
359 action_new(ACTION_SPY_INVESTIGATE_CITY
, ATK_CITY
,
360 TRUE
, FALSE
, FALSE
, TRUE
,
362 actions
[ACTION_SPY_STEAL_GOLD
] =
363 action_new(ACTION_SPY_STEAL_GOLD
, ATK_CITY
,
364 TRUE
, FALSE
, FALSE
, TRUE
,
366 actions
[ACTION_TRADE_ROUTE
] =
367 action_new(ACTION_TRADE_ROUTE
, ATK_CITY
,
368 FALSE
, FALSE
, FALSE
, TRUE
,
370 actions
[ACTION_MARKETPLACE
] =
371 action_new(ACTION_MARKETPLACE
, ATK_CITY
,
372 FALSE
, FALSE
, FALSE
, TRUE
,
374 actions
[ACTION_HELP_WONDER
] =
375 action_new(ACTION_HELP_WONDER
, ATK_CITY
,
376 FALSE
, FALSE
, FALSE
, TRUE
,
378 actions
[ACTION_CAPTURE_UNITS
] =
379 action_new(ACTION_CAPTURE_UNITS
, ATK_UNITS
,
380 TRUE
, FALSE
, FALSE
, TRUE
,
381 /* A single domestic unit at the target tile will make the
382 * action illegal. It must therefore be performed from
385 actions
[ACTION_FOUND_CITY
] =
386 action_new(ACTION_FOUND_CITY
, ATK_TILE
,
387 FALSE
, FALSE
, TRUE
, TRUE
,
388 /* Illegal to perform to a target on another tile.
389 * Reason: The Freeciv code assumes that the city founding
390 * unit is located at the tile were the new city is
393 actions
[ACTION_JOIN_CITY
] =
394 action_new(ACTION_JOIN_CITY
, ATK_CITY
,
395 FALSE
, FALSE
, TRUE
, TRUE
,
397 actions
[ACTION_STEAL_MAPS
] =
398 action_new(ACTION_STEAL_MAPS
, ATK_CITY
,
399 TRUE
, FALSE
, FALSE
, TRUE
,
401 actions
[ACTION_BOMBARD
] =
402 action_new(ACTION_BOMBARD
,
403 /* FIXME: Target is actually Units + City */
405 TRUE
, FALSE
, FALSE
, TRUE
,
406 /* A single domestic unit at the target tile will make the
407 * action illegal. It must therefore be performed from
410 /* Overwritten by the ruleset's bombard_max_range */
412 actions
[ACTION_SPY_NUKE
] =
413 action_new(ACTION_SPY_NUKE
, ATK_CITY
,
414 TRUE
, FALSE
, FALSE
, TRUE
,
416 actions
[ACTION_NUKE
] =
417 action_new(ACTION_NUKE
,
418 /* FIXME: Target is actually Tile + Units + City */
420 TRUE
, FALSE
, TRUE
, TRUE
,
422 actions
[ACTION_DESTROY_CITY
] =
423 action_new(ACTION_DESTROY_CITY
, ATK_CITY
,
424 TRUE
, FALSE
, TRUE
, TRUE
,
426 actions
[ACTION_EXPEL_UNIT
] =
427 action_new(ACTION_EXPEL_UNIT
, ATK_UNIT
,
428 TRUE
, FALSE
, FALSE
, TRUE
,
430 actions
[ACTION_RECYCLE_UNIT
] =
431 action_new(ACTION_RECYCLE_UNIT
, ATK_CITY
,
432 FALSE
, FALSE
, TRUE
, TRUE
,
433 /* Illegal to perform to a target on another tile to
434 * keep the rules exactly as they were for now. */
436 actions
[ACTION_DISBAND_UNIT
] =
437 action_new(ACTION_DISBAND_UNIT
, ATK_SELF
,
438 FALSE
, FALSE
, TRUE
, TRUE
,
440 actions
[ACTION_HOME_CITY
] =
441 action_new(ACTION_HOME_CITY
, ATK_CITY
,
442 FALSE
, FALSE
, TRUE
, FALSE
,
443 /* Illegal to perform to a target on another tile to
444 * keep the rules exactly as they were for now. */
446 actions
[ACTION_UPGRADE_UNIT
] =
447 action_new(ACTION_UPGRADE_UNIT
, ATK_CITY
,
448 FALSE
, FALSE
, TRUE
, TRUE
,
449 /* Illegal to perform to a target on another tile to
450 * keep the rules exactly as they were for now. */
452 actions
[ACTION_PARADROP
] =
453 action_new(ACTION_PARADROP
, ATK_TILE
,
454 FALSE
, FALSE
, TRUE
, TRUE
,
456 /* Still limited by each unit type's paratroopers_range
458 ACTION_DISTANCE_MAX
);
459 actions
[ACTION_AIRLIFT
] =
460 action_new(ACTION_AIRLIFT
, ATK_CITY
,
461 FALSE
, FALSE
, TRUE
, TRUE
,
462 1, ACTION_DISTANCE_UNLIMITED
);
463 actions
[ACTION_ATTACK
] =
464 action_new(ACTION_ATTACK
,
465 /* FIXME: Target is actually City and, depending on the
466 * unreachable_protects setting, each unit at the target
467 * tile (Units) or any unit at the target tile. */
469 TRUE
, FALSE
, FALSE
, TRUE
,
471 actions
[ACTION_CONQUER_CITY
] =
472 action_new(ACTION_CONQUER_CITY
, ATK_CITY
,
473 TRUE
, FALSE
, FALSE
, TRUE
,
475 actions
[ACTION_HEAL_UNIT
] =
476 action_new(ACTION_HEAL_UNIT
, ATK_UNIT
,
477 FALSE
, FALSE
, FALSE
, TRUE
,
481 /**************************************************************************
482 Initialize the actions and the action enablers.
483 **************************************************************************/
484 void actions_init(void)
488 /* Hard code the actions */
491 /* Initialize the action enabler list */
492 action_iterate(act
) {
493 action_enablers_by_action
[act
] = action_enabler_list_new();
494 } action_iterate_end
;
496 /* Initialize action obligatory hard requirements. */
498 /* Obligatory hard requirements are sorted by action in memory. This makes
499 * it easy to access the data. */
500 action_iterate(act
) {
501 /* Prepare each action's storage area. */
502 obligatory_req_vector_init(&obligatory_hard_reqs
[act
]);
503 } action_iterate_end
;
505 /* Obligatory hard requirements are sorted by requirement in the source
506 * code. This makes it easy to read, modify and explain it. */
507 hard_code_oblig_hard_reqs();
509 /* Initialize the action auto performers. */
510 for (i
= 0; i
< MAX_NUM_ACTION_AUTO_PERFORMERS
; i
++) {
511 /* Nothing here. Nothing after this point. */
512 auto_perfs
[i
].cause
= AAPC_COUNT
;
514 /* The criteria to pick *this* auto performer for its cause. */
515 requirement_vector_init(&auto_perfs
[i
].reqs
);
517 for (j
= 0; j
< MAX_NUM_ACTIONS
; j
++) {
518 /* Nothing here. Nothing after this point. */
519 auto_perfs
[i
].alternatives
[j
] = ACTION_NONE
;
523 /* The actions them self are now initialized. */
524 actions_initialized
= TRUE
;
527 /**************************************************************************
528 Generate action related data based on the currently loaded ruleset. Done
529 before ruleset sanity checking and ruleset compatibility post
531 **************************************************************************/
532 void actions_rs_pre_san_gen(void)
534 /* Some obligatory hard requirements needs access to the rest of the
536 hard_code_oblig_hard_reqs_ruleset();
539 /**************************************************************************
540 Free the actions and the action enablers.
541 **************************************************************************/
542 void actions_free(void)
546 /* Don't consider the actions to be initialized any longer. */
547 actions_initialized
= FALSE
;
549 action_iterate(act
) {
550 action_enabler_list_iterate(action_enablers_by_action
[act
], enabler
) {
551 requirement_vector_free(&enabler
->actor_reqs
);
552 requirement_vector_free(&enabler
->target_reqs
);
554 } action_enabler_list_iterate_end
;
556 action_enabler_list_destroy(action_enablers_by_action
[act
]);
558 FC_FREE(actions
[act
]);
559 } action_iterate_end
;
561 /* Free the obligatory hard action requirements. */
562 action_iterate(act
) {
563 obligatory_req_vector_free(&obligatory_hard_reqs
[act
]);
564 } action_iterate_end
;
566 /* Free the action auto performers. */
567 for (i
= 0; i
< MAX_NUM_ACTION_AUTO_PERFORMERS
; i
++) {
568 requirement_vector_free(&auto_perfs
[i
].reqs
);
572 /**************************************************************************
573 Returns TRUE iff the actions are initialized.
575 Doesn't care about action enablers.
576 **************************************************************************/
577 bool actions_are_ready(void)
579 if (!actions_initialized
) {
580 /* The actions them self aren't initialized yet. */
584 action_iterate(act
) {
585 if (actions
[act
]->ui_name
[0] == '\0') {
586 /* An action without a UI name exists means that the ruleset haven't
587 * loaded yet. The ruleset loading will assign a default name to
588 * any actions not named by the ruleset. The client will get this
589 * name from the server. */
592 } action_iterate_end
;
594 /* The actions should be ready for use. */
598 /**************************************************************************
600 **************************************************************************/
601 static struct action
*action_new(enum gen_action id
,
602 enum action_target_kind target_kind
,
603 bool hostile
, bool requires_details
,
605 bool unitwaittime_controlled
,
606 const int min_distance
,
607 const int max_distance
)
609 struct action
*action
;
611 action
= fc_malloc(sizeof(*action
));
614 action
->actor_kind
= AAK_UNIT
;
615 action
->target_kind
= target_kind
;
617 action
->hostile
= hostile
;
618 action
->requires_details
= requires_details
;
619 action
->rare_pop_up
= rare_pop_up
;
621 /* The distance between the actor and itself is always 0. */
622 fc_assert(target_kind
!= ATK_SELF
623 || (min_distance
== 0 && max_distance
== 0));
625 action
->min_distance
= min_distance
;
626 action
->max_distance
= max_distance
;
628 action
->unitwaittime_controlled
= unitwaittime_controlled
;
630 /* Loaded from the ruleset. Until generalized actions are ready it has to
631 * be defined seperatly from other action data. */
632 action
->ui_name
[0] = '\0';
633 action
->quiet
= FALSE
;
634 BV_CLR_ALL(action
->blocked_by
);
639 /**************************************************************************
640 Returns TRUE iff the specified action ID refers to a valid action.
641 **************************************************************************/
642 bool action_id_exists(const int action_id
)
644 /* Actions are still hard coded. */
645 return gen_action_is_valid(action_id
) && actions
[action_id
];
648 /**************************************************************************
649 Return the action with the given id.
651 Returns NULL if no action with the given id exists.
652 **************************************************************************/
653 struct action
*action_by_number(int action_id
)
655 if (!action_id_exists(action_id
)) {
656 /* Nothing to return. */
658 log_verbose("Asked for non existing action numbered %d", action_id
);
663 fc_assert_msg(actions
[action_id
], "Action %d don't exist.", action_id
);
665 return actions
[action_id
];
668 /**************************************************************************
669 Return the action with the given name.
671 Returns NULL if no action with the given name exists.
672 **************************************************************************/
673 struct action
*action_by_rule_name(const char *name
)
675 /* Actions are still hard coded in the gen_action enum. */
676 int action_id
= gen_action_by_name(name
, fc_strcasecmp
);
678 if (!action_id_exists(action_id
)) {
679 /* Nothing to return. */
681 log_verbose("Asked for non existing action named %s", name
);
686 return action_by_number(action_id
);
689 /**************************************************************************
690 Get the actor kind of an action.
691 **************************************************************************/
692 enum action_actor_kind
action_get_actor_kind(const struct action
*paction
)
694 fc_assert_ret_val_msg(paction
, AAK_COUNT
, "Action doesn't exist.");
696 return paction
->actor_kind
;
699 /**************************************************************************
700 Get the target kind of an action.
701 **************************************************************************/
702 enum action_target_kind
action_get_target_kind(
703 const struct action
*paction
)
705 fc_assert_ret_val_msg(paction
, ATK_COUNT
, "Action doesn't exist.");
707 return paction
->target_kind
;
710 /**************************************************************************
711 Returns TRUE iff performing the specified action has the specified
713 **************************************************************************/
714 bool action_has_result(struct action
*paction
, enum gen_action result
)
716 /* The action result is currently used as the action id. */
717 return paction
->id
== result
;
720 /**************************************************************************
721 Returns TRUE iff the specified action is hostile.
722 **************************************************************************/
723 bool action_is_hostile(int action_id
)
725 fc_assert_msg(actions
[action_id
], "Action %d don't exist.", action_id
);
727 return actions
[action_id
]->hostile
;
730 /**************************************************************************
731 Returns TRUE iff the specified action REQUIRES the player to provide
732 details in addition to actor and target. Returns FALSE if the action
733 doesn't support any additional details or if they can be set by Freeciv
735 **************************************************************************/
736 bool action_requires_details(int action_id
)
738 fc_assert_msg(actions
[action_id
], "Action %d don't exist.", action_id
);
740 return actions
[action_id
]->requires_details
;
743 /**************************************************************************
744 Returns TRUE iff a unit's ability to perform this action will pop up the
745 action selection dialog before the player asks for it only in exceptional
748 An example of an exceptional case is when the player tries to move a
749 unit to a tile it can't move to but can perform this action to.
750 **************************************************************************/
751 bool action_id_is_rare_pop_up(int action_id
)
753 fc_assert_ret_val_msg((action_id_exists(action_id
)),
754 FALSE
, "Action %d don't exist.", action_id
);
756 return actions
[action_id
]->rare_pop_up
;
759 /**************************************************************************
760 Returns TRUE iff the specified distance between actor and target is
761 sm,aller or equal to the max range accepted by the specified action.
762 **************************************************************************/
763 bool action_distance_inside_max(const struct action
*action
,
766 return (distance
<= action
->max_distance
767 || action
->max_distance
== ACTION_DISTANCE_UNLIMITED
);
770 /**************************************************************************
771 Returns TRUE iff the specified distance between actor and target is
772 within the range acceptable to the specified action.
773 **************************************************************************/
774 bool action_distance_accepted(const struct action
*action
,
777 fc_assert_ret_val(action
, FALSE
);
779 return (distance
>= action
->min_distance
780 && action_distance_inside_max(action
, distance
));
783 /**************************************************************************
784 Returns TRUE iff blocked will be illegal if blocker is legal.
785 **************************************************************************/
786 bool action_would_be_blocked_by(const struct action
*blocked
,
787 const struct action
*blocker
)
789 fc_assert_ret_val(blocked
, FALSE
);
790 fc_assert_ret_val(blocker
, FALSE
);
792 return BV_ISSET(blocked
->blocked_by
, action_number(blocker
));
795 /**************************************************************************
796 Get the universal number of the action.
797 **************************************************************************/
798 int action_number(const struct action
*action
)
803 /**************************************************************************
804 Get the rule name of the action.
805 **************************************************************************/
806 const char *action_rule_name(const struct action
*action
)
808 /* Rule name is still hard coded. */
809 return action_id_rule_name(action
->id
);
812 /**************************************************************************
813 Get the action name used when displaying the action in the UI. Nothing
814 is added to the UI name.
815 **************************************************************************/
816 const char *action_name_translation(const struct action
*action
)
818 /* Use action_id_name_translation() to format the UI name. */
819 return action_id_name_translation(action
->id
);
822 /**************************************************************************
823 Get the rule name of the action.
824 **************************************************************************/
825 const char *action_id_rule_name(int action_id
)
827 fc_assert_msg(actions
[action_id
], "Action %d don't exist.", action_id
);
829 return gen_action_name(action_id
);
832 /**************************************************************************
833 Get the action name used when displaying the action in the UI. Nothing
834 is added to the UI name.
835 **************************************************************************/
836 const char *action_id_name_translation(int action_id
)
838 return action_prepare_ui_name(action_id
, "", ACTPROB_NA
, NULL
);
841 /**************************************************************************
842 Get the action name with a mnemonic ready to display in the UI.
843 **************************************************************************/
844 const char *action_get_ui_name_mnemonic(int action_id
,
845 const char* mnemonic
)
847 return action_prepare_ui_name(action_id
, mnemonic
, ACTPROB_NA
, NULL
);
850 /**************************************************************************
851 Get the UI name ready to show the action in the UI. It is possible to
852 add a client specific mnemonic. Success probability information is
853 interpreted and added to the text. A custom text can be inserted before
854 the probability information.
855 **************************************************************************/
856 const char *action_prepare_ui_name(int action_id
, const char* mnemonic
,
857 const struct act_prob prob
,
860 static struct astring str
= ASTRING_INIT
;
861 static struct astring chance
= ASTRING_INIT
;
863 /* Text representation of the probability. */
866 if (!actions_are_ready()) {
867 /* Could be a client who haven't gotten the ruleset yet */
869 /* so there shouldn't be any action probability to show */
870 fc_assert(action_prob_not_relevant(prob
));
872 /* but the action should be valid */
873 fc_assert_ret_val_msg(action_id_exists(action_id
),
875 "Invalid action %d", action_id
);
877 /* and no custom text will be inserted */
878 fc_assert(custom
== NULL
|| custom
[0] == '\0');
880 /* Make the best of what is known */
881 astr_set(&str
, _("%s%s (name may be wrong)"),
882 mnemonic
, gen_action_name(action_id
));
884 /* Return the guess. */
885 return astr_str(&str
);
888 /* How to interpret action probabilities like prob is documented in
890 if (action_prob_is_signal(prob
)) {
891 fc_assert(action_prob_not_impl(prob
)
892 || action_prob_not_relevant(prob
));
894 /* Unknown because of missing server support or should not exits. */
897 if (prob
.min
== prob
.max
) {
898 /* Only one probability in range. */
900 /* TRANS: the probability that an action will succeed. Given in
901 * percentage. Resolution is 0.5%. */
902 astr_set(&chance
, _("%.1f%%"), (double)prob
.max
/ ACTPROB_VAL_1_PCT
);
904 /* TRANS: the interval (end points included) where the probability of
905 * the action's success is. Given in percentage. Resolution is 0.5%. */
906 astr_set(&chance
, _("[%.1f%%, %.1f%%]"),
907 (double)prob
.min
/ ACTPROB_VAL_1_PCT
,
908 (double)prob
.max
/ ACTPROB_VAL_1_PCT
);
910 probtxt
= astr_str(&chance
);
913 /* Format the info part of the action's UI name. */
914 if (probtxt
!= NULL
&& custom
!= NULL
) {
915 /* TRANS: action UI name's info part with custom info and probability.
916 * Hint: you can move the paren handling from this sting to the action
917 * names if you need to add extra information (like a mnemonic letter
918 * that doesn't appear in the action UI name) to it. In that case you
919 * must do so for all strings with this comment and for every action
920 * name. To avoid a `()` when no UI name info part is added you have
921 * to add the extra information to every action name or remove the
922 * surrounding parens. */
923 astr_set(&chance
, _(" (%s; %s)"), custom
, probtxt
);
924 } else if (probtxt
!= NULL
) {
925 /* TRANS: action UI name's info part with probability.
926 * Hint: you can move the paren handling from this sting to the action
927 * names if you need to add extra information (like a mnemonic letter
928 * that doesn't appear in the action UI name) to it. In that case you
929 * must do so for all strings with this comment and for every action
930 * name. To avoid a `()` when no UI name info part is added you have
931 * to add the extra information to every action name or remove the
932 * surrounding parens. */
933 astr_set(&chance
, _(" (%s)"), probtxt
);
934 } else if (custom
!= NULL
) {
935 /* TRANS: action UI name's info part with custom info.
936 * Hint: you can move the paren handling from this sting to the action
937 * names if you need to add extra information (like a mnemonic letter
938 * that doesn't appear in the action UI name) to it. In that case you
939 * must do so for all strings with this comment and for every action
940 * name. To avoid a `()` when no UI name info part is added you have
941 * to add the extra information to every action name or remove the
942 * surrounding parens. */
943 astr_set(&chance
, _(" (%s)"), custom
);
945 /* No info part to display. */
949 fc_assert_msg(actions
[action_id
], "Action %d don't exist.", action_id
);
951 astr_set(&str
, _(actions
[action_id
]->ui_name
), mnemonic
,
954 return astr_str(&str
);
957 /**************************************************************************
958 Get information about starting the action in the current situation.
959 Suitable for a tool tip for the button that starts it.
960 **************************************************************************/
961 const char *action_get_tool_tip(const int action_id
,
962 const struct act_prob prob
)
964 static struct astring tool_tip
= ASTRING_INIT
;
966 if (action_prob_is_signal(prob
)) {
967 fc_assert(action_prob_not_impl(prob
));
969 /* Missing server support. No in game action will change this. */
970 astr_clear(&tool_tip
);
971 } else if (prob
.min
== prob
.max
) {
972 /* TRANS: action probability of success. Given in percentage.
973 * Resolution is 0.5%. */
974 astr_set(&tool_tip
, _("The probability of success is %.1f%%."),
975 (double)prob
.max
/ ACTPROB_VAL_1_PCT
);
978 /* TRANS: action probability range (min to max). Given in
979 * percentage. Resolution is 0.5%. */
980 _("The probability of success is %.1f%%, %.1f%% or somewhere"
982 (double)prob
.min
/ ACTPROB_VAL_1_PCT
,
983 (double)prob
.max
/ ACTPROB_VAL_1_PCT
);
986 return astr_str(&tool_tip
);
989 /**************************************************************************
990 Get the unit type role corresponding to the ability to do the specified
992 **************************************************************************/
993 int action_get_role(const struct action
*paction
)
995 fc_assert_msg(AAK_UNIT
== action_get_actor_kind(paction
),
996 "Action %s isn't performed by a unit",
997 action_rule_name(paction
));
999 return paction
->id
+ L_LAST
;
1002 /**************************************************************************
1003 Create a new action enabler.
1004 **************************************************************************/
1005 struct action_enabler
*action_enabler_new(void)
1007 struct action_enabler
*enabler
;
1009 enabler
= fc_malloc(sizeof(*enabler
));
1010 enabler
->disabled
= FALSE
;
1011 requirement_vector_init(&enabler
->actor_reqs
);
1012 requirement_vector_init(&enabler
->target_reqs
);
1014 /* Make sure that action doesn't end up as a random value that happens to
1015 * be a valid action id. */
1016 enabler
->action
= ACTION_NONE
;
1021 /**************************************************************************
1022 Create a new copy of an existing action enabler.
1023 **************************************************************************/
1024 struct action_enabler
*
1025 action_enabler_copy(const struct action_enabler
*original
)
1027 struct action_enabler
*enabler
= action_enabler_new();
1029 enabler
->action
= original
->action
;
1031 requirement_vector_copy(&enabler
->actor_reqs
, &original
->actor_reqs
);
1032 requirement_vector_copy(&enabler
->target_reqs
, &original
->target_reqs
);
1037 /**************************************************************************
1038 Add an action enabler to the current ruleset.
1039 **************************************************************************/
1040 void action_enabler_add(struct action_enabler
*enabler
)
1042 /* Sanity check: a non existing action enabler can't be added. */
1043 fc_assert_ret(enabler
);
1044 /* Sanity check: a non existing action doesn't have enablers. */
1045 fc_assert_ret(action_id_exists(enabler
->action
));
1047 action_enabler_list_append(
1048 action_enablers_for_action(enabler
->action
),
1052 /**************************************************************************
1053 Remove an action enabler from the current ruleset.
1055 Returns TRUE on success.
1056 **************************************************************************/
1057 bool action_enabler_remove(struct action_enabler
*enabler
)
1059 /* Sanity check: a non existing action enabler can't be removed. */
1060 fc_assert_ret_val(enabler
, FALSE
);
1061 /* Sanity check: a non existing action doesn't have enablers. */
1062 fc_assert_ret_val(action_id_exists(enabler
->action
), FALSE
);
1064 return action_enabler_list_remove(
1065 action_enablers_for_action(enabler
->action
),
1069 /**************************************************************************
1070 Get all enablers for an action in the current ruleset.
1071 **************************************************************************/
1072 struct action_enabler_list
*
1073 action_enablers_for_action(enum gen_action action
)
1075 /* Sanity check: a non existing action doesn't have enablers. */
1076 fc_assert_ret_val(action_id_exists(action
), NULL
);
1078 return action_enablers_by_action
[action
];
1081 /**************************************************************************
1082 Returns an error message text if the action enabler is missing at least
1083 one of its action's obligatory hard requirement. Returns NULL if all
1084 obligatory hard requirements are there.
1086 An action may force its enablers to include one or more of its hard
1087 requirements. (See the section "Actions and their hard requirements" of
1090 This doesn't include those of the action's hard requirements that can't
1091 be expressed as a requirement vector or hard requirements that the
1092 action doesn't force enablers to include.
1093 **************************************************************************/
1095 action_enabler_obligatory_reqs_missing(struct action_enabler
*enabler
)
1097 /* Sanity check: a non existing action enabler is missing but it doesn't
1098 * miss any obligatory hard requirements. */
1099 fc_assert_ret_val(enabler
, NULL
);
1101 /* Sanity check: a non existing action doesn't have any obligatory hard
1103 fc_assert_ret_val(action_id_exists(enabler
->action
), NULL
);
1105 obligatory_req_vector_iterate(&obligatory_hard_reqs
[enabler
->action
],
1107 struct requirement_vector
*ae_vec
;
1109 /* Select action enabler requirement vector. */
1110 ae_vec
= (obreq
->is_target
? &enabler
->target_reqs
:
1111 &enabler
->actor_reqs
);
1113 if (!does_req_contradicts_reqs(&obreq
->contradiction
, ae_vec
)) {
1114 /* Sanity check: doesn't return NULL when a problem is detected. */
1115 fc_assert_ret_val(obreq
->error_msg
,
1116 "Missing obligatory hard requirement for %s.");
1118 return obreq
->error_msg
;
1120 } obligatory_req_vector_iterate_end
;
1122 /* No missing obligatory hard requirements. */
1126 /**************************************************************************
1127 Inserts any missing obligatory hard requirements in the action enabler
1128 based on its action.
1130 See action_enabler_obligatory_reqs_missing()
1131 **************************************************************************/
1132 void action_enabler_obligatory_reqs_add(struct action_enabler
*enabler
)
1134 /* Sanity check: a non existing action enabler is missing but it doesn't
1135 * miss any obligatory hard requirements. */
1136 fc_assert_ret(enabler
);
1138 /* Sanity check: a non existing action doesn't have any obligatory hard
1140 fc_assert_ret(action_id_exists(enabler
->action
));
1142 obligatory_req_vector_iterate(&obligatory_hard_reqs
[enabler
->action
],
1144 struct requirement_vector
*ae_vec
;
1146 /* Select action enabler requirement vector. */
1147 ae_vec
= (obreq
->is_target
? &enabler
->target_reqs
:
1148 &enabler
->actor_reqs
);
1150 if (!does_req_contradicts_reqs(&obreq
->contradiction
, ae_vec
)) {
1151 struct requirement missing
;
1153 /* Change the requirement from what should conflict to what is
1155 missing
.present
= !obreq
->contradiction
.present
;
1156 missing
.source
= obreq
->contradiction
.source
;
1157 missing
.range
= obreq
->contradiction
.range
;
1158 missing
.survives
= obreq
->contradiction
.survives
;
1159 missing
.quiet
= obreq
->contradiction
.quiet
;
1161 /* Insert the missing requirement. */
1162 requirement_vector_append(ae_vec
, missing
);
1164 } obligatory_req_vector_iterate_end
;
1166 /* Remove anything that conflicts with the newly added reqs. */
1167 requirement_vector_contradiction_clean(&enabler
->actor_reqs
);
1168 requirement_vector_contradiction_clean(&enabler
->target_reqs
);
1170 /* Sanity check: obligatory requirement insertion should have fixed the
1171 * action enabler. */
1172 fc_assert(action_enabler_obligatory_reqs_missing(enabler
) == NULL
);
1175 /**************************************************************************
1176 Returns TRUE iff the specified player knows (has seen) the specified
1178 **************************************************************************/
1179 static bool plr_knows_tile(const struct player
*plr
,
1180 const struct tile
*ttile
)
1182 return plr
&& ttile
&& (tile_get_known(ttile
, plr
) != TILE_UNKNOWN
);
1185 /**************************************************************************
1186 Returns TRUE iff the specified player can see the specified tile.
1187 **************************************************************************/
1188 static bool plr_sees_tile(const struct player
*plr
,
1189 const struct tile
*ttile
)
1191 return plr
&& ttile
&& (tile_get_known(ttile
, plr
) == TILE_KNOWN_SEEN
);
1194 /**************************************************************************
1195 Returns the local building type of a city target.
1197 target_city can't be NULL
1198 **************************************************************************/
1199 static struct impr_type
*
1200 tgt_city_local_building(const struct city
*target_city
)
1202 /* Only used with city targets */
1203 fc_assert_ret_val(target_city
, NULL
);
1205 if (target_city
->production
.kind
== VUT_IMPROVEMENT
) {
1206 /* The local building is what the target city currently is building. */
1207 return target_city
->production
.value
.building
;
1209 /* In the current semantic the local building is always the building
1211 /* TODO: Consider making the local building the target building for
1212 * actions that allows specifying a building target. */
1217 /**************************************************************************
1218 Returns the local unit type of a city target.
1220 target_city can't be NULL
1221 **************************************************************************/
1222 static struct unit_type
*
1223 tgt_city_local_utype(const struct city
*target_city
)
1225 /* Only used with city targets */
1226 fc_assert_ret_val(target_city
, NULL
);
1228 if (target_city
->production
.kind
== VUT_UTYPE
) {
1229 /* The local unit type is what the target city currently is
1231 return target_city
->production
.value
.utype
;
1233 /* In the current semantic the local utype is always the type of the
1234 * unit being built. */
1239 /**************************************************************************
1240 Returns the target tile for actions that may block the specified action.
1241 This is needed because some actions can be blocked by an action with a
1242 different target kind. The target tile could therefore be missing.
1244 Example: The ATK_SELF action ACTION_DISBAND_UNIT can be blocked by the
1245 ATK_CITY action ACTION_RECYCLE_UNIT.
1246 **************************************************************************/
1247 static const struct tile
*
1248 blocked_find_target_tile(const int action_id
,
1249 const struct unit
*actor_unit
,
1250 const struct tile
*target_tile_arg
,
1251 const struct city
*target_city
,
1252 const struct unit
*target_unit
)
1254 if (target_tile_arg
!= NULL
) {
1255 /* Trust the caller. */
1256 return target_tile_arg
;
1259 switch (action_id_get_target_kind(action_id
)) {
1261 fc_assert_ret_val(target_city
, NULL
);
1262 return city_tile(target_city
);
1264 fc_assert_ret_val(target_unit
, NULL
);
1265 return unit_tile(target_unit
);
1267 fc_assert_ret_val(target_unit
|| target_tile_arg
, NULL
);
1269 return unit_tile(target_unit
);
1273 fc_assert_ret_val(target_tile_arg
, NULL
);
1274 return target_tile_arg
;
1276 fc_assert_ret_val(actor_unit
, NULL
);
1277 return unit_tile(actor_unit
);
1279 /* Handled below. */
1283 fc_assert_msg(FALSE
, "Bad action target kind %d for action %d",
1284 action_id_get_target_kind(action_id
), action_id
);
1288 /**************************************************************************
1289 Returns the target city for actions that may block the specified action.
1290 This is needed because some actions can be blocked by an action with a
1291 different target kind. The target city argument could therefore be
1294 Example: The ATK_SELF action ACTION_DISBAND_UNIT can be blocked by the
1295 ATK_CITY action ACTION_RECYCLE_UNIT.
1296 **************************************************************************/
1297 static const struct city
*
1298 blocked_find_target_city(const int action_id
,
1299 const struct unit
*actor_unit
,
1300 const struct tile
*target_tile
,
1301 const struct city
*target_city_arg
,
1302 const struct unit
*target_unit
)
1304 if (target_city_arg
!= NULL
) {
1305 /* Trust the caller. */
1306 return target_city_arg
;
1309 switch (action_id_get_target_kind(action_id
)) {
1311 fc_assert_ret_val(target_city_arg
, NULL
);
1312 return target_city_arg
;
1314 fc_assert_ret_val(target_unit
, NULL
);
1315 fc_assert_ret_val(unit_tile(target_unit
), NULL
);
1316 return tile_city(unit_tile(target_unit
));
1318 fc_assert_ret_val(target_unit
|| target_tile
, NULL
);
1320 fc_assert_ret_val(unit_tile(target_unit
), NULL
);
1321 return tile_city(unit_tile(target_unit
));
1325 fc_assert_ret_val(target_tile
, NULL
);
1326 return tile_city(target_tile
);
1328 fc_assert_ret_val(actor_unit
, NULL
);
1329 fc_assert_ret_val(unit_tile(actor_unit
), NULL
);
1330 return tile_city(unit_tile(actor_unit
));
1332 /* Handled below. */
1336 fc_assert_msg(FALSE
, "Bad action target kind %d for action %d",
1337 action_id_get_target_kind(action_id
), action_id
);
1341 /**************************************************************************
1342 Returns the action that blocks the specified action or NULL if the
1343 specified action isn't blocked.
1345 An action that can block another blocks when it is forced and possible.
1346 **************************************************************************/
1347 struct action
*action_is_blocked_by(const int action_id
,
1348 const struct unit
*actor_unit
,
1349 const struct tile
*target_tile_arg
,
1350 const struct city
*target_city_arg
,
1351 const struct unit
*target_unit
)
1355 const struct tile
*target_tile
1356 = blocked_find_target_tile(action_id
, actor_unit
, target_tile_arg
,
1357 target_city_arg
, target_unit
);
1358 const struct city
*target_city
1359 = blocked_find_target_city(action_id
, actor_unit
, target_tile
,
1360 target_city_arg
, target_unit
);
1362 action_iterate(blocker_id
) {
1363 fc_assert_action(action_id_get_actor_kind(blocker_id
) == AAK_UNIT
,
1366 if (!action_id_would_be_blocked_by(action_id
, blocker_id
)) {
1367 /* It doesn't matter if it is legal. It won't block the action. */
1371 switch (action_id_get_target_kind(blocker_id
)) {
1374 /* Can't be enabled. No target. */
1377 if (is_action_enabled_unit_on_city(blocker_id
,
1378 actor_unit
, target_city
)) {
1379 return action_by_number(blocker_id
);
1384 /* Can't be enabled. No target. */
1387 if (is_action_enabled_unit_on_unit(blocker_id
,
1388 actor_unit
, target_unit
)) {
1389 return action_by_number(blocker_id
);
1394 /* Can't be enabled. No target. */
1397 if (is_action_enabled_unit_on_units(blocker_id
,
1398 actor_unit
, target_tile
)) {
1399 return action_by_number(blocker_id
);
1404 /* Can't be enabled. No target. */
1407 if (is_action_enabled_unit_on_tile(blocker_id
,
1408 actor_unit
, target_tile
)) {
1409 return action_by_number(blocker_id
);
1413 if (is_action_enabled_unit_on_self(blocker_id
, actor_unit
)) {
1414 return action_by_number(blocker_id
);
1418 fc_assert_action(action_id_get_target_kind(blocker_id
) != ATK_COUNT
,
1422 } action_iterate_end
;
1428 /**************************************************************************
1429 Returns TRUE if the specified unit type can perform the wanted action
1430 given that an action enabler later will enable it.
1432 This is done by checking the action's hard requirements. Hard
1433 requirements must be TRUE before an action can be done. The reason why
1434 is usually that code dealing with the action assumes that the
1435 requirements are true. A requirement may also end up here if it can't be
1436 expressed in a requirement vector or if its absence makes the action
1439 When adding a new hard requirement here:
1440 * explain why it is a hard requirement in a comment.
1441 **************************************************************************/
1443 action_actor_utype_hard_reqs_ok(const enum gen_action wanted_action
,
1444 const struct unit_type
*actor_unittype
)
1446 switch (wanted_action
) {
1447 case ACTION_JOIN_CITY
:
1448 if (utype_pop_value(actor_unittype
) <= 0) {
1449 /* Reason: Must have population to add. */
1454 case ACTION_BOMBARD
:
1455 if (actor_unittype
->bombard_rate
<= 0) {
1456 /* Reason: Can't bombard if it never fires. */
1460 if (actor_unittype
->attack_strength
<= 0) {
1461 /* Reason: Can't bombard without attack strength. */
1467 case ACTION_UPGRADE_UNIT
:
1468 if (actor_unittype
->obsoleted_by
== U_NOT_OBSOLETED
) {
1469 /* Reason: Nothing to upgrade to. */
1475 if (actor_unittype
->attack_strength
<= 0) {
1476 /* Reason: Can't attack without strength. */
1481 case ACTION_ESTABLISH_EMBASSY
:
1482 case ACTION_SPY_INVESTIGATE_CITY
:
1483 case ACTION_SPY_POISON
:
1484 case ACTION_SPY_STEAL_GOLD
:
1485 case ACTION_SPY_SABOTAGE_CITY
:
1486 case ACTION_SPY_TARGETED_SABOTAGE_CITY
:
1487 case ACTION_SPY_STEAL_TECH
:
1488 case ACTION_SPY_TARGETED_STEAL_TECH
:
1489 case ACTION_SPY_INCITE_CITY
:
1490 case ACTION_TRADE_ROUTE
:
1491 case ACTION_MARKETPLACE
:
1492 case ACTION_HELP_WONDER
:
1493 case ACTION_SPY_BRIBE_UNIT
:
1494 case ACTION_SPY_SABOTAGE_UNIT
:
1495 case ACTION_CAPTURE_UNITS
:
1496 case ACTION_FOUND_CITY
:
1497 case ACTION_STEAL_MAPS
:
1498 case ACTION_SPY_NUKE
:
1500 case ACTION_DESTROY_CITY
:
1501 case ACTION_EXPEL_UNIT
:
1502 case ACTION_RECYCLE_UNIT
:
1503 case ACTION_DISBAND_UNIT
:
1504 case ACTION_HOME_CITY
:
1505 case ACTION_PARADROP
:
1506 case ACTION_AIRLIFT
:
1507 case ACTION_CONQUER_CITY
:
1508 case ACTION_HEAL_UNIT
:
1509 /* No hard unit type requirements. */
1513 fc_assert_ret_val(wanted_action
!= ACTION_COUNT
, FALSE
);
1520 /**************************************************************************
1521 Returns TRUE iff the wanted action is possible as far as the actor is
1522 concerned given that an action enabler later will enable it. Will, unlike
1523 action_actor_utype_hard_reqs_ok(), check the actor unit's current state.
1525 Can return maybe when not omniscient. Should always return yes or no when
1527 **************************************************************************/
1528 static enum fc_tristate
1529 action_hard_reqs_actor(const enum gen_action wanted_action
,
1530 const struct player
*actor_player
,
1531 const struct city
*actor_city
,
1532 const struct impr_type
*actor_building
,
1533 const struct tile
*actor_tile
,
1534 const struct unit
*actor_unit
,
1535 const struct unit_type
*actor_unittype
,
1536 const struct output_type
*actor_output
,
1537 const struct specialist
*actor_specialist
,
1538 const bool omniscient
,
1539 const struct city
*homecity
)
1541 if (!action_actor_utype_hard_reqs_ok(wanted_action
, actor_unittype
)) {
1542 /* Info leak: The actor player knows the type of his unit. */
1543 /* The actor unit type can't perform the action because of hard
1544 * unit type requirements. */
1548 switch (wanted_action
) {
1549 case ACTION_TRADE_ROUTE
:
1550 case ACTION_MARKETPLACE
:
1551 /* It isn't possible to establish a trade route from a non existing
1552 * city. The Freeciv code assumes this applies to Enter Marketplace
1554 /* Info leak: The actor player knowns his unit's home city. */
1555 if (homecity
== NULL
) {
1561 case ACTION_PARADROP
:
1562 /* Reason: Keep the old rules. */
1563 /* Info leak: The player knows if his unit already has paradropped this
1565 if (actor_unit
->paradropped
) {
1569 /* Reason: Support the paratroopers_mr_req unit type field. */
1570 /* Info leak: The player knows how many move fragments his unit has
1572 if (actor_unit
->moves_left
< actor_unittype
->paratroopers_mr_req
) {
1578 case ACTION_AIRLIFT
:
1580 const struct city
*psrc_city
= tile_city(actor_tile
);
1582 if (psrc_city
== NULL
) {
1583 /* No city to airlift from. */
1587 if (actor_player
!= city_owner(psrc_city
)
1588 && !(game
.info
.airlifting_style
& AIRLIFTING_ALLIED_SRC
1589 && pplayers_allied(actor_player
, city_owner(psrc_city
)))) {
1590 /* Not allowed to airlift from this source. */
1594 if (!(omniscient
|| city_owner(psrc_city
) == actor_player
)) {
1595 /* Can't check for airlifting capacity. */
1599 if (0 >= psrc_city
->airlift
) {
1600 /* The source cannot airlift for this turn (maybe already airlifted
1603 * Note that (game.info.airlifting_style & AIRLIFTING_UNLIMITED_SRC)
1604 * is not handled here because it always needs an airport to airlift.
1605 * See also do_airline() in server/unittools.h. */
1611 case ACTION_ESTABLISH_EMBASSY
:
1612 case ACTION_SPY_INVESTIGATE_CITY
:
1613 case ACTION_SPY_POISON
:
1614 case ACTION_SPY_STEAL_GOLD
:
1615 case ACTION_SPY_SABOTAGE_CITY
:
1616 case ACTION_SPY_TARGETED_SABOTAGE_CITY
:
1617 case ACTION_SPY_STEAL_TECH
:
1618 case ACTION_SPY_TARGETED_STEAL_TECH
:
1619 case ACTION_SPY_INCITE_CITY
:
1620 case ACTION_HELP_WONDER
:
1621 case ACTION_SPY_BRIBE_UNIT
:
1622 case ACTION_SPY_SABOTAGE_UNIT
:
1623 case ACTION_CAPTURE_UNITS
:
1624 case ACTION_FOUND_CITY
:
1625 case ACTION_JOIN_CITY
:
1626 case ACTION_STEAL_MAPS
:
1627 case ACTION_BOMBARD
:
1628 case ACTION_SPY_NUKE
:
1630 case ACTION_DESTROY_CITY
:
1631 case ACTION_EXPEL_UNIT
:
1632 case ACTION_RECYCLE_UNIT
:
1633 case ACTION_DISBAND_UNIT
:
1634 case ACTION_HOME_CITY
:
1635 case ACTION_UPGRADE_UNIT
:
1637 case ACTION_CONQUER_CITY
:
1638 case ACTION_HEAL_UNIT
:
1639 /* No hard unit type requirements. */
1643 fc_assert_ret_val(wanted_action
!= ACTION_COUNT
, TRI_NO
);
1650 /**************************************************************************
1651 Returns if the wanted action is possible given that an action enabler
1652 later will enable it.
1654 Can return maybe when not omniscient. Should always return yes or no when
1657 This is done by checking the action's hard requirements. Hard
1658 requirements must be fulfilled before an action can be done. The reason
1659 why is usually that code dealing with the action assumes that the
1660 requirements are true. A requirement may also end up here if it can't be
1661 expressed in a requirement vector or if its absence makes the action
1664 When adding a new hard requirement here:
1665 * explain why it is a hard requirement in a comment.
1666 * remember that this is called from action_prob(). Should information
1667 the player don't have access to be used in a test it must check if
1668 the evaluation can see the thing being tested.
1669 **************************************************************************/
1670 static enum fc_tristate
1671 is_action_possible(const enum gen_action wanted_action
,
1672 const struct player
*actor_player
,
1673 const struct city
*actor_city
,
1674 const struct impr_type
*actor_building
,
1675 const struct tile
*actor_tile
,
1676 const struct unit
*actor_unit
,
1677 const struct unit_type
*actor_unittype
,
1678 const struct output_type
*actor_output
,
1679 const struct specialist
*actor_specialist
,
1680 const struct player
*target_player
,
1681 const struct city
*target_city
,
1682 const struct impr_type
*target_building
,
1683 const struct tile
*target_tile
,
1684 const struct unit
*target_unit
,
1685 const struct unit_type
*target_unittype
,
1686 const struct output_type
*target_output
,
1687 const struct specialist
*target_specialist
,
1688 const bool omniscient
,
1689 const struct city
*homecity
,
1692 bool can_see_tgt_unit
;
1693 bool can_see_tgt_tile
;
1694 enum fc_tristate out
;
1696 fc_assert_msg((action_id_get_target_kind(wanted_action
) == ATK_CITY
1697 && target_city
!= NULL
)
1698 || (action_id_get_target_kind(wanted_action
) == ATK_TILE
1699 && target_tile
!= NULL
)
1700 || (action_id_get_target_kind(wanted_action
) == ATK_UNIT
1701 && target_unit
!= NULL
)
1702 || (action_id_get_target_kind(wanted_action
) == ATK_UNITS
1703 /* At this level each individual unit is tested. */
1704 && target_unit
!= NULL
)
1705 || (action_id_get_target_kind(wanted_action
) == ATK_SELF
),
1708 /* Only check requirement against the target unit if the actor can see it
1709 * or if the evaluator is omniscient. The game checking the rules is
1710 * omniscient. The player asking about his odds isn't. */
1711 can_see_tgt_unit
= (omniscient
|| (target_unit
1712 && can_player_see_unit(actor_player
,
1715 /* Only check requirement against the target tile if the actor can see it
1716 * or if the evaluator is omniscient. The game checking the rules is
1717 * omniscient. The player asking about his odds isn't. */
1718 can_see_tgt_tile
= (omniscient
1719 || plr_sees_tile(actor_player
, target_tile
));
1721 /* Info leak: The player knows where his unit is. */
1722 if (!ignore_dist
&& action_id_get_target_kind(wanted_action
) != ATK_SELF
1723 && !action_id_distance_accepted(wanted_action
,
1724 real_map_distance(actor_tile
,
1726 /* The distance between the actor and the target isn't inside the
1727 * action's accepted range. */
1731 switch (action_id_get_target_kind(wanted_action
)) {
1733 /* The Freeciv code for all actions that is controlled by action
1734 * enablers and targets a unit assumes that the acting
1735 * player can see the target unit.
1737 * - action_prob_vs_unit()'s quick check that the distance between actor
1738 * and target is acceptable would leak distance to target unit if the
1739 * target unit can't be seen.
1741 if (!can_player_see_unit(actor_player
, target_unit
)) {
1746 /* The Freeciv code assumes that the player is aware of the target
1747 * city's existence. (How can you order an airlift to a city when you
1748 * don't know its city ID?) */
1749 if (fc_funcs
->player_tile_city_id_get(city_tile(target_city
),
1751 != target_city
->id
) {
1758 /* No special player knowledge checks. */
1761 fc_assert(action_id_get_target_kind(wanted_action
) != ATK_COUNT
);
1765 if (action_is_blocked_by(wanted_action
, actor_unit
,
1766 target_tile
, target_city
, target_unit
)) {
1767 /* Allows an action to block an other action. If a blocking action is
1768 * legal the actions it blocks becomes illegal. */
1772 /* Actor specific hard requirements. */
1773 out
= action_hard_reqs_actor(wanted_action
,
1774 actor_player
, actor_city
, actor_building
,
1775 actor_tile
, actor_unit
, actor_unittype
,
1776 actor_output
, actor_specialist
,
1777 omniscient
, homecity
);
1779 if (out
== TRI_NO
) {
1780 /* Illegal because of a hard actor requirement. */
1784 /* Hard requirements for individual actions. */
1785 switch (wanted_action
) {
1786 case ACTION_CAPTURE_UNITS
:
1787 case ACTION_SPY_BRIBE_UNIT
:
1788 /* Why this is a hard requirement: Can't transfer a unique unit if the
1789 * actor player already has one. */
1790 /* Info leak: This is only checked for when the actor player can see
1791 * the target unit. Since the target unit is seen its type is known.
1792 * The fact that a city hiding the unseen unit is occupied is known. */
1794 if (!can_see_tgt_unit
) {
1795 /* An omniscient player can see the target unit. */
1796 fc_assert(!omniscient
);
1801 if (utype_player_already_has_this_unique(actor_player
,
1806 /* FIXME: Capture Unit may want to look for more than one unique unit
1807 * of the same kind at the target tile. Currently caught by sanity
1808 * check in do_capture_units(). */
1812 case ACTION_ESTABLISH_EMBASSY
:
1813 /* Why this is a hard requirement: There is currently no point in
1814 * establishing an embassy when a real embassy already exists.
1815 * (Possible exception: crazy hack using the Lua callback
1816 * action_started_callback() to make establish embassy do something
1817 * else even if the UI still call the action Establish Embassy) */
1818 /* Info leak: The actor player known who he has a real embassy to. */
1819 if (player_has_real_embassy(actor_player
, target_player
)) {
1825 case ACTION_SPY_TARGETED_STEAL_TECH
:
1826 /* Reason: The Freeciv code don't support selecting a target tech
1827 * unless it is known that the victim player has it. */
1828 /* Info leak: The actor player knowns who's techs he can see. */
1829 if (!can_see_techs_of_target(actor_player
, target_player
)) {
1835 case ACTION_SPY_STEAL_GOLD
:
1836 /* If actor_unit can do the action the actor_player can see how much
1837 * gold target_player have. Not requireing it is therefore pointless.
1839 if (target_player
->economic
.gold
<= 0) {
1845 case ACTION_TRADE_ROUTE
:
1846 case ACTION_MARKETPLACE
:
1848 /* Checked in action_hard_reqs_actor() */
1849 fc_assert_ret_val(homecity
!= NULL
, TRI_NO
);
1851 /* Can't establish a trade route or enter the market place if the
1852 * cities can't trade at all. */
1853 /* TODO: Should this restriction (and the above restriction that the
1854 * actor unit must have a home city) be kept for Enter Marketplace? */
1855 if (!can_cities_trade(homecity
, target_city
)) {
1859 /* There are more restrictions on establishing a trade route than on
1860 * entering the market place. */
1861 if (wanted_action
== ACTION_TRADE_ROUTE
1862 && !can_establish_trade_route(homecity
, target_city
)) {
1869 case ACTION_HELP_WONDER
:
1870 case ACTION_RECYCLE_UNIT
:
1871 /* It is only possible to help the production if the production needs
1872 * the help. (If not it would be possible to add shields for something
1873 * that can't legally receive help if it is build later) */
1874 /* Info leak: The player knows that the production in his own city has
1875 * been hurried (bought or helped). The information isn't revealed when
1876 * asking for action probabilities since omniscient is FALSE. */
1878 && !can_player_see_city_internals(actor_player
, target_city
)) {
1882 if (!(target_city
->shield_stock
1883 < city_production_build_shield_cost(target_city
))) {
1889 case ACTION_FOUND_CITY
:
1890 if (game
.scenario
.prevent_new_cities
) {
1891 /* Reason: allow scenarios to disable city founding. */
1892 /* Info leak: the setting is public knowledge. */
1896 if (can_see_tgt_tile
&& tile_city(target_tile
)) {
1897 /* Reason: a tile can have 0 or 1 cities. */
1901 switch (city_build_here_test(target_tile
, actor_unit
)) {
1903 /* If the player knows this is checked below. */
1905 case CB_BAD_CITY_TERRAIN
:
1906 case CB_BAD_UNIT_TERRAIN
:
1907 case CB_BAD_BORDERS
:
1908 if (can_see_tgt_tile
) {
1909 /* Known to be blocked. Target tile is seen. */
1913 case CB_NO_MIN_DIST
:
1915 /* No need to check again. */
1918 square_iterate(target_tile
, game
.info
.citymindist
- 1, otile
) {
1919 if (tile_city(otile
) != NULL
1920 && plr_sees_tile(actor_player
, otile
)) {
1921 /* Known to be blocked by citymindist */
1924 } square_iterate_end
;
1929 /* The player may not have enough information to be certain. */
1931 if (!can_see_tgt_tile
) {
1932 /* Need to know if target tile already has a city, has TER_NO_CITIES
1933 * terrain, is non native to the actor or is owned by a foreigner. */
1938 /* The player may not have enough information to find out if
1939 * citymindist blocks or not. This doesn't depend on if it blocks. */
1940 square_iterate(target_tile
, game
.info
.citymindist
- 1, otile
) {
1941 if (!plr_sees_tile(actor_player
, otile
)) {
1942 /* Could have a city that blocks via citymindist. Even if this
1943 * tile has TER_NO_CITIES terrain the player don't know that it
1944 * didn't change and had a city built on it. */
1947 } square_iterate_end
;
1952 case ACTION_JOIN_CITY
:
1957 && !player_can_see_city_externals(actor_player
, target_city
)) {
1961 new_pop
= city_size_get(target_city
) + unit_pop_value(actor_unit
);
1963 if (new_pop
> game
.info
.add_to_size_limit
) {
1964 /* Reason: Make the add_to_size_limit setting work. */
1968 if (!city_can_grow_to(target_city
, new_pop
)) {
1969 /* Reason: respect city size limits. */
1970 /* Info leak: when it is legal to join a foreign city is legal and
1971 * the EFT_SIZE_UNLIMIT effect or the EFT_SIZE_ADJ effect depends on
1972 * something the actor player don't have access to.
1973 * Example: depends on a building (like Aqueduct) that isn't
1974 * VisibleByOthers. */
1981 case ACTION_BOMBARD
:
1982 /* FIXME: Target of Bombard should be city and units. */
1983 if (tile_city(target_tile
)
1984 && !pplayers_at_war(city_owner(tile_city(target_tile
)),
1992 if (actor_tile
!= target_tile
) {
1993 /* The old rules only restricted other tiles. Keep them for now. */
1997 if (actor_unit
->moves_left
<= 0) {
2001 if (!(tcity
= tile_city(target_tile
))
2002 && !unit_list_size(target_tile
->units
)) {
2006 if (tcity
&& !pplayers_at_war(city_owner(tcity
), actor_player
)) {
2010 if (is_non_attack_unit_tile(target_tile
, actor_player
)) {
2015 && (unit_attack_units_at_tile_result(actor_unit
, target_tile
)
2023 case ACTION_HOME_CITY
:
2024 /* Reason: can't change to what is. */
2025 /* Info leak: The player knows his unit's current home city. */
2026 if (homecity
!= NULL
&& homecity
->id
== target_city
->id
) {
2027 /* This is already the unit's home city. */
2032 int slots
= unit_type_get(actor_unit
)->city_slots
;
2034 if (slots
> 0 && city_unit_slots_available(target_city
) < slots
) {
2041 case ACTION_UPGRADE_UNIT
:
2042 /* Reason: Keep the old rules. */
2043 /* Info leak: The player knows his unit's type. He knows if he can
2044 * build the unit type upgraded to. If the upgrade happens in a foreign
2045 * city that fact may leak. This can be seen as a price for changing
2046 * the rules to allow upgrading in a foreign city.
2047 * The player knows how much gold he has. If the Upgrade_Price_Pct
2048 * effect depends on information he don't have that information may
2049 * leak. The player knows the location of his unit. He knows if the
2050 * tile has a city and if the unit can exist there outside a transport.
2051 * The player knows his unit's cargo. By knowing their number and type
2052 * he can predict if there will be room for them in the unit upgraded
2053 * to as long as he knows what unit type his unit will end up as. */
2054 if (unit_upgrade_test(actor_unit
, FALSE
) != UU_OK
) {
2060 case ACTION_PARADROP
:
2061 /* Reason: Keep the old rules. */
2062 /* Info leak: The player knows if he knows the target tile. */
2063 if (!plr_knows_tile(actor_player
, target_tile
)) {
2067 /* Reason: Keep paratroopers_range working. */
2068 /* Info leak: The player knows the location of the actor and of the
2071 && (unit_type_get(actor_unit
)->paratroopers_range
2072 < real_map_distance(actor_tile
, target_tile
))) {
2078 case ACTION_AIRLIFT
:
2079 /* Reason: Keep the old rules. */
2080 /* Info leak: same as test_unit_can_airlift_to() */
2081 switch (test_unit_can_airlift_to(omniscient
? NULL
: actor_player
,
2082 actor_unit
, target_city
)) {
2085 case AR_OK_SRC_UNKNOWN
:
2086 case AR_OK_DST_UNKNOWN
:
2089 case AR_WRONG_UNITTYPE
:
2091 case AR_NOT_IN_CITY
:
2092 case AR_BAD_SRC_CITY
:
2093 case AR_BAD_DST_CITY
:
2094 case AR_SRC_NO_FLIGHTS
:
2095 case AR_DST_NO_FLIGHTS
:
2102 /* Reason: must have a unit to attack. */
2103 if (unit_list_size(target_tile
->units
) <= 0) {
2107 /* Reason: Keep the old rules. */
2108 if (!can_unit_attack_tile(actor_unit
, target_tile
)) {
2113 case ACTION_CONQUER_CITY
:
2114 /* Reason: "Conquer City" involves moving into the city. */
2115 if (!unit_can_move_to_tile(actor_unit
, target_tile
, FALSE
, TRUE
)) {
2121 case ACTION_HEAL_UNIT
:
2122 /* Reason: It is not the healthy who need a doctor, but the sick. */
2123 /* Info leak: the actor can see the target's HP. */
2124 if (!(target_unit
->hp
< target_unittype
->hp
)) {
2129 case ACTION_SPY_INVESTIGATE_CITY
:
2130 case ACTION_SPY_POISON
:
2131 case ACTION_SPY_SABOTAGE_CITY
:
2132 case ACTION_SPY_TARGETED_SABOTAGE_CITY
:
2133 case ACTION_SPY_STEAL_TECH
:
2134 case ACTION_SPY_INCITE_CITY
:
2135 case ACTION_SPY_SABOTAGE_UNIT
:
2136 case ACTION_STEAL_MAPS
:
2137 case ACTION_SPY_NUKE
:
2138 case ACTION_DESTROY_CITY
:
2139 case ACTION_EXPEL_UNIT
:
2140 case ACTION_DISBAND_UNIT
:
2141 /* No known hard coded requirements. */
2144 fc_assert(action_id_exists(wanted_action
));
2151 /**************************************************************************
2152 Return TRUE iff the action enabler is active
2153 **************************************************************************/
2154 static bool is_enabler_active(const struct action_enabler
*enabler
,
2155 const struct player
*actor_player
,
2156 const struct city
*actor_city
,
2157 const struct impr_type
*actor_building
,
2158 const struct tile
*actor_tile
,
2159 const struct unit
*actor_unit
,
2160 const struct unit_type
*actor_unittype
,
2161 const struct output_type
*actor_output
,
2162 const struct specialist
*actor_specialist
,
2163 const struct player
*target_player
,
2164 const struct city
*target_city
,
2165 const struct impr_type
*target_building
,
2166 const struct tile
*target_tile
,
2167 const struct unit
*target_unit
,
2168 const struct unit_type
*target_unittype
,
2169 const struct output_type
*target_output
,
2170 const struct specialist
*target_specialist
)
2172 return are_reqs_active(actor_player
, target_player
, actor_city
,
2173 actor_building
, actor_tile
,
2174 actor_unit
, actor_unittype
,
2175 actor_output
, actor_specialist
, NULL
,
2176 &enabler
->actor_reqs
, RPT_CERTAIN
)
2177 && are_reqs_active(target_player
, actor_player
, target_city
,
2178 target_building
, target_tile
,
2179 target_unit
, target_unittype
,
2180 target_output
, target_specialist
, NULL
,
2181 &enabler
->target_reqs
, RPT_CERTAIN
);
2184 /**************************************************************************
2185 Returns TRUE if the wanted action is enabled.
2187 Note that the action may disable it self because of hard requirements
2188 even if an action enabler returns TRUE.
2189 **************************************************************************/
2190 static bool is_action_enabled(const enum gen_action wanted_action
,
2191 const struct player
*actor_player
,
2192 const struct city
*actor_city
,
2193 const struct impr_type
*actor_building
,
2194 const struct tile
*actor_tile
,
2195 const struct unit
*actor_unit
,
2196 const struct unit_type
*actor_unittype
,
2197 const struct output_type
*actor_output
,
2198 const struct specialist
*actor_specialist
,
2199 const struct player
*target_player
,
2200 const struct city
*target_city
,
2201 const struct impr_type
*target_building
,
2202 const struct tile
*target_tile
,
2203 const struct unit
*target_unit
,
2204 const struct unit_type
*target_unittype
,
2205 const struct output_type
*target_output
,
2206 const struct specialist
*target_specialist
,
2207 const struct city
*homecity
, bool ignore_dist
)
2209 enum fc_tristate possible
;
2211 possible
= is_action_possible(wanted_action
,
2212 actor_player
, actor_city
,
2213 actor_building
, actor_tile
,
2214 actor_unit
, actor_unittype
,
2215 actor_output
, actor_specialist
,
2216 target_player
, target_city
,
2217 target_building
, target_tile
,
2218 target_unit
, target_unittype
,
2219 target_output
, target_specialist
,
2220 TRUE
, homecity
, ignore_dist
);
2222 if (possible
!= TRI_YES
) {
2223 /* This context is omniscient. Should be yes or no. */
2224 fc_assert_msg(possible
!= TRI_MAYBE
,
2225 "Is omniscient, should get yes or no.");
2227 /* The action enablers are irrelevant since the action it self is
2232 action_enabler_list_iterate(action_enablers_for_action(wanted_action
),
2234 if (is_enabler_active(enabler
, actor_player
, actor_city
,
2235 actor_building
, actor_tile
,
2236 actor_unit
, actor_unittype
,
2237 actor_output
, actor_specialist
,
2238 target_player
, target_city
,
2239 target_building
, target_tile
,
2240 target_unit
, target_unittype
,
2241 target_output
, target_specialist
)) {
2244 } action_enabler_list_iterate_end
;
2249 /**************************************************************************
2250 Returns TRUE if actor_unit can do wanted_action to target_city as far as
2251 action enablers are concerned.
2253 See note in is_action_enabled for why the action may still be disabled.
2254 **************************************************************************/
2255 bool is_action_enabled_unit_on_city(const enum gen_action wanted_action
,
2256 const struct unit
*actor_unit
,
2257 const struct city
*target_city
)
2259 return is_action_enabled_unit_on_city_full(wanted_action
, actor_unit
,
2261 game_city_by_number(actor_unit
->homecity
),
2265 /**************************************************************************
2266 Returns TRUE if actor_unit can do wanted_action to target_city as far as
2267 action enablers are concerned.
2269 See note in is_action_enabled for why the action may still be disabled.
2270 **************************************************************************/
2271 bool is_action_enabled_unit_on_city_full(const enum gen_action wanted_action
,
2272 const struct unit
*actor_unit
,
2273 const struct city
*target_city
,
2274 const struct city
*homecity
,
2277 struct tile
*actor_tile
= unit_tile(actor_unit
);
2278 struct impr_type
*target_building
;
2279 struct unit_type
*target_utype
;
2281 if (actor_unit
== NULL
|| target_city
== NULL
) {
2282 /* Can't do an action when actor or target are missing. */
2286 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(wanted_action
),
2287 FALSE
, "Action %s is performed by %s not %s",
2288 gen_action_name(wanted_action
),
2289 action_actor_kind_name(
2290 action_id_get_actor_kind(wanted_action
)),
2291 action_actor_kind_name(AAK_UNIT
));
2293 fc_assert_ret_val_msg(ATK_CITY
2294 == action_id_get_target_kind(wanted_action
),
2295 FALSE
, "Action %s is against %s not %s",
2296 gen_action_name(wanted_action
),
2297 action_target_kind_name(
2298 action_id_get_target_kind(wanted_action
)),
2299 action_target_kind_name(ATK_CITY
));
2301 if (!unit_can_do_action(actor_unit
, wanted_action
)) {
2302 /* No point in continuing. */
2306 target_building
= tgt_city_local_building(target_city
);
2307 target_utype
= tgt_city_local_utype(target_city
);
2309 return is_action_enabled(wanted_action
,
2310 unit_owner(actor_unit
), tile_city(actor_tile
),
2312 actor_unit
, unit_type_get(actor_unit
),
2314 city_owner(target_city
), target_city
,
2315 target_building
, city_tile(target_city
),
2316 NULL
, target_utype
, NULL
, NULL
, homecity
,
2320 /**************************************************************************
2321 Returns TRUE if actor_unit can do wanted_action to target_unit as far as
2322 action enablers are concerned.
2324 See note in is_action_enabled for why the action may still be disabled.
2325 **************************************************************************/
2326 bool is_action_enabled_unit_on_unit(const enum gen_action wanted_action
,
2327 const struct unit
*actor_unit
,
2328 const struct unit
*target_unit
)
2330 struct tile
*actor_tile
= unit_tile(actor_unit
);
2332 if (actor_unit
== NULL
|| target_unit
== NULL
) {
2333 /* Can't do an action when actor or target are missing. */
2337 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(wanted_action
),
2338 FALSE
, "Action %s is performed by %s not %s",
2339 gen_action_name(wanted_action
),
2340 action_actor_kind_name(
2341 action_id_get_actor_kind(wanted_action
)),
2342 action_actor_kind_name(AAK_UNIT
));
2344 fc_assert_ret_val_msg(ATK_UNIT
2345 == action_id_get_target_kind(wanted_action
),
2346 FALSE
, "Action %s is against %s not %s",
2347 gen_action_name(wanted_action
),
2348 action_target_kind_name(
2349 action_id_get_target_kind(wanted_action
)),
2350 action_target_kind_name(ATK_UNIT
));
2352 if (!unit_can_do_action(actor_unit
, wanted_action
)) {
2353 /* No point in continuing. */
2357 return is_action_enabled(wanted_action
,
2358 unit_owner(actor_unit
), tile_city(actor_tile
),
2360 actor_unit
, unit_type_get(actor_unit
),
2362 unit_owner(target_unit
),
2363 tile_city(unit_tile(target_unit
)), NULL
,
2364 unit_tile(target_unit
),
2365 target_unit
, unit_type_get(target_unit
),
2367 game_city_by_number(actor_unit
->homecity
),
2371 /**************************************************************************
2372 Returns TRUE if actor_unit can do wanted_action to all units on the
2373 target_tile as far as action enablers are concerned.
2375 See note in is_action_enabled for why the action may still be disabled.
2376 **************************************************************************/
2377 bool is_action_enabled_unit_on_units(const enum gen_action wanted_action
,
2378 const struct unit
*actor_unit
,
2379 const struct tile
*target_tile
)
2381 struct tile
*actor_tile
= unit_tile(actor_unit
);
2382 struct city
*homecity
;
2384 if (actor_unit
== NULL
|| target_tile
== NULL
2385 || unit_list_size(target_tile
->units
) == 0) {
2386 /* Can't do an action when actor or target are missing. */
2390 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(wanted_action
),
2391 FALSE
, "Action %s is performed by %s not %s",
2392 gen_action_name(wanted_action
),
2393 action_actor_kind_name(
2394 action_id_get_actor_kind(wanted_action
)),
2395 action_actor_kind_name(AAK_UNIT
));
2397 fc_assert_ret_val_msg(ATK_UNITS
2398 == action_id_get_target_kind(wanted_action
),
2399 FALSE
, "Action %s is against %s not %s",
2400 gen_action_name(wanted_action
),
2401 action_target_kind_name(
2402 action_id_get_target_kind(wanted_action
)),
2403 action_target_kind_name(ATK_UNITS
));
2405 if (!unit_can_do_action(actor_unit
, wanted_action
)) {
2406 /* No point in continuing. */
2410 homecity
= game_city_by_number(actor_unit
->homecity
);
2412 unit_list_iterate(target_tile
->units
, target_unit
) {
2413 if (!is_action_enabled(wanted_action
,
2414 unit_owner(actor_unit
), tile_city(actor_tile
),
2416 actor_unit
, unit_type_get(actor_unit
),
2418 unit_owner(target_unit
),
2419 tile_city(unit_tile(target_unit
)), NULL
,
2420 unit_tile(target_unit
),
2421 target_unit
, unit_type_get(target_unit
),
2422 NULL
, NULL
, homecity
, FALSE
)) {
2423 /* One unit makes it impossible for all units. */
2426 } unit_list_iterate_end
;
2428 /* Not impossible for any of the units at the tile. */
2432 /**************************************************************************
2433 Returns TRUE if actor_unit can do wanted_action to the target_tile as far
2434 as action enablers are concerned.
2436 See note in is_action_enabled for why the action may still be disabled.
2437 **************************************************************************/
2438 bool is_action_enabled_unit_on_tile(const enum gen_action wanted_action
,
2439 const struct unit
*actor_unit
,
2440 const struct tile
*target_tile
)
2442 struct tile
*actor_tile
= unit_tile(actor_unit
);
2444 if (actor_unit
== NULL
|| target_tile
== NULL
) {
2445 /* Can't do an action when actor or target are missing. */
2449 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(wanted_action
),
2450 FALSE
, "Action %s is performed by %s not %s",
2451 gen_action_name(wanted_action
),
2452 action_actor_kind_name(
2453 action_id_get_actor_kind(wanted_action
)),
2454 action_actor_kind_name(AAK_UNIT
));
2456 fc_assert_ret_val_msg(ATK_TILE
2457 == action_id_get_target_kind(wanted_action
),
2458 FALSE
, "Action %s is against %s not %s",
2459 gen_action_name(wanted_action
),
2460 action_target_kind_name(
2461 action_id_get_target_kind(wanted_action
)),
2462 action_target_kind_name(ATK_TILE
));
2464 if (!unit_can_do_action(actor_unit
, wanted_action
)) {
2465 /* No point in continuing. */
2469 return is_action_enabled(wanted_action
,
2470 unit_owner(actor_unit
), tile_city(actor_tile
),
2472 actor_unit
, unit_type_get(actor_unit
),
2474 tile_owner(target_tile
), NULL
, NULL
,
2475 target_tile
, NULL
, NULL
, NULL
, NULL
,
2476 game_city_by_number(actor_unit
->homecity
),
2480 /**************************************************************************
2481 Returns TRUE if actor_unit can do wanted_action to itself as far as
2482 action enablers are concerned.
2484 See note in is_action_enabled() for why the action still may be
2486 **************************************************************************/
2487 bool is_action_enabled_unit_on_self(const enum gen_action wanted_action
,
2488 const struct unit
*actor_unit
)
2490 struct tile
*actor_tile
= unit_tile(actor_unit
);
2492 if (actor_unit
== NULL
) {
2493 /* Can't do an action when the actor is missing. */
2497 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(wanted_action
),
2498 FALSE
, "Action %s is performed by %s not %s",
2499 gen_action_name(wanted_action
),
2500 action_actor_kind_name(
2501 action_id_get_actor_kind(wanted_action
)),
2502 action_actor_kind_name(AAK_UNIT
));
2504 fc_assert_ret_val_msg(ATK_SELF
2505 == action_id_get_target_kind(wanted_action
),
2506 FALSE
, "Action %s is against %s not %s",
2507 gen_action_name(wanted_action
),
2508 action_target_kind_name(
2509 action_id_get_target_kind(wanted_action
)),
2510 action_target_kind_name(ATK_SELF
));
2512 if (!unit_can_do_action(actor_unit
, wanted_action
)) {
2513 /* No point in continuing. */
2517 return is_action_enabled(wanted_action
,
2518 unit_owner(actor_unit
), tile_city(actor_tile
),
2520 actor_unit
, unit_type_get(actor_unit
),
2522 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
,
2523 game_city_by_number(actor_unit
->homecity
), FALSE
);
2526 /**************************************************************************
2527 Find out if the action is enabled, may be enabled or isn't enabled given
2528 what the player owning the actor knowns.
2530 A player don't always know everything needed to figure out if an action
2531 is enabled or not. A server side AI with the same limits on its knowledge
2532 as a human player or a client should use this to figure out what is what.
2534 Assumes to be called from the point of view of the actor. Its knowledge
2535 is assumed to be given in the parameters.
2537 Returns TRI_YES if the action is enabled, TRI_NO if it isn't and
2538 TRI_MAYBE if the player don't know enough to tell.
2540 If meta knowledge is missing TRI_MAYBE will be returned.
2541 **************************************************************************/
2542 static enum fc_tristate
2543 action_enabled_local(const enum gen_action wanted_action
,
2544 const struct player
*actor_player
,
2545 const struct city
*actor_city
,
2546 const struct impr_type
*actor_building
,
2547 const struct tile
*actor_tile
,
2548 const struct unit
*actor_unit
,
2549 const struct output_type
*actor_output
,
2550 const struct specialist
*actor_specialist
,
2551 const struct player
*target_player
,
2552 const struct city
*target_city
,
2553 const struct impr_type
*target_building
,
2554 const struct tile
*target_tile
,
2555 const struct unit
*target_unit
,
2556 const struct output_type
*target_output
,
2557 const struct specialist
*target_specialist
)
2559 enum fc_tristate current
;
2560 enum fc_tristate result
;
2563 action_enabler_list_iterate(action_enablers_for_action(wanted_action
),
2565 current
= fc_tristate_and(mke_eval_reqs(actor_player
, actor_player
,
2566 target_player
, actor_city
,
2567 actor_building
, actor_tile
,
2568 actor_unit
, actor_output
,
2570 &enabler
->actor_reqs
,
2572 mke_eval_reqs(actor_player
, target_player
,
2573 actor_player
, target_city
,
2574 target_building
, target_tile
,
2575 target_unit
, target_output
,
2577 &enabler
->target_reqs
,
2579 if (current
== TRI_YES
) {
2581 } else if (current
== TRI_MAYBE
) {
2584 } action_enabler_list_iterate_end
;
2589 /**************************************************************************
2590 Find out if the effect value is known
2592 The knowledge of the actor is assumed to be given in the parameters.
2594 If meta knowledge is missing TRI_MAYBE will be returned.
2595 **************************************************************************/
2596 static bool is_effect_val_known(enum effect_type effect_type
,
2597 const struct player
*pow_player
,
2598 const struct player
*target_player
,
2599 const struct player
*other_player
,
2600 const struct city
*target_city
,
2601 const struct impr_type
*target_building
,
2602 const struct tile
*target_tile
,
2603 const struct unit
*target_unit
,
2604 const struct output_type
*target_output
,
2605 const struct specialist
*target_specialist
)
2607 effect_list_iterate(get_effects(effect_type
), peffect
) {
2608 if (TRI_MAYBE
== mke_eval_reqs(pow_player
, target_player
,
2609 other_player
, target_city
,
2610 target_building
, target_tile
,
2611 target_unit
, target_output
,
2613 &(peffect
->reqs
), RPT_CERTAIN
)) {
2616 } effect_list_iterate_end
;
2621 /**************************************************************************
2622 Does the target has any techs the actor don't?
2623 **************************************************************************/
2624 static enum fc_tristate
2625 tech_can_be_stolen(const struct player
*actor_player
,
2626 const struct player
*target_player
)
2628 const struct research
*actor_research
= research_get(actor_player
);
2629 const struct research
*target_research
= research_get(target_player
);
2631 if (actor_research
!= target_research
) {
2632 if (can_see_techs_of_target(actor_player
, target_player
)) {
2633 advance_iterate(A_FIRST
, padvance
) {
2634 Tech_type_id i
= advance_number(padvance
);
2636 if (research_invention_state(target_research
, i
) == TECH_KNOWN
2637 && research_invention_gettable(actor_research
, i
,
2638 game
.info
.tech_steal_allow_holes
)
2639 && (research_invention_state(actor_research
, i
) == TECH_UNKNOWN
2640 || (research_invention_state(actor_research
, i
)
2641 == TECH_PREREQS_KNOWN
))) {
2644 } advance_iterate_end
;
2653 /**************************************************************************
2654 The action probability that pattacker will win a diplomatic battle.
2656 It is assumed that pattacker and pdefender have different owners and that
2657 the defender can defend in a diplomatic battle.
2659 See diplomat_success_vs_defender() in server/diplomats.c
2660 **************************************************************************/
2661 static struct act_prob
ap_dipl_battle_win(const struct unit
*pattacker
,
2662 const struct unit
*pdefender
)
2664 /* Keep unconverted until the end to avoid scaling each step */
2666 struct act_prob out
;
2668 /* Superspy always win */
2669 if (unit_has_type_flag(pdefender
, UTYF_SUPERSPY
)) {
2670 /* A defending UTYF_SUPERSPY will defeat every possible attacker. */
2671 return ACTPROB_IMPOSSIBLE
;
2673 if (unit_has_type_flag(pattacker
, UTYF_SUPERSPY
)) {
2674 /* An attacking UTYF_SUPERSPY will defeat every possible defender
2675 * except another UTYF_SUPERSPY. */
2676 return ACTPROB_CERTAIN
;
2679 /* Base chance is 50% */
2682 /* Spy attack bonus */
2683 if (unit_has_type_flag(pattacker
, UTYF_SPY
)) {
2687 /* Spy defense bonus */
2688 if (unit_has_type_flag(pdefender
, UTYF_SPY
)) {
2692 /* Veteran attack and defense bonus */
2694 const struct veteran_level
*vatt
=
2695 utype_veteran_level(unit_type_get(pattacker
), pattacker
->veteran
);
2696 const struct veteran_level
*vdef
=
2697 utype_veteran_level(unit_type_get(pdefender
), pdefender
->veteran
);
2699 chance
+= vatt
->power_fact
- vdef
->power_fact
;
2702 /* Defense bonus. */
2704 if (!is_effect_val_known(EFT_SPY_RESISTANT
, unit_owner(pattacker
),
2705 tile_owner(pdefender
->tile
), NULL
,
2706 tile_city(pdefender
->tile
), NULL
,
2707 pdefender
->tile
, NULL
, NULL
, NULL
)) {
2708 return ACTPROB_NOT_KNOWN
;
2711 /* Reduce the chance of an attack by EFT_SPY_RESISTANT percent. */
2713 * get_target_bonus_effects(NULL
,
2714 tile_owner(pdefender
->tile
), NULL
,
2715 tile_city(pdefender
->tile
), NULL
,
2716 pdefender
->tile
, NULL
, NULL
, NULL
,
2718 EFT_SPY_RESISTANT
) / 100;
2721 /* Convert to action probability */
2722 out
.min
= chance
* ACTPROB_VAL_1_PCT
;
2723 out
.max
= chance
* ACTPROB_VAL_1_PCT
;
2728 /**************************************************************************
2729 The action probability that pattacker will win a diplomatic battle.
2731 See diplomat_infiltrate_tile() in server/diplomats.c
2732 **************************************************************************/
2733 static struct act_prob
ap_diplomat_battle(const struct unit
*pattacker
,
2734 const struct unit
*pvictim
)
2736 unit_list_iterate(unit_tile(pvictim
)->units
, punit
) {
2737 if (unit_owner(punit
) == unit_owner(pattacker
)) {
2738 /* Won't defend against its owner. */
2742 if (punit
== pvictim
2743 && !unit_has_type_flag(punit
, UTYF_SUPERSPY
)) {
2744 /* The victim unit is defenseless unless it's a SuperSpy.
2745 * Rationalization: A regular diplomat don't mind being bribed. A
2746 * SuperSpy is high enough up the chain that accepting a bribe is
2747 * against his own interests. */
2751 if (!(unit_has_type_flag(punit
, UTYF_DIPLOMAT
)
2752 || unit_has_type_flag(punit
, UTYF_SUPERSPY
))) {
2753 /* The unit can't defend. */
2757 /* There will be a diplomatic battle in stead of an action. */
2758 return ap_dipl_battle_win(pattacker
, punit
);
2759 } unit_list_iterate_end
;
2761 /* No diplomatic battle will occur. */
2762 return ACTPROB_CERTAIN
;
2765 /***************************************************************************
2766 Returns the action probability for when a target is unseen.
2767 ***************************************************************************/
2768 static struct act_prob
act_prob_unseen_target(int action_id
,
2769 const struct unit
*actor_unit
)
2771 if (action_maybe_possible_actor_unit(action_id
, actor_unit
)) {
2772 /* Unknown because the target is unseen. */
2773 return ACTPROB_NOT_KNOWN
;
2775 /* The actor it self can't do this. */
2776 return ACTPROB_IMPOSSIBLE
;
2780 /**************************************************************************
2781 An action's probability of success.
2783 "Success" indicates that the action achieves its goal, not that the
2784 actor survives. For actions that cost money it is assumed that the
2785 player has and is willing to spend the money. This is so the player can
2786 figure out what his odds are before deciding to get the extra money.
2787 **************************************************************************/
2788 static struct act_prob
2789 action_prob(const enum gen_action wanted_action
,
2790 const struct player
*actor_player
,
2791 const struct city
*actor_city
,
2792 const struct impr_type
*actor_building
,
2793 const struct tile
*actor_tile
,
2794 const struct unit
*actor_unit
,
2795 const struct unit_type
*actor_unittype_p
,
2796 const struct output_type
*actor_output
,
2797 const struct specialist
*actor_specialist
,
2798 const struct player
*target_player
,
2799 const struct city
*target_city
,
2800 const struct impr_type
*target_building
,
2801 const struct tile
*target_tile
,
2802 const struct unit
*target_unit
,
2803 const struct unit_type
*target_unittype_p
,
2804 const struct output_type
*target_output
,
2805 const struct specialist
*target_specialist
)
2808 struct act_prob chance
;
2809 const struct unit_type
*actor_unittype
;
2810 const struct unit_type
*target_unittype
;
2811 const struct city
*homecity
;
2813 if (actor_unittype_p
== NULL
&& actor_unit
!= NULL
) {
2814 actor_unittype
= unit_type_get(actor_unit
);
2816 actor_unittype
= actor_unittype_p
;
2819 if (target_unittype_p
== NULL
&& target_unit
!= NULL
) {
2820 target_unittype
= unit_type_get(target_unit
);
2822 target_unittype
= target_unittype_p
;
2825 if (actor_unit
!= NULL
) {
2826 homecity
= game_city_by_number(actor_unit
->homecity
);
2831 known
= is_action_possible(wanted_action
,
2832 actor_player
, actor_city
,
2833 actor_building
, actor_tile
,
2834 actor_unit
, actor_unittype
,
2835 actor_output
, actor_specialist
,
2836 target_player
, target_city
,
2837 target_building
, target_tile
,
2838 target_unit
, target_unittype
,
2839 target_output
, target_specialist
,
2840 FALSE
, homecity
, FALSE
);
2842 if (known
== TRI_NO
) {
2843 /* The action enablers are irrelevant since the action it self is
2845 return ACTPROB_IMPOSSIBLE
;
2848 chance
= ACTPROB_NOT_IMPLEMENTED
;
2850 known
= fc_tristate_and(known
,
2851 action_enabled_local(wanted_action
,
2852 actor_player
, actor_city
,
2853 actor_building
, actor_tile
,
2857 target_player
, target_city
,
2858 target_building
, target_tile
,
2861 target_specialist
));
2863 switch (wanted_action
) {
2864 case ACTION_SPY_POISON
:
2867 case ACTION_SPY_STEAL_GOLD
:
2870 case ACTION_STEAL_MAPS
:
2873 case ACTION_SPY_SABOTAGE_UNIT
:
2874 /* All uncertainty comes from potential diplomatic battles. */
2875 chance
= ap_diplomat_battle(actor_unit
, target_unit
);
2877 case ACTION_SPY_BRIBE_UNIT
:
2878 /* All uncertainty comes from potential diplomatic battles. */
2879 chance
= ap_diplomat_battle(actor_unit
, target_unit
);;
2881 case ACTION_SPY_SABOTAGE_CITY
:
2884 case ACTION_SPY_TARGETED_SABOTAGE_CITY
:
2887 case ACTION_SPY_INCITE_CITY
:
2890 case ACTION_ESTABLISH_EMBASSY
:
2891 chance
= ACTPROB_CERTAIN
;
2893 case ACTION_SPY_STEAL_TECH
:
2894 /* Do the victim have anything worth taking? */
2895 known
= fc_tristate_and(known
,
2896 tech_can_be_stolen(actor_player
,
2899 /* TODO: Calculate actual chance */
2902 case ACTION_SPY_TARGETED_STEAL_TECH
:
2903 /* Do the victim have anything worth taking? */
2904 known
= fc_tristate_and(known
,
2905 tech_can_be_stolen(actor_player
,
2908 /* TODO: Calculate actual chance */
2911 case ACTION_SPY_INVESTIGATE_CITY
:
2912 /* There is no risk that the city won't get investigated. */
2913 chance
= ACTPROB_CERTAIN
;
2915 case ACTION_TRADE_ROUTE
:
2918 case ACTION_MARKETPLACE
:
2919 /* Possible when not blocked by is_action_possible() */
2920 chance
= ACTPROB_CERTAIN
;
2922 case ACTION_HELP_WONDER
:
2923 /* Possible when not blocked by is_action_possible() */
2924 chance
= ACTPROB_CERTAIN
;
2926 case ACTION_CAPTURE_UNITS
:
2927 /* No battle is fought first. */
2928 chance
= ACTPROB_CERTAIN
;
2930 case ACTION_EXPEL_UNIT
:
2931 /* No battle is fought first. */
2932 chance
= ACTPROB_CERTAIN
;
2934 case ACTION_BOMBARD
:
2935 /* No battle is fought first. */
2936 chance
= ACTPROB_CERTAIN
;
2938 case ACTION_FOUND_CITY
:
2939 /* Possible when not blocked by is_action_possible() */
2940 chance
= ACTPROB_CERTAIN
;
2942 case ACTION_JOIN_CITY
:
2943 /* Possible when not blocked by is_action_possible() */
2944 chance
= ACTPROB_CERTAIN
;
2946 case ACTION_SPY_NUKE
:
2952 case ACTION_DESTROY_CITY
:
2953 /* No battle is fought first. */
2954 chance
= ACTPROB_CERTAIN
;
2956 case ACTION_RECYCLE_UNIT
:
2957 /* No battle is fought first. */
2958 chance
= ACTPROB_CERTAIN
;
2960 case ACTION_DISBAND_UNIT
:
2961 /* No battle is fought first. */
2962 chance
= ACTPROB_CERTAIN
;
2964 case ACTION_HOME_CITY
:
2965 /* No battle is fought first. */
2966 chance
= ACTPROB_CERTAIN
;
2968 case ACTION_UPGRADE_UNIT
:
2969 /* No battle is fought first. */
2970 chance
= ACTPROB_CERTAIN
;
2972 case ACTION_PARADROP
:
2975 case ACTION_AIRLIFT
:
2980 struct unit
*defender_unit
= get_defender(actor_unit
, target_tile
);
2982 if (can_player_see_unit(actor_player
, defender_unit
)) {
2983 double unconverted
= unit_win_chance(actor_unit
, defender_unit
);
2985 chance
.min
= MAX(ACTPROB_VAL_MIN
,
2986 floor((double)ACTPROB_VAL_MAX
* unconverted
));
2987 chance
.max
= MIN(ACTPROB_VAL_MAX
,
2988 ceil((double)ACTPROB_VAL_MAX
* unconverted
));
2989 } else if (known
== TRI_YES
) {
2994 case ACTION_CONQUER_CITY
:
2995 /* No battle is fought first. */
2996 chance
= ACTPROB_CERTAIN
;
2998 case ACTION_HEAL_UNIT
:
2999 /* No battle is fought first. */
3000 chance
= ACTPROB_CERTAIN
;
3003 fc_assert(wanted_action
!= ACTION_COUNT
);
3007 /* Non signal action probabilities should be in range. */
3008 fc_assert_action((action_prob_is_signal(chance
)
3009 || chance
.max
<= ACTPROB_VAL_MAX
),
3010 chance
.max
= ACTPROB_VAL_MAX
);
3011 fc_assert_action((action_prob_is_signal(chance
)
3012 || chance
.min
>= ACTPROB_VAL_MIN
),
3013 chance
.min
= ACTPROB_VAL_MIN
);
3017 return ACTPROB_IMPOSSIBLE
;
3020 return ACTPROB_NOT_KNOWN
;
3027 fc_assert_ret_val_msg(FALSE
, ACTPROB_NOT_IMPLEMENTED
,
3028 "Should be yes, maybe or no");
3031 /**************************************************************************
3032 Get the actor unit's probability of successfully performing the chosen
3033 action on the target city.
3034 **************************************************************************/
3035 struct act_prob
action_prob_vs_city(const struct unit
* actor_unit
,
3036 const int action_id
,
3037 const struct city
* target_city
)
3039 struct tile
*actor_tile
= unit_tile(actor_unit
);
3040 struct impr_type
*target_building
;
3041 struct unit_type
*target_utype
;
3043 if (actor_unit
== NULL
|| target_city
== NULL
) {
3044 /* Can't do an action when actor or target are missing. */
3045 return ACTPROB_IMPOSSIBLE
;
3048 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(action_id
),
3050 "Action %s is performed by %s not %s",
3051 gen_action_name(action_id
),
3052 action_actor_kind_name(
3053 action_id_get_actor_kind(action_id
)),
3054 action_actor_kind_name(AAK_UNIT
));
3056 fc_assert_ret_val_msg(ATK_CITY
== action_id_get_target_kind(action_id
),
3058 "Action %s is against %s not %s",
3059 gen_action_name(action_id
),
3060 action_target_kind_name(
3061 action_id_get_target_kind(action_id
)),
3062 action_target_kind_name(ATK_CITY
));
3064 if (!unit_can_do_action(actor_unit
, action_id
)) {
3065 /* No point in continuing. */
3066 return ACTPROB_IMPOSSIBLE
;
3069 /* Doesn't leak information about city position since an unknown city
3070 * can't be targeted and a city can't move. */
3071 if (!action_id_distance_accepted(action_id
,
3072 real_map_distance(unit_tile(actor_unit
),
3073 city_tile(target_city
)))) {
3074 /* No point in continuing. */
3075 return ACTPROB_IMPOSSIBLE
;
3078 if (!player_can_see_city_externals(unit_owner(actor_unit
), target_city
)) {
3079 /* The invisible city at this tile may, as far as the player knows, not
3081 return act_prob_unseen_target(action_id
, actor_unit
);
3084 target_building
= tgt_city_local_building(target_city
);
3085 target_utype
= tgt_city_local_utype(target_city
);
3087 return action_prob(action_id
,
3088 unit_owner(actor_unit
), tile_city(actor_tile
),
3089 NULL
, actor_tile
, actor_unit
, NULL
,
3091 city_owner(target_city
), target_city
,
3092 target_building
, city_tile(target_city
),
3093 NULL
, target_utype
, NULL
, NULL
);
3096 /**************************************************************************
3097 Get the actor unit's probability of successfully performing the chosen
3098 action on the target unit.
3099 **************************************************************************/
3100 struct act_prob
action_prob_vs_unit(const struct unit
* actor_unit
,
3101 const int action_id
,
3102 const struct unit
* target_unit
)
3104 struct tile
*actor_tile
= unit_tile(actor_unit
);
3106 if (actor_unit
== NULL
|| target_unit
== NULL
) {
3107 /* Can't do an action when actor or target are missing. */
3108 return ACTPROB_IMPOSSIBLE
;
3111 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(action_id
),
3113 "Action %s is performed by %s not %s",
3114 gen_action_name(action_id
),
3115 action_actor_kind_name(
3116 action_id_get_actor_kind(action_id
)),
3117 action_actor_kind_name(AAK_UNIT
));
3119 fc_assert_ret_val_msg(ATK_UNIT
== action_id_get_target_kind(action_id
),
3121 "Action %s is against %s not %s",
3122 gen_action_name(action_id
),
3123 action_target_kind_name(
3124 action_id_get_target_kind(action_id
)),
3125 action_target_kind_name(ATK_UNIT
));
3127 if (!unit_can_do_action(actor_unit
, action_id
)) {
3128 /* No point in continuing. */
3129 return ACTPROB_IMPOSSIBLE
;
3132 /* Doesn't leak information about unit position since an unseen unit can't
3134 if (!action_id_distance_accepted(action_id
,
3135 real_map_distance(unit_tile(actor_unit
),
3136 unit_tile(target_unit
)))) {
3137 /* No point in continuing. */
3138 return ACTPROB_IMPOSSIBLE
;
3141 return action_prob(action_id
,
3142 unit_owner(actor_unit
), tile_city(actor_tile
),
3143 NULL
, actor_tile
, actor_unit
, NULL
,
3145 unit_owner(target_unit
),
3146 tile_city(unit_tile(target_unit
)), NULL
,
3147 unit_tile(target_unit
),
3148 target_unit
, NULL
, NULL
, NULL
);
3151 /**************************************************************************
3152 Get the actor unit's probability of successfully performing the chosen
3153 action on all units at the target tile.
3154 **************************************************************************/
3155 struct act_prob
action_prob_vs_units(const struct unit
* actor_unit
,
3156 const int action_id
,
3157 const struct tile
* target_tile
)
3159 struct act_prob prob_all
;
3160 struct tile
*actor_tile
= unit_tile(actor_unit
);
3162 if (actor_unit
== NULL
|| target_tile
== NULL
) {
3163 /* Can't do an action when actor or target are missing. */
3164 return ACTPROB_IMPOSSIBLE
;
3167 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(action_id
),
3169 "Action %s is performed by %s not %s",
3170 gen_action_name(action_id
),
3171 action_actor_kind_name(
3172 action_id_get_actor_kind(action_id
)),
3173 action_actor_kind_name(AAK_UNIT
));
3175 fc_assert_ret_val_msg(ATK_UNITS
== action_id_get_target_kind(action_id
),
3177 "Action %s is against %s not %s",
3178 gen_action_name(action_id
),
3179 action_target_kind_name(
3180 action_id_get_target_kind(action_id
)),
3181 action_target_kind_name(ATK_UNITS
));
3183 if (!unit_can_do_action(actor_unit
, action_id
)) {
3184 /* No point in continuing. */
3185 return ACTPROB_IMPOSSIBLE
;
3188 /* Doesn't leak information about unit stack position since it is
3189 * specified as a tile and an unknown tile's position is known. */
3190 if (!action_id_distance_accepted(action_id
,
3191 real_map_distance(unit_tile(actor_unit
),
3193 /* No point in continuing. */
3194 return ACTPROB_IMPOSSIBLE
;
3197 /* Does the player know if there are units at the tile? Must be done here
3198 * since an empthy unseen tile will result in false. */
3199 if (!can_player_see_hypotetic_units_at(unit_owner(actor_unit
),
3201 /* Invisible units at this tile can make the action legal or
3203 return act_prob_unseen_target(action_id
, actor_unit
);
3204 } else if (unit_list_size(target_tile
->units
) == 0) {
3205 /* Known empty tile. */
3206 return ACTPROB_IMPOSSIBLE
;
3209 prob_all
= ACTPROB_CERTAIN
;
3210 unit_list_iterate(target_tile
->units
, target_unit
) {
3211 struct act_prob prob_unit
;
3213 prob_unit
= action_prob(action_id
,
3214 unit_owner(actor_unit
),
3215 tile_city(actor_tile
),
3216 NULL
, actor_tile
, actor_unit
, NULL
,
3218 unit_owner(target_unit
),
3219 tile_city(unit_tile(target_unit
)), NULL
,
3220 unit_tile(target_unit
),
3224 if (!action_prob_possible(prob_unit
)) {
3225 /* One unit makes it impossible for all units. */
3226 return ACTPROB_IMPOSSIBLE
;
3227 } else if (action_prob_not_impl(prob_unit
)) {
3228 /* Not implemented dominates all except impossible. */
3229 prob_all
= ACTPROB_NOT_IMPLEMENTED
;
3231 fc_assert_msg(!action_prob_is_signal(prob_unit
),
3232 "Invalid probability [%d, %d]",
3233 prob_unit
.min
, prob_unit
.max
);
3235 if (action_prob_is_signal(prob_all
)) {
3236 /* Special values dominate regular values. */
3240 /* Probability against all target units considered until this moment
3241 * and the probability against this target unit. */
3242 prob_all
.min
= (prob_all
.min
* prob_unit
.min
) / ACTPROB_VAL_MAX
;
3243 prob_all
.max
= (prob_all
.max
* prob_unit
.max
) / ACTPROB_VAL_MAX
;
3246 } unit_list_iterate_end
;
3248 /* Not impossible for any of the units at the tile. */
3252 /**************************************************************************
3253 Get the actor unit's probability of successfully performing the chosen
3254 action on the target tile.
3255 **************************************************************************/
3256 struct act_prob
action_prob_vs_tile(const struct unit
* actor_unit
,
3257 const int action_id
,
3258 const struct tile
* target_tile
)
3260 struct tile
*actor_tile
= unit_tile(actor_unit
);
3262 if (actor_unit
== NULL
|| target_tile
== NULL
) {
3263 /* Can't do an action when actor or target are missing. */
3264 return ACTPROB_IMPOSSIBLE
;
3267 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(action_id
),
3269 "Action %s is performed by %s not %s",
3270 gen_action_name(action_id
),
3271 action_actor_kind_name(
3272 action_id_get_actor_kind(action_id
)),
3273 action_actor_kind_name(AAK_UNIT
));
3275 fc_assert_ret_val_msg(ATK_TILE
== action_id_get_target_kind(action_id
),
3277 "Action %s is against %s not %s",
3278 gen_action_name(action_id
),
3279 action_target_kind_name(
3280 action_id_get_target_kind(action_id
)),
3281 action_target_kind_name(ATK_TILE
));
3283 if (!unit_can_do_action(actor_unit
, action_id
)) {
3284 /* No point in continuing. */
3285 return ACTPROB_IMPOSSIBLE
;
3288 /* Doesn't leak information about tile position since an unknown tile's
3289 * position is known. */
3290 if (!action_id_distance_accepted(action_id
,
3291 real_map_distance(unit_tile(actor_unit
),
3293 /* No point in continuing. */
3294 return ACTPROB_IMPOSSIBLE
;
3297 return action_prob(action_id
,
3298 unit_owner(actor_unit
), tile_city(actor_tile
),
3299 NULL
, actor_tile
, actor_unit
, NULL
,
3301 tile_owner(target_tile
), NULL
, NULL
,
3302 target_tile
, NULL
, NULL
, NULL
, NULL
);
3305 /**************************************************************************
3306 Get the actor unit's probability of successfully performing the chosen
3308 **************************************************************************/
3309 struct act_prob
action_prob_self(const struct unit
* actor_unit
,
3310 const int action_id
)
3312 struct tile
*actor_tile
= unit_tile(actor_unit
);
3314 if (actor_unit
== NULL
) {
3315 /* Can't do the action when the actor is missing. */
3316 return ACTPROB_IMPOSSIBLE
;
3319 /* No point in checking distance to target. It is always 0. */
3321 fc_assert_ret_val_msg(AAK_UNIT
== action_id_get_actor_kind(action_id
),
3323 "Action %s is performed by %s not %s",
3324 gen_action_name(action_id
),
3325 action_actor_kind_name(
3326 action_id_get_actor_kind(action_id
)),
3327 action_actor_kind_name(AAK_UNIT
));
3329 fc_assert_ret_val_msg(ATK_SELF
== action_id_get_target_kind(action_id
),
3331 "Action %s is against %s not %s",
3332 gen_action_name(action_id
),
3333 action_target_kind_name(
3334 action_id_get_target_kind(action_id
)),
3335 action_target_kind_name(ATK_SELF
));
3337 if (!unit_can_do_action(actor_unit
, action_id
)) {
3338 /* No point in continuing. */
3339 return ACTPROB_IMPOSSIBLE
;
3342 return action_prob(action_id
,
3343 unit_owner(actor_unit
), tile_city(actor_tile
),
3344 NULL
, actor_tile
, actor_unit
, NULL
,
3346 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
3349 /**************************************************************************
3350 Returns the impossible action probability.
3351 **************************************************************************/
3352 struct act_prob
action_prob_new_impossible(void)
3354 struct act_prob out
= { ACTPROB_VAL_MIN
, ACTPROB_VAL_MIN
};
3359 /**************************************************************************
3360 Returns the certain action probability.
3361 **************************************************************************/
3362 struct act_prob
action_prob_new_certain(void)
3364 struct act_prob out
= { ACTPROB_VAL_MAX
, ACTPROB_VAL_MAX
};
3369 /**************************************************************************
3370 Returns the n/a action probability.
3371 **************************************************************************/
3372 struct act_prob
action_prob_new_not_relevant(void)
3374 struct act_prob out
= { ACTPROB_VAL_NA
, ACTPROB_VAL_MIN
};
3379 /**************************************************************************
3380 Returns the "not implemented" action probability.
3381 **************************************************************************/
3382 struct act_prob
action_prob_new_not_impl(void)
3384 struct act_prob out
= { ACTPROB_VAL_NOT_IMPL
, ACTPROB_VAL_MIN
};
3389 /**************************************************************************
3390 Returns the "user don't know" action probability.
3391 **************************************************************************/
3392 struct act_prob
action_prob_new_unknown(void)
3394 struct act_prob out
= { ACTPROB_VAL_MIN
, ACTPROB_VAL_MAX
};
3399 /**************************************************************************
3400 Returns TRUE iff the given action probability belongs to an action that
3402 **************************************************************************/
3403 bool action_prob_possible(const struct act_prob probability
)
3405 return (ACTPROB_VAL_MIN
< probability
.max
3406 || action_prob_not_impl(probability
));
3409 /**************************************************************************
3410 Returns TRUE iff the given action probability represents the lack of
3411 an action probability.
3412 **************************************************************************/
3414 action_prob_not_relevant(const struct act_prob probability
)
3416 return probability
.min
== ACTPROB_VAL_NA
3417 && probability
.max
== ACTPROB_VAL_MIN
;
3420 /**************************************************************************
3421 Returns TRUE iff the given action probability represents that support
3422 for finding this action probability currently is missing from Freeciv.
3423 **************************************************************************/
3425 action_prob_not_impl(const struct act_prob probability
)
3427 return probability
.min
== ACTPROB_VAL_NOT_IMPL
3428 && probability
.max
== ACTPROB_VAL_MIN
;
3431 /**************************************************************************
3432 Returns TRUE iff the given action probability represents a special
3433 signal value rather than a regular action probability value.
3434 **************************************************************************/
3436 action_prob_is_signal(const struct act_prob probability
)
3438 return probability
.max
< probability
.min
;
3441 /**************************************************************************
3442 Returns TRUE iff ap1 and ap2 are equal.
3443 **************************************************************************/
3444 bool are_action_probabilitys_equal(const struct act_prob
*ap1
,
3445 const struct act_prob
*ap2
)
3447 return ap1
->min
== ap2
->min
&& ap1
->max
== ap2
->max
;
3450 /**************************************************************************
3451 Compare action probabilities. Prioritize the lowest possible value.
3452 **************************************************************************/
3453 int action_prob_cmp_pessimist(const struct act_prob ap1
,
3454 const struct act_prob ap2
)
3456 struct act_prob my_ap1
;
3457 struct act_prob my_ap2
;
3459 /* The action probabilities are real. */
3460 fc_assert(!action_prob_not_relevant(ap1
));
3461 fc_assert(!action_prob_not_relevant(ap2
));
3463 /* Convert any signals to ACTPROB_NOT_KNOWN. */
3464 if (action_prob_is_signal(ap1
)) {
3465 /* Assert that it is OK to convert the signal. */
3466 fc_assert(action_prob_not_impl(ap1
));
3468 my_ap1
= ACTPROB_NOT_KNOWN
;
3473 if (action_prob_is_signal(ap2
)) {
3474 /* Assert that it is OK to convert the signal. */
3475 fc_assert(action_prob_not_impl(ap2
));
3477 my_ap2
= ACTPROB_NOT_KNOWN
;
3482 /* The action probabilities now have a comparison friendly form. */
3483 fc_assert(!action_prob_is_signal(my_ap1
));
3484 fc_assert(!action_prob_is_signal(my_ap2
));
3486 /* Do the comparison. Start with min. Continue with max. */
3487 if (my_ap1
.min
< my_ap2
.min
) {
3489 } else if (my_ap1
.min
> my_ap2
.min
) {
3491 } else if (my_ap1
.max
< my_ap2
.max
) {
3493 } else if (my_ap1
.max
> my_ap2
.max
) {
3500 /**************************************************************************
3501 Returns double in the range [0-1] representing the minimum of the given
3503 **************************************************************************/
3504 double action_prob_to_0_to_1_pessimist(const struct act_prob ap
)
3506 struct act_prob my_ap
;
3508 /* The action probability is real. */
3509 fc_assert(!action_prob_not_relevant(ap
));
3511 /* Convert any signals to ACTPROB_NOT_KNOWN. */
3512 if (action_prob_is_signal(ap
)) {
3513 /* Assert that it is OK to convert the signal. */
3514 fc_assert(action_prob_not_impl(ap
));
3516 my_ap
= ACTPROB_NOT_KNOWN
;
3521 /* The action probability now has a math friendly form. */
3522 fc_assert(!action_prob_is_signal(my_ap
));
3524 return (double)my_ap
.min
/ (double) ACTPROB_VAL_MAX
;
3527 /**************************************************************************
3528 Returns ap1 with ap2 as fall back in cases where ap1 doesn't happen.
3529 Said in math that is: P(A) + P(A') * P(B)
3531 This is useful to calculate the probability of doing action A or, when A
3532 is impossible, falling back to doing action B.
3533 **************************************************************************/
3534 struct act_prob
action_prob_fall_back(const struct act_prob
*ap1
,
3535 const struct act_prob
*ap2
)
3537 struct act_prob my_ap1
;
3538 struct act_prob my_ap2
;
3539 struct act_prob out
;
3541 /* The action probabilities are real. */
3542 fc_assert(ap1
&& !action_prob_not_relevant(*ap1
));
3543 fc_assert(ap2
&& !action_prob_not_relevant(*ap2
));
3545 if (action_prob_is_signal(*ap1
)
3546 && are_action_probabilitys_equal(ap1
, ap2
)) {
3547 /* Keep the information rather than converting the signal to
3548 * ACTPROB_NOT_KNOWN. */
3550 /* Assert that it is OK to convert the signal. */
3551 fc_assert(action_prob_not_impl(*ap1
));
3559 /* Convert any signals to ACTPROB_NOT_KNOWN. */
3560 if (action_prob_is_signal(*ap1
)) {
3561 /* Assert that it is OK to convert the signal. */
3562 fc_assert(action_prob_not_impl(*ap1
));
3564 my_ap1
.min
= ACTPROB_VAL_MIN
;
3565 my_ap1
.max
= ACTPROB_VAL_MAX
;
3567 my_ap1
.min
= ap1
->min
;
3568 my_ap1
.max
= ap1
->max
;
3571 if (action_prob_is_signal(*ap2
)) {
3572 /* Assert that it is OK to convert the signal. */
3573 fc_assert(action_prob_not_impl(*ap2
));
3575 my_ap2
.min
= ACTPROB_VAL_MIN
;
3576 my_ap2
.max
= ACTPROB_VAL_MAX
;
3578 my_ap2
.min
= ap2
->min
;
3579 my_ap2
.max
= ap2
->max
;
3582 /* The action probabilities now have a math friendly form. */
3583 fc_assert(!action_prob_is_signal(my_ap1
));
3584 fc_assert(!action_prob_is_signal(my_ap2
));
3587 out
.min
= my_ap1
.min
+ (((ACTPROB_VAL_MAX
- my_ap1
.min
) * my_ap2
.min
)
3589 out
.max
= my_ap1
.max
+ (((ACTPROB_VAL_MAX
- my_ap1
.max
) * my_ap2
.max
)
3595 /**************************************************************************
3596 Will a player with the government gov be immune to the action act?
3597 **************************************************************************/
3598 bool action_immune_government(struct government
*gov
, int act
)
3600 /* Always immune since its not enabled. Doesn't count. */
3601 if (action_enabler_list_size(action_enablers_for_action(act
)) == 0) {
3605 action_enabler_list_iterate(action_enablers_for_action(act
), enabler
) {
3606 if (requirement_fulfilled_by_government(gov
, &(enabler
->target_reqs
))) {
3609 } action_enabler_list_iterate_end
;
3614 /**************************************************************************
3615 Returns TRUE if the specified action never can be performed when the
3616 situation requirement is fulfilled for the actor.
3617 **************************************************************************/
3618 bool action_blocked_by_situation_act(const struct action
*paction
,
3619 const struct requirement
*situation
)
3621 action_enabler_list_iterate(action_enablers_for_action(paction
->id
),
3623 if (!does_req_contradicts_reqs(situation
, &enabler
->actor_reqs
)) {
3626 } action_enabler_list_iterate_end
;
3631 /**************************************************************************
3632 Returns TRUE if the specified action never can be performed when the
3633 situation requirement is fulfilled for the target.
3634 **************************************************************************/
3635 bool action_blocked_by_situation_tgt(const struct action
*paction
,
3636 const struct requirement
*situation
)
3638 action_enabler_list_iterate(action_enablers_for_action(paction
->id
),
3640 if (!does_req_contradicts_reqs(situation
, &enabler
->target_reqs
)) {
3643 } action_enabler_list_iterate_end
;
3648 /**************************************************************************
3649 Returns TRUE if the wanted action can be done to the target.
3650 **************************************************************************/
3651 static bool is_target_possible(const enum gen_action wanted_action
,
3652 const struct player
*actor_player
,
3653 const struct player
*target_player
,
3654 const struct city
*target_city
,
3655 const struct impr_type
*target_building
,
3656 const struct tile
*target_tile
,
3657 const struct unit
*target_unit
,
3658 const struct unit_type
*target_unittype
,
3659 const struct output_type
*target_output
,
3660 const struct specialist
*target_specialist
)
3662 action_enabler_list_iterate(action_enablers_for_action(wanted_action
),
3664 if (are_reqs_active(target_player
, actor_player
, target_city
,
3665 target_building
, target_tile
,
3666 target_unit
, target_unittype
,
3667 target_output
, target_specialist
, NULL
,
3668 &enabler
->target_reqs
, RPT_POSSIBLE
)) {
3671 } action_enabler_list_iterate_end
;
3676 /**************************************************************************
3677 Returns TRUE if the wanted action can be done to the target city.
3678 **************************************************************************/
3679 bool is_action_possible_on_city(const enum gen_action action_id
,
3680 const struct player
*actor_player
,
3681 const struct city
* target_city
)
3683 fc_assert_ret_val_msg(ATK_CITY
== action_id_get_target_kind(action_id
),
3684 FALSE
, "Action %s is against %s not cities",
3685 gen_action_name(action_id
),
3686 action_target_kind_name(
3687 action_id_get_target_kind(action_id
)));
3689 return is_target_possible(action_id
, actor_player
,
3690 city_owner(target_city
), target_city
, NULL
,
3691 city_tile(target_city
), NULL
, NULL
,
3695 /**************************************************************************
3696 Returns TRUE if the wanted action (as far as the player knows) can be
3697 performed right now by the specified actor unit if an approriate target
3699 **************************************************************************/
3700 bool action_maybe_possible_actor_unit(const int action_id
,
3701 const struct unit
*actor_unit
)
3703 const struct player
*actor_player
= unit_owner(actor_unit
);
3704 const struct tile
*actor_tile
= unit_tile(actor_unit
);
3705 const struct city
*actor_city
= tile_city(actor_tile
);
3706 const struct unit_type
*actor_unittype
= unit_type_get(actor_unit
);
3708 enum fc_tristate result
;
3710 fc_assert_ret_val(actor_unit
, FALSE
);
3712 if (!utype_can_do_action(actor_unit
->utype
, action_id
)) {
3713 /* The unit type can't perform the action. */
3717 result
= action_hard_reqs_actor(action_id
,
3718 actor_player
, actor_city
, NULL
,
3719 actor_tile
, actor_unit
, actor_unittype
,
3721 game_city_by_number(actor_unit
->homecity
));
3723 if (result
== TRI_NO
) {
3724 /* The hard requirements aren't fulfilled. */
3728 action_enabler_list_iterate(action_enablers_for_action(action_id
),
3730 const enum fc_tristate current
3731 = mke_eval_reqs(actor_player
,
3732 actor_player
, NULL
, actor_city
, NULL
, actor_tile
,
3733 actor_unit
, NULL
, NULL
,
3734 &enabler
->actor_reqs
,
3735 /* Needed since no player to evaluate DiplRel
3736 * requirements against. */
3739 if (current
== TRI_YES
3740 || current
== TRI_MAYBE
) {
3741 /* The ruleset requirements may be fulfilled. */
3744 } action_enabler_list_iterate_end
;
3746 /* No action enabler allows this action. */
3750 /**************************************************************************
3751 Returns TRUE if the specified action can't be done now but would have
3752 been legal if the unit had full movement.
3753 **************************************************************************/
3754 bool action_mp_full_makes_legal(const struct unit
*actor
,
3755 const int action_id
)
3757 fc_assert(action_id_exists(action_id
) || action_id
== ACTION_ANY
);
3759 /* Check if full movement points may enable the specified action. */
3760 return !utype_may_act_move_frags(unit_type_get(actor
),
3763 && utype_may_act_move_frags(unit_type_get(actor
),
3765 unit_move_rate(actor
));
3768 /**************************************************************************
3769 Returns action auto performer rule slot number num so it can be filled.
3770 **************************************************************************/
3771 struct action_auto_perf
*action_auto_perf_slot_number(const int num
)
3773 fc_assert_ret_val(num
>= 0, NULL
);
3774 fc_assert_ret_val(num
< MAX_NUM_ACTION_AUTO_PERFORMERS
, NULL
);
3776 return &auto_perfs
[num
];
3779 /**************************************************************************
3780 Returns action auto performer rule number num.
3782 Used in action_auto_perf_iterate()
3784 WARNING: If the cause of the returned action performer rule is
3785 AAPC_COUNT it means that it is unused.
3786 **************************************************************************/
3787 const struct action_auto_perf
*action_auto_perf_by_number(const int num
)
3789 return action_auto_perf_slot_number(num
);