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 |
|
|
44 |
HWGame::HWGame()
|
|
45 |
{
|
|
46 |
IPCServer = new QTcpServer(this);
|
|
47 |
IPCServer->setMaxPendingConnections(1);
|
|
48 |
if (!IPCServer->listen(QHostAddress("127.0.0.1"), IPC_PORT))
|
|
49 |
{
|
|
50 |
QMessageBox::critical(this, tr("Error"),
|
|
51 |
tr("Unable to start the server: %1.")
|
|
52 |
.arg(IPCServer->errorString()));
|
|
53 |
}
|
|
54 |
connect(IPCServer, SIGNAL(newConnection()), this, SLOT(NewConnection()));
|
|
55 |
IPCSocket = 0;
|
|
56 |
TeamCount = 0;
|
|
57 |
seed = "seed";
|
|
58 |
}
|
|
59 |
|
|
60 |
void HWGame::NewConnection()
|
|
61 |
{
|
|
62 |
QTcpSocket * client = IPCServer->nextPendingConnection();
|
|
63 |
if(!IPCSocket)
|
|
64 |
{
|
|
65 |
IPCSocket = client;
|
|
66 |
connect(client, SIGNAL(disconnected()), this, SLOT(ClientDisconnect()));
|
|
67 |
connect(client, SIGNAL(readyRead()), this, SLOT(ClientRead()));
|
|
68 |
msgsize = 0;
|
|
69 |
} else
|
|
70 |
{
|
|
71 |
client->disconnectFromHost();
|
|
72 |
delete client;
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
void HWGame::ClientDisconnect()
|
|
77 |
{
|
|
78 |
IPCSocket = 0;
|
|
79 |
SaveDemo("demo.hwd_1");
|
|
80 |
delete this;
|
|
81 |
}
|
|
82 |
|
|
83 |
void HWGame::SendTeamConfig(int index)
|
|
84 |
{
|
|
85 |
QFile teamcfg(teams[index]);
|
|
86 |
if (!teamcfg.open(QIODevice::ReadOnly))
|
|
87 |
{
|
|
88 |
return ;
|
|
89 |
}
|
|
90 |
QTextStream stream(&teamcfg);
|
|
91 |
stream.setCodec("UTF-8");
|
|
92 |
QString str;
|
|
93 |
|
|
94 |
while (!stream.atEnd())
|
|
95 |
{
|
|
96 |
str = stream.readLine();
|
|
97 |
if (str.startsWith(";") || (str.length() > 254)) continue;
|
|
98 |
str.prepend("e");
|
|
99 |
SendIPC(str.toLocal8Bit());
|
|
100 |
}
|
|
101 |
teamcfg.close();
|
|
102 |
}
|
|
103 |
|
|
104 |
void HWGame::SendConfig()
|
|
105 |
{
|
|
106 |
SENDIPC("TL");
|
|
107 |
SENDIPC("e$gmflags 0");
|
|
108 |
SENDIPC("eaddteam");
|
|
109 |
SendTeamConfig(0);
|
|
110 |
SENDIPC("ecolor 65535");
|
|
111 |
SENDIPC("eadd hh0 0");
|
|
112 |
SENDIPC("eadd hh1 0");
|
|
113 |
SENDIPC("eadd hh2 0");
|
|
114 |
SENDIPC("eadd hh3 0");
|
|
115 |
SENDIPC("eaddteam");
|
|
116 |
SendTeamConfig(1);
|
|
117 |
SENDIPC("ecolor 16776960");
|
|
118 |
SENDIPC("eadd hh0 1");
|
|
119 |
SENDIPC("eadd hh1 1");
|
|
120 |
SENDIPC("eadd hh2 1");
|
|
121 |
SENDIPC("eadd hh3 1");
|
|
122 |
}
|
|
123 |
|
|
124 |
void HWGame::ParseMessage()
|
|
125 |
{
|
|
126 |
switch(msgbuf[0])
|
|
127 |
{
|
|
128 |
case '?':
|
|
129 |
{
|
|
130 |
SENDIPC("!");
|
|
131 |
break;
|
|
132 |
}
|
|
133 |
case 'C':
|
|
134 |
{
|
|
135 |
SendConfig();
|
|
136 |
break;
|
|
137 |
}
|
|
138 |
case '+':
|
|
139 |
{
|
|
140 |
break;
|
|
141 |
}
|
|
142 |
default:
|
|
143 |
{
|
|
144 |
demo->append(msgsize);
|
|
145 |
demo->append(QByteArray::fromRawData(msgbuf, msgsize));
|
|
146 |
}
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
void HWGame::SendIPC(const char * msg, quint8 len)
|
|
151 |
{
|
|
152 |
IPCSocket->write((char *)&len, 1);
|
|
153 |
IPCSocket->write(msg, len);
|
|
154 |
if ((len > 5) && !((msg[0] == 'e') && (msg[1] == 'b')))
|
|
155 |
{
|
|
156 |
demo->append(len);
|
|
157 |
demo->append(QByteArray::fromRawData(msg, len));
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
void HWGame::SendIPC(const QByteArray & buf)
|
|
162 |
{
|
|
163 |
if (buf.size() > 255) return;
|
|
164 |
quint8 len = buf.size();
|
|
165 |
IPCSocket->write((char *)&len, 1);
|
|
166 |
IPCSocket->write(buf);
|
|
167 |
demo->append(len);
|
|
168 |
demo->append(buf);
|
|
169 |
}
|
|
170 |
|
|
171 |
void HWGame::ClientRead()
|
|
172 |
{
|
|
173 |
qint64 readbytes = 1;
|
|
174 |
while (readbytes > 0)
|
|
175 |
{
|
|
176 |
if (msgsize == 0)
|
|
177 |
{
|
|
178 |
msgbufsize = 0;
|
|
179 |
readbytes = IPCSocket->read((char *)&msgsize, 1);
|
|
180 |
} else
|
|
181 |
{
|
|
182 |
msgbufsize +=
|
|
183 |
readbytes = IPCSocket->read((char *)&msgbuf[msgbufsize], msgsize - msgbufsize);
|
|
184 |
if (msgbufsize = msgsize)
|
|
185 |
{
|
|
186 |
ParseMessage();
|
|
187 |
msgsize = 0;
|
|
188 |
}
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
void HWGame::Start(int Resolution, bool Fullscreen)
|
|
194 |
{
|
|
195 |
if (TeamCount < 2) return;
|
|
196 |
QProcess * process;
|
|
197 |
QStringList arguments;
|
|
198 |
seedgen.GenRNDStr(seed, 10);
|
|
199 |
process = new QProcess;
|
|
200 |
arguments << resolutions[0][Resolution];
|
|
201 |
arguments << resolutions[1][Resolution];
|
|
202 |
arguments << GetThemeBySeed();
|
|
203 |
arguments << "46631";
|
|
204 |
arguments << seed;
|
|
205 |
arguments << (Fullscreen ? "1" : "0");
|
|
206 |
process->start("hw", arguments);
|
|
207 |
demo = new QByteArray;
|
|
208 |
demo->append(seed.toLocal8Bit());
|
|
209 |
demo->append(10);
|
|
210 |
}
|
|
211 |
|
|
212 |
void HWGame::AddTeam(const QString & teamname)
|
|
213 |
{
|
|
214 |
if (TeamCount == 5) return;
|
|
215 |
teams[TeamCount] = teamname;
|
|
216 |
TeamCount++;
|
|
217 |
}
|
|
218 |
|
|
219 |
QString HWGame::GetThemeBySeed()
|
|
220 |
{
|
|
221 |
QFile themesfile("Data/Themes/themes.cfg");
|
|
222 |
QStringList themes;
|
|
223 |
if (themesfile.open(QIODevice::ReadOnly))
|
|
224 |
{
|
|
225 |
QTextStream stream(&themesfile);
|
|
226 |
QString str;
|
|
227 |
while (!stream.atEnd())
|
|
228 |
{
|
|
229 |
themes << stream.readLine();
|
|
230 |
}
|
|
231 |
themesfile.close();
|
|
232 |
}
|
|
233 |
quint32 len = themes.size();
|
|
234 |
if (len == 0)
|
|
235 |
{
|
|
236 |
QMessageBox::critical(this, "Error", "Cannot access themes.cfg or bad data", "OK");
|
|
237 |
return "avematan";
|
|
238 |
}
|
|
239 |
if (seed.isEmpty())
|
|
240 |
{
|
|
241 |
QMessageBox::critical(this, "Error", "seed not defined", "OK");
|
|
242 |
return "avematan";
|
|
243 |
}
|
|
244 |
quint32 k = 0;
|
|
245 |
for (int i = 0; i < seed.length(); i++)
|
|
246 |
{
|
|
247 |
k += seed[i].unicode();
|
|
248 |
}
|
|
249 |
return themes[k % len];
|
|
250 |
}
|
|
251 |
|
|
252 |
void HWGame::SaveDemo(const QString & filename)
|
|
253 |
{
|
|
254 |
QFile demofile(filename);
|
|
255 |
if (!demofile.open(QIODevice::WriteOnly))
|
|
256 |
{
|
|
257 |
QMessageBox::critical(this,
|
|
258 |
tr("Error"),
|
|
259 |
tr("Cannot save demo to file %s").arg(filename),
|
|
260 |
tr("Quit"));
|
|
261 |
return ;
|
|
262 |
}
|
|
263 |
QDataStream stream(&demofile);
|
|
264 |
stream.writeRawData(demo->constData(), demo->size());
|
|
265 |
demofile.close();
|
|
266 |
}
|