Bunch of fixes to Anvil, still completely broken.

This commit is contained in:
Justin Aquadro 2012-03-12 02:10:41 -04:00
parent f82ea3e946
commit c2956a7d2a
5 changed files with 188 additions and 57 deletions

View file

@ -26,7 +26,7 @@ namespace Substrate
private IDataArray3 _blockLight; private IDataArray3 _blockLight;
private IDataArray3 _skyLight; private IDataArray3 _skyLight;
private ZXByteArray _heightMap; private IDataArray2 _heightMap;
private TagNodeList _tileEntities; private TagNodeList _tileEntities;
private TagNodeList _tileTicks; private TagNodeList _tileTicks;
@ -80,7 +80,7 @@ namespace Substrate
IDataArray3 data, IDataArray3 data,
IDataArray3 blockLight, IDataArray3 blockLight,
IDataArray3 skyLight, IDataArray3 skyLight,
ZXByteArray heightMap, IDataArray2 heightMap,
TagNodeList tileEntities) TagNodeList tileEntities)
: this(blocks, data, blockLight, skyLight, heightMap, tileEntities, null) : this(blocks, data, blockLight, skyLight, heightMap, tileEntities, null)
{ {
@ -101,7 +101,7 @@ namespace Substrate
IDataArray3 data, IDataArray3 data,
IDataArray3 blockLight, IDataArray3 blockLight,
IDataArray3 skyLight, IDataArray3 skyLight,
ZXByteArray heightMap, IDataArray2 heightMap,
TagNodeList tileEntities, TagNodeList tileEntities,
TagNodeList tileTicks) TagNodeList tileTicks)
{ {

View file

@ -5,7 +5,7 @@ using Substrate.Nbt;
using Substrate.Core; using Substrate.Core;
using System.IO; using System.IO;
namespace Substrate.Source namespace Substrate
{ {
public class AnvilSection : INbtObject<AnvilSection>, ICopyable<AnvilSection> public class AnvilSection : INbtObject<AnvilSection>, ICopyable<AnvilSection>
{ {
@ -15,7 +15,7 @@ namespace Substrate.Source
new SchemaNodeArray("Data", 2048), new SchemaNodeArray("Data", 2048),
new SchemaNodeArray("SkyLight", 2048), new SchemaNodeArray("SkyLight", 2048),
new SchemaNodeArray("BlockLight", 2048), new SchemaNodeArray("BlockLight", 2048),
new SchemaNodeScaler("Y", TagType.TAG_INT), new SchemaNodeScaler("Y", TagType.TAG_BYTE),
new SchemaNodeArray("AddBlocks", 2048, SchemaOptions.OPTIONAL), new SchemaNodeArray("AddBlocks", 2048, SchemaOptions.OPTIONAL),
}; };
@ -28,7 +28,7 @@ namespace Substrate.Source
private TagNodeCompound _tree; private TagNodeCompound _tree;
private int _y; private byte _y;
private YZXByteArray _blocks; private YZXByteArray _blocks;
private YZXNibbleArray _data; private YZXNibbleArray _data;
private YZXNibbleArray _blockLight; private YZXNibbleArray _blockLight;
@ -44,13 +44,13 @@ namespace Substrate.Source
if (y < MIN_Y || y > MAX_Y) if (y < MIN_Y || y > MAX_Y)
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
_y = y; _y = (byte)y;
BuildNbtTree(); BuildNbtTree();
} }
public AnvilSection (TagNodeCompound tree) public AnvilSection (TagNodeCompound tree)
{ {
LoadTree(_tree); LoadTree(tree);
} }
public int Y public int Y
@ -61,8 +61,8 @@ namespace Substrate.Source
if (value < MIN_Y || value > MAX_Y) if (value < MIN_Y || value > MAX_Y)
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
_y = value; _y = (byte)value;
_tree["Y"].ToTagInt().Data = _y; _tree["Y"].ToTagByte().Data = _y;
} }
} }
@ -122,7 +122,7 @@ namespace Substrate.Source
return null; return null;
} }
_y = ctree["Y"] as TagNodeInt; _y = ctree["Y"] as TagNodeByte;
_blocks = new YZXByteArray(XDIM, YDIM, ZDIM, ctree["Blocks"] as TagNodeByteArray); _blocks = new YZXByteArray(XDIM, YDIM, ZDIM, ctree["Blocks"] as TagNodeByteArray);
_data = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["Data"] as TagNodeByteArray); _data = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["Data"] as TagNodeByteArray);
@ -130,7 +130,7 @@ namespace Substrate.Source
_blockLight = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["BlockLight"] as TagNodeByteArray); _blockLight = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["BlockLight"] as TagNodeByteArray);
if (!ctree.ContainsKey("AddBlocks")) if (!ctree.ContainsKey("AddBlocks"))
_tree["AddBlocks"] = new TagNodeByteArray(new byte[2048]); _ctree["AddBlocks"] = new TagNodeByteArray(new byte[2048]);
_addBlocks = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["AddBlocks"] as TagNodeByteArray); _addBlocks = new YZXNibbleArray(XDIM, YDIM, ZDIM, ctree["AddBlocks"] as TagNodeByteArray);
return this; return this;
@ -193,7 +193,7 @@ namespace Substrate.Source
_addBlocks = new YZXNibbleArray(XDIM, YDIM, ZDIM, addBlocks); _addBlocks = new YZXNibbleArray(XDIM, YDIM, ZDIM, addBlocks);
TagNodeCompound tree = new TagNodeCompound(); TagNodeCompound tree = new TagNodeCompound();
tree.Add("Y", new TagNodeInt(_y)); tree.Add("Y", new TagNodeByte(_y));
tree.Add("Blocks", blocks); tree.Add("Blocks", blocks);
tree.Add("Data", data); tree.Add("Data", data);
tree.Add("SkyLight", skyLight); tree.Add("SkyLight", skyLight);
@ -229,7 +229,7 @@ namespace Substrate.Source
#region IByteArray3 Members #region IByteArray3 Members
public byte this[int x, int y, int z] public int this[int x, int y, int z]
{ {
get get
{ {
@ -279,7 +279,7 @@ namespace Substrate.Source
#region IByteArray Members #region IByteArray Members
public byte this[int i] public int this[int i]
{ {
get get
{ {
@ -332,7 +332,7 @@ namespace Substrate.Source
#region IByteArray3 Members #region IByteArray3 Members
public byte this[int x, int y, int z] public int this[int x, int y, int z]
{ {
get get
{ {
@ -345,7 +345,7 @@ namespace Substrate.Source
{ {
int ydiv = y / _sections[0].YDim; int ydiv = y / _sections[0].YDim;
int yrem = y - (ydiv * _sections[0].YDim); int yrem = y - (ydiv * _sections[0].YDim);
_sections[ydiv][x, yrem, z] = value; _sections[ydiv][x, yrem, z] = (byte)value;
} }
} }
@ -382,7 +382,7 @@ namespace Substrate.Source
#region IByteArray Members #region IByteArray Members
public byte this[int i] public int this[int i]
{ {
get get
{ {
@ -394,7 +394,7 @@ namespace Substrate.Source
{ {
int idiv = i / _sections[0].Length; int idiv = i / _sections[0].Length;
int irem = i - (idiv * _sections[0].Length); int irem = i - (idiv * _sections[0].Length);
_sections[idiv][irem] = value; _sections[idiv][irem] = (byte)value;
} }
} }
@ -412,7 +412,7 @@ namespace Substrate.Source
#endregion #endregion
} }
public class AnvilChunk : IChunk, INbtObject<AnvilChunk>, ICopyable<AnvilChunk> public class Chunk : IChunk, INbtObject<Chunk>, ICopyable<Chunk>
{ {
public static SchemaNodeCompound LevelSchema = new SchemaNodeCompound() public static SchemaNodeCompound LevelSchema = new SchemaNodeCompound()
{ {
@ -423,11 +423,11 @@ namespace Substrate.Source
new SchemaNodeArray("Data", 2048), new SchemaNodeArray("Data", 2048),
new SchemaNodeArray("SkyLight", 2048), new SchemaNodeArray("SkyLight", 2048),
new SchemaNodeArray("BlockLight", 2048), new SchemaNodeArray("BlockLight", 2048),
new SchemaNodeScaler("Y", TagType.TAG_INT), new SchemaNodeScaler("Y", TagType.TAG_BYTE),
new SchemaNodeArray("AddBlocks", 2048, SchemaOptions.OPTIONAL), new SchemaNodeArray("AddBlocks", 2048, SchemaOptions.OPTIONAL),
}), }),
new SchemaNodeArray("Biomes", 256), new SchemaNodeArray("Biomes", 256),
new SchemaNodeArray("HeightMap", 256), new SchemaNodeIntArray("HeightMap", 256),
new SchemaNodeList("Entities", TagType.TAG_COMPOUND, SchemaOptions.CREATE_ON_MISSING), new SchemaNodeList("Entities", TagType.TAG_COMPOUND, SchemaOptions.CREATE_ON_MISSING),
new SchemaNodeList("TileEntities", TagType.TAG_COMPOUND, TileEntity.Schema, SchemaOptions.CREATE_ON_MISSING), new SchemaNodeList("TileEntities", TagType.TAG_COMPOUND, TileEntity.Schema, SchemaOptions.CREATE_ON_MISSING),
new SchemaNodeList("TileTicks", TagType.TAG_COMPOUND, TileTick.Schema, SchemaOptions.OPTIONAL), new SchemaNodeList("TileTicks", TagType.TAG_COMPOUND, TileTick.Schema, SchemaOptions.OPTIONAL),
@ -454,7 +454,7 @@ namespace Substrate.Source
private IDataArray3 _blockLight; private IDataArray3 _blockLight;
private IDataArray3 _skyLight; private IDataArray3 _skyLight;
private ZXByteArray _heightMap; private ZXIntArray _heightMap;
private TagNodeList _entities; private TagNodeList _entities;
private TagNodeList _tileEntities; private TagNodeList _tileEntities;
@ -464,7 +464,7 @@ namespace Substrate.Source
private EntityCollection _entityManager; private EntityCollection _entityManager;
private AnvilChunk () private Chunk ()
{ {
_sections = new AnvilSection[16]; _sections = new AnvilSection[16];
} }
@ -500,9 +500,9 @@ namespace Substrate.Source
set { _tree.Root["Level"].ToTagCompound()["TerrainPopulated"].ToTagByte().Data = (byte)(value ? 1 : 0); } set { _tree.Root["Level"].ToTagCompound()["TerrainPopulated"].ToTagByte().Data = (byte)(value ? 1 : 0); }
} }
public static AnvilChunk Create (int x, int z) public static Chunk Create (int x, int z)
{ {
AnvilChunk c = new AnvilChunk(); Chunk c = new Chunk();
c._cx = x; c._cx = x;
c._cz = z; c._cz = z;
@ -511,16 +511,16 @@ namespace Substrate.Source
return c; return c;
} }
public static AnvilChunk Create (NbtTree tree) public static Chunk Create (NbtTree tree)
{ {
AnvilChunk c = new AnvilChunk(); Chunk c = new Chunk();
return c.LoadTree(tree.Root); return c.LoadTree(tree.Root);
} }
public static AnvilChunk CreateVerified (NbtTree tree) public static Chunk CreateVerified (NbtTree tree)
{ {
AnvilChunk c = new AnvilChunk(); Chunk c = new Chunk();
return c.LoadTreeSafe(tree.Root); return c.LoadTreeSafe(tree.Root);
} }
@ -612,7 +612,7 @@ namespace Substrate.Source
#region INbtObject<AnvilChunk> Members #region INbtObject<AnvilChunk> Members
public AnvilChunk LoadTree (TagNode tree) public Chunk LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null) { if (ctree == null) {
@ -623,8 +623,8 @@ namespace Substrate.Source
TagNodeCompound level = _tree.Root["Level"] as TagNodeCompound; TagNodeCompound level = _tree.Root["Level"] as TagNodeCompound;
TagNodeCompound sections = level["Sections"] as TagNodeCompound; TagNodeList sections = level["Sections"] as TagNodeList;
foreach (TagNodeCompound section in sections.Values) { foreach (TagNodeCompound section in sections) {
AnvilSection anvilSection = new AnvilSection(section); AnvilSection anvilSection = new AnvilSection(section);
if (anvilSection.Y < 0 || anvilSection.Y >= _sections.Length) if (anvilSection.Y < 0 || anvilSection.Y >= _sections.Length)
continue; continue;
@ -651,7 +651,7 @@ namespace Substrate.Source
_skyLight = new CompositeYZXNibbleArray(skyLightBA); _skyLight = new CompositeYZXNibbleArray(skyLightBA);
_blockLight = new CompositeYZXNibbleArray(blockLightBA); _blockLight = new CompositeYZXNibbleArray(blockLightBA);
_heightMap = new ZXByteArray(XDIM, ZDIM, level["HeightMap"] as TagNodeByteArray); _heightMap = new ZXIntArray(XDIM, ZDIM, level["HeightMap"] as TagNodeIntArray);
_entities = level["Entities"] as TagNodeList; _entities = level["Entities"] as TagNodeList;
_tileEntities = level["TileEntities"] as TagNodeList; _tileEntities = level["TileEntities"] as TagNodeList;
@ -686,7 +686,7 @@ namespace Substrate.Source
return this; return this;
} }
public AnvilChunk LoadTreeSafe (TagNode tree) public Chunk LoadTreeSafe (TagNode tree)
{ {
if (!ValidateTree(tree)) { if (!ValidateTree(tree)) {
return null; return null;
@ -712,9 +712,9 @@ namespace Substrate.Source
#region ICopyable<AnvilChunk> Members #region ICopyable<AnvilChunk> Members
public AnvilChunk Copy () public Chunk Copy ()
{ {
return AnvilChunk.Create(_tree.Copy()); return Chunk.Create(_tree.Copy());
} }
#endregion #endregion
@ -740,8 +740,25 @@ namespace Substrate.Source
sections.Add(_sections[i].BuildTree()); sections.Add(_sections[i].BuildTree());
} }
TagNodeByteArray heightMap = new TagNodeByteArray(new byte[elements2]); YZXByteArray[] blocksBA = new YZXByteArray[_sections.Length];
_heightMap = new ZXByteArray(XDIM, ZDIM, heightMap); YZXNibbleArray[] dataBA = new YZXNibbleArray[_sections.Length];
YZXNibbleArray[] skyLightBA = new YZXNibbleArray[_sections.Length];
YZXNibbleArray[] blockLightBA = new YZXNibbleArray[_sections.Length];
for (int i = 0; i < _sections.Length; i++) {
blocksBA[i] = _sections[i].Blocks;
dataBA[i] = _sections[i].Data;
skyLightBA[i] = _sections[i].SkyLight;
blockLightBA[i] = _sections[i].BlockLight;
}
_blocks = new CompositeYZXByteArray(blocksBA);
_data = new CompositeYZXNibbleArray(dataBA);
_skyLight = new CompositeYZXNibbleArray(skyLightBA);
_blockLight = new CompositeYZXNibbleArray(blockLightBA);
TagNodeIntArray heightMap = new TagNodeIntArray(new int[elements2]);
_heightMap = new ZXIntArray(XDIM, ZDIM, heightMap);
_entities = new TagNodeList(TagType.TAG_COMPOUND); _entities = new TagNodeList(TagType.TAG_COMPOUND);
_tileEntities = new TagNodeList(TagType.TAG_COMPOUND); _tileEntities = new TagNodeList(TagType.TAG_COMPOUND);

View file

@ -6,15 +6,23 @@ namespace Substrate.Core
{ {
public interface IDataArray public interface IDataArray
{ {
byte this[int i] { get; set; } int this[int i] { get; set; }
int Length { get; } int Length { get; }
void Clear (); void Clear ();
} }
public interface IDataArray2 : IDataArray
{
int this[int x, int z] { get; set; }
int XDim { get; }
int ZDim { get; }
}
public interface IDataArray3 : IDataArray public interface IDataArray3 : IDataArray
{ {
byte this[int x, int y, int z] { get; set; } int this[int x, int y, int z] { get; set; }
int XDim { get; } int XDim { get; }
int YDim { get; } int YDim { get; }
@ -38,10 +46,10 @@ namespace Substrate.Core
dataArray = data; dataArray = data;
} }
public byte this[int i] public int this[int i]
{ {
get { return dataArray[i]; } get { return dataArray[i]; }
set { dataArray[i] = value; } set { dataArray[i] = (byte)value; }
} }
public int Length public int Length
@ -97,7 +105,7 @@ namespace Substrate.Core
} }
} }
public byte this[int x, int y, int z] public int this[int x, int y, int z]
{ {
get get
{ {
@ -108,7 +116,7 @@ namespace Substrate.Core
set set
{ {
int index = _ydim * (x * _zdim + z) + y; int index = _ydim * (x * _zdim + z) + y;
dataArray[index] = value; dataArray[index] = (byte)value;
} }
} }
@ -181,7 +189,7 @@ namespace Substrate.Core
} }
} }
public byte this[int x, int y, int z] public int this[int x, int y, int z]
{ {
get get
{ {
@ -192,7 +200,7 @@ namespace Substrate.Core
set set
{ {
int index = _xdim * (y * _zdim + z) + x; int index = _xdim * (y * _zdim + z) + x;
dataArray[index] = value; dataArray[index] = (byte)value;
} }
} }
@ -239,7 +247,7 @@ namespace Substrate.Core
#endregion #endregion
} }
public sealed class ZXByteArray : ByteArray public sealed class ZXByteArray : ByteArray, IDataArray2
{ {
private readonly int _xdim; private readonly int _xdim;
private readonly int _zdim; private readonly int _zdim;
@ -263,7 +271,113 @@ namespace Substrate.Core
} }
} }
public byte this[int x, int z] public int this[int x, int z]
{
get
{
int index = z * _xdim + x;
return dataArray[index];
}
set
{
int index = z * _xdim + x;
dataArray[index] = (byte)value;
}
}
public int XDim
{
get { return _xdim; }
}
public int ZDim
{
get { return _zdim; }
}
#region ICopyable<ZXByteArray> Members
public override ByteArray Copy ()
{
byte[] data = new byte[dataArray.Length];
dataArray.CopyTo(data, 0);
return new ZXByteArray(_xdim, _zdim, data);
}
#endregion
}
public class IntArray : IDataArray, ICopyable<IntArray>
{
protected readonly int[] dataArray;
public IntArray (int length)
{
dataArray = new int[length];
}
public IntArray (int[] data)
{
dataArray = data;
}
public int this[int i]
{
get { return dataArray[i]; }
set { dataArray[i] = value; }
}
public int Length
{
get { return dataArray.Length; }
}
public void Clear ()
{
for (int i = 0; i < dataArray.Length; i++) {
dataArray[i] = 0;
}
}
#region ICopyable<ByteArray> Members
public virtual IntArray Copy ()
{
int[] data = new int[dataArray.Length];
dataArray.CopyTo(data, 0);
return new IntArray(data);
}
#endregion
}
public sealed class ZXIntArray : IntArray, IDataArray2
{
private readonly int _xdim;
private readonly int _zdim;
public ZXIntArray (int xdim, int zdim)
: base(xdim * zdim)
{
_xdim = xdim;
_zdim = zdim;
}
public ZXIntArray (int xdim, int zdim, int[] data)
: base(data)
{
_xdim = xdim;
_zdim = zdim;
if (xdim * zdim != data.Length) {
throw new ArgumentException("Product of dimensions must equal length of data");
}
}
public int this[int x, int z]
{ {
get get
{ {
@ -290,12 +404,12 @@ namespace Substrate.Core
#region ICopyable<ZXByteArray> Members #region ICopyable<ZXByteArray> Members
public override ByteArray Copy () public override IntArray Copy ()
{ {
byte[] data = new byte[dataArray.Length]; int[] data = new int[dataArray.Length];
dataArray.CopyTo(data, 0); dataArray.CopyTo(data, 0);
return new ZXByteArray(_xdim, _zdim, data); return new ZXIntArray(_xdim, _zdim, data);
} }
#endregion #endregion

View file

@ -19,7 +19,7 @@ namespace Substrate.Core
_data = data; _data = data;
} }
public byte this[int index] public int this[int index]
{ {
get get
{ {
@ -109,7 +109,7 @@ namespace Substrate.Core
} }
} }
public byte this[int x, int y, int z] public int this[int x, int y, int z]
{ {
get get
{ {
@ -193,7 +193,7 @@ namespace Substrate.Core
} }
} }
public byte this[int x, int y, int z] public int this[int x, int y, int z]
{ {
get get
{ {

View file

@ -26,7 +26,7 @@ namespace Substrate
private RegionManager _regionMan; private RegionManager _regionMan;
private static Regex _namePattern = new Regex("r\\.(-?[0-9]+)\\.(-?[0-9]+)\\.mcr$"); private static Regex _namePattern = new Regex("r\\.(-?[0-9]+)\\.(-?[0-9]+)\\.mca$");
private WeakReference _regionFile; private WeakReference _regionFile;
@ -157,7 +157,7 @@ namespace Substrate
/// <returns>The filename of the region with encoded coordinates.</returns> /// <returns>The filename of the region with encoded coordinates.</returns>
public string GetFileName () public string GetFileName ()
{ {
return "r." + _rx + "." + _rz + ".mcr"; return "r." + _rx + "." + _rz + ".mca";
} }