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