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++/actor.cpp | 53 +++++++++++++++++++++++++++---------------- pacman-c++/actor.h | 4 +++- pacman-c++/server.cpp | 63 +++++++++++++++++++++++++++++++++------------------ pacman-c++/server.h | 1 + 4 files changed, 78 insertions(+), 43 deletions(-) (limited to 'pacman-c++') diff --git a/pacman-c++/actor.cpp b/pacman-c++/actor.cpp index fb4b38a..9ff51f3 100644 --- a/pacman-c++/actor.cpp +++ b/pacman-c++/actor.cpp @@ -128,25 +128,19 @@ void Actor::move(Actor::Movement direction) m_eating[m_direction]->stop(); } - QPointF endpos(0, 0); + QPointF endpos = movementToPoint(direction); switch(direction) { - case Actor::None: - break; case Actor::Left: - endpos.setX(static_cast(Constants::field_size.width) * -1); - break; case Actor::Right: - endpos.setX(Constants::field_size.width); + endpos *= Constants::field_size.width; break; case Actor::Up: - endpos.setY(static_cast(Constants::field_size.height) * -1); - break; case Actor::Down: - endpos.setY(Constants::field_size.height); + endpos *= Constants::field_size.height; break; + case Actor::None: default: - Q_ASSERT(false); break; } @@ -175,25 +169,19 @@ void Actor::move(Actor::Movement direction) void Actor::moveByServer(Actor::Movement direction) { - QPointF endpos(0, 0); + QPointF endpos = movementToPoint(direction); switch(direction) { - case Actor::None: - break; case Actor::Left: - endpos.setX(static_cast(Constants::field_size.width) * -1); - break; case Actor::Right: - endpos.setX(Constants::field_size.width); + endpos *= Constants::field_size.width; break; case Actor::Up: - endpos.setY(static_cast(Constants::field_size.height) * -1); - break; case Actor::Down: - endpos.setY(Constants::field_size.height); + endpos *= Constants::field_size.height; break; + case Actor::None: default: - Q_ASSERT(false); break; } setPos(pos() + endpos); @@ -260,3 +248,28 @@ void Actor::finishRound() m_gamePoints += m_roundPoints; m_roundPoints = 0; } + +QPoint Actor::movementToPoint(const Actor::Movement direction) +{ + QPoint endpos(0,0); + switch (direction) + { + case Actor::Up: + endpos = QPoint(0, -1); + break; + case Actor::Down: + endpos = QPoint(0, 1); + break; + case Actor::Left: + endpos = QPoint(-1, 0); + break; + case Actor::Right: + endpos = QPoint(1, 0); + break; + case Actor::None: + break; + default: + Q_ASSERT(false); + } + return endpos; +} diff --git a/pacman-c++/actor.h b/pacman-c++/actor.h index 0738593..902951b 100644 --- a/pacman-c++/actor.h +++ b/pacman-c++/actor.h @@ -32,7 +32,6 @@ public: virtual ~Actor() {}; - QSequentialAnimationGroup *setupEatingAnimation(Actor::Movement direction); PixmapItem &icon(); Movement direction(); bool isLocal(); @@ -49,8 +48,11 @@ public: void addRoundPoints(unsigned int amount); void finishRound(); + static QPoint movementToPoint(const Actor::Movement direction); + private: void moveByServer(Movement direction); + QSequentialAnimationGroup *setupEatingAnimation(Actor::Movement direction); private: QPixmap m_pix; 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; diff --git a/pacman-c++/server.h b/pacman-c++/server.h index 1a25137..a4bbe58 100644 --- a/pacman-c++/server.h +++ b/pacman-c++/server.h @@ -36,6 +36,7 @@ protected: QPoint addRandomPoint(Transmission::map_t map, Transmission::field_t type = Transmission::bonuspoint); void colorizeBlocks(Transmission::map_t map); + void aiCalculate(Actor *actor); protected: QMap m_clientConnections; -- cgit v1.2.3