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
//! Systems that implement the two-stage async chunk generation pipeline.
//!
//! # Pipeline overview
//!
//! Stage 1 — **[`chunk_request_system`]**: runs first in [`super::GeneratorSystems`].
//! It drains [`ChunkLoadRequest`] messages and for each one:
//!
//! - Attempts a synchronous disk restore when [`PersistenceConfig`] is present.
//! On success the entity is spawned and [`ChunkModified`] is emitted
//! immediately — no async task is needed.
//! - On a cache miss (or when persistence is absent) it clones the
//! [`super::GeneratorResource`]'s `Arc`, spawns a new bare entity, and
//! offloads the [`super::ChunkGenerator::generate`] call to
//! [`AsyncComputeTaskPool`]. The returned [`bevy::tasks::Task`] is stored
//! as a [`ChunkTask<V>`] component.
//!
//! Stage 2 — **[`chunk_task_poll_system`]**: runs second, chained after stage 1.
//! It iterates every entity with a [`ChunkTask<V>`] component and calls
//! `poll_once` — a non-blocking single-frame check. When a task resolves:
//!
//! - The [`ChunkTask<V>`] component is removed.
//! - The generated [`Chunk<V>`] and [`ChunkCoordComp`] are inserted.
//! - [`ChunkManager::register`] is called to make the chunk queryable.
//! - [`ChunkModified`] is emitted so [`crate::mesh::systems::chunk_mesh_system`]
//! builds geometry on the same frame.
//!
//! The two-stage design keeps the main thread free: generation work happens
//! entirely on worker threads, and the main thread only pays a `poll_once` cost
//! per in-flight task per frame.
use *;
use ;
use crate;
use crateVoxelType;
use crate;
use GeneratorResource;
// ─── ChunkTask ────────────────────────────────────────────────────────────────
/// ECS component that tracks an in-flight async chunk generation task.
///
/// [`chunk_request_system`] attaches this component to the chunk entity when
/// it offloads generation to [`AsyncComputeTaskPool`]. The entity has no other
/// meaningful components until [`chunk_task_poll_system`] resolves the task and
/// replaces this component with the fully populated [`Chunk<V>`].
///
/// The inner [`Task`] yields `(ChunkCoord, Chunk<V>)` so the coord is
/// available after the task completes without requiring an additional query.
// ─── chunk_request_system ────────────────────────────────────────────────────
/// Responds to [`ChunkLoadRequest`] messages by either loading from disk or
/// spawning async generation tasks.
///
/// For each incoming request this system:
/// 1. If [`PersistenceConfig`] is present, attempts to load the chunk from
/// disk via [`try_load_chunk`]. On success the chunk entity is spawned
/// immediately (no async task), the manager is updated, and a
/// [`ChunkModified`] message is emitted so the mesh system builds geometry.
/// 2. If no saved file exists (or [`PersistenceConfig`] is absent), the
/// original async generation path is used:
/// a. Spawns a new Bevy entity.
/// b. Clones the generator `Arc` so the task owns it without holding the `World`.
/// c. Offloads chunk generation to [`AsyncComputeTaskPool`], attaching the
/// returned [`Task`] as a [`ChunkTask<V>`] component.
///
/// The entity produced by the async path has no other components until
/// [`chunk_task_poll_system`] completes the task and attaches the real
/// [`Chunk<V>`] data.
// ─── chunk_task_poll_system ───────────────────────────────────────────────────
/// Polls all in-flight [`ChunkTask<V>`] components.
///
/// When a task is ready this system:
/// 1. Removes the [`ChunkTask`] component from the entity.
/// 2. Inserts the generated [`Chunk<V>`] and a [`ChunkCoordComp`] component.
/// 3. Calls [`ChunkManager::register`] so the streaming system knows the chunk
/// is fully loaded.
/// 4. Emits a [`ChunkModified`] message to trigger mesh generation.