The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
action.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
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  * @file
17  */
18 
19 #ifndef WB_ACTION_HPP_
20 #define WB_ACTION_HPP_
21 
22 #include "typedefs.hpp"
23 #include "map/location.hpp"
24 #include "game_errors.hpp"
25 
26 namespace wb {
27 
28 class visitor;
29 
30 /**
31  * Abstract base class for all the whiteboard planned actions.
32  */
33 class action : public boost::enable_shared_from_this<action>
34 {
35 public:
36  action(size_t team_index, bool hidden);
37  action(config const&, bool hidden); // For deserialization
38  virtual ~action();
39 
40  virtual std::ostream& print(std::ostream& s) const = 0;
41 
42  virtual void accept(visitor& v) = 0;
43 
44  /**
45  * Output parameters:
46  * success: Whether or not to continue an execute-all after this execution
47  * complete: Whether or not to delete this action after execution
48  */
49  virtual void execute(bool& success, bool& complete) = 0;
50 
51  /** Applies temporarily the result of this action to the specified unit map. */
52  virtual void apply_temp_modifier(unit_map& unit_map) = 0;
53  /** Removes the result of this action from the specified unit map. */
54  virtual void remove_temp_modifier(unit_map& unit_map) = 0;
55 
56  /** Gets called by display when drawing a hex, to allow actions to draw to the screen. */
57  virtual void draw_hex(const map_location& hex) = 0;
58  /** Redrawing function, called each time the action situation might have changed. */
59  virtual void redraw(){}
60 
61  /** Sets whether or not the action should be drawn on the screen. */
62  void hide();
63  void show();
64  bool hidden() const {return hidden_;}
65 
66  /** Indicates whether this hex is the preferred hex to draw the numbering for this action. */
67  bool is_numbering_hex(const map_location& hex) const {return hex==get_numbering_hex();}
68  virtual map_location get_numbering_hex() const = 0;
69 
70  /** Return the unit targeted by this action. Null if unit doesn't exist. */
71  virtual unit_ptr get_unit() const = 0;
72 
73  /**
74  * Returns the id of the unit targeted by this action.
75  * @retval 0 no unit is targeted.
76  */
77  size_t get_unit_id() const;
78 
79  /** @return pointer to the fake unit used only for visuals */
80  virtual fake_unit_ptr get_fake_unit() = 0;
81  /** Returns the index of the team that owns this action */
82  size_t team_index() const { return team_index_; }
83  /** Returns the number of the side that owns this action, i.e. the team index + 1. */
84  int side_number() const
85  {
86  return static_cast<int>(team_index_) + 1;
87  }
88 
89  /** Constructs and returns a config object representing this object. */
90  virtual config to_config() const;
91  /** Constructs an object of a subclass of wb::action using a config. Current behavior is to return a null pointer for unrecognized config. */
92  static action_ptr from_config(config const&, bool hidden);
93 
94  struct ctor_err : public game::error
95  {
96  ctor_err(const std::string& message) : game::error(message){}
97  };
98 
99  /**
100  * Possible errors.
101  *
102  * Returned by the @ref check function.
103  */
104  enum error
105  {
106  OK,
118  };
119 
120  /**
121  * Check the validity of the action.
122  *
123  * @return the error preventing the action from being executed.
124  * @retval OK if there isn't any error (the action can be executed.)
125  */
126  virtual error check_validity() const = 0;
127 
128  /**
129  * Returns whether this action is valid or not.
130  *
131  * @note This value is now calculated each time the function is called.
132  */
133  bool valid(){ return check_validity()==OK; }
134 
135 private:
136  /** Called by the non-virtual hide() and show(), respectively. */
137  virtual void do_hide() {}
138  virtual void do_show() {}
139 
140  size_t team_index_;
141  bool hidden_;
142 };
143 
144 std::ostream& operator<<(std::ostream& s, action_ptr action);
145 std::ostream& operator<<(std::ostream& s, action_const_ptr action);
146 
147 } // end namespace wb
148 
149 #endif /* WB_ACTION_HPP_ */
bool hidden() const
Definition: action.hpp:64
size_t team_index_
Definition: action.hpp:140
bool hidden_
Definition: action.hpp:141
virtual error check_validity() const =0
Check the validity of the action.
virtual fake_unit_ptr get_fake_unit()=0
virtual void redraw()
Redrawing function, called each time the action situation might have changed.
Definition: action.hpp:59
bool valid()
Returns whether this action is valid or not.
Definition: action.hpp:133
size_t team_index() const
Returns the index of the team that owns this action.
Definition: action.hpp:82
virtual void draw_hex(const map_location &hex)=0
Gets called by display when drawing a hex, to allow actions to draw to the screen.
virtual void do_show()
Definition: action.hpp:138
error
Possible errors.
Definition: action.hpp:104
const GLdouble * v
Definition: glew.h:1359
Contains typedefs for the whiteboard.
ctor_err(const std::string &message)
Definition: action.hpp:96
virtual void execute(bool &success, bool &complete)=0
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
virtual std::ostream & print(std::ostream &s) const =0
Definition: action.cpp:45
size_t get_unit_id() const
Returns the id of the unit targeted by this action.
Definition: action.cpp:117
virtual unit_ptr get_unit() const =0
Return the unit targeted by this action.
Encapsulates the map of the game.
Definition: location.hpp:38
int side_number() const
Returns the number of the side that owns this action, i.e.
Definition: action.hpp:84
static action_ptr from_config(config const &, bool hidden)
Constructs an object of a subclass of wb::action using a config.
Definition: action.cpp:59
void hide()
Sets whether or not the action should be drawn on the screen.
Definition: action.cpp:79
virtual ~action()
Definition: action.cpp:113
void show()
Definition: action.cpp:87
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:50
Container associating units to locations.
Definition: map.hpp:90
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:33
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
action(size_t team_index, bool hidden)
Definition: action.cpp:95
virtual void apply_temp_modifier(unit_map &unit_map)=0
Applies temporarily the result of this action to the specified unit map.
virtual void accept(visitor &v)=0
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:33
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
virtual void remove_temp_modifier(unit_map &unit_map)=0
Removes the result of this action from the specified unit map.
GLdouble s
Definition: glew.h:1358
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.
virtual map_location get_numbering_hex() const =0
Definition: display.hpp:47
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses...
Definition: visitor.hpp:32
bool is_numbering_hex(const map_location &hex) const
Indicates whether this hex is the preferred hex to draw the numbering for this action.
Definition: action.hpp:67
virtual void do_hide()
Called by the non-virtual hide() and show(), respectively.
Definition: action.hpp:137