nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
terrain.cc
Go to the documentation of this file.
1#include "terrain/terrain.h"
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <numbers>
7
8#include "terrain/noise.h"
9
10namespace nanommo {
11
12namespace {
13
14constexpr int kBoardCells = 6;
15constexpr int kIslandCells = 256;
16
17// The players start in a clearing.
18constexpr float kClearing = 16.0f;
19
20// Above the beach, and below the rock.
21constexpr float kLowestTree = 1.0f;
22constexpr float kHighestTree = 11.0f;
23
24// From the top 24 bits of a hash, from 0 to 1.
25float Unit(uint32_t hash) {
26 return static_cast<float>(hash >> 8) / 16777215.0f;
27}
28
29float Smooth(float t) {
30 const float clamped = std::clamp(t, 0.0f, 1.0f);
31 return clamped * clamped * (3.0f - 2.0f * clamped);
32}
33
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);
37 const float peaks =
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;
40 // The land sinks into the sea towards the edge.
41 const float coast = Smooth((from_middle / half_size - 0.55f) / 0.4f);
42 height = height * (1.0f - coast) - 12.0f * coast;
43 // Gentle slopes in the middle, where the players start, and never under the
44 // sea.
45 const float meadow = Smooth((from_middle - 12.0f) / 24.0f);
46 return 2.0f + (height - 2.0f) * (0.15f + 0.85f * meadow);
47}
48
49} // namespace
50
52 : cells_(kBoardCells), corners_((kBoardCells + 1) * (kBoardCells + 1)) {}
53
54Terrain::Terrain(uint32_t seed, float wading)
55 : seed_(seed),
56 wading_(wading),
57 cells_(kIslandCells),
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) {
62 const float x = i * kCell - half_size();
63 const float z = j * kCell - half_size();
64 corners[j, i] = IslandHeight(seed, x, z, half_size());
65 }
66 }
67 Plant();
68}
69
70void Terrain::Plant() {
71 tree_squares_ = static_cast<int>(2.0f * half_size() / kTreeSpacing);
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) {
76 // Each square rolls its own dice: where in the square its tree
77 // stands, whether it grows, which way it faces, and how big it is.
78 const auto roll = [this, i, j](uint32_t die) {
79 return Unit(Hash(seed_ + 1000 + die, i, j));
80 };
81 const float x = i * kTreeSpacing - half_size() +
82 kTreeSpacing * (0.2f + 0.6f * roll(0));
83 const float z = j * kTreeSpacing - half_size() +
84 kTreeSpacing * (0.2f + 0.6f * roll(1));
85 // Woods where the noise is high, and a few lone trees elsewhere.
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) {
91 continue;
92 }
93 squares[j, i] = static_cast<int>(trees_.size());
94 trees_.push_back({.x = x,
95 .z = z,
96 .yaw = 2.0f * std::numbers::pi_v<float> * roll(3),
97 .scale = 0.8f + 0.6f * roll(4)});
98 }
99 }
100}
101
102float Terrain::Height(float x, float z) const {
103 const float across = (x + half_size()) / kCell;
104 const float down = (z + half_size()) / kCell;
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);
113 // Each cell's two triangles meet on the diagonal from top left to bottom
114 // right.
115 if (u >= v) {
116 return top_left + (top_right - top_left) * u +
117 (bottom_right - top_right) * v;
118 }
119 return top_left + (bottom_right - bottom_left) * u +
120 (bottom_left - top_left) * v;
121}
122
123bool Terrain::Walkable(float x, float z) const {
124 return std::abs(x) <= edge() && std::abs(z) <= edge() &&
125 Height(x, z) >= kSeaLevel - wading_ && !InATree(x, z);
126}
127
128bool Terrain::InATree(float x, float z) const {
129 const int i = static_cast<int>(std::floor((x + half_size()) / kTreeSpacing));
130 const int j = static_cast<int>(std::floor((z + half_size()) / kTreeSpacing));
131 const Grid<const int> squares(tree_in_.data(), tree_squares_, tree_squares_);
132 // A trunk near the side of its square is closer to us than its square is.
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];
138 if (tree >= 0 &&
139 std::hypot(trees_[tree].x - x, trees_[tree].z - z) < kTrunk) {
140 return true;
141 }
142 }
143 }
144 return false;
145}
146
147} // namespace nanommo
float half_size() const
From the middle to each side of the grid.
Definition terrain.h:88
float edge() const
Half a metre short of each side, so that players stay on the grid.
Definition terrain.h:91
static constexpr float kCell
In metres. Each cell of the grid is two triangles.
Definition terrain.h:48
Terrain()
The board we had: flat, twelve metres across, for the tests.
Definition terrain.cc:51
static constexpr float kSeaLevel
Definition terrain.h:50
float Corner(int i, int j) const
The height at a corner of the grid, from 0 to cells() along each side.
Definition terrain.h:78
uint32_t seed() const
Definition terrain.h:82
static constexpr float kTrunk
How close to the middle of a trunk a player can stand.
Definition terrain.h:59
static constexpr float kTreeSpacing
The trees grow on a grid of squares this size, at most one in each.
Definition terrain.h:56
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
Definition terrain.cc:102
bool Walkable(float x, float z) const
Definition terrain.cc:123
Definition chat.cc:9
float Fractal(uint32_t seed, float x, float z, int octaves)
Definition noise.cc:47
uint32_t Hash(uint32_t seed, int32_t x, int32_t z)
Definition noise.cc:22