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 |
{
|
|
46 |
SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_1");
|
|
47 |
}
|
|
48 |
|
|
49 |
void HWGame::SendTeamConfig(int index)
|
|
50 |
{
|
|
51 |
LocalCFG(teams[index]);
|
|
52 |
}
|
|
53 |
|
|
54 |
void HWGame::SendConfig()
|
|
55 |
{
|
|
56 |
SendIPC(QString("eseed %1").arg(seed).toAscii());
|
228
|
57 |
// SendIPC(QString("emap %1").arg("mushrooms").toAscii());
|
184
|
58 |
SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii());
|
|
59 |
SendIPC("TL");
|
|
60 |
SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii());
|
|
61 |
|
|
62 |
for (int i = 0; i < TeamCount; i++)
|
|
63 |
{
|
239
|
64 |
HWTeam team(teams[i]);
|
|
65 |
team.LoadFromFile();
|
|
66 |
|
|
67 |
QColor clr = m_teamsParams[teams[i]].teamColor;
|
|
68 |
QByteArray buf;
|
|
69 |
QStringList sl = team.TeamGameConfig(clr.rgb()&0xFFFFFF, m_teamsParams[teams[i]].numHedgehogs);
|
|
70 |
HWProto::addStringListToBuffer(buf, sl);
|
|
71 |
RawSendIPC(buf);
|
184
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
void HWGame::SendQuickConfig()
|
|
76 |
{
|
|
77 |
SendIPC(QString("eseed %1").arg(seed).toAscii());
|
|
78 |
SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii());
|
|
79 |
SendIPC("TL");
|
|
80 |
SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii());
|
239
|
81 |
|
|
82 |
QByteArray teamscfg;
|
|
83 |
HWTeam team1(0);
|
|
84 |
team1.difficulty = 0;
|
|
85 |
HWProto::addStringListToBuffer(teamscfg, team1.TeamGameConfig(65535, 4));
|
|
86 |
|
|
87 |
HWTeam team2(2);
|
|
88 |
team2.difficulty = 4;
|
|
89 |
RawSendIPC(HWProto::addStringListToBuffer(teamscfg, team2.TeamGameConfig(16776960, 4)));
|
184
|
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 |
|
213
|
192 |
void HWGame::AddTeam(const QString & teamname, HWTeamTempParams teamParams)
|
184
|
193 |
{
|
|
194 |
if (TeamCount == 5) return;
|
|
195 |
teams[TeamCount] = teamname;
|
|
196 |
TeamCount++;
|
213
|
197 |
m_teamsParams[teamname]=teamParams;
|
184
|
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;
|
239
|
262 |
seed = gamecfg->getCurrentSeed();
|
184
|
263 |
demo = new QByteArray;
|
|
264 |
Start();
|
|
265 |
}
|
|
266 |
|
|
267 |
void HWGame::StartQuick()
|
|
268 |
{
|
|
269 |
gameType = gtQLocal;
|
239
|
270 |
seed = gamecfg->getCurrentSeed();
|
184
|
271 |
demo = new QByteArray;
|
|
272 |
Start();
|
|
273 |
}
|
|
274 |
|
|
275 |
|
|
276 |
void HWGame::LocalCFG(const QString & teamname)
|
|
277 |
{
|
239
|
278 |
QByteArray teamcfg;
|
184
|
279 |
HWTeam team(teamname);
|
239
|
280 |
RawSendIPC(HWProto::addStringListToBuffer(teamcfg, team.TeamGameConfig(16776960, 4)));
|
184
|
281 |
}
|
|
282 |
|