author | nemo |
Tue, 08 Sep 2009 19:44:49 +0000 | |
changeset 2357 | babe1a55e284 |
parent 2049 | 243d363546e7 |
child 2377 | f3fab2b09e0c |
permissions | -rw-r--r-- |
184 | 1 |
/* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
883 | 3 |
* Copyright (c) 2005-2008 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> |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
21 |
#include <QUuid> |
184 | 22 |
|
23 |
#include "game.h" |
|
24 |
#include "hwconsts.h" |
|
25 |
#include "gameuiconfig.h" |
|
26 |
#include "gamecfgwidget.h" |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
27 |
#include "teamselect.h" |
210 | 28 |
#include "KB.h" |
239 | 29 |
#include "proto.h" |
184 | 30 |
|
706 | 31 |
#include <QTextStream> |
32 |
||
681 | 33 |
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
|
34 |
TCPBase(true), |
1904 | 35 |
ammostr(ammo), |
36 |
m_pTeamSelWidget(pTeamSelWidget) |
|
184 | 37 |
{ |
38 |
this->config = config; |
|
39 |
this->gamecfg = gamecfg; |
|
40 |
TeamCount = 0; |
|
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) { |
1490 | 51 |
case gtDemo: break; |
541 | 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); |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
77 |
|
318 | 78 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 79 |
|
80 |
if (m_pTeamSelWidget) |
|
81 |
{ |
|
352 | 82 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 83 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
84 |
{ |
|
85 |
HWProto::addStringListToBuffer(buf, |
|
86 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
682 | 87 |
HWProto::addStringToBuffer(buf, QString("eammstore %1").arg(ammostr)); |
341 | 88 |
} |
89 |
} |
|
318 | 90 |
RawSendIPC(buf); |
253 | 91 |
} |
92 |
||
93 |
void HWGame::SendConfig() |
|
94 |
{ |
|
95 |
commonConfig(); |
|
184 | 96 |
} |
97 |
||
98 |
void HWGame::SendQuickConfig() |
|
99 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
100 |
QByteArray teamscfg; |
239 | 101 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
102 |
HWProto::addStringToBuffer(teamscfg, "TL"); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
103 |
HWProto::addStringToBuffer(teamscfg, QString("etheme %1") |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
104 |
.arg((Themes->size() > 0) ? Themes->at(rand() % Themes->size()) : "steel")); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
105 |
HWProto::addStringToBuffer(teamscfg, "eseed " + QUuid::createUuid().toString()); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
106 |
|
1907 | 107 |
HWNamegen namegen; |
239 | 108 |
|
1907 | 109 |
HWTeam * team1; |
110 |
team1 = new HWTeam; |
|
111 |
team1->difficulty = 0; |
|
112 |
team1->teamColor = *color1; |
|
113 |
team1->numHedgehogs = 4; |
|
114 |
namegen.TeamRandomNames(team1,TRUE); |
|
607 | 115 |
HWProto::addStringListToBuffer(teamscfg, |
1907 | 116 |
team1->TeamGameConfig(100)); |
117 |
||
118 |
HWTeam * team2; |
|
119 |
team2 = new HWTeam; |
|
120 |
team2->difficulty = 4; |
|
121 |
team2->teamColor = *color2; |
|
122 |
team2->numHedgehogs = 4; |
|
123 |
namegen.TeamRandomNames(team2,TRUE); |
|
124 |
HWProto::addStringListToBuffer(teamscfg, |
|
125 |
team2->TeamGameConfig(100)); |
|
607 | 126 |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1490
diff
changeset
|
127 |
HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1490
diff
changeset
|
128 |
HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore); |
607 | 129 |
RawSendIPC(teamscfg); |
341 | 130 |
} |
131 |
||
588 | 132 |
void HWGame::SendTrainingConfig() |
133 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
134 |
QByteArray traincfg; |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
135 |
HWProto::addStringToBuffer(traincfg, "TL"); |
588 | 136 |
|
2049
243d363546e7
Fix team name in training (broken by ttsmj's random teams patch)
unc0rr
parents:
1907
diff
changeset
|
137 |
HWTeam team1; |
588 | 138 |
team1.difficulty = 0; |
616 | 139 |
team1.teamColor = *color1; |
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
603
diff
changeset
|
140 |
team1.numHedgehogs = 1; |
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
141 |
HWProto::addStringListToBuffer(traincfg, |
588 | 142 |
team1.TeamGameConfig(100)); |
143 |
||
722
993faebbe546
- Use shotgun training as more attractive than the second one with bazooka
unc0rr
parents:
706
diff
changeset
|
144 |
QFile file(datadir->absolutePath() + "/Trainings/001_Shotgun.txt"); |
596 | 145 |
if(!file.open(QFile::ReadOnly)) |
146 |
{ |
|
147 |
emit ErrorMessage(tr("Error reading training config file")); |
|
148 |
return; |
|
149 |
} |
|
150 |
||
151 |
QTextStream stream(&file); |
|
152 |
while(!stream.atEnd()) |
|
153 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
154 |
HWProto::addStringToBuffer(traincfg, "e" + stream.readLine()); |
596 | 155 |
} |
593 | 156 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
157 |
RawSendIPC(traincfg); |
588 | 158 |
} |
159 |
||
341 | 160 |
void HWGame::SendNetConfig() |
161 |
{ |
|
162 |
commonConfig(); |
|
184 | 163 |
} |
164 |
||
165 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
166 |
{ |
|
306 | 167 |
switch(msg.at(1)) { |
184 | 168 |
case '?': { |
395 | 169 |
SendIPC("!"); |
184 | 170 |
break; |
171 |
} |
|
172 |
case 'C': { |
|
173 |
switch (gameType) { |
|
174 |
case gtLocal: { |
|
1358 | 175 |
SendConfig(); |
184 | 176 |
break; |
177 |
} |
|
178 |
case gtQLocal: { |
|
1358 | 179 |
SendQuickConfig(); |
184 | 180 |
break; |
181 |
} |
|
182 |
case gtDemo: break; |
|
183 |
case gtNet: { |
|
341 | 184 |
SendNetConfig(); |
184 | 185 |
break; |
186 |
} |
|
588 | 187 |
case gtTraining: { |
1358 | 188 |
SendTrainingConfig(); |
588 | 189 |
break; |
190 |
} |
|
184 | 191 |
} |
192 |
break; |
|
193 |
} |
|
194 |
case 'E': { |
|
660 | 195 |
int size = msg.size(); |
1277 | 196 |
emit ErrorMessage(QString("Last two engine messages:\n") + QString().append(msg.mid(2)).left(size - 4)); |
184 | 197 |
return; |
198 |
} |
|
208 | 199 |
case 'K': { |
200 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 201 |
if (kb==1) { |
468 | 202 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 203 |
return; |
204 |
} |
|
208 | 205 |
if (kb && kb <= KBmsgsCount) |
206 |
{ |
|
425 | 207 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 208 |
} |
209 |
return; |
|
210 |
} |
|
306 | 211 |
case 'i': { |
660 | 212 |
int size = msg.size(); |
213 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3).left(size - 5))); |
|
306 | 214 |
break; |
215 |
} |
|
324
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(gsInterrupted); |
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 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
220 |
case 'q': { |
533 | 221 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
222 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
223 |
} |
1356 | 224 |
case 's': { |
225 |
int size = msg.size(); |
|
1358 | 226 |
QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4)); |
227 |
emit SendChat(msgbody); |
|
228 |
QByteArray buf; |
|
229 |
HWProto::addStringToBuffer(buf, QString("s%1: %2\x20\x20").arg(config->netNick()).arg(msgbody)); |
|
230 |
demo.append(buf); |
|
231 |
break; |
|
1356 | 232 |
} |
184 | 233 |
default: { |
234 |
if (gameType == gtNet) |
|
235 |
{ |
|
236 |
emit SendNet(msg); |
|
237 |
} |
|
1358 | 238 |
if (msg.at(1) != 's') |
533 | 239 |
demo.append(msg); |
184 | 240 |
} |
241 |
} |
|
242 |
} |
|
243 |
||
244 |
void HWGame::FromNet(const QByteArray & msg) |
|
245 |
{ |
|
246 |
RawSendIPC(msg); |
|
247 |
} |
|
248 |
||
1356 | 249 |
void HWGame::FromNetChat(const QString & msg) |
250 |
{ |
|
251 |
QByteArray buf; |
|
252 |
HWProto::addStringToBuffer(buf, 's' + msg + "\x20\x20"); |
|
253 |
RawSendIPC(buf); |
|
254 |
} |
|
255 |
||
184 | 256 |
void HWGame::onClientRead() |
257 |
{ |
|
258 |
quint8 msglen; |
|
259 |
quint32 bufsize; |
|
406 | 260 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 261 |
((msglen = readbuffer.data()[0]) < bufsize)) |
262 |
{ |
|
263 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
264 |
readbuffer.remove(0, msglen + 1); |
|
265 |
ParseMessage(msg); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
QStringList HWGame::setArguments() |
|
270 |
{ |
|
271 |
QStringList arguments; |
|
555 | 272 |
QRect resolution = config->vid_Resolution(); |
497 | 273 |
arguments << cfgdir->absolutePath(); |
555 | 274 |
arguments << QString::number(resolution.width()); |
275 |
arguments << QString::number(resolution.height()); |
|
759 | 276 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 277 |
arguments << QString("%1").arg(ipc_port); |
184 | 278 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
279 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
280 |
arguments << tr("en.txt"); |
|
1777 | 281 |
arguments << QString::number(config->volume()); // sound volume |
297 | 282 |
arguments << QString::number(config->timerInterval()); |
267 | 283 |
arguments << datadir->absolutePath(); |
297 | 284 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 285 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
949 | 286 |
arguments << config->netNick().toUtf8().toBase64(); |
1129 | 287 |
arguments << (config->isMusicEnabled() ? "1" : "0"); |
1812 | 288 |
arguments << (config->isReducedQuality() ? "1" : "0"); |
184 | 289 |
return arguments; |
290 |
} |
|
291 |
||
341 | 292 |
void HWGame::AddTeam(const QString & teamname) |
184 | 293 |
{ |
294 |
if (TeamCount == 5) return; |
|
295 |
teams[TeamCount] = teamname; |
|
296 |
TeamCount++; |
|
297 |
} |
|
298 |
||
299 |
void HWGame::PlayDemo(const QString & demofilename) |
|
300 |
{ |
|
301 |
gameType = gtDemo; |
|
302 |
QFile demofile(demofilename); |
|
303 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
304 |
{ |
|
425 | 305 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 306 |
return ; |
307 |
} |
|
308 |
||
309 |
// read demo |
|
512 | 310 |
toSendBuf = demofile.readAll(); |
184 | 311 |
|
312 |
// run engine |
|
533 | 313 |
demo.clear(); |
184 | 314 |
Start(); |
533 | 315 |
SetGameState(gsStarted); |
184 | 316 |
} |
317 |
||
318 |
void HWGame::StartNet() |
|
319 |
{ |
|
320 |
gameType = gtNet; |
|
533 | 321 |
demo.clear(); |
184 | 322 |
Start(); |
533 | 323 |
SetGameState(gsStarted); |
184 | 324 |
} |
325 |
||
326 |
void HWGame::StartLocal() |
|
327 |
{ |
|
328 |
gameType = gtLocal; |
|
533 | 329 |
demo.clear(); |
184 | 330 |
Start(); |
533 | 331 |
SetGameState(gsStarted); |
184 | 332 |
} |
333 |
||
334 |
void HWGame::StartQuick() |
|
335 |
{ |
|
336 |
gameType = gtQLocal; |
|
533 | 337 |
demo.clear(); |
184 | 338 |
Start(); |
533 | 339 |
SetGameState(gsStarted); |
184 | 340 |
} |
533 | 341 |
|
587 | 342 |
void HWGame::StartTraining() |
343 |
{ |
|
588 | 344 |
gameType = gtTraining; |
345 |
demo.clear(); |
|
346 |
Start(); |
|
347 |
SetGameState(gsStarted); |
|
587 | 348 |
} |
349 |
||
533 | 350 |
void HWGame::SetGameState(GameState state) |
351 |
{ |
|
352 |
gameState = state; |
|
353 |
emit GameStateChanged(state); |
|
354 |
} |