summaryrefslogtreecommitdiffstats
path: root/pacman-c++/server.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pacman-c++/server.cpp')
-rw-r--r--pacman-c++/server.cpp100
1 files changed, 61 insertions, 39 deletions
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp
index 72ee995..be45a66 100644
--- a/pacman-c++/server.cpp
+++ b/pacman-c++/server.cpp
@@ -11,7 +11,8 @@
11 11
12Server::Server(QWidget *parent) 12Server::Server(QWidget *parent)
13 : SceneHolder(parent), m_bindaddress(QHostAddress::Any), 13 : SceneHolder(parent), m_bindaddress(QHostAddress::Any),
14 m_port(Constants::Networking::port), m_rounds(3), m_numbots(0), m_curRound(0) 14 m_port(Constants::Networking::port), m_numbots(0),
15 m_rounds(3), m_curRound(0), m_roundFinished(false)
15{ 16{
16 /* determine max players by using order array */ 17 /* determine max players by using order array */
17 for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers); 18 for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers);
@@ -24,41 +25,65 @@ bool Server::run()
24 m_eatingorder.append(Color::order[i]); 25 m_eatingorder.append(Color::order[i]);
25 m_eatingorder.append(Color::order[0]); 26 m_eatingorder.append(Color::order[0]);
26 27
28 m_tickTimer = new QTimer(this);
29 m_tickTimer->setInterval(Constants::tick);
30 connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(tick()));
31
27 qDebug() << "[Server] Running server..."; 32 qDebug() << "[Server] Running server...";
28 qDebug() << "[Server] Max players:" << m_maxplayers; 33 qDebug() << "[Server] Max players:" << m_maxplayers;
29 qDebug() << "[Server] Number of rounds:" << m_rounds;
30 qDebug() << "[Server] Number of bots:" << m_numbots; 34 qDebug() << "[Server] Number of bots:" << m_numbots;
35 qDebug() << "[Server] Number of rounds:" << m_rounds;
31 if (!waitForClientConnections()) 36 if (!waitForClientConnections())
32 return false; 37 return false;
33 38
39 connect(this, SIGNAL(allPointsRemoved()), this, SLOT(setRoundFinished()));
34 initRoundMap(); 40 initRoundMap();
35
36 m_tickTimer = new QTimer(this);
37 connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(tick()));
38 m_tickTimer->start(Constants::tick);
39
40 return true; 41 return true;
41} 42}
42 43
43void Server::tick() 44void Server::tick()
44{ 45{
45 //qDebug() << "[Tick] Doing server update"; 46 //qDebug() << "[Tick] Doing server update";
46 foreach (Color::Color color, m_bots) 47 if (m_roundFinished)
47 botCalculate(m_actors[color]); 48 {
48 Transmission::map_t map = calculateUpdates(); 49 // TODO: call this when a pacman get's eaten
49 updateMap(map); 50 /* first finish previous round */
51 foreach(Actor *actor, m_actors)
52 actor->finishRound();
53
54 ++m_curRound;
55 if(m_curRound < m_rounds)
56 initRoundMap();
57 else
58 {
59 /* end of game */
60 qDebug() << "All round finished. Exiting...";
61 m_tickTimer->stop();
62 qApp->quit();
63 }
64 }
65 else
66 {
67 /* let the bots move */
68 foreach (Color::Color color, m_bots)
69 botCalculate(m_actors[color]);
50 70
51 /* add a random bonus point */ 71 /* move on the virtual map */
52 QPoint pos = addRandomPoint(map, Transmission::bonuspoint); 72 Transmission::map_t map = calculateUpdates();
53 if (!pos.isNull()) 73 updateMap(map);
54 updateMap(map, pos.x(), pos.y());
55 74
56 /* add/remove random colorized block */ 75 /* add a random bonus point */
57 if (this->property("coloredblocks").toBool()) 76 QPoint pos = addRandomPoint(map, Transmission::bonuspoint);
58 colorizeBlocks(map); 77 if (!pos.isNull())
78 updateMap(map, pos.x(), pos.y());
59 79
60 sendUpdate(map); 80 /* add/remove random colorized block */
61 Util::deleteMap(map); 81 if (this->property("coloredblocks").toBool())
82 colorizeBlocks(map);
83
84 sendUpdate(map);
85 Util::deleteMap(map);
86 }
62} 87}
63 88
64Transmission::map_t Server::calculateUpdates() 89Transmission::map_t Server::calculateUpdates()
@@ -364,10 +389,7 @@ void Server::botCalculate(Actor *actor)
364 /* check if neighbour is a block */ 389 /* check if neighbour is a block */
365 Block *block = qgraphicsitem_cast<Block *>(item); 390 Block *block = qgraphicsitem_cast<Block *>(item);
366 if (block != NULL && block->color() != actor->color()) 391 if (block != NULL && block->color() != actor->color())
367 {
368 i.remove(); 392 i.remove();
369 continue;
370 }
371 } 393 }
372 394
373 /* we're enclosed by blocks */ 395 /* we're enclosed by blocks */
@@ -410,7 +432,13 @@ void Server::botCalculate(Actor *actor)
410 int olddistance = (actorpos - otherpos).manhattanLength(); 432 int olddistance = (actorpos - otherpos).manhattanLength();
411 int newdistance = (newpos - otherpos).manhattanLength(); 433 int newdistance = (newpos - otherpos).manhattanLength();
412 if (newdistance >= olddistance) 434 if (newdistance >= olddistance)
413 i.setValue(i.value() += Constants::AI::weight_afraid); 435 i.setValue(i.value() + Constants::AI::weight_afraid);
436
437 /* check for blocks of own color: other pacman can't follow their */
438 GameEntity *item = visualMap[newpos.x()][newpos.y()];
439 Block *block = qgraphicsitem_cast<Block *>(item);
440 if (block != NULL && block->color() == actor->color())
441 i.setValue(i.value() + Constants::AI::weight_colorblock);
414 } 442 }
415 443
416 /* check for new positions in hunt list */ 444 /* check for new positions in hunt list */
@@ -419,7 +447,7 @@ void Server::botCalculate(Actor *actor)
419 int olddistance = (actorpos - otherpos).manhattanLength(); 447 int olddistance = (actorpos - otherpos).manhattanLength();
420 int newdistance = (newpos - otherpos).manhattanLength(); 448 int newdistance = (newpos - otherpos).manhattanLength();
421 if (newdistance <= olddistance) 449 if (newdistance <= olddistance)
422 i.setValue(i.value() += Constants::AI::weight_hunt); 450 i.setValue(i.value() + Constants::AI::weight_hunt);
423 } 451 }
424 452
425 /* check for bonuspoint */ 453 /* check for bonuspoint */
@@ -482,24 +510,15 @@ void Server::keyPressUpdate()
482 } 510 }
483} 511}
484 512
485void Server::onRoundFinished() 513void Server::setRoundFinished(bool value)
486{ 514{
487 // TODO: call this when a pacman get's eaten 515 m_roundFinished = value;
488 foreach(Actor *actor, m_actors)
489 actor->finishRound();
490
491 initRoundMap();
492 ++m_curRound;
493
494 /* end of game */
495 if(m_curRound >= m_rounds)
496 m_tickTimer->stop();
497} 516}
498 517
499void Server::initRoundMap() 518void Server::initRoundMap()
500{ 519{
501 /* delete actors first */ 520 qDebug() << "[initRoundMap] New round starts...";
502 removeActors(); 521 m_tickTimer->stop();
503 522
504 /* create new map */ 523 /* create new map */
505 Transmission::map_t map = Util::createDemoMap(); 524 Transmission::map_t map = Util::createDemoMap();
@@ -523,7 +542,9 @@ void Server::initRoundMap()
523 Util::deleteMap(map); 542 Util::deleteMap(map);
524 map = NULL; 543 map = NULL;
525 544
526 connect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished()), Qt::UniqueConnection); 545 m_roundFinished = false;
546 m_actorMovements.clear();
547 m_tickTimer->start();
527} 548}
528 549
529bool Server::parseCommandline() 550bool Server::parseCommandline()
@@ -564,6 +585,7 @@ bool Server::parseCommandline()
564 opt.setOption("rounds", 'r'); 585 opt.setOption("rounds", 'r');
565 out << " -r, --rounds [1..n]" << endl 586 out << " -r, --rounds [1..n]" << endl
566 << " Number of rounds to play" << endl 587 << " Number of rounds to play" << endl
588 << " Default: " << m_rounds << endl
567 << endl; 589 << endl;
568 opt.setFlag("nocolorblocks"); 590 opt.setFlag("nocolorblocks");
569 out << " -h, --help" << endl 591 out << " -h, --help" << endl