unbanished_game/behaviour/character/
movement.rs1use super::*;
5
6#[derive(Debug, Clone)]
11pub struct MoveTo {
12 pub target: [f32; 3],
15 pub flags: u32,
18 pub walk: usize,
21 pub stand: usize,
23 pub mode: u32,
26 pub speed: f32,
29 pub following: bool,
34 pending: Option<Option<Vec<i32>>>,
36 fresh: bool,
39 index: i32,
41 count: i32,
42 path: [i32; PATH_CELLS],
43}
44
45impl MoveTo {
46 pub fn new(
52 target: [f32; 3],
53 walk: usize,
54 stand: usize,
55 flags: u32,
56 mode: u32,
57 speed: f32,
58 rng: &mut Random,
59 ) -> MoveTo {
60 MoveTo {
61 target,
62 flags,
63 walk,
64 stand,
65 mode,
66 speed: if speed < 0.0 {
68 rng.rand_float_tol() * 0.1 + 1.0
69 } else {
70 speed
71 },
72 following: false,
73 pending: None,
74 fresh: false,
75 index: -1,
76 count: 0,
77 path: [0; PATH_CELLS],
78 }
79 }
80
81 #[allow(clippy::too_many_arguments)]
88 pub fn restored(
89 target: [f32; 3],
90 walk: usize,
91 stand: usize,
92 flags: u32,
93 mode: u32,
94 speed: f32,
95 following: bool,
96 index: i32,
97 path: &[i32],
98 ) -> MoveTo {
99 let n = path.len().min(PATH_CELLS);
100 let mut cells = [0; PATH_CELLS];
101 cells[..n].copy_from_slice(&path[..n]);
102 MoveTo {
103 target,
104 flags,
105 walk,
106 stand,
107 mode,
108 speed,
109 following,
110 pending: None,
111 fresh: false,
112 index,
113 count: n as i32,
114 path: cells,
115 }
116 }
117
118 pub fn saved_way(&self) -> (i32, &[i32]) {
121 (
122 self.index,
123 &self.path[..(self.count.max(0) as usize).min(PATH_CELLS)],
124 )
125 }
126
127 pub(super) fn request(&mut self, from: [f32; 3], world: &mut World) {
128 let a = world.cell(from);
129 let b = world.cell(self.target);
130 self.index = -1;
131 if a == b {
132 self.count = 1;
133 self.path[0] = a;
134 self.index = 0;
135 return;
136 }
137 world.pathfinder.set_mode(self.mode);
138 self.pending = Some(pathfind::request(
139 world.pathfinder,
140 world.types,
141 world.regions,
142 self.flags,
143 a,
144 b,
145 ));
146 }
147
148 pub(super) fn face_waypoint(&self, ch: &mut Character, world: &World) {
149 let cell = self.path[(self.index as usize).min(PATH_CELLS - 1)];
150 ch.face(world.cell_centre(cell));
151 }
152
153 pub(super) fn start(
155 &mut self,
156 ch: &mut Character,
157 world: &mut World,
158 group: &AnimationGroup,
159 ) -> Status {
160 let d = [
161 ch.position[0] - self.target[0],
162 ch.position[1] - self.target[1],
163 ];
164 if (d[0] * d[0] + d[1] * d[1]).sqrt() < ARRIVE {
165 return Status::Done;
166 }
167 self.request(ch.position, world);
168 self.fresh = self.pending.is_some();
169 if self.index < 0 {
170 return match self.pending {
171 Some(Some(_)) => Status::Running,
172 _ => Status::Failed,
173 };
174 }
175 self.face_waypoint(ch, world);
176 ch.play(self.walk, group, 1.0);
177 Status::Running
178 }
179
180 pub(super) fn update(
182 &mut self,
183 ch: &mut Character,
184 world: &mut World,
185 group: &AnimationGroup,
186 ) -> Status {
187 if self.index != -1 {
188 let here = world.cell(ch.position);
189 if world.cell(self.target) == here {
190 let d = [
191 ch.position[0] - self.target[0],
192 ch.position[1] - self.target[1],
193 ];
194 #[allow(clippy::neg_cmp_op_on_partial_ord)]
196 if !((d[0] * d[0] + d[1] * d[1]).sqrt() < ARRIVE) {
197 ch.face(self.target);
198 return Status::Running;
199 }
200 return Status::Done;
201 }
202 if self.count < 2 {
203 return Status::Running;
204 }
205 if self.index < self.count {
206 let t = world.types[here as usize].0;
207 let scale = if self.following {
208 ch.desc.follow_speed
209 } else if t == 1 || t == 9 {
210 ch.desc.road_speed
211 } else if t == 0 {
212 ch.desc.type0_speed
213 } else {
214 1.0
215 };
216 ch.speed = scale * self.speed;
217 if here != self.path[self.index as usize] {
218 return Status::Running;
219 }
220 self.index += 1;
221 self.face_waypoint(ch, world);
222 return Status::Running;
223 }
224 ch.play(self.stand, group, 1.0);
225 self.index = -1;
226 return Status::Running;
227 }
228 if self.pending.is_some() && std::mem::take(&mut self.fresh) {
231 return Status::Running;
232 }
233 if let Some(result) = self.pending.take() {
234 let Some(path) = result else {
235 return Status::Failed;
236 };
237 let n = path.len().min(PATH_CELLS);
238 self.path[..n].copy_from_slice(&path[..n]);
239 self.count = n as i32;
240 if n == 0 {
241 return Status::Failed;
242 }
243 self.index = 1;
244 self.face_waypoint(ch, world);
245 ch.play(self.walk, group, 1.0);
246 return Status::Running;
247 }
248 let status = self.start(ch, world, group);
250 self.fresh = false;
251 match status {
252 Status::Failed => Status::Failed,
253 _ => Status::Running,
254 }
255 }
256}