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>
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
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.
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
21
#include <QTextStream>
22
#include <QStringList>
26
#include "HWDataManager.h"
30
HWNamegen::HWNamegen() {}
32
QList<QStringList> HWNamegen::TypesTeamnames;
33
QList<QStringList> HWNamegen::TypesHatnames;
34
bool HWNamegen::typesAvailable = false;
37
void HWNamegen::teamRandomNames(HWTeam & team, const bool changeteamname)
39
// load types if not already loaded
42
return; // abort if loading failed
44
// abort if there are no hat types
45
if (TypesHatnames.size() <= 0)
48
// the hat will influence which names the hogs get
49
int kind = (rand()%(TypesHatnames.size()));
51
// pick team name based on hat
54
if (TypesTeamnames[kind].size() > 0)
55
team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]);
57
team.setGrave(getRandomGrave());
58
team.setFort(getRandomFort());
59
team.setVoicepack("Default");
65
if ((TypesHatnames[kind].size()) <= 0)
67
dicts = dictsForHat(team.hedgehog(0).Hat);
68
dict = dictContents(dicts[rand()%(dicts.size())]);
71
for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
73
if ((TypesHatnames[kind].size()) > 0)
75
HWHog hh = team.hedgehog(i);
76
hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())];
77
team.setHedgehog(i,hh);
80
// there is a chance that this hog has the same hat as the previous one
81
// let's reuse the hat-specific dict in this case
82
if ((i == 0) or (team.hedgehog(i).Hat != team.hedgehog(i-1).Hat))
84
dicts = dictsForHat(team.hedgehog(i).Hat);
85
dict = dictContents(dicts[rand()%(dicts.size())]);
88
// give each hedgehog a random name
89
HWNamegen::teamRandomName(team,i,dict);
94
void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber)
96
QStringList dicts = dictsForHat(team.hedgehog(HedgehogNumber).Hat);
98
QStringList dict = dictContents(dicts[rand()%(dicts.size())]);
100
teamRandomName(team, HedgehogNumber, dict);
103
void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber, const QStringList & dict)
105
QStringList namesDict = dict;
107
for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++)
109
namesDict.removeOne(team.hedgehog(i).Name);
112
// if our dict doesn't have any new names we'll have to use duplicates
113
if (namesDict.size() < 1)
116
HWHog hh = team.hedgehog(HedgehogNumber);
118
hh.Name = namesDict[rand()%(namesDict.size())];
120
team.setHedgehog(HedgehogNumber, hh);
123
QStringList HWNamegen::dictContents(const QString filename)
127
// find .txt to load the names from
128
QFile * file = new QFile(HWDataManager::instance().findFileForRead(QString(
129
"Names/%1.txt").arg(filename)));
131
if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
133
QTextStream in(file);
136
QString line = in.readLine();
142
// this QFile isn't needed any further
145
if (list.size() == 0)
146
list.append(filename);
152
QStringList HWNamegen::dictsForHat(const QString hatname)
156
// find .cfg to load the dicts from
157
QFile * file = new QFile(HWDataManager::instance().findFileForRead(QString(
158
"Names/%1.cfg").arg(hatname)));
160
if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
162
QTextStream in(file);
165
QString line = in.readLine();
171
// this QFile isn't needed any further
174
if (list.size() == 0)
175
list.append(QString("generic"));
180
// loades types from ini files. returns true on success.
181
bool HWNamegen::loadTypes()
183
typesAvailable = false;
185
// find .ini to load the names from
186
QFile * file = new QFile(
187
HWDataManager::instance().findFileForRead(QString("Names/types.ini")));
190
if (file->exists() && file->open(QIODevice::ReadOnly | QIODevice::Text))
193
int counter = 0; //counter starts with 0 (teamnames mode)
194
TypesTeamnames.append(QStringList());
195
TypesHatnames.append(QStringList());
197
QTextStream in(file);
200
QString line = in.readLine();
201
if (line == QString("#####"))
203
counter++; //toggle mode (teamnames || hats)
204
if ((counter%2) == 0)
206
TypesTeamnames.append(QStringList());
207
TypesHatnames.append(QStringList());
210
else if ((line == QString("*****")) || (line == QString("*END*")))
212
typesAvailable = true;
213
return true; // bye bye
217
if ((counter%2) == 0)
219
// even => teamnames mode
220
TypesTeamnames[(counter/2)].append(line);
225
TypesHatnames[((counter-1)/2)].append(line);
230
typesAvailable = true;
233
// this QFile isn't needed any further
236
return typesAvailable;
241
QString HWNamegen::getRandomGrave()
245
//list all available Graves
246
Graves.append(HWDataManager::instance().entryList(
250
).replaceInStrings(QRegExp("\\.png$"), "")
255
// TODO do some serious error handling
259
//pick a random grave
260
return Graves[rand()%(Graves.size())];
263
QString HWNamegen::getRandomFort()
267
//list all available Forts
268
Forts.append(HWDataManager::instance().entryList(
271
QStringList("*L.png")
272
).replaceInStrings(QRegExp("L\\.png$"), "")
277
// TODO do some serious error handling
282
return Forts[rand()%(Forts.size())];