From c2a13536a88660b51aafaa8003d2f17474b8979c Mon Sep 17 00:00:00 2001 From: jake Date: Fri, 31 Jan 2020 08:57:36 -0800 Subject: [PATCH] init map code --- src/ship/map.rs | 181 ++++++++++++++++++++++++++++++++++++++++++++++++ src/ship/mod.rs | 1 + 2 files changed, 182 insertions(+) create mode 100644 src/ship/map.rs diff --git a/src/ship/map.rs b/src/ship/map.rs new file mode 100644 index 0000000..bdbb3fd --- /dev/null +++ b/src/ship/map.rs @@ -0,0 +1,181 @@ +use log::warn; +use std::io::Cursor; +use std::convert::Into; +use std::path::PathBuf; +use std::io::{Read}; +use std::fs::File; + +use byteorder::{LittleEndian, ReadBytesExt}; + +use crate::ship::monster::MonsterType; + +#[derive(Debug, Copy, Clone)] +struct RawMapEnemy { + id: u32, + _unknown1: u16, + children: u16, + _unknown3: u16, + _unknown4: u16, + section: u16, + wave_idd: u16, + wave_id: u32, + x: f32, + y: f32, + z: f32, + xrot: u32, + yrot: u32, + zrot: u32, + _field1: f32, + _field2: f32, + _field3: f32, + _field4: f32, + _field5: f32, + skin: u32, + _field6: u32 +} + +impl RawMapEnemy { + fn from_byte_stream(cursor: &mut R) -> Result { + Ok(RawMapEnemy { + id: cursor.read_u32::()?, + _unknown1: cursor.read_u16::()?, + children: cursor.read_u16::()?, + _unknown3: cursor.read_u16::()?, + _unknown4: cursor.read_u16::()?, + section: cursor.read_u16::()?, + wave_idd: cursor.read_u16::()?, + wave_id: cursor.read_u32::()?, + x: cursor.read_f32::()?, + y: cursor.read_f32::()?, + z: cursor.read_f32::()?, + xrot: cursor.read_u32::()?, + yrot: cursor.read_u32::()?, + zrot: cursor.read_u32::()?, + _field1: cursor.read_f32::()?, + _field2: cursor.read_f32::()?, + _field3: cursor.read_f32::()?, + _field4: cursor.read_f32::()?, + _field5: cursor.read_f32::()?, + skin: cursor.read_u32::()?, + _field6: cursor.read_u32::()?, + }) + } +} + + + +struct MapObjects { + +} + + + + +struct MapEnemies { + +} + +impl MapEnemies { + fn new(path: PathBuf) -> MapEnemies { + MapEnemies { + + } + } + + fn read_next_enemy(&mut self) -> Option { + None + } +} + + + + + +#[derive(Debug, Copy, Clone)] +pub struct MapEnemy { + pub monster: MonsterType, + hp: u32, + // other stats from bp.n + dead: bool, +} + + +impl MapEnemy { + fn from_raw(enemy: RawMapEnemy /*, battleparam */) -> MapEnemy { + //warn!("loading {} {} {}", enemy.id, enemy.skin, enemy.children); + warn!("{:?}", enemy); + let monster = match (enemy.id, enemy.skin) { + (66, 0) => MonsterType::Monest, + (68, 0) => MonsterType::Booma, + (68, 1) => MonsterType::Gobooma, + (68, 2) => MonsterType::Gigobooma, + + (99, 0) => MonsterType::EvilShark, + + _ => MonsterType::AlRappy, + }; + + MapEnemy { + monster: monster, + hp: 0, + dead: false, + } + } + + fn new(monster: MonsterType) -> MapEnemy { + MapEnemy { + monster: monster, + hp: 0, + dead: false, + } + } +} + +//impl From for MapEnemy + + + +#[derive(Debug)] +pub struct Maps { + //map_indexes: + enemy_data: Vec + +} + + + +impl Maps { + pub fn new() -> Maps { + let mut maps = Maps { + enemy_data: Vec::new(), + }; + maps.add_map("data/maps/map_city00_00e.dat".into()); + maps.add_map("data/maps/map_forest01_00e.dat".into()); + maps.add_map("data/maps/map_cave01_00_00e.dat".into()); + + warn!("len {}", maps.enemy_data.len()); + maps + } + + fn add_map(&mut self, path: PathBuf) { + let mut cursor = File::open(path).unwrap(); + while let Ok(enemy) = RawMapEnemy::from_byte_stream(&mut cursor) { + let new_enemy = MapEnemy::from_raw(enemy); + match new_enemy.monster { + MonsterType::Monest => { + self.enemy_data.push(new_enemy); + for _ in 0..30 { + self.enemy_data.push(MapEnemy::new(MonsterType::Mothmant)); + } + }, + _ => self.enemy_data.push(new_enemy) + } + } + } + + pub fn enemy_by_id(&self, id: usize) -> MapEnemy { + self.enemy_data[id] + } +} + + diff --git a/src/ship/mod.rs b/src/ship/mod.rs index f55a4aa..62dbf73 100644 --- a/src/ship/mod.rs +++ b/src/ship/mod.rs @@ -3,4 +3,5 @@ pub mod location; pub mod character; pub mod room; pub mod items; +pub mod map; pub mod monster;