Newer
Older
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use std::fs::File;
use std::path::Path;
use std::time::{Duration, Instant};
use serde::Deserialize;
use specs::{
Component, DenseVecStorage, Entities, Entity, Join, LazyUpdate, Read, ReadStorage, System,
WriteStorage,
};
use crate::{
actions::{Action, ActionManager, ActionManagerWrapper, ActionState},
choices::{Choice, ChoiceBuilder},
considerations::Utility,
pickers::Picker,
stopwatch::Stopwatch,
};
#[derive(Debug, Clone)]
pub struct ActionEnt(pub Entity);
#[derive(Debug, Clone)]
pub struct ConsiderationEnt(pub Entity);
#[derive(Component, Debug)]
pub struct Thinker {
pub picker: Box<dyn Picker>,
pub otherwise: Option<ActionEnt>,
pub choices: Vec<Choice>,
pub actor: Entity,
pub current_action: Option<ActionEnt>,
pub timer: Stopwatch,
}
impl Thinker {
pub fn load_from<P: AsRef<Path>>(path: P) -> builder::Thinker {
let f = File::open(&path).expect("Failed to open file");
ron::de::from_reader(f).expect("Failed to read .ron file")
}
}
mod builder {
use super::*;
#[derive(Debug, Deserialize)]
pub struct Thinker {
pub picker: Box<dyn Picker>,
pub otherwise: Option<Box<dyn Action>>,
pub choices: Vec<ChoiceBuilder>,
}
}
impl builder::Thinker {
pub fn build(self, actor: Entity, ents: &Entities, lazy: &LazyUpdate) -> ActionEnt {
let action_ent = ActionState::build(Box::new(self), actor, ents, lazy);
lazy.insert(action_ent.0.clone(), ActiveThinker(true));
lazy.insert(action_ent.0.clone(), ActionState::Requested);
action_ent
}
}
#[typetag::deserialize]
impl Action for builder::Thinker {
fn build(
self: Box<Self>,
actor: Entity,
action_ent: ActionEnt,
ents: &Entities,
lazy: &LazyUpdate,
) -> Box<dyn ActionManager> {
lazy.insert(
action_ent.0.clone(),
Thinker {
picker: self.picker,
actor,
choices: self
.choices
.into_iter()
.map(|choice| choice.build(actor.clone(), &ents, &lazy))
.collect(),
otherwise: self
.otherwise
.map(|builder| ActionState::build(builder, actor.clone(), ents, lazy)),
current_action: None,
timer: Stopwatch::new(),
},
);
Box::new(ThinkerManager)
}
}
#[derive(Debug, Component)]
pub struct ActiveThinker(bool);
#[derive(Debug)]
pub struct ThinkerManager;
impl ActionManager for ThinkerManager {
fn activate(&self, _: Entity, action_ent: ActionEnt, lazy: &LazyUpdate) {
lazy.insert(action_ent.0, ActiveThinker(false));
lazy.insert(action_ent.0, ActionState::Requested);
}
fn deactivate(&self, action_ent: ActionEnt, lazy: &LazyUpdate) {
lazy.remove::<ActiveThinker>(action_ent.0);
}
}
pub struct ThinkerSystem {
index: usize,
max_duration: Duration,
}
impl ThinkerSystem {
pub fn new(max_duration: Duration) -> Self {
Self {
index: 0,
max_duration,
}
}
}
impl<'a> System<'a> for ThinkerSystem {
type SystemData = (
Entities<'a>,
WriteStorage<'a, Thinker>,
ReadStorage<'a, ActiveThinker>,
ReadStorage<'a, Utility>,
WriteStorage<'a, ActionState>,
ReadStorage<'a, ActionManagerWrapper>,
Read<'a, LazyUpdate>,
);
fn run(
&mut self,
(ents, mut thinkers, active_thinkers, utilities, mut action_states, builder_wrappers, lazy): Self::SystemData,
) {
let start = Instant::now();
for (thinker, thinker_ent, active_thinker) in (&mut thinkers, &ents, &active_thinkers)
.join()
.skip(self.index)
{
self.index += 1;
let thinker_state = action_states
.get(thinker_ent.clone())
.expect("Where is it?")
.clone();
match thinker_state {
ActionState::Init | ActionState::Success | ActionState::Failure => {
if let ActiveThinker(true) = active_thinker {
let act_state = action_states.get_mut(thinker_ent.clone()).expect("???");
*act_state = ActionState::Requested;
}
}
ActionState::Cancelled => {
if let Some(current) = &mut thinker.current_action {
let state = action_states.get(current.0.clone()).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.").clone();
match state {
ActionState::Success | ActionState::Failure => {
let act_state =
action_states.get_mut(thinker_ent.clone()).expect("???");
*act_state = state.clone();
let state = action_states.get_mut(current.0.clone()).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
*state = ActionState::Init;
thinker.current_action = None;
}
_ => {
let state = action_states.get_mut(current.0.clone()).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
*state = ActionState::Cancelled;
}
}
}
}
ActionState::Requested | ActionState::Executing => {
if let Some(picked_action_ent) =
thinker.picker.pick(&thinker.choices, &utilities)
{
// Think about what action we're supposed to be taking. We do this
// every tick, because we might change our mind.
// ...and then execute it (details below).
exec_picked_action(
thinker_ent,
thinker,
&picked_action_ent,
&mut action_states,
&builder_wrappers,
&lazy,
);
} else if let Some(default_action_ent) = &thinker.otherwise {
// Otherwise, let's just execute the default one! (if it's there)
let default_action_ent = default_action_ent.clone();
exec_picked_action(
thinker_ent,
thinker,
&default_action_ent,
&mut action_states,
&builder_wrappers,
&lazy,
);
} else if let Some(current) = &mut thinker.current_action {
// If we didn't pick anything, and there's no default action,
// we need to see if there's any action currently executing,
// and cancel it. We also use this opportunity to clean up
// stale action components so they don't slow down joins.
let state = action_states.get_mut(current.0.clone()).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
let factory = builder_wrappers.get(current.0.clone()).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
match state {
ActionState::Init | ActionState::Success | ActionState::Failure => {
factory.0.deactivate(current.clone(), &lazy);
*state = ActionState::Init;
thinker.current_action = None;
}
_ => {
*state = ActionState::Cancelled;
}
}
}
}
}
thinker.timer.reset();
thinker.timer.start();
if self.index % 500 == 0 && start.elapsed() > self.max_duration {
return;
}
}
self.index = 0;
}
}
fn exec_picked_action(
thinker_ent: Entity,
thinker: &mut Thinker,
picked_action_ent: &ActionEnt,
states: &mut WriteStorage<ActionState>,
builder_wrappers: &ReadStorage<ActionManagerWrapper>,
lazy: &Read<LazyUpdate>,
) {
// If we do find one, then we need to grab the corresponding
// component for it. The "action" that `picker.pick()` returns
// is just a newtype for an Entity.
//
// Now we check the current action. We need to check if we picked the same one as the previous tick.
//
// TODO: I don't know where the right place to put this is
// (maybe not in this logic), but we do need some kind of
// oscillation protection so we're not just bouncing back and
// forth between the same couple of actions.
if let Some(current) = &mut thinker.current_action {
if current.0 != picked_action_ent.0 {
// So we've picked a different action than we were
// currently executing. Just like before, we grab the
// actual Action component (and we assume it exists).
let curr_action_state = states.get_mut(current.0.clone()).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
// If the action is executing, or was requested, we
// need to cancel it to make sure it stops. The Action
// system will take care of resetting its state as
// needed.
match curr_action_state {
ActionState::Executing | ActionState::Requested => {
*curr_action_state = ActionState::Cancelled;
let thinker_state = states.get_mut(thinker_ent.clone()).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
*thinker_state = ActionState::Cancelled;
}
ActionState::Init | ActionState::Success | ActionState::Failure => {
let current_action_factory = builder_wrappers.get(current.0.clone()).expect("Couldn't find an Action component corresponding to an Action entity. This is definitely a bug.");
current_action_factory
.0
.deactivate(picked_action_ent.clone(), &lazy);
let old_state = curr_action_state.clone();
*curr_action_state = ActionState::Init;
*current = picked_action_ent.clone();
let thinker_state = states.get_mut(thinker_ent.clone()).expect("Couldn't find a component corresponding to the current action. This is definitely a bug.");
*thinker_state = old_state;
}
ActionState::Cancelled => {}
};
} else {
// Otherwise, it turns out we want to keep executing
// the same action. Just in case, we go ahead and set
// it as Requested if for some reason it had finished
// but the Action System hasn't gotten around to
// cleaning it up.
let picked_action_state = states.get_mut(picked_action_ent.0.clone()).expect("Couldn't find an Action component corresponding to an Action entity. This is definitely a bug.");
match picked_action_state {
ActionState::Init => {
let picked_action_factory = builder_wrappers.get(picked_action_ent.0.clone()).expect("Couldn't find an Action component corresponding to an Action entity. This is definitely a bug.");
picked_action_factory.0.activate(
thinker.actor.clone(),
picked_action_ent.clone(),
lazy,
);
*picked_action_state = ActionState::Requested;
}
_ => {}
}
}
} else {
// This branch arm is called when there's no
// current_action in the thinker. The logic here is pretty
// straightforward -- we set the action, Request it, and
// that's it.
let picked_action_factory = builder_wrappers.get(picked_action_ent.0.clone()).expect("Couldn't find an Action component corresponding to an Action entity. This is definitely a bug.");
let picked_action_state = states.get_mut(picked_action_ent.0.clone()).expect("Couldn't find an Action component corresponding to an Action entity. This is definitely a bug.");
picked_action_factory
.0
.activate(thinker.actor.clone(), picked_action_ent.clone(), lazy);
thinker.current_action = Some(picked_action_ent.clone());
*picked_action_state = ActionState::Requested;
}
}