Skip to content

Commit

Permalink
Even more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kokosha committed Dec 28, 2024
1 parent b12903f commit 3fee9e5
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions ragnarok_packets/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl From<&[isize; 2]> for Direction {
[-1, -1] => Direction::SW,
[-1, 0] => Direction::W,
[-1, 1] => Direction::NW,
_ => unreachable!(),
_ => panic!("impossible direction"),
}
}
}
Expand Down Expand Up @@ -118,11 +118,20 @@ pub struct WorldPosition2 {
pub y1: usize,
pub x2: usize,
pub y2: usize,
pub shift_x: isize,
pub shift_y: isize,
}

impl WorldPosition2 {
pub fn new(x1: usize, y1: usize, x2: usize, y2: usize) -> Self {
Self { x1, y1, x2, y2 }
Self {
x1,
y1,
x2,
y2,
shift_x: 0,
shift_y: 0,
}
}

pub fn to_origin_destination(self) -> (WorldPosition, WorldPosition) {
Expand All @@ -149,11 +158,17 @@ impl FromBytes for WorldPosition2 {
let y1 = (coordinates[2] >> 4) | ((coordinates[1] & 0b111111) << 4);
let x2 = (coordinates[3] >> 2) | ((coordinates[2] & 0b1111) << 6);
let y2 = coordinates[4] | ((coordinates[3] & 0b11) << 8);
/* Not related with direction at all is bigger than 7, it seems to be always 8.
let reserved1 = coordinates[5] >> 4;
let reserved2 = coordinates[5] & 0b1111;*/
let shift_x = (coordinates[5] >> 4) as isize - 8;
let shift_y = (coordinates[5] & 0b1111) as isize - 8;

Ok(Self { x1, y1, x2, y2 })
Ok(Self {
x1,
y1,
x2,
y2,
shift_x,
shift_y,
})
}
}

Expand All @@ -166,8 +181,7 @@ impl ToBytes for WorldPosition2 {
bytes[2] = ((self.y1 << 4) as u8) | ((self.x2 >> 6) as u8);
bytes[3] = ((self.x2 << 2) as u8) | ((self.y2 >> 8) as u8);
bytes[4] = self.y2 as u8;
/* Not related with direction at all is bigger than 7, it seems to be always 8.
bytes[5] = ((self.reserved1 << 4) as u8) || ((self.reserved2 & 0xF) as u8);*/
bytes[5] = (((self.shift_x + 8) << 4) as u8) | ((((self.shift_y + 8) as usize) & 0xF) as u8);

Ok(bytes)
}
Expand Down Expand Up @@ -206,6 +220,7 @@ mod conversion {
[0, 0, 255, 0, 0, 0],
[0, 0, 0, 255, 0, 0],
[0, 0, 0, 0, 255, 0],
[0, 0, 0, 0, 0, 255],
];

for case in cases {
Expand Down

0 comments on commit 3fee9e5

Please sign in to comment.