using System;
using System.Runtime.CompilerServices;
namespace MinecraftClient.Mapping
{
///
/// Represent a chunk of terrain in a Minecraft world
///
public class Chunk
{
public const int SizeX = 16;
public const int SizeY = 16;
public const int SizeZ = 16;
///
/// Blocks contained into the chunk
///
private readonly Block[] blocks = new Block[SizeY * SizeZ * SizeX];
///
/// Read, or set the specified block
///
/// Block X
/// Block Y
/// Block Z
/// chunk at the given location
public Block this[int blockX, int blockY, int blockZ]
{
get
{
if (blockX < 0 || blockX >= SizeX)
throw new ArgumentOutOfRangeException(nameof(blockX), "Must be between 0 and " + (SizeX - 1) + " (inclusive)");
if (blockY < 0 || blockY >= SizeY)
throw new ArgumentOutOfRangeException(nameof(blockY), "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
if (blockZ < 0 || blockZ >= SizeZ)
throw new ArgumentOutOfRangeException(nameof(blockZ), "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
return blocks[(blockY << 8) | (blockZ << 4) | blockX];
}
set
{
if (blockX < 0 || blockX >= SizeX)
throw new ArgumentOutOfRangeException(nameof(blockX), "Must be between 0 and " + (SizeX - 1) + " (inclusive)");
if (blockY < 0 || blockY >= SizeY)
throw new ArgumentOutOfRangeException(nameof(blockY), "Must be between 0 and " + (SizeY - 1) + " (inclusive)");
if (blockZ < 0 || blockZ >= SizeZ)
throw new ArgumentOutOfRangeException(nameof(blockZ), "Must be between 0 and " + (SizeZ - 1) + " (inclusive)");
blocks[(blockY << 8) | (blockZ << 4) | blockX] = value;
}
}
///
/// Used when parsing chunks
///
/// Block X
/// Block Y
/// Block Z
/// Block
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public void SetWithoutCheck(int blockX, int blockY, int blockZ, Block block)
{
blocks[(blockY << 8) | (blockZ << 4) | blockX] = block;
}
///
/// Get block at the specified location
///
/// Location, a modulo will be applied
/// The block
public Block GetBlock(Location location)
{
return this[location.ChunkBlockX, location.ChunkBlockY, location.ChunkBlockZ];
}
}
}