author | unc0rr |
Fri, 14 Sep 2007 20:51:40 +0000 | |
changeset 593 | 1f5e66379a43 |
parent 588 | b2e61fc49e5d |
child 595 | 5ee863f2f568 |
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 |
||
142 |
HWProto::addStringToBuffer(teamscfg, "eaddtrig 2147483649"); |
|
143 |
||
144 |
RawSendIPC(teamscfg); |
|
588 | 145 |
} |
146 |
||
341 | 147 |
void HWGame::SendNetConfig() |
148 |
{ |
|
149 |
commonConfig(); |
|
184 | 150 |
} |
151 |
||
152 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
153 |
{ |
|
306 | 154 |
switch(msg.at(1)) { |
184 | 155 |
case '?': { |
395 | 156 |
SendIPC("!"); |
184 | 157 |
break; |
158 |
} |
|
159 |
case 'C': { |
|
160 |
switch (gameType) { |
|
161 |
case gtLocal: { |
|
162 |
SendConfig(); |
|
163 |
break; |
|
164 |
} |
|
165 |
case gtQLocal: { |
|
166 |
SendQuickConfig(); |
|
167 |
break; |
|
168 |
} |
|
169 |
case gtDemo: break; |
|
170 |
case gtNet: { |
|
341 | 171 |
SendNetConfig(); |
184 | 172 |
break; |
173 |
} |
|
588 | 174 |
case gtTraining: { |
175 |
SendTrainingConfig(); |
|
176 |
break; |
|
177 |
} |
|
184 | 178 |
} |
179 |
break; |
|
180 |
} |
|
181 |
case 'E': { |
|
425 | 182 |
emit ErrorMessage(QString().append(msg.mid(2)).left(msg.size() - 6)); |
184 | 183 |
return; |
184 |
} |
|
208 | 185 |
case 'K': { |
186 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 187 |
if (kb==1) { |
468 | 188 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 189 |
return; |
190 |
} |
|
208 | 191 |
if (kb && kb <= KBmsgsCount) |
192 |
{ |
|
425 | 193 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 194 |
} |
195 |
return; |
|
196 |
} |
|
184 | 197 |
case '+': { |
198 |
if (gameType == gtNet) |
|
199 |
{ |
|
200 |
emit SendNet(msg); |
|
201 |
} |
|
202 |
break; |
|
203 |
} |
|
306 | 204 |
case 'i': { |
205 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
206 |
break; |
|
207 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
208 |
case 'Q': { |
533 | 209 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
210 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
211 |
} |
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(gsFinished); |
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 |
} |
184 | 216 |
default: { |
217 |
if (gameType == gtNet) |
|
218 |
{ |
|
219 |
emit SendNet(msg); |
|
220 |
} |
|
533 | 221 |
demo.append(msg); |
184 | 222 |
} |
223 |
} |
|
224 |
} |
|
225 |
||
226 |
void HWGame::FromNet(const QByteArray & msg) |
|
227 |
{ |
|
228 |
RawSendIPC(msg); |
|
229 |
} |
|
230 |
||
231 |
void HWGame::onClientRead() |
|
232 |
{ |
|
233 |
quint8 msglen; |
|
234 |
quint32 bufsize; |
|
406 | 235 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 236 |
((msglen = readbuffer.data()[0]) < bufsize)) |
237 |
{ |
|
238 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
239 |
readbuffer.remove(0, msglen + 1); |
|
240 |
ParseMessage(msg); |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
QStringList HWGame::setArguments() |
|
245 |
{ |
|
246 |
QStringList arguments; |
|
555 | 247 |
QRect resolution = config->vid_Resolution(); |
497 | 248 |
arguments << cfgdir->absolutePath(); |
555 | 249 |
arguments << QString::number(resolution.width()); |
250 |
arguments << QString::number(resolution.height()); |
|
296 | 251 |
arguments << "16"; // bpp |
291 | 252 |
arguments << QString("%1").arg(ipc_port); |
184 | 253 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
254 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
255 |
arguments << tr("en.txt"); |
|
296 | 256 |
arguments << "128"; // sound volume |
297 | 257 |
arguments << QString::number(config->timerInterval()); |
267 | 258 |
arguments << datadir->absolutePath(); |
297 | 259 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 260 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
184 | 261 |
return arguments; |
262 |
} |
|
263 |
||
341 | 264 |
void HWGame::AddTeam(const QString & teamname) |
184 | 265 |
{ |
266 |
if (TeamCount == 5) return; |
|
267 |
teams[TeamCount] = teamname; |
|
268 |
TeamCount++; |
|
269 |
} |
|
270 |
||
271 |
void HWGame::PlayDemo(const QString & demofilename) |
|
272 |
{ |
|
273 |
gameType = gtDemo; |
|
274 |
QFile demofile(demofilename); |
|
275 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
276 |
{ |
|
425 | 277 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 278 |
return ; |
279 |
} |
|
280 |
||
281 |
// read demo |
|
512 | 282 |
toSendBuf = demofile.readAll(); |
184 | 283 |
|
284 |
// run engine |
|
533 | 285 |
demo.clear(); |
184 | 286 |
Start(); |
533 | 287 |
SetGameState(gsStarted); |
184 | 288 |
} |
289 |
||
290 |
void HWGame::StartNet() |
|
291 |
{ |
|
292 |
gameType = gtNet; |
|
533 | 293 |
demo.clear(); |
184 | 294 |
Start(); |
533 | 295 |
SetGameState(gsStarted); |
184 | 296 |
} |
297 |
||
298 |
void HWGame::StartLocal() |
|
299 |
{ |
|
300 |
gameType = gtLocal; |
|
239 | 301 |
seed = gamecfg->getCurrentSeed(); |
533 | 302 |
demo.clear(); |
184 | 303 |
Start(); |
533 | 304 |
SetGameState(gsStarted); |
184 | 305 |
} |
306 |
||
307 |
void HWGame::StartQuick() |
|
308 |
{ |
|
309 |
gameType = gtQLocal; |
|
239 | 310 |
seed = gamecfg->getCurrentSeed(); |
533 | 311 |
demo.clear(); |
184 | 312 |
Start(); |
533 | 313 |
SetGameState(gsStarted); |
184 | 314 |
} |
533 | 315 |
|
587 | 316 |
void HWGame::StartTraining() |
317 |
{ |
|
588 | 318 |
gameType = gtTraining; |
319 |
seed = "training"; |
|
320 |
demo.clear(); |
|
321 |
Start(); |
|
322 |
SetGameState(gsStarted); |
|
587 | 323 |
} |
324 |
||
533 | 325 |
void HWGame::SetGameState(GameState state) |
326 |
{ |
|
327 |
gameState = state; |
|
328 |
emit GameStateChanged(state); |
|
329 |
} |