OSDN Git Service

Post-PR4 #2: Heightmap in PNG Metadata, JSON encoding for POI notes
[automap/automap.git] / Automap / Data / ColumnMeta.cs
index bd07aaa..ed792a0 100644 (file)
@@ -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<Vec2i, ColumnMeta>