Skip to main content

azalea_protocol/packets/game/
c_move_entity_pos_rot.rs

1use std::io::{self, Cursor, Write};
2
3use azalea_buf::{AzBuf, AzBufVar, BufReadError};
4use azalea_core::{delta::PositionDelta8, entity_id::MinecraftEntityId};
5use azalea_entity::{LookDirection, vec_delta_codec::VecDeltaCodec};
6use azalea_protocol_macros::ClientboundGamePacket;
7
8use crate::packets::game::c_entity_position_sync::{PositionPath, PositionStep};
9
10/// This packet is sent by the server when an entity moves less then 8 blocks.
11#[derive(ClientboundGamePacket, Clone, Debug, PartialEq)]
12pub struct ClientboundMoveEntityPosRot {
13    pub entity_id: MinecraftEntityId,
14    pub delta: VecDelta,
15    pub look_direction: CompactLookDirection,
16    pub properties: Properties,
17}
18impl AzBuf for ClientboundMoveEntityPosRot {
19    fn azalea_read(
20        ctx: azalea_buf::Ctx,
21        buf: &mut Cursor<&[u8]>,
22    ) -> Result<Self, azalea_buf::BufReadError> {
23        read_move_entity_packet(ctx, buf, true)
24    }
25    fn azalea_write(&self, ctx: azalea_buf::Ctx, buf: &mut impl Write) -> io::Result<()> {
26        write_move_entity_packet(ctx, buf, self, true)
27    }
28}
29
30pub fn read_move_entity_packet(
31    ctx: azalea_buf::Ctx,
32    buf: &mut Cursor<&[u8]>,
33    include_look_direction: bool,
34) -> Result<ClientboundMoveEntityPosRot, BufReadError> {
35    let entity_id = MinecraftEntityId::azalea_read_var(ctx, buf)?;
36    let properties = Properties::azalea_read(ctx, buf)?;
37
38    let delta = VecDelta::read_with_steps(ctx, buf, properties.step_count())?;
39
40    let look_direction = if include_look_direction {
41        CompactLookDirection::azalea_read(ctx, buf)?
42    } else {
43        Default::default()
44    };
45
46    Ok(ClientboundMoveEntityPosRot {
47        entity_id,
48        properties,
49        delta,
50        look_direction,
51    })
52}
53pub fn write_move_entity_packet(
54    ctx: azalea_buf::Ctx,
55    buf: &mut impl Write,
56    p: &ClientboundMoveEntityPosRot,
57    include_look_direction: bool,
58) -> io::Result<()> {
59    p.entity_id.azalea_write_var(ctx, buf)?;
60    p.properties.azalea_write(ctx, buf)?;
61    p.delta.write_and_get_steps(ctx, buf)?;
62
63    if include_look_direction {
64        p.look_direction.azalea_write(ctx, buf)?;
65    };
66
67    Ok(())
68}
69
70#[derive(Clone, Debug, PartialEq)]
71pub enum VecDelta {
72    Linear(PositionDelta8),
73    Stepped(Vec<DeltaStep>),
74}
75#[derive(AzBuf, Clone, Debug, PartialEq)]
76pub struct DeltaStep {
77    #[var]
78    pub ticks: i32,
79    pub pos: PositionDelta8,
80}
81impl VecDelta {
82    pub fn read_with_steps(
83        ctx: azalea_buf::Ctx,
84        buf: &mut Cursor<&[u8]>,
85        step_count: i32,
86    ) -> Result<Self, azalea_buf::BufReadError> {
87        if step_count <= 0 {
88            return Ok(Self::Linear(PositionDelta8::azalea_read(ctx, buf)?));
89        }
90
91        let mut steps = Vec::new();
92        for _ in 0..step_count {
93            steps.push(DeltaStep::azalea_read(ctx, buf)?);
94        }
95        Ok(Self::Stepped(steps))
96    }
97    pub fn write_and_get_steps(
98        &self,
99        ctx: azalea_buf::Ctx,
100        buf: &mut impl Write,
101    ) -> io::Result<i32> {
102        match self {
103            VecDelta::Linear(v) => {
104                v.azalea_write(ctx, buf)?;
105                Ok(0)
106            }
107            VecDelta::Stepped(steps) => {
108                for s in steps {
109                    s.azalea_write(ctx, buf)?;
110                }
111                Ok(steps.len() as i32)
112            }
113        }
114    }
115
116    pub fn decode(&self, codec: &VecDeltaCodec) -> PositionPath {
117        match self {
118            VecDelta::Linear(p) => PositionPath::Linear(codec.decode(p)),
119            VecDelta::Stepped(steps) => {
120                if steps.is_empty() {
121                    return PositionPath::Linear(codec.base());
122                }
123                let mut position_steps = Vec::with_capacity(steps.len());
124                let mut codec = codec.clone();
125
126                for step in steps {
127                    let pos = codec.decode(&step.pos);
128                    position_steps.push(PositionStep {
129                        pos,
130                        tick_offset: step.ticks,
131                    });
132                    codec.set_base(pos);
133                }
134                PositionPath::Stepped(position_steps)
135            }
136        }
137    }
138}
139
140#[derive(AzBuf, Copy, Clone, Debug, PartialEq, Default)]
141pub struct Properties {
142    #[var]
143    data: u32,
144}
145impl Properties {
146    pub fn new() -> Self {
147        Self::default()
148    }
149
150    pub fn on_ground(self) -> bool {
151        self.data & 0b1 != 0
152    }
153    pub fn with_on_ground(mut self, value: bool) -> Self {
154        if value {
155            self.data |= 0b1;
156        } else {
157            self.data = !(!self.data | 0b1);
158        }
159        self
160    }
161
162    pub fn step_count(self) -> i32 {
163        (self.data >> 1) as i32
164    }
165    pub fn with_step_count(mut self, value: u32) -> Self {
166        self.data = (value << 1) | (self.on_ground() as u32);
167        self
168    }
169}
170
171#[derive(AzBuf, Clone, Copy, Debug, PartialEq, Default)]
172pub struct CompactLookDirection {
173    pub y_rot: i8,
174    pub x_rot: i8,
175}
176
177impl From<CompactLookDirection> for LookDirection {
178    fn from(l: CompactLookDirection) -> Self {
179        LookDirection::new(
180            (l.y_rot as i32 * 360) as f32 / 256.,
181            (l.x_rot as i32 * 360) as f32 / 256.,
182        )
183    }
184}