author | displacer |
Thu, 08 Feb 2007 21:11:58 +0000 | |
changeset 420 | 6cdfc07dceed |
parent 419 | fdeed9718e6b |
child 425 | a7e1dabc8fb7 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
|
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 |
#include <QUuid> |
|
341 | 27 |
#include <QDebug> |
184 | 28 |
|
29 |
#include "game.h" |
|
30 |
#include "hwconsts.h" |
|
31 |
#include "gameuiconfig.h" |
|
32 |
#include "gamecfgwidget.h" |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
33 |
#include "teamselect.h" |
210 | 34 |
#include "KB.h" |
239 | 35 |
#include "proto.h" |
184 | 36 |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
37 |
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
|
38 |
TCPBase(true), |
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
39 |
m_pTeamSelWidget(pTeamSelWidget) |
184 | 40 |
{ |
41 |
this->config = config; |
|
42 |
this->gamecfg = gamecfg; |
|
43 |
TeamCount = 0; |
|
44 |
seed = ""; |
|
45 |
} |
|
46 |
||
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
47 |
HWGame::~HWGame() |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
48 |
{ |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
49 |
} |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
50 |
|
184 | 51 |
void HWGame::onClientDisconnect() |
52 |
{ |
|
271 | 53 |
SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_" + cProtoVer); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
54 |
emit GameStateChanged(gsStopped); |
184 | 55 |
} |
56 |
||
253 | 57 |
void HWGame::commonConfig() |
184 | 58 |
{ |
318 | 59 |
QByteArray buf; |
341 | 60 |
QString gt; |
61 |
switch (gameType) { |
|
62 |
case gtDemo: |
|
63 |
gt = "TD"; |
|
64 |
break; |
|
65 |
case gtNet: |
|
66 |
gt = "TN"; |
|
67 |
break; |
|
68 |
default: |
|
69 |
gt = "TL"; |
|
70 |
} |
|
71 |
HWProto::addStringToBuffer(buf, gt); |
|
72 |
qDebug() << "game type" << gt; |
|
318 | 73 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 74 |
qDebug() << "config" << gamecfg->getFullConfig(); |
75 |
||
76 |
if (m_pTeamSelWidget) |
|
77 |
{ |
|
78 |
qDebug() << "adding teams from teamselwidget..."; |
|
352 | 79 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 80 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
81 |
{ |
|
82 |
qDebug() << "a team..."; |
|
352 | 83 |
//(*it).numHedgehogs = 4; |
341 | 84 |
HWProto::addStringListToBuffer(buf, |
85 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
86 |
} |
|
87 |
} |
|
318 | 88 |
RawSendIPC(buf); |
253 | 89 |
} |
90 |
||
91 |
void HWGame::SendConfig() |
|
92 |
{ |
|
93 |
commonConfig(); |
|
184 | 94 |
} |
95 |
||
96 |
void HWGame::SendQuickConfig() |
|
97 |
{ |
|
253 | 98 |
commonConfig(); |
239 | 99 |
|
100 |
QByteArray teamscfg; |
|
101 |
HWTeam team1(0); |
|
102 |
team1.difficulty = 0; |
|
341 | 103 |
team1.teamColor = QColor(65535); |
104 |
team1.numHedgehogs = 4; |
|
312 | 105 |
HWProto::addStringListToBuffer(teamscfg, |
341 | 106 |
team1.TeamGameConfig(gamecfg->getInitHealth())); |
239 | 107 |
|
108 |
HWTeam team2(2); |
|
109 |
team2.difficulty = 4; |
|
341 | 110 |
team2.teamColor = QColor(16776960); |
111 |
team2.numHedgehogs = 4; |
|
312 | 112 |
RawSendIPC(HWProto::addStringListToBuffer(teamscfg, |
341 | 113 |
team2.TeamGameConfig(gamecfg->getInitHealth()))); |
114 |
} |
|
115 |
||
116 |
void HWGame::SendNetConfig() |
|
117 |
{ |
|
118 |
commonConfig(); |
|
184 | 119 |
} |
120 |
||
121 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
122 |
{ |
|
306 | 123 |
switch(msg.at(1)) { |
184 | 124 |
case '?': { |
395 | 125 |
SendIPC("!"); |
184 | 126 |
break; |
127 |
} |
|
128 |
case 'C': { |
|
341 | 129 |
qDebug("ASK Config"); |
184 | 130 |
switch (gameType) { |
131 |
case gtLocal: { |
|
132 |
SendConfig(); |
|
133 |
break; |
|
134 |
} |
|
135 |
case gtQLocal: { |
|
136 |
SendQuickConfig(); |
|
137 |
break; |
|
138 |
} |
|
139 |
case gtDemo: break; |
|
140 |
case gtNet: { |
|
341 | 141 |
SendNetConfig(); |
184 | 142 |
break; |
143 |
} |
|
144 |
} |
|
145 |
break; |
|
146 |
} |
|
147 |
case 'E': { |
|
148 |
QMessageBox::critical(0, |
|
149 |
"Hedgewars: error message", |
|
150 |
QString().append(msg.mid(2)).left(msg.size() - 6), |
|
151 |
QMessageBox::Ok, |
|
152 |
QMessageBox::NoButton, |
|
153 |
QMessageBox::NoButton); |
|
154 |
return; |
|
155 |
} |
|
208 | 156 |
case 'K': { |
157 |
ulong kb = msg.mid(2).toULong(); |
|
158 |
if (kb && kb <= KBmsgsCount) |
|
159 |
{ |
|
160 |
QMessageBox::information(0, |
|
161 |
"Hedgewars: information", |
|
162 |
KBMessages[kb - 1], |
|
163 |
QMessageBox::Ok, |
|
164 |
QMessageBox::NoButton, |
|
165 |
QMessageBox::NoButton); |
|
166 |
} |
|
167 |
return; |
|
168 |
} |
|
184 | 169 |
case '+': { |
170 |
if (gameType == gtNet) |
|
171 |
{ |
|
172 |
emit SendNet(msg); |
|
173 |
} |
|
174 |
break; |
|
175 |
} |
|
306 | 176 |
case 'i': { |
177 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
178 |
break; |
|
179 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
180 |
case 'Q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
181 |
emit GameStateChanged(gsInterrupted); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
182 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
183 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
184 |
case 'q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
185 |
emit GameStateChanged(gsFinished); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
186 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
187 |
} |
184 | 188 |
default: { |
189 |
if (gameType == gtNet) |
|
190 |
{ |
|
191 |
emit SendNet(msg); |
|
192 |
} |
|
193 |
demo->append(msg); |
|
194 |
} |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
void HWGame::FromNet(const QByteArray & msg) |
|
199 |
{ |
|
200 |
RawSendIPC(msg); |
|
201 |
} |
|
202 |
||
203 |
void HWGame::onClientRead() |
|
204 |
{ |
|
205 |
quint8 msglen; |
|
206 |
quint32 bufsize; |
|
406 | 207 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 208 |
((msglen = readbuffer.data()[0]) < bufsize)) |
209 |
{ |
|
210 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
211 |
readbuffer.remove(0, msglen + 1); |
|
212 |
ParseMessage(msg); |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
QStringList HWGame::setArguments() |
|
217 |
{ |
|
218 |
QStringList arguments; |
|
219 |
arguments << resolutions[0][config->vid_Resolution()]; |
|
220 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 221 |
arguments << "16"; // bpp |
291 | 222 |
arguments << QString("%1").arg(ipc_port); |
184 | 223 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
224 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
225 |
arguments << tr("en.txt"); |
|
296 | 226 |
arguments << "128"; // sound volume |
297 | 227 |
arguments << QString::number(config->timerInterval()); |
267 | 228 |
arguments << datadir->absolutePath(); |
297 | 229 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
184 | 230 |
return arguments; |
231 |
} |
|
232 |
||
341 | 233 |
void HWGame::AddTeam(const QString & teamname) |
184 | 234 |
{ |
235 |
if (TeamCount == 5) return; |
|
236 |
teams[TeamCount] = teamname; |
|
237 |
TeamCount++; |
|
238 |
} |
|
239 |
||
240 |
void HWGame::SaveDemo(const QString & filename) |
|
241 |
{ |
|
242 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
|
243 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
244 |
||
245 |
QFile demofile(filename); |
|
246 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
247 |
{ |
|
248 |
QMessageBox::critical(0, |
|
249 |
tr("Error"), |
|
250 |
tr("Cannot save demo to file %1").arg(filename), |
|
251 |
tr("Quit")); |
|
252 |
return ; |
|
253 |
} |
|
254 |
QDataStream stream(&demofile); |
|
255 |
stream.writeRawData(demo->constData(), demo->size()); |
|
256 |
demofile.close(); |
|
257 |
delete demo; |
|
379 | 258 |
demo=0; |
184 | 259 |
} |
260 |
||
261 |
void HWGame::PlayDemo(const QString & demofilename) |
|
262 |
{ |
|
263 |
gameType = gtDemo; |
|
264 |
QFile demofile(demofilename); |
|
265 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
266 |
{ |
|
267 |
QMessageBox::critical(0, |
|
268 |
tr("Error"), |
|
269 |
tr("Cannot open demofile %1").arg(demofilename), |
|
270 |
tr("Quit")); |
|
271 |
return ; |
|
272 |
} |
|
273 |
||
274 |
// read demo |
|
275 |
QDataStream stream(&demofile); |
|
276 |
char buf[512]; |
|
277 |
int readbytes; |
|
278 |
do |
|
279 |
{ |
|
280 |
readbytes = stream.readRawData((char *)&buf, 512); |
|
281 |
toSendBuf.append(QByteArray((char *)&buf, readbytes)); |
|
282 |
//SendIPC(QByteArray((char *)&buf, readbytes)); |
|
283 |
||
284 |
} while (readbytes > 0); |
|
285 |
demofile.close(); |
|
286 |
||
287 |
// run engine |
|
288 |
demo = new QByteArray; |
|
289 |
Start(); |
|
306 | 290 |
emit GameStateChanged(gsStarted); |
184 | 291 |
} |
292 |
||
293 |
void HWGame::StartNet() |
|
294 |
{ |
|
295 |
gameType = gtNet; |
|
296 |
demo = new QByteArray; |
|
297 |
Start(); |
|
306 | 298 |
emit GameStateChanged(gsStarted); |
184 | 299 |
} |
300 |
||
301 |
void HWGame::StartLocal() |
|
302 |
{ |
|
303 |
gameType = gtLocal; |
|
239 | 304 |
seed = gamecfg->getCurrentSeed(); |
184 | 305 |
demo = new QByteArray; |
306 |
Start(); |
|
306 | 307 |
emit GameStateChanged(gsStarted); |
184 | 308 |
} |
309 |
||
310 |
void HWGame::StartQuick() |
|
311 |
{ |
|
312 |
gameType = gtQLocal; |
|
239 | 313 |
seed = gamecfg->getCurrentSeed(); |
184 | 314 |
demo = new QByteArray; |
315 |
Start(); |
|
306 | 316 |
emit GameStateChanged(gsStarted); |
184 | 317 |
} |