From 7d1833e6f00b0134ac393a2c6ffc352a85b160af Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Fri, 10 Jun 2011 16:08:36 +0000 Subject: [PATCH] Some tweaks to help the JIT out. Changes to BlockRef but this will probably be removed entirely. --- .../Source/AlphaBlockCollection.cs | 142 +++++++++++++++++- .../SubstrateCS/Source/BlockInterface.cs | 8 + Substrate/SubstrateCS/Source/BlockRef.cs | 101 +++++++++++++ Substrate/SubstrateCS/Source/ChunkRef.cs | 16 +- 4 files changed, 261 insertions(+), 6 deletions(-) diff --git a/Substrate/SubstrateCS/Source/AlphaBlockCollection.cs b/Substrate/SubstrateCS/Source/AlphaBlockCollection.cs index 0056313..6f7dd91 100644 --- a/Substrate/SubstrateCS/Source/AlphaBlockCollection.cs +++ b/Substrate/SubstrateCS/Source/AlphaBlockCollection.cs @@ -46,8 +46,12 @@ namespace Substrate } } - public class AlphaBlockCollection : IBoundedAlphaBlockCollection + public class AlphaBlockCollection : IBoundedAlphaBlockCollection, IEnumerable { + private readonly int _xdim; + private readonly int _ydim; + private readonly int _zdim; + private XZYByteArray _blocks; private XZYNibbleArray _data; private XZYNibbleArray _blockLight; @@ -122,6 +126,10 @@ namespace Substrate _lightManager = new BlockLight(this); _fluidManager = new BlockFluid(this); _tileEntityManager = new BlockTileEntities(_blocks, _tileEntities); + + _xdim = _blocks.XDim; + _ydim = _blocks.YDim; + _zdim = _blocks.ZDim; } public Block GetBlock (int x, int y, int z) @@ -146,17 +154,17 @@ namespace Substrate public int XDim { - get { return _blocks.XDim; } + get { return _xdim; } } public int YDim { - get { return _blocks.YDim; } + get { return _ydim; } } public int ZDim { - get { return _blocks.ZDim; } + get { return _zdim; } } IBlock IBlockCollection.GetBlock (int x, int y, int z) @@ -179,11 +187,21 @@ namespace Substrate return BlockInfo.BlockTable[_blocks[x, y, z]]; } + internal BlockInfo GetInfo (int index) + { + return BlockInfo.BlockTable[_blocks[index]]; + } + public int GetID (int x, int y, int z) { return _blocks[x, y, z]; } + internal int GetID (int index) + { + return _blocks[index]; + } + public void SetID (int x, int y, int z, int id) { int oldid = _blocks[x, y, z]; @@ -278,6 +296,11 @@ namespace Substrate return _data[x, y, z]; } + internal int GetData (int index) + { + return _data[index]; + } + public void SetData (int x, int y, int z, int data) { if (_data[x, y, z] != data) { @@ -292,6 +315,14 @@ namespace Substrate }*/ } + internal void SetData (int index, int data) + { + if (_data[index] != data) { + _data[index] = (byte)data; + _dirty = true; + } + } + public int CountByData (int id, int data) { int c = 0; @@ -331,11 +362,21 @@ namespace Substrate return _blockLight[x, y, z]; } + internal int GetBlockLight (int index) + { + return _blockLight[index]; + } + public int GetSkyLight (int x, int y, int z) { return _skyLight[x, y, z]; } + internal int GetSkyLight (int index) + { + return _skyLight[index]; + } + public void SetBlockLight (int x, int y, int z, int light) { if (_blockLight[x, y, z] != light) { @@ -344,6 +385,14 @@ namespace Substrate } } + internal void SetBlockLight (int index, int light) + { + if (_blockLight[index] != light) { + _blockLight[index] = (byte)light; + _dirty = true; + } + } + public void SetSkyLight (int x, int y, int z, int light) { if (_skyLight[x, y, z] != light) { @@ -352,6 +401,14 @@ namespace Substrate } } + internal void SetSkyLight (int index, int light) + { + if (_skyLight[index] != light) { + _skyLight[index] = (byte)light; + _dirty = true; + } + } + public int GetHeight (int x, int z) { return _heightMap[x, z]; @@ -506,5 +563,82 @@ namespace Substrate _autoFluid = autofluid; } + + + #region IEnumerable Members + + public IEnumerator GetEnumerator () + { + return new AlphaBlockEnumerator(this); + } + + #endregion + + #region IEnumerable Members + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () + { + return new AlphaBlockEnumerator(this); + } + + #endregion + + public class AlphaBlockEnumerator : IEnumerator + { + private AlphaBlockCollection _collection; + private int _index; + private int _size; + + public AlphaBlockEnumerator (AlphaBlockCollection collection) + { + _collection = collection; + _index = -1; + _size = collection.XDim * collection.YDim * collection.ZDim; + } + + #region IEnumerator Members + + public AlphaBlockRef Current + { + get + { + if (_index == -1 || _index == _size) { + throw new InvalidOperationException(); + } + return new AlphaBlockRef(_collection, _index); + } + } + + #endregion + + #region IDisposable Members + + public void Dispose () { } + + #endregion + + #region IEnumerator Members + + object System.Collections.IEnumerator.Current + { + get { return Current; } + } + + public bool MoveNext () + { + if (++_index == _size) { + return false; + } + + return true; + } + + public void Reset () + { + _index = -1; + } + + #endregion + } } } diff --git a/Substrate/SubstrateCS/Source/BlockInterface.cs b/Substrate/SubstrateCS/Source/BlockInterface.cs index 2e05caf..61c67d8 100644 --- a/Substrate/SubstrateCS/Source/BlockInterface.cs +++ b/Substrate/SubstrateCS/Source/BlockInterface.cs @@ -36,6 +36,14 @@ namespace Substrate void ClearTileEntity (); } + public interface IAlphaBlock : IDataBlock, IPropertyBlock + { + } + + public interface IAlphaBlockRef : IDataBlock, ILitBlock, IPropertyBlock + { + } + public interface IBlockCollection { IBlock GetBlock (int x, int y, int z); diff --git a/Substrate/SubstrateCS/Source/BlockRef.cs b/Substrate/SubstrateCS/Source/BlockRef.cs index 050fc09..00b8227 100644 --- a/Substrate/SubstrateCS/Source/BlockRef.cs +++ b/Substrate/SubstrateCS/Source/BlockRef.cs @@ -110,4 +110,105 @@ namespace Substrate #endregion } + + public struct AlphaBlockRef : IAlphaBlockRef + { + private readonly AlphaBlockCollection _collection; + private readonly int _index; + + internal AlphaBlockRef (AlphaBlockCollection collection, int index) + { + _collection = collection; + _index = index; + } + + #region IBlock Members + + public BlockInfo Info + { + get { return _collection.GetInfo(_index); } + } + + public int ID + { + get + { + return _collection.GetID(_index); + } + set + { + throw new NotImplementedException(); + } + } + + #endregion + + #region IDataBlock Members + + public int Data + { + get + { + return _collection.GetData(_index); + } + set + { + _collection.SetData(_index, value); + } + } + + public void SetData (int value) + { + _collection.SetData(_index, value); + } + + #endregion + + #region ILitBlock Members + + public int BlockLight + { + get + { + return _collection.GetBlockLight(_index); + } + set + { + _collection.SetBlockLight(_index, value); + } + } + + public int SkyLight + { + get + { + return _collection.GetSkyLight(_index); + } + set + { + _collection.SetSkyLight(_index, value); + } + } + + #endregion + + #region IPropertyBlock Members + + public TileEntity GetTileEntity () + { + throw new NotImplementedException(); + } + + public void SetTileEntity (TileEntity te) + { + throw new NotImplementedException(); + } + + public void ClearTileEntity () + { + throw new NotImplementedException(); + } + + #endregion + } } diff --git a/Substrate/SubstrateCS/Source/ChunkRef.cs b/Substrate/SubstrateCS/Source/ChunkRef.cs index 0c6511f..fd5099f 100644 --- a/Substrate/SubstrateCS/Source/ChunkRef.cs +++ b/Substrate/SubstrateCS/Source/ChunkRef.cs @@ -39,12 +39,24 @@ namespace Substrate public AlphaBlockCollection Blocks { - get { GetChunk(); return _blocks; } + get + { + if (_blocks == null) { + GetChunk(); + } + return _blocks; + } } public EntityCollection Entities { - get { GetChunk(); return _entities; } + get + { + if (_entities == null) { + GetChunk(); + } + return _entities; + } } public bool IsDirty