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