author | unc0rr |
Mon, 15 Jan 2007 21:20:36 +0000 | |
changeset 341 | 184230eb4151 |
parent 339 | 7535ab6c3820 |
child 352 | 4665bfe25470 |
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 |
||
53 |
void HWGame::SendTeamConfig(int index) |
|
54 |
{ |
|
55 |
LocalCFG(teams[index]); |
|
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..."; |
|
80 |
QList<HWTeam> teams = QList<HWTeam>::fromStdList(m_pTeamSelWidget->getPlayingTeams()); |
|
81 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
|
82 |
{ |
|
83 |
qDebug() << "a team..."; |
|
84 |
(*it).numHedgehogs = 4; |
|
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 '?': { |
126 |
if (gameType == gtNet) |
|
127 |
emit SendNet(QByteArray("\x01""?")); |
|
128 |
else |
|
129 |
SendIPC("!"); |
|
130 |
break; |
|
131 |
} |
|
132 |
case 'C': { |
|
341 | 133 |
qDebug("ASK Config"); |
184 | 134 |
switch (gameType) { |
135 |
case gtLocal: { |
|
136 |
SendConfig(); |
|
137 |
break; |
|
138 |
} |
|
139 |
case gtQLocal: { |
|
140 |
SendQuickConfig(); |
|
141 |
break; |
|
142 |
} |
|
143 |
case gtDemo: break; |
|
144 |
case gtNet: { |
|
145 |
emit SendNet(QByteArray("\x01""C")); |
|
341 | 146 |
SendNetConfig(); |
184 | 147 |
break; |
148 |
} |
|
149 |
} |
|
150 |
break; |
|
151 |
} |
|
152 |
case 'E': { |
|
153 |
QMessageBox::critical(0, |
|
154 |
"Hedgewars: error message", |
|
155 |
QString().append(msg.mid(2)).left(msg.size() - 6), |
|
156 |
QMessageBox::Ok, |
|
157 |
QMessageBox::NoButton, |
|
158 |
QMessageBox::NoButton); |
|
159 |
return; |
|
160 |
} |
|
208 | 161 |
case 'K': { |
162 |
ulong kb = msg.mid(2).toULong(); |
|
163 |
if (kb && kb <= KBmsgsCount) |
|
164 |
{ |
|
165 |
QMessageBox::information(0, |
|
166 |
"Hedgewars: information", |
|
167 |
KBMessages[kb - 1], |
|
168 |
QMessageBox::Ok, |
|
169 |
QMessageBox::NoButton, |
|
170 |
QMessageBox::NoButton); |
|
171 |
} |
|
172 |
return; |
|
173 |
} |
|
184 | 174 |
case '+': { |
175 |
if (gameType == gtNet) |
|
176 |
{ |
|
177 |
emit SendNet(msg); |
|
178 |
} |
|
179 |
break; |
|
180 |
} |
|
306 | 181 |
case 'i': { |
182 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
183 |
break; |
|
184 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
185 |
case 'Q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
186 |
emit GameStateChanged(gsInterrupted); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
187 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
188 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
189 |
case 'q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
190 |
emit GameStateChanged(gsFinished); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
191 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
192 |
} |
184 | 193 |
default: { |
194 |
if (gameType == gtNet) |
|
195 |
{ |
|
196 |
emit SendNet(msg); |
|
197 |
} |
|
198 |
demo->append(msg); |
|
199 |
} |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
void HWGame::FromNet(const QByteArray & msg) |
|
204 |
{ |
|
205 |
RawSendIPC(msg); |
|
206 |
} |
|
207 |
||
208 |
void HWGame::onClientRead() |
|
209 |
{ |
|
210 |
quint8 msglen; |
|
211 |
quint32 bufsize; |
|
212 |
while (((bufsize = readbuffer.size()) > 0) && |
|
213 |
((msglen = readbuffer.data()[0]) < bufsize)) |
|
214 |
{ |
|
215 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
216 |
readbuffer.remove(0, msglen + 1); |
|
217 |
ParseMessage(msg); |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
QStringList HWGame::setArguments() |
|
222 |
{ |
|
223 |
QStringList arguments; |
|
224 |
arguments << resolutions[0][config->vid_Resolution()]; |
|
225 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 226 |
arguments << "16"; // bpp |
291 | 227 |
arguments << QString("%1").arg(ipc_port); |
184 | 228 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
229 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
230 |
arguments << tr("en.txt"); |
|
296 | 231 |
arguments << "128"; // sound volume |
297 | 232 |
arguments << QString::number(config->timerInterval()); |
267 | 233 |
arguments << datadir->absolutePath(); |
297 | 234 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
184 | 235 |
return arguments; |
236 |
} |
|
237 |
||
341 | 238 |
void HWGame::AddTeam(const QString & teamname) |
184 | 239 |
{ |
240 |
if (TeamCount == 5) return; |
|
241 |
teams[TeamCount] = teamname; |
|
242 |
TeamCount++; |
|
243 |
} |
|
244 |
||
245 |
void HWGame::SaveDemo(const QString & filename) |
|
246 |
{ |
|
247 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
|
248 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
249 |
||
250 |
QFile demofile(filename); |
|
251 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
252 |
{ |
|
253 |
QMessageBox::critical(0, |
|
254 |
tr("Error"), |
|
255 |
tr("Cannot save demo to file %1").arg(filename), |
|
256 |
tr("Quit")); |
|
257 |
return ; |
|
258 |
} |
|
259 |
QDataStream stream(&demofile); |
|
260 |
stream.writeRawData(demo->constData(), demo->size()); |
|
261 |
demofile.close(); |
|
262 |
delete demo; |
|
263 |
} |
|
264 |
||
265 |
void HWGame::PlayDemo(const QString & demofilename) |
|
266 |
{ |
|
267 |
gameType = gtDemo; |
|
268 |
QFile demofile(demofilename); |
|
269 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
270 |
{ |
|
271 |
QMessageBox::critical(0, |
|
272 |
tr("Error"), |
|
273 |
tr("Cannot open demofile %1").arg(demofilename), |
|
274 |
tr("Quit")); |
|
275 |
return ; |
|
276 |
} |
|
277 |
||
278 |
// read demo |
|
279 |
QDataStream stream(&demofile); |
|
280 |
char buf[512]; |
|
281 |
int readbytes; |
|
282 |
do |
|
283 |
{ |
|
284 |
readbytes = stream.readRawData((char *)&buf, 512); |
|
285 |
toSendBuf.append(QByteArray((char *)&buf, readbytes)); |
|
286 |
//SendIPC(QByteArray((char *)&buf, readbytes)); |
|
287 |
||
288 |
} while (readbytes > 0); |
|
289 |
demofile.close(); |
|
290 |
||
291 |
// run engine |
|
292 |
demo = new QByteArray; |
|
293 |
Start(); |
|
306 | 294 |
emit GameStateChanged(gsStarted); |
184 | 295 |
} |
296 |
||
297 |
void HWGame::StartNet() |
|
298 |
{ |
|
299 |
gameType = gtNet; |
|
300 |
demo = new QByteArray; |
|
301 |
Start(); |
|
306 | 302 |
emit GameStateChanged(gsStarted); |
184 | 303 |
} |
304 |
||
305 |
void HWGame::StartLocal() |
|
306 |
{ |
|
307 |
gameType = gtLocal; |
|
239 | 308 |
seed = gamecfg->getCurrentSeed(); |
184 | 309 |
demo = new QByteArray; |
310 |
Start(); |
|
306 | 311 |
emit GameStateChanged(gsStarted); |
184 | 312 |
} |
313 |
||
314 |
void HWGame::StartQuick() |
|
315 |
{ |
|
316 |
gameType = gtQLocal; |
|
239 | 317 |
seed = gamecfg->getCurrentSeed(); |
184 | 318 |
demo = new QByteArray; |
319 |
Start(); |
|
306 | 320 |
emit GameStateChanged(gsStarted); |
184 | 321 |
} |
322 |
||
323 |
||
324 |
void HWGame::LocalCFG(const QString & teamname) |
|
325 |
{ |
|
341 | 326 |
qDebug("HWGame::LocalCFG is UNIMPLEMENTED"); |
327 |
/* QByteArray teamcfg; |
|
184 | 328 |
HWTeam team(teamname); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
239
diff
changeset
|
329 |
team.LoadFromFile(); |
312 | 330 |
RawSendIPC(HWProto::addStringListToBuffer(teamcfg, |
331 |
team.TeamGameConfig(16776960, 4, gamecfg->getInitHealth()))); |
|
341 | 332 |
*/ |
184 | 333 |
} |
334 |