More preparation towards refactoring

This commit is contained in:
Justin Aquadro 2011-05-08 08:17:20 +00:00
parent c8cdf32eb2
commit 0f1c6970c9
6 changed files with 292 additions and 15 deletions

View file

@ -653,12 +653,12 @@ namespace Substrate
cc.SetBlockSkyLight(x, y, z, dimStr);
if (info.TransmitsLight) {
spread.Enqueue(new LightRecord(rec.x - 1, rec.y, rec.z, dimStr - 1));
/*spread.Enqueue(new LightRecord(rec.x - 1, rec.y, rec.z, dimStr - 1));
spread.Enqueue(new LightRecord(rec.x + 1, rec.y, rec.z, dimStr - 1));
spread.Enqueue(new LightRecord(rec.x, rec.y - 1, rec.z, dimStr - 1));
spread.Enqueue(new LightRecord(rec.x, rec.y + 1, rec.z, dimStr - 1));
spread.Enqueue(new LightRecord(rec.x, rec.y, rec.z - 1, dimStr - 1));
spread.Enqueue(new LightRecord(rec.x, rec.y, rec.z + 1, dimStr - 1));
spread.Enqueue(new LightRecord(rec.x, rec.y, rec.z + 1, dimStr - 1));*/
if (heightMap[xi, zi] > rec.y - 1) {
spread.Enqueue(new LightRecord(rec.x, rec.y - 1, rec.z, dimStr - 1));

View file

@ -0,0 +1,168 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace Substrate.Utility
{
public class ByteArray : ICopyable<ByteArray>
{
protected readonly byte[] _data;
public ByteArray (byte[] data)
{
_data = data;
}
public byte this[int i]
{
get { return _data[i]; }
set { _data[i] = value; }
}
public int Length
{
get { return _data.Length; }
}
public void Clear ()
{
for (int i = 0; i < _data.Length; i++)
{
_data[i] = 0;
}
}
#region ICopyable<yteArray> Members
public virtual ByteArray Copy ()
{
byte[] data = new byte[_data.Length];
_data.CopyTo(data, 0);
return new ByteArray(data);
}
#endregion
}
public sealed class XZYByteArray : ByteArray
{
private readonly int _xdim;
private readonly int _ydim;
private readonly int _zdim;
private readonly byte[] _data;
public XZYByteArray (int xdim, int ydim, int zdim, byte[] data)
: base(data)
{
_xdim = xdim;
_ydim = ydim;
_zdim = zdim;
if (xdim * ydim * zdim != data.Length)
{
throw new ArgumentException("Product of dimensions must equal length of data");
}
}
public byte this[int x, int y, int z]
{
get
{
int index = _ydim * (x * _zdim + z) + y;
return _data[index];
}
set
{
int index = _ydim * (x * _zdim + z) + y;
_data[index] = value;
}
}
public int XDim
{
get { return _xdim; }
}
public int YDim
{
get { return _ydim; }
}
public int ZDim
{
get { return _zdim; }
}
#region ICopyable<XZYByteArray> Members
public override ByteArray Copy ()
{
byte[] data = new byte[_data.Length];
_data.CopyTo(data, 0);
return new XZYByteArray(_xdim, _ydim, _zdim, data);
}
#endregion
}
public sealed class ZXByteArray : ByteArray
{
private readonly int _xdim;
private readonly int _zdim;
private readonly byte[] _data;
public ZXByteArray (int xdim, int zdim, byte[] data)
: base(data)
{
_xdim = xdim;
_zdim = zdim;
if (xdim * zdim != data.Length)
{
throw new ArgumentException("Product of dimensions must equal length of data");
}
}
public byte this[int x, int z]
{
get
{
int index = z * _xdim + x;
return _data[index];
}
set
{
int index = z * _xdim + x;
_data[index] = 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[_data.Length];
_data.CopyTo(data, 0);
return new ZXByteArray(_xdim, _zdim, data);
}
#endregion
}
}

View file

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace Substrate.Utility
{
class IndexedLinkedList<T> : ICollection<T>, ICollection
public class IndexedLinkedList<T> : ICollection<T>, ICollection
{
private LinkedList<T> _list;
private Dictionary<T, LinkedListNode<T>> _index;
@ -111,7 +111,7 @@ namespace Substrate.Utility
object ICollection.SyncRoot
{
get { return this; }
get { return (_list as ICollection).SyncRoot; }
}
#endregion

View file

@ -4,8 +4,35 @@ using System.Collections.Generic;
namespace Substrate.Utility
{
class LRUCache<TKey, TValue> : IDictionary<TKey, TValue>
public class LRUCache<TKey, TValue> : IDictionary<TKey, TValue>
{
public delegate void RemoveCacheValueHandler (Object o, CacheValueArgs e);
public class CacheValueArgs : EventArgs
{
private TKey _key;
private TValue _value;
public TKey Key
{
get { return _key; }
}
public TValue Value
{
get { return _value; }
}
public CacheValueArgs (TKey key, TValue value)
{
_key = key;
_value = value;
}
}
public event RemoveCacheValueHandler RemoveCacheValue;
private Dictionary<TKey, TValue> _data;
private IndexedLinkedList<TKey> _index;
@ -38,6 +65,8 @@ namespace Substrate.Utility
if (_data.Count > _capacity)
{
OnRemoveCacheValue(new CacheValueArgs(_index.First, _data[_index.First]));
_data.Remove(_index.First);
_index.RemoveFirst();
}
@ -99,6 +128,8 @@ namespace Substrate.Utility
if (_data.Count > _capacity)
{
OnRemoveCacheValue(new CacheValueArgs(_index.First, _data[_index.First]));
_data.Remove(_index.First);
_index.RemoveFirst();
}
@ -170,5 +201,14 @@ namespace Substrate.Utility
}
#endregion
private void OnRemoveCacheValue (CacheValueArgs e)
{
if (RemoveCacheValue != null)
{
RemoveCacheValue(this, e);
}
}
}
}

View file

@ -1,10 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Substrate.Utility
{
public class NibbleArray : ICopyable<NibbleArray>
{
protected readonly byte[] _data = null;
@ -14,26 +14,30 @@ namespace Substrate.Utility
_data = data;
}
public int this[int index]
public byte this[int index]
{
get
{
int subs = index >> 1;
if ((index & 1) == 0) {
return _data[subs] & 0x0F;
if ((index & 1) == 0)
{
return (byte)(_data[subs] & 0x0F);
}
else {
return (_data[subs] >> 4) & 0x0F;
else
{
return (byte)((_data[subs] >> 4) & 0x0F);
}
}
set
{
int subs = index >> 1;
if ((index & 1) == 0) {
if ((index & 1) == 0)
{
_data[subs] = (byte)((_data[subs] & 0xF0) | (value & 0x0F));
}
else {
else
{
_data[subs] = (byte)((_data[subs] & 0x0F) | ((value & 0x0F) << 4));
}
}
@ -49,14 +53,15 @@ namespace Substrate.Utility
public void Clear ()
{
for (int i = 0; i < _data.Length; i++) {
for (int i = 0; i < _data.Length; i++)
{
_data[i] = 0;
}
}
#region ICopyable<NibbleArray> Members
public NibbleArray Copy ()
public virtual NibbleArray Copy ()
{
byte[] data = new byte[_data.Length];
_data.CopyTo(data, 0);
@ -66,4 +71,66 @@ namespace Substrate.Utility
#endregion
}
public sealed class XZYNibbleArray : NibbleArray
{
private readonly int _xdim;
private readonly int _ydim;
private readonly int _zdim;
public XZYNibbleArray (int xdim, int ydim, int zdim, byte[] data)
: base(data)
{
_xdim = xdim;
_ydim = ydim;
_zdim = zdim;
if (xdim * ydim * zdim != data.Length / 2)
{
throw new ArgumentException("Product of dimensions must equal half length of raw data");
}
}
public byte this[int x, int y, int z]
{
get
{
int index = _ydim * (x * _zdim + z) + y;
return this[index];
}
set
{
int index = _ydim * (x * _zdim + z) + y;
this[index] = value;
}
}
public int XDim
{
get { return _xdim; }
}
public int YDim
{
get { return _ydim; }
}
public int ZDim
{
get { return _zdim; }
}
#region ICopyable<NibbleArray> Members
public override NibbleArray Copy ()
{
byte[] data = new byte[_data.Length];
_data.CopyTo(data, 0);
return new XZYNibbleArray(_xdim, _ydim, _zdim, data);
}
#endregion
}
}

View file

@ -72,6 +72,7 @@
<None Include="Source\Experimental\BlockInterface.cs" />
<Compile Include="Source\Entities\EntitySquid.cs" />
<None Include="Source\Experimental\Interface2.cs" />
<Compile Include="Source\Experimental\BlockLight.cs" />
<Compile Include="Source\Level.cs" />
<Compile Include="Source\PlayerManager.cs" />
<Compile Include="Source\PlayerFile.cs" />
@ -139,6 +140,7 @@
<Compile Include="Source\TileEntity.cs" />
<Compile Include="Source\TileEntityFactory.cs" />
<Compile Include="Source\Utility\Base.cs" />
<Compile Include="Source\Utility\ByteArray.cs" />
<Compile Include="Source\Utility\IndexedLinkedList.cs" />
<Compile Include="Source\Utility\Interface.cs" />
<Compile Include="Source\Utility\LRUCache.cs" />