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