The Battle for Wesnoth  1.17.17+dev
suppose_dead.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2023
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 "draw.hpp"
31 #include "game_end_exceptions.hpp"
32 #include "mouse_events.hpp"
33 #include "play_controller.hpp"
34 #include "replay.hpp"
35 #include "resources.hpp"
36 #include "units/unit.hpp"
37 #include "units/udisplay.hpp"
38 #include "units/map.hpp"
39 
40 namespace wb
41 {
42 
43 std::ostream& operator<<(std::ostream &s, suppose_dead_ptr sup_d)
44 {
45  assert(sup_d);
46  return sup_d->print(s);
47 }
48 
49 std::ostream& operator<<(std::ostream &s, suppose_dead_const_ptr sup_d)
50 {
51  assert(sup_d);
52  return sup_d->print(s);
53 }
54 
55 std::ostream& suppose_dead::print(std::ostream &s) const
56 {
57  s << "Suppose-dead for unit " << get_unit()->name() << " [" << get_unit()->id() << "] "
58  << "at (" << loc_ << ")";
59  return s;
60 }
61 
62 suppose_dead::suppose_dead(std::size_t team_index, bool hidden, unit& curr_unit, const map_location& loc)
63  : action(team_index,hidden)
64  , unit_underlying_id_(curr_unit.underlying_id())
65  , unit_id_(curr_unit.id())
66  , loc_(loc)
67 {
68  this->init();
69 }
70 
71 suppose_dead::suppose_dead(const config& cfg, bool hidden)
72  : action(cfg,hidden)
73  , unit_underlying_id_(0)
74  , unit_id_()
75  , loc_(cfg.mandatory_child("loc_")["x"],cfg.mandatory_child("loc_")["y"], wml_loc())
76 {
77  // Construct and validate unit_
78  unit_map::iterator unit_itor = resources::gameboard->units().find(cfg["unit_"]);
79  if(unit_itor == resources::gameboard->units().end())
80  throw action::ctor_err("suppose_dead: Invalid underlying_id");
81 
82  unit_underlying_id_ = unit_itor->underlying_id();
83  unit_id_ = unit_itor->id();
84 
85  this->init();
86 }
87 
89 {
91 }
92 
94 {
95  //invalidate hex so that skull indicator is properly cleared
98 }
99 
101 {
103  if (itor.valid())
104  return itor.get_shared_ptr();
105  else
106  return unit_ptr();
107 }
108 
110 {
111  v.visit(shared_from_this());
112 }
113 
114 void suppose_dead::execute(bool& success, bool& complete)
115  {success = false; complete = true;}
116 
118 {
119  // Remove the unit
120  const unit_const_ptr removed_unit = unit_map.extract(loc_);
121  DBG_WB << "Suppose dead: Temporarily removing unit " << removed_unit->name() << " [" << removed_unit->id()
122  << "] from (" << loc_ << ")";
123 
124  // Just check to make sure we removed the unit we expected to remove
125  assert(get_unit().get() == removed_unit.get());
126 }
127 
129 {
130  // Just check to make sure the hex is empty
132  assert(unit_it == resources::gameboard->units().end());
133 
134  // Restore the unit
136 }
137 
139 {
140  if (hex != loc_) {
141  return;
142  }
143 
144  //@todo: Possibly use a different layer
146 
148  layer, loc_, [tex = image::get_texture(image::locator{"whiteboard/suppose_dead.png"}, image::HEXED)](const rect& d) {
149  draw::blit(tex, d);
150  });
151 }
152 
154 {
156 }
157 
159 {
160  if(!get_source_hex().valid()) {
161  return INVALID_LOCATION;
162  }
163  //Check that the unit still exists in the source hex
165  if(unit_it == resources::gameboard->units().end()) {
166  return NO_UNIT;
167  }
168  //check if the unit in the source hex has the same unit id as before,
169  //i.e. that it's the same unit
170  if(unit_id_ != unit_it->id()) {
171  return UNIT_CHANGED;
172  }
173 
174  return OK;
175 }
176 
178 {
179  config final_cfg = action::to_config();
180 
181  final_cfg["type"]="suppose_dead";
182  final_cfg["unit_"]=static_cast<int>(unit_underlying_id_);
183  final_cfg["unit_id_"]=unit_id_;
184 
185  config loc_cfg;
186  loc_cfg["x"]=loc_.wml_x();
187  loc_cfg["y"]=loc_.wml_y();
188  final_cfg.add_child("loc_", std::move(loc_cfg));
189 
190  return final_cfg;
191 }
192 
193 } // end namespace wb
Arrows destined to be drawn on the map.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
config & add_child(config_key_type key)
Definition: config.cpp:445
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3122
drawing_layer
The layers to render something on.
Definition: display.hpp:803
@ LAYER_ARROWS
Arrows from the arrows framework.
Definition: display.hpp:833
void drawing_buffer_add(const drawing_layer layer, const map_location &loc, decltype(draw_helper::do_draw) draw_func)
Add an item to the drawing buffer.
Definition: display.cpp:1259
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:101
virtual const unit_map & units() const override
Definition: game_board.hpp:113
Generic locator abstracting the location of an image.
Definition: picture.hpp:64
Container associating units to locations.
Definition: map.hpp:99
unit_ptr extract(const map_location &loc)
Extracts a unit from the map.
Definition: map.cpp:258
unit_iterator find(std::size_t id)
Definition: map.cpp:301
umap_retval_pair_t insert(unit_ptr p)
Inserts the unit pointed to by p into the map.
Definition: map.cpp:134
This class represents a single unit of a specific type.
Definition: unit.hpp:134
Abstract base class for all the whiteboard planned actions.
Definition: action.hpp:34
bool valid()
Returns whether this action is valid or not.
Definition: action.hpp:135
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:51
error
Possible errors.
Definition: action.hpp:107
@ INVALID_LOCATION
Definition: action.hpp:109
@ UNIT_CHANGED
Definition: action.hpp:111
virtual void accept(visitor &v)
std::size_t unit_underlying_id_
virtual unit_ptr get_unit() const
Return the unit targeted by this action.
virtual void execute(bool &success, bool &complete)
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
virtual error check_validity() const
Check the validity of the action.
virtual std::ostream & print(std::ostream &s) const
virtual void apply_temp_modifier(unit_map &unit_map)
Applies temporarily the result of this action to the specified unit map.
suppose_dead(std::size_t team_index, bool hidden, unit &curr_unit, const map_location &loc)
virtual void remove_temp_modifier(unit_map &unit_map)
Removes the result of this action from the specified unit map.
std::string unit_id_
std::shared_ptr< suppose_dead > shared_from_this()
virtual void redraw()
Redrawing function, called each time the action situation might have changed.
virtual ~suppose_dead()
virtual void draw_hex(const map_location &hex)
Gets called by display when drawing a hex, to allow actions to draw to the screen.
virtual map_location get_source_hex() const
Return the location at which this action was planned.
map_location loc_
virtual config to_config() const
Constructs and returns a config object representing this object.
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses.
Definition: visitor.hpp:33
virtual void visit(move_ptr move)=0
Drawing functions, for drawing things on the screen.
map_location loc_
Contains the exception interfaces used to signal completion of a scenario, campaign or turn.
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
CURSOR_TYPE get()
Definition: cursor.cpp:216
void blit(const texture &tex, const SDL_Rect &dst)
Draws a texture, or part of a texture, at the given location.
Definition: draw.cpp:301
@ HEXED
Standard hexagonal tile mask applied, removing portions that don't fit.
Definition: picture.hpp:235
texture get_texture(const image::locator &i_locator, TYPE type, bool skip_cache)
Returns an image texture suitable for hardware-accelerated rendering.
Definition: picture.cpp:985
game_board * gameboard
Definition: resources.cpp:21
Definition: display.hpp:49
std::shared_ptr< suppose_dead > suppose_dead_ptr
Definition: typedefs.hpp:76
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:34
std::shared_ptr< suppose_dead const > suppose_dead_const_ptr
Definition: typedefs.hpp:77
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
std::shared_ptr< unit > unit_ptr
Definition: ptr.hpp:26
Replay control code.
Encapsulates the map of the game.
Definition: location.hpp:38
int wml_y() const
Definition: location.hpp:154
int wml_x() const
Definition: location.hpp:153
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:47
bool valid() const
Definition: map.hpp:274
pointer get_shared_ptr() const
This is exactly the same as operator-> but it's slightly more readable, and can replace &*iter syntax...
Definition: map.hpp:218
static map_location::DIRECTION s
#define DBG_WB
Definition: typedefs.hpp:28
Display units performing various actions: moving, attacking, and dying.
visitor is an abstract interface : action.accept(visitor) calls visitor.visit(action)
#define d