The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
suppose_dead.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Tommy Schmitz
3  Part of the Battle for Wesnoth Project http://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 
19 #include "suppose_dead.hpp"
20 
21 #include "visitor.hpp"
22 #include "manager.hpp"
23 #include "side_actions.hpp"
24 #include "utility.hpp"
25 
26 #include "arrow.hpp"
27 #include "config.hpp"
28 #include "game_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(size_t team_index, bool hidden, unit& curr_unit, map_location const& 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 
69 suppose_dead::suppose_dead(config const& cfg, bool hidden)
70  : action(cfg,hidden)
71  , unit_underlying_id_(0)
72  , unit_id_()
73  , loc_(cfg.child("loc_")["x"],cfg.child("loc_")["y"])
74 {
75  // Construct and validate unit_
76  unit_map::iterator unit_itor = resources::units->find(cfg["unit_"]);
77  if(unit_itor == resources::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::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 #ifdef SDL_GPU
146  resources::screen->drawing_buffer_add(layer, loc_, xpos, ypos,
147  image::get_texture("whiteboard/suppose_dead.png", image::SCALED_TO_HEX));
148 #else
149  resources::screen->drawing_buffer_add(layer, loc_, xpos, ypos,
150  image::get_image("whiteboard/suppose_dead.png", image::SCALED_TO_HEX));
151 #endif
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::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_.x;
189  loc_cfg["y"]=loc_.y;
190  final_cfg.add_child("loc_",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.
void drawing_buffer_add(const tdrawing_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:1241
surface get_image(const image::locator &i_locator, TYPE type)
function to get the surface corresponding to an image.
Definition: image.cpp:878
Arrows destined to be drawn on the map.
virtual unit_ptr get_unit() const
Return the unit targeted by this action.
bool invalidate(const map_location &loc)
Function to invalidate a specific tile for redrawing.
Definition: display.cpp:3536
suppose_dead(size_t team_index, bool hidden, unit &curr_unit, map_location const &loc)
virtual map_location get_source_hex() const
Return the location at which this action was planned.
Definition: unit.hpp:95
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.
size_t unit_underlying_id_
virtual error check_validity() const
Check the validity of the action.
game_display * screen
Definition: resources.cpp:27
Arrows from the arrows framework.
Definition: display.hpp:894
virtual void visit(move_ptr move)=0
virtual std::ostream & print(std::ostream &s) const
Replay control code.
int get_location_x(const map_location &loc) const
Functions to get the on-screen positions of hexes.
Definition: display.cpp:713
Contains the exception interfaces used to signal completion of a scenario, campaign or turn...
Definitions for the interface to Wesnoth Markup Language (WML).
std::string unit_id_
virtual void accept(visitor &v)
int get_location_y(const map_location &loc) const
Definition: display.cpp:718
map_location loc_
tdrawing_layer
The layers to render something on.
Definition: display.hpp:864
bool valid()
Returns whether this action is valid or not.
Definition: action.hpp:133
GLuint GLuint end
Definition: glew.h:1221
GLuint id
Definition: glew.h:1647
pointer get_shared_ptr() const
Definition: map.hpp:180
GLenum GLenum GLuint GLint GLint layer
Definition: glew.h:3455
std::pair< unit_iterator, bool > insert(unit_ptr p)
Adds the unit to the map.
Definition: map.cpp:126
error
Possible errors.
Definition: action.hpp:104
const GLdouble * v
Definition: glew.h:1359
config & add_child(const std::string &key)
Definition: config.cpp:743
virtual void execute(bool &success, bool &complete)
Output parameters: success: Whether or not to continue an execute-all after this execution complete: ...
virtual config to_config() const
Constructs and returns a config object representing this object.
Encapsulates the map of the game.
Definition: location.hpp:38
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
boost::shared_ptr< suppose_dead > shared_from_this()
virtual ~suppose_dead()
map_location loc_
boost::intrusive_ptr< unit > unit_ptr
Definition: ptr.hpp:29
virtual config to_config() const
Constructs and returns a config object representing this object.
Definition: action.cpp:50
#define DBG_WB
Definition: typedefs.hpp:29
unit_ptr extract(const map_location &loc)
Extracts a unit from the map.
Definition: map.cpp:249
Container associating units to locations.
Definition: map.hpp:90
std::ostream & operator<<(std::ostream &s, action_ptr action)
Definition: action.cpp:33
unit_iterator find(size_t id)
Definition: map.cpp:285
bool valid() const
Definition: map.hpp:229
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:83
GLdouble s
Definition: glew.h:1358
unit_map * units
Definition: resources.cpp:35
Display units performing various actions: moving, attacking, and dying.
Definition: display.hpp:47
Abstract base class for all the visitors (cf GoF Visitor Design Pattern) the whiteboard uses...
Definition: visitor.hpp:32