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