From bbd2a69a962d15f74a4afcb7b66462eac9fa5008 Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 20 Apr 2011 02:19:08 +0200 Subject: game rounds finally implemented: - game rounds will be detected during the round AND - a new map will be send in the NEXT tick to the clients --- pacman-c++/server.cpp | 100 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 61 insertions(+), 39 deletions(-) (limited to 'pacman-c++/server.cpp') 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 @@ Server::Server(QWidget *parent) : SceneHolder(parent), m_bindaddress(QHostAddress::Any), - m_port(Constants::Networking::port), m_rounds(3), m_numbots(0), m_curRound(0) + m_port(Constants::Networking::port), m_numbots(0), + m_rounds(3), m_curRound(0), m_roundFinished(false) { /* determine max players by using order array */ for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers); @@ -24,41 +25,65 @@ bool Server::run() m_eatingorder.append(Color::order[i]); m_eatingorder.append(Color::order[0]); + m_tickTimer = new QTimer(this); + m_tickTimer->setInterval(Constants::tick); + connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(tick())); + qDebug() << "[Server] Running server..."; qDebug() << "[Server] Max players:" << m_maxplayers; - qDebug() << "[Server] Number of rounds:" << m_rounds; qDebug() << "[Server] Number of bots:" << m_numbots; + qDebug() << "[Server] Number of rounds:" << m_rounds; if (!waitForClientConnections()) return false; + connect(this, SIGNAL(allPointsRemoved()), this, SLOT(setRoundFinished())); initRoundMap(); - - m_tickTimer = new QTimer(this); - connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(tick())); - m_tickTimer->start(Constants::tick); - return true; } void Server::tick() { //qDebug() << "[Tick] Doing server update"; - foreach (Color::Color color, m_bots) - botCalculate(m_actors[color]); - Transmission::map_t map = calculateUpdates(); - updateMap(map); + if (m_roundFinished) + { + // TODO: call this when a pacman get's eaten + /* first finish previous round */ + foreach(Actor *actor, m_actors) + actor->finishRound(); + + ++m_curRound; + if(m_curRound < m_rounds) + initRoundMap(); + else + { + /* end of game */ + qDebug() << "All round finished. Exiting..."; + m_tickTimer->stop(); + qApp->quit(); + } + } + else + { + /* let the bots move */ + foreach (Color::Color color, m_bots) + botCalculate(m_actors[color]); - /* add a random bonus point */ - QPoint pos = addRandomPoint(map, Transmission::bonuspoint); - if (!pos.isNull()) - updateMap(map, pos.x(), pos.y()); + /* move on the virtual map */ + Transmission::map_t map = calculateUpdates(); + updateMap(map); - /* add/remove random colorized block */ - if (this->property("coloredblocks").toBool()) - colorizeBlocks(map); + /* add a random bonus point */ + QPoint pos = addRandomPoint(map, Transmission::bonuspoint); + if (!pos.isNull()) + updateMap(map, pos.x(), pos.y()); - sendUpdate(map); - Util::deleteMap(map); + /* add/remove random colorized block */ + if (this->property("coloredblocks").toBool()) + colorizeBlocks(map); + + sendUpdate(map); + Util::deleteMap(map); + } } Transmission::map_t Server::calculateUpdates() @@ -364,10 +389,7 @@ void Server::botCalculate(Actor *actor) /* check if neighbour is a block */ Block *block = qgraphicsitem_cast(item); if (block != NULL && block->color() != actor->color()) - { i.remove(); - continue; - } } /* we're enclosed by blocks */ @@ -410,7 +432,13 @@ void Server::botCalculate(Actor *actor) int olddistance = (actorpos - otherpos).manhattanLength(); int newdistance = (newpos - otherpos).manhattanLength(); if (newdistance >= olddistance) - i.setValue(i.value() += Constants::AI::weight_afraid); + i.setValue(i.value() + Constants::AI::weight_afraid); + + /* check for blocks of own color: other pacman can't follow their */ + GameEntity *item = visualMap[newpos.x()][newpos.y()]; + Block *block = qgraphicsitem_cast(item); + if (block != NULL && block->color() == actor->color()) + i.setValue(i.value() + Constants::AI::weight_colorblock); } /* check for new positions in hunt list */ @@ -419,7 +447,7 @@ void Server::botCalculate(Actor *actor) int olddistance = (actorpos - otherpos).manhattanLength(); int newdistance = (newpos - otherpos).manhattanLength(); if (newdistance <= olddistance) - i.setValue(i.value() += Constants::AI::weight_hunt); + i.setValue(i.value() + Constants::AI::weight_hunt); } /* check for bonuspoint */ @@ -482,24 +510,15 @@ void Server::keyPressUpdate() } } -void Server::onRoundFinished() +void Server::setRoundFinished(bool value) { - // TODO: call this when a pacman get's eaten - foreach(Actor *actor, m_actors) - actor->finishRound(); - - initRoundMap(); - ++m_curRound; - - /* end of game */ - if(m_curRound >= m_rounds) - m_tickTimer->stop(); + m_roundFinished = value; } void Server::initRoundMap() { - /* delete actors first */ - removeActors(); + qDebug() << "[initRoundMap] New round starts..."; + m_tickTimer->stop(); /* create new map */ Transmission::map_t map = Util::createDemoMap(); @@ -523,7 +542,9 @@ void Server::initRoundMap() Util::deleteMap(map); map = NULL; - connect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished()), Qt::UniqueConnection); + m_roundFinished = false; + m_actorMovements.clear(); + m_tickTimer->start(); } bool Server::parseCommandline() @@ -564,6 +585,7 @@ bool Server::parseCommandline() opt.setOption("rounds", 'r'); out << " -r, --rounds [1..n]" << endl << " Number of rounds to play" << endl + << " Default: " << m_rounds << endl << endl; opt.setFlag("nocolorblocks"); out << " -h, --help" << endl -- cgit v1.2.3