author | igorko |
Tue, 05 Apr 2011 10:59:23 -0400 | |
changeset 5101 | a2b72f966073 |
parent 4976 | 088d40d8aba2 |
child 5114 | a05081bc2769 |
permissions | -rw-r--r-- |
1907 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2009 Martin Minarik <ttsmj@pokec.sk> |
|
4976 | 4 |
* Copyright (c) 2009-2011 Andrey Korotaev <unC0Rr@gmail.com> |
1907 | 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 <QApplication> |
|
23 |
#include <QStringList> |
|
24 |
#include <QLineEdit> |
|
25 |
#include "namegen.h" |
|
26 |
#include "hwform.h" |
|
27 |
#include "hwconsts.h" |
|
28 |
||
29 |
||
30 |
HWNamegen::HWNamegen() : |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
31 |
TypesAvliable(false) |
1907 | 32 |
{ |
33 |
||
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
34 |
TypesLoad(); |
1907 | 35 |
} |
36 |
||
37 |
HWNamegen::~HWNamegen() |
|
38 |
{ |
|
39 |
} |
|
40 |
||
41 |
||
42 |
||
43 |
void HWNamegen::TeamRandomName(HWTeam*& team, const int &i) |
|
44 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
45 |
RandomNameByHat(team,i); |
1907 | 46 |
} |
47 |
||
48 |
void HWNamegen::TeamRandomNames(HWTeam*& team, const bool changeteamname) |
|
49 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
50 |
if ((TypesHatnames.size() > 0) && TypesAvliable){ |
1907 | 51 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
52 |
int kind = (rand()%(TypesHatnames.size())); |
1907 | 53 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
54 |
if (changeteamname){ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
55 |
if (TypesTeamnames[kind].size() > 0){ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
56 |
team->TeamName = TypesTeamnames[kind][rand()%(TypesTeamnames[kind].size())]; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
57 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
58 |
team->Grave = "Simple"; // Todo: make it semi-random |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
59 |
team->Fort = "Island"; // Todo: make it semi-random |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
60 |
team->Voicepack = "Default"; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
61 |
} |
1907 | 62 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
63 |
for(int i = 0; i < 8; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
64 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
65 |
if ((TypesHatnames[kind].size()) > 0){ |
3344 | 66 |
team->Hedgehogs[i].Hat = TypesHatnames[kind][rand()%(TypesHatnames[kind].size())]; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
67 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
68 |
RandomNameByHat(team,i); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
69 |
} |
1907 | 70 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
71 |
} |
1907 | 72 |
|
73 |
} |
|
74 |
||
75 |
||
76 |
void HWNamegen::RandomNameByHat(HWTeam*& team, const int &i) |
|
77 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
78 |
QStringList Dictionaries; |
3344 | 79 |
HatCfgLoad(team->Hedgehogs[i].Hat,Dictionaries); |
1907 | 80 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
81 |
QStringList Dictionary; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2192
diff
changeset
|
82 |
DictLoad(Dictionaries[rand()%(Dictionaries.size())],Dictionary); |
1907 | 83 |
|
3344 | 84 |
team->Hedgehogs[i].Name = Dictionary[rand()%(Dictionary.size())]; |
1907 | 85 |
} |
86 |
||
87 |
void HWNamegen::DictLoad(const QString filename, QStringList &list) |
|
88 |
{ |
|
89 |
list.clear(); |
|
90 |
||
91 |
QFile file(QString("%1/Names/%2.txt").arg(datadir->absolutePath()).arg(filename)); |
|
92 |
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
93 |
{ |
|
94 |
||
95 |
QTextStream in(&file); |
|
96 |
while (!in.atEnd()) { |
|
97 |
QString line = in.readLine(); |
|
98 |
if(line != QString("")) |
|
99 |
{list.append(line);} |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
if (list.size()==0) |
|
104 |
list.append(filename); |
|
105 |
||
106 |
} |
|
107 |
||
108 |
||
109 |
void HWNamegen::HatCfgLoad(const QString hatname, QStringList &list) |
|
110 |
{ |
|
111 |
list.clear(); |
|
112 |
||
113 |
QFile file(QString("%1/Names/%2.cfg").arg(datadir->absolutePath()).arg(hatname)); |
|
114 |
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
115 |
{ |
|
116 |
||
117 |
QTextStream in(&file); |
|
118 |
while (!in.atEnd()) { |
|
119 |
QString line = in.readLine(); |
|
120 |
if(line != QString("")) |
|
121 |
{list.append(line);} |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
if (list.size()==0) |
|
126 |
list.append(QString("generic")); |
|
127 |
||
128 |
} |
|
129 |
||
130 |
||
131 |
void HWNamegen::TypesLoad() |
|
132 |
{ |
|
133 |
||
134 |
QFile file(QString("%1/Names/types.ini").arg(datadir->absolutePath())); |
|
135 |
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
|
136 |
{TypesAvliable = FALSE; return;} |
|
137 |
||
138 |
int counter = 0; //counter starts with 0 (teamnames mode) |
|
139 |
TypesTeamnames.append(QStringList()); |
|
140 |
TypesHatnames.append(QStringList()); |
|
141 |
||
142 |
QTextStream in(&file); |
|
143 |
while (!in.atEnd()) { |
|
144 |
QString line = in.readLine(); |
|
145 |
if (line == QString("#####")){ |
|
146 |
counter++; //toggle mode (teamnames || hats) |
|
147 |
if ((counter%2) == 0){ |
|
148 |
TypesTeamnames.append(QStringList()); |
|
149 |
TypesHatnames.append(QStringList()); |
|
150 |
} |
|
151 |
} else if ((line == QString("*****")) || (line == QString("*END*"))){ |
|
152 |
TypesAvliable = TRUE; return; // bye bye |
|
153 |
} else { |
|
154 |
if ((counter%2) == 0){ // even => teamnames mode |
|
155 |
TypesTeamnames[(counter/2)].append(line); |
|
156 |
} else { // odd => hats mode |
|
157 |
TypesHatnames[((counter-1)/2)].append(line); |
|
158 |
} |
|
159 |
} |
|
160 |
// Types.append(line); |
|
161 |
} |
|
162 |
TypesAvliable = TRUE; |
|
163 |
return; |
|
164 |
} |
|
165 |
||
166 |