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
|
#pragma once
#include "../Common/Sizes.hpp"
#include "../Math/Vector.hpp"
#include "ChunkDimensions.hpp"
#include "Position.hpp"
namespace MC::World {
struct ChunkIndex {
ChunkIndex() : x(0), y(0) {}
ChunkIndex(I32 x, I32 y) : x(x), y(y) {}
Vector<3> middle() const {
using namespace ChunkDimensions;
return {(x + 0.5f) * Width, Height / 2.0f, (y + 0.5f) * Width};
}
Position::BlockWorld local_to_world_position(Position::BlockLocal local) const {
using namespace ChunkDimensions;
return {TO(I64, x) * Width + local.x(), local.y(), TO(I64, y) * Width + local.z()};
}
Position::BlockWorld world_position() const {
return local_to_world_position({0, 0, 0});
}
static ChunkIndex from_position(Position::BlockWorld pos) {
I32 chunk_x = std::floor((Real)pos.x() / ChunkDimensions::Width);
I32 chunk_y = std::floor((Real)pos.z() / ChunkDimensions::Width);
return {chunk_x, chunk_y};
}
static ChunkIndex from_position(Position::World pos) {
return from_position(pos.round_to_block());
}
Bool operator==(const ChunkIndex& other) const {
return x == other.x && y == other.y;
}
I32 x, y;
};
}
template<> struct std::equal_to<MC::World::ChunkIndex> {
Bool operator()(const MC::World::ChunkIndex& i1, const MC::World::ChunkIndex& i2) const noexcept {
return i1.x == i2.x && i1.y == i2.y;
}
};
template<> struct std::hash<MC::World::ChunkIndex> {
USize operator()(const MC::World::ChunkIndex& i) const noexcept {
return (I64)i.x << 32 | i.y;
}
};
|