summaryrefslogtreecommitdiffhomepage
path: root/test/serializer.cpp
blob: 928fb25b2c5970db532f39c666c90f9c9bae248d (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
#include "app.hpp"
#include "src/world.hpp"
#include "loader/loader.hpp"
#include "src/scenery.hpp"
#include "src/character.hpp"
#include "src/tile-atlas.hpp"
#include "src/anim-atlas.hpp"
#include <Corrade/Utility/Path.h>

namespace floormat {

namespace Path = Corrade::Utility::Path;

chunk& test_app::make_test_chunk(chunk_coords ch)
{
    chunk& c = w[ch];
    c.mark_modified();
    auto metal1 = loader.tile_atlas("metal1", {2, 2}, pass_mode::pass),
         metal2 = loader.tile_atlas("metal2", {2, 2}, pass_mode::blocked),
         tiles  = loader.tile_atlas("tiles", {8, 5}, pass_mode::pass);
    constexpr auto N = TILE_MAX_DIM;
    for (auto [x, k, pt] : c)
        x.ground() = { tiles, variant_t(k % tiles->num_tiles()) };
    auto door = loader.scenery("door1"),
         table = loader.scenery("table1"),
         control_panel = loader.scenery("control panel (wall) 1");
    control_panel.r = rotation::W;
    constexpr auto K = N/2;
    c[{K,   K  }].wall_north() = { metal1, 0 };
    c[{K,   K  }].wall_west()  = { metal2, 0 };
    c[{K,   K+1}].wall_north() = { metal1, 0 };
    c[{K+1, K  }].wall_west()  = { metal2, 0 };
    w.make_entity<scenery>(w.make_id(), {ch, {3, 4}}, table);
    w.make_entity<scenery>(w.make_id(), {ch, {K, K+1}}, control_panel);
    {
        auto& e = *w.make_entity<scenery>(w.make_id(), {ch, {K+3, K+1}}, door);
        auto i = e.index();
        e.activate(i);
        e.update(i, 1.f/60);
        fm_assert(e.active);
        fm_assert(e.frame != 0 && e.frame != e.atlas->info().nframes - 1);
    }
    return c;
}

static void assert_chunks_equal(const chunk& a, const chunk& b)
{
    fm_assert(a.entities().size() == b.entities().size());

    for (auto i = 0_uz; i < TILE_COUNT; i++)
    {
        const auto &a1 = a[i], &b1 = b[i];
        fm_assert(a1 == b1);
    }

    for (auto i = 0_uz; i < a.entities().size(); i++)
    {
        const auto& ae = *a.entities()[i];
        const auto& be = *b.entities()[i];
        fm_assert(ae.type == be.type);
        switch (ae.type)
        {
        case entity_type::character: {
            const auto& e1 = static_cast<const character&>(ae);
            const auto& e2 = static_cast<const character&>(be);
            const auto p1 = character_proto(e1), p2 = character_proto(e2);
            fm_assert(p1 == p2);
            break;
        }
        case entity_type::scenery: {
            const auto& e1 = static_cast<const scenery&>(ae);
            const auto& e2 = static_cast<const scenery&>(be);
            const auto p1 = scenery_proto(e1), p2 = scenery_proto(e2);
            fm_assert(p1 == p2);
            break;
        }
        default:
            fm_abort("invalid entity type '%d'", (int)ae.type);
        }
    }
}

void test_app::test_serializer()
{
    constexpr auto filename = "../test/test-serializer1.dat";
    if (Path::exists(filename))
        Path::remove(filename);
    const chunk_coords coord{1, 1};
    auto& c = make_test_chunk(coord);
    w.serialize(filename);
    auto w2 = world::deserialize(filename);
    auto& c2 = w2[coord];
    assert_chunks_equal(c, c2);
}

} // namespace floormat