author | unc0rr |
Fri, 16 Oct 2009 13:01:28 +0000 | |
changeset 2482 | 36c44f4156ad |
parent 2468 | 0b62498c201a |
child 2584 | cc049fbb65ef |
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 |
|
2049
243d363546e7
Fix team name in training (broken by ttsmj's random teams patch)
unc0rr
parents:
1907
diff
changeset
|
139 |
HWTeam team1; |
588 | 140 |
team1.difficulty = 0; |
616 | 141 |
team1.teamColor = *color1; |
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
603
diff
changeset
|
142 |
team1.numHedgehogs = 1; |
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
143 |
HWProto::addStringListToBuffer(traincfg, |
588 | 144 |
team1.TeamGameConfig(100)); |
145 |
||
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
|
146 |
QFile file(datadir->absolutePath() + "/Trainings/" + training + ".txt"); |
596 | 147 |
if(!file.open(QFile::ReadOnly)) |
148 |
{ |
|
149 |
emit ErrorMessage(tr("Error reading training config file")); |
|
150 |
return; |
|
151 |
} |
|
2377 | 152 |
|
596 | 153 |
QTextStream stream(&file); |
154 |
while(!stream.atEnd()) |
|
155 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
156 |
HWProto::addStringToBuffer(traincfg, "e" + stream.readLine()); |
596 | 157 |
} |
593 | 158 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
159 |
RawSendIPC(traincfg); |
588 | 160 |
} |
161 |
||
341 | 162 |
void HWGame::SendNetConfig() |
163 |
{ |
|
164 |
commonConfig(); |
|
184 | 165 |
} |
166 |
||
167 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
168 |
{ |
|
306 | 169 |
switch(msg.at(1)) { |
184 | 170 |
case '?': { |
395 | 171 |
SendIPC("!"); |
184 | 172 |
break; |
173 |
} |
|
174 |
case 'C': { |
|
175 |
switch (gameType) { |
|
176 |
case gtLocal: { |
|
1358 | 177 |
SendConfig(); |
184 | 178 |
break; |
179 |
} |
|
180 |
case gtQLocal: { |
|
1358 | 181 |
SendQuickConfig(); |
184 | 182 |
break; |
183 |
} |
|
184 |
case gtDemo: break; |
|
185 |
case gtNet: { |
|
341 | 186 |
SendNetConfig(); |
184 | 187 |
break; |
188 |
} |
|
588 | 189 |
case gtTraining: { |
1358 | 190 |
SendTrainingConfig(); |
588 | 191 |
break; |
192 |
} |
|
184 | 193 |
} |
194 |
break; |
|
195 |
} |
|
196 |
case 'E': { |
|
660 | 197 |
int size = msg.size(); |
1277 | 198 |
emit ErrorMessage(QString("Last two engine messages:\n") + QString().append(msg.mid(2)).left(size - 4)); |
184 | 199 |
return; |
200 |
} |
|
208 | 201 |
case 'K': { |
202 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 203 |
if (kb==1) { |
468 | 204 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 205 |
return; |
206 |
} |
|
208 | 207 |
if (kb && kb <= KBmsgsCount) |
208 |
{ |
|
425 | 209 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 210 |
} |
211 |
return; |
|
212 |
} |
|
306 | 213 |
case 'i': { |
2382 | 214 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
306 | 215 |
break; |
216 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
217 |
case 'Q': { |
533 | 218 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
219 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
220 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
221 |
case 'q': { |
533 | 222 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
223 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
224 |
} |
1356 | 225 |
case 's': { |
226 |
int size = msg.size(); |
|
1358 | 227 |
QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4)); |
228 |
emit SendChat(msgbody); |
|
2403 | 229 |
// FIXME: /me command doesn't work here |
2405 | 230 |
QByteArray buf; |
231 |
HWProto::addStringToBuffer(buf, "s" + HWProto::formatChatMsg(config->netNick(), msgbody) + "\x20\x20"); |
|
232 |
demo.append(buf); |
|
2403 | 233 |
break; |
234 |
} |
|
235 |
case 'b': { |
|
236 |
int size = msg.size(); |
|
237 |
QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4)); |
|
238 |
emit SendTeamMessage(msgbody); |
|
1358 | 239 |
break; |
1356 | 240 |
} |
184 | 241 |
default: { |
242 |
if (gameType == gtNet) |
|
243 |
{ |
|
244 |
emit SendNet(msg); |
|
245 |
} |
|
1358 | 246 |
if (msg.at(1) != 's') |
533 | 247 |
demo.append(msg); |
184 | 248 |
} |
249 |
} |
|
250 |
} |
|
251 |
||
252 |
void HWGame::FromNet(const QByteArray & msg) |
|
253 |
{ |
|
254 |
RawSendIPC(msg); |
|
255 |
} |
|
256 |
||
1356 | 257 |
void HWGame::FromNetChat(const QString & msg) |
258 |
{ |
|
259 |
QByteArray buf; |
|
260 |
HWProto::addStringToBuffer(buf, 's' + msg + "\x20\x20"); |
|
261 |
RawSendIPC(buf); |
|
262 |
} |
|
263 |
||
184 | 264 |
void HWGame::onClientRead() |
265 |
{ |
|
266 |
quint8 msglen; |
|
267 |
quint32 bufsize; |
|
406 | 268 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 269 |
((msglen = readbuffer.data()[0]) < bufsize)) |
270 |
{ |
|
271 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
272 |
readbuffer.remove(0, msglen + 1); |
|
273 |
ParseMessage(msg); |
|
274 |
} |
|
275 |
} |
|
276 |
||
277 |
QStringList HWGame::setArguments() |
|
278 |
{ |
|
279 |
QStringList arguments; |
|
555 | 280 |
QRect resolution = config->vid_Resolution(); |
497 | 281 |
arguments << cfgdir->absolutePath(); |
555 | 282 |
arguments << QString::number(resolution.width()); |
283 |
arguments << QString::number(resolution.height()); |
|
759 | 284 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 285 |
arguments << QString("%1").arg(ipc_port); |
184 | 286 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
287 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
2443
fececcbc2189
Smaxx patch for fixing all sound related issues on any system
koda
parents:
2428
diff
changeset
|
288 |
#ifdef _WIN32 |
2392
a55dbef5cf31
Smaxx's patch fixing openal sound issues with poor card drivers
koda
parents:
2384
diff
changeset
|
289 |
arguments << (config->isSoundHardware() ? "1" : "0"); |
2443
fececcbc2189
Smaxx patch for fixing all sound related issues on any system
koda
parents:
2428
diff
changeset
|
290 |
#else |
fececcbc2189
Smaxx patch for fixing all sound related issues on any system
koda
parents:
2428
diff
changeset
|
291 |
arguments << "0"; |
fececcbc2189
Smaxx patch for fixing all sound related issues on any system
koda
parents:
2428
diff
changeset
|
292 |
#endif |
184 | 293 |
arguments << tr("en.txt"); |
1777 | 294 |
arguments << QString::number(config->volume()); // sound volume |
297 | 295 |
arguments << QString::number(config->timerInterval()); |
267 | 296 |
arguments << datadir->absolutePath(); |
297 | 297 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 298 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
949 | 299 |
arguments << config->netNick().toUtf8().toBase64(); |
1129 | 300 |
arguments << (config->isMusicEnabled() ? "1" : "0"); |
1812 | 301 |
arguments << (config->isReducedQuality() ? "1" : "0"); |
184 | 302 |
return arguments; |
303 |
} |
|
304 |
||
341 | 305 |
void HWGame::AddTeam(const QString & teamname) |
184 | 306 |
{ |
307 |
if (TeamCount == 5) return; |
|
308 |
teams[TeamCount] = teamname; |
|
309 |
TeamCount++; |
|
310 |
} |
|
311 |
||
312 |
void HWGame::PlayDemo(const QString & demofilename) |
|
313 |
{ |
|
314 |
gameType = gtDemo; |
|
315 |
QFile demofile(demofilename); |
|
316 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
317 |
{ |
|
425 | 318 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 319 |
return ; |
320 |
} |
|
321 |
||
322 |
// read demo |
|
512 | 323 |
toSendBuf = demofile.readAll(); |
184 | 324 |
|
325 |
// run engine |
|
533 | 326 |
demo.clear(); |
184 | 327 |
Start(); |
533 | 328 |
SetGameState(gsStarted); |
184 | 329 |
} |
330 |
||
331 |
void HWGame::StartNet() |
|
332 |
{ |
|
333 |
gameType = gtNet; |
|
533 | 334 |
demo.clear(); |
184 | 335 |
Start(); |
533 | 336 |
SetGameState(gsStarted); |
184 | 337 |
} |
338 |
||
339 |
void HWGame::StartLocal() |
|
340 |
{ |
|
341 |
gameType = gtLocal; |
|
533 | 342 |
demo.clear(); |
184 | 343 |
Start(); |
533 | 344 |
SetGameState(gsStarted); |
184 | 345 |
} |
346 |
||
347 |
void HWGame::StartQuick() |
|
348 |
{ |
|
349 |
gameType = gtQLocal; |
|
533 | 350 |
demo.clear(); |
184 | 351 |
Start(); |
533 | 352 |
SetGameState(gsStarted); |
184 | 353 |
} |
533 | 354 |
|
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
|
355 |
void HWGame::StartTraining(const QString & file) |
587 | 356 |
{ |
588 | 357 |
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
|
358 |
training = file; |
588 | 359 |
demo.clear(); |
360 |
Start(); |
|
361 |
SetGameState(gsStarted); |
|
587 | 362 |
} |
363 |
||
533 | 364 |
void HWGame::SetGameState(GameState state) |
365 |
{ |
|
366 |
gameState = state; |
|
367 |
emit GameStateChanged(state); |
|
368 |
} |