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