nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
terrain.h
Go to the documentation of this file.
1#ifndef NANOMMO_TERRAIN_TERRAIN_H_
2#define NANOMMO_TERRAIN_TERRAIN_H_
3
4#include <cstdint>
5#include <vector>
6
7#include "absl/log/check.h"
8
9namespace nanommo {
10
11/// A grid kept in a single array, a row after another: grid[j, i] is the i-th
12/// of row j. Like a span, it points into the array and doesn't keep it. An
13/// index outside its row or column stops the program, as a column past the end
14/// of a row would still be inside the array, in the next row.
15template <typename T>
16class Grid {
17 public:
18 Grid(T* cells, int rows, int columns)
19 : cells_(cells), rows_(rows), columns_(columns) {}
20
21 T& operator[](int j, int i) const {
22 CHECK(j >= 0 && j < rows_ && i >= 0 && i < columns_)
23 << "grid[" << j << ", " << i << "] is outside its " << rows_
24 << " rows of " << columns_;
25 return cells_[j * columns_ + i];
26 }
27
28 private:
29 T* cells_;
30 int rows_;
31 int columns_;
32};
33
34struct Tree {
35 float x = 0.0f;
36 float z = 0.0f;
37 /// In radians, around the vertical.
38 float yaw = 0.0f;
39 /// How much bigger or smaller than the model it is.
40 float scale = 1.0f;
41};
42
43/// The ground everyone walks on. The server and the clients each make it
44/// from the same seed, so only the seed goes over the network.
45class Terrain {
46 public:
47 /// In metres. Each cell of the grid is two triangles.
48 static constexpr float kCell = 2.0f;
49
50 static constexpr float kSeaLevel = 0.0f;
51
52 /// Players wade into the sea up to their knees.
53 static constexpr float kWading = 0.4f;
54
55 /// The trees grow on a grid of squares this size, at most one in each.
56 static constexpr float kTreeSpacing = 4.0f;
57
58 /// How close to the middle of a trunk a player can stand.
59 static constexpr float kTrunk = 0.5f;
60
61 /// The board we had: flat, twelve metres across, for the tests.
62 Terrain();
63
64 /// An island, 512 metres across, with the sea all around it, which players
65 /// can wade into as deep as wading, in metres.
66 explicit Terrain(uint32_t seed, float wading = kWading);
67
68 /// On the triangles between the grid's corners, as the client draws them.
69 float Height(float x, float z) const;
70
71 /// Whether a player can stand there: on the grid, out of the sea, and not
72 /// in a tree.
73 bool Walkable(float x, float z) const;
74
75 const std::vector<Tree>& trees() const { return trees_; }
76
77 /// The height at a corner of the grid, from 0 to cells() along each side.
78 float Corner(int i, int j) const {
79 return Grid<const float>(corners_.data(), cells_ + 1, cells_ + 1)[j, i];
80 }
81
82 uint32_t seed() const { return seed_; }
83
84 /// Along each side.
85 int cells() const { return cells_; }
86
87 /// From the middle to each side of the grid.
88 float half_size() const { return cells_ * kCell / 2.0f; }
89
90 /// Half a metre short of each side, so that players stay on the grid.
91 float edge() const { return half_size() - 0.5f; }
92
93 private:
94 void Plant();
95 bool InATree(float x, float z) const;
96
97 uint32_t seed_ = 0;
98 float wading_ = kWading;
99 int cells_ = 0;
100 std::vector<float> corners_;
101 std::vector<Tree> trees_;
102 // For each square of the trees' grid, the tree in it, or -1.
103 std::vector<int> tree_in_;
104 int tree_squares_ = 0;
105};
106
107} // namespace nanommo
108
109#endif // NANOMMO_TERRAIN_TERRAIN_H_
T & operator[](int j, int i) const
Definition terrain.h:21
Grid(T *cells, int rows, int columns)
Definition terrain.h:18
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
const std::vector< Tree > & trees() const
Definition terrain.h:75
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
int cells() const
Along each side.
Definition terrain.h:85
static constexpr float kWading
Players wade into the sea up to their knees.
Definition terrain.h:53
Definition chat.cc:9
float yaw
In radians, around the vertical.
Definition terrain.h:38
float scale
How much bigger or smaller than the model it is.
Definition terrain.h:40