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