author | unc0rr |
Sat, 18 Oct 2008 08:59:43 +0000 | |
changeset 1369 | f5b1b3fd70cc |
parent 1358 | 99ba722e5a09 |
child 1490 | 4eb4fc12cc30 |
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 |
{ |
|
306 | 160 |
switch(msg.at(1)) { |
184 | 161 |
case '?': { |
395 | 162 |
SendIPC("!"); |
184 | 163 |
break; |
164 |
} |
|
165 |
case 'C': { |
|
166 |
switch (gameType) { |
|
167 |
case gtLocal: { |
|
1358 | 168 |
SendConfig(); |
184 | 169 |
break; |
170 |
} |
|
171 |
case gtQLocal: { |
|
1358 | 172 |
SendQuickConfig(); |
184 | 173 |
break; |
174 |
} |
|
175 |
case gtDemo: break; |
|
176 |
case gtNet: { |
|
341 | 177 |
SendNetConfig(); |
184 | 178 |
break; |
179 |
} |
|
588 | 180 |
case gtTraining: { |
1358 | 181 |
SendTrainingConfig(); |
588 | 182 |
break; |
183 |
} |
|
184 | 184 |
} |
185 |
break; |
|
186 |
} |
|
187 |
case 'E': { |
|
660 | 188 |
int size = msg.size(); |
1277 | 189 |
emit ErrorMessage(QString("Last two engine messages:\n") + QString().append(msg.mid(2)).left(size - 4)); |
184 | 190 |
return; |
191 |
} |
|
208 | 192 |
case 'K': { |
193 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 194 |
if (kb==1) { |
468 | 195 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 196 |
return; |
197 |
} |
|
208 | 198 |
if (kb && kb <= KBmsgsCount) |
199 |
{ |
|
425 | 200 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 201 |
} |
202 |
return; |
|
203 |
} |
|
306 | 204 |
case 'i': { |
660 | 205 |
int size = msg.size(); |
206 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3).left(size - 5))); |
|
306 | 207 |
break; |
208 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
209 |
case 'Q': { |
533 | 210 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
211 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
212 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
213 |
case 'q': { |
533 | 214 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
215 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
216 |
} |
1356 | 217 |
case 's': { |
218 |
int size = msg.size(); |
|
1358 | 219 |
QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4)); |
220 |
emit SendChat(msgbody); |
|
221 |
QByteArray buf; |
|
222 |
HWProto::addStringToBuffer(buf, QString("s%1: %2\x20\x20").arg(config->netNick()).arg(msgbody)); |
|
223 |
demo.append(buf); |
|
224 |
break; |
|
1356 | 225 |
} |
184 | 226 |
default: { |
227 |
if (gameType == gtNet) |
|
228 |
{ |
|
229 |
emit SendNet(msg); |
|
230 |
} |
|
1358 | 231 |
if (msg.at(1) != 's') |
533 | 232 |
demo.append(msg); |
184 | 233 |
} |
234 |
} |
|
235 |
} |
|
236 |
||
237 |
void HWGame::FromNet(const QByteArray & msg) |
|
238 |
{ |
|
239 |
RawSendIPC(msg); |
|
240 |
} |
|
241 |
||
1356 | 242 |
void HWGame::FromNetChat(const QString & msg) |
243 |
{ |
|
244 |
QByteArray buf; |
|
245 |
HWProto::addStringToBuffer(buf, 's' + msg + "\x20\x20"); |
|
246 |
RawSendIPC(buf); |
|
247 |
} |
|
248 |
||
184 | 249 |
void HWGame::onClientRead() |
250 |
{ |
|
251 |
quint8 msglen; |
|
252 |
quint32 bufsize; |
|
406 | 253 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 254 |
((msglen = readbuffer.data()[0]) < bufsize)) |
255 |
{ |
|
256 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
257 |
readbuffer.remove(0, msglen + 1); |
|
258 |
ParseMessage(msg); |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
QStringList HWGame::setArguments() |
|
263 |
{ |
|
264 |
QStringList arguments; |
|
555 | 265 |
QRect resolution = config->vid_Resolution(); |
497 | 266 |
arguments << cfgdir->absolutePath(); |
555 | 267 |
arguments << QString::number(resolution.width()); |
268 |
arguments << QString::number(resolution.height()); |
|
759 | 269 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 270 |
arguments << QString("%1").arg(ipc_port); |
184 | 271 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
272 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
273 |
arguments << tr("en.txt"); |
|
296 | 274 |
arguments << "128"; // sound volume |
297 | 275 |
arguments << QString::number(config->timerInterval()); |
267 | 276 |
arguments << datadir->absolutePath(); |
297 | 277 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 278 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
949 | 279 |
arguments << config->netNick().toUtf8().toBase64(); |
1129 | 280 |
arguments << (config->isMusicEnabled() ? "1" : "0"); |
184 | 281 |
return arguments; |
282 |
} |
|
283 |
||
341 | 284 |
void HWGame::AddTeam(const QString & teamname) |
184 | 285 |
{ |
286 |
if (TeamCount == 5) return; |
|
287 |
teams[TeamCount] = teamname; |
|
288 |
TeamCount++; |
|
289 |
} |
|
290 |
||
291 |
void HWGame::PlayDemo(const QString & demofilename) |
|
292 |
{ |
|
293 |
gameType = gtDemo; |
|
294 |
QFile demofile(demofilename); |
|
295 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
296 |
{ |
|
425 | 297 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 298 |
return ; |
299 |
} |
|
300 |
||
301 |
// read demo |
|
512 | 302 |
toSendBuf = demofile.readAll(); |
184 | 303 |
|
304 |
// run engine |
|
533 | 305 |
demo.clear(); |
184 | 306 |
Start(); |
533 | 307 |
SetGameState(gsStarted); |
184 | 308 |
} |
309 |
||
310 |
void HWGame::StartNet() |
|
311 |
{ |
|
312 |
gameType = gtNet; |
|
533 | 313 |
demo.clear(); |
184 | 314 |
Start(); |
533 | 315 |
SetGameState(gsStarted); |
184 | 316 |
} |
317 |
||
318 |
void HWGame::StartLocal() |
|
319 |
{ |
|
320 |
gameType = gtLocal; |
|
533 | 321 |
demo.clear(); |
184 | 322 |
Start(); |
533 | 323 |
SetGameState(gsStarted); |
184 | 324 |
} |
325 |
||
326 |
void HWGame::StartQuick() |
|
327 |
{ |
|
328 |
gameType = gtQLocal; |
|
533 | 329 |
demo.clear(); |
184 | 330 |
Start(); |
533 | 331 |
SetGameState(gsStarted); |
184 | 332 |
} |
533 | 333 |
|
587 | 334 |
void HWGame::StartTraining() |
335 |
{ |
|
588 | 336 |
gameType = gtTraining; |
337 |
demo.clear(); |
|
338 |
Start(); |
|
339 |
SetGameState(gsStarted); |
|
587 | 340 |
} |
341 |
||
533 | 342 |
void HWGame::SetGameState(GameState state) |
343 |
{ |
|
344 |
gameState = state; |
|
345 |
emit GameStateChanged(state); |
|
346 |
} |