TankType.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D 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 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <tank/TankType.h>
00022 #include <XML/XMLParser.h>
00023 #include <common/Defines.h>
00024 #include <engine/ScorchedContext.h>
00025 #include <weapons/AccessoryStore.h>
00026 
00027 TankType::TankType()
00028 {
00029 }
00030 
00031 TankType::~TankType()
00032 {
00033 }
00034 
00035 bool TankType::initFromXML(ScorchedContext &context, XMLNode *node)
00036 {
00037         if (!node->getNamedChild("name", name_)) return false;
00038         if (!node->getNamedChild("life", life_)) return false;
00039         if (!node->getNamedChild("power", power_)) return false;
00040 
00041         XMLNode *accessoryNode = 0;
00042         while (node->getNamedChild("accessory", accessoryNode, false))
00043         {
00044                 std::string name;
00045                 int count;
00046                 if (!accessoryNode->getNamedChild("name", name)) return false;
00047                 if (!accessoryNode->getNamedChild("count", count)) return false;
00048                 if (!accessoryNode->failChildren()) return false;
00049 
00050                 Accessory *accessory = context.getAccessoryStore().
00051                         findByPrimaryAccessoryName(name.c_str());
00052                 if (!accessory)
00053                 {
00054                         return accessoryNode->returnError("Failed to find named accessory");
00055                 }
00056 
00057                 accessories_[accessory] = count;
00058         }
00059         while (node->getNamedChild("disableaccessory", accessoryNode, false))
00060         {
00061                 std::string name;
00062                 if (!accessoryNode->getNamedChild("name", name)) return false;
00063                 if (!accessoryNode->failChildren()) return false;
00064 
00065                 Accessory *accessory = context.getAccessoryStore().
00066                         findByPrimaryAccessoryName(name.c_str());
00067                 if (!accessory)
00068                 {
00069                         return accessoryNode->returnError("Failed to find named accessory");
00070                 }
00071 
00072                 disabledAccessories_.insert(accessory);
00073         }
00074 
00075         return node->failChildren();
00076 }
00077 
00078 bool TankType::getAccessoryDisabled(Accessory *accessory)
00079 {
00080         if (disabledAccessories_.empty()) return false;
00081         return (disabledAccessories_.find(accessory) != disabledAccessories_.end());
00082 }
00083 
00084 const char *TankType::getDescription()
00085 {
00086         std::string accessoryBuffer;
00087         {
00088                 if (!accessories_.empty()) accessoryBuffer.append("\n");
00089 
00090                 unsigned int count = 0;
00091                 std::map<Accessory *, int>::iterator itor;
00092                 for (itor = accessories_.begin();
00093                         itor != accessories_.end();
00094                         itor++, count++)
00095                 {
00096                         Accessory *accessory = (*itor).first;
00097                         accessoryBuffer.append("+ ").append(accessory->getName());
00098                         if (count + 1 < accessories_.size()) accessoryBuffer.append("\n");
00099                 }
00100         }
00101         {
00102                 if (!disabledAccessories_.empty()) accessoryBuffer.append("\n");
00103 
00104                 unsigned int count = 0;
00105                 std::set<Accessory *>::iterator itor;
00106                 for (itor = disabledAccessories_.begin();
00107                         itor != disabledAccessories_.end();
00108                         itor++, count++)
00109                 {
00110                         Accessory *accessory = (*itor);
00111                         accessoryBuffer.append("- ").append(accessory->getName());
00112                         if (count + 1 < disabledAccessories_.size()) accessoryBuffer.append("\n");
00113                 }
00114         }
00115 
00116         description_ = S3D::formatStringBuffer(
00117                 "Life : %.0f\n"
00118                 "Power : %.0f%s",
00119                 getLife().asFloat(),
00120                 getPower().asFloat(),
00121                 accessoryBuffer.c_str());       
00122         return description_.c_str();
00123 }

Generated on Mon Feb 16 15:14:52 2009 for Scorched3D by  doxygen 1.5.3