qbsp 0.14.1

Rust crate for parsing and operating with Quake 1, 2, and GoldSrc BSP files
Documentation
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! Data definitions for the BSP node tree.

use crate::{
	BspParseError,
	data::{util::UBspValue, visdata::VisDataRef},
};
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
use glam::{U16Vec3, Vec3};
use qbsp_macros::{BspValue, BspVariableValue};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use bitflags::bitflags;

use crate::{
	BspResult,
	data::util::NoField,
	reader::{BspByteReader, BspParseContext, BspValue, BspVariableValue},
};

/// A reference to a [`BspNode`].
///
/// Reads an `i32` or `i16` (depending on context and format). Negative indices are treated as a leaf index
/// of `1 - signed_node_ref`. `-1` (leaf 0) means out-of-bounds.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BspNodeRef<T = u32> {
	/// A reference to a node.
	Node(T),
	/// A reference to a leaf.
	Leaf(T),
}

impl BspValue for BspNodeRef<u32> {
	fn bsp_parse(reader: &mut BspByteReader) -> BspResult<Self> {
		Ok(BspNodeRef::from_i32(i32::bsp_parse(reader)?))
	}

	fn bsp_struct_size(ctx: &BspParseContext) -> usize {
		i32::bsp_struct_size(ctx)
	}
}

impl BspValue for BspNodeRef<u16> {
	fn bsp_parse(reader: &mut BspByteReader) -> BspResult<Self> {
		Ok(BspNodeRef::from_i16(i16::bsp_parse(reader)?))
	}

	fn bsp_struct_size(ctx: &BspParseContext) -> usize {
		i16::bsp_struct_size(ctx)
	}
}

/// A reference to a [`BspNode`]. Reads an `i32`, if positive it's an index of a node, if negative it's the index of a leaf.
#[derive(BspVariableValue, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[bsp29(BspNodeRef<u16>)]
#[bsp2(BspNodeRef<u32>)]
#[bsp30(BspNodeRef<u16>)]
#[bsp38(BspNodeRef<u32>)]
#[qbism(BspNodeRef<u32>)]
pub struct BspNodeSubRef(BspNodeRef);

impl From<BspNodeRef<u16>> for BspNodeRef<u32> {
	fn from(value: BspNodeRef<u16>) -> Self {
		match value {
			BspNodeRef::Node(val) => BspNodeRef::Node(val.into()),
			BspNodeRef::Leaf(val) => BspNodeRef::Leaf(val.into()),
		}
	}
}

impl<T> BspNodeRef<T>
where
	T: Copy,
{
	/// If this reference points to a node, get the index of the node.
	pub fn node(&self) -> Option<T> {
		match *self {
			Self::Node(i) => Some(i),
			Self::Leaf(_) => None,
		}
	}

	/// If this reference points to a leaf, get the index of the leaf. Note that `Some(0)`
	/// means out-of-bounds.
	pub fn leaf(&self) -> Option<T> {
		match *self {
			Self::Leaf(i) => Some(i),
			Self::Node(_) => None,
		}
	}
}

impl From<i32> for BspNodeRef {
	fn from(value: i32) -> Self {
		Self::from_i32(value)
	}
}

impl From<i16> for BspNodeRef<u16> {
	fn from(value: i16) -> Self {
		Self::from_i16(value as _)
	}
}

impl BspNodeRef<u32> {
	/// Create a 32-bit `BspNodeRef` from an `i32`. Negative indices are treated as a leaf index
	/// of `1 - signed_node_ref`. `-1` (leaf 0) means out-of-bounds.
	pub const fn from_i32(value: i32) -> Self {
		if value.is_negative() {
			// Bitwise not handles integer asymmetry and overflow.
			Self::Leaf(!value as u32)
		} else {
			Self::Node(value as u32)
		}
	}
}

impl BspNodeRef<u16> {
	/// Create a 16-bit `BspNodeRef` from an `i16`. Negative indices are treated as a leaf index
	/// of `1 - signed_node_ref`. `-1` (leaf 0) means out-of-bounds.
	pub const fn from_i16(value: i16) -> Self {
		if value.is_negative() {
			// Bitwise not handles integer asymmetry and overflow.
			Self::Leaf(!value as u16)
		} else {
			Self::Node(value as u16)
		}
	}
}

/// Leaf contents for BSP29 and BSP2, descriptions taken from
/// [the community BSP29 specification](https://www.gamers.org/dEngine/quake/spec/quake-spec34/qkspec_4.htm),
/// and refer to the behavior when the camera is inside the given leaf contents.
#[derive(BspValue, Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(i32)]
pub enum Bsp29LeafContents {
	/// Ordinary leaf (air).
	#[default]
	Empty = -1,
	/// The leaf is entirely inside a solid (nothing is displayed).
	Solid = -2,
	/// Water, the vision is troubled.
	Water = -3,
	/// Slime, green acid that hurts the player.
	Slime = -4,
	/// Lava, vision turns red and the player is badly hurt.
	Lava = -5,
	/// Behaves like water, but is used for sky.
	///
	/// > *Note*: For clarification, this is labeled "sky" internally, but Quake 1 treats
	/// > any leaf contants of value `-3` or less to be water, with extra effects only
	/// > defined for `-4` and `-5`. As a result, despite the below contents having specific
	/// > meanings while compiling the map, if found in a map at runtime they just behave
	/// > like water. See the Quake GPL release:
	/// >
	/// > - [Rendering functions](https://github.com/id-Software/Quake/blob/0023db327bc1db00068284b70e1db45857aeee35/WinQuake/r_misc.c#L439)
	/// > - [Server-side](https://github.com/id-Software/Quake/blob/0023db327bc1db00068284b70e1db45857aeee35/WinQuake/world.c#L532-L533).
	Sky = -6,
	// Origin = -7, removed at csg time
	/// Clip brushes
	///
	/// > *Note*: Behaves like water in Quake 1 if found at runtime, see comment for [`Self::Sky`].
	Clip = -8,
	/// *TODO*: Only has meaning at BSP compile-time.
	///
	/// > *Note*: Behaves like water in Quake 1 if found at runtime, see comment for [`Self::Sky`].
	Current0 = -9,
	/// *TODO*: Only has meaning at BSP compile-time.
	///
	/// > *Note*: Behaves like water in Quake 1 if found at runtime, see comment for [`Self::Sky`].
	Current90 = -10,
	/// *TODO*: Only has meaning at BSP compile-time.
	///
	/// > *Note*: Behaves like water in Quake 1 if found at runtime, see comment for [`Self::Sky`].
	Current180 = -11,
	/// *TODO*: Only has meaning at BSP compile-time.
	///
	/// > *Note*: Behaves like water in Quake 1 if found at runtime, see comment for [`Self::Sky`].
	Current270 = -12,
	/// *TODO*: Only has meaning at BSP compile-time.
	///
	/// > *Note*: Behaves like water in Quake 1 if found at runtime, see comment for [`Self::Sky`].
	CurrentUp = -13,
	/// *TODO*: Only has meaning at BSP compile-time.
	///
	/// > *Note*: Behaves like water in Quake 1 if found at runtime, see comment for [`Self::Sky`].
	CurrentDown = -14,
}

/// Wrapper for [`BspLeafContents`] that reads an [`prim@i16`] rather than an [`prim@i32`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ShortBsp29LeafContents(pub Bsp29LeafContents);

impl BspValue for ShortBsp29LeafContents {
	fn bsp_parse(reader: &mut BspByteReader) -> BspResult<Self> {
		let value = reader.read::<i16>()? as i32;

		Bsp29LeafContents::bsp_parse(&mut BspByteReader::new(&value.to_le_bytes(), reader.ctx)).map(Self)
	}

	fn bsp_struct_size(_ctx: &BspParseContext) -> usize {
		size_of::<i16>()
	}
}

bitflags! {
	#[derive(Debug, Clone, Copy, PartialEq, Eq)]
	#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
	#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
	#[repr(transparent)]
	#[cfg_attr(feature = "bevy_reflect", reflect(opaque))]
	pub struct BspLeafContentFlags: u32 {
		// An eye is never valid in a solid
		const SOLID = 0b0000_0000_0000_0000_0000_0000_0000_0001;
		const WINDOW = 0b0000_0000_0000_0000_0000_0000_0000_0010;
		const AUX = 0b0000_0000_0000_0000_0000_0000_0000_0100;
		const LAVA = 0b0000_0000_0000_0000_0000_0000_0000_1000;
		const SLIME = 0b0000_0000_0000_0000_0000_0000_0001_0000;
		const WATER = 0b0000_0000_0000_0000_0000_0000_0010_0000;
		const MIST = 0b0000_0000_0000_0000_0000_0000_0100_0000;

		const AREA_PORTAL = 0b0000_0000_0000_0000_1000_0000_0000_0000;

		const PLAYER_CLIP = 0b0000_0000_0000_0001_0000_0000_0000_0000;
		const MONSTER_CLIP = 0b0000_0000_0000_0010_0000_0000_0000_0000;

		// Bot-specific contents types
		const CURRENT_0 = 0b0000_0000_0000_0100_0000_0000_0000_0000;
		const CURRENT_90 = 0b0000_0000_0000_1000_0000_0000_0000_0000;
		const CURRENT_180 = 0b0000_0000_0001_0000_0000_0000_0000_0000;
		const CURRENT_270 = 0b0000_0000_0010_0000_0000_0000_0000_0000;
		const CURRENT_UP = 0b0000_0000_0100_0000_0000_0000_0000_0000;
		const CURRENT_DOWN = 0b0000_0000_1000_0000_0000_0000_0000_0000;

		// Removed before bsping an entity
		const ORIGIN = 0b0000_0001_0000_0000_0000_0000_0000_0000;

		// Should never be on a brush; only in game
		const MONSTER = 0b0000_0010_0000_0000_0000_0000_0000_0000;
		const DEAD_MONSTER = 0b0000_0100_0000_0000_0000_0000_0000_0000;
		// Brushes not used for the bsp
		const DETAIL = 0b0000_1000_0000_0000_0000_0000_0000_0000;
		// Don't consume surface fragments inside
		const TRANSLUCENT = 0b0001_0000_0000_0000_0000_0000_0000_0000;
		const LADDER = 0b0010_0000_0000_0000_0000_0000_0000_0000;
		/// Unused in Quake 2; but included in case you want to add an extension.
		const UNUSED1 = 0b0100_0000_0000_0000_0000_0000_0000_0000;
		/// Unused in Quake 2; but included in case you want to add an extension.
		const UNUSED2 = 0b1000_0000_0000_0000_0000_0000_0000_0000;
	}
}

impl BspValue for BspLeafContentFlags {
	fn bsp_parse(reader: &mut BspByteReader) -> BspResult<Self> {
		u32::bsp_parse(reader).map(Self::from_bits_truncate)
	}

	fn bsp_struct_size(ctx: &BspParseContext) -> usize {
		u32::bsp_struct_size(ctx)
	}
}

impl From<Bsp29LeafContents> for BspLeafContentFlags {
	fn from(value: Bsp29LeafContents) -> Self {
		match value {
			Bsp29LeafContents::Empty => Self::empty(),
			Bsp29LeafContents::Solid => BspLeafContentFlags::SOLID,
			Bsp29LeafContents::Water => BspLeafContentFlags::WATER,
			Bsp29LeafContents::Slime => BspLeafContentFlags::SLIME,
			Bsp29LeafContents::Lava => BspLeafContentFlags::LAVA,
			// Sky is considered solid for the purposes of tracing.
			Bsp29LeafContents::Sky => BspLeafContentFlags::SOLID,
			// Clip is considered solid for the purposes of tracing.
			Bsp29LeafContents::Clip => BspLeafContentFlags::SOLID,
			Bsp29LeafContents::Current0 => BspLeafContentFlags::CURRENT_0,
			Bsp29LeafContents::Current90 => BspLeafContentFlags::CURRENT_90,
			Bsp29LeafContents::Current180 => BspLeafContentFlags::CURRENT_180,
			Bsp29LeafContents::Current270 => BspLeafContentFlags::CURRENT_270,
			Bsp29LeafContents::CurrentUp => BspLeafContentFlags::CURRENT_UP,
			Bsp29LeafContents::CurrentDown => BspLeafContentFlags::CURRENT_DOWN,
		}
	}
}

impl From<ShortBsp29LeafContents> for BspLeafContentFlags {
	fn from(value: ShortBsp29LeafContents) -> Self {
		value.0.into()
	}
}

/// If loading a BSP2, parses a float-based bounding box, otherwise parses a short-based bounding box.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BoundingBox {
	pub min: Vec3,
	pub max: Vec3,
}

impl From<FloatBoundingBox> for BoundingBox {
	fn from(value: FloatBoundingBox) -> Self {
		Self {
			min: value.min,
			max: value.max,
		}
	}
}

impl From<ShortBoundingBox> for BoundingBox {
	fn from(value: ShortBoundingBox) -> Self {
		Self {
			min: value.min.as_vec3(),
			max: value.max.as_vec3(),
		}
	}
}

impl BspVariableValue for BoundingBox {
	type Bsp29 = ShortBoundingBox;
	type Bsp2 = FloatBoundingBox;
	type Bsp30 = ShortBoundingBox;
	type Bsp38 = ShortBoundingBox;
	type Qbism = FloatBoundingBox;
}

#[derive(BspValue, Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct FloatBoundingBox {
	pub min: Vec3,
	pub max: Vec3,
}

#[derive(BspValue, Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ShortBoundingBox {
	pub min: U16Vec3,
	pub max: U16Vec3,
}

#[derive(BspValue, Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BspNode {
	/// Index of the [`BspPlane`] that splits the node.
	pub plane_idx: u32,

	pub front: BspNodeSubRef,
	pub back: BspNodeSubRef,

	/// Bounding box of the node and all its children.
	pub bound: BoundingBox,
	/// Index of the first [`BspFace`](crate::data::models::BspFace) the node contains.
	pub face_idx: UBspValue,
	/// Number of faces this node contains.
	pub face_num: UBspValue,
}

/// According to the [`gamers.org` specification](https://www.gamers.org/dEngine/quake/spec/quake-spec34/qkspec_4.htm#BL9):
///
/// > This structure is used to give a rough and somewhat exaggerated boundary to a given model. It does not separate models from each others, and is not used at all in the rendering of the levels
///
/// > Actually, the clip nodes are only used as a first and primitive collision checking method.
///
/// I think it is also used for shape casting.
#[derive(BspValue, Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BspClipNode {
	/// Index of the [`BspPlane`] that splits the clip node.
	pub plane_idx: u32,

	/// If positive, id of Front child node. If -2, the Front part is inside the model. If -1, the Front part is outside the model.
	pub front: BspNodeSubRef,
	/// If positive, id of Back child node. If -2, the Back part is inside the model. If -1, the Back part is outside the model.
	pub back: BspNodeSubRef,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BspAreaIdx(pub Option<u32>);

impl BspVariableValue for BspAreaIdx {
	type Bsp29 = NoField;
	type Bsp2 = NoField;
	type Bsp30 = NoField;
	type Bsp38 = u16;
	type Qbism = u32;
}

impl From<u16> for BspAreaIdx {
	fn from(value: u16) -> Self {
		Self(Some(value as u32))
	}
}
impl From<u32> for BspAreaIdx {
	fn from(value: u32) -> Self {
		Self(Some(value))
	}
}
impl From<NoField> for BspAreaIdx {
	fn from(_: NoField) -> Self {
		Self(None)
	}
}

#[derive(BspVariableValue, Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[bsp29(Bsp29LeafContents)]
#[bsp2(Bsp29LeafContents)]
#[bsp30(BspLeafContentFlags)]
#[bsp38(BspLeafContentFlags)]
#[qbism(BspLeafContentFlags)]
pub struct BspLeafContents(pub BspLeafContentFlags);

#[derive(BspValue, Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BspLeaf {
	pub contents: BspLeafContents,

	/// For BSP29, BSP2, and BSP30, this is an index into the `visibility` byte array where the
	/// visdata for this leaf starts. For BSP38, this is a "cluster". Leafs with the same cluster
	/// should be put within the same model, and are considered potentially visible or not as a
	/// group.
	pub visdata: VisDataRef,

	pub area: BspAreaIdx,

	/// The AABB bounding box of this leaf.
	pub bound: BoundingBox,

	/// Index in the `mark_surfaces` list.
	pub face_idx: UBspValue,
	/// Number of elements in the `mark_surfaces` list.
	pub face_num: UBspValue,

	pub ambience: Option<BspAmbience>,
	pub leaf_brushes: Option<BspLeafBrushes>,
}

#[derive(BspValue, Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BspLeafBrushes {
	pub idx: UBspValue,
	pub num: UBspValue,
}

impl BspVariableValue for Option<BspLeafBrushes> {
	type Bsp29 = NoField;
	type Bsp2 = NoField;
	type Bsp30 = NoField;
	type Bsp38 = BspLeafBrushes;
	type Qbism = BspLeafBrushes;
}

#[derive(BspValue, Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BspAmbience {
	pub water: u8,
	pub sky: u8,
	pub slime: u8,
	pub lava: u8,
}

impl BspVariableValue for Option<BspAmbience> {
	type Bsp29 = BspAmbience;
	type Bsp2 = BspAmbience;
	type Bsp30 = BspAmbience;
	// Quake 2 does not have the `ambience` field.
	type Bsp38 = NoField;
	type Qbism = NoField;
}

#[derive(BspValue, Debug, Clone, Copy)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BspPlane {
	pub normal: Vec3,
	pub dist: f32,
	/// Type of plane depending on normal vector.
	pub ty: BspPlaneType,
}

impl BspPlane {
	/// `>0` = front, `<0` = back, `0` = on plane
	pub fn point_side(&self, point: Vec3) -> f32 {
		let plane_axis = self.ty as usize;

		// If the plane lies on a cardinal axis, the computation is much simpler.
		if plane_axis < 3 {
			point[plane_axis] - self.dist
		} else {
			(self.normal.as_dvec3().dot(point.as_dvec3()) - self.dist as f64) as f32
		}
	}
}

/// Type of plane depending on normal vector.
///
/// Referenced from [this specification](https://www.gamers.org/dEngine/quake/spec/quake-spec34/qkspec_4.htm#BL1).
#[derive(BspValue, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(u32)]
pub enum BspPlaneType {
	/// Axial plane, in X
	AxialX = 0,
	/// Axial plane, in Y
	AxialY = 1,
	/// Axial plane, in Z
	AxialZ = 2,
	/// Non axial plane, roughly toward X
	AroundX = 3,
	/// Non axial plane, roughly toward Y
	AroundY = 4,
	/// Non axial plane, roughly toward Z
	AroundZ = 5,
}