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 if (this.HeightMap == null) this.HeightMap = new ushort[ChunkSize, ChunkSize];
133 if (_flattened_HeightMap != null)
136 _ = new BitVector32(0);
137 var rowSection = BitVector32.CreateSection((short) (ChunkSize - 1));
138 var colSection = BitVector32.CreateSection((short) (ChunkSize - 1), rowSection);
140 for (uint rowcol = 0; rowcol < (ChunkSize * ChunkSize); rowcol++)
142 BitVector32 bitMasker = new BitVector32(data: (int) rowcol);
143 row = bitMasker[rowSection];
144 col = bitMasker[colSection];
145 HeightMap[col, row] = _flattened_HeightMap[rowcol];
155 public class ColumnsMetadata : KeyedCollection<Vec2i, ColumnMeta>
157 private ColumnsMetadata()
159 throw new NotSupportedException();
162 public ColumnsMetadata(Vec2i startChunkColumn)
164 North_mostChunk = startChunkColumn.Y;
165 South_mostChunk = startChunkColumn.Y;
166 East_mostChunk = startChunkColumn.X;
167 West_mostChunk = startChunkColumn.X;
170 public int North_mostChunk
175 public int South_mostChunk
180 public int East_mostChunk
185 public int West_mostChunk
190 protected override Vec2i GetKeyForItem(ColumnMeta item)
192 return item.Location;
195 internal void Update(ColumnMeta metaData)
197 if (this.Contains(metaData.Location))
199 this.Remove(metaData.Location);
209 public new void Add(ColumnMeta newItem)
211 if (North_mostChunk > newItem.Location.Y)
213 North_mostChunk = newItem.Location.Y;
216 if (South_mostChunk < newItem.Location.Y)
218 South_mostChunk = newItem.Location.Y;
221 if (East_mostChunk < newItem.Location.X)
223 East_mostChunk = newItem.Location.X;
226 if (West_mostChunk > newItem.Location.X)
228 West_mostChunk = newItem.Location.X;