The Battle for Wesnoth  1.17.0-dev
attack.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
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 "move.hpp"
23 
24 namespace wb
25 {
26 
27 class attack: public move
28 {
29 public:
30  attack(std::size_t team_index, bool hidden, unit& mover, const map_location& target_hex, int weapon_choice, const pathfind::marked_route& route,
31  arrow_ptr arrow, fake_unit_ptr fake_unit);
32  attack(const config&, bool hidden); // For deserialization
33  virtual ~attack();
34 
35  virtual std::ostream& print(std::ostream& s) const;
36 
37  virtual void accept(visitor& v);
38 
39  virtual void execute(bool& success, bool& complete);
40 
41  /**
42  * Check the validity of the action.
43  *
44  * @return the error preventing the action from being executed.
45  * @retval OK if there isn't any error (the action can be executed.)
46  */
47  virtual error check_validity() const;
48 
49  /** Applies temporarily the result of this action to the specified unit map. */
50  virtual void apply_temp_modifier(unit_map& unit_map);
51  /** Removes the result of this action from the specified unit map. */
52  virtual void remove_temp_modifier(unit_map& unit_map);
53 
54  /** Gets called by display when drawing a hex, to allow actions to draw to the screen. */
55  virtual void draw_hex(const map_location& hex);
56  /** Redrawing function, called each time the action situation might have changed. */
57  virtual void redraw();
58 
59  const map_location& get_target_hex() const {return target_hex_; }
60 
61  virtual config to_config() const;
62 
63 protected:
64 
65  std::shared_ptr<attack> shared_from_this() {
66  return std::static_pointer_cast<attack>(move::shared_from_this());
67  }
68 
69 private:
70 
71  void init();
72 
73  virtual void do_hide() {invalidate();}
74  virtual void do_show() {invalidate();}
75 
76  /** invalidates the move-destination and attack-target hexes */
77  void invalidate();
78 
79  /** the target of the attack */
81 
85 };
86 
87 /** Dumps an attack on a stream, for debug purposes. */
88 std::ostream& operator<<(std::ostream &s, attack_ptr attack);
89 std::ostream& operator<<(std::ostream &s, attack_const_ptr attack);
90 
91 } // end namespace wb
map_location target_hex_
the target of the attack
Definition: attack.hpp:80
virtual void apply_temp_modifier(unit_map &unit_map)
Applies temporarily the result of this action to the specified unit map.
Definition: attack.cpp:151
virtual void execute(bool &success, bool &complete)
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
Definition: attack.cpp:115
int weapon_choice_
Definition: attack.hpp:82
This class represents a single unit of a specific type.
Definition: unit.hpp:121
attack(std::size_t team_index, bool hidden, unit &mover, const map_location &target_hex, int weapon_choice, const pathfind::marked_route &route, arrow_ptr arrow, fake_unit_ptr fake_unit)
Definition: attack.cpp:56
int attack_movement_cost_
Definition: attack.hpp:83
virtual void redraw()
Redrawing function, called each time the action situation might have changed.
Definition: attack.cpp:222
std::shared_ptr< attack > attack_ptr
Definition: typedefs.hpp:70
int temp_movement_subtracted_
Definition: attack.hpp:84
std::shared_ptr< attack const > attack_const_ptr
Definition: typedefs.hpp:71
std::shared_ptr< attack > shared_from_this()
Definition: attack.hpp:65
virtual void do_show()
Definition: attack.hpp:74
virtual error check_validity() const
Check the validity of the action.
Definition: attack.cpp:228
std::size_t team_index() const
Returns the index of the team that owns this action.
Definition: action.hpp:84
virtual ~attack()
Definition: attack.cpp:94
virtual void accept(visitor &v)
Definition: attack.cpp:99
Arrows destined to be drawn on the map.
Definition: arrow.hpp:30
error
Possible errors.
Definition: action.hpp:106
Structure which holds a single route and marks for special events.
Definition: pathfind.hpp:141
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: attack.cpp:256
Encapsulates the map of the game.
Definition: location.hpp:38
virtual void remove_temp_modifier(unit_map &unit_map)
Removes the result of this action from the specified unit map.
Definition: attack.cpp:176
static map_location::DIRECTION s
const map_location & get_target_hex() const
Definition: attack.hpp:59
std::shared_ptr< move > shared_from_this()
Definition: move.hpp:104
std::shared_ptr< arrow > arrow_ptr
Definition: typedefs.hpp:60
bool hidden() const
Definition: action.hpp:64
virtual void do_hide()
Called by the non-virtual hide() and show(), respectively.
Definition: attack.hpp:73
Container associating units to locations.
Definition: map.hpp:98
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:34
virtual std::ostream & print(std::ostream &s) const
Definition: attack.cpp:49
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
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: attack.cpp:191
void invalidate()
invalidates the move-destination and attack-target hexes
Definition: attack.cpp:105
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:35
Definition: display.hpp:49
void init()
Definition: attack.cpp:89
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses...
Definition: visitor.hpp:32