azalea/pathfinder/moves/
parkour.rs1use azalea_client::{SprintDirection, WalkDirection};
2use azalea_core::{direction::CardinalDirection, position::BlockPos};
3use azalea_physics::collision::BlockWithShape;
4use tracing::trace;
5
6use super::{Edge, ExecuteCtx, IsReachedCtx, MoveData, MovesCtx};
7use crate::pathfinder::{astar, costs::*, player_pos_to_block_pos, positions::RelBlockPos};
8
9pub fn parkour_move(ctx: &mut MovesCtx, node: RelBlockPos) {
10 if !ctx.world.is_block_solid(node.down(1)) {
11 return;
14 }
15
16 parkour_forward_1_move(ctx, node);
17 parkour_forward_2_move(ctx, node);
18 parkour_forward_3_move(ctx, node);
19}
20
21fn parkour_forward_1_move(ctx: &mut MovesCtx, pos: RelBlockPos) {
22 for dir in CardinalDirection::iter() {
23 let gap_offset = RelBlockPos::new(dir.x(), 0, dir.z());
24 let offset = RelBlockPos::new(dir.x() * 2, 0, dir.z() * 2);
25
26 if ctx.world.is_block_standable((pos + gap_offset).down(1)) {
28 continue;
29 }
30 if !ctx.world.is_passable(pos + gap_offset) {
31 continue;
32 }
33
34 let ascend: i32 = if ctx.world.is_standable(pos + offset.up(1)) {
35 1
37 } else if ctx.world.is_standable(pos + offset) {
38 0
43 } else {
44 continue;
45 };
46
47 if !ctx.world.is_block_passable((pos + gap_offset).up(2)) {
49 continue;
50 }
51
52 if !ctx.world.is_block_passable(pos.up(2)) {
54 continue;
55 }
56 if !ctx.world.is_block_passable((pos + offset).up(2)) {
58 continue;
59 }
60 let cost = JUMP_PENALTY + WALK_ONE_BLOCK_COST * 2.;
61
62 ctx.edges.push(Edge {
63 movement: astar::Movement {
64 target: pos + offset.up(ascend),
65 data: MoveData {
66 execute: &execute_parkour_move,
67 is_reached: &parkour_is_reached,
68 },
69 },
70 cost,
71 })
72 }
73}
74
75fn parkour_forward_2_move(ctx: &mut MovesCtx, pos: RelBlockPos) {
76 'dir: for dir in CardinalDirection::iter() {
77 let gap_1_offset = RelBlockPos::new(dir.x(), 0, dir.z());
78 let gap_2_offset = RelBlockPos::new(dir.x() * 2, 0, dir.z() * 2);
79 let offset = RelBlockPos::new(dir.x() * 3, 0, dir.z() * 3);
80
81 if ctx.world.is_block_standable((pos + gap_1_offset).down(1))
83 || ctx.world.is_block_standable((pos + gap_2_offset).down(1))
84 {
85 continue;
86 }
87
88 let mut cost = JUMP_PENALTY + WALK_ONE_BLOCK_COST * 3.;
89
90 let ascend: i32 = if ctx.world.is_standable(pos + offset.up(1)) {
91 1
92 } else if ctx.world.is_standable(pos + offset) {
93 0
94 } else if ctx.world.is_standable(pos + offset.down(1)) {
95 cost += FALL_N_BLOCKS_COST[2] / 2.;
98 -1
99 } else {
100 continue;
101 };
102
103 for offset in [gap_1_offset, gap_2_offset] {
105 if !ctx.world.is_passable(pos + offset) {
106 continue 'dir;
107 }
108 if !ctx.world.is_block_passable((pos + offset).up(2)) {
109 continue 'dir;
110 }
111 }
112 if !ctx.world.is_block_passable(pos.up(2)) {
114 continue;
115 }
116 if !ctx.world.is_block_passable((pos + offset).up(2)) {
118 continue;
119 }
120
121 ctx.edges.push(Edge {
122 movement: astar::Movement {
123 target: pos + offset.up(ascend),
124 data: MoveData {
125 execute: &execute_parkour_move,
126 is_reached: &parkour_is_reached,
127 },
128 },
129 cost,
130 })
131 }
132}
133
134fn parkour_forward_3_move(ctx: &mut MovesCtx, pos: RelBlockPos) {
135 'dir: for dir in CardinalDirection::iter() {
136 let gap_1_offset = RelBlockPos::new(dir.x(), 0, dir.z());
137 let gap_2_offset = RelBlockPos::new(dir.x() * 2, 0, dir.z() * 2);
138 let gap_3_offset = RelBlockPos::new(dir.x() * 3, 0, dir.z() * 3);
139 let offset = RelBlockPos::new(dir.x() * 4, 0, dir.z() * 4);
140
141 if ctx.world.is_block_standable((pos + gap_1_offset).down(1))
143 || ctx.world.is_block_standable((pos + gap_2_offset).down(1))
144 || ctx.world.is_block_standable((pos + gap_3_offset).down(1))
145 {
146 continue;
147 }
148
149 if !ctx.world.is_standable(pos + offset) {
150 continue;
151 };
152
153 for offset in [gap_1_offset, gap_2_offset, gap_3_offset] {
155 if !ctx.world.is_passable(pos + offset) {
156 continue 'dir;
157 }
158 if !ctx.world.is_block_passable((pos + offset).up(2)) {
159 continue 'dir;
160 }
161 }
162 if !ctx.world.is_block_passable(pos.up(2)) {
164 continue;
165 }
166 if !ctx.world.is_block_passable((pos + offset).up(2)) {
168 continue;
169 }
170
171 let cost = JUMP_PENALTY + SPRINT_ONE_BLOCK_COST * 4.;
172
173 ctx.edges.push(Edge {
174 movement: astar::Movement {
175 target: pos + offset,
176 data: MoveData {
177 execute: &execute_parkour_move,
178 is_reached: &parkour_is_reached,
179 },
180 },
181 cost,
182 })
183 }
184}
185
186fn execute_parkour_move(mut ctx: ExecuteCtx) {
187 let ExecuteCtx {
188 position,
189 target,
190 start,
191 ..
192 } = ctx;
193
194 let start_center = start.center();
195 let target_center = target.center();
196
197 let jump_distance = i32::max((target - start).x.abs(), (target - start).z.abs());
198
199 let ascend: i32 = target.y - start.y;
200
201 if jump_distance >= 4 || (ascend > 0 && jump_distance >= 3) {
202 ctx.sprint(SprintDirection::Forward);
204 } else {
205 ctx.walk(WalkDirection::Forward);
206 }
207
208 let x_dir = (target.x - start.x).clamp(-1, 1);
209 let z_dir = (target.z - start.z).clamp(-1, 1);
210 let dir = BlockPos::new(x_dir, 0, z_dir);
211 let jump_at_pos = start + dir;
212
213 let is_at_start_block = player_pos_to_block_pos(position) == start;
214 let is_at_jump_block = player_pos_to_block_pos(position) == jump_at_pos;
215
216 let required_distance_from_center = if jump_distance <= 2 {
217 0.0
219 } else {
220 0.6
221 };
222 let distance_from_start = f64::max(
223 (position.x - start_center.x).abs(),
224 (position.z - start_center.z).abs(),
225 );
226
227 if !is_at_start_block
228 && !is_at_jump_block
229 && (position.y - start.y as f64) < 0.094
230 && distance_from_start < 0.85
231 {
232 ctx.look_at(start_center);
234 trace!("looking at start_center");
235 } else {
236 ctx.look_at(target_center);
237 trace!("looking at target_center");
238
239 if !ctx.physics.on_ground() && ctx.physics.velocity.y.abs() < 0.25 {
243 let should_sneak = {
244 let world = ctx.world.read();
245 let pos_above = ctx.position.up(1.8 + 0.25);
246 let block_pos_above = BlockPos::from(pos_above);
247 let block_pos_above_plus_velocity =
248 BlockPos::from(pos_above + ctx.physics.velocity.with_y(0.) * 5.);
249
250 let block_above = world.get_block_state(block_pos_above).unwrap_or_default();
251 let block_above_plus_velocity = world
252 .get_block_state(block_pos_above_plus_velocity)
253 .unwrap_or_default();
254
255 !block_above.is_collision_shape_full()
257 && !block_above_plus_velocity.is_collision_shape_empty()
258 };
259 if should_sneak {
260 ctx.sneak();
261 }
262 }
263 }
264
265 if !is_at_start_block && is_at_jump_block && distance_from_start > required_distance_from_center
266 {
267 ctx.jump();
268 }
269}
270
271#[must_use]
272pub fn parkour_is_reached(
273 IsReachedCtx {
274 position,
275 target,
276 physics,
277 ..
278 }: IsReachedCtx,
279) -> bool {
280 if player_pos_to_block_pos(position) == target && (position.y - target.y as f64) < 0.094 {
282 return true;
283 }
284
285 player_pos_to_block_pos(position) == target && physics.on_ground()
287}