author | displacer |
Sun, 18 Feb 2007 12:20:36 +0000 | |
changeset 448 | e968e19a464c |
parent 425 | a7e1dabc8fb7 |
child 466 | bc45a038d47a |
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': { |
|
425 | 148 |
emit ErrorMessage(QString().append(msg.mid(2)).left(msg.size() - 6)); |
184 | 149 |
return; |
150 |
} |
|
208 | 151 |
case 'K': { |
152 |
ulong kb = msg.mid(2).toULong(); |
|
153 |
if (kb && kb <= KBmsgsCount) |
|
154 |
{ |
|
425 | 155 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 156 |
} |
157 |
return; |
|
158 |
} |
|
184 | 159 |
case '+': { |
160 |
if (gameType == gtNet) |
|
161 |
{ |
|
162 |
emit SendNet(msg); |
|
163 |
} |
|
164 |
break; |
|
165 |
} |
|
306 | 166 |
case 'i': { |
167 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
168 |
break; |
|
169 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
170 |
case 'Q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
171 |
emit GameStateChanged(gsInterrupted); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
172 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
173 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
174 |
case 'q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
175 |
emit GameStateChanged(gsFinished); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
176 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
177 |
} |
184 | 178 |
default: { |
179 |
if (gameType == gtNet) |
|
180 |
{ |
|
181 |
emit SendNet(msg); |
|
182 |
} |
|
183 |
demo->append(msg); |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
void HWGame::FromNet(const QByteArray & msg) |
|
189 |
{ |
|
190 |
RawSendIPC(msg); |
|
191 |
} |
|
192 |
||
193 |
void HWGame::onClientRead() |
|
194 |
{ |
|
195 |
quint8 msglen; |
|
196 |
quint32 bufsize; |
|
406 | 197 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 198 |
((msglen = readbuffer.data()[0]) < bufsize)) |
199 |
{ |
|
200 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
201 |
readbuffer.remove(0, msglen + 1); |
|
202 |
ParseMessage(msg); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
QStringList HWGame::setArguments() |
|
207 |
{ |
|
208 |
QStringList arguments; |
|
209 |
arguments << resolutions[0][config->vid_Resolution()]; |
|
210 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 211 |
arguments << "16"; // bpp |
291 | 212 |
arguments << QString("%1").arg(ipc_port); |
184 | 213 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
214 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
215 |
arguments << tr("en.txt"); |
|
296 | 216 |
arguments << "128"; // sound volume |
297 | 217 |
arguments << QString::number(config->timerInterval()); |
267 | 218 |
arguments << datadir->absolutePath(); |
297 | 219 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
184 | 220 |
return arguments; |
221 |
} |
|
222 |
||
341 | 223 |
void HWGame::AddTeam(const QString & teamname) |
184 | 224 |
{ |
225 |
if (TeamCount == 5) return; |
|
226 |
teams[TeamCount] = teamname; |
|
227 |
TeamCount++; |
|
228 |
} |
|
229 |
||
230 |
void HWGame::SaveDemo(const QString & filename) |
|
231 |
{ |
|
232 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
|
233 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
234 |
||
235 |
QFile demofile(filename); |
|
236 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
237 |
{ |
|
425 | 238 |
emit ErrorMessage(tr("Cannot save demo to file %1").arg(filename)); |
184 | 239 |
return ; |
240 |
} |
|
241 |
QDataStream stream(&demofile); |
|
242 |
stream.writeRawData(demo->constData(), demo->size()); |
|
243 |
demofile.close(); |
|
244 |
delete demo; |
|
379 | 245 |
demo=0; |
184 | 246 |
} |
247 |
||
248 |
void HWGame::PlayDemo(const QString & demofilename) |
|
249 |
{ |
|
250 |
gameType = gtDemo; |
|
251 |
QFile demofile(demofilename); |
|
252 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
253 |
{ |
|
425 | 254 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 255 |
return ; |
256 |
} |
|
257 |
||
258 |
// read demo |
|
259 |
QDataStream stream(&demofile); |
|
260 |
char buf[512]; |
|
261 |
int readbytes; |
|
262 |
do |
|
263 |
{ |
|
264 |
readbytes = stream.readRawData((char *)&buf, 512); |
|
265 |
toSendBuf.append(QByteArray((char *)&buf, readbytes)); |
|
266 |
//SendIPC(QByteArray((char *)&buf, readbytes)); |
|
267 |
||
268 |
} while (readbytes > 0); |
|
269 |
demofile.close(); |
|
270 |
||
271 |
// run engine |
|
272 |
demo = new QByteArray; |
|
273 |
Start(); |
|
306 | 274 |
emit GameStateChanged(gsStarted); |
184 | 275 |
} |
276 |
||
277 |
void HWGame::StartNet() |
|
278 |
{ |
|
279 |
gameType = gtNet; |
|
280 |
demo = new QByteArray; |
|
281 |
Start(); |
|
306 | 282 |
emit GameStateChanged(gsStarted); |
184 | 283 |
} |
284 |
||
285 |
void HWGame::StartLocal() |
|
286 |
{ |
|
287 |
gameType = gtLocal; |
|
239 | 288 |
seed = gamecfg->getCurrentSeed(); |
184 | 289 |
demo = new QByteArray; |
290 |
Start(); |
|
306 | 291 |
emit GameStateChanged(gsStarted); |
184 | 292 |
} |
293 |
||
294 |
void HWGame::StartQuick() |
|
295 |
{ |
|
296 |
gameType = gtQLocal; |
|
239 | 297 |
seed = gamecfg->getCurrentSeed(); |
184 | 298 |
demo = new QByteArray; |
299 |
Start(); |
|
306 | 300 |
emit GameStateChanged(gsStarted); |
184 | 301 |
} |