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"
|
208
|
32 |
#include "kb.h"
|
184
|
33 |
|
|
34 |
HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg) :
|
|
35 |
TCPBase(true)
|
|
36 |
{
|
|
37 |
this->config = config;
|
|
38 |
this->gamecfg = gamecfg;
|
|
39 |
TeamCount = 0;
|
|
40 |
seed = "";
|
|
41 |
}
|
|
42 |
|
|
43 |
void HWGame::onClientDisconnect()
|
|
44 |
{
|
|
45 |
SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_1");
|
|
46 |
}
|
|
47 |
|
|
48 |
void HWGame::SendTeamConfig(int index)
|
|
49 |
{
|
|
50 |
LocalCFG(teams[index]);
|
|
51 |
}
|
|
52 |
|
|
53 |
void HWGame::SendConfig()
|
|
54 |
{
|
|
55 |
SendIPC(QString("eseed %1").arg(seed).toAscii());
|
|
56 |
SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii());
|
|
57 |
SendIPC("TL");
|
|
58 |
SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii());
|
|
59 |
|
|
60 |
for (int i = 0; i < TeamCount; i++)
|
|
61 |
{
|
|
62 |
SendIPC("eaddteam");
|
|
63 |
LocalCFG(teams[i]);
|
|
64 |
SendIPC(QString("ecolor %1").arg(65535 << i * 8).toAscii());
|
|
65 |
for (int t = 0; t < hdNum[teams[i]]; t++)
|
|
66 |
SendIPC(QString("eadd hh%1 0").arg(t).toAscii());
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
void HWGame::SendQuickConfig()
|
|
71 |
{
|
|
72 |
SendIPC(QString("eseed %1").arg(seed).toAscii());
|
|
73 |
SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii());
|
|
74 |
SendIPC("TL");
|
|
75 |
SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii());
|
|
76 |
SendIPC("eaddteam");
|
|
77 |
LocalCFG(0);
|
|
78 |
SendIPC("ecolor 65535");
|
|
79 |
SendIPC("eadd hh0 0");
|
|
80 |
SendIPC("eadd hh1 0");
|
|
81 |
SendIPC("eadd hh2 0");
|
|
82 |
SendIPC("eadd hh3 0");
|
|
83 |
SendIPC("eaddteam");
|
|
84 |
LocalCFG(2);
|
|
85 |
SendIPC("ecolor 16776960");
|
|
86 |
SendIPC("eadd hh0 1");
|
|
87 |
SendIPC("eadd hh1 1");
|
|
88 |
SendIPC("eadd hh2 1");
|
|
89 |
SendIPC("eadd hh3 1");
|
|
90 |
}
|
|
91 |
|
|
92 |
void HWGame::ParseMessage(const QByteArray & msg)
|
|
93 |
{
|
|
94 |
switch(msg.data()[1]) {
|
|
95 |
case '?': {
|
|
96 |
if (gameType == gtNet)
|
|
97 |
emit SendNet(QByteArray("\x01""?"));
|
|
98 |
else
|
|
99 |
SendIPC("!");
|
|
100 |
break;
|
|
101 |
}
|
|
102 |
case 'C': {
|
|
103 |
switch (gameType) {
|
|
104 |
case gtLocal: {
|
|
105 |
SendConfig();
|
|
106 |
break;
|
|
107 |
}
|
|
108 |
case gtQLocal: {
|
|
109 |
SendQuickConfig();
|
|
110 |
break;
|
|
111 |
}
|
|
112 |
case gtDemo: break;
|
|
113 |
case gtNet: {
|
|
114 |
SendIPC("TN");
|
|
115 |
emit SendNet(QByteArray("\x01""C"));
|
|
116 |
break;
|
|
117 |
}
|
|
118 |
}
|
|
119 |
break;
|
|
120 |
}
|
|
121 |
case 'E': {
|
|
122 |
QMessageBox::critical(0,
|
|
123 |
"Hedgewars: error message",
|
|
124 |
QString().append(msg.mid(2)).left(msg.size() - 6),
|
|
125 |
QMessageBox::Ok,
|
|
126 |
QMessageBox::NoButton,
|
|
127 |
QMessageBox::NoButton);
|
|
128 |
return;
|
|
129 |
}
|
208
|
130 |
case 'K': {
|
|
131 |
ulong kb = msg.mid(2).toULong();
|
|
132 |
if (kb && kb <= KBmsgsCount)
|
|
133 |
{
|
|
134 |
QMessageBox::information(0,
|
|
135 |
"Hedgewars: information",
|
|
136 |
KBMessages[kb - 1],
|
|
137 |
QMessageBox::Ok,
|
|
138 |
QMessageBox::NoButton,
|
|
139 |
QMessageBox::NoButton);
|
|
140 |
}
|
|
141 |
return;
|
|
142 |
}
|
184
|
143 |
case '+': {
|
|
144 |
if (gameType == gtNet)
|
|
145 |
{
|
|
146 |
emit SendNet(msg);
|
|
147 |
}
|
|
148 |
break;
|
|
149 |
}
|
|
150 |
default: {
|
|
151 |
if (gameType == gtNet)
|
|
152 |
{
|
|
153 |
emit SendNet(msg);
|
|
154 |
}
|
|
155 |
demo->append(msg);
|
|
156 |
}
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
void HWGame::FromNet(const QByteArray & msg)
|
|
161 |
{
|
|
162 |
RawSendIPC(msg);
|
|
163 |
}
|
|
164 |
|
|
165 |
void HWGame::onClientRead()
|
|
166 |
{
|
|
167 |
quint8 msglen;
|
|
168 |
quint32 bufsize;
|
|
169 |
while (((bufsize = readbuffer.size()) > 0) &&
|
|
170 |
((msglen = readbuffer.data()[0]) < bufsize))
|
|
171 |
{
|
|
172 |
QByteArray msg = readbuffer.left(msglen + 1);
|
|
173 |
readbuffer.remove(0, msglen + 1);
|
|
174 |
ParseMessage(msg);
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
QStringList HWGame::setArguments()
|
|
179 |
{
|
|
180 |
QStringList arguments;
|
|
181 |
arguments << resolutions[0][config->vid_Resolution()];
|
|
182 |
arguments << resolutions[1][config->vid_Resolution()];
|
|
183 |
arguments << "16";
|
|
184 |
arguments << "46631";
|
|
185 |
arguments << (config->vid_Fullscreen() ? "1" : "0");
|
|
186 |
arguments << (config->isSoundEnabled() ? "1" : "0");
|
|
187 |
arguments << tr("en.txt");
|
|
188 |
arguments << "128";
|
|
189 |
return arguments;
|
|
190 |
}
|
|
191 |
|
|
192 |
void HWGame::AddTeam(const QString & teamname, unsigned char numHedgedogs)
|
|
193 |
{
|
|
194 |
if (TeamCount == 5) return;
|
|
195 |
teams[TeamCount] = teamname;
|
|
196 |
TeamCount++;
|
|
197 |
hdNum[teamname]=numHedgedogs;
|
|
198 |
}
|
|
199 |
|
|
200 |
void HWGame::SaveDemo(const QString & filename)
|
|
201 |
{
|
|
202 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD"));
|
|
203 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD"));
|
|
204 |
|
|
205 |
QFile demofile(filename);
|
|
206 |
if (!demofile.open(QIODevice::WriteOnly))
|
|
207 |
{
|
|
208 |
QMessageBox::critical(0,
|
|
209 |
tr("Error"),
|
|
210 |
tr("Cannot save demo to file %1").arg(filename),
|
|
211 |
tr("Quit"));
|
|
212 |
return ;
|
|
213 |
}
|
|
214 |
QDataStream stream(&demofile);
|
|
215 |
stream.writeRawData(demo->constData(), demo->size());
|
|
216 |
demofile.close();
|
|
217 |
delete demo;
|
|
218 |
}
|
|
219 |
|
|
220 |
void HWGame::PlayDemo(const QString & demofilename)
|
|
221 |
{
|
|
222 |
gameType = gtDemo;
|
|
223 |
QFile demofile(demofilename);
|
|
224 |
if (!demofile.open(QIODevice::ReadOnly))
|
|
225 |
{
|
|
226 |
QMessageBox::critical(0,
|
|
227 |
tr("Error"),
|
|
228 |
tr("Cannot open demofile %1").arg(demofilename),
|
|
229 |
tr("Quit"));
|
|
230 |
return ;
|
|
231 |
}
|
|
232 |
|
|
233 |
// read demo
|
|
234 |
QDataStream stream(&demofile);
|
|
235 |
char buf[512];
|
|
236 |
int readbytes;
|
|
237 |
do
|
|
238 |
{
|
|
239 |
readbytes = stream.readRawData((char *)&buf, 512);
|
|
240 |
toSendBuf.append(QByteArray((char *)&buf, readbytes));
|
|
241 |
//SendIPC(QByteArray((char *)&buf, readbytes));
|
|
242 |
|
|
243 |
} while (readbytes > 0);
|
|
244 |
demofile.close();
|
|
245 |
|
|
246 |
// run engine
|
|
247 |
demo = new QByteArray;
|
|
248 |
Start();
|
|
249 |
}
|
|
250 |
|
|
251 |
void HWGame::StartNet()
|
|
252 |
{
|
|
253 |
gameType = gtNet;
|
|
254 |
demo = new QByteArray;
|
|
255 |
Start();
|
|
256 |
}
|
|
257 |
|
|
258 |
void HWGame::StartLocal()
|
|
259 |
{
|
|
260 |
gameType = gtLocal;
|
|
261 |
if (TeamCount < 2) return;
|
|
262 |
seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
|
|
263 |
demo = new QByteArray;
|
|
264 |
Start();
|
|
265 |
}
|
|
266 |
|
|
267 |
void HWGame::StartQuick()
|
|
268 |
{
|
|
269 |
gameType = gtQLocal;
|
|
270 |
seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString();
|
|
271 |
demo = new QByteArray;
|
|
272 |
Start();
|
|
273 |
}
|
|
274 |
|
|
275 |
|
|
276 |
void HWGame::LocalCFG(const QString & teamname)
|
|
277 |
{
|
|
278 |
HWTeam team(teamname);
|
|
279 |
if (!team.LoadFromFile()) {
|
|
280 |
QMessageBox::critical(0,
|
|
281 |
"Error",
|
|
282 |
QString("Cannot load team config ""%1""").arg(teamname),
|
|
283 |
QMessageBox::Ok,
|
|
284 |
QMessageBox::NoButton,
|
|
285 |
QMessageBox::NoButton);
|
|
286 |
return;
|
|
287 |
}
|
|
288 |
RawSendIPC(team.IPCTeamInfo());
|
|
289 |
}
|
|
290 |
|
|
291 |
void HWGame::LocalCFG(quint8 num)
|
|
292 |
{
|
|
293 |
HWTeam team(num);
|
|
294 |
RawSendIPC(team.IPCTeamInfo());
|
|
295 |
}
|