author | unc0rr |
Thu, 11 Jan 2007 23:19:18 +0000 | |
changeset 318 | 46a43b02bbb3 |
parent 312 | c36d0b34ac3d |
child 324 | f4c109c82a0c |
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 |
{ |
318 | 57 |
QByteArray buf; |
58 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
|
59 |
HWProto::addStringToBuffer(buf, "TL"); |
|
60 |
RawSendIPC(buf); |
|
253 | 61 |
} |
62 |
||
63 |
void HWGame::SendConfig() |
|
64 |
{ |
|
65 |
commonConfig(); |
|
184 | 66 |
|
67 |
for (int i = 0; i < TeamCount; i++) |
|
68 |
{ |
|
239 | 69 |
HWTeam team(teams[i]); |
70 |
team.LoadFromFile(); |
|
71 |
||
72 |
QColor clr = m_teamsParams[teams[i]].teamColor; |
|
73 |
QByteArray buf; |
|
312 | 74 |
QStringList sl = team.TeamGameConfig(clr.rgb()&0xFFFFFF, |
75 |
m_teamsParams[teams[i]].numHedgehogs, |
|
76 |
gamecfg->getInitHealth()); |
|
239 | 77 |
HWProto::addStringListToBuffer(buf, sl); |
78 |
RawSendIPC(buf); |
|
184 | 79 |
} |
80 |
} |
|
81 |
||
82 |
void HWGame::SendQuickConfig() |
|
83 |
{ |
|
253 | 84 |
commonConfig(); |
239 | 85 |
|
86 |
QByteArray teamscfg; |
|
87 |
HWTeam team1(0); |
|
88 |
team1.difficulty = 0; |
|
312 | 89 |
HWProto::addStringListToBuffer(teamscfg, |
90 |
team1.TeamGameConfig(65535, 4, gamecfg->getInitHealth())); |
|
239 | 91 |
|
92 |
HWTeam team2(2); |
|
93 |
team2.difficulty = 4; |
|
312 | 94 |
RawSendIPC(HWProto::addStringListToBuffer(teamscfg, |
95 |
team2.TeamGameConfig(16776960, 4, gamecfg->getInitHealth()))); |
|
184 | 96 |
} |
97 |
||
98 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
99 |
{ |
|
306 | 100 |
switch(msg.at(1)) { |
184 | 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 |
} |
|
306 | 156 |
case 'i': { |
157 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
158 |
break; |
|
159 |
} |
|
184 | 160 |
default: { |
161 |
if (gameType == gtNet) |
|
162 |
{ |
|
163 |
emit SendNet(msg); |
|
164 |
} |
|
165 |
demo->append(msg); |
|
166 |
} |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
void HWGame::FromNet(const QByteArray & msg) |
|
171 |
{ |
|
172 |
RawSendIPC(msg); |
|
173 |
} |
|
174 |
||
175 |
void HWGame::onClientRead() |
|
176 |
{ |
|
177 |
quint8 msglen; |
|
178 |
quint32 bufsize; |
|
179 |
while (((bufsize = readbuffer.size()) > 0) && |
|
180 |
((msglen = readbuffer.data()[0]) < bufsize)) |
|
181 |
{ |
|
182 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
183 |
readbuffer.remove(0, msglen + 1); |
|
184 |
ParseMessage(msg); |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
QStringList HWGame::setArguments() |
|
189 |
{ |
|
190 |
QStringList arguments; |
|
191 |
arguments << resolutions[0][config->vid_Resolution()]; |
|
192 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 193 |
arguments << "16"; // bpp |
291 | 194 |
arguments << QString("%1").arg(ipc_port); |
184 | 195 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
196 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
197 |
arguments << tr("en.txt"); |
|
296 | 198 |
arguments << "128"; // sound volume |
297 | 199 |
arguments << QString::number(config->timerInterval()); |
267 | 200 |
arguments << datadir->absolutePath(); |
297 | 201 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
184 | 202 |
return arguments; |
203 |
} |
|
204 |
||
213 | 205 |
void HWGame::AddTeam(const QString & teamname, HWTeamTempParams teamParams) |
184 | 206 |
{ |
207 |
if (TeamCount == 5) return; |
|
208 |
teams[TeamCount] = teamname; |
|
209 |
TeamCount++; |
|
213 | 210 |
m_teamsParams[teamname]=teamParams; |
184 | 211 |
} |
212 |
||
213 |
void HWGame::SaveDemo(const QString & filename) |
|
214 |
{ |
|
215 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
|
216 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
217 |
||
218 |
QFile demofile(filename); |
|
219 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
220 |
{ |
|
221 |
QMessageBox::critical(0, |
|
222 |
tr("Error"), |
|
223 |
tr("Cannot save demo to file %1").arg(filename), |
|
224 |
tr("Quit")); |
|
225 |
return ; |
|
226 |
} |
|
227 |
QDataStream stream(&demofile); |
|
228 |
stream.writeRawData(demo->constData(), demo->size()); |
|
229 |
demofile.close(); |
|
230 |
delete demo; |
|
231 |
} |
|
232 |
||
233 |
void HWGame::PlayDemo(const QString & demofilename) |
|
234 |
{ |
|
235 |
gameType = gtDemo; |
|
236 |
QFile demofile(demofilename); |
|
237 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
238 |
{ |
|
239 |
QMessageBox::critical(0, |
|
240 |
tr("Error"), |
|
241 |
tr("Cannot open demofile %1").arg(demofilename), |
|
242 |
tr("Quit")); |
|
243 |
return ; |
|
244 |
} |
|
245 |
||
246 |
// read demo |
|
247 |
QDataStream stream(&demofile); |
|
248 |
char buf[512]; |
|
249 |
int readbytes; |
|
250 |
do |
|
251 |
{ |
|
252 |
readbytes = stream.readRawData((char *)&buf, 512); |
|
253 |
toSendBuf.append(QByteArray((char *)&buf, readbytes)); |
|
254 |
//SendIPC(QByteArray((char *)&buf, readbytes)); |
|
255 |
||
256 |
} while (readbytes > 0); |
|
257 |
demofile.close(); |
|
258 |
||
259 |
// run engine |
|
260 |
demo = new QByteArray; |
|
261 |
Start(); |
|
306 | 262 |
emit GameStateChanged(gsStarted); |
184 | 263 |
} |
264 |
||
265 |
void HWGame::StartNet() |
|
266 |
{ |
|
267 |
gameType = gtNet; |
|
268 |
demo = new QByteArray; |
|
269 |
Start(); |
|
306 | 270 |
emit GameStateChanged(gsStarted); |
184 | 271 |
} |
272 |
||
273 |
void HWGame::StartLocal() |
|
274 |
{ |
|
275 |
gameType = gtLocal; |
|
276 |
if (TeamCount < 2) return; |
|
239 | 277 |
seed = gamecfg->getCurrentSeed(); |
184 | 278 |
demo = new QByteArray; |
279 |
Start(); |
|
306 | 280 |
emit GameStateChanged(gsStarted); |
184 | 281 |
} |
282 |
||
283 |
void HWGame::StartQuick() |
|
284 |
{ |
|
285 |
gameType = gtQLocal; |
|
239 | 286 |
seed = gamecfg->getCurrentSeed(); |
184 | 287 |
demo = new QByteArray; |
288 |
Start(); |
|
306 | 289 |
emit GameStateChanged(gsStarted); |
184 | 290 |
} |
291 |
||
292 |
||
293 |
void HWGame::LocalCFG(const QString & teamname) |
|
294 |
{ |
|
239 | 295 |
QByteArray teamcfg; |
184 | 296 |
HWTeam team(teamname); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
239
diff
changeset
|
297 |
team.LoadFromFile(); |
312 | 298 |
RawSendIPC(HWProto::addStringListToBuffer(teamcfg, |
299 |
team.TeamGameConfig(16776960, 4, gamecfg->getInitHealth()))); |
|
184 | 300 |
} |
301 |