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;
14 using Newtonsoft.Json.Linq;
19 public struct ColumnMeta
22 public Vec2i Location;
25 [DisplayName(0, "Coords.")]
26 public string PrettyLocation;
29 public TimeSpan ChunkAge;//OLDEST CHUNK. from chunk last edit
31 //[DisplayName(1, "Age")]
32 //public string ShortChunkAge { get => ChunkAge.ToString("c"); }
35 [DisplayName(2, "Temp.")]
36 public float Temperature;// Temperature - surface
39 [DisplayName(3, "Y Max.")]
40 public ushort YMax;// Y feature height
43 //public Dictionary<int, uint> RockRatio;//[Column] Geographic region (rock) Ratio. [BlockID * count]
45 //[DisplayName(10, "Rocks")]
46 //public JArray FlatRocks
49 // string[] rocks = new string[this.RockRatio.Count];
51 // foreach (var roc in RockRatio)
53 // rocks[i++] = $"\"{roc.Key}\":\"{roc.Value}\"";
55 // return new JArray(rocks);
61 [DisplayName(4, "Fert.")]
62 public float Fertility;
65 //[DisplayName(5, "Forest")]
66 public float ForestDensity; // not given to client
69 [DisplayName(6, "Rain")]
70 public float Rainfall;
73 //[DisplayName(7, "Shrub")]
74 public float ShrubDensity; // not given to client
77 [DisplayName(8, "Air blocks")]
78 public uint AirBlocks;
81 [DisplayName(9, "Non-air")]
82 public uint NonAirBlocks;
85 public byte ChunkSize;
89 //public ushort[,] HeightMap;//Needs to be 'flattened' for Protocol-Buffer serialization
92 //private ushort[] _flattened_HeightMap;
94 public ColumnMeta(Vec2i loc, ICoreClientAPI clientAPI, byte chunkSize = 32)
97 PrettyLocation = loc.PrettyCoords(clientAPI);
98 ChunkAge = TimeSpan.Zero;
101 //RockRatio = new Dictionary<int, uint>(10);
108 ChunkSize = chunkSize;
109 //HeightMap = new ushort[ChunkSize, ChunkSize];
110 //_flattened_HeightMap = null;
113 internal void UpdateFieldsFrom(ClimateCondition climate, IMapChunk mapChunk, TimeSpan chunkAge)
115 this.ChunkAge = chunkAge;
116 this.Temperature = climate.Temperature;
117 this.Fertility = climate.Fertility;
118 this.ForestDensity = climate.ForestDensity;
119 this.Rainfall = climate.Rainfall;
120 this.ShrubDensity = climate.ShrubDensity;
122 this.YMax = mapChunk.YMax;
125 [ProtoBeforeSerialization]
126 private void PrepareData()
129 //if (HeightMap != null)
131 // _flattened_HeightMap = new ushort[ChunkSize * ChunkSize];
132 // int flatIndex = 0;
134 // for (byte col = 0; col < ChunkSize; col++)
136 // for (byte row = 0; row < ChunkSize; row++)
138 // _flattened_HeightMap[flatIndex] = HeightMap[col, row];
148 [ProtoAfterDeserialization]
149 private void PostProcess()
151 //if (this.HeightMap == null) this.HeightMap = new ushort[ChunkSize, ChunkSize];
153 //if (_flattened_HeightMap != null)
156 // _ = new BitVector32(0);
157 // var rowSection = BitVector32.CreateSection((short) (ChunkSize - 1));
158 // var colSection = BitVector32.CreateSection((short) (ChunkSize - 1), rowSection);
160 // for (uint rowcol = 0; rowcol < (ChunkSize * ChunkSize); rowcol++)
162 // BitVector32 bitMasker = new BitVector32(data: (int) rowcol);
163 // row = bitMasker[rowSection];
164 // col = bitMasker[colSection];
165 // HeightMap[col, row] = _flattened_HeightMap[rowcol];
172 internal ColumnMeta Reload(ICoreClientAPI clientAPI)
174 this.PrettyLocation = Location.PrettyCoords(clientAPI);
179 public class ColumnsMetadata : KeyedCollection<Vec2i, ColumnMeta>
181 private ColumnsMetadata()
183 throw new NotSupportedException();
186 public ColumnsMetadata(Vec2i startChunkColumn)
188 North_mostChunk = startChunkColumn.Y;
189 South_mostChunk = startChunkColumn.Y;
190 East_mostChunk = startChunkColumn.X;
191 West_mostChunk = startChunkColumn.X;
194 public int North_mostChunk
199 public int South_mostChunk
204 public int East_mostChunk
209 public int West_mostChunk
214 protected override Vec2i GetKeyForItem(ColumnMeta item)
216 return item.Location;
219 internal void Update(ColumnMeta metaData)
221 if (this.Contains(metaData.Location))
223 this.Remove(metaData.Location);
233 public new void Add(ColumnMeta newItem)
234 { // south and east are positive
235 if (North_mostChunk > newItem.Location.Y)
236 North_mostChunk = newItem.Location.Y;
238 if (South_mostChunk < newItem.Location.Y)
239 South_mostChunk = newItem.Location.Y;
241 if (East_mostChunk < newItem.Location.X)
242 East_mostChunk = newItem.Location.X;
244 if (West_mostChunk > newItem.Location.X)
245 West_mostChunk = newItem.Location.X;
250 public void ClearMetadata()
252 for (int i = 0, maxItemsCount = this.Items.Count; i < maxItemsCount; i++)
254 ColumnMeta entry = this.Items[i];
255 //entry.HeightMap = null;
256 //entry.RockRatio = null;//Also regenerated when any chunk in a column is changed...