From 93558cf3c8d9ffedff7ceed89bae08c8e871af42 Mon Sep 17 00:00:00 2001 From: ashley Date: Thu, 2 Nov 2023 21:49:34 -0400 Subject: [PATCH] Metatile converter --- src/reskit/level/system.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/reskit/level/system.rs b/src/reskit/level/system.rs index 7ec1251..6ec8630 100644 --- a/src/reskit/level/system.rs +++ b/src/reskit/level/system.rs @@ -1,6 +1,6 @@ use std::{error::Error, collections::HashMap}; use image::GenericImageView; -use crate::reskit::{tileset::image_to_tiles, utility::symbol_to_pascal, cli::settings::TileOrder}; +use crate::reskit::{tileset::image_to_tiles, utility::{symbol_to_pascal, print_info}, cli::settings::TileOrder}; use super::{converter::{TiledTilemap, Layer, SystemPlane, TiledTileset}, dmapack::get_buckets}; /** @@ -114,8 +114,10 @@ pub fn get_tiles( tilemap: &TiledTilemap ) -> Result<(Vec, Vec), Box>> * 2 bytes: The offset to the instances table - * (n) 2 byte offsets: For the number of defined metatiles, 16-bit offsets into the file - * pointing to the metatile definition. Index into this table = the metatile ID. + * For each metatile: + * 2 bytes: Metatile width + * 2 bytes: Metatile height + * 2 bytes: Offset into `dmapack` buckets pointing directly to the metatile * * <<< DEFINITIONS >>> * For each `dmapack` bucket: @@ -131,8 +133,10 @@ pub fn get_tiles( tilemap: &TiledTilemap ) -> Result<(Vec, Vec), Box Result, Box> { + print_info( &format!( "number of metatiles: {}", tilemap.metatiles.len() ) ); + // Assemble header of offsets - let header_offset = ( tilemap.metatiles.len() * 2 ) + 2; + let header_offset = ( tilemap.metatiles.len() * 6 ) + 2; let mut header: Vec = vec![]; // Get offsets for each bucket @@ -140,33 +144,43 @@ pub fn get_metatile_maps( tilemap: &TiledTilemap ) -> Result, Box = HashMap::new(); let mut total_offset = header_offset; for ( bucket_id, bucket ) in &buckets { + print_info( &format!( "bucket {} is of size {}", bucket_id, bucket.len() ) ); bucket_offsets.insert( *bucket_id, total_offset ); total_offset += bucket.len(); } // Get the offset for each metatile within the buckets + let mut index = 0; for metatile in &tilemap.metatiles { let bucket_offset = bucket_offsets.get( &metatile.pack.bucket ).ok_or( "internal error: no offset for dmapack bucket" )?; let cell_x = metatile.pack.slot[ 0 ]; let cell_y = metatile.pack.slot[ 1 ]; let metatile_byte_offset: u16 = *bucket_offset as u16 + ( ( ( cell_y * 64 ) + cell_x ) * 2 ); + header.extend( metatile.width.to_be_bytes() ); + header.extend( metatile.height.to_be_bytes() ); header.extend( metatile_byte_offset.to_be_bytes() ); + + print_info( &format!( "metatile {}: width {} height {} file offset {}", index, metatile.width, metatile.height, metatile_byte_offset ) ); + index += 1; } // Assemble definitions let mut definitions: Vec = vec![]; - + let mut wasted_space: usize = 0; for ( _, bucket ) in &buckets { for cell in bucket { if let Some( nametable_entry ) = cell { definitions.extend( nametable_entry.to_be_bytes() ); } else { definitions.extend( ( 0 as u16 ).to_be_bytes() ); + wasted_space += 1; } } } + print_info( &format!( "total dmapack unused space is {} bytes", wasted_space ) ); + // Assemble instances let mut instances: Vec = vec![]; @@ -179,13 +193,16 @@ pub fn get_metatile_maps( tilemap: &TiledTilemap ) -> Result, Box Result, Box