Browse Source

Replace tuples with structs

Structs are more readable because the fields have names.
pull/6598/head
Gleb Mazovetskiy 3 years ago
parent
commit
cf925d1edc
  1. 16
      Source/dvlnet/base_protocol.h
  2. 1
      Source/engine.h
  3. 29
      Source/levels/gendung.cpp

16
Source/dvlnet/base_protocol.h

@ -45,7 +45,13 @@ private:
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_;
@ -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();
@ -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);
@ -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;
}

1
Source/engine.h

@ -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`

29
Source/levels/gendung.cpp

@ -2,6 +2,7 @@
#include <cstdint>
#include <stack>
#include <vector>
#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<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);
};
@ -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);

Loading…
Cancel
Save