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