author | unc0rr |
Mon, 26 Jan 2009 15:53:43 +0000 | |
changeset 1760 | 55a1edd97911 |
parent 1576 | a02353129a41 |
child 1777 | 88674c291331 |
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) { |
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 |
|
239 | 107 |
HWTeam team1(0); |
108 |
team1.difficulty = 0; |
|
616 | 109 |
team1.teamColor = *color1; |
341 | 110 |
team1.numHedgehogs = 4; |
312 | 111 |
HWProto::addStringListToBuffer(teamscfg, |
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
112 |
team1.TeamGameConfig(100)); |
239 | 113 |
|
114 |
HWTeam team2(2); |
|
115 |
team2.difficulty = 4; |
|
616 | 116 |
team2.teamColor = *color2; |
341 | 117 |
team2.numHedgehogs = 4; |
607 | 118 |
HWProto::addStringListToBuffer(teamscfg, |
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
119 |
team2.TeamGameConfig(100)); |
607 | 120 |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1490
diff
changeset
|
121 |
HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1490
diff
changeset
|
122 |
HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore); |
607 | 123 |
RawSendIPC(teamscfg); |
341 | 124 |
} |
125 |
||
588 | 126 |
void HWGame::SendTrainingConfig() |
127 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
128 |
QByteArray traincfg; |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
129 |
HWProto::addStringToBuffer(traincfg, "TL"); |
588 | 130 |
|
131 |
HWTeam team1(0); |
|
132 |
team1.difficulty = 0; |
|
616 | 133 |
team1.teamColor = *color1; |
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
603
diff
changeset
|
134 |
team1.numHedgehogs = 1; |
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
135 |
HWProto::addStringListToBuffer(traincfg, |
588 | 136 |
team1.TeamGameConfig(100)); |
137 |
||
722
993faebbe546
- Use shotgun training as more attractive than the second one with bazooka
unc0rr
parents:
706
diff
changeset
|
138 |
QFile file(datadir->absolutePath() + "/Trainings/001_Shotgun.txt"); |
596 | 139 |
if(!file.open(QFile::ReadOnly)) |
140 |
{ |
|
141 |
emit ErrorMessage(tr("Error reading training config file")); |
|
142 |
return; |
|
143 |
} |
|
144 |
||
145 |
QTextStream stream(&file); |
|
146 |
while(!stream.atEnd()) |
|
147 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
148 |
HWProto::addStringToBuffer(traincfg, "e" + stream.readLine()); |
596 | 149 |
} |
593 | 150 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
151 |
RawSendIPC(traincfg); |
588 | 152 |
} |
153 |
||
341 | 154 |
void HWGame::SendNetConfig() |
155 |
{ |
|
156 |
commonConfig(); |
|
184 | 157 |
} |
158 |
||
159 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
160 |
{ |
|
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 |
} |