From 536ddd91ae7f162045226d4b358ba95d678f6deb Mon Sep 17 00:00:00 2001 From: manuel Date: Fri, 15 Apr 2011 03:20:17 +0200 Subject: add support for random bonus points --- pacman-c++/server.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'pacman-c++/server.cpp') diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index a045cad..01c74c4 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -3,6 +3,7 @@ #include "pacman.pb.h" #include "block.h" #include "anyoption.h" +#include "bonuspoint.h" #include #include #include @@ -26,7 +27,7 @@ bool Server::run() qDebug() << "[Server] Creating map..."; Transmission::map_t map = Util::createDemoMap(); Util::placeActors(map, m_maxplayers, Color::order); - Util::makePoints(map); + Util::fillPoints(map); updateMap(map); sendUpdate(map); Util::deleteMap(map); @@ -43,6 +44,12 @@ void Server::tick() qDebug() << "[Tick] Doing server update"; Transmission::map_t map = calculateUpdates(); updateMap(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); } @@ -161,6 +168,43 @@ invalid_direction: return map; } +QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t type) +{ + int chance = Constants::Random::bouns_point_chance; + int rand = (int) (chance * (qrand() / (RAND_MAX + 1.0))); + if (rand != 0) + return QPoint(); + + /* get list of valid positions */ + QList validpos; + for (unsigned int x = 0; x < Constants::map_size.width; ++x) + { + for (unsigned int y = 0; y < Constants::map_size.height; ++y) + { + if (visualMap[x][y] == NULL) + validpos.append(QPoint(x, y)); + } + } + + /* remove actors possitions from list + * performance would be better if actors would be listed in visualMap too + * but this isn't possible that easily. see comment in updateMap(map) + */ + foreach (Actor *tmp, m_actors) + { + QPoint oldpos = CoordToMapPosition(tmp->pos().toPoint()); + validpos.removeAll(oldpos); + } + + if (validpos.empty()) + return QPoint(); + + rand = (int) (validpos.size() * (qrand() / (RAND_MAX + 1.0))); + QPoint pos = validpos.at(rand); + map[pos.x()][pos.y()] = type; + return pos; +} + bool Server::waitForClientConnections() { // server must stay alive as long as sockets (qt parent mem mechanism) @@ -185,14 +229,12 @@ bool Server::waitForClientConnections() QTcpSocket *socket = tcpSrv->nextPendingConnection(); connect(socket, SIGNAL(readyRead()), this, SLOT(keyPressUpdate())); - /* assign color */ + /* assign color and notify client */ 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; } -- cgit v1.2.3