Use linear-gradient instead of gtk css extension in gtk3.22-client theme
[freeciv.git] / common / actions.c
blobe31ea4f06086040234906587f9ef44cfb345a5e0
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 /* utility */
19 #include "astring.h"
21 /* common */
22 #include "actions.h"
23 #include "city.h"
24 #include "game.h"
25 #include "map.h"
26 #include "movement.h"
27 #include "unit.h"
28 #include "research.h"
29 #include "tile.h"
31 /* Values used to interpret action probabilities.
33 * Action probabilities are sent over the network. A change in a value here
34 * will therefore change the network protocol.
36 * A change in a value here should also update the action probability
37 * format documentation in fc_types.h */
38 /* The lowest possible probability value (0%) */
39 #define ACTPROB_VAL_MIN 0
40 /* The highest possible probability value (100%) */
41 #define ACTPROB_VAL_MAX 200
42 /* A probability increase of 1% corresponds to this increase. */
43 #define ACTPROB_VAL_1_PCT (ACTPROB_VAL_MAX / 100)
44 /* Action probability doesn't apply when min is this. */
45 #define ACTPROB_VAL_NA 253
46 /* Action probability unsupported when min is this. */
47 #define ACTPROB_VAL_NOT_IMPL 254
49 static struct action *actions[ACTION_COUNT];
50 static bool actions_initialized = FALSE;
52 static struct action_enabler_list *action_enablers_by_action[ACTION_COUNT];
54 static struct action *action_new(enum gen_action id,
55 enum action_target_kind target_kind,
56 bool hostile);
58 static bool is_enabler_active(const struct action_enabler *enabler,
59 const struct player *actor_player,
60 const struct city *actor_city,
61 const struct impr_type *actor_building,
62 const struct tile *actor_tile,
63 const struct unit *actor_unit,
64 const struct unit_type *actor_unittype,
65 const struct output_type *actor_output,
66 const struct specialist *actor_specialist,
67 const struct player *target_player,
68 const struct city *target_city,
69 const struct impr_type *target_building,
70 const struct tile *target_tile,
71 const struct unit *target_unit,
72 const struct unit_type *target_unittype,
73 const struct output_type *target_output,
74 const struct specialist *target_specialist);
76 static inline bool
77 action_prob_is_signal(const struct act_prob probability);
78 static inline bool
79 action_prob_not_relevant(const struct act_prob probability);
80 static inline bool
81 action_prob_not_impl(const struct act_prob probability);
83 /**************************************************************************
84 Initialize the actions and the action enablers.
85 **************************************************************************/
86 void actions_init(void)
88 /* Hard code the actions */
89 actions[ACTION_SPY_POISON] = action_new(ACTION_SPY_POISON, ATK_CITY,
90 TRUE);
91 actions[ACTION_SPY_SABOTAGE_UNIT] =
92 action_new(ACTION_SPY_SABOTAGE_UNIT, ATK_UNIT,
93 TRUE);
94 actions[ACTION_SPY_BRIBE_UNIT] =
95 action_new(ACTION_SPY_BRIBE_UNIT, ATK_UNIT,
96 TRUE);
97 actions[ACTION_SPY_SABOTAGE_CITY] =
98 action_new(ACTION_SPY_SABOTAGE_CITY, ATK_CITY,
99 TRUE);
100 actions[ACTION_SPY_TARGETED_SABOTAGE_CITY] =
101 action_new(ACTION_SPY_TARGETED_SABOTAGE_CITY, ATK_CITY,
102 TRUE);
103 actions[ACTION_SPY_INCITE_CITY] =
104 action_new(ACTION_SPY_INCITE_CITY, ATK_CITY,
105 TRUE);
106 actions[ACTION_ESTABLISH_EMBASSY] =
107 action_new(ACTION_ESTABLISH_EMBASSY, ATK_CITY,
108 FALSE);
109 actions[ACTION_SPY_STEAL_TECH] =
110 action_new(ACTION_SPY_STEAL_TECH, ATK_CITY,
111 TRUE);
112 actions[ACTION_SPY_TARGETED_STEAL_TECH] =
113 action_new(ACTION_SPY_TARGETED_STEAL_TECH, ATK_CITY,
114 TRUE);
115 actions[ACTION_SPY_INVESTIGATE_CITY] =
116 action_new(ACTION_SPY_INVESTIGATE_CITY, ATK_CITY,
117 TRUE);
118 actions[ACTION_SPY_STEAL_GOLD] =
119 action_new(ACTION_SPY_STEAL_GOLD, ATK_CITY,
120 TRUE);
121 actions[ACTION_TRADE_ROUTE] =
122 action_new(ACTION_TRADE_ROUTE, ATK_CITY,
123 FALSE);
124 actions[ACTION_MARKETPLACE] =
125 action_new(ACTION_MARKETPLACE, ATK_CITY,
126 FALSE);
127 actions[ACTION_HELP_WONDER] =
128 action_new(ACTION_HELP_WONDER, ATK_CITY,
129 FALSE);
131 /* Initialize the action enabler list */
132 action_iterate(act) {
133 action_enablers_by_action[act] = action_enabler_list_new();
134 } action_iterate_end;
136 /* The actions them self are now initialized. */
137 actions_initialized = TRUE;
140 /**************************************************************************
141 Free the actions and the action enablers.
142 **************************************************************************/
143 void actions_free(void)
145 /* Don't consider the actions to be initialized any longer. */
146 actions_initialized = FALSE;
148 action_iterate(act) {
149 action_enabler_list_iterate(action_enablers_by_action[act], enabler) {
150 requirement_vector_free(&enabler->actor_reqs);
151 requirement_vector_free(&enabler->target_reqs);
152 free(enabler);
153 } action_enabler_list_iterate_end;
155 action_enabler_list_destroy(action_enablers_by_action[act]);
157 FC_FREE(actions[act]);
158 } action_iterate_end;
161 /**************************************************************************
162 Returns TRUE iff the actions are initialized.
164 Doesn't care about action enablers.
165 **************************************************************************/
166 bool actions_are_ready(void)
168 if (!actions_initialized) {
169 /* The actions them self aren't initialized yet. */
170 return FALSE;
173 action_iterate(act) {
174 if (actions[act]->ui_name[0] == '\0') {
175 /* An action without a UI name exists means that the ruleset haven't
176 * loaded yet. The ruleset loading will assign a default name to
177 * any actions not named by the ruleset. The client will get this
178 * name from the server. */
179 return FALSE;
181 } action_iterate_end;
183 /* The actions should be ready for use. */
184 return TRUE;
187 /**************************************************************************
188 Create a new action.
189 **************************************************************************/
190 static struct action *action_new(enum gen_action id,
191 enum action_target_kind target_kind,
192 bool hostile)
194 struct action *action;
196 action = fc_malloc(sizeof(*action));
198 action->id = id;
199 action->actor_kind = AAK_UNIT;
200 action->target_kind = target_kind;
201 action->hostile = hostile;
203 /* Loaded from the ruleset. Until generalized actions are ready it has to
204 * be defined seperatly from other action data. */
205 action->ui_name[0] = '\0';
206 action->quiet = FALSE;
208 return action;
211 /**************************************************************************
212 Returns TRUE iff the specified action ID refers to a valid action.
213 **************************************************************************/
214 bool action_id_is_valid(const int action_id)
216 return gen_action_is_valid(action_id);
219 /**************************************************************************
220 Return the action with the given id.
222 Returns NULL if no action with the given id exists.
223 **************************************************************************/
224 struct action *action_by_number(int action_id)
226 if (!action_id_is_valid(action_id)) {
227 /* Nothing to return. */
229 log_verbose("Asked for non existing action numbered %d", action_id);
231 return NULL;
234 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
236 return actions[action_id];
239 /**************************************************************************
240 Get the actor kind of an action.
241 **************************************************************************/
242 enum action_actor_kind action_get_actor_kind(const struct action *paction)
244 fc_assert_ret_val_msg(paction, AAK_COUNT, "Action doesn't exist.");
246 return paction->actor_kind;
249 /**************************************************************************
250 Get the target kind of an action.
251 **************************************************************************/
252 enum action_target_kind action_get_target_kind(
253 const struct action *paction)
255 fc_assert_ret_val_msg(paction, ATK_COUNT, "Action doesn't exist.");
257 return paction->target_kind;
260 /**************************************************************************
261 Returns TRUE iff the specified action is hostile.
262 **************************************************************************/
263 bool action_is_hostile(int action_id)
265 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
267 return actions[action_id]->hostile;
270 /**************************************************************************
271 Get the rule name of the action.
272 **************************************************************************/
273 const char *action_id_rule_name(int action_id)
275 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
277 return gen_action_name(action_id);
280 /**************************************************************************
281 Get the action name used when displaying the action in the UI. Nothing
282 is added to the UI name.
283 **************************************************************************/
284 const char *action_id_name_translation(int action_id)
286 return action_prepare_ui_name(action_id, "", ACTPROB_NA, NULL);
289 /**************************************************************************
290 Get the action name with a mnemonic ready to display in the UI.
291 **************************************************************************/
292 const char *action_get_ui_name_mnemonic(int action_id,
293 const char* mnemonic)
295 return action_prepare_ui_name(action_id, mnemonic, ACTPROB_NA, NULL);
298 /**************************************************************************
299 Get the UI name ready to show the action in the UI. It is possible to
300 add a client specific mnemonic. Success probability information is
301 interpreted and added to the text. A custom text can be inserted before
302 the probability information.
303 **************************************************************************/
304 const char *action_prepare_ui_name(int action_id, const char* mnemonic,
305 const struct act_prob prob,
306 const char* custom)
308 static struct astring str = ASTRING_INIT;
309 static struct astring chance = ASTRING_INIT;
311 /* Text representation of the probability. */
312 const char* probtxt;
314 if (!actions_are_ready()) {
315 /* Could be a client who haven't gotten the ruleset yet */
317 /* so there shouldn't be any action probability to show */
318 fc_assert(action_prob_not_relevant(prob));
320 /* but the action should be valid */
321 fc_assert_ret_val_msg(action_id_is_valid(action_id),
322 "Invalid action",
323 "Invalid action %d", action_id);
325 /* and no custom text will be inserted */
326 fc_assert(custom == NULL || custom[0] == '\0');
328 /* Make the best of what is known */
329 astr_set(&str, _("%s%s (name may be wrong)"),
330 mnemonic, gen_action_name(action_id));
332 /* Return the guess. */
333 return astr_str(&str);
336 /* How to interpret action probabilities like prob is documented in
337 * fc_types.h */
338 if (action_prob_is_signal(prob)) {
339 fc_assert(action_prob_not_impl(prob)
340 || action_prob_not_relevant(prob));
342 /* Unknown because of missing server support or should not exits. */
343 probtxt = NULL;
344 } else {
345 if (prob.min == prob.max) {
346 /* Only one probability in range. */
348 /* TRANS: the probability that an action will succeed. Given in
349 * percentage. Resolution is 0.5%. */
350 astr_set(&chance, _("%.1f%%"), (double)prob.max / ACTPROB_VAL_1_PCT);
351 } else {
352 /* TRANS: the interval (end points included) where the probability of
353 * the action's success is. Given in percentage. Resolution is 0.5%. */
354 astr_set(&chance, _("[%.1f%%, %.1f%%]"),
355 (double)prob.min / ACTPROB_VAL_1_PCT,
356 (double)prob.max / ACTPROB_VAL_1_PCT);
358 probtxt = astr_str(&chance);
361 /* Format the info part of the action's UI name. */
362 if (probtxt != NULL && custom != NULL) {
363 /* TRANS: action UI name's info part with custom info and probability.
364 * Hint: you can move the paren handling from this sting to the action
365 * names if you need to add extra information (like a mnemonic letter
366 * that doesn't appear in the action UI name) to it. In that case you
367 * must do so for all strings with this comment and for every action
368 * name. To avoid a `()` when no UI name info part is added you have
369 * to add the extra information to every action name or remove the
370 * surrounding parens. */
371 astr_set(&chance, _(" (%s; %s)"), custom, probtxt);
372 } else if (probtxt != NULL) {
373 /* TRANS: action UI name's info part with probability.
374 * Hint: you can move the paren handling from this sting to the action
375 * names if you need to add extra information (like a mnemonic letter
376 * that doesn't appear in the action UI name) to it. In that case you
377 * must do so for all strings with this comment and for every action
378 * name. To avoid a `()` when no UI name info part is added you have
379 * to add the extra information to every action name or remove the
380 * surrounding parens. */
381 astr_set(&chance, _(" (%s)"), probtxt);
382 } else if (custom != NULL) {
383 /* TRANS: action UI name's info part with custom info.
384 * Hint: you can move the paren handling from this sting to the action
385 * names if you need to add extra information (like a mnemonic letter
386 * that doesn't appear in the action UI name) to it. In that case you
387 * must do so for all strings with this comment and for every action
388 * name. To avoid a `()` when no UI name info part is added you have
389 * to add the extra information to every action name or remove the
390 * surrounding parens. */
391 astr_set(&chance, _(" (%s)"), custom);
392 } else {
393 /* No info part to display. */
394 astr_clear(&chance);
397 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
399 astr_set(&str, _(actions[action_id]->ui_name), mnemonic,
400 astr_str(&chance));
402 return astr_str(&str);
405 /**************************************************************************
406 Get information about starting the action in the current situation.
407 Suitable for a tool tip for the button that starts it.
408 **************************************************************************/
409 const char *action_get_tool_tip(const int action_id,
410 const struct act_prob prob)
412 static struct astring tool_tip = ASTRING_INIT;
414 if (action_prob_is_signal(prob)) {
415 fc_assert(action_prob_not_impl(prob));
417 /* Missing server support. No in game action will change this. */
418 astr_clear(&tool_tip);
419 } else if (prob.min == prob.max) {
420 /* TRANS: action probability of success. Given in percentage.
421 * Resolution is 0.5%. */
422 astr_set(&tool_tip, _("The probability of success is %.1f%%."),
423 (double)prob.max / ACTPROB_VAL_1_PCT);
424 } else {
425 astr_set(&tool_tip,
426 /* TRANS: action probability range (min to max). Given in
427 * percentage. Resolution is 0.5%. */
428 _("The probability of success is %.1f%%, %.1f%% or somewhere"
429 " in between."),
430 (double)prob.min / ACTPROB_VAL_1_PCT,
431 (double)prob.max / ACTPROB_VAL_1_PCT);
434 return astr_str(&tool_tip);
437 /**************************************************************************
438 Get the unit type role corresponding to the ability to do action.
439 **************************************************************************/
440 int action_get_role(int action_id)
442 fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
444 fc_assert_msg(AAK_UNIT == action_id_get_actor_kind(action_id),
445 "Action %s isn't performed by a unit",
446 action_id_rule_name(action_id));
448 return action_id + L_LAST;
451 /**************************************************************************
452 Create a new action enabler.
453 **************************************************************************/
454 struct action_enabler *action_enabler_new(void)
456 struct action_enabler *enabler;
458 enabler = fc_malloc(sizeof(*enabler));
459 requirement_vector_init(&enabler->actor_reqs);
460 requirement_vector_init(&enabler->target_reqs);
462 /* Make sure that action doesn't end up as a random value that happens to
463 * be a valid action id. */
464 enabler->action = ACTION_NONE;
466 return enabler;
469 /**************************************************************************
470 Add an action enabler to the current ruleset.
471 **************************************************************************/
472 void action_enabler_add(struct action_enabler *enabler)
474 /* Sanity check: a non existing action enabler can't be added. */
475 fc_assert_ret(enabler);
476 /* Sanity check: a non existing action doesn't have enablers. */
477 fc_assert_ret(action_id_is_valid(enabler->action));
479 action_enabler_list_append(
480 action_enablers_for_action(enabler->action),
481 enabler);
484 /**************************************************************************
485 Remove an action enabler from the current ruleset.
487 Returns TRUE on success.
488 **************************************************************************/
489 bool action_enabler_remove(struct action_enabler *enabler)
491 /* Sanity check: a non existing action enabler can't be removed. */
492 fc_assert_ret_val(enabler, FALSE);
493 /* Sanity check: a non existing action doesn't have enablers. */
494 fc_assert_ret_val(action_id_is_valid(enabler->action), FALSE);
496 return action_enabler_list_remove(
497 action_enablers_for_action(enabler->action),
498 enabler);
501 /**************************************************************************
502 Get all enablers for an action in the current ruleset.
503 **************************************************************************/
504 struct action_enabler_list *
505 action_enablers_for_action(enum gen_action action)
507 /* Sanity check: a non existing action doesn't have enablers. */
508 fc_assert_ret_val(action_id_is_valid(action), NULL);
510 return action_enablers_by_action[action];
513 /**************************************************************************
514 Returns TRUE if the wanted action is possible given that an action
515 enabler later will enable it.
517 This is done by checking the action's hard requirements. Hard requirements
518 must be TRUE before an action can be done. The reason why is usually that
519 code dealing with the action assumes that the requirements are true. A
520 requirement may also end up here if it can't be expressed in a requirement
521 vector or if its abstence makes the action pointless.
523 When adding a new hard requirement here:
524 * explain why it is a hard requirement in a comment.
525 * remember that this is called from action_prob(). Should information
526 the player don't have access to be used in a test it must check if
527 this evaluation is omniscient.
528 **************************************************************************/
529 static bool is_action_possible(const enum gen_action wanted_action,
530 const struct player *actor_player,
531 const struct city *actor_city,
532 const struct impr_type *actor_building,
533 const struct tile *actor_tile,
534 const struct unit *actor_unit,
535 const struct unit_type *actor_unittype,
536 const struct output_type *actor_output,
537 const struct specialist *actor_specialist,
538 const struct player *target_player,
539 const struct city *target_city,
540 const struct impr_type *target_building,
541 const struct tile *target_tile,
542 const struct unit *target_unit,
543 const struct unit_type *target_unittype,
544 const struct output_type *target_output,
545 const struct specialist *target_specialist,
546 const bool omniscient,
547 const struct city *homecity,
548 bool ignore_dist)
550 fc_assert_msg((action_id_get_target_kind(wanted_action) == ATK_CITY
551 && target_city != NULL)
552 || (action_id_get_target_kind(wanted_action) == ATK_UNIT
553 && target_unit != NULL),
554 "Missing target!");
556 if (!ignore_dist && action_id_get_actor_kind(wanted_action) == AAK_UNIT) {
557 /* The Freeciv code for all actions controlled by enablers assumes that
558 * an acting unit is on the same tile as its target or on the tile next
559 * to it. */
560 if (real_map_distance(actor_tile, target_tile) > 1) {
561 return FALSE;
565 if (action_id_get_target_kind(wanted_action) == ATK_UNIT) {
566 /* The Freeciv code for all actions with a unit target that is
567 * controlled by action enablers assumes that the acting player can see
568 * the target unit. */
569 if (!can_player_see_unit(actor_player, target_unit)) {
570 return FALSE;
574 if (wanted_action == ACTION_ESTABLISH_EMBASSY
575 || wanted_action == ACTION_SPY_INVESTIGATE_CITY
576 || wanted_action == ACTION_SPY_STEAL_GOLD
577 || wanted_action == ACTION_SPY_STEAL_TECH
578 || wanted_action == ACTION_SPY_TARGETED_STEAL_TECH
579 || wanted_action == ACTION_SPY_INCITE_CITY
580 || wanted_action == ACTION_SPY_BRIBE_UNIT) {
581 /* Why this is a hard requirement: There is currently no point in
582 * allowing the listed actions against domestic targets.
583 * (Possible counter argument: crazy hack involving the Lua callback
584 * action_started_callback() to use an action to do something else. */
585 /* Info leak: The actor player knows what targets he owns. */
586 /* TODO: Move this restriction to the ruleset as a part of false flag
587 * operation support. */
588 if (actor_player == target_player) {
589 return FALSE;
593 /* Hard requirements for individual actions. */
594 switch (wanted_action) {
595 case ACTION_SPY_BRIBE_UNIT:
596 /* Why this is a hard requirement: Can't transfer a unique unit if the
597 * actor player already has one. */
598 if (utype_player_already_has_this_unique(actor_player,
599 target_unittype)) {
600 return FALSE;
603 break;
605 case ACTION_ESTABLISH_EMBASSY:
606 /* Why this is a hard requirement: There is currently no point in
607 * establishing an embassy when a real embassy already exists.
608 * (Possible exception: crazy hack using the Lua callback
609 * action_started_callback() to make establish embassy do something
610 * else even if the UI still call the action Establish Embassy) */
611 /* Info leak: The actor player known who he has a real embassy to. */
612 if (player_has_real_embassy(actor_player, target_player)) {
613 return FALSE;
616 break;
618 case ACTION_SPY_TARGETED_STEAL_TECH:
619 /* Reason: The Freeciv code don't support selecting a target tech
620 * unless it is known that the victim player has it. */
621 /* Info leak: The actor player knowns who's techs he can see. */
622 if (!can_see_techs_of_target(actor_player, target_player)) {
623 return FALSE;
626 break;
628 case ACTION_SPY_STEAL_GOLD:
629 /* If actor_unit can do the action the actor_player can see how much
630 * gold target_player have. Not requireing it is therefore pointless.
632 if (target_player->economic.gold <= 0) {
633 return FALSE;
636 break;
638 case ACTION_TRADE_ROUTE:
639 case ACTION_MARKETPLACE:
641 /* It isn't possible to establish a trade route from a non existing
642 * city. The Freeciv code assumes this applies to Enter Marketplace
643 * too. */
644 if (homecity == NULL) {
645 return FALSE;
648 /* Can't establish a trade route or enter the market place if the
649 * cities can't trade at all. */
650 /* TODO: Should this restriction (and the above restriction that the
651 * actor unit must have a home city) be kept for Enter Marketplace? */
652 if (!can_cities_trade(homecity, target_city)) {
653 return FALSE;
656 /* Allow a ruleset to forbid units from entering the marketplace if a
657 * trade route can be established in stead. */
658 if (wanted_action == ACTION_MARKETPLACE
659 && game.info.force_trade_route
660 && is_action_enabled_unit_on_city(ACTION_TRADE_ROUTE,
661 actor_unit, target_city)) {
662 return FALSE;
665 /* There are more restrictions on establishing a trade route than on
666 * entering the market place. */
667 if (wanted_action == ACTION_TRADE_ROUTE &&
668 !can_establish_trade_route(homecity, target_city)) {
669 return FALSE;
673 break;
675 case ACTION_HELP_WONDER:
676 /* It is only possible to help the production of a wonder. */
677 /* Info leak: It is already known when a foreign city is building a
678 * wonder. */
679 /* TODO: Do this rule belong in the ruleset? */
680 if (!(VUT_IMPROVEMENT == target_city->production.kind
681 && is_wonder(target_city->production.value.building))) {
682 return FALSE;
685 /* It is only possible to help the production if the production needs
686 * the help. (If not it would be possible to add shields for a non
687 * wonder if it is build after a wonder) */
688 /* Info leak: No new information is sent with the old rules. When the
689 * ruleset is changed to make helping foreign wonders legal the
690 * information that a wonder have been hurried (bought, helped) leaks.
691 * That a foreign wonder will be ready next turn (from work) is already
692 * known. That it will be finished because of help is not. */
693 if (!(target_city->shield_stock
694 < impr_build_shield_cost(
695 target_city->production.value.building))) {
696 return FALSE;
699 break;
701 case ACTION_SPY_INVESTIGATE_CITY:
702 case ACTION_SPY_POISON:
703 case ACTION_SPY_SABOTAGE_CITY:
704 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
705 case ACTION_SPY_STEAL_TECH:
706 case ACTION_SPY_INCITE_CITY:
707 case ACTION_SPY_SABOTAGE_UNIT:
708 /* No known hard coded requirements. */
709 break;
710 case ACTION_COUNT:
711 fc_assert(action_id_is_valid(wanted_action));
712 break;
715 return TRUE;
718 /**************************************************************************
719 Return TRUE iff the action enabler is active
720 **************************************************************************/
721 static bool is_enabler_active(const struct action_enabler *enabler,
722 const struct player *actor_player,
723 const struct city *actor_city,
724 const struct impr_type *actor_building,
725 const struct tile *actor_tile,
726 const struct unit *actor_unit,
727 const struct unit_type *actor_unittype,
728 const struct output_type *actor_output,
729 const struct specialist *actor_specialist,
730 const struct player *target_player,
731 const struct city *target_city,
732 const struct impr_type *target_building,
733 const struct tile *target_tile,
734 const struct unit *target_unit,
735 const struct unit_type *target_unittype,
736 const struct output_type *target_output,
737 const struct specialist *target_specialist)
739 return are_reqs_active(actor_player, target_player, actor_city,
740 actor_building, actor_tile,
741 actor_unit, actor_unittype,
742 actor_output, actor_specialist,
743 &enabler->actor_reqs, RPT_CERTAIN)
744 && are_reqs_active(target_player, actor_player, target_city,
745 target_building, target_tile,
746 target_unit, target_unittype,
747 target_output, target_specialist,
748 &enabler->target_reqs, RPT_CERTAIN);
751 /**************************************************************************
752 Returns TRUE if the wanted action is enabled.
754 Note that the action may disable it self because of hard requirements
755 even if an action enabler returns TRUE.
756 **************************************************************************/
757 static bool is_action_enabled(const enum gen_action wanted_action,
758 const struct player *actor_player,
759 const struct city *actor_city,
760 const struct impr_type *actor_building,
761 const struct tile *actor_tile,
762 const struct unit *actor_unit,
763 const struct unit_type *actor_unittype,
764 const struct output_type *actor_output,
765 const struct specialist *actor_specialist,
766 const struct player *target_player,
767 const struct city *target_city,
768 const struct impr_type *target_building,
769 const struct tile *target_tile,
770 const struct unit *target_unit,
771 const struct unit_type *target_unittype,
772 const struct output_type *target_output,
773 const struct specialist *target_specialist,
774 const struct city *homecity, bool ignore_dist)
776 if (!is_action_possible(wanted_action,
777 actor_player, actor_city,
778 actor_building, actor_tile,
779 actor_unit, actor_unittype,
780 actor_output, actor_specialist,
781 target_player, target_city,
782 target_building, target_tile,
783 target_unit, target_unittype,
784 target_output, target_specialist,
785 TRUE, homecity, ignore_dist)) {
786 /* The action enablers are irrelevant since the action it self is
787 * impossible. */
788 return FALSE;
791 action_enabler_list_iterate(action_enablers_for_action(wanted_action),
792 enabler) {
793 if (is_enabler_active(enabler, actor_player, actor_city,
794 actor_building, actor_tile,
795 actor_unit, actor_unittype,
796 actor_output, actor_specialist,
797 target_player, target_city,
798 target_building, target_tile,
799 target_unit, target_unittype,
800 target_output, target_specialist)) {
801 return TRUE;
803 } action_enabler_list_iterate_end;
805 return FALSE;
808 /**************************************************************************
809 Returns TRUE if actor_unit can do wanted_action to target_city as far as
810 action enablers are concerned.
812 See note in is_action_enabled for why the action may still be disabled.
813 **************************************************************************/
814 bool is_action_enabled_unit_on_city(const enum gen_action wanted_action,
815 const struct unit *actor_unit,
816 const struct city *target_city)
818 return is_action_enabled_unit_on_city_full(wanted_action, actor_unit,
819 target_city,
820 game_city_by_number(actor_unit->homecity),
821 FALSE);
824 /**************************************************************************
825 Returns TRUE if actor_unit can do wanted_action to target_city as far as
826 action enablers are concerned.
828 See note in is_action_enabled for why the action may still be disabled.
829 **************************************************************************/
830 bool is_action_enabled_unit_on_city_full(const enum gen_action wanted_action,
831 const struct unit *actor_unit,
832 const struct city *target_city,
833 const struct city *homecity,
834 bool ignore_dist)
836 struct tile *actor_tile = unit_tile(actor_unit);
838 if (actor_unit == NULL || target_city == NULL) {
839 /* Can't do an action when actor or target are missing. */
840 return FALSE;
843 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(wanted_action),
844 FALSE, "Action %s is performed by %s not %s",
845 gen_action_name(wanted_action),
846 action_actor_kind_name(
847 action_id_get_actor_kind(wanted_action)),
848 action_actor_kind_name(AAK_UNIT));
850 fc_assert_ret_val_msg(ATK_CITY
851 == action_id_get_target_kind(wanted_action),
852 FALSE, "Action %s is against %s not %s",
853 gen_action_name(wanted_action),
854 action_target_kind_name(
855 action_id_get_target_kind(wanted_action)),
856 action_target_kind_name(ATK_CITY));
858 if (!unit_can_do_action(actor_unit, wanted_action)) {
859 /* No point in continuing. */
860 return FALSE;
863 return is_action_enabled(wanted_action,
864 unit_owner(actor_unit), tile_city(actor_tile),
865 NULL, actor_tile,
866 actor_unit, unit_type_get(actor_unit),
867 NULL, NULL,
868 city_owner(target_city), target_city, NULL,
869 city_tile(target_city), NULL, NULL, NULL, NULL,
870 homecity, ignore_dist);
873 /**************************************************************************
874 Returns TRUE if actor_unit can do wanted_action to target_unit as far as
875 action enablers are concerned.
877 See note in is_action_enabled() for why the action still may be
878 disabled.
879 **************************************************************************/
880 bool is_action_enabled_unit_on_unit(const enum gen_action wanted_action,
881 const struct unit *actor_unit,
882 const struct unit *target_unit)
884 struct tile *actor_tile = unit_tile(actor_unit);
886 if (actor_unit == NULL || target_unit == NULL) {
887 /* Can't do an action when actor or target are missing. */
888 return FALSE;
891 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(wanted_action),
892 FALSE, "Action %s is performed by %s not %s",
893 gen_action_name(wanted_action),
894 action_actor_kind_name(
895 action_id_get_actor_kind(wanted_action)),
896 action_actor_kind_name(AAK_UNIT));
898 fc_assert_ret_val_msg(ATK_UNIT
899 == action_id_get_target_kind(wanted_action),
900 FALSE, "Action %s is against %s not %s",
901 gen_action_name(wanted_action),
902 action_target_kind_name(
903 action_id_get_target_kind(wanted_action)),
904 action_target_kind_name(ATK_UNIT));
906 if (!unit_can_do_action(actor_unit, wanted_action)) {
907 /* No point in continuing. */
908 return FALSE;
911 return is_action_enabled(wanted_action,
912 unit_owner(actor_unit), tile_city(actor_tile),
913 NULL, actor_tile,
914 actor_unit, unit_type_get(actor_unit),
915 NULL, NULL,
916 unit_owner(target_unit),
917 tile_city(unit_tile(target_unit)), NULL,
918 unit_tile(target_unit),
919 target_unit, unit_type_get(target_unit),
920 NULL, NULL,
921 game_city_by_number(actor_unit->homecity),
922 FALSE);
925 /**************************************************************************
926 Find out if the action is enabled, may be enabled or isn't enabled given
927 what the player owning the actor knowns.
929 A player don't always know everything needed to figure out if an action
930 is enabled or not. A server side AI with the same limits on its knowledge
931 as a human player or a client should use this to figure out what is what.
933 Assumes to be called from the point of view of the actor. Its knowledge
934 is assumed to be given in the parameters.
936 Returns TRI_YES if the action is enabled, TRI_NO if it isn't and
937 TRI_MAYBE if the player don't know enough to tell.
939 If meta knowledge is missing TRI_MAYBE will be returned.
940 **************************************************************************/
941 static enum fc_tristate
942 action_enabled_local(const enum gen_action wanted_action,
943 const struct player *actor_player,
944 const struct city *actor_city,
945 const struct impr_type *actor_building,
946 const struct tile *actor_tile,
947 const struct unit *actor_unit,
948 const struct output_type *actor_output,
949 const struct specialist *actor_specialist,
950 const struct player *target_player,
951 const struct city *target_city,
952 const struct impr_type *target_building,
953 const struct tile *target_tile,
954 const struct unit *target_unit,
955 const struct output_type *target_output,
956 const struct specialist *target_specialist)
958 enum fc_tristate current;
959 enum fc_tristate result;
961 result = TRI_NO;
962 action_enabler_list_iterate(action_enablers_for_action(wanted_action),
963 enabler) {
964 current = tri_and(mke_eval_reqs(actor_player, actor_player,
965 target_player, actor_city,
966 actor_building, actor_tile,
967 actor_unit, actor_output,
968 actor_specialist,
969 &enabler->actor_reqs, RPT_CERTAIN),
970 mke_eval_reqs(actor_player, target_player,
971 actor_player, target_city,
972 target_building, target_tile,
973 target_unit, target_output,
974 target_specialist,
975 &enabler->target_reqs, RPT_CERTAIN));
976 if (current == TRI_YES) {
977 return TRI_YES;
978 } else if (current == TRI_MAYBE) {
979 result = TRI_MAYBE;
981 } action_enabler_list_iterate_end;
983 return result;
986 /**************************************************************************
987 Find out if the effect value is known
989 The knowledge of the actor is assumed to be given in the parameters.
991 If meta knowledge is missing TRI_MAYBE will be returned.
992 **************************************************************************/
993 static bool is_effect_val_known(enum effect_type effect_type,
994 const struct player *pow_player,
995 const struct player *target_player,
996 const struct player *other_player,
997 const struct city *target_city,
998 const struct impr_type *target_building,
999 const struct tile *target_tile,
1000 const struct unit *target_unit,
1001 const struct output_type *target_output,
1002 const struct specialist *target_specialist)
1004 effect_list_iterate(get_effects(effect_type), peffect) {
1005 if (TRI_MAYBE == mke_eval_reqs(pow_player, target_player,
1006 other_player, target_city,
1007 target_building, target_tile,
1008 target_unit, target_output,
1009 target_specialist,
1010 &(peffect->reqs), RPT_CERTAIN)) {
1011 return FALSE;
1013 } effect_list_iterate_end;
1015 return TRUE;
1018 /**************************************************************************
1019 Does the target has any techs the actor don't?
1020 **************************************************************************/
1021 static enum fc_tristate
1022 tech_can_be_stolen(const struct player *actor_player,
1023 const struct player *target_player)
1025 const struct research *actor_research = research_get(actor_player);
1026 const struct research *target_research = research_get(target_player);
1028 if (actor_research != target_research) {
1029 if (can_see_techs_of_target(actor_player, target_player)) {
1030 advance_iterate(A_FIRST, padvance) {
1031 Tech_type_id i = advance_number(padvance);
1033 if (research_invention_state(target_research, i) == TECH_KNOWN
1034 && research_invention_gettable(actor_research, i,
1035 game.info.tech_steal_allow_holes)
1036 && (research_invention_state(actor_research, i) == TECH_UNKNOWN
1037 || (research_invention_state(actor_research, i)
1038 == TECH_PREREQS_KNOWN))) {
1039 return TRI_YES;
1041 } advance_iterate_end;
1042 } else {
1043 return TRI_MAYBE;
1047 return TRI_NO;
1050 /**************************************************************************
1051 The action probability that pattacker will win a diplomatic battle.
1053 It is assumed that pattacker and pdefender have different owners and that
1054 the defender can defend in a diplomatic battle.
1056 See diplomat_success_vs_defender() in server/diplomats.c
1057 **************************************************************************/
1058 static struct act_prob ap_dipl_battle_win(const struct unit *pattacker,
1059 const struct unit *pdefender)
1061 struct city *pcity;
1063 /* Keep unconverted until the end to avoid scaling each step */
1064 int chance;
1065 struct act_prob out;
1067 /* Superspy always win */
1068 if (unit_has_type_flag(pdefender, UTYF_SUPERSPY)) {
1069 /* A defending UTYF_SUPERSPY will defeat every possible attacker. */
1070 return ACTPROB_IMPOSSIBLE;
1072 if (unit_has_type_flag(pattacker, UTYF_SUPERSPY)) {
1073 /* An attacking UTYF_SUPERSPY will defeat every possible defender
1074 * except another UTYF_SUPERSPY. */
1075 return ACTPROB_CERTAIN;
1078 /* Base chance is 50% */
1079 chance = 50;
1081 /* Spy attack bonus */
1082 if (unit_has_type_flag(pattacker, UTYF_SPY)) {
1083 chance += 25;
1086 /* Spy defense bonus */
1087 if (unit_has_type_flag(pdefender, UTYF_SPY)) {
1088 chance -= 25;
1091 /* Veteran attack and defense bonus */
1093 const struct veteran_level *vatt =
1094 utype_veteran_level(unit_type_get(pattacker), pattacker->veteran);
1095 const struct veteran_level *vdef =
1096 utype_veteran_level(unit_type_get(pdefender), pdefender->veteran);
1098 chance += vatt->power_fact - vdef->power_fact;
1101 /* City and base defense bonuses */
1102 if (tile_has_base_flag_for_unit(pdefender->tile, unit_type_get(pdefender),
1103 BF_DIPLOMAT_DEFENSE)) {
1104 chance -= chance * 25 / 100;
1107 pcity = tile_city(pdefender->tile);
1108 if (pcity) {
1109 if (!is_effect_val_known(EFT_SPY_RESISTANT, unit_owner(pattacker),
1110 city_owner(pcity), NULL, pcity, NULL,
1111 city_tile(pcity), NULL, NULL, NULL)) {
1112 return ACTPROB_NOT_KNOWN;
1115 chance -= chance * get_city_bonus(tile_city(pdefender->tile),
1116 EFT_SPY_RESISTANT) / 100;
1119 /* Convert to action probability */
1120 out.min = chance * ACTPROB_VAL_1_PCT;
1121 out.max = chance * ACTPROB_VAL_1_PCT;
1123 return out;
1126 /**************************************************************************
1127 The action probability that pattacker will win a diplomatic battle.
1129 See diplomat_infiltrate_tile() in server/diplomats.c
1130 **************************************************************************/
1131 static struct act_prob ap_diplomat_battle(const struct unit *pattacker,
1132 const struct unit *pvictim)
1134 unit_list_iterate(unit_tile(pvictim)->units, punit) {
1135 if (unit_owner(punit) == unit_owner(pattacker)) {
1136 /* Won't defend against its owner. */
1137 continue;
1140 if (punit == pvictim
1141 && !unit_has_type_flag(punit, UTYF_SUPERSPY)) {
1142 /* The victim unit is defenseless unless it's a SuperSpy.
1143 * Rationalization: A regular diplomat don't mind being bribed. A
1144 * SuperSpy is high enough up the chain that accepting a bribe is
1145 * against his own interests. */
1146 continue;
1149 if (!(unit_has_type_flag(punit, UTYF_DIPLOMAT)
1150 || unit_has_type_flag(punit, UTYF_SUPERSPY))) {
1151 /* The unit can't defend. */
1152 continue;
1155 /* There will be a diplomatic battle in stead of an action. */
1156 return ap_dipl_battle_win(pattacker, punit);
1157 } unit_list_iterate_end;
1159 /* No diplomatic battle will occur. */
1160 return ACTPROB_CERTAIN;
1163 /**************************************************************************
1164 An action's probability of success.
1166 "Success" indicates that the action achieves its goal, not that the
1167 actor survives. For actions that cost money it is assumed that the
1168 player has and is willing to spend the money. This is so the player can
1169 figure out what his odds are before deciding to get the extra money.
1170 **************************************************************************/
1171 static struct act_prob
1172 action_prob(const enum gen_action wanted_action,
1173 const struct player *actor_player,
1174 const struct city *actor_city,
1175 const struct impr_type *actor_building,
1176 const struct tile *actor_tile,
1177 const struct unit *actor_unit,
1178 const struct output_type *actor_output,
1179 const struct specialist *actor_specialist,
1180 const struct player *target_player,
1181 const struct city *target_city,
1182 const struct impr_type *target_building,
1183 const struct tile *target_tile,
1184 const struct unit *target_unit,
1185 const struct output_type *target_output,
1186 const struct specialist *target_specialist)
1188 int known;
1189 struct act_prob chance;
1190 const struct unit_type *actor_unittype;
1191 const struct unit_type *target_unittype;
1192 const struct city *homecity;
1194 if (actor_unit == NULL) {
1195 actor_unittype = NULL;
1196 } else {
1197 actor_unittype = unit_type_get(actor_unit);
1200 if (target_unit == NULL) {
1201 target_unittype = NULL;
1202 } else {
1203 target_unittype = unit_type_get(target_unit);
1206 if (actor_unit != NULL) {
1207 homecity = game_city_by_number(actor_unit->homecity);
1208 } else {
1209 homecity = NULL;
1212 if (!is_action_possible(wanted_action,
1213 actor_player, actor_city,
1214 actor_building, actor_tile,
1215 actor_unit, actor_unittype,
1216 actor_output, actor_specialist,
1217 target_player, target_city,
1218 target_building, target_tile,
1219 target_unit, target_unittype,
1220 target_output, target_specialist,
1221 FALSE, homecity, FALSE)) {
1222 /* The action enablers are irrelevant since the action it self is
1223 * impossible. */
1224 return ACTPROB_IMPOSSIBLE;
1227 chance = ACTPROB_NOT_IMPLEMENTED;
1229 known = action_enabled_local(wanted_action,
1230 actor_player, actor_city,
1231 actor_building, actor_tile, actor_unit,
1232 actor_output, actor_specialist,
1233 target_player, target_city,
1234 target_building, target_tile, target_unit,
1235 target_output, target_specialist);
1237 switch (wanted_action) {
1238 case ACTION_SPY_POISON:
1239 /* TODO */
1240 break;
1241 case ACTION_SPY_STEAL_GOLD:
1242 /* TODO */
1243 break;
1244 case ACTION_SPY_SABOTAGE_UNIT:
1245 /* All uncertainty comes from potential diplomatic battles. */
1246 chance = ap_diplomat_battle(actor_unit, target_unit);
1247 break;
1248 case ACTION_SPY_BRIBE_UNIT:
1249 /* All uncertainty comes from potential diplomatic battles. */
1250 chance = ap_diplomat_battle(actor_unit, target_unit);;
1251 break;
1252 case ACTION_SPY_SABOTAGE_CITY:
1253 /* TODO */
1254 break;
1255 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
1256 /* TODO */
1257 break;
1258 case ACTION_SPY_INCITE_CITY:
1259 /* TODO */
1260 break;
1261 case ACTION_ESTABLISH_EMBASSY:
1262 chance = ACTPROB_CERTAIN;
1263 break;
1264 case ACTION_SPY_STEAL_TECH:
1265 /* Do the victim have anything worth taking? */
1266 known = tri_and(known,
1267 tech_can_be_stolen(actor_player, target_player));
1269 /* TODO: Calculate actual chance */
1271 break;
1272 case ACTION_SPY_TARGETED_STEAL_TECH:
1273 /* Do the victim have anything worth taking? */
1274 known = tri_and(known,
1275 tech_can_be_stolen(actor_player, target_player));
1277 /* TODO: Calculate actual chance */
1279 break;
1280 case ACTION_SPY_INVESTIGATE_CITY:
1281 /* There is no risk that the city won't get investigated. */
1282 chance = ACTPROB_CERTAIN;
1283 break;
1284 case ACTION_TRADE_ROUTE:
1285 /* TODO */
1286 break;
1287 case ACTION_MARKETPLACE:
1288 /* Possible when not blocked by is_action_possible() */
1289 chance = ACTPROB_CERTAIN;
1290 break;
1291 case ACTION_HELP_WONDER:
1292 /* Possible when not blocked by is_action_possible() */
1293 chance = ACTPROB_CERTAIN;
1294 break;
1295 case ACTION_COUNT:
1296 fc_assert(FALSE);
1297 break;
1300 /* Non signal action probabilities should be in range. */
1301 fc_assert_action((action_prob_is_signal(chance)
1302 || chance.max <= ACTPROB_VAL_MAX),
1303 chance.max = ACTPROB_VAL_MAX);
1304 fc_assert_action((action_prob_is_signal(chance)
1305 || chance.min >= ACTPROB_VAL_MIN),
1306 chance.min = ACTPROB_VAL_MIN);
1308 switch (known) {
1309 case TRI_NO:
1310 return ACTPROB_IMPOSSIBLE;
1311 break;
1312 case TRI_MAYBE:
1313 return ACTPROB_NOT_KNOWN;
1314 break;
1315 case TRI_YES:
1316 return chance;
1317 break;
1320 fc_assert_ret_val_msg(FALSE, ACTPROB_NOT_IMPLEMENTED,
1321 "Should be yes, maybe or no");
1324 /**************************************************************************
1325 Get the actor unit's probability of successfully performing the chosen
1326 action on the target city.
1327 **************************************************************************/
1328 struct act_prob action_prob_vs_city(const struct unit *actor_unit,
1329 const int action_id,
1330 const struct city *target_city)
1332 struct tile *actor_tile = unit_tile(actor_unit);
1334 if (actor_unit == NULL || target_city == NULL) {
1335 /* Can't do an action when actor or target are missing. */
1336 return ACTPROB_IMPOSSIBLE;
1339 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(action_id),
1340 ACTPROB_IMPOSSIBLE,
1341 "Action %s is performed by %s not %s",
1342 gen_action_name(action_id),
1343 action_actor_kind_name(
1344 action_id_get_actor_kind(action_id)),
1345 action_actor_kind_name(AAK_UNIT));
1347 fc_assert_ret_val_msg(ATK_CITY == action_id_get_target_kind(action_id),
1348 ACTPROB_IMPOSSIBLE,
1349 "Action %s is against %s not %s",
1350 gen_action_name(action_id),
1351 action_target_kind_name(
1352 action_id_get_target_kind(action_id)),
1353 action_target_kind_name(ATK_CITY));
1355 if (!unit_can_do_action(actor_unit, action_id)) {
1356 /* No point in continuing. */
1357 return ACTPROB_IMPOSSIBLE;
1360 return action_prob(action_id,
1361 unit_owner(actor_unit), tile_city(actor_tile),
1362 NULL, actor_tile, actor_unit,
1363 NULL, NULL,
1364 city_owner(target_city), target_city, NULL,
1365 city_tile(target_city), NULL, NULL, NULL);
1368 /**************************************************************************
1369 Get the actor unit's probability of successfully performing the chosen
1370 action on the target unit.
1371 **************************************************************************/
1372 struct act_prob action_prob_vs_unit(const struct unit* actor_unit,
1373 const int action_id,
1374 const struct unit* target_unit)
1376 struct tile *actor_tile = unit_tile(actor_unit);
1378 if (actor_unit == NULL || target_unit == NULL) {
1379 /* Can't do an action when actor or target are missing. */
1380 return ACTPROB_IMPOSSIBLE;
1383 fc_assert_ret_val_msg(AAK_UNIT == action_id_get_actor_kind(action_id),
1384 ACTPROB_IMPOSSIBLE,
1385 "Action %s is performed by %s not %s",
1386 gen_action_name(action_id),
1387 action_actor_kind_name(
1388 action_id_get_actor_kind(action_id)),
1389 action_actor_kind_name(AAK_UNIT));
1391 fc_assert_ret_val_msg(ATK_UNIT == action_id_get_target_kind(action_id),
1392 ACTPROB_IMPOSSIBLE,
1393 "Action %s is against %s not %s",
1394 gen_action_name(action_id),
1395 action_target_kind_name(
1396 action_id_get_target_kind(action_id)),
1397 action_target_kind_name(ATK_UNIT));
1399 if (!unit_can_do_action(actor_unit, action_id)) {
1400 /* No point in continuing. */
1401 return ACTPROB_IMPOSSIBLE;
1404 return action_prob(action_id,
1405 unit_owner(actor_unit), tile_city(actor_tile),
1406 NULL, actor_tile, actor_unit,
1407 NULL, NULL,
1408 unit_owner(target_unit),
1409 tile_city(unit_tile(target_unit)), NULL,
1410 unit_tile(target_unit),
1411 target_unit, NULL, NULL);
1414 /**************************************************************************
1415 Returns the impossible action probability.
1416 **************************************************************************/
1417 struct act_prob action_prob_new_impossible(void)
1419 struct act_prob out = { ACTPROB_VAL_MIN, ACTPROB_VAL_MIN };
1421 return out;
1424 /**************************************************************************
1425 Returns the certain action probability.
1426 **************************************************************************/
1427 struct act_prob action_prob_new_certain(void)
1429 struct act_prob out = { ACTPROB_VAL_MAX, ACTPROB_VAL_MAX };
1431 return out;
1434 /**************************************************************************
1435 Returns the n/a action probability.
1436 **************************************************************************/
1437 struct act_prob action_prob_new_not_relevant(void)
1439 struct act_prob out = { ACTPROB_VAL_NA, ACTPROB_VAL_MIN};
1441 return out;
1444 /**************************************************************************
1445 Returns the "not implemented" action probability.
1446 **************************************************************************/
1447 struct act_prob action_prob_new_not_impl(void)
1449 struct act_prob out = { ACTPROB_VAL_NOT_IMPL, ACTPROB_VAL_MIN };
1451 return out;
1454 /**************************************************************************
1455 Returns the "user don't know" action probability.
1456 **************************************************************************/
1457 struct act_prob action_prob_new_unknown(void)
1459 struct act_prob out = { ACTPROB_VAL_MIN, ACTPROB_VAL_MAX };
1461 return out;
1464 /**************************************************************************
1465 Returns TRUE iff the given action probability belongs to an action that
1466 may be possible.
1467 **************************************************************************/
1468 bool action_prob_possible(const struct act_prob probability)
1470 return (ACTPROB_VAL_MIN < probability.max
1471 || action_prob_not_impl(probability));
1474 /**************************************************************************
1475 Returns TRUE iff the given action probability represents the lack of
1476 an action probability.
1477 **************************************************************************/
1478 static inline bool
1479 action_prob_not_relevant(const struct act_prob probability)
1481 return probability.min == ACTPROB_VAL_NA
1482 && probability.max == ACTPROB_VAL_MIN;
1485 /**************************************************************************
1486 Returns TRUE iff the given action probability represents that support
1487 for finding this action probability currently is missing from Freeciv.
1488 **************************************************************************/
1489 static inline bool
1490 action_prob_not_impl(const struct act_prob probability)
1492 return probability.min == ACTPROB_VAL_NOT_IMPL
1493 && probability.max == ACTPROB_VAL_MIN;
1496 /**************************************************************************
1497 Returns TRUE iff the given action probability represents a special
1498 signal value rather than a regular action probability value.
1499 **************************************************************************/
1500 static inline bool
1501 action_prob_is_signal(const struct act_prob probability)
1503 return probability.max < probability.min;
1506 /**************************************************************************
1507 Returns TRUE iff ap1 and ap2 are equal.
1508 **************************************************************************/
1509 bool are_action_probabilitys_equal(const struct act_prob *ap1,
1510 const struct act_prob *ap2)
1512 return ap1->min == ap2->min && ap1->max == ap2->max;
1515 /**************************************************************************
1516 Will a player with the government gov be immune to the action act?
1517 **************************************************************************/
1518 bool action_immune_government(struct government *gov, int act)
1520 /* Always immune since its not enabled. Doesn't count. */
1521 if (action_enabler_list_size(action_enablers_for_action(act)) == 0) {
1522 return FALSE;
1525 action_enabler_list_iterate(action_enablers_for_action(act), enabler) {
1526 if (requirement_fulfilled_by_government(gov, &(enabler->target_reqs))) {
1527 return FALSE;
1529 } action_enabler_list_iterate_end;
1531 return TRUE;
1534 /**************************************************************************
1535 Returns TRUE if the wanted action can be done to the target.
1536 **************************************************************************/
1537 static bool is_target_possible(const enum gen_action wanted_action,
1538 const struct player *actor_player,
1539 const struct player *target_player,
1540 const struct city *target_city,
1541 const struct impr_type *target_building,
1542 const struct tile *target_tile,
1543 const struct unit *target_unit,
1544 const struct unit_type *target_unittype,
1545 const struct output_type *target_output,
1546 const struct specialist *target_specialist)
1548 action_enabler_list_iterate(action_enablers_for_action(wanted_action),
1549 enabler) {
1550 if (are_reqs_active(target_player, actor_player, target_city,
1551 target_building, target_tile,
1552 target_unit, target_unittype,
1553 target_output, target_specialist,
1554 &enabler->target_reqs, RPT_POSSIBLE)) {
1555 return TRUE;
1557 } action_enabler_list_iterate_end;
1559 return FALSE;
1562 /**************************************************************************
1563 Returns TRUE if the wanted action can be done to the target city.
1564 **************************************************************************/
1565 bool is_action_possible_on_city(const enum gen_action action_id,
1566 const struct player *actor_player,
1567 const struct city* target_city)
1569 fc_assert_ret_val_msg(ATK_CITY == action_id_get_target_kind(action_id),
1570 FALSE, "Action %s is against %s not cities",
1571 gen_action_name(action_id),
1572 action_target_kind_name(
1573 action_id_get_target_kind(action_id)));
1575 return is_target_possible(action_id, actor_player,
1576 city_owner(target_city), target_city, NULL,
1577 city_tile(target_city), NULL, NULL,
1578 NULL, NULL);
1581 /**************************************************************************
1582 Returns TRUE if the specified action can't be done now but would have
1583 been legal if the unit had full movement.
1584 **************************************************************************/
1585 bool action_mp_full_makes_legal(const struct unit *actor,
1586 const int action_id)
1588 fc_assert(action_id_is_valid(action_id) || action_id == ACTION_ANY);
1590 /* Check if full movement points may enable the specified action. */
1591 return !utype_may_act_move_frags(unit_type_get(actor),
1592 action_id,
1593 actor->moves_left)
1594 && utype_may_act_move_frags(unit_type_get(actor),
1595 action_id,
1596 unit_move_rate(actor));