author | unc0rr |
Mon, 12 Oct 2009 16:44:30 +0000 | |
changeset 2428 | 6800f8aa0184 |
parent 2405 | 3b8607ac32c6 |
child 2443 | fececcbc2189 |
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 |
||
2428 | 144 |
QFile file(datadir->absolutePath() + "/Trainings/003_RCPlane.txt"); |
596 | 145 |
if(!file.open(QFile::ReadOnly)) |
146 |
{ |
|
147 |
emit ErrorMessage(tr("Error reading training config file")); |
|
148 |
return; |
|
149 |
} |
|
2377 | 150 |
|
596 | 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': { |
2382 | 212 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
306 | 213 |
break; |
214 |
} |
|
324
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(gsInterrupted); |
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 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
219 |
case 'q': { |
533 | 220 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
221 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
222 |
} |
1356 | 223 |
case 's': { |
224 |
int size = msg.size(); |
|
1358 | 225 |
QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4)); |
226 |
emit SendChat(msgbody); |
|
2403 | 227 |
// FIXME: /me command doesn't work here |
2405 | 228 |
QByteArray buf; |
229 |
HWProto::addStringToBuffer(buf, "s" + HWProto::formatChatMsg(config->netNick(), msgbody) + "\x20\x20"); |
|
230 |
demo.append(buf); |
|
2403 | 231 |
break; |
232 |
} |
|
233 |
case 'b': { |
|
234 |
int size = msg.size(); |
|
235 |
QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4)); |
|
236 |
emit SendTeamMessage(msgbody); |
|
1358 | 237 |
break; |
1356 | 238 |
} |
184 | 239 |
default: { |
240 |
if (gameType == gtNet) |
|
241 |
{ |
|
242 |
emit SendNet(msg); |
|
243 |
} |
|
1358 | 244 |
if (msg.at(1) != 's') |
533 | 245 |
demo.append(msg); |
184 | 246 |
} |
247 |
} |
|
248 |
} |
|
249 |
||
250 |
void HWGame::FromNet(const QByteArray & msg) |
|
251 |
{ |
|
252 |
RawSendIPC(msg); |
|
253 |
} |
|
254 |
||
1356 | 255 |
void HWGame::FromNetChat(const QString & msg) |
256 |
{ |
|
257 |
QByteArray buf; |
|
258 |
HWProto::addStringToBuffer(buf, 's' + msg + "\x20\x20"); |
|
259 |
RawSendIPC(buf); |
|
260 |
} |
|
261 |
||
184 | 262 |
void HWGame::onClientRead() |
263 |
{ |
|
264 |
quint8 msglen; |
|
265 |
quint32 bufsize; |
|
406 | 266 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 267 |
((msglen = readbuffer.data()[0]) < bufsize)) |
268 |
{ |
|
269 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
270 |
readbuffer.remove(0, msglen + 1); |
|
271 |
ParseMessage(msg); |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
QStringList HWGame::setArguments() |
|
276 |
{ |
|
277 |
QStringList arguments; |
|
555 | 278 |
QRect resolution = config->vid_Resolution(); |
497 | 279 |
arguments << cfgdir->absolutePath(); |
555 | 280 |
arguments << QString::number(resolution.width()); |
281 |
arguments << QString::number(resolution.height()); |
|
759 | 282 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 283 |
arguments << QString("%1").arg(ipc_port); |
184 | 284 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
285 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
2392
a55dbef5cf31
Smaxx's patch fixing openal sound issues with poor card drivers
koda
parents:
2384
diff
changeset
|
286 |
arguments << (config->isSoundHardware() ? "1" : "0"); |
184 | 287 |
arguments << tr("en.txt"); |
1777 | 288 |
arguments << QString::number(config->volume()); // sound volume |
297 | 289 |
arguments << QString::number(config->timerInterval()); |
267 | 290 |
arguments << datadir->absolutePath(); |
297 | 291 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 292 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
949 | 293 |
arguments << config->netNick().toUtf8().toBase64(); |
1129 | 294 |
arguments << (config->isMusicEnabled() ? "1" : "0"); |
1812 | 295 |
arguments << (config->isReducedQuality() ? "1" : "0"); |
184 | 296 |
return arguments; |
297 |
} |
|
298 |
||
341 | 299 |
void HWGame::AddTeam(const QString & teamname) |
184 | 300 |
{ |
301 |
if (TeamCount == 5) return; |
|
302 |
teams[TeamCount] = teamname; |
|
303 |
TeamCount++; |
|
304 |
} |
|
305 |
||
306 |
void HWGame::PlayDemo(const QString & demofilename) |
|
307 |
{ |
|
308 |
gameType = gtDemo; |
|
309 |
QFile demofile(demofilename); |
|
310 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
311 |
{ |
|
425 | 312 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 313 |
return ; |
314 |
} |
|
315 |
||
316 |
// read demo |
|
512 | 317 |
toSendBuf = demofile.readAll(); |
184 | 318 |
|
319 |
// run engine |
|
533 | 320 |
demo.clear(); |
184 | 321 |
Start(); |
533 | 322 |
SetGameState(gsStarted); |
184 | 323 |
} |
324 |
||
325 |
void HWGame::StartNet() |
|
326 |
{ |
|
327 |
gameType = gtNet; |
|
533 | 328 |
demo.clear(); |
184 | 329 |
Start(); |
533 | 330 |
SetGameState(gsStarted); |
184 | 331 |
} |
332 |
||
333 |
void HWGame::StartLocal() |
|
334 |
{ |
|
335 |
gameType = gtLocal; |
|
533 | 336 |
demo.clear(); |
184 | 337 |
Start(); |
533 | 338 |
SetGameState(gsStarted); |
184 | 339 |
} |
340 |
||
341 |
void HWGame::StartQuick() |
|
342 |
{ |
|
343 |
gameType = gtQLocal; |
|
533 | 344 |
demo.clear(); |
184 | 345 |
Start(); |
533 | 346 |
SetGameState(gsStarted); |
184 | 347 |
} |
533 | 348 |
|
587 | 349 |
void HWGame::StartTraining() |
350 |
{ |
|
588 | 351 |
gameType = gtTraining; |
352 |
demo.clear(); |
|
353 |
Start(); |
|
354 |
SetGameState(gsStarted); |
|
587 | 355 |
} |
356 |
||
533 | 357 |
void HWGame::SetGameState(GameState state) |
358 |
{ |
|
359 |
gameState = state; |
|
360 |
emit GameStateChanged(state); |
|
361 |
} |