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