Skip to content

Commit

Permalink
Overhaul block data in progress
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alex-irt committed Sep 17, 2015
1 parent a81f4ed commit d227f36
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
/// <summary>
/// Must return a number between 0 and 15, 0 for non emitting
/// </summary>
public virtual byte LightEmitted() { return 0; }

/// <summary>
/// Returns true if the block can be used as a possible path for path finding
Expand Down
4 changes: 2 additions & 2 deletions Assets/Voxelmetric/Code/Blocks/Block Types/CustomMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
14 changes: 4 additions & 10 deletions Assets/Voxelmetric/Code/Blocks/Block.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions Assets/Voxelmetric/Code/Blocks/Builders/BlockBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
23 changes: 19 additions & 4 deletions Assets/Voxelmetric/Code/Blocks/Builders/MeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
43 changes: 43 additions & 0 deletions Assets/Voxelmetric/Code/Data types/BlockData.cs
Original file line number Diff line number Diff line change
@@ -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));
}

}
12 changes: 12 additions & 0 deletions Assets/Voxelmetric/Code/Data types/BlockData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions Assets/Voxelmetric/Code/Generate/ClassicTerrainGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand All @@ -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);
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions Assets/Voxelmetric/Code/Generate/StructureTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions Assets/Voxelmetric/Code/Generate/TerrainLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ public class TextureCollection
bool usesConnectedTextures = false;
Rect[] connectedTextures = new Rect[48];
List<Rect> textures = new List<Rect>();
Noise noiseGen;

public TextureCollection(string name)
{
textureName = name;
noiseGen = new Noise();
}

public void AddTexture(Rect texture, int connectedTextureType, int randomChance)
Expand Down
23 changes: 6 additions & 17 deletions Assets/Voxelmetric/Code/Save and Load/Save.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,12 @@ public Save(Chunk chunk)
//blocks already in the save fie if there is one. Then add
Dictionary<BlockPos, Block> 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];
Expand Down
3 changes: 1 addition & 2 deletions Assets/Voxelmetric/Code/Save and Load/Serialization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading

0 comments on commit d227f36

Please sign in to comment.