source_sdk_2013 0.1.0-alpha.8

Higher-level bindings for the Source SDK.
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
//! Tests of `EngineTrace`: the boxes and points it clips to one entity, with
//! the ray the engine receives, as `Ray_t::Init` makes it, and the answer it
//! gives; and the lines and hulls it traces, with the ray, mask and kind of
//! trace the engine receives, the entities the filters let a ray hit, and
//! what the traces report.

use super::*;
use crate::test_support::entities::{MockEntity, set_datamap, state_maps};
use sdk_raw::test_support::{mock_vtable, unexpected_call};
use std::cell::{Cell, RefCell};

thread_local! {
	/// The entities along every line the mock engine traces, nearest first.
	static ALONG: RefCell<Vec<*mut sys::IHandleEntity>> = const { RefCell::new(Vec::new()) };

	/// Every call to `ClipRayToEntity`, in order.
	static CLIPPED: RefCell<Vec<Clipped>> = const { RefCell::new(Vec::new()) };

	/// Whether the traces `ClipRayToEntity` fills start solid.
	static START_SOLID: Cell<bool> = const { Cell::new(false) };

	/// Every call to `TraceRay`, in order.
	static TRACED: RefCell<Vec<Traced>> = const { RefCell::new(Vec::new()) };
}

/// The arguments `ClipRayToEntity` received.
#[derive(Debug, Clone, PartialEq)]
struct Clipped {
	start: Vector,
	delta: Vector,
	start_offset: Vector,
	extents: Vector,
	is_ray: bool,
	is_swept: bool,
	mask: c_uint,
	entity: *mut sys::IHandleEntity,
}

/// The arguments `TraceRay` received, with the kind of trace its filter asked
/// for.
#[derive(Debug, Clone, PartialEq)]
struct Traced {
	start: Vector,
	delta: Vector,
	start_offset: Vector,
	extents: Vector,
	is_ray: bool,
	swept: bool,
	mask: c_uint,
	trace_type: sys::TraceType_t,
}

/// A mock of the engine's traces, which keeps its vtable alive.
struct MockEngineTrace {
	_vtable: Box<sys::IEngineTrace__bindgen_vtable>,
	interface: Box<sys::IEngineTrace>,
}

impl MockEngineTrace {
	/// Records `ClipRayToEntity`, whose traces start solid as [`START_SOLID`]
	/// says, and `TraceRay`, which stops halfway along a line at the nearest of
	/// the entities `along` it that the filter lets it hit, and fills every
	/// other slot with a stub that fails the test if called.
	fn new(along: &[*mut sys::IHandleEntity]) -> Self {
		// SAFETY: The vtable holds only function pointers, `unexpected_call`
		// aborts whichever slot reaches it, and the patch only writes slots of
		// the vtable being built.
		let vtable = unsafe {
			mock_vtable::<sys::IEngineTrace__bindgen_vtable>(
				unexpected_call as *const (),
				|vtable| {
					(&raw mut (*vtable).IEngineTrace_ClipRayToEntity).write(clip_ray_to_entity);
					(&raw mut (*vtable).IEngineTrace_TraceRay).write(trace_ray);
				},
			)
		};
		let interface = Box::new(sys::IEngineTrace {
			vtable_: &raw const *vtable,
		});

		ALONG.set(along.to_vec());
		CLIPPED.take();
		START_SOLID.set(false);
		TRACED.take();

		Self {
			_vtable: vtable,
			interface,
		}
	}

	/// The mock, as the wrapper sees it.
	fn engine_trace(&mut self) -> EngineTrace<'_> {
		// SAFETY: The mock outlives the borrow.
		unsafe { EngineTrace::from_raw(NonNull::from(&mut *self.interface)) }
	}
}

/// `IEngineTrace::ClipRayToEntity`, which records its arguments, and fills
/// the trace as starting solid if [`START_SOLID`] is set.
unsafe extern "C" fn clip_ray_to_entity(
	_this: *mut sys::IEngineTrace,
	ray: *const sys::Ray_t,
	mask: c_uint,
	entity: *mut sys::IHandleEntity,
	trace: *mut sys::trace_t,
) {
	// SAFETY: The wrapper passes a live ray, and a live trace to fill, which
	// are read and written as the engine does.
	let clipped = unsafe {
		let ray = &*ray;

		(&raw mut (*trace)._base.startsolid).write(START_SOLID.get());

		Clipped {
			start: ray.m_Start._base.into(),
			delta: ray.m_Delta._base.into(),
			start_offset: ray.m_StartOffset._base.into(),
			extents: ray.m_Extents._base.into(),
			is_ray: ray.m_IsRay,
			is_swept: ray.m_IsSwept,
			mask,
			entity,
		}
	};

	CLIPPED.with_borrow_mut(|calls| calls.push(clipped));
}

/// `IEngineTrace::TraceRay`: offers the filter the entities along the ray,
/// unless it asks for the world alone, and stops halfway at the first it lets
/// the ray hit, on a surface facing back along the X axis, or reaches the
/// ray's end. As the engine does, it moves the end by the ray's start offset,
/// back to where a box's position is. A hit is on [`SURFACE`] of a
/// displacement, with hitbox 4, and the ray leaves what it starts in a
/// quarter of the way along.
unsafe extern "C" fn trace_ray(
	_this: *mut sys::IEngineTrace,
	ray: *const sys::Ray_t,
	mask: c_uint,
	filter: *mut sys::ITraceFilter,
	trace: *mut sys::trace_t,
) {
	// SAFETY: The wrappers pass a live ray, filter and trace, which nothing
	// else uses during the call.
	let (ray, methods, trace) = unsafe { (&*ray, &*(*filter).vtable_, &mut *trace) };

	// SAFETY: The filter is the wrapper's own, live during the call.
	let trace_type = unsafe { (methods.ITraceFilter_GetTraceType)(filter) };
	let (start, delta, start_offset) = (
		Vector::from(ray.m_Start._base),
		Vector::from(ray.m_Delta._base),
		Vector::from(ray.m_StartOffset._base),
	);

	TRACED.with_borrow_mut(|traced| {
		traced.push(Traced {
			start,
			delta,
			start_offset,
			extents: ray.m_Extents._base.into(),
			is_ray: ray.m_IsRay,
			swept: ray.m_IsSwept,
			mask,
			trace_type,
		});
	});

	let hit = ALONG
		.with_borrow(Vec::clone)
		.into_iter()
		.filter(|_| trace_type != sys::TraceType_t_TRACE_WORLD_ONLY)
		// SAFETY: As for the trace type. The filters only compare the entities.
		.find(|&entity| unsafe {
			(methods.ITraceFilter_ShouldHitEntity)(filter, entity, mask.cast_signed())
		});

	let fraction = if hit.is_some() { 0.5 } else { 1.0 };

	trace._base.fraction = fraction;
	trace._base.endpos = Vector(*start + *delta * fraction + *start_offset).into();

	if hit.is_some() {
		trace._base.plane.normal = Vector::new(-1.0, 0.0, 0.0).into();
		trace._base.contents = CONTENTS_MONSTER.cast_signed();
		trace._base.dispFlags = 0b111;
		trace.fractionleftsolid = 0.25;
		trace.surface.name = SURFACE.as_ptr();
		trace.surface.flags = 0x4;
		trace.hitbox = 4;
	}

	trace.m_pEnt = hit.map_or(ptr::null_mut(), <*mut _>::cast);
}

/// A line from the origin along the X axis, 100 units long.
const LINE: (Vector, Vector) = (Vector::new(0.0, 0.0, 0.0), Vector::new(100.0, 0.0, 0.0));

/// The name of the surface the mock engine's hits are on.
const SURFACE: &CStr = c"**studio**";

#[test]
fn boxes_start_at_their_center_and_lead_back_to_their_position() {
	let mut mock = MockEngineTrace::new(&[]);
	let mut room = MockEntity::new(7);
	let pointer = room.as_ptr().cast::<sys::IHandleEntity>();

	let overlaps = mock.engine_trace().box_overlaps_entity(
		room.entity(),
		Vector::new(10.0, 20.0, 30.0),
		Vector::new(-24.0, -24.0, 0.0),
		Vector::new(24.0, 24.0, 82.0),
		MASK_ALL,
	);

	assert!(!overlaps);

	CLIPPED.with_borrow(|clipped| {
		assert_eq!(
			*clipped,
			[Clipped {
				start: Vector::new(10.0, 20.0, 71.0),
				delta: Vector::new(0.0, 0.0, 0.0),
				start_offset: Vector::new(0.0, 0.0, -41.0),
				extents: Vector::new(24.0, 24.0, 41.0),
				is_ray: false,
				is_swept: false,
				mask: MASK_ALL,
				entity: pointer,
			}]
		);
	});
}

#[test]
fn points_are_rays_that_do_not_move() {
	let mut mock = MockEngineTrace::new(&[]);
	let mut room = MockEntity::new(7);
	let pointer = room.as_ptr().cast::<sys::IHandleEntity>();
	let origin = Vector::new(0.0, 0.0, 0.0);

	START_SOLID.set(true);

	let within = mock.engine_trace().box_overlaps_entity(
		room.entity(),
		Vector::new(-1.5, 2.0, 64.0),
		origin,
		origin,
		CONTENTS_SOLID,
	);

	assert!(within);

	CLIPPED.with_borrow(|clipped| {
		assert_eq!(
			*clipped,
			[Clipped {
				start: Vector::new(-1.5, 2.0, 64.0),
				delta: origin,
				start_offset: origin,
				extents: origin,
				is_ray: true,
				is_swept: false,
				mask: CONTENTS_SOLID,
				entity: pointer,
			}]
		);
	});
}

#[test]
fn lines_hit_the_nearest_entity_but_the_skipped_one() {
	let (mut skipped, mut other) = (MockEntity::new(1), MockEntity::new(2));
	let along = [skipped.as_ptr().cast(), other.as_ptr().cast()];
	let mut mock = MockEngineTrace::new(&along);

	let trace = mock
		.engine_trace()
		.trace_line(LINE.0, LINE.1, MASK_SOLID, Some(skipped.entity()));

	assert!(trace.hit());
	assert_eq!(trace.fraction, 0.5);
	assert_eq!(trace.end, Vector::new(50.0, 0.0, 0.0));
	assert_eq!(trace.entity.map(Entity::as_ptr), Some(other.as_ptr()));

	TRACED.with_borrow(|traced| {
		assert_eq!(
			*traced,
			[Traced {
				start: LINE.0,
				delta: LINE.1,
				start_offset: Vector::new(0.0, 0.0, 0.0),
				extents: Vector::new(0.0, 0.0, 0.0),
				is_ray: true,
				swept: true,
				mask: MASK_SOLID,
				trace_type: sys::TraceType_t_TRACE_EVERYTHING,
			}]
		);
	});
}

#[test]
fn lines_skipping_nothing_hit_the_nearest_entity() {
	let (mut first, mut second) = (MockEntity::new(1), MockEntity::new(2));
	let mut mock = MockEngineTrace::new(&[first.as_ptr().cast(), second.as_ptr().cast()]);

	let trace = mock
		.engine_trace()
		.trace_line(LINE.0, LINE.1, MASK_SOLID, None);

	assert_eq!(trace.entity.map(Entity::as_ptr), Some(first.as_ptr()));
}

#[test]
fn lines_past_only_the_skipped_entity_hit_nothing() {
	let mut skipped = MockEntity::new(1);
	let mut mock = MockEngineTrace::new(&[skipped.as_ptr().cast()]);

	let trace = mock
		.engine_trace()
		.trace_line(LINE.0, LINE.1, MASK_SOLID, Some(skipped.entity()));

	assert!(!trace.hit());
	assert_eq!(trace.end, LINE.1);
	assert_eq!(trace.entity, None);
}

#[test]
fn world_lines_pass_through_every_entity() {
	let mut entity = MockEntity::new(1);
	let mut mock = MockEngineTrace::new(&[entity.as_ptr().cast()]);

	let trace = mock
		.engine_trace()
		.trace_world_line(LINE.0, LINE.1, MASK_SOLID_BRUSHONLY);

	assert!(!trace.hit());
	assert_eq!(trace.end, LINE.1);

	TRACED.with_borrow(|traced| {
		assert_eq!(traced.len(), 1);
		assert_eq!(traced[0].mask, MASK_SOLID_BRUSHONLY);
		assert_eq!(traced[0].trace_type, sys::TraceType_t_TRACE_WORLD_ONLY);
	});
}

#[test]
fn hulls_start_at_their_center_and_end_at_their_position() {
	let mut entity = MockEntity::new(1);
	let mut mock = MockEngineTrace::new(&[entity.as_ptr().cast()]);
	let (mins, maxs) = (
		Vector::new(-24.0, -24.0, 0.0),
		Vector::new(24.0, 24.0, 82.0),
	);

	let trace = mock.engine_trace().trace(
		Ray::hull(LINE.0, LINE.1, mins, maxs),
		MASK_PLAYERSOLID,
		TraceFilter::Everything,
	);

	assert!(trace.hit());
	assert_eq!(trace.end, Vector::new(50.0, 0.0, 0.0));
	assert_eq!(trace.fraction_left_solid, 0.25);
	assert_eq!(trace.normal, Vector::new(-1.0, 0.0, 0.0));
	assert_eq!(trace.contents, CONTENTS_MONSTER.cast_signed());
	assert_eq!(trace.surface_name, Some(SURFACE));
	assert_eq!(trace.surface_flags, 0x4);
	assert_eq!(trace.displacement_flags, 0b111);
	assert_eq!(trace.hitbox, 4);
	assert_eq!(trace.entity.map(Entity::as_ptr), Some(entity.as_ptr()));

	TRACED.with_borrow(|traced| {
		assert_eq!(
			*traced,
			[Traced {
				start: Vector::new(0.0, 0.0, 41.0),
				delta: LINE.1,
				start_offset: Vector::new(0.0, 0.0, -41.0),
				extents: Vector::new(24.0, 24.0, 41.0),
				is_ray: false,
				swept: true,
				mask: MASK_PLAYERSOLID,
				trace_type: sys::TraceType_t_TRACE_EVERYTHING,
			}]
		);
	});

	// A hull that does not move is tested where it is.
	let still = mock.engine_trace().trace(
		Ray::hull(LINE.1, LINE.1, mins, maxs),
		MASK_PLAYERSOLID,
		TraceFilter::WorldAndProps,
	);

	assert!(!still.hit());
	assert_eq!(still.end, LINE.1);
	assert_eq!(still.normal, Vector::new(0.0, 0.0, 0.0));
	assert_eq!(still.surface_name, None);
	assert_eq!(still.displacement_flags, 0);
	assert_eq!(still.entity, None);
	TRACED.with_borrow(|traced| assert!(!traced[1].swept));
}

#[test]
fn static_props_are_hits_on_the_world_with_a_hitbox() {
	let mut world = MockEntity::new(0);
	let mut prop = MockEntity::new(1);

	// The mock's hits have hitbox 4, on a displacement.
	let mut mock = MockEngineTrace::new(&[world.as_ptr().cast()]);
	let trace = mock.engine_trace().trace(
		Ray::line(LINE.0, LINE.1),
		MASK_SOLID,
		TraceFilter::Everything,
	);

	assert_eq!(trace.static_prop(), Some(3));
	assert!(trace.hit_displacement());

	// An entity's hitboxes are no static prop.
	let mut mock = MockEngineTrace::new(&[prop.as_ptr().cast()]);
	let trace = mock.engine_trace().trace(
		Ray::line(LINE.0, LINE.1),
		MASK_SOLID,
		TraceFilter::Everything,
	);

	assert_eq!(trace.static_prop(), None);

	// Nor is a miss.
	let mut mock = MockEngineTrace::new(&[]);
	let trace = mock.engine_trace().trace(
		Ray::line(LINE.0, LINE.1),
		MASK_SOLID,
		TraceFilter::Everything,
	);

	assert_eq!(trace.static_prop(), None);
	assert!(!trace.hit_displacement());
}

/// The entity a line along [`LINE`] traced through `filter` stopped at, and
/// the kind of trace the filter asked the engine for.
fn stopped_at(
	engine: EngineTrace<'_>,
	filter: TraceFilter<'_, '_>,
) -> (Option<*mut sys::CBaseEntity>, sys::TraceType_t) {
	let trace = engine.trace(Ray::line(LINE.0, LINE.1), MASK_SOLID, filter);
	let trace_type = TRACED.with_borrow(|traced| traced.last().unwrap().trace_type);

	(trace.entity.map(Entity::as_ptr), trace_type)
}

#[test]
fn filters_choose_the_entities_a_ray_stops_at() {
	let mut mocks = [1, 2, 3].map(MockEntity::new);
	let [first, second, third] = mocks.each_mut().map(|entity| entity.entity());
	let along = [first, second, third].map(|entity| entity.as_ptr().cast());
	let mut mock = MockEngineTrace::new(&along);
	let engine = mock.engine_trace();
	let everything = sys::TraceType_t_TRACE_EVERYTHING;

	assert_eq!(
		stopped_at(engine, TraceFilter::Everything),
		(Some(first.as_ptr()), everything)
	);
	assert_eq!(
		stopped_at(engine, TraceFilter::EntitiesOnly),
		(Some(first.as_ptr()), sys::TraceType_t_TRACE_ENTITIES_ONLY)
	);
	assert_eq!(
		stopped_at(engine, TraceFilter::Skip(&[first, second])),
		(Some(third.as_ptr()), everything)
	);
	assert_eq!(
		stopped_at(engine, TraceFilter::Only(&[second])),
		(Some(second.as_ptr()), everything)
	);
	assert_eq!(
		stopped_at(engine, TraceFilter::Only(&[])),
		(None, everything)
	);
	assert_eq!(
		stopped_at(engine, TraceFilter::WorldAndProps),
		(None, everything)
	);
	assert_eq!(
		stopped_at(engine, TraceFilter::WorldOnly),
		(None, sys::TraceType_t_TRACE_WORLD_ONLY)
	);
}

#[test]
fn team_filters_pass_through_the_teams_entities() {
	let mut mocks = [1, 2, 3].map(MockEntity::new);

	// Every mock entity reports this datamap chain, which declares
	// `m_iTeamNum`.
	set_datamap(state_maps(Vec::new()));

	for (mock, team) in mocks.iter_mut().zip([2, 2, 3]) {
		mock.state().team = team;
	}

	let [red, red_too, blue] = mocks.each_mut().map(|entity| entity.entity());
	let along = [red, red_too, blue].map(|entity| entity.as_ptr().cast());
	let mut mock = MockEngineTrace::new(&along);
	let engine = mock.engine_trace();

	assert_eq!(
		stopped_at(engine, TraceFilter::SkipTeam(2)).0,
		Some(blue.as_ptr())
	);
	assert_eq!(
		stopped_at(engine, TraceFilter::SkipTeam(3)).0,
		Some(red.as_ptr())
	);
	assert_eq!(
		stopped_at(engine, TraceFilter::SkipTeam(0)).0,
		Some(red.as_ptr())
	);
}