author | unc0rr |
Sun, 16 Dec 2007 15:58:29 +0000 | |
changeset 673 | bd82104a28af |
parent 660 | 7c155e849602 |
child 681 | 7a20c50988ec |
permissions | -rw-r--r-- |
184 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
486 | 3 |
* Copyright (c) 2005-2007 Andrey Korotaev <unC0Rr@gmail.com> |
184 | 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 |
||
27 |
#include "game.h" |
|
28 |
#include "hwconsts.h" |
|
29 |
#include "gameuiconfig.h" |
|
30 |
#include "gamecfgwidget.h" |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
31 |
#include "teamselect.h" |
210 | 32 |
#include "KB.h" |
239 | 33 |
#include "proto.h" |
184 | 34 |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
35 |
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
|
36 |
TCPBase(true), |
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
37 |
m_pTeamSelWidget(pTeamSelWidget) |
184 | 38 |
{ |
39 |
this->config = config; |
|
40 |
this->gamecfg = gamecfg; |
|
41 |
TeamCount = 0; |
|
42 |
seed = ""; |
|
43 |
} |
|
44 |
||
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
45 |
HWGame::~HWGame() |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
46 |
{ |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
47 |
} |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
48 |
|
184 | 49 |
void HWGame::onClientDisconnect() |
50 |
{ |
|
541 | 51 |
switch (gameType) { |
52 |
case gtNet: |
|
53 |
emit HaveRecord(true, demo); |
|
54 |
break; |
|
55 |
default: |
|
56 |
if (gameState == gsInterrupted) emit HaveRecord(false, demo); |
|
57 |
else if (gameState == gsFinished) emit HaveRecord(true, demo); |
|
58 |
} |
|
533 | 59 |
SetGameState(gsStopped); |
184 | 60 |
} |
61 |
||
253 | 62 |
void HWGame::commonConfig() |
184 | 63 |
{ |
318 | 64 |
QByteArray buf; |
341 | 65 |
QString gt; |
66 |
switch (gameType) { |
|
67 |
case gtDemo: |
|
68 |
gt = "TD"; |
|
69 |
break; |
|
70 |
case gtNet: |
|
71 |
gt = "TN"; |
|
72 |
break; |
|
73 |
default: |
|
74 |
gt = "TL"; |
|
75 |
} |
|
76 |
HWProto::addStringToBuffer(buf, gt); |
|
318 | 77 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 78 |
|
79 |
if (m_pTeamSelWidget) |
|
80 |
{ |
|
352 | 81 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 82 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
83 |
{ |
|
84 |
HWProto::addStringListToBuffer(buf, |
|
85 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
607 | 86 |
HWProto::addStringToBuffer(buf, *cDefaultAmmoStore); |
341 | 87 |
} |
88 |
} |
|
318 | 89 |
RawSendIPC(buf); |
253 | 90 |
} |
91 |
||
92 |
void HWGame::SendConfig() |
|
93 |
{ |
|
94 |
commonConfig(); |
|
184 | 95 |
} |
96 |
||
97 |
void HWGame::SendQuickConfig() |
|
98 |
{ |
|
253 | 99 |
commonConfig(); |
239 | 100 |
|
101 |
QByteArray teamscfg; |
|
102 |
HWTeam team1(0); |
|
103 |
team1.difficulty = 0; |
|
616 | 104 |
team1.teamColor = *color1; |
341 | 105 |
team1.numHedgehogs = 4; |
312 | 106 |
HWProto::addStringListToBuffer(teamscfg, |
341 | 107 |
team1.TeamGameConfig(gamecfg->getInitHealth())); |
239 | 108 |
|
109 |
HWTeam team2(2); |
|
110 |
team2.difficulty = 4; |
|
616 | 111 |
team2.teamColor = *color2; |
341 | 112 |
team2.numHedgehogs = 4; |
607 | 113 |
HWProto::addStringListToBuffer(teamscfg, |
114 |
team2.TeamGameConfig(gamecfg->getInitHealth())); |
|
115 |
||
116 |
HWProto::addStringToBuffer(teamscfg, *cDefaultAmmoStore); |
|
117 |
HWProto::addStringToBuffer(teamscfg, *cDefaultAmmoStore); |
|
118 |
RawSendIPC(teamscfg); |
|
341 | 119 |
} |
120 |
||
588 | 121 |
void HWGame::SendTrainingConfig() |
122 |
{ |
|
123 |
QByteArray teamscfg; |
|
124 |
HWProto::addStringToBuffer(teamscfg, "TL"); |
|
125 |
||
126 |
HWTeam team1(0); |
|
127 |
team1.difficulty = 0; |
|
616 | 128 |
team1.teamColor = *color1; |
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
603
diff
changeset
|
129 |
team1.numHedgehogs = 1; |
588 | 130 |
HWProto::addStringListToBuffer(teamscfg, |
131 |
team1.TeamGameConfig(100)); |
|
132 |
||
622 | 133 |
QFile file(datadir->absolutePath() + "/Trainings/002_Bazooka.txt"); |
596 | 134 |
if(!file.open(QFile::ReadOnly)) |
135 |
{ |
|
136 |
emit ErrorMessage(tr("Error reading training config file")); |
|
137 |
return; |
|
138 |
} |
|
139 |
||
140 |
QTextStream stream(&file); |
|
141 |
while(!stream.atEnd()) |
|
142 |
{ |
|
610 | 143 |
HWProto::addStringToBuffer(teamscfg, "e" + stream.readLine()); |
596 | 144 |
} |
593 | 145 |
|
146 |
RawSendIPC(teamscfg); |
|
588 | 147 |
} |
148 |
||
341 | 149 |
void HWGame::SendNetConfig() |
150 |
{ |
|
151 |
commonConfig(); |
|
184 | 152 |
} |
153 |
||
154 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
155 |
{ |
|
306 | 156 |
switch(msg.at(1)) { |
184 | 157 |
case '?': { |
395 | 158 |
SendIPC("!"); |
184 | 159 |
break; |
160 |
} |
|
161 |
case 'C': { |
|
162 |
switch (gameType) { |
|
163 |
case gtLocal: { |
|
164 |
SendConfig(); |
|
165 |
break; |
|
166 |
} |
|
167 |
case gtQLocal: { |
|
168 |
SendQuickConfig(); |
|
169 |
break; |
|
170 |
} |
|
171 |
case gtDemo: break; |
|
172 |
case gtNet: { |
|
341 | 173 |
SendNetConfig(); |
184 | 174 |
break; |
175 |
} |
|
588 | 176 |
case gtTraining: { |
177 |
SendTrainingConfig(); |
|
178 |
break; |
|
179 |
} |
|
184 | 180 |
} |
181 |
break; |
|
182 |
} |
|
183 |
case 'E': { |
|
660 | 184 |
int size = msg.size(); |
185 |
emit ErrorMessage(QString().append(msg.mid(2)).left(size - 4)); |
|
184 | 186 |
return; |
187 |
} |
|
208 | 188 |
case 'K': { |
189 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 190 |
if (kb==1) { |
468 | 191 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 192 |
return; |
193 |
} |
|
208 | 194 |
if (kb && kb <= KBmsgsCount) |
195 |
{ |
|
425 | 196 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 197 |
} |
198 |
return; |
|
199 |
} |
|
184 | 200 |
case '+': { |
201 |
if (gameType == gtNet) |
|
202 |
{ |
|
203 |
emit SendNet(msg); |
|
204 |
} |
|
205 |
break; |
|
206 |
} |
|
306 | 207 |
case 'i': { |
660 | 208 |
int size = msg.size(); |
209 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3).left(size - 5))); |
|
306 | 210 |
break; |
211 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
212 |
case 'Q': { |
533 | 213 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
214 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
215 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
216 |
case 'q': { |
533 | 217 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
218 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
219 |
} |
184 | 220 |
default: { |
221 |
if (gameType == gtNet) |
|
222 |
{ |
|
223 |
emit SendNet(msg); |
|
224 |
} |
|
533 | 225 |
demo.append(msg); |
184 | 226 |
} |
227 |
} |
|
228 |
} |
|
229 |
||
230 |
void HWGame::FromNet(const QByteArray & msg) |
|
231 |
{ |
|
232 |
RawSendIPC(msg); |
|
233 |
} |
|
234 |
||
235 |
void HWGame::onClientRead() |
|
236 |
{ |
|
237 |
quint8 msglen; |
|
238 |
quint32 bufsize; |
|
406 | 239 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 240 |
((msglen = readbuffer.data()[0]) < bufsize)) |
241 |
{ |
|
242 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
243 |
readbuffer.remove(0, msglen + 1); |
|
244 |
ParseMessage(msg); |
|
245 |
} |
|
246 |
} |
|
247 |
||
248 |
QStringList HWGame::setArguments() |
|
249 |
{ |
|
250 |
QStringList arguments; |
|
555 | 251 |
QRect resolution = config->vid_Resolution(); |
497 | 252 |
arguments << cfgdir->absolutePath(); |
555 | 253 |
arguments << QString::number(resolution.width()); |
254 |
arguments << QString::number(resolution.height()); |
|
603 | 255 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 256 |
arguments << QString("%1").arg(ipc_port); |
184 | 257 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
258 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
259 |
arguments << tr("en.txt"); |
|
296 | 260 |
arguments << "128"; // sound volume |
297 | 261 |
arguments << QString::number(config->timerInterval()); |
267 | 262 |
arguments << datadir->absolutePath(); |
297 | 263 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 264 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
184 | 265 |
return arguments; |
266 |
} |
|
267 |
||
341 | 268 |
void HWGame::AddTeam(const QString & teamname) |
184 | 269 |
{ |
270 |
if (TeamCount == 5) return; |
|
271 |
teams[TeamCount] = teamname; |
|
272 |
TeamCount++; |
|
273 |
} |
|
274 |
||
275 |
void HWGame::PlayDemo(const QString & demofilename) |
|
276 |
{ |
|
277 |
gameType = gtDemo; |
|
278 |
QFile demofile(demofilename); |
|
279 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
280 |
{ |
|
425 | 281 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 282 |
return ; |
283 |
} |
|
284 |
||
285 |
// read demo |
|
512 | 286 |
toSendBuf = demofile.readAll(); |
184 | 287 |
|
288 |
// run engine |
|
533 | 289 |
demo.clear(); |
184 | 290 |
Start(); |
533 | 291 |
SetGameState(gsStarted); |
184 | 292 |
} |
293 |
||
294 |
void HWGame::StartNet() |
|
295 |
{ |
|
296 |
gameType = gtNet; |
|
533 | 297 |
demo.clear(); |
184 | 298 |
Start(); |
533 | 299 |
SetGameState(gsStarted); |
184 | 300 |
} |
301 |
||
302 |
void HWGame::StartLocal() |
|
303 |
{ |
|
304 |
gameType = gtLocal; |
|
239 | 305 |
seed = gamecfg->getCurrentSeed(); |
533 | 306 |
demo.clear(); |
184 | 307 |
Start(); |
533 | 308 |
SetGameState(gsStarted); |
184 | 309 |
} |
310 |
||
311 |
void HWGame::StartQuick() |
|
312 |
{ |
|
313 |
gameType = gtQLocal; |
|
239 | 314 |
seed = gamecfg->getCurrentSeed(); |
533 | 315 |
demo.clear(); |
184 | 316 |
Start(); |
533 | 317 |
SetGameState(gsStarted); |
184 | 318 |
} |
533 | 319 |
|
587 | 320 |
void HWGame::StartTraining() |
321 |
{ |
|
588 | 322 |
gameType = gtTraining; |
323 |
seed = "training"; |
|
324 |
demo.clear(); |
|
325 |
Start(); |
|
326 |
SetGameState(gsStarted); |
|
587 | 327 |
} |
328 |
||
533 | 329 |
void HWGame::SetGameState(GameState state) |
330 |
{ |
|
331 |
gameState = state; |
|
332 |
emit GameStateChanged(state); |
|
333 |
} |