diff options
| author | manuel <manuel@mausz.at> | 2011-04-17 21:50:32 +0200 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2011-04-17 21:50:32 +0200 |
| commit | f5c00e32e6f32ca1216c72281b547faf372f0179 (patch) | |
| tree | bcaf8caacd662b4891591c0b04cf39d0d6a66c21 | |
| parent | becd390b551f4ee705aa14a97c90eaa8596c824c (diff) | |
| download | foop-f5c00e32e6f32ca1216c72281b547faf372f0179.tar.gz foop-f5c00e32e6f32ca1216c72281b547faf372f0179.tar.bz2 foop-f5c00e32e6f32ca1216c72281b547faf372f0179.zip | |
funny ai implementation
| -rw-r--r-- | pacman-c++/actor.cpp | 53 | ||||
| -rw-r--r-- | pacman-c++/actor.h | 4 | ||||
| -rw-r--r-- | pacman-c++/server.cpp | 63 | ||||
| -rw-r--r-- | pacman-c++/server.h | 1 |
4 files changed, 78 insertions, 43 deletions
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) | |||
| 128 | m_eating[m_direction]->stop(); | 128 | m_eating[m_direction]->stop(); |
| 129 | } | 129 | } |
| 130 | 130 | ||
| 131 | QPointF endpos(0, 0); | 131 | QPointF endpos = movementToPoint(direction); |
| 132 | switch(direction) | 132 | switch(direction) |
| 133 | { | 133 | { |
| 134 | case Actor::None: | ||
| 135 | break; | ||
| 136 | case Actor::Left: | 134 | case Actor::Left: |
| 137 | endpos.setX(static_cast<qreal>(Constants::field_size.width) * -1); | ||
| 138 | break; | ||
| 139 | case Actor::Right: | 135 | case Actor::Right: |
| 140 | endpos.setX(Constants::field_size.width); | 136 | endpos *= Constants::field_size.width; |
| 141 | break; | 137 | break; |
| 142 | case Actor::Up: | 138 | case Actor::Up: |
| 143 | endpos.setY(static_cast<qreal>(Constants::field_size.height) * -1); | ||
| 144 | break; | ||
| 145 | case Actor::Down: | 139 | case Actor::Down: |
| 146 | endpos.setY(Constants::field_size.height); | 140 | endpos *= Constants::field_size.height; |
| 147 | break; | 141 | break; |
| 142 | case Actor::None: | ||
| 148 | default: | 143 | default: |
| 149 | Q_ASSERT(false); | ||
| 150 | break; | 144 | break; |
| 151 | } | 145 | } |
| 152 | 146 | ||
| @@ -175,25 +169,19 @@ void Actor::move(Actor::Movement direction) | |||
| 175 | 169 | ||
| 176 | void Actor::moveByServer(Actor::Movement direction) | 170 | void Actor::moveByServer(Actor::Movement direction) |
| 177 | { | 171 | { |
| 178 | QPointF endpos(0, 0); | 172 | QPointF endpos = movementToPoint(direction); |
| 179 | switch(direction) | 173 | switch(direction) |
| 180 | { | 174 | { |
| 181 | case Actor::None: | ||
| 182 | break; | ||
| 183 | case Actor::Left: | 175 | case Actor::Left: |
| 184 | endpos.setX(static_cast<qreal>(Constants::field_size.width) * -1); | ||
| 185 | break; | ||
| 186 | case Actor::Right: | 176 | case Actor::Right: |
| 187 | endpos.setX(Constants::field_size.width); | 177 | endpos *= Constants::field_size.width; |
| 188 | break; | 178 | break; |
| 189 | case Actor::Up: | 179 | case Actor::Up: |
| 190 | endpos.setY(static_cast<qreal>(Constants::field_size.height) * -1); | ||
| 191 | break; | ||
| 192 | case Actor::Down: | 180 | case Actor::Down: |
| 193 | endpos.setY(Constants::field_size.height); | 181 | endpos *= Constants::field_size.height; |
| 194 | break; | 182 | break; |
| 183 | case Actor::None: | ||
| 195 | default: | 184 | default: |
| 196 | Q_ASSERT(false); | ||
| 197 | break; | 185 | break; |
| 198 | } | 186 | } |
| 199 | setPos(pos() + endpos); | 187 | setPos(pos() + endpos); |
| @@ -260,3 +248,28 @@ void Actor::finishRound() | |||
| 260 | m_gamePoints += m_roundPoints; | 248 | m_gamePoints += m_roundPoints; |
| 261 | m_roundPoints = 0; | 249 | m_roundPoints = 0; |
| 262 | } | 250 | } |
| 251 | |||
| 252 | QPoint Actor::movementToPoint(const Actor::Movement direction) | ||
| 253 | { | ||
| 254 | QPoint endpos(0,0); | ||
| 255 | switch (direction) | ||
| 256 | { | ||
| 257 | case Actor::Up: | ||
| 258 | endpos = QPoint(0, -1); | ||
| 259 | break; | ||
| 260 | case Actor::Down: | ||
| 261 | endpos = QPoint(0, 1); | ||
| 262 | break; | ||
| 263 | case Actor::Left: | ||
| 264 | endpos = QPoint(-1, 0); | ||
| 265 | break; | ||
| 266 | case Actor::Right: | ||
| 267 | endpos = QPoint(1, 0); | ||
| 268 | break; | ||
| 269 | case Actor::None: | ||
| 270 | break; | ||
| 271 | default: | ||
| 272 | Q_ASSERT(false); | ||
| 273 | } | ||
| 274 | return endpos; | ||
| 275 | } | ||
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: | |||
| 32 | virtual ~Actor() | 32 | virtual ~Actor() |
| 33 | {}; | 33 | {}; |
| 34 | 34 | ||
| 35 | QSequentialAnimationGroup *setupEatingAnimation(Actor::Movement direction); | ||
| 36 | PixmapItem &icon(); | 35 | PixmapItem &icon(); |
| 37 | Movement direction(); | 36 | Movement direction(); |
| 38 | bool isLocal(); | 37 | bool isLocal(); |
| @@ -49,8 +48,11 @@ public: | |||
| 49 | void addRoundPoints(unsigned int amount); | 48 | void addRoundPoints(unsigned int amount); |
| 50 | void finishRound(); | 49 | void finishRound(); |
| 51 | 50 | ||
| 51 | static QPoint movementToPoint(const Actor::Movement direction); | ||
| 52 | |||
| 52 | private: | 53 | private: |
| 53 | void moveByServer(Movement direction); | 54 | void moveByServer(Movement direction); |
| 55 | QSequentialAnimationGroup *setupEatingAnimation(Actor::Movement direction); | ||
| 54 | 56 | ||
| 55 | private: | 57 | private: |
| 56 | QPixmap m_pix; | 58 | 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() | |||
| 54 | void Server::tick() | 54 | void Server::tick() |
| 55 | { | 55 | { |
| 56 | //qDebug() << "[Tick] Doing server update"; | 56 | //qDebug() << "[Tick] Doing server update"; |
| 57 | //TODO: add ai here | 57 | foreach(Color::Color color, m_bots) |
| 58 | aiCalculate(m_actors[color]); | ||
| 58 | Transmission::map_t map = calculateUpdates(); | 59 | Transmission::map_t map = calculateUpdates(); |
| 59 | updateMap(map); | 60 | updateMap(map); |
| 60 | 61 | ||
| @@ -89,27 +90,7 @@ invalid_direction: | |||
| 89 | qDebug() << "[Calc] Actor wants to move: color=" << i.key() | 90 | qDebug() << "[Calc] Actor wants to move: color=" << i.key() |
| 90 | << "pos=" << mapPosition << "direction=" << direction; | 91 | << "pos=" << mapPosition << "direction=" << direction; |
| 91 | 92 | ||
| 92 | QPoint newMapPosition = mapPosition; | 93 | QPoint newMapPosition = mapPosition + Actor::movementToPoint(direction); |
| 93 | switch (direction) | ||
| 94 | { | ||
| 95 | case Actor::Up: | ||
| 96 | newMapPosition += QPoint(0, -1); | ||
| 97 | break; | ||
| 98 | case Actor::Down: | ||
| 99 | newMapPosition += QPoint(0, 1); | ||
| 100 | break; | ||
| 101 | case Actor::Left: | ||
| 102 | newMapPosition += QPoint(-1, 0); | ||
| 103 | break; | ||
| 104 | case Actor::Right: | ||
| 105 | newMapPosition += QPoint(1, 0); | ||
| 106 | break; | ||
| 107 | case Actor::None: | ||
| 108 | break; | ||
| 109 | default: | ||
| 110 | Q_ASSERT(false); | ||
| 111 | } | ||
| 112 | |||
| 113 | if (newMapPosition.x() < 0) | 94 | if (newMapPosition.x() < 0) |
| 114 | newMapPosition.setX(0); | 95 | newMapPosition.setX(0); |
| 115 | if (newMapPosition.x() >= visualMap.size()) | 96 | if (newMapPosition.x() >= visualMap.size()) |
| @@ -308,7 +289,10 @@ bool Server::waitForClientConnections() | |||
| 308 | } | 289 | } |
| 309 | 290 | ||
| 310 | for (unsigned int i = (m_maxplayers - m_numbots); i < m_maxplayers; ++i) | 291 | for (unsigned int i = (m_maxplayers - m_numbots); i < m_maxplayers; ++i) |
| 292 | { | ||
| 311 | m_bots.append(Color::order[i]); | 293 | m_bots.append(Color::order[i]); |
| 294 | m_actorMovements[Color::order[i]] = Actor::None; | ||
| 295 | } | ||
| 312 | 296 | ||
| 313 | qDebug() << "[Server] All Clients connected"; | 297 | qDebug() << "[Server] All Clients connected"; |
| 314 | return true; | 298 | return true; |
| @@ -355,6 +339,41 @@ void Server::sendUpdate(Transmission::map_t map) | |||
| 355 | } | 339 | } |
| 356 | } | 340 | } |
| 357 | 341 | ||
| 342 | void Server::aiCalculate(Actor *actor) | ||
| 343 | { | ||
| 344 | /* move as long as possible in one direction */ | ||
| 345 | if (m_actorMovements[actor->color()] != Actor::None) | ||
| 346 | return; | ||
| 347 | |||
| 348 | QPoint actorpos = CoordToMapPosition(actor->pos().toPoint()); | ||
| 349 | QList<Actor::Movement> directions; | ||
| 350 | directions << Actor::Left << Actor::Right << Actor::Up << Actor::Down; | ||
| 351 | |||
| 352 | QMutableListIterator<Actor::Movement> i(directions); | ||
| 353 | while(i.hasNext()) | ||
| 354 | { | ||
| 355 | i.next(); | ||
| 356 | Actor::Movement direction = i.value(); | ||
| 357 | QPoint pos = actorpos + Actor::movementToPoint(direction); | ||
| 358 | if (pos.x() < 0 || pos.x() >= visualMap.size()) | ||
| 359 | continue; | ||
| 360 | if (pos.y() < 0 || pos.y() >= visualMap[pos.x()].size()) | ||
| 361 | continue; | ||
| 362 | GameEntity *item = visualMap[pos.x()][pos.y()]; | ||
| 363 | |||
| 364 | /* check if neighbour is a block */ | ||
| 365 | Block *block = qgraphicsitem_cast<Block *>(item); | ||
| 366 | if (block != NULL && block->color() != actor->color()) | ||
| 367 | { | ||
| 368 | i.remove(); | ||
| 369 | continue; | ||
| 370 | } | ||
| 371 | } | ||
| 372 | |||
| 373 | int rand = (int) (directions.size() * (qrand() / (RAND_MAX + 1.0))); | ||
| 374 | m_actorMovements[actor->color()] = directions.at(rand); | ||
| 375 | } | ||
| 376 | |||
| 358 | void Server::keyPressUpdate() | 377 | void Server::keyPressUpdate() |
| 359 | { | 378 | { |
| 360 | ProtoBuf::KeyPressUpdate packet; | 379 | 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: | |||
| 36 | 36 | ||
| 37 | QPoint addRandomPoint(Transmission::map_t map, Transmission::field_t type = Transmission::bonuspoint); | 37 | QPoint addRandomPoint(Transmission::map_t map, Transmission::field_t type = Transmission::bonuspoint); |
| 38 | void colorizeBlocks(Transmission::map_t map); | 38 | void colorizeBlocks(Transmission::map_t map); |
| 39 | void aiCalculate(Actor *actor); | ||
| 39 | 40 | ||
| 40 | protected: | 41 | protected: |
| 41 | QMap<Color::Color, QTcpSocket *> m_clientConnections; | 42 | QMap<Color::Color, QTcpSocket *> m_clientConnections; |
