tess 1.0.0
Performance-first tile and path simulation substrate
Loading...
Searching...
No Matches
scheduler.h
1#pragma once
2
3#include <tess/ops/queued.h>
4#include <tess/sim/path_agent_tick.h>
5#include <tess/sim/render_delta.h>
6
7#include <cstddef>
8#include <cstdint>
9#include <span>
10#include <utility>
11#include <vector>
12
13namespace tess {
14
17 PathAgentTickState path_agents{};
18};
19
22 DirtyMask pathing_dirty_mask{};
23 DirtyMask render_dirty_mask{};
24 PathAgentTickOptions path_agent_options{};
25 bool clear_render_dirty = false;
26};
27
30 std::uint64_t tick = 0;
31 bool planned_ops = false;
32 bool executed_ops = false;
33 ExecutionReport op_report{};
34 PlannedExecutionResult op_execution{};
35 PathAgentTickStats path_agents{};
36 std::size_t render_delta_count = 0;
37};
38
40template <typename World, WritePolicy Policy, typename Fn>
41auto run_queued_operations(World& world, const OperationBatch& ops, Fn&& fn)
44 stats.op_report = plan_operations(world, ops);
45 stats.planned_ops = !ops.empty();
46 if (!stats.op_report.ok()) {
47 return stats;
48 }
49 stats.op_execution = execute_plan<Policy>(world, stats.op_report.plan(), fn);
50 stats.executed_ops =
51 stats.op_execution.status == PlannedExecutionStatus::Executed;
52 return stats;
53}
54
55namespace detail {
56
57// Shared tick sequence for all scheduler variants: run queued operations,
58// mark pathing dirty when planned work dirtied configured pathing fields,
59// run the variant-specific path-agent tick, then collect (and optionally
60// clear) render deltas. `path_tick` returns the variant's
61// `PathAgentTickStats`.
62template <typename World, WritePolicy Policy, typename Fn, typename PathTick>
63auto tick_scheduler_core(SimSchedulerState& state, World& world,
64 const OperationBatch& ops,
65 std::vector<RenderTileDelta>& render_deltas, Fn&& fn,
66 const SimSchedulerOptions& options,
67 PathTick&& path_tick) -> SimSchedulerStats {
68 auto stats =
69 run_queued_operations<World, Policy>(world, ops, std::forward<Fn>(fn));
70 // A plan can abort partway (e.g. PolicyMismatch): operations that
71 // already executed have applied their world writes, and execute_plan
72 // reports their chunks in the aborted result's chunk_count. Gate on any
73 // executed chunk rather than on full-plan success so path caches are
74 // refreshed over partially applied plans too.
75 const bool any_op_wrote =
76 stats.executed_ops || stats.op_execution.chunk_count > 0;
77 if (any_op_wrote && options.pathing_dirty_mask) {
78 for (const auto& report : stats.op_report.operations()) {
79 if (report.status == OperationStatus::Planned &&
80 (report.field_access.dirty_mask & options.pathing_dirty_mask)) {
81 mark_pathing_dirty(state.path_agents);
82 break;
83 }
84 }
85 }
86
87 stats.path_agents = path_tick();
88 stats.tick = stats.path_agents.tick;
89
90 const auto old_size = render_deltas.size();
91 collect_render_tile_deltas(render_deltas, world, options.render_dirty_mask);
92 stats.render_delta_count = render_deltas.size() - old_size;
93 if (options.clear_render_dirty) {
94 clear_render_delta_dirty(world, options.render_dirty_mask);
95 }
96 return stats;
97}
98
99} // namespace detail
100
102template <typename World, typename PassableTag, WritePolicy Policy, typename Fn>
103auto tick_unit_scheduler(SimSchedulerState& state, World& world,
104 const OperationBatch& ops,
105 std::span<PathAgentState> agents,
106 PathRequestRuntime& runtime,
107 std::vector<RenderTileDelta>& render_deltas, Fn&& fn,
108 SimSchedulerOptions options = {})
110 return detail::tick_scheduler_core<World, Policy>(
111 state, world, ops, render_deltas, std::forward<Fn>(fn), options, [&] {
112 return tick_unit_path_agents<World, PassableTag>(
113 state.path_agents, world, agents, runtime,
114 options.path_agent_options);
115 });
116}
117
118template <typename World, typename PassableTag, typename OccupancyTag,
119 typename ReservationTag, WritePolicy Policy, typename Fn>
121auto tick_unit_movement_scheduler(SimSchedulerState& state, World& world,
122 const OperationBatch& ops,
123 std::span<PathAgentState> agents,
124 PathRequestRuntime& runtime,
125 std::vector<RenderTileDelta>& render_deltas,
126 Fn&& fn, SimSchedulerOptions options = {})
128 return detail::tick_scheduler_core<World, Policy>(
129 state, world, ops, render_deltas, std::forward<Fn>(fn), options, [&] {
130 return tick_unit_path_agents_with_movement<
131 World, PassableTag, OccupancyTag, ReservationTag>(
132 state.path_agents, world, agents, runtime,
133 options.path_agent_options);
134 });
135}
136
137template <typename World, typename Class, std::uint32_t MaxCost,
138 WritePolicy Policy, typename Fn>
140auto tick_weighted_scheduler(SimSchedulerState& state, World& world,
141 const OperationBatch& ops,
142 std::span<PathAgentState> agents,
143 PathRequestRuntime& runtime,
144 std::vector<RenderTileDelta>& render_deltas,
145 Fn&& fn, SimSchedulerOptions options = {})
147 return detail::tick_scheduler_core<World, Policy>(
148 state, world, ops, render_deltas, std::forward<Fn>(fn), options, [&] {
149 return tick_weighted_path_agents<World, Class, MaxCost>(
150 state.path_agents, world, agents, runtime,
151 options.path_agent_options);
152 });
153}
154
155template <typename World, typename Class, std::uint32_t MaxCost,
156 typename OccupancyTag, typename ReservationTag, WritePolicy Policy,
157 typename Fn>
159auto tick_weighted_movement_scheduler(
160 SimSchedulerState& state, World& world, const OperationBatch& ops,
161 std::span<PathAgentState> agents, PathRequestRuntime& runtime,
162 std::vector<RenderTileDelta>& render_deltas, Fn&& fn,
163 SimSchedulerOptions options = {}) -> SimSchedulerStats {
164 return detail::tick_scheduler_core<World, Policy>(
165 state, world, ops, render_deltas, std::forward<Fn>(fn), options, [&] {
166 return tick_weighted_path_agents_with_movement<
167 World, Class, MaxCost, OccupancyTag, ReservationTag>(
168 state.path_agents, world, agents, runtime,
169 options.path_agent_options);
170 });
171}
172
173} // namespace tess
Definition queued.h:1521
Definition queued.h:1668
Definition path_runtime.h:197
Definition world.h:22
Definition metadata_types.h:12
Configures per-tick movement, caching, and blocked-agent retry limits.
Definition path_agent_tick.h:84
Definition path_agent_tick.h:17
Summarizes path planning and movement performed during one tick.
Definition path_agent_tick.h:531
Definition phase_executor.h:65
Configures dirty propagation, path-agent work, and render-delta clearing.
Definition scheduler.h:21
Owns simulation subsystem state retained across scheduler ticks.
Definition scheduler.h:16
Summarizes queued operations, path agents, and render deltas for one tick.
Definition scheduler.h:29