The Battle for Wesnoth  1.13.4+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 - 2016 by Yurii Chernyi <[email protected]>
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 #ifndef AI_ACTIONS_HPP_INCLUDED
21 #define AI_ACTIONS_HPP_INCLUDED
22 
23 #include "ai/game_info.hpp"
24 
25 #include "actions/move.hpp"
27 #include "units/ptr.hpp"
28 
29 namespace pathfind {
30 struct plain_route;
31 } // of namespace pathfind
32 
33 class unit;
34 class unit_type;
35 class team;
36 class gamemap;
37 
38 namespace ai {
39 
41 friend void sim_gamestate_changed(action_result *result, bool gamestate_changed); // Manage gamestate changed in simulated actions.
42 
43 public:
44 
45  enum tresult {
49  };
50 
51  virtual ~action_result();
52 
53  /* check as must as possible without executing anything */
54  void check_before();
55 
56  /* execute the action */
57  void execute();
58 
59  /* has the game state changed during execution ? */
60  bool is_gamestate_changed() const;
61 
62  /* check the return value of the action. mandatory to call. */
63  bool is_ok();
64 
65  /* get the return value of the action */
66  int get_status() const;
67 
68  /* describe the action */
69  virtual std::string do_describe() const =0;
70 protected:
71  action_result( side_number side );
72 
73  /* do check before execution or just check. setting status_ via set_error to != cancels the execution.*/
74  virtual void do_check_before() = 0;
75 
76  /* do some additional checks after execution. */
77  virtual void do_check_after() = 0;
78 
79  /* execute. assert(is_success()) */
80  virtual void do_execute() = 0;
81 
82  /* runs before cheching before execution */
83  virtual void do_init_for_execution() = 0;
84 
85  /* are we going to execute the action now ? */
86  bool is_execution() const;
87 
88  /* return the side number */
89  int get_side() const { return side_; }
90 
91  /* return real information about the game state */
92  game_info& get_info() const;
93 
94  team& get_my_team() const;
95 
96  /* set error code */
97  void set_error(int error_code, bool log_as_error = true);
98 
99  /* is error code equal to 0 (no errors)? */
100  bool is_success() const;
101 
102  /* note that the game state has been changed */
103  void set_gamestate_changed();
104 private:
105 
106  /* Check after the execution */
107  void check_after();
108 
109  /* Initialization before execution */
110  void init_for_execution();
111 
112  /* set the flag that the return value had been checked */
113  void set_ok_checked();
114 
115  /* was the return value checked ? */
117 
118  /* current side number */
119  int side_;
120 
121  /* execution status. if 0, all is ok. if !=0, then there were some problems. */
122  int status_;
123 
124  /* are we going to execute the action now ? */
126 
128 
129 };
130 
131 class attack_result : public action_result {
132 public:
134  const map_location& attacker_loc,
135  const map_location& defender_loc,
136  int attacker_weapon,
137  double aggression,
138  const unit_advancements_aspect& advancements = unit_advancements_aspect());
139 
140  enum tresult {
151  };
152 
153  virtual std::string do_describe() const;
154 protected:
155  virtual void do_check_before();
156  virtual void do_check_after();
157  virtual void do_execute();
158  virtual void do_init_for_execution();
159 private:
163  double aggression_;
165 };
166 
167 class move_result : public action_result {
168 public:
169  move_result( side_number side,
170  const map_location& from,
171  const map_location& to,
172  bool remove_movement,
173  bool unreach_is_ok);
174 
175  enum tresult {
176  E_EMPTY_MOVE = 2001,
177  E_NO_UNIT = 2002,
180  E_AMBUSHED = 2005,
183  E_NO_ROUTE = 2008
184  };
185 
186  virtual std::string do_describe() const;
187  virtual const map_location& get_unit_location() const;
188 protected:
189  virtual void do_check_before();
190  virtual void do_check_after();
191  virtual void do_execute();
192  virtual void do_init_for_execution();
193 private:
194  const unit *get_unit();
195  bool test_route(const unit &un);
204 };
205 
206 
207 class recall_result : public action_result {
208 public:
209  recall_result (side_number side, const std::string &unit_id, const map_location& where, const map_location& from);
210 
211  enum tresult {
213  E_NO_GOLD = 6003,
214  E_NO_LEADER = 6004,
217  };
218 
219  virtual std::string do_describe() const;
220 protected:
221  virtual void do_check_before();
222  virtual void do_check_after();
223  virtual void do_execute();
224  virtual void do_init_for_execution();
225 private:
227  const team& my_team);
228  bool test_enough_gold(
229  const team& my_team);
230 
236 };
237 
239 public:
240  recruit_result( side_number side, const std::string& unit_name, const map_location& where, const map_location& from);
241 
242  enum tresult {
245  E_NO_GOLD = 3003,
246  E_NO_LEADER = 3004,
249  };
250 
251  virtual std::string do_describe() const;
252 protected:
253  virtual void do_check_before();
254  virtual void do_check_after();
255  virtual void do_execute();
256  virtual void do_init_for_execution();
257 private:
259  const std::string &recruit);
260  bool test_enough_gold(
261  const team& my_team,
262  const unit_type &type );
263 
269 };
270 
272 public:
274  const map_location& unit_location,
275  bool remove_movement,
276  bool remove_attacks );
277 
278  enum tresult {
279  E_NO_UNIT = 4002,
282  };
283 
284  virtual std::string do_describe() const;
285 protected:
286  virtual void do_check_before();
287  virtual void do_check_after();
288  virtual void do_execute();
289  virtual void do_init_for_execution();
290 private:
291  const unit *get_unit();
293  const bool remove_movement_;
294  const bool remove_attacks_;
295 };
296 
298 public:
300  const std::string& lua_code,
301  const map_location& location );
302 
303  virtual std::string do_describe() const;
304 protected:
305  virtual void do_check_before();
306  virtual void do_check_after();
307  virtual void do_execute();
308  virtual void do_init_for_execution();
309 private:
312 };
313 
314 
315 class actions {
316 
317 public:
318 // =======================================================================
319 // Stateless interface to actions
320 // =======================================================================
321 
322 
323 /**
324  * Ask the game to attack an enemy defender using our unit attacker from attackers current location,
325  * @param side the side which tries to execute the move
326  * @param execute should move be actually executed or not
327  * @param attacker_loc location of attacker
328  * @param defender_loc location of defender
329  * @param attacker_weapon weapon of attacker
330  * @param aggression aggression of attacker, is used to determine attacker's weapon if it is not specified
331  * @retval possible result: ok
332  * @retval possible result: something wrong
333  * @retval possible result: attacker and/or defender are invalid
334  * @retval possible result: attacker doesn't have the specified weapon
335  */
337  bool execute,
338  const map_location& attacker_loc,
339  const map_location& defender_loc,
340  int attacker_weapon,
341  double aggression,
342  const unit_advancements_aspect& advancements = unit_advancements_aspect());
343 
344 
345 /**
346  * Ask the game to move our unit from location 'from' to location 'to', optionally - doing a partial move
347  * @param side the side which tries to execute the move
348  * @param execute should move be actually executed or not
349  * @param from location of our unit
350  * @param to where to move
351  * @param remove_movement set unit movement to 0 in case of successful move
352  * @retval possible result: ok
353  * @retval possible result: something wrong
354  * @retval possible result: move is interrupted
355  * @retval possible result: move is impossible
356  */
358  bool execute,
359  const map_location& from,
360  const map_location& to,
361  bool remove_movement,
362  bool unreach_is_ok = false);
363 
364 
365 
366 /**
367  * Ask the game to recall a unit for us on specified location
368  * @param side the side which tries to execute the move
369  * @param execute should move be actually executed or not
370  * @param unit_id the id of the unit to be recalled.
371  * @param where location where the unit is to be recalled.
372  * @retval possible result: ok
373  * @retval possible_result: something wrong
374  * @retval possible_result: leader not on keep
375  * @retval possible_result: no free space on keep
376  * @retval possible_result: not enough gold
377  */
379  bool execute,
380  const std::string& unit_id,
381  const map_location& where,
382  const map_location& from);
383 
384 
385 
386 /**
387  * Ask the game to recruit a unit for us on specified location
388  * @param side the side which tries to execute the move
389  * @param execute should move be actually executed or not
390  * @param unit_name the name of the unit to be recruited.
391  * @param where location where the unit is to be recruited.
392  * @retval possible result: ok
393  * @retval possible_result: something wrong
394  * @retval possible_result: leader not on keep
395  * @retval possible_result: no free space on keep
396  * @retval possible_result: not enough gold
397  */
399  bool execute,
400  const std::string& unit_name,
401  const map_location& where,
402  const map_location& from);
403 
404 
405 /**
406  * Ask the game to remove unit movements and/or attack
407  * @param side the side which tries to execute the move
408  * @param execute should move be actually executed or not
409  * @param unit_location the location of our unit
410  * @param remove_movement set remaining movements to 0
411  * @param remove_attacks set remaining attacks to 0
412  * @retval possible result: ok
413  * @retval possible_result: something wrong
414  * @retval possible_result: nothing to do
415  */
417  bool execute,
418  const map_location& unit_location,
419  bool remove_movement,
420  bool remove_attacks );
421 
422 
423 /**
424  * Ask the game to run Lua code
425  * @param side the side which tries to execute the move
426  * @param execute should move be actually executed or not
427  * @param lua_code the code to be run
428  * @param location location to be passed to the code as x1/y1
429  * @retval possible result: ok
430  * @retval possible_result: something wrong
431  * @retval possible_result: nothing to do
432  */
434  bool execute,
435  const std::string& lua_code,
436  const map_location& location );
437 
438 
439 /**
440  * get human-readable name of the error by code.
441  * @param error_code error code.
442  * @retval result the name of the error.
443  */
444 const static std::string& get_error_name(int error_code);
445 
446 private:
447 
448 static std::map<int,std::string> error_names_;
449 
450 };
451 
452 
453 ///@todo 1.7.11 important! Add an ai action (and fai function) to set a goto on a unit
454 ///@todo 1.7.11 important! Add an ai action (and fai function) to send a chat message to a player
455 
456 } //end of namespace ai
457 
458 std::ostream &operator<<(std::ostream &s, ai::attack_result const &r);
459 std::ostream &operator<<(std::ostream &s, ai::move_result const &r);
460 std::ostream &operator<<(std::ostream &s, ai::recall_result const &r);
461 std::ostream &operator<<(std::ostream &s, ai::recruit_result const &r);
462 std::ostream &operator<<(std::ostream &s, ai::stopunit_result const &r);
463 std::ostream &operator<<(std::ostream &s, ai::synced_command_result const &r);
464 
465 #endif
bool unreach_is_ok_
Definition: actions.hpp:201
map_location recruit_location_
Definition: actions.hpp:266
map_location recruit_from_
Definition: actions.hpp:267
virtual void do_init_for_execution()
Definition: actions.cpp:926
virtual void do_check_after()
Definition: actions.cpp:859
bool has_ambusher_
Definition: actions.hpp:202
std::ostream & operator<<(std::ostream &s, ai::attack_result const &r)
Definition: actions.cpp:1127
virtual void do_execute()
Definition: actions.cpp:259
void check_before()
Definition: actions.cpp:85
bool remove_movement_
Definition: actions.hpp:198
map_location recall_from_
Definition: actions.hpp:234
unit_const_ptr get_recall_unit(const team &my_team)
Definition: actions.cpp:537
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:994
virtual void do_check_before()
Definition: actions.cpp:394
Definition: unit.hpp:95
static std::map< int, std::string > error_names_
Definition: actions.hpp:448
team & get_my_team() const
Definition: actions.cpp:166
const unit * get_unit()
Definition: actions.cpp:340
virtual void do_execute()=0
map_location unit_location_
Definition: actions.hpp:200
virtual void do_init_for_execution()
Definition: actions.cpp:985
bool is_success() const
Definition: actions.cpp:151
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
virtual void do_check_after()
Definition: actions.cpp:411
bool test_route(const unit &un)
Definition: actions.cpp:359
map_location recall_location_
Definition: actions.hpp:233
virtual void do_init_for_execution()
Definition: actions.cpp:520
const map_location & where_
Definition: actions.hpp:265
const std::string & unit_id_
Definition: actions.hpp:231
const unit_type * get_unit_type_known(const std::string &recruit)
Definition: actions.cpp:688
virtual std::string do_describe() const
Definition: actions.cpp:770
virtual void do_init_for_execution()
Definition: actions.cpp:820
virtual std::string do_describe() const
Definition: actions.cpp:246
virtual std::string do_describe() const
Definition: actions.cpp:618
virtual void do_check_before()
Definition: actions.cpp:850
int get_status() const
Definition: actions.cpp:146
const unit_advancements_aspect & advancements_
Definition: actions.hpp:164
virtual void do_execute()
Definition: actions.cpp:444
const map_location from_
Definition: actions.hpp:196
static config unit_name(const unit *u)
Definition: reports.cpp:133
action_result(side_number side)
Definition: actions.cpp:68
virtual void do_check_after()=0
const std::string & lua_code_
Definition: actions.hpp:310
void init_for_execution()
Definition: actions.cpp:109
bool has_interrupted_teleport_
Definition: actions.hpp:203
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
A small explanation about what's going on here: Each action has access to two game_info objects First...
Definition: actions.cpp:57
virtual void do_init_for_execution()
Definition: actions.cpp:671
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:1019
GLuint64EXT * result
Definition: glew.h:10727
virtual void do_check_after()
Definition: actions.cpp:752
Structure which holds a single route between one location and another.
Definition: pathfind.hpp:132
bool return_value_checked_
Definition: actions.hpp:116
bool is_execution() const
Definition: actions.cpp:156
virtual void do_check_after()
Definition: actions.cpp:600
virtual void do_execute()
Definition: actions.cpp:955
bool location_checked_
Definition: actions.hpp:235
Encapsulates the map of the game.
Definition: map.hpp:37
bool test_enough_gold(const team &my_team, const unit_type &type)
Definition: actions.cpp:698
synced_command_result(side_number side, const std::string &lua_code, const map_location &location)
Definition: actions.cpp:932
double aggression_
Definition: actions.hpp:163
virtual void do_check_after()
Definition: actions.cpp:242
virtual void do_execute()
Definition: actions.cpp:633
const bool remove_movement_
Definition: actions.hpp:293
friend void sim_gamestate_changed(action_result *result, bool gamestate_changed)
Definition: actions.cpp:1117
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:173
const std::string & unit_name_
Definition: actions.hpp:264
virtual std::string do_describe() const
Definition: actions.cpp:429
virtual void do_check_before()
Definition: actions.cpp:707
Encapsulates the map of the game.
Definition: location.hpp:38
Various functions related to moving units.
const bool remove_attacks_
Definition: actions.hpp:294
virtual void do_check_before()
Definition: actions.cpp:555
const map_location & defender_loc_
Definition: actions.hpp:161
void check_after()
Definition: actions.cpp:80
Game information for the AI.
virtual std::string do_describe() const
Definition: actions.cpp:946
virtual const map_location & get_unit_location() const
Definition: actions.cpp:406
bool test_enough_gold(const team &my_team)
Definition: actions.cpp:546
void set_gamestate_changed()
Definition: actions.cpp:141
virtual void do_check_before()
Definition: actions.cpp:937
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:1030
stopunit_result(side_number side, const map_location &unit_location, bool remove_movement, bool remove_attacks)
Definition: actions.cpp:826
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:1052
bool is_gamestate_changed_
Definition: actions.hpp:127
virtual std::string do_describe() const
Definition: actions.cpp:876
virtual void do_check_before()
Definition: actions.cpp:177
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
const map_location & unit_location_
Definition: actions.hpp:292
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:1041
void set_error(int error_code, bool log_as_error=true)
Definition: actions.cpp:128
boost::shared_ptr< pathfind::plain_route > route_
Definition: actions.hpp:199
bool is_gamestate_changed() const
Definition: actions.cpp:117
int get_side() const
Definition: actions.hpp:89
const map_location to_
Definition: actions.hpp:197
virtual void do_execute()
Definition: actions.cpp:895
virtual std::string do_describe() const =0
int side_number
Definition: game_info.hpp:44
recall_result(side_number side, const std::string &unit_id, const map_location &where, const map_location &from)
Definition: actions.cpp:526
const map_location where_
Definition: actions.hpp:232
const unit * get_unit()
Definition: actions.cpp:831
const map_location & attacker_loc_
Definition: actions.hpp:160
GLdouble s
Definition: glew.h:1358
move_result(side_number side, const map_location &from, const map_location &to, bool remove_movement, bool unreach_is_ok)
Definition: actions.cpp:326
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual void do_check_after()
Definition: actions.cpp:942
recruit_result(side_number side, const std::string &unit_name, const map_location &where, const map_location &from)
Definition: actions.cpp:677
virtual ~action_result()
Definition: actions.cpp:73
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:1007
virtual void do_init_for_execution()
Definition: actions.cpp:320
static const std::string & get_error_name(int error_code)
get human-readable name of the error by code.
Definition: actions.cpp:1062
const map_location & location_
Definition: actions.hpp:311
virtual void do_execute()
Definition: actions.cpp:785
game_info & get_info() const
Definition: actions.cpp:161