author | unc0rr |
Thu, 05 Oct 2006 16:33:18 +0000 | |
changeset 183 | 57c2ef19f719 |
parent 180 | ea83b9e9057f |
child 184 | f97a7a3dc8f6 |
permissions | -rw-r--r-- |
25 | 1 |
/* |
2 |
* Hedgewars, a worms-like game |
|
128 | 3 |
* Copyright (c) 2005, 2006 Andrey Korotaev <unC0Rr@gmail.com> |
25 | 4 |
* |
183 | 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 |
|
25 | 8 |
* |
183 | 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. |
|
25 | 13 |
* |
183 | 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 |
|
25 | 17 |
*/ |
18 |
||
19 |
#include <QMessageBox> |
|
20 |
#include <QProcess> |
|
21 |
#include <QTimer> |
|
22 |
#include <QString> |
|
23 |
#include <QByteArray> |
|
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
24 |
#include <QFile> |
25 | 25 |
#include <QTextStream> |
127 | 26 |
#include <QUuid> |
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
27 |
|
25 | 28 |
#include "game.h" |
29 |
#include "hwconsts.h" |
|
85
44d9045b26ff
New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents:
84
diff
changeset
|
30 |
#include "gameuiconfig.h" |
87 | 31 |
#include "gamecfgwidget.h" |
25 | 32 |
|
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
33 |
HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg) : |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
34 |
TCPBase(true) |
25 | 35 |
{ |
84 | 36 |
this->config = config; |
85
44d9045b26ff
New GameCFGWidget. Now it's possible to set forts mode from ui
unc0rr
parents:
84
diff
changeset
|
37 |
this->gamecfg = gamecfg; |
25 | 38 |
TeamCount = 0; |
26 | 39 |
seed = ""; |
25 | 40 |
} |
41 |
||
178
efdc2a63be8e
united TCP transfers for start game and receive preview
displacer
parents:
177
diff
changeset
|
42 |
void HWGame::onClientDisconnect() |
25 | 43 |
{ |
157 | 44 |
SaveDemo(cfgdir->absolutePath() + "/Demos/LastRound.hwd_1"); |
25 | 45 |
} |
46 |
||
47 |
void HWGame::SendTeamConfig(int index) |
|
48 |
{ |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
49 |
LocalCFG(teams[index]); |
25 | 50 |
} |
51 |
||
52 |
void HWGame::SendConfig() |
|
53 |
{ |
|
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
54 |
SendIPC(QString("eseed %1").arg(seed).toAscii()); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
55 |
SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii()); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
56 |
SendIPC("TL"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
57 |
SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii()); |
141 | 58 |
|
59 |
for (int i = 0; i < TeamCount; i++) |
|
60 |
{ |
|
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
61 |
SendIPC("eaddteam"); |
141 | 62 |
LocalCFG(teams[i]); |
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
63 |
SendIPC(QString("ecolor %1").arg(65535 << i * 8).toAscii()); |
141 | 64 |
for (int t = 0; t < hdNum[teams[i]]; t++) |
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
65 |
SendIPC(QString("eadd hh%1 0").arg(t).toAscii()); |
141 | 66 |
} |
67 |
} |
|
68 |
||
69 |
void HWGame::SendQuickConfig() |
|
70 |
{ |
|
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
71 |
SendIPC(QString("eseed %1").arg(seed).toAscii()); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
72 |
SendIPC(QString("etheme %1").arg(config->GetRandomTheme()).toAscii()); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
73 |
SendIPC("TL"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
74 |
SendIPC(QString("e$gmflags %1").arg(gamecfg->getGameFlags()).toAscii()); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
75 |
SendIPC("eaddteam"); |
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
76 |
LocalCFG(0); |
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
77 |
SendIPC("ecolor 65535"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
78 |
SendIPC("eadd hh0 0"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
79 |
SendIPC("eadd hh1 0"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
80 |
SendIPC("eadd hh2 0"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
81 |
SendIPC("eadd hh3 0"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
82 |
SendIPC("eaddteam"); |
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
87
diff
changeset
|
83 |
LocalCFG(2); |
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
84 |
SendIPC("ecolor 16776960"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
85 |
SendIPC("eadd hh0 1"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
86 |
SendIPC("eadd hh1 1"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
87 |
SendIPC("eadd hh2 1"); |
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
88 |
SendIPC("eadd hh3 1"); |
25 | 89 |
} |
90 |
||
43 | 91 |
void HWGame::ParseMessage(const QByteArray & msg) |
25 | 92 |
{ |
141 | 93 |
switch(msg.data()[1]) { |
94 |
case '?': { |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
95 |
if (gameType == gtNet) |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
96 |
emit SendNet(QByteArray("\x01""?")); |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
97 |
else |
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
98 |
SendIPC("!"); |
25 | 99 |
break; |
100 |
} |
|
141 | 101 |
case 'C': { |
102 |
switch (gameType) { |
|
142 | 103 |
case gtLocal: { |
104 |
SendConfig(); |
|
105 |
break; |
|
106 |
} |
|
141 | 107 |
case gtQLocal: { |
142 | 108 |
SendQuickConfig(); |
141 | 109 |
break; |
110 |
} |
|
111 |
case gtDemo: break; |
|
112 |
case gtNet: { |
|
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
113 |
SendIPC("TN"); |
141 | 114 |
emit SendNet(QByteArray("\x01""C")); |
115 |
break; |
|
116 |
} |
|
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
31
diff
changeset
|
117 |
} |
25 | 118 |
break; |
119 |
} |
|
141 | 120 |
case 'E': { |
43 | 121 |
QMessageBox::critical(0, |
122 |
"Hedgewars: error message", |
|
123 |
QString().append(msg.mid(2)).left(msg.size() - 6), |
|
124 |
QMessageBox::Ok, |
|
125 |
QMessageBox::NoButton, |
|
126 |
QMessageBox::NoButton); |
|
127 |
return; |
|
128 |
} |
|
141 | 129 |
case '+': { |
36 | 130 |
if (gameType == gtNet) |
131 |
{ |
|
43 | 132 |
emit SendNet(msg); |
36 | 133 |
} |
25 | 134 |
break; |
135 |
} |
|
141 | 136 |
default: { |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
137 |
if (gameType == gtNet) |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
138 |
{ |
43 | 139 |
emit SendNet(msg); |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
140 |
} |
43 | 141 |
demo->append(msg); |
25 | 142 |
} |
143 |
} |
|
144 |
} |
|
145 |
||
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
146 |
void HWGame::FromNet(const QByteArray & msg) |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
147 |
{ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
148 |
RawSendIPC(msg); |
26 | 149 |
} |
150 |
||
177 | 151 |
void HWGame::onClientRead() |
152 |
{ |
|
43 | 153 |
quint8 msglen; |
154 |
quint32 bufsize; |
|
155 |
while (((bufsize = readbuffer.size()) > 0) && |
|
156 |
((msglen = readbuffer.data()[0]) < bufsize)) |
|
25 | 157 |
{ |
43 | 158 |
QByteArray msg = readbuffer.left(msglen + 1); |
159 |
readbuffer.remove(0, msglen + 1); |
|
160 |
ParseMessage(msg); |
|
25 | 161 |
} |
162 |
} |
|
163 |
||
177 | 164 |
QStringList HWGame::setArguments() |
165 |
{ |
|
166 |
QStringList arguments; |
|
84 | 167 |
arguments << resolutions[0][config->vid_Resolution()]; |
168 |
arguments << resolutions[1][config->vid_Resolution()]; |
|
55
e09f7c952a40
Send run parameters by cmd line, game parameters by IPC... breaks network game
unc0rr
parents:
52
diff
changeset
|
169 |
arguments << "16"; |
25 | 170 |
arguments << "46631"; |
84 | 171 |
arguments << (config->vid_Fullscreen() ? "1" : "0"); |
172 |
arguments << (config->isSoundEnabled() ? "1" : "0"); |
|
80 | 173 |
arguments << tr("en.txt"); |
174 | 174 |
arguments << "128"; |
177 | 175 |
return arguments; |
25 | 176 |
} |
177 |
||
140
50ccde437ea1
teams and hedgedogs num selection added to HWgame
displacer
parents:
128
diff
changeset
|
178 |
void HWGame::AddTeam(const QString & teamname, unsigned char numHedgedogs) |
25 | 179 |
{ |
180 |
if (TeamCount == 5) return; |
|
181 |
teams[TeamCount] = teamname; |
|
182 |
TeamCount++; |
|
140
50ccde437ea1
teams and hedgedogs num selection added to HWgame
displacer
parents:
128
diff
changeset
|
183 |
hdNum[teamname]=numHedgedogs; |
25 | 184 |
} |
185 |
||
186 |
void HWGame::SaveDemo(const QString & filename) |
|
187 |
{ |
|
157 | 188 |
demo->replace(QByteArray("\x02TL"), QByteArray("\x02TD")); |
189 |
demo->replace(QByteArray("\x02TN"), QByteArray("\x02TD")); |
|
190 |
||
25 | 191 |
QFile demofile(filename); |
192 |
if (!demofile.open(QIODevice::WriteOnly)) |
|
193 |
{ |
|
28 | 194 |
QMessageBox::critical(0, |
25 | 195 |
tr("Error"), |
80 | 196 |
tr("Cannot save demo to file %1").arg(filename), |
25 | 197 |
tr("Quit")); |
198 |
return ; |
|
199 |
} |
|
200 |
QDataStream stream(&demofile); |
|
201 |
stream.writeRawData(demo->constData(), demo->size()); |
|
202 |
demofile.close(); |
|
26 | 203 |
delete demo; |
25 | 204 |
} |
26 | 205 |
|
206 |
void HWGame::PlayDemo(const QString & demofilename) |
|
207 |
{ |
|
208 |
gameType = gtDemo; |
|
209 |
QFile demofile(demofilename); |
|
210 |
if (!demofile.open(QIODevice::ReadOnly)) |
|
211 |
{ |
|
28 | 212 |
QMessageBox::critical(0, |
26 | 213 |
tr("Error"), |
80 | 214 |
tr("Cannot open demofile %1").arg(demofilename), |
26 | 215 |
tr("Quit")); |
216 |
return ; |
|
217 |
} |
|
218 |
||
219 |
// read demo |
|
220 |
QDataStream stream(&demofile); |
|
221 |
char buf[512]; |
|
142 | 222 |
int readbytes; |
26 | 223 |
do |
224 |
{ |
|
225 |
readbytes = stream.readRawData((char *)&buf, 512); |
|
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
31
diff
changeset
|
226 |
toSendBuf.append(QByteArray((char *)&buf, readbytes)); |
180
ea83b9e9057f
tcp sockets are incapsulated in TCPBase class now
displacer
parents:
178
diff
changeset
|
227 |
//SendIPC(QByteArray((char *)&buf, readbytes)); |
26 | 228 |
|
229 |
} while (readbytes > 0); |
|
230 |
demofile.close(); |
|
231 |
||
232 |
// run engine |
|
178
efdc2a63be8e
united TCP transfers for start game and receive preview
displacer
parents:
177
diff
changeset
|
233 |
demo = new QByteArray; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
31
diff
changeset
|
234 |
Start(); |
26 | 235 |
} |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
236 |
|
97
e7c1df9cce2c
- make the game be run from ${PREFIX}/bin with data in ${PREFIX}/share/hedgewars/Data
unc0rr
parents:
87
diff
changeset
|
237 |
void HWGame::StartNet() |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
238 |
{ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
239 |
gameType = gtNet; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
240 |
demo = new QByteArray; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
31
diff
changeset
|
241 |
Start(); |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
242 |
} |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
243 |
|
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
244 |
void HWGame::StartLocal() |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
245 |
{ |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
246 |
gameType = gtLocal; |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
247 |
if (TeamCount < 2) return; |
172 | 248 |
seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString(); |
178
efdc2a63be8e
united TCP transfers for start game and receive preview
displacer
parents:
177
diff
changeset
|
249 |
demo = new QByteArray; |
32
78bff13b11c0
With this patch the game doesn't crash when gaming by net
unc0rr
parents:
31
diff
changeset
|
250 |
Start(); |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
251 |
} |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
252 |
|
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
253 |
void HWGame::StartQuick() |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
254 |
{ |
141 | 255 |
gameType = gtQLocal; |
172 | 256 |
seed = gamecfg->getCurrentSeed();//QUuid::createUuid().toString(); |
178
efdc2a63be8e
united TCP transfers for start game and receive preview
displacer
parents:
177
diff
changeset
|
257 |
demo = new QByteArray; |
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
258 |
Start(); |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
259 |
} |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
260 |
|
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
261 |
|
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
262 |
void HWGame::LocalCFG(const QString & teamname) |
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
263 |
{ |
116 | 264 |
HWTeam team(teamname); |
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
265 |
if (!team.LoadFromFile()) { |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
266 |
QMessageBox::critical(0, |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
267 |
"Error", |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
268 |
QString("Cannot load team config ""%1""").arg(teamname), |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
269 |
QMessageBox::Ok, |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
270 |
QMessageBox::NoButton, |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
271 |
QMessageBox::NoButton); |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
272 |
return; |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
273 |
} |
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
274 |
RawSendIPC(team.IPCTeamInfo()); |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
275 |
} |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
276 |
|
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
277 |
void HWGame::LocalCFG(quint8 num) |
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
278 |
{ |
116 | 279 |
HWTeam team(num); |
86
664b536a1c27
Predefined teams, 'simple game' doesn't need to create team
unc0rr
parents:
85
diff
changeset
|
280 |
RawSendIPC(team.IPCTeamInfo()); |
31
99888245a4e8
Now net game is near available, but exception occurs...
unc0rr
parents:
28
diff
changeset
|
281 |
} |