TankModel.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/TankModel.h>
00022 #include <tank/TankType.h>
00023 #include <tank/TankModelStore.h>
00024 #include <XML/XMLNode.h>
00025 #include <engine/ScorchedContext.h>
00026 
00027 TankModel::TankModel() :
00028         init_(false), 
00029         aiOnly_(false), 
00030         movementSmoke_(true)
00031 {
00032         catagories_.insert("All");
00033 }
00034 
00035 TankModel::~TankModel()
00036 {
00037 }
00038 
00039 bool TankModel::initFromXML(ScorchedContext &context, XMLNode *node)
00040 {
00041         // Get the name of tank
00042         if (!node->getNamedChild("name", tankName_)) return false;
00043 
00044         // Get the tank type (if any)
00045         typeName_ = "none";
00046         node->getNamedChild("type", typeName_, false);
00047         TankType *type = context.getTankModels().getTypeByName(typeName_.c_str());
00048         if (!type)
00049         {
00050                 return node->returnError(
00051                         S3D::formatStringBuffer(
00052                         "Failed to find tank type \"%s\" for tank \"%s\"",
00053                         typeName_.c_str(),
00054                         tankName_.c_str()));
00055         }
00056 
00057         // Get the model node
00058         XMLNode *modelNode;
00059         if (!node->getNamedChild("model", modelNode)) return false;
00060 
00061         // Parse the modelId which tells us which files and
00062         // 3d type the model actuall is
00063         // The model files are not parsed until later
00064         if (!modelId_.initFromNode("data/tanks", modelNode))
00065         {
00066                 return modelNode->returnError(
00067                         S3D::formatStringBuffer("Failed to load mesh for tank \"%s\"",
00068                         tankName_.c_str()));
00069         }
00070 
00071         // Get the projectile model node (if any)
00072         XMLNode *projectileModelNode;
00073         if (node->getNamedChild("projectilemodel", projectileModelNode, false))
00074         {
00075                 if (!projectileModelId_.initFromNode("data/accessories", projectileModelNode))
00076                 {
00077                         return projectileModelNode->returnError(
00078                                 S3D::formatStringBuffer("Failed to load projectile mesh for tank \"%s\"",
00079                                 tankName_.c_str()));
00080                 }
00081         }
00082 
00083         // Get the tracks node (if any)
00084         if (!loadImage(node, "tracksv", tracksVId_, "data/tanks/tracksv.bmp")) return false;
00085         if (!loadImage(node, "tracksh", tracksHId_, "data/tanks/tracksh.bmp")) return false;
00086         if (!loadImage(node, "tracksvh", tracksVHId_, "data/tanks/tracksvh.bmp")) return false;
00087         if (!loadImage(node, "trackshv", tracksHVId_, "data/tanks/trackshv.bmp")) return false;
00088 
00089         // Read all of the tank display catagories
00090         std::string catagory;
00091         while (node->getNamedChild("catagory", catagory, false))
00092         {
00093                 catagories_.insert(catagory);
00094         }
00095 
00096         // Read all of the tank team catagories
00097         int team;
00098         while (node->getNamedChild("team", team, false))
00099         {
00100                 teams_.insert(team);
00101         }
00102 
00103         // Read aionly attribute
00104         aiOnly_ = false;
00105         node->getNamedChild("aionly", aiOnly_, false);
00106 
00107         // Read movementSmoke attr
00108         movementSmoke_ = true;
00109         node->getNamedChild("movementsmoke", movementSmoke_, false);
00110 
00111         // Check there are no more nodes in this node
00112         return node->failChildren();
00113 }
00114 
00115 bool TankModel::loadImage(XMLNode *node, const char *nodeName, 
00116         ImageID &image, const char *backupImage)
00117 {
00118         XMLNode *imageNode;
00119         if (node->getNamedChild(nodeName, imageNode, false))
00120         {
00121                 if (!image.initFromNode("data/tanks", imageNode))
00122                 {
00123                         return imageNode->returnError(
00124                                 S3D::formatStringBuffer("Failed to load tracks image for tank \"%s\"",
00125                                 tankName_.c_str()));
00126                 }
00127         }
00128         else
00129         {
00130                 image.initFromString("bmp", backupImage, backupImage, true);
00131         }
00132         return true;
00133 }
00134 
00135 void TankModel::clear()
00136 {
00137         init_ = false;
00138 }
00139 
00140 bool TankModel::lessThan(TankModel *other)
00141 {
00142         return (strcmp(getName(), other->getName()) < 0);
00143 }
00144 
00145 bool TankModel::isOfCatagory(const char *catagory)
00146 {
00147         std::set<std::string>::iterator itor =
00148                 catagories_.find(catagory);
00149         return (itor != catagories_.end());
00150 }
00151 
00152 bool TankModel::isOfAi(bool ai)
00153 {
00154         if (!aiOnly_) return true;
00155         if (ai) return true;
00156         return false;
00157 }
00158 
00159 bool TankModel::isOfTeam(int team)
00160 {
00161         if (team == 0) return true; // No Team
00162         if (teams_.empty()) return true; // Tank not in a team
00163         std::set<int>::iterator itor =
00164                 teams_.find(team);
00165         return (itor != teams_.end());
00166 }

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