00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 }