-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilemap.cpp
95 lines (77 loc) · 2.93 KB
/
tilemap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "kamkar.h"
#pragma warning(disable : 26812)
constexpr uint treeSizeLog2 = 5;
constexpr uint treeSize = 1 << treeSizeLog2;
constexpr uint chunkSizeLog2 = 6;
constexpr uint chunkSize = 1 << chunkSizeLog2;
struct Chunk {
Tile tiles[chunkSize][chunkSize];
};
constexpr uint maxDepth = 4;
constexpr uint tilemapHalfSize = 1 << (treeSizeLog2 * maxDepth);
constexpr uint tilemapTotalSize = 1 << (treeSizeLog2 * maxDepth + chunkSizeLog2);
static_assert(tilemapTotalSize, "tilemap size exceeds 32 bit unsigned integer limit");
struct Tree {
void *nodes[treeSize][treeSize];
};
struct Tilemap {
Tree tree;
fnl_state fnlState;
Chunk *chunkCache;
vec2<int> cachePosition;
};
template <uchar N>
static Terrain splitFloat(float input, const float (&splitPoints)[N]) {
for (uchar i = 0; i < N; ++i) {
if (input < splitPoints[i]) {
return Terrain(i);
}
}
return Terrain(N);
}
static Terrain terrainFromNoise(float noise) {
static const float splitPoints[] = { 0.0f, 0.2f, 0.7f };
return Terrain(splitFloat(noise, splitPoints) + 1);
}
static vec2<float> hexToPixel(vec2<int> hex) {
return { sqrt3 * hex.x + sqrt3 / 2 * hex.y, hexHeight * hex.y };
}
static Chunk *generateChunk(vec2<int> position, fnl_state *fnlState) {
Chunk *chunk = ecalloc<Chunk>(1);
for (int x = 0; x < chunkSize; ++x) {
for (int y = 0; y < chunkSize; ++y) {
const vec2 pixel = hexToPixel(position * chunkSize + vec2<int>{ x, y });
chunk->tiles[x][y].terrain = terrainFromNoise(fnlGetNoise2D(fnlState, pixel.x, pixel.y));
}
}
return chunk;
};
Tilemap *createTilemap() {
Tilemap *const tilemap = ecalloc<Tilemap>(1);
tilemap->fnlState = fnlCreateState();
tilemap->fnlState.fractal_type = FNL_FRACTAL_FBM;
return tilemap;
};
Tile *getTile(Tilemap *tilemap, vec2<int> position) {
// using >> and & because / and % are fucked up at negative values
const vec2<int> chunkPosition = position >> chunkSizeLog2;
if (tilemap->chunkCache == nullptr || tilemap->cachePosition != chunkPosition) {
void *node = &tilemap->tree;
for (int currentDepth = maxDepth; currentDepth >= 0; --currentDepth) {
const vec2<int> treePosition = (chunkPosition >> (currentDepth * treeSizeLog2)) & (treeSize - 1);
void *&subnode = static_cast<Tree *>(node)->nodes[treePosition.x][treePosition.y];
if (subnode == nullptr) {
if (currentDepth > 0) {
subnode = ecalloc<Tree>(1);
} else {
subnode = generateChunk(chunkPosition, &tilemap->fnlState);
}
}
node = subnode;
}
tilemap->chunkCache = static_cast<Chunk *>(node);
tilemap->cachePosition = chunkPosition;
}
const vec2<int> inChunkPosition = position & (chunkSize - 1);
return &tilemap->chunkCache->tiles[inChunkPosition.x][inChunkPosition.y];
}