author | unc0rr |
Mon, 21 May 2007 15:42:21 +0000 | |
changeset 516 | f682e134ac2e |
parent 512 | efc640bb60d0 |
child 529 | 812682c1ab62 |
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 |
#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; |
|
497 | 207 |
arguments << cfgdir->absolutePath(); |
184 | 208 |
arguments << resolutions[0][config->vid_Resolution()]; |
209 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 210 |
arguments << "16"; // bpp |
291 | 211 |
arguments << QString("%1").arg(ipc_port); |
184 | 212 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
213 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
214 |
arguments << tr("en.txt"); |
|
296 | 215 |
arguments << "128"; // sound volume |
297 | 216 |
arguments << QString::number(config->timerInterval()); |
267 | 217 |
arguments << datadir->absolutePath(); |
297 | 218 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
184 | 219 |
return arguments; |
220 |
} |
|
221 |
||
341 | 222 |
void HWGame::AddTeam(const QString & teamname) |
184 | 223 |
{ |
224 |
if (TeamCount == 5) return; |
|
225 |
teams[TeamCount] = teamname; |
|
226 |
TeamCount++; |
|
227 |
} |
|
228 |
||
229 |
void HWGame::SaveDemo(const QString & filename) |
|
230 |
{ |
|
231 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
|
232 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
233 |
||
234 |
QFile demofile(filename); |
|
235 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
236 |
{ |
|
425 | 237 |
emit ErrorMessage(tr("Cannot save demo to file %1").arg(filename)); |
184 | 238 |
return ; |
239 |
} |
|
240 |
QDataStream stream(&demofile); |
|
241 |
stream.writeRawData(demo->constData(), demo->size()); |
|
242 |
demofile.close(); |
|
243 |
delete demo; |
|
379 | 244 |
demo=0; |
184 | 245 |
} |
246 |
||
247 |
void HWGame::PlayDemo(const QString & demofilename) |
|
248 |
{ |
|
249 |
gameType = gtDemo; |
|
250 |
QFile demofile(demofilename); |
|
251 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
252 |
{ |
|
425 | 253 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 254 |
return ; |
255 |
} |
|
256 |
||
257 |
// read demo |
|
512 | 258 |
toSendBuf = demofile.readAll(); |
184 | 259 |
|
260 |
// run engine |
|
261 |
demo = new QByteArray; |
|
262 |
Start(); |
|
306 | 263 |
emit GameStateChanged(gsStarted); |
184 | 264 |
} |
265 |
||
266 |
void HWGame::StartNet() |
|
267 |
{ |
|
268 |
gameType = gtNet; |
|
269 |
demo = new QByteArray; |
|
270 |
Start(); |
|
306 | 271 |
emit GameStateChanged(gsStarted); |
184 | 272 |
} |
273 |
||
274 |
void HWGame::StartLocal() |
|
275 |
{ |
|
276 |
gameType = gtLocal; |
|
239 | 277 |
seed = gamecfg->getCurrentSeed(); |
184 | 278 |
demo = new QByteArray; |
279 |
Start(); |
|
306 | 280 |
emit GameStateChanged(gsStarted); |
184 | 281 |
} |
282 |
||
283 |
void HWGame::StartQuick() |
|
284 |
{ |
|
285 |
gameType = gtQLocal; |
|
239 | 286 |
seed = gamecfg->getCurrentSeed(); |
184 | 287 |
demo = new QByteArray; |
288 |
Start(); |
|
306 | 289 |
emit GameStateChanged(gsStarted); |
184 | 290 |
} |