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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
mod data;
mod dimensions;
use crate::Dimension;
use azalea_block::BlockState;
use azalea_core::{BlockPos, Vec3, AABB};
pub use data::*;
pub use dimensions::*;
use std::ops::{Deref, DerefMut};
use std::ptr::NonNull;
use uuid::Uuid;
#[derive(Debug)]
pub struct EntityRef<'d> {
/// The dimension this entity is in.
pub dimension: &'d Dimension,
/// The incrementing numerical id of the entity.
pub id: u32,
pub data: &'d EntityData,
}
impl<'d> EntityRef<'d> {
pub fn new(dimension: &'d Dimension, id: u32, data: &'d EntityData) -> Self {
// TODO: have this be based on the entity type
Self {
dimension,
id,
data,
}
}
}
impl<'d> EntityRef<'d> {
#[inline]
pub fn pos(&self) -> &Vec3 {
&self.pos
}
pub fn make_bounding_box(&self) -> AABB {
self.dimensions.make_bounding_box(self.pos())
}
/// Get the position of the block below the entity, but a little lower.
pub fn on_pos_legacy(&self) -> BlockPos {
self.on_pos(0.2)
}
// int x = Mth.floor(this.position.x);
// int y = Mth.floor(this.position.y - (double)var1);
// int z = Mth.floor(this.position.z);
// BlockPos var5 = new BlockPos(x, y, z);
// if (this.level.getBlockState(var5).isAir()) {
// BlockPos var6 = var5.below();
// BlockState var7 = this.level.getBlockState(var6);
// if (var7.is(BlockTags.FENCES) || var7.is(BlockTags.WALLS) || var7.getBlock() instanceof FenceGateBlock) {
// return var6;
// }
// }
// return var5;
pub fn on_pos(&self, offset: f32) -> BlockPos {
let x = self.pos().x.floor() as i32;
let y = (self.pos().y - offset as f64).floor() as i32;
let z = self.pos().z.floor() as i32;
let pos = BlockPos { x, y, z };
// TODO: check if block below is a fence, wall, or fence gate
let block_pos = pos.below();
let block_state = self.dimension.get_block_state(&block_pos);
if block_state == Some(BlockState::Air) {
let block_pos_below = block_pos.below();
let block_state_below = self.dimension.get_block_state(&block_pos_below);
if let Some(_block_state_below) = block_state_below {
// if block_state_below.is_fence()
// || block_state_below.is_wall()
// || block_state_below.is_fence_gate()
// {
// return block_pos_below;
// }
}
}
pos
}
}
#[derive(Debug)]
pub struct EntityMut<'d> {
/// The dimension this entity is in.
pub dimension: &'d mut Dimension,
/// The incrementing numerical id of the entity.
pub id: u32,
pub data: NonNull<EntityData>,
}
impl<'d> EntityMut<'d> {
pub fn new(dimension: &'d mut Dimension, id: u32, data: NonNull<EntityData>) -> Self {
Self {
dimension,
id,
data,
}
}
/// Sets the position of the entity. This doesn't update the cache in
/// azalea-world, and should only be used within azalea-world!
///
/// # Safety
/// Cached position in the dimension must be updated.
pub unsafe fn move_unchecked(&mut self, new_pos: Vec3) {
self.pos = new_pos;
let bounding_box = self.make_bounding_box();
self.bounding_box = bounding_box;
}
pub fn set_rotation(&mut self, y_rot: f32, x_rot: f32) {
self.y_rot = y_rot % 360.0;
self.x_rot = x_rot.clamp(-90.0, 90.0) % 360.0;
// TODO: minecraft also sets yRotO and xRotO to xRot and yRot ... but idk what they're used for so
}
pub fn move_relative(&mut self, speed: f32, acceleration: &Vec3) {
let input_vector = self.input_vector(speed, acceleration);
self.delta += input_vector;
}
pub fn input_vector(&self, speed: f32, acceleration: &Vec3) -> Vec3 {
let distance = acceleration.length_squared();
if distance < 1.0E-7 {
return Vec3::default();
}
let acceleration = if distance > 1.0 {
acceleration.normalize()
} else {
*acceleration
}
.scale(speed as f64);
let y_rot = f32::sin(self.y_rot * 0.017453292f32);
let x_rot = f32::cos(self.y_rot * 0.017453292f32);
Vec3 {
x: acceleration.x * (x_rot as f64) - acceleration.z * (y_rot as f64),
y: acceleration.y,
z: acceleration.z * (x_rot as f64) + acceleration.x * (y_rot as f64),
}
}
}
impl<'d> EntityMut<'d> {
#[inline]
pub fn pos(&self) -> &Vec3 {
&self.pos
}
pub fn make_bounding_box(&self) -> AABB {
self.dimensions.make_bounding_box(self.pos())
}
/// Get the position of the block below the entity, but a little lower.
pub fn on_pos_legacy(&self) -> BlockPos {
self.on_pos(0.2)
}
// int x = Mth.floor(this.position.x);
// int y = Mth.floor(this.position.y - (double)var1);
// int z = Mth.floor(this.position.z);
// BlockPos var5 = new BlockPos(x, y, z);
// if (this.level.getBlockState(var5).isAir()) {
// BlockPos var6 = var5.below();
// BlockState var7 = this.level.getBlockState(var6);
// if (var7.is(BlockTags.FENCES) || var7.is(BlockTags.WALLS) || var7.getBlock() instanceof FenceGateBlock) {
// return var6;
// }
// }
// return var5;
pub fn on_pos(&self, offset: f32) -> BlockPos {
let x = self.pos().x.floor() as i32;
let y = (self.pos().y - offset as f64).floor() as i32;
let z = self.pos().z.floor() as i32;
let pos = BlockPos { x, y, z };
// TODO: check if block below is a fence, wall, or fence gate
let block_pos = pos.below();
let block_state = self.dimension.get_block_state(&block_pos);
if block_state == Some(BlockState::Air) {
let block_pos_below = block_pos.below();
let block_state_below = self.dimension.get_block_state(&block_pos_below);
if let Some(_block_state_below) = block_state_below {
// if block_state_below.is_fence()
// || block_state_below.is_wall()
// || block_state_below.is_fence_gate()
// {
// return block_pos_below;
// }
}
}
pos
}
}
impl<'d> From<EntityMut<'d>> for EntityRef<'d> {
fn from(entity: EntityMut<'d>) -> EntityRef<'d> {
let data = unsafe { entity.data.as_ref() };
EntityRef {
dimension: entity.dimension,
id: entity.id,
data,
}
}
}
impl Deref for EntityMut<'_> {
type Target = EntityData;
fn deref(&self) -> &Self::Target {
unsafe { self.data.as_ref() }
}
}
impl DerefMut for EntityMut<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.data.as_mut() }
}
}
impl Deref for EntityRef<'_> {
type Target = EntityData;
fn deref(&self) -> &Self::Target {
self.data
}
}
#[derive(Debug)]
pub struct EntityData {
pub uuid: Uuid,
/// The position of the entity right now.
/// This can be changde with unsafe_move, but the correct way is with dimension.move_entity
pos: Vec3,
/// The position of the entity last tick.
pub last_pos: Vec3,
pub delta: Vec3,
/// X acceleration.
pub xxa: f32,
/// Y acceleration.
pub yya: f32,
/// Z acceleration.
pub zza: f32,
pub x_rot: f32,
pub y_rot: f32,
pub x_rot_last: f32,
pub y_rot_last: f32,
pub on_ground: bool,
pub last_on_ground: bool,
/// The width and height of the entity.
pub dimensions: EntityDimensions,
/// The bounding box of the entity. This is more than just width and height, unlike dimensions.
pub bounding_box: AABB,
/// Whether the entity will try to jump every tick
/// (equivalent to the space key being held down in vanilla).
pub jumping: bool,
}
impl EntityData {
pub fn new(uuid: Uuid, pos: Vec3) -> Self {
let dimensions = EntityDimensions {
width: 0.6,
height: 1.8,
};
Self {
uuid,
pos,
last_pos: pos,
delta: Vec3::default(),
xxa: 0.,
yya: 0.,
zza: 0.,
x_rot: 0.,
y_rot: 0.,
y_rot_last: 0.,
x_rot_last: 0.,
on_ground: false,
last_on_ground: false,
// TODO: have this be based on the entity type
bounding_box: dimensions.make_bounding_box(&pos),
dimensions,
jumping: false,
}
}
#[inline]
pub fn pos(&self) -> &Vec3 {
&self.pos
}
pub(crate) unsafe fn as_ptr(&mut self) -> NonNull<EntityData> {
NonNull::new_unchecked(self as *mut EntityData)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_mut_entity_to_ref_entity() {
let mut dim = Dimension::default();
let uuid = Uuid::from_u128(100);
dim.add_entity(0, EntityData::new(uuid, Vec3::default()));
let entity: EntityMut = dim.entity_mut(0).unwrap();
let entity_ref: EntityRef = entity.into();
assert_eq!(entity_ref.uuid, uuid);
}
}
|