Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving land support #126

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ path = "bin/convert/main.rs"
m3d = { path = "lib/m3d" }
splay = { path = "lib/splay" }
tiff = { path = "lib/tiff" }
vot = { path = "lib/vot" }
# library
bytemuck = "1"
byteorder = "1.0"
Expand Down
6 changes: 6 additions & 0 deletions bin/convert/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ fn main() {
let level_data = layers.export();
level_data.save_vmp(&dst_path);
}
("vot", "ron") => {
println!("\tLoading the VOT...");
let mut file = File::open(&src_path).unwrap();
let vot = vot::MobileLocation::load(&mut file, 16);
println!("\tGot {} frames", vot.frames.len());
}
("pal", "png") => {
println!("Converting palette to PNG...");
let data = fs_read(&src_path).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ layout: home
- fearless GPU and multi-core use

This blog tells the story of this adventure :)

Also see the [related videos](https://peertube.fidonet.io/video-channels/vangers_dev/videos) on PeerTube!
9 changes: 9 additions & 0 deletions lib/rle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "rle"
version = "0.1.0"
workspace = "../.."
authors = ["Dzmitry Malyshau <kvarkus@gmail.com>"]
edition = "2018"

[dependencies]
byteorder = "1.0"
35 changes: 35 additions & 0 deletions lib/rle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
pub fn decode(data: &[u8], output: &mut Vec<u8>) {
let mut i = 1;
let mut cur = data[0];
let mut len = 0;
while i <= data.len() {
while data.get(i) == Some(&cur) && len < 127 {
len += 1;
i += 1;
}

if len != 0 {
output.push(len);
output.push(cur);
cur = data.get(i).cloned().unwrap_or(0);
i += 1;
len = 0;
}

while match data.get(i) {
Some(&ch) if ch != cur && len < 127 => {
len += 1;
cur = ch;
true
}
_ => false,
} {}

if len != 0 {
output.push(0x80 + (len - 1));
output.extend_from_slice(&data[i - (len - 1) as usize..i - 1]);
output.push(cur);
len = 0;
}
}
}
10 changes: 10 additions & 0 deletions lib/vot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "vot"
version = "0.1.0"
workspace = "../.."
authors = ["Dzmitry Malyshau <kvarkus@gmail.com>"]
edition = "2018"

[dependencies]
byteorder = "1.0"
rle = { path = "../rle" }
151 changes: 151 additions & 0 deletions lib/vot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use byteorder::{LittleEndian as E, ReadBytesExt};

const SIGNATURE: &[u8; 3] = b"ML3"; //MLSign
const NAME_LEN: usize = 16; //MLNAMELEN + 1
const MAX_KEYPHRASE: usize = 4; //MAX_KEYPHASE

#[derive(Clone, Copy, Debug)]
pub enum Mode {
Relative = 0,
Absolute = 1,
Rel2Abs = 2,
}

pub struct Frame {
pos: (i32, i32),
size: (u32, u32),
period: u32,
surf_type: u32,
csd: u32,
cst: u32,
delta: Vec<u8>,
terrain: Vec<u8>,
sign_bits: Vec<u32>,
}

impl Frame {
pub fn load<I: ReadBytesExt>(
input: &mut I,
mode: Mode,
max_suface_type: u32,
mut temp: &mut Vec<u8>,
) -> Self {
let x0 = input.read_i32::<E>().unwrap();
let y0 = input.read_i32::<E>().unwrap();
let sx = input.read_u32::<E>().unwrap();
let sy = input.read_u32::<E>().unwrap();
let period = input.read_u32::<E>().unwrap();
let surf_type = input.read_u32::<E>().unwrap();
let csd = input.read_u32::<E>().unwrap();
let cst = input.read_u32::<E>().unwrap();
let _ = input.read_u32::<E>();
let _ = input.read_u32::<E>();
let total = (sx * sy) as usize;

temp.clear();
temp.resize(total, 0u8);

let delta = if csd == 0 {
input.read(&mut temp).unwrap();
let mut d = Vec::new();
rle::decode(&temp, &mut d);
d
} else {
let mut d = vec![0u8; csd as usize];
input.read(&mut d).unwrap();
d
};

let terrain = if surf_type >= max_suface_type {
if cst == 0 {
input.read(&mut temp).unwrap();
let mut t = Vec::new();
rle::decode(&temp, &mut t);
t
} else {
let mut t = vec![0u8; cst as usize];
input.read(&mut t).unwrap();
t
}
} else {
Vec::new()
};

let sign_bits = match mode {
Mode::Relative => {
let words = total / 32 + 1;
let mut sb = Vec::with_capacity(words);
for _ in 0..words {
sb.push(input.read_u32::<E>().unwrap());
}
sb
}
_ => Vec::new(),
};

Frame {
pos: (x0, y0),
size: (sx, sy),
period,
surf_type,
csd,
cst,
delta,
terrain,
sign_bits,
}
}
}

pub struct MobileLocation {
pub max_stage: u32,
pub frames: Vec<Frame>,
}

impl MobileLocation {
pub fn load<I: ReadBytesExt>(input: &mut I, max_surface: u32) -> Self {
let mut signature = [0u8; 3];
input.read(&mut signature).unwrap();
assert_eq!(&signature, SIGNATURE);

let mut raw_name = [0u8; NAME_LEN];
input.read(&mut raw_name).unwrap();

let max_frame = input.read_u32::<E>().unwrap();
let _dry_terrain = input.read_u32::<E>().unwrap();
let _impulse = input.read_u32::<E>().unwrap();

let _ = input.read_u8();
let mode = match input.read_u8().unwrap() {
0 => Mode::Relative,
1 => Mode::Absolute,
2 => Mode::Rel2Abs,
other => panic!("Unexpected mode {}", other),
};
let _ = input.read_u8();
let _ = input.read_u8();

let mut keyphrase = [0u32; MAX_KEYPHRASE];
for key in keyphrase[1..].iter_mut() {
*key = input.read_u32::<E>().unwrap();
}
let _ = input.read_u32::<E>();

let mut is_alt = false;
let mut max_stage = 0;
let mut alt_size = (0, 0);
let mut frames = Vec::with_capacity(max_frame as usize);
let mut temp = Vec::new();
for _ in 0..max_frame {
let frame = Frame::load(input, mode, max_surface, &mut temp);
alt_size.0 = alt_size.0.max(frame.size.0);
alt_size.1 = alt_size.1.max(frame.size.1);
is_alt |= frame.period > 1;
max_stage += frame.period;
frames.push(frame);
}
let _ = is_alt;

MobileLocation { max_stage, frames }
}
}