diff options
Diffstat (limited to 'pacman-c++/server.cpp')
| -rw-r--r-- | pacman-c++/server.cpp | 143 |
1 files changed, 90 insertions, 53 deletions
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index be45a66..ef271f0 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp | |||
| @@ -8,11 +8,12 @@ | |||
| 8 | #include <QtNetwork/QTcpServer> | 8 | #include <QtNetwork/QTcpServer> |
| 9 | #include <QtNetwork/QTcpSocket> | 9 | #include <QtNetwork/QTcpSocket> |
| 10 | #include <QTextStream> | 10 | #include <QTextStream> |
| 11 | #include <sys/types.h> | ||
| 11 | 12 | ||
| 12 | Server::Server(QWidget *parent) | 13 | Server::Server(QWidget *parent) |
| 13 | : SceneHolder(parent), m_bindaddress(QHostAddress::Any), | 14 | : SceneHolder(parent), m_bindaddress(QHostAddress::Any), |
| 14 | m_port(Constants::Networking::port), m_numbots(0), | 15 | m_port(Constants::Networking::port), m_numbots(0), |
| 15 | m_rounds(3), m_curRound(0), m_roundFinished(false) | 16 | m_rounds(3), m_curRound(0), m_running(false), m_finishRound(false) |
| 16 | { | 17 | { |
| 17 | /* determine max players by using order array */ | 18 | /* determine max players by using order array */ |
| 18 | for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers); | 19 | for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers); |
| @@ -36,54 +37,43 @@ bool Server::run() | |||
| 36 | if (!waitForClientConnections()) | 37 | if (!waitForClientConnections()) |
| 37 | return false; | 38 | return false; |
| 38 | 39 | ||
| 39 | connect(this, SIGNAL(allPointsRemoved()), this, SLOT(setRoundFinished())); | 40 | connect(this, SIGNAL(allPointsRemoved()), this, SLOT(setFinishRound())); |
| 40 | initRoundMap(); | 41 | initRoundMap(); |
| 41 | return true; | 42 | return true; |
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | void Server::tick() | 45 | void Server::tick() |
| 45 | { | 46 | { |
| 46 | //qDebug() << "[Tick] Doing server update"; | 47 | qDebug() << "[Tick] Doing server update"; |
| 47 | if (m_roundFinished) | 48 | if (m_finishRound) |
| 49 | stopGame(true); | ||
| 50 | if (!m_running) | ||
| 48 | { | 51 | { |
| 49 | // TODO: call this when a pacman get's eaten | 52 | Transmission::map_t map = Util::createEmptyMap(); |
| 50 | /* first finish previous round */ | 53 | sendUpdate(map); |
| 51 | foreach(Actor *actor, m_actors) | 54 | Util::deleteMap(map); |
| 52 | actor->finishRound(); | 55 | return; |
| 53 | |||
| 54 | ++m_curRound; | ||
| 55 | if(m_curRound < m_rounds) | ||
| 56 | initRoundMap(); | ||
| 57 | else | ||
| 58 | { | ||
| 59 | /* end of game */ | ||
| 60 | qDebug() << "All round finished. Exiting..."; | ||
| 61 | m_tickTimer->stop(); | ||
| 62 | qApp->quit(); | ||
| 63 | } | ||
| 64 | } | 56 | } |
| 65 | else | ||
| 66 | { | ||
| 67 | /* let the bots move */ | ||
| 68 | foreach (Color::Color color, m_bots) | ||
| 69 | botCalculate(m_actors[color]); | ||
| 70 | 57 | ||
| 71 | /* move on the virtual map */ | 58 | /* let the bots move */ |
| 72 | Transmission::map_t map = calculateUpdates(); | 59 | foreach (Color::Color color, m_bots) |
| 73 | updateMap(map); | 60 | botCalculate(m_actors[color]); |
| 74 | 61 | ||
| 75 | /* add a random bonus point */ | 62 | /* move on the virtual map */ |
| 76 | QPoint pos = addRandomPoint(map, Transmission::bonuspoint); | 63 | Transmission::map_t map = calculateUpdates(); |
| 77 | if (!pos.isNull()) | 64 | updateMap(map); |
| 78 | updateMap(map, pos.x(), pos.y()); | ||
| 79 | 65 | ||
| 80 | /* add/remove random colorized block */ | 66 | /* add a random bonus point */ |
| 81 | if (this->property("coloredblocks").toBool()) | 67 | QPoint pos = addRandomPoint(map, Transmission::bonuspoint); |
| 82 | colorizeBlocks(map); | 68 | if (!pos.isNull()) |
| 69 | updateMap(map, pos.x(), pos.y()); | ||
| 83 | 70 | ||
| 84 | sendUpdate(map); | 71 | /* add/remove random colorized block */ |
| 85 | Util::deleteMap(map); | 72 | if (this->property("coloredblocks").toBool()) |
| 86 | } | 73 | colorizeBlocks(map); |
| 74 | |||
| 75 | sendUpdate(map); | ||
| 76 | Util::deleteMap(map); | ||
| 87 | } | 77 | } |
| 88 | 78 | ||
| 89 | Transmission::map_t Server::calculateUpdates() | 79 | Transmission::map_t Server::calculateUpdates() |
| @@ -94,14 +84,15 @@ Transmission::map_t Server::calculateUpdates() | |||
| 94 | while (i.hasNext()) | 84 | while (i.hasNext()) |
| 95 | { | 85 | { |
| 96 | i.next(); | 86 | i.next(); |
| 97 | Actor *actor = m_actors.value(i.key()); | 87 | Color::Color color = i.key(); |
| 88 | Actor *actor = m_actors[color]; | ||
| 98 | QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); | 89 | QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); |
| 99 | Actor::Movement direction = i.value(); | 90 | Actor::Movement direction = i.value(); |
| 100 | int turn = 0; | 91 | int turn = 0; |
| 101 | 92 | ||
| 102 | invalid_direction: | 93 | invalid_direction: |
| 103 | ++turn; | 94 | ++turn; |
| 104 | qDebug() << "[Calc] Actor wants to move: color=" << i.key() | 95 | qDebug() << "[Calc] Actor wants to move: color=" << color |
| 105 | << "pos=" << mapPosition << "direction=" << direction; | 96 | << "pos=" << mapPosition << "direction=" << direction; |
| 106 | 97 | ||
| 107 | QPoint newMapPosition = mapPosition + Actor::movementToPoint(direction); | 98 | QPoint newMapPosition = mapPosition + Actor::movementToPoint(direction); |
| @@ -132,10 +123,15 @@ invalid_direction: | |||
| 132 | else | 123 | else |
| 133 | { | 124 | { |
| 134 | /* apply actions of entering this field */ | 125 | /* apply actions of entering this field */ |
| 135 | bool survive = item->enter(actor); | 126 | GameEntity::EnteredState survive = item->enter(actor); |
| 136 | if (!survive) | 127 | if (survive == GameEntity::DestroyedEntity) |
| 137 | { | ||
| 138 | map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty | actor->color(); | 128 | map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty | actor->color(); |
| 129 | else if (survive == GameEntity::DestroyedActor) | ||
| 130 | { | ||
| 131 | m_actors[item->color()]->addRoundPoints(actor->getRoundPoints()); | ||
| 132 | actor->finishRound(true); | ||
| 133 | map[newMapPosition.x()][newMapPosition.y()] = Transmission::death | actor->color(); | ||
| 134 | setFinishRound(); | ||
| 139 | } | 135 | } |
| 140 | } | 136 | } |
| 141 | } | 137 | } |
| @@ -161,7 +157,7 @@ invalid_direction: | |||
| 161 | } | 157 | } |
| 162 | 158 | ||
| 163 | map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | | 159 | map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | |
| 164 | i.key() | Util::actorMovementToTransmission(direction); | 160 | color | Util::actorMovementToTransmission(direction); |
| 165 | 161 | ||
| 166 | /* DEBUG: uncomments to disable auto-movement */ | 162 | /* DEBUG: uncomments to disable auto-movement */ |
| 167 | //direction = Actor::None; | 163 | //direction = Actor::None; |
| @@ -169,7 +165,7 @@ invalid_direction: | |||
| 169 | if (direction == Actor::None) | 165 | if (direction == Actor::None) |
| 170 | { | 166 | { |
| 171 | /* set actor to non-moving */ | 167 | /* set actor to non-moving */ |
| 172 | m_actorMovements[i.key()] = Actor::None; | 168 | m_actorMovements[color] = Actor::None; |
| 173 | i.remove(); | 169 | i.remove(); |
| 174 | } | 170 | } |
| 175 | } | 171 | } |
| @@ -228,11 +224,12 @@ void Server::colorizeBlocks(Transmission::map_t map) | |||
| 228 | i.setValue(--val); | 224 | i.setValue(--val); |
| 229 | else | 225 | else |
| 230 | { | 226 | { |
| 227 | QPoint block = i.key(); | ||
| 231 | /* check for actor collision */ | 228 | /* check for actor collision */ |
| 232 | bool skip = false; | 229 | bool skip = false; |
| 233 | foreach (Actor *actor, m_actors) | 230 | foreach (Actor *actor, m_actors) |
| 234 | { | 231 | { |
| 235 | if (CoordToMapPosition(actor->pos().toPoint()) == i.key()) | 232 | if (CoordToMapPosition(actor->pos().toPoint()) == block) |
| 236 | skip = true; | 233 | skip = true; |
| 237 | if (skip) | 234 | if (skip) |
| 238 | break; | 235 | break; |
| @@ -240,7 +237,6 @@ void Server::colorizeBlocks(Transmission::map_t map) | |||
| 240 | if (skip) | 237 | if (skip) |
| 241 | continue; | 238 | continue; |
| 242 | 239 | ||
| 243 | QPoint block = i.key(); | ||
| 244 | map[block.x()][block.y()] |= Transmission::block | Color::none; | 240 | map[block.x()][block.y()] |= Transmission::block | Color::none; |
| 245 | updateMap(map, block.x(), block.y()); | 241 | updateMap(map, block.x(), block.y()); |
| 246 | m_blocks.append(block); | 242 | m_blocks.append(block); |
| @@ -510,16 +506,14 @@ void Server::keyPressUpdate() | |||
| 510 | } | 506 | } |
| 511 | } | 507 | } |
| 512 | 508 | ||
| 513 | void Server::setRoundFinished(bool value) | ||
| 514 | { | ||
| 515 | m_roundFinished = value; | ||
| 516 | } | ||
| 517 | |||
| 518 | void Server::initRoundMap() | 509 | void Server::initRoundMap() |
| 519 | { | 510 | { |
| 520 | qDebug() << "[initRoundMap] New round starts..."; | 511 | qDebug() << "[initRoundMap] New round starts..."; |
| 521 | m_tickTimer->stop(); | 512 | m_tickTimer->stop(); |
| 522 | 513 | ||
| 514 | /* reset scene and clean up items */ | ||
| 515 | reset(); | ||
| 516 | |||
| 523 | /* create new map */ | 517 | /* create new map */ |
| 524 | Transmission::map_t map = Util::createDemoMap(); | 518 | Transmission::map_t map = Util::createDemoMap(); |
| 525 | Util::placeActors(map, m_maxplayers, Color::order); | 519 | Util::placeActors(map, m_maxplayers, Color::order); |
| @@ -542,11 +536,54 @@ void Server::initRoundMap() | |||
| 542 | Util::deleteMap(map); | 536 | Util::deleteMap(map); |
| 543 | map = NULL; | 537 | map = NULL; |
| 544 | 538 | ||
| 545 | m_roundFinished = false; | ||
| 546 | m_actorMovements.clear(); | 539 | m_actorMovements.clear(); |
| 540 | |||
| 541 | disconnect(AudioManager::self()->audioPlayer(), NULL, this, NULL); | ||
| 542 | connect(AudioManager::self()->audioPlayer(), SIGNAL(finished()), this, SLOT(startGame())); | ||
| 543 | AudioManager::self()->play(Sound::Intro, true); | ||
| 547 | m_tickTimer->start(); | 544 | m_tickTimer->start(); |
| 548 | } | 545 | } |
| 549 | 546 | ||
| 547 | void Server::startGame() | ||
| 548 | { | ||
| 549 | m_running = true; | ||
| 550 | } | ||
| 551 | |||
| 552 | void Server::stopGame(bool delay) | ||
| 553 | { | ||
| 554 | /* first finish previous round */ | ||
| 555 | foreach(Actor *actor, m_actors) | ||
| 556 | actor->finishRound(); | ||
| 557 | m_finishRound = false; | ||
| 558 | m_running = false; | ||
| 559 | |||
| 560 | /* add delay if requested */ | ||
| 561 | if (delay) | ||
| 562 | { | ||
| 563 | disconnect(AudioManager::self()->audioPlayer(), NULL, this, NULL); | ||
| 564 | connect(AudioManager::self()->audioPlayer(), SIGNAL(finished()), this, SLOT(stopGame())); | ||
| 565 | AudioManager::self()->play(Sound::Die, true); | ||
| 566 | return; | ||
| 567 | } | ||
| 568 | |||
| 569 | /* do next-round work */ | ||
| 570 | ++m_curRound; | ||
| 571 | if(m_rounds == 0 || m_curRound < m_rounds) | ||
| 572 | initRoundMap(); | ||
| 573 | else | ||
| 574 | { | ||
| 575 | /* end of game */ | ||
| 576 | qDebug() << "All round finished. Exiting..."; | ||
| 577 | qApp->quit(); | ||
| 578 | } | ||
| 579 | } | ||
| 580 | |||
| 581 | |||
| 582 | void Server::setFinishRound() | ||
| 583 | { | ||
| 584 | m_finishRound = true; | ||
| 585 | } | ||
| 586 | |||
| 550 | bool Server::parseCommandline() | 587 | bool Server::parseCommandline() |
| 551 | { | 588 | { |
| 552 | AnyOption opt; | 589 | AnyOption opt; |
| @@ -583,7 +620,7 @@ bool Server::parseCommandline() | |||
| 583 | << " Disable random colorized blocks" << endl | 620 | << " Disable random colorized blocks" << endl |
| 584 | << endl; | 621 | << endl; |
| 585 | opt.setOption("rounds", 'r'); | 622 | opt.setOption("rounds", 'r'); |
| 586 | out << " -r, --rounds [1..n]" << endl | 623 | out << " -r, --rounds [0 | 1..n]" << endl |
| 587 | << " Number of rounds to play" << endl | 624 | << " Number of rounds to play" << endl |
| 588 | << " Default: " << m_rounds << endl | 625 | << " Default: " << m_rounds << endl |
| 589 | << endl; | 626 | << endl; |
