-
Notifications
You must be signed in to change notification settings - Fork 42
/
Region.cs
348 lines (273 loc) · 12.2 KB
/
Region.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
using Microsoft.Extensions.Logging;
using Obsidian.API.Utilities;
using Obsidian.ChunkData;
using Obsidian.Entities;
using Obsidian.Nbt;
using Obsidian.Registries;
using Obsidian.Utilities.Collections;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace Obsidian.WorldData;
public class Region : IRegion
{
public const int CubicRegionSizeShift = 5;
public const int CubicRegionSize = 1 << CubicRegionSizeShift;
public int X { get; }
public int Z { get; }
public bool IsDirty { get; private set; } = true;
public string RegionFolder { get; }
public NbtCompression ChunkCompression { get; }
public ConcurrentDictionary<int, Entity> Entities { get; } = new();
public int LoadedChunkCount => loadedChunks.Count(c => c.IsGenerated);
private DenseCollection<Chunk> loadedChunks { get; } = new(CubicRegionSize, CubicRegionSize);
private readonly RegionFile regionFile;
private readonly ConcurrentDictionary<Vector, BlockUpdate> blockUpdates = new();
internal Region(int x, int z, string worldFolderPath, NbtCompression chunkCompression = NbtCompression.ZLib,
ILogger? logger = null)
{
X = x;
Z = z;
RegionFolder = Path.Join(worldFolderPath, "regions");
Directory.CreateDirectory(RegionFolder);
var filePath = Path.Join(RegionFolder, $"r.{X}.{Z}.mca");
regionFile = new RegionFile(filePath, chunkCompression, CubicRegionSize, logger);
ChunkCompression = chunkCompression;
}
internal void AddBlockUpdate(BlockUpdate bu)
{
if (!blockUpdates.TryAdd(bu.position, bu))
{
blockUpdates[bu.position] = bu;
}
}
internal Task<bool> InitAsync() => regionFile.InitializeAsync();
internal async Task FlushAsync()
{
foreach (Chunk c in loadedChunks)
await SerializeChunkAsync(c);
regionFile.Flush();
}
internal async ValueTask<Chunk> GetChunkAsync(int x, int z)
{
var chunk = loadedChunks[x, z];
if (chunk is null)
{
chunk = await GetChunkFromFileAsync(x, z); // Still might be null but that's okay.
loadedChunks[x, z] = chunk!;
}
return chunk!;
}
internal async Task UnloadChunk(int x, int z)
{
var chunk = loadedChunks[x, z];
if (chunk is null) { return; }
await SerializeChunkAsync(chunk);
loadedChunks[x, z] = null;
}
private async Task<Chunk?> GetChunkFromFileAsync(int x, int z)
{
var chunkBuffer = await regionFile.GetChunkBytesAsync(x, z);
if (chunkBuffer is not Memory<byte> chunkData)
return null;
await using var bytesStream = new ReadOnlyStream(chunkData);
var nbtReader = new NbtReader(bytesStream);
return DeserializeChunk(nbtReader.ReadNextTag() as NbtCompound);
}
internal IEnumerable<Chunk> GeneratedChunks()
{
foreach (var c in loadedChunks)
{
if (c is not null && c.IsGenerated)
{
yield return c;
}
}
}
internal void SetChunk(Chunk chunk)
{
if (chunk is null) { return; } // I dunno... maybe we'll need to null out a chunk someday?
var (x, z) = (NumericsHelper.Modulo(chunk.X, CubicRegionSize), NumericsHelper.Modulo(chunk.Z, CubicRegionSize));
loadedChunks[x, z] = chunk;
}
internal async Task SerializeChunkAsync(Chunk chunk)
{
NbtCompound chunkNbt = SerializeChunk(chunk);
var (x, z) = (NumericsHelper.Modulo(chunk.X, CubicRegionSize), NumericsHelper.Modulo(chunk.Z, CubicRegionSize));
await using MemoryStream strm = new();
await using NbtWriter writer = new(strm, ChunkCompression);
writer.WriteTag(chunkNbt);
await writer.TryFinishAsync();
await regionFile.SetChunkAsync(x, z, strm.ToArray());
}
internal async Task BeginTickAsync(CancellationToken cts = default)
{
//var timer = new BalancingTimer(50, cts);
//while (await timer.WaitForNextTickAsync())
//{
//}
await Task.WhenAll(Entities.Select(entityEntry => entityEntry.Value.TickAsync()));
List<BlockUpdate> neighborUpdates = [];
List<BlockUpdate> delayed = [];
foreach (var pos in blockUpdates.Keys)
{
blockUpdates.Remove(pos, out var bu);
if (bu.delayCounter > 0)
{
bu.delayCounter--;
delayed.Add(bu);
}
else
{
bool updateNeighbor = await bu.world.HandleBlockUpdateAsync(bu);
if (updateNeighbor) { neighborUpdates.Add(bu); }
}
}
delayed.ForEach(i => AddBlockUpdate(i));
neighborUpdates.ForEach(async u => await u.world.BlockUpdateNeighborsAsync(u));
}
#region NBT Ops
private static Chunk DeserializeChunk(NbtCompound chunkCompound)
{
int x = chunkCompound.GetInt("xPos");
int z = chunkCompound.GetInt("zPos");
var chunk = new Chunk(x, z);
foreach (var child in (NbtList)chunkCompound["sections"])
{
if (child is not NbtCompound sectionCompound)
throw new InvalidOperationException("Nbt Tag is not a compound.");
var secY = (int)sectionCompound.GetByte("Y");
secY = secY > 20 ? secY - 256 : secY;
if (!sectionCompound.TryGetTag("block_states", out var statesTag))
throw new UnreachableException("Unable to find block states from NBT.");
var statesCompound = statesTag as NbtCompound;
var section = chunk.Sections[secY + 4];
var chunkSecPalette = section.BlockStateContainer.Palette;
if (statesCompound!.TryGetTag("palette", out var palleteArrayTag))
{
var blockStatesPalette = palleteArrayTag as NbtList;
foreach (NbtCompound entry in blockStatesPalette!)
{
var id = entry.GetInt("Id");
chunkSecPalette.GetOrAddId(BlocksRegistry.Get(id));//TODO PROCESS ADDED PROPERTIES TO GET CORRECT BLOCK STATE
}
section.BlockStateContainer.GrowDataArray();
}
//TODO find a way around this (We shouldn't be storing the states data array in nbt anyway.)
if (statesCompound.TryGetTag("data", out var dataArrayTag))
{
var data = dataArrayTag as NbtArray<long>;
section.BlockStateContainer.DataArray.storage = data!.GetArray();
}
var biomesCompound = sectionCompound["biomes"] as NbtCompound;
var biomesPalette = biomesCompound!["palette"] as NbtList;
var biomePalette = section.BiomeContainer.Palette;
foreach (NbtTag<string> biome in biomesPalette!)
{
if (Enum.TryParse<Biome>(biome.Value.TrimResourceTag(), true, out var value))
biomePalette.GetOrAddId(value);
}
if (biomesPalette.Count > 1)
{
if (biomesCompound.TryGetTag("data", out var biomeDataArrayTag))
{
var data = biomeDataArrayTag as NbtArray<long>;
section.BiomeContainer.DataArray.storage = data!.GetArray();
}
}
if (sectionCompound.TryGetTag("SkyLight", out var skyLightTag))
{
var array = (NbtArray<byte>)skyLightTag;
section.SetLight(array.GetArray(), LightType.Sky);
}
if (sectionCompound.TryGetTag("BlockLight", out var blockLightTag))
{
var array = (NbtArray<byte>)blockLightTag;
section.SetLight(array.GetArray(), LightType.Block);
}
}
foreach (var (name, heightmap) in (NbtCompound)chunkCompound["Heightmaps"])
{
var heightmapType = (HeightmapType)Enum.Parse(typeof(HeightmapType), name.Replace("_", ""), true);
chunk.Heightmaps[heightmapType].data.storage = ((NbtArray<long>)heightmap).GetArray();
}
foreach (var tileEntityNbt in (NbtList)chunkCompound["block_entities"])
{
var tileEntityCompound = tileEntityNbt as NbtCompound;
chunk.SetBlockEntity(tileEntityCompound.GetInt("x"), tileEntityCompound.GetInt("y"), tileEntityCompound.GetInt("z"), tileEntityCompound);
}
chunk.chunkStatus = (ChunkStatus)(Enum.TryParse(typeof(ChunkStatus), chunkCompound.GetString("Status"), out var status) ? status : ChunkStatus.empty);
return chunk;
}
private static NbtCompound SerializeChunk(Chunk chunk)
{
var sectionsCompound = new NbtList(NbtTagType.Compound, "sections");
foreach (var section in chunk.Sections)
{
if (section.YBase is null)
throw new UnreachableException("Section Ybase should not be null");//THIS should never happen
var blockStatesCompound = new NbtCompound("block_states");
if (section.BlockStateContainer.Palette is IndirectPalette indirect)
{
var palette = new NbtList(NbtTagType.Compound, "palette");
Span<int> span = indirect.Values;
for (int i = 0; i < indirect.Count; i++)
{
var id = span[i];
var block = BlocksRegistry.Get(id);
palette.Add(new NbtCompound
{
new NbtTag<string>("Name", block.UnlocalizedName),
new NbtTag<int>("Id", id)
});//TODO INCLUDE PROPERTIES
}
blockStatesCompound.Add(palette);
blockStatesCompound.Add(new NbtArray<long>("data", section.BlockStateContainer.DataArray.storage));
}
var biomesCompound = new NbtCompound("biomes");
if (section.BiomeContainer.Palette is BaseIndirectPalette<Biome> indirectBiomePalette)
{
var palette = new NbtList(NbtTagType.String, "palette");
foreach (var id in indirectBiomePalette.Values)
{
var biome = (Biome)id;
palette.Add(new NbtTag<string>(string.Empty, $"minecraft:{biome.ToString().ToLower()}"));
}
biomesCompound.Add(palette);
if (indirectBiomePalette.Values.Length > 1)
biomesCompound.Add(new NbtArray<long>("data", section.BiomeContainer.DataArray.storage));
}
sectionsCompound.Add(new NbtCompound
{
new NbtTag<byte>("Y", (byte)section.YBase),
blockStatesCompound,
biomesCompound,
new NbtArray<byte>("SkyLight", section.SkyLightArray.ToArray()),
new NbtArray<byte>("BlockLight", section.BlockLightArray.ToArray())
});
}
var blockEntities = new NbtList(NbtTagType.Compound, "block_entities");
foreach (var (_, blockEntity) in chunk.BlockEntities)
blockEntities.Add(blockEntity);
#pragma warning disable IDE0028 // Use collection initializers - Will not compile with this suggestion applied
return new NbtCompound
{
new NbtTag<int>("xPos", chunk.X),
new NbtTag<int>("zPos", chunk.Z),
new NbtTag<int>("yPos", -4),
new NbtTag<string>("Status", chunk.chunkStatus.ToString()),
new NbtCompound("Heightmaps")
{
new NbtArray<long>("MOTION_BLOCKING", chunk.Heightmaps[HeightmapType.MotionBlocking].data.storage),
//new NbtArray<long>("OCEAN_FLOOR", chunk.Heightmaps[HeightmapType.OceanFloor].data.Storage),
//new NbtArray<long>("WORLD_SURFACE", chunk.Heightmaps[HeightmapType.WorldSurface].data.Storage),
},
blockEntities,
sectionsCompound,
new NbtTag<int>("DataVersion", 3337)// Hardcoded version try to get data version through minecraft data and use data correctly
};
#pragma warning restore IDE0028 // Use collection initializers
}
#endregion NBT Ops
public async ValueTask DisposeAsync() => await regionFile.DisposeAsync();
}