From 92d773bd7225817d003259954e1874f63ef19cc8 Mon Sep 17 00:00:00 2001 From: Thinkofname Date: Sun, 3 Apr 2016 18:26:52 +0100 Subject: [PATCH] Move blocks into its own crate to speed up compile times --- Cargo.lock | 10 ++ Cargo.toml | 4 + blocks/Cargo.lock | 60 ++++++++ blocks/Cargo.toml | 9 ++ src/world/block/mod.rs => blocks/src/lib.rs | 154 ++++++++++++++++++-- {src/world/block => blocks/src}/material.rs | 0 src/main.rs | 1 + src/world/mod.rs | 8 +- 8 files changed, 233 insertions(+), 13 deletions(-) create mode 100644 blocks/Cargo.lock create mode 100644 blocks/Cargo.toml rename src/world/block/mod.rs => blocks/src/lib.rs (97%) rename {src/world/block => blocks/src}/material.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 6c8af2a..8d22800 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,6 +15,7 @@ dependencies = [ "sdl2 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "steven_blocks 0.0.1", "steven_gl 0.0.1", "steven_openssl 0.0.1", "steven_resources 0.1.0", @@ -475,6 +476,15 @@ dependencies = [ "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "steven_blocks" +version = "0.0.1" +dependencies = [ + "cgmath 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "collision 0.5.1 (git+https://github.com/csherratt/collision-rs?rev=f80825e)", + "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "steven_gl" version = "0.0.1" diff --git a/Cargo.toml b/Cargo.toml index d452c20..3d013f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,7 @@ version = "0" [dependencies.steven_resources] path = "./resources" version = "0" + +[dependencies.steven_blocks] +path = "./blocks" +version = "0" diff --git a/blocks/Cargo.lock b/blocks/Cargo.lock new file mode 100644 index 0000000..88642f7 --- /dev/null +++ b/blocks/Cargo.lock @@ -0,0 +1,60 @@ +[root] +name = "steven_blocks" +version = "0.0.1" +dependencies = [ + "cgmath 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "collision 0.5.1 (git+https://github.com/csherratt/collision-rs?rev=f80825e)", + "lazy_static 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cgmath" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "collision" +version = "0.5.1" +source = "git+https://github.com/csherratt/collision-rs?rev=f80825e#f80825eca687ff1053ff492e54fa782944c9cf6b" +dependencies = [ + "cgmath 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "num" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" + diff --git a/blocks/Cargo.toml b/blocks/Cargo.toml new file mode 100644 index 0000000..88dbe36 --- /dev/null +++ b/blocks/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "steven_blocks" +version = "0.0.1" +authors = [ "Thinkofdeath " ] + +[dependencies] +lazy_static = "0.1.15" +cgmath = "0.7.0" +collision = {git = "https://github.com/csherratt/collision-rs", rev = "f80825e"} diff --git a/src/world/block/mod.rs b/blocks/src/lib.rs similarity index 97% rename from src/world/block/mod.rs rename to blocks/src/lib.rs index c2db836..faf5113 100644 --- a/src/world/block/mod.rs +++ b/blocks/src/lib.rs @@ -24,15 +24,50 @@ // Fire (Update State) // CobblestoneWall (Connections) +#![recursion_limit="300"] + +extern crate cgmath; +extern crate collision; +#[macro_use] +extern crate lazy_static; + use collision::{Aabb, Aabb3}; use cgmath::Point3; -use types::Direction; pub mod material; pub use self::material::Material; pub use self::Block::*; +pub trait WorldAccess { + fn get_block(&self, x: i32, y: i32, z: i32) -> Block; +} + +#[doc(hidden)] +#[macro_export] +macro_rules! create_ids { + ($t:ty, ) => (); + ($t:ty, prev($prev:ident), $name:ident) => ( + #[allow(non_upper_case_globals)] + pub const $name: $t = $prev + 1; + ); + ($t:ty, prev($prev:ident), $name:ident, $($n:ident),+) => ( + #[allow(non_upper_case_globals)] + pub const $name: $t = $prev + 1; + create_ids!($t, prev($name), $($n),+); + ); + ($t:ty, $name:ident, $($n:ident),+) => ( + #[allow(non_upper_case_globals)] + pub const $name: $t = 0; + create_ids!($t, prev($name), $($n),+); + ); + ($t:ty, $name:ident) => ( + #[allow(non_upper_case_globals)] + pub const $name: $t = 0; + ); +} + + macro_rules! consume_token { ($i:tt) => (0) } macro_rules! offsets { @@ -240,7 +275,7 @@ macro_rules! define_blocks { } #[allow(unused_variables, unreachable_code)] - pub fn update_state(&self, world: &super::World, x: i32, y: i32, z: i32) -> Block { + pub fn update_state(&self, world: &W, x: i32, y: i32, z: i32) -> Block { match *self { $( Block::$name { @@ -3940,8 +3975,9 @@ define_blocks! { } } -fn can_connect bool>(world: &super::World, x: i32, y: i32, z: i32, dir: Direction, f: &F) -> bool { - let block = world.get_block_offset(x, y, z, dir); +fn can_connect bool, W: WorldAccess>(world: &W, x: i32, y: i32, z: i32, dir: Direction, f: &F) -> bool { + let (ox, oy, oz) = dir.get_offset(); + let block = world.get_block(x + ox, y + oy, z + oz); f(block) || (block.get_material().renderable && block.get_material().should_cull_against) } @@ -3973,11 +4009,12 @@ fn can_connect_glasspane(block: Block) -> bool { } } -fn can_connect_redstone(world: &super::World, x: i32, y: i32, z: i32, dir: Direction) -> RedstoneSide { - let block = world.get_block_offset(x, y, z, dir); +fn can_connect_redstone(world: &W, x: i32, y: i32, z: i32, dir: Direction) -> RedstoneSide { + let (ox, oy, oz) = dir.get_offset(); + let block = world.get_block(x + ox, y + oy, z + oz); if block.get_material().should_cull_against { - let side_up = world.get_block_offset(x, y + 1, z, dir); + let side_up = world.get_block(x + ox, y + oy + 1, z + oz); let up = world.get_block(x, y + 1, z); if match side_up { Block::RedstoneWire{..} => true, _ => false,} && !up.get_material().should_cull_against { @@ -3987,7 +4024,7 @@ fn can_connect_redstone(world: &super::World, x: i32, y: i32, z: i32, dir: Direc return RedstoneSide::None; } - let side_down = world.get_block_offset(x, y - 1, z, dir); + let side_down = world.get_block(x + ox, y + oy - 1, z + oz); if match block { Block::RedstoneWire{..} => true, _ => false,} || match side_down { Block::RedstoneWire{..} => true, _ => false,} { return RedstoneSide::Side; } @@ -4039,7 +4076,7 @@ fn door_data(facing: Direction, half: DoorHalf, hinge: Side, open: bool, powered } } -fn update_door_state(world: &super::World, x: i32, y: i32, z: i32, ohalf: DoorHalf, ofacing: Direction, ohinge: Side, oopen: bool, opowered: bool) -> (Direction, Side, bool, bool) { +fn update_door_state(world: &W, x: i32, y: i32, z: i32, ohalf: DoorHalf, ofacing: Direction, ohinge: Side, oopen: bool, opowered: bool) -> (Direction, Side, bool, bool) { let oy = if ohalf == DoorHalf::Upper { -1 } else { @@ -4068,7 +4105,7 @@ fn update_door_state(world: &super::World, x: i32, y: i32, z: i32, ohalf: DoorHa (ofacing, ohinge, oopen, opowered) } -fn update_double_plant_state(world: &super::World, x: i32, y: i32, z: i32, ohalf: BlockHalf, ovariant: DoublePlantVariant) -> (BlockHalf, DoublePlantVariant) { +fn update_double_plant_state(world: &W, x: i32, y: i32, z: i32, ohalf: BlockHalf, ovariant: DoublePlantVariant) -> (BlockHalf, DoublePlantVariant) { if ohalf != BlockHalf::Upper { return (ohalf, ovariant); } @@ -4896,7 +4933,7 @@ impl StairShape { } } -fn get_stair_info(world: &super::World, x: i32, y: i32, z: i32) -> Option<(Direction, BlockHalf)> { +fn get_stair_info(world: &W, x: i32, y: i32, z: i32) -> Option<(Direction, BlockHalf)> { use self::Block::*; match world.get_block(x, y, z) { OakStairs{facing, half, ..} | @@ -4917,7 +4954,7 @@ fn get_stair_info(world: &super::World, x: i32, y: i32, z: i32) -> Option<(Direc } } -fn update_stair_shape(world: &super::World, x: i32, y: i32, z: i32, facing: Direction) -> StairShape { +fn update_stair_shape(world: &W, x: i32, y: i32, z: i32, facing: Direction) -> StairShape { let (ox, oy, oz) = facing.get_offset(); if let Some((other_facing, _)) = get_stair_info(world, x+ox, y+oy, z+oz) { if other_facing != facing && other_facing != facing.opposite() { @@ -5108,3 +5145,96 @@ impl FlowerPotVariant { } } } + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Direction { + Invalid, + Up, + Down, + North, + South, + West, + East, +} + +impl Direction { + pub fn all() -> Vec { + vec![ + Direction::Up, Direction::Down, + Direction::North, Direction::South, + Direction::West, Direction::East, + ] + } + + pub fn from_string(val: &str) -> Direction { + match val { + "up" => Direction::Up, + "down" => Direction::Down, + "north" => Direction::North, + "south" => Direction::South, + "west" => Direction::West, + "east" => Direction::East, + _ => Direction::Invalid, + } + } + + pub fn opposite(&self) -> Direction { + match *self { + Direction::Up => Direction::Down, + Direction::Down => Direction::Up, + Direction::North => Direction::South, + Direction::South => Direction::North, + Direction::West => Direction::East, + Direction::East => Direction::West, + _ => unreachable!(), + } + } + + pub fn clockwise(&self) -> Direction { + match *self { + Direction::Up => Direction::Up, + Direction::Down => Direction::Down, + Direction::East => Direction::South, + Direction::West => Direction::North, + Direction::South => Direction::West, + Direction::North => Direction::East, + _ => unreachable!(), + } + } + + pub fn counter_clockwise(&self) -> Direction { + match *self { + Direction::Up => Direction::Up, + Direction::Down => Direction::Down, + Direction::East => Direction::North, + Direction::West => Direction::South, + Direction::South => Direction::East, + Direction::North => Direction::West, + _ => unreachable!(), + } + } + + pub fn get_offset(&self) -> (i32, i32, i32) { + match *self { + Direction::Up => (0, 1, 0), + Direction::Down => (0, -1, 0), + Direction::North => (0, 0, -1), + Direction::South => (0, 0, 1), + Direction::West => (-1, 0, 0), + Direction::East => (1, 0, 0), + _ => unreachable!(), + } + } + + pub fn as_string(&self) -> &'static str { + match *self { + Direction::Up => "up", + Direction::Down => "down", + Direction::North => "north", + Direction::South => "south", + Direction::West => "west", + Direction::East => "east", + Direction::Invalid => "invalid", + } + } +} diff --git a/src/world/block/material.rs b/blocks/src/material.rs similarity index 100% rename from src/world/block/material.rs rename to blocks/src/material.rs diff --git a/src/main.rs b/src/main.rs index 2d15a73..aa4ff55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ extern crate log; #[macro_use] extern crate lazy_static; extern crate collision; +pub extern crate steven_blocks; #[macro_use] pub mod macros; diff --git a/src/world/mod.rs b/src/world/mod.rs index 437a5b4..c4edd68 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -pub mod block; +pub use steven_blocks as block; use std::collections::HashMap; use std::collections::VecDeque; @@ -590,6 +590,12 @@ impl World { } } +impl block::WorldAccess for World { + fn get_block(&self, x: i32, y: i32, z: i32) -> block::Block { + World::get_block(self, x, y, z) + } +} + pub struct Snapshot { blocks: Vec, block_light: nibble::Array,