OSDN Git Service

Pre-PR5 Configurable Designators, Autostart, fix for Entity processing
[automap/automap.git] / Automap / Data / ColumnMeta.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Collections.Specialized;
5
6
7 using Vintagestory.API.MathTools;
8 using Vintagestory.API.Common;
9
10 using ProtoBuf;
11
12
13 namespace Automap
14 {
15         [ProtoContract]
16         public struct ColumnMeta
17         {
18                 [ProtoMember(1)]
19                 public Vec2i Location;
20
21                 [ProtoMember(2)]
22                 public TimeSpan ChunkAge;//OLDEST CHUNK. from chunk last edit
23
24                 [ProtoMember(3)]
25                 public float Temperature;// Temperature - surface
26
27                 [ProtoMember(4)]
28                 public ushort YMax;// Y feature height
29
30                 [ProtoMember(5)]
31                 public Dictionary<int, uint> RockRatio;//[Column] Geographic region (rock) Ratio. [BlockID * count]
32
33                 [ProtoMember(6)]
34                 public float Fertility;
35
36                 [ProtoMember(7)]
37                 public float ForestDensity;
38
39                 [ProtoMember(8)]
40                 public float Rainfall;
41
42                 [ProtoMember(9)]
43                 public float ShrubDensity;
44
45                 [ProtoMember(10)]
46                 public uint AirBlocks;
47
48                 [ProtoMember(11)]
49                 public uint NonAirBlocks;
50
51                 [ProtoMember(12)]
52                 public byte ChunkSize;
53
54
55                 [ProtoIgnore]
56                 public ushort[,] HeightMap;//Needs to be 'flattened' for Protocol-Buffer serialization
57
58                 [ProtoMember(13)]
59                 private ushort[ ] _flattened_HeightMap;
60
61
62                 public ColumnMeta(Vec2i loc, byte chunkSize = 32)
63                 {
64                         Location = loc;
65                         ChunkAge = TimeSpan.Zero;
66                         Temperature = 0f;
67                         YMax = 0;
68                         RockRatio = new Dictionary<int, uint>(10);
69                         Fertility = 0f;
70                         ForestDensity = 0f;
71                         Rainfall = 0f;
72                         ShrubDensity = 0f;
73                         AirBlocks = 0;
74                         NonAirBlocks = 0;
75                         ChunkSize = chunkSize;
76                         HeightMap = new ushort[ChunkSize, ChunkSize];
77                         _flattened_HeightMap = null;
78                 }
79
80                 internal void UpdateFieldsFrom(ClimateCondition climate, IMapChunk mapChunk, TimeSpan chunkAge)
81                 {
82                         this.ChunkAge = chunkAge;
83                         this.Temperature = climate.Temperature;
84                         this.Fertility = climate.Fertility;
85                         this.ForestDensity = climate.ForestDensity;
86                         this.Rainfall = climate.Rainfall;
87                         this.ShrubDensity = climate.ShrubDensity;
88
89                         this.YMax = mapChunk.YMax;
90                 }
91
92
93
94                 [ProtoBeforeSerialization]
95                 private void PrepareData( )
96                 {
97
98                 if (HeightMap != null) {
99                         _flattened_HeightMap = new ushort[ChunkSize * ChunkSize];
100                 int flatIndex = 0;
101
102                 for (byte col = 0; col < ChunkSize; col++) {
103                         for (byte row = 0; row < ChunkSize; row++) {
104                         _flattened_HeightMap[flatIndex] = HeightMap[col, row];
105                         flatIndex++;
106                         }
107                 }
108
109                 }
110
111                 }
112
113
114                 [ProtoAfterDeserialization]
115                 private void PostProcess( )
116                 {
117                 if (this.HeightMap == null)     this.HeightMap = new ushort[ChunkSize, ChunkSize];
118
119                 if (_flattened_HeightMap != null) {
120                 int col, row;
121
122                 BitVector32 bitMasker = new BitVector32(0);
123                 var rowSection = BitVector32.CreateSection(ChunkSize);
124                 var colSection = BitVector32.CreateSection(ChunkSize, rowSection);
125
126                 for (uint rowcol = 0; rowcol < (ChunkSize * ChunkSize); rowcol++) {
127                 bitMasker = new BitVector32(data: ( int )rowcol);
128                 row = bitMasker[rowSection];
129                 col = bitMasker[colSection];
130                 HeightMap[col, row] = _flattened_HeightMap[rowcol];
131                 }
132
133                 }
134
135                 }
136
137
138         }
139
140         public class ColumnsMetadata : KeyedCollection<Vec2i, ColumnMeta>
141         {
142                 private ColumnsMetadata()
143                 {
144                         throw new NotSupportedException();
145                 }
146
147                 public ColumnsMetadata(Vec2i startChunkColumn)
148                 {
149                         North_mostChunk = startChunkColumn.Y;
150                         South_mostChunk = startChunkColumn.Y;
151                         East_mostChunk = startChunkColumn.X;
152                         West_mostChunk = startChunkColumn.X;
153                 }
154
155                 public int North_mostChunk
156                 {
157                         get; private set;
158                 }
159
160                 public int South_mostChunk
161                 {
162                         get; private set;
163                 }
164
165                 public int East_mostChunk
166                 {
167                         get; private set;
168                 }
169
170                 public int West_mostChunk
171                 {
172                         get; private set;
173                 }
174
175                 protected override Vec2i GetKeyForItem(ColumnMeta item)
176                 {
177                         return item.Location;
178                 }
179
180                 internal void Update(ColumnMeta metaData)
181                 {
182                         if (this.Contains(metaData.Location))
183                         {
184                                 this.Remove(metaData.Location);
185                                 this.Add(metaData);
186                         }
187                         else
188                         {
189                                 this.Add(metaData);
190                         }
191
192                 }
193
194                 public new void Add(ColumnMeta newItem)
195                 {
196                         if (North_mostChunk > newItem.Location.Y)
197                         {
198                                 North_mostChunk = newItem.Location.Y;
199                         }
200
201                         if (South_mostChunk < newItem.Location.Y)
202                         {
203                                 South_mostChunk = newItem.Location.Y;
204                         }
205
206                         if (East_mostChunk < newItem.Location.X)
207                         {
208                                 East_mostChunk = newItem.Location.X;
209                         }
210
211                         if (West_mostChunk > newItem.Location.X)
212                         {
213                                 West_mostChunk = newItem.Location.X;
214                         }
215
216                         base.Add(newItem);
217                 }
218
219         }
220 }
221