OSDN Git Service

old changes to json generator i forgot/didnt commit
[automap/automap.git] / Automap / Data / ColumnMeta.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.Specialized;
4
5
6 using Vintagestory.API.MathTools;
7 using Vintagestory.API.Common;
8
9 using ProtoBuf;
10 using System.IO;
11 using System.Collections.ObjectModel;
12 using System.Text;
13 using Vintagestory.API.Client;
14 using Newtonsoft.Json.Linq;
15
16 namespace Automap
17 {
18         [ProtoContract]
19         public struct ColumnMeta
20         {
21                 [ProtoMember(1)]
22                 public Vec2i Location;
23
24                 [ProtoIgnore]
25                 [DisplayName(0, "Coords.")]
26                 public string PrettyLocation;
27
28                 [ProtoMember(2)]
29                 public TimeSpan ChunkAge;//OLDEST CHUNK. from chunk last edit
30
31                 //[DisplayName(1, "Age")]
32                 //public string ShortChunkAge { get => ChunkAge.ToString("c"); }
33
34                 [ProtoMember(3)]
35                 [DisplayName(2, "Temp.")]
36                 public float Temperature;// Temperature - surface
37
38                 [ProtoMember(4)]
39                 [DisplayName(3, "Y Max.")]
40                 public ushort YMax;// Y feature height
41
42                 //[ProtoMember(5)]
43                 //public Dictionary<int, uint> RockRatio;//[Column] Geographic region (rock) Ratio. [BlockID * count]
44
45                 //[DisplayName(10, "Rocks")]
46                 //public JArray FlatRocks
47                 //{
48                 //      get {
49                 //              string[] rocks = new string[this.RockRatio.Count];
50                 //              int i = 0;
51                 //              foreach (var roc in RockRatio)
52                 //              {
53                 //                      rocks[i++] = $"\"{roc.Key}\":\"{roc.Value}\"";
54                 //              }
55                 //              return new JArray(rocks);
56                 //      }
57                 //}
58
59
60                 [ProtoMember(6)]
61                 [DisplayName(4, "Fert.")]
62                 public float Fertility;
63
64                 [ProtoMember(7)]
65                 //[DisplayName(5, "Forest")]
66                 public float ForestDensity; // not given to client
67
68                 [ProtoMember(8)]
69                 [DisplayName(6, "Rain")]
70                 public float Rainfall;
71
72                 [ProtoMember(9)]
73                 //[DisplayName(7, "Shrub")]
74                 public float ShrubDensity; // not given to client
75
76                 [ProtoMember(10)]
77                 [DisplayName(8, "Air blocks")]
78                 public uint AirBlocks;
79
80                 [ProtoMember(11)]
81                 [DisplayName(9, "Non-air")]
82                 public uint NonAirBlocks;
83
84                 [ProtoMember(12)]
85                 public byte ChunkSize;
86
87
88                 //[ProtoIgnore]
89                 //public ushort[,] HeightMap;//Needs to be 'flattened' for Protocol-Buffer serialization
90
91                 //[ProtoMember(13)]
92                 //private ushort[] _flattened_HeightMap;
93
94                 public ColumnMeta(Vec2i loc, ICoreClientAPI clientAPI, byte chunkSize = 32)
95                 {
96                         Location = loc;
97                         PrettyLocation = loc.PrettyCoords(clientAPI);
98                         ChunkAge = TimeSpan.Zero;
99                         Temperature = 0f;
100                         YMax = 0;
101                         //RockRatio = new Dictionary<int, uint>(10);
102                         Fertility = 0f;
103                         ForestDensity = 0f;
104                         Rainfall = 0f;
105                         ShrubDensity = 0f;
106                         AirBlocks = 0;
107                         NonAirBlocks = 0;
108                         ChunkSize = chunkSize;
109                         //HeightMap = new ushort[ChunkSize, ChunkSize];
110                         //_flattened_HeightMap = null;
111                 }
112
113                 internal void UpdateFieldsFrom(ClimateCondition climate, IMapChunk mapChunk, TimeSpan chunkAge)
114                 {
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;
121
122                         this.YMax = mapChunk.YMax;
123                 }
124
125                 [ProtoBeforeSerialization]
126                 private void PrepareData()
127                 {
128
129                         //if (HeightMap != null)
130                         //{
131                         //      _flattened_HeightMap = new ushort[ChunkSize * ChunkSize];
132                         //      int flatIndex = 0;
133
134                         //      for (byte col = 0; col < ChunkSize; col++)
135                         //      {
136                         //              for (byte row = 0; row < ChunkSize; row++)
137                         //              {
138                         //                      _flattened_HeightMap[flatIndex] = HeightMap[col, row];
139                         //                      flatIndex++;
140                         //              }
141                         //      }
142
143                         //}
144
145                 }
146
147
148                 [ProtoAfterDeserialization]
149                 private void PostProcess()
150                 {
151                         //if (this.HeightMap == null) this.HeightMap = new ushort[ChunkSize, ChunkSize];
152
153                         //if (_flattened_HeightMap != null)
154                         //{
155                         //      int col, row;
156                         //      _ = new BitVector32(0);
157                         //      var rowSection = BitVector32.CreateSection((short) (ChunkSize - 1));
158                         //      var colSection = BitVector32.CreateSection((short) (ChunkSize - 1), rowSection);
159
160                         //      for (uint rowcol = 0; rowcol < (ChunkSize * ChunkSize); rowcol++)
161                         //      {
162                         //              BitVector32 bitMasker = new BitVector32(data: (int) rowcol);
163                         //              row = bitMasker[rowSection];
164                         //              col = bitMasker[colSection];
165                         //              HeightMap[col, row] = _flattened_HeightMap[rowcol];
166                         //      }
167
168                         //}
169
170                 }
171
172                 internal ColumnMeta Reload(ICoreClientAPI clientAPI)
173                 {
174                         this.PrettyLocation = Location.PrettyCoords(clientAPI);
175                         return this;
176                 }
177         }
178
179         public class ColumnsMetadata : KeyedCollection<Vec2i, ColumnMeta>
180         {
181                 private ColumnsMetadata()
182                 {
183                         throw new NotSupportedException();
184                 }
185
186                 public ColumnsMetadata(Vec2i startChunkColumn)
187                 {
188                         North_mostChunk = startChunkColumn.Y;
189                         South_mostChunk = startChunkColumn.Y;
190                         East_mostChunk = startChunkColumn.X;
191                         West_mostChunk = startChunkColumn.X;
192                 }
193
194                 public int North_mostChunk
195                 {
196                         get; private set;
197                 }
198
199                 public int South_mostChunk
200                 {
201                         get; private set;
202                 }
203
204                 public int East_mostChunk
205                 {
206                         get; private set;
207                 }
208
209                 public int West_mostChunk
210                 {
211                         get; private set;
212                 }
213
214                 protected override Vec2i GetKeyForItem(ColumnMeta item)
215                 {
216                         return item.Location;
217                 }
218
219                 internal void Update(ColumnMeta metaData)
220                 {
221                         if (this.Contains(metaData.Location))
222                         {
223                                 this.Remove(metaData.Location);
224                                 this.Add(metaData);
225                         }
226                         else
227                         {
228                                 this.Add(metaData);
229                         }
230
231                 }
232
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;
237
238                         if (South_mostChunk < newItem.Location.Y)
239                                 South_mostChunk = newItem.Location.Y;
240
241                         if (East_mostChunk < newItem.Location.X)
242                                 East_mostChunk = newItem.Location.X;
243
244                         if (West_mostChunk > newItem.Location.X)
245                                 West_mostChunk = newItem.Location.X;
246
247                         base.Add(newItem);
248                 }
249
250                 public void ClearMetadata()
251                 {
252                         for (int i = 0, maxItemsCount = this.Items.Count; i < maxItemsCount; i++)
253                         {
254                                 ColumnMeta entry = this.Items[i];
255                                 //entry.HeightMap = null;
256                                 //entry.RockRatio = null;//Also regenerated when any chunk in a column is changed...
257                         }
258                 }
259
260         }
261 }