veloren/common/src/terrain/block.rs

461 lines
14 KiB
Rust
Raw Normal View History

use crate::vol::Vox;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
2019-01-23 20:01:58 +00:00
use vek::*;
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
#[repr(u8)]
pub enum BlockKind {
Air,
Normal,
Dense,
Rock,
Grass,
Leaves,
Water,
2019-08-21 17:22:05 +00:00
LargeCactus,
BarrelCactus,
2019-09-01 19:04:03 +00:00
RoundCactus,
ShortCactus,
MedFlatCactus,
ShortFlatCactus,
2019-08-21 17:22:05 +00:00
BlueFlower,
PinkFlower,
PurpleFlower,
RedFlower,
WhiteFlower,
YellowFlower,
Sunflower,
LongGrass,
2019-08-21 17:22:05 +00:00
MediumGrass,
ShortGrass,
Apple,
2019-09-01 19:04:03 +00:00
Mushroom,
Liana,
2019-09-25 20:22:39 +00:00
Velorite,
VeloriteFrag,
2019-10-09 19:28:05 +00:00
Chest,
2020-01-25 11:14:02 +00:00
Pumpkin,
Welwitch,
LingonBerry,
LeafyPlant,
Fern,
DeadBush,
Blueberry,
2020-04-10 19:08:21 +00:00
Ember,
2020-04-13 23:13:03 +00:00
Corn,
WheatYellow,
WheatGreen,
Cabbage,
2020-04-17 23:29:01 +00:00
Flax,
Carrot,
Tomato,
Radish,
2020-04-13 23:13:03 +00:00
Coconut,
2020-04-19 15:48:12 +00:00
Turnip,
Window1,
Window2,
Window3,
Window4,
2020-04-19 17:59:59 +00:00
Scarecrow,
2020-04-20 19:52:46 +00:00
StreetLamp,
2020-06-27 21:08:21 +00:00
StreetLampTall,
2020-04-20 19:52:46 +00:00
Door,
2020-06-15 16:39:21 +00:00
Bed,
Bench,
2020-06-27 21:08:21 +00:00
ChairSingle,
ChairDouble,
2020-06-15 16:39:21 +00:00
CoatRack,
Crate,
DrawerLarge,
2020-06-27 21:08:21 +00:00
DrawerMedium,
2020-06-15 16:39:21 +00:00
DrawerSmall,
DungeonWallDecor,
HangingBasket,
HangingSign,
WallLamp,
Planter,
Shelf,
2020-06-27 21:08:21 +00:00
TableSide,
TableDining,
TableDouble,
WardrobeSingle,
WardrobeDouble,
2020-06-25 21:12:31 +00:00
LargeGrass,
2020-06-27 21:08:21 +00:00
Pot,
}
impl BlockKind {
pub const MAX_HEIGHT: f32 = 3.0;
2019-09-26 13:03:41 +00:00
pub fn is_tangible(&self) -> bool {
2019-09-25 14:56:57 +00:00
match self {
BlockKind::Air => false,
kind => !kind.is_fluid(),
}
}
pub fn is_air(&self) -> bool {
match self {
BlockKind::Air => true,
2019-10-04 18:27:12 +00:00
BlockKind::LargeCactus => true,
2019-08-21 17:22:05 +00:00
BlockKind::BarrelCactus => true,
2019-09-01 19:04:03 +00:00
BlockKind::RoundCactus => true,
BlockKind::ShortCactus => true,
BlockKind::MedFlatCactus => true,
BlockKind::ShortFlatCactus => true,
2019-08-21 17:22:05 +00:00
BlockKind::BlueFlower => true,
BlockKind::PinkFlower => true,
BlockKind::PurpleFlower => true,
BlockKind::RedFlower => true,
BlockKind::WhiteFlower => true,
BlockKind::YellowFlower => true,
BlockKind::Sunflower => true,
BlockKind::LongGrass => true,
2019-08-21 17:22:05 +00:00
BlockKind::MediumGrass => true,
BlockKind::ShortGrass => true,
BlockKind::Apple => true,
2019-09-01 19:04:03 +00:00
BlockKind::Mushroom => true,
BlockKind::Liana => true,
2019-09-25 20:22:39 +00:00
BlockKind::Velorite => true,
BlockKind::VeloriteFrag => true,
2019-10-09 19:28:05 +00:00
BlockKind::Chest => true,
2020-01-25 11:14:02 +00:00
BlockKind::Welwitch => true,
BlockKind::LingonBerry => true,
BlockKind::LeafyPlant => true,
BlockKind::Fern => true,
BlockKind::DeadBush => true,
BlockKind::Blueberry => true,
2020-04-10 19:08:21 +00:00
BlockKind::Ember => true,
2020-04-13 23:13:03 +00:00
BlockKind::Corn => true,
BlockKind::WheatYellow => true,
BlockKind::WheatGreen => true,
2020-04-17 23:29:01 +00:00
BlockKind::Cabbage => false,
BlockKind::Pumpkin => false,
BlockKind::Flax => true,
BlockKind::Carrot => true,
BlockKind::Tomato => false,
BlockKind::Radish => true,
2020-04-18 18:28:19 +00:00
BlockKind::Turnip => true,
2020-04-13 23:13:03 +00:00
BlockKind::Coconut => true,
BlockKind::Window1 => true,
BlockKind::Window2 => true,
BlockKind::Window3 => true,
BlockKind::Window4 => true,
2020-04-19 17:59:59 +00:00
BlockKind::Scarecrow => true,
2020-04-20 19:52:46 +00:00
BlockKind::StreetLamp => true,
2020-06-27 21:08:21 +00:00
BlockKind::StreetLampTall => true,
2020-04-20 19:52:46 +00:00
BlockKind::Door => false,
2020-06-15 16:39:21 +00:00
BlockKind::Bed => false,
BlockKind::Bench => false,
2020-06-27 21:08:21 +00:00
BlockKind::ChairSingle => false,
BlockKind::ChairDouble => false,
2020-06-15 16:39:21 +00:00
BlockKind::CoatRack => false,
BlockKind::Crate => false,
BlockKind::DrawerLarge => false,
2020-06-27 21:08:21 +00:00
BlockKind::DrawerMedium => false,
2020-06-15 16:39:21 +00:00
BlockKind::DrawerSmall => false,
BlockKind::DungeonWallDecor => false,
BlockKind::HangingBasket => true,
BlockKind::HangingSign => true,
BlockKind::WallLamp => true,
BlockKind::Planter => false,
BlockKind::Shelf => true,
2020-06-27 21:08:21 +00:00
BlockKind::TableSide => false,
BlockKind::TableDining => false,
BlockKind::TableDouble => false,
BlockKind::WardrobeSingle => false,
BlockKind::WardrobeDouble => false,
BlockKind::Pot => false,
_ => false,
}
}
pub fn is_fluid(&self) -> bool {
match self {
2020-04-11 00:13:48 +00:00
BlockKind::Water => true,
_ => false,
}
}
pub fn is_opaque(&self) -> bool {
match self {
BlockKind::Air => false,
BlockKind::Water => false,
2019-08-21 17:22:05 +00:00
BlockKind::LargeCactus => false,
BlockKind::BarrelCactus => false,
2019-09-01 19:04:03 +00:00
BlockKind::RoundCactus => false,
BlockKind::ShortCactus => false,
BlockKind::MedFlatCactus => false,
BlockKind::ShortFlatCactus => false,
2019-08-21 17:22:05 +00:00
BlockKind::BlueFlower => false,
BlockKind::PinkFlower => false,
BlockKind::PurpleFlower => false,
BlockKind::RedFlower => false,
BlockKind::WhiteFlower => false,
BlockKind::YellowFlower => false,
BlockKind::Sunflower => false,
BlockKind::LongGrass => false,
2019-08-21 17:22:05 +00:00
BlockKind::MediumGrass => false,
BlockKind::ShortGrass => false,
BlockKind::Apple => false,
2019-09-01 19:04:03 +00:00
BlockKind::Mushroom => false,
BlockKind::Liana => false,
2019-09-25 20:22:39 +00:00
BlockKind::Velorite => false,
BlockKind::VeloriteFrag => false,
2019-10-09 19:28:05 +00:00
BlockKind::Chest => false,
2020-04-14 00:50:58 +00:00
BlockKind::Pumpkin => false,
2020-01-25 11:14:02 +00:00
BlockKind::Welwitch => false,
BlockKind::LingonBerry => false,
BlockKind::LeafyPlant => false,
BlockKind::Fern => false,
BlockKind::DeadBush => false,
2020-04-11 00:13:48 +00:00
BlockKind::Blueberry => false,
BlockKind::Ember => false,
2020-04-13 23:13:03 +00:00
BlockKind::Corn => false,
BlockKind::WheatYellow => false,
BlockKind::WheatGreen => false,
BlockKind::Cabbage => false,
2020-04-17 23:29:01 +00:00
BlockKind::Flax => false,
BlockKind::Carrot => false,
BlockKind::Tomato => false,
BlockKind::Radish => false,
2020-04-18 18:28:19 +00:00
BlockKind::Turnip => false,
2020-04-13 23:13:03 +00:00
BlockKind::Coconut => false,
2020-04-18 18:28:19 +00:00
BlockKind::Window1 => false,
BlockKind::Window2 => false,
BlockKind::Window3 => false,
BlockKind::Window4 => false,
2020-04-19 17:59:59 +00:00
BlockKind::Scarecrow => false,
2020-04-20 19:52:46 +00:00
BlockKind::StreetLamp => false,
2020-06-27 21:08:21 +00:00
BlockKind::StreetLampTall => false,
2020-04-20 19:52:46 +00:00
BlockKind::Door => false,
2020-06-15 16:39:21 +00:00
BlockKind::Bed => false,
BlockKind::Bench => false,
2020-06-27 21:08:21 +00:00
BlockKind::ChairSingle => false,
BlockKind::ChairDouble => false,
2020-06-15 16:39:21 +00:00
BlockKind::CoatRack => false,
BlockKind::Crate => false,
BlockKind::DrawerLarge => false,
2020-06-27 21:08:21 +00:00
BlockKind::DrawerMedium => false,
2020-06-15 16:39:21 +00:00
BlockKind::DrawerSmall => false,
BlockKind::DungeonWallDecor => false,
BlockKind::HangingBasket => false,
BlockKind::HangingSign => false,
BlockKind::WallLamp => false,
BlockKind::Planter => false,
BlockKind::Shelf => false,
2020-06-27 21:08:21 +00:00
BlockKind::TableSide => false,
BlockKind::TableDining => false,
BlockKind::TableDouble => false,
BlockKind::WardrobeSingle => false,
BlockKind::WardrobeDouble => false,
2020-06-25 21:12:31 +00:00
BlockKind::LargeGrass => false,
2020-06-27 21:08:21 +00:00
BlockKind::Pot => false,
_ => true,
}
}
pub fn is_solid(&self) -> bool {
match self {
BlockKind::Air => false,
BlockKind::Water => false,
2019-08-21 17:22:05 +00:00
BlockKind::LargeCactus => true,
BlockKind::BarrelCactus => true,
2019-09-01 19:04:03 +00:00
BlockKind::RoundCactus => true,
BlockKind::ShortCactus => true,
BlockKind::MedFlatCactus => true,
BlockKind::ShortFlatCactus => true,
2019-08-21 17:22:05 +00:00
BlockKind::BlueFlower => false,
BlockKind::PinkFlower => false,
BlockKind::PurpleFlower => false,
BlockKind::RedFlower => false,
BlockKind::WhiteFlower => false,
BlockKind::YellowFlower => false,
BlockKind::Sunflower => false,
BlockKind::LongGrass => false,
2019-08-21 17:22:05 +00:00
BlockKind::MediumGrass => false,
BlockKind::ShortGrass => false,
BlockKind::Apple => true,
2019-09-01 19:04:03 +00:00
BlockKind::Mushroom => false,
BlockKind::Liana => false,
2019-10-09 19:28:05 +00:00
BlockKind::Chest => true,
2020-04-20 19:52:46 +00:00
BlockKind::Pumpkin => true,
2020-01-25 11:14:02 +00:00
BlockKind::Welwitch => false,
BlockKind::LingonBerry => false,
BlockKind::LeafyPlant => false,
BlockKind::Fern => false,
BlockKind::DeadBush => false,
BlockKind::Blueberry => false,
2020-04-10 19:08:21 +00:00
BlockKind::Ember => false,
2020-04-13 23:13:03 +00:00
BlockKind::Corn => false,
BlockKind::WheatYellow => false,
BlockKind::WheatGreen => false,
2020-04-20 19:52:46 +00:00
BlockKind::Cabbage => true,
2020-04-17 23:29:01 +00:00
BlockKind::Flax => false,
2020-04-20 19:52:46 +00:00
BlockKind::Carrot => true,
2020-04-17 23:29:01 +00:00
BlockKind::Tomato => true,
2020-04-20 19:52:46 +00:00
BlockKind::Radish => true,
BlockKind::Turnip => true,
BlockKind::Coconut => true,
2020-04-19 17:59:59 +00:00
BlockKind::Scarecrow => true,
2020-04-20 19:52:46 +00:00
BlockKind::StreetLamp => true,
2020-06-27 21:08:21 +00:00
BlockKind::StreetLampTall => true,
2020-04-20 19:52:46 +00:00
BlockKind::Door => false,
2020-06-15 16:39:21 +00:00
BlockKind::Bed => true,
BlockKind::Bench => true,
2020-06-27 21:08:21 +00:00
BlockKind::ChairSingle => true,
BlockKind::ChairDouble => true,
2020-06-15 16:39:21 +00:00
BlockKind::CoatRack => true,
BlockKind::Crate => true,
BlockKind::DrawerLarge => true,
2020-06-27 21:08:21 +00:00
BlockKind::DrawerMedium => true,
2020-06-15 16:39:21 +00:00
BlockKind::DrawerSmall => true,
BlockKind::DungeonWallDecor => true,
BlockKind::HangingBasket => false,
BlockKind::HangingSign => false,
BlockKind::WallLamp => false,
BlockKind::Planter => true,
BlockKind::Shelf => false,
2020-06-27 21:08:21 +00:00
BlockKind::TableSide => true,
BlockKind::TableDining => true,
BlockKind::TableDouble => true,
BlockKind::WardrobeSingle => true,
BlockKind::WardrobeDouble => true,
BlockKind::Pot => true,
_ => true,
}
}
pub fn is_explodable(&self) -> bool {
match self {
BlockKind::Leaves | BlockKind::Grass | BlockKind::Rock => true,
_ => false,
}
}
// TODO: Integrate this into `is_solid` by returning an `Option<f32>`
pub fn get_height(&self) -> f32 {
// Beware: the height *must* be <= `MAX_HEIGHT` or the collision system will not
// properly detect it!
match self {
2020-04-18 20:46:39 +00:00
BlockKind::Tomato => 1.65,
BlockKind::LargeCactus => 2.5,
BlockKind::Scarecrow => 3.0,
2020-04-20 19:52:46 +00:00
BlockKind::Turnip => 0.36,
BlockKind::Pumpkin => 0.81,
BlockKind::Cabbage => 0.45,
BlockKind::Chest => 1.09,
BlockKind::StreetLamp => 3.0,
BlockKind::Carrot => 0.18,
BlockKind::Radish => 0.18,
BlockKind::Door => 3.0,
2020-06-15 16:39:21 +00:00
BlockKind::Bed => 1.54,
BlockKind::Bench => 1.45,
2020-06-27 21:08:21 +00:00
BlockKind::ChairSingle => 1.36,
BlockKind::ChairDouble => 1.36,
2020-06-15 16:39:21 +00:00
BlockKind::CoatRack => 2.36,
BlockKind::Crate => 0.90,
BlockKind::DrawerSmall => 1.0,
2020-06-27 21:08:21 +00:00
BlockKind::DrawerMedium => 2.0,
2020-06-15 16:39:21 +00:00
BlockKind::DrawerLarge => 2.0,
BlockKind::DungeonWallDecor => 1.0,
BlockKind::Planter => 1.09,
2020-06-27 21:08:21 +00:00
BlockKind::TableSide => 1.27,
BlockKind::TableDining => 1.45,
BlockKind::TableDouble => 1.45,
BlockKind::WardrobeSingle => 3.0,
BlockKind::WardrobeDouble => 3.0,
BlockKind::Pot => 0.90,
_ => 1.0,
}
}
pub fn is_collectible(&self) -> bool {
match self {
BlockKind::BlueFlower => false,
BlockKind::PinkFlower => false,
BlockKind::PurpleFlower => false,
BlockKind::RedFlower => false,
BlockKind::WhiteFlower => false,
BlockKind::YellowFlower => false,
BlockKind::Sunflower => false,
BlockKind::LongGrass => false,
BlockKind::MediumGrass => false,
BlockKind::ShortGrass => false,
BlockKind::Apple => true,
BlockKind::Mushroom => true,
BlockKind::Velorite => true,
BlockKind::VeloriteFrag => true,
2019-10-09 19:28:05 +00:00
BlockKind::Chest => true,
2020-04-13 23:13:03 +00:00
BlockKind::Coconut => true,
_ => false,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[repr(packed)]
2019-01-02 19:22:01 +00:00
pub struct Block {
kind: BlockKind,
2019-01-02 19:22:01 +00:00
color: [u8; 3],
}
2019-01-14 18:49:53 +00:00
2019-01-23 20:01:58 +00:00
impl Block {
pub fn new(kind: BlockKind, color: Rgb<u8>) -> Self {
2019-01-23 20:01:58 +00:00
Self {
kind,
color: color.into_array(),
}
}
pub fn get_color(&self) -> Option<Rgb<u8>> {
if !self.is_air() {
2019-01-23 20:01:58 +00:00
Some(self.color.into())
} else {
None
2019-01-23 20:01:58 +00:00
}
}
2019-05-31 20:37:11 +00:00
pub fn get_ori(&self) -> Option<u8> {
match self.kind {
2020-04-23 16:00:48 +00:00
BlockKind::Window1
| BlockKind::Window2
| BlockKind::Window3
| BlockKind::Window4
| BlockKind::Door => Some(self.color[0] & 0b111),
_ => None,
}
}
pub fn kind(&self) -> BlockKind { self.kind }
}
impl Deref for Block {
type Target = BlockKind;
fn deref(&self) -> &Self::Target { &self.kind }
2019-01-23 20:01:58 +00:00
}
2019-01-14 18:49:53 +00:00
impl Vox for Block {
fn empty() -> Self {
Self {
kind: BlockKind::Air,
2019-01-14 18:49:53 +00:00
color: [0; 3],
}
}
fn is_empty(&self) -> bool { self.is_air() }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn block_size() {
assert_eq!(std::mem::size_of::<BlockKind>(), 1);
assert_eq!(std::mem::size_of::<Block>(), 4);
2019-01-14 18:49:53 +00:00
}
}