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