author | unc0rr |
Tue, 23 Sep 2008 06:27:53 +0000 | |
changeset 1286 | a02a5345b91e |
parent 1245 | d2eca4a053f5 |
child 1287 | 4a0cbcbe3521 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
486 | 3 |
* Copyright (c) 2005-2007 Andrey Korotaev <unC0Rr@gmail.com> |
184 | 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> |
|
471 | 22 |
#include <QStringList> |
23 |
#include <QLineEdit> |
|
184 | 24 |
#include "team.h" |
25 |
#include "hwform.h" |
|
26 |
#include "predefteams.h" |
|
27 |
#include "pages.h" |
|
28 |
#include "hwconsts.h" |
|
1240 | 29 |
#include "hats.h" |
314 | 30 |
|
352 | 31 |
HWTeam::HWTeam(const QString & teamname, unsigned int netID) : |
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
336
diff
changeset
|
32 |
difficulty(0), |
353 | 33 |
numHedgehogs(4), |
352 | 34 |
m_netID(netID) |
184 | 35 |
{ |
36 |
TeamName = teamname; |
|
245 | 37 |
OldTeamName = TeamName; |
1240 | 38 |
for (int i = 0; i < 8; i++) |
39 |
{ |
|
40 |
HHName[i].sprintf("hedgehog %d", i); |
|
41 |
HHHat[i] = "NoHat"; |
|
42 |
} |
|
184 | 43 |
Grave = "Simple"; |
44 |
Fort = "Barrelhouse"; |
|
45 |
for(int i = 0; i < BINDS_NUMBER; i++) |
|
46 |
{ |
|
47 |
binds[i].action = cbinds[i].action; |
|
48 |
binds[i].strbind = cbinds[i].strbind; |
|
49 |
} |
|
50 |
} |
|
51 |
||
353 | 52 |
HWTeam::HWTeam(const QStringList& strLst) : |
53 |
numHedgehogs(4) |
|
314 | 54 |
{ |
1245
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
55 |
// net teams are configured from QStringList |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
56 |
if(strLst.size() < 21) throw HWTeamConstructException(); |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
57 |
TeamName=strLst[0]; |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
58 |
m_netID=strLst[1].toUInt(); |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
59 |
Grave=strLst[2]; |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
60 |
Fort=strLst[3]; |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
61 |
difficulty=strLst[4].toUInt(); |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
62 |
for(int i = 0; i < 8; i++) |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
63 |
{ |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
64 |
HHName[i]=strLst[i * 2 + 5]; |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
65 |
HHHat[i]=strLst[i * 2 + 6]; |
d2eca4a053f5
Send hats info via net. Hats implementation complete now.
unc0rr
parents:
1244
diff
changeset
|
66 |
} |
314 | 67 |
} |
68 |
||
231 | 69 |
HWTeam::HWTeam(quint8 num) : |
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
336
diff
changeset
|
70 |
difficulty(0), |
353 | 71 |
numHedgehogs(4), |
352 | 72 |
m_netID(0) |
184 | 73 |
{ |
74 |
num %= PREDEFTEAMS_COUNT; |
|
75 |
TeamName = QApplication::translate("teams", pteams[num].TeamName); |
|
76 |
HHName[0] = QApplication::translate("teams", pteams[num].hh0name); |
|
77 |
HHName[1] = QApplication::translate("teams", pteams[num].hh1name); |
|
78 |
HHName[2] = QApplication::translate("teams", pteams[num].hh2name); |
|
79 |
HHName[3] = QApplication::translate("teams", pteams[num].hh3name); |
|
80 |
HHName[4] = QApplication::translate("teams", pteams[num].hh4name); |
|
81 |
HHName[5] = QApplication::translate("teams", pteams[num].hh5name); |
|
82 |
HHName[6] = QApplication::translate("teams", pteams[num].hh6name); |
|
83 |
HHName[7] = QApplication::translate("teams", pteams[num].hh7name); |
|
84 |
Grave = pteams[num].Grave; |
|
85 |
Fort = pteams[num].Fort; |
|
86 |
for(int i = 0; i < BINDS_NUMBER; i++) |
|
87 |
{ |
|
88 |
binds[i].action = cbinds[i].action; |
|
89 |
binds[i].strbind = cbinds[i].strbind; |
|
90 |
} |
|
91 |
} |
|
92 |
||
93 |
||
94 |
bool HWTeam::LoadFromFile() |
|
95 |
{ |
|
353 | 96 |
numHedgehogs=4; |
184 | 97 |
QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg"); |
98 |
if (!cfgfile.open(QIODevice::ReadOnly)) return false; |
|
99 |
QTextStream stream(&cfgfile); |
|
100 |
stream.setCodec("UTF-8"); |
|
101 |
QString str; |
|
102 |
QString action; |
|
103 |
||
104 |
while (!stream.atEnd()) |
|
105 |
{ |
|
106 |
str = stream.readLine(); |
|
107 |
if (str.startsWith(";")) continue; |
|
245 | 108 |
/*if (str.startsWith("name team ")) |
184 | 109 |
{ |
110 |
str.remove(0, 10); |
|
111 |
TeamName = str; |
|
245 | 112 |
} else*/ |
184 | 113 |
if (str.startsWith("name hh")) |
114 |
{ |
|
115 |
str.remove(0, 7); |
|
116 |
long i = str.left(1).toLong(); |
|
117 |
if ((i < 0) || (i > 7)) continue; |
|
118 |
str.remove(0, 2); |
|
119 |
HHName[i] = str; |
|
120 |
} else |
|
1240 | 121 |
if (str.startsWith("hat")) |
122 |
{ |
|
123 |
str.remove(0, 3); |
|
124 |
long i = str.left(1).toLong(); |
|
125 |
if ((i < 0) || (i > 7)) continue; |
|
126 |
str.remove(0, 2); |
|
127 |
HHHat[i] = str; |
|
128 |
} else |
|
184 | 129 |
if (str.startsWith("grave ")) |
130 |
{ |
|
131 |
str.remove(0, 6); |
|
132 |
Grave = str; |
|
133 |
} else |
|
134 |
if (str.startsWith("fort ")) |
|
135 |
{ |
|
136 |
str.remove(0, 5); |
|
137 |
Fort = str; |
|
138 |
} else |
|
139 |
if (str.startsWith("bind ")) |
|
140 |
{ |
|
141 |
str.remove(0, 5); |
|
142 |
action = str.section(' ', 1); |
|
143 |
str = str.section(' ', 0, 0); |
|
144 |
str.truncate(15); |
|
145 |
for (int i = 0; i < BINDS_NUMBER; i++) |
|
146 |
if (action == binds[i].action) |
|
147 |
{ |
|
148 |
binds[i].strbind = str; |
|
149 |
break; |
|
150 |
} |
|
239 | 151 |
} else |
231 | 152 |
if (str.startsWith("difficulty ")) |
153 |
{ |
|
154 |
str.remove(0, 11); |
|
155 |
difficulty=str.toUInt(); |
|
156 |
if (difficulty>5) difficulty=0; // this shouldn't normally happen |
|
184 | 157 |
} |
158 |
} |
|
159 |
cfgfile.close(); |
|
160 |
return true; |
|
161 |
} |
|
162 |
||
163 |
bool HWTeam::SaveToFile() |
|
164 |
{ |
|
245 | 165 |
if (OldTeamName != TeamName) |
166 |
{ |
|
167 |
QFile cfgfile(cfgdir->absolutePath() + "/" + OldTeamName + ".cfg"); |
|
168 |
cfgfile.remove(); |
|
169 |
OldTeamName = TeamName; |
|
170 |
} |
|
184 | 171 |
QFile cfgfile(cfgdir->absolutePath() + "/" + TeamName + ".cfg"); |
172 |
if (!cfgfile.open(QIODevice::WriteOnly)) return false; |
|
173 |
QTextStream stream(&cfgfile); |
|
174 |
stream.setCodec("UTF-8"); |
|
175 |
stream << "; Generated by Hedgewars, do not modify" << endl; |
|
176 |
stream << "name team " << TeamName << endl; |
|
177 |
for (int i = 0; i < 8; i++) |
|
1240 | 178 |
{ |
184 | 179 |
stream << "name hh" << i << " " << HHName[i] << endl; |
1240 | 180 |
stream << "hat" << i << " " << HHHat[i] << endl; |
181 |
} |
|
184 | 182 |
stream << "grave " << Grave << endl; |
183 |
stream << "fort " << Fort << endl; |
|
184 |
for(int i = 0; i < BINDS_NUMBER; i++) |
|
185 |
{ |
|
186 |
stream << "bind " << binds[i].strbind << " " << binds[i].action << endl; |
|
187 |
} |
|
231 | 188 |
stream << "difficulty " << difficulty << endl; |
184 | 189 |
cfgfile.close(); |
190 |
return true; |
|
191 |
} |
|
192 |
||
193 |
void HWTeam::SetToPage(HWForm * hwform) |
|
194 |
{ |
|
195 |
hwform->ui.pageEditTeam->TeamNameEdit->setText(TeamName); |
|
336 | 196 |
hwform->ui.pageEditTeam->CBTeamLvl->setCurrentIndex(difficulty); |
197 |
hwform->ui.pageEditTeam->CBTeamLvl_activated(difficulty); |
|
184 | 198 |
for(int i = 0; i < 8; i++) |
199 |
{ |
|
200 |
hwform->ui.pageEditTeam->HHNameEdit[i]->setText(HHName[i]); |
|
1240 | 201 |
hwform->ui.pageEditTeam->HHHats[i]->setCurrentIndex(hwform->ui.pageEditTeam->HHHats[i]->findData(HHHat[i], Qt::DisplayRole)); |
184 | 202 |
} |
203 |
hwform->ui.pageEditTeam->CBGrave->setCurrentIndex(hwform->ui.pageEditTeam->CBGrave->findText(Grave)); |
|
204 |
hwform->ui.pageEditTeam->CBGrave_activated(Grave); |
|
205 |
||
206 |
hwform->ui.pageEditTeam->CBFort->setCurrentIndex(hwform->ui.pageEditTeam->CBFort->findText(Fort)); |
|
207 |
hwform->ui.pageEditTeam->CBFort_activated(Fort); |
|
208 |
||
209 |
for(int i = 0; i < BINDS_NUMBER; i++) |
|
210 |
{ |
|
211 |
hwform->ui.pageEditTeam->CBBind[i]->setCurrentIndex(hwform->ui.pageEditTeam->CBBind[i]->findText(binds[i].strbind)); |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
void HWTeam::GetFromPage(HWForm * hwform) |
|
216 |
{ |
|
217 |
TeamName = hwform->ui.pageEditTeam->TeamNameEdit->text(); |
|
336 | 218 |
difficulty = hwform->ui.pageEditTeam->CBTeamLvl->currentIndex(); |
184 | 219 |
for(int i = 0; i < 8; i++) |
220 |
{ |
|
221 |
HHName[i] = hwform->ui.pageEditTeam->HHNameEdit[i]->text(); |
|
1240 | 222 |
HHHat[i] = hwform->ui.pageEditTeam->HHHats[i]->currentText(); |
184 | 223 |
} |
224 |
||
225 |
Grave = hwform->ui.pageEditTeam->CBGrave->currentText(); |
|
226 |
Fort = hwform->ui.pageEditTeam->CBFort->currentText(); |
|
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
245
diff
changeset
|
227 |
for(int i = 0; i < BINDS_NUMBER; i++) |
184 | 228 |
{ |
229 |
binds[i].strbind = hwform->ui.pageEditTeam->CBBind[i]->currentText(); |
|
230 |
} |
|
231 |
} |
|
232 |
||
341 | 233 |
QStringList HWTeam::TeamGameConfig(quint32 InitHealth) const |
184 | 234 |
{ |
239 | 235 |
QStringList sl; |
605 | 236 |
sl.push_back(QString("eaddteam %1 %2").arg(teamColor.rgb() & 0xffffff).arg(TeamName)); |
606
9d800fdfd3bd
Add ammostores in frontend, should help further ammostores implemetation
unc0rr
parents:
605
diff
changeset
|
237 |
|
352 | 238 |
if (m_netID) |
341 | 239 |
sl.push_back("erdriven"); |
240 |
||
241 |
sl.push_back(QString("egrave " + Grave)); |
|
242 |
sl.push_back(QString("efort " + Fort)); |
|
243 |
||
352 | 244 |
if (!m_netID) |
341 | 245 |
for(int i = 0; i < BINDS_NUMBER; i++) |
246 |
sl.push_back(QString("ebind " + binds[i].strbind + " " + binds[i].action)); |
|
247 |
||
248 |
for (int t = 0; t < numHedgehogs; t++) |
|
1242 | 249 |
{ |
605 | 250 |
sl.push_back(QString("eaddhh %1 %2 %3") |
319 | 251 |
.arg(QString::number(difficulty), |
605 | 252 |
QString::number(InitHealth), |
253 |
HHName[t])); |
|
1242 | 254 |
sl.push_back(QString("ehat %1") |
255 |
.arg(HHHat[t])); |
|
256 |
} |
|
239 | 257 |
return sl; |
184 | 258 |
} |
259 |
||
352 | 260 |
bool HWTeam::isNetTeam() const |
261 |
{ |
|
262 |
return m_netID!=0; |
|
263 |
} |
|
264 |
||
265 |
unsigned int HWTeam::getNetID() const |
|
266 |
{ |
|
267 |
return m_netID; |
|
268 |
} |
|
269 |
||
184 | 270 |
bool HWTeam::operator==(const HWTeam& t1) const { |
352 | 271 |
return TeamName==t1.TeamName && m_netID==t1.m_netID; |
184 | 272 |
} |
273 |
||
274 |
bool HWTeam::operator<(const HWTeam& t1) const { |
|
352 | 275 |
return m_netID<t1.m_netID || TeamName<t1.TeamName; // if names are equal - test if it is net team |
184 | 276 |
} |