00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00042 if (!node->getNamedChild("name", tankName_)) return false;
00043
00044
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
00058 XMLNode *modelNode;
00059 if (!node->getNamedChild("model", modelNode)) return false;
00060
00061
00062
00063
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
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
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
00090 std::string catagory;
00091 while (node->getNamedChild("catagory", catagory, false))
00092 {
00093 catagories_.insert(catagory);
00094 }
00095
00096
00097 int team;
00098 while (node->getNamedChild("team", team, false))
00099 {
00100 teams_.insert(team);
00101 }
00102
00103
00104 aiOnly_ = false;
00105 node->getNamedChild("aionly", aiOnly_, false);
00106
00107
00108 movementSmoke_ = true;
00109 node->getNamedChild("movementsmoke", movementSmoke_, false);
00110
00111
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;
00162 if (teams_.empty()) return true;
00163 std::set<int>::iterator itor =
00164 teams_.find(team);
00165 return (itor != teams_.end());
00166 }