aboutsummaryrefslogtreecommitdiff
path: root/azalea/src/pathfinder/moves.rs
blob: 0dca3c95657b2f9319456f15e0f8740dd0d8a159 (plain)
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
use super::{Node, VerticalVel};
use azalea_core::{BlockPos, CardinalDirection};
use azalea_physics::collision::{self, BlockWithShape};
use azalea_world::Instance;

/// whether this block is passable
fn is_block_passable(pos: &BlockPos, world: &Instance) -> bool {
    if let Some(block) = world.chunks.get_block_state(pos) {
        block.shape() == &collision::empty_shape()
    } else {
        false
    }
}

/// whether this block has a solid hitbox (i.e. we can stand on it)
fn is_block_solid(pos: &BlockPos, world: &Instance) -> bool {
    if let Some(block) = world.chunks.get_block_state(pos) {
        // block.shape() == &collision::block_shape()
        block.shape() != &collision::empty_shape()
    } else {
        false
    }
}

/// Whether this block and the block above are passable
fn is_passable(pos: &BlockPos, world: &Instance) -> bool {
    is_block_passable(pos, world) && is_block_passable(&pos.up(1), world)
}

/// Whether we can stand in this position. Checks if the block below is solid,
/// and that the two blocks above that are passable.

fn is_standable(pos: &BlockPos, world: &Instance) -> bool {
    is_block_solid(&pos.down(1), world) && is_passable(pos, world)
}

const JUMP_COST: f32 = 0.5;
const WALK_ONE_BLOCK_COST: f32 = 1.0;

pub trait Move: Send + Sync {
    fn cost(&self, world: &Instance, node: &Node) -> f32;
    /// Returns by how much the entity's position should be changed when this
    /// move is executed.
    fn offset(&self) -> BlockPos;
    fn next_node(&self, node: &Node) -> Node {
        Node {
            pos: node.pos + self.offset(),
            vertical_vel: VerticalVel::None,
        }
    }
}

pub struct ForwardMove(pub CardinalDirection);
impl Move for ForwardMove {
    fn cost(&self, world: &Instance, node: &Node) -> f32 {
        if is_standable(&(node.pos + self.offset()), world)
            && node.vertical_vel == VerticalVel::None
        {
            WALK_ONE_BLOCK_COST
        } else {
            f32::INFINITY
        }
    }
    fn offset(&self) -> BlockPos {
        BlockPos::new(self.0.x(), 0, self.0.z())
    }
}

pub struct AscendMove(pub CardinalDirection);
impl Move for AscendMove {
    fn cost(&self, world: &Instance, node: &Node) -> f32 {
        if node.vertical_vel == VerticalVel::None
            && is_block_passable(&node.pos.up(2), world)
            && is_standable(&(node.pos + self.offset()), world)
        {
            WALK_ONE_BLOCK_COST + JUMP_COST
        } else {
            f32::INFINITY
        }
    }
    fn offset(&self) -> BlockPos {
        BlockPos::new(self.0.x(), 1, self.0.z())
    }
    fn next_node(&self, node: &Node) -> Node {
        Node {
            pos: node.pos + self.offset(),
            vertical_vel: VerticalVel::None,
        }
    }
}
pub struct DescendMove(pub CardinalDirection);
impl Move for DescendMove {
    fn cost(&self, world: &Instance, node: &Node) -> f32 {
        // check whether 3 blocks vertically forward are passable
        if node.vertical_vel == VerticalVel::None
            && is_standable(&(node.pos + self.offset()), world)
            && is_block_passable(&(node.pos + self.offset().up(2)), world)
        {
            WALK_ONE_BLOCK_COST
        } else {
            f32::INFINITY
        }
    }
    fn offset(&self) -> BlockPos {
        BlockPos::new(self.0.x(), -1, self.0.z())
    }
    fn next_node(&self, node: &Node) -> Node {
        Node {
            pos: node.pos + self.offset(),
            vertical_vel: VerticalVel::None,
        }
    }
}
pub struct DiagonalMove(pub CardinalDirection);
impl Move for DiagonalMove {
    fn cost(&self, world: &Instance, node: &Node) -> f32 {
        if node.vertical_vel != VerticalVel::None {
            return f32::INFINITY;
        }
        if !is_passable(
            &BlockPos::new(node.pos.x + self.0.x(), node.pos.y, node.pos.z + self.0.z()),
            world,
        ) && !is_passable(
            &BlockPos::new(
                node.pos.x + self.0.right().x(),
                node.pos.y,
                node.pos.z + self.0.right().z(),
            ),
            world,
        ) {
            return f32::INFINITY;
        }
        if !is_standable(&(node.pos + self.offset()), world) {
            return f32::INFINITY;
        }
        WALK_ONE_BLOCK_COST * 1.4
    }
    fn offset(&self) -> BlockPos {
        let right = self.0.right();
        BlockPos::new(self.0.x() + right.x(), 0, self.0.z() + right.z())
    }
    fn next_node(&self, node: &Node) -> Node {
        Node {
            pos: node.pos + self.offset(),
            vertical_vel: VerticalVel::None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use azalea_block::BlockState;
    use azalea_core::ChunkPos;
    use azalea_world::{Chunk, ChunkStorage, PartialInstance};

    #[test]
    fn test_is_passable() {
        let mut partial_world = PartialInstance::default();
        let mut chunk_storage = ChunkStorage::default();

        partial_world.chunks.set(
            &ChunkPos { x: 0, z: 0 },
            Some(Chunk::default()),
            &mut chunk_storage,
        );
        partial_world.chunks.set_block_state(
            &BlockPos::new(0, 0, 0),
            azalea_registry::Block::Stone.into(),
            &mut chunk_storage,
        );
        partial_world.chunks.set_block_state(
            &BlockPos::new(0, 1, 0),
            BlockState::AIR,
            &mut chunk_storage,
        );

        let world = chunk_storage.into();
        assert_eq!(is_block_passable(&BlockPos::new(0, 0, 0), &world), false);
        assert_eq!(is_block_passable(&BlockPos::new(0, 1, 0), &world), true);
    }

    #[test]
    fn test_is_solid() {
        let mut partial_world = PartialInstance::default();
        let mut chunk_storage = ChunkStorage::default();
        partial_world.chunks.set(
            &ChunkPos { x: 0, z: 0 },
            Some(Chunk::default()),
            &mut chunk_storage,
        );
        partial_world.chunks.set_block_state(
            &BlockPos::new(0, 0, 0),
            azalea_registry::Block::Stone.into(),
            &mut chunk_storage,
        );
        partial_world.chunks.set_block_state(
            &BlockPos::new(0, 1, 0),
            BlockState::AIR,
            &mut chunk_storage,
        );

        let world = chunk_storage.into();
        assert_eq!(is_block_solid(&BlockPos::new(0, 0, 0), &world), true);
        assert_eq!(is_block_solid(&BlockPos::new(0, 1, 0), &world), false);
    }

    #[test]
    fn test_is_standable() {
        let mut partial_world = PartialInstance::default();
        let mut chunk_storage = ChunkStorage::default();
        partial_world.chunks.set(
            &ChunkPos { x: 0, z: 0 },
            Some(Chunk::default()),
            &mut chunk_storage,
        );
        partial_world.chunks.set_block_state(
            &BlockPos::new(0, 0, 0),
            azalea_registry::Block::Stone.into(),
            &mut chunk_storage,
        );
        partial_world.chunks.set_block_state(
            &BlockPos::new(0, 1, 0),
            BlockState::AIR,
            &mut chunk_storage,
        );
        partial_world.chunks.set_block_state(
            &BlockPos::new(0, 2, 0),
            BlockState::AIR,
            &mut chunk_storage,
        );
        partial_world.chunks.set_block_state(
            &BlockPos::new(0, 3, 0),
            BlockState::AIR,
            &mut chunk_storage,
        );

        let world = chunk_storage.into();
        assert!(is_standable(&BlockPos::new(0, 1, 0), &world));
        assert!(!is_standable(&BlockPos::new(0, 0, 0), &world));
        assert!(!is_standable(&BlockPos::new(0, 2, 0), &world));
    }
}