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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
use azalea_core::{
aabb::AABB,
direction::Direction,
hit_result::{BlockHitResult, EntityHitResult, HitResult},
position::Vec3,
};
use azalea_entity::{
Attributes, Dead, EyeHeight, LocalEntity, LookDirection, Physics, Position,
metadata::{ArmorStandMarker, Marker},
view_vector,
};
use azalea_physics::{
clip::{BlockShapeType, ClipContext, FluidPickType},
collision::entity_collisions::{PhysicsQuery, get_entities},
};
use azalea_world::{Instance, InstanceContainer, InstanceName};
use bevy_ecs::prelude::*;
use derive_more::{Deref, DerefMut};
/// A component that contains the block or entity that the player is currently
/// looking at.
#[doc(alias("looking at", "looking at block", "crosshair"))]
#[derive(Component, Clone, Debug, Deref, DerefMut)]
pub struct HitResultComponent(HitResult);
#[allow(clippy::type_complexity)]
pub fn update_hit_result_component(
mut commands: Commands,
mut query: Query<
(
Entity,
Option<&mut HitResultComponent>,
&Position,
&EyeHeight,
&LookDirection,
&InstanceName,
&Physics,
&Attributes,
),
With<LocalEntity>,
>,
instance_container: Res<InstanceContainer>,
physics_query: PhysicsQuery,
pickable_query: PickableEntityQuery,
) {
for (
entity,
hit_result_ref,
position,
eye_height,
look_direction,
world_name,
physics,
attributes,
) in &mut query
{
let block_pick_range = attributes.block_interaction_range.calculate();
let entity_pick_range = attributes.entity_interaction_range.calculate();
let eye_position = position.up(eye_height.into());
let Some(world_lock) = instance_container.get(world_name) else {
continue;
};
let world = world_lock.read();
let hit_result = pick(PickOpts {
source_entity: entity,
look_direction: *look_direction,
eye_position,
aabb: &physics.bounding_box,
world: &world,
entity_pick_range,
block_pick_range,
physics_query: &physics_query,
pickable_query: &pickable_query,
});
if let Some(mut hit_result_ref) = hit_result_ref {
**hit_result_ref = hit_result;
} else {
commands
.entity(entity)
.insert(HitResultComponent(hit_result));
}
}
}
pub type PickableEntityQuery<'world, 'state, 'a> = Query<
'world,
'state,
Option<&'a ArmorStandMarker>,
(Without<Dead>, Without<Marker>, Without<LocalEntity>),
>;
pub struct PickOpts<'world, 'state, 'a, 'b, 'c> {
source_entity: Entity,
look_direction: LookDirection,
eye_position: Vec3,
aabb: &'a AABB,
world: &'a Instance,
entity_pick_range: f64,
block_pick_range: f64,
physics_query: &'a PhysicsQuery<'world, 'state, 'b>,
pickable_query: &'a PickableEntityQuery<'world, 'state, 'c>,
}
/// Get the block or entity that a player would be looking at if their eyes were
/// at the given direction and position.
///
/// If you need to get the block/entity the player is looking at right now, use
/// [`HitResultComponent`].
///
/// Also see [`pick_block`].
pub fn pick(opts: PickOpts<'_, '_, '_, '_, '_>) -> HitResult {
// vanilla does extra math here to calculate the pick result in between ticks by
// interpolating, but since clients can still only interact on exact ticks, that
// isn't relevant for us.
let mut max_range = opts.entity_pick_range.max(opts.block_pick_range);
let mut max_range_squared = max_range.powi(2);
let block_hit_result = pick_block(
opts.look_direction,
opts.eye_position,
&opts.world.chunks,
max_range,
);
let block_hit_result_dist_squared = block_hit_result
.location
.distance_squared_to(opts.eye_position);
if !block_hit_result.miss {
max_range_squared = block_hit_result_dist_squared;
max_range = block_hit_result_dist_squared.sqrt();
}
let view_vector = view_vector(opts.look_direction);
let end_position = opts.eye_position + (view_vector * max_range);
let inflate_by = 1.;
let pick_aabb = opts
.aabb
.expand_towards(view_vector * max_range)
.inflate_all(inflate_by);
let is_pickable = |entity: Entity| {
// TODO: ender dragon and projectiles have extra logic here. also, we shouldn't
// be able to pick spectators.
if let Ok(armor_stand_marker) = opts.pickable_query.get(entity) {
if let Some(armor_stand_marker) = armor_stand_marker
&& armor_stand_marker.0
{
false
} else {
true
}
} else {
true
}
};
let entity_hit_result = pick_entity(PickEntityOpts {
source_entity: opts.source_entity,
eye_position: opts.eye_position,
end_position,
world: opts.world,
pick_range_squared: max_range_squared,
predicate: &is_pickable,
aabb: &pick_aabb,
physics_query: opts.physics_query,
});
if let Some(entity_hit_result) = entity_hit_result
&& entity_hit_result
.location
.distance_squared_to(opts.eye_position)
< block_hit_result_dist_squared
{
filter_hit_result(
HitResult::Entity(entity_hit_result),
opts.eye_position,
opts.entity_pick_range,
)
} else {
filter_hit_result(
HitResult::Block(block_hit_result),
opts.eye_position,
opts.block_pick_range,
)
}
}
fn filter_hit_result(hit_result: HitResult, eye_position: Vec3, range: f64) -> HitResult {
let location = hit_result.location();
if !location.closer_than(eye_position, range) {
let direction = Direction::nearest(location - eye_position);
HitResult::new_miss(location, direction, location.into())
} else {
hit_result
}
}
/// Get the block that a player would be looking at if their eyes were at the
/// given direction and position.
///
/// Also see [`pick`].
pub fn pick_block(
look_direction: LookDirection,
eye_position: Vec3,
chunks: &azalea_world::ChunkStorage,
pick_range: f64,
) -> BlockHitResult {
let view_vector = view_vector(look_direction);
let end_position = eye_position + (view_vector * pick_range);
azalea_physics::clip::clip(
chunks,
ClipContext {
from: eye_position,
to: end_position,
block_shape_type: BlockShapeType::Outline,
fluid_pick_type: FluidPickType::None,
},
)
}
struct PickEntityOpts<'world, 'state, 'a, 'b> {
source_entity: Entity,
eye_position: Vec3,
end_position: Vec3,
world: &'a azalea_world::Instance,
pick_range_squared: f64,
predicate: &'a dyn Fn(Entity) -> bool,
aabb: &'a AABB,
physics_query: &'a PhysicsQuery<'world, 'state, 'b>,
}
// port of getEntityHitResult
fn pick_entity(opts: PickEntityOpts) -> Option<EntityHitResult> {
let mut picked_distance_squared = opts.pick_range_squared;
let mut result = None;
for (candidate, candidate_aabb) in get_entities(
opts.world,
Some(opts.source_entity),
opts.aabb,
opts.predicate,
opts.physics_query,
) {
// TODO: if the entity is "REDIRECTABLE_PROJECTILE" then this should be 1.0.
// azalea needs support for entity tags first for this to be possible. see
// getPickRadius in decompiled minecraft source
let candidate_pick_radius = 0.;
let candidate_aabb = candidate_aabb.inflate_all(candidate_pick_radius);
let clip_location = candidate_aabb.clip(opts.eye_position, opts.end_position);
if candidate_aabb.contains(opts.eye_position) {
if picked_distance_squared >= 0. {
result = Some(EntityHitResult {
location: clip_location.unwrap_or(opts.eye_position),
entity: candidate,
});
picked_distance_squared = 0.;
}
} else if let Some(clip_location) = clip_location {
let distance_squared = opts.eye_position.distance_squared_to(clip_location);
if distance_squared < picked_distance_squared || picked_distance_squared == 0. {
// TODO: don't pick the entity we're riding on
// if candidate_root_vehicle == entity_root_vehicle {
// if picked_distance_squared == 0. {
// picked_entity = Some(candidate);
// picked_location = Some(clip_location);
// }
// } else {
result = Some(EntityHitResult {
location: clip_location,
entity: candidate,
});
picked_distance_squared = distance_squared;
}
}
}
result
}
|