1
0
mirror of https://git.ngram.ca/OpenJam/rc-servers synced 2026-08-23 23:08:52 +00:00

Implement factory offset saving

This commit is contained in:
NG (Graham)
2026-01-05 16:21:14 -05:00
parent 49255e7e7c
commit 437b7f9fdd
8 changed files with 166 additions and 18 deletions

View File

@@ -9,12 +9,16 @@ pub use cpu_count::CpuListParser;
mod locations_of;
pub use locations_of::{CubeLocationsParser, CubeLocationInfo};
mod offsetter;
pub use offsetter::OffsetParser;
//pub mod prefabs;
pub struct CubeParsers {
weapon_list: std::sync::Arc<WeaponListParser>,
cpu_counter: std::sync::Arc<CpuListParser>,
locations: std::sync::Arc<CubeLocationsParser>,
offset: std::sync::Arc<OffsetParser>,
}
impl CubeParsers {
@@ -24,6 +28,7 @@ impl CubeParsers {
weapon_list: std::sync::Arc::new(WeaponListParser::with_cubes(cubes.values())),
cpu_counter: std::sync::Arc::new(CpuListParser::with_cubes(cubes.values())),
locations: std::sync::Arc::new(CubeLocationsParser::with_cubes(cubes.values())),
offset: std::sync::Arc::new(OffsetParser::with_cubes(cubes.values())),
}
}
@@ -38,4 +43,8 @@ impl CubeParsers {
pub fn locations_of(&self) -> std::sync::Arc<CubeLocationsParser> {
self.locations.clone()
}
pub fn offset(&self) -> std::sync::Arc<OffsetParser> {
self.offset.clone()
}
}

View File

@@ -0,0 +1,26 @@
pub struct OffsetParser;
impl OffsetParser {
pub fn with_cubes<'a, I: std::iter::Iterator<Item=&'a crate::persist::Cube>>(_iter: I) -> Self {
Self
}
/// offset is (x, y, z)
pub fn offset_inplace_by(&self, cubes: &mut [u8], colours: &mut [u8], offset: (i16, i16, i16)) {
// this assumes cubes and colours are valid and length-prefixed
let cubes = &mut cubes[4..];
let colours = &mut colours[4..];
for cube in cubes.chunks_mut(8) {
// bytes 4, 5, 6 are x, y, z (respectively)
cube[4] = (cube[4] as i16 + offset.0) as _;
cube[5] = (cube[5] as i16 + offset.1) as _;
cube[6] = (cube[6] as i16 + offset.2) as _;
}
for colour in colours.chunks_mut(4) {
// last 3 bytes are x, y, z (respectively)
colour[1] = (colour[1] as i16 + offset.0) as _;
colour[2] = (colour[2] as i16 + offset.1) as _;
colour[3] = (colour[3] as i16 + offset.2) as _;
}
}
}

View File

@@ -85,4 +85,19 @@ impl Colour {
}
Ok(cubes)
}
pub fn dump(&self, w: &mut dyn std::io::Write) -> std::io::Result<usize> {
w.write_all(&[self.colour, self.x, self.y, self.z])?;
Ok(4)
}
pub fn dump_list(items: Vec<Self>) -> std::io::Result<Vec<u8>> {
let mut buf = Vec::with_capacity(4 + (items.len() * 8));
let mut dumped = std::io::Cursor::new(&mut buf);
dumped.write_all(&(items.len() as u32).to_le_bytes())?;
for item in items {
item.dump(&mut dumped)?;
}
Ok(buf)
}
}

View File

@@ -49,6 +49,14 @@ impl oj_rc_factory::VehicleFactoryAdapter for Factory {
Self::None => Ok(()),
}
}
async fn update_vehicle(&self, id: i32, cube_data: Option<Vec<u8>>, colour_data: Option<Vec<u8>>) -> Result<(), Box<dyn std::error::Error>> {
match self {
Self::Arc(x) => x.update_vehicle(id, cube_data, colour_data).await,
Self::Custom(x) => x.update_vehicle(id, cube_data, colour_data).await,
Self::None => Ok(()),
}
}
}
impl Factory {