author | displacer |
Fri, 23 Feb 2007 13:15:04 +0000 | |
changeset 484 | 3583f6289eee |
parent 468 | 8403d6884707 |
child 486 | 7ea71cd3acd5 |
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> |
|
466 | 27 |
#include <QTextStream> |
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); |
|
318 | 72 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 73 |
|
74 |
if (m_pTeamSelWidget) |
|
75 |
{ |
|
352 | 76 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 77 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
78 |
{ |
|
79 |
HWProto::addStringListToBuffer(buf, |
|
80 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
81 |
} |
|
82 |
} |
|
318 | 83 |
RawSendIPC(buf); |
253 | 84 |
} |
85 |
||
86 |
void HWGame::SendConfig() |
|
87 |
{ |
|
88 |
commonConfig(); |
|
184 | 89 |
} |
90 |
||
91 |
void HWGame::SendQuickConfig() |
|
92 |
{ |
|
253 | 93 |
commonConfig(); |
239 | 94 |
|
95 |
QByteArray teamscfg; |
|
96 |
HWTeam team1(0); |
|
97 |
team1.difficulty = 0; |
|
341 | 98 |
team1.teamColor = QColor(65535); |
99 |
team1.numHedgehogs = 4; |
|
312 | 100 |
HWProto::addStringListToBuffer(teamscfg, |
341 | 101 |
team1.TeamGameConfig(gamecfg->getInitHealth())); |
239 | 102 |
|
103 |
HWTeam team2(2); |
|
104 |
team2.difficulty = 4; |
|
341 | 105 |
team2.teamColor = QColor(16776960); |
106 |
team2.numHedgehogs = 4; |
|
312 | 107 |
RawSendIPC(HWProto::addStringListToBuffer(teamscfg, |
341 | 108 |
team2.TeamGameConfig(gamecfg->getInitHealth()))); |
109 |
} |
|
110 |
||
111 |
void HWGame::SendNetConfig() |
|
112 |
{ |
|
113 |
commonConfig(); |
|
184 | 114 |
} |
115 |
||
116 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
117 |
{ |
|
306 | 118 |
switch(msg.at(1)) { |
184 | 119 |
case '?': { |
395 | 120 |
SendIPC("!"); |
184 | 121 |
break; |
122 |
} |
|
123 |
case 'C': { |
|
124 |
switch (gameType) { |
|
125 |
case gtLocal: { |
|
126 |
SendConfig(); |
|
127 |
break; |
|
128 |
} |
|
129 |
case gtQLocal: { |
|
130 |
SendQuickConfig(); |
|
131 |
break; |
|
132 |
} |
|
133 |
case gtDemo: break; |
|
134 |
case gtNet: { |
|
341 | 135 |
SendNetConfig(); |
184 | 136 |
break; |
137 |
} |
|
138 |
} |
|
139 |
break; |
|
140 |
} |
|
141 |
case 'E': { |
|
425 | 142 |
emit ErrorMessage(QString().append(msg.mid(2)).left(msg.size() - 6)); |
184 | 143 |
return; |
144 |
} |
|
208 | 145 |
case 'K': { |
146 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 147 |
if (kb==1) { |
468 | 148 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 149 |
return; |
150 |
} |
|
208 | 151 |
if (kb && kb <= KBmsgsCount) |
152 |
{ |
|
425 | 153 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 154 |
} |
155 |
return; |
|
156 |
} |
|
184 | 157 |
case '+': { |
158 |
if (gameType == gtNet) |
|
159 |
{ |
|
160 |
emit SendNet(msg); |
|
161 |
} |
|
162 |
break; |
|
163 |
} |
|
306 | 164 |
case 'i': { |
165 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
166 |
break; |
|
167 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
168 |
case 'Q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
169 |
emit GameStateChanged(gsInterrupted); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
170 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
171 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
172 |
case 'q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
173 |
emit GameStateChanged(gsFinished); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
174 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
175 |
} |
184 | 176 |
default: { |
177 |
if (gameType == gtNet) |
|
178 |
{ |
|
179 |
emit SendNet(msg); |
|
180 |
} |
|
181 |
demo->append(msg); |
|
182 |
} |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
void HWGame::FromNet(const QByteArray & msg) |
|
187 |
{ |
|
188 |
RawSendIPC(msg); |
|
189 |
} |
|
190 |
||
191 |
void HWGame::onClientRead() |
|
192 |
{ |
|
193 |
quint8 msglen; |
|
194 |
quint32 bufsize; |
|
406 | 195 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 196 |
((msglen = readbuffer.data()[0]) < bufsize)) |
197 |
{ |
|
198 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
199 |
readbuffer.remove(0, msglen + 1); |
|
200 |
ParseMessage(msg); |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
QStringList HWGame::setArguments() |
|
205 |
{ |
|
206 |
QStringList arguments; |
|
207 |
arguments << resolutions[0][config->vid_Resolution()]; |
|
208 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 209 |
arguments << "16"; // bpp |
291 | 210 |
arguments << QString("%1").arg(ipc_port); |
184 | 211 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
212 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
213 |
arguments << tr("en.txt"); |
|
296 | 214 |
arguments << "128"; // sound volume |
297 | 215 |
arguments << QString::number(config->timerInterval()); |
267 | 216 |
arguments << datadir->absolutePath(); |
297 | 217 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
184 | 218 |
return arguments; |
219 |
} |
|
220 |
||
341 | 221 |
void HWGame::AddTeam(const QString & teamname) |
184 | 222 |
{ |
223 |
if (TeamCount == 5) return; |
|
224 |
teams[TeamCount] = teamname; |
|
225 |
TeamCount++; |
|
226 |
} |
|
227 |
||
228 |
void HWGame::SaveDemo(const QString & filename) |
|
229 |
{ |
|
230 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
|
231 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
232 |
||
233 |
QFile demofile(filename); |
|
234 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
235 |
{ |
|
425 | 236 |
emit ErrorMessage(tr("Cannot save demo to file %1").arg(filename)); |
184 | 237 |
return ; |
238 |
} |
|
239 |
QDataStream stream(&demofile); |
|
240 |
stream.writeRawData(demo->constData(), demo->size()); |
|
241 |
demofile.close(); |
|
242 |
delete demo; |
|
379 | 243 |
demo=0; |
184 | 244 |
} |
245 |
||
246 |
void HWGame::PlayDemo(const QString & demofilename) |
|
247 |
{ |
|
248 |
gameType = gtDemo; |
|
249 |
QFile demofile(demofilename); |
|
250 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
251 |
{ |
|
425 | 252 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 253 |
return ; |
254 |
} |
|
255 |
||
256 |
// read demo |
|
257 |
QDataStream stream(&demofile); |
|
258 |
char buf[512]; |
|
259 |
int readbytes; |
|
260 |
do |
|
261 |
{ |
|
262 |
readbytes = stream.readRawData((char *)&buf, 512); |
|
263 |
toSendBuf.append(QByteArray((char *)&buf, readbytes)); |
|
264 |
//SendIPC(QByteArray((char *)&buf, readbytes)); |
|
265 |
||
266 |
} while (readbytes > 0); |
|
267 |
demofile.close(); |
|
268 |
||
269 |
// run engine |
|
270 |
demo = new QByteArray; |
|
271 |
Start(); |
|
306 | 272 |
emit GameStateChanged(gsStarted); |
184 | 273 |
} |
274 |
||
275 |
void HWGame::StartNet() |
|
276 |
{ |
|
277 |
gameType = gtNet; |
|
278 |
demo = new QByteArray; |
|
279 |
Start(); |
|
306 | 280 |
emit GameStateChanged(gsStarted); |
184 | 281 |
} |
282 |
||
283 |
void HWGame::StartLocal() |
|
284 |
{ |
|
285 |
gameType = gtLocal; |
|
239 | 286 |
seed = gamecfg->getCurrentSeed(); |
184 | 287 |
demo = new QByteArray; |
288 |
Start(); |
|
306 | 289 |
emit GameStateChanged(gsStarted); |
184 | 290 |
} |
291 |
||
292 |
void HWGame::StartQuick() |
|
293 |
{ |
|
294 |
gameType = gtQLocal; |
|
239 | 295 |
seed = gamecfg->getCurrentSeed(); |
184 | 296 |
demo = new QByteArray; |
297 |
Start(); |
|
306 | 298 |
emit GameStateChanged(gsStarted); |
184 | 299 |
} |