summaryrefslogtreecommitdiffhomepage
path: root/serialize/world-reader.cpp
blob: 0b84dbf0108017a57c3708a41b87d9cf5ceea111 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#define FM_SERIALIZE_WORLD_IMPL
#include "world-impl.hpp"
#include "binary-reader.inl"
#include "src/world.hpp"
#include "src/scenery.hpp"
#include "src/character.hpp"
#include "loader/loader.hpp"
#include "loader/scenery.hpp"
#include "src/tile-atlas.hpp"
#include "src/anim-atlas.hpp"

#include <cstring>

namespace {

using namespace floormat;
using namespace floormat::Serialize;

struct reader_state final {
    explicit reader_state(world& world) noexcept;
    void deserialize_world(ArrayView<const char> buf);

private:
    using reader_t = binary_reader<decltype(ArrayView<const char>{}.cbegin())>;

    const std::shared_ptr<tile_atlas>& lookup_atlas(atlasid id);
    const scenery_proto& lookup_scenery(atlasid id);
    void read_atlases(reader_t& reader);
    void read_sceneries(reader_t& reader);
    void read_chunks(reader_t& reader);
    void read_old_scenery(reader_t& s, chunk_coords ch, std::size_t i);

    std::vector<scenery_proto> sceneries;
    std::vector<std::shared_ptr<tile_atlas>> atlases;
    world* _world;
    std::uint16_t PROTO = 0;
};

reader_state::reader_state(world& world) noexcept : _world{&world} {}

void reader_state::read_atlases(reader_t& s)
{
    const auto N = s.read<atlasid>();
    atlases.reserve(N);
    for (atlasid i = 0; i < N; i++)
    {
        Vector2ub size;
        size[0] << s;
        size[1] << s;
        const auto& [buf, len] = s.read_asciiz_string<atlas_name_max>();
        auto atlas = loader.tile_atlas({buf, len});
        fm_soft_assert(size <= atlas->num_tiles2());
        atlases.push_back(std::move(atlas));
    }
}

template<typename T, entity_subtype U>
bool read_entity_flags(binary_reader<T>& s, U& e)
{
    constexpr auto tag = entity_type_<U>::value;
    std::uint8_t flags; flags << s;
    e.pass = pass_mode(flags & pass_mask);
    if (e.type != tag)
         fm_abort("invalid entity type '%d'", (int)e.type);
    if constexpr(tag == entity_type::scenery)
    {
        e.active      = !!(flags & 1 << 2);
        e.closing     = !!(flags & 1 << 3);
        e.interactive = !!(flags & 1 << 4);
    }
    else if constexpr(tag == entity_type::character)
    {
        e.playable    = !!(flags & 1 << 2);
    }
    else
        static_assert(tag == entity_type::none);
    return flags & 1 << 7;
}

void reader_state::read_sceneries(reader_t& s)
{
    (void)loader.sceneries();

    std::uint16_t magic; magic << s;
    if (magic != scenery_magic)
        fm_throw("bad scenery magic"_cf);
    atlasid sz; sz << s;
    fm_soft_assert(sz < scenery_id_max);
    sceneries.resize(sz);

    auto i = 0_uz;
    while (i < sz)
    {
        std::uint8_t num; num << s;
        fm_soft_assert(num > 0);
        auto str = s.read_asciiz_string<atlas_name_max>();
        auto sc = loader.scenery(str);
        for (auto n = 0_uz; n < num; n++)
        {
            atlasid id; id << s;
            fm_soft_assert(id < sz);
            fm_soft_assert(!sceneries[id]);
            bool short_frame = read_entity_flags(s, sc);
            fm_debug_assert(sc.atlas != nullptr);
            if (short_frame)
                sc.frame = s.read<std::uint8_t>();
            else
                sc.frame << s;
            fm_soft_assert(sc.frame < sc.atlas->info().nframes);
            sceneries[id] = sc;
        }
        i += num;
    }
    fm_soft_assert(i == sz);
}

const std::shared_ptr<tile_atlas>& reader_state::lookup_atlas(atlasid id)
{
    if (id < atlases.size())
        return atlases[id];
    else
        fm_throw("no such atlas: '{}'"_cf, id);
}

const scenery_proto& reader_state::lookup_scenery(atlasid id)
{
    if (id < sceneries.size())
        return sceneries[id];
    else
        fm_throw("no such scenery: '{}'"_cf, id);
}

#ifndef FM_NO_DEBUG
#   define SET_CHUNK_SIZE() do { nbytes_read = s.bytes_read() - nbytes_start; } while (false)
#else
#   define SET_CHUNK_SIZE() void()
#endif

void reader_state::read_chunks(reader_t& s)
{
    const auto N = s.read<chunksiz>();
#ifndef FM_NO_DEBUG
    [[maybe_unused]] std::size_t nbytes_read = 0;
#endif

    for (auto k = 0_uz; k < N; k++)
    {
        const auto nbytes_start = s.bytes_read();

        std::decay_t<decltype(chunk_magic)> magic;
        magic << s;
        if (magic != chunk_magic)
            fm_throw("bad chunk magic"_cf);
        chunk_coords ch;
        ch.x << s;
        ch.y << s;
        auto& c = (*_world)[ch];
        c.mark_modified();
        for (auto i = 0_uz; i < TILE_COUNT; i++)
        {
            SET_CHUNK_SIZE();
            const tilemeta flags = s.read<tilemeta>();
            tile_ref t = c[i];
            using uchar = std::uint8_t;
            const auto make_atlas = [&]() -> tile_image_proto {
                atlasid id;
                if (PROTO < 8) [[unlikely]]
                    id = flags & meta_short_atlasid_ ? atlasid{s.read<uchar>()} : s.read<atlasid>();
                else
                    id << s;
                variant_t v;
                if (PROTO >= 2) [[likely]]
                    v << s;
                else
                    v = flags & meta_short_variant_
                        ? s.read<std::uint8_t>()
                        : std::uint8_t(s.read<std::uint16_t>());
                auto atlas = lookup_atlas(id);
                fm_soft_assert(v < atlas->num_tiles());
                return { atlas, v };
            };
            SET_CHUNK_SIZE();
            //t.passability() = pass_mode(flags & pass_mask);
            if (flags & meta_ground)
                t.ground() = make_atlas();
            if (flags & meta_wall_n)
                t.wall_north() = make_atlas();
            if (flags & meta_wall_w)
                t.wall_west() = make_atlas();
            if (PROTO >= 3 && PROTO < 8) [[unlikely]]
                if (flags & meta_scenery_)
                    read_old_scenery(s, ch, i);
            SET_CHUNK_SIZE();
        }
        std::uint32_t entity_count = 0;
        if (PROTO >= 8) [[likely]]
                entity_count << s;

        SET_CHUNK_SIZE();

        for (auto i = 0_uz; i < entity_count; i++)
        {
            std::uint64_t _id; _id << s;
            const auto oid = _id & (1ULL << 60)-1;
            fm_soft_assert(oid != 0);
            static_assert(entity_type_BITS == 3);
            const auto type = entity_type(_id >> 61);
            const auto local = local_coords{s.read<std::uint8_t>()};
            constexpr auto read_offsets = [](auto& s, auto& e) {
                s >> e.offset[0];
                s >> e.offset[1];
                s >> e.bbox_offset[0];
                s >> e.bbox_offset[1];
                s >> e.bbox_size[0];
                s >> e.bbox_size[1];
            };
            SET_CHUNK_SIZE();
            switch (type)
            {
            case entity_type::character: {
                character_proto proto;
                std::uint8_t id; id << s;
                proto.r = rotation(id >> sizeof(id)*8-1-rotation_BITS & rotation_MASK);
                if (read_entity_flags(s, proto))
                    proto.frame = s.read<std::uint8_t>();
                else
                    proto.frame << s;
                Vector2s offset_frac;
                offset_frac[0] << s;
                offset_frac[1] << s;
                SET_CHUNK_SIZE();
                const auto name = s.read_asciiz_string<character_name_max>();
                proto.name = StringView{name.buf, name.len, StringViewFlag::Global|StringViewFlag::NullTerminated};
                if (!(id & meta_short_scenery_bit))
                    read_offsets(s, proto);
                SET_CHUNK_SIZE();
                auto e = _world->make_entity<character, false>(oid, {ch, local}, proto);
                (void)e;
                break;
            }
            case entity_type::scenery: {
                atlasid id; id << s;
                const bool exact = id & meta_short_scenery_bit;
                const auto r = rotation(id >> sizeof(id)*8-1-rotation_BITS & rotation_MASK);
                id &= ~scenery_id_flag_mask;
                auto sc = lookup_scenery(id);
                (void)sc.atlas->group(r);
                sc.r = r;
                if (!exact)
                {
                    if (read_entity_flags(s, sc))
                        sc.frame = s.read<std::uint8_t>();
                    else
                        sc.frame << s;
                    read_offsets(s, sc);
                    if (sc.active)
                        sc.delta << s;
                }
                auto e = _world->make_entity<scenery, false>(oid, {ch, local}, sc);
                (void)e;
                break;
            }
            default:
                fm_throw("invalid_entity_type '{}'"_cf, (int)type);
            }
        }

        SET_CHUNK_SIZE();
        fm_assert(c.is_scenery_modified());
        fm_assert(c.is_passability_modified());
        c.sort_entities();
        c.ensure_ground_mesh();
        c.ensure_wall_mesh();
        c.ensure_scenery_mesh();
        c.ensure_passability();
    }
}

void reader_state::read_old_scenery(reader_t& s, chunk_coords ch, std::size_t i)
{
    atlasid id; id << s;
    const bool exact = id & meta_short_scenery_bit;
    const auto r = rotation(id >> sizeof(id)*8-1-rotation_BITS & rotation_MASK);
    id &= ~scenery_id_flag_mask;
    auto sc = lookup_scenery(id);
    (void)sc.atlas->group(r);
    sc.r = r;
    if (!exact)
    {
        if (read_entity_flags(s, sc))
            sc.frame = s.read<std::uint8_t>();
        else
            sc.frame << s;
        if (PROTO >= 5) [[likely]]
        {
            sc.offset[0] << s;
            sc.offset[1] << s;
        }
        if (PROTO >= 6) [[likely]]
        {
            sc.bbox_size[0] << s;
            sc.bbox_size[1] << s;
        }
        if (PROTO >= 7) [[likely]]
        {
            sc.bbox_offset[0] << s;
            sc.bbox_offset[1] << s;
        }
        if (sc.active)
        {
            if (PROTO >= 4) [[likely]]
                sc.delta << s;
            else
                sc.delta = (std::uint16_t)Math::clamp(int(s.read<float>() * 65535), 0, 65535);
        }
    }
    global_coords coord{ch, local_coords{i}};
    auto e = _world->make_entity<scenery, false>(_world->make_id(), coord, sc);
    (void)e;
}

void reader_state::deserialize_world(ArrayView<const char> buf)
{
    fm_assert(_world != nullptr);
    auto s = binary_reader{buf};
    if (!!::memcmp(s.read<std::size(file_magic)-1>().data(), file_magic, std::size(file_magic)-1))
        fm_throw("bad magic"_cf);
    proto_t proto;
    proto << s;
    if (!(proto >= min_proto_version && proto <= proto_version))
        fm_throw("bad proto version '{}' (should be between '{}' and '{}')"_cf,
                 (std::size_t)proto, (std::size_t)min_proto_version, (std::size_t)proto_version);
    PROTO = proto;
    fm_assert(PROTO > 0);
    std::uint64_t entity_counter = 0;
    read_atlases(s);
    if (PROTO >= 3) [[likely]]
        read_sceneries(s);
    if (PROTO >= 8) [[likely]]
        entity_counter << s;
    read_chunks(s);
    s.assert_end();
    if (PROTO >= 8) [[likely]]
    {
        fm_assert(_world->entity_counter() == 0);
        _world->set_entity_counter(entity_counter);
    }
    _world = nullptr;
}

} // namespace

namespace floormat {

world world::deserialize(StringView filename)
{
    char errbuf[128];
    constexpr auto get_error_string = []<std::size_t N> (char (&buf)[N]) -> const char* {
        buf[0] = '\0';
#ifndef _WIN32
        (void)::strerror_r(errno, buf, std::size(buf));
#else
        (void)::strerror_s(buf, std::size(buf), errno);
#endif
        return buf;
    };
    fm_soft_assert(filename.flags() & StringViewFlag::NullTerminated);
    FILE_raii f = ::fopen(filename.data(), "rb");
    if (!f)
    {
        fm_throw("fopen(\"{}\", \"r\"): {}"_cf, filename.data(), get_error_string(errbuf));
    }
    if (int ret = ::fseek(f, 0, SEEK_END); ret != 0)
        fm_throw("fseek(SEEK_END): {}"_cf, get_error_string(errbuf));
    std::size_t len;
    if (auto len_ = ::ftell(f); len_ >= 0)
        len = (std::size_t)len_;
    else
        fm_throw("ftell: {}"_cf, get_error_string(errbuf));
    if (int ret = ::fseek(f, 0, SEEK_SET); ret != 0)
        fm_throw("fseek(SEEK_SET): {}"_cf, get_error_string(errbuf));
    auto buf_ = std::make_unique<char[]>(len);

    if (auto ret = ::fread(&buf_[0], 1, len, f); ret != len)
        fm_throw("fread short read: {}"_cf, get_error_string(errbuf));

    world w;
    reader_state s{w};
    s.deserialize_world({buf_.get(), len});
    return w;
}

} // namespace floormat