From 65195fdab6262d31056c74f922376de3b009943c Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 17 Apr 2011 19:19:56 +0200 Subject: a bigger commit again: - fix pacman movement. now more like real pacman (again). e.g. if you press a direction-key again: the pacman will move in that direction as soon as possible and no repeated keypress is needed - add random colorized blocks (without dieing yet) - add cmdline-option: --nocolorblocks to disable that --- pacman-c++/server.cpp | 122 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 105 insertions(+), 17 deletions(-) (limited to 'pacman-c++/server.cpp') diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index 01c74c4..628faa2 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -28,6 +28,18 @@ bool Server::run() Transmission::map_t map = Util::createDemoMap(); Util::placeActors(map, m_maxplayers, Color::order); Util::fillPoints(map); + + /* save positions of blocks for later usage */ + for (unsigned int x = 0; x < Constants::map_size.width; ++x) + { + for (unsigned int y = 0; y < Constants::map_size.height; ++y) + { + Transmission::field_t &cur = map[x][y]; + if (cur & Transmission::block) + m_blocks.append(QPoint(x, y)); + } + } + updateMap(map); sendUpdate(map); Util::deleteMap(map); @@ -41,7 +53,7 @@ bool Server::run() void Server::tick() { - qDebug() << "[Tick] Doing server update"; + //qDebug() << "[Tick] Doing server update"; Transmission::map_t map = calculateUpdates(); updateMap(map); @@ -50,6 +62,10 @@ void Server::tick() if (!pos.isNull()) updateMap(map, pos.x(), pos.y()); + /* add/remove random colorized block */ + if (this->property("coloredblocks").toBool()) + colorizeBlocks(map); + sendUpdate(map); Util::deleteMap(map); } @@ -66,17 +82,18 @@ Transmission::map_t Server::calculateUpdates() while (i.hasNext()) { i.next(); + Actor *actor = m_actors.value(i.key()); + QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); + Actor::Movement direction = i.value(); int turn = 0; invalid_direction: ++turn; - Actor *actor = m_actors.value(i.key()); - QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); qDebug() << "[Calc] Actor wants to move: color=" << i.key() - << "pos=" << mapPosition << "direction=" << i.value(); + << "pos=" << mapPosition << "direction=" << direction; QPoint newMapPosition = mapPosition; - switch (i.value()) + switch (direction) { case Actor::Up: newMapPosition += QPoint(0, -1); @@ -136,29 +153,29 @@ invalid_direction: /* movement didn't work - e.g. was blocked */ if (mapPosition == newMapPosition) { - /* */ - if (turn == 1 && i.value() != actor->direction()) + /* check turn */ + if (turn == 1 && direction != actor->direction()) { /* set direction back to last known direction and try again */ qDebug() << "[Calc] Movement was blocked. Trying last known actor direction"; - m_actorMovements[i.key()] = actor->direction(); + direction = actor->direction(); goto invalid_direction; } else { /* second turn didn't work too -> stop movement */ - m_actorMovements[i.key()] = Actor::None; + direction = Actor::None; qDebug() << "[Calc] No good direction known. Movement stopped"; } } map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | - i.key() | Util::actorMovementToTransmission(i.value()); + i.key() | Util::actorMovementToTransmission(direction); /* DEBUG: uncomments to disable auto-movement */ - //m_actorMovements[i.key()] = Actor::None; + //direction = Actor::None; - if (i.value() == Actor::None) + if (direction == Actor::None) { /* set actor to non-moving */ m_actorMovements[i.key()] = Actor::None; @@ -170,7 +187,8 @@ invalid_direction: QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t type) { - int chance = Constants::Random::bouns_point_chance; + int chance = Constants::Game::bouns_point_chance + - Constants::Game::bouns_point_chance_playerfactor * (m_maxplayers - 1); int rand = (int) (chance * (qrand() / (RAND_MAX + 1.0))); if (rand != 0) return QPoint(); @@ -190,10 +208,10 @@ QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t typ * performance would be better if actors would be listed in visualMap too * but this isn't possible that easily. see comment in updateMap(map) */ - foreach (Actor *tmp, m_actors) + foreach (Actor *actor, m_actors) { - QPoint oldpos = CoordToMapPosition(tmp->pos().toPoint()); - validpos.removeAll(oldpos); + QPoint actorpos = CoordToMapPosition(actor->pos().toPoint()); + validpos.removeAll(actorpos); } if (validpos.empty()) @@ -205,6 +223,60 @@ QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t typ return pos; } +void Server::colorizeBlocks(Transmission::map_t map) +{ + /* decrement tickcount of colored blocks */ + if (!m_coloredBlocks.empty()) + { + QMutableMapIterator i(m_coloredBlocks); + while(i.hasNext()) + { + i.next(); + unsigned val = i.value(); + if (val > 0) + i.setValue(--val); + else + { + /* check for actor collision */ + bool skip = false; + foreach(Actor *actor, m_actors) + { + if (CoordToMapPosition(actor->pos().toPoint()) == i.key()) + skip = true; + if (skip) + break; + } + if (skip) + continue; + + QPoint block = i.key(); + map[block.x()][block.y()] |= Transmission::block | Color::none; + updateMap(map, block.x(), block.y()); + m_blocks.append(block); + i.remove(); + } + } + } + + /* colorize a random block */ + int rand = (int) (Constants::Game::colorize_block_chance * (qrand() / (RAND_MAX + 1.0))); + if (rand == 0) + { + rand = (int) (m_blocks.size() * (qrand() / (RAND_MAX + 1.0))); + QPoint block = m_blocks.at(rand); + m_blocks.removeAt(rand); + + unsigned int min = Constants::Game::colorize_block_tickcount_min; + unsigned int max = Constants::Game::colorize_block_tickcount_max; + int tickcount = min + (int) ((max - min + 1) * (qrand() / (RAND_MAX + 1.0))); + m_coloredBlocks.insert(block, tickcount); + + unsigned int color = (int) ((m_maxplayers - 1) * (qrand() / (RAND_MAX + 1.0))); + map[block.x()][block.y()] |= Transmission::block | Color::order[color]; + updateMap(map, block.x(), block.y()); + } +} + bool Server::waitForClientConnections() { // server must stay alive as long as sockets (qt parent mem mechanism) @@ -324,6 +396,10 @@ bool Server::parseCommandline() << " Default: " << m_numbots << endl << endl; opt.setOption("bots"); + out << " --nocolorblocks" << endl + << " Disable random colorized blocks" << endl + << endl; + opt.setFlag("nocolorblocks"); out << " -h, --help" << endl << " Prints this help message" << endl; opt.setFlag("help", 'h'); @@ -386,12 +462,24 @@ bool Server::parseCommandline() m_numbots = numbots; } + this->setProperty("coloredblocks", !opt.getFlag("nocolorblocks")); + return true; } +bool operator<(const QPoint& lhs, const QPoint& rhs) +{ + if (lhs.x() < rhs.x()) + return true; + else if (lhs.x() == rhs.x()) + return lhs.y() < rhs.y(); + else + return false; +} + bool Constants::server = true; -int main(int argc, char ** argv) +int main(int argc, char **argv) { /* Verify that the version of the library that we linked against is * compatible with the version of the headers we compiled against. -- cgit v1.2.3