author | unc0rr |
Sun, 14 Jan 2007 21:51:17 +0000 | |
changeset 337 | a46abb65eb0c |
parent 324 | f4c109c82a0c |
child 339 | 7535ab6c3820 |
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); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
47 |
emit GameStateChanged(gsStopped); |
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 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
160 |
case 'Q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
161 |
emit GameStateChanged(gsInterrupted); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
162 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
163 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
164 |
case 'q': { |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
165 |
emit GameStateChanged(gsFinished); |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
166 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
167 |
} |
184 | 168 |
default: { |
169 |
if (gameType == gtNet) |
|
170 |
{ |
|
171 |
emit SendNet(msg); |
|
172 |
} |
|
173 |
demo->append(msg); |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
void HWGame::FromNet(const QByteArray & msg) |
|
179 |
{ |
|
180 |
RawSendIPC(msg); |
|
181 |
} |
|
182 |
||
183 |
void HWGame::onClientRead() |
|
184 |
{ |
|
185 |
quint8 msglen; |
|
186 |
quint32 bufsize; |
|
187 |
while (((bufsize = readbuffer.size()) > 0) && |
|
188 |
((msglen = readbuffer.data()[0]) < bufsize)) |
|
189 |
{ |
|
190 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
191 |
readbuffer.remove(0, msglen + 1); |
|
192 |
ParseMessage(msg); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
QStringList HWGame::setArguments() |
|
197 |
{ |
|
198 |
QStringList arguments; |
|
199 |
arguments << resolutions[0][config->vid_Resolution()]; |
|
200 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 201 |
arguments << "16"; // bpp |
291 | 202 |
arguments << QString("%1").arg(ipc_port); |
184 | 203 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
204 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
205 |
arguments << tr("en.txt"); |
|
296 | 206 |
arguments << "128"; // sound volume |
297 | 207 |
arguments << QString::number(config->timerInterval()); |
267 | 208 |
arguments << datadir->absolutePath(); |
297 | 209 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
184 | 210 |
return arguments; |
211 |
} |
|
212 |
||
213 | 213 |
void HWGame::AddTeam(const QString & teamname, HWTeamTempParams teamParams) |
184 | 214 |
{ |
215 |
if (TeamCount == 5) return; |
|
216 |
teams[TeamCount] = teamname; |
|
217 |
TeamCount++; |
|
213 | 218 |
m_teamsParams[teamname]=teamParams; |
184 | 219 |
} |
220 |
||
221 |
void HWGame::SaveDemo(const QString & filename) |
|
222 |
{ |
|
223 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
|
224 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
225 |
||
226 |
QFile demofile(filename); |
|
227 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
228 |
{ |
|
229 |
QMessageBox::critical(0, |
|
230 |
tr("Error"), |
|
231 |
tr("Cannot save demo to file %1").arg(filename), |
|
232 |
tr("Quit")); |
|
233 |
return ; |
|
234 |
} |
|
235 |
QDataStream stream(&demofile); |
|
236 |
stream.writeRawData(demo->constData(), demo->size()); |
|
237 |
demofile.close(); |
|
238 |
delete demo; |
|
239 |
} |
|
240 |
||
241 |
void HWGame::PlayDemo(const QString & demofilename) |
|
242 |
{ |
|
243 |
gameType = gtDemo; |
|
244 |
QFile demofile(demofilename); |
|
245 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
246 |
{ |
|
247 |
QMessageBox::critical(0, |
|
248 |
tr("Error"), |
|
249 |
tr("Cannot open demofile %1").arg(demofilename), |
|
250 |
tr("Quit")); |
|
251 |
return ; |
|
252 |
} |
|
253 |
||
254 |
// read demo |
|
255 |
QDataStream stream(&demofile); |
|
256 |
char buf[512]; |
|
257 |
int readbytes; |
|
258 |
do |
|
259 |
{ |
|
260 |
readbytes = stream.readRawData((char *)&buf, 512); |
|
261 |
toSendBuf.append(QByteArray((char *)&buf, readbytes)); |
|
262 |
//SendIPC(QByteArray((char *)&buf, readbytes)); |
|
263 |
||
264 |
} while (readbytes > 0); |
|
265 |
demofile.close(); |
|
266 |
||
267 |
// run engine |
|
268 |
demo = new QByteArray; |
|
269 |
Start(); |
|
306 | 270 |
emit GameStateChanged(gsStarted); |
184 | 271 |
} |
272 |
||
273 |
void HWGame::StartNet() |
|
274 |
{ |
|
275 |
gameType = gtNet; |
|
276 |
demo = new QByteArray; |
|
277 |
Start(); |
|
306 | 278 |
emit GameStateChanged(gsStarted); |
184 | 279 |
} |
280 |
||
281 |
void HWGame::StartLocal() |
|
282 |
{ |
|
283 |
gameType = gtLocal; |
|
284 |
if (TeamCount < 2) return; |
|
239 | 285 |
seed = gamecfg->getCurrentSeed(); |
184 | 286 |
demo = new QByteArray; |
287 |
Start(); |
|
306 | 288 |
emit GameStateChanged(gsStarted); |
184 | 289 |
} |
290 |
||
291 |
void HWGame::StartQuick() |
|
292 |
{ |
|
293 |
gameType = gtQLocal; |
|
239 | 294 |
seed = gamecfg->getCurrentSeed(); |
184 | 295 |
demo = new QByteArray; |
296 |
Start(); |
|
306 | 297 |
emit GameStateChanged(gsStarted); |
184 | 298 |
} |
299 |
||
300 |
||
301 |
void HWGame::LocalCFG(const QString & teamname) |
|
302 |
{ |
|
239 | 303 |
QByteArray teamcfg; |
184 | 304 |
HWTeam team(teamname); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
239
diff
changeset
|
305 |
team.LoadFromFile(); |
312 | 306 |
RawSendIPC(HWProto::addStringListToBuffer(teamcfg, |
307 |
team.TeamGameConfig(16776960, 4, gamecfg->getInitHealth()))); |
|
184 | 308 |
} |
309 |