author | unc0rr |
Sat, 22 Sep 2007 10:45:18 +0000 | |
changeset 607 | 26a91a9f5514 |
parent 604 | 2f1165467a66 |
child 610 | 9b5a6200f667 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
2 |
* Hedgewars, a worms-like 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 <QMessageBox> |
|
20 |
#include <QProcess> |
|
21 |
#include <QTimer> |
|
22 |
#include <QString> |
|
23 |
#include <QByteArray> |
|
24 |
#include <QFile> |
|
25 |
#include <QTextStream> |
|
26 |
||
27 |
#include "game.h" |
|
28 |
#include "hwconsts.h" |
|
29 |
#include "gameuiconfig.h" |
|
30 |
#include "gamecfgwidget.h" |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
31 |
#include "teamselect.h" |
210 | 32 |
#include "KB.h" |
239 | 33 |
#include "proto.h" |
184 | 34 |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
35 |
HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg, TeamSelWidget* pTeamSelWidget) : |
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
36 |
TCPBase(true), |
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
37 |
m_pTeamSelWidget(pTeamSelWidget) |
184 | 38 |
{ |
39 |
this->config = config; |
|
40 |
this->gamecfg = gamecfg; |
|
41 |
TeamCount = 0; |
|
42 |
seed = ""; |
|
43 |
} |
|
44 |
||
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
45 |
HWGame::~HWGame() |
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 |
} |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
48 |
|
184 | 49 |
void HWGame::onClientDisconnect() |
50 |
{ |
|
541 | 51 |
switch (gameType) { |
52 |
case gtDemo: |
|
53 |
break; |
|
54 |
case gtNet: |
|
55 |
emit HaveRecord(true, demo); |
|
56 |
break; |
|
57 |
default: |
|
58 |
if (gameState == gsInterrupted) emit HaveRecord(false, demo); |
|
59 |
else if (gameState == gsFinished) emit HaveRecord(true, demo); |
|
60 |
} |
|
533 | 61 |
SetGameState(gsStopped); |
184 | 62 |
} |
63 |
||
253 | 64 |
void HWGame::commonConfig() |
184 | 65 |
{ |
318 | 66 |
QByteArray buf; |
341 | 67 |
QString gt; |
68 |
switch (gameType) { |
|
69 |
case gtDemo: |
|
70 |
gt = "TD"; |
|
71 |
break; |
|
72 |
case gtNet: |
|
73 |
gt = "TN"; |
|
74 |
break; |
|
75 |
default: |
|
76 |
gt = "TL"; |
|
77 |
} |
|
78 |
HWProto::addStringToBuffer(buf, gt); |
|
318 | 79 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 80 |
|
81 |
if (m_pTeamSelWidget) |
|
82 |
{ |
|
352 | 83 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 84 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
85 |
{ |
|
86 |
HWProto::addStringListToBuffer(buf, |
|
87 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
607 | 88 |
HWProto::addStringToBuffer(buf, *cDefaultAmmoStore); |
341 | 89 |
} |
90 |
} |
|
318 | 91 |
RawSendIPC(buf); |
253 | 92 |
} |
93 |
||
94 |
void HWGame::SendConfig() |
|
95 |
{ |
|
96 |
commonConfig(); |
|
184 | 97 |
} |
98 |
||
99 |
void HWGame::SendQuickConfig() |
|
100 |
{ |
|
253 | 101 |
commonConfig(); |
239 | 102 |
|
103 |
QByteArray teamscfg; |
|
104 |
HWTeam team1(0); |
|
105 |
team1.difficulty = 0; |
|
341 | 106 |
team1.teamColor = QColor(65535); |
107 |
team1.numHedgehogs = 4; |
|
312 | 108 |
HWProto::addStringListToBuffer(teamscfg, |
341 | 109 |
team1.TeamGameConfig(gamecfg->getInitHealth())); |
239 | 110 |
|
111 |
HWTeam team2(2); |
|
112 |
team2.difficulty = 4; |
|
341 | 113 |
team2.teamColor = QColor(16776960); |
114 |
team2.numHedgehogs = 4; |
|
607 | 115 |
HWProto::addStringListToBuffer(teamscfg, |
116 |
team2.TeamGameConfig(gamecfg->getInitHealth())); |
|
117 |
||
118 |
HWProto::addStringToBuffer(teamscfg, *cDefaultAmmoStore); |
|
119 |
HWProto::addStringToBuffer(teamscfg, *cDefaultAmmoStore); |
|
120 |
RawSendIPC(teamscfg); |
|
341 | 121 |
} |
122 |
||
588 | 123 |
void HWGame::SendTrainingConfig() |
124 |
{ |
|
125 |
QByteArray teamscfg; |
|
126 |
HWProto::addStringToBuffer(teamscfg, "TL"); |
|
127 |
||
128 |
HWTeam team1(0); |
|
129 |
team1.difficulty = 0; |
|
130 |
team1.teamColor = QColor(65535); |
|
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
603
diff
changeset
|
131 |
team1.numHedgehogs = 1; |
588 | 132 |
HWProto::addStringListToBuffer(teamscfg, |
133 |
team1.TeamGameConfig(100)); |
|
134 |
||
596 | 135 |
QFile file(datadir->absolutePath() + "/Trainings/001_Shotgun.txt"); |
136 |
if(!file.open(QFile::ReadOnly)) |
|
137 |
{ |
|
138 |
emit ErrorMessage(tr("Error reading training config file")); |
|
139 |
return; |
|
140 |
} |
|
141 |
||
142 |
QTextStream stream(&file); |
|
143 |
while(!stream.atEnd()) |
|
144 |
{ |
|
145 |
HWProto::addStringToBuffer(teamscfg, stream.readLine()); |
|
146 |
} |
|
593 | 147 |
|
148 |
RawSendIPC(teamscfg); |
|
588 | 149 |
} |
150 |
||
341 | 151 |
void HWGame::SendNetConfig() |
152 |
{ |
|
153 |
commonConfig(); |
|
184 | 154 |
} |
155 |
||
156 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
157 |
{ |
|
306 | 158 |
switch(msg.at(1)) { |
184 | 159 |
case '?': { |
395 | 160 |
SendIPC("!"); |
184 | 161 |
break; |
162 |
} |
|
163 |
case 'C': { |
|
164 |
switch (gameType) { |
|
165 |
case gtLocal: { |
|
166 |
SendConfig(); |
|
167 |
break; |
|
168 |
} |
|
169 |
case gtQLocal: { |
|
170 |
SendQuickConfig(); |
|
171 |
break; |
|
172 |
} |
|
173 |
case gtDemo: break; |
|
174 |
case gtNet: { |
|
341 | 175 |
SendNetConfig(); |
184 | 176 |
break; |
177 |
} |
|
588 | 178 |
case gtTraining: { |
179 |
SendTrainingConfig(); |
|
180 |
break; |
|
181 |
} |
|
184 | 182 |
} |
183 |
break; |
|
184 |
} |
|
185 |
case 'E': { |
|
425 | 186 |
emit ErrorMessage(QString().append(msg.mid(2)).left(msg.size() - 6)); |
184 | 187 |
return; |
188 |
} |
|
208 | 189 |
case 'K': { |
190 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 191 |
if (kb==1) { |
468 | 192 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 193 |
return; |
194 |
} |
|
208 | 195 |
if (kb && kb <= KBmsgsCount) |
196 |
{ |
|
425 | 197 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 198 |
} |
199 |
return; |
|
200 |
} |
|
184 | 201 |
case '+': { |
202 |
if (gameType == gtNet) |
|
203 |
{ |
|
204 |
emit SendNet(msg); |
|
205 |
} |
|
206 |
break; |
|
207 |
} |
|
306 | 208 |
case 'i': { |
209 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
210 |
break; |
|
211 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
212 |
case 'Q': { |
533 | 213 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
214 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
215 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
216 |
case 'q': { |
533 | 217 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
218 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
219 |
} |
184 | 220 |
default: { |
221 |
if (gameType == gtNet) |
|
222 |
{ |
|
223 |
emit SendNet(msg); |
|
224 |
} |
|
533 | 225 |
demo.append(msg); |
184 | 226 |
} |
227 |
} |
|
228 |
} |
|
229 |
||
230 |
void HWGame::FromNet(const QByteArray & msg) |
|
231 |
{ |
|
232 |
RawSendIPC(msg); |
|
233 |
} |
|
234 |
||
235 |
void HWGame::onClientRead() |
|
236 |
{ |
|
237 |
quint8 msglen; |
|
238 |
quint32 bufsize; |
|
406 | 239 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 240 |
((msglen = readbuffer.data()[0]) < bufsize)) |
241 |
{ |
|
242 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
243 |
readbuffer.remove(0, msglen + 1); |
|
244 |
ParseMessage(msg); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
QStringList HWGame::setArguments() |
|
249 |
{ |
|
250 |
QStringList arguments; |
|
555 | 251 |
QRect resolution = config->vid_Resolution(); |
497 | 252 |
arguments << cfgdir->absolutePath(); |
555 | 253 |
arguments << QString::number(resolution.width()); |
254 |
arguments << QString::number(resolution.height()); |
|
603 | 255 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 256 |
arguments << QString("%1").arg(ipc_port); |
184 | 257 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
258 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
259 |
arguments << tr("en.txt"); |
|
296 | 260 |
arguments << "128"; // sound volume |
297 | 261 |
arguments << QString::number(config->timerInterval()); |
267 | 262 |
arguments << datadir->absolutePath(); |
297 | 263 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 264 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
184 | 265 |
return arguments; |
266 |
} |
|
267 |
||
341 | 268 |
void HWGame::AddTeam(const QString & teamname) |
184 | 269 |
{ |
270 |
if (TeamCount == 5) return; |
|
271 |
teams[TeamCount] = teamname; |
|
272 |
TeamCount++; |
|
273 |
} |
|
274 |
||
275 |
void HWGame::PlayDemo(const QString & demofilename) |
|
276 |
{ |
|
277 |
gameType = gtDemo; |
|
278 |
QFile demofile(demofilename); |
|
279 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
280 |
{ |
|
425 | 281 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 282 |
return ; |
283 |
} |
|
284 |
||
285 |
// read demo |
|
512 | 286 |
toSendBuf = demofile.readAll(); |
184 | 287 |
|
288 |
// run engine |
|
533 | 289 |
demo.clear(); |
184 | 290 |
Start(); |
533 | 291 |
SetGameState(gsStarted); |
184 | 292 |
} |
293 |
||
294 |
void HWGame::StartNet() |
|
295 |
{ |
|
296 |
gameType = gtNet; |
|
533 | 297 |
demo.clear(); |
184 | 298 |
Start(); |
533 | 299 |
SetGameState(gsStarted); |
184 | 300 |
} |
301 |
||
302 |
void HWGame::StartLocal() |
|
303 |
{ |
|
304 |
gameType = gtLocal; |
|
239 | 305 |
seed = gamecfg->getCurrentSeed(); |
533 | 306 |
demo.clear(); |
184 | 307 |
Start(); |
533 | 308 |
SetGameState(gsStarted); |
184 | 309 |
} |
310 |
||
311 |
void HWGame::StartQuick() |
|
312 |
{ |
|
313 |
gameType = gtQLocal; |
|
239 | 314 |
seed = gamecfg->getCurrentSeed(); |
533 | 315 |
demo.clear(); |
184 | 316 |
Start(); |
533 | 317 |
SetGameState(gsStarted); |
184 | 318 |
} |
533 | 319 |
|
587 | 320 |
void HWGame::StartTraining() |
321 |
{ |
|
588 | 322 |
gameType = gtTraining; |
323 |
seed = "training"; |
|
324 |
demo.clear(); |
|
325 |
Start(); |
|
326 |
SetGameState(gsStarted); |
|
587 | 327 |
} |
328 |
||
533 | 329 |
void HWGame::SetGameState(GameState state) |
330 |
{ |
|
331 |
gameState = state; |
|
332 |
emit GameStateChanged(state); |
|
333 |
} |