diff options
Diffstat (limited to 'stockton-levels/src/q3')
-rw-r--r-- | stockton-levels/src/q3/brushes.rs | 19 | ||||
-rw-r--r-- | stockton-levels/src/q3/effects.rs | 17 | ||||
-rw-r--r-- | stockton-levels/src/q3/entities.rs | 13 | ||||
-rw-r--r-- | stockton-levels/src/q3/faces.rs | 22 | ||||
-rw-r--r-- | stockton-levels/src/q3/file.rs | 232 | ||||
-rw-r--r-- | stockton-levels/src/q3/header.rs | 6 | ||||
-rw-r--r-- | stockton-levels/src/q3/light_maps.rs | 16 | ||||
-rw-r--r-- | stockton-levels/src/q3/light_vols.rs | 9 | ||||
-rw-r--r-- | stockton-levels/src/q3/mod.rs | 24 | ||||
-rw-r--r-- | stockton-levels/src/q3/models.rs | 21 | ||||
-rw-r--r-- | stockton-levels/src/q3/planes.rs | 14 | ||||
-rw-r--r-- | stockton-levels/src/q3/textures.rs | 38 | ||||
-rw-r--r-- | stockton-levels/src/q3/tree.rs | 31 | ||||
-rw-r--r-- | stockton-levels/src/q3/vertices.rs | 13 | ||||
-rw-r--r-- | stockton-levels/src/q3/visdata.rs | 16 |
15 files changed, 246 insertions, 245 deletions
diff --git a/stockton-levels/src/q3/brushes.rs b/stockton-levels/src/q3/brushes.rs index f82e57a..cd848a2 100644 --- a/stockton-levels/src/q3/brushes.rs +++ b/stockton-levels/src/q3/brushes.rs @@ -23,18 +23,18 @@ const BRUSH_SIZE: usize = 4 * 3; /// The size of one brushsize record const SIDE_SIZE: usize = 4 * 2; +use super::Q3BSPFile; +use crate::coords::CoordSystem; use crate::helpers::slice_to_i32; -use crate::types::{ParseError, Result}; use crate::traits::brushes::*; -use crate::coords::CoordSystem; -use super::Q3BSPFile; +use crate::types::{ParseError, Result}; /// Parse the brushes & brushsides lump from a bsp file. pub fn from_data( brushes_data: &[u8], sides_data: &[u8], n_textures: u32, - n_planes: u32 + n_planes: u32, ) -> Result<Box<[Brush]>> { if brushes_data.len() % BRUSH_SIZE != 0 || sides_data.len() % SIDE_SIZE != 0 { return Err(ParseError::Invalid); @@ -57,9 +57,9 @@ pub fn from_data( slice_to_i32(&brush[0..4]), slice_to_i32(&brush[4..8]), n_textures as usize, - n_planes as usize + n_planes as usize, )?, - texture_idx + texture_idx, }); } @@ -96,7 +96,7 @@ fn get_sides( sides.push(BrushSide { plane_idx, texture_idx, - is_opposing + is_opposing, }); } } @@ -104,15 +104,14 @@ fn get_sides( Ok(sides.into_boxed_slice()) } - impl<T: CoordSystem> HasBrushes<T> for Q3BSPFile<T> { type BrushesIter<'a> = std::slice::Iter<'a, Brush>; - fn brushes_iter<'a>(&'a self) -> Self::BrushesIter<'a> { + fn brushes_iter(&self) -> Self::BrushesIter<'_> { self.brushes.iter() } - fn get_brush<'a>(&'a self, index: u32) -> &'a Brush { + fn get_brush(&self, index: u32) -> &Brush { &self.brushes[index as usize] } } diff --git a/stockton-levels/src/q3/effects.rs b/stockton-levels/src/q3/effects.rs index 77ad1ed..a796570 100644 --- a/stockton-levels/src/q3/effects.rs +++ b/stockton-levels/src/q3/effects.rs @@ -17,11 +17,11 @@ use std::str; -use crate::helpers::slice_to_u32; -use crate::types::{Result, ParseError}; -use crate::traits::effects::*; use super::Q3BSPFile; use crate::coords::CoordSystem; +use crate::helpers::slice_to_u32; +use crate::traits::effects::*; +use crate::types::{ParseError, Result}; /// The size of one effect definition const EFFECT_SIZE: usize = 64 + 4 + 4; @@ -42,23 +42,24 @@ pub fn from_data(data: &[u8], n_brushes: u32) -> Result<Box<[Effect]>> { } effects.push(Effect { - name: str::from_utf8(&raw[..64]).map_err(|_| ParseError::Invalid)?.to_owned(), - brush_idx + name: str::from_utf8(&raw[..64]) + .map_err(|_| ParseError::Invalid)? + .to_owned(), + brush_idx, }); } Ok(effects.into_boxed_slice()) } - impl<T: CoordSystem> HasEffects<T> for Q3BSPFile<T> { type EffectsIter<'a> = std::slice::Iter<'a, Effect>; - fn effects_iter<'a>(&'a self) -> Self::EffectsIter<'a> { + fn effects_iter(&self) -> Self::EffectsIter<'_> { self.effects.iter() } - fn get_effect<'a>(&'a self, index: u32) -> &'a Effect { + fn get_effect(&self, index: u32) -> &Effect { &self.effects[index as usize] } } diff --git a/stockton-levels/src/q3/entities.rs b/stockton-levels/src/q3/entities.rs index 4382ef2..950d89a 100644 --- a/stockton-levels/src/q3/entities.rs +++ b/stockton-levels/src/q3/entities.rs @@ -15,13 +15,13 @@ // You should have received a copy of the GNU General Public License // along with stockton-bsp. If not, see <http://www.gnu.org/licenses/>. -use std::str; use std::collections::HashMap; +use std::str; -use crate::types::{Result, ParseError}; -use crate::traits::entities::*; use super::Q3BSPFile; use crate::coords::CoordSystem; +use crate::traits::entities::*; +use crate::types::{ParseError, Result}; const QUOTE: u8 = b'"'; const END_BRACKET: u8 = b'}'; @@ -72,7 +72,10 @@ pub fn from_data(data: &[u8]) -> Result<Box<[Entity]>> { state = ParseState::InsideEntity; val_end = i; - attrs.insert(string[key_start..key_end].to_owned(), string[val_start..val_end].to_owned()); + attrs.insert( + string[key_start..key_end].to_owned(), + string[val_start..val_end].to_owned(), + ); } _ => { return Err(ParseError::Invalid); @@ -103,7 +106,7 @@ pub fn from_data(data: &[u8]) -> Result<Box<[Entity]>> { impl<T: CoordSystem> HasEntities for Q3BSPFile<T> { type EntitiesIter<'a> = std::slice::Iter<'a, Entity>; - fn entities_iter<'a>(&'a self) -> Self::EntitiesIter<'a> { + fn entities_iter(&self) -> Self::EntitiesIter<'_> { self.entities.iter() } } diff --git a/stockton-levels/src/q3/faces.rs b/stockton-levels/src/q3/faces.rs index a53af6e..ab768ce 100644 --- a/stockton-levels/src/q3/faces.rs +++ b/stockton-levels/src/q3/faces.rs @@ -15,16 +15,15 @@ // You should have received a copy of the GNU General Public License // along with stockton-bsp. If not, see <http://www.gnu.org/licenses/>. -use crate::helpers::{slice_to_i32, slice_to_u32, slice_to_vec2ui, slice_to_vec3}; -use crate::types::{Result, ParseError}; -use na::Vector3; -use crate::traits::faces::*; use super::Q3BSPFile; use crate::coords::CoordSystem; +use crate::helpers::{slice_to_i32, slice_to_u32, slice_to_vec2ui, slice_to_vec3}; +use crate::traits::faces::*; +use crate::types::{ParseError, Result}; +use na::Vector3; const FACE_SIZE: usize = (4 * 8) + (4 * 2) + (4 * 2) + (4 * 3) + ((4 * 2) * 3) + (4 * 3) + (4 * 2); - pub fn from_data( data: &[u8], n_textures: u32, @@ -53,7 +52,6 @@ pub fn from_data( Ok(faces.into_boxed_slice()) } - fn face_from_slice( data: &[u8], n_textures: u32, @@ -125,9 +123,9 @@ fn face_from_slice( // map_vecs let mut map_vecs = [Vector3::new(0.0, 0.0, 0.0); 2]; - for n in 0..2 { + for (n, map_vec) in map_vecs.iter_mut().enumerate() { let offset = 60 + (n * 3 * 4); - map_vecs[n] = slice_to_vec3(&data[offset..offset + 12]); + *map_vec = slice_to_vec3(&data[offset..offset + 12]); } // normal & size @@ -150,11 +148,10 @@ fn face_from_slice( }) } - impl<T: CoordSystem> HasFaces<T> for Q3BSPFile<T> { type FacesIter<'a> = std::slice::Iter<'a, Face>; - - fn faces_iter<'a>(&'a self) -> Self::FacesIter<'a> { + + fn faces_iter(&self) -> Self::FacesIter<'_> { self.faces.iter() } @@ -162,8 +159,7 @@ impl<T: CoordSystem> HasFaces<T> for Q3BSPFile<T> { self.faces.len() as u32 } - fn get_face<'a>(&'a self, index: u32) -> &'a Face { + fn get_face(&self, index: u32) -> &Face { &self.faces[index as usize] } - } diff --git a/stockton-levels/src/q3/file.rs b/stockton-levels/src/q3/file.rs index ecce783..7906823 100644 --- a/stockton-levels/src/q3/file.rs +++ b/stockton-levels/src/q3/file.rs @@ -1,4 +1,4 @@ -// Copyright (C) Oscar Shrimpton 2019 +// Copyright (C) Oscar Shrimpton 2019 // This program is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free @@ -21,126 +21,142 @@ use bitvec::prelude::*; use std::marker::PhantomData; use self::header::Header; -use crate::types::Result; use crate::coords::*; +use crate::types::Result; use super::*; -use crate::traits::textures::Texture; -use crate::traits::entities::Entity; -use crate::traits::planes::Plane; -use crate::traits::vertices::{Vertex, MeshVert}; -use crate::traits::light_maps::LightMap; -use crate::traits::light_vols::LightVol; use crate::traits::brushes::Brush; use crate::traits::effects::Effect; +use crate::traits::entities::Entity; use crate::traits::faces::Face; -use crate::traits::tree::BSPNode; +use crate::traits::light_maps::LightMap; +use crate::traits::light_vols::LightVol; use crate::traits::models::Model; +use crate::traits::planes::Plane; +use crate::traits::textures::Texture; +use crate::traits::tree::BSPNode; +use crate::traits::vertices::{MeshVert, Vertex}; /// A parsed Quake 3 BSP File. pub struct Q3BSPFile<T: CoordSystem> { - pub(crate) visdata: Box<[BitBox<Local, u8>]>, - pub(crate) textures: Box<[Texture]>, - pub(crate) entities: Box<[Entity]>, - pub(crate) planes: Box<[Plane]>, - pub(crate) vertices: Box<[Vertex]>, - pub(crate) meshverts: Box<[MeshVert]>, - pub(crate) light_maps: Box<[LightMap]>, - pub(crate) light_vols: Box<[LightVol]>, - pub(crate) brushes: Box<[Brush]>, - pub(crate) effects: Box<[Effect]>, - pub(crate) faces: Box<[Face]>, - pub(crate) models: Box<[Model]>, - pub(crate) tree_root: BSPNode, - _phantom: PhantomData<T> + pub(crate) visdata: Box<[BitBox<Local, u8>]>, + pub(crate) textures: Box<[Texture]>, + pub(crate) entities: Box<[Entity]>, + pub(crate) planes: Box<[Plane]>, + pub(crate) vertices: Box<[Vertex]>, + pub(crate) meshverts: Box<[MeshVert]>, + pub(crate) light_maps: Box<[LightMap]>, + pub(crate) light_vols: Box<[LightVol]>, + pub(crate) brushes: Box<[Brush]>, + pub(crate) effects: Box<[Effect]>, + pub(crate) faces: Box<[Face]>, + pub(crate) models: Box<[Model]>, + pub(crate) tree_root: BSPNode, + _phantom: PhantomData<T>, } impl Q3BSPFile<Q3System> { - /// Parse `data` as a quake 3 bsp file. - pub fn parse_file(data: &[u8]) -> Result<Q3BSPFile<Q3System>> { - let header = Header::from(data)?; - - let entities = entities::from_data(header.get_lump(&data, 0))?; - let textures = textures::from_data(header.get_lump(&data, 1))?; - let planes = planes::from_data(header.get_lump(&data, 2))?; - let vertices = vertices::verts_from_data(header.get_lump(&data, 10))?; - let meshverts = vertices::meshverts_from_data(header.get_lump(&data, 11))?; - let light_maps = light_maps::from_data(header.get_lump(&data, 14))?; - let light_vols = light_vols::from_data(header.get_lump(&data, 15))?; - let visdata = visdata::from_data(header.get_lump(&data, 16))?; - let brushes = brushes::from_data( - header.get_lump(&data, 8), - header.get_lump(&data, 9), - textures.len() as u32, - planes.len() as u32 - )?; - let effects = effects::from_data(header.get_lump(&data, 12), brushes.len() as u32)?; - let faces = faces::from_data( - header.get_lump(&data, 13), - textures.len() as u32, - effects.len() as u32, - vertices.len() as u32, - meshverts.len() as u32, - light_maps.len() as u32 - )?; - - let tree_root = tree::from_data( - header.get_lump(&data, 3), - header.get_lump(&data, 4), - header.get_lump(&data, 5), - header.get_lump(&data, 6), - faces.len() as u32, - brushes.len() as u32 - )?; - - let models = models::from_data(header.get_lump(&data, 7), faces.len() as u32, brushes.len() as u32)?; - - Ok(Q3BSPFile { - visdata, textures, entities, planes, vertices, meshverts, light_maps, - light_vols, brushes, effects, faces, tree_root, models, - _phantom: PhantomData - }) - } + /// Parse `data` as a quake 3 bsp file. + pub fn parse_file(data: &[u8]) -> Result<Q3BSPFile<Q3System>> { + let header = Header::from(data)?; + + let entities = entities::from_data(header.get_lump(&data, 0))?; + let textures = textures::from_data(header.get_lump(&data, 1))?; + let planes = planes::from_data(header.get_lump(&data, 2))?; + let vertices = vertices::verts_from_data(header.get_lump(&data, 10))?; + let meshverts = vertices::meshverts_from_data(header.get_lump(&data, 11))?; + let light_maps = light_maps::from_data(header.get_lump(&data, 14))?; + let light_vols = light_vols::from_data(header.get_lump(&data, 15))?; + let visdata = visdata::from_data(header.get_lump(&data, 16))?; + let brushes = brushes::from_data( + header.get_lump(&data, 8), + header.get_lump(&data, 9), + textures.len() as u32, + planes.len() as u32, + )?; + let effects = effects::from_data(header.get_lump(&data, 12), brushes.len() as u32)?; + let faces = faces::from_data( + header.get_lump(&data, 13), + textures.len() as u32, + effects.len() as u32, + vertices.len() as u32, + meshverts.len() as u32, + light_maps.len() as u32, + )?; + + let tree_root = tree::from_data( + header.get_lump(&data, 3), + header.get_lump(&data, 4), + header.get_lump(&data, 5), + header.get_lump(&data, 6), + faces.len() as u32, + brushes.len() as u32, + )?; + + let models = models::from_data( + header.get_lump(&data, 7), + faces.len() as u32, + brushes.len() as u32, + )?; + + Ok(Q3BSPFile { + visdata, + textures, + entities, + planes, + vertices, + meshverts, + light_maps, + light_vols, + brushes, + effects, + faces, + tree_root, + models, + _phantom: PhantomData, + }) + } } impl<T: CoordSystem> Q3BSPFile<T> { - pub fn swizzle_to<D: CoordSystem>(mut self) -> Q3BSPFile<D> - where Swizzler: SwizzleFromTo<T, D> { - - for vertex in self.vertices.iter_mut() { - Swizzler::swizzle(&mut vertex.normal); - Swizzler::swizzle(&mut vertex.position); - } - - for model in self.models.iter_mut() { - Swizzler::swizzle(&mut model.mins); - Swizzler::swizzle(&mut model.maxs); - } - - for face in self.faces.iter_mut() { - Swizzler::swizzle(&mut face.normal); - } - - for plane in self.planes.iter_mut() { - Swizzler::swizzle(&mut plane.normal); - } - - // TODO: Possibly don't need to move? - Q3BSPFile { - visdata: self.visdata, - textures: self.textures, - entities: self.entities, - planes: self.planes, - vertices: self.vertices, - meshverts: self.meshverts, - light_maps: self.light_maps, - light_vols: self.light_vols, - brushes: self.brushes, - effects: self.effects, - faces: self.faces, - tree_root: self.tree_root, - models: self.models, - _phantom: PhantomData - } - } -}
\ No newline at end of file + pub fn swizzle_to<D: CoordSystem>(mut self) -> Q3BSPFile<D> + where + Swizzler: SwizzleFromTo<T, D>, + { + for vertex in self.vertices.iter_mut() { + Swizzler::swizzle(&mut vertex.normal); + Swizzler::swizzle(&mut vertex.position); + } + + for model in self.models.iter_mut() { + Swizzler::swizzle(&mut model.mins); + Swizzler::swizzle(&mut model.maxs); + } + + for face in self.faces.iter_mut() { + Swizzler::swizzle(&mut face.normal); + } + + for plane in self.planes.iter_mut() { + Swizzler::swizzle(&mut plane.normal); + } + + // TODO: Possibly don't need to move? + Q3BSPFile { + visdata: self.visdata, + textures: self.textures, + entities: self.entities, + planes: self.planes, + vertices: self.vertices, + meshverts: self.meshverts, + light_maps: self.light_maps, + light_vols: self.light_vols, + brushes: self.brushes, + effects: self.effects, + faces: self.faces, + tree_root: self.tree_root, + models: self.models, + _phantom: PhantomData, + } + } +} diff --git a/stockton-levels/src/q3/header.rs b/stockton-levels/src/q3/header.rs index d4e4f2f..46cfdac 100644 --- a/stockton-levels/src/q3/header.rs +++ b/stockton-levels/src/q3/header.rs @@ -1,4 +1,4 @@ -// Copyright (C) Oscar Shrimpton 2019 +// Copyright (C) Oscar Shrimpton 2019 // This program is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free @@ -13,8 +13,8 @@ // You should have received a copy of the GNU General Public License along // with this program. If not, see <http://www.gnu.org/licenses/>. -use std::convert::TryInto; use crate::types::{ParseError, Result}; +use std::convert::TryInto; const MAGIC_HEADER: &[u8] = &[0x49, 0x42, 0x53, 0x50]; const HEADER_LEN: usize = 4 + 4 + (17 * 4 * 2); @@ -80,4 +80,4 @@ impl Header { &buf[entry.offset as usize..entry.offset as usize + entry.length as usize] } -}
\ No newline at end of file +} diff --git a/stockton-levels/src/q3/light_maps.rs b/stockton-levels/src/q3/light_maps.rs index 8fc1c83..2d0fcc6 100644 --- a/stockton-levels/src/q3/light_maps.rs +++ b/stockton-levels/src/q3/light_maps.rs @@ -15,10 +15,10 @@ // You should have received a copy of the GNU General Public License // along with stockton-bsp. If not, see <http://www.gnu.org/licenses/>. -use crate::types::{Result, RGB, ParseError}; -use crate::traits::light_maps::*; use super::Q3BSPFile; use crate::coords::CoordSystem; +use crate::traits::light_maps::*; +use crate::types::{ParseError, Result, RGB}; /// The size of one LightMap const LIGHTMAP_SIZE: usize = 128 * 128 * 3; @@ -35,10 +35,10 @@ pub fn from_data(data: &[u8]) -> Result<Box<[LightMap]>> { let raw = &data[n * LIGHTMAP_SIZE..(n + 1) * LIGHTMAP_SIZE]; let mut map: [[RGB; 128]; 128] = [[RGB::white(); 128]; 128]; - for x in 0..128 { - for y in 0..128 { + for (x, outer) in map.iter_mut().enumerate() { + for (y, inner) in outer.iter_mut().enumerate() { let offset = (x * 128 * 3) + (y * 3); - map[x][y] = RGB::from_slice(&raw[offset..offset + 3]); + *inner = RGB::from_slice(&raw[offset..offset + 3]); } } maps.push(LightMap { map }) @@ -50,11 +50,11 @@ pub fn from_data(data: &[u8]) -> Result<Box<[LightMap]>> { impl<T: CoordSystem> HasLightMaps for Q3BSPFile<T> { type LightMapsIter<'a> = std::slice::Iter<'a, LightMap>; - fn lightmaps_iter<'a>(&'a self) -> Self::LightMapsIter<'a> { + fn lightmaps_iter(&self) -> Self::LightMapsIter<'_> { self.light_maps.iter() } - fn get_lightmap<'a>(&'a self, index: u32) -> &'a LightMap { + fn get_lightmap(&self, index: u32) -> &LightMap { &self.light_maps[index as usize] } -}
\ No newline at end of file +} diff --git a/stockton-levels/src/q3/light_vols.rs b/stockton-levels/src/q3/light_vols.rs index d5bbadf..bec3bea 100644 --- a/stockton-levels/src/q3/light_vols.rs +++ b/stockton-levels/src/q3/light_vols.rs @@ -17,10 +17,10 @@ use std::convert::TryInto; -use crate::types::{Result, ParseError, RGB}; -use crate::traits::light_vols::*; use super::Q3BSPFile; use crate::coords::CoordSystem; +use crate::traits::light_vols::*; +use crate::types::{ParseError, Result, RGB}; const VOL_LENGTH: usize = (3 * 2) + 2; @@ -43,15 +43,14 @@ pub fn from_data(data: &[u8]) -> Result<Box<[LightVol]>> { Ok(vols.into_boxed_slice()) } - impl<T: CoordSystem> HasLightVols for Q3BSPFile<T> { type LightVolsIter<'a> = std::slice::Iter<'a, LightVol>; - fn lightvols_iter<'a>(&'a self) -> Self::LightVolsIter<'a> { + fn lightvols_iter(&self) -> Self::LightVolsIter<'_> { self.light_vols.iter() } - fn get_lightvol<'a>(&'a self, index: u32) -> &'a LightVol { + fn get_lightvol(&self, index: u32) -> &LightVol { &self.light_vols[index as usize] } } diff --git a/stockton-levels/src/q3/mod.rs b/stockton-levels/src/q3/mod.rs index 72634f8..f7d3c86 100644 --- a/stockton-levels/src/q3/mod.rs +++ b/stockton-levels/src/q3/mod.rs @@ -1,4 +1,4 @@ -// Copyright (C) Oscar Shrimpton 2019 +// Copyright (C) Oscar Shrimpton 2019 // This program is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free @@ -15,19 +15,19 @@ //! Parsing data from Q3 and similar BSPs -mod visdata; -mod header; -mod textures; -mod entities; -mod planes; -mod vertices; -mod light_maps; -mod light_vols; mod brushes; mod effects; +mod entities; mod faces; -mod tree; -mod models; pub mod file; +mod header; +mod light_maps; +mod light_vols; +mod models; +mod planes; +mod textures; +mod tree; +mod vertices; +mod visdata; -pub use self::file::Q3BSPFile;
\ No newline at end of file +pub use self::file::Q3BSPFile; diff --git a/stockton-levels/src/q3/models.rs b/stockton-levels/src/q3/models.rs index 75f42fa..b197521 100644 --- a/stockton-levels/src/q3/models.rs +++ b/stockton-levels/src/q3/models.rs @@ -15,19 +15,15 @@ // You should have received a copy of the GNU General Public License // along with stockton-bsp. If not, see <http://www.gnu.org/licenses/>. -use crate::helpers::{slice_to_u32, slice_to_vec3}; -use crate::types::{Result, ParseError}; +use super::Q3BSPFile; use crate::coords::CoordSystem; +use crate::helpers::{slice_to_u32, slice_to_vec3}; use crate::traits::models::*; -use super::Q3BSPFile; +use crate::types::{ParseError, Result}; const MODEL_SIZE: usize = (4 * 3 * 2) + (4 * 4); -pub fn from_data( - data: &[u8], - n_faces: u32, - n_brushes: u32, -) -> Result<Box<[Model]>> { +pub fn from_data(data: &[u8], n_faces: u32, n_brushes: u32) -> Result<Box<[Model]>> { if data.len() % MODEL_SIZE != 0 { return Err(ParseError::Invalid); } @@ -48,7 +44,7 @@ pub fn from_data( return Err(ParseError::Invalid); } - start..start+n + start..start + n }; let brushes_idx = { @@ -59,7 +55,7 @@ pub fn from_data( return Err(ParseError::Invalid); } - start..start+n + start..start + n }; models.push(Model { @@ -73,15 +69,14 @@ pub fn from_data( Ok(models.into_boxed_slice()) } - impl<T: CoordSystem> HasModels<T> for Q3BSPFile<T> { type ModelsIter<'a> = std::slice::Iter<'a, Model>; - fn models_iter<'a>(&'a self) -> Self::ModelsIter<'a> { + fn models_iter(&self) -> Self::ModelsIter<'_> { self.models.iter() } - fn get_model<'a>(&'a self, index: u32) -> &'a Model { + fn get_model(&self, index: u32) -> &Model { &self.models[index as usize] } } diff --git a/stockton-levels/src/q3/planes.rs b/stockton-levels/src/q3/planes.rs index 618a441..7acc872 100644 --- a/stockton-levels/src/q3/planes.rs +++ b/stockton-levels/src/q3/planes.rs @@ -17,22 +17,20 @@ const PLANE_SIZE: usize = (4 * 3) + 4; -use crate::helpers::{slice_to_f32, slice_to_vec3}; -use crate::types::{Result, ParseError}; -use crate::traits::planes::*; use super::Q3BSPFile; use crate::coords::CoordSystem; +use crate::helpers::{slice_to_f32, slice_to_vec3}; +use crate::traits::planes::*; +use crate::types::{ParseError, Result}; /// Parse a lump of planes. /// A lump is (data length / plane size) planes long pub fn from_data(data: &[u8]) -> Result<Box<[Plane]>> { - let length = data.len() / PLANE_SIZE; if data.is_empty() || data.len() % PLANE_SIZE != 0 || length % 2 != 0 { return Err(ParseError::Invalid); } - let mut planes = Vec::with_capacity(length / 2); for n in 0..length { let offset = n * PLANE_SIZE; @@ -49,11 +47,11 @@ pub fn from_data(data: &[u8]) -> Result<Box<[Plane]>> { impl<T: CoordSystem> HasPlanes<T> for Q3BSPFile<T> { type PlanesIter<'a> = std::slice::Iter<'a, Plane>; - fn planes_iter<'a>(&'a self) -> Self::PlanesIter<'a> { + fn planes_iter(&self) -> Self::PlanesIter<'_> { self.planes.iter() } - fn get_plane<'a>(&'a self, idx: u32) -> &'a Plane { + fn get_plane(&self, idx: u32) -> &Plane { &self.planes[idx as usize] } -}
\ No newline at end of file +} diff --git a/stockton-levels/src/q3/textures.rs b/stockton-levels/src/q3/textures.rs index e317898..5f8a19f 100644 --- a/stockton-levels/src/q3/textures.rs +++ b/stockton-levels/src/q3/textures.rs @@ -18,10 +18,10 @@ use std::str; use super::Q3BSPFile; -use crate::traits::textures::*; -use crate::helpers::slice_to_u32; -use crate::types::{Result, ParseError}; use crate::coords::CoordSystem; +use crate::helpers::slice_to_u32; +use crate::traits::textures::*; +use crate::types::{ParseError, Result}; const TEXTURE_LUMP_SIZE: usize = 64 + 4 + 4; @@ -42,9 +42,16 @@ pub fn from_data(lump: &[u8]) -> Result<Box<[Texture]>> { for n in 0..length { let offset = n * TEXTURE_LUMP_SIZE; textures.push(Texture { - name: str::from_utf8(&lump[offset..offset + 64]).map_err(|_| ParseError::Invalid)?.trim_matches('\0').to_owned(), - surface: SurfaceFlags::from_bits_truncate(slice_to_u32(&lump[offset + 64..offset + 68])), - contents: ContentsFlags::from_bits_truncate(slice_to_u32(&lump[offset + 68..offset + 72])), + name: str::from_utf8(&lump[offset..offset + 64]) + .map_err(|_| ParseError::Invalid)? + .trim_matches('\0') + .to_owned(), + surface: SurfaceFlags::from_bits_truncate(slice_to_u32( + &lump[offset + 64..offset + 68], + )), + contents: ContentsFlags::from_bits_truncate(slice_to_u32( + &lump[offset + 68..offset + 72], + )), }); } @@ -54,11 +61,11 @@ pub fn from_data(lump: &[u8]) -> Result<Box<[Texture]>> { impl<T: CoordSystem> HasTextures for Q3BSPFile<T> { type TexturesIter<'a> = std::slice::Iter<'a, Texture>; - fn textures_iter<'a>(&'a self) -> Self::TexturesIter<'a> { + fn textures_iter(&self) -> Self::TexturesIter<'_> { self.textures.iter() } - fn get_texture<'a>(&'a self, idx: u32) -> &'a Texture { + fn get_texture(&self, idx: u32) -> &Texture { &self.textures[idx as usize] } } @@ -84,10 +91,7 @@ fn textures_single_texture() { lump[0].surface, SurfaceFlags::NO_DAMAGE | SurfaceFlags::SLICK | SurfaceFlags::FLESH | SurfaceFlags::DUST ); - assert_eq!( - lump[0].contents, - ContentsFlags::SOLID | ContentsFlags::LAVA - ); + assert_eq!(lump[0].contents, ContentsFlags::SOLID | ContentsFlags::LAVA); } #[test] @@ -140,13 +144,7 @@ fn textures_multiple_textures() { SurfaceFlags::POINT_LIGHT | SurfaceFlags::SKIP ); - assert_eq!( - lump[0].contents, - ContentsFlags::SOLID | ContentsFlags::LAVA - ); + assert_eq!(lump[0].contents, ContentsFlags::SOLID | ContentsFlags::LAVA); assert_eq!(lump[1].contents, ContentsFlags::SOLID); - assert_eq!( - lump[2].contents, - ContentsFlags::SOLID | ContentsFlags::FOG - ); + assert_eq!(lump[2].contents, ContentsFlags::SOLID | ContentsFlags::FOG); } diff --git a/stockton-levels/src/q3/tree.rs b/stockton-levels/src/q3/tree.rs index 4d49e76..bc22b22 100644 --- a/stockton-levels/src/q3/tree.rs +++ b/stockton-levels/src/q3/tree.rs @@ -17,11 +17,11 @@ //! Parses the BSP tree into a usable format +use super::Q3BSPFile; use crate::coords::CoordSystem; -use crate::helpers::{slice_to_u32, slice_to_i32, slice_to_vec3i}; -use crate::types::{ParseError, Result}; +use crate::helpers::{slice_to_i32, slice_to_u32, slice_to_vec3i}; use crate::traits::tree::*; -use super::Q3BSPFile; +use crate::types::{ParseError, Result}; const NODE_SIZE: usize = 4 + (4 * 2) + (4 * 3) + (4 * 3); const LEAF_SIZE: usize = 4 * 6 + (4 * 3 * 2); @@ -39,18 +39,17 @@ pub fn from_data( } Ok(compile_node( - 0, - nodes, - leaves, - leaf_faces, - leaf_brushes, - n_faces, - n_brushes, - )?, - ) + 0, + nodes, + leaves, + leaf_faces, + leaf_brushes, + n_faces, + n_brushes, + )?) } - /// Internal function. Visits given node and all its children. Used to recursively build tree. +/// Internal function. Visits given node and all its children. Used to recursively build tree. fn compile_node( i: i32, nodes: &[u8], @@ -93,7 +92,7 @@ fn compile_node( let start = slice_to_u32(&raw[40..44]) as usize; let n = slice_to_u32(&raw[44..48]) as usize; let mut brushes = Vec::with_capacity(n); - + if n > 0 { if start + n > leaf_brushes.len() / 4 { return Err(ParseError::Invalid); @@ -163,7 +162,7 @@ fn compile_node( } impl<T: CoordSystem> HasBSPTree<T> for Q3BSPFile<T> { - fn get_bsp_root<'a>(&'a self) -> &'a BSPNode { + fn get_bsp_root(&self) -> &BSPNode { &self.tree_root } -}
\ No newline at end of file +} diff --git a/stockton-levels/src/q3/vertices.rs b/stockton-levels/src/q3/vertices.rs index d517ede..649d0d5 100644 --- a/stockton-levels/src/q3/vertices.rs +++ b/stockton-levels/src/q3/vertices.rs @@ -18,10 +18,10 @@ use std::convert::TryInto; use super::Q3BSPFile; +use crate::coords::CoordSystem; use crate::helpers::{slice_to_u32, slice_to_vec3}; -use crate::types::{Result, ParseError, RGBA}; use crate::traits::vertices::*; -use crate::coords::CoordSystem; +use crate::types::{ParseError, Result, RGBA}; /// The size of one vertex const VERTEX_SIZE: usize = (4 * 3) + (2 * 2 * 4) + (4 * 3) + 4; @@ -56,7 +56,6 @@ pub fn meshverts_from_data(data: &[u8]) -> Result<Box<[MeshVert]>> { } let length = data.len() / 4; - let mut meshverts = Vec::with_capacity(length as usize); for n in 0..length { meshverts.push(slice_to_u32(&data[n * 4..(n + 1) * 4])) @@ -68,11 +67,11 @@ pub fn meshverts_from_data(data: &[u8]) -> Result<Box<[MeshVert]>> { impl<T: CoordSystem> HasVertices<T> for Q3BSPFile<T> { type VerticesIter<'a> = std::slice::Iter<'a, Vertex>; - fn vertices_iter<'a>(&'a self) -> Self::VerticesIter<'a> { + fn vertices_iter(&self) -> Self::VerticesIter<'_> { self.vertices.iter() } - fn get_vertex<'a>(&'a self, index: u32) -> &'a Vertex { + fn get_vertex(&self, index: u32) -> &Vertex { &self.vertices[index as usize] } } @@ -80,11 +79,11 @@ impl<T: CoordSystem> HasVertices<T> for Q3BSPFile<T> { impl<T: CoordSystem> HasMeshVerts<T> for Q3BSPFile<T> { type MeshVertsIter<'a> = std::slice::Iter<'a, MeshVert>; - fn meshverts_iter<'a>(&'a self) -> Self::MeshVertsIter<'a> { + fn meshverts_iter(&self) -> Self::MeshVertsIter<'_> { self.meshverts.iter() } fn get_meshvert<'a>(&self, index: u32) -> MeshVert { self.meshverts[index as usize] } -}
\ No newline at end of file +} diff --git a/stockton-levels/src/q3/visdata.rs b/stockton-levels/src/q3/visdata.rs index fd48415..92b61d5 100644 --- a/stockton-levels/src/q3/visdata.rs +++ b/stockton-levels/src/q3/visdata.rs @@ -1,4 +1,4 @@ -// Copyright (C) Oscar Shrimpton 2019 +// Copyright (C) Oscar Shrimpton 2019 // This program is free software: you can redistribute it and/or modify it // under the terms of the GNU General Public License as published by the Free @@ -14,22 +14,21 @@ // with this program. If not, see <http://www.gnu.org/licenses/>. //! Parses visdata from Q3 BSPs. - -use std::vec::IntoIter; use bitvec::prelude::*; +use std::vec::IntoIter; -use crate::types::{Result, ParseError}; -use crate::traits::visdata::*; -use crate::helpers::slice_to_i32; use super::file::Q3BSPFile; use crate::coords::CoordSystem; +use crate::helpers::slice_to_i32; +use crate::traits::visdata::*; +use crate::types::{ParseError, Result}; /// Stores cluster-to-cluster visibility information. pub fn from_data(data: &[u8]) -> Result<Box<[BitBox<Local, u8>]>> { if data.len() < 8 { return Err(ParseError::Invalid); } - + let n_vecs = slice_to_i32(&data[0..4]) as usize; let size_vecs = slice_to_i32(&data[4..8]) as usize; @@ -53,7 +52,7 @@ impl<T: CoordSystem> HasVisData for Q3BSPFile<T> { fn all_visible_from(&self, from: ClusterId) -> Self::VisibleIterator { let mut visible = vec![]; - for (idx,val) in self.visdata[from as usize].iter().enumerate() { + for (idx, val) in self.visdata[from as usize].iter().enumerate() { if *val { visible.push(idx as u32); } @@ -66,4 +65,3 @@ impl<T: CoordSystem> HasVisData for Q3BSPFile<T> { self.visdata[from as usize][dest as usize] } } - |