Stop sharing requirement_unit_state_ereq().
[freeciv.git] / common / actions.h
blobb361007b56e36289544376dea3ff6a0f27aa7128
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 #ifndef FC_ACTIONS_H
15 #define FC_ACTIONS_H
17 /* common */
18 #include "fc_types.h"
19 #include "metaknowledge.h"
20 #include "requirements.h"
22 #ifdef __cplusplus
23 extern "C" {
24 #endif /* __cplusplus */
26 #define SPECENUM_NAME action_actor_kind
27 #define SPECENUM_VALUE0 AAK_UNIT
28 #define SPECENUM_VALUE0NAME N_("a unit")
29 #define SPECENUM_COUNT AAK_COUNT
30 #include "specenum_gen.h"
32 #define SPECENUM_NAME action_target_kind
33 #define SPECENUM_VALUE0 ATK_CITY
34 #define SPECENUM_VALUE0NAME N_("individual cities")
35 #define SPECENUM_VALUE1 ATK_UNIT
36 #define SPECENUM_VALUE1NAME N_("individual units")
37 #define SPECENUM_VALUE2 ATK_UNITS
38 #define SPECENUM_VALUE2NAME N_("unit stacks")
39 #define SPECENUM_VALUE3 ATK_TILE
40 #define SPECENUM_VALUE3NAME N_("tiles")
41 /* No target except the actor itself. */
42 #define SPECENUM_VALUE4 ATK_SELF
43 #define SPECENUM_VALUE4NAME N_("itself")
44 #define SPECENUM_COUNT ATK_COUNT
45 #include "specenum_gen.h"
47 /* Values used in the network protocol. */
48 /* Names used in file formats but not normally shown to users. */
49 #define SPECENUM_NAME gen_action
50 #define SPECENUM_VALUE0 ACTION_ESTABLISH_EMBASSY
51 #define SPECENUM_VALUE0NAME "Establish Embassy"
52 #define SPECENUM_VALUE1 ACTION_SPY_INVESTIGATE_CITY
53 #define SPECENUM_VALUE1NAME "Investigate City"
54 #define SPECENUM_VALUE2 ACTION_SPY_POISON
55 #define SPECENUM_VALUE2NAME "Poison City"
56 #define SPECENUM_VALUE3 ACTION_SPY_STEAL_GOLD
57 #define SPECENUM_VALUE3NAME "Steal Gold"
58 #define SPECENUM_VALUE4 ACTION_SPY_SABOTAGE_CITY
59 #define SPECENUM_VALUE4NAME "Sabotage City"
60 #define SPECENUM_VALUE5 ACTION_SPY_TARGETED_SABOTAGE_CITY
61 #define SPECENUM_VALUE5NAME "Targeted Sabotage City"
62 #define SPECENUM_VALUE6 ACTION_SPY_STEAL_TECH
63 #define SPECENUM_VALUE6NAME "Steal Tech"
64 #define SPECENUM_VALUE7 ACTION_SPY_TARGETED_STEAL_TECH
65 #define SPECENUM_VALUE7NAME "Targeted Steal Tech"
66 #define SPECENUM_VALUE8 ACTION_SPY_INCITE_CITY
67 #define SPECENUM_VALUE8NAME "Incite City"
68 #define SPECENUM_VALUE9 ACTION_TRADE_ROUTE
69 #define SPECENUM_VALUE9NAME "Establish Trade Route"
70 #define SPECENUM_VALUE10 ACTION_MARKETPLACE
71 #define SPECENUM_VALUE10NAME "Enter Marketplace"
72 #define SPECENUM_VALUE11 ACTION_HELP_WONDER
73 #define SPECENUM_VALUE11NAME "Help Wonder"
74 #define SPECENUM_VALUE12 ACTION_SPY_BRIBE_UNIT
75 #define SPECENUM_VALUE12NAME "Bribe Unit"
76 #define SPECENUM_VALUE13 ACTION_SPY_SABOTAGE_UNIT
77 #define SPECENUM_VALUE13NAME "Sabotage Unit"
78 #define SPECENUM_VALUE14 ACTION_CAPTURE_UNITS
79 #define SPECENUM_VALUE14NAME "Capture Units"
80 #define SPECENUM_VALUE15 ACTION_FOUND_CITY
81 #define SPECENUM_VALUE15NAME "Found City"
82 #define SPECENUM_VALUE16 ACTION_JOIN_CITY
83 #define SPECENUM_VALUE16NAME "Join City"
84 #define SPECENUM_VALUE17 ACTION_STEAL_MAPS
85 #define SPECENUM_VALUE17NAME "Steal Maps"
86 #define SPECENUM_VALUE18 ACTION_BOMBARD
87 #define SPECENUM_VALUE18NAME "Bombard"
88 #define SPECENUM_VALUE19 ACTION_SPY_NUKE
89 #define SPECENUM_VALUE19NAME "Suitcase Nuke"
90 #define SPECENUM_VALUE20 ACTION_NUKE
91 #define SPECENUM_VALUE20NAME "Explode Nuclear"
92 #define SPECENUM_VALUE21 ACTION_DESTROY_CITY
93 #define SPECENUM_VALUE21NAME "Destroy City"
94 #define SPECENUM_VALUE22 ACTION_EXPEL_UNIT
95 #define SPECENUM_VALUE22NAME "Expel Unit"
96 #define SPECENUM_VALUE23 ACTION_RECYCLE_UNIT
97 #define SPECENUM_VALUE23NAME "Recycle Unit"
98 #define SPECENUM_VALUE24 ACTION_DISBAND_UNIT
99 #define SPECENUM_VALUE24NAME "Disband Unit"
100 #define SPECENUM_VALUE25 ACTION_HOME_CITY
101 #define SPECENUM_VALUE25NAME "Home City"
102 #define SPECENUM_VALUE26 ACTION_UPGRADE_UNIT
103 #define SPECENUM_VALUE26NAME "Upgrade Unit"
104 #define SPECENUM_VALUE27 ACTION_PARADROP
105 #define SPECENUM_VALUE27NAME "Paradrop Unit"
106 #define SPECENUM_VALUE28 ACTION_AIRLIFT
107 #define SPECENUM_VALUE28NAME "Airlift Unit"
108 #define SPECENUM_VALUE29 ACTION_ATTACK
109 #define SPECENUM_VALUE29NAME "Attack"
110 #define SPECENUM_VALUE30 ACTION_CONQUER_CITY
111 #define SPECENUM_VALUE30NAME "Conquer City"
112 #define SPECENUM_VALUE31 ACTION_HEAL_UNIT
113 #define SPECENUM_VALUE31NAME "Heal Unit"
114 #define SPECENUM_BITVECTOR bv_actions
115 /* Limited by what values num2char() can store in unit orders in
116 * savegames. */
117 #define SPECENUM_COUNT ACTION_COUNT
118 #include "specenum_gen.h"
120 /* Used in searches to signal that any action at all is OK. */
121 #define ACTION_ANY ACTION_COUNT
123 /* Used to signal the absence of any actions. */
124 #define ACTION_NONE ACTION_COUNT
126 /* Used in the network protocol. */
127 #define MAX_NUM_ACTIONS ACTION_COUNT
128 #define NUM_ACTIONS MAX_NUM_ACTIONS
130 /* Who ordered the action to be performed? */
131 #define SPECENUM_NAME action_requester
132 /* The player ordered it directly. */
133 #define SPECENUM_VALUE0 ACT_REQ_PLAYER
134 #define SPECENUM_VALUE0NAME N_("the player")
135 /* The game it self because the rules requires it. */
136 #define SPECENUM_VALUE1 ACT_REQ_RULES
137 #define SPECENUM_VALUE1NAME N_("the game rules")
138 /* A server side autonomous agent working for the player. */
139 #define SPECENUM_VALUE2 ACT_REQ_SS_AGENT
140 #define SPECENUM_VALUE2NAME N_("a server agent")
141 /* Number of action requesters. */
142 #define SPECENUM_COUNT ACT_REQ_COUNT
143 #include "specenum_gen.h"
145 /* The last action distance value that is interpreted as an actual
146 * distance rather than as a signal value.
148 * It is specified literally rather than referring to MAP_DISTANCE_MAX
149 * because Freeciv-web's MAP_DISTANCE_MAX differs from the regular Freeciv
150 * server's MAP_DISTANCE_MAX. A static assertion in actions.c makes sure
151 * that it can cover the whole map. */
152 #define ACTION_DISTANCE_LAST_NON_SIGNAL 128016
153 /* No action max distance to target limit. */
154 #define ACTION_DISTANCE_UNLIMITED (ACTION_DISTANCE_LAST_NON_SIGNAL + 1)
155 /* No action max distance can be bigger than this. */
156 #define ACTION_DISTANCE_MAX ACTION_DISTANCE_UNLIMITED
158 struct action
160 enum gen_action id;
161 enum action_actor_kind actor_kind;
162 enum action_target_kind target_kind;
164 bool hostile; /* TODO: Should this be a scale in stead? */
166 /* Is the player required to specify details about this action? Only true
167 * IFF the action needs details AND the server won't fill them in when
168 * unspecified. */
169 bool requires_details;
171 /* A unit's ability to perform this action will pop up the action
172 * selection dialog before the player asks for it only in exceptional
173 * cases.
175 * The motivation for setting rare_pop_up is to minimize player
176 * annoyance and mistakes. Getting a pop up every time a unit moves is
177 * annoying. An unexpected offer to do something that in many cases is
178 * destructive can lead the player's muscle memory to perform the wrong
179 * action. */
180 bool rare_pop_up;
182 /* Limits on the distance on the map between the actor and the target.
183 * The action is legal iff the distance is min_distance, max_distance or
184 * a value in between. */
185 int min_distance, max_distance;
187 /* The name of the action shown in the UI */
188 char ui_name[MAX_LEN_NAME];
190 /* Suppress automatic help text generation about what enables and/or
191 * disables this action. */
192 bool quiet;
194 /* The unitwaittime setting blocks this action when done too soon. */
195 bool unitwaittime_controlled;
197 /* Actions that blocks this action. The action will be illegal if any
198 * bloking action is legal. */
199 bv_actions blocked_by;
202 struct action_enabler
204 bool disabled;
205 enum gen_action action;
206 struct requirement_vector actor_reqs;
207 struct requirement_vector target_reqs;
210 #define enabler_get_action(_enabler_) action_by_number(_enabler_->action)
212 #define SPECLIST_TAG action_enabler
213 #define SPECLIST_TYPE struct action_enabler
214 #include "speclist.h"
215 #define action_enabler_list_iterate(action_enabler_list, aenabler) \
216 TYPED_LIST_ITERATE(struct action_enabler, action_enabler_list, aenabler)
217 #define action_enabler_list_iterate_end LIST_ITERATE_END
219 #define action_iterate(_act_) \
221 int _act_; \
222 for (_act_ = 0; _act_ < NUM_ACTIONS; _act_++) {
224 #define action_iterate_end \
228 #define action_enablers_iterate(_enabler_) \
230 action_iterate(_act_) { \
231 action_enabler_list_iterate( \
232 action_enablers_for_action((enum gen_action)_act_), _enabler_) {
234 #define action_enablers_iterate_end \
235 } action_enabler_list_iterate_end; \
236 } action_iterate_end; \
239 /* The reason why an action should be auto performed. */
240 #define SPECENUM_NAME action_auto_perf_cause
241 /* Can't pay the unit's upkeep. */
242 /* (Can be triggered by food, shield or gold upkeep) */
243 #define SPECENUM_VALUE0 AAPC_UNIT_UPKEEP
244 #define SPECENUM_VALUE0NAME N_("Unit Upkeep")
245 /* A unit moved to an adjacent tile (auto attack). */
246 #define SPECENUM_VALUE1 AAPC_UNIT_MOVED_ADJ
247 #define SPECENUM_VALUE1NAME N_("Moved Adjacent")
248 /* Number of forced action auto performer causes. */
249 #define SPECENUM_COUNT AAPC_COUNT
250 #include "specenum_gen.h"
252 /* An Action Auto Performer rule makes an actor try to perform an action
253 * without being ordered to do so by the player controlling it.
254 * - the first auto performer that matches the cause and fulfills the reqs
255 * is selected.
256 * - the actions listed by the selected auto performer is tried in order
257 * until an action is successful, all actions have been tried or the
258 * actor disappears.
259 * - if no action inside the selected auto performer is legal no action is
260 * performed. The system won't try to select another auto performer.
262 struct action_auto_perf
264 /* The reason for trying to auto perform an action. */
265 enum action_auto_perf_cause cause;
267 /* Must be fulfilled if the game should try to force an action from this
268 * action auto performer. */
269 struct requirement_vector reqs;
271 /* Auto perform the first legal action in this list.
272 * The list is terminated by ACTION_NONE. */
273 enum gen_action alternatives[MAX_NUM_ACTIONS];
276 #define action_auto_perf_iterate(_act_perf_) \
278 int _ap_num_; \
280 for (_ap_num_ = 0; \
281 _ap_num_ < MAX_NUM_ACTION_AUTO_PERFORMERS \
282 && (action_auto_perf_by_number(_ap_num_)->cause \
283 != AAPC_COUNT); \
284 _ap_num_++) { \
285 const struct action_auto_perf *_act_perf_ \
286 = action_auto_perf_by_number(_ap_num_);
288 #define action_auto_perf_iterate_end \
292 #define action_auto_perf_by_cause_iterate(_cause_, _act_perf_) \
293 action_auto_perf_iterate(_act_perf_) { \
294 if (_act_perf_->cause != _cause_) { \
295 continue; \
298 #define action_auto_perf_by_cause_iterate_end \
299 } action_auto_perf_iterate_end
301 #define action_auto_perf_actions_iterate(_autoperf_, _act_id_) \
303 int _perf_pos_; \
305 for (_perf_pos_ = 0; _perf_pos_ < NUM_ACTIONS; _perf_pos_++) { \
306 const int _act_id_ = _autoperf_->alternatives[_perf_pos_]; \
308 if (_act_id_ == ACTION_NONE) { \
309 /* No more alternative actions. */ \
310 break; \
313 #define action_auto_perf_actions_iterate_end \
317 /* Hard coded location of action auto performers. Used for conversion while
318 * action auto performers aren't directly exposed to the ruleset. */
319 #define ACTION_AUTO_UPKEEP_FOOD 0
320 #define ACTION_AUTO_UPKEEP_GOLD 1
321 #define ACTION_AUTO_UPKEEP_SHIELD 2
322 #define ACTION_AUTO_MOVED_ADJ 3
324 /* Initialization */
325 void actions_init(void);
326 void actions_rs_pre_san_gen(void);
327 void actions_free(void);
329 bool actions_are_ready(void);
331 bool action_id_exists(const int action_id);
333 struct action *action_by_number(int action_id);
334 struct action *action_by_rule_name(const char *name);
336 enum action_actor_kind action_get_actor_kind(const struct action *paction);
337 #define action_id_get_actor_kind(action_id) \
338 action_get_actor_kind(action_by_number(action_id))
339 enum action_target_kind action_get_target_kind(
340 const struct action *paction);
341 #define action_id_get_target_kind(action_id) \
342 action_get_target_kind(action_by_number(action_id))
344 int action_number(const struct action *action);
346 bool action_has_result(struct action *paction, enum gen_action result);
347 #define action_id_has_result(action_id, result) \
348 action_has_result(action_by_number(action_id), result)
350 bool action_is_hostile(int action_id);
352 bool action_requires_details(int action_id);
354 bool action_id_is_rare_pop_up(int action_id);
356 bool action_distance_accepted(const struct action *action,
357 const int distance);
358 #define action_id_distance_accepted(action_id, distance) \
359 action_distance_accepted(action_by_number(action_id), distance)
361 bool action_distance_inside_max(const struct action *action,
362 const int distance);
363 #define action_id_distance_inside_max(action_id, distance) \
364 action_distance_inside_max(action_by_number(action_id), distance)
366 bool action_would_be_blocked_by(const struct action *blocked,
367 const struct action *blocker);
368 #define action_id_would_be_blocked_by(blocked_id, blocker_id) \
369 action_would_be_blocked_by(action_by_number(blocked_id), \
370 action_by_number(blocker_id))
372 int action_get_role(const struct action *paction);
373 #define action_id_get_role(action_id) \
374 action_get_role(action_by_number(action_id))
376 const char *action_rule_name(const struct action *action);
377 const char *action_id_rule_name(int action_id);
379 const char *action_name_translation(const struct action *action);
380 const char *action_id_name_translation(int action_id);
381 const char *action_get_ui_name_mnemonic(int action_id,
382 const char* mnemonic);
383 const char *action_prepare_ui_name(int action_id, const char* mnemonic,
384 const struct act_prob prob,
385 const char *custom);
386 const char *action_get_tool_tip(const int action_id,
387 const struct act_prob prob);
389 struct action_enabler_list *
390 action_enablers_for_action(enum gen_action action);
392 struct action_enabler *action_enabler_new(void);
393 struct action_enabler *
394 action_enabler_copy(const struct action_enabler *original);
395 void action_enabler_add(struct action_enabler *enabler);
396 bool action_enabler_remove(struct action_enabler *enabler);
398 const char *
399 action_enabler_obligatory_reqs_missing(struct action_enabler *enabler);
400 void action_enabler_obligatory_reqs_add(struct action_enabler *enabler);
402 struct action *action_is_blocked_by(const int action_id,
403 const struct unit *actor_unit,
404 const struct tile *target_tile,
405 const struct city *target_city,
406 const struct unit *target_unit);
408 bool is_action_enabled_unit_on_city(const enum gen_action wanted_action,
409 const struct unit *actor_unit,
410 const struct city *target_city);
412 bool is_action_enabled_unit_on_city_full(const enum gen_action wanted_action,
413 const struct unit *actor_unit,
414 const struct city *target_city,
415 const struct city *homecity,
416 bool ignore_dist);
418 bool is_action_enabled_unit_on_unit(const enum gen_action wanted_action,
419 const struct unit *actor_unit,
420 const struct unit *target_unit);
422 bool is_action_enabled_unit_on_units(const enum gen_action wanted_action,
423 const struct unit *actor_unit,
424 const struct tile *target_tile);
426 bool is_action_enabled_unit_on_tile(const enum gen_action wanted_action,
427 const struct unit *actor_unit,
428 const struct tile *target_tile);
430 bool is_action_enabled_unit_on_self(const enum gen_action wanted_action,
431 const struct unit *actor_unit);
433 struct act_prob action_prob_vs_city(const struct unit* actor,
434 const int action_id,
435 const struct city* victim);
437 struct act_prob action_prob_vs_unit(const struct unit* actor,
438 const int action_id,
439 const struct unit* victim);
441 struct act_prob action_prob_vs_units(const struct unit* actor,
442 const int action_id,
443 const struct tile* victims);
445 struct act_prob action_prob_vs_tile(const struct unit *actor,
446 const int action_id,
447 const struct tile *victims);
449 struct act_prob action_prob_self(const struct unit *actor,
450 const int action_id);
452 bool action_prob_possible(const struct act_prob probability);
454 bool are_action_probabilitys_equal(const struct act_prob *ap1,
455 const struct act_prob *ap2);
457 int action_prob_cmp_pessimist(const struct act_prob ap1,
458 const struct act_prob ap2);
460 double action_prob_to_0_to_1_pessimist(const struct act_prob ap);
462 struct act_prob action_prob_fall_back(const struct act_prob *ap1,
463 const struct act_prob *ap2);
465 struct act_prob action_prob_new_impossible(void);
466 struct act_prob action_prob_new_not_relevant(void);
467 struct act_prob action_prob_new_not_impl(void);
468 struct act_prob action_prob_new_unknown(void);
469 struct act_prob action_prob_new_certain(void);
471 /* Special action probability values. Documented in fc_types.h's
472 * definition of struct act_prob. */
473 #define ACTPROB_IMPOSSIBLE action_prob_new_impossible()
474 #define ACTPROB_CERTAIN action_prob_new_certain()
475 #define ACTPROB_NA action_prob_new_not_relevant()
476 #define ACTPROB_NOT_IMPLEMENTED action_prob_new_not_impl()
477 #define ACTPROB_NOT_KNOWN action_prob_new_unknown()
479 bool
480 action_actor_utype_hard_reqs_ok(const enum gen_action wanted_action,
481 const struct unit_type *actor_unittype);
483 /* Reasoning about actions */
484 bool action_immune_government(struct government *gov, int act);
486 bool action_blocked_by_situation_act(const struct action *paction,
487 const struct requirement *situation);
488 #define action_id_blocked_by_situation_act(action_id, situation) \
489 action_blocked_by_situation_act(action_by_number(action_id), situation)
491 bool action_blocked_by_situation_tgt(const struct action *paction,
492 const struct requirement *situation);
493 #define action_id_blocked_by_situation_tgt(action_id, situation) \
494 action_blocked_by_situation_tgt(action_by_number(action_id), situation)
496 bool is_action_possible_on_city(const enum gen_action action_id,
497 const struct player *actor_player,
498 const struct city* target_city);
500 bool action_maybe_possible_actor_unit(const int wanted_action,
501 const struct unit* actor_unit);
503 bool action_mp_full_makes_legal(const struct unit *actor,
504 const int action_id);
506 /* Action auto performers */
507 const struct action_auto_perf *action_auto_perf_by_number(const int num);
508 struct action_auto_perf *action_auto_perf_slot_number(const int num);
510 #ifdef __cplusplus
512 #endif /* __cplusplus */
514 #endif /* FC_ACTIONS_H */