summary refs log tree commit diff
path: root/src/World/Chunk.hpp
blob: 9a29284e6185ba857e73b99c5f5e0ca11be2d92b (plain)
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
#pragma once

#include "../Common/Sizes.hpp"
#include "../Math/AABB.hpp"
#include "ChunkDimensions.hpp"
#include "BiomeType.hpp"
#include "BlockType.hpp"
#include "ChunkIndex.hpp"
#include "Position.hpp"
#include "../GFX/Mesh.hpp"

namespace MC::World {

class Chunk {
public:
    static constexpr U32 Width = ChunkDimensions::Width;
    static constexpr U32 Height = ChunkDimensions::Height;

    Chunk(I64 x, I64 y)
        : m_blocks{Width * Height * Width, {BlockType::Air}},
        m_index(x, y),
        m_position{(Real)x * Width, 0.0f, (Real)y * Width} {}

    struct BlockData {
        BlockData() : type(BlockType::Air), light{0} {}
        BlockData(BlockType t) : type(t), light{0} {}

        BlockType type;
        U8 light;

        Bool empty() const { return type == BlockType::Air; }
    };

    const BlockData& at(U32 x, U32 y, U32 z) const;
    BlockData& at(U32 x, U32 y, U32 z);
    const BlockData& at(Position::BlockLocal pos) const;
    BlockData& at(Position::BlockLocal pos);

    struct Details {
        Matrix<Width, Width> landmass_values{};
        Matrix<Width, Width> hill_values{};

        Matrix<Width, Width> temperature_values{};
        Matrix<Width, Width> humidity_values{};

        Matrix<Width, Width, BiomeType> biome_values{};
    };
    void set_details(const Details& details) { m_details = details; }
    Details& details(){ return m_details; }

    template <typename F>
    void for_each(F f) {
        for (U32 x = 0; x < Width; ++x) {
            for (U32 y = 0; y < Height; ++y) {
                for (U32 z = 0; z < Width; ++z) {
                    Position::BlockLocal pos{x, y, z};
                    f(pos, at(x, y, z));
                }
            }
        }
    }

    ChunkIndex index() const;
    Vector<3> position() const;

    Bool is_damaged() const;
    void damage();

    static Bool is_valid_position(Position::BlockLocal pos);
    static AABB block_bounds(Position::BlockLocal pos);
private:
    static U64 pos(U32 x, U32 y, U32 z);

    ChunkIndex m_index;
    Vector<3> m_position;
    std::vector<BlockData> m_blocks;

    Bool m_damaged = false;

    Details m_details;
};

}