Skip to content

Commit

Permalink
Replace tuples with structs
Browse files Browse the repository at this point in the history
Structs are more readable because the fields have names.
  • Loading branch information
glebm committed Sep 13, 2023
1 parent 3d3cc31 commit cf925d1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
16 changes: 11 additions & 5 deletions Source/dvlnet/base_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ class base_protocol : public base {

endpoint_t firstpeer;
std::string gamename;
std::map<std::string, std::tuple<GameData, std::vector<std::string>, endpoint_t>> game_list;

struct GameListValue {
GameData data;
std::vector<std::string> playerNames;
endpoint_t peer;
};
std::map</*name*/ std::string, GameListValue> game_list;
std::array<Peer, MAX_PLRS> peers;
bool isGameHost_;

Expand Down Expand Up @@ -99,8 +105,8 @@ bool base_protocol<P>::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();
Expand Down Expand Up @@ -310,7 +316,7 @@ void base_protocol<P>::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);
Expand Down Expand Up @@ -437,7 +443,7 @@ std::vector<GameInfo> base_protocol<P>::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;
}
Expand Down
1 change: 0 additions & 1 deletion Source/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <tuple>
#include <utility>

// We include `cinttypes` here so that it is included before `inttypes.h`
Expand Down
29 changes: 17 additions & 12 deletions Source/levels/gendung.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>
#include <stack>
#include <vector>

#include "engine/load_file.hpp"
#include "engine/random.hpp"
Expand Down Expand Up @@ -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<int, int, int, int>;
std::stack<Seed> 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<Seed, std::vector<Seed>> 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);
};

Expand All @@ -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;
Expand All @@ -307,17 +312,17 @@ 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) {
while (isInside(scanRight, y)) {
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);

Expand Down

0 comments on commit cf925d1

Please sign in to comment.