The Battle for Wesnoth  1.19.5+dev
action.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
3  by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * @file
18  */
19 
20 #pragma once
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 std::enable_shared_from_this<action>
34 {
35 public:
36  action(std::size_t team_index, bool hidden);
37  action(const config&, 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. */
53  /** Removes the result of this action from the specified unit map. */
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  /** Returns true for recall and recruit actions */
74  virtual bool places_new_unit() const { return false; };
75  /**
76  * Returns the id of the unit targeted by this action.
77  * @retval 0 no unit is targeted.
78  */
79  virtual std::size_t get_unit_id() const;
80 
81  /** @return pointer to the fake unit used only for visuals */
83  /** Returns the index of the team that owns this action */
84  std::size_t team_index() const { return team_index_; }
85  /** Returns the number of the side that owns this action, i.e. the team index + 1. */
86  int side_number() const
87  {
88  return static_cast<int>(team_index_) + 1;
89  }
90 
91  /** Constructs and returns a config object representing this object. */
92  virtual config to_config() const;
93  /** Constructs an object of a subclass of wb::action using a config. Current behavior is to return a null pointer for unrecognized config. */
94  static action_ptr from_config(const config&, bool hidden);
95 
96  struct ctor_err : public game::error
97  {
98  ctor_err(const std::string& message) : game::error(message){}
99  };
100 
101  /**
102  * Possible errors.
103  *
104  * Returned by the @ref check_validity function.
105  */
106  enum error
107  {
108  OK,
119  NO_LEADER
120  };
121 
122  /**
123  * Check the validity of the action.
124  *
125  * @return the error preventing the action from being executed.
126  * @retval OK if there isn't any error (the action can be executed.)
127  */
128  virtual error check_validity() const = 0;
129 
130  /**
131  * Returns whether this action is valid or not.
132  *
133  * @note This value is now calculated each time the function is called.
134  */
135  bool valid(){ return check_validity()==OK; }
136 
137 private:
138  /** Called by the non-virtual hide() and show(), respectively. */
139  virtual void do_hide() {}
140  virtual void do_show() {}
141 
142  std::size_t team_index_;
143  bool hidden_;
144 };
145 
146 std::ostream& operator<<(std::ostream& s, action_ptr action);
147 std::ostream& operator<<(std::ostream& s, action_const_ptr action);
148 
149 } // end namespace wb
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Holds a temporary unit that can be drawn on the map without being placed in the unit_map.
Container associating units to locations.
Definition: map.hpp:98
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:34
virtual unit_ptr get_unit() const =0
Return the unit targeted by this action.
std::size_t team_index_
Definition: action.hpp:142
bool hidden() const
Definition: action.hpp:64
virtual void remove_temp_modifier(unit_map &unit_map)=0
Removes the result of this action from the specified unit map.
void hide()
Sets whether or not the action should be drawn on the screen.
Definition: action.cpp:81
bool valid()
Returns whether this action is valid or not.
Definition: action.hpp:135
virtual void do_show()
Definition: action.hpp:140
virtual ~action()
Definition: action.cpp:115
virtual void do_hide()
Called by the non-virtual hide() and show(), respectively.
Definition: action.hpp:139
static action_ptr from_config(const config &, bool hidden)
Constructs an object of a subclass of wb::action using a config.
Definition: action.cpp:60
std::size_t team_index() const
Returns the index of the team that owns this action.
Definition: action.hpp:84
virtual void redraw()
Redrawing function, called each time the action situation might have changed.
Definition: action.hpp:59
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 execute(bool &success, bool &complete)=0
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
virtual bool places_new_unit() const
Returns true for recall and recruit actions.
Definition: action.hpp:74
action(std::size_t team_index, bool hidden)
Definition: action.cpp:97
virtual std::size_t get_unit_id() const
Returns the id of the unit targeted by this action.
Definition: action.cpp:119
virtual void apply_temp_modifier(unit_map &unit_map)=0
Applies temporarily the result of this action to the specified unit map.
void show()
Definition: action.cpp:89
virtual fake_unit_ptr get_fake_unit()=0
virtual std::ostream & print(std::ostream &s) const =0
Definition: action.cpp:46
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:51
bool hidden_
Definition: action.hpp:143
error
Possible errors.
Definition: action.hpp:107
@ INVALID_LOCATION
Definition: action.hpp:109
@ NO_ATTACK_LEFT
Definition: action.hpp:115
@ NOT_ENOUGH_GOLD
Definition: action.hpp:118
@ NOT_AN_ENEMY
Definition: action.hpp:116
@ UNIT_CHANGED
Definition: action.hpp:111
@ LOCATION_OCCUPIED
Definition: action.hpp:112
@ UNIT_UNAVAILABLE
Definition: action.hpp:117
int side_number() const
Returns the number of the side that owns this action, i.e.
Definition: action.hpp:86
virtual map_location get_numbering_hex() const =0
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 accept(visitor &v)=0
virtual error check_validity() const =0
Check the validity of the action.
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses.
Definition: visitor.hpp:33
Definition: display.hpp:45
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:34
std::shared_ptr< action > action_ptr
Definition: typedefs.hpp:62
std::shared_ptr< action const > action_const_ptr
Definition: typedefs.hpp:63
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string message
Definition: exceptions.hpp:30
Encapsulates the map of the game.
Definition: location.hpp:45
ctor_err(const std::string &message)
Definition: action.hpp:98
static map_location::direction s
Contains typedefs for the whiteboard.