The Battle for Wesnoth  1.19.0+dev
suppose_dead.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2024
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 
24 #include "config.hpp"
25 #include "display.hpp"
26 #include "draw.hpp"
27 #include "play_controller.hpp"
28 #include "resources.hpp"
29 #include "units/unit.hpp"
30 #include "units/map.hpp"
31 
32 namespace wb
33 {
34 
35 std::ostream& operator<<(std::ostream &s, suppose_dead_ptr sup_d)
36 {
37  assert(sup_d);
38  return sup_d->print(s);
39 }
40 
41 std::ostream& operator<<(std::ostream &s, suppose_dead_const_ptr sup_d)
42 {
43  assert(sup_d);
44  return sup_d->print(s);
45 }
46 
47 std::ostream& suppose_dead::print(std::ostream &s) const
48 {
49  s << "Suppose-dead for unit " << get_unit()->name() << " [" << get_unit()->id() << "] "
50  << "at (" << loc_ << ")";
51  return s;
52 }
53 
54 suppose_dead::suppose_dead(std::size_t team_index, bool hidden, unit& curr_unit, const map_location& loc)
55  : action(team_index,hidden)
56  , unit_underlying_id_(curr_unit.underlying_id())
57  , unit_id_(curr_unit.id())
58  , loc_(loc)
59 {
60  this->init();
61 }
62 
63 suppose_dead::suppose_dead(const config& cfg, bool hidden)
64  : action(cfg,hidden)
65  , unit_underlying_id_(0)
66  , unit_id_()
67  , loc_(cfg.mandatory_child("loc_")["x"],cfg.mandatory_child("loc_")["y"], wml_loc())
68 {
69  // Construct and validate unit_
70  unit_map::iterator unit_itor = resources::gameboard->units().find(cfg["unit_"]);
71  if(unit_itor == resources::gameboard->units().end())
72  throw action::ctor_err("suppose_dead: Invalid underlying_id");
73 
74  unit_underlying_id_ = unit_itor->underlying_id();
75  unit_id_ = unit_itor->id();
76 
77  this->init();
78 }
79 
81 {
83 }
84 
86 {
87  //invalidate hex so that skull indicator is properly cleared
90 }
91 
93 {
95  if (itor.valid())
96  return itor.get_shared_ptr();
97  else
98  return unit_ptr();
99 }
100 
102 {
103  v.visit(shared_from_this());
104 }
105 
106 void suppose_dead::execute(bool& success, bool& complete)
107  {success = false; complete = true;}
108 
110 {
111  // Remove the unit
112  const unit_const_ptr removed_unit = unit_map.extract(loc_);
113  DBG_WB << "Suppose dead: Temporarily removing unit " << removed_unit->name() << " [" << removed_unit->id()
114  << "] from (" << loc_ << ")";
115 
116  // Just check to make sure we removed the unit we expected to remove
117  assert(get_unit().get() == removed_unit.get());
118 }
119 
121 {
122  // Just check to make sure the hex is empty
124  assert(unit_it == resources::gameboard->units().end());
125 
126  // Restore the unit
128 }
129 
131 {
132  if (hex != loc_) {
133  return;
134  }
135 
136  //@todo: Possibly use a different layer
138 
140  layer, loc_, [tex = image::get_texture(image::locator{"whiteboard/suppose_dead.png"}, image::HEXED)](const rect& d) {
141  draw::blit(tex, d);
142  });
143 }
144 
146 {
148 }
149 
151 {
152  if(!get_source_hex().valid()) {
153  return INVALID_LOCATION;
154  }
155  //Check that the unit still exists in the source hex
157  if(unit_it == resources::gameboard->units().end()) {
158  return NO_UNIT;
159  }
160  //check if the unit in the source hex has the same unit id as before,
161  //i.e. that it's the same unit
162  if(unit_id_ != unit_it->id()) {
163  return UNIT_CHANGED;
164  }
165 
166  return OK;
167 }
168 
170 {
171  config final_cfg = action::to_config();
172 
173  final_cfg["type"]="suppose_dead";
174  final_cfg["unit_"]=static_cast<int>(unit_underlying_id_);
175  final_cfg["unit_id_"]=unit_id_;
176 
177  config loc_cfg;
178  loc_cfg["x"]=loc_.wml_x();
179  loc_cfg["y"]=loc_.wml_y();
180  final_cfg.add_child("loc_", std::move(loc_cfg));
181 
182  return final_cfg;
183 }
184 
185 } // end namespace wb
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
config & add_child(config_key_type key)
Definition: config.cpp:441
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3145
drawing_layer
The layers to render something on.
Definition: display.hpp:820
@ LAYER_ARROWS
Arrows from the arrows framework.
Definition: display.hpp:850
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:1287
static display * get_singleton()
Returns the display object if a display object exists.
Definition: display.hpp:102
virtual const unit_map & units() const override
Definition: game_board.hpp:106
Generic locator abstracting the location of an image.
Definition: picture.hpp:63
Container associating units to locations.
Definition: map.hpp:98
unit_ptr extract(const map_location &loc)
Extracts a unit from the map.
Definition: map.cpp:259
unit_iterator find(std::size_t id)
Definition: map.cpp:302
umap_retval_pair_t insert(unit_ptr p)
Inserts the unit pointed to by p into the map.
Definition: map.cpp:135
This class represents a single unit of a specific type.
Definition: unit.hpp:133
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
map_display and display: classes which take care of displaying the map and game-data on the screen.
Drawing functions, for drawing things on the screen.
map_location loc_
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:205
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:310
@ HEXED
Standard hexagonal tile mask applied, removing portions that don't fit.
Definition: picture.hpp:228
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:960
game_board * gameboard
Definition: resources.cpp:20
Definition: display.hpp:45
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
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:273
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:217
static map_location::DIRECTION s
#define DBG_WB
Definition: typedefs.hpp:28
visitor is an abstract interface : action.accept(visitor) calls visitor.visit(action)
#define d