Move tools_fc_interface.[c|h] to tools/shared.
[freeciv.git] / common / actions.c
bloba7c955d3a4fc46e1152f5664d9cf2329c250b9b0
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)
6 any later version.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #include <math.h> /* ceil, floor */
20 /* utility */
21 #include "astring.h"
23 /* common */
24 #include "actions.h"
25 #include "city.h"
26 #include "combat.h"
27 #include "fc_interface.h"
28 #include "game.h"
29 #include "map.h"
30 #include "movement.h"
31 #include "unit.h"
32 #include "research.h"
33 #include "tile.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. */
42 bool is_target;
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
51 #include "specvec.h"
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,
86 bool rare_pop_up,
87 bool unitwaittime_controlled,
88 const int min_distance,
89 const int max_distance,
90 bool actor_consuming_always);
92 static bool is_enabler_active(const struct action_enabler *enabler,
93 const struct player *actor_player,
94 const struct city *actor_city,
95 const struct impr_type *actor_building,
96 const struct tile *actor_tile,
97 const struct unit *actor_unit,
98 const struct unit_type *actor_unittype,
99 const struct output_type *actor_output,
100 const struct specialist *actor_specialist,
101 const struct player *target_player,
102 const struct city *target_city,
103 const struct impr_type *target_building,
104 const struct tile *target_tile,
105 const struct unit *target_unit,
106 const struct unit_type *target_unittype,
107 const struct output_type *target_output,
108 const struct specialist *target_specialist);
110 static inline bool
111 action_prob_is_signal(const struct act_prob probability);
112 static inline bool
113 action_prob_not_relevant(const struct act_prob probability);
114 static inline bool
115 action_prob_not_impl(const struct act_prob probability);
117 /* Make sure that an action distance can target the whole map. */
118 FC_STATIC_ASSERT(MAP_DISTANCE_MAX <= ACTION_DISTANCE_LAST_NON_SIGNAL,
119 action_range_can_not_cover_the_whole_map);
121 /**************************************************************************
122 Register an obligatory hard requirement for the actions it applies to.
124 The vararg parameter is a list of action ids it applies to terminated
125 by ACTION_NONE.
126 **************************************************************************/
127 static void oblig_hard_req_register(struct requirement contradiction,
128 bool is_target,
129 const char *error_message,
130 ...)
132 struct obligatory_req oreq;
133 va_list args;
134 enum gen_action act;
136 /* A non null action message is used to indicate that an obligatory hard
137 * requirement is missing. */
138 fc_assert_ret(error_message);
140 /* Pack the obligatory hard requirement. */
141 oreq.contradiction = contradiction;
142 oreq.is_target = is_target;
143 oreq.error_msg = error_message;
145 /* Add the obligatory hard requirement to each action it applies to. */
146 va_start(args, error_message);
148 while (ACTION_NONE != (act = va_arg(args, enum gen_action))) {
149 /* Any invalid action result should terminate the loop before this
150 * assertion. */
151 fc_assert_ret_msg(gen_action_is_valid(act), "Invalid action id %d", act);
153 obligatory_req_vector_append(&obligatory_hard_reqs[act], oreq);
156 va_end(args);
159 /**************************************************************************
160 Hard code the obligatory hard requirements that don't depend on the rest
161 of the ruleset. They are sorted by requirement to make it easy to read,
162 modify and explain them.
163 **************************************************************************/
164 static void hard_code_oblig_hard_reqs(void)
166 /* Why this is a hard requirement: There is currently no point in
167 * allowing the listed actions against domestic targets.
168 * (Possible counter argument: crazy hack involving the Lua
169 * callback action_started_callback() to use an action to do
170 * something else. */
171 /* TODO: Unhardcode as a part of false flag operation support. */
172 oblig_hard_req_register(req_from_values(VUT_DIPLREL, REQ_RANGE_LOCAL,
173 FALSE, FALSE, TRUE, DRO_FOREIGN),
174 FALSE,
175 "All action enablers for %s must require a "
176 "foreign target.",
177 ACTION_ESTABLISH_EMBASSY,
178 ACTION_ESTABLISH_EMBASSY_STAY,
179 ACTION_SPY_INVESTIGATE_CITY,
180 ACTION_INV_CITY_SPEND,
181 ACTION_SPY_STEAL_GOLD,
182 ACTION_STEAL_MAPS,
183 ACTION_SPY_STEAL_TECH,
184 ACTION_SPY_TARGETED_STEAL_TECH,
185 ACTION_SPY_INCITE_CITY,
186 ACTION_SPY_INCITE_CITY_ESC,
187 ACTION_SPY_BRIBE_UNIT,
188 ACTION_CAPTURE_UNITS,
189 ACTION_CONQUER_CITY,
190 ACTION_NONE);
192 /* Why this is a hard requirement: there is a hard requirement that
193 * the actor player is at war with the owner of any city on the
194 * target tile. It can't move to the ruleset as long as Bombard is
195 * targeted at unit stacks only. Having the same requirement
196 * against each unit in the stack as against any city at the tile
197 * ensures compatibility with any future solution that allows the
198 * requirement against any city on the target tile to move to the
199 * ruleset. */
200 oblig_hard_req_register(req_from_values(VUT_DIPLREL, REQ_RANGE_LOCAL,
201 FALSE, FALSE, TRUE, DS_WAR),
202 FALSE,
203 "All action enablers for %s must require a "
204 "target the actor is at war with.",
205 ACTION_BOMBARD, ACTION_NONE);
207 /* Why this is a hard requirement: Keep the old rules. Need to work
208 * out corner cases. */
209 oblig_hard_req_register(req_from_values(VUT_DIPLREL, REQ_RANGE_LOCAL,
210 FALSE, TRUE, TRUE, DRO_FOREIGN),
211 FALSE,
212 "All action enablers for %s must require a "
213 "domestic target.",
214 ACTION_UPGRADE_UNIT, ACTION_NONE);
216 /* Why this is a hard requirement: Preserve semantics of NoHome
217 * flag. Need to replace other uses in game engine before this can
218 * be demoted to a regular unit flag. */
219 oblig_hard_req_register(req_from_values(VUT_UTFLAG, REQ_RANGE_LOCAL,
220 FALSE, TRUE, TRUE, UTYF_NOHOME),
221 FALSE,
222 "All action enablers for %s must require that "
223 "the actor doesn't have the NoHome utype flag.",
224 ACTION_HOME_CITY, ACTION_NONE);
226 /* Why this is a hard requirement: Preserve semantics of NonMil
227 * flag. Need to replace other uses in game engine before this can
228 * be demoted to a regular unit flag. */
229 oblig_hard_req_register(req_from_values(VUT_UTFLAG, REQ_RANGE_LOCAL,
230 FALSE, TRUE, TRUE, UTYF_CIVILIAN),
231 FALSE,
232 "All action enablers for %s must require that "
233 "the actor doesn't have the NonMil utype flag.",
234 ACTION_ATTACK, ACTION_CONQUER_CITY, ACTION_NONE);
236 /* Why this is a hard requirement: Preserve semantics of
237 * CanOccupyCity unit class flag. */
238 oblig_hard_req_register(req_from_values(VUT_UCFLAG, REQ_RANGE_LOCAL,
239 FALSE, FALSE, TRUE,
240 UCF_CAN_OCCUPY_CITY),
241 FALSE,
242 "All action enablers for %s must require that "
243 "the actor has the CanOccupyCity uclass flag.",
244 ACTION_CONQUER_CITY, ACTION_NONE);
246 /* Why this is a hard requirement: Consistency with ACTION_ATTACK.
247 * Assumed by other locations in the Freeciv code. Examples:
248 * unit_move_to_tile_test() and unit_conquer_city(). */
249 oblig_hard_req_register(req_from_values(VUT_DIPLREL, REQ_RANGE_LOCAL,
250 FALSE, FALSE, TRUE, DS_WAR),
251 FALSE,
252 "All action enablers for %s must require that "
253 "the actor is at war with the target.",
254 ACTION_CONQUER_CITY, ACTION_NONE);
256 /* Why this is a hard requirement: a unit must move into a city to
257 * conquer it. */
258 oblig_hard_req_register(req_from_values(VUT_MINMOVES, REQ_RANGE_LOCAL,
259 FALSE, FALSE, TRUE, 1),
260 FALSE,
261 "All action enablers for %s must require that "
262 "the actor has a movement point left.",
263 ACTION_CONQUER_CITY, ACTION_NONE);
265 /* Why this is a hard requirement: this eliminates the need to
266 * check if units transported by the actor unit can exist at the
267 * same tile as all the units in the occupied city.
269 * This makes an implicit rule explicit:
270 * 1. A unit must move into a city to conquer it.
271 * 2. It can't move into the city if the tile contains a non allied
272 * unit (see unit_move_to_tile_test()).
273 * 3. A city could, at the time this rule was made explicit, only
274 * contain units allied to its owner.
275 * 3. A player could, at the time this rule was made explicit, not
276 * be allied to a player that is at war with another ally.
277 * 4. A player could, at the time this rule was made explicit, only
278 * conquer a city belonging to someone he was at war with.
279 * Conclusion: the conquered city had to be empty.
281 oblig_hard_req_register(req_from_values(VUT_MAXTILEUNITS, REQ_RANGE_LOCAL,
282 FALSE, FALSE, TRUE, 0),
283 TRUE,
284 "All action enablers for %s must require that "
285 "the target city is empty.",
286 ACTION_CONQUER_CITY, ACTION_NONE);
288 /* Why this is a hard requirement: Assumed in the code. Corner case
289 * where diplomacy prevents a transported unit to go to the target
290 * tile. The paradrop code doesn't check if transported units can
291 * coexist with the target tile city and units. */
292 oblig_hard_req_register(req_from_values(VUT_UNITSTATE, REQ_RANGE_LOCAL,
293 FALSE, TRUE, TRUE,
294 USP_TRANSPORTING),
295 FALSE,
296 "All action enablers for %s must require that "
297 "the actor isn't transporting another unit.",
298 ACTION_PARADROP, ACTION_AIRLIFT, ACTION_NONE);
301 /**************************************************************************
302 Hard code the obligatory hard requirements that needs access to the
303 ruleset before they can be generated.
304 **************************************************************************/
305 static void hard_code_oblig_hard_reqs_ruleset(void)
307 /* Why this is a hard requirement: the "animal can't conquer a city"
308 * rule. Assumed in unit_can_take_over(). */
309 nations_iterate(pnation) {
310 if (nation_barbarian_type(pnation) == ANIMAL_BARBARIAN) {
311 oblig_hard_req_register(req_from_values(VUT_NATION, REQ_RANGE_PLAYER,
312 FALSE, TRUE, TRUE,
313 nation_number(pnation)),
314 TRUE,
315 "All action enablers for %s must require a "
316 "non animal player actor.",
317 ACTION_CONQUER_CITY, ACTION_NONE);
319 } nations_iterate_end;
322 /**************************************************************************
323 Hard code the actions.
324 **************************************************************************/
325 static void hard_code_actions(void)
327 actions[ACTION_SPY_POISON] = action_new(ACTION_SPY_POISON, ATK_CITY,
328 TRUE, FALSE, FALSE, TRUE,
329 0, 1, FALSE);
330 actions[ACTION_SPY_SABOTAGE_UNIT] =
331 action_new(ACTION_SPY_SABOTAGE_UNIT, ATK_UNIT,
332 TRUE, FALSE, FALSE, TRUE,
333 0, 1, FALSE);
334 actions[ACTION_SPY_BRIBE_UNIT] =
335 action_new(ACTION_SPY_BRIBE_UNIT, ATK_UNIT,
336 TRUE, FALSE, FALSE, TRUE,
337 0, 1, FALSE);
338 actions[ACTION_SPY_SABOTAGE_CITY] =
339 action_new(ACTION_SPY_SABOTAGE_CITY, ATK_CITY,
340 TRUE, FALSE, FALSE, TRUE,
341 0, 1, FALSE);
342 actions[ACTION_SPY_TARGETED_SABOTAGE_CITY] =
343 action_new(ACTION_SPY_TARGETED_SABOTAGE_CITY, ATK_CITY,
344 TRUE, TRUE, FALSE, TRUE,
345 0, 1, FALSE);
346 actions[ACTION_SPY_INCITE_CITY] =
347 action_new(ACTION_SPY_INCITE_CITY, ATK_CITY,
348 TRUE, FALSE, FALSE, TRUE,
349 0, 1, TRUE);
350 actions[ACTION_SPY_INCITE_CITY_ESC] =
351 action_new(ACTION_SPY_INCITE_CITY_ESC, ATK_CITY,
352 TRUE, FALSE, FALSE, TRUE,
353 0, 1, FALSE);
354 actions[ACTION_ESTABLISH_EMBASSY] =
355 action_new(ACTION_ESTABLISH_EMBASSY, ATK_CITY,
356 FALSE, FALSE, FALSE, TRUE,
357 0, 1, FALSE);
358 actions[ACTION_ESTABLISH_EMBASSY_STAY] =
359 action_new(ACTION_ESTABLISH_EMBASSY_STAY, ATK_CITY,
360 FALSE, FALSE, FALSE, TRUE,
361 0, 1, TRUE);
362 actions[ACTION_SPY_STEAL_TECH] =
363 action_new(ACTION_SPY_STEAL_TECH, ATK_CITY,
364 TRUE, FALSE, FALSE, TRUE,
365 0, 1, FALSE);
366 actions[ACTION_SPY_TARGETED_STEAL_TECH] =
367 action_new(ACTION_SPY_TARGETED_STEAL_TECH, ATK_CITY,
368 TRUE, TRUE, FALSE, TRUE,
369 0, 1, FALSE);
370 actions[ACTION_SPY_INVESTIGATE_CITY] =
371 action_new(ACTION_SPY_INVESTIGATE_CITY, ATK_CITY,
372 TRUE, FALSE, FALSE, TRUE,
373 0, 1, FALSE);
374 actions[ACTION_INV_CITY_SPEND] =
375 action_new(ACTION_INV_CITY_SPEND, ATK_CITY,
376 TRUE, FALSE, FALSE, TRUE,
377 0, 1, TRUE);
378 actions[ACTION_SPY_STEAL_GOLD] =
379 action_new(ACTION_SPY_STEAL_GOLD, ATK_CITY,
380 TRUE, FALSE, FALSE, TRUE,
381 0, 1, FALSE);
382 actions[ACTION_TRADE_ROUTE] =
383 action_new(ACTION_TRADE_ROUTE, ATK_CITY,
384 FALSE, FALSE, FALSE, TRUE,
385 0, 1, TRUE);
386 actions[ACTION_MARKETPLACE] =
387 action_new(ACTION_MARKETPLACE, ATK_CITY,
388 FALSE, FALSE, FALSE, TRUE,
389 0, 1, TRUE);
390 actions[ACTION_HELP_WONDER] =
391 action_new(ACTION_HELP_WONDER, ATK_CITY,
392 FALSE, FALSE, FALSE, TRUE,
393 0, 1, TRUE);
394 actions[ACTION_CAPTURE_UNITS] =
395 action_new(ACTION_CAPTURE_UNITS, ATK_UNITS,
396 TRUE, FALSE, FALSE, TRUE,
397 /* A single domestic unit at the target tile will make the
398 * action illegal. It must therefore be performed from
399 * another tile. */
400 1, 1,
401 FALSE);
402 actions[ACTION_FOUND_CITY] =
403 action_new(ACTION_FOUND_CITY, ATK_TILE,
404 FALSE, FALSE, TRUE, TRUE,
405 /* Illegal to perform to a target on another tile.
406 * Reason: The Freeciv code assumes that the city founding
407 * unit is located at the tile were the new city is
408 * founded. */
409 0, 0,
410 TRUE);
411 actions[ACTION_JOIN_CITY] =
412 action_new(ACTION_JOIN_CITY, ATK_CITY,
413 FALSE, FALSE, TRUE, TRUE,
414 0, 1, TRUE);
415 actions[ACTION_STEAL_MAPS] =
416 action_new(ACTION_STEAL_MAPS, ATK_CITY,
417 TRUE, FALSE, FALSE, TRUE,
418 0, 1, FALSE);
419 actions[ACTION_BOMBARD] =
420 action_new(ACTION_BOMBARD,
421 /* FIXME: Target is actually Units + City */
422 ATK_UNITS,
423 TRUE, FALSE, FALSE, TRUE,
424 /* A single domestic unit at the target tile will make the
425 * action illegal. It must therefore be performed from
426 * another tile. */
428 /* Overwritten by the ruleset's bombard_max_range */
430 FALSE);
431 actions[ACTION_SPY_NUKE] =
432 action_new(ACTION_SPY_NUKE, ATK_CITY,
433 TRUE, FALSE, FALSE, TRUE,
434 0, 1, TRUE);
435 actions[ACTION_SPY_NUKE_ESC] =
436 action_new(ACTION_SPY_NUKE_ESC, ATK_CITY,
437 TRUE, FALSE, FALSE, TRUE,
438 0, 1, FALSE);
439 actions[ACTION_NUKE] =
440 action_new(ACTION_NUKE,
441 /* FIXME: Target is actually Tile + Units + City */
442 ATK_TILE,
443 TRUE, FALSE, TRUE, TRUE,
444 0, 1, TRUE);
445 actions[ACTION_DESTROY_CITY] =
446 action_new(ACTION_DESTROY_CITY, ATK_CITY,
447 TRUE, FALSE, TRUE, TRUE,
448 0, 1, FALSE);
449 actions[ACTION_EXPEL_UNIT] =
450 action_new(ACTION_EXPEL_UNIT, ATK_UNIT,
451 TRUE, FALSE, FALSE, TRUE,
452 0, 1, FALSE);
453 actions[ACTION_RECYCLE_UNIT] =
454 action_new(ACTION_RECYCLE_UNIT, ATK_CITY,
455 FALSE, FALSE, TRUE, TRUE,
456 /* Illegal to perform to a target on another tile to
457 * keep the rules exactly as they were for now. */
458 0, 1,
459 TRUE);
460 actions[ACTION_DISBAND_UNIT] =
461 action_new(ACTION_DISBAND_UNIT, ATK_SELF,
462 FALSE, FALSE, TRUE, TRUE,
463 0, 0, TRUE);
464 actions[ACTION_HOME_CITY] =
465 action_new(ACTION_HOME_CITY, ATK_CITY,
466 FALSE, FALSE, TRUE, FALSE,
467 /* Illegal to perform to a target on another tile to
468 * keep the rules exactly as they were for now. */
469 0, 0, FALSE);
470 actions[ACTION_UPGRADE_UNIT] =
471 action_new(ACTION_UPGRADE_UNIT, ATK_CITY,
472 FALSE, FALSE, TRUE, TRUE,
473 /* Illegal to perform to a target on another tile to
474 * keep the rules exactly as they were for now. */
475 0, 0,
476 FALSE);
477 actions[ACTION_PARADROP] =
478 action_new(ACTION_PARADROP, ATK_TILE,
479 FALSE, FALSE, TRUE, TRUE,
481 /* Still limited by each unit type's paratroopers_range
482 * field. */
483 ACTION_DISTANCE_MAX,
484 FALSE);
485 actions[ACTION_AIRLIFT] =
486 action_new(ACTION_AIRLIFT, ATK_CITY,
487 FALSE, FALSE, TRUE, TRUE,
488 1, ACTION_DISTANCE_UNLIMITED,
489 FALSE);
490 actions[ACTION_ATTACK] =
491 action_new(ACTION_ATTACK,
492 /* FIXME: Target is actually City and, depending on the
493 * unreachable_protects setting, each unit at the target
494 * tile (Units) or any unit at the target tile. */
495 ATK_TILE,
496 TRUE, FALSE, FALSE, TRUE,
497 1, 1, FALSE);
498 actions[ACTION_CONQUER_CITY] =
499 action_new(ACTION_CONQUER_CITY, ATK_CITY,
500 TRUE, FALSE, FALSE, TRUE,
501 1, 1, FALSE);
502 actions[ACTION_HEAL_UNIT] =
503 action_new(ACTION_HEAL_UNIT, ATK_UNIT,
504 FALSE, FALSE, FALSE, TRUE,
505 0, 1, FALSE);
508 /**************************************************************************
509 Initialize the actions and the action enablers.
510 **************************************************************************/
511 void actions_init(void)
513 int i, j;
515 /* Hard code the actions */
516 hard_code_actions();
518 /* Initialize the action enabler list */
519 action_iterate(act) {
520 action_enablers_by_action[act] = action_enabler_list_new();
521 } action_iterate_end;
523 /* Initialize action obligatory hard requirements. */
525 /* Obligatory hard requirements are sorted by action in memory. This makes
526 * it easy to access the data. */
527 action_iterate(act) {
528 /* Prepare each action's storage area. */
529 obligatory_req_vector_init(&obligatory_hard_reqs[act]);
530 } action_iterate_end;
532 /* Obligatory hard requirements are sorted by requirement in the source
533 * code. This makes it easy to read, modify and explain it. */
534 hard_code_oblig_hard_reqs();
536 /* Initialize the action auto performers. */
537 for (i = 0; i < MAX_NUM_ACTION_AUTO_PERFORMERS; i++) {
538 /* Nothing here. Nothing after this point. */
539 auto_perfs[i].cause = AAPC_COUNT;
541 /* The criteria to pick *this* auto performer for its cause. */
542 requirement_vector_init(&auto_perfs[i].reqs);
544 for (j = 0; j < MAX_NUM_ACTIONS; j++) {
545 /* Nothing here. Nothing after this point. */
546 auto_perfs[i].alternatives[j] = ACTION_NONE;
550 /* The actions them self are now initialized. */
551 actions_initialized = TRUE;
554 /**************************************************************************
555 Generate action related data based on the currently loaded ruleset. Done
556 before ruleset sanity checking and ruleset compatibility post
557 processing.
558 **************************************************************************/
559 void actions_rs_pre_san_gen(void)
561 /* Some obligatory hard requirements needs access to the rest of the
562 * ruleset. */
563 hard_code_oblig_hard_reqs_ruleset();
566 /**************************************************************************
567 Free the actions and the action enablers.
568 **************************************************************************/
569 void actions_free(void)
571 int i;
573 /* Don't consider the actions to be initialized any longer. */
574 actions_initialized = FALSE;
576 action_iterate(act) {
577 action_enabler_list_iterate(action_enablers_by_action[act], enabler) {
578 requirement_vector_free(&enabler->actor_reqs);
579 requirement_vector_free(&enabler->target_reqs);
580 free(enabler);
581 } action_enabler_list_iterate_end;
583 action_enabler_list_destroy(action_enablers_by_action[act]);
585 FC_FREE(actions[act]);
586 } action_iterate_end;
588 /* Free the obligatory hard action requirements. */
589 action_iterate(act) {
590 obligatory_req_vector_free(&obligatory_hard_reqs[act]);
591 } action_iterate_end;
593 /* Free the action auto performers. */
594 for (i = 0; i < MAX_NUM_ACTION_AUTO_PERFORMERS; i++) {
595 requirement_vector_free(&auto_perfs[i].reqs);
599 /**************************************************************************
600 Returns TRUE iff the actions are initialized.
602 Doesn't care about action enablers.
603 **************************************************************************/
604 bool actions_are_ready(void)
606 if (!actions_initialized) {
607 /* The actions them self aren't initialized yet. */
608 return FALSE;
611 action_iterate(act) {
612 if (actions[act]->ui_name[0] == '\0') {
613 /* An action without a UI name exists means that the ruleset haven't
614 * loaded yet. The ruleset loading will assign a default name to
615 * any actions not named by the ruleset. The client will get this
616 * name from the server. */
617 return FALSE;
619 } action_iterate_end;
621 /* The actions should be ready for use. */
622 return TRUE;
625 /**************************************************************************
626 Create a new action.
627 **************************************************************************/
628 static struct action *action_new(enum gen_action id,
629 enum action_target_kind target_kind,
630 bool hostile, bool requires_details,
631 bool rare_pop_up,
632 bool unitwaittime_controlled,
633 const int min_distance,
634 const int max_distance,
635 bool actor_consuming_always)
637 struct action *action;
639 action = fc_malloc(sizeof(*action));
641 action->id = id;
642 action->actor_kind = AAK_UNIT;
643 action->target_kind = target_kind;
645 action->hostile = hostile;
646 action->requires_details = requires_details;
647 action->rare_pop_up = rare_pop_up;
649 /* The distance between the actor and itself is always 0. */
650 fc_assert(target_kind != ATK_SELF
651 || (min_distance == 0 && max_distance == 0));
653 action->min_distance = min_distance;
654 action->max_distance = max_distance;
656 action->unitwaittime_controlled = unitwaittime_controlled;
658 action->actor_consuming_always = actor_consuming_always;
660 /* Loaded from the ruleset. Until generalized actions are ready it has to
661 * be defined seperatly from other action data. */
662 action->ui_name[0] = '\0';
663 action->quiet = FALSE;
664 BV_CLR_ALL(action->blocked_by);
666 return action;
669 /**************************************************************************
670 Returns TRUE iff the specified action ID refers to a valid action.
671 **************************************************************************/
672 bool action_id_exists(const int action_id)
674 /* Actions are still hard coded. */
675 return gen_action_is_valid(action_id) && actions[action_id];
678 /**************************************************************************
679 Return the action with the given id.
681 Returns NULL if no action with the given id exists.
682 **************************************************************************/
683 struct action *action_by_number(int action_id)
685 if (!action_id_exists(action_id)) {
686 /* Nothing to return. */
688 log_verbose("Asked for non existing action numbered %d", action_id);
690 return NULL;
693 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
695 return actions[action_id];
698 /**************************************************************************
699 Return the action with the given name.
701 Returns NULL if no action with the given name exists.
702 **************************************************************************/
703 struct action *action_by_rule_name(const char *name)
705 /* Actions are still hard coded in the gen_action enum. */
706 int action_id = gen_action_by_name(name, fc_strcasecmp);
708 if (!action_id_exists(action_id)) {
709 /* Nothing to return. */
711 log_verbose("Asked for non existing action named %s", name);
713 return NULL;
716 return action_by_number(action_id);
719 /**************************************************************************
720 Get the actor kind of an action.
721 **************************************************************************/
722 enum action_actor_kind action_get_actor_kind(const struct action *paction)
724 fc_assert_ret_val_msg(paction, AAK_COUNT, "Action doesn't exist.");
726 return paction->actor_kind;
729 /**************************************************************************
730 Get the target kind of an action.
731 **************************************************************************/
732 enum action_target_kind action_get_target_kind(
733 const struct action *paction)
735 fc_assert_ret_val_msg(paction, ATK_COUNT, "Action doesn't exist.");
737 return paction->target_kind;
740 /**************************************************************************
741 Returns TRUE iff performing the specified action has the specified
742 result.
743 **************************************************************************/
744 bool action_has_result(const struct action *paction,
745 enum gen_action result)
747 /* The action result is currently used as the action id. */
748 return paction->id == result;
751 /**************************************************************************
752 Returns TRUE iff the specified action is hostile.
753 **************************************************************************/
754 bool action_is_hostile(int action_id)
756 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
758 return actions[action_id]->hostile;
761 /**************************************************************************
762 Returns TRUE iff the specified action REQUIRES the player to provide
763 details in addition to actor and target. Returns FALSE if the action
764 doesn't support any additional details or if they can be set by Freeciv
765 it self.
766 **************************************************************************/
767 bool action_requires_details(int action_id)
769 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
771 return actions[action_id]->requires_details;
774 /**************************************************************************
775 Returns TRUE iff a unit's ability to perform this action will pop up the
776 action selection dialog before the player asks for it only in exceptional
777 cases.
779 An example of an exceptional case is when the player tries to move a
780 unit to a tile it can't move to but can perform this action to.
781 **************************************************************************/
782 bool action_id_is_rare_pop_up(int action_id)
784 fc_assert_ret_val_msg((action_id_exists(action_id)),
785 FALSE, "Action %d don't exist.", action_id);
787 return actions[action_id]->rare_pop_up;
790 /**************************************************************************
791 Returns TRUE iff the specified distance between actor and target is
792 sm,aller or equal to the max range accepted by the specified action.
793 **************************************************************************/
794 bool action_distance_inside_max(const struct action *action,
795 const int distance)
797 return (distance <= action->max_distance
798 || action->max_distance == ACTION_DISTANCE_UNLIMITED);
801 /**************************************************************************
802 Returns TRUE iff the specified distance between actor and target is
803 within the range acceptable to the specified action.
804 **************************************************************************/
805 bool action_distance_accepted(const struct action *action,
806 const int distance)
808 fc_assert_ret_val(action, FALSE);
810 return (distance >= action->min_distance
811 && action_distance_inside_max(action, distance));
814 /**************************************************************************
815 Returns TRUE iff blocked will be illegal if blocker is legal.
816 **************************************************************************/
817 bool action_would_be_blocked_by(const struct action *blocked,
818 const struct action *blocker)
820 fc_assert_ret_val(blocked, FALSE);
821 fc_assert_ret_val(blocker, FALSE);
823 return BV_ISSET(blocked->blocked_by, action_number(blocker));
826 /**************************************************************************
827 Get the universal number of the action.
828 **************************************************************************/
829 int action_number(const struct action *action)
831 return action->id;
834 /**************************************************************************
835 Get the rule name of the action.
836 **************************************************************************/
837 const char *action_rule_name(const struct action *action)
839 /* Rule name is still hard coded. */
840 return action_id_rule_name(action->id);
843 /**************************************************************************
844 Get the action name used when displaying the action in the UI. Nothing
845 is added to the UI name.
846 **************************************************************************/
847 const char *action_name_translation(const struct action *action)
849 /* Use action_id_name_translation() to format the UI name. */
850 return action_id_name_translation(action->id);
853 /**************************************************************************
854 Get the rule name of the action.
855 **************************************************************************/
856 const char *action_id_rule_name(int action_id)
858 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
860 return gen_action_name(action_id);
863 /**************************************************************************
864 Get the action name used when displaying the action in the UI. Nothing
865 is added to the UI name.
866 **************************************************************************/
867 const char *action_id_name_translation(int action_id)
869 return action_prepare_ui_name(action_id, "", ACTPROB_NA, NULL);
872 /**************************************************************************
873 Get the action name with a mnemonic ready to display in the UI.
874 **************************************************************************/
875 const char *action_get_ui_name_mnemonic(int action_id,
876 const char* mnemonic)
878 return action_prepare_ui_name(action_id, mnemonic, ACTPROB_NA, NULL);
881 /**************************************************************************
882 Get the UI name ready to show the action in the UI. It is possible to
883 add a client specific mnemonic. Success probability information is
884 interpreted and added to the text. A custom text can be inserted before
885 the probability information.
886 **************************************************************************/
887 const char *action_prepare_ui_name(int action_id, const char* mnemonic,
888 const struct act_prob prob,
889 const char* custom)
891 static struct astring str = ASTRING_INIT;
892 static struct astring chance = ASTRING_INIT;
894 /* Text representation of the probability. */
895 const char* probtxt;
897 if (!actions_are_ready()) {
898 /* Could be a client who haven't gotten the ruleset yet */
900 /* so there shouldn't be any action probability to show */
901 fc_assert(action_prob_not_relevant(prob));
903 /* but the action should be valid */
904 fc_assert_ret_val_msg(action_id_exists(action_id),
905 "Invalid action",
906 "Invalid action %d", action_id);
908 /* and no custom text will be inserted */
909 fc_assert(custom == NULL || custom[0] == '\0');
911 /* Make the best of what is known */
912 astr_set(&str, _("%s%s (name may be wrong)"),
913 mnemonic, gen_action_name(action_id));
915 /* Return the guess. */
916 return astr_str(&str);
919 /* How to interpret action probabilities like prob is documented in
920 * fc_types.h */
921 if (action_prob_is_signal(prob)) {
922 fc_assert(action_prob_not_impl(prob)
923 || action_prob_not_relevant(prob));
925 /* Unknown because of missing server support or should not exits. */
926 probtxt = NULL;
927 } else {
928 if (prob.min == prob.max) {
929 /* Only one probability in range. */
931 /* TRANS: the probability that an action will succeed. Given in
932 * percentage. Resolution is 0.5%. */
933 astr_set(&chance, _("%.1f%%"), (double)prob.max / ACTPROB_VAL_1_PCT);
934 } else {
935 /* TRANS: the interval (end points included) where the probability of
936 * the action's success is. Given in percentage. Resolution is 0.5%. */
937 astr_set(&chance, _("[%.1f%%, %.1f%%]"),
938 (double)prob.min / ACTPROB_VAL_1_PCT,
939 (double)prob.max / ACTPROB_VAL_1_PCT);
941 probtxt = astr_str(&chance);
944 /* Format the info part of the action's UI name. */
945 if (probtxt != NULL && custom != NULL) {
946 /* TRANS: action UI name's info part with custom info and probability.
947 * Hint: you can move the paren handling from this sting to the action
948 * names if you need to add extra information (like a mnemonic letter
949 * that doesn't appear in the action UI name) to it. In that case you
950 * must do so for all strings with this comment and for every action
951 * name. To avoid a `()` when no UI name info part is added you have
952 * to add the extra information to every action name or remove the
953 * surrounding parens. */
954 astr_set(&chance, _(" (%s; %s)"), custom, probtxt);
955 } else if (probtxt != NULL) {
956 /* TRANS: action UI name's info part with probability.
957 * Hint: you can move the paren handling from this sting to the action
958 * names if you need to add extra information (like a mnemonic letter
959 * that doesn't appear in the action UI name) to it. In that case you
960 * must do so for all strings with this comment and for every action
961 * name. To avoid a `()` when no UI name info part is added you have
962 * to add the extra information to every action name or remove the
963 * surrounding parens. */
964 astr_set(&chance, _(" (%s)"), probtxt);
965 } else if (custom != NULL) {
966 /* TRANS: action UI name's info part with custom info.
967 * Hint: you can move the paren handling from this sting to the action
968 * names if you need to add extra information (like a mnemonic letter
969 * that doesn't appear in the action UI name) to it. In that case you
970 * must do so for all strings with this comment and for every action
971 * name. To avoid a `()` when no UI name info part is added you have
972 * to add the extra information to every action name or remove the
973 * surrounding parens. */
974 astr_set(&chance, _(" (%s)"), custom);
975 } else {
976 /* No info part to display. */
977 astr_clear(&chance);
980 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
982 astr_set(&str, _(actions[action_id]->ui_name), mnemonic,
983 astr_str(&chance));
985 return astr_str(&str);
988 /**************************************************************************
989 Get information about starting the action in the current situation.
990 Suitable for a tool tip for the button that starts it.
991 **************************************************************************/
992 const char *action_get_tool_tip(const int action_id,
993 const struct act_prob prob)
995 static struct astring tool_tip = ASTRING_INIT;
997 if (action_prob_is_signal(prob)) {
998 fc_assert(action_prob_not_impl(prob));
1000 /* Missing server support. No in game action will change this. */
1001 astr_clear(&tool_tip);
1002 } else if (prob.min == prob.max) {
1003 /* TRANS: action probability of success. Given in percentage.
1004 * Resolution is 0.5%. */
1005 astr_set(&tool_tip, _("The probability of success is %.1f%%."),
1006 (double)prob.max / ACTPROB_VAL_1_PCT);
1007 } else {
1008 astr_set(&tool_tip,
1009 /* TRANS: action probability interval (min to max). Given in
1010 * percentage. Resolution is 0.5%. The string at the end is
1011 * shown when the interval is wide enough to not be caused by
1012 * rounding. It explains that the interval is imprecise because
1013 * the player doesn't have enough information. */
1014 _("The probability of success is %.1f%%, %.1f%% or somewhere"
1015 " in between.%s"),
1016 (double)prob.min / ACTPROB_VAL_1_PCT,
1017 (double)prob.max / ACTPROB_VAL_1_PCT,
1018 prob.max - prob.min > 1 ?
1019 /* TRANS: explanation used in the action probability tooltip
1020 * above. */
1021 _(" (This is the most precise interval I can calculate "
1022 "given the information our nation has access to.)") :
1023 "");
1026 return astr_str(&tool_tip);
1029 /**************************************************************************
1030 Get the unit type role corresponding to the ability to do the specified
1031 action.
1032 **************************************************************************/
1033 int action_get_role(const struct action *paction)
1035 fc_assert_msg(AAK_UNIT == action_get_actor_kind(paction),
1036 "Action %s isn't performed by a unit",
1037 action_rule_name(paction));
1039 return paction->id + L_LAST;
1042 /**************************************************************************
1043 Create a new action enabler.
1044 **************************************************************************/
1045 struct action_enabler *action_enabler_new(void)
1047 struct action_enabler *enabler;
1049 enabler = fc_malloc(sizeof(*enabler));
1050 enabler->disabled = FALSE;
1051 requirement_vector_init(&enabler->actor_reqs);
1052 requirement_vector_init(&enabler->target_reqs);
1054 /* Make sure that action doesn't end up as a random value that happens to
1055 * be a valid action id. */
1056 enabler->action = ACTION_NONE;
1058 return enabler;
1061 /**************************************************************************
1062 Create a new copy of an existing action enabler.
1063 **************************************************************************/
1064 struct action_enabler *
1065 action_enabler_copy(const struct action_enabler *original)
1067 struct action_enabler *enabler = action_enabler_new();
1069 enabler->action = original->action;
1071 requirement_vector_copy(&enabler->actor_reqs, &original->actor_reqs);
1072 requirement_vector_copy(&enabler->target_reqs, &original->target_reqs);
1074 return enabler;
1077 /**************************************************************************
1078 Add an action enabler to the current ruleset.
1079 **************************************************************************/
1080 void action_enabler_add(struct action_enabler *enabler)
1082 /* Sanity check: a non existing action enabler can't be added. */
1083 fc_assert_ret(enabler);
1084 /* Sanity check: a non existing action doesn't have enablers. */
1085 fc_assert_ret(action_id_exists(enabler->action));
1087 action_enabler_list_append(
1088 action_enablers_for_action(enabler->action),
1089 enabler);
1092 /**************************************************************************
1093 Remove an action enabler from the current ruleset.
1095 Returns TRUE on success.
1096 **************************************************************************/
1097 bool action_enabler_remove(struct action_enabler *enabler)
1099 /* Sanity check: a non existing action enabler can't be removed. */
1100 fc_assert_ret_val(enabler, FALSE);
1101 /* Sanity check: a non existing action doesn't have enablers. */
1102 fc_assert_ret_val(action_id_exists(enabler->action), FALSE);
1104 return action_enabler_list_remove(
1105 action_enablers_for_action(enabler->action),
1106 enabler);
1109 /**************************************************************************
1110 Get all enablers for an action in the current ruleset.
1111 **************************************************************************/
1112 struct action_enabler_list *
1113 action_enablers_for_action(enum gen_action action)
1115 /* Sanity check: a non existing action doesn't have enablers. */
1116 fc_assert_ret_val(action_id_exists(action), NULL);
1118 return action_enablers_by_action[action];
1121 /**************************************************************************
1122 Returns an error message text if the action enabler is missing at least
1123 one of its action's obligatory hard requirement. Returns NULL if all
1124 obligatory hard requirements are there.
1126 An action may force its enablers to include one or more of its hard
1127 requirements. (See the section "Actions and their hard requirements" of
1128 doc/README.actions)
1130 This doesn't include those of the action's hard requirements that can't
1131 be expressed as a requirement vector or hard requirements that the
1132 action doesn't force enablers to include.
1133 **************************************************************************/
1134 const char *
1135 action_enabler_obligatory_reqs_missing(struct action_enabler *enabler)
1137 /* Sanity check: a non existing action enabler is missing but it doesn't
1138 * miss any obligatory hard requirements. */
1139 fc_assert_ret_val(enabler, NULL);
1141 /* Sanity check: a non existing action doesn't have any obligatory hard
1142 * requirements. */
1143 fc_assert_ret_val(action_id_exists(enabler->action), NULL);
1145 obligatory_req_vector_iterate(&obligatory_hard_reqs[enabler->action],
1146 obreq) {
1147 struct requirement_vector *ae_vec;
1149 /* Select action enabler requirement vector. */
1150 ae_vec = (obreq->is_target ? &enabler->target_reqs :
1151 &enabler->actor_reqs);
1153 if (!does_req_contradicts_reqs(&obreq->contradiction, ae_vec)) {
1154 /* Sanity check: doesn't return NULL when a problem is detected. */
1155 fc_assert_ret_val(obreq->error_msg,
1156 "Missing obligatory hard requirement for %s.");
1158 return obreq->error_msg;
1160 } obligatory_req_vector_iterate_end;
1162 /* No missing obligatory hard requirements. */
1163 return NULL;
1166 /**************************************************************************
1167 Inserts any missing obligatory hard requirements in the action enabler
1168 based on its action.
1170 See action_enabler_obligatory_reqs_missing()
1171 **************************************************************************/
1172 void action_enabler_obligatory_reqs_add(struct action_enabler *enabler)
1174 /* Sanity check: a non existing action enabler is missing but it doesn't
1175 * miss any obligatory hard requirements. */
1176 fc_assert_ret(enabler);
1178 /* Sanity check: a non existing action doesn't have any obligatory hard
1179 * requirements. */
1180 fc_assert_ret(action_id_exists(enabler->action));
1182 obligatory_req_vector_iterate(&obligatory_hard_reqs[enabler->action],
1183 obreq) {
1184 struct requirement_vector *ae_vec;
1186 /* Select action enabler requirement vector. */
1187 ae_vec = (obreq->is_target ? &enabler->target_reqs :
1188 &enabler->actor_reqs);
1190 if (!does_req_contradicts_reqs(&obreq->contradiction, ae_vec)) {
1191 struct requirement missing;
1193 /* Change the requirement from what should conflict to what is
1194 * wanted. */
1195 missing.present = !obreq->contradiction.present;
1196 missing.source = obreq->contradiction.source;
1197 missing.range = obreq->contradiction.range;
1198 missing.survives = obreq->contradiction.survives;
1199 missing.quiet = obreq->contradiction.quiet;
1201 /* Insert the missing requirement. */
1202 requirement_vector_append(ae_vec, missing);
1204 } obligatory_req_vector_iterate_end;
1206 /* Remove anything that conflicts with the newly added reqs. */
1207 requirement_vector_contradiction_clean(&enabler->actor_reqs);
1208 requirement_vector_contradiction_clean(&enabler->target_reqs);
1210 /* Sanity check: obligatory requirement insertion should have fixed the
1211 * action enabler. */
1212 fc_assert(action_enabler_obligatory_reqs_missing(enabler) == NULL);
1215 /**************************************************************************
1216 Returns TRUE iff the specified player knows (has seen) the specified
1217 tile.
1218 **************************************************************************/
1219 static bool plr_knows_tile(const struct player *plr,
1220 const struct tile *ttile)
1222 return plr && ttile && (tile_get_known(ttile, plr) != TILE_UNKNOWN);
1225 /**************************************************************************
1226 Returns TRUE iff the specified player can see the specified tile.
1227 **************************************************************************/
1228 static bool plr_sees_tile(const struct player *plr,
1229 const struct tile *ttile)
1231 return plr && ttile && (tile_get_known(ttile, plr) == TILE_KNOWN_SEEN);
1234 /**************************************************************************
1235 Returns the local building type of a city target.
1237 target_city can't be NULL
1238 **************************************************************************/
1239 static struct impr_type *
1240 tgt_city_local_building(const struct city *target_city)
1242 /* Only used with city targets */
1243 fc_assert_ret_val(target_city, NULL);
1245 if (target_city->production.kind == VUT_IMPROVEMENT) {
1246 /* The local building is what the target city currently is building. */
1247 return target_city->production.value.building;
1248 } else {
1249 /* In the current semantic the local building is always the building
1250 * being built. */
1251 /* TODO: Consider making the local building the target building for
1252 * actions that allows specifying a building target. */
1253 return NULL;
1257 /**************************************************************************
1258 Returns the local unit type of a city target.
1260 target_city can't be NULL
1261 **************************************************************************/
1262 static struct unit_type *
1263 tgt_city_local_utype(const struct city *target_city)
1265 /* Only used with city targets */
1266 fc_assert_ret_val(target_city, NULL);
1268 if (target_city->production.kind == VUT_UTYPE) {
1269 /* The local unit type is what the target city currently is
1270 * building. */
1271 return target_city->production.value.utype;
1272 } else {
1273 /* In the current semantic the local utype is always the type of the
1274 * unit being built. */
1275 return NULL;
1279 /**************************************************************************
1280 Returns the target tile for actions that may block the specified action.
1281 This is needed because some actions can be blocked by an action with a
1282 different target kind. The target tile could therefore be missing.
1284 Example: The ATK_SELF action ACTION_DISBAND_UNIT can be blocked by the
1285 ATK_CITY action ACTION_RECYCLE_UNIT.
1286 **************************************************************************/
1287 static const struct tile *
1288 blocked_find_target_tile(const int action_id,
1289 const struct unit *actor_unit,
1290 const struct tile *target_tile_arg,
1291 const struct city *target_city,
1292 const struct unit *target_unit)
1294 if (target_tile_arg != NULL) {
1295 /* Trust the caller. */
1296 return target_tile_arg;
1299 switch (action_id_get_target_kind(action_id)) {
1300 case ATK_CITY:
1301 fc_assert_ret_val(target_city, NULL);
1302 return city_tile(target_city);
1303 case ATK_UNIT:
1304 fc_assert_ret_val(target_unit, NULL);
1305 return unit_tile(target_unit);
1306 case ATK_UNITS:
1307 fc_assert_ret_val(target_unit || target_tile_arg, NULL);
1308 if (target_unit) {
1309 return unit_tile(target_unit);
1311 /* Fall through. */
1312 case ATK_TILE:
1313 fc_assert_ret_val(target_tile_arg, NULL);
1314 return target_tile_arg;
1315 case ATK_SELF:
1316 fc_assert_ret_val(actor_unit, NULL);
1317 return unit_tile(actor_unit);
1318 case ATK_COUNT:
1319 /* Handled below. */
1320 break;
1323 fc_assert_msg(FALSE, "Bad action target kind %d for action %d",
1324 action_id_get_target_kind(action_id), action_id);
1325 return NULL;
1328 /**************************************************************************
1329 Returns the target city for actions that may block the specified action.
1330 This is needed because some actions can be blocked by an action with a
1331 different target kind. The target city argument could therefore be
1332 missing.
1334 Example: The ATK_SELF action ACTION_DISBAND_UNIT can be blocked by the
1335 ATK_CITY action ACTION_RECYCLE_UNIT.
1336 **************************************************************************/
1337 static const struct city *
1338 blocked_find_target_city(const int action_id,
1339 const struct unit *actor_unit,
1340 const struct tile *target_tile,
1341 const struct city *target_city_arg,
1342 const struct unit *target_unit)
1344 if (target_city_arg != NULL) {
1345 /* Trust the caller. */
1346 return target_city_arg;
1349 switch (action_id_get_target_kind(action_id)) {
1350 case ATK_CITY:
1351 fc_assert_ret_val(target_city_arg, NULL);
1352 return target_city_arg;
1353 case ATK_UNIT:
1354 fc_assert_ret_val(target_unit, NULL);
1355 fc_assert_ret_val(unit_tile(target_unit), NULL);
1356 return tile_city(unit_tile(target_unit));
1357 case ATK_UNITS:
1358 fc_assert_ret_val(target_unit || target_tile, NULL);
1359 if (target_unit) {
1360 fc_assert_ret_val(unit_tile(target_unit), NULL);
1361 return tile_city(unit_tile(target_unit));
1363 /* Fall through. */
1364 case ATK_TILE:
1365 fc_assert_ret_val(target_tile, NULL);
1366 return tile_city(target_tile);
1367 case ATK_SELF:
1368 fc_assert_ret_val(actor_unit, NULL);
1369 fc_assert_ret_val(unit_tile(actor_unit), NULL);
1370 return tile_city(unit_tile(actor_unit));
1371 case ATK_COUNT:
1372 /* Handled below. */
1373 break;
1376 fc_assert_msg(FALSE, "Bad action target kind %d for action %d",
1377 action_id_get_target_kind(action_id), action_id);
1378 return NULL;
1381 /**************************************************************************
1382 Returns the action that blocks the specified action or NULL if the
1383 specified action isn't blocked.
1385 An action that can block another blocks when it is forced and possible.
1386 **************************************************************************/
1387 struct action *action_is_blocked_by(const int action_id,
1388 const struct unit *actor_unit,
1389 const struct tile *target_tile_arg,
1390 const struct city *target_city_arg,
1391 const struct unit *target_unit)
1395 const struct tile *target_tile
1396 = blocked_find_target_tile(action_id, actor_unit, target_tile_arg,
1397 target_city_arg, target_unit);
1398 const struct city *target_city
1399 = blocked_find_target_city(action_id, actor_unit, target_tile,
1400 target_city_arg, target_unit);
1402 action_iterate(blocker_id) {
1403 fc_assert_action(action_id_get_actor_kind(blocker_id) == AAK_UNIT,
1404 continue);
1406 if (!action_id_would_be_blocked_by(action_id, blocker_id)) {
1407 /* It doesn't matter if it is legal. It won't block the action. */
1408 continue;
1411 switch (action_id_get_target_kind(blocker_id)) {
1412 case ATK_CITY:
1413 if (!target_city) {
1414 /* Can't be enabled. No target. */
1415 continue;
1417 if (is_action_enabled_unit_on_city(blocker_id,
1418 actor_unit, target_city)) {
1419 return action_by_number(blocker_id);
1421 break;
1422 case ATK_UNIT:
1423 if (!target_unit) {
1424 /* Can't be enabled. No target. */
1425 continue;
1427 if (is_action_enabled_unit_on_unit(blocker_id,
1428 actor_unit, target_unit)) {
1429 return action_by_number(blocker_id);
1431 break;
1432 case ATK_UNITS:
1433 if (!target_tile) {
1434 /* Can't be enabled. No target. */
1435 continue;
1437 if (is_action_enabled_unit_on_units(blocker_id,
1438 actor_unit, target_tile)) {
1439 return action_by_number(blocker_id);
1441 break;
1442 case ATK_TILE:
1443 if (!target_tile) {
1444 /* Can't be enabled. No target. */
1445 continue;
1447 if (is_action_enabled_unit_on_tile(blocker_id,
1448 actor_unit, target_tile)) {
1449 return action_by_number(blocker_id);
1451 break;
1452 case ATK_SELF:
1453 if (is_action_enabled_unit_on_self(blocker_id, actor_unit)) {
1454 return action_by_number(blocker_id);
1456 break;
1457 case ATK_COUNT:
1458 fc_assert_action(action_id_get_target_kind(blocker_id) != ATK_COUNT,
1459 continue);
1460 break;
1462 } action_iterate_end;
1464 /* Not blocked. */
1465 return NULL;
1468 /**************************************************************************
1469 Returns TRUE if the specified unit type can perform the wanted action
1470 given that an action enabler later will enable it.
1472 This is done by checking the action's hard requirements. Hard
1473 requirements must be TRUE before an action can be done. The reason why
1474 is usually that code dealing with the action assumes that the
1475 requirements are true. A requirement may also end up here if it can't be
1476 expressed in a requirement vector or if its absence makes the action
1477 pointless.
1479 When adding a new hard requirement here:
1480 * explain why it is a hard requirement in a comment.
1481 **************************************************************************/
1482 bool
1483 action_actor_utype_hard_reqs_ok(const enum gen_action wanted_action,
1484 const struct unit_type *actor_unittype)
1486 switch (wanted_action) {
1487 case ACTION_JOIN_CITY:
1488 if (utype_pop_value(actor_unittype) <= 0) {
1489 /* Reason: Must have population to add. */
1490 return FALSE;
1492 break;
1494 case ACTION_BOMBARD:
1495 if (actor_unittype->bombard_rate <= 0) {
1496 /* Reason: Can't bombard if it never fires. */
1497 return FALSE;
1500 if (actor_unittype->attack_strength <= 0) {
1501 /* Reason: Can't bombard without attack strength. */
1502 return FALSE;
1505 break;
1507 case ACTION_UPGRADE_UNIT:
1508 if (actor_unittype->obsoleted_by == U_NOT_OBSOLETED) {
1509 /* Reason: Nothing to upgrade to. */
1510 return FALSE;
1512 break;
1514 case ACTION_ATTACK:
1515 if (actor_unittype->attack_strength <= 0) {
1516 /* Reason: Can't attack without strength. */
1517 return FALSE;
1519 break;
1521 case ACTION_ESTABLISH_EMBASSY:
1522 case ACTION_ESTABLISH_EMBASSY_STAY:
1523 case ACTION_SPY_INVESTIGATE_CITY:
1524 case ACTION_INV_CITY_SPEND:
1525 case ACTION_SPY_POISON:
1526 case ACTION_SPY_STEAL_GOLD:
1527 case ACTION_SPY_SABOTAGE_CITY:
1528 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
1529 case ACTION_SPY_STEAL_TECH:
1530 case ACTION_SPY_TARGETED_STEAL_TECH:
1531 case ACTION_SPY_INCITE_CITY:
1532 case ACTION_SPY_INCITE_CITY_ESC:
1533 case ACTION_TRADE_ROUTE:
1534 case ACTION_MARKETPLACE:
1535 case ACTION_HELP_WONDER:
1536 case ACTION_SPY_BRIBE_UNIT:
1537 case ACTION_SPY_SABOTAGE_UNIT:
1538 case ACTION_CAPTURE_UNITS:
1539 case ACTION_FOUND_CITY:
1540 case ACTION_STEAL_MAPS:
1541 case ACTION_SPY_NUKE:
1542 case ACTION_SPY_NUKE_ESC:
1543 case ACTION_NUKE:
1544 case ACTION_DESTROY_CITY:
1545 case ACTION_EXPEL_UNIT:
1546 case ACTION_RECYCLE_UNIT:
1547 case ACTION_DISBAND_UNIT:
1548 case ACTION_HOME_CITY:
1549 case ACTION_PARADROP:
1550 case ACTION_AIRLIFT:
1551 case ACTION_CONQUER_CITY:
1552 case ACTION_HEAL_UNIT:
1553 /* No hard unit type requirements. */
1554 break;
1556 case ACTION_COUNT:
1557 fc_assert_ret_val(wanted_action != ACTION_COUNT, FALSE);
1558 break;
1561 return TRUE;
1564 /**************************************************************************
1565 Returns TRUE iff the wanted action is possible as far as the actor is
1566 concerned given that an action enabler later will enable it. Will, unlike
1567 action_actor_utype_hard_reqs_ok(), check the actor unit's current state.
1569 Can return maybe when not omniscient. Should always return yes or no when
1570 omniscient.
1571 **************************************************************************/
1572 static enum fc_tristate
1573 action_hard_reqs_actor(const enum gen_action wanted_action,
1574 const struct player *actor_player,
1575 const struct city *actor_city,
1576 const struct impr_type *actor_building,
1577 const struct tile *actor_tile,
1578 const struct unit *actor_unit,
1579 const struct unit_type *actor_unittype,
1580 const struct output_type *actor_output,
1581 const struct specialist *actor_specialist,
1582 const bool omniscient,
1583 const struct city *homecity)
1585 if (!action_actor_utype_hard_reqs_ok(wanted_action, actor_unittype)) {
1586 /* Info leak: The actor player knows the type of his unit. */
1587 /* The actor unit type can't perform the action because of hard
1588 * unit type requirements. */
1589 return TRI_NO;
1592 switch (wanted_action) {
1593 case ACTION_TRADE_ROUTE:
1594 case ACTION_MARKETPLACE:
1595 /* It isn't possible to establish a trade route from a non existing
1596 * city. The Freeciv code assumes this applies to Enter Marketplace
1597 * too. */
1598 /* Info leak: The actor player knowns his unit's home city. */
1599 if (homecity == NULL) {
1600 return TRI_NO;
1603 break;
1605 case ACTION_PARADROP:
1606 /* Reason: Keep the old rules. */
1607 /* Info leak: The player knows if his unit already has paradropped this
1608 * turn. */
1609 if (actor_unit->paradropped) {
1610 return TRI_NO;
1613 /* Reason: Support the paratroopers_mr_req unit type field. */
1614 /* Info leak: The player knows how many move fragments his unit has
1615 * left. */
1616 if (actor_unit->moves_left < actor_unittype->paratroopers_mr_req) {
1617 return TRI_NO;
1620 break;
1622 case ACTION_AIRLIFT:
1624 const struct city *psrc_city = tile_city(actor_tile);
1626 if (psrc_city == NULL) {
1627 /* No city to airlift from. */
1628 return TRI_NO;
1631 if (actor_player != city_owner(psrc_city)
1632 && !(game.info.airlifting_style & AIRLIFTING_ALLIED_SRC
1633 && pplayers_allied(actor_player, city_owner(psrc_city)))) {
1634 /* Not allowed to airlift from this source. */
1635 return TRI_NO;
1638 if (!(omniscient || city_owner(psrc_city) == actor_player)) {
1639 /* Can't check for airlifting capacity. */
1640 return TRI_MAYBE;
1643 if (0 >= psrc_city->airlift) {
1644 /* The source cannot airlift for this turn (maybe already airlifted
1645 * or no airport).
1647 * Note that (game.info.airlifting_style & AIRLIFTING_UNLIMITED_SRC)
1648 * is not handled here because it always needs an airport to airlift.
1649 * See also do_airline() in server/unittools.h. */
1650 return TRI_NO;
1653 break;
1655 case ACTION_ESTABLISH_EMBASSY:
1656 case ACTION_ESTABLISH_EMBASSY_STAY:
1657 case ACTION_SPY_INVESTIGATE_CITY:
1658 case ACTION_INV_CITY_SPEND:
1659 case ACTION_SPY_POISON:
1660 case ACTION_SPY_STEAL_GOLD:
1661 case ACTION_SPY_SABOTAGE_CITY:
1662 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
1663 case ACTION_SPY_STEAL_TECH:
1664 case ACTION_SPY_TARGETED_STEAL_TECH:
1665 case ACTION_SPY_INCITE_CITY:
1666 case ACTION_SPY_INCITE_CITY_ESC:
1667 case ACTION_HELP_WONDER:
1668 case ACTION_SPY_BRIBE_UNIT:
1669 case ACTION_SPY_SABOTAGE_UNIT:
1670 case ACTION_CAPTURE_UNITS:
1671 case ACTION_FOUND_CITY:
1672 case ACTION_JOIN_CITY:
1673 case ACTION_STEAL_MAPS:
1674 case ACTION_BOMBARD:
1675 case ACTION_SPY_NUKE:
1676 case ACTION_SPY_NUKE_ESC:
1677 case ACTION_NUKE:
1678 case ACTION_DESTROY_CITY:
1679 case ACTION_EXPEL_UNIT:
1680 case ACTION_RECYCLE_UNIT:
1681 case ACTION_DISBAND_UNIT:
1682 case ACTION_HOME_CITY:
1683 case ACTION_UPGRADE_UNIT:
1684 case ACTION_ATTACK:
1685 case ACTION_CONQUER_CITY:
1686 case ACTION_HEAL_UNIT:
1687 /* No hard unit type requirements. */
1688 break;
1690 case ACTION_COUNT:
1691 fc_assert_ret_val(wanted_action != ACTION_COUNT, TRI_NO);
1692 break;
1695 return TRI_YES;
1698 /**************************************************************************
1699 Returns if the wanted action is possible given that an action enabler
1700 later will enable it.
1702 Can return maybe when not omniscient. Should always return yes or no when
1703 omniscient.
1705 This is done by checking the action's hard requirements. Hard
1706 requirements must be fulfilled before an action can be done. The reason
1707 why is usually that code dealing with the action assumes that the
1708 requirements are true. A requirement may also end up here if it can't be
1709 expressed in a requirement vector or if its absence makes the action
1710 pointless.
1712 When adding a new hard requirement here:
1713 * explain why it is a hard requirement in a comment.
1714 * remember that this is called from action_prob(). Should information
1715 the player don't have access to be used in a test it must check if
1716 the evaluation can see the thing being tested.
1717 **************************************************************************/
1718 static enum fc_tristate
1719 is_action_possible(const enum gen_action wanted_action,
1720 const struct player *actor_player,
1721 const struct city *actor_city,
1722 const struct impr_type *actor_building,
1723 const struct tile *actor_tile,
1724 const struct unit *actor_unit,
1725 const struct unit_type *actor_unittype,
1726 const struct output_type *actor_output,
1727 const struct specialist *actor_specialist,
1728 const struct player *target_player,
1729 const struct city *target_city,
1730 const struct impr_type *target_building,
1731 const struct tile *target_tile,
1732 const struct unit *target_unit,
1733 const struct unit_type *target_unittype,
1734 const struct output_type *target_output,
1735 const struct specialist *target_specialist,
1736 const bool omniscient,
1737 const struct city *homecity,
1738 bool ignore_dist)
1740 bool can_see_tgt_unit;
1741 bool can_see_tgt_tile;
1742 enum fc_tristate out;
1744 fc_assert_msg((action_id_get_target_kind(wanted_action) == ATK_CITY
1745 && target_city != NULL)
1746 || (action_id_get_target_kind(wanted_action) == ATK_TILE
1747 && target_tile != NULL)
1748 || (action_id_get_target_kind(wanted_action) == ATK_UNIT
1749 && target_unit != NULL)
1750 || (action_id_get_target_kind(wanted_action) == ATK_UNITS
1751 /* At this level each individual unit is tested. */
1752 && target_unit != NULL)
1753 || (action_id_get_target_kind(wanted_action) == ATK_SELF),
1754 "Missing target!");
1756 /* Only check requirement against the target unit if the actor can see it
1757 * or if the evaluator is omniscient. The game checking the rules is
1758 * omniscient. The player asking about his odds isn't. */
1759 can_see_tgt_unit = (omniscient || (target_unit
1760 && can_player_see_unit(actor_player,
1761 target_unit)));
1763 /* Only check requirement against the target tile if the actor can see it
1764 * or if the evaluator is omniscient. The game checking the rules is
1765 * omniscient. The player asking about his odds isn't. */
1766 can_see_tgt_tile = (omniscient
1767 || plr_sees_tile(actor_player, target_tile));
1769 /* Info leak: The player knows where his unit is. */
1770 if (!ignore_dist && action_id_get_target_kind(wanted_action) != ATK_SELF
1771 && !action_id_distance_accepted(wanted_action,
1772 real_map_distance(actor_tile,
1773 target_tile))) {
1774 /* The distance between the actor and the target isn't inside the
1775 * action's accepted range. */
1776 return TRI_NO;
1779 switch (action_id_get_target_kind(wanted_action)) {
1780 case ATK_UNIT:
1781 /* The Freeciv code for all actions that is controlled by action
1782 * enablers and targets a unit assumes that the acting
1783 * player can see the target unit.
1784 * Examples:
1785 * - action_prob_vs_unit()'s quick check that the distance between actor
1786 * and target is acceptable would leak distance to target unit if the
1787 * target unit can't be seen.
1789 if (!can_player_see_unit(actor_player, target_unit)) {
1790 return TRI_NO;
1792 break;
1793 case ATK_CITY:
1794 /* The Freeciv code assumes that the player is aware of the target
1795 * city's existence. (How can you order an airlift to a city when you
1796 * don't know its city ID?) */
1797 if (fc_funcs->player_tile_city_id_get(city_tile(target_city),
1798 actor_player)
1799 != target_city->id) {
1800 return TRI_NO;
1802 break;
1803 case ATK_UNITS:
1804 case ATK_TILE:
1805 case ATK_SELF:
1806 /* No special player knowledge checks. */
1807 break;
1808 case ATK_COUNT:
1809 fc_assert(action_id_get_target_kind(wanted_action) != ATK_COUNT);
1810 break;
1813 if (action_is_blocked_by(wanted_action, actor_unit,
1814 target_tile, target_city, target_unit)) {
1815 /* Allows an action to block an other action. If a blocking action is
1816 * legal the actions it blocks becomes illegal. */
1817 return TRI_NO;
1820 /* Actor specific hard requirements. */
1821 out = action_hard_reqs_actor(wanted_action,
1822 actor_player, actor_city, actor_building,
1823 actor_tile, actor_unit, actor_unittype,
1824 actor_output, actor_specialist,
1825 omniscient, homecity);
1827 if (out == TRI_NO) {
1828 /* Illegal because of a hard actor requirement. */
1829 return TRI_NO;
1832 /* Hard requirements for individual actions. */
1833 switch (wanted_action) {
1834 case ACTION_CAPTURE_UNITS:
1835 case ACTION_SPY_BRIBE_UNIT:
1836 /* Why this is a hard requirement: Can't transfer a unique unit if the
1837 * actor player already has one. */
1838 /* Info leak: This is only checked for when the actor player can see
1839 * the target unit. Since the target unit is seen its type is known.
1840 * The fact that a city hiding the unseen unit is occupied is known. */
1842 if (!can_see_tgt_unit) {
1843 /* An omniscient player can see the target unit. */
1844 fc_assert(!omniscient);
1846 return TRI_MAYBE;
1849 if (utype_player_already_has_this_unique(actor_player,
1850 target_unittype)) {
1851 return TRI_NO;
1854 /* FIXME: Capture Unit may want to look for more than one unique unit
1855 * of the same kind at the target tile. Currently caught by sanity
1856 * check in do_capture_units(). */
1858 break;
1860 case ACTION_ESTABLISH_EMBASSY:
1861 case ACTION_ESTABLISH_EMBASSY_STAY:
1862 /* Why this is a hard requirement: There is currently no point in
1863 * establishing an embassy when a real embassy already exists.
1864 * (Possible exception: crazy hack using the Lua callback
1865 * action_started_callback() to make establish embassy do something
1866 * else even if the UI still call the action Establish Embassy) */
1867 /* Info leak: The actor player known who he has a real embassy to. */
1868 if (player_has_real_embassy(actor_player, target_player)) {
1869 return TRI_NO;
1872 break;
1874 case ACTION_SPY_TARGETED_STEAL_TECH:
1875 /* Reason: The Freeciv code don't support selecting a target tech
1876 * unless it is known that the victim player has it. */
1877 /* Info leak: The actor player knowns who's techs he can see. */
1878 if (!can_see_techs_of_target(actor_player, target_player)) {
1879 return TRI_NO;
1882 break;
1884 case ACTION_SPY_STEAL_GOLD:
1885 /* If actor_unit can do the action the actor_player can see how much
1886 * gold target_player have. Not requireing it is therefore pointless.
1888 if (target_player->economic.gold <= 0) {
1889 return TRI_NO;
1892 break;
1894 case ACTION_TRADE_ROUTE:
1895 case ACTION_MARKETPLACE:
1897 /* Checked in action_hard_reqs_actor() */
1898 fc_assert_ret_val(homecity != NULL, TRI_NO);
1900 /* Can't establish a trade route or enter the market place if the
1901 * cities can't trade at all. */
1902 /* TODO: Should this restriction (and the above restriction that the
1903 * actor unit must have a home city) be kept for Enter Marketplace? */
1904 if (!can_cities_trade(homecity, target_city)) {
1905 return TRI_NO;
1908 /* There are more restrictions on establishing a trade route than on
1909 * entering the market place. */
1910 if (wanted_action == ACTION_TRADE_ROUTE
1911 && !can_establish_trade_route(homecity, target_city)) {
1912 return TRI_NO;
1916 break;
1918 case ACTION_HELP_WONDER:
1919 case ACTION_RECYCLE_UNIT:
1920 /* It is only possible to help the production if the production needs
1921 * the help. (If not it would be possible to add shields for something
1922 * that can't legally receive help if it is build later) */
1923 /* Info leak: The player knows that the production in his own city has
1924 * been hurried (bought or helped). The information isn't revealed when
1925 * asking for action probabilities since omniscient is FALSE. */
1926 if (!omniscient
1927 && !can_player_see_city_internals(actor_player, target_city)) {
1928 return TRI_MAYBE;
1931 if (!(target_city->shield_stock
1932 < city_production_build_shield_cost(target_city))) {
1933 return TRI_NO;
1936 break;
1938 case ACTION_FOUND_CITY:
1939 if (game.scenario.prevent_new_cities) {
1940 /* Reason: allow scenarios to disable city founding. */
1941 /* Info leak: the setting is public knowledge. */
1942 return TRI_NO;
1945 if (can_see_tgt_tile && tile_city(target_tile)) {
1946 /* Reason: a tile can have 0 or 1 cities. */
1947 return TRI_NO;
1950 switch (city_build_here_test(target_tile, actor_unit)) {
1951 case CB_OK:
1952 /* If the player knows this is checked below. */
1953 break;
1954 case CB_BAD_CITY_TERRAIN:
1955 case CB_BAD_UNIT_TERRAIN:
1956 case CB_BAD_BORDERS:
1957 if (can_see_tgt_tile) {
1958 /* Known to be blocked. Target tile is seen. */
1959 return TRI_NO;
1961 break;
1962 case CB_NO_MIN_DIST:
1963 if (omniscient) {
1964 /* No need to check again. */
1965 return TRI_NO;
1966 } else {
1967 square_iterate(&(wld.map), target_tile,
1968 game.info.citymindist - 1, otile) {
1969 if (tile_city(otile) != NULL
1970 && plr_sees_tile(actor_player, otile)) {
1971 /* Known to be blocked by citymindist */
1972 return TRI_NO;
1974 } square_iterate_end;
1976 break;
1979 /* The player may not have enough information to be certain. */
1981 if (!can_see_tgt_tile) {
1982 /* Need to know if target tile already has a city, has TER_NO_CITIES
1983 * terrain, is non native to the actor or is owned by a foreigner. */
1984 return TRI_MAYBE;
1987 if (!omniscient) {
1988 /* The player may not have enough information to find out if
1989 * citymindist blocks or not. This doesn't depend on if it blocks. */
1990 square_iterate(&(wld.map), target_tile,
1991 game.info.citymindist - 1, otile) {
1992 if (!plr_sees_tile(actor_player, otile)) {
1993 /* Could have a city that blocks via citymindist. Even if this
1994 * tile has TER_NO_CITIES terrain the player don't know that it
1995 * didn't change and had a city built on it. */
1996 return TRI_MAYBE;
1998 } square_iterate_end;
2001 break;
2003 case ACTION_JOIN_CITY:
2005 int new_pop;
2007 if (!omniscient
2008 && !player_can_see_city_externals(actor_player, target_city)) {
2009 return TRI_MAYBE;
2012 new_pop = city_size_get(target_city) + unit_pop_value(actor_unit);
2014 if (new_pop > game.info.add_to_size_limit) {
2015 /* Reason: Make the add_to_size_limit setting work. */
2016 return TRI_NO;
2019 if (!city_can_grow_to(target_city, new_pop)) {
2020 /* Reason: respect city size limits. */
2021 /* Info leak: when it is legal to join a foreign city is legal and
2022 * the EFT_SIZE_UNLIMIT effect or the EFT_SIZE_ADJ effect depends on
2023 * something the actor player don't have access to.
2024 * Example: depends on a building (like Aqueduct) that isn't
2025 * VisibleByOthers. */
2026 return TRI_NO;
2030 break;
2032 case ACTION_BOMBARD:
2033 /* FIXME: Target of Bombard should be city and units. */
2034 if (tile_city(target_tile)
2035 && !pplayers_at_war(city_owner(tile_city(target_tile)),
2036 actor_player)) {
2037 return TRI_NO;
2040 break;
2042 case ACTION_NUKE:
2043 if (actor_tile != target_tile) {
2044 /* The old rules only restricted other tiles. Keep them for now. */
2046 struct city *tcity;
2048 if (actor_unit->moves_left <= 0) {
2049 return TRI_NO;
2052 if (!(tcity = tile_city(target_tile))
2053 && !unit_list_size(target_tile->units)) {
2054 return TRI_NO;
2057 if (tcity && !pplayers_at_war(city_owner(tcity), actor_player)) {
2058 return TRI_NO;
2061 if (is_non_attack_unit_tile(target_tile, actor_player)) {
2062 return TRI_NO;
2065 if (!tcity
2066 && (unit_attack_units_at_tile_result(actor_unit, target_tile)
2067 != ATT_OK)) {
2068 return TRI_NO;
2072 break;
2074 case ACTION_HOME_CITY:
2075 /* Reason: can't change to what is. */
2076 /* Info leak: The player knows his unit's current home city. */
2077 if (homecity != NULL && homecity->id == target_city->id) {
2078 /* This is already the unit's home city. */
2079 return TRI_NO;
2083 int slots = unit_type_get(actor_unit)->city_slots;
2085 if (slots > 0 && city_unit_slots_available(target_city) < slots) {
2086 return TRI_NO;
2090 break;
2092 case ACTION_UPGRADE_UNIT:
2093 /* Reason: Keep the old rules. */
2094 /* Info leak: The player knows his unit's type. He knows if he can
2095 * build the unit type upgraded to. If the upgrade happens in a foreign
2096 * city that fact may leak. This can be seen as a price for changing
2097 * the rules to allow upgrading in a foreign city.
2098 * The player knows how much gold he has. If the Upgrade_Price_Pct
2099 * effect depends on information he don't have that information may
2100 * leak. The player knows the location of his unit. He knows if the
2101 * tile has a city and if the unit can exist there outside a transport.
2102 * The player knows his unit's cargo. By knowing their number and type
2103 * he can predict if there will be room for them in the unit upgraded
2104 * to as long as he knows what unit type his unit will end up as. */
2105 if (unit_upgrade_test(actor_unit, FALSE) != UU_OK) {
2106 return TRI_NO;
2109 break;
2111 case ACTION_PARADROP:
2112 /* Reason: Keep the old rules. */
2113 /* Info leak: The player knows if he knows the target tile. */
2114 if (!plr_knows_tile(actor_player, target_tile)) {
2115 return TRI_NO;
2118 /* Reason: Keep paratroopers_range working. */
2119 /* Info leak: The player knows the location of the actor and of the
2120 * target tile. */
2121 if (!ignore_dist
2122 && (unit_type_get(actor_unit)->paratroopers_range
2123 < real_map_distance(actor_tile, target_tile))) {
2124 return TRI_NO;
2127 break;
2129 case ACTION_AIRLIFT:
2130 /* Reason: Keep the old rules. */
2131 /* Info leak: same as test_unit_can_airlift_to() */
2132 switch (test_unit_can_airlift_to(omniscient ? NULL : actor_player,
2133 actor_unit, target_city)) {
2134 case AR_OK:
2135 return TRI_YES;
2136 case AR_OK_SRC_UNKNOWN:
2137 case AR_OK_DST_UNKNOWN:
2138 return TRI_MAYBE;
2139 case AR_NO_MOVES:
2140 case AR_WRONG_UNITTYPE:
2141 case AR_OCCUPIED:
2142 case AR_NOT_IN_CITY:
2143 case AR_BAD_SRC_CITY:
2144 case AR_BAD_DST_CITY:
2145 case AR_SRC_NO_FLIGHTS:
2146 case AR_DST_NO_FLIGHTS:
2147 return TRI_NO;
2150 break;
2152 case ACTION_ATTACK:
2153 /* Reason: must have a unit to attack. */
2154 if (unit_list_size(target_tile->units) <= 0) {
2155 return TRI_NO;
2158 /* Reason: Keep the old rules. */
2159 if (!can_unit_attack_tile(actor_unit, target_tile)) {
2160 return TRI_NO;
2162 break;
2164 case ACTION_CONQUER_CITY:
2165 /* Reason: "Conquer City" involves moving into the city. */
2166 if (!unit_can_move_to_tile(&(wld.map), actor_unit, target_tile,
2167 FALSE, TRUE)) {
2168 return TRI_NO;
2171 break;
2173 case ACTION_HEAL_UNIT:
2174 /* Reason: It is not the healthy who need a doctor, but the sick. */
2175 /* Info leak: the actor can see the target's HP. */
2176 if (!(target_unit->hp < target_unittype->hp)) {
2177 return TRI_NO;
2179 break;
2181 case ACTION_SPY_INVESTIGATE_CITY:
2182 case ACTION_INV_CITY_SPEND:
2183 case ACTION_SPY_POISON:
2184 case ACTION_SPY_SABOTAGE_CITY:
2185 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
2186 case ACTION_SPY_STEAL_TECH:
2187 case ACTION_SPY_INCITE_CITY:
2188 case ACTION_SPY_INCITE_CITY_ESC:
2189 case ACTION_SPY_SABOTAGE_UNIT:
2190 case ACTION_STEAL_MAPS:
2191 case ACTION_SPY_NUKE:
2192 case ACTION_SPY_NUKE_ESC:
2193 case ACTION_DESTROY_CITY:
2194 case ACTION_EXPEL_UNIT:
2195 case ACTION_DISBAND_UNIT:
2196 /* No known hard coded requirements. */
2197 break;
2198 case ACTION_COUNT:
2199 fc_assert(action_id_exists(wanted_action));
2200 break;
2203 return out;
2206 /**************************************************************************
2207 Return TRUE iff the action enabler is active
2208 **************************************************************************/
2209 static bool is_enabler_active(const struct action_enabler *enabler,
2210 const struct player *actor_player,
2211 const struct city *actor_city,
2212 const struct impr_type *actor_building,
2213 const struct tile *actor_tile,
2214 const struct unit *actor_unit,
2215 const struct unit_type *actor_unittype,
2216 const struct output_type *actor_output,
2217 const struct specialist *actor_specialist,
2218 const struct player *target_player,
2219 const struct city *target_city,
2220 const struct impr_type *target_building,
2221 const struct tile *target_tile,
2222 const struct unit *target_unit,
2223 const struct unit_type *target_unittype,
2224 const struct output_type *target_output,
2225 const struct specialist *target_specialist)
2227 return are_reqs_active(actor_player, target_player, actor_city,
2228 actor_building, actor_tile,
2229 actor_unit, actor_unittype,
2230 actor_output, actor_specialist, NULL,
2231 &enabler->actor_reqs, RPT_CERTAIN)
2232 && are_reqs_active(target_player, actor_player, target_city,
2233 target_building, target_tile,
2234 target_unit, target_unittype,
2235 target_output, target_specialist, NULL,
2236 &enabler->target_reqs, RPT_CERTAIN);
2239 /**************************************************************************
2240 Returns TRUE if the wanted action is enabled.
2242 Note that the action may disable it self because of hard requirements
2243 even if an action enabler returns TRUE.
2244 **************************************************************************/
2245 static bool is_action_enabled(const enum gen_action wanted_action,
2246 const struct player *actor_player,
2247 const struct city *actor_city,
2248 const struct impr_type *actor_building,
2249 const struct tile *actor_tile,
2250 const struct unit *actor_unit,
2251 const struct unit_type *actor_unittype,
2252 const struct output_type *actor_output,
2253 const struct specialist *actor_specialist,
2254 const struct player *target_player,
2255 const struct city *target_city,
2256 const struct impr_type *target_building,
2257 const struct tile *target_tile,
2258 const struct unit *target_unit,
2259 const struct unit_type *target_unittype,
2260 const struct output_type *target_output,
2261 const struct specialist *target_specialist,
2262 const struct city *homecity, bool ignore_dist)
2264 enum fc_tristate possible;
2266 possible = is_action_possible(wanted_action,
2267 actor_player, actor_city,
2268 actor_building, actor_tile,
2269 actor_unit, actor_unittype,
2270 actor_output, actor_specialist,
2271 target_player, target_city,
2272 target_building, target_tile,
2273 target_unit, target_unittype,
2274 target_output, target_specialist,
2275 TRUE, homecity, ignore_dist);
2277 if (possible != TRI_YES) {
2278 /* This context is omniscient. Should be yes or no. */
2279 fc_assert_msg(possible != TRI_MAYBE,
2280 "Is omniscient, should get yes or no.");
2282 /* The action enablers are irrelevant since the action it self is
2283 * impossible. */
2284 return FALSE;
2287 action_enabler_list_iterate(action_enablers_for_action(wanted_action),
2288 enabler) {
2289 if (is_enabler_active(enabler, actor_player, actor_city,
2290 actor_building, actor_tile,
2291 actor_unit, actor_unittype,
2292 actor_output, actor_specialist,
2293 target_player, target_city,
2294 target_building, target_tile,
2295 target_unit, target_unittype,
2296 target_output, target_specialist)) {
2297 return TRUE;
2299 } action_enabler_list_iterate_end;
2301 return FALSE;
2304 /**************************************************************************
2305 Returns TRUE if actor_unit can do wanted_action to target_city as far as
2306 action enablers are concerned.
2308 See note in is_action_enabled for why the action may still be disabled.
2309 **************************************************************************/
2310 bool is_action_enabled_unit_on_city(const enum gen_action wanted_action,
2311 const struct unit *actor_unit,
2312 const struct city *target_city)
2314 return is_action_enabled_unit_on_city_full(wanted_action, actor_unit,
2315 target_city,
2316 game_city_by_number(actor_unit->homecity),
2317 FALSE);
2320 /**************************************************************************
2321 Returns TRUE if actor_unit can do wanted_action to target_city 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_city_full(const enum gen_action wanted_action,
2327 const struct unit *actor_unit,
2328 const struct city *target_city,
2329 const struct city *homecity,
2330 bool ignore_dist)
2332 struct tile *actor_tile = unit_tile(actor_unit);
2333 struct impr_type *target_building;
2334 struct unit_type *target_utype;
2336 if (actor_unit == NULL || target_city == NULL) {
2337 /* Can't do an action when actor or target are missing. */
2338 return FALSE;
2341 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(wanted_action),
2342 FALSE, "Action %s is performed by %s not %s",
2343 gen_action_name(wanted_action),
2344 action_actor_kind_name(
2345 action_id_get_actor_kind(wanted_action)),
2346 action_actor_kind_name(AAK_UNIT));
2348 fc_assert_ret_val_msg(ATK_CITY
2349 == action_id_get_target_kind(wanted_action),
2350 FALSE, "Action %s is against %s not %s",
2351 gen_action_name(wanted_action),
2352 action_target_kind_name(
2353 action_id_get_target_kind(wanted_action)),
2354 action_target_kind_name(ATK_CITY));
2356 if (!unit_can_do_action(actor_unit, wanted_action)) {
2357 /* No point in continuing. */
2358 return FALSE;
2361 target_building = tgt_city_local_building(target_city);
2362 target_utype = tgt_city_local_utype(target_city);
2364 return is_action_enabled(wanted_action,
2365 unit_owner(actor_unit), tile_city(actor_tile),
2366 NULL, actor_tile,
2367 actor_unit, unit_type_get(actor_unit),
2368 NULL, NULL,
2369 city_owner(target_city), target_city,
2370 target_building, city_tile(target_city),
2371 NULL, target_utype, NULL, NULL, homecity,
2372 ignore_dist);
2375 /**************************************************************************
2376 Returns TRUE if actor_unit can do wanted_action to target_unit as far as
2377 action enablers are concerned.
2379 See note in is_action_enabled for why the action may still be disabled.
2380 **************************************************************************/
2381 bool is_action_enabled_unit_on_unit(const enum gen_action wanted_action,
2382 const struct unit *actor_unit,
2383 const struct unit *target_unit)
2385 struct tile *actor_tile = unit_tile(actor_unit);
2387 if (actor_unit == NULL || target_unit == NULL) {
2388 /* Can't do an action when actor or target are missing. */
2389 return FALSE;
2392 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(wanted_action),
2393 FALSE, "Action %s is performed by %s not %s",
2394 gen_action_name(wanted_action),
2395 action_actor_kind_name(
2396 action_id_get_actor_kind(wanted_action)),
2397 action_actor_kind_name(AAK_UNIT));
2399 fc_assert_ret_val_msg(ATK_UNIT
2400 == action_id_get_target_kind(wanted_action),
2401 FALSE, "Action %s is against %s not %s",
2402 gen_action_name(wanted_action),
2403 action_target_kind_name(
2404 action_id_get_target_kind(wanted_action)),
2405 action_target_kind_name(ATK_UNIT));
2407 if (!unit_can_do_action(actor_unit, wanted_action)) {
2408 /* No point in continuing. */
2409 return FALSE;
2412 return is_action_enabled(wanted_action,
2413 unit_owner(actor_unit), tile_city(actor_tile),
2414 NULL, actor_tile,
2415 actor_unit, unit_type_get(actor_unit),
2416 NULL, NULL,
2417 unit_owner(target_unit),
2418 tile_city(unit_tile(target_unit)), NULL,
2419 unit_tile(target_unit),
2420 target_unit, unit_type_get(target_unit),
2421 NULL, NULL,
2422 game_city_by_number(actor_unit->homecity),
2423 FALSE);
2426 /**************************************************************************
2427 Returns TRUE if actor_unit can do wanted_action to all units on the
2428 target_tile as far as action enablers are concerned.
2430 See note in is_action_enabled for why the action may still be disabled.
2431 **************************************************************************/
2432 bool is_action_enabled_unit_on_units(const enum gen_action wanted_action,
2433 const struct unit *actor_unit,
2434 const struct tile *target_tile)
2436 struct tile *actor_tile = unit_tile(actor_unit);
2437 struct city *homecity;
2439 if (actor_unit == NULL || target_tile == NULL
2440 || unit_list_size(target_tile->units) == 0) {
2441 /* Can't do an action when actor or target are missing. */
2442 return FALSE;
2445 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(wanted_action),
2446 FALSE, "Action %s is performed by %s not %s",
2447 gen_action_name(wanted_action),
2448 action_actor_kind_name(
2449 action_id_get_actor_kind(wanted_action)),
2450 action_actor_kind_name(AAK_UNIT));
2452 fc_assert_ret_val_msg(ATK_UNITS
2453 == action_id_get_target_kind(wanted_action),
2454 FALSE, "Action %s is against %s not %s",
2455 gen_action_name(wanted_action),
2456 action_target_kind_name(
2457 action_id_get_target_kind(wanted_action)),
2458 action_target_kind_name(ATK_UNITS));
2460 if (!unit_can_do_action(actor_unit, wanted_action)) {
2461 /* No point in continuing. */
2462 return FALSE;
2465 homecity = game_city_by_number(actor_unit->homecity);
2467 unit_list_iterate(target_tile->units, target_unit) {
2468 if (!is_action_enabled(wanted_action,
2469 unit_owner(actor_unit), tile_city(actor_tile),
2470 NULL, actor_tile,
2471 actor_unit, unit_type_get(actor_unit),
2472 NULL, NULL,
2473 unit_owner(target_unit),
2474 tile_city(unit_tile(target_unit)), NULL,
2475 unit_tile(target_unit),
2476 target_unit, unit_type_get(target_unit),
2477 NULL, NULL, homecity, FALSE)) {
2478 /* One unit makes it impossible for all units. */
2479 return FALSE;
2481 } unit_list_iterate_end;
2483 /* Not impossible for any of the units at the tile. */
2484 return TRUE;
2487 /**************************************************************************
2488 Returns TRUE if actor_unit can do wanted_action to the target_tile as far
2489 as action enablers are concerned.
2491 See note in is_action_enabled for why the action may still be disabled.
2492 **************************************************************************/
2493 bool is_action_enabled_unit_on_tile(const enum gen_action wanted_action,
2494 const struct unit *actor_unit,
2495 const struct tile *target_tile)
2497 struct tile *actor_tile = unit_tile(actor_unit);
2499 if (actor_unit == NULL || target_tile == NULL) {
2500 /* Can't do an action when actor or target are missing. */
2501 return FALSE;
2504 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(wanted_action),
2505 FALSE, "Action %s is performed by %s not %s",
2506 gen_action_name(wanted_action),
2507 action_actor_kind_name(
2508 action_id_get_actor_kind(wanted_action)),
2509 action_actor_kind_name(AAK_UNIT));
2511 fc_assert_ret_val_msg(ATK_TILE
2512 == action_id_get_target_kind(wanted_action),
2513 FALSE, "Action %s is against %s not %s",
2514 gen_action_name(wanted_action),
2515 action_target_kind_name(
2516 action_id_get_target_kind(wanted_action)),
2517 action_target_kind_name(ATK_TILE));
2519 if (!unit_can_do_action(actor_unit, wanted_action)) {
2520 /* No point in continuing. */
2521 return FALSE;
2524 return is_action_enabled(wanted_action,
2525 unit_owner(actor_unit), tile_city(actor_tile),
2526 NULL, actor_tile,
2527 actor_unit, unit_type_get(actor_unit),
2528 NULL, NULL,
2529 tile_owner(target_tile), NULL, NULL,
2530 target_tile, NULL, NULL, NULL, NULL,
2531 game_city_by_number(actor_unit->homecity),
2532 FALSE);
2535 /**************************************************************************
2536 Returns TRUE if actor_unit can do wanted_action to itself as far as
2537 action enablers are concerned.
2539 See note in is_action_enabled() for why the action still may be
2540 disabled.
2541 **************************************************************************/
2542 bool is_action_enabled_unit_on_self(const enum gen_action wanted_action,
2543 const struct unit *actor_unit)
2545 struct tile *actor_tile = unit_tile(actor_unit);
2547 if (actor_unit == NULL) {
2548 /* Can't do an action when the actor is missing. */
2549 return FALSE;
2552 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(wanted_action),
2553 FALSE, "Action %s is performed by %s not %s",
2554 gen_action_name(wanted_action),
2555 action_actor_kind_name(
2556 action_id_get_actor_kind(wanted_action)),
2557 action_actor_kind_name(AAK_UNIT));
2559 fc_assert_ret_val_msg(ATK_SELF
2560 == action_id_get_target_kind(wanted_action),
2561 FALSE, "Action %s is against %s not %s",
2562 gen_action_name(wanted_action),
2563 action_target_kind_name(
2564 action_id_get_target_kind(wanted_action)),
2565 action_target_kind_name(ATK_SELF));
2567 if (!unit_can_do_action(actor_unit, wanted_action)) {
2568 /* No point in continuing. */
2569 return FALSE;
2572 return is_action_enabled(wanted_action,
2573 unit_owner(actor_unit), tile_city(actor_tile),
2574 NULL, actor_tile,
2575 actor_unit, unit_type_get(actor_unit),
2576 NULL, NULL,
2577 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2578 game_city_by_number(actor_unit->homecity), FALSE);
2581 /**************************************************************************
2582 Find out if the action is enabled, may be enabled or isn't enabled given
2583 what the player owning the actor knowns.
2585 A player don't always know everything needed to figure out if an action
2586 is enabled or not. A server side AI with the same limits on its knowledge
2587 as a human player or a client should use this to figure out what is what.
2589 Assumes to be called from the point of view of the actor. Its knowledge
2590 is assumed to be given in the parameters.
2592 Returns TRI_YES if the action is enabled, TRI_NO if it isn't and
2593 TRI_MAYBE if the player don't know enough to tell.
2595 If meta knowledge is missing TRI_MAYBE will be returned.
2596 **************************************************************************/
2597 static enum fc_tristate
2598 action_enabled_local(const enum gen_action wanted_action,
2599 const struct player *actor_player,
2600 const struct city *actor_city,
2601 const struct impr_type *actor_building,
2602 const struct tile *actor_tile,
2603 const struct unit *actor_unit,
2604 const struct output_type *actor_output,
2605 const struct specialist *actor_specialist,
2606 const struct player *target_player,
2607 const struct city *target_city,
2608 const struct impr_type *target_building,
2609 const struct tile *target_tile,
2610 const struct unit *target_unit,
2611 const struct output_type *target_output,
2612 const struct specialist *target_specialist)
2614 enum fc_tristate current;
2615 enum fc_tristate result;
2617 result = TRI_NO;
2618 action_enabler_list_iterate(action_enablers_for_action(wanted_action),
2619 enabler) {
2620 current = fc_tristate_and(mke_eval_reqs(actor_player, actor_player,
2621 target_player, actor_city,
2622 actor_building, actor_tile,
2623 actor_unit, actor_output,
2624 actor_specialist,
2625 &enabler->actor_reqs,
2626 RPT_CERTAIN),
2627 mke_eval_reqs(actor_player, target_player,
2628 actor_player, target_city,
2629 target_building, target_tile,
2630 target_unit, target_output,
2631 target_specialist,
2632 &enabler->target_reqs,
2633 RPT_CERTAIN));
2634 if (current == TRI_YES) {
2635 return TRI_YES;
2636 } else if (current == TRI_MAYBE) {
2637 result = TRI_MAYBE;
2639 } action_enabler_list_iterate_end;
2641 return result;
2644 /**************************************************************************
2645 Find out if the effect value is known
2647 The knowledge of the actor is assumed to be given in the parameters.
2649 If meta knowledge is missing TRI_MAYBE will be returned.
2650 **************************************************************************/
2651 static bool is_effect_val_known(enum effect_type effect_type,
2652 const struct player *pow_player,
2653 const struct player *target_player,
2654 const struct player *other_player,
2655 const struct city *target_city,
2656 const struct impr_type *target_building,
2657 const struct tile *target_tile,
2658 const struct unit *target_unit,
2659 const struct output_type *target_output,
2660 const struct specialist *target_specialist)
2662 effect_list_iterate(get_effects(effect_type), peffect) {
2663 if (TRI_MAYBE == mke_eval_reqs(pow_player, target_player,
2664 other_player, target_city,
2665 target_building, target_tile,
2666 target_unit, target_output,
2667 target_specialist,
2668 &(peffect->reqs), RPT_CERTAIN)) {
2669 return FALSE;
2671 } effect_list_iterate_end;
2673 return TRUE;
2676 /**************************************************************************
2677 Does the target has any techs the actor don't?
2678 **************************************************************************/
2679 static enum fc_tristate
2680 tech_can_be_stolen(const struct player *actor_player,
2681 const struct player *target_player)
2683 const struct research *actor_research = research_get(actor_player);
2684 const struct research *target_research = research_get(target_player);
2686 if (actor_research != target_research) {
2687 if (can_see_techs_of_target(actor_player, target_player)) {
2688 advance_iterate(A_FIRST, padvance) {
2689 Tech_type_id i = advance_number(padvance);
2691 if (research_invention_state(target_research, i) == TECH_KNOWN
2692 && research_invention_gettable(actor_research, i,
2693 game.info.tech_steal_allow_holes)
2694 && (research_invention_state(actor_research, i) == TECH_UNKNOWN
2695 || (research_invention_state(actor_research, i)
2696 == TECH_PREREQS_KNOWN))) {
2697 return TRI_YES;
2699 } advance_iterate_end;
2700 } else {
2701 return TRI_MAYBE;
2705 return TRI_NO;
2708 /**************************************************************************
2709 The action probability that pattacker will win a diplomatic battle.
2711 It is assumed that pattacker and pdefender have different owners and that
2712 the defender can defend in a diplomatic battle.
2714 See diplomat_success_vs_defender() in server/diplomats.c
2715 **************************************************************************/
2716 static struct act_prob ap_dipl_battle_win(const struct unit *pattacker,
2717 const struct unit *pdefender)
2719 /* Keep unconverted until the end to avoid scaling each step */
2720 int chance;
2721 struct act_prob out;
2723 /* Superspy always win */
2724 if (unit_has_type_flag(pdefender, UTYF_SUPERSPY)) {
2725 /* A defending UTYF_SUPERSPY will defeat every possible attacker. */
2726 return ACTPROB_IMPOSSIBLE;
2728 if (unit_has_type_flag(pattacker, UTYF_SUPERSPY)) {
2729 /* An attacking UTYF_SUPERSPY will defeat every possible defender
2730 * except another UTYF_SUPERSPY. */
2731 return ACTPROB_CERTAIN;
2734 /* Base chance is 50% */
2735 chance = 50;
2737 /* Spy attack bonus */
2738 if (unit_has_type_flag(pattacker, UTYF_SPY)) {
2739 chance += 25;
2742 /* Spy defense bonus */
2743 if (unit_has_type_flag(pdefender, UTYF_SPY)) {
2744 chance -= 25;
2747 /* Veteran attack and defense bonus */
2749 const struct veteran_level *vatt =
2750 utype_veteran_level(unit_type_get(pattacker), pattacker->veteran);
2751 const struct veteran_level *vdef =
2752 utype_veteran_level(unit_type_get(pdefender), pdefender->veteran);
2754 chance += vatt->power_fact - vdef->power_fact;
2757 /* Defense bonus. */
2759 if (!is_effect_val_known(EFT_SPY_RESISTANT, unit_owner(pattacker),
2760 tile_owner(pdefender->tile), NULL,
2761 tile_city(pdefender->tile), NULL,
2762 pdefender->tile, NULL, NULL, NULL)) {
2763 return ACTPROB_NOT_KNOWN;
2766 /* Reduce the chance of an attack by EFT_SPY_RESISTANT percent. */
2767 chance -= chance
2768 * get_target_bonus_effects(NULL,
2769 tile_owner(pdefender->tile), NULL,
2770 tile_city(pdefender->tile), NULL,
2771 pdefender->tile, NULL, NULL, NULL,
2772 NULL, NULL,
2773 EFT_SPY_RESISTANT) / 100;
2776 /* Convert to action probability */
2777 out.min = chance * ACTPROB_VAL_1_PCT;
2778 out.max = chance * ACTPROB_VAL_1_PCT;
2780 return out;
2783 /**************************************************************************
2784 The action probability that pattacker will win a diplomatic battle.
2786 See diplomat_infiltrate_tile() in server/diplomats.c
2787 **************************************************************************/
2788 static struct act_prob ap_diplomat_battle(const struct unit *pattacker,
2789 const struct unit *pvictim)
2791 unit_list_iterate(unit_tile(pvictim)->units, punit) {
2792 if (unit_owner(punit) == unit_owner(pattacker)) {
2793 /* Won't defend against its owner. */
2794 continue;
2797 if (punit == pvictim
2798 && !unit_has_type_flag(punit, UTYF_SUPERSPY)) {
2799 /* The victim unit is defenseless unless it's a SuperSpy.
2800 * Rationalization: A regular diplomat don't mind being bribed. A
2801 * SuperSpy is high enough up the chain that accepting a bribe is
2802 * against his own interests. */
2803 continue;
2806 if (!(unit_has_type_flag(punit, UTYF_DIPLOMAT)
2807 || unit_has_type_flag(punit, UTYF_SUPERSPY))) {
2808 /* The unit can't defend. */
2809 continue;
2812 /* There will be a diplomatic battle in stead of an action. */
2813 return ap_dipl_battle_win(pattacker, punit);
2814 } unit_list_iterate_end;
2816 /* No diplomatic battle will occur. */
2817 return ACTPROB_CERTAIN;
2820 /***************************************************************************
2821 Returns the action probability for when a target is unseen.
2822 ***************************************************************************/
2823 static struct act_prob act_prob_unseen_target(int action_id,
2824 const struct unit *actor_unit)
2826 if (action_maybe_possible_actor_unit(action_id, actor_unit)) {
2827 /* Unknown because the target is unseen. */
2828 return ACTPROB_NOT_KNOWN;
2829 } else {
2830 /* The actor it self can't do this. */
2831 return ACTPROB_IMPOSSIBLE;
2835 /**************************************************************************
2836 An action's probability of success.
2838 "Success" indicates that the action achieves its goal, not that the
2839 actor survives. For actions that cost money it is assumed that the
2840 player has and is willing to spend the money. This is so the player can
2841 figure out what his odds are before deciding to get the extra money.
2842 **************************************************************************/
2843 static struct act_prob
2844 action_prob(const enum gen_action wanted_action,
2845 const struct player *actor_player,
2846 const struct city *actor_city,
2847 const struct impr_type *actor_building,
2848 const struct tile *actor_tile,
2849 const struct unit *actor_unit,
2850 const struct unit_type *actor_unittype_p,
2851 const struct output_type *actor_output,
2852 const struct specialist *actor_specialist,
2853 const struct player *target_player,
2854 const struct city *target_city,
2855 const struct impr_type *target_building,
2856 const struct tile *target_tile,
2857 const struct unit *target_unit,
2858 const struct unit_type *target_unittype_p,
2859 const struct output_type *target_output,
2860 const struct specialist *target_specialist)
2862 int known;
2863 struct act_prob chance;
2864 const struct unit_type *actor_unittype;
2865 const struct unit_type *target_unittype;
2866 const struct city *homecity;
2868 if (actor_unittype_p == NULL && actor_unit != NULL) {
2869 actor_unittype = unit_type_get(actor_unit);
2870 } else {
2871 actor_unittype = actor_unittype_p;
2874 if (target_unittype_p == NULL && target_unit != NULL) {
2875 target_unittype = unit_type_get(target_unit);
2876 } else {
2877 target_unittype = target_unittype_p;
2880 if (actor_unit != NULL) {
2881 homecity = game_city_by_number(actor_unit->homecity);
2882 } else {
2883 homecity = NULL;
2886 known = is_action_possible(wanted_action,
2887 actor_player, actor_city,
2888 actor_building, actor_tile,
2889 actor_unit, actor_unittype,
2890 actor_output, actor_specialist,
2891 target_player, target_city,
2892 target_building, target_tile,
2893 target_unit, target_unittype,
2894 target_output, target_specialist,
2895 FALSE, homecity, FALSE);
2897 if (known == TRI_NO) {
2898 /* The action enablers are irrelevant since the action it self is
2899 * impossible. */
2900 return ACTPROB_IMPOSSIBLE;
2903 chance = ACTPROB_NOT_IMPLEMENTED;
2905 known = fc_tristate_and(known,
2906 action_enabled_local(wanted_action,
2907 actor_player, actor_city,
2908 actor_building, actor_tile,
2909 actor_unit,
2910 actor_output,
2911 actor_specialist,
2912 target_player, target_city,
2913 target_building, target_tile,
2914 target_unit,
2915 target_output,
2916 target_specialist));
2918 switch (wanted_action) {
2919 case ACTION_SPY_POISON:
2920 /* TODO */
2921 break;
2922 case ACTION_SPY_STEAL_GOLD:
2923 /* TODO */
2924 break;
2925 case ACTION_STEAL_MAPS:
2926 /* TODO */
2927 break;
2928 case ACTION_SPY_SABOTAGE_UNIT:
2929 /* All uncertainty comes from potential diplomatic battles. */
2930 chance = ap_diplomat_battle(actor_unit, target_unit);
2931 break;
2932 case ACTION_SPY_BRIBE_UNIT:
2933 /* All uncertainty comes from potential diplomatic battles. */
2934 chance = ap_diplomat_battle(actor_unit, target_unit);;
2935 break;
2936 case ACTION_SPY_SABOTAGE_CITY:
2937 /* TODO */
2938 break;
2939 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
2940 /* TODO */
2941 break;
2942 case ACTION_SPY_INCITE_CITY:
2943 /* TODO */
2944 break;
2945 case ACTION_SPY_INCITE_CITY_ESC:
2946 /* TODO */
2947 break;
2948 case ACTION_ESTABLISH_EMBASSY:
2949 chance = ACTPROB_CERTAIN;
2950 break;
2951 case ACTION_ESTABLISH_EMBASSY_STAY:
2952 chance = ACTPROB_CERTAIN;
2953 break;
2954 case ACTION_SPY_STEAL_TECH:
2955 /* Do the victim have anything worth taking? */
2956 known = fc_tristate_and(known,
2957 tech_can_be_stolen(actor_player,
2958 target_player));
2960 /* TODO: Calculate actual chance */
2962 break;
2963 case ACTION_SPY_TARGETED_STEAL_TECH:
2964 /* Do the victim have anything worth taking? */
2965 known = fc_tristate_and(known,
2966 tech_can_be_stolen(actor_player,
2967 target_player));
2969 /* TODO: Calculate actual chance */
2971 break;
2972 case ACTION_SPY_INVESTIGATE_CITY:
2973 /* There is no risk that the city won't get investigated. */
2974 chance = ACTPROB_CERTAIN;
2975 break;
2976 case ACTION_INV_CITY_SPEND:
2977 /* There is no risk that the city won't get investigated. */
2978 chance = ACTPROB_CERTAIN;
2979 break;
2980 case ACTION_TRADE_ROUTE:
2981 /* TODO */
2982 break;
2983 case ACTION_MARKETPLACE:
2984 /* Possible when not blocked by is_action_possible() */
2985 chance = ACTPROB_CERTAIN;
2986 break;
2987 case ACTION_HELP_WONDER:
2988 /* Possible when not blocked by is_action_possible() */
2989 chance = ACTPROB_CERTAIN;
2990 break;
2991 case ACTION_CAPTURE_UNITS:
2992 /* No battle is fought first. */
2993 chance = ACTPROB_CERTAIN;
2994 break;
2995 case ACTION_EXPEL_UNIT:
2996 /* No battle is fought first. */
2997 chance = ACTPROB_CERTAIN;
2998 break;
2999 case ACTION_BOMBARD:
3000 /* No battle is fought first. */
3001 chance = ACTPROB_CERTAIN;
3002 break;
3003 case ACTION_FOUND_CITY:
3004 /* Possible when not blocked by is_action_possible() */
3005 chance = ACTPROB_CERTAIN;
3006 break;
3007 case ACTION_JOIN_CITY:
3008 /* Possible when not blocked by is_action_possible() */
3009 chance = ACTPROB_CERTAIN;
3010 break;
3011 case ACTION_SPY_NUKE:
3012 /* TODO */
3013 break;
3014 case ACTION_SPY_NUKE_ESC:
3015 /* TODO */
3016 break;
3017 case ACTION_NUKE:
3018 /* TODO */
3019 break;
3020 case ACTION_DESTROY_CITY:
3021 /* No battle is fought first. */
3022 chance = ACTPROB_CERTAIN;
3023 break;
3024 case ACTION_RECYCLE_UNIT:
3025 /* No battle is fought first. */
3026 chance = ACTPROB_CERTAIN;
3027 break;
3028 case ACTION_DISBAND_UNIT:
3029 /* No battle is fought first. */
3030 chance = ACTPROB_CERTAIN;
3031 break;
3032 case ACTION_HOME_CITY:
3033 /* No battle is fought first. */
3034 chance = ACTPROB_CERTAIN;
3035 break;
3036 case ACTION_UPGRADE_UNIT:
3037 /* No battle is fought first. */
3038 chance = ACTPROB_CERTAIN;
3039 break;
3040 case ACTION_PARADROP:
3041 /* TODO */
3042 break;
3043 case ACTION_AIRLIFT:
3044 /* TODO */
3045 break;
3046 case ACTION_ATTACK:
3048 struct unit *defender_unit = get_defender(actor_unit, target_tile);
3050 if (can_player_see_unit(actor_player, defender_unit)) {
3051 double unconverted = unit_win_chance(actor_unit, defender_unit);
3053 chance.min = MAX(ACTPROB_VAL_MIN,
3054 floor((double)ACTPROB_VAL_MAX * unconverted));
3055 chance.max = MIN(ACTPROB_VAL_MAX,
3056 ceil((double)ACTPROB_VAL_MAX * unconverted));
3057 } else if (known == TRI_YES) {
3058 known = TRI_MAYBE;
3061 break;
3062 case ACTION_CONQUER_CITY:
3063 /* No battle is fought first. */
3064 chance = ACTPROB_CERTAIN;
3065 break;
3066 case ACTION_HEAL_UNIT:
3067 /* No battle is fought first. */
3068 chance = ACTPROB_CERTAIN;
3069 break;
3070 case ACTION_COUNT:
3071 fc_assert(wanted_action != ACTION_COUNT);
3072 break;
3075 /* Non signal action probabilities should be in range. */
3076 fc_assert_action((action_prob_is_signal(chance)
3077 || chance.max <= ACTPROB_VAL_MAX),
3078 chance.max = ACTPROB_VAL_MAX);
3079 fc_assert_action((action_prob_is_signal(chance)
3080 || chance.min >= ACTPROB_VAL_MIN),
3081 chance.min = ACTPROB_VAL_MIN);
3083 switch (known) {
3084 case TRI_NO:
3085 return ACTPROB_IMPOSSIBLE;
3086 break;
3087 case TRI_MAYBE:
3088 return ACTPROB_NOT_KNOWN;
3089 break;
3090 case TRI_YES:
3091 return chance;
3092 break;
3095 fc_assert_ret_val_msg(FALSE, ACTPROB_NOT_IMPLEMENTED,
3096 "Should be yes, maybe or no");
3099 /**************************************************************************
3100 Get the actor unit's probability of successfully performing the chosen
3101 action on the target city.
3102 **************************************************************************/
3103 struct act_prob action_prob_vs_city(const struct unit* actor_unit,
3104 const int action_id,
3105 const struct city* target_city)
3107 struct tile *actor_tile = unit_tile(actor_unit);
3108 struct impr_type *target_building;
3109 struct unit_type *target_utype;
3111 if (actor_unit == NULL || target_city == NULL) {
3112 /* Can't do an action when actor or target are missing. */
3113 return ACTPROB_IMPOSSIBLE;
3116 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(action_id),
3117 ACTPROB_IMPOSSIBLE,
3118 "Action %s is performed by %s not %s",
3119 gen_action_name(action_id),
3120 action_actor_kind_name(
3121 action_id_get_actor_kind(action_id)),
3122 action_actor_kind_name(AAK_UNIT));
3124 fc_assert_ret_val_msg(ATK_CITY == action_id_get_target_kind(action_id),
3125 ACTPROB_IMPOSSIBLE,
3126 "Action %s is against %s not %s",
3127 gen_action_name(action_id),
3128 action_target_kind_name(
3129 action_id_get_target_kind(action_id)),
3130 action_target_kind_name(ATK_CITY));
3132 if (!unit_can_do_action(actor_unit, action_id)) {
3133 /* No point in continuing. */
3134 return ACTPROB_IMPOSSIBLE;
3137 /* Doesn't leak information about city position since an unknown city
3138 * can't be targeted and a city can't move. */
3139 if (!action_id_distance_accepted(action_id,
3140 real_map_distance(unit_tile(actor_unit),
3141 city_tile(target_city)))) {
3142 /* No point in continuing. */
3143 return ACTPROB_IMPOSSIBLE;
3146 if (!player_can_see_city_externals(unit_owner(actor_unit), target_city)) {
3147 /* The invisible city at this tile may, as far as the player knows, not
3148 * exist anymore. */
3149 return act_prob_unseen_target(action_id, actor_unit);
3152 target_building = tgt_city_local_building(target_city);
3153 target_utype = tgt_city_local_utype(target_city);
3155 return action_prob(action_id,
3156 unit_owner(actor_unit), tile_city(actor_tile),
3157 NULL, actor_tile, actor_unit, NULL,
3158 NULL, NULL,
3159 city_owner(target_city), target_city,
3160 target_building, city_tile(target_city),
3161 NULL, target_utype, NULL, NULL);
3164 /**************************************************************************
3165 Get the actor unit's probability of successfully performing the chosen
3166 action on the target unit.
3167 **************************************************************************/
3168 struct act_prob action_prob_vs_unit(const struct unit* actor_unit,
3169 const int action_id,
3170 const struct unit* target_unit)
3172 struct tile *actor_tile = unit_tile(actor_unit);
3174 if (actor_unit == NULL || target_unit == NULL) {
3175 /* Can't do an action when actor or target are missing. */
3176 return ACTPROB_IMPOSSIBLE;
3179 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(action_id),
3180 ACTPROB_IMPOSSIBLE,
3181 "Action %s is performed by %s not %s",
3182 gen_action_name(action_id),
3183 action_actor_kind_name(
3184 action_id_get_actor_kind(action_id)),
3185 action_actor_kind_name(AAK_UNIT));
3187 fc_assert_ret_val_msg(ATK_UNIT == action_id_get_target_kind(action_id),
3188 ACTPROB_IMPOSSIBLE,
3189 "Action %s is against %s not %s",
3190 gen_action_name(action_id),
3191 action_target_kind_name(
3192 action_id_get_target_kind(action_id)),
3193 action_target_kind_name(ATK_UNIT));
3195 if (!unit_can_do_action(actor_unit, action_id)) {
3196 /* No point in continuing. */
3197 return ACTPROB_IMPOSSIBLE;
3200 /* Doesn't leak information about unit position since an unseen unit can't
3201 * be targeted. */
3202 if (!action_id_distance_accepted(action_id,
3203 real_map_distance(unit_tile(actor_unit),
3204 unit_tile(target_unit)))) {
3205 /* No point in continuing. */
3206 return ACTPROB_IMPOSSIBLE;
3209 return action_prob(action_id,
3210 unit_owner(actor_unit), tile_city(actor_tile),
3211 NULL, actor_tile, actor_unit, NULL,
3212 NULL, NULL,
3213 unit_owner(target_unit),
3214 tile_city(unit_tile(target_unit)), NULL,
3215 unit_tile(target_unit),
3216 target_unit, NULL, NULL, NULL);
3219 /**************************************************************************
3220 Get the actor unit's probability of successfully performing the chosen
3221 action on all units at the target tile.
3222 **************************************************************************/
3223 struct act_prob action_prob_vs_units(const struct unit* actor_unit,
3224 const int action_id,
3225 const struct tile* target_tile)
3227 struct act_prob prob_all;
3228 struct tile *actor_tile = unit_tile(actor_unit);
3230 if (actor_unit == NULL || target_tile == NULL) {
3231 /* Can't do an action when actor or target are missing. */
3232 return ACTPROB_IMPOSSIBLE;
3235 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(action_id),
3236 ACTPROB_IMPOSSIBLE,
3237 "Action %s is performed by %s not %s",
3238 gen_action_name(action_id),
3239 action_actor_kind_name(
3240 action_id_get_actor_kind(action_id)),
3241 action_actor_kind_name(AAK_UNIT));
3243 fc_assert_ret_val_msg(ATK_UNITS == action_id_get_target_kind(action_id),
3244 ACTPROB_IMPOSSIBLE,
3245 "Action %s is against %s not %s",
3246 gen_action_name(action_id),
3247 action_target_kind_name(
3248 action_id_get_target_kind(action_id)),
3249 action_target_kind_name(ATK_UNITS));
3251 if (!unit_can_do_action(actor_unit, action_id)) {
3252 /* No point in continuing. */
3253 return ACTPROB_IMPOSSIBLE;
3256 /* Doesn't leak information about unit stack position since it is
3257 * specified as a tile and an unknown tile's position is known. */
3258 if (!action_id_distance_accepted(action_id,
3259 real_map_distance(unit_tile(actor_unit),
3260 target_tile))) {
3261 /* No point in continuing. */
3262 return ACTPROB_IMPOSSIBLE;
3265 /* Doesn't leak information since the actor player can see the target
3266 * tile. */
3267 if (tile_is_seen(target_tile, unit_owner(actor_unit))
3268 && tile_city(target_tile) != NULL
3269 && !utype_can_do_act_if_tgt_citytile(unit_type_get(actor_unit),
3270 action_id,
3271 CITYT_CENTER, TRUE)) {
3272 /* Don't offer to perform actions that never can target a unit stack in
3273 * a city. */
3274 return ACTPROB_IMPOSSIBLE;
3277 /* Must be done here since an empty unseen tile will result in
3278 * ACTPROB_IMPOSSIBLE. */
3279 if (unit_list_size(target_tile->units) == 0) {
3280 /* Can't act against an empty tile. */
3282 if (player_can_trust_tile_has_no_units(unit_owner(actor_unit),
3283 target_tile)) {
3284 /* Known empty tile. */
3285 return ACTPROB_IMPOSSIBLE;
3286 } else {
3287 /* The player doesn't know that the tile is empty. */
3288 return act_prob_unseen_target(action_id, actor_unit);
3292 /* Invisible units at this tile can make the action legal or illegal.
3293 * Invisible units can be stacked with visible units. The possible
3294 * existence of invisible units therefore makes the result uncertain. */
3295 prob_all = (can_player_see_hypotetic_units_at(unit_owner(actor_unit),
3296 target_tile)
3297 ? ACTPROB_CERTAIN : ACTPROB_NOT_KNOWN);
3299 unit_list_iterate(target_tile->units, target_unit) {
3300 struct act_prob prob_unit;
3302 if (!can_player_see_unit(unit_owner(actor_unit), target_unit)) {
3303 /* Only visible units are considered. The invisible units contributed
3304 * their uncertainty to prob_all above. */
3305 continue;
3308 prob_unit = action_prob(action_id,
3309 unit_owner(actor_unit),
3310 tile_city(actor_tile),
3311 NULL, actor_tile, actor_unit, NULL,
3312 NULL, NULL,
3313 unit_owner(target_unit),
3314 tile_city(unit_tile(target_unit)), NULL,
3315 unit_tile(target_unit),
3316 target_unit, NULL,
3317 NULL, NULL);
3319 if (!action_prob_possible(prob_unit)) {
3320 /* One unit makes it impossible for all units. */
3321 return ACTPROB_IMPOSSIBLE;
3322 } else if (action_prob_not_impl(prob_unit)) {
3323 /* Not implemented dominates all except impossible. */
3324 prob_all = ACTPROB_NOT_IMPLEMENTED;
3325 } else {
3326 fc_assert_msg(!action_prob_is_signal(prob_unit),
3327 "Invalid probability [%d, %d]",
3328 prob_unit.min, prob_unit.max);
3330 if (action_prob_is_signal(prob_all)) {
3331 /* Special values dominate regular values. */
3332 continue;
3335 /* Probability against all target units considered until this moment
3336 * and the probability against this target unit. */
3337 prob_all.min = (prob_all.min * prob_unit.min) / ACTPROB_VAL_MAX;
3338 prob_all.max = (prob_all.max * prob_unit.max) / ACTPROB_VAL_MAX;
3339 break;
3341 } unit_list_iterate_end;
3343 /* Not impossible for any of the units at the tile. */
3344 return prob_all;
3347 /**************************************************************************
3348 Get the actor unit's probability of successfully performing the chosen
3349 action on the target tile.
3350 **************************************************************************/
3351 struct act_prob action_prob_vs_tile(const struct unit* actor_unit,
3352 const int action_id,
3353 const struct tile* target_tile)
3355 struct tile *actor_tile = unit_tile(actor_unit);
3357 if (actor_unit == NULL || target_tile == NULL) {
3358 /* Can't do an action when actor or target are missing. */
3359 return ACTPROB_IMPOSSIBLE;
3362 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(action_id),
3363 ACTPROB_IMPOSSIBLE,
3364 "Action %s is performed by %s not %s",
3365 gen_action_name(action_id),
3366 action_actor_kind_name(
3367 action_id_get_actor_kind(action_id)),
3368 action_actor_kind_name(AAK_UNIT));
3370 fc_assert_ret_val_msg(ATK_TILE == action_id_get_target_kind(action_id),
3371 ACTPROB_IMPOSSIBLE,
3372 "Action %s is against %s not %s",
3373 gen_action_name(action_id),
3374 action_target_kind_name(
3375 action_id_get_target_kind(action_id)),
3376 action_target_kind_name(ATK_TILE));
3378 if (!unit_can_do_action(actor_unit, action_id)) {
3379 /* No point in continuing. */
3380 return ACTPROB_IMPOSSIBLE;
3383 /* Doesn't leak information about tile position since an unknown tile's
3384 * position is known. */
3385 if (!action_id_distance_accepted(action_id,
3386 real_map_distance(unit_tile(actor_unit),
3387 target_tile))) {
3388 /* No point in continuing. */
3389 return ACTPROB_IMPOSSIBLE;
3392 return action_prob(action_id,
3393 unit_owner(actor_unit), tile_city(actor_tile),
3394 NULL, actor_tile, actor_unit, NULL,
3395 NULL, NULL,
3396 tile_owner(target_tile), NULL, NULL,
3397 target_tile, NULL, NULL, NULL, NULL);
3400 /**************************************************************************
3401 Get the actor unit's probability of successfully performing the chosen
3402 action on itself.
3403 **************************************************************************/
3404 struct act_prob action_prob_self(const struct unit* actor_unit,
3405 const int action_id)
3407 struct tile *actor_tile = unit_tile(actor_unit);
3409 if (actor_unit == NULL) {
3410 /* Can't do the action when the actor is missing. */
3411 return ACTPROB_IMPOSSIBLE;
3414 /* No point in checking distance to target. It is always 0. */
3416 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(action_id),
3417 ACTPROB_IMPOSSIBLE,
3418 "Action %s is performed by %s not %s",
3419 gen_action_name(action_id),
3420 action_actor_kind_name(
3421 action_id_get_actor_kind(action_id)),
3422 action_actor_kind_name(AAK_UNIT));
3424 fc_assert_ret_val_msg(ATK_SELF == action_id_get_target_kind(action_id),
3425 ACTPROB_IMPOSSIBLE,
3426 "Action %s is against %s not %s",
3427 gen_action_name(action_id),
3428 action_target_kind_name(
3429 action_id_get_target_kind(action_id)),
3430 action_target_kind_name(ATK_SELF));
3432 if (!unit_can_do_action(actor_unit, action_id)) {
3433 /* No point in continuing. */
3434 return ACTPROB_IMPOSSIBLE;
3437 return action_prob(action_id,
3438 unit_owner(actor_unit), tile_city(actor_tile),
3439 NULL, actor_tile, actor_unit, NULL,
3440 NULL, NULL,
3441 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
3444 /**************************************************************************
3445 Returns the impossible action probability.
3446 **************************************************************************/
3447 struct act_prob action_prob_new_impossible(void)
3449 struct act_prob out = { ACTPROB_VAL_MIN, ACTPROB_VAL_MIN };
3451 return out;
3454 /**************************************************************************
3455 Returns the certain action probability.
3456 **************************************************************************/
3457 struct act_prob action_prob_new_certain(void)
3459 struct act_prob out = { ACTPROB_VAL_MAX, ACTPROB_VAL_MAX };
3461 return out;
3464 /**************************************************************************
3465 Returns the n/a action probability.
3466 **************************************************************************/
3467 struct act_prob action_prob_new_not_relevant(void)
3469 struct act_prob out = { ACTPROB_VAL_NA, ACTPROB_VAL_MIN};
3471 return out;
3474 /**************************************************************************
3475 Returns the "not implemented" action probability.
3476 **************************************************************************/
3477 struct act_prob action_prob_new_not_impl(void)
3479 struct act_prob out = { ACTPROB_VAL_NOT_IMPL, ACTPROB_VAL_MIN };
3481 return out;
3484 /**************************************************************************
3485 Returns the "user don't know" action probability.
3486 **************************************************************************/
3487 struct act_prob action_prob_new_unknown(void)
3489 struct act_prob out = { ACTPROB_VAL_MIN, ACTPROB_VAL_MAX };
3491 return out;
3494 /**************************************************************************
3495 Returns TRUE iff the given action probability belongs to an action that
3496 may be possible.
3497 **************************************************************************/
3498 bool action_prob_possible(const struct act_prob probability)
3500 return (ACTPROB_VAL_MIN < probability.max
3501 || action_prob_not_impl(probability));
3504 /**************************************************************************
3505 Returns TRUE iff the given action probability represents the lack of
3506 an action probability.
3507 **************************************************************************/
3508 static inline bool
3509 action_prob_not_relevant(const struct act_prob probability)
3511 return probability.min == ACTPROB_VAL_NA
3512 && probability.max == ACTPROB_VAL_MIN;
3515 /**************************************************************************
3516 Returns TRUE iff the given action probability represents that support
3517 for finding this action probability currently is missing from Freeciv.
3518 **************************************************************************/
3519 static inline bool
3520 action_prob_not_impl(const struct act_prob probability)
3522 return probability.min == ACTPROB_VAL_NOT_IMPL
3523 && probability.max == ACTPROB_VAL_MIN;
3526 /**************************************************************************
3527 Returns TRUE iff the given action probability represents a special
3528 signal value rather than a regular action probability value.
3529 **************************************************************************/
3530 static inline bool
3531 action_prob_is_signal(const struct act_prob probability)
3533 return probability.max < probability.min;
3536 /**************************************************************************
3537 Returns TRUE iff ap1 and ap2 are equal.
3538 **************************************************************************/
3539 bool are_action_probabilitys_equal(const struct act_prob *ap1,
3540 const struct act_prob *ap2)
3542 return ap1->min == ap2->min && ap1->max == ap2->max;
3545 /**************************************************************************
3546 Compare action probabilities. Prioritize the lowest possible value.
3547 **************************************************************************/
3548 int action_prob_cmp_pessimist(const struct act_prob ap1,
3549 const struct act_prob ap2)
3551 struct act_prob my_ap1;
3552 struct act_prob my_ap2;
3554 /* The action probabilities are real. */
3555 fc_assert(!action_prob_not_relevant(ap1));
3556 fc_assert(!action_prob_not_relevant(ap2));
3558 /* Convert any signals to ACTPROB_NOT_KNOWN. */
3559 if (action_prob_is_signal(ap1)) {
3560 /* Assert that it is OK to convert the signal. */
3561 fc_assert(action_prob_not_impl(ap1));
3563 my_ap1 = ACTPROB_NOT_KNOWN;
3564 } else {
3565 my_ap1 = ap1;
3568 if (action_prob_is_signal(ap2)) {
3569 /* Assert that it is OK to convert the signal. */
3570 fc_assert(action_prob_not_impl(ap2));
3572 my_ap2 = ACTPROB_NOT_KNOWN;
3573 } else {
3574 my_ap2 = ap2;
3577 /* The action probabilities now have a comparison friendly form. */
3578 fc_assert(!action_prob_is_signal(my_ap1));
3579 fc_assert(!action_prob_is_signal(my_ap2));
3581 /* Do the comparison. Start with min. Continue with max. */
3582 if (my_ap1.min < my_ap2.min) {
3583 return -1;
3584 } else if (my_ap1.min > my_ap2.min) {
3585 return 1;
3586 } else if (my_ap1.max < my_ap2.max) {
3587 return -1;
3588 } else if (my_ap1.max > my_ap2.max) {
3589 return 1;
3590 } else {
3591 return 0;
3595 /**************************************************************************
3596 Returns double in the range [0-1] representing the minimum of the given
3597 action probability.
3598 **************************************************************************/
3599 double action_prob_to_0_to_1_pessimist(const struct act_prob ap)
3601 struct act_prob my_ap;
3603 /* The action probability is real. */
3604 fc_assert(!action_prob_not_relevant(ap));
3606 /* Convert any signals to ACTPROB_NOT_KNOWN. */
3607 if (action_prob_is_signal(ap)) {
3608 /* Assert that it is OK to convert the signal. */
3609 fc_assert(action_prob_not_impl(ap));
3611 my_ap = ACTPROB_NOT_KNOWN;
3612 } else {
3613 my_ap = ap;
3616 /* The action probability now has a math friendly form. */
3617 fc_assert(!action_prob_is_signal(my_ap));
3619 return (double)my_ap.min / (double) ACTPROB_VAL_MAX;
3622 /**************************************************************************
3623 Returns ap1 with ap2 as fall back in cases where ap1 doesn't happen.
3624 Said in math that is: P(A) + P(A') * P(B)
3626 This is useful to calculate the probability of doing action A or, when A
3627 is impossible, falling back to doing action B.
3628 **************************************************************************/
3629 struct act_prob action_prob_fall_back(const struct act_prob *ap1,
3630 const struct act_prob *ap2)
3632 struct act_prob my_ap1;
3633 struct act_prob my_ap2;
3634 struct act_prob out;
3636 /* The action probabilities are real. */
3637 fc_assert(ap1 && !action_prob_not_relevant(*ap1));
3638 fc_assert(ap2 && !action_prob_not_relevant(*ap2));
3640 if (action_prob_is_signal(*ap1)
3641 && are_action_probabilitys_equal(ap1, ap2)) {
3642 /* Keep the information rather than converting the signal to
3643 * ACTPROB_NOT_KNOWN. */
3645 /* Assert that it is OK to convert the signal. */
3646 fc_assert(action_prob_not_impl(*ap1));
3648 out.min = ap1->min;
3649 out.max = ap2->max;
3651 return out;
3654 /* Convert any signals to ACTPROB_NOT_KNOWN. */
3655 if (action_prob_is_signal(*ap1)) {
3656 /* Assert that it is OK to convert the signal. */
3657 fc_assert(action_prob_not_impl(*ap1));
3659 my_ap1.min = ACTPROB_VAL_MIN;
3660 my_ap1.max = ACTPROB_VAL_MAX;
3661 } else {
3662 my_ap1.min = ap1->min;
3663 my_ap1.max = ap1->max;
3666 if (action_prob_is_signal(*ap2)) {
3667 /* Assert that it is OK to convert the signal. */
3668 fc_assert(action_prob_not_impl(*ap2));
3670 my_ap2.min = ACTPROB_VAL_MIN;
3671 my_ap2.max = ACTPROB_VAL_MAX;
3672 } else {
3673 my_ap2.min = ap2->min;
3674 my_ap2.max = ap2->max;
3677 /* The action probabilities now have a math friendly form. */
3678 fc_assert(!action_prob_is_signal(my_ap1));
3679 fc_assert(!action_prob_is_signal(my_ap2));
3681 /* Do the math. */
3682 out.min = my_ap1.min + (((ACTPROB_VAL_MAX - my_ap1.min) * my_ap2.min)
3683 / ACTPROB_VAL_MAX);
3684 out.max = my_ap1.max + (((ACTPROB_VAL_MAX - my_ap1.max) * my_ap2.max)
3685 / ACTPROB_VAL_MAX);
3687 return out;
3690 /**************************************************************************
3691 Will a player with the government gov be immune to the action act?
3692 **************************************************************************/
3693 bool action_immune_government(struct government *gov, int act)
3695 /* Always immune since its not enabled. Doesn't count. */
3696 if (action_enabler_list_size(action_enablers_for_action(act)) == 0) {
3697 return FALSE;
3700 action_enabler_list_iterate(action_enablers_for_action(act), enabler) {
3701 if (requirement_fulfilled_by_government(gov, &(enabler->target_reqs))) {
3702 return FALSE;
3704 } action_enabler_list_iterate_end;
3706 return TRUE;
3709 /**************************************************************************
3710 Returns TRUE if the specified action never can be performed when the
3711 situation requirement is fulfilled for the actor.
3712 **************************************************************************/
3713 bool action_blocked_by_situation_act(const struct action *paction,
3714 const struct requirement *situation)
3716 action_enabler_list_iterate(action_enablers_for_action(paction->id),
3717 enabler) {
3718 if (!does_req_contradicts_reqs(situation, &enabler->actor_reqs)) {
3719 return FALSE;
3721 } action_enabler_list_iterate_end;
3723 return TRUE;
3726 /**************************************************************************
3727 Returns TRUE if the specified action never can be performed when the
3728 situation requirement is fulfilled for the target.
3729 **************************************************************************/
3730 bool action_blocked_by_situation_tgt(const struct action *paction,
3731 const struct requirement *situation)
3733 action_enabler_list_iterate(action_enablers_for_action(paction->id),
3734 enabler) {
3735 if (!does_req_contradicts_reqs(situation, &enabler->target_reqs)) {
3736 return FALSE;
3738 } action_enabler_list_iterate_end;
3740 return TRUE;
3743 /**************************************************************************
3744 Returns TRUE if the wanted action can be done to the target.
3745 **************************************************************************/
3746 static bool is_target_possible(const enum gen_action wanted_action,
3747 const struct player *actor_player,
3748 const struct player *target_player,
3749 const struct city *target_city,
3750 const struct impr_type *target_building,
3751 const struct tile *target_tile,
3752 const struct unit *target_unit,
3753 const struct unit_type *target_unittype,
3754 const struct output_type *target_output,
3755 const struct specialist *target_specialist)
3757 action_enabler_list_iterate(action_enablers_for_action(wanted_action),
3758 enabler) {
3759 if (are_reqs_active(target_player, actor_player, target_city,
3760 target_building, target_tile,
3761 target_unit, target_unittype,
3762 target_output, target_specialist, NULL,
3763 &enabler->target_reqs, RPT_POSSIBLE)) {
3764 return TRUE;
3766 } action_enabler_list_iterate_end;
3768 return FALSE;
3771 /**************************************************************************
3772 Returns TRUE if the wanted action can be done to the target city.
3773 **************************************************************************/
3774 bool is_action_possible_on_city(const enum gen_action action_id,
3775 const struct player *actor_player,
3776 const struct city* target_city)
3778 fc_assert_ret_val_msg(ATK_CITY == action_id_get_target_kind(action_id),
3779 FALSE, "Action %s is against %s not cities",
3780 gen_action_name(action_id),
3781 action_target_kind_name(
3782 action_id_get_target_kind(action_id)));
3784 return is_target_possible(action_id, actor_player,
3785 city_owner(target_city), target_city, NULL,
3786 city_tile(target_city), NULL, NULL,
3787 NULL, NULL);
3790 /**************************************************************************
3791 Returns TRUE if the wanted action (as far as the player knows) can be
3792 performed right now by the specified actor unit if an approriate target
3793 is provided.
3794 **************************************************************************/
3795 bool action_maybe_possible_actor_unit(const int action_id,
3796 const struct unit *actor_unit)
3798 const struct player *actor_player = unit_owner(actor_unit);
3799 const struct tile *actor_tile = unit_tile(actor_unit);
3800 const struct city *actor_city = tile_city(actor_tile);
3801 const struct unit_type *actor_unittype = unit_type_get(actor_unit);
3803 enum fc_tristate result;
3805 fc_assert_ret_val(actor_unit, FALSE);
3807 if (!utype_can_do_action(actor_unit->utype, action_id)) {
3808 /* The unit type can't perform the action. */
3809 return FALSE;
3812 result = action_hard_reqs_actor(action_id,
3813 actor_player, actor_city, NULL,
3814 actor_tile, actor_unit, actor_unittype,
3815 NULL, NULL, FALSE,
3816 game_city_by_number(actor_unit->homecity));
3818 if (result == TRI_NO) {
3819 /* The hard requirements aren't fulfilled. */
3820 return FALSE;
3823 action_enabler_list_iterate(action_enablers_for_action(action_id),
3824 enabler) {
3825 const enum fc_tristate current
3826 = mke_eval_reqs(actor_player,
3827 actor_player, NULL, actor_city, NULL, actor_tile,
3828 actor_unit, NULL, NULL,
3829 &enabler->actor_reqs,
3830 /* Needed since no player to evaluate DiplRel
3831 * requirements against. */
3832 RPT_POSSIBLE);
3834 if (current == TRI_YES
3835 || current == TRI_MAYBE) {
3836 /* The ruleset requirements may be fulfilled. */
3837 return TRUE;
3839 } action_enabler_list_iterate_end;
3841 /* No action enabler allows this action. */
3842 return FALSE;
3845 /**************************************************************************
3846 Returns TRUE if the specified action can't be done now but would have
3847 been legal if the unit had full movement.
3848 **************************************************************************/
3849 bool action_mp_full_makes_legal(const struct unit *actor,
3850 const int action_id)
3852 fc_assert(action_id_exists(action_id) || action_id == ACTION_ANY);
3854 /* Check if full movement points may enable the specified action. */
3855 return !utype_may_act_move_frags(unit_type_get(actor),
3856 action_id,
3857 actor->moves_left)
3858 && utype_may_act_move_frags(unit_type_get(actor),
3859 action_id,
3860 unit_move_rate(actor));
3863 /**************************************************************************
3864 Returns action auto performer rule slot number num so it can be filled.
3865 **************************************************************************/
3866 struct action_auto_perf *action_auto_perf_slot_number(const int num)
3868 fc_assert_ret_val(num >= 0, NULL);
3869 fc_assert_ret_val(num < MAX_NUM_ACTION_AUTO_PERFORMERS, NULL);
3871 return &auto_perfs[num];
3874 /**************************************************************************
3875 Returns action auto performer rule number num.
3877 Used in action_auto_perf_iterate()
3879 WARNING: If the cause of the returned action performer rule is
3880 AAPC_COUNT it means that it is unused.
3881 **************************************************************************/
3882 const struct action_auto_perf *action_auto_perf_by_number(const int num)
3884 return action_auto_perf_slot_number(num);