14 : m_scheduler(scheduler)
15 , m_event_manager(event_manager)
16 , m_index(Kinesis::make_spatial_index_3d(cell_size))
23 if (!reg.task_name.empty()) {
26 if (!reg.chain_name.empty()) {
29 if (!reg.event_name.empty()) {
44 if (emitter->m_position.has_value()) {
45 reg.spatial_id =
m_index->insert(*emitter->m_position);
48 return Wiring { *
this,
id };
56 if (sensor->m_position.has_value()) {
57 reg.spatial_id =
m_index->insert(*sensor->m_position);
60 return Wiring { *
this,
id };
68 if (agent->m_position.has_value()) {
69 reg.spatial_id =
m_index->insert(*agent->m_position);
72 return Wiring { *
this,
id };
82 auto& reg = it->second;
84 if (!reg.task_name.empty()) {
87 if (!reg.chain_name.empty()) {
90 if (!reg.event_name.empty()) {
93 if (reg.spatial_id.has_value()) {
94 m_index->remove(*reg.spatial_id);
104 m_expanses.try_emplace(
id, std::move(expanse));
115 std::vector<uint32_t> ids;
126 return std::visit([](
const auto&
ptr) ->
Kind {
127 using T = std::decay_t<
decltype(*ptr)>;
128 if constexpr (std::is_same_v<T, Emitter>) {
130 }
else if constexpr (std::is_same_v<T, Sensor>) {
144 if (
auto*
ptr = std::get_if<std::shared_ptr<Emitter>>(&it->second.member)) {
155 if (
auto*
ptr = std::get_if<std::shared_ptr<Sensor>>(&it->second.member)) {
166 if (
auto*
ptr = std::get_if<std::shared_ptr<Agent>>(&it->second.member)) {
179 if (reg.spatial_id.has_value()) {
180 std::visit([&](
const auto&
ptr) {
181 if (
ptr->m_position.has_value()) {
182 m_index->update(*reg.spatial_id, *ptr->m_position);
192 const auto snapshot =
m_index->all();
198 if (reg.commit_driven) {
209 "Fabric::fire: id {} not registered",
id);
213 it->second.pending_cursor.reset();
221 const glm::vec3& center,
float radius)
const
223 return m_index->within_radius(center, radius);
227 const glm::vec3& center, uint32_t k)
const
229 return m_index->k_nearest(center, k);
238 return &*it->second.wiring;
248 std::visit([
id](
const auto&
ptr) {
257 std::visit([&](
const auto&
ptr) {
258 using T = std::decay_t<
decltype(*ptr)>;
260 if constexpr (std::is_same_v<T, Emitter>) {
262 if (
ptr->m_position.has_value()) {
273 }
else if constexpr (std::is_same_v<T, Sensor>) {
275 if (
ptr->m_position.has_value()) {
277 auto results =
m_index->within_radius(*
ptr->m_position,
ptr->m_query_radius);
282 }
else if constexpr (std::is_same_v<T, Agent>) {
283 if (
ptr->m_position.has_value()) {
284 auto results =
m_index->within_radius(*
ptr->m_position,
ptr->m_query_radius);
288 ptr->invoke_perception(pctx);
297 ptr->invoke_influence(ictx);
305 ptr->invoke_influence(ictx);
338 std::vector<uint32_t> ids;
348 return it !=
m_expanses.end() ? it->second :
nullptr;
#define MF_WARN(comp, ctx,...)
std::vector< Kinesis::QueryResult > k_nearest(const glm::vec3 ¢er, uint32_t k) const
Find the k nearest objects to a point.
std::shared_ptr< Sensor > get_sensor(uint32_t id) const
Get the Sensor registered under id.
std::vector< uint32_t > all_ids() const
List all registered entity ids in insertion order.
void remove_expanse(uint32_t id)
Remove a registered Expanse.
std::unordered_map< std::string, std::shared_ptr< Expanse::CrossingFn > > m_crossing_fns
std::unordered_map< uint32_t, std::shared_ptr< Expanse > > m_expanses
std::shared_ptr< Emitter::InfluenceFn > resolve_influence_fn(std::string_view name) const
Look up a registered influence function by name.
std::shared_ptr< Presence::RadiateFn > resolve_radiate_fn(std::string_view name) const
Look up a registered radiation function by name.
std::shared_ptr< Expanse::CrossingFn > resolve_crossing_fn(std::string_view name) const
Look up a registered Expanse crossing function by name.
std::unique_ptr< Kinesis::SpatialIndex3D > m_index
std::unordered_map< std::string, std::shared_ptr< Expanse::ContainsFn > > m_expanse_fns
std::variant< std::shared_ptr< Emitter >, std::shared_ptr< Sensor >, std::shared_ptr< Agent > > Member
void fire(uint32_t id)
Fire a single object against the current snapshot without republishing.
void remove(uint32_t id)
Remove a registered object by id, cancelling any associated tasks.
std::shared_ptr< Expanse > get_expanse(uint32_t id) const
Look up a registered Expanse by id.
std::unordered_map< std::string, std::shared_ptr< Presence::RadiateFn > > m_radiate_fns
Fabric(Vruta::TaskScheduler &scheduler, Vruta::EventManager &event_manager, float cell_size=1.0F)
Construct with scheduler and event manager from the engine.
std::unordered_map< std::string, std::shared_ptr< Sensor::PerceptionFn > > m_perception_fns
std::shared_ptr< Sensor::PerceptionFn > resolve_perception_fn(std::string_view name) const
Look up a registered perception function by name.
uint32_t add_expanse(std::shared_ptr< Expanse > expanse)
Register an Expanse for per-commit crossing detection.
std::shared_ptr< Agent > get_agent(uint32_t id) const
Get the Agent registered under id.
const Wiring * wiring_for(uint32_t id) const
Access the finalised wiring for an entity for introspection.
const std::string & name() const
Assigned name, empty if the Fabric was constructed outside a Tapestry.
Kind kind(uint32_t id) const
Return the kind of entity registered under id.
std::vector< uint32_t > all_expanse_ids() const
Stable ids of all registered Expanses in insertion order.
uint32_t assign_id(Member &m)
Vruta::TaskScheduler & m_scheduler
uint32_t id() const
Stable id for this Fabric, assigned by Tapestry at construction.
std::vector< Kinesis::QueryResult > within_radius(const glm::vec3 ¢er, float radius) const
Find all objects within a radius of a point.
std::shared_ptr< Emitter > get_emitter(uint32_t id) const
Get the Emitter registered under id.
Wiring wire(std::shared_ptr< Emitter > emitter)
Begin wiring an Emitter into the Fabric.
std::unordered_map< uint32_t, Registration > m_registrations
void commit()
Update all positions, publish the snapshot, fire all registered objects.
std::unordered_map< std::string, std::shared_ptr< Emitter::InfluenceFn > > m_influence_fns
Vruta::EventManager & m_event_manager
std::shared_ptr< Expanse::ContainsFn > resolve_expanse_fn(std::string_view name) const
Look up a registered Expanse containment function by name.
Fluent builder that wires an entity into Fabric's scheduling infrastructure.
bool cancel_event(const std::shared_ptr< Event > &event)
Cancels and removes a event from the manager.
bool cancel_task(const std::shared_ptr< Routine > &routine)
Cancels and removes a task from the scheduler.
Token-based multimodal task scheduling system for unified coroutine processing.
@ Runtime
General runtime operations (default fallback)
@ Nexus
Spatial indexing and scheduling for user-defined behaviour.
std::optional< glm::vec2 > pending_cursor
std::weak_ptr< Buffers::RenderProcessor > render_proc
glm::vec3 position
Position of the influence point in world space.
std::optional< glm::vec2 > cursor_pos
Optional cursor position for pending interactions, useful for real-time editing or previewing influen...
std::optional< glm::vec3 > color
Optional color hint for the influence, may be used for visualisation or shader effects.
std::optional< float > size
Optional size hint for the influence, may be used for visualisation or shader effects.
float intensity
Intensity of the influence, typically in the range [0, 1], but may exceed 1 for strong influences.
float radius
Radius of influence around the position, defining the area of effect for spatially-dependent influenc...
Data passed to an Emitter or Agent influence function on each commit.
std::span< const Kinesis::QueryResult > spatial_results
Data passed to a Sensor or Agent perception function on each commit.