author | unc0rr |
Mon, 11 Jun 2007 15:53:29 +0000 | |
changeset 541 | bc7be7f6d3e8 |
parent 533 | eebb7684ac22 |
child 555 | 0376fdcc6322 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
486 | 3 |
* Copyright (c) 2005-2007 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 <QMessageBox> |
|
20 |
#include <QProcess> |
|
21 |
#include <QTimer> |
|
22 |
#include <QString> |
|
23 |
#include <QByteArray> |
|
24 |
#include <QFile> |
|
25 |
#include <QTextStream> |
|
26 |
||
27 |
#include "game.h" |
|
28 |
#include "hwconsts.h" |
|
29 |
#include "gameuiconfig.h" |
|
30 |
#include "gamecfgwidget.h" |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
31 |
#include "teamselect.h" |
210 | 32 |
#include "KB.h" |
239 | 33 |
#include "proto.h" |
184 | 34 |
|
339
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
35 |
HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg, TeamSelWidget* pTeamSelWidget) : |
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
36 |
TCPBase(true), |
7535ab6c3820
Run game message added, team and config info provided for net game
displacer
parents:
324
diff
changeset
|
37 |
m_pTeamSelWidget(pTeamSelWidget) |
184 | 38 |
{ |
39 |
this->config = config; |
|
40 |
this->gamecfg = gamecfg; |
|
41 |
TeamCount = 0; |
|
42 |
seed = ""; |
|
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 |
{ |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
47 |
} |
fdeed9718e6b
virtual destructors for tcpBase derived classes, readarray clear removed as unneeded
displacer
parents:
406
diff
changeset
|
48 |
|
184 | 49 |
void HWGame::onClientDisconnect() |
50 |
{ |
|
541 | 51 |
switch (gameType) { |
52 |
case gtDemo: |
|
53 |
break; |
|
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); |
|
318 | 79 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 80 |
|
81 |
if (m_pTeamSelWidget) |
|
82 |
{ |
|
352 | 83 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 84 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
85 |
{ |
|
86 |
HWProto::addStringListToBuffer(buf, |
|
87 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
88 |
} |
|
89 |
} |
|
318 | 90 |
RawSendIPC(buf); |
253 | 91 |
} |
92 |
||
93 |
void HWGame::SendConfig() |
|
94 |
{ |
|
95 |
commonConfig(); |
|
184 | 96 |
} |
97 |
||
98 |
void HWGame::SendQuickConfig() |
|
99 |
{ |
|
253 | 100 |
commonConfig(); |
239 | 101 |
|
102 |
QByteArray teamscfg; |
|
103 |
HWTeam team1(0); |
|
104 |
team1.difficulty = 0; |
|
341 | 105 |
team1.teamColor = QColor(65535); |
106 |
team1.numHedgehogs = 4; |
|
312 | 107 |
HWProto::addStringListToBuffer(teamscfg, |
341 | 108 |
team1.TeamGameConfig(gamecfg->getInitHealth())); |
239 | 109 |
|
110 |
HWTeam team2(2); |
|
111 |
team2.difficulty = 4; |
|
341 | 112 |
team2.teamColor = QColor(16776960); |
113 |
team2.numHedgehogs = 4; |
|
312 | 114 |
RawSendIPC(HWProto::addStringListToBuffer(teamscfg, |
341 | 115 |
team2.TeamGameConfig(gamecfg->getInitHealth()))); |
116 |
} |
|
117 |
||
118 |
void HWGame::SendNetConfig() |
|
119 |
{ |
|
120 |
commonConfig(); |
|
184 | 121 |
} |
122 |
||
123 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
124 |
{ |
|
306 | 125 |
switch(msg.at(1)) { |
184 | 126 |
case '?': { |
395 | 127 |
SendIPC("!"); |
184 | 128 |
break; |
129 |
} |
|
130 |
case 'C': { |
|
131 |
switch (gameType) { |
|
132 |
case gtLocal: { |
|
133 |
SendConfig(); |
|
134 |
break; |
|
135 |
} |
|
136 |
case gtQLocal: { |
|
137 |
SendQuickConfig(); |
|
138 |
break; |
|
139 |
} |
|
140 |
case gtDemo: break; |
|
141 |
case gtNet: { |
|
341 | 142 |
SendNetConfig(); |
184 | 143 |
break; |
144 |
} |
|
145 |
} |
|
146 |
break; |
|
147 |
} |
|
148 |
case 'E': { |
|
425 | 149 |
emit ErrorMessage(QString().append(msg.mid(2)).left(msg.size() - 6)); |
184 | 150 |
return; |
151 |
} |
|
208 | 152 |
case 'K': { |
153 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 154 |
if (kb==1) { |
468 | 155 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 156 |
return; |
157 |
} |
|
208 | 158 |
if (kb && kb <= KBmsgsCount) |
159 |
{ |
|
425 | 160 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 161 |
} |
162 |
return; |
|
163 |
} |
|
184 | 164 |
case '+': { |
165 |
if (gameType == gtNet) |
|
166 |
{ |
|
167 |
emit SendNet(msg); |
|
168 |
} |
|
169 |
break; |
|
170 |
} |
|
306 | 171 |
case 'i': { |
172 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
173 |
break; |
|
174 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
175 |
case 'Q': { |
533 | 176 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
177 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
178 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
179 |
case 'q': { |
533 | 180 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
181 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
182 |
} |
184 | 183 |
default: { |
184 |
if (gameType == gtNet) |
|
185 |
{ |
|
186 |
emit SendNet(msg); |
|
187 |
} |
|
533 | 188 |
demo.append(msg); |
184 | 189 |
} |
190 |
} |
|
191 |
} |
|
192 |
||
193 |
void HWGame::FromNet(const QByteArray & msg) |
|
194 |
{ |
|
195 |
RawSendIPC(msg); |
|
196 |
} |
|
197 |
||
198 |
void HWGame::onClientRead() |
|
199 |
{ |
|
200 |
quint8 msglen; |
|
201 |
quint32 bufsize; |
|
406 | 202 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 203 |
((msglen = readbuffer.data()[0]) < bufsize)) |
204 |
{ |
|
205 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
206 |
readbuffer.remove(0, msglen + 1); |
|
207 |
ParseMessage(msg); |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
QStringList HWGame::setArguments() |
|
212 |
{ |
|
213 |
QStringList arguments; |
|
497 | 214 |
arguments << cfgdir->absolutePath(); |
184 | 215 |
arguments << resolutions[0][config->vid_Resolution()]; |
216 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 217 |
arguments << "16"; // bpp |
291 | 218 |
arguments << QString("%1").arg(ipc_port); |
184 | 219 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
220 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
221 |
arguments << tr("en.txt"); |
|
296 | 222 |
arguments << "128"; // sound volume |
297 | 223 |
arguments << QString::number(config->timerInterval()); |
267 | 224 |
arguments << datadir->absolutePath(); |
297 | 225 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 226 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
184 | 227 |
return arguments; |
228 |
} |
|
229 |
||
341 | 230 |
void HWGame::AddTeam(const QString & teamname) |
184 | 231 |
{ |
232 |
if (TeamCount == 5) return; |
|
233 |
teams[TeamCount] = teamname; |
|
234 |
TeamCount++; |
|
235 |
} |
|
236 |
||
237 |
void HWGame::PlayDemo(const QString & demofilename) |
|
238 |
{ |
|
239 |
gameType = gtDemo; |
|
240 |
QFile demofile(demofilename); |
|
241 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
242 |
{ |
|
425 | 243 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 244 |
return ; |
245 |
} |
|
246 |
||
247 |
// read demo |
|
512 | 248 |
toSendBuf = demofile.readAll(); |
184 | 249 |
|
250 |
// run engine |
|
533 | 251 |
demo.clear(); |
184 | 252 |
Start(); |
533 | 253 |
SetGameState(gsStarted); |
184 | 254 |
} |
255 |
||
256 |
void HWGame::StartNet() |
|
257 |
{ |
|
258 |
gameType = gtNet; |
|
533 | 259 |
demo.clear(); |
184 | 260 |
Start(); |
533 | 261 |
SetGameState(gsStarted); |
184 | 262 |
} |
263 |
||
264 |
void HWGame::StartLocal() |
|
265 |
{ |
|
266 |
gameType = gtLocal; |
|
239 | 267 |
seed = gamecfg->getCurrentSeed(); |
533 | 268 |
demo.clear(); |
184 | 269 |
Start(); |
533 | 270 |
SetGameState(gsStarted); |
184 | 271 |
} |
272 |
||
273 |
void HWGame::StartQuick() |
|
274 |
{ |
|
275 |
gameType = gtQLocal; |
|
239 | 276 |
seed = gamecfg->getCurrentSeed(); |
533 | 277 |
demo.clear(); |
184 | 278 |
Start(); |
533 | 279 |
SetGameState(gsStarted); |
184 | 280 |
} |
533 | 281 |
|
282 |
void HWGame::SetGameState(GameState state) |
|
283 |
{ |
|
284 |
gameState = state; |
|
285 |
emit GameStateChanged(state); |
|
286 |
} |