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
use crate::{components::*, writer::TaskWriter, TaskData};
use specs::{prelude::*, storage::GenericReadStorage, world::LazyBuilder};
pub type TaskUser<'a> = TaskData<'a,
ReadStorage<'a, TaskProgress>,
ReadStorage<'a, SingleEdge>,
ReadStorage<'a, MultiEdge>,
(),
>;
impl<'a, P, S, M, F> TaskData<'a, P, S, M, F>
where
P: GenericReadStorage<Component=TaskProgress>,
S: GenericReadStorage<Component=SingleEdge>,
M: GenericReadStorage<Component=MultiEdge>,
&'a P: 'a + Join,
{
pub fn make_task_with_entity_lazy<'b, T: TaskComponent<'b> + Send + Sync>(
&self, entity: Entity, task: T
) {
self.lazy.exec(move |world| {
world.exec(move |(mut builder, mut task_storage): (TaskWriter, WriteStorage<T>)| {
builder.make_task_with_entity(entity, task, &mut task_storage);
})
})
}
pub fn make_task_lazy<'b, T: TaskComponent<'b> + Send + Sync>(&self, task: T) -> Entity {
let entity = self.entities.create();
self.make_task_with_entity_lazy(entity, task);
entity
}
pub fn make_final_task_lazy<'b, T: TaskComponent<'b> + Send + Sync>(
&self, task: T, on_completion: OnCompletion,
) -> Entity {
let entity = self.make_task_lazy(task);
self.finalize_lazy(entity, on_completion);
entity
}
pub fn make_fork_lazy(&self) -> Entity {
let entity = self
.lazy
.create_entity(&self.entities)
.with(MultiEdge::default())
.build();
log::debug!("Created fork {:?}", entity);
entity
}
pub fn add_prong_lazy(&self, fork_entity: Entity, prong: Entity) {
self.lazy.exec(move |world| {
world.exec(move |mut builder: TaskWriter| {
builder.add_prong(fork_entity, prong);
})
});
}
pub fn join_lazy(&self, parent: Entity, child: Entity) {
self.lazy.exec(move |world| {
world.exec(move |mut builder: TaskWriter| {
builder.join(parent, child);
})
});
}
pub fn finalize_lazy(&self, entity: Entity, on_completion: OnCompletion) {
LazyBuilder {
entity,
lazy: &self.lazy,
}
.with(FinalTag { on_completion })
.build();
}
pub fn task_is_complete(&self, task: Entity) -> bool {
if let Some(progress) = self.progress.get(task) {
progress.is_complete()
} else {
false
}
}
fn fork_is_complete(&self, multi_children: &[Entity]) -> bool {
for child in multi_children.iter() {
if !self.entity_is_complete(*child) {
return false;
}
}
true
}
pub fn entity_is_complete(&self, entity: Entity) -> bool {
if let Some(MultiEdge { children }) = self.multi_edges.get(entity) {
self.fork_is_complete(&children)
} else {
self.task_is_complete(entity)
}
}
pub fn count_tasks_in_progress(&'a self) -> usize {
(&self.progress).join().count()
}
fn delete_descendents(&self, entity: Entity) {
if let Some(MultiEdge { children }) = self.multi_edges.get(entity) {
for child in children.iter() {
self.delete_entity_and_descendents(*child);
}
}
if let Some(SingleEdge { child }) = self.single_edges.get(entity) {
self.delete_entity_and_descendents(*child);
}
}
pub fn delete_entity_and_descendents(&self, entity: Entity) {
if !self.entities.is_alive(entity) {
return;
}
self.delete_descendents(entity);
log::debug!("Deleting {:?}", entity);
self.entities.delete(entity).unwrap();
}
pub fn delete_if_complete(&self, entity: Entity) -> bool {
if self.entity_is_complete(entity) {
self.delete_entity_and_descendents(entity);
true
} else {
false
}
}
fn maintain_task_and_descendents(&self, entity: Entity) -> bool {
let (is_unblocked, is_complete) = if let Some(progress) = self.progress.get(entity) {
(progress.is_unblocked(), progress.is_complete())
} else {
return true;
};
if is_complete {
log::debug!(
"Noticed task {:?} is complete, removing TaskProgress",
entity
);
return true;
}
if is_unblocked {
return false;
}
let mut child_complete = true;
if let Some(SingleEdge { child }) = self.single_edges.get(entity).cloned() {
child_complete = self.maintain_entity_and_descendents(child);
}
if child_complete {
log::debug!("Unblocking task {:?}", entity);
let progress = self
.progress
.get(entity)
.expect("Blocked task must have progress");
progress.unblock();
}
false
}
fn maintain_fork_and_descendents(
&self,
entity: Entity,
multi_edge_children: &[Entity],
) -> bool {
let mut single_child_complete = true;
if let Some(SingleEdge { child }) = self.single_edges.get(entity).cloned() {
single_child_complete = self.maintain_entity_and_descendents(child);
}
let mut multi_children_complete = true;
if single_child_complete {
for child in multi_edge_children.iter() {
multi_children_complete &= self.maintain_entity_and_descendents(*child);
}
}
single_child_complete && multi_children_complete
}
pub(crate) fn maintain_entity_and_descendents(&self, entity: Entity) -> bool {
if let Some(MultiEdge { children }) = self.multi_edges.get(entity).cloned() {
self.maintain_fork_and_descendents(entity, &children)
} else {
self.maintain_task_and_descendents(entity)
}
}
}