summary refs log tree commit diff
path: root/src/World/Chunk.hpp
blob: e958b9a6f421d3166bad8c60c178db827fcd5857 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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 "../Common/Iteration.hpp"
#include "../GFX/Mesh.hpp"

namespace MC::World {

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

    static constexpr U32 BlockCount = Width * Height * Width;

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

    // Cache-friendly iteration through all blocks in the chunk.
    template <typename F>
    void for_each(F f) {
        for (U32 i = 0; i < BlockCount; ++i) {
            Iteration control = f(pos(i), m_blocks[i]);
            if (control == Iteration::Break) break;
        }
    }

    // Iteration through all blocks in the chunk by column.
    // Starts from the top of the chunk and goes down.
    enum class ColumnIteration { Continue, Break, SkipColumn };
    template <typename F>
    void for_each_by_column(F f) {
        // TODO: Maybe add a way to lookup the highest block in a column
        // to skip all the air?
        for (U32 x = 0; x < Width; ++x) {
            for (U32 z = 0; z < Width; ++z) {
                for (UInt y = Height - 1; y != 0; y--) {
                    ColumnIteration control = f({x, y, z}, m_blocks.at(pos(x, y, z)));
                    if (control == ColumnIteration::Break) goto end;
                    if (control == ColumnIteration::SkipColumn) break;
                }
            }
        }
        end: return;
    }

    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);
    static AABB block_bounds(Position::BlockWorld pos);
private:
    // Convert a local position to a chunk block index.
    static U64 pos(U32 x, U32 y, U32 z);
    // Convert a chunk block index to a local position.
    static Position::BlockLocal pos(U64 i);

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

    Bool m_damaged = false;

    Details m_details;
};

}