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