From cf925d1edca6bc1865232250d1f71249e512708f Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Wed, 13 Sep 2023 09:59:07 +0100 Subject: [PATCH] Replace tuples with structs Structs are more readable because the fields have names. --- Source/dvlnet/base_protocol.h | 16 +++++++++++----- Source/engine.h | 1 - Source/levels/gendung.cpp | 29 +++++++++++++++++------------ 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/Source/dvlnet/base_protocol.h b/Source/dvlnet/base_protocol.h index a299eab7b..9d0ab0bd6 100644 --- a/Source/dvlnet/base_protocol.h +++ b/Source/dvlnet/base_protocol.h @@ -45,7 +45,13 @@ private: endpoint_t firstpeer; std::string gamename; - std::map, endpoint_t>> game_list; + + struct GameListValue { + GameData data; + std::vector playerNames; + endpoint_t peer; + }; + std::map game_list; std::array peers; bool isGameHost_; @@ -99,8 +105,8 @@ bool base_protocol

::wait_firstpeer() { // wait for peer for 5 seconds for (auto i = 0; i < 500; ++i) { - if (game_list.count(gamename)) { - firstpeer = std::get<2>(game_list[gamename]); + if (game_list.find(gamename) != game_list.end()) { + firstpeer = game_list[gamename].peer; break; } send_info_request(); @@ -310,7 +316,7 @@ void base_protocol

::recv_decrypted(packet &pkt, endpoint_t sender) size_t gameNameSize = pkt.Info().size() - neededSize; gameName.resize(gameNameSize); std::memcpy(&gameName[0], pkt.Info().data() + neededSize, gameNameSize); - game_list[gameName] = std::make_tuple(*gameData, playerNames, sender); + game_list[gameName] = GameListValue { *gameData, std::move(playerNames), sender }; return; } recv_ingame(pkt, sender); @@ -437,7 +443,7 @@ std::vector base_protocol

::get_gamelist() ret.reserve(game_list.size()); for (const auto &[name, gameInfo] : game_list) { const auto &[gameData, players, _] = gameInfo; - ret.push_back({ name, gameData, players }); + ret.push_back(GameInfo { name, gameData, players }); } return ret; } diff --git a/Source/engine.h b/Source/engine.h index bec0fd133..b84dff4ed 100644 --- a/Source/engine.h +++ b/Source/engine.h @@ -16,7 +16,6 @@ #include #include #include -#include #include // We include `cinttypes` here so that it is included before `inttypes.h` diff --git a/Source/levels/gendung.cpp b/Source/levels/gendung.cpp index e6f9565c4..77d1ea44d 100644 --- a/Source/levels/gendung.cpp +++ b/Source/levels/gendung.cpp @@ -2,6 +2,7 @@ #include #include +#include #include "engine/load_file.hpp" #include "engine/random.hpp" @@ -265,17 +266,22 @@ void FindTransparencyValues(Point floor, uint8_t floorID) // Algorithm adapted from https://en.wikipedia.org/wiki/Flood_fill#Span_Filling // Modified to include diagonally adjacent tiles that would otherwise not be visited // Also, Wikipedia's selection for the initial seed is incorrect - using Seed = std::tuple; - std::stack seedStack; - seedStack.push(std::make_tuple(floor.x, floor.x + 1, floor.y, 1)); + struct Seed { + int scanStart; + int scanEnd; + int y; + int dy; + }; + std::stack> seedStack; + seedStack.push({ floor.x, floor.x + 1, floor.y, 1 }); - const auto isInside = [&](int x, int y) { + const auto isInside = [floorID](int x, int y) { if (dTransVal[x][y] != 0) return false; return IsFloor({ x, y }, floorID); }; - const auto set = [&](int x, int y) { + const auto set = [floorID](int x, int y) { FillTransparencyValues({ x, y }, floorID); }; @@ -285,17 +291,16 @@ void FindTransparencyValues(Point floor, uint8_t floorID) Point up = p + Displacement { 0, -1 }; Point upOver = up + direction; if (!isInside(up.x, up.y) && isInside(upOver.x, upOver.y)) - seedStack.push(std::make_tuple(upOver.x, upOver.x + 1, upOver.y, -1)); + seedStack.push({ upOver.x, upOver.x + 1, upOver.y, -1 }); Point down = p + Displacement { 0, 1 }; Point downOver = down + direction; if (!isInside(down.x, down.y) && isInside(downOver.x, downOver.y)) - seedStack.push(std::make_tuple(downOver.x, downOver.x + 1, downOver.y, 1)); + seedStack.push(Seed { downOver.x, downOver.x + 1, downOver.y, 1 }); }; while (!seedStack.empty()) { - int scanStart, scanEnd, y, dy; - std::tie(scanStart, scanEnd, y, dy) = seedStack.top(); + const auto [scanStart, scanEnd, y, dy] = seedStack.top(); seedStack.pop(); int scanLeft = scanStart; @@ -307,7 +312,7 @@ void FindTransparencyValues(Point floor, uint8_t floorID) checkDiagonals({ scanLeft, y }, left); } if (scanLeft < scanStart) - seedStack.push(std::make_tuple(scanLeft, scanStart - 1, y - dy, -dy)); + seedStack.push(Seed { scanLeft, scanStart - 1, y - dy, -dy }); int scanRight = scanStart; while (scanRight < scanEnd) { @@ -315,9 +320,9 @@ void FindTransparencyValues(Point floor, uint8_t floorID) set(scanRight, y); scanRight++; } - seedStack.push(std::make_tuple(scanLeft, scanRight - 1, y + dy, dy)); + seedStack.push(Seed { scanLeft, scanRight - 1, y + dy, dy }); if (scanRight - 1 > scanEnd) - seedStack.push(std::make_tuple(scanEnd + 1, scanRight - 1, y - dy, -dy)); + seedStack.push(Seed { scanEnd + 1, scanRight - 1, y - dy, -dy }); if (scanLeft < scanRight) checkDiagonals({ scanRight - 1, y }, right);