From d227f36ecd4dac46ae7817bb251763ee6b21619e Mon Sep 17 00:00:00 2001 From: AlexSTV Date: Thu, 17 Sep 2015 23:05:19 +0100 Subject: [PATCH] Overhaul block data in progress Issue #46. More testing to be done and an example block is needed but this overhaul is in progress, I think this will make block data more flexible. --- .../Blocks/Block Types/BlockController.cs | 5 +- .../Code/Blocks/Block Types/CustomMesh.cs | 4 +- Assets/Voxelmetric/Code/Blocks/Block.cs | 14 +-- .../Code/Blocks/Builders/BlockBuilder.cs | 12 +- .../Code/Blocks/Builders/MeshBuilder.cs | 23 +++- .../Voxelmetric/Code/Data types/BlockData.cs | 43 +++++++ .../Code/Data types/BlockData.cs.meta | 12 ++ .../Code/Generate/ClassicTerrainGen.cs | 5 +- .../Code/Generate/StructureTree.cs | 12 +- .../Voxelmetric/Code/Generate/TerrainLayer.cs | 7 +- .../Textures/TextureCollection.cs | 2 - Assets/Voxelmetric/Code/Save and Load/Save.cs | 23 +--- .../Code/Save and Load/Serialization.cs | 3 +- .../Code/Utilities/BlockDataMap.cs | 108 ++++++++++++++++++ .../Code/Utilities/BlockDataMap.cs.meta | 12 ++ .../Voxelmetric/Code/Utilities/BlockLight.cs | 42 +++---- Assets/Voxelmetric/Code/World/Chunk.cs | 16 ++- Assets/Voxelmetric/Code/World/EmptyChunk.cs | 2 +- Assets/Voxelmetric/Code/World/World.cs | 4 +- Assets/Voxelmetric/Extend/CavesLayer.cs | 5 +- .../Voxelmetric/Extend/wildgrassOverride.cs | 16 +-- 21 files changed, 268 insertions(+), 102 deletions(-) create mode 100644 Assets/Voxelmetric/Code/Data types/BlockData.cs create mode 100644 Assets/Voxelmetric/Code/Data types/BlockData.cs.meta create mode 100644 Assets/Voxelmetric/Code/Utilities/BlockDataMap.cs create mode 100644 Assets/Voxelmetric/Code/Utilities/BlockDataMap.cs.meta diff --git a/Assets/Voxelmetric/Code/Blocks/Block Types/BlockController.cs b/Assets/Voxelmetric/Code/Blocks/Block Types/BlockController.cs index b9e69838..d2be37b4 100644 --- a/Assets/Voxelmetric/Code/Blocks/Block Types/BlockController.cs +++ b/Assets/Voxelmetric/Code/Blocks/Block Types/BlockController.cs @@ -21,7 +21,10 @@ public virtual void BuildBlock(Chunk chunk, BlockPos pos, MeshData meshData, Blo public virtual bool IsTransparent() { return false; } - public virtual byte LightEmmitted() { return 0; } + /// + /// Must return a number between 0 and 15, 0 for non emitting + /// + public virtual byte LightEmitted() { return 0; } /// /// Returns true if the block can be used as a possible path for path finding diff --git a/Assets/Voxelmetric/Code/Blocks/Block Types/CustomMesh.cs b/Assets/Voxelmetric/Code/Blocks/Block Types/CustomMesh.cs index de9caa0e..390acf0f 100644 --- a/Assets/Voxelmetric/Code/Blocks/Block Types/CustomMesh.cs +++ b/Assets/Voxelmetric/Code/Blocks/Block Types/CustomMesh.cs @@ -56,10 +56,10 @@ public override void AddBlockData(Chunk chunk, BlockPos pos, MeshData meshData, if (uvs.Length == 0) meshData.uv.Add(new Vector2(0, 0)); - float lighting; + float lighting = 1; if (Config.Toggle.BlockLighting) { - lighting = block.data1 / 255f; + //lighting = block.data1 / 255f; } else { diff --git a/Assets/Voxelmetric/Code/Blocks/Block.cs b/Assets/Voxelmetric/Code/Blocks/Block.cs index 0651af58..4e6c0425 100644 --- a/Assets/Voxelmetric/Code/Blocks/Block.cs +++ b/Assets/Voxelmetric/Code/Blocks/Block.cs @@ -1,24 +1,18 @@ using System; +using System.Runtime.InteropServices; using UnityEngine; [Serializable] public struct Block { public readonly ushort type; - public byte data1; - public byte data2; - public byte data3; - public byte data4; - public bool modified; + + public BlockData data; public Block(int type) { this.type = (ushort)type; - modified = true; - data1 = 0; - data2 = 0; - data3 = 0; - data4 = 0; + data = new BlockData(); } public BlockController controller diff --git a/Assets/Voxelmetric/Code/Blocks/Builders/BlockBuilder.cs b/Assets/Voxelmetric/Code/Blocks/Builders/BlockBuilder.cs index 7562283d..38a32d6d 100644 --- a/Assets/Voxelmetric/Code/Blocks/Builders/BlockBuilder.cs +++ b/Assets/Voxelmetric/Code/Blocks/Builders/BlockBuilder.cs @@ -41,7 +41,7 @@ public static void BuildColors(Chunk chunk, BlockPos pos, MeshData meshData, Dir esSolid = chunk.GetBlock(pos.Add(1, 1, -1)).controller.IsSolid(Direction.west) && chunk.GetBlock(pos.Add(1, 1, -1)).controller.IsSolid(Direction.north); swSolid = chunk.GetBlock(pos.Add(-1, 1, -1)).controller.IsSolid(Direction.north) && chunk.GetBlock(pos.Add(-1, 1, -1)).controller.IsSolid(Direction.east); - light = chunk.GetBlock(pos.Add(0, 1, 0)).data1 / 255f; + light = BlockDataMap.NonSolid.Light(chunk.GetBlock(pos.Add(0, 1, 0)))/ 15f; break; case Direction.down: @@ -55,7 +55,7 @@ public static void BuildColors(Chunk chunk, BlockPos pos, MeshData meshData, Dir esSolid = chunk.GetBlock(pos.Add(1, -1, 1)).controller.IsSolid(Direction.west) && chunk.GetBlock(pos.Add(1, -1, 1)).controller.IsSolid(Direction.north); swSolid = chunk.GetBlock(pos.Add(-1, -1, 1)).controller.IsSolid(Direction.north) && chunk.GetBlock(pos.Add(-1, -1, 1)).controller.IsSolid(Direction.east); - light = chunk.GetBlock(pos.Add(0, -1, 0)).data1 / 255f; + light = BlockDataMap.NonSolid.Light(chunk.GetBlock(pos.Add(0, -1, 0))) / 15f; break; case Direction.north: @@ -69,7 +69,7 @@ public static void BuildColors(Chunk chunk, BlockPos pos, MeshData meshData, Dir wnSolid = chunk.GetBlock(pos.Add(1, -1, 1)).controller.IsSolid(Direction.west) && chunk.GetBlock(pos.Add(1, -1, 1)).controller.IsSolid(Direction.north); swSolid = chunk.GetBlock(pos.Add(-1, -1, 1)).controller.IsSolid(Direction.north) && chunk.GetBlock(pos.Add(-1, -1, 1)).controller.IsSolid(Direction.east); - light = chunk.GetBlock(pos.Add(0, 0, 1)).data1 / 255f; + light = BlockDataMap.NonSolid.Light(chunk.GetBlock(pos.Add(0, 0, 1))) / 15f; break; case Direction.east: @@ -83,7 +83,7 @@ public static void BuildColors(Chunk chunk, BlockPos pos, MeshData meshData, Dir wnSolid = chunk.GetBlock(pos.Add(1, -1, -1)).controller.IsSolid(Direction.east) && chunk.GetBlock(pos.Add(1, -1, -1)).controller.IsSolid(Direction.north); swSolid = chunk.GetBlock(pos.Add(1, -1, 1)).controller.IsSolid(Direction.north) && chunk.GetBlock(pos.Add(1, -1, 1)).controller.IsSolid(Direction.east); - light = chunk.GetBlock(pos.Add(1, 0, 0)).data1 / 255f; + light = BlockDataMap.NonSolid.Light(chunk.GetBlock(pos.Add(1, 0, 0))) / 15f; break; case Direction.south: @@ -97,7 +97,7 @@ public static void BuildColors(Chunk chunk, BlockPos pos, MeshData meshData, Dir wnSolid = chunk.GetBlock(pos.Add(-1, -1, -1)).controller.IsSolid(Direction.east) && chunk.GetBlock(pos.Add(-1, -1, -1)).controller.IsSolid(Direction.north); swSolid = chunk.GetBlock(pos.Add(1, -1, -1)).controller.IsSolid(Direction.north) && chunk.GetBlock(pos.Add(1, -1, -1)).controller.IsSolid(Direction.east); - light = chunk.GetBlock(pos.Add(0, 0, -1)).data1 / 255f; + light = BlockDataMap.NonSolid.Light(chunk.GetBlock(pos.Add(0, 0, -1))) / 15f; break; case Direction.west: @@ -111,7 +111,7 @@ public static void BuildColors(Chunk chunk, BlockPos pos, MeshData meshData, Dir wnSolid = chunk.GetBlock(pos.Add(-1, -1, 1)).controller.IsSolid(Direction.east) && chunk.GetBlock(pos.Add(-1, -1, 1)).controller.IsSolid(Direction.north); swSolid = chunk.GetBlock(pos.Add(-1, -1, -1)).controller.IsSolid(Direction.north) && chunk.GetBlock(pos.Add(-1, -1, -1)).controller.IsSolid(Direction.east); - light = chunk.GetBlock(pos.Add(-1, 0, 0)).data1 / 255f; + light = BlockDataMap.NonSolid.Light(chunk.GetBlock(pos.Add(-1, 0, 0))) / 15f; break; default: diff --git a/Assets/Voxelmetric/Code/Blocks/Builders/MeshBuilder.cs b/Assets/Voxelmetric/Code/Blocks/Builders/MeshBuilder.cs index a3cb908d..c96017a8 100644 --- a/Assets/Voxelmetric/Code/Blocks/Builders/MeshBuilder.cs +++ b/Assets/Voxelmetric/Code/Blocks/Builders/MeshBuilder.cs @@ -7,17 +7,32 @@ public static void CrossMeshRenderer(Chunk chunk, BlockPos pos, MeshData meshDat { float halfBlock = (Config.Env.BlockSize / 2) + Config.Env.BlockFacePadding; float colliderOffest = 0.05f * Config.Env.BlockSize; - float blockHeight = halfBlock * 2 * (block.data2 / 255f); - float offsetX = (halfBlock * 2 * ((byte)(block.data3 & 0x0F) / 32f)) - (halfBlock/2); - float offsetZ = (halfBlock * 2 * ((byte)((block.data3 & 0xF0) >> 4) / 32f)) - (halfBlock/2); + //Using the block positions hash is much better for random numbers than saving the offset and height in the block data + int hash = pos.GetHashCode(); + if (hash < 0) + hash *= -1; + + float blockHeight = halfBlock * 2 * (hash % 100) / 100f; + + hash *= 39; + if (hash < 0) + hash *= -1; + + float offsetX = (halfBlock * (hash % 100) / 100f) - (halfBlock / 2); + + hash *= 39; + if (hash < 0) + hash *= -1; + + float offsetZ = (halfBlock * (hash % 100) / 100f)-(halfBlock / 2); //Converting the position to a vector adjusts it based on block size and gives us real world coordinates for x, y and z Vector3 vPos = pos; Vector3 vPosCollider = pos; vPos += new Vector3(offsetX, 0, offsetZ); - float blockLight = ( (block.data1/255f) * Config.Env.BlockLightStrength) + (0.8f*Config.Env.AOStrength); + float blockLight = ( BlockDataMap.NonSolid.Light(block) / 15f * Config.Env.BlockLightStrength) +(0.8f * Config.Env.AOStrength); meshData.AddVertex(new Vector3(vPos.x - halfBlock, vPos.y - halfBlock, vPos.z + halfBlock)); meshData.AddVertex(new Vector3(vPos.x - halfBlock, vPos.y - halfBlock + blockHeight, vPos.z + halfBlock)); diff --git a/Assets/Voxelmetric/Code/Data types/BlockData.cs b/Assets/Voxelmetric/Code/Data types/BlockData.cs new file mode 100644 index 00000000..d8415612 --- /dev/null +++ b/Assets/Voxelmetric/Code/Data types/BlockData.cs @@ -0,0 +1,43 @@ +using System; + +[Serializable] +public struct BlockData +{ + // This can be set to a long if 32 bits isn't enough memory but be careful raising this too high + public int data; + + public bool this[int index] + { + get + { + return ((data >> index) & 0x1) == 1; + } + + set + { + int mask = (1 << index); + if (value) + { + data |= mask; + } + else + { + data &= ~mask; + } + } + } + + public void SetData(int index, int value, int width) + { + for (int i = 0; i < width; i++) + { + this[index + i] = ((value >> i) & 0x1) == 1; + } + } + + public int GetData(int index, int width) + { + return (int)((data >> index) & (16 ^ width)); + } + +} \ No newline at end of file diff --git a/Assets/Voxelmetric/Code/Data types/BlockData.cs.meta b/Assets/Voxelmetric/Code/Data types/BlockData.cs.meta new file mode 100644 index 00000000..f6a17fd7 --- /dev/null +++ b/Assets/Voxelmetric/Code/Data types/BlockData.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: f0d5d876355fca14d95a8efc8774af0f +timeCreated: 1442098368 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Voxelmetric/Code/Generate/ClassicTerrainGen.cs b/Assets/Voxelmetric/Code/Generate/ClassicTerrainGen.cs index 73fda5f4..035c7313 100644 --- a/Assets/Voxelmetric/Code/Generate/ClassicTerrainGen.cs +++ b/Assets/Voxelmetric/Code/Generate/ClassicTerrainGen.cs @@ -101,8 +101,6 @@ protected virtual void GenerateTerrain(Chunk chunk, int x, int z) else if (y + chunk.pos.y == dirtHeight + 1 && GetNoise(x + chunk.pos.x, y + chunk.pos.y, z + chunk.pos.z, 10, 10, 1) > 5) { Block wildGrass = "wildgrass"; - wildGrass.data2 = (byte)(GetNoise(x + chunk.pos.x, y + chunk.pos.y, z + chunk.pos.z, 1, 155, 1) + 100); - SetBlock(chunk, wildGrass, new BlockPos(x, y, z)); } @@ -116,8 +114,7 @@ public static void SetBlock(Chunk chunk, Block block, BlockPos pos, bool replace { if (replaceBlocks || chunk.GetBlock(pos).type == Block.Air.type) { - block.modified = false; - chunk.SetBlock(pos, block, false); + chunk.SetBlock(pos, block, updateChunk: false, setBlockModified: false); } } } diff --git a/Assets/Voxelmetric/Code/Generate/StructureTree.cs b/Assets/Voxelmetric/Code/Generate/StructureTree.cs index af741023..3089b25e 100644 --- a/Assets/Voxelmetric/Code/Generate/StructureTree.cs +++ b/Assets/Voxelmetric/Code/Generate/StructureTree.cs @@ -26,8 +26,7 @@ public override void Build(World world, BlockPos chunkPos, BlockPos pos, Terrain if (Chunk.InRange(pos.x + x - chunkPos.x) && Chunk.InRange(pos.z + z - chunkPos.z)) { Block block = "leaves"; - block.modified = false; - world.SetBlock(pos.Add(x, y, z), block, false); + world.SetBlock(pos.Add(x, y, z), block, updateChunk: false, setBlockModified: false); } } } @@ -37,8 +36,7 @@ public override void Build(World world, BlockPos chunkPos, BlockPos pos, Terrain if (y < Config.Env.WorldMaxY) { Block block = "log"; - block.modified = false; - world.SetBlock(pos.Add(0, y, 0), block, false); + world.SetBlock(pos.Add(0, y, 0), block, updateChunk: false, setBlockModified: false); } } } @@ -57,8 +55,7 @@ public void OldBuild(World world, BlockPos chunkPos, BlockPos pos, OldTerrainGen if (Chunk.InRange(pos.x + x - chunkPos.x) && Chunk.InRange(pos.z + z - chunkPos.z)) { Block block = "leaves"; - block.modified = false; - world.SetBlock(pos.Add(x, y, z), block, false); + world.SetBlock(pos.Add(x, y, z), block, updateChunk: false, setBlockModified: false); } } } @@ -68,8 +65,7 @@ public void OldBuild(World world, BlockPos chunkPos, BlockPos pos, OldTerrainGen if (y < Config.Env.WorldMaxY) { Block block = "log"; - block.modified = false; - world.SetBlock(pos.Add(0, y, 0), block, false); + world.SetBlock(pos.Add(0, y, 0), block, updateChunk: false, setBlockModified: false); } } } diff --git a/Assets/Voxelmetric/Code/Generate/TerrainLayer.cs b/Assets/Voxelmetric/Code/Generate/TerrainLayer.cs index cd4213f8..1ef15c60 100644 --- a/Assets/Voxelmetric/Code/Generate/TerrainLayer.cs +++ b/Assets/Voxelmetric/Code/Generate/TerrainLayer.cs @@ -85,14 +85,13 @@ public virtual int GenerateLayer(int x, int z, int heightSoFar, float strength, } Block blockToPlace = blockName; - blockToPlace.modified = false; if (layerType == LayerType.Chance) { if (GetNoise(x, -10555, z, 1, 100, 1) < chanceToSpawnBlock) { if(!justGetHeight) - world.SetBlock(new BlockPos(x, heightSoFar, z), blockToPlace, false); + world.SetBlock(new BlockPos(x, heightSoFar, z), blockToPlace, updateChunk: false, setBlockModified: false); return heightSoFar + 1; } @@ -112,14 +111,14 @@ public virtual int GenerateLayer(int x, int z, int heightSoFar, float strength, { for (int y = heightSoFar; y < height + Config.Env.WorldMinY; y++) { - world.SetBlock(new BlockPos(x, y, z), blockToPlace, false); + world.SetBlock(new BlockPos(x, y, z), blockToPlace, updateChunk: false, setBlockModified: false); } } else //additive or surface { for (int y = heightSoFar; y < height + heightSoFar; y++) { - world.SetBlock(new BlockPos(x, y, z), blockToPlace, false); + world.SetBlock(new BlockPos(x, y, z), blockToPlace, updateChunk: false, setBlockModified: false); } } } diff --git a/Assets/Voxelmetric/Code/Load Resources/Textures/TextureCollection.cs b/Assets/Voxelmetric/Code/Load Resources/Textures/TextureCollection.cs index 3760823e..08c77b31 100644 --- a/Assets/Voxelmetric/Code/Load Resources/Textures/TextureCollection.cs +++ b/Assets/Voxelmetric/Code/Load Resources/Textures/TextureCollection.cs @@ -9,12 +9,10 @@ public class TextureCollection bool usesConnectedTextures = false; Rect[] connectedTextures = new Rect[48]; List textures = new List(); - Noise noiseGen; public TextureCollection(string name) { textureName = name; - noiseGen = new Noise(); } public void AddTexture(Rect texture, int connectedTextureType, int randomChance) diff --git a/Assets/Voxelmetric/Code/Save and Load/Save.cs b/Assets/Voxelmetric/Code/Save and Load/Save.cs index 9a8c579f..342663e9 100644 --- a/Assets/Voxelmetric/Code/Save and Load/Save.cs +++ b/Assets/Voxelmetric/Code/Save and Load/Save.cs @@ -23,23 +23,12 @@ public Save(Chunk chunk) //blocks already in the save fie if there is one. Then add Dictionary blocksDictionary = AddSavedBlocks(chunk); - for (int x = 0; x < Config.Env.ChunkSize; x++) - { - for (int y = 0; y < Config.Env.ChunkSize; y++) - { - for (int z = 0; z < Config.Env.ChunkSize; z++) - { - BlockPos pos = new BlockPos(x, y, z); - if (chunk.GetBlock(pos).modified) - { - //remove any existing blocks in the dictionary as they're - //from the existing save and are overwritten - blocksDictionary.Remove(pos); - blocksDictionary.Add(pos, chunk.GetBlock(pos)); - changed = true; - } - } - } + foreach(var pos in chunk.modifiedBlocks) { + //remove any existing blocks in the dictionary as they're + //from the existing save and are overwritten + blocksDictionary.Remove(pos); + blocksDictionary.Add(pos, chunk.GetBlock(pos)); + changed = true; } blocks = new Block[blocksDictionary.Keys.Count]; diff --git a/Assets/Voxelmetric/Code/Save and Load/Serialization.cs b/Assets/Voxelmetric/Code/Save and Load/Serialization.cs index 674ecffa..15b4853c 100644 --- a/Assets/Voxelmetric/Code/Save and Load/Serialization.cs +++ b/Assets/Voxelmetric/Code/Save and Load/Serialization.cs @@ -63,8 +63,7 @@ public static bool Load(Chunk chunk) for (int i =0; i< save.blocks.Length; i++) { Block placeBlock = save.blocks[i]; - placeBlock.modified = false; - chunk.SetBlock(save.positions[i], placeBlock, false); + chunk.SetBlock(save.positions[i], placeBlock, updateChunk: false, setBlockModified: false); } stream.Close(); diff --git a/Assets/Voxelmetric/Code/Utilities/BlockDataMap.cs b/Assets/Voxelmetric/Code/Utilities/BlockDataMap.cs new file mode 100644 index 00000000..4debfcd7 --- /dev/null +++ b/Assets/Voxelmetric/Code/Utilities/BlockDataMap.cs @@ -0,0 +1,108 @@ +namespace BlockDataMap { + + //An interface for handling light data for non-solid blocks + //Handles all the logic in fetching light values for a block + public static class NonSolid + { + /// + /// returns the block data as a byte between 0 and 15 + /// + public static byte Light(Block block) + { + if (!block.controller.IsTransparent()) + { + return 0; + } + + if (block.controller.LightEmitted() != 0) + { + return block.controller.LightEmitted(); + } + + return (byte)block.data.GetData(0, 4); + } + + /// + /// sets the blocks data to a byte between 0 and 15 + /// + public static byte Light(Block block, byte newValue) + { + if (!block.controller.IsTransparent()) + { + return 0; + } + + if (block.controller.LightEmitted() > newValue) + { + block.data.SetData(0, block.controller.LightEmitted(), 4); + return block.controller.LightEmitted(); + } + + block.data.SetData(0, newValue, 4); + return newValue; + } + } + + //An interface for handling crossmesh data + public static class CrossMesh + { + // The data for crossmesh parameters is stored in nibbles. Nibbles are 4bit large + // variables that go between 0 and 15. We don't really need anything bigger than 16 + // so this works nicely and saves space. However the set and return values are all + // abstracted into floats between 0 and 1 to make it easier for other code to use + // this data without manipulating it. All the logic in fetching the value is here and + // other code just gets and sets a simple float. + + /// + /// Returns a float between 1 and 0 describing the height of the mesh + /// + public static float Height(Block block) + { + return block.data.GetData(4, 4) / 15f; + } + + /// + /// Sets the height of the mesh based on a new value between 1 and 0 + /// + public static float Height(Block block, float newValue) + { + block.data.SetData(4, (byte)(newValue * 15), 4); + return newValue; + } + + /// + /// Returns a float between 1 and 0 describing the XOffset of the mesh + /// + public static float XOffset(Block block) + { + return block.data.GetData(8, 4) / 15f; + } + + /// + /// Sets the XOffset of the mesh based on a new value between 1 and 0 + /// + public static float XOffset(Block block, float newValue) + { + block.data.SetData(8, (byte)(newValue*15), 4); + return newValue; + } + + /// + /// Returns a float between 1 and 0 describing the yOffset of the mesh + /// + public static float YOffset(Block block) + { + return block.data.GetData(12, 4) / 15f; + } + + /// + /// Sets the yOffset of the mesh based on a new value between 1 and 0 + /// + public static float YOffset(Block block, float newValue) + { + block.data.SetData(12, (byte)(newValue * 15), 4); + return newValue; + } + + } +} diff --git a/Assets/Voxelmetric/Code/Utilities/BlockDataMap.cs.meta b/Assets/Voxelmetric/Code/Utilities/BlockDataMap.cs.meta new file mode 100644 index 00000000..df4f1554 --- /dev/null +++ b/Assets/Voxelmetric/Code/Utilities/BlockDataMap.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: e07c6a44a6367fc47876701b30f8b9e0 +timeCreated: 1442510281 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Voxelmetric/Code/Utilities/BlockLight.cs b/Assets/Voxelmetric/Code/Utilities/BlockLight.cs index 96f70178..9a54832c 100644 --- a/Assets/Voxelmetric/Code/Utilities/BlockLight.cs +++ b/Assets/Voxelmetric/Code/Utilities/BlockLight.cs @@ -6,7 +6,7 @@ public static class BlockLight public static int lightEffectRadius = 8; - public static byte lightReduceBy = 32; + public static byte lightReduceBy = 2; public static void LightArea(World world, BlockPos pos) { @@ -104,19 +104,19 @@ public static void ResetLightColumn(World world, int x, int z, List ch if (block.controller.IsTransparent()) { - byte originalData1 = block.data1; + byte originalData1 = BlockDataMap.NonSolid.Light(block); if (sunlightObstructed) { - block.data1 = block.controller.LightEmmitted(); + BlockDataMap.NonSolid.Light(block, block.controller.LightEmitted()); } else { - block.data1 = 255; + BlockDataMap.NonSolid.Light(block, 15); } chunk.SetBlock(localPos, block, false); - if (block.data1 != originalData1 && !chunksToUpdate.Contains(chunk.pos)) + if (BlockDataMap.NonSolid.Light(block) != originalData1 && !chunksToUpdate.Contains(chunk.pos)) chunksToUpdate.Add(chunk.pos); } @@ -127,12 +127,12 @@ public static void FloodLight(World world, int x, int y, int z, List c { Block block = world.GetBlock(new BlockPos(x, y, z)); - if (!block.controller.IsTransparent() & block.controller.LightEmmitted() == 0) + if (!block.controller.IsTransparent() & block.controller.LightEmitted() == 0) return; - byte lightSpill = block.data1; - if (block.controller.LightEmmitted() > lightSpill) - lightSpill = block.controller.LightEmmitted(); + byte lightSpill = BlockDataMap.NonSolid.Light(block); + if (block.controller.LightEmitted() > lightSpill) + lightSpill = block.controller.LightEmitted(); if (lightSpill <= lightReduceBy) return; @@ -166,25 +166,27 @@ public static void SpillLight(World world, BlockPos pos, byte light, List= light) + if (BlockDataMap.NonSolid.Light(block) >= light) return; if (!chunksToUpdate.Contains(chunk.pos)) chunksToUpdate.Add(chunk.pos); - block.data1 = light; + BlockDataMap.NonSolid.Light(block, light); chunk.SetBlock(localPos, block, false); - if (block.data1 > lightReduceBy) + byte blockLight = BlockDataMap.NonSolid.Light(block); + + if (blockLight > lightReduceBy) { - block.data1 -= lightReduceBy; - - CallSpillLight(world, chunk, pos.Add(1, 0, 0), block.data1, chunksToUpdate, stayWithinChunk); - CallSpillLight(world, chunk, pos.Add(0, 1, 0), block.data1, chunksToUpdate, stayWithinChunk); - CallSpillLight(world, chunk, pos.Add(0, 0, 1), block.data1, chunksToUpdate, stayWithinChunk); - CallSpillLight(world, chunk, pos.Add(-1, 0, 0), block.data1, chunksToUpdate, stayWithinChunk); - CallSpillLight(world, chunk, pos.Add(0, -1, 0), block.data1, chunksToUpdate, stayWithinChunk); - CallSpillLight(world, chunk, pos.Add(0, 0, -1), block.data1, chunksToUpdate, stayWithinChunk); + BlockDataMap.NonSolid.Light(block, (byte)(blockLight - lightReduceBy)); + + CallSpillLight(world, chunk, pos.Add(1, 0, 0), blockLight, chunksToUpdate, stayWithinChunk); + CallSpillLight(world, chunk, pos.Add(0, 1, 0), blockLight, chunksToUpdate, stayWithinChunk); + CallSpillLight(world, chunk, pos.Add(0, 0, 1), blockLight, chunksToUpdate, stayWithinChunk); + CallSpillLight(world, chunk, pos.Add(-1, 0, 0), blockLight, chunksToUpdate, stayWithinChunk); + CallSpillLight(world, chunk, pos.Add(0, -1, 0), blockLight, chunksToUpdate, stayWithinChunk); + CallSpillLight(world, chunk, pos.Add(0, 0, -1), blockLight, chunksToUpdate, stayWithinChunk); } return; } diff --git a/Assets/Voxelmetric/Code/World/Chunk.cs b/Assets/Voxelmetric/Code/World/Chunk.cs index 8c970bfe..d9daf3b7 100644 --- a/Assets/Voxelmetric/Code/World/Chunk.cs +++ b/Assets/Voxelmetric/Code/World/Chunk.cs @@ -3,6 +3,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Runtime.InteropServices; [RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] @@ -26,6 +27,7 @@ public enum Flag {busy, meshReady, loaded, terrainGenerated, markedForDeletion, float randomUpdateTime = 0; MeshData meshData = new MeshData(); + public List modifiedBlocks = new List(); void Start() { @@ -222,7 +224,7 @@ public static bool InRange(int index) /// Local position /// Block to place at the given location /// Optional parameter, set to false to keep the chunk unupdated despite the change - public virtual void SetBlock(BlockPos blockPos, Block block, bool updateChunk = true) + public virtual void SetBlock(BlockPos blockPos, Block block, bool updateChunk = true, bool setBlockModified = true) { if (InRange(blockPos)) { @@ -235,8 +237,8 @@ public virtual void SetBlock(BlockPos blockPos, Block block, bool updateChunk = blocks[blockPos.x, blockPos.y, blockPos.z] = block; - if (block.modified) - SetFlag(Flag.chunkModified, true); + if (setBlockModified) + SetBlockModified(blockPos); if (updateChunk) UpdateChunk(); @@ -321,4 +323,12 @@ void ReturnChunkToPool() world.AddToChunkPool(gameObject); } + public void SetBlockModified(BlockPos pos) + { + if (!modifiedBlocks.Contains(pos)) + { + modifiedBlocks.Add(pos); + SetFlag(Flag.chunkModified, true); + } + } } \ No newline at end of file diff --git a/Assets/Voxelmetric/Code/World/EmptyChunk.cs b/Assets/Voxelmetric/Code/World/EmptyChunk.cs index ea2c1ec5..4505c262 100644 --- a/Assets/Voxelmetric/Code/World/EmptyChunk.cs +++ b/Assets/Voxelmetric/Code/World/EmptyChunk.cs @@ -9,7 +9,7 @@ public override Block GetBlock(BlockPos blockPos) return Block.Air; } - public override void SetBlock(BlockPos blockPos, Block block, bool updateChunk = true) { } + public override void SetBlock(BlockPos blockPos, Block block, bool updateChunk = true, bool setBlockModified = true) { } protected override void TimedUpdated() {} } diff --git a/Assets/Voxelmetric/Code/World/World.cs b/Assets/Voxelmetric/Code/World/World.cs index 3ef384a0..0d5190ec 100644 --- a/Assets/Voxelmetric/Code/World/World.cs +++ b/Assets/Voxelmetric/Code/World/World.cs @@ -197,13 +197,13 @@ public Block GetBlock(BlockPos pos) /// Global position of the block /// The block be placed /// Optional parameter, set to false not update the chunk despite the change - public void SetBlock(BlockPos pos, Block block, bool updateChunk = true) + public void SetBlock(BlockPos pos, Block block, bool updateChunk = true, bool setBlockModified = true) { Chunk chunk = GetChunk(pos); if (chunk != null) { BlockPos localPos = pos - chunk.pos; - chunk.SetBlock(localPos, block, updateChunk); + chunk.SetBlock(localPos, block, updateChunk, setBlockModified); if (updateChunk) { diff --git a/Assets/Voxelmetric/Extend/CavesLayer.cs b/Assets/Voxelmetric/Extend/CavesLayer.cs index c7f530d2..a4d5e0c1 100644 --- a/Assets/Voxelmetric/Extend/CavesLayer.cs +++ b/Assets/Voxelmetric/Extend/CavesLayer.cs @@ -6,8 +6,7 @@ public class CavesLayer : LayerOverride { public override int GenerateLayer(int x, int z, int heightSoFar, float strength, bool justGetHeight = false) { - Block blockToPlace = blockName; - blockToPlace.modified = false; + Block blockToPlace = blockName; int caveBottom = GetNoise(x, -1000, z, 500, 70, 1) + Config.Env.WorldMinY; int caveHeight = GetNoise(x, 1000, z, 50, 35, 1) + caveBottom; @@ -22,7 +21,7 @@ public override int GenerateLayer(int x, int z, int heightSoFar, float strength, { for (int y = caveBottom; y < caveTop; y++) { - world.SetBlock(new BlockPos(x, y, z), blockToPlace, false); + world.SetBlock(new BlockPos(x, y, z), blockToPlace, updateChunk: false, setBlockModified: false); } } diff --git a/Assets/Voxelmetric/Extend/wildgrassOverride.cs b/Assets/Voxelmetric/Extend/wildgrassOverride.cs index b5b193ed..a0728325 100644 --- a/Assets/Voxelmetric/Extend/wildgrassOverride.cs +++ b/Assets/Voxelmetric/Extend/wildgrassOverride.cs @@ -7,20 +7,10 @@ public class wildgrassOverride : BlockOverride // On create set the height to 10 and schedule and update in 1 second public override Block OnCreate(Chunk chunk, BlockPos pos, Block block) { - block.data2 = (byte)(64 + ((chunk.world.noiseGen.Generate(pos.x * 1000, pos.y * 1000, pos.z * 1000) + 1) * 96)); - int offset1 = (int)((chunk.world.noiseGen.Generate(pos.x* 1000, pos.y* 1000, pos.z * 1000) + 1) * 16); - block.data3 = (byte)((block.data3 & 240) | (offset1 & 15)); - int offset2 = (int)((chunk.world.noiseGen.Generate(pos.x*1000, pos.y * 10000, pos.z * 1000) + 1) * 16); - block.data3 = (byte)((offset2 << 4) | (block.data3 & 15)); + //Debug.Log("Height: " + BlockDataMap.CrossMesh.Height(block, chunk.world.noiseGen.Generate(pos.x * 1000, pos.y * 1000, pos.z * 1000) )); + //Debug.Log("XOffset: " + BlockDataMap.CrossMesh.XOffset(block, (chunk.world.noiseGen.Generate(pos.x * 1000, pos.y * 1000, pos.z * 1000) + 0.5f))); + //Debug.Log("YOffset: " + BlockDataMap.CrossMesh.YOffset(block, (chunk.world.noiseGen.Generate(pos.x * 1000, pos.y * 10000, pos.z * 1000) + 0.5f))); return block; } - - //On random update add 100 to the height - //Removed this because although it is nice it means every block with wild grass needs to get saved - //public override void RandomUpdate(Chunk chunk, BlockPos pos, Block block) - //{ - // block.data2 += 100; - // chunk.SetBlock(pos, block); - //} }