diff options
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/pass-mode.cpp | 44 | ||||
-rw-r--r-- | serialize/pass-mode.hpp | 13 | ||||
-rw-r--r-- | serialize/scenery.cpp | 13 | ||||
-rw-r--r-- | serialize/world-impl.hpp | 3 | ||||
-rw-r--r-- | serialize/world-reader.cpp | 15 | ||||
-rw-r--r-- | serialize/world-writer.cpp | 7 |
6 files changed, 75 insertions, 20 deletions
diff --git a/serialize/pass-mode.cpp b/serialize/pass-mode.cpp new file mode 100644 index 00000000..6d52a5de --- /dev/null +++ b/serialize/pass-mode.cpp @@ -0,0 +1,44 @@ +#include "pass-mode.hpp" +#include "compat/exception.hpp" +#include "serialize/corrade-string.hpp" +#include <nlohmann/json.hpp> +#include <Corrade/Containers/StringStlView.h> + +namespace nlohmann { + +using namespace floormat; + +static constexpr struct { + pass_mode mode; + StringView str; +} table[] = { + { pass_mode::shoot_through, "shoot-through"_s }, + { pass_mode::pass, "pass"_s }, + { pass_mode::blocked, "blocked"_s }, + { pass_mode::see_through, "see-through"_s }, +}; + +void adl_serializer<pass_mode>::to_json(json& j, pass_mode val) +{ + for (const auto [mode, str] : table) + if (mode == val) + { + j = str; + return; + } + fm_throw("invalid pass mode '{}'"_cf, std::size_t(val)); +} + +void adl_serializer<pass_mode>::from_json(const json& j, pass_mode& val) +{ + StringView value = j; + for (const auto [mode, str] : table) + if (str == value) + { + val = mode; + return; + } + fm_throw("invalid pass mode '{}'"_cf, value); +} + +} // namespace nlohmann diff --git a/serialize/pass-mode.hpp b/serialize/pass-mode.hpp new file mode 100644 index 00000000..a3e1cd66 --- /dev/null +++ b/serialize/pass-mode.hpp @@ -0,0 +1,13 @@ +#pragma once +#include "src/pass-mode.hpp" +#include <nlohmann/json_fwd.hpp> + +namespace nlohmann { + +template<> +struct adl_serializer<floormat::pass_mode> { + static void to_json(json& j, floormat::pass_mode val); + static void from_json(const json& j, floormat::pass_mode& val); +}; + +} // namespace nlohmann diff --git a/serialize/scenery.cpp b/serialize/scenery.cpp index 7b402945..ccc80270 100644 --- a/serialize/scenery.cpp +++ b/serialize/scenery.cpp @@ -4,6 +4,7 @@ #include "loader/loader.hpp" #include "serialize/corrade-string.hpp" #include "loader/scenery.hpp" +#include "serialize/pass-mode.hpp" #include <Corrade/Containers/StringStlView.h> #include <nlohmann/json.hpp> @@ -85,8 +86,7 @@ void adl_serializer<scenery_proto>::to_json(json& j, const scenery_proto& val) j["atlas-name"] = val.atlas->name(); j["frame"] = f.frame; j["rotation"] = f.r; - j["passable"] = f.passable; - j["blocks-view"] = f.blocks_view; + j["pass-mode"] = f.passability; j["active"] = f.active; j["interactive"] = f.interactive; } @@ -108,17 +108,16 @@ void adl_serializer<scenery_proto>::from_json(const json& j, scenery_proto& val) auto type = scenery_type::generic; get("type", type); auto r = val.atlas->first_rotation(); get("rotation", r); - auto frame = f.frame; get("frame", frame); - bool passable = f.passable; get("passable", passable); - bool blocks_view = f.blocks_view; get("blocks-view", blocks_view); - bool active = f.active; get("active", active); + auto frame = f.frame; get("frame", frame); + pass_mode pass = f.passability; get("pass-mode", pass); + bool active = f.active; get("active", active); switch (type) { default: fm_abort("unhandled scenery type '%u'", (unsigned)type); case scenery_type::generic: - f = { scenery::generic, *val.atlas, r, frame, passable, blocks_view, active }; + f = { scenery::generic, *val.atlas, r, frame, pass, active }; break; case scenery_type::door: f = { scenery::door, *val.atlas, r, false }; diff --git a/serialize/world-impl.hpp b/serialize/world-impl.hpp index ea2a9cde..99db4cc0 100644 --- a/serialize/world-impl.hpp +++ b/serialize/world-impl.hpp @@ -38,7 +38,8 @@ constexpr inline proto_t min_proto_version = 1; constexpr inline auto chunk_magic = (std::uint16_t)~0xc0d3; constexpr inline auto scenery_magic = (std::uint16_t)~0xb00b; -constexpr inline std::underlying_type_t<pass_mode> pass_mask = pass_blocked | pass_shoot_through | pass_ok; +using pass_mode_ = std::underlying_type_t<pass_mode>; +constexpr inline pass_mode_ pass_mask = pass_mode_COUNT - 1; constexpr inline auto pass_bits = std::bit_width(pass_mask); template<typename T> constexpr inline auto highbit = T(1) << sizeof(T)*8-1; diff --git a/serialize/world-reader.cpp b/serialize/world-reader.cpp index b6cf8839..b0e14254 100644 --- a/serialize/world-reader.cpp +++ b/serialize/world-reader.cpp @@ -53,8 +53,7 @@ template<typename T> bool read_scenery_flags(binary_reader<T>& s, scenery& sc) { std::uint8_t flags; flags << s; - sc.passable = !!(flags & 1 << 0); - sc.blocks_view = !!(flags & 1 << 1); + sc.passability = pass_mode(flags & pass_mask); sc.active = !!(flags & 1 << 2); sc.closing = !!(flags & 1 << 3); sc.interactive = !!(flags & 1 << 4); @@ -148,7 +147,7 @@ void reader_state::read_chunks(reader_t& s) return { atlas, v }; }; - t.pass_mode() = pass_mode(flags & pass_mask); + t.passability() = pass_mode(flags & pass_mask); if (flags & meta_ground) t.ground() = make_atlas(); if (flags & meta_wall_n) @@ -179,13 +178,13 @@ void reader_state::read_chunks(reader_t& s) switch (auto x = pass_mode(flags & pass_mask)) { - case pass_shoot_through: - case pass_blocked: - case pass_ok: - t.pass_mode() = x; + case pass_mode::shoot_through: + case pass_mode::blocked: + case pass_mode::pass: + t.passability() = x; break; default: [[unlikely]] - fm_throw("bad pass mode '{}' for tile {}"_cf, i, x); + fm_throw("bad pass mode '{}' for tile {}"_cf, i, pass_mode_(x)); } } } diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index 7cd4b6d9..5ff0a39c 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -180,8 +180,7 @@ template<typename T> void write_scenery_flags(binary_writer<T>& s, const scenery& proto) { std::uint8_t flags = 0; - flags |= (1 << 0) * proto.passable; - flags |= (1 << 1) * proto.blocks_view; + flags |= pass_mode_(proto.passability) & pass_mask; flags |= (1 << 2) * proto.active; flags |= (1 << 3) * proto.closing; flags |= (1 << 4) * proto.interactive; @@ -320,8 +319,8 @@ void writer_state::serialize_chunk(const chunk& c, chunk_coords coord) if (flags != 0 && ashortp(img_g) && ashortp(img_n) && ashortp(img_w)) flags |= meta_short_atlasid; - fm_debug_assert((x.pass_mode & pass_mask) == x.pass_mode); - flags |= x.pass_mode; + fm_debug_assert((pass_mode_(x.passability) & pass_mask) == pass_mode_(x.passability)); + flags |= pass_mode_(x.passability); s << flags; |