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 typesAvailable(false) |
|
31 { |
|
32 |
|
33 loadTypes(); |
|
34 } |
|
35 |
|
36 HWNamegen::~HWNamegen() |
|
37 { |
|
38 } |
|
39 |
|
40 |
|
41 |
|
42 void HWNamegen::teamRandomName(HWTeam & team, const int HedgehogNumber) |
|
43 { |
|
44 randomNameByHat(team, HedgehogNumber); |
|
45 } |
|
46 |
|
47 void HWNamegen::teamRandomNames(HWTeam & team, const bool changeteamname) |
|
48 { |
|
49 if ((TypesHatnames.size() > 0) && typesAvailable){ |
|
50 |
|
51 int kind = (rand()%(TypesHatnames.size())); |
|
52 |
|
53 if (changeteamname){ |
|
54 if (TypesTeamnames[kind].size() > 0){ |
|
55 team.setName(TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]); |
|
56 } |
|
57 team.setGrave(getRandomGrave()); |
|
58 team.setFort(getRandomFort()); |
|
59 team.setVoicepack("Default"); |
|
60 } |
|
61 |
|
62 //give each hedgehog a random name: |
|
63 //TODO: load the dictionary only once! (right now it's loaded once for each hedgehog) |
|
64 for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++) |
|
65 { |
|
66 if ((TypesHatnames[kind].size()) > 0){ |
|
67 HWHog hh = team.hedgehog(i); |
|
68 hh.Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())]; |
|
69 team.setHedgehog(i,hh); |
|
70 } |
|
71 randomNameByHat(team,i); |
|
72 } |
|
73 |
|
74 } |
|
75 |
|
76 } |
|
77 |
|
78 |
|
79 void HWNamegen::randomNameByHat(HWTeam & team, const int HedgehogNumber) |
|
80 { |
|
81 QStringList Dictionaries; |
|
82 hatCfgLoad(team.hedgehog(HedgehogNumber).Hat,Dictionaries); |
|
83 |
|
84 QStringList Dictionary; |
|
85 dictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary); |
|
86 |
|
87 HWHog hh = team.hedgehog(HedgehogNumber); |
|
88 hh.Name = Dictionary[rand()%(Dictionary.size())]; |
|
89 team.setHedgehog(HedgehogNumber, hh); |
|
90 } |
|
91 |
|
92 void HWNamegen::dictLoad(const QString filename, QStringList &list) |
|
93 { |
|
94 list.clear(); |
|
95 |
|
96 QFile file; |
|
97 file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename)); |
|
98 if (!file.exists()) file.setFileName(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename)); |
|
99 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
100 { |
|
101 |
|
102 QTextStream in(&file); |
|
103 while (!in.atEnd()) { |
|
104 QString line = in.readLine(); |
|
105 if(line != QString("")) |
|
106 {list.append(line);} |
|
107 } |
|
108 } |
|
109 |
|
110 if (list.size()==0) |
|
111 list.append(filename); |
|
112 |
|
113 } |
|
114 |
|
115 |
|
116 void HWNamegen::hatCfgLoad(const QString hatname, QStringList &list) |
|
117 { |
|
118 list.clear(); |
|
119 |
|
120 QFile file; |
|
121 file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname)); |
|
122 if (!file.exists()) file.setFileName(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname)); |
|
123 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
124 { |
|
125 |
|
126 QTextStream in(&file); |
|
127 while (!in.atEnd()) { |
|
128 QString line = in.readLine(); |
|
129 if(line != QString("")) |
|
130 {list.append(line);} |
|
131 } |
|
132 } |
|
133 |
|
134 if (list.size()==0) |
|
135 list.append(QString("generic")); |
|
136 |
|
137 } |
|
138 |
|
139 |
|
140 void HWNamegen::loadTypes() |
|
141 { |
|
142 QFile file; |
|
143 file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath())); |
|
144 if (!file.exists()) file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath())); |
|
145 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
146 {typesAvailable = false; return;} |
|
147 |
|
148 int counter = 0; //counter starts with 0 (teamnames mode) |
|
149 TypesTeamnames.append(QStringList()); |
|
150 TypesHatnames.append(QStringList()); |
|
151 |
|
152 QTextStream in(&file); |
|
153 while (!in.atEnd()) { |
|
154 QString line = in.readLine(); |
|
155 if (line == QString("#####")){ |
|
156 counter++; //toggle mode (teamnames || hats) |
|
157 if ((counter%2) == 0){ |
|
158 TypesTeamnames.append(QStringList()); |
|
159 TypesHatnames.append(QStringList()); |
|
160 } |
|
161 } else if ((line == QString("*****")) || (line == QString("*END*"))){ |
|
162 typesAvailable = true; return; // bye bye |
|
163 } else { |
|
164 if ((counter%2) == 0){ // even => teamnames mode |
|
165 TypesTeamnames[(counter/2)].append(line); |
|
166 } else { // odd => hats mode |
|
167 TypesHatnames[((counter-1)/2)].append(line); |
|
168 } |
|
169 } |
|
170 // Types.append(line); |
|
171 } |
|
172 typesAvailable = true; |
|
173 return; |
|
174 } |
|
175 |
|
176 |
|
177 |
|
178 QString HWNamegen::getRandomGrave() |
|
179 { |
|
180 QStringList Graves; |
|
181 |
|
182 //list all available Graves |
|
183 QDir tmpdir; |
|
184 tmpdir.cd(cfgdir->absolutePath()); |
|
185 tmpdir.cd("Data/Graphics/Graves"); |
|
186 tmpdir.setFilter(QDir::Files); |
|
187 Graves.append(tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1")); |
|
188 |
|
189 tmpdir.cd(datadir->absolutePath()); |
|
190 tmpdir.cd("Graphics/Graves"); |
|
191 tmpdir.setFilter(QDir::Files); |
|
192 QStringList tmpList = tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1"); |
|
193 for (QStringList::Iterator it = tmpList.begin(); it != tmpList.end(); ++it) |
|
194 if (!Graves.contains(*it,Qt::CaseInsensitive)) Graves.append(*it); |
|
195 |
|
196 if(Graves.size()==0) |
|
197 { |
|
198 //do some serious error handling |
|
199 return "Error"; |
|
200 } |
|
201 |
|
202 //pick a random grave |
|
203 return Graves[rand()%(Graves.size())]; |
|
204 } |
|
205 |
|
206 QString HWNamegen::getRandomFort() |
|
207 { |
|
208 QStringList Forts; |
|
209 |
|
210 //list all available Forts |
|
211 QDir tmpdir; |
|
212 tmpdir.cd(datadir->absolutePath()); |
|
213 tmpdir.cd("Forts"); |
|
214 tmpdir.setFilter(QDir::Files); |
|
215 Forts.append(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1")); |
|
216 |
|
217 if(Forts.size()==0) |
|
218 { |
|
219 //do some serious error handling |
|
220 return "Error"; |
|
221 } |
|
222 |
|
223 //pick a random fort |
|
224 return Forts[rand()%(Forts.size())]; |
|
225 } |
|