OSDN Git Service

Merge branch 'Split_renderers' into vgd
[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                 [ProtoBeforeSerialization]
93                 private void PrepareData( )
94                 {
95
96                 if (HeightMap != null) {
97                         _flattened_HeightMap = new ushort[ChunkSize * ChunkSize];
98                 int flatIndex = 0;
99
100                 for (byte col = 0; col < ChunkSize; col++) {
101                         for (byte row = 0; row < ChunkSize; row++) {
102                         _flattened_HeightMap[flatIndex] = HeightMap[col, row];
103                         flatIndex++;
104                         }
105                 }
106
107                 }
108
109                 }
110
111
112                 [ProtoAfterDeserialization]
113                 private void PostProcess( )
114                 {
115                 if (this.HeightMap == null)     this.HeightMap = new ushort[ChunkSize, ChunkSize];
116
117                 if (_flattened_HeightMap != null) {
118                 int col, row;
119
120                 BitVector32 bitMasker = new BitVector32(0);
121                 var rowSection = BitVector32.CreateSection(ChunkSize);
122                 var colSection = BitVector32.CreateSection(ChunkSize, rowSection);
123
124                 for (uint rowcol = 0; rowcol < (ChunkSize * ChunkSize); rowcol++) {
125                 bitMasker = new BitVector32(data: ( int )rowcol);
126                 row = bitMasker[rowSection];
127                 col = bitMasker[colSection];
128                 HeightMap[col, row] = _flattened_HeightMap[rowcol];
129                 }
130
131                 }
132
133                 }
134
135
136         }
137
138         public class ColumnsMetadata : KeyedCollection<Vec2i, ColumnMeta>
139         {
140                 private ColumnsMetadata()
141                 {
142                         throw new NotSupportedException();
143                 }
144
145                 public ColumnsMetadata(Vec2i startChunkColumn)
146                 {
147                         North_mostChunk = startChunkColumn.Y;
148                         South_mostChunk = startChunkColumn.Y;
149                         East_mostChunk = startChunkColumn.X;
150                         West_mostChunk = startChunkColumn.X;
151                 }
152
153                 public int North_mostChunk
154                 {
155                         get; private set;
156                 }
157
158                 public int South_mostChunk
159                 {
160                         get; private set;
161                 }
162
163                 public int East_mostChunk
164                 {
165                         get; private set;
166                 }
167
168                 public int West_mostChunk
169                 {
170                         get; private set;
171                 }
172
173                 protected override Vec2i GetKeyForItem(ColumnMeta item)
174                 {
175                         return item.Location;
176                 }
177
178                 internal void Update(ColumnMeta metaData)
179                 {
180                         if (this.Contains(metaData.Location))
181                         {
182                                 this.Remove(metaData.Location);
183                                 this.Add(metaData);
184                         }
185                         else
186                         {
187                                 this.Add(metaData);
188                         }
189
190                 }
191
192                 public new void Add(ColumnMeta newItem)
193                 {
194                         if (North_mostChunk > newItem.Location.Y)
195                         {
196                                 North_mostChunk = newItem.Location.Y;
197                         }
198
199                         if (South_mostChunk < newItem.Location.Y)
200                         {
201                                 South_mostChunk = newItem.Location.Y;
202                         }
203
204                         if (East_mostChunk < newItem.Location.X)
205                         {
206                                 East_mostChunk = newItem.Location.X;
207                         }
208
209                         if (West_mostChunk > newItem.Location.X)
210                         {
211                                 West_mostChunk = newItem.Location.X;
212                         }
213
214                         base.Add(newItem);
215                 }
216
217         }
218 }
219