summaryrefslogtreecommitdiffhomepage
path: root/src/chunk.cpp
blob: 72b35b84cca04810ebe39b14dcb2c1239a68ef0e (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
#include "chunk.hpp"
#include "src/tile-atlas.hpp"

namespace floormat {

bool chunk::empty(bool force) const noexcept
{
    if (!force && !_maybe_empty)
        return false;
    for (auto i = 0_uz; i < TILE_COUNT; i++)
        if (_ground_atlases[i] || _wall_atlases[i*2 + 0] || _wall_atlases[i*2 + 1] || _scenery_atlases[i])
            return _maybe_empty = false;
    return true;
}

tile_atlas* chunk::ground_atlas_at(std::size_t i) const noexcept { return _ground_atlases[i].get(); }
tile_atlas* chunk::wall_atlas_at(std::size_t i) const noexcept { return _wall_atlases[i].get(); }

std::shared_ptr<anim_atlas>& chunk::scenery_atlas_at(std::size_t i) noexcept { return _scenery_atlases[i]; }
scenery& chunk::scenery_at(std::size_t i) noexcept { return _scenery_variants[i]; }

tile_ref chunk::operator[](std::size_t idx) noexcept { return { *this, std::uint8_t(idx) }; }
tile_proto chunk::operator[](std::size_t idx) const noexcept { return tile_proto(tile_ref { *const_cast<chunk*>(this), std::uint8_t(idx) }); }
tile_ref chunk::operator[](local_coords xy) noexcept { return operator[](xy.to_index()); }
tile_proto chunk::operator[](local_coords xy) const noexcept { return operator[](xy.to_index()); }

auto chunk::begin() noexcept -> iterator { return iterator { *this, 0 }; }
auto chunk::end() noexcept -> iterator { return iterator { *this, TILE_COUNT }; }
auto chunk::cbegin() const noexcept -> const_iterator { return const_iterator { *this, 0 }; }
auto chunk::cend() const noexcept -> const_iterator { return const_iterator { *this, TILE_COUNT }; }
auto chunk::begin() const noexcept -> const_iterator { return cbegin(); }
auto chunk::end() const noexcept -> const_iterator { return cend(); }

//#define FM_DEBUG_RELOAD

#ifdef FM_DEBUG_RELOAD
static std::size_t _reloadno_ = 0;
#endif

void chunk::mark_ground_modified() noexcept
{
#ifdef FM_DEBUG_RELOAD
    if (!_ground_modified)
        fm_debug("ground reload %zu", ++_reloadno_);
#endif
    _ground_modified = true;
    mark_passability_modified();
}

// todo this can use _replace_bbox too
void chunk::mark_walls_modified() noexcept
{
#ifdef FM_DEBUG_RELOAD
    if (!_walls_modified)
        fm_debug("wall reload %zu", ++_reloadno_);
#endif
    _walls_modified = true;
    mark_passability_modified();
}

void chunk::mark_scenery_modified(bool collision_too) noexcept // todo remove bool
{
#ifdef FM_DEBUG_RELOAD
    if (!_scenery_modified)
        fm_debug("scenery reload %zu", ++_reloadno_);
#endif
    _scenery_modified = true;
    if (collision_too)
        mark_passability_modified();
}

void chunk::mark_passability_modified() noexcept
{
#ifdef FM_DEBUG_RELOAD
    if (!_pass_modified)
        fm_debug("pass reload %zu", ++_reloadno_);
#endif
    _pass_modified = true;
}

bool chunk::is_passability_modified() const noexcept { return _pass_modified; }
bool chunk::is_scenery_modified() const noexcept { return _scenery_modified; }

void chunk::mark_modified() noexcept
{
    mark_ground_modified();
    mark_walls_modified();
    mark_scenery_modified();
}

chunk::chunk() noexcept = default;
chunk::~chunk() noexcept = default;

chunk::chunk(chunk&&) noexcept = default;
chunk& chunk::operator=(chunk&&) noexcept = default;

bool chunk::bbox::operator==(const bbox& other) const noexcept = default;

} // namespace floormat