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