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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
#include "server.h"
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include "util.h"
#include "pacman.pb.h"
#include "block.h"
Server::Server(QWidget *parent)
: SceneHolder(parent)
{
qDebug() << "waiting for clients";
waitForClientConnections();
qDebug() << "clients connected";
updateMap(Util::createDemoMap());
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
timer->start(Constants::tick);
}
void Server::tick()
{
qDebug() << "doing srv update";
Transmission::map_t map = calculateUpdates();
updateMap(map);
QSharedPointer<ProtoBuf::MapUpdate> packet = createUpdatePacket(map);
sendUpdate(packet);
}
Transmission::map_t Server::calculateUpdates()
{
Transmission::map_t map = Util::createEmptyMap();
//TODO: ai
//m_actorMovements[Color::blue] = Actor::Movement((qrand() % 4) + 1);
//m_actorMovements[Color::green] = Actor::Movement((qrand() % 4) + 1);
QMutableMapIterator<Color::Color, Actor::Movement> i(m_actorMovements);
while (i.hasNext())
{
i.next();
Actor *actor = m_actors.value(i.key());
QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint());
qDebug() << "actor " << i.key() << " is at " << mapPosition << "moving " << i.value();
QPoint newMapPosition = mapPosition;
switch (i.value())
{
case Actor::Up:
newMapPosition += QPoint(0, -1);
break;
case Actor::Down:
newMapPosition += QPoint(0, 1);
break;
case Actor::Left:
newMapPosition += QPoint(-1, 0);
break;
case Actor::Right:
newMapPosition += QPoint(1, 0);
break;
case Actor::None:
break;
default:
Q_ASSERT(false);
}
if (newMapPosition.x() < 0)
newMapPosition.setX(0);
if (newMapPosition.x() >= visualMap.size())
newMapPosition.setX(visualMap.size() - 1);
if (newMapPosition.y() < 0)
newMapPosition.setY(0);
if (newMapPosition.y() >= visualMap[newMapPosition.x()].size())
newMapPosition.setY(visualMap[newMapPosition.x()].size() - 1);
// <t3h g4m3 10gic>
// TODO: support actors eating each other
// old item
PixmapItem *oldItem = visualMap[mapPosition.x()][mapPosition.y()];
if (oldItem != NULL)
{
if (oldItem->eaten())
map[mapPosition.x()][mapPosition.y()] = Transmission::empty;
}
// new item
PixmapItem *item = visualMap[newMapPosition.x()][newMapPosition.y()];
if (item != NULL && oldItem != item)
{
if (!item->checkEnter(actor))
{
/* movement invalid. e.g. move against wall */
newMapPosition = mapPosition;
}
else
{
// apply actions of entering this field
bool survive = item->enter(actor);
if (!survive)
{
//map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty;
}
}
}
// </t3h g4m2 10gic>
if (mapPosition == newMapPosition)
m_actorMovements[i.key()] = Actor::None;
map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman |
i.key() | Util::actorMovementToTransmission(i.value());
if (i.value() == Actor::None)
{
/* set actor to non-moving */
m_actorMovements[i.key()] = Actor::None;
i.remove();
}
}
return map;
}
QSharedPointer<ProtoBuf::MapUpdate> Server::createUpdatePacket(Transmission::map_t map)
{
QSharedPointer<ProtoBuf::MapUpdate> updatePacket =
QSharedPointer<ProtoBuf::MapUpdate>(new ProtoBuf::MapUpdate);
for (unsigned int x = 0; x < Constants::map_size.width; ++x)
{
for (unsigned int y = 0; y < Constants::map_size.height; ++y)
updatePacket->add_field(map[x][y]);
}
for(unsigned i = 0; Color::order[i] != Color::none; ++i)
{
updatePacket->add_round_points(m_actors.value(Color::order[i])->getRoundPoints());
updatePacket->add_game_points(m_actors.value(Color::order[i])->getGamePoints());
}
//qDebug() << "field sz "<< updatePacket->field_size();
return updatePacket;
}
void Server::waitForClientConnections()
{
QTcpServer *tcpSrv = new QTcpServer(this);
// server must stay alive as long as sockets (qt parent mem mechanism)
tcpSrv->listen(QHostAddress::Any, Constants::Networking::port);
#define SINGLE
#ifdef SINGLE
for (unsigned int i = 0; i < 1; ++i)
{
#else
for (unsigned int i = 0; Color::order[i] != Color::none; ++i)
{
#endif
bool connectionAvailable = tcpSrv->waitForNewConnection(-1);
Q_ASSERT(connectionAvailable);
QTcpSocket *socket = tcpSrv->nextPendingConnection();
connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate()));
// assign color
Color::Color color = Color::order[i];
m_clientConnections[color] = socket;
// notify player of color
ProtoBuf::WhoAmI packet;
packet.set_color(color);
Util::sendPacket(packet, socket);
qDebug() << "new player of col " << color;
}
}
void Server::sendUpdate(QSharedPointer< ProtoBuf::MapUpdate > packet)
{
std::string dataStr = packet->SerializeAsString();
const char *data = dataStr.c_str();
foreach(QTcpSocket *socket, m_clientConnections)
Util::sendPacket(data, dataStr.length(), socket);
}
void Server::keyPressUpdate()
{
qDebug() << "kpress";
QMapIterator<Color::Color, QTcpSocket*> i(m_clientConnections);
while (i.hasNext())
{
i.next();
Color::Color color = i.key();
QTcpSocket *socket = i.value();
qDebug() << "data?";
if (socket->bytesAvailable() > 0)
{
qDebug() << "data!";
std::string dataStr;
Util::QByteArrayToStdString(socket->readAll(), dataStr);
ProtoBuf::KeyPressUpdate packet;
bool worked = packet.ParseFromString(dataStr);
Q_ASSERT(worked);
Transmission::field_t direction = packet.newkey();
qDebug() << "data:" << direction;
m_actorMovements[color] = Util::transmissionMovementToActor(direction);
}
}
}
bool Constants::server = true;
int main(int argc, char ** argv)
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
QApplication app(argc, argv);
app.setApplicationName("Pacman Server");
app.setWindowIcon(QIcon(":/appicon"));
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
Server Server;
return app.exec();
}
|