Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bfops committed Feb 12, 2017
1 parent e3a39d0 commit ba3002b
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 66 deletions.
9 changes: 4 additions & 5 deletions client/lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use rand;
use rand::{Rng, SeedableRng};
use std::sync::Mutex;

use common::entity;
use common::id_allocator;
use common::protocol;
use common::surroundings_loader;
Expand All @@ -23,17 +22,17 @@ pub struct T {
#[allow(missing_docs)]
pub id : protocol::ClientId,
/// id for the player in vram
pub player_id : entity::id::Player,
pub player_id : view::entity::id::Player,
/// position of the player in world coordinates
pub player_position : Mutex<Point3<f32>>,
/// the location where we last played a footstep sound
pub last_footstep : Mutex<Point3<f32>>,
/// world position to center terrain loading around
pub load_position : Mutex<Option<Point3<f32>>>,
#[allow(missing_docs)]
pub terrain_allocator : Mutex<id_allocator::T<entity::id::Terrain>>,
pub terrain_allocator : Mutex<id_allocator::T<view::entity::id::Terrain>>,
#[allow(missing_docs)]
pub grass_allocator : Mutex<id_allocator::T<entity::id::Grass>>,
pub grass_allocator : Mutex<id_allocator::T<view::entity::id::Grass>>,
#[allow(missing_docs)]
pub surroundings_loader : Mutex<surroundings_loader::T>,
#[allow(missing_docs)]
Expand Down Expand Up @@ -92,7 +91,7 @@ fn load_distance(mut polygon_budget: i32) -> u32 {
}

#[allow(missing_docs)]
pub fn new(client_id: protocol::ClientId, player_id: entity::id::Player, position: Point3<f32>) -> T {
pub fn new(client_id: protocol::ClientId, player_id: view::entity::id::Player, position: Point3<f32>) -> T {
let mut rng: rand::XorShiftRng = rand::SeedableRng::from_seed([1, 2, 3, 4]);
let s1 = rng.next_u32();
let s2 = rng.next_u32();
Expand Down
62 changes: 35 additions & 27 deletions client/lib/src/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ impl T {
/// Iterate through some enqueued voxel loads and load any updated chunks.
pub fn tick<Rng, UpdateView>(
&mut self,
id_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Terrain>>,
rng : &mut Rng,
chunk_stats : &mut chunk_stats::T,
update_view : &mut UpdateView,
player_position : &cgmath::Point3<f32>,
terrain_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Terrain>>,
grass_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Grass>>,
rng : &mut Rng,
chunk_stats : &mut chunk_stats::T,
update_view : &mut UpdateView,
player_position : &cgmath::Point3<f32>,
) where
UpdateView : FnMut(view::update::T),
Rng : rand::Rng,
Expand All @@ -104,7 +105,8 @@ impl T {
match msg {
Load::Voxels { voxels, time_requested } => {
self.load_voxels(
id_allocator,
terrain_allocator,
grass_allocator,
rng,
chunk_stats,
update_view,
Expand All @@ -124,18 +126,20 @@ impl T {
#[inline(never)]
fn force_load_chunk<Rng, UpdateView>(
&mut self,
id_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Terrain>>,
rng : &mut Rng,
chunk_stats : &mut chunk_stats::T,
update_view : &mut UpdateView,
chunk_position : &chunk::position::T,
lod : lod::T,
terrain_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Terrain>>,
grass_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Grass>>,
rng : &mut Rng,
chunk_stats : &mut chunk_stats::T,
update_view : &mut UpdateView,
chunk_position : &chunk::position::T,
lod : lod::T,
) where
UpdateView : FnMut(view::update::T),
Rng : rand::Rng,
{
debug!("generate {:?} at {:?}", chunk_position, lod);
let mesh_chunk: terrain_mesh::T = terrain_mesh::generate(&self.voxels, chunk_stats, &chunk_position, lod, id_allocator, rng);
let mesh_chunk: terrain_mesh::T =
terrain_mesh::generate(&self.voxels, chunk_stats, &chunk_position, lod, terrain_allocator, grass_allocator, rng);

let mut updates = Vec::new();

Expand Down Expand Up @@ -168,12 +172,13 @@ impl T {
/// if some voxels are missing, returns an Err of all the voxels that need to be fetched from the server.
pub fn load_chunk<Rng, UpdateView>(
&mut self,
id_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Terrain>>,
rng : &mut Rng,
chunk_stats : &mut chunk_stats::T,
update_view : &mut UpdateView,
chunk_position : &chunk::position::T,
lod : lod::T,
terrain_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Terrain>>,
grass_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Grass>>,
rng : &mut Rng,
chunk_stats : &mut chunk_stats::T,
update_view : &mut UpdateView,
chunk_position : &chunk::position::T,
lod : lod::T,
) -> Result<(), Vec<voxel::bounds::T>> where
UpdateView : FnMut(view::update::T),
Rng : rand::Rng,
Expand All @@ -185,7 +190,8 @@ impl T {
);
if all_voxels_loaded {
self.force_load_chunk(
id_allocator,
terrain_allocator,
grass_allocator,
rng,
chunk_stats,
update_view,
Expand Down Expand Up @@ -301,12 +307,13 @@ impl T {
#[inline(never)]
fn load_voxels<Rng, UpdateView>(
&mut self,
id_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Terrain>>,
rng : &mut Rng,
chunk_stats : &mut chunk_stats::T,
update_view : &mut UpdateView,
player_position : &cgmath::Point3<f32>,
voxel_updates : Vec<(voxel::bounds::T, voxel::T)>,
terrain_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Terrain>>,
grass_allocator : &std::sync::Mutex<id_allocator::T<view::entity::id::Grass>>,
rng : &mut Rng,
chunk_stats : &mut chunk_stats::T,
update_view : &mut UpdateView,
player_position : &cgmath::Point3<f32>,
voxel_updates : Vec<(voxel::bounds::T, voxel::T)>,
time_requested : Option<u64>,
) where
UpdateView : FnMut(view::update::T),
Expand All @@ -328,7 +335,8 @@ impl T {
for (chunk, lod) in update_chunks {
let _ =
self.load_chunk(
id_allocator,
terrain_allocator,
grass_allocator,
rng,
chunk_stats,
update_view,
Expand Down
2 changes: 1 addition & 1 deletion client/lib/src/terrain_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ pub fn generate<Rng: rand::Rng>(

if polygon.material == voxel::Material::Terrain && lod <= lod::MAX_GRASS_LOD {
grass.tex_ids.push(rng.gen_range(0, 9));
grass.ids.push(id_allocator::allocate(grass_allocator));
grass.ids.push(grass_allocator.lock().unwrap().allocate());
grass.polygon_offsets.push(polygon_offset);
}
}
Expand Down
3 changes: 2 additions & 1 deletion client/lib/src/update_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ fn load_or_request_chunk<UpdateServer, UpdateView>(
let rng = &mut *client.rng.lock().unwrap();
let r =
terrain.load_chunk(
&client.id_allocator,
&client.terrain_allocator,
&client.grass_allocator,
&mut *rng,
chunk_stats,
update_view,
Expand Down
8 changes: 1 addition & 7 deletions client/lib/src/view/entity.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
use common;
//! Entity types from common, extended with client-specific entity types.
mod types {
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct Terrain;

#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct Mob;

#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct Grass;
Expand All @@ -21,7 +17,5 @@ pub mod id {
#[allow(missing_docs)]
pub type Terrain = T<super::types::Terrain>;
#[allow(missing_docs)]
pub type Mob = T<super::types::Mob>;
#[allow(missing_docs)]
pub type Grass = T<super::types::Grass>;
}
38 changes: 14 additions & 24 deletions common/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
/// Phantom types to use with `id`.
mod types {
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct Player;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
pub struct Mob;
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -32,31 +34,19 @@ pub mod id {
}
}

pub type Player = T<super::types::Player>;

pub mod allocator {
use std;

/// Data structure to produce unique IDs.
pub struct T<U> {
units : std::marker::PhantomData<U>,
next : u32,
}

pub fn new<U>() -> T<U> {
T {
units : std::marker::PhantomData,
next : 0,
}
impl<U> std::default::Default for T<U> {
fn default() -> Self {
of_u32(0)
}
}

impl<U> T<U> {
/// Produce an Id that hasn't been produced yet by this object.
pub fn allocate(&mut self) -> super::T<U> {
let ret = super::of_u32(self.next);
self.next = self.next + 1;
ret
}
impl<U> std::ops::Add<u32> for T<U> {
type Output = T<U>;
fn add(self, rhs: u32) -> T<U> {
of_u32(self.value + rhs)
}
}

pub type Player = T<super::types::Player>;
pub type Mob = T<super::types::Mob>;
}
29 changes: 29 additions & 0 deletions common/id_allocator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Data structure to produce unique IDs.
use std::default::Default;
use std::ops::Add;

/// Data structure to produce unique IDs.
pub struct T<Id> {
next: Id,
}

impl<Id> T<Id> where
Id : Clone + Add<u32, Output=Id>,
{
/// Produce an Id that hasn't been produced yet by this object.
pub fn allocate(&mut self) -> Id {
let ret = self.next.clone();
self.next = self.next.clone() + 1;
ret
}
}

#[allow(missing_docs)]
pub fn new<Id>() -> T<Id> where
Id : Default
{
T {
next: Default::default()
}
}
1 change: 1 addition & 0 deletions common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod cube_shell;
pub mod entity;
pub mod fnv_map;
pub mod fnv_set;
pub mod id_allocator;
pub mod interval_timer;
pub mod protocol;
pub mod range_abs;
Expand Down
2 changes: 1 addition & 1 deletion common/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub enum ServerToClient {
/// Update a player's position.
UpdatePlayer(entity::id::Player, Aabb3<f32>),
/// Update the client's view of a mob with a given mesh.
UpdateMob(entity::id::T<()>, Aabb3<f32>),
UpdateMob(entity::id::Mob, Aabb3<f32>),
/// The sun as a [0, 1) portion of its cycle.
UpdateSun(f32),

Expand Down

0 comments on commit ba3002b

Please sign in to comment.