Skip to content

Commit 6dbf15e

Browse files
[server] fix sparse world generator not being seed-deterministic #5
1 parent d5512a0 commit 6dbf15e

File tree

5 files changed

+6
-13
lines changed

5 files changed

+6
-13
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SERVER_EXECUTABLE := server
1313
# Configs
1414
CONFIG_FOLDER := my-core-bot/configs
1515
CONFIG_SERVER_FILE := $(CONFIG_FOLDER)/server-config.json
16-
CONFIG_GAME_FILE := $(CONFIG_FOLDER)/hard-config.json
16+
CONFIG_GAME_FILE := $(CONFIG_FOLDER)/soft-config.json
1717

1818
# Git info
1919
CURRENT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)

server/inc/Common.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ struct Position
4444
{
4545
return std::abs(x - other.x) + std::abs(y - other.y);
4646
}
47-
48-
static Position random(int maxSizeSquare)
49-
{
50-
static std::default_random_engine eng = std::default_random_engine(time(nullptr));
51-
std::uniform_int_distribution<int> posX(0, maxSizeSquare - 1);
52-
std::uniform_int_distribution<int> posY(0, maxSizeSquare - 1);
53-
return Position(posX(eng), posY(eng));
54-
}
5547
};
5648

5749
#endif // COMMON_H

server/inc/config/worldgen/SparseWorldGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SparseWorldGenerator : public WorldGenerator
2020
void generateWorld(unsigned int seed);
2121

2222
private:
23-
std::mt19937_64 eng_ = std::mt19937_64(std::chrono::system_clock::now().time_since_epoch().count());
23+
std::mt19937_64 eng_{};
2424
};
2525

2626
#endif // DISTANCED_RESOURCE_WORLD_GENERATOR_H

server/src/config/worldgen/SparseWorldGenerator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static bool positionHasNeighbours(const Position& pos, int N)
2222

2323
void SparseWorldGenerator::generateWorld(unsigned int seed)
2424
{
25-
eng_ = std::mt19937_64(seed);
25+
eng_.seed(seed);
2626

2727
unsigned int gridSize = Config::game().gridSize;
2828
int depositCount = Config::game().worldGeneratorConfig.value("depositCount", 20);
@@ -43,7 +43,8 @@ void SparseWorldGenerator::generateWorld(unsigned int seed)
4343

4444
const int N = static_cast<int>(gridSize);
4545
auto tryPlace = [&](ObjectType t) -> bool {
46-
Position p = Position::random(gridSize);
46+
static std::uniform_int_distribution<int> dist(0, static_cast<int>(gridSize) - 1);
47+
Position p{dist(eng_), dist(eng_)};
4748
if (p.x < 0 || p.x >= N || p.y < 0 || p.y >= N) return false;
4849
if (Board::instance().getObjectAtPos(p)) return false;
4950
if (positionHasNeighbours(p, N)) return false;

0 commit comments

Comments
 (0)