|
1 /* |
|
2 * Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2009 Martin Minarik <ttsmj@pokec.sk> |
|
4 * Copyright (c) 2009-2011 Andrey Korotaev <unC0Rr@gmail.com> |
|
5 * |
|
6 * This program 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; version 2 of the License |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program; if not, write to the Free Software |
|
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
18 */ |
|
19 |
|
20 #include <QFile> |
|
21 #include <QTextStream> |
|
22 #include <QStringList> |
|
23 #include <QLineEdit> |
|
24 #include "namegen.h" |
|
25 #include "hwform.h" |
|
26 #include "hwconsts.h" |
|
27 |
|
28 |
|
29 HWNamegen::HWNamegen() {} |
|
30 |
|
31 QList<QStringList> HWNamegen::TypesTeamnames; |
|
32 QList<QStringList> HWNamegen::TypesHatnames; |
|
33 bool HWNamegen::typesAvailable = false; |
|
34 |
|
35 |
|
36 void HWNamegen::teamRandomNames(HWTeam & team, const bool changeteamname) |
|
37 { |
|
38 // load types if not already loaded |
|
39 if (!typesAvailable) |
|
40 if (!loadTypes()) |
|
41 return; // abort if loading failed |
|
42 |
|
43 // abort if there are no hat types |
|
44 if (TypesHatnames.size() <= 0) |
|
45 return; |
|
46 |
|
47 // the hat will influence which names the hogs get |
|
48 int kind = (rand()%(TypesHatnames.size())); |
|
49 |
|
50 // pick team name based on hat |
|
51 if (changeteamname) |
|
52 { |
|
53 if (TypesTeamnames[kind].size() > 0) |
|
54 team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]); |
|
55 |
|
56 team.setGrave(getRandomGrave()); |
|
57 team.setFort(getRandomFort()); |
|
58 team.setVoicepack("Default"); |
|
59 } |
|
60 |
|
61 QStringList dicts; |
|
62 QStringList dict; |
|
63 |
|
64 if ((TypesHatnames[kind].size()) <= 0) |
|
65 { |
|
66 dicts = dictsForHat(team.hedgehog(0).Hat); |
|
67 dict = dictContents(dicts[rand()%(dicts.size())]); |
|
68 } |
|
69 |
|
70 for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++) |
|
71 { |
|
72 if ((TypesHatnames[kind].size()) > 0) |
|
73 { |
|
74 HWHog hh = team.hedgehog(i); |
|
75 hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())]; |
|
76 team.setHedgehog(i,hh); |
|
77 } |
|
78 |
|
79 // there is a chance that this hog has the same hat as the previous one |
|
80 // let's reuse the hat-specific dict in this case |
|
81 if ((i == 0) or (team.hedgehog(i).Hat != team.hedgehog(i-1).Hat)) |
|
82 { |
|
83 dicts = dictsForHat(team.hedgehog(i).Hat); |
|
84 dict = dictContents(dicts[rand()%(dicts.size())]); |
|
85 } |
|
86 |
|
87 // give each hedgehog a random name |
|
88 HWNamegen::teamRandomName(team,i,dict); |
|
89 } |
|
90 |
|
91 } |
|
92 |
|
93 void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber) |
|
94 { |
|
95 QStringList dicts = dictsForHat(team.hedgehog(HedgehogNumber).Hat); |
|
96 |
|
97 QStringList dict = dictContents(dicts[rand()%(dicts.size())]); |
|
98 |
|
99 teamRandomName(team, HedgehogNumber, dict); |
|
100 } |
|
101 |
|
102 void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber, const QStringList & dict) |
|
103 { |
|
104 QStringList namesDict = dict; |
|
105 |
|
106 for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++) |
|
107 { |
|
108 namesDict.removeOne(team.hedgehog(i).Name); |
|
109 } |
|
110 |
|
111 // if our dict doesn't have any new names we'll have to use duplicates |
|
112 if (namesDict.size() < 1) |
|
113 namesDict = dict; |
|
114 |
|
115 HWHog hh = team.hedgehog(HedgehogNumber); |
|
116 |
|
117 hh.Name = namesDict[rand()%(namesDict.size())]; |
|
118 |
|
119 team.setHedgehog(HedgehogNumber, hh); |
|
120 } |
|
121 |
|
122 QStringList HWNamegen::dictContents(const QString filename) |
|
123 { |
|
124 QStringList list; |
|
125 |
|
126 QFile file; |
|
127 |
|
128 // find .cfg to load the names from |
|
129 file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename)); |
|
130 if (!file.exists()) |
|
131 file.setFileName(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename)); |
|
132 |
|
133 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
134 { |
|
135 |
|
136 QTextStream in(&file); |
|
137 while (!in.atEnd()) |
|
138 { |
|
139 QString line = in.readLine(); |
|
140 if(!line.isEmpty()) |
|
141 list.append(line); |
|
142 } |
|
143 } |
|
144 |
|
145 if (list.size() == 0) |
|
146 list.append(filename); |
|
147 |
|
148 return list; |
|
149 } |
|
150 |
|
151 |
|
152 QStringList HWNamegen::dictsForHat(const QString hatname) |
|
153 { |
|
154 QStringList list; |
|
155 |
|
156 QFile file; |
|
157 |
|
158 // find .cfg to load the names from |
|
159 file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname)); |
|
160 if (!file.exists()) |
|
161 file.setFileName(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname)); |
|
162 |
|
163 |
|
164 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
165 { |
|
166 QTextStream in(&file); |
|
167 while (!in.atEnd()) |
|
168 { |
|
169 QString line = in.readLine(); |
|
170 if(!line.isEmpty()) |
|
171 list.append(line); |
|
172 } |
|
173 } |
|
174 |
|
175 if (list.size() == 0) |
|
176 list.append(QString("generic")); |
|
177 |
|
178 return list; |
|
179 } |
|
180 |
|
181 // loades types from ini files. returns true on success. |
|
182 bool HWNamegen::loadTypes() |
|
183 { |
|
184 QFile file; |
|
185 |
|
186 // find .cfg to load the names from |
|
187 file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath())); |
|
188 if (!file.exists()) |
|
189 file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath())); |
|
190 |
|
191 |
|
192 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
193 return false; |
|
194 |
|
195 int counter = 0; //counter starts with 0 (teamnames mode) |
|
196 TypesTeamnames.append(QStringList()); |
|
197 TypesHatnames.append(QStringList()); |
|
198 |
|
199 QTextStream in(&file); |
|
200 while (!in.atEnd()) |
|
201 { |
|
202 QString line = in.readLine(); |
|
203 if (line == QString("#####")) |
|
204 { |
|
205 counter++; //toggle mode (teamnames || hats) |
|
206 if ((counter%2) == 0) |
|
207 { |
|
208 TypesTeamnames.append(QStringList()); |
|
209 TypesHatnames.append(QStringList()); |
|
210 } |
|
211 } |
|
212 else if ((line == QString("*****")) || (line == QString("*END*"))) |
|
213 { |
|
214 typesAvailable = true; |
|
215 return true; // bye bye |
|
216 } |
|
217 else |
|
218 { |
|
219 if ((counter%2) == 0) |
|
220 { |
|
221 // even => teamnames mode |
|
222 TypesTeamnames[(counter/2)].append(line); |
|
223 } |
|
224 else |
|
225 { |
|
226 // odd => hats mode |
|
227 TypesHatnames[((counter-1)/2)].append(line); |
|
228 } |
|
229 } |
|
230 } |
|
231 |
|
232 typesAvailable = true; |
|
233 return true; |
|
234 } |
|
235 |
|
236 |
|
237 |
|
238 QString HWNamegen::getRandomGrave() |
|
239 { |
|
240 QStringList Graves; |
|
241 |
|
242 //list all available Graves |
|
243 QDir tmpdir; |
|
244 tmpdir.cd(cfgdir->absolutePath()); |
|
245 tmpdir.cd("Data/Graphics/Graves"); |
|
246 tmpdir.setFilter(QDir::Files); |
|
247 Graves.append(tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1")); |
|
248 |
|
249 tmpdir.cd(datadir->absolutePath()); |
|
250 tmpdir.cd("Graphics/Graves"); |
|
251 tmpdir.setFilter(QDir::Files); |
|
252 QStringList tmpList = tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1"); |
|
253 for (QStringList::Iterator it = tmpList.begin(); it != tmpList.end(); ++it) |
|
254 if (!Graves.contains(*it,Qt::CaseInsensitive)) Graves.append(*it); |
|
255 |
|
256 if(Graves.size()==0) |
|
257 { |
|
258 //do some serious error handling |
|
259 return "Error"; |
|
260 } |
|
261 |
|
262 //pick a random grave |
|
263 return Graves[rand()%(Graves.size())]; |
|
264 } |
|
265 |
|
266 QString HWNamegen::getRandomFort() |
|
267 { |
|
268 QStringList Forts; |
|
269 |
|
270 //list all available Forts |
|
271 QDir tmpdir; |
|
272 tmpdir.cd(datadir->absolutePath()); |
|
273 tmpdir.cd("Forts"); |
|
274 tmpdir.setFilter(QDir::Files); |
|
275 Forts.append(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1")); |
|
276 |
|
277 if(Forts.size()==0) |
|
278 { |
|
279 //do some serious error handling |
|
280 return "Error"; |
|
281 } |
|
282 |
|
283 //pick a random fort |
|
284 return Forts[rand()%(Forts.size())]; |
|
285 } |