diff options
Diffstat (limited to 'pacman-c++/server.cpp')
| -rw-r--r-- | pacman-c++/server.cpp | 166 |
1 files changed, 153 insertions, 13 deletions
diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index ef271f0..2b43bb9 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp | |||
| @@ -44,7 +44,7 @@ bool Server::run() | |||
| 44 | 44 | ||
| 45 | void Server::tick() | 45 | void Server::tick() |
| 46 | { | 46 | { |
| 47 | qDebug() << "[Tick] Doing server update"; | 47 | //qDebug() << "[Tick] Doing server update"; |
| 48 | if (m_finishRound) | 48 | if (m_finishRound) |
| 49 | stopGame(true); | 49 | stopGame(true); |
| 50 | if (!m_running) | 50 | if (!m_running) |
| @@ -78,6 +78,7 @@ void Server::tick() | |||
| 78 | 78 | ||
| 79 | Transmission::map_t Server::calculateUpdates() | 79 | Transmission::map_t Server::calculateUpdates() |
| 80 | { | 80 | { |
| 81 | QMap<Color::Color, QPair<QPoint, QPoint> > movements; | ||
| 81 | Transmission::map_t map = Util::createEmptyMap(); | 82 | Transmission::map_t map = Util::createEmptyMap(); |
| 82 | 83 | ||
| 83 | QMutableMapIterator<Color::Color, Actor::Movement> i(m_actorMovements); | 84 | QMutableMapIterator<Color::Color, Actor::Movement> i(m_actorMovements); |
| @@ -105,12 +106,9 @@ invalid_direction: | |||
| 105 | if (newMapPosition.y() >= visualMap[newMapPosition.x()].size()) | 106 | if (newMapPosition.y() >= visualMap[newMapPosition.x()].size()) |
| 106 | newMapPosition.setY(visualMap[newMapPosition.x()].size() - 1); | 107 | newMapPosition.setY(visualMap[newMapPosition.x()].size() - 1); |
| 107 | 108 | ||
| 108 | // <t3h g4m3 10gic> | ||
| 109 | // TODO: support actors eating each other | ||
| 110 | GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; | ||
| 111 | |||
| 112 | /* check if there's an item at new location of actor */ | 109 | /* check if there's an item at new location of actor */ |
| 113 | GameEntity *item = visualMap[newMapPosition.x()][newMapPosition.y()]; | 110 | GameEntity *item = visualMap[newMapPosition.x()][newMapPosition.y()]; |
| 111 | GameEntity *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; | ||
| 114 | if (item != NULL && oldItem != item) | 112 | if (item != NULL && oldItem != item) |
| 115 | { | 113 | { |
| 116 | qDebug() << "[Calc] Found item at new actor location"; | 114 | qDebug() << "[Calc] Found item at new actor location"; |
| @@ -128,14 +126,13 @@ invalid_direction: | |||
| 128 | map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty | actor->color(); | 126 | map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty | actor->color(); |
| 129 | else if (survive == GameEntity::DestroyedActor) | 127 | else if (survive == GameEntity::DestroyedActor) |
| 130 | { | 128 | { |
| 129 | map[newMapPosition.x()][newMapPosition.y()] = Transmission::death | actor->color(); | ||
| 131 | m_actors[item->color()]->addRoundPoints(actor->getRoundPoints()); | 130 | m_actors[item->color()]->addRoundPoints(actor->getRoundPoints()); |
| 132 | actor->finishRound(true); | 131 | actor->finishRound(true); |
| 133 | map[newMapPosition.x()][newMapPosition.y()] = Transmission::death | actor->color(); | ||
| 134 | setFinishRound(); | 132 | setFinishRound(); |
| 135 | } | 133 | } |
| 136 | } | 134 | } |
| 137 | } | 135 | } |
| 138 | // </t3h g4m2 10gic> | ||
| 139 | 136 | ||
| 140 | /* movement didn't work - e.g. was blocked */ | 137 | /* movement didn't work - e.g. was blocked */ |
| 141 | if (mapPosition == newMapPosition) | 138 | if (mapPosition == newMapPosition) |
| @@ -156,19 +153,108 @@ invalid_direction: | |||
| 156 | } | 153 | } |
| 157 | } | 154 | } |
| 158 | 155 | ||
| 159 | map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | | 156 | /* store movement for collision */ |
| 160 | color | Util::actorMovementToTransmission(direction); | 157 | movements.insert(color, QPair<QPoint, QPoint>(mapPosition, newMapPosition)); |
| 158 | |||
| 159 | /* set direction (used for collision detection) */ | ||
| 160 | actor->setDirection(direction); | ||
| 161 | 161 | ||
| 162 | /* DEBUG: uncomments to disable auto-movement */ | 162 | /* DEBUG: uncomments to disable auto-movement */ |
| 163 | //direction = Actor::None; | 163 | //direction = Actor::None; |
| 164 | 164 | ||
| 165 | /* actor is not moving anymore: remove from list */ | ||
| 165 | if (direction == Actor::None) | 166 | if (direction == Actor::None) |
| 166 | { | ||
| 167 | /* set actor to non-moving */ | ||
| 168 | m_actorMovements[color] = Actor::None; | ||
| 169 | i.remove(); | 167 | i.remove(); |
| 168 | } | ||
| 169 | |||
| 170 | /* check for actor collision */ | ||
| 171 | QList<Actor *> blocked; | ||
| 172 | foreach(Color::Color color, movements.keys()) | ||
| 173 | { | ||
| 174 | Actor *actor = m_actors[color]; | ||
| 175 | QPoint oldpos = movements[color].first; | ||
| 176 | QPoint newpos = movements[color].second; | ||
| 177 | QPoint scenepos = actor->pos().toPoint(); | ||
| 178 | |||
| 179 | /* first move actor to new position */ | ||
| 180 | actor->move(actor->direction()); | ||
| 181 | |||
| 182 | /* next check for collisions */ | ||
| 183 | Actor *orderer = NULL; | ||
| 184 | Actor *meal = NULL; | ||
| 185 | foreach(Actor *other, m_actors) | ||
| 186 | { | ||
| 187 | if (actor == other) | ||
| 188 | continue; | ||
| 189 | if (!actor->collidesWithItem(other)) | ||
| 190 | continue; | ||
| 191 | /* both move in the same direction */ | ||
| 192 | if (actor->direction() == other->direction()) | ||
| 193 | continue; | ||
| 194 | |||
| 195 | if (other->canEat(actor, m_eatingorder)) | ||
| 196 | { | ||
| 197 | qDebug() << "[Collision] Actor" << actor->color() << "got EATEN by" << other->color(); | ||
| 198 | orderer = other; | ||
| 199 | meal = actor; | ||
| 200 | break; | ||
| 201 | } | ||
| 202 | else if (actor->canEat(other, m_eatingorder)) | ||
| 203 | { | ||
| 204 | qDebug() << "[Collision] Actor" << actor->color() << "EATS" << other->color(); | ||
| 205 | orderer = actor; | ||
| 206 | meal = other; | ||
| 207 | blocked.append(other); | ||
| 208 | break; | ||
| 209 | } | ||
| 210 | else | ||
| 211 | { | ||
| 212 | qDebug() << "[Collision] Actor" << actor->color() << "got BLOCKED by" << other->color(); | ||
| 213 | blocked.append(actor); | ||
| 214 | /* no break here */ | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | /* update map depending on collision */ | ||
| 219 | if (orderer != NULL && meal != NULL) | ||
| 220 | { | ||
| 221 | map[newpos.x()][newpos.y()] |= Transmission::pacman | Transmission::death | ||
| 222 | | orderer->color() | meal->color(); | ||
| 223 | orderer->addRoundPoints(meal->getRoundPoints()); | ||
| 224 | meal->finishRound(true); | ||
| 225 | setFinishRound(); | ||
| 226 | } | ||
| 227 | else if (blocked.contains(actor)) | ||
| 228 | { | ||
| 229 | /* move actor back early cause he won't move */ | ||
| 230 | actor->setPos(scenepos); | ||
| 231 | map[oldpos.x()][oldpos.y()] |= Transmission::pacman | color; | ||
| 232 | } | ||
| 233 | else | ||
| 234 | { | ||
| 235 | map[newpos.x()][newpos.y()] |= Transmission::pacman | color; | ||
| 170 | } | 236 | } |
| 171 | } | 237 | } |
| 238 | |||
| 239 | /* move all actors back to their origin position */ | ||
| 240 | foreach(Color::Color color, movements.keys()) | ||
| 241 | m_actors[color]->setPos(mapPositionToCoord(movements[color].first)); | ||
| 242 | |||
| 243 | foreach(Color::Color col, m_eatingorder.toSet()) | ||
| 244 | { | ||
| 245 | QList<QPoint> found; | ||
| 246 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | ||
| 247 | { | ||
| 248 | for (unsigned int y = 0; y < Constants::map_size.height; ++y) | ||
| 249 | { | ||
| 250 | if ((map[x][y] & Transmission::color_mask) & col) | ||
| 251 | found.append(QPoint(x, y)); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | if (found.count() > 1) | ||
| 255 | qDebug() << "found" << found << "fields with color=" << col; | ||
| 256 | } | ||
| 257 | |||
| 172 | return map; | 258 | return map; |
| 173 | } | 259 | } |
| 174 | 260 | ||
| @@ -519,6 +605,60 @@ void Server::initRoundMap() | |||
| 519 | Util::placeActors(map, m_maxplayers, Color::order); | 605 | Util::placeActors(map, m_maxplayers, Color::order); |
| 520 | Util::fillPoints(map); | 606 | Util::fillPoints(map); |
| 521 | 607 | ||
| 608 | #if 0 // actor eating actor tests | ||
| 609 | m_actorMovements.clear(); | ||
| 610 | #if 0 | ||
| 611 | //works | ||
| 612 | map[0][0] = Color::order[0] | Transmission::pacman; | ||
| 613 | map[0][1] = Color::order[1] | Transmission::pacman; | ||
| 614 | m_actorMovements.insert(Color::order[0], Actor::Down); | ||
| 615 | #elif 0 | ||
| 616 | //works | ||
| 617 | map[0][0] = Color::order[0] | Transmission::pacman; | ||
| 618 | map[0][3] = Color::order[1] | Transmission::pacman; | ||
| 619 | m_actorMovements.insert(Color::order[0], Actor::Down); | ||
| 620 | #elif 0 | ||
| 621 | //works | ||
| 622 | map[0][0] = Color::order[0] | Transmission::pacman; | ||
| 623 | map[0][4] = Color::order[1] | Transmission::pacman; | ||
| 624 | m_actorMovements.insert(Color::order[0], Actor::Down); | ||
| 625 | #elif 0 | ||
| 626 | //works | ||
| 627 | map[0][0] = Color::order[0] | Transmission::pacman; | ||
| 628 | map[0][5] = Color::order[1] | Transmission::pacman; | ||
| 629 | m_actorMovements.insert(Color::order[0], Actor::Down); | ||
| 630 | #elif 0 | ||
| 631 | //works | ||
| 632 | map[0][0] = Color::order[1] | Transmission::pacman; | ||
| 633 | map[0][5] = Color::order[0] | Transmission::pacman; | ||
| 634 | m_actorMovements.insert(Color::order[1], Actor::Down); | ||
| 635 | #elif 0 | ||
| 636 | //works | ||
| 637 | map[0][0] = Color::order[0] | Transmission::pacman; | ||
| 638 | map[0][5] = Color::order[1] | Transmission::pacman; | ||
| 639 | m_actorMovements.insert(Color::order[0], Actor::Down); | ||
| 640 | m_actorMovements.insert(Color::order[1], Actor::Up); | ||
| 641 | #elif 0 | ||
| 642 | //works | ||
| 643 | map[0][0] = Color::order[0] | Transmission::pacman; | ||
| 644 | map[0][6] = Color::order[1] | Transmission::pacman; | ||
| 645 | m_actorMovements.insert(Color::order[0], Actor::Down); | ||
| 646 | m_actorMovements.insert(Color::order[1], Actor::Up); | ||
| 647 | #elif 0 | ||
| 648 | //works | ||
| 649 | map[0][0] = Color::order[0] | Transmission::pacman; | ||
| 650 | map[0][7] = Color::order[1] | Transmission::pacman; | ||
| 651 | m_actorMovements.insert(Color::order[0], Actor::Down); | ||
| 652 | m_actorMovements.insert(Color::order[1], Actor::Up); | ||
| 653 | #elif 1 | ||
| 654 | //works | ||
| 655 | map[0][0] = Color::order[0] | Transmission::pacman; | ||
| 656 | map[0][1] = Color::order[1] | Transmission::pacman; | ||
| 657 | m_actorMovements.insert(Color::order[0], Actor::Down); | ||
| 658 | m_actorMovements.insert(Color::order[1], Actor::Down); | ||
| 659 | #endif | ||
| 660 | #endif | ||
| 661 | |||
| 522 | /* save positions of blocks for later usage */ | 662 | /* save positions of blocks for later usage */ |
| 523 | m_blocks.clear(); | 663 | m_blocks.clear(); |
| 524 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) | 664 | for (unsigned int x = 0; x < Constants::map_size.width; ++x) |
| @@ -696,7 +836,7 @@ bool Server::parseCommandline() | |||
| 696 | { | 836 | { |
| 697 | bool ok; | 837 | bool ok; |
| 698 | unsigned int rounds = QString(opt.getValue("rounds")).toUInt(&ok); | 838 | unsigned int rounds = QString(opt.getValue("rounds")).toUInt(&ok); |
| 699 | if (!ok || rounds == 0) | 839 | if (!ok) |
| 700 | { | 840 | { |
| 701 | qCritical() << "Invalid number of rounds: " << opt.getValue("rounds") << endl; | 841 | qCritical() << "Invalid number of rounds: " << opt.getValue("rounds") << endl; |
| 702 | return false; | 842 | return false; |
