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