Move blocks into its own crate to speed up compile times

This commit is contained in:
Thinkofname 2016-04-03 18:26:52 +01:00
parent 051f836fde
commit 92d773bd72
8 changed files with 233 additions and 13 deletions

10
Cargo.lock generated
View File

@ -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"

View File

@ -32,3 +32,7 @@ version = "0"
[dependencies.steven_resources]
path = "./resources"
version = "0"
[dependencies.steven_blocks]
path = "./blocks"
version = "0"

60
blocks/Cargo.lock generated Normal file
View File

@ -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"

9
blocks/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "steven_blocks"
version = "0.0.1"
authors = [ "Thinkofdeath <thinkofdeath@spigotmc.org>" ]
[dependencies]
lazy_static = "0.1.15"
cgmath = "0.7.0"
collision = {git = "https://github.com/csherratt/collision-rs", rev = "f80825e"}

View File

@ -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<W: WorldAccess>(&self, world: &W, x: i32, y: i32, z: i32) -> Block {
match *self {
$(
Block::$name {
@ -3940,8 +3975,9 @@ define_blocks! {
}
}
fn can_connect<F: Fn(Block) -> 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<F: Fn(Block) -> 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<W: WorldAccess>(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<W: WorldAccess>(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<W: WorldAccess>(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<W: WorldAccess>(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<W: WorldAccess>(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<Direction> {
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",
}
}
}

View File

@ -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;

View File

@ -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<u16>,
block_light: nibble::Array,