The Battle for Wesnoth  1.19.0-dev
move.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 "action.hpp"
23 
24 #include <cassert>
25 
27 class unit;
28 
29 namespace wb {
30 
31 /**
32  * A planned move, represented on the map by an arrow and
33  * a ghosted unit in the destination hex.
34  */
35 class move : public action
36 {
37 public:
38  move(std::size_t team_index, bool hidden, unit& mover, const pathfind::marked_route& route,
39  arrow_ptr arrow, fake_unit_ptr fake_unit);
40  move(const config&, bool hidden); // For deserialization
41  virtual ~move();
42 
43  virtual std::ostream& print(std::ostream& s) const;
44 
45  virtual void accept(visitor& v);
46 
47  virtual void execute(bool& success, bool& complete);
48 
49  /**
50  * Check the validity of the action.
51  *
52  * @return the error preventing the action from being executed.
53  * @retval OK if there isn't any error (the action can be executed.)
54  */
55  virtual error check_validity() const;
56 
57  /** Return the unit targeted by this action. Null if unit doesn't exist. */
58  virtual unit_ptr get_unit() const;
59  virtual size_t get_unit_id() const { return unit_underlying_id_; }
60  /** @return pointer to the fake unit used only for visuals */
61  virtual fake_unit_ptr get_fake_unit() { return fake_unit_; }
62 
63  virtual map_location get_source_hex() const;
64  virtual map_location get_dest_hex() const;
65 
66  std::size_t raw_uid() const { return unit_underlying_id_; }
67 
68  void modify_unit(unit& new_unit);
69  virtual void set_route(const pathfind::marked_route& route);
70  virtual const pathfind::marked_route& get_route() const { assert(route_); return *route_; }
71  /**
72  * attempts to pathfind a new marked route for this path between these two hexes;
73  * returns true and assigns it to the internal route if successful.
74  */
75  virtual bool calculate_new_route(const map_location& source_hex, const map_location& dest_hex);
76 
77  virtual arrow_ptr get_arrow() { return arrow_; }
78 
79  /** Applies temporarily the result of this action to the specified unit map. */
80  virtual void apply_temp_modifier(unit_map& unit_map);
81  /** Removes the result of this action from the specified unit map. */
82  virtual void remove_temp_modifier(unit_map& unit_map);
83 
84  /** Gets called by display when drawing a hex, to allow actions to draw to the screen. */
85  virtual void draw_hex(const map_location& hex);
86  /** Redrawing function, called each time the action situation might have changed. */
87  void redraw();
88 
89  /** Assigns a turn number to display to this planned move. Assigning zero removes any turn number. */
90  virtual void set_turn_number(int turn) { turn_number_ = turn; }
91 
92  virtual map_location get_numbering_hex() const;
93 
94  virtual config to_config() const;
95 
96  /** @todo Make use of safe_enum idiom? */
101 
102 protected:
103 
104  std::shared_ptr<move> shared_from_this() {
105  return std::static_pointer_cast<move>(action::shared_from_this());
106  }
107 
108  int calculate_moves_left(unit& u);
109 
110  std::size_t unit_underlying_id_;
111  std::string unit_id_;
112  std::unique_ptr<pathfind::marked_route> route_;
114  /** Turn end number to draw if greater than zero. Assigned by the map builder. */
116 
119 
122 
123 private:
124  virtual void do_hide();
125  virtual void do_show();
126 
127  void hide_fake_unit();
128  void show_fake_unit();
129 
130  void init(unit* u = nullptr);
131  void update_arrow_style();
132  std::unique_ptr<temporary_unit_mover> mover_;
134 };
135 
136 /** Dumps an move on a stream, for debug purposes. */
137 std::ostream &operator<<(std::ostream &s, move_ptr move);
138 std::ostream &operator<<(std::ostream &s, move_const_ptr move);
139 
140 } // end namespace wb
Arrows destined to be drawn on the map.
Definition: arrow.hpp:30
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
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
This class represents a single unit of a specific type.
Definition: unit.hpp:133
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:34
bool hidden() const
Definition: action.hpp:64
std::size_t team_index() const
Returns the index of the team that owns this action.
Definition: action.hpp:84
error
Possible errors.
Definition: action.hpp:107
A planned move, represented on the map by an arrow and a ghosted unit in the destination hex.
Definition: move.hpp:36
move(std::size_t team_index, bool hidden, unit &mover, const pathfind::marked_route &route, arrow_ptr arrow, fake_unit_ptr fake_unit)
Definition: move.cpp:68
void hide_fake_unit()
Definition: move.cpp:419
void redraw()
Redrawing function, called each time the action situation might have changed.
Definition: move.cpp:565
virtual void set_turn_number(int turn)
Assigns a turn number to display to this planned move.
Definition: move.hpp:90
virtual void accept(visitor &v)
Definition: move.cpp:199
void show_fake_unit()
Definition: move.cpp:426
virtual void remove_temp_modifier(unit_map &unit_map)
Removes the result of this action from the specified unit map.
Definition: move.cpp:371
virtual unit_ptr get_unit() const
Return the unit targeted by this action.
Definition: move.cpp:287
virtual error check_validity() const
Check the validity of the action.
Definition: move.cpp:438
std::string unit_id_
Definition: move.hpp:111
std::unique_ptr< pathfind::marked_route > route_
Definition: move.hpp:112
virtual void draw_hex(const map_location &hex)
Gets called by display when drawing a hex, to allow actions to draw to the screen.
Definition: move.cpp:394
virtual ~move()
Definition: move.cpp:197
void set_arrow_texture(ARROW_TEXTURE x) const
Definition: move.hpp:100
arrow_ptr arrow_
Definition: move.hpp:117
void modify_unit(unit &new_unit)
Definition: move.cpp:314
bool fake_unit_hidden_
Definition: move.hpp:133
virtual void do_hide()
Called by the non-virtual hide() and show(), respectively.
Definition: move.cpp:405
virtual std::ostream & print(std::ostream &s) const
Definition: move.cpp:56
virtual map_location get_dest_hex() const
Definition: move.cpp:302
virtual void do_show()
Definition: move.cpp:412
std::size_t unit_underlying_id_
Definition: move.hpp:110
virtual void set_route(const pathfind::marked_route &route)
Definition: move.cpp:308
virtual size_t get_unit_id() const
Returns the id of the unit targeted by this action.
Definition: move.hpp:59
int movement_cost_
Definition: move.hpp:113
void set_arrow_brightness(ARROW_BRIGHTNESS x) const
Definition: move.hpp:98
std::unique_ptr< temporary_unit_mover > mover_
Definition: move.hpp:132
virtual const pathfind::marked_route & get_route() const
Definition: move.hpp:70
ARROW_TEXTURE arrow_texture_
Definition: move.hpp:121
virtual bool calculate_new_route(const map_location &source_hex, const map_location &dest_hex)
attempts to pathfind a new marked route for this path between these two hexes; returns true and assig...
Definition: move.cpp:320
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: move.cpp:504
int turn_number_
Turn end number to draw if greater than zero.
Definition: move.hpp:115
int calculate_moves_left(unit &u)
Definition: move.cpp:540
std::shared_ptr< move > shared_from_this()
Definition: move.hpp:104
virtual map_location get_source_hex() const
Definition: move.cpp:296
virtual fake_unit_ptr get_fake_unit()
Definition: move.hpp:61
virtual arrow_ptr get_arrow()
Definition: move.hpp:77
void init(unit *u=nullptr)
Definition: move.cpp:148
virtual void execute(bool &success, bool &complete)
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
Definition: move.cpp:204
void update_arrow_style()
Definition: move.cpp:574
ARROW_BRIGHTNESS arrow_brightness_
Definition: move.hpp:120
ARROW_BRIGHTNESS
Definition: move.hpp:97
@ ARROW_BRIGHTNESS_FOCUS
Definition: move.hpp:97
@ ARROW_BRIGHTNESS_STANDARD
Definition: move.hpp:97
@ ARROW_BRIGHTNESS_HIGHLIGHTED
Definition: move.hpp:97
fake_unit_ptr fake_unit_
Definition: move.hpp:118
ARROW_TEXTURE
Definition: move.hpp:99
@ ARROW_TEXTURE_INVALID
Definition: move.hpp:99
@ ARROW_TEXTURE_VALID
Definition: move.hpp:99
std::size_t raw_uid() const
Definition: move.hpp:66
virtual map_location get_numbering_hex() const
Definition: move.cpp:433
virtual void apply_temp_modifier(unit_map &unit_map)
Applies temporarily the result of this action to the specified unit map.
Definition: move.cpp:333
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< move > move_ptr
Definition: typedefs.hpp:68
std::shared_ptr< arrow > arrow_ptr
Definition: typedefs.hpp:60
std::shared_ptr< move const > move_const_ptr
Definition: typedefs.hpp:69
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
Encapsulates the map of the game.
Definition: location.hpp:38
Structure which holds a single route and marks for special events.
Definition: pathfind.hpp:142
This object is used to temporary move a unit in the unit map, swapping out any unit that is already t...
Definition: game_board.hpp:224
static map_location::DIRECTION s