1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include <tanket/TanketType.h>
22 #include <XML/XMLParser.h>
23 #include <common/ToolTip.h>
24 #include <common/Defines.h>
25 #include <engine/ScorchedContext.h>
26 #include <weapons/AccessoryStore.h>
27 
TanketType()28 TanketType::TanketType() : tooltip_(0), default_(false)
29 {
30 }
31 
~TanketType()32 TanketType::~TanketType()
33 {
34 }
35 
initFromXML(ScorchedContext & context,XMLNode * node)36 bool TanketType::initFromXML(ScorchedContext &context, XMLNode *node)
37 {
38 	node->getNamedChild("default", default_, false);
39 	if (!node->getNamedChild("name", name_)) return false;
40 	if (!node->getNamedChild("life", life_)) return false;
41 	if (!node->getNamedChild("power", power_)) return false;
42 	if (!node->getNamedChild("description", description_)) return false;
43 
44 	XMLNode *accessoryNode = 0;
45 	while (node->getNamedChild("accessory", accessoryNode, false))
46 	{
47 		std::string name;
48 		int count;
49 		if (!accessoryNode->getNamedChild("name", name)) return false;
50 		if (!accessoryNode->getNamedChild("count", count)) return false;
51 		if (!accessoryNode->failChildren()) return false;
52 
53 		Accessory *accessory = context.getAccessoryStore().
54 			findByPrimaryAccessoryName(name.c_str());
55 		if (!accessory)
56 		{
57 			return accessoryNode->returnError("Failed to find named accessory");
58 		}
59 
60 		accessories_[accessory] = count;
61 	}
62 	while (node->getNamedChild("disableaccessory", accessoryNode, false))
63 	{
64 		std::string name;
65 		if (!accessoryNode->getNamedChild("name", name)) return false;
66 		if (!accessoryNode->failChildren()) return false;
67 
68 		Accessory *accessory = context.getAccessoryStore().
69 			findByPrimaryAccessoryName(name.c_str());
70 		if (!accessory)
71 		{
72 			return accessoryNode->returnError("Failed to find named accessory");
73 		}
74 
75 		disabledAccessories_.insert(accessory);
76 	}
77 
78 	formTooltip();
79 
80 	return node->failChildren();
81 }
82 
getAccessoryDisabled(Accessory * accessory)83 bool TanketType::getAccessoryDisabled(Accessory *accessory)
84 {
85 	if (disabledAccessories_.empty()) return false;
86 	return (disabledAccessories_.find(accessory) != disabledAccessories_.end());
87 }
88 
formTooltip()89 void TanketType::formTooltip()
90 {
91 	delete tooltip_;
92 
93 	std::string accessoryBuffer;
94 	{
95 		if (!accessories_.empty()) accessoryBuffer.append("\n");
96 
97 		unsigned int count = 0;
98 		std::map<Accessory *, int>::iterator itor;
99 		for (itor = accessories_.begin();
100 			itor != accessories_.end();
101 			++itor, count++)
102 		{
103 			Accessory *accessory = (*itor).first;
104 			accessoryBuffer.append("+ ").append(accessory->getName());
105 			if (count + 1 < accessories_.size()) accessoryBuffer.append("\n");
106 		}
107 	}
108 	{
109 		if (!disabledAccessories_.empty()) accessoryBuffer.append("\n");
110 
111 		unsigned int count = 0;
112 		std::set<Accessory *>::iterator itor;
113 		for (itor = disabledAccessories_.begin();
114 			itor != disabledAccessories_.end();
115 			++itor, count++)
116 		{
117 			Accessory *accessory = (*itor);
118 			accessoryBuffer.append("- ").append(accessory->getName());
119 			if (count + 1 < disabledAccessories_.size()) accessoryBuffer.append("\n");
120 		}
121 	}
122 
123 	tooltip_ = new ToolTip(
124 		ToolTip::ToolTipNone,
125 		LANG_STRING(name_),
126 		LANG_STRING(S3D::formatStringBuffer(
127 			"%s\n"
128 			"Life : %.0f\n"
129 			"Power : %.0f%s",
130 			description_.c_str(),
131 			getLife().asFloat(),
132 			getPower().asFloat(),
133 			accessoryBuffer.c_str())));
134 }
135