1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#include "util.h"
#include <QtNetwork/QTcpSocket>
namespace Util
{
Transmission::map_t createUninitialisedMap()
{
Transmission::map_t map;
map = new Transmission::field_t*[Constants::map_size.width];
for (unsigned int i = 0; i < Constants::map_size.width; ++i)
map[i] = new Transmission::field_t[Constants::map_size.height];
return map;
}
Transmission::map_t createEmptyMap()
{
Transmission::map_t map = createUninitialisedMap();
for (unsigned int x = 0; x < Constants::map_size.width; ++x)
{
for (unsigned int y = 0; y < Constants::map_size.height; ++y)
{
Transmission::field_t &cur = map[x][y];
cur = Transmission::none;
}
}
return map;
}
// temporary
Transmission::map_t createDemoMap()
{
Transmission::map_t map = createEmptyMap();
const char *tmpl[] = {
" # # ",
" #### ###### # #### # # ###### ### ",
" # # ",
" # ##### # # # # # ### # # # ",
" # # # # # # # # # # ## # # ",
" # # # # # # # # ### # # # # ",
" # # # # # # # # # # # # ## # ",
" # # ### ##### # ### # # # ",
" ### # ",
" # # ### #### #### #### ##### ",
" #### # #..# #..# #..# # # ",
" # # ### #..# #..# #### # # # # ",
" # # # #..# #..# # # ",
" # #### # #### #### # # ##### # ",
" # # ",
" #### ###### # ##### # ####### ### ",
" # # "
};
for (unsigned int x = 0; x < Constants::map_size.width; ++x)
{
for (unsigned int y = 0; y < Constants::map_size.height; ++y)
{
Transmission::field_t &cur = map[x][y];
cur = Transmission::none;
if (tmpl[y][x] == '#')
cur |= Color::none | Transmission::block;
/* this is a simple hack to create areas where no
* autoplaced points will be placed (see below)
*/
else if (tmpl[y][x] == '.')
cur |= Transmission::point;
}
}
map[0][0] |= Transmission::bonuspoint;
map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right;
map[23][0] = Color::blue | Transmission::pacman | Transmission::direction_none;
map[24][0] = Color::green | Transmission::pacman | Transmission::direction_none;
/* auto place normal points*/
for (unsigned int x = 0; x < Constants::map_size.width; ++x)
{
for (unsigned int y = 0; y < Constants::map_size.height; ++y)
{
Transmission::field_t &cur = map[x][y];
if (cur == Transmission::none)
cur |= Transmission::point;
else if (cur == Transmission::point)
cur = Transmission::none;
}
}
return map;
}
Transmission::field_t actorMovementToTransmission(Actor::Movement mov, Transmission::field_t def)
{
switch (mov)
{
case Actor::None:
return Transmission::direction_none;
break;
case Actor::Left:
return Transmission::direction_left;
break;
case Actor::Right:
return Transmission::direction_right;
break;
case Actor::Up:
return Transmission::direction_up;
break;
case Actor::Down:
return Transmission::direction_down;
break;
default:
if (def == static_cast<Transmission::field_t>(-1))
Q_ASSERT(false);
else
return def;
}
return 0; // for pleasing the compiler
}
Actor::Movement transmissionMovementToActor(Transmission::field_t field, Actor::Movement def)
{
switch (field)
{
case Transmission::direction_none:
return Actor::None;
break;
case Transmission::direction_left:
return Actor::Left;
break;
case Transmission::direction_right:
return Actor::Right;
break;
case Transmission::direction_up:
return Actor::Up;
break;
case Transmission::direction_down:
return Actor::Down;
break;
default:
if (def == Actor::Movement(-1))
Q_ASSERT(false);
else
return def;
}
return Actor::None; // for pleasing the compiler
}
void QByteArrayToStdString(const QByteArray& arr, std::string& str)
{
// TODO: normal conversion to std::string won't work,
// probably due to \0-bytes.
//std::string dataStr = std::string(data.constData());
//std::string dataStr = QString(data).toStdString();
for (int i=0; i<arr.size(); ++i)
str += arr[i];
}
void sendPacket(const ::google::protobuf::Message& packet, QTcpSocket *socket)
{
std::string dataStr = packet.SerializeAsString();
const char *data = dataStr.c_str();
sendPacket(data, dataStr.length(), socket);
}
void sendPacket(const char *data, unsigned int length, QTcpSocket *socket)
{
unsigned int bytesWritten = socket->write(data, length);
if (bytesWritten != length)
{
qDebug() << "written: " << bytesWritten;
qDebug() << "strl: " << length;
}
Q_ASSERT(bytesWritten == length);
socket->flush();
}
}
|