The Battle for Wesnoth  1.17.0-dev
suppose_dead.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2021
3  by Tommy Schmitz
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 
21 
22 #include "whiteboard/visitor.hpp"
23 #include "whiteboard/manager.hpp"
25 #include "whiteboard/utility.hpp"
26 
27 #include "arrow.hpp"
28 #include "config.hpp"
29 #include "display.hpp"
30 #include "game_end_exceptions.hpp"
31 #include "mouse_events.hpp"
32 #include "play_controller.hpp"
33 #include "replay.hpp"
34 #include "resources.hpp"
35 #include "units/unit.hpp"
36 #include "units/udisplay.hpp"
37 #include "units/map.hpp"
38 
39 namespace wb
40 {
41 
42 std::ostream& operator<<(std::ostream &s, suppose_dead_ptr sup_d)
43 {
44  assert(sup_d);
45  return sup_d->print(s);
46 }
47 
48 std::ostream& operator<<(std::ostream &s, suppose_dead_const_ptr sup_d)
49 {
50  assert(sup_d);
51  return sup_d->print(s);
52 }
53 
54 std::ostream& suppose_dead::print(std::ostream &s) const
55 {
56  s << "Suppose-dead for unit " << get_unit()->name() << " [" << get_unit()->id() << "] "
57  << "at (" << loc_ << ")";
58  return s;
59 }
60 
61 suppose_dead::suppose_dead(std::size_t team_index, bool hidden, unit& curr_unit, const map_location& loc)
62  : action(team_index,hidden)
63  , unit_underlying_id_(curr_unit.underlying_id())
64  , unit_id_(curr_unit.id())
65  , loc_(loc)
66 {
67  this->init();
68 }
69 
71  : action(cfg,hidden)
73  , unit_id_()
74  , loc_(cfg.child("loc_")["x"],cfg.child("loc_")["y"], wml_loc())
75 {
76  // Construct and validate unit_
77  unit_map::iterator unit_itor = resources::gameboard->units().find(cfg["unit_"]);
78  if(unit_itor == resources::gameboard->units().end())
79  throw action::ctor_err("suppose_dead: Invalid underlying_id");
80 
81  unit_underlying_id_ = unit_itor->underlying_id();
82  unit_id_ = unit_itor->id();
83 
84  this->init();
85 }
86 
88 {
90 }
91 
93 {
94  //invalidate hex so that skull indicator is properly cleared
97 }
98 
100 {
102  if (itor.valid())
103  return itor.get_shared_ptr();
104  else
105  return unit_ptr();
106 }
107 
109 {
110  v.visit(shared_from_this());
111 }
112 
113 void suppose_dead::execute(bool& success, bool& complete)
114  {success = false; complete = true;}
115 
117 {
118  // Remove the unit
119  const unit_const_ptr removed_unit = unit_map.extract(loc_);
120  DBG_WB << "Suppose dead: Temporarily removing unit " << removed_unit->name() << " [" << removed_unit->id()
121  << "] from (" << loc_ << ")\n";
122 
123  // Just check to make sure we removed the unit we expected to remove
124  assert(get_unit().get() == removed_unit.get());
125 }
126 
128 {
129  // Just check to make sure the hex is empty
131  assert(unit_it == resources::gameboard->units().end());
132 
133  // Restore the unit
134  unit_map.insert(get_unit());
135 }
136 
138 {
139  if(hex == loc_) //add symbol to hex
140  {
141  //@todo: Possibly use a different layer
143 
146  display::get_singleton()->drawing_buffer_add(layer, loc_, xpos, ypos,
147  image::get_image("whiteboard/suppose_dead.png", image::SCALED_TO_HEX));
148  }
149 }
150 
152 {
154 }
155 
157 {
158  if(!get_source_hex().valid()) {
159  return INVALID_LOCATION;
160  }
161  //Check that the unit still exists in the source hex
163  if(unit_it == resources::gameboard->units().end()) {
164  return NO_UNIT;
165  }
166  //check if the unit in the source hex has the same unit id as before,
167  //i.e. that it's the same unit
168  if(unit_id_ != unit_it->id()) {
169  return UNIT_CHANGED;
170  }
171 
172  return OK;
173 }
174 
176 {
177  config final_cfg = action::to_config();
178 
179  final_cfg["type"]="suppose_dead";
180  final_cfg["unit_"]=static_cast<int>(unit_underlying_id_);
181  final_cfg["unit_id_"]=unit_id_;
182 
183  config loc_cfg;
184  loc_cfg["x"]=loc_.wml_x();
185  loc_cfg["y"]=loc_.wml_y();
186  final_cfg.add_child("loc_", std::move(loc_cfg));
187 
188  return final_cfg;
189 }
190 
191 } // end namespace wb
virtual void apply_temp_modifier(unit_map &unit_map)
Applies temporarily the result of this action to the specified unit map.
surface get_image(const image::locator &i_locator, TYPE type)
Caches and returns an image.
Definition: picture.cpp:816
Arrows destined to be drawn on the map.
virtual map_location get_source_hex() const
Return the location at which this action was planned.
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:92
virtual unit_ptr get_unit() const
Return the unit targeted by this action.
virtual const unit_map & units() const override
Definition: game_board.hpp:112
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3026
This class represents a single unit of a specific type.
Definition: unit.hpp:121
virtual void remove_temp_modifier(unit_map &unit_map)
Removes the result of this action from the specified unit map.
virtual void redraw()
Redrawing function, called each time the action situation might have changed.
umap_retval_pair_t insert(unit_ptr p)
Inserts the unit pointed to by p into the map.
Definition: map.cpp:134
Arrows from the arrows framework.
Definition: display.hpp:854
int wml_x() const
Definition: location.hpp:153
virtual void visit(move_ptr move)=0
Replay control code.
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
std::shared_ptr< suppose_dead > shared_from_this()
drawing_layer
The layers to render something on.
Definition: display.hpp:824
Definitions for the interface to Wesnoth Markup Language (WML).
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
std::string unit_id_
virtual void accept(visitor &v)
std::size_t team_index() const
Returns the index of the team that owns this action.
Definition: action.hpp:84
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
bool valid()
Returns whether this action is valid or not.
Definition: action.hpp:135
suppose_dead(std::size_t team_index, bool hidden, unit &curr_unit, const map_location &loc)
std::shared_ptr< suppose_dead const > suppose_dead_const_ptr
Definition: typedefs.hpp:77
error
Possible errors.
Definition: action.hpp:106
int wml_y() const
Definition: location.hpp:154
game_board * gameboard
Definition: resources.cpp:21
virtual void execute(bool &success, bool &complete)
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
map_display and display: classes which take care of displaying the map and game-data on the screen...
Encapsulates the map of the game.
Definition: location.hpp:38
unit_iterator find(std::size_t id)
Definition: map.cpp:310
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
pointer get_shared_ptr() const
This is exactly the same as operator-> but it&#39;s slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:218
static map_location::DIRECTION s
int get_location_y(const map_location &loc) const
Definition: display.cpp:755
std::shared_ptr< suppose_dead > suppose_dead_ptr
Definition: typedefs.hpp:76
virtual ~suppose_dead()
config & add_child(config_key_type key)
Definition: config.cpp:514
map_location loc_
virtual error check_validity() const
Check the validity of the action.
void drawing_buffer_add(const drawing_layer layer, const map_location &loc, int x, int y, const surface &surf, const SDL_Rect &clip=SDL_Rect())
Add an item to the drawing buffer.
Definition: display.cpp:1209
int get_location_x(const map_location &loc) const
Functions to get the on-screen positions of hexes.
Definition: display.cpp:750
Image rescaled to fit into a hexagonal tile according to the zoom settings.
Definition: picture.hpp:238
bool hidden() const
Definition: action.hpp:64
virtual std::ostream & print(std::ostream &s) const
std::size_t unit_underlying_id_
#define DBG_WB
Definition: typedefs.hpp:28
unit_ptr extract(const map_location &loc)
Extracts a unit from the map.
Definition: map.cpp:267
Container associating units to locations.
Definition: map.hpp:98
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:34
virtual config to_config() const
Constructs and returns a config object representing this object.
virtual void draw_hex(const map_location &hex)
Gets called by display when drawing a hex, to allow actions to draw to the screen.
visitor is an abstract interface : action.accept(visitor) calls visitor.visit(action) ...
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:33
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
bool valid() const
Definition: map.hpp:274
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:51
Display units performing various actions: moving, attacking, and dying.
Definition: display.hpp:49
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses...
Definition: visitor.hpp:32