Skip to content

Commit

Permalink
Structures (#424)
Browse files Browse the repository at this point in the history
* Turn world manager into a BackgroundService

* Abstractions refactor pt.1

* Update Region.cs

* Abstractions refactor pt.2

* Move world generator registering to WorldManager

* abstractions pt.3

* Use packet broadcaster

* Fix event parameters

* Simplify ConsoleApp.Program.cs

* Refactor Living.cs

* A little cleanup

* WE START WOOOOO

* Update Player.cs

* oop

* Fix up some NREs and wait for worlds to be ready

* Region loading does not need to be blocked

* Add a direct send method that won't get processed through the queue

* Just use JsonNamingPolicy.SnakeCaseLower :))

* world gen status

* Fix strange chunk unloading bug

* Forgot to save O_O

* Improve caves & ores generation performance

* Update OperatorList & IOperatorList

* Last bit refactor

* Make user cache a service instead

* Add sealed modifier to most classes (final commit)

* OKAY ACTUAL FINAL COMMIT

* Tweaks to rivers, mountain heights and beaches

* Fix bug loading worlds from disk

* Still mucking about

* interim

* interim

* Interim

* Personal code review

* fix biome reload issue

* Ready for review

* Interim

* got it

* kinda works

* EEETS WORKINGGGG

* Cleanup

* Personal code review

* Added my own tree asset

* I made this

---------

Co-authored-by: Tides <[email protected]>
Co-authored-by: Seb-stian <[email protected]>
  • Loading branch information
3 people authored Feb 13, 2024
1 parent 058cc30 commit 25c882b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
Binary file added Obsidian/Assets/Structures/felled_spruce.nbt
Binary file not shown.
1 change: 1 addition & 0 deletions Obsidian/Obsidian.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

<ItemGroup>
<EmbeddedResource Include="Assets/*.*" CopyToOutputDirectory="PreserveNewest" />
<EmbeddedResource Include="Assets/Structures/*.*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
Expand Down
51 changes: 51 additions & 0 deletions Obsidian/Registries/StructureRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Obsidian.Nbt;
using System.Diagnostics;
using System.IO;

namespace Obsidian.Registries;
internal static class StructureRegistry
{
public static readonly ConcurrentDictionary<string, Dictionary<Vector, IBlock>> storage = new();
public static void Initialize()
{
var structDir = "Assets/Structures/";
var files = Directory.GetFiles(structDir, "*.nbt");
foreach (var file in files)
{
var structureName = Path.GetFileNameWithoutExtension(file);
storage[structureName] = new();
byte[] nbtData = File.ReadAllBytes(file);
using var byteStream = new ReadOnlyStream(nbtData);
var nbtReader = new NbtReader(byteStream, NbtCompression.GZip);
var baseCompound = nbtReader.ReadNextTag() as NbtCompound;

// Get palette
List<IBlock> paletteBuffer = new();
if (baseCompound!.TryGetTag("palette", out var palette))
{
foreach (NbtCompound entry in (palette as NbtList).Cast<NbtCompound>())
{
paletteBuffer.Add(entry.ToBlock());
}
}

if (baseCompound.TryGetTag("blocks", out var blocks))
{
foreach (NbtCompound b in (blocks as NbtList).Cast<NbtCompound>())
{
IBlock block = paletteBuffer[b!.GetInt("state")];
if (b!.TryGetTag("pos", out var coords))
{
var c = (NbtList)coords;
var offset = new Vector(
((NbtTag<int>)c[0]).Value,
((NbtTag<int>)c[1]).Value,
((NbtTag<int>)c[2]).Value);

storage[structureName][offset] = block;
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions Obsidian/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ public async Task RunAsync()

await RecipesRegistry.InitializeAsync();

_logger.LogInformation("Loading structures...");
StructureRegistry.Initialize();

await this.userCache.LoadAsync(this._cancelTokenSource.Token);

_logger.LogInformation("Loading properties...");
Expand Down
43 changes: 43 additions & 0 deletions Obsidian/Utilities/Extensions.Nbt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Obsidian.API.Utilities;
using Obsidian.Nbt;
using Obsidian.Registries;
using System.Reflection;
using System.Text.Json;

namespace Obsidian.Utilities;
Expand Down Expand Up @@ -241,6 +242,48 @@ public static NbtCompound ToNbt(this ItemStack? value)
return itemStack;
}

public static IBlock ToBlock(this NbtCompound comp)
{
var name = comp.GetString("Name").Split(":")[1].ToPascalCase();
var assm = Assembly.Load("Obsidian.API");
Type builderType = assm.GetType($"Obsidian.API.BlockStates.Builders.{name}StateBuilder");

if (builderType == null)
{
return BlocksRegistry.Get(comp.GetString("Name"));
}
var inst = Activator.CreateInstance(builderType);

if (comp.TryGetTag("Properties", out var props))
{
foreach (var prop in props as NbtCompound)
{
var instProp = builderType.GetProperty(prop.Key.ToPascalCase());
Type propType = instProp.PropertyType;
if (propType.IsSubclassOf(typeof(Enum)))
{
if (prop.Value is NbtTag<string> enumVal && Enum.TryParse(propType, enumVal.Value.ToPascalCase(), out var val))
instProp.SetValue(inst, val);
}
else if (propType.Name == "Boolean")
{
if (prop.Value is NbtTag<string> boolVal && bool.TryParse(boolVal.Value, out var val))
instProp.SetValue(inst, val);
}
else if (propType.Name == "Int32")
{
if (prop.Value is NbtTag<string> numVal && int.TryParse(numVal.Value, out var val))
instProp.SetValue(inst, val);
}
}
}

MethodInfo buildMeth = builderType.GetMethod("Build");
var bs = (IBlockState)buildMeth.Invoke(inst, null);
var n = comp.GetString("Name");
return BlocksRegistry.Get(n, bs);
}

#region Dimension Codec Writing
public static NbtCompound WriteElement(this DimensionCodec value)
{
Expand Down
1 change: 0 additions & 1 deletion Obsidian/WorldData/Generators/Overworld/ChunkBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Obsidian.ChunkData;
using Obsidian.Registries;
using System.Diagnostics;

namespace Obsidian.WorldData.Generators.Overworld;

Expand Down

0 comments on commit 25c882b

Please sign in to comment.