1 /***************************************************************************
2  *      Mechanized Assault and Exploration Reloaded Projectfile            *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 
20 #include "game/data/player/clans.h"
21 #include "game/data/units/unitdata.h"
22 
23 using namespace std;
24 
25 
26 //--------------------------------------------------
addModification(const string & area,int value)27 void cClanUnitStat::addModification (const string& area, int value)
28 {
29 	modifications[area] = value;
30 }
31 
32 //--------------------------------------------------
hasModification(const string & key) const33 bool cClanUnitStat::hasModification (const string& key) const
34 {
35 	return modifications.find (key) != modifications.end();
36 }
37 
38 //--------------------------------------------------
getModificationValue(const string & key) const39 int cClanUnitStat::getModificationValue (const string& key) const
40 {
41 	map<string, int>::const_iterator it = modifications.find (key);
42 	if (it != modifications.end())
43 		return it->second;
44 	return 0;
45 }
46 
47 //--------------------------------------------------
GetModificatorString(int original,int modified)48 static string GetModificatorString (int original, int modified)
49 {
50 	const int diff = modified - original;
51 	if (diff > 0)
52 		return " +" + iToStr (diff);
53 	else if (diff < 0)
54 		return " -" + iToStr (-diff);
55 	else // diff == 0
56 		return " =" + iToStr (modified);
57 }
58 
59 //--------------------------------------------------
getClanStatsDescription() const60 string cClanUnitStat::getClanStatsDescription() const
61 {
62 	const sUnitData* data = unitId.getUnitDataOriginalVersion();
63 
64 	if (data == nullptr) return "Unknown";
65 
66 	string result = string (data->name) + lngPack.i18n ("Text~Punctuation~Colon");
67 	const char* const commaSep = ", ";
68 	const char* sep = "";
69 
70 	struct
71 	{
72 		const char* type;
73 		const char* textToTranslate;
74 		int originalValue;
75 	} t[] =
76 	{
77 		// ToDo / Fixme if #756 fixed, use the non "_7" version of the text files
78 		{"Damage", "Text~Others~Attack_7", data->getDamage()},
79 		{"Range", "Text~Others~Range", data->getRange()},
80 		{"Armor", "Text~Others~Armor_7", data->getArmor()},
81 		{"Hitpoints", "Text~Others~Hitpoints_7", data->getHitpointsMax()},
82 		{"Scan", "Text~Others~Scan_7", data->getScan()},
83 		{"Speed", "Text~Others~Speed_7", data->getSpeedMax() / 4},
84 	};
85 
86 	for (int i = 0; i != sizeof (t) / sizeof (*t); ++i)
87 	{
88 		if (hasModification (t[i].type) == false) continue;
89 		result += sep;
90 		result += lngPack.i18n (t[i].textToTranslate);
91 		result += GetModificatorString (t[i].originalValue, getModificationValue (t[i].type));
92 		sep = commaSep;
93 	}
94 	if (hasModification ("Built_Costs"))
95 	{
96 		result += sep;
97 		int nrTurns = getModificationValue ("Built_Costs");
98 		if (data->isHuman == false) nrTurns /= unitId.isAVehicle() == 0 ? 2 : 3;
99 
100 		result += iToStr (nrTurns) + " " + lngPack.i18n ("Text~Comp~Turns");
101 	}
102 	return result;
103 }
104 
105 //--------------------------------------------------
~cClan()106 cClan::~cClan()
107 {
108 }
109 
110 //--------------------------------------------------
getUnitStat(sID id) const111 cClanUnitStat* cClan::getUnitStat (sID id) const
112 {
113 	for (const auto& stat : stats)
114 		if (stat->getUnitId() == id)
115 			return stat.get();
116 	return nullptr;
117 }
118 
119 //--------------------------------------------------
getUnitStat(unsigned int index) const120 cClanUnitStat* cClan::getUnitStat (unsigned int index) const
121 {
122 	if (index < stats.size())
123 		return stats[index].get();
124 	return nullptr;
125 }
126 
127 //--------------------------------------------------
addUnitStat(sID id)128 cClanUnitStat* cClan::addUnitStat (sID id)
129 {
130 	stats.push_back (std::make_unique<cClanUnitStat> (id));
131 	return stats.back().get();
132 }
133 
134 //--------------------------------------------------
setDescription(const string & newDescription)135 void cClan::setDescription (const string& newDescription)
136 {
137 	description = newDescription;
138 }
139 //--------------------------------------------------
setName(const string & newName)140 void cClan::setName (const string& newName)
141 {
142 	name = newName;
143 }
144 
145 //--------------------------------------------------
getClanStatsDescription() const146 vector<string> cClan::getClanStatsDescription() const
147 {
148 	vector<string> result;
149 	for (int i = 0; i != getNrUnitStats(); ++i)
150 	{
151 		cClanUnitStat* stat = getUnitStat (i);
152 		result.push_back (stat->getClanStatsDescription());
153 	}
154 	return result;
155 }
156 
157 //--------------------------------------------------
instance()158 /*static*/ cClanData& cClanData::instance()
159 {
160 	static cClanData _instance;
161 	return _instance;
162 }
163 
164 //--------------------------------------------------
~cClanData()165 cClanData::~cClanData()
166 {
167 }
168 
169 //--------------------------------------------------
addClan()170 cClan* cClanData::addClan()
171 {
172 	clans.push_back (std::make_unique<cClan> ((int) clans.size()));
173 	return clans.back().get();
174 }
175 
176 //--------------------------------------------------
getClan(unsigned int num)177 cClan* cClanData::getClan (unsigned int num)
178 {
179 	if (num < clans.size())
180 		return clans[num].get();
181 	return nullptr;
182 }
183