Skip to content

Commit

Permalink
Close #9
Browse files Browse the repository at this point in the history
  • Loading branch information
widberg committed Sep 28, 2023
1 parent bfe2036 commit 338767e
Showing 1 changed file with 74 additions and 8 deletions.
82 changes: 74 additions & 8 deletions bff/src/class/gw_road/v1_381_67_09_pc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use binrw::BinRead;
use std::io::{Read, Seek, Write};

use bilge::prelude::*;
use binrw::{BinRead, BinResult, BinWrite, BinWriterExt, Endian};
use derive_more::{Deref, DerefMut};
use serde::Serialize;

use crate::class::trivial_class::TrivialClass;
Expand All @@ -7,17 +11,79 @@ use crate::link_header::ResourceObjectLinkHeader;
use crate::math::Vec2f;
use crate::name::Name;

#[derive(BinRead, Debug, Serialize)]
struct Point {
#[br(big)]
encoded_vec2f_data0: i32,
encoded_vec2f_data1: i8,
#[bitsize(7)]
#[derive(TryFromBits, Debug, Serialize)]
enum SubType {
ShortCutForest = 0,
ShortCutField = 1,
ShortCutShort = 2,
ShortCutLong = 3,
FieldRoad = 4,
GoatPath = 5, // Bike trail lined with thin posts
SmallDirtRoad = 6, // Vehicle trail lines with A-frames
SnowyDirtRoad = 7,
NormalDirtRoad = 8, // Two vehicle roads lined with signs and guard rails
BigDirtRoad = 9, // Even wider I guess
SmallCircuitTrack = 10, // Thin track around Redrock Bluffs
SmallTarmacRoad = 11,
NormalTarmacRoad = 12,
BigTarmacRoad = 13,
River = 14,
CircuitTrack = 15, // Small tracks near Offshore Shack
SaltRoad = 16,
Bridge = 17,
HighWay = 18,
}

#[bitsize(8)]
#[derive(BinRead, DebugBits, SerializeBits)]
struct RoadType {
sub_type: SubType,
short_cut: u1,
}

#[derive(Debug, Serialize, Deref, DerefMut)]
struct EncodedPoint(Vec2f);

impl BinRead for EncodedPoint {
type Args<'a> = ();

fn read_options<R: Read + Seek>(
reader: &mut R,
_endian: Endian,
_args: Self::Args<'_>,
) -> BinResult<Self> {
let a = i32::read_be(reader)?;
let b = u8::read_be(reader)?;
Ok(EncodedPoint([
(a >> 12) as f32 / 4.,
(((b as i32 | (a << 8)) << 12) >> 12) as f32 / 4.,
]))
}
}

impl BinWrite for EncodedPoint {
type Args<'a> = ();

fn write_options<W: Write + Seek>(
&self,
writer: &mut W,
_endian: Endian,
_args: Self::Args<'_>,
) -> BinResult<()> {
let a: i32 = ((self[0] * 4.) as i32) << 12;
let b: u8 = (self[1] * 4.) as u8;

writer.write_be(&a)?;
writer.write_be(&b)?;
Ok(())
}
}

#[derive(BinRead, Debug, Serialize)]
struct Road {
r#type: u8,
points: DynArray<Point, u16>,
r#type: RoadType,
points: DynArray<EncodedPoint, u16>,
}

#[derive(BinRead, Debug, Serialize)]
Expand Down

0 comments on commit 338767e

Please sign in to comment.