author | nemo |
Mon, 01 Mar 2010 00:19:23 +0000 | |
changeset 2892 | ac3b29b10bd9 |
parent 2786 | 85f6425a4d74 |
child 2948 | 3f21a9dc93d0 |
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 |
||
2468
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2443
diff
changeset
|
33 |
QString training; // TODO: Cleaner solution? |
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2443
diff
changeset
|
34 |
|
681 | 35 |
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
|
36 |
TCPBase(true), |
1904 | 37 |
ammostr(ammo), |
38 |
m_pTeamSelWidget(pTeamSelWidget) |
|
184 | 39 |
{ |
40 |
this->config = config; |
|
41 |
this->gamecfg = gamecfg; |
|
42 |
TeamCount = 0; |
|
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 |
{ |
686 | 47 |
SetGameState(gsDestroyed); |
419
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
48 |
} |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
49 |
|
184 | 50 |
void HWGame::onClientDisconnect() |
51 |
{ |
|
541 | 52 |
switch (gameType) { |
1490 | 53 |
case gtDemo: break; |
541 | 54 |
case gtNet: |
55 |
emit HaveRecord(true, demo); |
|
56 |
break; |
|
57 |
default: |
|
58 |
if (gameState == gsInterrupted) emit HaveRecord(false, demo); |
|
59 |
else if (gameState == gsFinished) emit HaveRecord(true, demo); |
|
60 |
} |
|
533 | 61 |
SetGameState(gsStopped); |
184 | 62 |
} |
63 |
||
253 | 64 |
void HWGame::commonConfig() |
184 | 65 |
{ |
318 | 66 |
QByteArray buf; |
341 | 67 |
QString gt; |
68 |
switch (gameType) { |
|
69 |
case gtDemo: |
|
70 |
gt = "TD"; |
|
71 |
break; |
|
72 |
case gtNet: |
|
73 |
gt = "TN"; |
|
74 |
break; |
|
75 |
default: |
|
76 |
gt = "TL"; |
|
77 |
} |
|
78 |
HWProto::addStringToBuffer(buf, gt); |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
79 |
|
318 | 80 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 81 |
|
82 |
if (m_pTeamSelWidget) |
|
83 |
{ |
|
352 | 84 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 85 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
86 |
{ |
|
87 |
HWProto::addStringListToBuffer(buf, |
|
88 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
682 | 89 |
HWProto::addStringToBuffer(buf, QString("eammstore %1").arg(ammostr)); |
341 | 90 |
} |
91 |
} |
|
318 | 92 |
RawSendIPC(buf); |
253 | 93 |
} |
94 |
||
95 |
void HWGame::SendConfig() |
|
96 |
{ |
|
97 |
commonConfig(); |
|
184 | 98 |
} |
99 |
||
100 |
void HWGame::SendQuickConfig() |
|
101 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
102 |
QByteArray teamscfg; |
239 | 103 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
104 |
HWProto::addStringToBuffer(teamscfg, "TL"); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
105 |
HWProto::addStringToBuffer(teamscfg, QString("etheme %1") |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
106 |
.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
|
107 |
HWProto::addStringToBuffer(teamscfg, "eseed " + QUuid::createUuid().toString()); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
108 |
|
1907 | 109 |
HWNamegen namegen; |
239 | 110 |
|
1907 | 111 |
HWTeam * team1; |
112 |
team1 = new HWTeam; |
|
113 |
team1->difficulty = 0; |
|
114 |
team1->teamColor = *color1; |
|
115 |
team1->numHedgehogs = 4; |
|
116 |
namegen.TeamRandomNames(team1,TRUE); |
|
607 | 117 |
HWProto::addStringListToBuffer(teamscfg, |
1907 | 118 |
team1->TeamGameConfig(100)); |
119 |
||
120 |
HWTeam * team2; |
|
121 |
team2 = new HWTeam; |
|
122 |
team2->difficulty = 4; |
|
123 |
team2->teamColor = *color2; |
|
124 |
team2->numHedgehogs = 4; |
|
125 |
namegen.TeamRandomNames(team2,TRUE); |
|
126 |
HWProto::addStringListToBuffer(teamscfg, |
|
127 |
team2->TeamGameConfig(100)); |
|
607 | 128 |
|
1576
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1490
diff
changeset
|
129 |
HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore); |
a02353129a41
Check for deprecated ammo schemes at startup and delete them
unc0rr
parents:
1490
diff
changeset
|
130 |
HWProto::addStringToBuffer(teamscfg, "eammstore " + *cDefaultAmmoStore); |
607 | 131 |
RawSendIPC(teamscfg); |
341 | 132 |
} |
133 |
||
588 | 134 |
void HWGame::SendTrainingConfig() |
135 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
136 |
QByteArray traincfg; |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
137 |
HWProto::addStringToBuffer(traincfg, "TL"); |
588 | 138 |
|
2786 | 139 |
HWProto::addStringToBuffer(traincfg, "escript " + datadir->absolutePath() + "/Missions/" + training + ".hwt"); |
593 | 140 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
141 |
RawSendIPC(traincfg); |
588 | 142 |
} |
143 |
||
341 | 144 |
void HWGame::SendNetConfig() |
145 |
{ |
|
146 |
commonConfig(); |
|
184 | 147 |
} |
148 |
||
149 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
150 |
{ |
|
306 | 151 |
switch(msg.at(1)) { |
184 | 152 |
case '?': { |
395 | 153 |
SendIPC("!"); |
184 | 154 |
break; |
155 |
} |
|
156 |
case 'C': { |
|
157 |
switch (gameType) { |
|
158 |
case gtLocal: { |
|
1358 | 159 |
SendConfig(); |
184 | 160 |
break; |
161 |
} |
|
162 |
case gtQLocal: { |
|
1358 | 163 |
SendQuickConfig(); |
184 | 164 |
break; |
165 |
} |
|
166 |
case gtDemo: break; |
|
167 |
case gtNet: { |
|
341 | 168 |
SendNetConfig(); |
184 | 169 |
break; |
170 |
} |
|
588 | 171 |
case gtTraining: { |
1358 | 172 |
SendTrainingConfig(); |
588 | 173 |
break; |
174 |
} |
|
184 | 175 |
} |
176 |
break; |
|
177 |
} |
|
178 |
case 'E': { |
|
660 | 179 |
int size = msg.size(); |
1277 | 180 |
emit ErrorMessage(QString("Last two engine messages:\n") + QString().append(msg.mid(2)).left(size - 4)); |
184 | 181 |
return; |
182 |
} |
|
208 | 183 |
case 'K': { |
184 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 185 |
if (kb==1) { |
468 | 186 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 187 |
return; |
188 |
} |
|
208 | 189 |
if (kb && kb <= KBmsgsCount) |
190 |
{ |
|
425 | 191 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 192 |
} |
193 |
return; |
|
194 |
} |
|
306 | 195 |
case 'i': { |
2382 | 196 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
306 | 197 |
break; |
198 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
199 |
case 'Q': { |
533 | 200 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
201 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
202 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
203 |
case 'q': { |
533 | 204 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
205 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
206 |
} |
1356 | 207 |
case 's': { |
208 |
int size = msg.size(); |
|
1358 | 209 |
QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4)); |
210 |
emit SendChat(msgbody); |
|
2403 | 211 |
// FIXME: /me command doesn't work here |
2405 | 212 |
QByteArray buf; |
213 |
HWProto::addStringToBuffer(buf, "s" + HWProto::formatChatMsg(config->netNick(), msgbody) + "\x20\x20"); |
|
214 |
demo.append(buf); |
|
2403 | 215 |
break; |
216 |
} |
|
217 |
case 'b': { |
|
218 |
int size = msg.size(); |
|
219 |
QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4)); |
|
220 |
emit SendTeamMessage(msgbody); |
|
1358 | 221 |
break; |
1356 | 222 |
} |
184 | 223 |
default: { |
224 |
if (gameType == gtNet) |
|
225 |
{ |
|
226 |
emit SendNet(msg); |
|
227 |
} |
|
1358 | 228 |
if (msg.at(1) != 's') |
533 | 229 |
demo.append(msg); |
184 | 230 |
} |
231 |
} |
|
232 |
} |
|
233 |
||
234 |
void HWGame::FromNet(const QByteArray & msg) |
|
235 |
{ |
|
236 |
RawSendIPC(msg); |
|
237 |
} |
|
238 |
||
1356 | 239 |
void HWGame::FromNetChat(const QString & msg) |
240 |
{ |
|
241 |
QByteArray buf; |
|
242 |
HWProto::addStringToBuffer(buf, 's' + msg + "\x20\x20"); |
|
243 |
RawSendIPC(buf); |
|
244 |
} |
|
245 |
||
184 | 246 |
void HWGame::onClientRead() |
247 |
{ |
|
248 |
quint8 msglen; |
|
249 |
quint32 bufsize; |
|
406 | 250 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 251 |
((msglen = readbuffer.data()[0]) < bufsize)) |
252 |
{ |
|
253 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
254 |
readbuffer.remove(0, msglen + 1); |
|
255 |
ParseMessage(msg); |
|
256 |
} |
|
257 |
} |
|
258 |
||
259 |
QStringList HWGame::setArguments() |
|
260 |
{ |
|
261 |
QStringList arguments; |
|
555 | 262 |
QRect resolution = config->vid_Resolution(); |
497 | 263 |
arguments << cfgdir->absolutePath(); |
555 | 264 |
arguments << QString::number(resolution.width()); |
265 |
arguments << QString::number(resolution.height()); |
|
759 | 266 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 267 |
arguments << QString("%1").arg(ipc_port); |
184 | 268 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
269 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
2443
fececcbc2189
Smaxx patch for fixing all sound related issues on any system
koda
parents:
2428
diff
changeset
|
270 |
#ifdef _WIN32 |
2392
a55dbef5cf31
Smaxx's patch fixing openal sound issues with poor card drivers
koda
parents:
2384
diff
changeset
|
271 |
arguments << (config->isSoundHardware() ? "1" : "0"); |
2443
fececcbc2189
Smaxx patch for fixing all sound related issues on any system
koda
parents:
2428
diff
changeset
|
272 |
#else |
fececcbc2189
Smaxx patch for fixing all sound related issues on any system
koda
parents:
2428
diff
changeset
|
273 |
arguments << "0"; |
fececcbc2189
Smaxx patch for fixing all sound related issues on any system
koda
parents:
2428
diff
changeset
|
274 |
#endif |
2747 | 275 |
arguments << (config->isWeaponTooltip() ? "1" : "0"); |
184 | 276 |
arguments << tr("en.txt"); |
1777 | 277 |
arguments << QString::number(config->volume()); // sound volume |
297 | 278 |
arguments << QString::number(config->timerInterval()); |
267 | 279 |
arguments << datadir->absolutePath(); |
297 | 280 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 281 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
949 | 282 |
arguments << config->netNick().toUtf8().toBase64(); |
1129 | 283 |
arguments << (config->isMusicEnabled() ? "1" : "0"); |
1812 | 284 |
arguments << (config->isReducedQuality() ? "1" : "0"); |
184 | 285 |
return arguments; |
286 |
} |
|
287 |
||
341 | 288 |
void HWGame::AddTeam(const QString & teamname) |
184 | 289 |
{ |
290 |
if (TeamCount == 5) return; |
|
291 |
teams[TeamCount] = teamname; |
|
292 |
TeamCount++; |
|
293 |
} |
|
294 |
||
295 |
void HWGame::PlayDemo(const QString & demofilename) |
|
296 |
{ |
|
297 |
gameType = gtDemo; |
|
298 |
QFile demofile(demofilename); |
|
299 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
300 |
{ |
|
425 | 301 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 302 |
return ; |
303 |
} |
|
304 |
||
305 |
// read demo |
|
512 | 306 |
toSendBuf = demofile.readAll(); |
184 | 307 |
|
308 |
// run engine |
|
533 | 309 |
demo.clear(); |
184 | 310 |
Start(); |
533 | 311 |
SetGameState(gsStarted); |
184 | 312 |
} |
313 |
||
314 |
void HWGame::StartNet() |
|
315 |
{ |
|
316 |
gameType = gtNet; |
|
533 | 317 |
demo.clear(); |
184 | 318 |
Start(); |
533 | 319 |
SetGameState(gsStarted); |
184 | 320 |
} |
321 |
||
322 |
void HWGame::StartLocal() |
|
323 |
{ |
|
324 |
gameType = gtLocal; |
|
533 | 325 |
demo.clear(); |
184 | 326 |
Start(); |
533 | 327 |
SetGameState(gsStarted); |
184 | 328 |
} |
329 |
||
330 |
void HWGame::StartQuick() |
|
331 |
{ |
|
332 |
gameType = gtQLocal; |
|
533 | 333 |
demo.clear(); |
184 | 334 |
Start(); |
533 | 335 |
SetGameState(gsStarted); |
184 | 336 |
} |
533 | 337 |
|
2468
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2443
diff
changeset
|
338 |
void HWGame::StartTraining(const QString & file) |
587 | 339 |
{ |
588 | 340 |
gameType = gtTraining; |
2468
0b62498c201a
openal fix, training map selection and rcplane adjustments from Smaxx (untested, but look reasonable). Bunch of new graphics from Tiy, new translation for pt-pt from inu_
nemo
parents:
2443
diff
changeset
|
341 |
training = file; |
588 | 342 |
demo.clear(); |
343 |
Start(); |
|
344 |
SetGameState(gsStarted); |
|
587 | 345 |
} |
346 |
||
533 | 347 |
void HWGame::SetGameState(GameState state) |
348 |
{ |
|
349 |
gameState = state; |
|
350 |
emit GameStateChanged(state); |
|
351 |
} |