author | unc0rr |
Mon, 11 Jun 2007 15:52:32 +0000 | |
changeset 540 | b06c5aace2fa |
parent 533 | eebb7684ac22 |
child 541 | bc7be7f6d3e8 |
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 |
{ |
|
533 | 51 |
if (gameState == gsInterrupted) emit HaveRecord(false, demo); |
52 |
else if (gameState == gsFinished) emit HaveRecord(true, demo); |
|
53 |
SetGameState(gsStopped); |
|
184 | 54 |
} |
55 |
||
253 | 56 |
void HWGame::commonConfig() |
184 | 57 |
{ |
318 | 58 |
QByteArray buf; |
341 | 59 |
QString gt; |
60 |
switch (gameType) { |
|
61 |
case gtDemo: |
|
62 |
gt = "TD"; |
|
63 |
break; |
|
64 |
case gtNet: |
|
65 |
gt = "TN"; |
|
66 |
break; |
|
67 |
default: |
|
68 |
gt = "TL"; |
|
69 |
} |
|
70 |
HWProto::addStringToBuffer(buf, gt); |
|
318 | 71 |
HWProto::addStringListToBuffer(buf, gamecfg->getFullConfig()); |
341 | 72 |
|
73 |
if (m_pTeamSelWidget) |
|
74 |
{ |
|
352 | 75 |
QList<HWTeam> teams = m_pTeamSelWidget->getPlayingTeams(); |
341 | 76 |
for(QList<HWTeam>::iterator it = teams.begin(); it != teams.end(); ++it) |
77 |
{ |
|
78 |
HWProto::addStringListToBuffer(buf, |
|
79 |
(*it).TeamGameConfig(gamecfg->getInitHealth())); |
|
80 |
} |
|
81 |
} |
|
318 | 82 |
RawSendIPC(buf); |
253 | 83 |
} |
84 |
||
85 |
void HWGame::SendConfig() |
|
86 |
{ |
|
87 |
commonConfig(); |
|
184 | 88 |
} |
89 |
||
90 |
void HWGame::SendQuickConfig() |
|
91 |
{ |
|
253 | 92 |
commonConfig(); |
239 | 93 |
|
94 |
QByteArray teamscfg; |
|
95 |
HWTeam team1(0); |
|
96 |
team1.difficulty = 0; |
|
341 | 97 |
team1.teamColor = QColor(65535); |
98 |
team1.numHedgehogs = 4; |
|
312 | 99 |
HWProto::addStringListToBuffer(teamscfg, |
341 | 100 |
team1.TeamGameConfig(gamecfg->getInitHealth())); |
239 | 101 |
|
102 |
HWTeam team2(2); |
|
103 |
team2.difficulty = 4; |
|
341 | 104 |
team2.teamColor = QColor(16776960); |
105 |
team2.numHedgehogs = 4; |
|
312 | 106 |
RawSendIPC(HWProto::addStringListToBuffer(teamscfg, |
341 | 107 |
team2.TeamGameConfig(gamecfg->getInitHealth()))); |
108 |
} |
|
109 |
||
110 |
void HWGame::SendNetConfig() |
|
111 |
{ |
|
112 |
commonConfig(); |
|
184 | 113 |
} |
114 |
||
115 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
116 |
{ |
|
306 | 117 |
switch(msg.at(1)) { |
184 | 118 |
case '?': { |
395 | 119 |
SendIPC("!"); |
184 | 120 |
break; |
121 |
} |
|
122 |
case 'C': { |
|
123 |
switch (gameType) { |
|
124 |
case gtLocal: { |
|
125 |
SendConfig(); |
|
126 |
break; |
|
127 |
} |
|
128 |
case gtQLocal: { |
|
129 |
SendQuickConfig(); |
|
130 |
break; |
|
131 |
} |
|
132 |
case gtDemo: break; |
|
133 |
case gtNet: { |
|
341 | 134 |
SendNetConfig(); |
184 | 135 |
break; |
136 |
} |
|
137 |
} |
|
138 |
break; |
|
139 |
} |
|
140 |
case 'E': { |
|
425 | 141 |
emit ErrorMessage(QString().append(msg.mid(2)).left(msg.size() - 6)); |
184 | 142 |
return; |
143 |
} |
|
208 | 144 |
case 'K': { |
145 |
ulong kb = msg.mid(2).toULong(); |
|
466 | 146 |
if (kb==1) { |
468 | 147 |
qWarning("%s", KBMessages[kb - 1].toLocal8Bit().constData()); |
466 | 148 |
return; |
149 |
} |
|
208 | 150 |
if (kb && kb <= KBmsgsCount) |
151 |
{ |
|
425 | 152 |
emit ErrorMessage(KBMessages[kb - 1]); |
208 | 153 |
} |
154 |
return; |
|
155 |
} |
|
184 | 156 |
case '+': { |
157 |
if (gameType == gtNet) |
|
158 |
{ |
|
159 |
emit SendNet(msg); |
|
160 |
} |
|
161 |
break; |
|
162 |
} |
|
306 | 163 |
case 'i': { |
164 |
emit GameStats(msg.at(2), QString::fromUtf8(msg.mid(3))); |
|
165 |
break; |
|
166 |
} |
|
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
167 |
case 'Q': { |
533 | 168 |
SetGameState(gsInterrupted); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
169 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
170 |
} |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
171 |
case 'q': { |
533 | 172 |
SetGameState(gsFinished); |
324
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
173 |
break; |
f4c109c82a0c
Don't show game stats in case of interrupted by command '/quit' game
unc0rr
parents:
318
diff
changeset
|
174 |
} |
184 | 175 |
default: { |
176 |
if (gameType == gtNet) |
|
177 |
{ |
|
178 |
emit SendNet(msg); |
|
179 |
} |
|
533 | 180 |
demo.append(msg); |
184 | 181 |
} |
182 |
} |
|
183 |
} |
|
184 |
||
185 |
void HWGame::FromNet(const QByteArray & msg) |
|
186 |
{ |
|
187 |
RawSendIPC(msg); |
|
188 |
} |
|
189 |
||
190 |
void HWGame::onClientRead() |
|
191 |
{ |
|
192 |
quint8 msglen; |
|
193 |
quint32 bufsize; |
|
406 | 194 |
while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) && |
184 | 195 |
((msglen = readbuffer.data()[0]) < bufsize)) |
196 |
{ |
|
197 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
198 |
readbuffer.remove(0, msglen + 1); |
|
199 |
ParseMessage(msg); |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
QStringList HWGame::setArguments() |
|
204 |
{ |
|
205 |
QStringList arguments; |
|
497 | 206 |
arguments << cfgdir->absolutePath(); |
184 | 207 |
arguments << resolutions[0][config->vid_Resolution()]; |
208 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
296 | 209 |
arguments << "16"; // bpp |
291 | 210 |
arguments << QString("%1").arg(ipc_port); |
184 | 211 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
212 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
213 |
arguments << tr("en.txt"); |
|
296 | 214 |
arguments << "128"; // sound volume |
297 | 215 |
arguments << QString::number(config->timerInterval()); |
267 | 216 |
arguments << datadir->absolutePath(); |
297 | 217 |
arguments << (config->isShowFPSEnabled() ? "1" : "0"); |
529 | 218 |
arguments << (config->isAltDamageEnabled() ? "1" : "0"); |
184 | 219 |
return arguments; |
220 |
} |
|
221 |
||
341 | 222 |
void HWGame::AddTeam(const QString & teamname) |
184 | 223 |
{ |
224 |
if (TeamCount == 5) return; |
|
225 |
teams[TeamCount] = teamname; |
|
226 |
TeamCount++; |
|
227 |
} |
|
228 |
||
229 |
void HWGame::PlayDemo(const QString & demofilename) |
|
230 |
{ |
|
231 |
gameType = gtDemo; |
|
232 |
QFile demofile(demofilename); |
|
233 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
234 |
{ |
|
425 | 235 |
emit ErrorMessage(tr("Cannot open demofile %1").arg(demofilename)); |
184 | 236 |
return ; |
237 |
} |
|
238 |
||
239 |
// read demo |
|
512 | 240 |
toSendBuf = demofile.readAll(); |
184 | 241 |
|
242 |
// run engine |
|
533 | 243 |
demo.clear(); |
184 | 244 |
Start(); |
533 | 245 |
SetGameState(gsStarted); |
184 | 246 |
} |
247 |
||
248 |
void HWGame::StartNet() |
|
249 |
{ |
|
250 |
gameType = gtNet; |
|
533 | 251 |
demo.clear(); |
184 | 252 |
Start(); |
533 | 253 |
SetGameState(gsStarted); |
184 | 254 |
} |
255 |
||
256 |
void HWGame::StartLocal() |
|
257 |
{ |
|
258 |
gameType = gtLocal; |
|
239 | 259 |
seed = gamecfg->getCurrentSeed(); |
533 | 260 |
demo.clear(); |
184 | 261 |
Start(); |
533 | 262 |
SetGameState(gsStarted); |
184 | 263 |
} |
264 |
||
265 |
void HWGame::StartQuick() |
|
266 |
{ |
|
267 |
gameType = gtQLocal; |
|
239 | 268 |
seed = gamecfg->getCurrentSeed(); |
533 | 269 |
demo.clear(); |
184 | 270 |
Start(); |
533 | 271 |
SetGameState(gsStarted); |
184 | 272 |
} |
533 | 273 |
|
274 |
void HWGame::SetGameState(GameState state) |
|
275 |
{ |
|
276 |
gameState = state; |
|
277 |
emit GameStateChanged(state); |
|
278 |
} |