diff options
Diffstat (limited to 'pacman-c++/server.cpp')
| -rw-r--r-- | pacman-c++/server.cpp | 127 |
1 files changed, 104 insertions, 23 deletions
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index 07e4c72..1c3caf9 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp | |||
| @@ -11,7 +11,7 @@ | |||
| 11 | 11 | ||
| 12 | Server::Server(QWidget *parent) | 12 | Server::Server(QWidget *parent) |
| 13 | : SceneHolder(parent), m_bindaddress(QHostAddress::Any), | 13 | : SceneHolder(parent), m_bindaddress(QHostAddress::Any), |
| 14 | m_port(Constants::Networking::port), m_numbots(0) | 14 | m_port(Constants::Networking::port), m_rounds(3), m_numbots(0), m_curRound(0) |
| 15 | { | 15 | { |
| 16 | /* determine max players by using order array */ | 16 | /* determine max players by using order array */ |
| 17 | for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers); | 17 | for(m_maxplayers = 0; Color::order[m_maxplayers] != Color::none; ++m_maxplayers); |
| @@ -26,34 +26,25 @@ bool Server::run() | |||
| 26 | 26 | ||
| 27 | qDebug() << "[Server] Running server..."; | 27 | qDebug() << "[Server] Running server..."; |
| 28 | qDebug() << "[Server] Max players:" << m_maxplayers; | 28 | qDebug() << "[Server] Max players:" << m_maxplayers; |
| 29 | qDebug() << "[Server] Number of rounds:" << m_rounds; | ||
| 29 | qDebug() << "[Server] Number of bots:" << m_numbots; | 30 | qDebug() << "[Server] Number of bots:" << m_numbots; |
| 30 | if (!waitForClientConnections()) | 31 | if (!waitForClientConnections()) |
| 31 | return false; | 32 | return false; |
| 32 | 33 | ||
| 33 | qDebug() << "[Server] Creating map..."; | 34 | initRoundMap(true); |
| 34 | Transmission::map_t map = Util::createDemoMap(); | ||
| 35 | Util::placeActors(map, m_maxplayers, Color::order); | ||
| 36 | Util::fillPoints(map); | ||
| 37 | 35 | ||
| 38 | /* save positions of blocks for later usage */ | 36 | m_tickTimer = new QTimer(this); |
| 39 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | 37 | connect(m_tickTimer, SIGNAL(timeout()), this, SLOT(tick())); |
| 40 | { | 38 | m_tickTimer->start(Constants::tick); |
| 41 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 42 | { | ||
| 43 | Transmission::field_t &cur = map[x][y]; | ||
| 44 | if (cur & Transmission::block) | ||
| 45 | m_blocks.append(QPoint(x, y)); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | 39 | ||
| 49 | updateMap(map); | 40 | /* finish round every 3 sec |
| 50 | sendUpdate(map, true); | 41 | QTimer *timer2 = new QTimer(this); |
| 51 | Util::deleteMap(map); | 42 | connect(timer2, SIGNAL(timeout()), this, SLOT(onRoundFinished())); |
| 52 | map = NULL; | 43 | timer2->start(3000); |
| 44 | // */ | ||
| 45 | |||
| 46 | connect(this, SIGNAL(allPointsRemoved()), SLOT(onRoundFinished())); | ||
| 53 | 47 | ||
| 54 | QTimer *timer = new QTimer(this); | ||
| 55 | connect(timer, SIGNAL(timeout()), this, SLOT(tick())); | ||
| 56 | timer->start(Constants::tick); | ||
| 57 | return true; | 48 | return true; |
| 58 | } | 49 | } |
| 59 | 50 | ||
| @@ -108,7 +99,6 @@ invalid_direction: | |||
| 108 | 99 | ||
| 109 | // <t3h g4m3 10gic> | 100 | // <t3h g4m3 10gic> |
| 110 | // TODO: support actors eating each other | 101 | // TODO: support actors eating each other |
| 111 | // TODO: old item - REMOVE THAT? | ||
| 112 | GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; | 102 | GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; |
| 113 | 103 | ||
| 114 | /* check if there's an item at new location of actor */ | 104 | /* check if there's an item at new location of actor */ |
| @@ -500,6 +490,81 @@ void Server::keyPressUpdate() | |||
| 500 | } | 490 | } |
| 501 | } | 491 | } |
| 502 | 492 | ||
| 493 | void Server::onRoundFinished() | ||
| 494 | { | ||
| 495 | foreach(Actor *actor, m_actors) { | ||
| 496 | actor->finishRound(); | ||
| 497 | } | ||
| 498 | |||
| 499 | initRoundMap(); | ||
| 500 | ++m_curRound; | ||
| 501 | |||
| 502 | if(m_curRound >= m_rounds) { | ||
| 503 | // end of game | ||
| 504 | m_tickTimer->stop(); | ||
| 505 | } | ||
| 506 | } | ||
| 507 | |||
| 508 | void Server::initRoundMap(bool firstPacket) | ||
| 509 | { | ||
| 510 | disconnect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished())); | ||
| 511 | if (!firstPacket) { | ||
| 512 | // clear old map | ||
| 513 | for (unsigned int i=0; i<visualMap.size(); ++i) { | ||
| 514 | for (unsigned int j=0; j<visualMap[0].size(); ++j) { | ||
| 515 | GameEntity *e = visualMap[i][j]; | ||
| 516 | if (e != NULL) { | ||
| 517 | if (qgraphicsitem_cast<Actor *>(e) == NULL) { | ||
| 518 | removeItem(e); | ||
| 519 | delete e; | ||
| 520 | } | ||
| 521 | } | ||
| 522 | visualMap[i][j] = 0; | ||
| 523 | } | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | // create new map | ||
| 528 | Transmission::map_t map = Util::createDemoMap(); | ||
| 529 | // add content | ||
| 530 | Util::placeActors(map, m_maxplayers, Color::order); | ||
| 531 | /* | ||
| 532 | if (!firstPacket) { | ||
| 533 | for (unsigned int i=0; i<Constants::map_size.width; ++i) { | ||
| 534 | for (unsigned int j=0; j<Constants::map_size.height; ++j) { | ||
| 535 | if (map[i][j] & Transmission::pacman) { | ||
| 536 | Color::Color color = static_cast<Color::Color>(map[i][j] & Transmission::color_mask); | ||
| 537 | Actor *actor = m_actors.value(color, NULL); | ||
| 538 | qDebug() << "setting actor to " << i << j << mapPositionToCoord(i, j); | ||
| 539 | actor->setPos(mapPositionToCoord(i, j)); | ||
| 540 | } | ||
| 541 | } | ||
| 542 | } | ||
| 543 | } | ||
| 544 | */ | ||
| 545 | |||
| 546 | Util::fillPoints(map); | ||
| 547 | |||
| 548 | /* save positions of blocks for later usage */ | ||
| 549 | m_blocks.clear(); | ||
| 550 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 551 | { | ||
| 552 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 553 | { | ||
| 554 | Transmission::field_t &cur = map[x][y]; | ||
| 555 | if (cur & Transmission::block) | ||
| 556 | m_blocks.append(QPoint(x, y)); | ||
| 557 | } | ||
| 558 | } | ||
| 559 | |||
| 560 | updateMap(map); | ||
| 561 | sendUpdate(map, firstPacket); | ||
| 562 | Util::deleteMap(map); | ||
| 563 | map = NULL; | ||
| 564 | |||
| 565 | connect(this, SIGNAL(allPointsRemoved()), this, SLOT(onRoundFinished())); | ||
| 566 | } | ||
| 567 | |||
| 503 | bool Server::parseCommandline() | 568 | bool Server::parseCommandline() |
| 504 | { | 569 | { |
| 505 | AnyOption opt; | 570 | AnyOption opt; |
| @@ -535,6 +600,10 @@ bool Server::parseCommandline() | |||
| 535 | out << " --nocolorblocks" << endl | 600 | out << " --nocolorblocks" << endl |
| 536 | << " Disable random colorized blocks" << endl | 601 | << " Disable random colorized blocks" << endl |
| 537 | << endl; | 602 | << endl; |
| 603 | opt.setOption("rounds", 'r'); | ||
| 604 | out << " -r, --rounds [1..n]" << endl | ||
| 605 | << " Number of rounds to play" << endl | ||
| 606 | << endl; | ||
| 538 | opt.setFlag("nocolorblocks"); | 607 | opt.setFlag("nocolorblocks"); |
| 539 | out << " -h, --help" << endl | 608 | out << " -h, --help" << endl |
| 540 | << " Prints this help message" << endl; | 609 | << " Prints this help message" << endl; |
| @@ -603,6 +672,18 @@ bool Server::parseCommandline() | |||
| 603 | m_numbots = numbots; | 672 | m_numbots = numbots; |
| 604 | } | 673 | } |
| 605 | 674 | ||
| 675 | if (opt.getValue("rounds") != NULL) | ||
| 676 | { | ||
| 677 | bool ok; | ||
| 678 | unsigned int rounds = QString(opt.getValue("rounds")).toUInt(&ok); | ||
| 679 | if (!ok || rounds == 0) | ||
| 680 | { | ||
| 681 | qCritical() << "Invalid number of rounds: " << opt.getValue("rounds") << endl; | ||
| 682 | return false; | ||
| 683 | } | ||
| 684 | m_rounds = rounds; | ||
| 685 | } | ||
| 686 | |||
| 606 | this->setProperty("coloredblocks", !opt.getFlag("nocolorblocks")); | 687 | this->setProperty("coloredblocks", !opt.getFlag("nocolorblocks")); |
| 607 | 688 | ||
| 608 | return true; | 689 | return true; |
