The Battle for Wesnoth  1.15.1+dev
suppose_dead.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2018 by Tommy Schmitz
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13  */
14 
15 /**
16  * @file
17  */
18 
20 
21 #include "whiteboard/visitor.hpp"
22 #include "whiteboard/manager.hpp"
24 #include "whiteboard/utility.hpp"
25 
26 #include "arrow.hpp"
27 #include "config.hpp"
28 #include "display.hpp"
29 #include "game_end_exceptions.hpp"
30 #include "mouse_events.hpp"
31 #include "play_controller.hpp"
32 #include "replay.hpp"
33 #include "resources.hpp"
34 #include "units/unit.hpp"
35 #include "units/udisplay.hpp"
36 #include "units/map.hpp"
37 
38 namespace wb
39 {
40 
41 std::ostream& operator<<(std::ostream &s, suppose_dead_ptr sup_d)
42 {
43  assert(sup_d);
44  return sup_d->print(s);
45 }
46 
47 std::ostream& operator<<(std::ostream &s, suppose_dead_const_ptr sup_d)
48 {
49  assert(sup_d);
50  return sup_d->print(s);
51 }
52 
53 std::ostream& suppose_dead::print(std::ostream &s) const
54 {
55  s << "Suppose-dead for unit " << get_unit()->name() << " [" << get_unit()->id() << "] "
56  << "at (" << loc_ << ")";
57  return s;
58 }
59 
60 suppose_dead::suppose_dead(std::size_t team_index, bool hidden, unit& curr_unit, const map_location& loc)
61  : action(team_index,hidden)
62  , unit_underlying_id_(curr_unit.underlying_id())
63  , unit_id_(curr_unit.id())
64  , loc_(loc)
65 {
66  this->init();
67 }
68 
70  : action(cfg,hidden)
72  , unit_id_()
73  , loc_(cfg.child("loc_")["x"],cfg.child("loc_")["y"], wml_loc())
74 {
75  // Construct and validate unit_
76  unit_map::iterator unit_itor = resources::gameboard->units().find(cfg["unit_"]);
77  if(unit_itor == resources::gameboard->units().end())
78  throw action::ctor_err("suppose_dead: Invalid underlying_id");
79 
80  unit_underlying_id_ = unit_itor->underlying_id();
81  unit_id_ = unit_itor->id();
82 
83  this->init();
84 }
85 
87 {
89 }
90 
92 {
93  //invalidate hex so that skull indicator is properly cleared
96 }
97 
99 {
101  if (itor.valid())
102  return itor.get_shared_ptr();
103  else
104  return unit_ptr();
105 }
106 
108 {
109  v.visit(shared_from_this());
110 }
111 
112 void suppose_dead::execute(bool& success, bool& complete)
113  {success = false; complete = true;}
114 
116 {
117  // Remove the unit
118  const unit_const_ptr removed_unit = unit_map.extract(loc_);
119  DBG_WB << "Suppose dead: Temporarily removing unit " << removed_unit->name() << " [" << removed_unit->id()
120  << "] from (" << loc_ << ")\n";
121 
122  // Just check to make sure we removed the unit we expected to remove
123  assert(get_unit().get() == removed_unit.get());
124 }
125 
127 {
128  // Just check to make sure the hex is empty
130  assert(unit_it == resources::gameboard->units().end());
131 
132  // Restore the unit
133  unit_map.insert(get_unit());
134 }
135 
137 {
138  if(hex == loc_) //add symbol to hex
139  {
140  //@todo: Possibly use a different layer
142 
145  display::get_singleton()->drawing_buffer_add(layer, loc_, xpos, ypos,
146  image::get_image("whiteboard/suppose_dead.png", image::SCALED_TO_HEX));
147  }
148 }
149 
151 {
153 }
154 
156 {
157  if(!get_source_hex().valid()) {
158  return INVALID_LOCATION;
159  }
160  //Check that the unit still exists in the source hex
162  if(unit_it == resources::gameboard->units().end()) {
163  return NO_UNIT;
164  }
165  //check if the unit in the source hex has the same unit id as before,
166  //i.e. that it's the same unit
167  if(unit_id_ != unit_it->id()) {
168  return UNIT_CHANGED;
169  }
170 
171  return OK;
172 }
173 
175 {
176  config final_cfg = action::to_config();
177 
178  final_cfg["type"]="suppose_dead";
179  final_cfg["unit_"]=static_cast<int>(unit_underlying_id_);
180  final_cfg["unit_id_"]=unit_id_;
181 
182  config loc_cfg;
183  loc_cfg["x"]=loc_.wml_x();
184  loc_cfg["y"]=loc_.wml_y();
185  final_cfg.add_child("loc_", std::move(loc_cfg));
186 
187  return final_cfg;
188 }
189 
190 } // end namespace wb
virtual void apply_temp_modifier(unit_map &unit_map)
Applies temporarily the result of this action to the specified unit map.
boost::intrusive_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:30
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: picture.cpp:875
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:88
virtual unit_ptr get_unit() const
Return the unit targeted by this action.
virtual const unit_map & units() const override
Definition: game_board.hpp:114
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:2975
This class represents a single unit of a specific type.
Definition: unit.hpp:99
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:135
Arrows from the arrows framework.
Definition: display.hpp:845
int wml_x() const
Definition: location.hpp:157
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:815
Definitions for the interface to Wesnoth Markup Language (WML).
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:83
bool valid()
Returns whether this action is valid or not.
Definition: action.hpp:134
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:76
error
Possible errors.
Definition: action.hpp:105
int wml_y() const
Definition: location.hpp:158
game_board * gameboard
Definition: resources.cpp:20
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:42
unit_iterator find(std::size_t id)
Definition: map.cpp:311
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:220
static map_location::DIRECTION s
int get_location_y(const map_location &loc) const
Definition: display.cpp:726
std::shared_ptr< suppose_dead > suppose_dead_ptr
Definition: typedefs.hpp:75
virtual ~suppose_dead()
config & add_child(config_key_type key)
Definition: config.cpp:476
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:1177
int get_location_x(const map_location &loc) const
Functions to get the on-screen positions of hexes.
Definition: display.cpp:721
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
bool hidden() const
Definition: action.hpp:63
virtual std::ostream & print(std::ostream &s) const
std::size_t unit_underlying_id_
#define DBG_WB
Definition: typedefs.hpp:27
unit_ptr extract(const map_location &loc)
Extracts a unit from the map.
Definition: map.cpp:268
Container associating units to locations.
Definition: map.hpp:99
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:33
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:32
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
bool valid() const
Definition: map.hpp:276
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:50
Display units performing various actions: moving, attacking, and dying.
Definition: display.hpp:48
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses...
Definition: visitor.hpp:31