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