whiteboard/suppose_dead.cpp

Go to the documentation of this file.
00001 /* $Id: suppose_dead.cpp 52869 2012-02-03 20:18:33Z shadowmaster $ */
00002 /*
00003  Copyright (C) 2011 - 2012 by Tommy Schmitz
00004  Part of the Battle for Wesnoth Project http://www.wesnoth.org
00005 
00006  This program is free software; you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010  This program is distributed in the hope that it will be useful,
00011  but WITHOUT ANY WARRANTY.
00012 
00013  See the COPYING file for more details.
00014  */
00015 
00016 /**
00017  * @file
00018  */
00019 
00020 #include "suppose_dead.hpp"
00021 
00022 #include "visitor.hpp"
00023 #include "manager.hpp"
00024 #include "side_actions.hpp"
00025 #include "utility.hpp"
00026 
00027 #include "arrow.hpp"
00028 #include "config.hpp"
00029 #include "foreach.hpp"
00030 #include "game_display.hpp"
00031 #include "game_end_exceptions.hpp"
00032 #include "mouse_events.hpp"
00033 #include "play_controller.hpp"
00034 #include "replay.hpp"
00035 #include "resources.hpp"
00036 #include "team.hpp"
00037 #include "unit.hpp"
00038 #include "unit_display.hpp"
00039 #include "unit_map.hpp"
00040 
00041 namespace wb
00042 {
00043 
00044     std::ostream& operator<<(std::ostream &s, suppose_dead_ptr sup_d)
00045     {
00046         assert(sup_d);
00047         return sup_d->print(s);
00048     }
00049 
00050     std::ostream& operator<<(std::ostream &s, suppose_dead_const_ptr sup_d)
00051     {
00052         assert(sup_d);
00053         return sup_d->print(s);
00054     }
00055 
00056     std::ostream& suppose_dead::print(std::ostream &s) const
00057     {
00058         s << "Suppose-dead for unit " << get_unit()->name() << " [" << get_unit()->id() << "] "
00059                 << "at (" << loc_ << ")";
00060         return s;
00061     }
00062 
00063     suppose_dead::suppose_dead(size_t team_index, bool hidden, unit& curr_unit, map_location const& loc)
00064     : action(team_index,hidden)
00065     , unit_underlying_id_(curr_unit.underlying_id())
00066     , unit_id_(curr_unit.id())
00067     , loc_(loc)
00068     , valid_(true)
00069     {
00070         this->init();
00071     }
00072 
00073     suppose_dead::suppose_dead(config const& cfg, bool hidden)
00074     : action(cfg,hidden)
00075     , unit_underlying_id_(0)
00076     , unit_id_()
00077     , loc_(cfg.child("loc_")["x"],cfg.child("loc_")["y"])
00078     , valid_(true)
00079     {
00080         // Construct and validate unit_
00081         unit_map::iterator unit_itor = resources::units->find(cfg["unit_"]);
00082         if(unit_itor == resources::units->end())
00083             throw action::ctor_err("suppose_dead: Invalid underlying_id");
00084 
00085         unit_underlying_id_ = unit_itor->underlying_id();
00086         unit_id_ = unit_itor->id();
00087 
00088         this->init();
00089     }
00090 
00091     void suppose_dead::init()
00092     {
00093         resources::screen->invalidate(loc_);
00094     }
00095 
00096     suppose_dead::~suppose_dead()
00097     {
00098         //invalidate hex so that skull indicator is properly cleared
00099         if(resources::screen)
00100             resources::screen->invalidate(loc_);
00101     }
00102 
00103     unit* suppose_dead::get_unit() const
00104     {
00105         unit_map::iterator itor = resources::units->find(unit_underlying_id_);
00106         if (itor.valid())
00107             return &*itor;
00108         else
00109             return NULL;
00110     }
00111 
00112     void suppose_dead::accept(visitor& v)
00113     {
00114         v.visit(shared_from_this());
00115     }
00116 
00117     void suppose_dead::execute(bool& success, bool& complete)
00118         {success = false;   complete = true;}
00119 
00120     void suppose_dead::apply_temp_modifier(unit_map& unit_map)
00121     {
00122         // Remove the unit
00123         unit const* removed_unit = unit_map.extract(loc_);
00124         DBG_WB << "Suppose dead: Temporarily removing unit " << removed_unit->name() << " [" << removed_unit->id()
00125                 << "] from (" << loc_ << ")\n";
00126 
00127         // Just check to make sure we removed the unit we expected to remove
00128         assert(get_unit() == removed_unit);
00129     }
00130 
00131     void suppose_dead::remove_temp_modifier(unit_map& unit_map)
00132     {
00133         // Just check to make sure the hex is empty
00134         unit_map::iterator unit_it = resources::units->find(loc_);
00135         assert(unit_it == resources::units->end());
00136 
00137         // Restore the unit
00138         unit_map.insert(get_unit());
00139     }
00140 
00141     void suppose_dead::draw_hex(const map_location& hex)
00142     {
00143         if(hex == loc_) //add symbol to hex
00144         {
00145             //@todo: Possibly use a different layer
00146             const display::tdrawing_layer layer = display::LAYER_ARROWS;
00147 
00148             int xpos = resources::screen->get_location_x(loc_);
00149             int ypos = resources::screen->get_location_y(loc_);
00150 
00151             resources::screen->drawing_buffer_add(layer, loc_, xpos, ypos,
00152                     image::get_image("whiteboard/suppose_dead.png", image::SCALED_TO_HEX));
00153         }
00154     }
00155 
00156     void suppose_dead::set_valid(bool valid)
00157     {
00158         valid_ = valid;
00159     }
00160 
00161     config suppose_dead::to_config() const
00162     {
00163         config final_cfg = action::to_config();
00164 
00165         final_cfg["type"]="suppose_dead";
00166         final_cfg["unit_"]=static_cast<int>(unit_underlying_id_);
00167         final_cfg["unit_id_"]=unit_id_;
00168 
00169         config loc_cfg;
00170         loc_cfg["x"]=loc_.x;
00171         loc_cfg["y"]=loc_.y;
00172         final_cfg.add_child("loc_",loc_cfg);
00173 
00174         return final_cfg;
00175     }
00176 
00177 } // end namespace wb
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:15 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs