25
|
1 |
/*
|
|
2 |
* Hedgewars, a worms-like game
|
|
3 |
* Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
|
|
4 |
*
|
|
5 |
* Distributed under the terms of the BSD-modified licence:
|
|
6 |
*
|
|
7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8 |
* of this software and associated documentation files (the "Software"), to deal
|
|
9 |
* with the Software without restriction, including without limitation the
|
|
10 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
11 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
12 |
* furnished to do so, subject to the following conditions:
|
|
13 |
*
|
|
14 |
* 1. Redistributions of source code must retain the above copyright notice,
|
|
15 |
* this list of conditions and the following disclaimer.
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
17 |
* this list of conditions and the following disclaimer in the documentation
|
|
18 |
* and/or other materials provided with the distribution.
|
|
19 |
* 3. The name of the author may not be used to endorse or promote products
|
|
20 |
* derived from this software without specific prior written permission.
|
|
21 |
*
|
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
23 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
24 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
|
25 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
27 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
28 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
29 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
30 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
31 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32 |
*/
|
|
33 |
|
|
34 |
#include <QMessageBox>
|
|
35 |
#include <QProcess>
|
|
36 |
#include <QTimer>
|
|
37 |
#include <QString>
|
|
38 |
#include <QByteArray>
|
|
39 |
#include <QTextStream>
|
|
40 |
#include <QFile>
|
|
41 |
#include "game.h"
|
|
42 |
#include "hwconsts.h"
|
|
43 |
|
26
|
44 |
HWGame::HWGame(int Resolution, bool Fullscreen)
|
25
|
45 |
{
|
26
|
46 |
vid_Resolution = Resolution;
|
|
47 |
vid_Fullscreen = Fullscreen;
|
25
|
48 |
IPCServer = new QTcpServer(this);
|
|
49 |
IPCServer->setMaxPendingConnections(1);
|
|
50 |
if (!IPCServer->listen(QHostAddress("127.0.0.1"), IPC_PORT))
|
|
51 |
{
|
28
|
52 |
QMessageBox::critical(0, tr("Error"),
|
25
|
53 |
tr("Unable to start the server: %1.")
|
|
54 |
.arg(IPCServer->errorString()));
|
|
55 |
}
|
|
56 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
|
|
57 |
IPCSocket = 0;
|
|
58 |
TeamCount = 0;
|
26
|
59 |
seed = "";
|
25
|
60 |
}
|
|
61 |
|
|
62 |
void HWGame::NewConnection()
|
|
63 |
{
|
|
64 |
QTcpSocket * client = IPCServer->nextPendingConnection();
|
|
65 |
if(!IPCSocket)
|
|
66 |
{
|
|
67 |
IPCSocket = client;
|
|
68 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
|
|
69 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
70 |
msgsize = 0;
|
|
71 |
} else
|
|
72 |
{
|
|
73 |
client->disconnectFromHost();
|
|
74 |
delete client;
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
void HWGame::ClientDisconnect()
|
|
79 |
{
|
|
80 |
IPCSocket = 0;
|
|
81 |
SaveDemo("demo.hwd_1");
|
|
82 |
delete this;
|
|
83 |
}
|
|
84 |
|
|
85 |
void HWGame::SendTeamConfig(int index)
|
|
86 |
{
|
|
87 |
QFile teamcfg(teams[index]);
|
|
88 |
if (!teamcfg.open(QIODevice::ReadOnly))
|
|
89 |
{
|
|
90 |
return ;
|
|
91 |
}
|
|
92 |
QTextStream stream(&teamcfg);
|
|
93 |
stream.setCodec("UTF-8");
|
|
94 |
QString str;
|
|
95 |
|
|
96 |
while (!stream.atEnd())
|
|
97 |
{
|
|
98 |
str = stream.readLine();
|
|
99 |
if (str.startsWith(";") || (str.length() > 254)) continue;
|
|
100 |
str.prepend("e");
|
|
101 |
SendIPC(str.toLocal8Bit());
|
|
102 |
}
|
|
103 |
teamcfg.close();
|
|
104 |
}
|
|
105 |
|
|
106 |
void HWGame::SendConfig()
|
|
107 |
{
|
26
|
108 |
if (gameType == gtDemo)
|
|
109 |
{
|
|
110 |
SENDIPC("TD");
|
|
111 |
SendIPCRaw(toSendBuf->constData(), toSendBuf->size());
|
|
112 |
delete toSendBuf;
|
|
113 |
return ;
|
|
114 |
}
|
25
|
115 |
SENDIPC("TL");
|
|
116 |
SENDIPC("e$gmflags 0");
|
|
117 |
SENDIPC("eaddteam");
|
|
118 |
SendTeamConfig(0);
|
|
119 |
SENDIPC("ecolor 65535");
|
|
120 |
SENDIPC("eadd hh0 0");
|
|
121 |
SENDIPC("eadd hh1 0");
|
|
122 |
SENDIPC("eadd hh2 0");
|
|
123 |
SENDIPC("eadd hh3 0");
|
28
|
124 |
SENDIPC("eadd hh4 0");
|
25
|
125 |
SENDIPC("eaddteam");
|
|
126 |
SendTeamConfig(1);
|
|
127 |
SENDIPC("ecolor 16776960");
|
|
128 |
SENDIPC("eadd hh0 1");
|
|
129 |
SENDIPC("eadd hh1 1");
|
|
130 |
SENDIPC("eadd hh2 1");
|
|
131 |
SENDIPC("eadd hh3 1");
|
28
|
132 |
SENDIPC("eadd hh4 1");
|
25
|
133 |
}
|
|
134 |
|
|
135 |
void HWGame::ParseMessage()
|
|
136 |
{
|
|
137 |
switch(msgbuf[0])
|
|
138 |
{
|
|
139 |
case '?':
|
|
140 |
{
|
|
141 |
SENDIPC("!");
|
|
142 |
break;
|
|
143 |
}
|
|
144 |
case 'C':
|
|
145 |
{
|
|
146 |
SendConfig();
|
|
147 |
break;
|
|
148 |
}
|
|
149 |
case '+':
|
|
150 |
{
|
|
151 |
break;
|
|
152 |
}
|
|
153 |
default:
|
|
154 |
{
|
|
155 |
demo->append(msgsize);
|
|
156 |
demo->append(QByteArray::fromRawData(msgbuf, msgsize));
|
|
157 |
}
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
void HWGame::SendIPC(const char * msg, quint8 len)
|
|
162 |
{
|
|
163 |
IPCSocket->write((char *)&len, 1);
|
|
164 |
IPCSocket->write(msg, len);
|
|
165 |
if ((len > 5) && !((msg[0] == 'e') && (msg[1] == 'b')))
|
|
166 |
{
|
|
167 |
demo->append(len);
|
|
168 |
demo->append(QByteArray::fromRawData(msg, len));
|
|
169 |
}
|
|
170 |
}
|
|
171 |
|
|
172 |
void HWGame::SendIPC(const QByteArray & buf)
|
|
173 |
{
|
28
|
174 |
if (buf.size() > MAXMSGCHARS) return;
|
25
|
175 |
quint8 len = buf.size();
|
|
176 |
IPCSocket->write((char *)&len, 1);
|
|
177 |
IPCSocket->write(buf);
|
|
178 |
demo->append(len);
|
|
179 |
demo->append(buf);
|
|
180 |
}
|
|
181 |
|
26
|
182 |
void HWGame::SendIPCRaw(const char * msg, quint32 len)
|
|
183 |
{
|
|
184 |
IPCSocket->write(msg, len);
|
|
185 |
demo->append(QByteArray::fromRawData(msg, len));
|
|
186 |
}
|
|
187 |
|
25
|
188 |
void HWGame::ClientRead()
|
|
189 |
{
|
|
190 |
qint64 readbytes = 1;
|
|
191 |
while (readbytes > 0)
|
|
192 |
{
|
|
193 |
if (msgsize == 0)
|
|
194 |
{
|
|
195 |
msgbufsize = 0;
|
|
196 |
readbytes = IPCSocket->read((char *)&msgsize, 1);
|
|
197 |
} else
|
|
198 |
{
|
|
199 |
msgbufsize +=
|
|
200 |
readbytes = IPCSocket->read((char *)&msgbuf[msgbufsize], msgsize - msgbufsize);
|
|
201 |
if (msgbufsize = msgsize)
|
|
202 |
{
|
|
203 |
ParseMessage();
|
|
204 |
msgsize = 0;
|
|
205 |
}
|
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
26
|
210 |
void HWGame::Start()
|
25
|
211 |
{
|
26
|
212 |
gameType = gtLocal;
|
25
|
213 |
if (TeamCount < 2) return;
|
|
214 |
QProcess * process;
|
|
215 |
QStringList arguments;
|
|
216 |
seedgen.GenRNDStr(seed, 10);
|
|
217 |
process = new QProcess;
|
26
|
218 |
arguments << resolutions[0][vid_Resolution];
|
|
219 |
arguments << resolutions[1][vid_Resolution];
|
25
|
220 |
arguments << GetThemeBySeed();
|
|
221 |
arguments << "46631";
|
|
222 |
arguments << seed;
|
26
|
223 |
arguments << (vid_Fullscreen ? "1" : "0");
|
25
|
224 |
process->start("hw", arguments);
|
|
225 |
demo = new QByteArray;
|
|
226 |
demo->append(seed.toLocal8Bit());
|
|
227 |
demo->append(10);
|
|
228 |
}
|
|
229 |
|
|
230 |
void HWGame::AddTeam(const QString & teamname)
|
|
231 |
{
|
|
232 |
if (TeamCount == 5) return;
|
|
233 |
teams[TeamCount] = teamname;
|
|
234 |
TeamCount++;
|
|
235 |
}
|
|
236 |
|
|
237 |
QString HWGame::GetThemeBySeed()
|
|
238 |
{
|
26
|
239 |
QFile themesfile(QString(DATA_PATH) + "/Themes/themes.cfg");
|
25
|
240 |
QStringList themes;
|
|
241 |
if (themesfile.open(QIODevice::ReadOnly))
|
|
242 |
{
|
|
243 |
QTextStream stream(&themesfile);
|
|
244 |
QString str;
|
|
245 |
while (!stream.atEnd())
|
|
246 |
{
|
|
247 |
themes << stream.readLine();
|
|
248 |
}
|
|
249 |
themesfile.close();
|
|
250 |
}
|
|
251 |
quint32 len = themes.size();
|
|
252 |
if (len == 0)
|
|
253 |
{
|
28
|
254 |
QMessageBox::critical(0, "Error", "Cannot access themes.cfg or bad data", "OK");
|
25
|
255 |
return "avematan";
|
|
256 |
}
|
|
257 |
if (seed.isEmpty())
|
|
258 |
{
|
28
|
259 |
QMessageBox::critical(0, "Error", "seed not defined", "OK");
|
25
|
260 |
return "avematan";
|
|
261 |
}
|
|
262 |
quint32 k = 0;
|
|
263 |
for (int i = 0; i < seed.length(); i++)
|
|
264 |
{
|
|
265 |
k += seed[i].unicode();
|
|
266 |
}
|
|
267 |
return themes[k % len];
|
|
268 |
}
|
|
269 |
|
|
270 |
void HWGame::SaveDemo(const QString & filename)
|
|
271 |
{
|
|
272 |
QFile demofile(filename);
|
|
273 |
if (!demofile.open(QIODevice::WriteOnly))
|
|
274 |
{
|
28
|
275 |
QMessageBox::critical(0,
|
25
|
276 |
tr("Error"),
|
|
277 |
tr("Cannot save demo to file %s").arg(filename),
|
|
278 |
tr("Quit"));
|
|
279 |
return ;
|
|
280 |
}
|
|
281 |
QDataStream stream(&demofile);
|
|
282 |
stream.writeRawData(demo->constData(), demo->size());
|
|
283 |
demofile.close();
|
26
|
284 |
delete demo;
|
25
|
285 |
}
|
26
|
286 |
|
|
287 |
void HWGame::PlayDemo(const QString & demofilename)
|
|
288 |
{
|
|
289 |
gameType = gtDemo;
|
|
290 |
QFile demofile(demofilename);
|
|
291 |
if (!demofile.open(QIODevice::ReadOnly))
|
|
292 |
{
|
28
|
293 |
QMessageBox::critical(0,
|
26
|
294 |
tr("Error"),
|
|
295 |
tr("Cannot open demofile %s").arg(demofilename),
|
|
296 |
tr("Quit"));
|
|
297 |
return ;
|
|
298 |
}
|
|
299 |
|
|
300 |
// read demo
|
|
301 |
QDataStream stream(&demofile);
|
|
302 |
toSendBuf = new QByteArray();
|
|
303 |
char buf[512];
|
|
304 |
quint32 readbytes;
|
|
305 |
do
|
|
306 |
{
|
|
307 |
readbytes = stream.readRawData((char *)&buf, 512);
|
|
308 |
toSendBuf -> append(QByteArray((char *)&buf, readbytes));
|
|
309 |
|
|
310 |
} while (readbytes > 0);
|
|
311 |
demofile.close();
|
|
312 |
|
|
313 |
// cut seed
|
|
314 |
quint32 index = toSendBuf->indexOf(10);
|
|
315 |
if (!index)
|
|
316 |
{
|
28
|
317 |
QMessageBox::critical(0,
|
26
|
318 |
tr("Error"),
|
|
319 |
tr("Corrupted demo file %s").arg(demofilename),
|
|
320 |
tr("Quit"));
|
|
321 |
return ;
|
|
322 |
}
|
|
323 |
seed = QString(toSendBuf->left(index++));
|
|
324 |
toSendBuf->remove(0, index);
|
|
325 |
|
|
326 |
// run engine
|
|
327 |
QProcess * process;
|
|
328 |
QStringList arguments;
|
|
329 |
process = new QProcess;
|
|
330 |
arguments << resolutions[0][vid_Resolution];
|
|
331 |
arguments << resolutions[1][vid_Resolution];
|
|
332 |
arguments << GetThemeBySeed();
|
|
333 |
arguments << "46631";
|
|
334 |
arguments << seed;
|
|
335 |
arguments << (vid_Fullscreen ? "1" : "0");
|
|
336 |
process->start("hw", arguments);
|
|
337 |
demo = new QByteArray;
|
|
338 |
}
|