author | unc0rr |
Tue, 18 Sep 2007 19:52:46 +0000 | |
changeset 600 | f6e5f4e122db |
parent 596 | 38bdde6a54c1 |
child 601 | 78a68cc4d846 |
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())); |
|
88 |
} |
|
89 |
} |
|
318 | 90 |
RawSendIPC(buf); |
253 | 91 |
} |
92 |
||
93 |
void HWGame::SendConfig() |
|
94 |
{ |
|
95 |
commonConfig(); |
|
184 | 96 |
} |
97 |
||
98 |
void HWGame::SendQuickConfig() |
|
99 |
{ |
|
253 | 100 |
commonConfig(); |
239 | 101 |
|
102 |
QByteArray teamscfg; |
|
103 |
HWTeam team1(0); |
|
104 |
team1.difficulty = 0; |
|
341 | 105 |
team1.teamColor = QColor(65535); |
106 |
team1.numHedgehogs = 4; |
|
312 | 107 |
HWProto::addStringListToBuffer(teamscfg, |
341 | 108 |
team1.TeamGameConfig(gamecfg->getInitHealth())); |
239 | 109 |
|
110 |
HWTeam team2(2); |
|
111 |
team2.difficulty = 4; |
|
341 | 112 |
team2.teamColor = QColor(16776960); |
113 |
team2.numHedgehogs = 4; |
|
312 | 114 |
RawSendIPC(HWProto::addStringListToBuffer(teamscfg, |
341 | 115 |
team2.TeamGameConfig(gamecfg->getInitHealth()))); |
116 |
} |
|
117 |
||
588 | 118 |
void HWGame::SendTrainingConfig() |
119 |
{ |
|
120 |
QByteArray teamscfg; |
|
121 |
HWProto::addStringToBuffer(teamscfg, "TL"); |
|
122 |
HWProto::addStringToBuffer(teamscfg, "eseed none"); |
|
123 |
HWProto::addStringToBuffer(teamscfg, "e$gmflags 0"); |
|
124 |
HWProto::addStringToBuffer(teamscfg, "e$turntime 60000"); |
|
125 |
HWProto::addStringToBuffer(teamscfg, "emap mushrooms"); |
|
126 |
HWProto::addStringToBuffer(teamscfg, "etheme avematan"); |
|
127 |
||
128 |
HWTeam team1(0); |
|
129 |
team1.difficulty = 0; |
|
130 |
team1.teamColor = QColor(65535); |
|
131 |
team1.numHedgehogs = 4; |
|
132 |
HWProto::addStringListToBuffer(teamscfg, |
|
133 |
team1.TeamGameConfig(100)); |
|
134 |
||
135 |
HWTeam team2(2); |
|
136 |
team2.difficulty = 4; |
|
137 |
team2.teamColor = QColor(16776960); |
|
138 |
team2.numHedgehogs = 4; |
|
593 | 139 |
HWProto::addStringListToBuffer(teamscfg, |
140 |
team2.TeamGameConfig(100)); |
|
141 |
||
596 | 142 |
QFile file(datadir->absolutePath() + "/Trainings/001_Shotgun.txt"); |
143 |
if(!file.open(QFile::ReadOnly)) |
|
144 |
{ |
|
145 |
emit ErrorMessage(tr("Error reading training config file")); |
|
146 |
return; |
|
147 |
} |
|
148 |
||
149 |
QTextStream stream(&file); |
|
150 |
while(!stream.atEnd()) |
|
151 |
{ |
|
152 |
HWProto::addStringToBuffer(teamscfg, stream.readLine()); |
|
153 |
} |
|
593 | 154 |
|
155 |
RawSendIPC(teamscfg); |
|
588 | 156 |
} |
157 |
||
341 | 158 |
void HWGame::SendNetConfig() |
159 |
{ |
|
160 |
commonConfig(); |
|
184 | 161 |
} |
162 |
||
163 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
164 |
{ |
|
306 | 165 |
switch(msg.at(1)) { |
184 | 166 |
case '?': { |
395 | 167 |
SendIPC("!"); |
184 | 168 |
break; |
169 |
} |
|
170 |
case 'C': { |
|
171 |
switch (gameType) { |
|
172 |
case gtLocal: { |
|
173 |
SendConfig(); |
|
174 |
break; |
|
175 |
} |
|
176 |
case gtQLocal: { |
|
177 |
SendQuickConfig(); |
|
178 |
break; |
|
179 |
} |
|
180 |
case gtDemo: break; |
|
181 |
case gtNet: { |
|
341 | 182 |
SendNetConfig(); |
184 | 183 |
break; |
184 |
} |
|
588 | 185 |
case gtTraining: { |
186 |
SendTrainingConfig(); |
|
187 |
break; |
|
188 |
} |
|
184 | 189 |
} |
190 |
break; |
|
191 |
} |
|
192 |
case 'E': { |
|
425 | 193 |
emit ErrorMessage(QString().append(msg.mid(2)).left(msg.size() - 6)); |
184 | 194 |
return; |
195 |
} |
|
208 | 196 |
case 'K': { |
197 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 198 |
if (kb==1) { |
468 | 199 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 200 |
return; |
201 |
} |
|
208 | 202 |
if (kb && kb <= KBmsgsCount) |
203 |
{ |
|
425 | 204 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 205 |
} |
206 |
return; |
|
207 |
} |
|
184 | 208 |
case '+': { |
209 |
if (gameType == gtNet) |
|
210 |
{ |
|
211 |
emit SendNet(msg); |
|
212 |
} |
|
213 |
break; |
|
214 |
} |
|
306 | 215 |
case 'i': { |
216 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
217 |
break; |
|
218 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
219 |
case 'Q': { |
533 | 220 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
221 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
222 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
223 |
case 'q': { |
533 | 224 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
225 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
226 |
} |
184 | 227 |
default: { |
228 |
if (gameType == gtNet) |
|
229 |
{ |
|
230 |
emit SendNet(msg); |
|
231 |
} |
|
533 | 232 |
demo.append(msg); |
184 | 233 |
} |
234 |
} |
|
235 |
} |
|
236 |
||
237 |
void HWGame::FromNet(const QByteArray & msg) |
|
238 |
{ |
|
239 |
RawSendIPC(msg); |
|
240 |
} |
|
241 |
||
242 |
void HWGame::onClientRead() |
|
243 |
{ |
|
244 |
quint8 msglen; |
|
245 |
quint32 bufsize; |
|
406 | 246 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 247 |
((msglen = readbuffer.data()[0]) < bufsize)) |
248 |
{ |
|
249 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
250 |
readbuffer.remove(0, msglen + 1); |
|
251 |
ParseMessage(msg); |
|
252 |
} |
|
253 |
} |
|
254 |
||
255 |
QStringList HWGame::setArguments() |
|
256 |
{ |
|
257 |
QStringList arguments; |
|
555 | 258 |
QRect resolution = config->vid_Resolution(); |
497 | 259 |
arguments << cfgdir->absolutePath(); |
555 | 260 |
arguments << QString::number(resolution.width()); |
261 |
arguments << QString::number(resolution.height()); |
|
296 | 262 |
arguments << "16"; // bpp |
291 | 263 |
arguments << QString("%1").arg(ipc_port); |
184 | 264 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
265 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
266 |
arguments << tr("en.txt"); |
|
296 | 267 |
arguments << "128"; // sound volume |
297 | 268 |
arguments << QString::number(config->timerInterval()); |
267 | 269 |
arguments << datadir->absolutePath(); |
297 | 270 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 271 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
184 | 272 |
return arguments; |
273 |
} |
|
274 |
||
341 | 275 |
void HWGame::AddTeam(const QString & teamname) |
184 | 276 |
{ |
277 |
if (TeamCount == 5) return; |
|
278 |
teams[TeamCount] = teamname; |
|
279 |
TeamCount++; |
|
280 |
} |
|
281 |
||
282 |
void HWGame::PlayDemo(const QString & demofilename) |
|
283 |
{ |
|
284 |
gameType = gtDemo; |
|
285 |
QFile demofile(demofilename); |
|
286 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
287 |
{ |
|
425 | 288 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 289 |
return ; |
290 |
} |
|
291 |
||
292 |
// read demo |
|
512 | 293 |
toSendBuf = demofile.readAll(); |
184 | 294 |
|
295 |
// run engine |
|
533 | 296 |
demo.clear(); |
184 | 297 |
Start(); |
533 | 298 |
SetGameState(gsStarted); |
184 | 299 |
} |
300 |
||
301 |
void HWGame::StartNet() |
|
302 |
{ |
|
303 |
gameType = gtNet; |
|
533 | 304 |
demo.clear(); |
184 | 305 |
Start(); |
533 | 306 |
SetGameState(gsStarted); |
184 | 307 |
} |
308 |
||
309 |
void HWGame::StartLocal() |
|
310 |
{ |
|
311 |
gameType = gtLocal; |
|
239 | 312 |
seed = gamecfg->getCurrentSeed(); |
533 | 313 |
demo.clear(); |
184 | 314 |
Start(); |
533 | 315 |
SetGameState(gsStarted); |
184 | 316 |
} |
317 |
||
318 |
void HWGame::StartQuick() |
|
319 |
{ |
|
320 |
gameType = gtQLocal; |
|
239 | 321 |
seed = gamecfg->getCurrentSeed(); |
533 | 322 |
demo.clear(); |
184 | 323 |
Start(); |
533 | 324 |
SetGameState(gsStarted); |
184 | 325 |
} |
533 | 326 |
|
587 | 327 |
void HWGame::StartTraining() |
328 |
{ |
|
588 | 329 |
gameType = gtTraining; |
330 |
seed = "training"; |
|
331 |
demo.clear(); |
|
332 |
Start(); |
|
333 |
SetGameState(gsStarted); |
|
587 | 334 |
} |
335 |
||
533 | 336 |
void HWGame::SetGameState(GameState state) |
337 |
{ |
|
338 |
gameState = state; |
|
339 |
emit GameStateChanged(state); |
|
340 |
} |