From f5c00e32e6f32ca1216c72281b547faf372f0179 Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 17 Apr 2011 21:50:32 +0200 Subject: funny ai implementation --- pacman-c++/server.cpp | 63 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 22 deletions(-) (limited to 'pacman-c++/server.cpp') diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index 9baa9ab..516d1f7 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -54,7 +54,8 @@ bool Server::run() void Server::tick() { //qDebug() << "[Tick] Doing server update"; - //TODO: add ai here + foreach(Color::Color color, m_bots) + aiCalculate(m_actors[color]); Transmission::map_t map = calculateUpdates(); updateMap(map); @@ -89,27 +90,7 @@ invalid_direction: qDebug() << "[Calc] Actor wants to move: color=" << i.key() << "pos=" << mapPosition << "direction=" << direction; - QPoint newMapPosition = mapPosition; - switch (direction) - { - 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); - } - + QPoint newMapPosition = mapPosition + Actor::movementToPoint(direction); if (newMapPosition.x() < 0) newMapPosition.setX(0); if (newMapPosition.x() >= visualMap.size()) @@ -308,7 +289,10 @@ bool Server::waitForClientConnections() } for (unsigned int i = (m_maxplayers - m_numbots); i < m_maxplayers; ++i) + { m_bots.append(Color::order[i]); + m_actorMovements[Color::order[i]] = Actor::None; + } qDebug() << "[Server] All Clients connected"; return true; @@ -355,6 +339,41 @@ void Server::sendUpdate(Transmission::map_t map) } } +void Server::aiCalculate(Actor *actor) +{ + /* move as long as possible in one direction */ + if (m_actorMovements[actor->color()] != Actor::None) + return; + + QPoint actorpos = CoordToMapPosition(actor->pos().toPoint()); + QList directions; + directions << Actor::Left << Actor::Right << Actor::Up << Actor::Down; + + QMutableListIterator i(directions); + while(i.hasNext()) + { + i.next(); + Actor::Movement direction = i.value(); + QPoint pos = actorpos + Actor::movementToPoint(direction); + if (pos.x() < 0 || pos.x() >= visualMap.size()) + continue; + if (pos.y() < 0 || pos.y() >= visualMap[pos.x()].size()) + continue; + GameEntity *item = visualMap[pos.x()][pos.y()]; + + /* check if neighbour is a block */ + Block *block = qgraphicsitem_cast(item); + if (block != NULL && block->color() != actor->color()) + { + i.remove(); + continue; + } + } + + int rand = (int) (directions.size() * (qrand() / (RAND_MAX + 1.0))); + m_actorMovements[actor->color()] = directions.at(rand); +} + void Server::keyPressUpdate() { ProtoBuf::KeyPressUpdate packet; -- cgit v1.2.3