Replace usages of x,y,z for block positions with Position

This commit is contained in:
Thinkofname 2016-04-03 20:53:40 +01:00
parent df9db09004
commit da40508291
3 changed files with 8 additions and 197 deletions

View File

@ -1,66 +0,0 @@
// Copyright 2016 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
extern crate byteorder;
use std::fmt;
use protocol::Serializable;
use std::io;
use std::io::Write;
use self::byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};
#[derive(Clone, Copy)]
pub struct Position(u64);
impl Position {
#[allow(dead_code)]
pub fn new(x: i32, y: i32, z: i32) -> Position {
Position((((x as u64) & 0x3FFFFFF) << 38) | (((y as u64) & 0xFFF) << 26) |
((z as u64) & 0x3FFFFFF))
}
pub fn get_x(&self) -> i32 {
((self.0 as i64) >> 38) as i32
}
pub fn get_y(&self) -> i32 {
(((self.0 as i64) >> 26) & 0xFFF) as i32
}
pub fn get_z(&self) -> i32 {
((self.0 as i64) << 38 >> 38) as i32
}
}
impl Default for Position {
fn default() -> Position {
Position(0)
}
}
impl fmt::Debug for Position {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "<{},{},{}>", self.get_x(), self.get_y(), self.get_z())
}
}
impl Serializable for Position {
fn read_from(buf: &mut io::Read) -> Result<Position, io::Error> {
Result::Ok(Position(try!(buf.read_u64::<BigEndian>())))
}
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
try!(buf.write_u64::<BigEndian>(self.0));
Result::Ok(())
}
}

View File

@ -21,6 +21,7 @@ use protocol;
use protocol::Serializable;
use format;
use item;
use shared::Position;
pub struct MetadataKey<T: MetaValue> {
index: i32,
@ -81,12 +82,12 @@ impl Serializable for Metadata {
[try!(f32::read_from(buf)),
try!(f32::read_from(buf)),
try!(f32::read_from(buf))]),
8 => m.put_raw(index, try!(super::Position::read_from(buf))),
8 => m.put_raw(index, try!(Position::read_from(buf))),
9 => {
if try!(bool::read_from(buf)) {
m.put_raw(index, try!(Option::<super::Position>::read_from(buf)));
m.put_raw(index, try!(Option::<Position>::read_from(buf)));
} else {
m.put_raw::<Option<super::Position>>(index, None);
m.put_raw::<Option<Position>>(index, None);
}
}
10 => m.put_raw(index, try!(protocol::VarInt::read_from(buf))),
@ -199,8 +200,8 @@ pub enum Value {
OptionalItemStack(Option<item::Stack>),
Bool(bool),
Vector([f32; 3]),
Position(super::Position),
OptionalPosition(Option<super::Position>),
Position(Position),
OptionalPosition(Option<Position>),
Direction(protocol::VarInt), // TODO: Proper type
OptionalUUID(Option<protocol::UUID>),
Block(u16), // TODO: Proper type
@ -307,7 +308,7 @@ impl MetaValue for [f32; 3] {
}
}
impl MetaValue for super::Position {
impl MetaValue for Position {
fn unwrap(value: &Value) -> &Self {
match *value {
Value::Position(ref val) => val,
@ -319,7 +320,7 @@ impl MetaValue for super::Position {
}
}
impl MetaValue for Option<super::Position> {
impl MetaValue for Option<Position> {
fn unwrap(value: &Value) -> &Self {
match *value {
Value::OptionalPosition(ref val) => val,

View File

@ -12,9 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
mod blockpos;
pub use self::blockpos::*;
mod metadata;
pub use self::metadata::*;
@ -22,127 +19,6 @@ pub mod bit;
pub mod nibble;
pub mod hash;
use model::{PRECOMPUTED_VERTS, BlockVertex};
#[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_verts(&self) -> &'static [BlockVertex; 4] {
match *self {
Direction::Up => PRECOMPUTED_VERTS[0],
Direction::Down => PRECOMPUTED_VERTS[1],
Direction::North => PRECOMPUTED_VERTS[2],
Direction::South => PRECOMPUTED_VERTS[3],
Direction::West => PRECOMPUTED_VERTS[4],
Direction::East => PRECOMPUTED_VERTS[5],
_ => 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",
}
}
pub fn index(&self) -> usize {
match *self {
Direction::Up => 0,
Direction::Down => 1,
Direction::North => 2,
Direction::South => 3,
Direction::West => 4,
Direction::East => 5,
_ => unreachable!(),
}
}
}
#[derive(Clone, Copy, Debug)]
pub enum Gamemode {
Survival = 0,