diff options
Diffstat (limited to 'pacman-c++')
| -rw-r--r-- | pacman-c++/block.cpp | 10 | ||||
| -rw-r--r-- | pacman-c++/block.h | 2 | ||||
| -rw-r--r-- | pacman-c++/client.cpp | 2 | ||||
| -rw-r--r-- | pacman-c++/constants.h | 15 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 9 | ||||
| -rw-r--r-- | pacman-c++/sceneholder.cpp | 120 | ||||
| -rw-r--r-- | pacman-c++/server.cpp | 122 | ||||
| -rw-r--r-- | pacman-c++/server.h | 6 |
8 files changed, 200 insertions, 86 deletions
diff --git a/pacman-c++/block.cpp b/pacman-c++/block.cpp index 16f62c4..eb51d89 100644 --- a/pacman-c++/block.cpp +++ b/pacman-c++/block.cpp | |||
| @@ -7,7 +7,7 @@ | |||
| 7 | QMap<Color::Color, QPixmap> Block::m_pixmaps; | 7 | QMap<Color::Color, QPixmap> Block::m_pixmaps; |
| 8 | 8 | ||
| 9 | Block::Block(Color::Color color, unsigned int neighbours, QGraphicsItem *parent) | 9 | Block::Block(Color::Color color, unsigned int neighbours, QGraphicsItem *parent) |
| 10 | : GameEntity(color, parent) | 10 | : GameEntity(color, parent), m_neighbours(neighbours) |
| 11 | { | 11 | { |
| 12 | /* empty object for servers */ | 12 | /* empty object for servers */ |
| 13 | if (Constants::server) | 13 | if (Constants::server) |
| @@ -20,11 +20,17 @@ Block::Block(Color::Color color, unsigned int neighbours, QGraphicsItem *parent) | |||
| 20 | m_pixmaps[color] = QPixmap(pixmapName); | 20 | m_pixmaps[color] = QPixmap(pixmapName); |
| 21 | } | 21 | } |
| 22 | setPixmap(m_pixmaps.find(color).value()); | 22 | setPixmap(m_pixmaps.find(color).value()); |
| 23 | setNeighbours(neighbours); | 23 | setNeighbours(m_neighbours); |
| 24 | } | ||
| 25 | |||
| 26 | unsigned int Block::neighbours() | ||
| 27 | { | ||
| 28 | return m_neighbours; | ||
| 24 | } | 29 | } |
| 25 | 30 | ||
| 26 | void Block::setNeighbours(unsigned int neighbours) | 31 | void Block::setNeighbours(unsigned int neighbours) |
| 27 | { | 32 | { |
| 33 | m_neighbours = neighbours; | ||
| 28 | setSprite(neighbours * Constants::sprite_offset, 0, Constants::field_size.width, Constants::field_size.height); | 34 | setSprite(neighbours * Constants::sprite_offset, 0, Constants::field_size.width, Constants::field_size.height); |
| 29 | } | 35 | } |
| 30 | 36 | ||
diff --git a/pacman-c++/block.h b/pacman-c++/block.h index 2f388c8..6d97a9a 100644 --- a/pacman-c++/block.h +++ b/pacman-c++/block.h | |||
| @@ -23,6 +23,7 @@ public: | |||
| 23 | virtual ~Block() | 23 | virtual ~Block() |
| 24 | {}; | 24 | {}; |
| 25 | 25 | ||
| 26 | unsigned int neighbours(); | ||
| 26 | void setNeighbours(unsigned int neighbours); | 27 | void setNeighbours(unsigned int neighbours); |
| 27 | virtual bool checkEnter(Actor *actor); | 28 | virtual bool checkEnter(Actor *actor); |
| 28 | virtual bool enter(Actor *actor); | 29 | virtual bool enter(Actor *actor); |
| @@ -30,6 +31,7 @@ public: | |||
| 30 | private: | 31 | private: |
| 31 | // map for saving QPixmaps for reuse | 32 | // map for saving QPixmaps for reuse |
| 32 | static QMap<Color::Color, QPixmap> m_pixmaps; | 33 | static QMap<Color::Color, QPixmap> m_pixmaps; |
| 34 | unsigned int m_neighbours; | ||
| 33 | }; | 35 | }; |
| 34 | 36 | ||
| 35 | #endif // BLOCK_H | 37 | #endif // BLOCK_H |
diff --git a/pacman-c++/client.cpp b/pacman-c++/client.cpp index 7c428c5..581778e 100644 --- a/pacman-c++/client.cpp +++ b/pacman-c++/client.cpp | |||
| @@ -152,7 +152,7 @@ void Client::showAbout() | |||
| 152 | 152 | ||
| 153 | bool Constants::server = false; | 153 | bool Constants::server = false; |
| 154 | 154 | ||
| 155 | int main(int argc, char ** argv) | 155 | int main(int argc, char **argv) |
| 156 | { | 156 | { |
| 157 | /* Verify that the version of the library that we linked against is | 157 | /* Verify that the version of the library that we linked against is |
| 158 | * compatible with the version of the headers we compiled against. | 158 | * compatible with the version of the headers we compiled against. |
diff --git a/pacman-c++/constants.h b/pacman-c++/constants.h index 06b6561..dc72611 100644 --- a/pacman-c++/constants.h +++ b/pacman-c++/constants.h | |||
| @@ -31,12 +31,15 @@ namespace Constants { | |||
| 31 | const unsigned int player_minimum_distance = 5; | 31 | const unsigned int player_minimum_distance = 5; |
| 32 | /* if the distance above isn't possible, decrease the distance by this value */ | 32 | /* if the distance above isn't possible, decrease the distance by this value */ |
| 33 | const unsigned int player_distance_decr = 2; | 33 | const unsigned int player_distance_decr = 2; |
| 34 | } | 34 | /* there's a chance of 1:30 that a bonus point will be added (with one actor) */ |
| 35 | 35 | const unsigned int bouns_point_chance = 30; | |
| 36 | namespace Random | 36 | /* every additional player will raise the chance of a bonus point by that factor */ |
| 37 | { | 37 | const unsigned int bouns_point_chance_playerfactor = 3; |
| 38 | /* there's a chance of 1:20 that a bonus point will be added */ | 38 | /* there's a chance of 1:5 that a block will be colorized */ |
| 39 | const unsigned int bouns_point_chance = 20; | 39 | const unsigned int colorize_block_chance = 5; |
| 40 | /* how long colorized blocks will stay colorized */ | ||
| 41 | const unsigned int colorize_block_tickcount_min = 50; | ||
| 42 | const unsigned int colorize_block_tickcount_max = 100; | ||
| 40 | } | 43 | } |
| 41 | } | 44 | } |
| 42 | 45 | ||
diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 705a3ca..c674c55 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp | |||
| @@ -33,11 +33,6 @@ MainWidget::MainWidget(QWidget *parent) | |||
| 33 | tick(); | 33 | tick(); |
| 34 | 34 | ||
| 35 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); | 35 | connect(m_socket, SIGNAL(readyRead()), this, SLOT(tick())); |
| 36 | |||
| 37 | QTimer *sendTimer = new QTimer(this); | ||
| 38 | connect(sendTimer, SIGNAL(timeout()), this, SLOT(sendKeyUpdate())); | ||
| 39 | sendTimer->start(Constants::tick); | ||
| 40 | |||
| 41 | qDebug() << "[Connect] mycolor=" << m_scene->color(); | 36 | qDebug() << "[Connect] mycolor=" << m_scene->color(); |
| 42 | 37 | ||
| 43 | //TODO: play intro as soon as there are enough players | 38 | //TODO: play intro as soon as there are enough players |
| @@ -172,6 +167,8 @@ void MainWidget::keyPressEvent(QKeyEvent* event) | |||
| 172 | { | 167 | { |
| 173 | if (!m_running) | 168 | if (!m_running) |
| 174 | return; | 169 | return; |
| 170 | if (event->isAutoRepeat()) | ||
| 171 | return; | ||
| 175 | 172 | ||
| 176 | QWidget::keyPressEvent(event); | 173 | QWidget::keyPressEvent(event); |
| 177 | Transmission::field_t newKey = translateKey(event->key()); | 174 | Transmission::field_t newKey = translateKey(event->key()); |
| @@ -199,6 +196,8 @@ void MainWidget::keyReleaseEvent(QKeyEvent* event) | |||
| 199 | { | 196 | { |
| 200 | if (!m_running) | 197 | if (!m_running) |
| 201 | return; | 198 | return; |
| 199 | if (event->isAutoRepeat()) | ||
| 200 | return; | ||
| 202 | 201 | ||
| 203 | QWidget::keyReleaseEvent(event); | 202 | QWidget::keyReleaseEvent(event); |
| 204 | m_currentKey = Transmission::none; | 203 | m_currentKey = Transmission::none; |
diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index 11e0772..38b49e5 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp | |||
| @@ -86,68 +86,77 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x | |||
| 86 | { | 86 | { |
| 87 | // no update | 87 | // no update |
| 88 | } | 88 | } |
| 89 | else if (cur & Transmission::block) | 89 | else |
| 90 | { | ||
| 91 | unsigned int neighbours = Block::None; | ||
| 92 | // check left side | ||
| 93 | if (x > 0 && map[x - 1][y] & Transmission::block) | ||
| 94 | neighbours |= Block::Left; | ||
| 95 | // check right side | ||
| 96 | if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block) | ||
| 97 | neighbours |= Block::Right; | ||
| 98 | // check upside | ||
| 99 | if (y > 0 && map[x][y - 1] & Transmission::block) | ||
| 100 | neighbours |= Block::Up; | ||
| 101 | // check down side | ||
| 102 | if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block) | ||
| 103 | neighbours |= Block::Down; | ||
| 104 | item = new Block(color, neighbours); | ||
| 105 | } | ||
| 106 | else if (cur & Transmission::bonuspoint) | ||
| 107 | item = new BonusPoint(); | ||
| 108 | else if (cur & Transmission::point) | ||
| 109 | { | ||
| 110 | item = new Point(); | ||
| 111 | connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints())); | ||
| 112 | ++m_pointsLeft; | ||
| 113 | } | ||
| 114 | else if (cur & Transmission::pacman) | ||
| 115 | { | 90 | { |
| 116 | Actor *actor = m_actors.value(color, NULL); | 91 | if (cur & Transmission::block) |
| 117 | if (actor == NULL) | 92 | { |
| 93 | unsigned int neighbours = Block::None; | ||
| 94 | // check for old block first | ||
| 95 | if (visualMap[x][y] != NULL) | ||
| 96 | { | ||
| 97 | Block *oldItem = dynamic_cast<Block *>(visualMap[x][y]); | ||
| 98 | if (oldItem != NULL) | ||
| 99 | neighbours = oldItem->neighbours(); | ||
| 100 | } | ||
| 101 | // check left side | ||
| 102 | if (x > 0 && map[x - 1][y] & Transmission::block) | ||
| 103 | neighbours |= Block::Left; | ||
| 104 | // check right side | ||
| 105 | if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block) | ||
| 106 | neighbours |= Block::Right; | ||
| 107 | // check upside | ||
| 108 | if (y > 0 && map[x][y - 1] & Transmission::block) | ||
| 109 | neighbours |= Block::Up; | ||
| 110 | // check down side | ||
| 111 | if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block) | ||
| 112 | neighbours |= Block::Down; | ||
| 113 | item = new Block(color, neighbours); | ||
| 114 | } | ||
| 115 | |||
| 116 | if (cur & Transmission::bonuspoint) | ||
| 117 | item = new BonusPoint(); | ||
| 118 | |||
| 119 | if (cur & Transmission::point) | ||
| 118 | { | 120 | { |
| 119 | actor = new Actor(color, (color == m_color)); | 121 | item = new Point(); |
| 120 | m_actors[color] = actor; | 122 | connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints())); |
| 121 | addItem(actor); | 123 | ++m_pointsLeft; |
| 122 | actor->setPos(mapPositionToCoord(x, y)); | ||
| 123 | } | 124 | } |
| 124 | else | 125 | |
| 126 | if (cur & Transmission::pacman) | ||
| 125 | { | 127 | { |
| 126 | Actor::Movement direction = Util::transmissionMovementToActor( | 128 | Actor *actor = m_actors.value(color, NULL); |
| 127 | cur & Transmission::direction_mask); | 129 | if (actor == NULL) |
| 128 | /* WARNING: do NOT add actor to visualMap as visualMap-items may | 130 | { |
| 131 | actor = new Actor(color, (color == m_color)); | ||
| 132 | m_actors[color] = actor; | ||
| 133 | addItem(actor); | ||
| 134 | actor->setPos(mapPositionToCoord(x, y)); | ||
| 135 | } | ||
| 136 | else | ||
| 137 | { | ||
| 138 | Actor::Movement direction = Util::transmissionMovementToActor( | ||
| 139 | cur & Transmission::direction_mask); | ||
| 140 | /* WARNING: do NOT add actor to visualMap as visualMap-items may | ||
| 129 | * get deleted during update and actors are referenced in_mactors too | 141 | * get deleted during update and actors are referenced in_mactors too |
| 130 | * if you REALLY need that you need to changed updateMap so that all actors | 142 | * if you REALLY need that you need to changed updateMap so that all actors |
| 131 | * will be moved before any new items get allocated (totally untested) | 143 | * will be moved before any new items get allocated (totally untested) |
| 132 | */ | 144 | */ |
| 133 | actor->move(direction); | 145 | actor->move(direction); |
| 134 | /* that's kind a hack but working right now | 146 | /* that's kind a hack but working right now |
| 135 | * I think that will fall on our's hat sooner or later | 147 | * I think that will fall on our's hat sooner or later |
| 136 | */ | 148 | */ |
| 137 | if (!(cur & Transmission::empty)) | 149 | if (!(cur & Transmission::empty)) |
| 138 | actor->stopEating(); | 150 | actor->stopEating(); |
| 139 | qDebug() << "[SceneUpdate] actor moves: color=" << color | 151 | qDebug() << "[SceneUpdate] actor moves: color=" << color |
| 140 | << "direction=" << direction << "newpos=" << QPoint(x, y); | 152 | << "direction=" << direction << "newpos=" << QPoint(x, y); |
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | if (cur & Transmission::empty) | ||
| 157 | { | ||
| 158 | /* already handled */ | ||
| 141 | } | 159 | } |
| 142 | } | ||
| 143 | else if (cur & Transmission::empty) | ||
| 144 | { | ||
| 145 | /* already handled */ | ||
| 146 | } | ||
| 147 | else | ||
| 148 | { | ||
| 149 | qWarning() << "Unknown data. value=" << cur; | ||
| 150 | Q_ASSERT(false); | ||
| 151 | } | 160 | } |
| 152 | 161 | ||
| 153 | /* add new created item to scene | 162 | /* add new created item to scene |
| @@ -155,15 +164,16 @@ void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x | |||
| 155 | */ | 164 | */ |
| 156 | if (item != NULL) | 165 | if (item != NULL) |
| 157 | { | 166 | { |
| 158 | addItem(item); | ||
| 159 | item->setPos(mapPositionToCoord(x, y)); | ||
| 160 | GameEntity *oldItem = visualMap[x][y]; | 167 | GameEntity *oldItem = visualMap[x][y]; |
| 161 | visualMap[x][y] = item; | ||
| 162 | if (oldItem != NULL) | 168 | if (oldItem != NULL) |
| 163 | { | 169 | { |
| 164 | removeItem(item); | 170 | removeItem(oldItem); |
| 165 | delete oldItem; | 171 | delete oldItem; |
| 166 | } | 172 | } |
| 173 | |||
| 174 | addItem(item); | ||
| 175 | item->setPos(mapPositionToCoord(x, y)); | ||
| 176 | visualMap[x][y] = item; | ||
| 167 | } | 177 | } |
| 168 | } | 178 | } |
| 169 | 179 | ||
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() | |||
| 28 | Transmission::map_t map = Util::createDemoMap(); | 28 | Transmission::map_t map = Util::createDemoMap(); |
| 29 | Util::placeActors(map, m_maxplayers, Color::order); | 29 | Util::placeActors(map, m_maxplayers, Color::order); |
| 30 | Util::fillPoints(map); | 30 | Util::fillPoints(map); |
| 31 | |||
| 32 | /* save positions of blocks for later usage */ | ||
| 33 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 34 | { | ||
| 35 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 36 | { | ||
| 37 | Transmission::field_t &cur = map[x][y]; | ||
| 38 | if (cur & Transmission::block) | ||
| 39 | m_blocks.append(QPoint(x, y)); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 31 | updateMap(map); | 43 | updateMap(map); |
| 32 | sendUpdate(map); | 44 | sendUpdate(map); |
| 33 | Util::deleteMap(map); | 45 | Util::deleteMap(map); |
| @@ -41,7 +53,7 @@ bool Server::run() | |||
| 41 | 53 | ||
| 42 | void Server::tick() | 54 | void Server::tick() |
| 43 | { | 55 | { |
| 44 | qDebug() << "[Tick] Doing server update"; | 56 | //qDebug() << "[Tick] Doing server update"; |
| 45 | Transmission::map_t map = calculateUpdates(); | 57 | Transmission::map_t map = calculateUpdates(); |
| 46 | updateMap(map); | 58 | updateMap(map); |
| 47 | 59 | ||
| @@ -50,6 +62,10 @@ void Server::tick() | |||
| 50 | if (!pos.isNull()) | 62 | if (!pos.isNull()) |
| 51 | updateMap(map, pos.x(), pos.y()); | 63 | updateMap(map, pos.x(), pos.y()); |
| 52 | 64 | ||
| 65 | /* add/remove random colorized block */ | ||
| 66 | if (this->property("coloredblocks").toBool()) | ||
| 67 | colorizeBlocks(map); | ||
| 68 | |||
| 53 | sendUpdate(map); | 69 | sendUpdate(map); |
| 54 | Util::deleteMap(map); | 70 | Util::deleteMap(map); |
| 55 | } | 71 | } |
| @@ -66,17 +82,18 @@ Transmission::map_t Server::calculateUpdates() | |||
| 66 | while (i.hasNext()) | 82 | while (i.hasNext()) |
| 67 | { | 83 | { |
| 68 | i.next(); | 84 | i.next(); |
| 85 | Actor *actor = m_actors.value(i.key()); | ||
| 86 | QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); | ||
| 87 | Actor::Movement direction = i.value(); | ||
| 69 | int turn = 0; | 88 | int turn = 0; |
| 70 | 89 | ||
| 71 | invalid_direction: | 90 | invalid_direction: |
| 72 | ++turn; | 91 | ++turn; |
| 73 | Actor *actor = m_actors.value(i.key()); | ||
| 74 | QPoint mapPosition = CoordToMapPosition(actor->pos().toPoint()); | ||
| 75 | qDebug() << "[Calc] Actor wants to move: color=" << i.key() | 92 | qDebug() << "[Calc] Actor wants to move: color=" << i.key() |
| 76 | << "pos=" << mapPosition << "direction=" << i.value(); | 93 | << "pos=" << mapPosition << "direction=" << direction; |
| 77 | 94 | ||
| 78 | QPoint newMapPosition = mapPosition; | 95 | QPoint newMapPosition = mapPosition; |
| 79 | switch (i.value()) | 96 | switch (direction) |
| 80 | { | 97 | { |
| 81 | case Actor::Up: | 98 | case Actor::Up: |
| 82 | newMapPosition += QPoint(0, -1); | 99 | newMapPosition += QPoint(0, -1); |
| @@ -136,29 +153,29 @@ invalid_direction: | |||
| 136 | /* movement didn't work - e.g. was blocked */ | 153 | /* movement didn't work - e.g. was blocked */ |
| 137 | if (mapPosition == newMapPosition) | 154 | if (mapPosition == newMapPosition) |
| 138 | { | 155 | { |
| 139 | /* */ | 156 | /* check turn */ |
| 140 | if (turn == 1 && i.value() != actor->direction()) | 157 | if (turn == 1 && direction != actor->direction()) |
| 141 | { | 158 | { |
| 142 | /* set direction back to last known direction and try again */ | 159 | /* set direction back to last known direction and try again */ |
| 143 | qDebug() << "[Calc] Movement was blocked. Trying last known actor direction"; | 160 | qDebug() << "[Calc] Movement was blocked. Trying last known actor direction"; |
| 144 | m_actorMovements[i.key()] = actor->direction(); | 161 | direction = actor->direction(); |
| 145 | goto invalid_direction; | 162 | goto invalid_direction; |
| 146 | } | 163 | } |
| 147 | else | 164 | else |
| 148 | { | 165 | { |
| 149 | /* second turn didn't work too -> stop movement */ | 166 | /* second turn didn't work too -> stop movement */ |
| 150 | m_actorMovements[i.key()] = Actor::None; | 167 | direction = Actor::None; |
| 151 | qDebug() << "[Calc] No good direction known. Movement stopped"; | 168 | qDebug() << "[Calc] No good direction known. Movement stopped"; |
| 152 | } | 169 | } |
| 153 | } | 170 | } |
| 154 | 171 | ||
| 155 | map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | | 172 | map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | |
| 156 | i.key() | Util::actorMovementToTransmission(i.value()); | 173 | i.key() | Util::actorMovementToTransmission(direction); |
| 157 | 174 | ||
| 158 | /* DEBUG: uncomments to disable auto-movement */ | 175 | /* DEBUG: uncomments to disable auto-movement */ |
| 159 | //m_actorMovements[i.key()] = Actor::None; | 176 | //direction = Actor::None; |
| 160 | 177 | ||
| 161 | if (i.value() == Actor::None) | 178 | if (direction == Actor::None) |
| 162 | { | 179 | { |
| 163 | /* set actor to non-moving */ | 180 | /* set actor to non-moving */ |
| 164 | m_actorMovements[i.key()] = Actor::None; | 181 | m_actorMovements[i.key()] = Actor::None; |
| @@ -170,7 +187,8 @@ invalid_direction: | |||
| 170 | 187 | ||
| 171 | QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t type) | 188 | QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t type) |
| 172 | { | 189 | { |
| 173 | int chance = Constants::Random::bouns_point_chance; | 190 | int chance = Constants::Game::bouns_point_chance |
| 191 | - Constants::Game::bouns_point_chance_playerfactor * (m_maxplayers - 1); | ||
| 174 | int rand = (int) (chance * (qrand() / (RAND_MAX + 1.0))); | 192 | int rand = (int) (chance * (qrand() / (RAND_MAX + 1.0))); |
| 175 | if (rand != 0) | 193 | if (rand != 0) |
| 176 | return QPoint(); | 194 | return QPoint(); |
| @@ -190,10 +208,10 @@ QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t typ | |||
| 190 | * performance would be better if actors would be listed in visualMap too | 208 | * performance would be better if actors would be listed in visualMap too |
| 191 | * but this isn't possible that easily. see comment in updateMap(map) | 209 | * but this isn't possible that easily. see comment in updateMap(map) |
| 192 | */ | 210 | */ |
| 193 | foreach (Actor *tmp, m_actors) | 211 | foreach (Actor *actor, m_actors) |
| 194 | { | 212 | { |
| 195 | QPoint oldpos = CoordToMapPosition(tmp->pos().toPoint()); | 213 | QPoint actorpos = CoordToMapPosition(actor->pos().toPoint()); |
| 196 | validpos.removeAll(oldpos); | 214 | validpos.removeAll(actorpos); |
| 197 | } | 215 | } |
| 198 | 216 | ||
| 199 | if (validpos.empty()) | 217 | if (validpos.empty()) |
| @@ -205,6 +223,60 @@ QPoint Server::addRandomPoint(Transmission::map_t map, Transmission::field_t typ | |||
| 205 | return pos; | 223 | return pos; |
| 206 | } | 224 | } |
| 207 | 225 | ||
| 226 | void Server::colorizeBlocks(Transmission::map_t map) | ||
| 227 | { | ||
| 228 | /* decrement tickcount of colored blocks */ | ||
| 229 | if (!m_coloredBlocks.empty()) | ||
| 230 | { | ||
| 231 | QMutableMapIterator<QPoint, unsigned int> i(m_coloredBlocks); | ||
| 232 | while(i.hasNext()) | ||
| 233 | { | ||
| 234 | i.next(); | ||
| 235 | unsigned val = i.value(); | ||
| 236 | if (val > 0) | ||
| 237 | i.setValue(--val); | ||
| 238 | else | ||
| 239 | { | ||
| 240 | /* check for actor collision */ | ||
| 241 | bool skip = false; | ||
| 242 | foreach(Actor *actor, m_actors) | ||
| 243 | { | ||
| 244 | if (CoordToMapPosition(actor->pos().toPoint()) == i.key()) | ||
| 245 | skip = true; | ||
| 246 | if (skip) | ||
| 247 | break; | ||
| 248 | } | ||
| 249 | if (skip) | ||
| 250 | continue; | ||
| 251 | |||
| 252 | QPoint block = i.key(); | ||
| 253 | map[block.x()][block.y()] |= Transmission::block | Color::none; | ||
| 254 | updateMap(map, block.x(), block.y()); | ||
| 255 | m_blocks.append(block); | ||
| 256 | i.remove(); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | /* colorize a random block */ | ||
| 262 | int rand = (int) (Constants::Game::colorize_block_chance * (qrand() / (RAND_MAX + 1.0))); | ||
| 263 | if (rand == 0) | ||
| 264 | { | ||
| 265 | rand = (int) (m_blocks.size() * (qrand() / (RAND_MAX + 1.0))); | ||
| 266 | QPoint block = m_blocks.at(rand); | ||
| 267 | m_blocks.removeAt(rand); | ||
| 268 | |||
| 269 | unsigned int min = Constants::Game::colorize_block_tickcount_min; | ||
| 270 | unsigned int max = Constants::Game::colorize_block_tickcount_max; | ||
| 271 | int tickcount = min + (int) ((max - min + 1) * (qrand() / (RAND_MAX + 1.0))); | ||
| 272 | m_coloredBlocks.insert(block, tickcount); | ||
| 273 | |||
| 274 | unsigned int color = (int) ((m_maxplayers - 1) * (qrand() / (RAND_MAX + 1.0))); | ||
| 275 | map[block.x()][block.y()] |= Transmission::block | Color::order[color]; | ||
| 276 | updateMap(map, block.x(), block.y()); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 208 | bool Server::waitForClientConnections() | 280 | bool Server::waitForClientConnections() |
| 209 | { | 281 | { |
| 210 | // server must stay alive as long as sockets (qt parent mem mechanism) | 282 | // server must stay alive as long as sockets (qt parent mem mechanism) |
| @@ -324,6 +396,10 @@ bool Server::parseCommandline() | |||
| 324 | << " Default: " << m_numbots << endl | 396 | << " Default: " << m_numbots << endl |
| 325 | << endl; | 397 | << endl; |
| 326 | opt.setOption("bots"); | 398 | opt.setOption("bots"); |
| 399 | out << " --nocolorblocks" << endl | ||
| 400 | << " Disable random colorized blocks" << endl | ||
| 401 | << endl; | ||
| 402 | opt.setFlag("nocolorblocks"); | ||
| 327 | out << " -h, --help" << endl | 403 | out << " -h, --help" << endl |
| 328 | << " Prints this help message" << endl; | 404 | << " Prints this help message" << endl; |
| 329 | opt.setFlag("help", 'h'); | 405 | opt.setFlag("help", 'h'); |
| @@ -386,12 +462,24 @@ bool Server::parseCommandline() | |||
| 386 | m_numbots = numbots; | 462 | m_numbots = numbots; |
| 387 | } | 463 | } |
| 388 | 464 | ||
| 465 | this->setProperty("coloredblocks", !opt.getFlag("nocolorblocks")); | ||
| 466 | |||
| 389 | return true; | 467 | return true; |
| 390 | } | 468 | } |
| 391 | 469 | ||
| 470 | bool operator<(const QPoint& lhs, const QPoint& rhs) | ||
| 471 | { | ||
| 472 | if (lhs.x() < rhs.x()) | ||
| 473 | return true; | ||
| 474 | else if (lhs.x() == rhs.x()) | ||
| 475 | return lhs.y() < rhs.y(); | ||
| 476 | else | ||
| 477 | return false; | ||
| 478 | } | ||
| 479 | |||
| 392 | bool Constants::server = true; | 480 | bool Constants::server = true; |
| 393 | 481 | ||
| 394 | int main(int argc, char ** argv) | 482 | int main(int argc, char **argv) |
| 395 | { | 483 | { |
| 396 | /* Verify that the version of the library that we linked against is | 484 | /* Verify that the version of the library that we linked against is |
| 397 | * compatible with the version of the headers we compiled against. | 485 | * compatible with the version of the headers we compiled against. |
diff --git a/pacman-c++/server.h b/pacman-c++/server.h index 0f2879d..a50cfe3 100644 --- a/pacman-c++/server.h +++ b/pacman-c++/server.h | |||
| @@ -35,6 +35,7 @@ protected: | |||
| 35 | void sendUpdate(Transmission::map_t map); | 35 | void sendUpdate(Transmission::map_t map); |
| 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 | 39 | ||
| 39 | protected: | 40 | protected: |
| 40 | QMap<Color::Color, QTcpSocket *> m_clientConnections; | 41 | QMap<Color::Color, QTcpSocket *> m_clientConnections; |
| @@ -45,6 +46,11 @@ protected: | |||
| 45 | /* allocate as member variable as this packet is large and used often */ | 46 | /* allocate as member variable as this packet is large and used often */ |
| 46 | ProtoBuf::MapUpdate m_updatepacket; | 47 | ProtoBuf::MapUpdate m_updatepacket; |
| 47 | 48 | ||
| 49 | /* list of blocks */ | ||
| 50 | QList<QPoint> m_blocks; | ||
| 51 | /* currently colored blocks + tickcount before they will turn to non-colored back */ | ||
| 52 | QMap<QPoint, unsigned int> m_coloredBlocks; | ||
| 53 | |||
| 48 | QHostAddress m_bindaddress; | 54 | QHostAddress m_bindaddress; |
| 49 | unsigned int m_port; | 55 | unsigned int m_port; |
| 50 | unsigned int m_maxplayers; | 56 | unsigned int m_maxplayers; |
