X-Git-Url: http://git.osdn.net/view?p=automap%2Fautomap.git;a=blobdiff_plain;f=Automap%2FData%2FColumnMeta.cs;h=ed792a0bf1d06a53106a90ab9cf6780fa0b705a6;hp=bd07aaa25945e06884583cba63d422bbfefc27c5;hb=5156c68e472ccc1f71aa688c34f649f0a952ee45;hpb=5fde0b7a52ff9c32db3bcb2749379d9431d0964e diff --git a/Automap/Data/ColumnMeta.cs b/Automap/Data/ColumnMeta.cs index bd07aaa..ed792a0 100644 --- a/Automap/Data/ColumnMeta.cs +++ b/Automap/Data/ColumnMeta.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Linq; +using System.Collections.Specialized; + using Vintagestory.API.MathTools; using Vintagestory.API.Common; @@ -42,16 +43,23 @@ namespace Automap public float ShrubDensity; [ProtoMember(10)] - public ushort AirBlocks; + public uint AirBlocks; [ProtoMember(11)] - public ushort NonAirBlocks; + public uint NonAirBlocks; + + [ProtoMember(12)] + public byte ChunkSize; + - //[ProtoMember(12,OverwriteList = true)] [ProtoIgnore] - public ushort[,] HeightMap; + public ushort[,] HeightMap;//Needs to be 'flattened' for Protocol-Buffer serialization + + [ProtoMember(13)] + private ushort[ ] _flattened_HeightMap; - public ColumnMeta(Vec2i loc, int chunkSize = 32) + + public ColumnMeta(Vec2i loc, byte chunkSize = 32) { Location = loc; ChunkAge = TimeSpan.Zero; @@ -64,7 +72,9 @@ namespace Automap ShrubDensity = 0f; AirBlocks = 0; NonAirBlocks = 0; - HeightMap = new ushort[chunkSize, chunkSize]; + ChunkSize = chunkSize; + HeightMap = new ushort[ChunkSize, ChunkSize]; + _flattened_HeightMap = null; } internal void UpdateFieldsFrom(ClimateCondition climate, IMapChunk mapChunk, TimeSpan chunkAge) @@ -77,8 +87,52 @@ namespace Automap this.ShrubDensity = climate.ShrubDensity; this.YMax = mapChunk.YMax; + } + + [ProtoBeforeSerialization] + private void PrepareData( ) + { + + if (HeightMap != null) { + _flattened_HeightMap = new ushort[ChunkSize * ChunkSize]; + int flatIndex = 0; + + for (byte col = 0; col < ChunkSize; col++) { + for (byte row = 0; row < ChunkSize; row++) { + _flattened_HeightMap[flatIndex] = HeightMap[col, row]; + flatIndex++; + } + } + + } } + + + [ProtoAfterDeserialization] + private void PostProcess( ) + { + if (this.HeightMap == null) this.HeightMap = new ushort[ChunkSize, ChunkSize]; + + if (_flattened_HeightMap != null) { + int col, row; + + BitVector32 bitMasker = new BitVector32(0); + var rowSection = BitVector32.CreateSection(ChunkSize); + var colSection = BitVector32.CreateSection(ChunkSize, rowSection); + + for (uint rowcol = 0; rowcol < (ChunkSize * ChunkSize); rowcol++) { + bitMasker = new BitVector32(data: ( int )rowcol); + row = bitMasker[rowSection]; + col = bitMasker[colSection]; + HeightMap[col, row] = _flattened_HeightMap[rowcol]; + } + + } + + } + + } public class ColumnsMetadata : KeyedCollection