author | displacer |
Thu, 11 Jan 2007 22:04:08 +0000 | |
changeset 317 | e95340dbfc1d |
parent 312 | c36d0b34ac3d |
child 318 | 46a43b02bbb3 |
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> |
|
27 |
||
28 |
#include "game.h" |
|
29 |
#include "hwconsts.h" |
|
30 |
#include "gameuiconfig.h" |
|
31 |
#include "gamecfgwidget.h" |
|
210 | 32 |
#include "KB.h" |
239 | 33 |
#include "proto.h" |
184 | 34 |
|
35 |
HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg) : |
|
36 |
TCPBase(true) |
|
37 |
{ |
|
38 |
this->config = config; |
|
39 |
this->gamecfg = gamecfg; |
|
40 |
TeamCount = 0; |
|
41 |
seed = ""; |
|
42 |
} |
|
43 |
||
44 |
void HWGame::onClientDisconnect() |
|
45 |
{ |
|
271 | 46 |
SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_" + cProtoVer); |
306 | 47 |
emit GameStateChanged(gsFinished); |
184 | 48 |
} |
49 |
||
50 |
void HWGame::SendTeamConfig(int index) |
|
51 |
{ |
|
52 |
LocalCFG(teams[index]); |
|
53 |
} |
|
54 |
||
253 | 55 |
void HWGame::commonConfig() |
184 | 56 |
{ |
57 |
SendIPC(QString("eseed %1").arg(seed).toAscii()); |
|
249 | 58 |
try { |
59 |
QString currentMap=gamecfg->getCurrentMap(); |
|
60 |
SendIPC((QString("emap ")+currentMap).toAscii()); |
|
61 |
SendIPC(QString("etheme %1").arg(gamecfg->getCurrentTheme()).toAscii()); |
|
62 |
} |
|
63 |
catch(const MapFileErrorException& e) { |
|
64 |
SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii()); |
|
65 |
} |
|
184 | 66 |
SendIPC("TL"); |
67 |
SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii()); |
|
312 | 68 |
SendIPC(QString("e$turntime %1").arg(gamecfg->getTurnTime() * 1000).toAscii()); |
253 | 69 |
} |
70 |
||
71 |
void HWGame::SendConfig() |
|
72 |
{ |
|
73 |
commonConfig(); |
|
184 | 74 |
|
75 |
for (int i = 0; i < TeamCount; i++) |
|
76 |
{ |
|
239 | 77 |
HWTeam team(teams[i]); |
78 |
team.LoadFromFile(); |
|
79 |
||
80 |
QColor clr = m_teamsParams[teams[i]].teamColor; |
|
81 |
QByteArray buf; |
|
312 | 82 |
QStringList sl = team.TeamGameConfig(clr.rgb()&0xFFFFFF, |
83 |
m_teamsParams[teams[i]].numHedgehogs, |
|
84 |
gamecfg->getInitHealth()); |
|
239 | 85 |
HWProto::addStringListToBuffer(buf, sl); |
86 |
RawSendIPC(buf); |
|
184 | 87 |
} |
88 |
} |
|
89 |
||
90 |
void HWGame::SendQuickConfig() |
|
91 |
{ |
|
253 | 92 |
commonConfig(); |
239 | 93 |
|
94 |
QByteArray teamscfg; |
|
95 |
HWTeam team1(0); |
|
96 |
team1.difficulty = 0; |
|
312 | 97 |
HWProto::addStringListToBuffer(teamscfg, |
98 |
team1.TeamGameConfig(65535, 4, gamecfg->getInitHealth())); |
|
239 | 99 |
|
100 |
HWTeam team2(2); |
|
101 |
team2.difficulty = 4; |
|
312 | 102 |
RawSendIPC(HWProto::addStringListToBuffer(teamscfg, |
103 |
team2.TeamGameConfig(16776960, 4, gamecfg->getInitHealth()))); |
|
184 | 104 |
} |
105 |
||
106 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
107 |
{ |
|
306 | 108 |
switch(msg.at(1)) { |
184 | 109 |
case '?': { |
110 |
if (gameType == gtNet) |
|
111 |
emit SendNet(QByteArray("\x01""?")); |
|
112 |
else |
|
113 |
SendIPC("!"); |
|
114 |
break; |
|
115 |
} |
|
116 |
case 'C': { |
|
117 |
switch (gameType) { |
|
118 |
case gtLocal: { |
|
119 |
SendConfig(); |
|
120 |
break; |
|
121 |
} |
|
122 |
case gtQLocal: { |
|
123 |
SendQuickConfig(); |
|
124 |
break; |
|
125 |
} |
|
126 |
case gtDemo: break; |
|
127 |
case gtNet: { |
|
128 |
SendIPC("TN"); |
|
129 |
emit SendNet(QByteArray("\x01""C")); |
|
130 |
break; |
|
131 |
} |
|
132 |
} |
|
133 |
break; |
|
134 |
} |
|
135 |
case 'E': { |
|
136 |
QMessageBox::critical(0, |
|
137 |
"Hedgewars: error message", |
|
138 |
QString().append(msg.mid(2)).left(msg.size() - 6), |
|
139 |
QMessageBox::Ok, |
|
140 |
QMessageBox::NoButton, |
|
141 |
QMessageBox::NoButton); |
|
142 |
return; |
|
143 |
} |
|
208 | 144 |
case 'K': { |
145 |
ulong kb = msg.mid(2).toULong(); |
|
146 |
if (kb && kb <= KBmsgsCount) |
|
147 |
{ |
|
148 |
QMessageBox::information(0, |
|
149 |
"Hedgewars: information", |
|
150 |
KBMessages[kb - 1], |
|
151 |
QMessageBox::Ok, |
|
152 |
QMessageBox::NoButton, |
|
153 |
QMessageBox::NoButton); |
|
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 |
} |
|
184 | 168 |
default: { |
169 |
if (gameType == gtNet) |
|
170 |
{ |
|
171 |
emit SendNet(msg); |
|
172 |
} |
|
173 |
demo->append(msg); |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
void HWGame::FromNet(const QByteArray & msg) |
|
179 |
{ |
|
180 |
RawSendIPC(msg); |
|
181 |
} |
|
182 |
||
183 |
void HWGame::onClientRead() |
|
184 |
{ |
|
185 |
quint8 msglen; |
|
186 |
quint32 bufsize; |
|
187 |
while (((bufsize = readbuffer.size()) > 0) && |
|
188 |
((msglen = readbuffer.data()[0]) < bufsize)) |
|
189 |
{ |
|
190 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
191 |
readbuffer.remove(0, msglen + 1); |
|
192 |
ParseMessage(msg); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
QStringList HWGame::setArguments() |
|
197 |
{ |
|
198 |
QStringList arguments; |
|
199 |
arguments << resolutions[0][config->vid_Resolution()]; |
|
200 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 201 |
arguments << "16"; // bpp |
291 | 202 |
arguments << QString("%1").arg(ipc_port); |
184 | 203 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
204 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
205 |
arguments << tr("en.txt"); |
|
296 | 206 |
arguments << "128"; // sound volume |
297 | 207 |
arguments << QString::number(config->timerInterval()); |
267 | 208 |
arguments << datadir->absolutePath(); |
297 | 209 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
184 | 210 |
return arguments; |
211 |
} |
|
212 |
||
213 | 213 |
void HWGame::AddTeam(const QString & teamname, HWTeamTempParams teamParams) |
184 | 214 |
{ |
215 |
if (TeamCount == 5) return; |
|
216 |
teams[TeamCount] = teamname; |
|
217 |
TeamCount++; |
|
213 | 218 |
m_teamsParams[teamname]=teamParams; |
184 | 219 |
} |
220 |
||
221 |
void HWGame::SaveDemo(const QString & filename) |
|
222 |
{ |
|
223 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
|
224 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
225 |
||
226 |
QFile demofile(filename); |
|
227 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
228 |
{ |
|
229 |
QMessageBox::critical(0, |
|
230 |
tr("Error"), |
|
231 |
tr("Cannot save demo to file %1").arg(filename), |
|
232 |
tr("Quit")); |
|
233 |
return ; |
|
234 |
} |
|
235 |
QDataStream stream(&demofile); |
|
236 |
stream.writeRawData(demo->constData(), demo->size()); |
|
237 |
demofile.close(); |
|
238 |
delete demo; |
|
239 |
} |
|
240 |
||
241 |
void HWGame::PlayDemo(const QString & demofilename) |
|
242 |
{ |
|
243 |
gameType = gtDemo; |
|
244 |
QFile demofile(demofilename); |
|
245 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
246 |
{ |
|
247 |
QMessageBox::critical(0, |
|
248 |
tr("Error"), |
|
249 |
tr("Cannot open demofile %1").arg(demofilename), |
|
250 |
tr("Quit")); |
|
251 |
return ; |
|
252 |
} |
|
253 |
||
254 |
// read demo |
|
255 |
QDataStream stream(&demofile); |
|
256 |
char buf[512]; |
|
257 |
int readbytes; |
|
258 |
do |
|
259 |
{ |
|
260 |
readbytes = stream.readRawData((char *)&buf, 512); |
|
261 |
toSendBuf.append(QByteArray((char *)&buf, readbytes)); |
|
262 |
//SendIPC(QByteArray((char *)&buf, readbytes)); |
|
263 |
||
264 |
} while (readbytes > 0); |
|
265 |
demofile.close(); |
|
266 |
||
267 |
// run engine |
|
268 |
demo = new QByteArray; |
|
269 |
Start(); |
|
306 | 270 |
emit GameStateChanged(gsStarted); |
184 | 271 |
} |
272 |
||
273 |
void HWGame::StartNet() |
|
274 |
{ |
|
275 |
gameType = gtNet; |
|
276 |
demo = new QByteArray; |
|
277 |
Start(); |
|
306 | 278 |
emit GameStateChanged(gsStarted); |
184 | 279 |
} |
280 |
||
281 |
void HWGame::StartLocal() |
|
282 |
{ |
|
283 |
gameType = gtLocal; |
|
284 |
if (TeamCount < 2) return; |
|
239 | 285 |
seed = gamecfg->getCurrentSeed(); |
184 | 286 |
demo = new QByteArray; |
287 |
Start(); |
|
306 | 288 |
emit GameStateChanged(gsStarted); |
184 | 289 |
} |
290 |
||
291 |
void HWGame::StartQuick() |
|
292 |
{ |
|
293 |
gameType = gtQLocal; |
|
239 | 294 |
seed = gamecfg->getCurrentSeed(); |
184 | 295 |
demo = new QByteArray; |
296 |
Start(); |
|
306 | 297 |
emit GameStateChanged(gsStarted); |
184 | 298 |
} |
299 |
||
300 |
||
301 |
void HWGame::LocalCFG(const QString & teamname) |
|
302 |
{ |
|
239 | 303 |
QByteArray teamcfg; |
184 | 304 |
HWTeam team(teamname); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
239
diff
changeset
|
305 |
team.LoadFromFile(); |
312 | 306 |
RawSendIPC(HWProto::addStringListToBuffer(teamcfg, |
307 |
team.TeamGameConfig(16776960, 4, gamecfg->getInitHealth()))); |
|
184 | 308 |
} |
309 |