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

#include "../Common/Sizes.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);

    Bool is_empty(U32 x, U32 y, U32 z) const;

    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; }

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

    Bool is_damaged() const;
    void damage();

    static Bool is_valid_position(U32 x, U32 y, U32 z);
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;
};

}