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 TypesAvliable(false) |
|
31 { |
|
32 |
|
33 TypesLoad(); |
|
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) && TypesAvliable){ |
|
50 |
|
51 int kind = (rand()%(TypesHatnames.size())); |
|
52 |
|
53 if (changeteamname){ |
|
54 if (TypesTeamnames[kind].size() > 0){ |
|
55 team->TeamName = TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]; |
|
56 } |
|
57 team->Grave = GetRandomGrave(); |
|
58 team->Fort = GetRandomFort(); |
|
59 team->Voicepack = "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 < 8; i++) |
|
65 { |
|
66 if ((TypesHatnames[kind].size()) > 0){ |
|
67 team->Hedgehogs[i].Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())]; |
|
68 } |
|
69 RandomNameByHat(team,i); |
|
70 } |
|
71 |
|
72 } |
|
73 |
|
74 } |
|
75 |
|
76 |
|
77 void HWNamegen::RandomNameByHat(HWTeam*& team, const int HedgehogNumber) |
|
78 { |
|
79 QStringList Dictionaries; |
|
80 HatCfgLoad(team->Hedgehogs[HedgehogNumber].Hat,Dictionaries); |
|
81 |
|
82 QStringList Dictionary; |
|
83 DictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary); |
|
84 |
|
85 team->Hedgehogs[HedgehogNumber].Name = Dictionary[rand()%(Dictionary.size())]; |
|
86 } |
|
87 |
|
88 void HWNamegen::DictLoad(const QString filename, QStringList &list) |
|
89 { |
|
90 list.clear(); |
|
91 |
|
92 QFile file; |
|
93 file.setFileName(QString("%1/Data/Names/%2.txt").arg(cfgdir->absolutePath()).arg(filename)); |
|
94 if (!file.exists()) file.setFileName(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename)); |
|
95 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
96 { |
|
97 |
|
98 QTextStream in(&file); |
|
99 while (!in.atEnd()) { |
|
100 QString line = in.readLine(); |
|
101 if(line != QString("")) |
|
102 {list.append(line);} |
|
103 } |
|
104 } |
|
105 |
|
106 if (list.size()==0) |
|
107 list.append(filename); |
|
108 |
|
109 } |
|
110 |
|
111 |
|
112 void HWNamegen::HatCfgLoad(const QString hatname, QStringList &list) |
|
113 { |
|
114 list.clear(); |
|
115 |
|
116 QFile file; |
|
117 file.setFileName(QString("%1/Data/Names/%2.cfg").arg(cfgdir->absolutePath()).arg(hatname)); |
|
118 if (!file.exists()) file.setFileName(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname)); |
|
119 if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
120 { |
|
121 |
|
122 QTextStream in(&file); |
|
123 while (!in.atEnd()) { |
|
124 QString line = in.readLine(); |
|
125 if(line != QString("")) |
|
126 {list.append(line);} |
|
127 } |
|
128 } |
|
129 |
|
130 if (list.size()==0) |
|
131 list.append(QString("generic")); |
|
132 |
|
133 } |
|
134 |
|
135 |
|
136 void HWNamegen::TypesLoad() |
|
137 { |
|
138 QFile file; |
|
139 file.setFileName(QString("%1/Data/Names/types.ini").arg(cfgdir->absolutePath())); |
|
140 if (!file.exists()) file.setFileName(QString("%1/Names/types.ini").arg(datadir->absolutePath())); |
|
141 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
142 {TypesAvliable = FALSE; return;} |
|
143 |
|
144 int counter = 0; //counter starts with 0 (teamnames mode) |
|
145 TypesTeamnames.append(QStringList()); |
|
146 TypesHatnames.append(QStringList()); |
|
147 |
|
148 QTextStream in(&file); |
|
149 while (!in.atEnd()) { |
|
150 QString line = in.readLine(); |
|
151 if (line == QString("#####")){ |
|
152 counter++; //toggle mode (teamnames || hats) |
|
153 if ((counter%2) == 0){ |
|
154 TypesTeamnames.append(QStringList()); |
|
155 TypesHatnames.append(QStringList()); |
|
156 } |
|
157 } else if ((line == QString("*****")) || (line == QString("*END*"))){ |
|
158 TypesAvliable = TRUE; return; // bye bye |
|
159 } else { |
|
160 if ((counter%2) == 0){ // even => teamnames mode |
|
161 TypesTeamnames[(counter/2)].append(line); |
|
162 } else { // odd => hats mode |
|
163 TypesHatnames[((counter-1)/2)].append(line); |
|
164 } |
|
165 } |
|
166 // Types.append(line); |
|
167 } |
|
168 TypesAvliable = TRUE; |
|
169 return; |
|
170 } |
|
171 |
|
172 |
|
173 |
|
174 QString HWNamegen::GetRandomGrave() |
|
175 { |
|
176 QStringList Graves; |
|
177 |
|
178 //list all available Graves |
|
179 QDir tmpdir; |
|
180 tmpdir.cd(cfgdir->absolutePath()); |
|
181 tmpdir.cd("Data/Graphics/Graves"); |
|
182 tmpdir.setFilter(QDir::Files); |
|
183 Graves.append(tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1")); |
|
184 |
|
185 tmpdir.cd(datadir->absolutePath()); |
|
186 tmpdir.cd("Graphics/Graves"); |
|
187 tmpdir.setFilter(QDir::Files); |
|
188 QStringList tmpList = tmpdir.entryList(QStringList("*.png")).replaceInStrings(QRegExp("^(.*)\\.png"), "\\1"); |
|
189 for (QStringList::Iterator it = tmpList.begin(); it != tmpList.end(); ++it) |
|
190 if (!Graves.contains(*it,Qt::CaseInsensitive)) Graves.append(*it); |
|
191 |
|
192 if(Graves.size()==0) |
|
193 { |
|
194 //do some serious error handling |
|
195 return "Error"; |
|
196 } |
|
197 |
|
198 //pick a random grave |
|
199 return Graves[rand()%(Graves.size())]; |
|
200 } |
|
201 |
|
202 QString HWNamegen::GetRandomFort() |
|
203 { |
|
204 QStringList Forts; |
|
205 |
|
206 //list all available Forts |
|
207 QDir tmpdir; |
|
208 tmpdir.cd(datadir->absolutePath()); |
|
209 tmpdir.cd("Forts"); |
|
210 tmpdir.setFilter(QDir::Files); |
|
211 Forts.append(tmpdir.entryList(QStringList("*L.png")).replaceInStrings(QRegExp("^(.*)L\\.png"), "\\1")); |
|
212 |
|
213 if(Forts.size()==0) |
|
214 { |
|
215 //do some serious error handling |
|
216 return "Error"; |
|
217 } |
|
218 |
|
219 //pick a random fort |
|
220 return Forts[rand()%(Forts.size())]; |
|
221 } |
|