Documentation

This commit is contained in:
Justin Aquadro 2011-06-18 03:01:39 +00:00
parent 79cd9afa7b
commit 0fefa0d640
2 changed files with 81 additions and 2 deletions

View file

@ -7,12 +7,21 @@ namespace Substrate
using NBT; using NBT;
using Utility; using Utility;
/// <summary>
/// A Minecraft Alpha-compatible chunk data structure.
/// </summary>
/// <remarks>
/// A Chunk internally wraps an NBT_Tree of raw chunk data. Modifying the chunk will update the tree, and vice-versa.
/// </remarks>
public class Chunk : IChunk, INBTObject<Chunk>, ICopyable<Chunk> public class Chunk : IChunk, INBTObject<Chunk>, ICopyable<Chunk>
{ {
private const int XDIM = 16; private const int XDIM = 16;
private const int YDIM = 128; private const int YDIM = 128;
private const int ZDIM = 16; private const int ZDIM = 16;
/// <summary>
/// An NBT Schema definition for valid chunk data.
/// </summary>
public static NBTCompoundNode LevelSchema = new NBTCompoundNode() public static NBTCompoundNode LevelSchema = new NBTCompoundNode()
{ {
new NBTCompoundNode("Level") new NBTCompoundNode("Level")
@ -48,41 +57,66 @@ namespace Substrate
private AlphaBlockCollection _blockManager; private AlphaBlockCollection _blockManager;
private EntityCollection _entityManager; private EntityCollection _entityManager;
/// <summary>
/// Gets the global X-coordinate of the chunk.
/// </summary>
public int X public int X
{ {
get { return _cx; } get { return _cx; }
} }
/// <summary>
/// Gets the global Z-coordinate of the chunk.
/// </summary>
public int Z public int Z
{ {
get { return _cz; } get { return _cz; }
} }
/// <summary>
/// Gets the collection of all blocks and their data stored in the chunk.
/// </summary>
public AlphaBlockCollection Blocks public AlphaBlockCollection Blocks
{ {
get { return _blockManager; } get { return _blockManager; }
} }
/// <summary>
/// Gets the collection of all entities stored in the chunk.
/// </summary>
public EntityCollection Entities public EntityCollection Entities
{ {
get { return _entityManager; } get { return _entityManager; }
} }
/// <summary>
/// Provides raw access to the underlying NBT_Tree.
/// </summary>
public NBT_Tree Tree public NBT_Tree Tree
{ {
get { return _tree; } get { return _tree; }
} }
/// <summary>
/// Gets or sets the chunk's TerrainPopulated status.
/// </summary>
public bool IsTerrainPopulated public bool IsTerrainPopulated
{ {
get { return _tree.Root["Level"].ToTagCompound()["TerrainPopulated"].ToTagByte() == 1; } get { return _tree.Root["Level"].ToTagCompound()["TerrainPopulated"].ToTagByte() == 1; }
set { _tree.Root["Level"].ToTagCompound()["TerrainPopulated"].ToTagByte().Data = (byte)(value ? 1 : 0); } set { _tree.Root["Level"].ToTagCompound()["TerrainPopulated"].ToTagByte().Data = (byte)(value ? 1 : 0); }
} }
private Chunk () private Chunk ()
{ {
} }
/// <summary>
/// Creates a default (empty) chunk.
/// </summary>
/// <param name="x">Global X-coordinate of the chunk.</param>
/// <param name="z">Global Z-coordinate of the chunk.</param>
/// <returns>A new Chunk object.</returns>
public static Chunk Create (int x, int z) public static Chunk Create (int x, int z)
{ {
Chunk c = new Chunk(); Chunk c = new Chunk();
@ -95,6 +129,11 @@ namespace Substrate
return c; return c;
} }
/// <summary>
/// Creates a chunk object from an existing NBT_Tree.
/// </summary>
/// <param name="tree">An NBT_Tree conforming to the chunk schema definition.</param>
/// <returns>A new Chunk object wrapping an existing NBT_Tree.</returns>
public static Chunk Create (NBT_Tree tree) public static Chunk Create (NBT_Tree tree)
{ {
Chunk c = new Chunk(); Chunk c = new Chunk();
@ -102,6 +141,11 @@ namespace Substrate
return c.LoadTree(tree.Root); return c.LoadTree(tree.Root);
} }
/// <summary>
/// Creates a chunk object from a verified NBT_Tree.
/// </summary>
/// <param name="tree">An NBT_Tree conforming to the chunk schema definition.</param>
/// <returns>A new Chunk object wrapping an existing NBT_Tree, or null on verification failure.</returns>
public static Chunk CreateVerified (NBT_Tree tree) public static Chunk CreateVerified (NBT_Tree tree)
{ {
Chunk c = new Chunk(); Chunk c = new Chunk();
@ -109,12 +153,22 @@ namespace Substrate
return c.LoadTreeSafe(tree.Root); return c.LoadTreeSafe(tree.Root);
} }
/// <summary>
/// Updates the chunk's global world coordinates.
/// </summary>
/// <param name="x">Global X-coordinate.</param>
/// <param name="z">Global Z-coordinate.</param>
public virtual void SetLocation (int x, int z) public virtual void SetLocation (int x, int z)
{ {
_cx = x; _cx = x;
_cz = z; _cz = z;
} }
/// <summary>
/// Saves a Chunk's underlying NBT_Tree to an output stream.
/// </summary>
/// <param name="outStream">An open, writable output stream.</param>
/// <returns>True if the data is written out to the stream.</returns>
public bool Save (Stream outStream) public bool Save (Stream outStream)
{ {
if (outStream == null || !outStream.CanWrite) { if (outStream == null || !outStream.CanWrite) {
@ -130,6 +184,11 @@ namespace Substrate
#region INBTObject<Chunk> Members #region INBTObject<Chunk> Members
/// <summary>
/// Loads the Chunk from an NBT tree rooted at the given TagValue node.
/// </summary>
/// <param name="tree">Root node of an NBT tree.</param>
/// <returns>A reference to the current Chunk, or null if the tree is unparsable.</returns>
public Chunk LoadTree (TagValue tree) public Chunk LoadTree (TagValue tree)
{ {
TagCompound ctree = tree as TagCompound; TagCompound ctree = tree as TagCompound;
@ -176,6 +235,11 @@ namespace Substrate
return this; return this;
} }
/// <summary>
/// Loads the Chunk from a validated NBT tree rooted at the given TagValue node.
/// </summary>
/// <param name="tree">Root node of an NBT tree.</param>
/// <returns>A reference to the current Chunk, or null if the tree does not conform to the chunk's NBT Schema definition.</returns>
public Chunk LoadTreeSafe (TagValue tree) public Chunk LoadTreeSafe (TagValue tree)
{ {
if (!ValidateTree(tree)) { if (!ValidateTree(tree)) {
@ -185,11 +249,20 @@ namespace Substrate
return LoadTree(tree); return LoadTree(tree);
} }
/// <summary>
/// Gets a valid NBT tree representing the Chunk.
/// </summary>
/// <returns>The root node of the Chunk's NBT tree.</returns>
public TagValue BuildTree () public TagValue BuildTree ()
{ {
return _tree.Root; return _tree.Root;
} }
/// <summary>
/// Validates an NBT tree against the chunk's NBT schema definition.
/// </summary>
/// <param name="tree">The root node of the NBT tree to verify.</param>
/// <returns>Status indicating if the tree represents a valid chunk.</returns>
public bool ValidateTree (TagValue tree) public bool ValidateTree (TagValue tree)
{ {
NBTVerifier v = new NBTVerifier(tree, LevelSchema); NBTVerifier v = new NBTVerifier(tree, LevelSchema);
@ -201,6 +274,10 @@ namespace Substrate
#region ICopyable<Chunk> Members #region ICopyable<Chunk> Members
/// <summary>
/// Creates a deep copy of the Chunk and its underlying NBT tree.
/// </summary>
/// <returns>A new Chunk with copied data.</returns>
public Chunk Copy () public Chunk Copy ()
{ {
return Chunk.Create(_tree.Copy()); return Chunk.Create(_tree.Copy());

View file

@ -5,9 +5,11 @@ using System.Collections.Generic;
namespace Substrate namespace Substrate
{ {
/// <summary> /// <summary>
/// Provides a wrapper around a physical Chunk stored in a chunk container. Modifying data in a ChunkRef will signal to the chunk /// Provides a wrapper around a physical Chunk stored in a chunk container.
/// container that the physical chunk needs to be saved.
/// </summary> /// </summary>
/// <remarks>
/// Modifying data in a ChunkRef will signal to the chunk container that the physical chunk needs to be saved.
/// </remarks>
public class ChunkRef : IChunk public class ChunkRef : IChunk
{ {
private IChunkContainer _container; private IChunkContainer _container;