author | unc0rr |
Wed, 16 Sep 2009 14:45:00 +0000 | |
changeset 2384 | 9098f0430b6b |
parent 2383 | cc7d1ad986c6 |
child 2392 | a55dbef5cf31 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2005-2008 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 <QString> |
|
20 |
#include <QByteArray> |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
21 |
#include <QUuid> |
184 | 22 |
|
23 |
#include "game.h" |
|
24 |
#include "hwconsts.h" |
|
25 |
#include "gameuiconfig.h" |
|
26 |
#include "gamecfgwidget.h" |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
27 |
#include "teamselect.h" |
210 | 28 |
#include "KB.h" |
239 | 29 |
#include "proto.h" |
184 | 30 |
|
706 | 31 |
#include <QTextStream> |
32 |
||
681 | 33 |
HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg, QString ammo, TeamSelWidget* pTeamSelWidget) : |
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
34 |
TCPBase(true), |
1904 | 35 |
ammostr(ammo), |
36 |
m_pTeamSelWidget(pTeamSelWidget) |
|
184 | 37 |
{ |
38 |
this->config = config; |
|
39 |
this->gamecfg = gamecfg; |
|
40 |
TeamCount = 0; |
|
41 |
} |
|
42 |
||
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
43 |
HWGame::~HWGame() |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
44 |
{ |
686 | 45 |
SetGameState(gsDestroyed); |
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
46 |
} |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
47 |
|
184 | 48 |
void HWGame::onClientDisconnect() |
49 |
{ |
|
541 | 50 |
switch (gameType) { |
1490 | 51 |
case gtDemo: break; |
541 | 52 |
case gtNet: |
53 |
emit HaveRecord(true, demo); |
|
54 |
break; |
|
55 |
default: |
|
56 |
if (gameState == gsInterrupted) emit HaveRecord(false, demo); |
|
57 |
else if (gameState == gsFinished) emit HaveRecord(true, demo); |
|
58 |
} |
|
533 | 59 |
SetGameState(gsStopped); |
184 | 60 |
} |
61 |
||
253 | 62 |
void HWGame::commonConfig() |
184 | 63 |
{ |
318 | 64 |
QByteArray buf; |
341 | 65 |
QString gt; |
66 |
switch (gameType) { |
|
67 |
case gtDemo: |
|
68 |
gt = "TD"; |
|
69 |
break; |
|
70 |
case gtNet: |
|
71 |
gt = "TN"; |
|
72 |
break; |
|
73 |
default: |
|
74 |
gt = "TL"; |
|
75 |
} |
|
76 |
HWProto::addStringToBuffer(buf, gt); |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
77 |
|
318 | 78 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 79 |
|
80 |
if (m_pTeamSelWidget) |
|
81 |
{ |
|
352 | 82 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 83 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
84 |
{ |
|
85 |
HWProto::addStringListToBuffer(buf, |
|
86 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
682 | 87 |
HWProto::addStringToBuffer(buf, QString("eammstore %1").arg(ammostr)); |
341 | 88 |
} |
89 |
} |
|
318 | 90 |
RawSendIPC(buf); |
253 | 91 |
} |
92 |
||
93 |
void HWGame::SendConfig() |
|
94 |
{ |
|
95 |
commonConfig(); |
|
184 | 96 |
} |
97 |
||
98 |
void HWGame::SendQuickConfig() |
|
99 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
100 |
QByteArray teamscfg; |
239 | 101 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
102 |
HWProto::addStringToBuffer(teamscfg, "TL"); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
103 |
HWProto::addStringToBuffer(teamscfg, QString("etheme %1") |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
104 |
.arg((Themes->size() > 0) ? Themes->at(rand() % Themes->size()) : "steel")); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
105 |
HWProto::addStringToBuffer(teamscfg, "eseed " + QUuid::createUuid().toString()); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
106 |
|
1907 | 107 |
HWNamegen namegen; |
239 | 108 |
|
1907 | 109 |
HWTeam * team1; |
110 |
team1 = new HWTeam; |
|
111 |
team1->difficulty = 0; |
|
112 |
team1->teamColor = *color1; |
|
113 |
team1->numHedgehogs = 4; |
|
114 |
namegen.TeamRandomNames(team1,TRUE); |
|
607 | 115 |
HWProto::addStringListToBuffer(teamscfg, |
1907 | 116 |
team1->TeamGameConfig(100)); |
117 |
||
118 |
HWTeam * team2; |
|
119 |
team2 = new HWTeam; |
|
120 |
team2->difficulty = 4; |
|
121 |
team2->teamColor = *color2; |
|
122 |
team2->numHedgehogs = 4; |
|
123 |
namegen.TeamRandomNames(team2,TRUE); |
|
124 |
HWProto::addStringListToBuffer(teamscfg, |
|
125 |
team2->TeamGameConfig(100)); |
|
607 | 126 |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1490
diff
changeset
|
127 |
HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1490
diff
changeset
|
128 |
HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore); |
607 | 129 |
RawSendIPC(teamscfg); |
341 | 130 |
} |
131 |
||
588 | 132 |
void HWGame::SendTrainingConfig() |
133 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
134 |
QByteArray traincfg; |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
135 |
HWProto::addStringToBuffer(traincfg, "TL"); |
588 | 136 |
|
2049
243d363546e7
Fix team name in training (broken by ttsmj's random teams patch)
unc0rr
parents:
1907
diff
changeset
|
137 |
HWTeam team1; |
588 | 138 |
team1.difficulty = 0; |
616 | 139 |
team1.teamColor = *color1; |
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
603
diff
changeset
|
140 |
team1.numHedgehogs = 1; |
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
141 |
HWProto::addStringListToBuffer(traincfg, |
588 | 142 |
team1.TeamGameConfig(100)); |
143 |
||
722
993faebbe546
- Use shotgun training as more attractive than the second one with bazooka
unc0rr
parents:
706
diff
changeset
|
144 |
QFile file(datadir->absolutePath() + "/Trainings/001_Shotgun.txt"); |
596 | 145 |
if(!file.open(QFile::ReadOnly)) |
146 |
{ |
|
147 |
emit ErrorMessage(tr("Error reading training config file")); |
|
148 |
return; |
|
149 |
} |
|
2377 | 150 |
|
596 | 151 |
QTextStream stream(&file); |
152 |
while(!stream.atEnd()) |
|
153 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
154 |
HWProto::addStringToBuffer(traincfg, "e" + stream.readLine()); |
596 | 155 |
} |
593 | 156 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
157 |
RawSendIPC(traincfg); |
588 | 158 |
} |
159 |
||
341 | 160 |
void HWGame::SendNetConfig() |
161 |
{ |
|
162 |
commonConfig(); |
|
184 | 163 |
} |
164 |
||
165 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
166 |
{ |
|
306 | 167 |
switch(msg.at(1)) { |
184 | 168 |
case '?': { |
395 | 169 |
SendIPC("!"); |
184 | 170 |
break; |
171 |
} |
|
172 |
case 'C': { |
|
173 |
switch (gameType) { |
|
174 |
case gtLocal: { |
|
1358 | 175 |
SendConfig(); |
184 | 176 |
break; |
177 |
} |
|
178 |
case gtQLocal: { |
|
1358 | 179 |
SendQuickConfig(); |
184 | 180 |
break; |
181 |
} |
|
182 |
case gtDemo: break; |
|
183 |
case gtNet: { |
|
341 | 184 |
SendNetConfig(); |
184 | 185 |
break; |
186 |
} |
|
588 | 187 |
case gtTraining: { |
1358 | 188 |
SendTrainingConfig(); |
588 | 189 |
break; |
190 |
} |
|
184 | 191 |
} |
192 |
break; |
|
193 |
} |
|
194 |
case 'E': { |
|
660 | 195 |
int size = msg.size(); |
1277 | 196 |
emit ErrorMessage(QString("Last two engine messages:\n") + QString().append(msg.mid(2)).left(size - 4)); |
184 | 197 |
return; |
198 |
} |
|
208 | 199 |
case 'K': { |
200 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 201 |
if (kb==1) { |
468 | 202 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 203 |
return; |
204 |
} |
|
208 | 205 |
if (kb && kb <= KBmsgsCount) |
206 |
{ |
|
425 | 207 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 208 |
} |
209 |
return; |
|
210 |
} |
|
306 | 211 |
case 'i': { |
2382 | 212 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
306 | 213 |
break; |
214 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
215 |
case 'Q': { |
533 | 216 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
217 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
218 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
219 |
case 'q': { |
533 | 220 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
221 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
222 |
} |
1356 | 223 |
case 's': { |
224 |
int size = msg.size(); |
|
1358 | 225 |
QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4)); |
226 |
emit SendChat(msgbody); |
|
227 |
QByteArray buf; |
|
228 |
HWProto::addStringToBuffer(buf, QString("s%1: %2\x20\x20").arg(config->netNick()).arg(msgbody)); |
|
229 |
demo.append(buf); |
|
230 |
break; |
|
1356 | 231 |
} |
184 | 232 |
default: { |
233 |
if (gameType == gtNet) |
|
234 |
{ |
|
235 |
emit SendNet(msg); |
|
236 |
} |
|
1358 | 237 |
if (msg.at(1) != 's') |
533 | 238 |
demo.append(msg); |
184 | 239 |
} |
240 |
} |
|
241 |
} |
|
242 |
||
243 |
void HWGame::FromNet(const QByteArray & msg) |
|
244 |
{ |
|
245 |
RawSendIPC(msg); |
|
246 |
} |
|
247 |
||
1356 | 248 |
void HWGame::FromNetChat(const QString & msg) |
249 |
{ |
|
250 |
QByteArray buf; |
|
251 |
HWProto::addStringToBuffer(buf, 's' + msg + "\x20\x20"); |
|
252 |
RawSendIPC(buf); |
|
253 |
} |
|
254 |
||
184 | 255 |
void HWGame::onClientRead() |
256 |
{ |
|
257 |
quint8 msglen; |
|
258 |
quint32 bufsize; |
|
406 | 259 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 260 |
((msglen = readbuffer.data()[0]) < bufsize)) |
261 |
{ |
|
262 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
263 |
readbuffer.remove(0, msglen + 1); |
|
264 |
ParseMessage(msg); |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
QStringList HWGame::setArguments() |
|
269 |
{ |
|
270 |
QStringList arguments; |
|
555 | 271 |
QRect resolution = config->vid_Resolution(); |
497 | 272 |
arguments << cfgdir->absolutePath(); |
555 | 273 |
arguments << QString::number(resolution.width()); |
274 |
arguments << QString::number(resolution.height()); |
|
759 | 275 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 276 |
arguments << QString("%1").arg(ipc_port); |
184 | 277 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
278 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
279 |
arguments << tr("en.txt"); |
|
1777 | 280 |
arguments << QString::number(config->volume()); // sound volume |
297 | 281 |
arguments << QString::number(config->timerInterval()); |
267 | 282 |
arguments << datadir->absolutePath(); |
297 | 283 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 284 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
949 | 285 |
arguments << config->netNick().toUtf8().toBase64(); |
1129 | 286 |
arguments << (config->isMusicEnabled() ? "1" : "0"); |
1812 | 287 |
arguments << (config->isReducedQuality() ? "1" : "0"); |
184 | 288 |
return arguments; |
289 |
} |
|
290 |
||
341 | 291 |
void HWGame::AddTeam(const QString & teamname) |
184 | 292 |
{ |
293 |
if (TeamCount == 5) return; |
|
294 |
teams[TeamCount] = teamname; |
|
295 |
TeamCount++; |
|
296 |
} |
|
297 |
||
298 |
void HWGame::PlayDemo(const QString & demofilename) |
|
299 |
{ |
|
300 |
gameType = gtDemo; |
|
301 |
QFile demofile(demofilename); |
|
302 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
303 |
{ |
|
425 | 304 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 305 |
return ; |
306 |
} |
|
307 |
||
308 |
// read demo |
|
512 | 309 |
toSendBuf = demofile.readAll(); |
184 | 310 |
|
311 |
// run engine |
|
533 | 312 |
demo.clear(); |
184 | 313 |
Start(); |
533 | 314 |
SetGameState(gsStarted); |
184 | 315 |
} |
316 |
||
317 |
void HWGame::StartNet() |
|
318 |
{ |
|
319 |
gameType = gtNet; |
|
533 | 320 |
demo.clear(); |
184 | 321 |
Start(); |
533 | 322 |
SetGameState(gsStarted); |
184 | 323 |
} |
324 |
||
325 |
void HWGame::StartLocal() |
|
326 |
{ |
|
327 |
gameType = gtLocal; |
|
533 | 328 |
demo.clear(); |
184 | 329 |
Start(); |
533 | 330 |
SetGameState(gsStarted); |
184 | 331 |
} |
332 |
||
333 |
void HWGame::StartQuick() |
|
334 |
{ |
|
335 |
gameType = gtQLocal; |
|
533 | 336 |
demo.clear(); |
184 | 337 |
Start(); |
533 | 338 |
SetGameState(gsStarted); |
184 | 339 |
} |
533 | 340 |
|
587 | 341 |
void HWGame::StartTraining() |
342 |
{ |
|
588 | 343 |
gameType = gtTraining; |
344 |
demo.clear(); |
|
345 |
Start(); |
|
346 |
SetGameState(gsStarted); |
|
587 | 347 |
} |
348 |
||
533 | 349 |
void HWGame::SetGameState(GameState state) |
350 |
{ |
|
351 |
gameState = state; |
|
352 |
emit GameStateChanged(state); |
|
353 |
} |