Some tweaks to help the JIT out. Changes to BlockRef but this will probably be removed entirely.

This commit is contained in:
Justin Aquadro 2011-06-10 16:08:36 +00:00
parent bfe7efcd1c
commit 7d1833e6f0
4 changed files with 261 additions and 6 deletions

View file

@ -46,8 +46,12 @@ namespace Substrate
} }
} }
public class AlphaBlockCollection : IBoundedAlphaBlockCollection public class AlphaBlockCollection : IBoundedAlphaBlockCollection, IEnumerable<AlphaBlockRef>
{ {
private readonly int _xdim;
private readonly int _ydim;
private readonly int _zdim;
private XZYByteArray _blocks; private XZYByteArray _blocks;
private XZYNibbleArray _data; private XZYNibbleArray _data;
private XZYNibbleArray _blockLight; private XZYNibbleArray _blockLight;
@ -122,6 +126,10 @@ namespace Substrate
_lightManager = new BlockLight(this); _lightManager = new BlockLight(this);
_fluidManager = new BlockFluid(this); _fluidManager = new BlockFluid(this);
_tileEntityManager = new BlockTileEntities(_blocks, _tileEntities); _tileEntityManager = new BlockTileEntities(_blocks, _tileEntities);
_xdim = _blocks.XDim;
_ydim = _blocks.YDim;
_zdim = _blocks.ZDim;
} }
public Block GetBlock (int x, int y, int z) public Block GetBlock (int x, int y, int z)
@ -146,17 +154,17 @@ namespace Substrate
public int XDim public int XDim
{ {
get { return _blocks.XDim; } get { return _xdim; }
} }
public int YDim public int YDim
{ {
get { return _blocks.YDim; } get { return _ydim; }
} }
public int ZDim public int ZDim
{ {
get { return _blocks.ZDim; } get { return _zdim; }
} }
IBlock IBlockCollection.GetBlock (int x, int y, int z) IBlock IBlockCollection.GetBlock (int x, int y, int z)
@ -179,11 +187,21 @@ namespace Substrate
return BlockInfo.BlockTable[_blocks[x, y, z]]; 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) public int GetID (int x, int y, int z)
{ {
return _blocks[x, y, 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) public void SetID (int x, int y, int z, int id)
{ {
int oldid = _blocks[x, y, z]; int oldid = _blocks[x, y, z];
@ -278,6 +296,11 @@ namespace Substrate
return _data[x, y, z]; return _data[x, y, z];
} }
internal int GetData (int index)
{
return _data[index];
}
public void SetData (int x, int y, int z, int data) public void SetData (int x, int y, int z, int data)
{ {
if (_data[x, y, z] != 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) public int CountByData (int id, int data)
{ {
int c = 0; int c = 0;
@ -331,11 +362,21 @@ namespace Substrate
return _blockLight[x, y, z]; return _blockLight[x, y, z];
} }
internal int GetBlockLight (int index)
{
return _blockLight[index];
}
public int GetSkyLight (int x, int y, int z) public int GetSkyLight (int x, int y, int z)
{ {
return _skyLight[x, y, 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) public void SetBlockLight (int x, int y, int z, int light)
{ {
if (_blockLight[x, y, z] != 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) public void SetSkyLight (int x, int y, int z, int light)
{ {
if (_skyLight[x, y, z] != 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) public int GetHeight (int x, int z)
{ {
return _heightMap[x, z]; return _heightMap[x, z];
@ -506,5 +563,82 @@ namespace Substrate
_autoFluid = autofluid; _autoFluid = autofluid;
} }
#region IEnumerable<AlphaBlockRef> Members
public IEnumerator<AlphaBlockRef> 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<AlphaBlockRef>
{
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<Entity> 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
}
} }
} }

View file

@ -36,6 +36,14 @@ namespace Substrate
void ClearTileEntity (); void ClearTileEntity ();
} }
public interface IAlphaBlock : IDataBlock, IPropertyBlock
{
}
public interface IAlphaBlockRef : IDataBlock, ILitBlock, IPropertyBlock
{
}
public interface IBlockCollection public interface IBlockCollection
{ {
IBlock GetBlock (int x, int y, int z); IBlock GetBlock (int x, int y, int z);

View file

@ -110,4 +110,105 @@ namespace Substrate
#endregion #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
}
} }

View file

@ -39,12 +39,24 @@ namespace Substrate
public AlphaBlockCollection Blocks public AlphaBlockCollection Blocks
{ {
get { GetChunk(); return _blocks; } get
{
if (_blocks == null) {
GetChunk();
}
return _blocks;
}
} }
public EntityCollection Entities public EntityCollection Entities
{ {
get { GetChunk(); return _entities; } get
{
if (_entities == null) {
GetChunk();
}
return _entities;
}
} }
public bool IsDirty public bool IsDirty