public Block this[BlockIndex index] { get { return this[index.Z, index.Y, index.X]; } set { this[index.Z, index.Y, index.X] = value; } }
public static double GetRayIntersectDistance(double maxValue, Vector3d direction, Vector3d startPosition) { Ray ray = new Ray(startPosition, direction); double distance = 0.0; BlockIndex index; double? intersect; while (distance <= maxValue) { index = new BlockIndex(direction * distance + startPosition); intersect = index.GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index).Solidity) { return intersect.Value; } intersect = (index + BlockIndex.UnitX).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitX).Solidity) { return intersect.Value; } intersect = (index - BlockIndex.UnitX).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitX).Solidity) { return intersect.Value; } intersect = (index + BlockIndex.UnitY).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitY).Solidity) { return intersect.Value; } intersect = (index - BlockIndex.UnitY).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitY).Solidity) { return intersect.Value; } intersect = (index + BlockIndex.UnitZ).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitZ).Solidity) { return intersect.Value; } intersect = (index - BlockIndex.UnitZ).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitZ).Solidity) { return intersect.Value; } distance += 0.5F; } return maxValue; }
private static void GrowTree(Chunk chunk, BlockIndex originPoint, int treeSize) { BlockIndex point = originPoint + BlockIndex.UnitY; int trunkHeight = (int)Math.Floor((float)treeSize * 4.0F / 5.0F); float leafRadius = (float)trunkHeight / 2.0F; int leafCenter = (int)Math.Floor((float)treeSize * 2.0F / 3.0F); // Check whether or not tree can be placed at all BlockIndex blockWorldPos; List<BlockIndex> leafPositions = new List<BlockIndex>(); List<BlockIndex> trunkPositions = new List<BlockIndex>(); for (int x = -(int)leafRadius; x <= leafRadius; x++) { for (int z = -(int)leafRadius; z <= leafRadius; z++) { for (int y = 0; y <= treeSize; y++) { blockWorldPos = new BlockIndex(point.X + x, point.Y + y, point.Z + z) + chunk.Index; if (x == 0 && z == 0 && y <= trunkHeight) { if (ChunkManager.GetBlock(blockWorldPos) != Block.Air) { return; } trunkPositions.Add(blockWorldPos); } else { if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(z, 2) + Math.Pow(y - leafCenter, 2)) < leafRadius) { if (ChunkManager.GetBlock(blockWorldPos) != Block.Air) { return; } leafPositions.Add(blockWorldPos); } } } } } foreach (BlockIndex index in leafPositions) { if (index.ToChunkIndex() == chunk.Index) { chunk[index - chunk.Index] = Block.Leaves; } else { //ChunkManager.SetBlock(index, Block.Leaves, false); } } foreach (BlockIndex index in trunkPositions) { if (index.ToChunkIndex() == chunk.Index) { chunk[index - chunk.Index] = Block.Log; } else { //Constants.World.Current.SetBlock(index, Block.Log, false); } } chunk[originPoint] = Block.Dirt; }
void RenderCursor(BlockIndex currentAim) { if (Constants.Graphics.BlockCursorType == 0) { return; } if (currentAim == null) { return; } GL.MatrixMode(MatrixMode.Projection); Matrix4 proj = Constants.Engines.Physics.Player.ProjectionMatrix; GL.LoadMatrix(ref proj); GL.MatrixMode(MatrixMode.Modelview); Matrix4 view = Constants.Engines.Physics.Player.ViewMatrix; GL.LoadMatrix(ref view); if (Constants.Graphics.BlockCursorType == 1 || Constants.Graphics.BlockCursorType == 3) { GL.Color4(0.0, 0.0, 0.0, 0.2); GL.Begin(BeginMode.Quads); { GL.Vertex3(new Vector3d(1.0, 0.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 1.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 1.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 0.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 0.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 1.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 1.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 0.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 1.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 1.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 1.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 1.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 0.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 0.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 0.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 0.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 0.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 1.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 1.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 0.0, 1.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 0.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(0.0, 1.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 1.0, 0.0) + currentAim.Position); GL.Vertex3(new Vector3d(1.0, 0.0, 0.0) + currentAim.Position); } GL.End(); } if (Constants.Graphics.BlockCursorType == 2 || Constants.Graphics.BlockCursorType == 3) { GL.Disable(EnableCap.DepthTest); GL.Color4(0.0, 0.0, 0.0, 0.2); GL.Begin(BeginMode.Lines); { GL.Vertex3(new Vector3d(0, 0, 0) + currentAim.Position); GL.Vertex3(new Vector3d(1, 0, 0) + currentAim.Position); GL.Vertex3(new Vector3d(0, 0, 1) + currentAim.Position); GL.Vertex3(new Vector3d(1, 0, 1) + currentAim.Position); GL.Vertex3(new Vector3d(0, 1, 0) + currentAim.Position); GL.Vertex3(new Vector3d(1, 1, 0) + currentAim.Position); GL.Vertex3(new Vector3d(0, 1, 1) + currentAim.Position); GL.Vertex3(new Vector3d(1, 1, 1) + currentAim.Position); GL.Vertex3(new Vector3d(0, 0, 0) + currentAim.Position); GL.Vertex3(new Vector3d(0, 1, 0) + currentAim.Position); GL.Vertex3(new Vector3d(0, 0, 1) + currentAim.Position); GL.Vertex3(new Vector3d(0, 1, 1) + currentAim.Position); GL.Vertex3(new Vector3d(1, 0, 0) + currentAim.Position); GL.Vertex3(new Vector3d(1, 1, 0) + currentAim.Position); GL.Vertex3(new Vector3d(1, 0, 1) + currentAim.Position); GL.Vertex3(new Vector3d(1, 1, 1) + currentAim.Position); GL.Vertex3(new Vector3d(0, 0, 0) + currentAim.Position); GL.Vertex3(new Vector3d(0, 0, 1) + currentAim.Position); GL.Vertex3(new Vector3d(0, 1, 0) + currentAim.Position); GL.Vertex3(new Vector3d(0, 1, 1) + currentAim.Position); GL.Vertex3(new Vector3d(1, 0, 0) + currentAim.Position); GL.Vertex3(new Vector3d(1, 0, 1) + currentAim.Position); GL.Vertex3(new Vector3d(1, 1, 0) + currentAim.Position); GL.Vertex3(new Vector3d(1, 1, 1) + currentAim.Position); } GL.End(); GL.Enable(EnableCap.DepthTest); } }
public static byte GetFaceShade(BlockIndex index, Direction direction) { if (Constants.Graphics.AmbientOcclusionEnabled) { double maxDist = 6; double dist = 0; dist += Lighting.GetRayIntersectDistance(maxDist, direction.GetVector3(), (index.Position + new Vector3d(0.5, 0.5, 0.5)) + direction.GetVector3() * 0.6); dist += Lighting.GetRayIntersectDistance(maxDist, direction.GetVector3() + direction.GetPerpendicularRight(), (index.Position + new Vector3d(0.5, 0.5, 0.5)) + direction.GetVector3() * 0.6); dist += Lighting.GetRayIntersectDistance(maxDist, direction.GetVector3() - direction.GetPerpendicularRight(), (index.Position + new Vector3d(0.5, 0.5, 0.5)) + direction.GetVector3() * 0.6); dist += Lighting.GetRayIntersectDistance(maxDist, direction.GetVector3() + direction.GetPerpendicularLeft(), (index.Position + new Vector3d(0.5, 0.5, 0.5)) + direction.GetVector3() * 0.6); dist += Lighting.GetRayIntersectDistance(maxDist, direction.GetVector3() - direction.GetPerpendicularLeft(), (index.Position + new Vector3d(0.5, 0.5, 0.5)) + direction.GetVector3() * 0.6); dist += Lighting.GetRayIntersectDistance(maxDist, direction.GetVector3() + direction.GetPerpendicularRight() + direction.GetPerpendicularLeft(), (index.Position + new Vector3d(0.5, 0.5, 0.5)) + direction.GetVector3() * 0.6); dist += Lighting.GetRayIntersectDistance(maxDist, direction.GetVector3() + direction.GetPerpendicularRight() - direction.GetPerpendicularLeft(), (index.Position + new Vector3d(0.5, 0.5, 0.5)) + direction.GetVector3() * 0.6); dist += Lighting.GetRayIntersectDistance(maxDist, direction.GetVector3() - direction.GetPerpendicularRight() + direction.GetPerpendicularLeft(), (index.Position + new Vector3d(0.5, 0.5, 0.5)) + direction.GetVector3() * 0.6); dist += Lighting.GetRayIntersectDistance(maxDist, direction.GetVector3() - direction.GetPerpendicularRight() - direction.GetPerpendicularLeft(), (index.Position + new Vector3d(0.5, 0.5, 0.5)) + direction.GetVector3() * 0.6); dist /= 9; byte baseShade = (byte)Math.Ceiling(Math.Sqrt(Math.Pow(maxDist, 2)*3)); baseShade -= (byte)(dist / 21 * 10); return baseShade; } else { return 0; } }
public static void SetBlock(BlockIndex index, Block block) { Chunk chunk = GetChunk(new ChunkIndex(index)); if (chunk == null) { throw new Exception("You stupid asshole!"); } chunk[EuclideanModulo(index.X, Constants.World.ChunkSize), EuclideanModulo(index.Y, Constants.World.ChunkSize), EuclideanModulo(index.Z, Constants.World.ChunkSize)] = block; ChunkIndex? actualIndex = GetArrayIndex(chunk.Index); if (actualIndex.HasValue) { GL.BindTexture(TextureTarget.Texture3D, DataID); GL.TexSubImage3D(TextureTarget.Texture3D, 0, actualIndex.Value.X * Constants.World.ChunkSize + EuclideanModulo(index.X, Constants.World.ChunkSize), actualIndex.Value.Y * Constants.World.ChunkSize + EuclideanModulo(index.Y, Constants.World.ChunkSize), actualIndex.Value.Z * Constants.World.ChunkSize + EuclideanModulo(index.Z, Constants.World.ChunkSize), 1, 1, 1, OpenTK.Graphics.OpenGL.PixelFormat.Luminance, PixelType.UnsignedByte, new byte[,,] { { { block.ByteValue } } }); } }
public static Block GetBlock(BlockIndex index) { Chunk chunk = GetChunk(new ChunkIndex(index)); if (chunk == null) { return Block.Vacuum; } return chunk[EuclideanModulo(index.X, Constants.World.ChunkSize), EuclideanModulo(index.Y, Constants.World.ChunkSize), EuclideanModulo(index.Z, Constants.World.ChunkSize)]; }
public void SetBlock(BlockIndex index, Block value, bool updateAdjacentChunks) { if (index.X >= 0 && index.X < Constants.World.ChunkSize && index.Y >= 0 && index.Y < Constants.World.ChunkSize && index.Z >= 0 && index.Z < Constants.World.ChunkSize) { Data[index.Z, index.Y, index.X] = value; if (updateAdjacentChunks) { if (index.X == 0) { Chunk chunk = ChunkManager.GetChunk(Index - ChunkIndex.UnitX); } else if (index.X == Constants.World.ChunkSize - 1) { Chunk chunk = ChunkManager.GetChunk(Index + ChunkIndex.UnitX); } if (index.Y == 0) { Chunk chunk = ChunkManager.GetChunk(Index - ChunkIndex.UnitY); } else if (index.Y == Constants.World.ChunkSize - 1) { Chunk chunk = ChunkManager.GetChunk(Index + ChunkIndex.UnitY); } if (index.Z == 0) { Chunk chunk = ChunkManager.GetChunk(Index - ChunkIndex.UnitZ); } else if (index.Z == Constants.World.ChunkSize - 1) { Chunk chunk = ChunkManager.GetChunk(Index + ChunkIndex.UnitZ); } } } }
public ChunkIndex(BlockIndex index) { X = (int)Math.Floor((float)index.X / 32); Y = (int)Math.Floor((float)index.Y / 32); Z = (int)Math.Floor((float)index.Z / 32); }
private static BlockIndex GetCursorIntersect(double maxReach, Vector3d direction, Vector3d startPosition, out Vector3d intersectionPoint) { Ray ray = new Ray(startPosition, direction); double distance = 0.0; BlockIndex index; double? intersect; while (distance <= Constants.Player.BlockEditing.Reach) { index = new BlockIndex(direction * distance + startPosition); intersect = index.GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index).Solidity) { intersectionPoint = intersect.Value * direction + startPosition; return index; } intersect = (index + BlockIndex.UnitX).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitX).Solidity) { intersectionPoint = intersect.Value * direction + startPosition; return index + BlockIndex.UnitX; } intersect = (index - BlockIndex.UnitX).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitX).Solidity) { intersectionPoint = intersect.Value * direction + startPosition; return index - BlockIndex.UnitX; } intersect = (index + BlockIndex.UnitY).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitY).Solidity) { intersectionPoint = intersect.Value * direction + startPosition; return index + BlockIndex.UnitY; } intersect = (index - BlockIndex.UnitY).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitY).Solidity) { intersectionPoint = intersect.Value * direction + startPosition; return index - BlockIndex.UnitY; } intersect = (index + BlockIndex.UnitZ).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index + BlockIndex.UnitZ).Solidity) { intersectionPoint = intersect.Value * direction + startPosition; return index + BlockIndex.UnitZ; } intersect = (index - BlockIndex.UnitZ).GetBoundingBox().Intersects(ray); if (intersect.HasValue && ChunkManager.GetBlock(index - BlockIndex.UnitZ).Solidity) { intersectionPoint = intersect.Value * direction + startPosition; return index - BlockIndex.UnitZ; } distance += 0.5F; } intersectionPoint = Vector3d.Zero; return null; }
public static int GetLandscapeHeight(int x, int y) { ChunkIndex i = new BlockIndex(x, 0, y).ToChunkIndex(); return GetHeightMap(i)[x - i.ToBlockIndex().X, y - i.ToBlockIndex().Z]; }