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