author | unc0rr |
Tue, 17 Jun 2008 21:30:34 +0000 | |
changeset 1006 | 8a0ed701eafb |
parent 949 | 866729775535 |
child 1066 | 1f1b3686a2b0 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
2 |
* Hedgewars, a worms-like 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(); |
184 | 270 |
return arguments; |
271 |
} |
|
272 |
||
341 | 273 |
void HWGame::AddTeam(const QString & teamname) |
184 | 274 |
{ |
275 |
if (TeamCount == 5) return; |
|
276 |
teams[TeamCount] = teamname; |
|
277 |
TeamCount++; |
|
278 |
} |
|
279 |
||
280 |
void HWGame::PlayDemo(const QString & demofilename) |
|
281 |
{ |
|
282 |
gameType = gtDemo; |
|
283 |
QFile demofile(demofilename); |
|
284 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
285 |
{ |
|
425 | 286 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 287 |
return ; |
288 |
} |
|
289 |
||
290 |
// read demo |
|
512 | 291 |
toSendBuf = demofile.readAll(); |
184 | 292 |
|
293 |
// run engine |
|
533 | 294 |
demo.clear(); |
184 | 295 |
Start(); |
533 | 296 |
SetGameState(gsStarted); |
184 | 297 |
} |
298 |
||
299 |
void HWGame::StartNet() |
|
300 |
{ |
|
301 |
gameType = gtNet; |
|
533 | 302 |
demo.clear(); |
184 | 303 |
Start(); |
533 | 304 |
SetGameState(gsStarted); |
184 | 305 |
} |
306 |
||
307 |
void HWGame::StartLocal() |
|
308 |
{ |
|
309 |
gameType = gtLocal; |
|
533 | 310 |
demo.clear(); |
184 | 311 |
Start(); |
533 | 312 |
SetGameState(gsStarted); |
184 | 313 |
} |
314 |
||
315 |
void HWGame::StartQuick() |
|
316 |
{ |
|
317 |
gameType = gtQLocal; |
|
533 | 318 |
demo.clear(); |
184 | 319 |
Start(); |
533 | 320 |
SetGameState(gsStarted); |
184 | 321 |
} |
533 | 322 |
|
587 | 323 |
void HWGame::StartTraining() |
324 |
{ |
|
588 | 325 |
gameType = gtTraining; |
326 |
demo.clear(); |
|
327 |
Start(); |
|
328 |
SetGameState(gsStarted); |
|
587 | 329 |
} |
330 |
||
533 | 331 |
void HWGame::SetGameState(GameState state) |
332 |
{ |
|
333 |
gameState = state; |
|
334 |
emit GameStateChanged(state); |
|
335 |
} |