The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
actions.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2017 by Yurii Chernyi <terraninfo@terraninfo.net>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * Managing the AI-Game interaction - AI actions and their results
17  * @file
18  * */
19 
20 #pragma once
21 
22 #include "ai/game_info.hpp"
23 
24 #include "actions/move.hpp"
26 #include "units/ptr.hpp"
27 
28 namespace pathfind {
29 struct plain_route;
30 } // of namespace pathfind
31 
32 class unit;
33 class unit_type;
34 class team;
35 class gamemap;
36 
37 namespace ai {
38 
40 friend void sim_gamestate_changed(action_result *result, bool gamestate_changed); // Manage gamestate changed in simulated actions.
41 
42 public:
43 
44  enum result {
48  };
49 
50  virtual ~action_result();
51 
52  /* check as must as possible without executing anything */
53  void check_before();
54 
55  /* execute the action */
56  void execute();
57 
58  /* has the game state changed during execution ? */
59  bool is_gamestate_changed() const;
60 
61  /* check the return value of the action. mandatory to call. */
62  bool is_ok();
63 
64  /* get the return value of the action */
65  int get_status() const;
66 
67  /* describe the action */
68  virtual std::string do_describe() const =0;
69 protected:
70  action_result( side_number side );
71 
72  /* do check before execution or just check. setting status_ via set_error to != cancels the execution.*/
73  virtual void do_check_before() = 0;
74 
75  /* do some additional checks after execution. */
76  virtual void do_check_after() = 0;
77 
78  /* execute. assert(is_success()) */
79  virtual void do_execute() = 0;
80 
81  /* runs before cheching before execution */
82  virtual void do_init_for_execution() = 0;
83 
84  /* are we going to execute the action now ? */
85  bool is_execution() const;
86 
87  /* return the side number */
88  int get_side() const { return side_; }
89 
90  /* return real information about the game state */
91  game_info& get_info() const;
92 
93  team& get_my_team() const;
94 
95  /* set error code */
96  void set_error(int error_code, bool log_as_error = true);
97 
98  /* is error code equal to 0 (no errors)? */
99  bool is_success() const;
100 
101  /* note that the game state has been changed */
102  void set_gamestate_changed();
103 private:
104 
105  /* Check after the execution */
106  void check_after();
107 
108  /* Initialization before execution */
109  void init_for_execution();
110 
111  /* set the flag that the return value had been checked */
112  void set_ok_checked();
113 
114  /* was the return value checked ? */
116 
117  /* current side number */
118  int side_;
119 
120  /* execution status. if 0, all is ok. if !=0, then there were some problems. */
121  int status_;
122 
123  /* are we going to execute the action now ? */
125 
127 
128 };
129 
130 class attack_result : public action_result {
131 public:
133  const map_location& attacker_loc,
134  const map_location& defender_loc,
135  int attacker_weapon,
136  double aggression,
137  const unit_advancements_aspect& advancements = unit_advancements_aspect());
138 
139  enum result {
150  };
151 
152  virtual std::string do_describe() const;
153 protected:
154  virtual void do_check_before();
155  virtual void do_check_after();
156  virtual void do_execute();
157  virtual void do_init_for_execution();
158 private:
162  double aggression_;
164 };
165 
166 class move_result : public action_result {
167 public:
168  move_result( side_number side,
169  const map_location& from,
170  const map_location& to,
171  bool remove_movement,
172  bool unreach_is_ok);
173 
174  enum result {
175  E_EMPTY_MOVE = 2001,
176  E_NO_UNIT = 2002,
179  E_AMBUSHED = 2005,
182  E_NO_ROUTE = 2008
183  };
184 
185  virtual std::string do_describe() const;
186  virtual const map_location& get_unit_location() const;
187 protected:
188  virtual void do_check_before();
189  virtual void do_check_after();
190  virtual void do_execute();
191  virtual void do_init_for_execution();
192 private:
193  const unit *get_unit();
194  bool test_route(const unit &un);
198  std::shared_ptr<pathfind::plain_route> route_;
203 };
204 
205 
206 class recall_result : public action_result {
207 public:
208  recall_result (side_number side, const std::string &unit_id, const map_location& where, const map_location& from);
209 
210  enum result {
212  E_NO_GOLD = 6003,
213  E_NO_LEADER = 6004,
216  };
217 
218  virtual std::string do_describe() const;
219 protected:
220  virtual void do_check_before();
221  virtual void do_check_after();
222  virtual void do_execute();
223  virtual void do_init_for_execution();
224 private:
226  const team& my_team);
227  bool test_enough_gold(
228  const team& my_team);
229 
235 };
236 
238 public:
239  recruit_result( side_number side, const std::string& unit_name, const map_location& where, const map_location& from);
240 
241  enum result {
244  E_NO_GOLD = 3003,
245  E_NO_LEADER = 3004,
248  };
249 
250  virtual std::string do_describe() const;
251 protected:
252  virtual void do_check_before();
253  virtual void do_check_after();
254  virtual void do_execute();
255  virtual void do_init_for_execution();
256 private:
258  const std::string &recruit);
259  bool test_enough_gold(
260  const team& my_team,
261  const unit_type &type );
262 
268 };
269 
271 public:
273  const map_location& unit_location,
274  bool remove_movement,
275  bool remove_attacks );
276 
277  enum result {
278  E_NO_UNIT = 4002,
281  };
282 
283  virtual std::string do_describe() const;
284 protected:
285  virtual void do_check_before();
286  virtual void do_check_after();
287  virtual void do_execute();
288  virtual void do_init_for_execution();
289 private:
290  const unit *get_unit();
292  const bool remove_movement_;
293  const bool remove_attacks_;
294 };
295 
297 public:
299  const std::string& lua_code,
300  const map_location& location );
301 
302  virtual std::string do_describe() const;
303 protected:
304  virtual void do_check_before();
305  virtual void do_check_after();
306  virtual void do_execute();
307  virtual void do_init_for_execution();
308 private:
311 };
312 
313 
314 class actions {
315 
316 public:
317 // =======================================================================
318 // Stateless interface to actions
319 // =======================================================================
320 
321 
322 /**
323  * Ask the game to attack an enemy defender using our unit attacker from attackers current location,
324  * @param side the side which tries to execute the move
325  * @param execute should move be actually executed or not
326  * @param attacker_loc location of attacker
327  * @param defender_loc location of defender
328  * @param attacker_weapon weapon of attacker
329  * @param aggression aggression of attacker, is used to determine attacker's weapon if it is not specified
330  * @retval possible result: ok
331  * @retval possible result: something wrong
332  * @retval possible result: attacker and/or defender are invalid
333  * @retval possible result: attacker doesn't have the specified weapon
334  */
336  bool execute,
337  const map_location& attacker_loc,
338  const map_location& defender_loc,
339  int attacker_weapon,
340  double aggression,
341  const unit_advancements_aspect& advancements = unit_advancements_aspect());
342 
343 
344 /**
345  * Ask the game to move our unit from location 'from' to location 'to', optionally - doing a partial move
346  * @param side the side which tries to execute the move
347  * @param execute should move be actually executed or not
348  * @param from location of our unit
349  * @param to where to move
350  * @param remove_movement set unit movement to 0 in case of successful move
351  * @retval possible result: ok
352  * @retval possible result: something wrong
353  * @retval possible result: move is interrupted
354  * @retval possible result: move is impossible
355  */
357  bool execute,
358  const map_location& from,
359  const map_location& to,
360  bool remove_movement,
361  bool unreach_is_ok = false);
362 
363 
364 
365 /**
366  * Ask the game to recall a unit for us on specified location
367  * @param side the side which tries to execute the move
368  * @param execute should move be actually executed or not
369  * @param unit_id the id of the unit to be recalled.
370  * @param where location where the unit is to be recalled.
371  * @retval possible result: ok
372  * @retval possible_result: something wrong
373  * @retval possible_result: leader not on keep
374  * @retval possible_result: no free space on keep
375  * @retval possible_result: not enough gold
376  */
378  bool execute,
379  const std::string& unit_id,
380  const map_location& where,
381  const map_location& from);
382 
383 
384 
385 /**
386  * Ask the game to recruit a unit for us on specified location
387  * @param side the side which tries to execute the move
388  * @param execute should move be actually executed or not
389  * @param unit_name the name of the unit to be recruited.
390  * @param where location where the unit is to be recruited.
391  * @retval possible result: ok
392  * @retval possible_result: something wrong
393  * @retval possible_result: leader not on keep
394  * @retval possible_result: no free space on keep
395  * @retval possible_result: not enough gold
396  */
398  bool execute,
399  const std::string& unit_name,
400  const map_location& where,
401  const map_location& from);
402 
403 
404 /**
405  * Ask the game to remove unit movements and/or attack
406  * @param side the side which tries to execute the move
407  * @param execute should move be actually executed or not
408  * @param unit_location the location of our unit
409  * @param remove_movement set remaining movements to 0
410  * @param remove_attacks set remaining attacks to 0
411  * @retval possible result: ok
412  * @retval possible_result: something wrong
413  * @retval possible_result: nothing to do
414  */
416  bool execute,
417  const map_location& unit_location,
418  bool remove_movement,
419  bool remove_attacks );
420 
421 
422 /**
423  * Ask the game to run Lua code
424  * @param side the side which tries to execute the move
425  * @param execute should move be actually executed or not
426  * @param lua_code the code to be run
427  * @param location location to be passed to the code as x1/y1
428  * @retval possible result: ok
429  * @retval possible_result: something wrong
430  * @retval possible_result: nothing to do
431  */
433  bool execute,
434  const std::string& lua_code,
435  const map_location& location );
436 
437 
438 /**
439  * get human-readable name of the error by code.
440  * @param error_code error code.
441  * @retval result the name of the error.
442  */
443 const static std::string& get_error_name(int error_code);
444 
445 private:
446 
447 static std::map<int,std::string> error_names_;
448 
449 };
450 
451 
452 ///@todo 1.7.11 important! Add an ai action (and fai function) to set a goto on a unit
453 ///@todo 1.7.11 important! Add an ai action (and fai function) to send a chat message to a player
454 
455 } //end of namespace ai
456 
457 std::ostream &operator<<(std::ostream &s, ai::attack_result const &r);
458 std::ostream &operator<<(std::ostream &s, ai::move_result const &r);
459 std::ostream &operator<<(std::ostream &s, ai::recall_result const &r);
460 std::ostream &operator<<(std::ostream &s, ai::recruit_result const &r);
461 std::ostream &operator<<(std::ostream &s, ai::stopunit_result const &r);
462 std::ostream &operator<<(std::ostream &s, ai::synced_command_result const &r);
463 
bool unreach_is_ok_
Definition: actions.hpp:200
map_location recruit_location_
Definition: actions.hpp:265
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
map_location recruit_from_
Definition: actions.hpp:266
virtual void do_init_for_execution()
Definition: actions.cpp:923
virtual void do_check_after()
Definition: actions.cpp:856
bool has_ambusher_
Definition: actions.hpp:201
std::ostream & operator<<(std::ostream &s, ai::attack_result const &r)
Definition: actions.cpp:1124
std::vector< char_t > string
virtual void do_execute()
Definition: actions.cpp:260
void check_before()
Definition: actions.cpp:86
bool remove_movement_
Definition: actions.hpp:197
map_location recall_from_
Definition: actions.hpp:233
std::shared_ptr< stopunit_result > stopunit_result_ptr
Definition: game_info.hpp:102
unit_const_ptr get_recall_unit(const team &my_team)
Definition: actions.cpp:538
static attack_result_ptr execute_attack_action(side_number side, bool execute, const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon, double aggression, const unit_advancements_aspect &advancements=unit_advancements_aspect())
Ask the game to attack an enemy defender using our unit attacker from attackers current location...
Definition: actions.cpp:991
virtual void do_check_before()
Definition: actions.cpp:395
This class represents a single unit of a specific type.
Definition: unit.hpp:101
static std::map< int, std::string > error_names_
Definition: actions.hpp:447
team & get_my_team() const
Definition: actions.cpp:167
const unit * get_unit()
Definition: actions.cpp:341
virtual void do_execute()=0
map_location unit_location_
Definition: actions.hpp:199
virtual void do_init_for_execution()
Definition: actions.cpp:982
bool is_success() const
Definition: actions.cpp:152
virtual void do_check_after()
Definition: actions.cpp:412
bool test_route(const unit &un)
Definition: actions.cpp:360
map_location recall_location_
Definition: actions.hpp:232
virtual void do_init_for_execution()
Definition: actions.cpp:521
const map_location & where_
Definition: actions.hpp:264
const std::string & unit_id_
Definition: actions.hpp:230
const unit_type * get_unit_type_known(const std::string &recruit)
Definition: actions.cpp:687
virtual std::string do_describe() const
Definition: actions.cpp:767
std::shared_ptr< move_result > move_result_ptr
Definition: game_info.hpp:100
virtual void do_init_for_execution()
Definition: actions.cpp:817
virtual std::string do_describe() const
Definition: actions.cpp:247
virtual std::string do_describe() const
Definition: actions.cpp:617
virtual void do_check_before()
Definition: actions.cpp:847
int get_status() const
Definition: actions.cpp:147
const unit_advancements_aspect & advancements_
Definition: actions.hpp:163
virtual void do_execute()
Definition: actions.cpp:445
std::shared_ptr< recruit_result > recruit_result_ptr
Definition: game_info.hpp:99
const map_location from_
Definition: actions.hpp:195
static config unit_name(const unit *u)
Definition: reports.cpp:132
action_result(side_number side)
Definition: actions.cpp:69
virtual void do_check_after()=0
const std::string & lua_code_
Definition: actions.hpp:309
std::shared_ptr< attack_result > attack_result_ptr
Definition: game_info.hpp:97
void init_for_execution()
Definition: actions.cpp:110
bool has_interrupted_teleport_
Definition: actions.hpp:202
std::shared_ptr< pathfind::plain_route > route_
Definition: actions.hpp:198
A single unit type that the player may recruit.
Definition: types.hpp:43
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:44
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:58
virtual void do_init_for_execution()
Definition: actions.cpp:670
static recall_result_ptr execute_recall_action(side_number side, bool execute, const std::string &unit_id, const map_location &where, const map_location &from)
Ask the game to recall a unit for us on specified location.
Definition: actions.cpp:1016
std::shared_ptr< recall_result > recall_result_ptr
Definition: game_info.hpp:98
virtual void do_check_after()
Definition: actions.cpp:749
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:131
bool return_value_checked_
Definition: actions.hpp:115
bool is_execution() const
Definition: actions.cpp:157
virtual void do_check_after()
Definition: actions.cpp:599
virtual void do_execute()
Definition: actions.cpp:952
bool location_checked_
Definition: actions.hpp:234
Encapsulates the map of the game.
Definition: map.hpp:34
bool test_enough_gold(const team &my_team, const unit_type &type)
Definition: actions.cpp:697
synced_command_result(side_number side, const std::string &lua_code, const map_location &location)
Definition: actions.cpp:929
double aggression_
Definition: actions.hpp:162
virtual void do_check_after()
Definition: actions.cpp:243
virtual void do_execute()
Definition: actions.cpp:632
const bool remove_movement_
Definition: actions.hpp:292
friend void sim_gamestate_changed(action_result *result, bool gamestate_changed)
Definition: actions.cpp:1114
virtual void do_check_before()=0
virtual void do_init_for_execution()=0
attack_result(side_number side, const map_location &attacker_loc, const map_location &defender_loc, int attacker_weapon, double aggression, const unit_advancements_aspect &advancements=unit_advancements_aspect())
Definition: actions.cpp:174
const std::string & unit_name_
Definition: actions.hpp:263
virtual std::string do_describe() const
Definition: actions.cpp:430
std::shared_ptr< synced_command_result > synced_command_result_ptr
Definition: game_info.hpp:103
virtual void do_check_before()
Definition: actions.cpp:706
Encapsulates the map of the game.
Definition: location.hpp:40
Various functions related to moving units.
const bool remove_attacks_
Definition: actions.hpp:293
virtual void do_check_before()
Definition: actions.cpp:556
const map_location & defender_loc_
Definition: actions.hpp:160
void check_after()
Definition: actions.cpp:81
Game information for the AI.
virtual std::string do_describe() const
Definition: actions.cpp:943
virtual const map_location & get_unit_location() const
Definition: actions.cpp:407
static map_location::DIRECTION s
bool test_enough_gold(const team &my_team)
Definition: actions.cpp:547
void set_gamestate_changed()
Definition: actions.cpp:142
virtual void do_check_before()
Definition: actions.cpp:934
static recruit_result_ptr execute_recruit_action(side_number side, bool execute, const std::string &unit_name, const map_location &where, const map_location &from)
Ask the game to recruit a unit for us on specified location.
Definition: actions.cpp:1027
stopunit_result(side_number side, const map_location &unit_location, bool remove_movement, bool remove_attacks)
Definition: actions.cpp:823
static synced_command_result_ptr execute_synced_command_action(side_number side, bool execute, const std::string &lua_code, const map_location &location)
Ask the game to run Lua code.
Definition: actions.cpp:1049
bool is_gamestate_changed_
Definition: actions.hpp:126
virtual std::string do_describe() const
Definition: actions.cpp:873
virtual void do_check_before()
Definition: actions.cpp:178
const map_location & unit_location_
Definition: actions.hpp:291
static stopunit_result_ptr execute_stopunit_action(side_number side, bool execute, const map_location &unit_location, bool remove_movement, bool remove_attacks)
Ask the game to remove unit movements and/or attack.
Definition: actions.cpp:1038
void set_error(int error_code, bool log_as_error=true)
Definition: actions.cpp:129
bool is_gamestate_changed() const
Definition: actions.cpp:118
int get_side() const
Definition: actions.hpp:88
const map_location to_
Definition: actions.hpp:196
virtual void do_execute()
Definition: actions.cpp:892
virtual std::string do_describe() const =0
int side_number
Definition: game_info.hpp:42
recall_result(side_number side, const std::string &unit_id, const map_location &where, const map_location &from)
Definition: actions.cpp:527
const map_location where_
Definition: actions.hpp:231
const unit * get_unit()
Definition: actions.cpp:828
const map_location & attacker_loc_
Definition: actions.hpp:159
move_result(side_number side, const map_location &from, const map_location &to, bool remove_movement, bool unreach_is_ok)
Definition: actions.cpp:327
virtual void do_check_after()
Definition: actions.cpp:939
recruit_result(side_number side, const std::string &unit_name, const map_location &where, const map_location &from)
Definition: actions.cpp:676
virtual ~action_result()
Definition: actions.cpp:74
static move_result_ptr execute_move_action(side_number side, bool execute, const map_location &from, const map_location &to, bool remove_movement, bool unreach_is_ok=false)
Ask the game to move our unit from location 'from' to location 'to', optionally - doing a partial mov...
Definition: actions.cpp:1004
virtual void do_init_for_execution()
Definition: actions.cpp:321
static const std::string & get_error_name(int error_code)
get human-readable name of the error by code.
Definition: actions.cpp:1059
const map_location & location_
Definition: actions.hpp:310
virtual void do_execute()
Definition: actions.cpp:782
game_info & get_info() const
Definition: actions.cpp:162