-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspawner.rs
193 lines (170 loc) · 6.08 KB
/
spawner.rs
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
mod behavior_nodes;
use behavior_tree_lite::{error::LoadError, Blackboard, Context};
use self::behavior_nodes::{build_tree, SpawnFighter, SpawnWorker};
use crate::{
agent::AgentClass,
behavior_tree_adapt::{BehaviorTree, GetIdCommand, GetResource, PrintCommand},
collision::{aabb_intersects, CollisionShape, Obb},
entity::{Entity, GameEvent, MAX_LOG_ENTRIES},
game::Game,
};
use std::{cell::RefCell, collections::VecDeque, rc::Rc};
pub(crate) const SPAWNER_MAX_HEALTH: u32 = 1000;
pub(crate) const SPAWNER_MAX_RESOURCE: i32 = 1000;
pub(crate) const SPAWNER_RADIUS: f64 = 1.0;
pub(crate) type SpawnerState = [f64; 2];
#[derive(Debug)]
pub struct Spawner {
pub id: usize,
pub pos: [f64; 2],
pub team: usize,
pub active: bool,
pub health: u32,
pub resource: i32,
behavior_source: Rc<String>,
behavior_tree: Option<BehaviorTree>,
blackboard: Blackboard,
log_buffer: VecDeque<String>,
spawn_progress: Option<(usize, AgentClass)>,
}
impl Spawner {
pub(crate) fn new(
id_gen: &mut usize,
pos: [f64; 2],
team: usize,
behavior_source: Rc<String>,
) -> Result<Self, LoadError> {
let id = *id_gen;
*id_gen += 1;
let tree = build_tree(&behavior_source)?;
Ok(Spawner {
id,
pos,
team,
active: true,
health: SPAWNER_MAX_HEALTH,
resource: 0,
behavior_source,
behavior_tree: Some(tree),
blackboard: Blackboard::new(),
log_buffer: VecDeque::new(),
spawn_progress: None,
})
}
pub(crate) fn get_health_rate(&self) -> f64 {
self.health as f64 / SPAWNER_MAX_HEALTH as f64
}
pub(crate) fn get_shape(&self) -> CollisionShape {
Self::collision_shape(self.pos)
}
pub(crate) fn collision_shape(pos: SpawnerState) -> CollisionShape {
CollisionShape::BBox(Obb {
center: pos.into(),
xs: SPAWNER_RADIUS,
ys: SPAWNER_RADIUS,
orient: 0.,
})
}
pub(crate) fn log_buffer(&self) -> &VecDeque<String> {
&self.log_buffer
}
pub(crate) fn behavior_source(&self) -> Rc<String> {
self.behavior_source.clone()
}
pub(crate) fn behavior_tree(&self) -> Option<&BehaviorTree> {
self.behavior_tree.as_ref()
}
pub(crate) fn qtree_collision(
ignore: Option<usize>,
newpos: SpawnerState,
others: &[RefCell<Entity>],
) -> bool {
let aabb = Self::collision_shape(newpos).buffer(1.).to_aabb();
for other in others {
let other = other.borrow();
if Some(other.get_id()) == ignore {
continue;
}
let other_aabb = other.get_shape().to_aabb();
if aabb_intersects(&aabb, &other_aabb) {
return true;
}
}
false
}
pub fn get_progress(&self) -> f32 {
self.spawn_progress
.map(|(remaining, class)| remaining as f32 / class.time() as f32)
.unwrap_or(0.)
}
pub(crate) fn update(
&mut self,
game: &mut Game,
entities: &[RefCell<Entity>],
) -> Vec<GameEvent> {
if self.resource < AgentClass::Worker.cost() {
self.resource += 1;
}
let mut ret = vec![];
let mut try_spawn = |class: AgentClass| -> Option<Box<dyn std::any::Any>> {
if class.cost() <= self.resource {
let agent_count = entities
.iter()
.filter(|entity| {
entity
.try_borrow()
.map(|entity| {
entity.is_agent()
&& entity.get_team() == self.team
&& entity.get_class() == Some(class)
})
.unwrap_or(false)
})
.count();
if agent_count < game.params.agent_count {
if let Some((remaining, class)) = self.spawn_progress.as_mut() {
if *remaining < 1 {
ret.push(GameEvent::SpawnAgent {
pos: self.pos,
team: self.team,
spawner: self.id,
class: *class,
});
self.spawn_progress = None;
} else {
*remaining -= 1;
}
return Some(Box::new(true));
} else {
self.spawn_progress = Some((class.time(), class));
}
}
}
Some(Box::new(false))
};
if let Some(mut tree) = self.behavior_tree.take() {
let mut ctx = Context::new(std::mem::take(&mut self.blackboard));
let mut process = |f: &dyn std::any::Any| {
if f.downcast_ref::<GetIdCommand>().is_some() {
return Some(Box::new(self.id) as Box<dyn std::any::Any>);
} else if let Some(s) = f.downcast_ref::<PrintCommand>() {
self.log_buffer.push_back(s.0.clone());
while MAX_LOG_ENTRIES < self.log_buffer.len() {
self.log_buffer.pop_front();
}
} else if f.downcast_ref::<GetResource>().is_some() {
return Some(Box::new(self.resource));
} else if f.downcast_ref::<SpawnFighter>().is_some() {
return try_spawn(AgentClass::Fighter);
} else if f.downcast_ref::<SpawnWorker>().is_some() {
return try_spawn(AgentClass::Worker);
}
None
};
let _res = tree.0.tick(&mut process, &mut ctx);
self.behavior_tree = Some(tree);
self.blackboard = ctx.take_blackboard();
}
ret
}
}