whiteboard/action.cpp

Go to the documentation of this file.
00001 /* $Id: action.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003  Copyright (C) 2010 - 2012 by Gabriel Morin <gabrielmorin (at) gmail (dot) com>
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 "action.hpp"
00021 #include "move.hpp"
00022 #include "attack.hpp"
00023 #include "recruit.hpp"
00024 #include "recall.hpp"
00025 #include "suppose_dead.hpp"
00026 
00027 #include "resources.hpp"
00028 #include "team.hpp"
00029 
00030 namespace wb {
00031 
00032 std::ostream& operator<<(std::ostream& s, action_ptr action)
00033 {
00034     assert(action);
00035     return action->print(s);
00036 }
00037 
00038 std::ostream& operator<<(std::ostream& s, action_const_ptr action)
00039 {
00040     assert(action);
00041     return action->print(s);
00042 }
00043 
00044 std::ostream& action::print(std::ostream& s) const
00045 {
00046     return s;
00047 }
00048 
00049 config action::to_config() const
00050 {
00051     config final_cfg;
00052     final_cfg["type"]="action";
00053     final_cfg["team_index_"]=static_cast<int>(team_index_);
00054     return final_cfg;
00055 }
00056 
00057 /* static */
00058 action_ptr action::from_config(config const& cfg, bool hidden)
00059 {
00060     std::string type = cfg["type"];
00061 
00062     try {
00063         if(type=="move")
00064             return action_ptr(new move(cfg,hidden));
00065         else if(type=="attack")
00066             return action_ptr(new attack(cfg,hidden));
00067         else if(type=="recruit")
00068             return action_ptr(new recruit(cfg,hidden));
00069         else if(type=="recall")
00070             return action_ptr(new recall(cfg,hidden));
00071         else if(type=="suppose_dead")
00072             return action_ptr(new suppose_dead(cfg,hidden));
00073     } catch(action::ctor_err const&) {}
00074 
00075     return action_ptr();
00076 }
00077 
00078 void action::hide()
00079 {
00080     if(hidden_)
00081         return;
00082     hidden_ = true;
00083     do_hide();
00084 }
00085 
00086 void action::show()
00087 {
00088     if(!hidden_)
00089         return;
00090     hidden_ = false;
00091     do_show();
00092 }
00093 
00094 action::action(size_t team_index, bool hidden)
00095     : team_index_(team_index)
00096     , hidden_(hidden)
00097 {
00098 }
00099 
00100 action::action(config const& cfg, bool hidden)
00101     : team_index_()
00102     , hidden_(hidden)
00103 {
00104     // Construct and validate team_index_
00105     int team_index_temp = cfg["team_index_"].to_int(-1); //default value: -1
00106     if(team_index_temp < 0
00107             || team_index_temp >= static_cast<int>(resources::teams->size()))
00108         throw ctor_err("action: Invalid team_index_");
00109     team_index_ = team_index_temp;
00110 }
00111 
00112 action::~action()
00113 {
00114 }
00115 
00116 } // 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:02:48 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs