author | unc0rr |
Tue, 24 Feb 2009 21:47:17 +0000 | |
changeset 1840 | 4747f0232b88 |
parent 1683 | af34da5726aa |
child 1907 | a104432e8301 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
1656
209cf0e2fc36
Finish voicepacks support in engine (not tested though)
unc0rr
parents:
1655
diff
changeset
|
3 |
* Copyright (c) 2005-2007, 2009 Andrey Korotaev <unC0Rr@gmail.com> |
184 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
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 Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
#include <QFile> |
|
20 |
#include <QTextStream> |
|
21 |
#include <QApplication> |
|
471 | 22 |
#include <QStringList> |
23 |
#include <QLineEdit> |
|
184 | 24 |
#include "team.h" |
25 |
#include "hwform.h" |
|
26 |
#include "predefteams.h" |
|
27 |
#include "pages.h" |
|
28 |
#include "hwconsts.h" |
|
1240 | 29 |
#include "hats.h" |
314 | 30 |
|
1325 | 31 |
HWTeam::HWTeam(const QString & teamname) : |
32 |
difficulty(0), |
|
33 |
numHedgehogs(4), |
|
34 |
m_isNetTeam(false) |
|
184 | 35 |
{ |
1840 | 36 |
TypesLoad(); |
37 |
||
184 | 38 |
TeamName = teamname; |
245 | 39 |
OldTeamName = TeamName; |
1240 | 40 |
for (int i = 0; i < 8; i++) |
41 |
{ |
|
42 |
HHName[i].sprintf("hedgehog %d", i); |
|
43 |
HHHat[i] = "NoHat"; |
|
44 |
} |
|
1538
8bc56dd2e997
Set default fort to 'Plane' and default grave to 'Statue'
unc0rr
parents:
1325
diff
changeset
|
45 |
Grave = "Statue"; |
8bc56dd2e997
Set default fort to 'Plane' and default grave to 'Statue'
unc0rr
parents:
1325
diff
changeset
|
46 |
Fort = "Plane"; |
1659 | 47 |
Voicepack = "Default"; |
184 | 48 |
for(int i = 0; i < BINDS_NUMBER; i++) |
49 |
{ |
|
50 |
binds[i].action = cbinds[i].action; |
|
51 |
binds[i].strbind = cbinds[i].strbind; |
|
52 |
} |
|
53 |
} |
|
54 |
||
353 | 55 |
HWTeam::HWTeam(const QStringList& strLst) : |
1325 | 56 |
numHedgehogs(4), |
57 |
m_isNetTeam(true) |
|
314 | 58 |
{ |
1840 | 59 |
TypesLoad(); |
60 |
||
1245
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
61 |
// net teams are configured from QStringList |
1683 | 62 |
if(strLst.size() != 22) throw HWTeamConstructException(); |
1325 | 63 |
TeamName = strLst[0]; |
64 |
Grave = strLst[1]; |
|
65 |
Fort = strLst[2]; |
|
1662 | 66 |
Voicepack = strLst[3]; |
1683 | 67 |
Owner = strLst[4]; |
68 |
difficulty = strLst[5].toUInt(); |
|
1245
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
69 |
for(int i = 0; i < 8; i++) |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
70 |
{ |
1683 | 71 |
HHName[i]=strLst[i * 2 + 6]; |
72 |
HHHat[i]=strLst[i * 2 + 7]; |
|
1245
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
73 |
} |
314 | 74 |
} |
75 |
||
231 | 76 |
HWTeam::HWTeam(quint8 num) : |
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
336
diff
changeset
|
77 |
difficulty(0), |
353 | 78 |
numHedgehogs(4), |
1325 | 79 |
m_isNetTeam(false) |
184 | 80 |
{ |
1840 | 81 |
TypesLoad(); |
82 |
||
184 | 83 |
num %= PREDEFTEAMS_COUNT; |
1840 | 84 |
/* |
85 |
//This is now generated by TeamRandomNames(TRUE); |
|
184 | 86 |
TeamName = QApplication::translate("teams", pteams[num].TeamName); |
87 |
HHName[0] = QApplication::translate("teams", pteams[num].hh0name); |
|
88 |
HHName[1] = QApplication::translate("teams", pteams[num].hh1name); |
|
89 |
HHName[2] = QApplication::translate("teams", pteams[num].hh2name); |
|
90 |
HHName[3] = QApplication::translate("teams", pteams[num].hh3name); |
|
91 |
HHName[4] = QApplication::translate("teams", pteams[num].hh4name); |
|
92 |
HHName[5] = QApplication::translate("teams", pteams[num].hh5name); |
|
93 |
HHName[6] = QApplication::translate("teams", pteams[num].hh6name); |
|
94 |
HHName[7] = QApplication::translate("teams", pteams[num].hh7name); |
|
1293 | 95 |
HHHat[0] = pteams[num].hh0hat; |
96 |
HHHat[1] = pteams[num].hh1hat; |
|
97 |
HHHat[2] = pteams[num].hh2hat; |
|
98 |
HHHat[3] = pteams[num].hh3hat; |
|
99 |
HHHat[4] = pteams[num].hh4hat; |
|
100 |
HHHat[5] = pteams[num].hh5hat; |
|
101 |
HHHat[6] = pteams[num].hh6hat; |
|
102 |
HHHat[7] = pteams[num].hh7hat; |
|
1840 | 103 |
*/ |
104 |
||
105 |
TeamName = QString("Team"); |
|
106 |
for (int i = 0; i < 8; i++) |
|
107 |
{ |
|
108 |
HHName[i].sprintf("hedgehog %d", i); |
|
109 |
HHHat[i] = "NoHat"; |
|
110 |
} |
|
111 |
||
184 | 112 |
Grave = pteams[num].Grave; |
113 |
Fort = pteams[num].Fort; |
|
114 |
for(int i = 0; i < BINDS_NUMBER; i++) |
|
115 |
{ |
|
116 |
binds[i].action = cbinds[i].action; |
|
117 |
binds[i].strbind = cbinds[i].strbind; |
|
118 |
} |
|
1840 | 119 |
|
120 |
TeamRandomNames(TRUE); |
|
184 | 121 |
} |
122 |
||
123 |
||
124 |
bool HWTeam::LoadFromFile() |
|
125 |
{ |
|
353 | 126 |
numHedgehogs=4; |
184 | 127 |
QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg"); |
128 |
if (!cfgfile.open(QIODevice::ReadOnly)) return false; |
|
129 |
QTextStream stream(&cfgfile); |
|
130 |
stream.setCodec("UTF-8"); |
|
131 |
QString str; |
|
132 |
QString action; |
|
133 |
||
134 |
while (!stream.atEnd()) |
|
135 |
{ |
|
136 |
str = stream.readLine(); |
|
137 |
if (str.startsWith(";")) continue; |
|
245 | 138 |
/*if (str.startsWith("name team ")) |
184 | 139 |
{ |
140 |
str.remove(0, 10); |
|
141 |
TeamName = str; |
|
245 | 142 |
} else*/ |
184 | 143 |
if (str.startsWith("name hh")) |
144 |
{ |
|
145 |
str.remove(0, 7); |
|
146 |
long i = str.left(1).toLong(); |
|
147 |
if ((i < 0) || (i > 7)) continue; |
|
148 |
str.remove(0, 2); |
|
149 |
HHName[i] = str; |
|
150 |
} else |
|
1240 | 151 |
if (str.startsWith("hat")) |
152 |
{ |
|
153 |
str.remove(0, 3); |
|
154 |
long i = str.left(1).toLong(); |
|
155 |
if ((i < 0) || (i > 7)) continue; |
|
156 |
str.remove(0, 2); |
|
157 |
HHHat[i] = str; |
|
158 |
} else |
|
184 | 159 |
if (str.startsWith("grave ")) |
160 |
{ |
|
161 |
str.remove(0, 6); |
|
162 |
Grave = str; |
|
163 |
} else |
|
164 |
if (str.startsWith("fort ")) |
|
165 |
{ |
|
166 |
str.remove(0, 5); |
|
167 |
Fort = str; |
|
168 |
} else |
|
1659 | 169 |
if (str.startsWith("voicepack ")) |
170 |
{ |
|
171 |
str.remove(0, 10); |
|
172 |
Voicepack = str; |
|
173 |
} else |
|
184 | 174 |
if (str.startsWith("bind ")) |
175 |
{ |
|
176 |
str.remove(0, 5); |
|
177 |
action = str.section(' ', 1); |
|
178 |
str = str.section(' ', 0, 0); |
|
179 |
str.truncate(15); |
|
180 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
181 |
if (action == binds[i].action) |
|
182 |
{ |
|
183 |
binds[i].strbind = str; |
|
184 |
break; |
|
185 |
} |
|
239 | 186 |
} else |
231 | 187 |
if (str.startsWith("difficulty ")) |
188 |
{ |
|
189 |
str.remove(0, 11); |
|
190 |
difficulty=str.toUInt(); |
|
191 |
if (difficulty>5) difficulty=0; // this shouldn't normally happen |
|
184 | 192 |
} |
193 |
} |
|
194 |
cfgfile.close(); |
|
195 |
return true; |
|
196 |
} |
|
197 |
||
198 |
bool HWTeam::SaveToFile() |
|
199 |
{ |
|
245 | 200 |
if (OldTeamName != TeamName) |
201 |
{ |
|
202 |
QFile cfgfile(cfgdir->absolutePath() + "/" + OldTeamName + ".cfg"); |
|
203 |
cfgfile.remove(); |
|
204 |
OldTeamName = TeamName; |
|
205 |
} |
|
184 | 206 |
QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg"); |
207 |
if (!cfgfile.open(QIODevice::WriteOnly)) return false; |
|
208 |
QTextStream stream(&cfgfile); |
|
209 |
stream.setCodec("UTF-8"); |
|
210 |
stream << "; Generated by Hedgewars, do not modify" << endl; |
|
211 |
stream << "name team " << TeamName << endl; |
|
212 |
for (int i = 0; i < 8; i++) |
|
1240 | 213 |
{ |
184 | 214 |
stream << "name hh" << i << " " << HHName[i] << endl; |
1240 | 215 |
stream << "hat" << i << " " << HHHat[i] << endl; |
216 |
} |
|
184 | 217 |
stream << "grave " << Grave << endl; |
218 |
stream << "fort " << Fort << endl; |
|
1659 | 219 |
stream << "voicepack " << Voicepack << endl; |
184 | 220 |
for(int i = 0; i < BINDS_NUMBER; i++) |
221 |
{ |
|
222 |
stream << "bind " << binds[i].strbind << " " << binds[i].action << endl; |
|
223 |
} |
|
231 | 224 |
stream << "difficulty " << difficulty << endl; |
184 | 225 |
cfgfile.close(); |
226 |
return true; |
|
227 |
} |
|
228 |
||
229 |
void HWTeam::SetToPage(HWForm * hwform) |
|
230 |
{ |
|
231 |
hwform->ui.pageEditTeam->TeamNameEdit->setText(TeamName); |
|
336 | 232 |
hwform->ui.pageEditTeam->CBTeamLvl->setCurrentIndex(difficulty); |
184 | 233 |
for(int i = 0; i < 8; i++) |
234 |
{ |
|
235 |
hwform->ui.pageEditTeam->HHNameEdit[i]->setText(HHName[i]); |
|
1240 | 236 |
hwform->ui.pageEditTeam->HHHats[i]->setCurrentIndex(hwform->ui.pageEditTeam->HHHats[i]->findData(HHHat[i], Qt::DisplayRole)); |
184 | 237 |
} |
238 |
hwform->ui.pageEditTeam->CBGrave->setCurrentIndex(hwform->ui.pageEditTeam->CBGrave->findText(Grave)); |
|
239 |
||
240 |
hwform->ui.pageEditTeam->CBFort->setCurrentIndex(hwform->ui.pageEditTeam->CBFort->findText(Fort)); |
|
1659 | 241 |
hwform->ui.pageEditTeam->CBVoicepack->setCurrentIndex(hwform->ui.pageEditTeam->CBVoicepack->findText(Voicepack)); |
1287 | 242 |
//hwform->ui.pageEditTeam->CBFort_activated(Fort); |
184 | 243 |
|
244 |
for(int i = 0; i < BINDS_NUMBER; i++) |
|
245 |
{ |
|
246 |
hwform->ui.pageEditTeam->CBBind[i]->setCurrentIndex(hwform->ui.pageEditTeam->CBBind[i]->findText(binds[i].strbind)); |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
void HWTeam::GetFromPage(HWForm * hwform) |
|
251 |
{ |
|
252 |
TeamName = hwform->ui.pageEditTeam->TeamNameEdit->text(); |
|
336 | 253 |
difficulty = hwform->ui.pageEditTeam->CBTeamLvl->currentIndex(); |
184 | 254 |
for(int i = 0; i < 8; i++) |
255 |
{ |
|
256 |
HHName[i] = hwform->ui.pageEditTeam->HHNameEdit[i]->text(); |
|
1240 | 257 |
HHHat[i] = hwform->ui.pageEditTeam->HHHats[i]->currentText(); |
184 | 258 |
} |
259 |
||
260 |
Grave = hwform->ui.pageEditTeam->CBGrave->currentText(); |
|
261 |
Fort = hwform->ui.pageEditTeam->CBFort->currentText(); |
|
1659 | 262 |
Voicepack = hwform->ui.pageEditTeam->CBVoicepack->currentText(); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
245
diff
changeset
|
263 |
for(int i = 0; i < BINDS_NUMBER; i++) |
184 | 264 |
{ |
265 |
binds[i].strbind = hwform->ui.pageEditTeam->CBBind[i]->currentText(); |
|
266 |
} |
|
267 |
} |
|
268 |
||
341 | 269 |
QStringList HWTeam::TeamGameConfig(quint32 InitHealth) const |
184 | 270 |
{ |
239 | 271 |
QStringList sl; |
605 | 272 |
sl.push_back(QString("eaddteam %1 %2").arg(teamColor.rgb() & 0xffffff).arg(TeamName)); |
606
9d800fdfd3bd
Add ammostores in frontend, should help further ammostores implemetation
unc0rr
parents:
605
diff
changeset
|
273 |
|
1325 | 274 |
if (m_isNetTeam) |
341 | 275 |
sl.push_back("erdriven"); |
276 |
||
277 |
sl.push_back(QString("egrave " + Grave)); |
|
278 |
sl.push_back(QString("efort " + Fort)); |
|
1659 | 279 |
sl.push_back(QString("evoicepack " + Voicepack)); |
341 | 280 |
|
1325 | 281 |
if (!m_isNetTeam) |
341 | 282 |
for(int i = 0; i < BINDS_NUMBER; i++) |
283 |
sl.push_back(QString("ebind " + binds[i].strbind + " " + binds[i].action)); |
|
284 |
||
285 |
for (int t = 0; t < numHedgehogs; t++) |
|
1242 | 286 |
{ |
605 | 287 |
sl.push_back(QString("eaddhh %1 %2 %3") |
319 | 288 |
.arg(QString::number(difficulty), |
605 | 289 |
QString::number(InitHealth), |
290 |
HHName[t])); |
|
1242 | 291 |
sl.push_back(QString("ehat %1") |
292 |
.arg(HHHat[t])); |
|
293 |
} |
|
239 | 294 |
return sl; |
184 | 295 |
} |
296 |
||
1840 | 297 |
void HWTeam::RandomNameByHat(const int &i) |
298 |
{ |
|
299 |
||
300 |
||
301 |
||
302 |
QStringList Dictionaries; |
|
303 |
HatCfgLoad(HHHat[i],Dictionaries); |
|
304 |
||
305 |
||
306 |
/* |
|
307 |
||
308 |
"Dismissed", |
|
309 |
"Dragon", |
|
310 |
"Mindblower", |
|
311 |
*/ |
|
312 |
||
313 |
QStringList Dictionary; |
|
314 |
DictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary); |
|
315 |
||
316 |
||
317 |
HHName[i] = Dictionary[rand()%(Dictionary.size())]; |
|
318 |
||
319 |
} |
|
320 |
||
321 |
void HWTeam::TeamRandomName(const int &i) |
|
322 |
{ |
|
323 |
||
324 |
RandomNameByHat(i); |
|
325 |
||
326 |
||
327 |
} |
|
328 |
||
329 |
void HWTeam::TeamRandomNames(bool changeteamname) |
|
330 |
{ |
|
331 |
||
332 |
if ((TypesHatnames.size() > 0) && TypesAvliable){ |
|
333 |
||
334 |
int kind = (rand()%(TypesHatnames.size())); |
|
335 |
||
336 |
if (changeteamname){ |
|
337 |
if (TypesTeamnames[kind].size() > 0){ |
|
338 |
TeamName = TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]; |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
for(int i = 0; i < 8; i++) |
|
343 |
{ |
|
344 |
if ((TypesHatnames[kind].size()) > 0){ |
|
345 |
HHHat[i] = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())]; |
|
346 |
} |
|
347 |
RandomNameByHat(i); |
|
348 |
} |
|
349 |
||
350 |
} |
|
351 |
} |
|
352 |
||
352 | 353 |
bool HWTeam::isNetTeam() const |
354 |
{ |
|
1325 | 355 |
return m_isNetTeam; |
352 | 356 |
} |
357 |
||
358 |
||
184 | 359 |
bool HWTeam::operator==(const HWTeam& t1) const { |
1325 | 360 |
return TeamName==t1.TeamName; |
184 | 361 |
} |
362 |
||
363 |
bool HWTeam::operator<(const HWTeam& t1) const { |
|
1325 | 364 |
return TeamName<t1.TeamName; // if names are equal - test if it is net team |
184 | 365 |
} |
1840 | 366 |
|
367 |
void HWTeam::TypesLoad() |
|
368 |
{ |
|
369 |
||
370 |
QFile file(QString("%1/Names/types.ini").arg(datadir->absolutePath())); |
|
371 |
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
372 |
TypesAvliable = FALSE; |
|
373 |
||
374 |
int counter = 0; //counter starts with 0 (teamnames mode) |
|
375 |
TypesTeamnames.append(QStringList()); |
|
376 |
TypesHatnames.append(QStringList()); |
|
377 |
||
378 |
QTextStream in(&file); |
|
379 |
while (!in.atEnd()) { |
|
380 |
QString line = in.readLine(); |
|
381 |
if (line == QString("#####")){ |
|
382 |
counter++; //toggle mode (teamnames || hats) |
|
383 |
if ((counter%2) == 0){ |
|
384 |
TypesTeamnames.append(QStringList()); |
|
385 |
TypesHatnames.append(QStringList()); |
|
386 |
} |
|
387 |
} else if ((line == QString("*****")) || (line == QString("*END*"))){ |
|
388 |
TypesAvliable = TRUE; return; // bye bye |
|
389 |
} else { |
|
390 |
if ((counter%2) == 0){ // even => teamnames mode |
|
391 |
TypesTeamnames[(counter/2)].append(line); |
|
392 |
} else { // odd => hats mode |
|
393 |
TypesHatnames[((counter-1)/2)].append(line); |
|
394 |
} |
|
395 |
} |
|
396 |
// Types.append(line); |
|
397 |
} |
|
398 |
TypesAvliable = TRUE; |
|
399 |
return; |
|
400 |
} |
|
401 |
||
402 |
||
403 |
void HWTeam::DictLoad(const QString filename, QStringList &list) |
|
404 |
{ |
|
405 |
list.clear(); |
|
406 |
||
407 |
QFile file(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename)); |
|
408 |
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
409 |
{ |
|
410 |
||
411 |
QTextStream in(&file); |
|
412 |
while (!in.atEnd()) { |
|
413 |
QString line = in.readLine(); |
|
414 |
if(line != QString("")) |
|
415 |
{list.append(line);} |
|
416 |
} |
|
417 |
} |
|
418 |
||
419 |
if (list.size()==0) |
|
420 |
list.append(filename); |
|
421 |
||
422 |
} |
|
423 |
||
424 |
||
425 |
void HWTeam::HatCfgLoad(const QString hatname, QStringList &list) |
|
426 |
{ |
|
427 |
list.clear(); |
|
428 |
||
429 |
QFile file(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname)); |
|
430 |
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
431 |
{ |
|
432 |
||
433 |
QTextStream in(&file); |
|
434 |
while (!in.atEnd()) { |
|
435 |
QString line = in.readLine(); |
|
436 |
if(line != QString("")) |
|
437 |
{list.append(line);} |
|
438 |
} |
|
439 |
} |
|
440 |
||
441 |
if (list.size()==0) |
|
442 |
list.append(QString("generic")); |
|
443 |
||
444 |
} |
|
445 |
||
446 |