2 using System.Collections.Generic;
3 using System.Collections.Specialized;
6 using Vintagestory.API.MathTools;
7 using Vintagestory.API.Common;
11 using System.Collections.ObjectModel;
13 using Vintagestory.API.Client;
18 public struct ColumnMeta
21 [DisplayName(0, "Coords.")]
22 public Vec2i Location;
25 [DisplayName(1, "Age")]
26 public TimeSpan ChunkAge;//OLDEST CHUNK. from chunk last edit
29 [DisplayName(2, "Temp.")]
30 public float Temperature;// Temperature - surface
33 [DisplayName(3, "Y Max.")]
34 public ushort YMax;// Y feature height
37 //[DisplayName(10, "Rocks")]
38 public Dictionary<int, uint> RockRatio;//[Column] Geographic region (rock) Ratio. [BlockID * count]
41 [DisplayName(4, "Fert.")]
42 public float Fertility;
45 [DisplayName(5, "Forest")]
46 public float ForestDensity;
49 [DisplayName(6, "Rain")]
50 public float Rainfall;
53 [DisplayName(7, "Shrub")]
54 public float ShrubDensity;
57 [DisplayName(8, "Air blocks")]
58 public uint AirBlocks;
61 [DisplayName(9, "Non-air")]
62 public uint NonAirBlocks;
65 public byte ChunkSize;
69 public ushort[,] HeightMap;//Needs to be 'flattened' for Protocol-Buffer serialization
72 private ushort[] _flattened_HeightMap;
75 public ColumnMeta(Vec2i loc, byte chunkSize = 32)
78 ChunkAge = TimeSpan.Zero;
81 RockRatio = new Dictionary<int, uint>(10);
88 ChunkSize = chunkSize;
89 HeightMap = new ushort[ChunkSize, ChunkSize];
90 _flattened_HeightMap = null;
93 internal void UpdateFieldsFrom(ClimateCondition climate, IMapChunk mapChunk, TimeSpan chunkAge)
95 this.ChunkAge = chunkAge;
96 this.Temperature = climate.Temperature;
97 this.Fertility = climate.Fertility;
98 this.ForestDensity = climate.ForestDensity;
99 this.Rainfall = climate.Rainfall;
100 this.ShrubDensity = climate.ShrubDensity;
102 this.YMax = mapChunk.YMax;
105 [ProtoBeforeSerialization]
106 private void PrepareData()
109 if (HeightMap != null)
111 _flattened_HeightMap = new ushort[ChunkSize * ChunkSize];
114 for (byte col = 0; col < ChunkSize; col++)
116 for (byte row = 0; row < ChunkSize; row++)
118 _flattened_HeightMap[flatIndex] = HeightMap[col, row];
128 [ProtoAfterDeserialization]
129 private void PostProcess()
131 ChunkSize = (ChunkSize == byte.MinValue) ? (byte)32 : ChunkSize;//Not good - if chunk wasn't 32 orignally!
133 if (this.HeightMap == null || this.HeightMap.Length != (ChunkSize * ChunkSize)) {
134 this.HeightMap = new ushort[ChunkSize, ChunkSize];
137 if (_flattened_HeightMap != null)
140 var bitMasker = new BitVector32(0);
141 var rowSection = BitVector32.CreateSection((short) (ChunkSize - 1));
142 var colSection = BitVector32.CreateSection((short) (ChunkSize - 1), rowSection);
144 for (uint rowcol = 0; rowcol < (ChunkSize * ChunkSize); rowcol++)
146 bitMasker = new BitVector32(data: (int) rowcol);
147 row = bitMasker[rowSection];
148 col = bitMasker[colSection];
149 HeightMap[col, row] = _flattened_HeightMap[rowcol];
159 public class ColumnsMetadata : KeyedCollection<Vec2i, ColumnMeta>
161 private ColumnsMetadata()
163 throw new NotSupportedException();
166 public ColumnsMetadata(Vec2i startChunkColumn)
168 North_mostChunk = startChunkColumn.Y;
169 South_mostChunk = startChunkColumn.Y;
170 East_mostChunk = startChunkColumn.X;
171 West_mostChunk = startChunkColumn.X;
174 public int North_mostChunk
179 public int South_mostChunk
184 public int East_mostChunk
189 public int West_mostChunk
194 protected override Vec2i GetKeyForItem(ColumnMeta item)
196 return item.Location;
199 internal void Update(ColumnMeta metaData)
201 if (this.Contains(metaData.Location))
203 this.Remove(metaData.Location);
213 public new void Add(ColumnMeta newItem)
215 if (North_mostChunk > newItem.Location.Y)
217 North_mostChunk = newItem.Location.Y;
220 if (South_mostChunk < newItem.Location.Y)
222 South_mostChunk = newItem.Location.Y;
225 if (East_mostChunk < newItem.Location.X)
227 East_mostChunk = newItem.Location.X;
230 if (West_mostChunk > newItem.Location.X)
232 West_mostChunk = newItem.Location.X;
238 public void ClearMetadata( )
240 for (int i = 0, maxItemsCount = this.Items.Count; i < maxItemsCount; i++) {
241 ColumnMeta entry = this.Items[i];
242 entry.HeightMap = null;
243 entry.RockRatio = null;//Also regenerated when any chunk in a column is changed...