The Battle for Wesnoth  1.17.10+dev
suppose_dead.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2022
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 
72  : action(cfg,hidden)
74  , unit_id_()
75  , loc_(cfg.child("loc_")["x"],cfg.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
135  unit_map.insert(get_unit());
136 }
137 
139 {
140  if (hex != loc_) {
141  return;
142  }
143 
144  //@todo: Possibly use a different layer
146 
147  auto disp = display::get_singleton();
148 
149  disp->drawing_buffer_add(
150  layer, loc_, [=, tex = image::get_texture("whiteboard/suppose_dead.png", image::HEXED)](const rect& d) {
151  draw::blit(tex, disp->scaled_to_zoom({d.x, d.y, tex.w(), tex.h()}));
152  });
153 }
154 
156 {
158 }
159 
161 {
162  if(!get_source_hex().valid()) {
163  return INVALID_LOCATION;
164  }
165  //Check that the unit still exists in the source hex
167  if(unit_it == resources::gameboard->units().end()) {
168  return NO_UNIT;
169  }
170  //check if the unit in the source hex has the same unit id as before,
171  //i.e. that it's the same unit
172  if(unit_id_ != unit_it->id()) {
173  return UNIT_CHANGED;
174  }
175 
176  return OK;
177 }
178 
180 {
181  config final_cfg = action::to_config();
182 
183  final_cfg["type"]="suppose_dead";
184  final_cfg["unit_"]=static_cast<int>(unit_underlying_id_);
185  final_cfg["unit_id_"]=unit_id_;
186 
187  config loc_cfg;
188  loc_cfg["x"]=loc_.wml_x();
189  loc_cfg["y"]=loc_.wml_y();
190  final_cfg.add_child("loc_", std::move(loc_cfg));
191 
192  return final_cfg;
193 }
194 
195 } // end namespace wb
virtual void apply_temp_modifier(unit_map &unit_map)
Applies temporarily the result of this action to the specified unit map.
Drawing functions, for drawing things on the screen.
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:98
virtual unit_ptr get_unit() const
Return the unit targeted by this action.
virtual const unit_map & units() const override
Definition: game_board.hpp:113
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3141
This class represents a single unit of a specific type.
Definition: unit.hpp:133
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:822
int wml_x() const
Definition: location.hpp:153
virtual void visit(move_ptr move)=0
Replay control code.
#define d
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
std::shared_ptr< suppose_dead > shared_from_this()
Standard hexagonal tile mask applied, removing portions that don&#39;t fit.
Definition: picture.hpp:233
drawing_layer
The layers to render something on.
Definition: display.hpp:792
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:301
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
std::shared_ptr< suppose_dead > suppose_dead_ptr
Definition: typedefs.hpp:76
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:46
virtual ~suppose_dead()
config & add_child(config_key_type key)
Definition: config.cpp:514
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
map_location loc_
virtual error check_validity() const
Check the validity of the action.
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:258
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:60
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:1128
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