mirror of
https://github.com/italicsjenga/valence.git
synced 2025-01-10 23:01:31 +11:00
eaf1e18610
## Description - `valence` and `valence_protocol` have been divided into smaller crates in order to parallelize the build and improve IDE responsiveness. In the process, code architecture has been made clearer by removing circular dependencies between modules. `valence` is now just a shell around the other crates. - `workspace.packages` and `workspace.dependencies` are now used. This makes dependency managements and crate configuration much easier. - `valence_protocol` is no more. Most things from `valence_protocol` ended up in `valence_core`. We won't advertise `valence_core` as a general-purpose protocol library since it contains too much valence-specific stuff. Closes #308. - Networking code (login, initial TCP connection handling, etc.) has been extracted into the `valence_network` crate. The API has been expanded and improved with better defaults. Player counts and initial connections to the server are now tracked separately. Player counts function by default without any user configuration. - Some crates like `valence_anvil`, `valence_network`, `valence_player_list`, `valence_inventory`, etc. are now optional. They can be enabled/disabled with feature flags and `DefaultPlugins` just like bevy. - Whole-server unit tests have been moved to `valence/src/tests` in order to avoid [cyclic dev-dependencies](https://github.com/rust-lang/cargo/issues/4242). - Tools like `valence_stresser` and `packet_inspector` have been moved to a new `tools` directory. Renamed `valence_stresser` to `stresser`. Closes #241. - Moved all benches to `valence/benches/` to make them easier to run and organize. Ignoring transitive dependencies and `valence_core`, here's what the dependency graph looks like now: ```mermaid graph TD network --> client client --> instance biome --> registry dimension --> registry instance --> biome instance --> dimension instance --> entity player_list --> client inventory --> client anvil --> instance entity --> block ``` ### Issues - Inventory tests inspect many private implementation details of the inventory module, forcing us to mark things as `pub` and `#[doc(hidden)]`. It would be ideal if the tests only looked at observable behavior. - Consider moving packets in `valence_core` elsewhere. `Particle` wants to use `BlockState`, but that's defined in `valence_block`, so we can't use it without causing cycles. - Unsure what exactly should go in `valence::prelude`. - This could use some more tests of course, but I'm holding off on that until I'm confident this is the direction we want to take things. ## TODOs - [x] Update examples. - [x] Update benches. - [x] Update main README. - [x] Add short READMEs to crates. - [x] Test new schedule to ensure behavior is the same. - [x] Update tools. - [x] Copy lints to all crates. - [x] Fix docs, clippy, etc.
194 lines
5.5 KiB
Rust
194 lines
5.5 KiB
Rust
#![doc = include_str!("../README.md")]
|
|
#![deny(
|
|
rustdoc::broken_intra_doc_links,
|
|
rustdoc::private_intra_doc_links,
|
|
rustdoc::missing_crate_level_docs,
|
|
rustdoc::invalid_codeblock_attributes,
|
|
rustdoc::invalid_rust_codeblocks,
|
|
rustdoc::bare_urls,
|
|
rustdoc::invalid_html_tags
|
|
)]
|
|
#![warn(
|
|
trivial_casts,
|
|
trivial_numeric_casts,
|
|
unused_lifetimes,
|
|
unused_import_braces,
|
|
unreachable_pub,
|
|
clippy::dbg_macro
|
|
)]
|
|
|
|
use vek::{Aabb, Vec3};
|
|
|
|
pub mod bvh;
|
|
|
|
pub trait SpatialIndex<N = f64> {
|
|
type Object: Bounded3D<N>;
|
|
|
|
/// Invokes `f` with every object in the spatial index considered
|
|
/// colliding according to `collides` in an arbitrary order.
|
|
///
|
|
/// `collides` takes an AABB and returns whether or not a collision
|
|
/// occurred with the given AABB.
|
|
///
|
|
/// `f` is called with every object considered colliding. If `f` returns
|
|
/// with `Some(x)`, then `query` exits early with `Some(x)`. If `f` never
|
|
/// returns with `Some`, then query returns `None`.
|
|
fn query<C, F, T>(&self, collides: C, f: F) -> Option<T>
|
|
where
|
|
C: FnMut(Aabb<N>) -> bool,
|
|
F: FnMut(&Self::Object) -> Option<T>;
|
|
|
|
/// Casts a ray defined by `origin` and `direction` through object AABBs
|
|
/// and returns the closest intersection for which `f` returns `true`.
|
|
///
|
|
/// `f` is a predicate used to filter intersections. For instance, if a ray
|
|
/// is shot from a player's eye position, you probably don't want the
|
|
/// ray to intersect with the player's own hitbox.
|
|
///
|
|
/// If no intersections are found or if `f` never returns `true` then `None`
|
|
/// is returned. Additionally, the given ray direction must be
|
|
/// normalized.
|
|
fn raycast<F>(
|
|
&self,
|
|
origin: Vec3<f64>,
|
|
direction: Vec3<f64>,
|
|
f: F,
|
|
) -> Option<RaycastHit<Self::Object, N>>
|
|
where
|
|
F: FnMut(RaycastHit<Self::Object, N>) -> bool;
|
|
}
|
|
|
|
pub trait Bounded3D<N = f64> {
|
|
fn aabb(&self) -> Aabb<N>;
|
|
}
|
|
|
|
/// Represents an intersection between a ray and an entity's axis-aligned
|
|
/// bounding box (hitbox).
|
|
#[derive(PartialEq, Eq, Debug)]
|
|
pub struct RaycastHit<'a, O, N = f64> {
|
|
/// The object that was hit by the ray.
|
|
pub object: &'a O,
|
|
/// The distance from the ray origin to the closest intersection point.
|
|
/// If the origin of the ray is inside the bounding box, then this will be
|
|
/// zero.
|
|
pub near: N,
|
|
/// The distance from the ray origin to the second intersection point. This
|
|
/// represents the point at which the ray exits the bounding box.
|
|
pub far: N,
|
|
}
|
|
|
|
impl<O, N: Clone> Clone for RaycastHit<'_, O, N> {
|
|
fn clone(&self) -> Self {
|
|
Self {
|
|
object: self.object,
|
|
near: self.near.clone(),
|
|
far: self.far.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<O, N: Copy> Copy for RaycastHit<'_, O, N> {}
|
|
|
|
impl<N: Clone> Bounded3D<N> for Aabb<N> {
|
|
fn aabb(&self) -> Aabb<N> {
|
|
self.clone()
|
|
}
|
|
}
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Default, Debug)]
|
|
pub struct WithAabb<O, N = f64> {
|
|
pub object: O,
|
|
pub aabb: Aabb<N>,
|
|
}
|
|
|
|
impl<O, N> WithAabb<O, N> {
|
|
pub fn new(object: O, aabb: Aabb<N>) -> Self {
|
|
Self { object, aabb }
|
|
}
|
|
}
|
|
|
|
impl<O, N: Clone> Bounded3D<N> for WithAabb<O, N> {
|
|
fn aabb(&self) -> Aabb<N> {
|
|
self.aabb.clone()
|
|
}
|
|
}
|
|
|
|
/// Calculates the intersection between an axis-aligned bounding box and a ray
|
|
/// defined by its origin `ro` and direction `rd`.
|
|
///
|
|
/// If an intersection occurs, `Some((near, far))` is returned. `near` and `far`
|
|
/// are the distance from the origin to the closest and furthest intersection
|
|
/// points respectively. If the intersection occurs inside the bounding box,
|
|
/// then `near` is zero.
|
|
pub fn ray_box_intersect(ro: Vec3<f64>, rd: Vec3<f64>, bb: Aabb<f64>) -> Option<(f64, f64)> {
|
|
let mut near = -f64::INFINITY;
|
|
let mut far = f64::INFINITY;
|
|
|
|
for i in 0..3 {
|
|
// Rust's definition of min and max properly handle the NaNs that these
|
|
// computations might produce.
|
|
let t0 = (bb.min[i] - ro[i]) / rd[i];
|
|
let t1 = (bb.max[i] - ro[i]) / rd[i];
|
|
|
|
near = near.max(t0.min(t1));
|
|
far = far.min(t0.max(t1));
|
|
}
|
|
|
|
if near <= far && far >= 0.0 {
|
|
Some((near.max(0.0), far))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn ray_box_edge_cases() {
|
|
let bb = Aabb {
|
|
min: Vec3::new(0.0, 0.0, 0.0),
|
|
max: Vec3::new(1.0, 1.0, 1.0),
|
|
};
|
|
|
|
let ros = [
|
|
// On a corner
|
|
Vec3::new(0.0, 0.0, 0.0),
|
|
// Outside
|
|
Vec3::new(-0.5, 0.5, -0.5),
|
|
// In the center
|
|
Vec3::new(0.5, 0.5, 0.5),
|
|
// On an edge
|
|
Vec3::new(0.0, 0.5, 0.0),
|
|
// On a face
|
|
Vec3::new(0.0, 0.5, 0.5),
|
|
// Outside slabs
|
|
Vec3::new(-2.0, -2.0, -2.0),
|
|
];
|
|
|
|
let rds = [
|
|
Vec3::new(1.0, 0.0, 0.0),
|
|
Vec3::new(-1.0, 0.0, 0.0),
|
|
Vec3::new(0.0, 1.0, 0.0),
|
|
Vec3::new(0.0, -1.0, 0.0),
|
|
Vec3::new(0.0, 0.0, 1.0),
|
|
Vec3::new(0.0, 0.0, -1.0),
|
|
];
|
|
|
|
assert!(rds.iter().all(|d| d.is_normalized()));
|
|
|
|
for ro in ros {
|
|
for rd in rds {
|
|
if let Some((near, far)) = ray_box_intersect(ro, rd, bb) {
|
|
assert!(near.is_finite());
|
|
assert!(far.is_finite());
|
|
assert!(near <= far);
|
|
assert!(near >= 0.0);
|
|
assert!(far >= 0.0);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|