1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const BRUSH_SIZE: usize = (4 * 3);
const SIDE_SIZE: usize = (4 * 2);
use crate::lumps::helpers::slice_to_i32;
use crate::lumps::planes::PlanesLump;
use crate::lumps::textures::TexturesLump;
use crate::types::Result;
#[derive(Debug, Clone)]
pub struct BrushesLump {
pub brushes: Box<[Brush]>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Brush {
pub sides: Box<[BrushSide]>,
pub texture_idx: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct BrushSide {
pub plane_idx: usize,
pub texture_idx: usize,
pub is_opposing: bool,
}
impl BrushesLump {
pub fn from_lump(
brushes_lump: &[u8],
brush_sides_lump: &[u8],
textures_lump: &TexturesLump,
planes_lump: &PlanesLump
) -> Result<BrushesLump> {
if brushes_lump.len() % BRUSH_SIZE != 0 || brush_sides_lump.len() % SIDE_SIZE != 0 {
return Err(invalid_error!("BrushesLump is incorrectly sized"));
}
let length = brushes_lump.len() / BRUSH_SIZE;
let mut brushes = Vec::with_capacity(length as usize);
for n in 0..length {
let offset = n * BRUSH_SIZE;
let brush = &brushes_lump[offset..offset + BRUSH_SIZE];
let texture_idx = slice_to_i32(&brush[8..12]) as usize;
if texture_idx >= textures_lump.textures.len() {
return Err(invalid_error!("Brushes references a texture that doesn't exist"));
}
brushes.push(Brush {
sides: BrushesLump::get_sides(
brush_sides_lump,
slice_to_i32(&brush[0..4]),
slice_to_i32(&brush[4..8]),
textures_lump,
planes_lump,
)?,
texture_idx
});
}
Ok(BrushesLump {
brushes: brushes.into_boxed_slice(),
})
}
fn get_sides(
brush_sides_lump: &[u8],
start: i32,
length: i32,
textures_lump: &TexturesLump,
planes_lump: &PlanesLump,
) -> Result<Box<[BrushSide]>> {
let mut sides = Vec::with_capacity(length as usize);
if length > 0 {
for n in start..start + length {
let offset = n as usize * SIDE_SIZE;
let brush = &brush_sides_lump[offset..offset + SIDE_SIZE];
let plane_idx = slice_to_i32(&brush[0..4]) as usize;
if plane_idx / 2 >= planes_lump.planes.len() {
return Err(invalid_error!("BrushSide references a plane that doesn't exist"));
}
let is_opposing = plane_idx % 2 != 0;
let texture_idx = slice_to_i32(&brush[4..8]) as usize;
if texture_idx >= textures_lump.textures.len() {
return Err(invalid_error!("BrushSide references a texture that doesn't exist"));
}
sides.push(BrushSide {
plane_idx,
texture_idx,
is_opposing
});
}
}
Ok(sides.into_boxed_slice())
}
}