2023-02-12 04:51:53 +11:00
|
|
|
use glam::DVec3;
|
|
|
|
|
Reorganize Project (#321)
## 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.
2023-04-22 07:43:59 +10:00
|
|
|
use crate::block_pos::BlockPos;
|
|
|
|
use crate::packet::{Decode, Encode};
|
|
|
|
|
|
|
|
/// The X and Z position of a chunk.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash, Debug, Encode, Decode)]
|
2023-02-12 04:51:53 +11:00
|
|
|
pub struct ChunkPos {
|
|
|
|
/// The X position of the chunk.
|
|
|
|
pub x: i32,
|
|
|
|
/// The Z position of the chunk.
|
|
|
|
pub z: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
const EXTRA_VIEW_RADIUS: i32 = 2;
|
|
|
|
|
|
|
|
impl ChunkPos {
|
|
|
|
/// Constructs a new chunk position.
|
|
|
|
pub const fn new(x: i32, z: i32) -> Self {
|
|
|
|
Self { x, z }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs a chunk position from a position in world space. Only the `x`
|
|
|
|
/// and `z` components are used.
|
|
|
|
pub fn from_dvec3(pos: DVec3) -> Self {
|
|
|
|
Self::at(pos.x, pos.z)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_block_pos(pos: BlockPos) -> Self {
|
|
|
|
Self::new(pos.x.div_euclid(16), pos.z.div_euclid(16))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Takes an X and Z position in world space and returns the chunk position
|
|
|
|
/// containing the point.
|
|
|
|
pub fn at(x: f64, z: f64) -> Self {
|
|
|
|
Self::new((x / 16.0).floor() as i32, (z / 16.0).floor() as i32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn distance_squared(self, other: Self) -> u64 {
|
|
|
|
let diff_x = other.x as i64 - self.x as i64;
|
|
|
|
let diff_z = other.z as i64 - self.z as i64;
|
|
|
|
|
|
|
|
(diff_x * diff_x + diff_z * diff_z) as u64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(i32, i32)> for ChunkPos {
|
|
|
|
fn from((x, z): (i32, i32)) -> Self {
|
|
|
|
Self { x, z }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ChunkPos> for (i32, i32) {
|
|
|
|
fn from(pos: ChunkPos) -> Self {
|
|
|
|
(pos.x, pos.z)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<[i32; 2]> for ChunkPos {
|
|
|
|
fn from([x, z]: [i32; 2]) -> Self {
|
|
|
|
Self { x, z }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ChunkPos> for [i32; 2] {
|
|
|
|
fn from(pos: ChunkPos) -> Self {
|
|
|
|
[pos.x, pos.z]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents the set of all chunk positions that a client can see, defined by
|
|
|
|
/// a center chunk position `pos` and view distance `dist`.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
|
|
|
|
pub struct ChunkView {
|
|
|
|
pub pos: ChunkPos,
|
|
|
|
pub dist: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChunkView {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(pos: impl Into<ChunkPos>, dist: u8) -> Self {
|
|
|
|
Self {
|
|
|
|
pos: pos.into(),
|
|
|
|
dist,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn with_pos(mut self, pos: impl Into<ChunkPos>) -> Self {
|
|
|
|
self.pos = pos.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn with_dist(mut self, dist: u8) -> Self {
|
|
|
|
self.dist = dist;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn contains(self, pos: ChunkPos) -> bool {
|
|
|
|
let true_dist = self.dist as u64 + EXTRA_VIEW_RADIUS as u64;
|
|
|
|
self.pos.distance_squared(pos) <= true_dist * true_dist
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator over all the chunk positions in this view.
|
|
|
|
pub fn iter(self) -> impl Iterator<Item = ChunkPos> {
|
|
|
|
let true_dist = self.dist as i32 + EXTRA_VIEW_RADIUS;
|
|
|
|
|
|
|
|
(self.pos.z - true_dist..=self.pos.z + true_dist)
|
|
|
|
.flat_map(move |z| {
|
|
|
|
(self.pos.x - true_dist..=self.pos.x + true_dist).map(move |x| ChunkPos { x, z })
|
|
|
|
})
|
|
|
|
.filter(move |&p| self.contains(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn diff(self, other: Self) -> impl Iterator<Item = ChunkPos> {
|
|
|
|
self.iter().filter(move |&p| !other.contains(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
// The foreach-based methods are optimizing better than the iterator ones.
|
|
|
|
|
|
|
|
#[inline]
|
Reorganize Project (#321)
## 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.
2023-04-22 07:43:59 +10:00
|
|
|
pub fn for_each(self, mut f: impl FnMut(ChunkPos)) {
|
2023-02-12 04:51:53 +11:00
|
|
|
let true_dist = self.dist as i32 + EXTRA_VIEW_RADIUS;
|
|
|
|
|
|
|
|
for z in self.pos.z - true_dist..=self.pos.z + true_dist {
|
|
|
|
for x in self.pos.x - true_dist..=self.pos.x + true_dist {
|
|
|
|
let p = ChunkPos { x, z };
|
|
|
|
if self.contains(p) {
|
|
|
|
f(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
Reorganize Project (#321)
## 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.
2023-04-22 07:43:59 +10:00
|
|
|
pub fn diff_for_each(self, other: Self, mut f: impl FnMut(ChunkPos)) {
|
2023-02-12 04:51:53 +11:00
|
|
|
self.for_each(|p| {
|
|
|
|
if !other.contains(p) {
|
|
|
|
f(p);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::collections::BTreeSet;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chunk_view_for_each_and_iter() {
|
|
|
|
let pos = ChunkPos::new(42, 24);
|
|
|
|
|
|
|
|
for dist in 2..=32 {
|
|
|
|
let mut positions = vec![];
|
|
|
|
|
|
|
|
let view = ChunkView { pos, dist };
|
|
|
|
|
|
|
|
view.for_each(|pos| {
|
|
|
|
positions.push(pos);
|
|
|
|
assert!(view.contains(pos))
|
|
|
|
});
|
|
|
|
|
|
|
|
for (i, pos) in view.iter().enumerate() {
|
|
|
|
assert_eq!(positions[i], pos);
|
|
|
|
assert!(view.contains(pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chunk_view_contains() {
|
|
|
|
let view = ChunkView::new([0, 0], 16);
|
|
|
|
let positions = BTreeSet::from_iter(view.iter());
|
|
|
|
|
|
|
|
for z in -64..64 {
|
|
|
|
for x in -64..64 {
|
|
|
|
let p = ChunkPos::new(x, z);
|
|
|
|
assert_eq!(view.contains(p), positions.contains(&p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn chunk_pos_round_trip_conv() {
|
|
|
|
let p = ChunkPos::new(rand::random(), rand::random());
|
|
|
|
|
|
|
|
assert_eq!(ChunkPos::from(<(i32, i32)>::from(p)), p);
|
|
|
|
assert_eq!(ChunkPos::from(<[i32; 2]>::from(p)), p);
|
|
|
|
}
|
|
|
|
}
|