14constexpr int kBoardCells = 6;
15constexpr int kIslandCells = 256;
18constexpr float kClearing = 16.0f;
21constexpr float kLowestTree = 1.0f;
22constexpr float kHighestTree = 11.0f;
25float Unit(uint32_t hash) {
26 return static_cast<float>(hash >> 8) / 16777215.0f;
29float Smooth(
float t) {
30 const float clamped = std::clamp(t, 0.0f, 1.0f);
31 return clamped * clamped * (3.0f - 2.0f * clamped);
34float IslandHeight(uint32_t seed,
float x,
float z,
float half_size) {
35 const float from_middle = std::sqrt(x * x + z * z);
36 const float hills =
Fractal(seed, x / 90.0f, z / 90.0f, 5);
38 std::max(
Fractal(seed + 101, x / 140.0f, z / 140.0f, 4), 0.0f);
39 float height = 3.0f + 7.0f * hills + 60.0f * peaks * peaks;
41 const float coast = Smooth((from_middle / half_size - 0.55f) / 0.4f);
42 height = height * (1.0f - coast) - 12.0f * coast;
45 const float meadow = Smooth((from_middle - 12.0f) / 24.0f);
46 return 2.0f + (height - 2.0f) * (0.15f + 0.85f * meadow);
52 : cells_(kBoardCells), corners_((kBoardCells + 1) * (kBoardCells + 1)) {}
58 corners_((kIslandCells + 1) * (kIslandCells + 1)) {
59 const Grid<float> corners(corners_.data(), cells_ + 1, cells_ + 1);
60 for (
int j = 0; j <= cells_; ++j) {
61 for (
int i = 0; i <= cells_; ++i) {
70void Terrain::Plant() {
72 tree_in_.assign(tree_squares_ * tree_squares_, -1);
73 const Grid<int> squares(tree_in_.data(), tree_squares_, tree_squares_);
74 for (
int j = 0; j < tree_squares_; ++j) {
75 for (
int i = 0; i < tree_squares_; ++i) {
78 const auto roll = [
this, i, j](uint32_t die) {
79 return Unit(
Hash(seed_ + 1000 + die, i, j));
86 const float woods =
Fractal(seed_ + 2000, x / 60.0f, z / 60.0f, 3);
87 const float chance = std::clamp(0.5f + 3.0f * woods, 0.05f, 0.95f);
88 const float height =
Height(x, z);
89 if (roll(2) >= chance || height < kLowestTree || height > kHighestTree ||
90 std::sqrt(x * x + z * z) < kClearing) {
93 squares[j, i] =
static_cast<int>(trees_.size());
94 trees_.push_back({.x = x,
96 .yaw = 2.0f * std::numbers::pi_v<float> * roll(3),
97 .scale = 0.8f + 0.6f * roll(4)});
105 const int i = std::clamp(
static_cast<int>(std::floor(across)), 0, cells_ - 1);
106 const int j = std::clamp(
static_cast<int>(std::floor(down)), 0, cells_ - 1);
107 const float u = std::clamp(across - i, 0.0f, 1.0f);
108 const float v = std::clamp(down - j, 0.0f, 1.0f);
109 const float top_left =
Corner(i, j);
110 const float top_right =
Corner(i + 1, j);
111 const float bottom_left =
Corner(i, j + 1);
112 const float bottom_right =
Corner(i + 1, j + 1);
116 return top_left + (top_right - top_left) * u +
117 (bottom_right - top_right) * v;
119 return top_left + (bottom_right - bottom_left) * u +
120 (bottom_left - top_left) * v;
124 return std::abs(x) <=
edge() && std::abs(z) <=
edge() &&
128bool Terrain::InATree(
float x,
float z)
const {
131 const Grid<const int> squares(tree_in_.data(), tree_squares_, tree_squares_);
133 for (
int down = std::max(j - 1, 0);
134 down <= std::min(j + 1, tree_squares_ - 1); ++down) {
135 for (
int across = std::max(i - 1, 0);
136 across <= std::min(i + 1, tree_squares_ - 1); ++across) {
137 const int tree = squares[down, across];
139 std::hypot(trees_[tree].x - x, trees_[tree].z - z) <
kTrunk) {
float half_size() const
From the middle to each side of the grid.
float edge() const
Half a metre short of each side, so that players stay on the grid.
static constexpr float kCell
In metres. Each cell of the grid is two triangles.
Terrain()
The board we had: flat, twelve metres across, for the tests.
static constexpr float kSeaLevel
float Corner(int i, int j) const
The height at a corner of the grid, from 0 to cells() along each side.
static constexpr float kTrunk
How close to the middle of a trunk a player can stand.
static constexpr float kTreeSpacing
The trees grow on a grid of squares this size, at most one in each.
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
bool Walkable(float x, float z) const
float Fractal(uint32_t seed, float x, float z, int octaves)
uint32_t Hash(uint32_t seed, int32_t x, int32_t z)