author | unc0rr |
Sun, 31 Aug 2008 19:23:29 +0000 | |
changeset 1245 | d2eca4a053f5 |
parent 1129 | b74ffca22762 |
child 1277 | 752b53481057 |
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), |
681 | 35 |
m_pTeamSelWidget(pTeamSelWidget), |
36 |
ammostr(ammo) |
|
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) { |
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); |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
76 |
|
318 | 77 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 78 |
|
79 |
if (m_pTeamSelWidget) |
|
80 |
{ |
|
352 | 81 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 82 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
83 |
{ |
|
84 |
HWProto::addStringListToBuffer(buf, |
|
85 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
682 | 86 |
HWProto::addStringToBuffer(buf, QString("eammstore %1").arg(ammostr)); |
341 | 87 |
} |
88 |
} |
|
318 | 89 |
RawSendIPC(buf); |
253 | 90 |
} |
91 |
||
92 |
void HWGame::SendConfig() |
|
93 |
{ |
|
94 |
commonConfig(); |
|
184 | 95 |
} |
96 |
||
97 |
void HWGame::SendQuickConfig() |
|
98 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
99 |
QByteArray teamscfg; |
239 | 100 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
101 |
HWProto::addStringToBuffer(teamscfg, "TL"); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
102 |
HWProto::addStringToBuffer(teamscfg, QString("etheme %1") |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
103 |
.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
|
104 |
HWProto::addStringToBuffer(teamscfg, "eseed " + QUuid::createUuid().toString()); |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
105 |
|
239 | 106 |
HWTeam team1(0); |
107 |
team1.difficulty = 0; |
|
616 | 108 |
team1.teamColor = *color1; |
341 | 109 |
team1.numHedgehogs = 4; |
312 | 110 |
HWProto::addStringListToBuffer(teamscfg, |
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
111 |
team1.TeamGameConfig(100)); |
239 | 112 |
|
113 |
HWTeam team2(2); |
|
114 |
team2.difficulty = 4; |
|
616 | 115 |
team2.teamColor = *color2; |
341 | 116 |
team2.numHedgehogs = 4; |
607 | 117 |
HWProto::addStringListToBuffer(teamscfg, |
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
118 |
team2.TeamGameConfig(100)); |
607 | 119 |
|
120 |
HWProto::addStringToBuffer(teamscfg, *cDefaultAmmoStore); |
|
121 |
HWProto::addStringToBuffer(teamscfg, *cDefaultAmmoStore); |
|
122 |
RawSendIPC(teamscfg); |
|
341 | 123 |
} |
124 |
||
588 | 125 |
void HWGame::SendTrainingConfig() |
126 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
127 |
QByteArray traincfg; |
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
128 |
HWProto::addStringToBuffer(traincfg, "TL"); |
588 | 129 |
|
130 |
HWTeam team1(0); |
|
131 |
team1.difficulty = 0; |
|
616 | 132 |
team1.teamColor = *color1; |
604
2f1165467a66
Let hedgehog position be taken from config, still more work is needed
unc0rr
parents:
603
diff
changeset
|
133 |
team1.numHedgehogs = 1; |
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
134 |
HWProto::addStringListToBuffer(traincfg, |
588 | 135 |
team1.TeamGameConfig(100)); |
136 |
||
722
993faebbe546
- Use shotgun training as more attractive than the second one with bazooka
unc0rr
parents:
706
diff
changeset
|
137 |
QFile file(datadir->absolutePath() + "/Trainings/001_Shotgun.txt"); |
596 | 138 |
if(!file.open(QFile::ReadOnly)) |
139 |
{ |
|
140 |
emit ErrorMessage(tr("Error reading training config file")); |
|
141 |
return; |
|
142 |
} |
|
143 |
||
144 |
QTextStream stream(&file); |
|
145 |
while(!stream.atEnd()) |
|
146 |
{ |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
147 |
HWProto::addStringToBuffer(traincfg, "e" + stream.readLine()); |
596 | 148 |
} |
593 | 149 |
|
788
00720357601f
- Get rid of PageSimpleGame, now pressing 'quick game' just starts round
unc0rr
parents:
759
diff
changeset
|
150 |
RawSendIPC(traincfg); |
588 | 151 |
} |
152 |
||
341 | 153 |
void HWGame::SendNetConfig() |
154 |
{ |
|
155 |
commonConfig(); |
|
184 | 156 |
} |
157 |
||
158 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
159 |
{ |
|
306 | 160 |
switch(msg.at(1)) { |
184 | 161 |
case '?': { |
395 | 162 |
SendIPC("!"); |
184 | 163 |
break; |
164 |
} |
|
165 |
case 'C': { |
|
166 |
switch (gameType) { |
|
167 |
case gtLocal: { |
|
168 |
SendConfig(); |
|
169 |
break; |
|
170 |
} |
|
171 |
case gtQLocal: { |
|
172 |
SendQuickConfig(); |
|
173 |
break; |
|
174 |
} |
|
175 |
case gtDemo: break; |
|
176 |
case gtNet: { |
|
341 | 177 |
SendNetConfig(); |
184 | 178 |
break; |
179 |
} |
|
588 | 180 |
case gtTraining: { |
181 |
SendTrainingConfig(); |
|
182 |
break; |
|
183 |
} |
|
184 | 184 |
} |
185 |
break; |
|
186 |
} |
|
187 |
case 'E': { |
|
660 | 188 |
int size = msg.size(); |
189 |
emit ErrorMessage(QString().append(msg.mid(2)).left(size - 4)); |
|
184 | 190 |
return; |
191 |
} |
|
208 | 192 |
case 'K': { |
193 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 194 |
if (kb==1) { |
468 | 195 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 196 |
return; |
197 |
} |
|
208 | 198 |
if (kb && kb <= KBmsgsCount) |
199 |
{ |
|
425 | 200 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 201 |
} |
202 |
return; |
|
203 |
} |
|
184 | 204 |
case '+': { |
205 |
if (gameType == gtNet) |
|
206 |
{ |
|
207 |
emit SendNet(msg); |
|
208 |
} |
|
209 |
break; |
|
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 |
} |
184 | 224 |
default: { |
225 |
if (gameType == gtNet) |
|
226 |
{ |
|
227 |
emit SendNet(msg); |
|
228 |
} |
|
533 | 229 |
demo.append(msg); |
184 | 230 |
} |
231 |
} |
|
232 |
} |
|
233 |
||
234 |
void HWGame::FromNet(const QByteArray & msg) |
|
235 |
{ |
|
236 |
RawSendIPC(msg); |
|
237 |
} |
|
238 |
||
239 |
void HWGame::onClientRead() |
|
240 |
{ |
|
241 |
quint8 msglen; |
|
242 |
quint32 bufsize; |
|
406 | 243 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 244 |
((msglen = readbuffer.data()[0]) < bufsize)) |
245 |
{ |
|
246 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
247 |
readbuffer.remove(0, msglen + 1); |
|
248 |
ParseMessage(msg); |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
QStringList HWGame::setArguments() |
|
253 |
{ |
|
254 |
QStringList arguments; |
|
555 | 255 |
QRect resolution = config->vid_Resolution(); |
497 | 256 |
arguments << cfgdir->absolutePath(); |
555 | 257 |
arguments << QString::number(resolution.width()); |
258 |
arguments << QString::number(resolution.height()); |
|
759 | 259 |
arguments << QString::number(config->bitDepth()); // bpp |
291 | 260 |
arguments << QString("%1").arg(ipc_port); |
184 | 261 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
262 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
263 |
arguments << tr("en.txt"); |
|
296 | 264 |
arguments << "128"; // sound volume |
297 | 265 |
arguments << QString::number(config->timerInterval()); |
267 | 266 |
arguments << datadir->absolutePath(); |
297 | 267 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 268 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
949 | 269 |
arguments << config->netNick().toUtf8().toBase64(); |
1129 | 270 |
arguments << (config->isMusicEnabled() ? "1" : "0"); |
184 | 271 |
return arguments; |
272 |
} |
|
273 |
||
341 | 274 |
void HWGame::AddTeam(const QString & teamname) |
184 | 275 |
{ |
276 |
if (TeamCount == 5) return; |
|
277 |
teams[TeamCount] = teamname; |
|
278 |
TeamCount++; |
|
279 |
} |
|
280 |
||
281 |
void HWGame::PlayDemo(const QString & demofilename) |
|
282 |
{ |
|
283 |
gameType = gtDemo; |
|
284 |
QFile demofile(demofilename); |
|
285 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
286 |
{ |
|
425 | 287 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 288 |
return ; |
289 |
} |
|
290 |
||
291 |
// read demo |
|
512 | 292 |
toSendBuf = demofile.readAll(); |
184 | 293 |
|
294 |
// run engine |
|
533 | 295 |
demo.clear(); |
184 | 296 |
Start(); |
533 | 297 |
SetGameState(gsStarted); |
184 | 298 |
} |
299 |
||
300 |
void HWGame::StartNet() |
|
301 |
{ |
|
302 |
gameType = gtNet; |
|
533 | 303 |
demo.clear(); |
184 | 304 |
Start(); |
533 | 305 |
SetGameState(gsStarted); |
184 | 306 |
} |
307 |
||
308 |
void HWGame::StartLocal() |
|
309 |
{ |
|
310 |
gameType = gtLocal; |
|
533 | 311 |
demo.clear(); |
184 | 312 |
Start(); |
533 | 313 |
SetGameState(gsStarted); |
184 | 314 |
} |
315 |
||
316 |
void HWGame::StartQuick() |
|
317 |
{ |
|
318 |
gameType = gtQLocal; |
|
533 | 319 |
demo.clear(); |
184 | 320 |
Start(); |
533 | 321 |
SetGameState(gsStarted); |
184 | 322 |
} |
533 | 323 |
|
587 | 324 |
void HWGame::StartTraining() |
325 |
{ |
|
588 | 326 |
gameType = gtTraining; |
327 |
demo.clear(); |
|
328 |
Start(); |
|
329 |
SetGameState(gsStarted); |
|
587 | 330 |
} |
331 |
||
533 | 332 |
void HWGame::SetGameState(GameState state) |
333 |
{ |
|
334 |
gameState = state; |
|
335 |
emit GameStateChanged(state); |
|
336 |
} |