Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
EntityChangeJournal.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4#include "../core/Types.hpp"
6#include <algorithm>
7#include <array>
8#include <cmath>
9#include <span>
10#include <unordered_set>
11#include <vector>
12
13namespace esengine::ecs {
15static constexpr u32 ENTITY_CHANGE_WORDS = 3;
17static constexpr u32 ENTITY_CHANGE_NO_COMPONENT = 4294967295;
19static constexpr u32 ENTITY_CHANGE_CAPACITY = 4096;
21static constexpr u32 ENTITY_CHANGE_SUBTREE = 1;
23static constexpr u32 ENTITY_CHANGE_RESET = 2;
25static constexpr u32 ENTITY_CHANGE_REPLACED = 4;
26
27// Component IDs belong to this host's type domain, never to persisted data.
28// Entity-level events carry ENTITY_CHANGE_NO_COMPONENT.
30static_assert(sizeof(EntityChangeRow) == ENTITY_CHANGE_WORDS * sizeof(u32));
31
37public:
39 if (readers_.empty()) {
40 rows_.resize(ENTITY_CHANGE_CAPACITY);
41 sequence_ = 0;
42 exhausted_ = false;
43 }
44 do { ++next_reader_; } while (!next_reader_ || readers_.contains(next_reader_));
45 readers_.insert(next_reader_);
46 return next_reader_;
47 }
48 void unwatch(u32 reader) {
49 readers_.erase(reader);
50 if (readers_.empty()) std::vector<EntityChangeRow>().swap(rows_);
51 }
52 double sequence() const { return static_cast<double>(sequence_); }
53 double resetEpoch() const { return reset_epoch_ == MAX_SEQUENCE ? -1 : static_cast<double>(reset_epoch_); }
54 double componentRevision(u32 component, bool topology) const {
55 const auto value = component == ENTITY_CHANGE_NO_COMPONENT ? entity_revisions_[topology ? 1 : 0]
56 : component < revisions_.size() ? revisions_[component][topology ? 1 : 0] : 0;
57 return value == MAX_SEQUENCE ? -1 : static_cast<double>(value);
58 }
59 usize readerCount() const { return readers_.size(); }
60 usize retainedRows() const { return rows_.empty() ? 0 : std::min<u64>(sequence_, ENTITY_CHANGE_CAPACITY); }
61
62 void record(u32 entity, u32 flags = 0, u32 component = ENTITY_CHANGE_NO_COMPONENT) {
63 if (revision_ < MAX_SEQUENCE) ++revision_;
64 if (component != ENTITY_CHANGE_NO_COMPONENT && component >= revisions_.size()) revisions_.resize(component + 1);
65 auto& revisions = component == ENTITY_CHANGE_NO_COMPONENT ? entity_revisions_ : revisions_[component];
66 revisions[0] = revision_;
67 if (!(flags & ENTITY_CHANGE_REPLACED)) revisions[1] = revision_;
68 if ((flags & ENTITY_CHANGE_RESET) && reset_epoch_ < MAX_SEQUENCE) ++reset_epoch_;
69 if (readers_.empty()) return;
70 // Saturation preserves the JS number contract by requiring a rescan.
71 if (sequence_ == MAX_SEQUENCE) { exhausted_ = true; return; }
72 ++sequence_;
73 rows_[sequence_ % ENTITY_CHANGE_CAPACITY] = {entity, flags, component};
74 }
75
76 i32 read(double after, std::span<EntityChangeRow> output) const {
77 if (rows_.empty() || exhausted_ || !std::isfinite(after) || after < 0
78 || after > sequence() || std::floor(after) != after) return -1;
79 const auto cursor = static_cast<u64>(after);
80 const auto count = sequence_ - cursor;
81 if (count > ENTITY_CHANGE_CAPACITY || count > output.size()) return -1;
82 for (u64 i = 0; i < count; ++i) output[i] = rows_[(cursor + i + 1) % ENTITY_CHANGE_CAPACITY];
83 return static_cast<i32>(count);
84 }
85
86private:
87 static constexpr u64 MAX_SEQUENCE = (u64{1} << 53) - 1;
88 std::vector<EntityChangeRow> rows_;
89 std::unordered_set<u32> readers_;
90 u64 sequence_ = 0;
91 u64 reset_epoch_ = 0;
92 u64 revision_ = 0;
93 std::vector<std::array<u64, 2>> revisions_;
94 std::array<u64, 2> entity_revisions_{};
95 u32 next_reader_ = 0;
96 bool exhausted_ = false;
97};
98} // namespace esengine::ecs
Reflection and metadata system for automatic binding generation.
#define ES_CONST(...)
Mark a constant that CROSSES the boundary, so EHT emits its TS twin.
Core type definitions and utilities for ESEngine.
i32 read(double after, std::span< EntityChangeRow > output) const
double componentRevision(u32 component, bool topology) const
void record(u32 entity, u32 flags=0, u32 component=ENTITY_CHANGE_NO_COMPONENT)
static constexpr u32 ENTITY_CHANGE_SUBTREE
static constexpr u32 ENTITY_CHANGE_REPLACED
static constexpr u32 ENTITY_CHANGE_WORDS
static constexpr u32 ENTITY_CHANGE_CAPACITY
static constexpr u32 ENTITY_CHANGE_RESET
static constexpr u32 ENTITY_CHANGE_NO_COMPONENT
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45