Skip to main content

unbanished_game/behaviour/character/
movement.rs

1//! Walking to a place (`MoveToAction`): asking for a path, following its cells and facing the next
2//! one.
3
4use super::*;
5
6// vtable 0x140166260
7/// `MoveTo`.
8///
9/// Original: `FUN_1400ce640`.
10#[derive(Debug, Clone)]
11pub struct MoveTo {
12    // +0x38
13    /// Destination.
14    pub target: [f32; 3],
15    // +0x5c
16    /// Allowed cell types.
17    pub flags: u32,
18    // +0x468 / +0x46c
19    /// Walk and stand clips.
20    pub walk: usize,
21    /// The clip played while standing.
22    pub stand: usize,
23    // +0x470
24    /// Pathfinder cost table.
25    pub mode: u32,
26    // +0x478
27    /// Speed factor (random 0.9–1.1 when constructed with a negative value).
28    pub speed: f32,
29    // +0x474
30    /// Walking behind another character.
31    ///
32    /// Original: `FUN_1400cde40`.
33    pub following: bool,
34    /// Pending request result (`+0x20/+0x24`); available on the next update.
35    pending: Option<Option<Vec<i32>>>,
36    /// The request was made by this pass's start, which the action stack follows with an update
37    /// at once. The original's worker thread has not answered by then, so that update waits.
38    fresh: bool,
39    /// `+0x60` index, `+0x64` count, `+0x68` cells.
40    index: i32,
41    count: i32,
42    path: [i32; PATH_CELLS],
43}
44
45impl MoveTo {
46    /// A `MoveTo` action. A speed below 0 draws a factor of 1 ± 0.1; any other speed is kept.
47    ///
48    /// Measured: `tools/emu/move_speed.py` (the original on 2,000 speeds, NaN and −0 among them).
49    ///
50    /// Original: `FUN_1400ce640`.
51    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            // comiss + jbe at 0x1400ce746: a NaN keeps its value, like 0 or more.
67            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    // +0x64; +0x68; +0x60
82    /// Plan from `from` to the target. A `MoveTo` from a save keeps its way (its cells and the next
83    /// one) as it was.
84    /// A path request in flight is not saved; one with no way restarts.
85    ///
86    /// Original: `FUN_1400ce390`, `FUN_1400ce0e0`.
87    #[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    // +0x60; +0x64; +0x68
119    /// What a save holds of the way: the next cell's index and the cells.
120    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    /// `FUN_1400cdf80` (start).
154    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    /// `FUN_1400cda40` (update).
181    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                // jbe at 0x1400cdc61: still away, or a NaN distance, keeps walking.
195                #[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        // Measured: Save2's citizens start their walk clip one iteration after the port did when
229        // the path was collected in the pass that asked for it.
230        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        // A re-plan from an update is collected on the next update.
249        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}