author | unc0rr |
Thu, 30 Nov 2006 22:36:07 +0000 | |
changeset 287 | b0eef98928f8 |
parent 271 | f2f9a3d5b441 |
child 291 | afa378f1e961 |
permissions | -rw-r--r-- |
184 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
|
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 |
#include <QUuid> |
|
27 |
||
28 |
#include "game.h" |
|
29 |
#include "hwconsts.h" |
|
30 |
#include "gameuiconfig.h" |
|
31 |
#include "gamecfgwidget.h" |
|
210 | 32 |
#include "KB.h" |
239 | 33 |
#include "proto.h" |
184 | 34 |
|
35 |
HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg) : |
|
36 |
TCPBase(true) |
|
37 |
{ |
|
38 |
this->config = config; |
|
39 |
this->gamecfg = gamecfg; |
|
40 |
TeamCount = 0; |
|
41 |
seed = ""; |
|
42 |
} |
|
43 |
||
44 |
void HWGame::onClientDisconnect() |
|
45 |
{ |
|
271 | 46 |
SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_" + cProtoVer); |
184 | 47 |
} |
48 |
||
49 |
void HWGame::SendTeamConfig(int index) |
|
50 |
{ |
|
51 |
LocalCFG(teams[index]); |
|
52 |
} |
|
53 |
||
253 | 54 |
void HWGame::commonConfig() |
184 | 55 |
{ |
56 |
SendIPC(QString("eseed %1").arg(seed).toAscii()); |
|
249 | 57 |
try { |
58 |
QString currentMap=gamecfg->getCurrentMap(); |
|
59 |
SendIPC((QString("emap ")+currentMap).toAscii()); |
|
60 |
SendIPC(QString("etheme %1").arg(gamecfg->getCurrentTheme()).toAscii()); |
|
61 |
} |
|
62 |
catch(const MapFileErrorException& e) { |
|
63 |
SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii()); |
|
64 |
} |
|
184 | 65 |
SendIPC("TL"); |
66 |
SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii()); |
|
253 | 67 |
} |
68 |
||
69 |
void HWGame::SendConfig() |
|
70 |
{ |
|
71 |
commonConfig(); |
|
184 | 72 |
|
73 |
for (int i = 0; i < TeamCount; i++) |
|
74 |
{ |
|
239 | 75 |
HWTeam team(teams[i]); |
76 |
team.LoadFromFile(); |
|
77 |
||
78 |
QColor clr = m_teamsParams[teams[i]].teamColor; |
|
79 |
QByteArray buf; |
|
80 |
QStringList sl = team.TeamGameConfig(clr.rgb()&0xFFFFFF, m_teamsParams[teams[i]].numHedgehogs); |
|
81 |
HWProto::addStringListToBuffer(buf, sl); |
|
82 |
RawSendIPC(buf); |
|
184 | 83 |
} |
84 |
} |
|
85 |
||
86 |
void HWGame::SendQuickConfig() |
|
87 |
{ |
|
253 | 88 |
commonConfig(); |
239 | 89 |
|
90 |
QByteArray teamscfg; |
|
91 |
HWTeam team1(0); |
|
92 |
team1.difficulty = 0; |
|
93 |
HWProto::addStringListToBuffer(teamscfg, team1.TeamGameConfig(65535, 4)); |
|
94 |
||
95 |
HWTeam team2(2); |
|
96 |
team2.difficulty = 4; |
|
97 |
RawSendIPC(HWProto::addStringListToBuffer(teamscfg, team2.TeamGameConfig(16776960, 4))); |
|
184 | 98 |
} |
99 |
||
100 |
void HWGame::ParseMessage(const QByteArray & msg) |
|
101 |
{ |
|
102 |
switch(msg.data()[1]) { |
|
103 |
case '?': { |
|
104 |
if (gameType == gtNet) |
|
105 |
emit SendNet(QByteArray("\x01""?")); |
|
106 |
else |
|
107 |
SendIPC("!"); |
|
108 |
break; |
|
109 |
} |
|
110 |
case 'C': { |
|
111 |
switch (gameType) { |
|
112 |
case gtLocal: { |
|
113 |
SendConfig(); |
|
114 |
break; |
|
115 |
} |
|
116 |
case gtQLocal: { |
|
117 |
SendQuickConfig(); |
|
118 |
break; |
|
119 |
} |
|
120 |
case gtDemo: break; |
|
121 |
case gtNet: { |
|
122 |
SendIPC("TN"); |
|
123 |
emit SendNet(QByteArray("\x01""C")); |
|
124 |
break; |
|
125 |
} |
|
126 |
} |
|
127 |
break; |
|
128 |
} |
|
129 |
case 'E': { |
|
130 |
QMessageBox::critical(0, |
|
131 |
"Hedgewars: error message", |
|
132 |
QString().append(msg.mid(2)).left(msg.size() - 6), |
|
133 |
QMessageBox::Ok, |
|
134 |
QMessageBox::NoButton, |
|
135 |
QMessageBox::NoButton); |
|
136 |
return; |
|
137 |
} |
|
208 | 138 |
case 'K': { |
139 |
ulong kb = msg.mid(2).toULong(); |
|
140 |
if (kb && kb <= KBmsgsCount) |
|
141 |
{ |
|
142 |
QMessageBox::information(0, |
|
143 |
"Hedgewars: information", |
|
144 |
KBMessages[kb - 1], |
|
145 |
QMessageBox::Ok, |
|
146 |
QMessageBox::NoButton, |
|
147 |
QMessageBox::NoButton); |
|
148 |
} |
|
149 |
return; |
|
150 |
} |
|
184 | 151 |
case '+': { |
152 |
if (gameType == gtNet) |
|
153 |
{ |
|
154 |
emit SendNet(msg); |
|
155 |
} |
|
156 |
break; |
|
157 |
} |
|
158 |
default: { |
|
159 |
if (gameType == gtNet) |
|
160 |
{ |
|
161 |
emit SendNet(msg); |
|
162 |
} |
|
163 |
demo->append(msg); |
|
164 |
} |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
void HWGame::FromNet(const QByteArray & msg) |
|
169 |
{ |
|
170 |
RawSendIPC(msg); |
|
171 |
} |
|
172 |
||
173 |
void HWGame::onClientRead() |
|
174 |
{ |
|
175 |
quint8 msglen; |
|
176 |
quint32 bufsize; |
|
177 |
while (((bufsize = readbuffer.size()) > 0) && |
|
178 |
((msglen = readbuffer.data()[0]) < bufsize)) |
|
179 |
{ |
|
180 |
QByteArray msg = readbuffer.left(msglen + 1); |
|
181 |
readbuffer.remove(0, msglen + 1); |
|
182 |
ParseMessage(msg); |
|
183 |
} |
|
184 |
} |
|
185 |
||
186 |
QStringList HWGame::setArguments() |
|
187 |
{ |
|
188 |
QStringList arguments; |
|
189 |
arguments << resolutions[0][config->vid_Resolution()]; |
|
190 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
191 |
arguments << "16"; |
|
192 |
arguments << "46631"; |
|
193 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
|
194 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
195 |
arguments << tr("en.txt"); |
|
196 |
arguments << "128"; |
|
267 | 197 |
arguments << datadir->absolutePath(); |
184 | 198 |
return arguments; |
199 |
} |
|
200 |
||
213 | 201 |
void HWGame::AddTeam(const QString & teamname, HWTeamTempParams teamParams) |
184 | 202 |
{ |
203 |
if (TeamCount == 5) return; |
|
204 |
teams[TeamCount] = teamname; |
|
205 |
TeamCount++; |
|
213 | 206 |
m_teamsParams[teamname]=teamParams; |
184 | 207 |
} |
208 |
||
209 |
void HWGame::SaveDemo(const QString & filename) |
|
210 |
{ |
|
211 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
|
212 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
213 |
||
214 |
QFile demofile(filename); |
|
215 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
216 |
{ |
|
217 |
QMessageBox::critical(0, |
|
218 |
tr("Error"), |
|
219 |
tr("Cannot save demo to file %1").arg(filename), |
|
220 |
tr("Quit")); |
|
221 |
return ; |
|
222 |
} |
|
223 |
QDataStream stream(&demofile); |
|
224 |
stream.writeRawData(demo->constData(), demo->size()); |
|
225 |
demofile.close(); |
|
226 |
delete demo; |
|
227 |
} |
|
228 |
||
229 |
void HWGame::PlayDemo(const QString & demofilename) |
|
230 |
{ |
|
231 |
gameType = gtDemo; |
|
232 |
QFile demofile(demofilename); |
|
233 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
234 |
{ |
|
235 |
QMessageBox::critical(0, |
|
236 |
tr("Error"), |
|
237 |
tr("Cannot open demofile %1").arg(demofilename), |
|
238 |
tr("Quit")); |
|
239 |
return ; |
|
240 |
} |
|
241 |
||
242 |
// read demo |
|
243 |
QDataStream stream(&demofile); |
|
244 |
char buf[512]; |
|
245 |
int readbytes; |
|
246 |
do |
|
247 |
{ |
|
248 |
readbytes = stream.readRawData((char *)&buf, 512); |
|
249 |
toSendBuf.append(QByteArray((char *)&buf, readbytes)); |
|
250 |
//SendIPC(QByteArray((char *)&buf, readbytes)); |
|
251 |
||
252 |
} while (readbytes > 0); |
|
253 |
demofile.close(); |
|
254 |
||
255 |
// run engine |
|
256 |
demo = new QByteArray; |
|
257 |
Start(); |
|
258 |
} |
|
259 |
||
260 |
void HWGame::StartNet() |
|
261 |
{ |
|
262 |
gameType = gtNet; |
|
263 |
demo = new QByteArray; |
|
264 |
Start(); |
|
265 |
} |
|
266 |
||
267 |
void HWGame::StartLocal() |
|
268 |
{ |
|
269 |
gameType = gtLocal; |
|
270 |
if (TeamCount < 2) return; |
|
239 | 271 |
seed = gamecfg->getCurrentSeed(); |
184 | 272 |
demo = new QByteArray; |
273 |
Start(); |
|
274 |
} |
|
275 |
||
276 |
void HWGame::StartQuick() |
|
277 |
{ |
|
278 |
gameType = gtQLocal; |
|
239 | 279 |
seed = gamecfg->getCurrentSeed(); |
184 | 280 |
demo = new QByteArray; |
281 |
Start(); |
|
282 |
} |
|
283 |
||
284 |
||
285 |
void HWGame::LocalCFG(const QString & teamname) |
|
286 |
{ |
|
239 | 287 |
QByteArray teamcfg; |
184 | 288 |
HWTeam team(teamname); |
247
07605d2a2024
- Fix infinite loop when selecting weapon with ammo menu
unc0rr
parents:
239
diff
changeset
|
289 |
team.LoadFromFile(); |
239 | 290 |
RawSendIPC(HWProto::addStringListToBuffer(teamcfg, team.TeamGameConfig(16776960, 4))); |
184 | 291 |
} |
292 |