1 /** 2 Copyright: Copyright (c) 2016 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 module voxelman.blockentity.utils; 7 8 import std.experimental.logger; 9 import voxelman.container.buffer; 10 import voxelman.math; 11 import voxelman.block.utils; 12 import voxelman.core.config; 13 import voxelman.core.events; 14 import voxelman.world.storage.coordinates; 15 import voxelman.world.storage.worldbox; 16 import voxelman.core.chunkmesh; 17 18 import voxelman.blockentity.blockentityaccess; 19 import voxelman.blockentity.blockentitydata; 20 21 enum BLOCK_ENTITY_FLAG = 1 << 15; 22 enum BLOCK_INDEX_MASK = (1 << 15) - 1; 23 24 bool isBlockEntity(BlockId blockId) 25 { 26 enum n = BlockId.sizeof*8 - 1; 27 return blockId >> n; // highest bit 28 } 29 30 ushort blockIndexFromBlockId(BlockId blockId) { 31 return blockId & BLOCK_INDEX_MASK; 32 } 33 34 BlockId blockIdFromBlockIndex(ushort blockIndex) { 35 return blockIndex | BLOCK_ENTITY_FLAG; 36 } 37 38 39 alias BlockEntityMeshhandler = void function( 40 Buffer!MeshVertex[] output, 41 BlockEntityData data, 42 ubyte[3] color, 43 ubyte sides, 44 //ivec3 worldPos, 45 ivec3 chunkPos, 46 ivec3 entityPos); 47 48 alias SolidityHandler = Solidity function(Side side, ivec3 chunkPos, ivec3 entityPos, BlockEntityData data); 49 alias EntityBoxHandler = WorldBox function(BlockWorldPos bwp, BlockEntityData data); 50 alias EntityDebugHandler = void function(BlockWorldPos bwp, BlockEntityData data); 51 WorldBox nullBoxHandler(BlockWorldPos bwp, BlockEntityData data) 52 { 53 return WorldBox(bwp.xyz, ivec3(1,1,1), cast(ushort)bwp.w); 54 } 55 56 void nullBlockEntityMeshhandler( 57 Buffer!MeshVertex[] output, BlockEntityData data, 58 ubyte[3] color, ubyte sides, //ivec3 worldPos, 59 ivec3 chunkPos, ivec3 entityPos) {} 60 61 Solidity nullSolidityHandler(Side side, ivec3 chunkPos, ivec3 entityPos, BlockEntityData data) { 62 return Solidity.solid; 63 } 64 65 void nullDebugHandler(BlockWorldPos, BlockEntityData) {} 66 67 struct BlockEntityInfo 68 { 69 string name; 70 BlockEntityMeshhandler meshHandler = &nullBlockEntityMeshhandler; 71 SolidityHandler sideSolidity = &nullSolidityHandler; 72 EntityBoxHandler boxHandler = &nullBoxHandler; 73 EntityDebugHandler debugHandler = &nullDebugHandler; 74 ubyte[3] color; 75 //bool isVisible = true; 76 size_t id; 77 } 78 BlockEntityInfo unknownBlockEntity = BlockEntityInfo("Unknown"); 79 80 struct BlockEntityInfoTable 81 { 82 immutable(BlockEntityInfo)[] blockEntityInfos; 83 size_t length() {return blockEntityInfos.length; } 84 BlockEntityInfo opIndex(ushort blockEntityId) { 85 blockEntityId = blockEntityId; 86 if (blockEntityId >= blockEntityInfos.length) 87 return unknownBlockEntity; 88 return blockEntityInfos[blockEntityId]; 89 } 90 }