common: Rework volume API

See the doc comments in `common/src/vol.rs` for more information on
the API itself.

The changes include:

* Consistent `Err`/`Error` naming.
  * Types are named `...Error`.
  * `enum` variants are named `...Err`.
* Rename `VolMap{2d, 3d}` -> `VolGrid{2d, 3d}`. This is in preparation
  to an upcoming change where a “map” in the game related sense will
  be added.
* Add volume iterators. There are two types of them:
  * _Position_ iterators obtained from the trait `IntoPosIterator`
    using the method
    `fn pos_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
    which returns an iterator over `Vec3<i32>`.
  * _Volume_ iterators obtained from the trait `IntoVolIterator`
    using the method
    `fn vol_iter(self, lower_bound: Vec3<i32>, upper_bound: Vec3<i32>) -> ...`
    which returns an iterator over `(Vec3<i32>, &Self::Vox)`.
  Those traits will usually be implemented by references to volume
  types (i.e. `impl IntoVolIterator<'a> for &'a T` where `T` is some
  type which usually implements several volume traits, such as `Chunk`).
  * _Position_ iterators iterate over the positions valid for that
    volume.
  * _Volume_ iterators do the same but return not only the position
    but also the voxel at that position, in each iteration.
* Introduce trait `RectSizedVol` for the use case which we have with
  `Chonk`: A `Chonk` is sized only in x and y direction.
* Introduce traits `RasterableVol`, `RectRasterableVol`
  * `RasterableVol` represents a volume that is compile-time sized and has
    its lower bound at `(0, 0, 0)`. The name `RasterableVol` was chosen
    because such a volume can be used with `VolGrid3d`.
  * `RectRasterableVol` represents a volume that is compile-time sized at
    least in x and y direction and has its lower bound at `(0, 0, z)`.
    There's no requirement on he lower bound or size in z direction.
    The name `RectRasterableVol` was chosen because such a volume can be
    used with `VolGrid2d`.
This commit is contained in:
haslersn
2019-09-04 00:23:29 +02:00
parent 886d554f52
commit 1796c09ca1
30 changed files with 443 additions and 257 deletions

View File

@ -22,9 +22,8 @@ use common::{
msg::{ClientMsg, ClientState, RequestStateError, ServerError, ServerInfo, ServerMsg},
net::PostOffice,
state::{BlockChange, State, TimeOfDay, Uid},
terrain::{block::Block, TerrainChunk, TerrainChunkSize, TerrainMap},
vol::Vox,
vol::{ReadVol, VolSize},
terrain::{block::Block, TerrainChunk, TerrainChunkSize, TerrainGrid},
vol::{ReadVol, RectVolSize, Vox},
};
use crossbeam::channel;
use hashbrown::HashSet;
@ -261,7 +260,7 @@ impl Server {
let mut block_change = ecs.write_resource::<BlockChange>();
let _ = ecs
.read_resource::<TerrainMap>()
.read_resource::<TerrainGrid>()
.ray(pos, pos + dir * radius)
.until(|_| rand::random::<f32>() < 0.05)
.for_each(|pos| block_change.set(pos, Block::empty()))
@ -479,7 +478,7 @@ impl Server {
fn chunk_in_vd(
player_pos: Vec3<f32>,
chunk_pos: Vec2<i32>,
terrain: &TerrainMap,
terrain: &TerrainGrid,
vd: u32,
) -> bool {
let player_chunk_pos = terrain.pos_key(player_pos.map(|e| e as i32));
@ -1085,8 +1084,8 @@ impl Server {
) {
{
// Check if the entity is in the client's range
(pos.0 - client_pos.0)
.map2(TerrainChunkSize::SIZE, |d, sz| {
Vec2::from(pos.0 - client_pos.0)
.map2(TerrainChunkSize::RECT_SIZE, |d: f32, sz| {
(d.abs() as u32 / sz).checked_sub(2).unwrap_or(0)
})
.magnitude_squared()