summaryrefslogtreecommitdiffstats
path: root/pacman-c++/server.cpp
blob: a045cad98dbcf82a33e1e9d4c1533733c847dc07 (plain)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "server.h"
#include "util.h"
#include "pacman.pb.h"
#include "block.h"
#include "anyoption.h"
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QTextStream>

Server::Server(QWidget *parent)
  : SceneHolder(parent), m_bindaddress(QHostAddress::Any),
  m_port(Constants::Networking::port), m_numbots(0)
{
  /* determine max players by using order array */
  for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers);
}

bool Server::run()
{
  qDebug() << "[Server] Running server...";
  qDebug() << "[Server] Max players:" << m_maxplayers;
  qDebug() << "[Server] Number of bots:" << m_numbots;
  if (!waitForClientConnections())
    return false;

  qDebug() << "[Server] Creating map...";
  Transmission::map_t map =  Util::createDemoMap();
  Util::placeActors(map, m_maxplayers, Color::order);
  Util::makePoints(map);
  updateMap(map);
  sendUpdate(map);
  Util::deleteMap(map);
  map = NULL;

  QTimer *timer = new QTimer(this);
  connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
  timer->start(Constants::tick);
  return true;
}

void Server::tick()
{
  qDebug() << "[Tick] Doing server update";
  Transmission::map_t map = calculateUpdates();
  updateMap(map);
  sendUpdate(map);
  Util::deleteMap(map);
}

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();
    int turn = 0;

invalid_direction:
    ++turn;
    Actor *actor = m_actors.value(i.key());
    QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint());
    qDebug() << "[Calc] Actor wants to move: color=" << i.key()
        << "pos=" << mapPosition << "direction=" << 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
    // TODO: old item - REMOVE THAT?
    GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()];

    /* check if there's an item at new location of actor */
    GameEntity *item = visualMap[newMapPosition.x()][newMapPosition.y()];
    if (item != NULL && oldItem != item)
    {
      qDebug() << "[Calc] Found item at new actor location";
      if (!item->checkEnter(actor))
      {
        /* movement invalid. e.g. move against wall */
        newMapPosition = mapPosition;
        qDebug() << "[Calc] Item blocks actor";
      }
      else
      {
        /* apply actions of entering this field */
        bool survive = item->enter(actor);
        if (!survive)
        {
          map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty | actor->color();
        }
      }
    }
    // </t3h g4m2 10gic>

    /* movement didn't work - e.g. was blocked */
    if (mapPosition == newMapPosition)
    {
      /* */
      if (turn == 1 && i.value() != actor->direction())
      {
        /* set direction back to last known direction and try again */
        qDebug() << "[Calc] Movement was blocked. Trying last known actor direction";
        m_actorMovements[i.key()] = actor->direction();
        goto invalid_direction;
      }
      else
      {
        /* second turn didn't work too -> stop movement */
        m_actorMovements[i.key()] = Actor::None;
        qDebug() << "[Calc] No good direction known. Movement stopped";
      }
    }

    map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman |
        i.key() | Util::actorMovementToTransmission(i.value());

    /* DEBUG: uncomments to disable auto-movement */
    //m_actorMovements[i.key()] = Actor::None;

    if (i.value() == Actor::None)
    {
      /* set actor to non-moving */
      m_actorMovements[i.key()] = Actor::None;
      i.remove();
    }
  }
  return map;
}

bool Server::waitForClientConnections()
{
  // server must stay alive as long as sockets (qt parent mem mechanism)
  QTcpServer *tcpSrv = new QTcpServer(this);
  tcpSrv->listen(m_bindaddress, m_port);
  if (!tcpSrv->isListening())
  {
    qCritical() << "Error while creating socket:" << qPrintable(tcpSrv->errorString());
    return false;
  }
  qDebug() << "[Server] Listening on:"
      << qPrintable(QString("%1:%2").arg(tcpSrv->serverAddress().toString())
      .arg(tcpSrv->serverPort()));

  qDebug() << "[Server] Waiting for clients";
  ProtoBuf::Init packet;
  for (unsigned int i = 0; i < m_maxplayers; ++i)
  {
    bool connectionAvailable = tcpSrv->waitForNewConnection(-1);
    Q_ASSERT(connectionAvailable);
    Q_UNUSED(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 */
    packet.set_color(color);
    packet.set_maxplayers(m_maxplayers);
    Util::sendPacket(packet, socket);

    qDebug() << "[Connect] New Player: color=" << color;
  }

  qDebug() << "[Server] All Clients connected";
  return true;
}

void Server::sendUpdate(Transmission::map_t map)
{
  m_updatepacket.Clear();

  for (unsigned int x = 0; x < Constants::map_size.width; ++x)
  {
    for (unsigned int y = 0; y < Constants::map_size.height; ++y)
      m_updatepacket.add_field(map[x][y]);
  }

  for(unsigned i = 0; i < m_maxplayers; ++i)
  {
    m_updatepacket.add_round_points(m_actors.value(Color::order[i])->getRoundPoints());
    m_updatepacket.add_game_points(m_actors.value(Color::order[i])->getGamePoints());
  }

  QSharedPointer<QByteArray> data = Util::createPacket(m_updatepacket);
  foreach(QTcpSocket *socket, m_clientConnections)
  {
    if (!Util::sendPacket(data.data(), socket))
    {
      qDebug() << "[sendUpdate] Error while sending data to client, exiting...";
      qApp->quit();
    }
  }
}

void Server::keyPressUpdate()
{
  ProtoBuf::KeyPressUpdate packet;
  QMapIterator<Color::Color, QTcpSocket *> i(m_clientConnections);
  while (i.hasNext())
  {
    i.next();
    Color::Color color = i.key();
    QTcpSocket *socket = i.value();
    QDataStream in(i.value());
    while (socket->bytesAvailable() > (qint64)sizeof(qint64))
    {
      QSharedPointer<QByteArray> data = Util::receivePacket(socket);
      bool worked = packet.ParseFromArray(data->data(), data->size());
      Q_ASSERT(worked);
      Q_UNUSED(worked);
      Transmission::field_t direction = packet.newkey();
      qDebug() << "[KeyPress] actor=" << color << "direction=" << direction;
      m_actorMovements[color] = Util::transmissionMovementToActor(direction);
    }
  }
}

bool Server::parseCommandline()
{
  AnyOption opt;
  opt.setVerbose();

  /* usage strings must remain valid until parsing is done */
  QString exec = QFileInfo(qApp->applicationFilePath()).fileName();
  QByteArray usage;
  QTextStream out(&usage, QIODevice::ReadWrite | QIODevice::Text);
  out << "Usage: " << exec << " [OPTION]" << endl
      << "Usage: " << exec << " -h" << endl
      << endl;
  out << "  -b, --bind <bind_address>" << endl
      << "  Specifies the ip address on which the server listens for connections" << endl
      << "  Default: " << m_bindaddress.toString() << endl
      << endl;
  opt.setOption("bind", 'b');
  out << "  -p, --port <port>" << endl
      << "  Specifies the port on which the server listens for connections" << endl
      << "  Default: " << m_port << endl
      << endl;
  opt.setOption("port", 'p');
  out << "  -m, --maxplayers [1.." << m_maxplayers << "]" << endl
      << "  Specifies the maximum number of players/pacmans" << endl
      << "  Default: " << m_maxplayers << endl
      << endl;
  opt.setOption("maxplayers", 'm');
  out << "  --bots [0..maxplayers-1]" << endl
      << "  Specifies the number of AI pacmans/bots" << endl
      << "  Default: " << m_numbots << endl
      << endl;
  opt.setOption("bots");
  out << "  -h, --help" << endl
      << "  Prints this help message" << endl;
  opt.setFlag("help", 'h');
  out.flush();
  opt.addUsage(usage.constData());
  opt.processCommandArgs(qApp->argc(), qApp->argv());

  if (opt.getFlag("help") || opt.getFlag('h'))
  {
    opt.printUsage();
    return false;
  }

  if (opt.getValue("port") != NULL)
  {
    bool ok;
    m_port = QString(opt.getValue("port")).toUInt(&ok);
    if (!ok || m_port < 1 || m_port > 65535)
    {
      qCritical() << "Invalid port-option:" << opt.getValue("port") << endl
          << "Port must be between 1 and 65535";
      return false;
    }
  }

  if (opt.getValue("bind") != NULL)
  {
    m_bindaddress = opt.getValue("bind");
    if (m_bindaddress.isNull())
    {
      qCritical() << "Invalid bind-option:" << opt.getValue("bind") << endl
          << "Bind address must be an ip address";
      return false;
    }
  }

  if (opt.getValue("maxplayers") != NULL)
  {
    bool ok;
    unsigned int maxplayers = QString(opt.getValue("maxplayers")).toUInt(&ok);
    if (!ok || maxplayers < 1 || maxplayers > m_maxplayers)
    {
      qCritical() << "Invalid maxplayers-option:" << opt.getValue("maxplayers") << endl
          << "Maxplayers must be between 1 and" << m_maxplayers;
      return false;
    }
    m_maxplayers = maxplayers;
  }

  if (opt.getValue("bots") != NULL)
  {
    bool ok;
    unsigned int numbots = QString(opt.getValue("bots")).toUInt(&ok);
    if (!ok || numbots >= m_maxplayers)
    {
      qCritical() << "Invalid numbots-options:" << opt.getValue("bots") << endl
          << "AI pacmans/bots must be between 0 and" << m_maxplayers - 1;
      return false;
    }
    m_numbots = numbots;
  }

  return true;
}

bool Constants::server = true;

int main(int argc, char ** argv)
{
  /* Verify that the version of the library that we linked against is
   * compatible with the version of the headers we compiled against.
   */
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  QApplication app(argc, argv, false);
  app.setApplicationName("Pacman Server");
  app.setWindowIcon(QIcon(":/appicon"));

  qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));

  int ret = 0;
  Server server;
  if (!ret && !server.parseCommandline())
    ret = 1;
  if (!ret && !server.run())
    ret = 1;
  if (!ret)
    ret = app.exec();

  /* Delete all global objects allocated by libprotobuf */
  google::protobuf::ShutdownProtobufLibrary();

  return ret;
}