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