using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinecraftClient.Mapping
{
///
/// Represents a Minecraft World
///
public class World
{
///
/// The chunks contained into the Minecraft world
///
private Dictionary> chunks = new Dictionary>();
///
/// Read, set or unload the specified chunk column
///
/// ChunkColumn X
/// ChunkColumn Y
/// chunk at the given location
public ChunkColumn this[int chunkX, int chunkZ]
{
get
{
//Read a chunk
if (chunks.ContainsKey(chunkX))
if (chunks[chunkX].ContainsKey(chunkZ))
return chunks[chunkX][chunkZ];
return null;
}
set
{
if (value != null)
{
//Update a chunk column
if (!chunks.ContainsKey(chunkX))
chunks[chunkX] = new Dictionary();
chunks[chunkX][chunkZ] = value;
}
else
{
//Unload a chunk column
if (chunks.ContainsKey(chunkX))
{
if (chunks[chunkX].ContainsKey(chunkZ))
{
chunks[chunkX].Remove(chunkZ);
if (chunks[chunkX].Count == 0)
chunks.Remove(chunkX);
}
}
}
}
}
///
/// Get chunk column at the specified location
///
/// Location to retrieve chunk column
/// The chunk column
public ChunkColumn GetChunkColumn(Location location)
{
return this[location.ChunkX, location.ChunkZ];
}
///
/// Get block at the specified location
///
/// Location to retrieve block from
/// Block at specified location or Air if the location is not loaded
public Block GetBlock(Location location)
{
ChunkColumn column = GetChunkColumn(location);
if (column != null)
{
Chunk chunk = column.GetChunk(location);
if (chunk != null)
return chunk.GetBlock(location);
}
return new Block(0); //Air
}
///
/// Set block at the specified location
///
/// Location to set block to
/// Block to set
public void SetBlock(Location location, Block block)
{
ChunkColumn column = this[location.ChunkX, location.ChunkZ];
if (column != null)
{
Chunk chunk = column[location.ChunkY];
if (chunk == null)
column[location.ChunkY] = chunk = new Chunk();
chunk[location.ChunkBlockX, location.ChunkBlockY, location.ChunkBlockZ] = block;
}
}
///
/// Clear all terrain data from the world
///
public void Clear()
{
chunks = new Dictionary>();
}
}
}