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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
#define FM_SERIALIZE_WORLD_IMPL
#include "world-impl.hpp"
#include "src/tile-atlas.hpp"
#include "binary-writer.inl"
#include "src/global-coords.hpp"
#include "src/chunk.hpp"
#include "src/world.hpp"
#include "src/emplacer.hpp"
#include "loader/loader.hpp"
#include "src/scenery.hpp"
#include "src/character.hpp"
#include "loader/scenery.hpp"
#include "src/anim-atlas.hpp"
#include <concepts>
#include <cstring>
#include <vector>
#include <algorithm>
#include <Corrade/Containers/StringView.h>
#include <Corrade/Utility/Path.h>
using namespace floormat;
using namespace floormat::Serialize;
namespace {
struct interned_atlas final {
const tile_atlas* img;
atlasid index;
};
struct interned_scenery {
const serialized_scenery* s;
atlasid index;
static_assert(sizeof index >= sizeof scenery::frame);
};
struct scenery_pair {
const serialized_scenery* s;
atlasid index;
bool exact_match;
};
struct writer_state final {
writer_state(const world& world);
ArrayView<const char> serialize_world();
fm_DECLARE_DEFAULT_MOVE_ASSIGNMENT_(writer_state);
fm_DECLARE_DEPRECATED_COPY_ASSIGNMENT(writer_state);
private:
atlasid intern_atlas(const tile_image_proto& img);
atlasid maybe_intern_atlas(const tile_image_proto& img);
scenery_pair intern_scenery(const scenery& sc, bool create);
void serialize_chunk(const chunk& c, chunk_coords coord);
void serialize_atlases();
void serialize_scenery();
void load_scenery_1(const serialized_scenery& s);
void load_scenery();
const world* _world;
std::vector<char> atlas_buf, scenery_buf, chunk_buf, file_buf;
std::vector<std::vector<char>> chunk_bufs;
std::unordered_map<const void*, interned_atlas> tile_images;
std::unordered_map<const void*, std::vector<interned_scenery>> scenery_map;
atlasid scenery_map_size = 0;
};
constexpr auto tile_size = sizeof(tilemeta) + (sizeof(atlasid) + sizeof(variant_t)) * 3 + sizeof(scenery);
constexpr auto chunkbuf_size = sizeof(chunk_magic) + sizeof(chunk_coords) + tile_size * TILE_COUNT;
constexpr auto entity_size = std::max(sizeof(character), sizeof(scenery)) + character_name_max;
#ifdef __GNUG__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
writer_state::writer_state(const world& world) : _world{&world}
{
chunk_buf.reserve(chunkbuf_size);
chunk_bufs.reserve(world.chunks().size());
atlas_buf.reserve(atlas_name_max * 64);
scenery_map.reserve(64);
}
#ifdef __GNUG__
#pragma GCC diagnostic pop
#elif defined _MSC_VER
#pragma warning(pop)
#endif
atlasid writer_state::intern_atlas(const tile_image_proto& img)
{
const void* const ptr = img.atlas.get();
fm_debug_assert(ptr != nullptr);
emplacer e{[&]() -> interned_atlas { return { &*img.atlas, (atlasid)tile_images.size() }; }};
return tile_images.try_emplace(ptr, e).first->second.index;
}
atlasid writer_state::maybe_intern_atlas(const tile_image_proto& img)
{
return img ? intern_atlas(img) : null_atlas;
}
void writer_state::load_scenery_1(const serialized_scenery& s)
{
const void* const ptr = s.proto.atlas.get();
fm_debug_assert(ptr != nullptr);
if (auto it = scenery_map.find(ptr); it == scenery_map.end())
scenery_map[ptr] = { { &s, null_atlas } };
else
{
auto& vec = scenery_map[ptr];
for (const auto& x : vec)
if (s.proto.frame == x.s->proto.frame)
return;
vec.push_back({ &s, null_atlas });
}
}
void writer_state::load_scenery()
{
for (const auto& s : loader.sceneries())
load_scenery_1(s);
}
scenery_pair writer_state::intern_scenery(const scenery& sc, bool create)
{
auto s = scenery_proto(sc);
const void* const ptr = s.atlas.get();
fm_debug_assert(ptr != nullptr);
auto it = scenery_map.find(ptr);
fm_assert(it != scenery_map.end() && !it->second.empty());
auto& vec = it->second;
interned_scenery *ret = nullptr, *ret2 = nullptr;
for (interned_scenery& x : vec)
{
const auto& proto = x.s->proto;
fm_assert(s.type == proto.type);
fm_assert(s.sc_type == proto.sc_type);
s.r = proto.r;
s.interactive = proto.interactive;
s.active = proto.active;
s.closing = proto.closing;
s.pass = proto.pass;
if (s == proto)
{
if (x.index != null_atlas)
return { x.s, x.index, true };
else
ret = &x;
}
if (x.index != null_atlas)
ret2 = &x;
}
if (ret)
{
ret->index = scenery_map_size++;
return { ret->s, ret->index, true };
}
else if (create)
{
if (ret2)
return { ret2->s, ret2->index, false };
else
{
fm_assert(vec[0].index == null_atlas);
return { vec[0].s, vec[0].index = scenery_map_size++, false };
}
}
else
return {};
}
template<typename T, entity_subtype U>
void write_entity_flags(binary_writer<T>& s, const U& e)
{
std::uint8_t flags = 0;
auto pass = (pass_mode_i)e.pass;
fm_assert((pass & pass_mask) == pass);
flags |= pass;
constexpr auto tag = entity_type_<U>::value;
if (e.type != tag)
fm_abort("invalid entity type '%d'", (int)e.type);
if constexpr(tag == entity_type::scenery)
{
flags |= (1 << 2) * e.active;
flags |= (1 << 3) * e.closing;
flags |= (1 << 4) * e.interactive;
}
else if constexpr(tag == entity_type::character)
{
flags |= (1 << 2) * e.playable;
}
else
static_assert(tag == entity_type::none);
flags |= (1 << 7) * (e.frame <= 0xff);
s << flags;
}
void writer_state::serialize_atlases()
{
fm_assert(tile_images.size() < int_max<atlasid>);
const auto sz = (atlasid)tile_images.size();
const auto atlasbuf_size = sizeof(sz) + atlas_name_max*sz;
atlas_buf.resize(atlasbuf_size);
auto s = binary_writer{atlas_buf.begin()};
fm_assert(sz <= int_max<atlasid>);
s << sz;
std::vector<interned_atlas> atlases;
atlases.reserve(tile_images.size());
for (const auto& [_, t] : tile_images)
atlases.push_back(t);
std::sort(atlases.begin(), atlases.end(), [](const auto& a, const auto& b) {
return a.index < b.index;
});
for (const auto& [atlas, _] : atlases)
{
const auto name = atlas->name();
const auto namesiz = name.size();
fm_debug_assert(s.bytes_written() + namesiz < atlasbuf_size);
fm_assert(namesiz < atlas_name_max);
fm_debug_assert(name.find('\0') == name.cend());
const auto sz2 = atlas->num_tiles2();
s << sz2[0]; s << sz2[1];
s.write_asciiz_string(name);
}
atlas_buf.resize(s.bytes_written());
fm_assert(s.bytes_written() <= atlasbuf_size);
}
constexpr auto atlasbuf_size0 = sizeof(atlasid) + sizeof(scenery);
constexpr auto atlasbuf_size1 = sizeof(std::uint8_t) + atlasbuf_size0*int_max<std::uint8_t> + atlas_name_max;
void writer_state::serialize_scenery()
{
fm_assert(scenery_map_size < scenery_id_max);
const std::size_t sz = scenery_map_size;
std::vector<interned_scenery> vec; vec.reserve(scenery_map_size);
for (const auto& x : scenery_map)
for (const auto& s : x.second)
if (s.index != null_atlas)
vec.push_back(s);
fm_assert(sz == vec.size());
std::sort(vec.begin(), vec.end(), [](const interned_scenery& a, const interned_scenery& b) {
auto cmp = a.s->name <=> b.s->name;
if (cmp == std::strong_ordering::equal)
return a.index < b.index;
else
return cmp == std::strong_ordering::less;
});
const auto atlasbuf_size = sizeof(std::uint16_t) + sizeof(sz) + atlasbuf_size1*sz;
scenery_buf.resize(atlasbuf_size);
auto s = binary_writer{scenery_buf.begin()};
s << std::uint16_t{scenery_magic};
fm_assert(sz < scenery_id_max);
s << (atlasid)sz;
StringView last;
for (auto i = 0_uz; i < sz; i++)
{
fm_debug_assert(s.bytes_written() + atlasbuf_size1 < atlasbuf_size);
const auto& [sc, idx] = vec[i];
if (sc->name != last)
{
fm_assert(sc->name.size() < atlas_name_max);
last = sc->name;
auto num = 1_uz;
for (auto j = i+1; j < sz && vec[j].s->name == sc->name; j++)
num++;
fm_assert(num < int_max<std::uint8_t>);
s << (std::uint8_t)num;
s.write_asciiz_string(sc->name);
}
s << idx;
write_entity_flags(s, sc->proto);
if (sc->proto.frame <= 0xff)
s << (std::uint8_t)sc->proto.frame;
else
s << sc->proto.frame;
}
scenery_buf.resize(s.bytes_written());
}
const auto def_char_bbox_size = character_proto{}.bbox_size;
const auto def_char_pass = character_proto{}.pass;
void writer_state::serialize_chunk(const chunk& c, chunk_coords coord)
{
fm_assert(chunk_buf.empty());
const auto es_size = sizeof(std::uint32_t) + entity_size*c.entities().size();
chunk_buf.resize(chunkbuf_size + es_size);
auto s = binary_writer{chunk_buf.begin()};
s << chunk_magic << coord.x << coord.y;
for (auto i = 0_uz; i < TILE_COUNT; i++)
{
const tile_proto x = c[i];
const auto ground = x.ground(), wall_north = x.wall_north(), wall_west = x.wall_west();
//const auto scenery = x.scenery_frame;
fm_debug_assert(s.bytes_written() + tile_size <= chunkbuf_size);
auto img_g = maybe_intern_atlas(ground);
auto img_n = maybe_intern_atlas(wall_north);
auto img_w = maybe_intern_atlas(wall_west);
tilemeta flags = {};
flags |= meta_ground * (img_g != null_atlas);
flags |= meta_wall_n * (img_n != null_atlas);
flags |= meta_wall_w * (img_w != null_atlas);
s << flags;
constexpr auto check_atlas = [](const tile_image_proto& x) {
fm_assert(!x.atlas || x.variant < x.atlas->num_tiles());
};
check_atlas(ground);
check_atlas(wall_north);
check_atlas(wall_west);
if (img_g != null_atlas)
{
s << img_g;
s << ground.variant;
}
if (img_n != null_atlas)
{
s << img_n;
s << wall_north.variant;
}
if (img_w != null_atlas)
{
s << img_w;
s << wall_west.variant;
}
}
const auto entity_count = (std::uint32_t)c.entities().size();
s << entity_count;
fm_assert(entity_count == c.entities().size());
for (const auto& e_ : c.entities())
{
const auto& e = *e_;
std::uint64_t oid = e.id;
fm_assert((oid & ((std::uint64_t)1 << 60)-1) == oid);
static_assert(entity_type_BITS == 3);
fm_assert(((entity_type_i)e.type & (1 << entity_type_BITS)-1) == (entity_type_i)e.type);
oid |= (std::uint64_t)e.type << 64 - entity_type_BITS;
s << oid;
const auto local = e.coord.local();
s << local.to_index();
constexpr auto write_offsets = [](auto& s, const 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];
};
switch (e.type)
{
default:
fm_abort("invalid entity type '%d'", (int)e.type);
case entity_type::character: {
const auto& C = static_cast<const character&>(e);
std::uint8_t id = 0;
const auto sc_exact =
C.offset.isZero() && C.bbox_offset.isZero() &&
C.bbox_size == def_char_bbox_size;
id |= meta_short_scenery_bit * sc_exact;
id |= static_cast<decltype(id)>(C.r) << sizeof(id)*8-1-rotation_BITS;
s << id;
write_entity_flags(s, C);
if (C.frame <= 0xff)
s << (std::uint8_t)C.frame;
else
s << C.frame;
s << C.offset_frac[0];
s << C.offset_frac[1];
fm_assert(C.name.size() < character_name_max);
s.write_asciiz_string(C.name);
if (!sc_exact)
write_offsets(s, C);
break;
}
case entity_type::scenery: {
const auto& sc = static_cast<const scenery&>(e);
auto [ss, img_s, sc_exact] = intern_scenery(sc, true);
sc_exact = sc_exact &&
e.offset.isZero() && e.bbox_offset.isZero() && e.bbox_size.isZero() &&
!sc.active && !sc.closing && !sc.interactive;
fm_assert(img_s != null_atlas);
atlasid id = img_s;
static_assert(rotation_BITS == 3);
fm_assert((id & (1 << 16-3-1)-1) == id);
id |= meta_short_scenery_bit * sc_exact;
id |= static_cast<decltype(id)>(sc.r) << sizeof(id)*8-1-rotation_BITS;
s << id;
if (!sc_exact)
{
write_entity_flags(s, sc);
fm_assert(sc.active || sc.delta == 0);
if (sc.frame <= 0xff)
s << (std::uint8_t)sc.frame;
else
s << sc.frame;
write_offsets(s, sc);
if (sc.active)
s << sc.delta;
}
break;
}
}
}
const auto nbytes = s.bytes_written();
fm_assert(nbytes <= chunkbuf_size);
chunk_buf.resize(nbytes);
chunk_bufs.push_back(std::move(chunk_buf));
chunk_buf.clear();
}
#ifdef __GNUG__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#endif
ArrayView<const char> writer_state::serialize_world()
{
fm_assert(_world != nullptr);
load_scenery();
for (const auto& [_, c] : _world->chunks())
{
for (const auto& e_ : c.entities())
{
const auto& e = *e_;
switch (e.type)
{
case entity_type::scenery:
intern_scenery(static_cast<const scenery&>(e), false);
break;
case entity_type::character:
break;
default:
fm_abort("invalid scenery type '%d'", (int)e.type);
}
}
}
for (const auto& [pos, c] : _world->chunks())
{
#ifndef FM_NO_DEBUG
if (c.empty(true))
fm_warn("chunk %hd:%hd is empty", pos.x, pos.y);
#endif
serialize_chunk(c, pos);
}
serialize_atlases();
serialize_scenery();
using proto_t = std::decay_t<decltype(proto_version)>;
fm_assert(_world->size() <= int_max<chunksiz>);
const auto len = fm_begin(
auto len = 0_uz;
len += std::size(file_magic)-1;
len += sizeof(proto_t);
len += atlas_buf.size();
len += scenery_buf.size();
len += sizeof(std::uint64_t);
len += sizeof(chunksiz);
for (const auto& buf : chunk_bufs)
len += buf.size();
return len;
);
file_buf.resize(len);
auto bytes_written = 0_uz;
auto it = file_buf.begin();
const auto copy = [&](const auto& in) {
auto len1 = std::distance(std::cbegin(in), std::cend(in)),
len2 = std::distance(it, file_buf.end());
fm_assert(len1 <= len2);
it = std::copy(std::cbegin(in), std::cend(in), it);
bytes_written += (std::size_t)len1;
};
const auto copy_int = [&]<typename T>(const T& value) {
union { T x; char bytes[sizeof x]; } c = {.x = maybe_byteswap(value)};
copy(c.bytes);
};
copy(Containers::StringView{file_magic, std::size(file_magic)-1});
copy_int((proto_t)proto_version);
copy(atlas_buf);
copy(scenery_buf);
copy_int((std::uint64_t)_world->entity_counter());
copy_int((chunksiz)_world->size());
for (const auto& buf : chunk_bufs)
copy(buf);
fm_assert(file_buf.size() == bytes_written);
fm_assert(len == bytes_written);
_world = nullptr;
return {file_buf.data(), file_buf.size()};
}
#ifdef __GNUG__
#pragma GCC diagnostic pop
#elif defined _MSC_VER
#pragma warning(pop)
#endif
} // namespace
namespace floormat {
void world::serialize(StringView filename)
{
collect(true);
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_assert(filename.flags() & StringViewFlag::NullTerminated);
if (Path::exists(filename))
Path::remove(filename);
FILE_raii file = ::fopen(filename.data(), "wb");
if (!file)
fm_abort("fopen(\"%s\", \"w\"): %s", filename.data(), get_error_string(errbuf));
writer_state s{*this};
const auto array = s.serialize_world();
if (auto len = ::fwrite(array.data(), array.size(), 1, file); len != 1)
fm_abort("fwrite: %s", get_error_string(errbuf));
if (int ret = ::fflush(file); ret != 0)
fm_abort("fflush: %s", get_error_string(errbuf));
}
} // namespace floormat
|