From 569265f414e01dddf8122c1132db8bf26dff9bd6 Mon Sep 17 00:00:00 2001 From: Wesley Irvin Date: Mon, 7 Apr 2025 19:09:33 -0400 Subject: [PATCH] Thing Reader Reads things from the WAD file. --- src/lumps/lump.rs | 1 + src/lumps/mod.rs | 2 ++ src/lumps/thing.rs | 19 ++++++++++++++++ src/tests/wad.rs | 45 ++++++++++++++++++++++++++++++++++++++ src/types/mod.rs | 2 ++ src/types/thing.rs | 8 +++++++ src/wad/wadfile.rs | 1 + src/wad/wadfile/readers.rs | 31 ++++++++++++++++++++++++-- 8 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/lumps/thing.rs create mode 100644 src/types/thing.rs diff --git a/src/lumps/lump.rs b/src/lumps/lump.rs index 7a29688..489f046 100644 --- a/src/lumps/lump.rs +++ b/src/lumps/lump.rs @@ -12,4 +12,5 @@ pub enum LumpType { Vertex, Linedef, Sidedef, + Thing, } diff --git a/src/lumps/mod.rs b/src/lumps/mod.rs index 67476bf..15171a7 100644 --- a/src/lumps/mod.rs +++ b/src/lumps/mod.rs @@ -1,9 +1,11 @@ mod linedef; mod lump; mod sidedef; +mod thing; mod vertex; pub use linedef::LinedefLump; pub use lump::{Lump, LumpType}; pub use sidedef::SidedefLump; +pub use thing::ThingLump; pub use vertex::VertexLump; diff --git a/src/lumps/thing.rs b/src/lumps/thing.rs new file mode 100644 index 0000000..d476bd9 --- /dev/null +++ b/src/lumps/thing.rs @@ -0,0 +1,19 @@ +use crate::types::Thing; + +pub struct ThingLump { + pub things: Vec, +} + +impl ThingLump { + pub fn get_num_things(&self) -> usize { + self.things.len() + } + + pub fn get_all_things(&self) -> Vec { + self.things.to_vec() + } + + pub fn get_thing(&self, pos: usize) -> Thing { + self.things[pos].to_owned() + } +} diff --git a/src/tests/wad.rs b/src/tests/wad.rs index 4bd11f4..25553d3 100644 --- a/src/tests/wad.rs +++ b/src/tests/wad.rs @@ -193,3 +193,48 @@ pub fn get_num_sidedefs() { assert_eq!(sidedef_lump.get_num_sidedefs(), 648); } + +#[test] +pub fn read_first_thing() { + use crate::types::Thing; + + let wad_file = get_wad(); + + let thing_lump = wad_file.get_thing_lump(&wad_file.directory[7]); + + let correct_thing = Thing { + x_position: 1056, + y_position: -3616, + angle: 90, + thing_type: 1, + flags: 7, + }; + + assert_eq!(thing_lump.get_thing(0), correct_thing); +} + +#[test] +pub fn get_num_thing_lumps() { + use crate::lumps::{Lump, LumpType}; + + let mut thing_lumps: Vec = Vec::new(); + + let wad_file = get_wad(); + + for entry in 0..wad_file.num_lumps as usize { + if let LumpType::Thing = wad_file.directory[entry].lump_type { + thing_lumps.push(wad_file.directory[entry].to_owned()); + } + } + + assert_eq!(thing_lumps.len(), 9); +} + +#[test] +pub fn get_num_things() { + let wad_file = get_wad(); + + let thing_lump = wad_file.get_thing_lump(&wad_file.directory[7]); + + assert_eq!(thing_lump.get_num_things(), 138); +} diff --git a/src/types/mod.rs b/src/types/mod.rs index 60ee0e6..bc6299f 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1,7 +1,9 @@ mod linedef; mod sidedef; +mod thing; mod vertex; pub use linedef::Linedef; pub use sidedef::Sidedef; +pub use thing::Thing; pub use vertex::Vertex; diff --git a/src/types/thing.rs b/src/types/thing.rs new file mode 100644 index 0000000..993e403 --- /dev/null +++ b/src/types/thing.rs @@ -0,0 +1,8 @@ +#[derive(Clone, Debug, PartialEq)] +pub struct Thing { + pub x_position: i16, + pub y_position: i16, + pub angle: i16, + pub thing_type: i16, + pub flags: i16, +} diff --git a/src/wad/wadfile.rs b/src/wad/wadfile.rs index 1bdc249..a0c6d4b 100644 --- a/src/wad/wadfile.rs +++ b/src/wad/wadfile.rs @@ -45,6 +45,7 @@ impl WADFile { "VERTEXES" => LumpType::Vertex, "LINEDEFS" => LumpType::Linedef, "SIDEDEFS" => LumpType::Sidedef, + "THINGS" => LumpType::Thing, _ => LumpType::Unknown, }; lump_dir.push(Lump { diff --git a/src/wad/wadfile/readers.rs b/src/wad/wadfile/readers.rs index b152241..560a0fa 100644 --- a/src/wad/wadfile/readers.rs +++ b/src/wad/wadfile/readers.rs @@ -1,5 +1,5 @@ -use crate::lumps::{LinedefLump, Lump, SidedefLump, VertexLump}; -use crate::types::{Linedef, Sidedef, Vertex}; +use crate::lumps::{LinedefLump, Lump, SidedefLump, ThingLump, VertexLump}; +use crate::types::{Linedef, Sidedef, Thing, Vertex}; use crate::utils::read_ascii; use crate::wad::WADFile; use crate::wad::wadfile::read_i16_le; @@ -85,4 +85,31 @@ impl WADFile { SidedefLump { sidedefs } } + + pub fn get_thing_lump(&self, lump: &Lump) -> ThingLump { + let lump_offset = lump.offset as usize; + let lump_size = lump.size as usize; + let lump_data = &self.data[lump_offset..lump_offset + lump_size]; + let lump_entries = lump_size / 10; + let mut things: Vec = Vec::with_capacity(lump_entries); + + for entry in 0..lump_entries { + let startpos = entry * 10; + let x_position = read_i16_le(&lump_data[startpos..startpos + 2]); + let y_position = read_i16_le(&lump_data[startpos + 2..startpos + 4]); + let angle = read_i16_le(&lump_data[startpos + 4..startpos + 6]); + let thing_type = read_i16_le(&lump_data[startpos + 6..startpos + 8]); + let flags = read_i16_le(&lump_data[startpos + 8..startpos + 10]); + + things.push(Thing { + x_position, + y_position, + angle, + thing_type, + flags, + }); + } + + ThingLump { things } + } } -- 2.49.1