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