summaryrefslogtreecommitdiffstats
path: root/pacman-c++/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pacman-c++/util.cpp')
-rw-r--r--pacman-c++/util.cpp357
1 files changed, 0 insertions, 357 deletions
diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp
deleted file mode 100644
index 09b2be1..0000000
--- a/pacman-c++/util.cpp
+++ /dev/null
@@ -1,357 +0,0 @@
1#include "util.h"
2#include <QtNetwork/QTcpSocket>
3
4namespace Util
5{
6 Transmission::map_t createUninitialisedMap()
7 {
8 Transmission::map_t map;
9 map = new Transmission::field_t*[Constants::map_size.width];
10 for (unsigned int i = 0; i < Constants::map_size.width; ++i)
11 map[i] = new Transmission::field_t[Constants::map_size.height];
12 return map;
13 }
14
15 Transmission::map_t createEmptyMap()
16 {
17 Transmission::map_t map = createUninitialisedMap();
18 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
19 {
20 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
21 {
22 Transmission::field_t &cur = map[x][y];
23 cur = Transmission::none;
24 }
25 }
26 return map;
27 }
28
29 void deleteMap(Transmission::map_t map)
30 {
31 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
32 delete[] map[x];
33 delete[] map;
34 }
35
36 Transmission::map_t createDemoMap()
37 {
38 Transmission::map_t map = createEmptyMap();
39
40 const char *tmpl[] = {
41 " # # ",
42 " #### ###### # #### # # ###### ### ",
43 " # # ",
44 " # ##### # # # # # ### # # # ",
45 " # # # # # # # # # # ## # # ",
46 " # # # # # # # # ### # # # # ",
47 " # # # # # # # # # # # # ## # ",
48 " # # ### ##### # ### # # # ",
49 " ### # ",
50 " # # ### #### #### #### ##### ",
51 " #### # #..# #..# #..# # # ",
52 " # # ### #..# #..# #### # # # # ",
53 " # # # #..# #..# # # ",
54 " # #### # #### #### # # ##### # ",
55 " # # ",
56 " #### ###### # ##### # ####### ### ",
57 " # # "
58 };
59
60 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
61 {
62 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
63 {
64 Transmission::field_t &cur = map[x][y];
65 cur = Transmission::none;
66 if (tmpl[y][x] == '#')
67 cur |= Color::none | Transmission::block;
68 /* this is a simple hack to create areas where no
69 * autoplaced points/actors will be placed (see makePoints)
70 */
71 else if (tmpl[y][x] == '.')
72 cur |= Transmission::point;
73 }
74 }
75 return map;
76 }
77
78 void placeActors(Transmission::map_t map, unsigned int players, const Color::Color *colors)
79 {
80#if 0
81 for(unsigned int i = 0; i < players; ++i)
82 map[i][0] = colors[i] | Transmission::pacman;
83 return;
84#endif
85
86 int mindistance = Constants::Game::player_minimum_distance;
87 /* this outer loop is used if there are no more valid places left
88 * so we can change mindistance
89 */
90 QList<QPoint> actors;
91 for(unsigned int i = 0; i < players; ++i)
92 {
93 /* first remove formerly placed actors from map */
94 foreach(QPoint pos, actors)
95 map[pos.x()][pos.y()] = Transmission::none;
96 actors.clear();
97
98 /* get list of valid positions */
99 QList<QPoint> validpos;
100 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
101 {
102 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
103 {
104 Transmission::field_t &cur = map[x][y];
105 if (cur == Transmission::none)
106 validpos.append(QPoint(x, y));
107 }
108 }
109
110 /* place actors at map */
111 for(i = 0; i < players; ++i)
112 {
113 int rand = (int) (validpos.size() * (qrand() / (RAND_MAX + 1.0)));
114 QPoint newpos = validpos.at(rand);
115 map[newpos.x()][newpos.y()] = colors[i] | Transmission::pacman;
116 qDebug() << "[Place] Actor" << i << "at" << newpos;
117 actors.append(newpos);
118 validpos.removeAt(rand);
119
120 QMutableListIterator<QPoint> j(validpos);
121 while(j.hasNext())
122 {
123 j.next();
124 QPoint tmp = j.value() - newpos;
125 if (tmp.manhattanLength() < mindistance)
126 j.remove();
127 }
128
129 if (validpos.empty())
130 {
131 qWarning() << "There are no more valid positions for actors left on the map";
132 mindistance -= Constants::Game::player_distance_decr;
133 break;
134 }
135 }
136 }
137 }
138
139 void fillPoints(Transmission::map_t map, Transmission::field_t type)
140 {
141 /* auto place normal points*/
142 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
143 {
144 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
145 {
146 Transmission::field_t &cur = map[x][y];
147 if (cur == Transmission::none)
148 {
149#if 0
150 /* use for endround testing */
151 if (x > 0)
152 continue;
153#endif
154 cur = type;
155 }
156 else if (cur == Transmission::point)
157 cur = Transmission::none;
158 }
159 }
160 }
161
162 Transmission::field_t actorMovementToTransmission(Actor::Movement mov, Transmission::field_t def)
163 {
164 switch (mov)
165 {
166 case Actor::None:
167 return Transmission::direction_none;
168 break;
169 case Actor::Left:
170 return Transmission::direction_left;
171 break;
172 case Actor::Right:
173 return Transmission::direction_right;
174 break;
175 case Actor::Up:
176 return Transmission::direction_up;
177 break;
178 case Actor::Down:
179 return Transmission::direction_down;
180 break;
181 default:
182 return def;
183 break;
184 }
185 return def;
186 }
187
188 Actor::Movement transmissionMovementToActor(Transmission::field_t field, Actor::Movement def)
189 {
190 switch (field)
191 {
192 case Transmission::direction_none:
193 return Actor::None;
194 break;
195 case Transmission::direction_left:
196 return Actor::Left;
197 break;
198 case Transmission::direction_right:
199 return Actor::Right;
200 break;
201 case Transmission::direction_up:
202 return Actor::Up;
203 break;
204 case Transmission::direction_down:
205 return Actor::Down;
206 break;
207 default:
208 return def;
209 break;
210 }
211 return def;
212 }
213
214 const QString colorToString(Color::Color color)
215 {
216 switch(color)
217 {
218 case Color::none:
219 return "none";
220 break;
221 case Color::red:
222 return "red";
223 break;
224 case Color::blue:
225 return "blue";
226 break;
227 case Color::green:
228 return "green";
229 break;
230 case Color::yellow:
231 return "yellow";
232 break;
233 default:
234 return "unknown";
235 break;
236 }
237 }
238
239 QSharedPointer<QByteArray> createPacket(const ::google::protobuf::MessageLite& packet)
240 {
241 qint64 packetlen = packet.ByteSize();
242 /* datalen = packet with length prepended */
243 qint64 datalen = sizeof(qint64) + packetlen;
244
245 QSharedPointer<QByteArray> data = QSharedPointer<QByteArray>(new QByteArray);
246 data->resize(datalen);
247
248 /* use QDataStream for length to avoid endianess shit */
249 QDataStream out(data.data(), QIODevice::WriteOnly);
250 out << packetlen;
251
252 /* use protobuf.SerializeWithCachedSizesToArray() to avoid calling protobuf.ByteSize() again */
253 ::google::protobuf::uint8 *dataptr = reinterpret_cast<google::protobuf::uint8 *>(data->data());
254 packet.SerializeWithCachedSizesToArray(dataptr + sizeof(qint64));
255
256 return data;
257 }
258
259 bool sendPacket(QByteArray *data, QTcpSocket *socket)
260 {
261 int bytesWritten = socket->write(*data);
262 if (bytesWritten != data->size())
263 {
264 qDebug() << "[sendPacket] Not all data has been sent:"
265 << "written=" << bytesWritten << ", length=" << data->size();
266 return false;
267 }
268 socket->flush();
269 return true;
270 }
271
272 bool sendPacket(const ::google::protobuf::MessageLite& packet, QTcpSocket *socket)
273 {
274 return sendPacket(createPacket(packet).data(), socket);
275 }
276
277 QSharedPointer<QByteArray> receivePacket(QTcpSocket *socket)
278 {
279 QDataStream in(socket);
280 qint64 datalen;
281 in >> datalen;
282
283 QSharedPointer<QByteArray> data = QSharedPointer<QByteArray>(new QByteArray);
284 data->resize(datalen);
285 socket->read(data->data(), data->size());
286 return data;
287 }
288
289 int floorLog2(unsigned int n)
290 {
291 if (n == 0)
292 return -1;
293
294 int pos = 0;
295 if (n >= 1<<16) { n >>= 16; pos += 16; }
296 if (n >= 1<< 8) { n >>= 8; pos += 8; }
297 if (n >= 1<< 4) { n >>= 4; pos += 4; }
298 if (n >= 1<< 2) { n >>= 2; pos += 2; }
299 if (n >= 1<< 1) { pos += 1; }
300 return pos;
301 }
302
303#if 0
304 void hexdump(void *pAddressIn, long lSize)
305 {
306 char szBuf[100];
307 long lIndent = 1;
308 long lOutLen, lIndex, lIndex2, lOutLen2;
309 long lRelPos;
310 struct { char *pData; unsigned long lSize; } buf;
311 unsigned char *pTmp,ucTmp;
312 unsigned char *pAddress = (unsigned char *)pAddressIn;
313
314 buf.pData = (char *)pAddress;
315 buf.lSize = lSize;
316
317 while (buf.lSize > 0)
318 {
319 pTmp = (unsigned char *)buf.pData;
320 lOutLen = (int)buf.lSize;
321 if (lOutLen > 16)
322 lOutLen = 16;
323
324 // create a 64-character formatted output line:
325 sprintf(szBuf, " > "
326 " "
327 " %08lX", pTmp-pAddress);
328 lOutLen2 = lOutLen;
329
330 for(lIndex = 1+lIndent, lIndex2 = 53-15+lIndent, lRelPos = 0;
331 lOutLen2;
332 lOutLen2--, lIndex += 2, lIndex2++
333 )
334 {
335 ucTmp = *pTmp++;
336
337 sprintf(szBuf + lIndex, "%02X ", (unsigned short)ucTmp);
338 if(!isprint(ucTmp)) ucTmp = '.'; // nonprintable char
339 szBuf[lIndex2] = ucTmp;
340
341 if (!(++lRelPos & 3)) // extra blank after 4 bytes
342 { lIndex++; szBuf[lIndex+2] = ' '; }
343 }
344
345 if (!(lRelPos & 3)) lIndex--;
346
347 szBuf[lIndex ] = '<';
348 szBuf[lIndex+1] = ' ';
349
350 printf("%s\n", szBuf);
351
352 buf.pData += lOutLen;
353 buf.lSize -= lOutLen;
354 }
355 }
356#endif
357}