author | unc0rr |
Fri, 14 Dec 2007 16:13:35 +0000 | |
changeset 660 | 7c155e849602 |
parent 622 | cedd47d956ec |
child 673 | bd82104a28af |
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; |
|
616 | 106 |
team1.teamColor = *color1; |
341 | 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; |
|
616 | 113 |
team2.teamColor = *color2; |
341 | 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; |
|
616 | 130 |
team1.teamColor = *color1; |
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 |
||
622 | 135 |
QFile file(datadir->absolutePath() + "/Trainings/002_Bazooka.txt"); |
596 | 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 |
{ |
|
610 | 145 |
HWProto::addStringToBuffer(teamscfg, "e" + stream.readLine()); |
596 | 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': { |
|
660 | 186 |
int size = msg.size(); |
187 |
emit ErrorMessage(QString().append(msg.mid(2)).left(size - 4)); |
|
184 | 188 |
return; |
189 |
} |
|
208 | 190 |
case 'K': { |
191 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 192 |
if (kb==1) { |
468 | 193 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 194 |
return; |
195 |
} |
|
208 | 196 |
if (kb && kb <= KBmsgsCount) |
197 |
{ |
|
425 | 198 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 199 |
} |
200 |
return; |
|
201 |
} |
|
184 | 202 |
case '+': { |
203 |
if (gameType == gtNet) |
|
204 |
{ |
|
205 |
emit SendNet(msg); |
|
206 |
} |
|
207 |
break; |
|
208 |
} |
|
306 | 209 |
case 'i': { |
660 | 210 |
int size = msg.size(); |
211 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3).left(size - 5))); |
|
306 | 212 |
break; |
213 |
} |
|
324
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(gsInterrupted); |
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 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
218 |
case 'q': { |
533 | 219 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
220 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
221 |
} |
184 | 222 |
default: { |
223 |
if (gameType == gtNet) |
|
224 |
{ |
|
225 |
emit SendNet(msg); |
|
226 |
} |
|
533 | 227 |
demo.append(msg); |
184 | 228 |
} |
229 |
} |
|
230 |
} |
|
231 |
||
232 |
void HWGame::FromNet(const QByteArray & msg) |
|
233 |
{ |
|
234 |
RawSendIPC(msg); |
|
235 |
} |
|
236 |
||
237 |
void HWGame::onClientRead() |
|
238 |
{ |
|
239 |
quint8 msglen; |
|
240 |
quint32 bufsize; |
|
406 | 241 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 242 |
((msglen = readbuffer.data()[0]) < bufsize)) |
243 |
{ |
|
244 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
245 |
readbuffer.remove(0, msglen + 1); |
|
246 |
ParseMessage(msg); |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
QStringList HWGame::setArguments() |
|
251 |
{ |
|
252 |
QStringList arguments; |
|
555 | 253 |
QRect resolution = config->vid_Resolution(); |
497 | 254 |
arguments << cfgdir->absolutePath(); |
555 | 255 |
arguments << QString::number(resolution.width()); |
256 |
arguments << QString::number(resolution.height()); |
|
603 | 257 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 258 |
arguments << QString("%1").arg(ipc_port); |
184 | 259 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
260 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
261 |
arguments << tr("en.txt"); |
|
296 | 262 |
arguments << "128"; // sound volume |
297 | 263 |
arguments << QString::number(config->timerInterval()); |
267 | 264 |
arguments << datadir->absolutePath(); |
297 | 265 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 266 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
184 | 267 |
return arguments; |
268 |
} |
|
269 |
||
341 | 270 |
void HWGame::AddTeam(const QString & teamname) |
184 | 271 |
{ |
272 |
if (TeamCount == 5) return; |
|
273 |
teams[TeamCount] = teamname; |
|
274 |
TeamCount++; |
|
275 |
} |
|
276 |
||
277 |
void HWGame::PlayDemo(const QString & demofilename) |
|
278 |
{ |
|
279 |
gameType = gtDemo; |
|
280 |
QFile demofile(demofilename); |
|
281 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
282 |
{ |
|
425 | 283 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 284 |
return ; |
285 |
} |
|
286 |
||
287 |
// read demo |
|
512 | 288 |
toSendBuf = demofile.readAll(); |
184 | 289 |
|
290 |
// run engine |
|
533 | 291 |
demo.clear(); |
184 | 292 |
Start(); |
533 | 293 |
SetGameState(gsStarted); |
184 | 294 |
} |
295 |
||
296 |
void HWGame::StartNet() |
|
297 |
{ |
|
298 |
gameType = gtNet; |
|
533 | 299 |
demo.clear(); |
184 | 300 |
Start(); |
533 | 301 |
SetGameState(gsStarted); |
184 | 302 |
} |
303 |
||
304 |
void HWGame::StartLocal() |
|
305 |
{ |
|
306 |
gameType = gtLocal; |
|
239 | 307 |
seed = gamecfg->getCurrentSeed(); |
533 | 308 |
demo.clear(); |
184 | 309 |
Start(); |
533 | 310 |
SetGameState(gsStarted); |
184 | 311 |
} |
312 |
||
313 |
void HWGame::StartQuick() |
|
314 |
{ |
|
315 |
gameType = gtQLocal; |
|
239 | 316 |
seed = gamecfg->getCurrentSeed(); |
533 | 317 |
demo.clear(); |
184 | 318 |
Start(); |
533 | 319 |
SetGameState(gsStarted); |
184 | 320 |
} |
533 | 321 |
|
587 | 322 |
void HWGame::StartTraining() |
323 |
{ |
|
588 | 324 |
gameType = gtTraining; |
325 |
seed = "training"; |
|
326 |
demo.clear(); |
|
327 |
Start(); |
|
328 |
SetGameState(gsStarted); |
|
587 | 329 |
} |
330 |
||
533 | 331 |
void HWGame::SetGameState(GameState state) |
332 |
{ |
|
333 |
gameState = state; |
|
334 |
emit GameStateChanged(state); |
|
335 |
} |