Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/floating islands #354

Merged
merged 10 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Obsidian/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ await QueuePacketAsync(new UpdateRecipeBookPacket

await SendPlayerListDecoration();
await SendPlayerInfoAsync();
await Player.UpdateChunksAsync(distance: 7);
while (!await Player.UpdateChunksAsync(distance: 7)) { Thread.Sleep(1); }
Tides marked this conversation as resolved.
Show resolved Hide resolved
Jonpro03 marked this conversation as resolved.
Show resolved Hide resolved
await SendInfoAsync();
await Server.Events.InvokePlayerJoinAsync(new PlayerJoinEventArgs(Player, DateTimeOffset.Now));
}
Expand Down
12 changes: 9 additions & 3 deletions Obsidian/Entities/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ public async Task LoadAsync(bool loadFromPersistentWorld = true)

if (!playerDataFile.Exists)
{
this.Position = new VectorF(0, 128, 0);
this.Position = World.LevelData.SpawnPosition;
return;
}

Expand Down Expand Up @@ -942,8 +942,9 @@ private async Task PickupNearbyItemsAsync(float distance = 0.5f)
/// <param name="unloadAll"></param>
/// <param name="distance"></param>
/// <returns></returns>
Jonpro03 marked this conversation as resolved.
Show resolved Hide resolved
internal async Task UpdateChunksAsync(bool unloadAll = false, int distance = 0)
internal async Task<bool> UpdateChunksAsync(bool unloadAll = false, int distance = 0)
{
bool sentAll = true;
if (unloadAll)
{
if (!Respawning)
Expand All @@ -961,7 +962,7 @@ internal async Task UpdateChunksAsync(bool unloadAll = false, int distance = 0)
(int playerChunkX, int playerChunkZ) = Position.ToChunkCoord();
(int lastPlayerChunkX, int lastPlayerChunkZ) = LastPosition.ToChunkCoord();

int dist = distance < 1 ? this.ClientInformation.ViewDistance : distance;
int dist = distance < 1 ? ClientInformation.ViewDistance : distance;
for (int x = playerChunkX + dist; x > playerChunkX - dist; x--)
for (int z = playerChunkZ + dist; z > playerChunkZ - dist; z--)
clientNeededChunks.Add((x, z));
Expand Down Expand Up @@ -990,7 +991,12 @@ await Parallel.ForEachAsync(clientNeededChunks, async (chunkLoc, _) =>
await client.SendChunkAsync(chunk);
client.LoadedChunks.Add((chunk.X, chunk.Z));
}
else
{
sentAll = false;
}
});
return sentAll;
}

private void WriteItems(NbtWriter writer, bool inventory = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async ValueTask HandleAsync(Server server, Player player)
await player.UpdateAsync(Position, OnGround);
if (player.Position.ToChunkCoord() != player.LastPosition.ToChunkCoord())
{
await player.UpdateChunksAsync();
await player.UpdateChunksAsync(distance: player.ClientInformation.ViewDistance);
(int cx, int cz) = player.Position.ToChunkCoord();
player.client.SendPacket(new SetCenterChunkPacket(cx, cz));
}
Expand Down
205 changes: 103 additions & 102 deletions Obsidian/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static string VERSION
get
{
var informalVersion = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if(informalVersion != null && informalVersion.InformationalVersion.Contains('+'))
if (informalVersion != null && informalVersion.InformationalVersion.Contains('+'))
return informalVersion.InformationalVersion.Split('+')[1];

return "0.1";
Expand Down Expand Up @@ -364,7 +364,7 @@ private async Task AcceptClientsAsync()
return;
}

if(this.Config.CanThrottle)
if (this.Config.CanThrottle)
{
if (throttler.TryGetValue(ip, out var time) && time <= DateTimeOffset.UtcNow)
{
Expand Down Expand Up @@ -507,132 +507,132 @@ internal async Task BroadcastPlayerDigAsync(PlayerDiggingStore store, IBlock blo
switch (digging.Status)
{
case DiggingStatus.DropItem:
{
var droppedItem = player.GetHeldItem();
{
var droppedItem = player.GetHeldItem();

if (droppedItem is null or { Type: Material.Air })
return;
if (droppedItem is null or { Type: Material.Air })
return;

var loc = new VectorF(player.Position.X, (float)player.HeadY - 0.3f, player.Position.Z);
var loc = new VectorF(player.Position.X, (float)player.HeadY - 0.3f, player.Position.Z);

var item = new ItemEntity
{
EntityId = player + player.World.GetTotalLoadedEntities() + 1,
Count = 1,
Id = droppedItem.AsItem().Id,
Glowing = true,
World = player.World,
Position = loc
};
var item = new ItemEntity
{
EntityId = player + player.World.GetTotalLoadedEntities() + 1,
Count = 1,
Id = droppedItem.AsItem().Id,
Glowing = true,
World = player.World,
Position = loc
};

TryAddEntity(player.World, item);
TryAddEntity(player.World, item);

var lookDir = player.GetLookDirection();
var lookDir = player.GetLookDirection();

var vel = Velocity.FromDirection(loc, lookDir);//TODO properly shoot the item towards the direction the players looking at
var vel = Velocity.FromDirection(loc, lookDir);//TODO properly shoot the item towards the direction the players looking at

BroadcastPacket(new SpawnEntityPacket
{
EntityId = item.EntityId,
Uuid = item.Uuid,
Type = EntityType.Item,
Position = item.Position,
Pitch = 0,
Yaw = 0,
Data = 1,
Velocity = vel
});
BroadcastPacket(new SetEntityMetadataPacket
{
EntityId = item.EntityId,
Entity = item
});
BroadcastPacket(new SpawnEntityPacket
{
EntityId = item.EntityId,
Uuid = item.Uuid,
Type = EntityType.Item,
Position = item.Position,
Pitch = 0,
Yaw = 0,
Data = 1,
Velocity = vel
});
BroadcastPacket(new SetEntityMetadataPacket
{
EntityId = item.EntityId,
Entity = item
});

player.Inventory.RemoveItem(player.inventorySlot, player.Sneaking ? 64 : 1);//TODO get max stack size for the item
player.Inventory.RemoveItem(player.inventorySlot, player.Sneaking ? 64 : 1);//TODO get max stack size for the item

player.client.SendPacket(new SetContainerSlotPacket
{
Slot = player.inventorySlot,
player.client.SendPacket(new SetContainerSlotPacket
{
Slot = player.inventorySlot,

WindowId = 0,
WindowId = 0,

SlotData = player.GetHeldItem(),
SlotData = player.GetHeldItem(),

StateId = player.Inventory.StateId++
});
StateId = player.Inventory.StateId++
});

break;
}
break;
}
case DiggingStatus.StartedDigging:
{
BroadcastPacket(new AcknowledgeBlockChangePacket
{
SequenceID = 0
});
BroadcastPacket(new AcknowledgeBlockChangePacket
{
SequenceID = 0
});

if (player.Gamemode == Gamemode.Creative)
{
await player.World.SetBlockAsync(digging.Position, BlocksRegistry.Get(Material.Air));
if (player.Gamemode == Gamemode.Creative)
{
await player.World.SetBlockAsync(digging.Position, BlocksRegistry.Get(Material.Air));
}
}
}
break;
break;
case DiggingStatus.CancelledDigging:
break;
case DiggingStatus.FinishedDigging:
{
BroadcastPacket(new AcknowledgeBlockChangePacket//TODO properly implement this
{
SequenceID = 0
});
BroadcastPacket(new AcknowledgeBlockChangePacket//TODO properly implement this
{
SequenceID = 0
});

BroadcastPacket(new SetBlockDestroyStagePacket
{
EntityId = player,
Position = digging.Position,
DestroyStage = -1
});
BroadcastPacket(new SetBlockDestroyStagePacket
{
EntityId = player,
Position = digging.Position,
DestroyStage = -1
});

BroadcastPacket(new BlockUpdatePacket(digging.Position, 0));
BroadcastPacket(new BlockUpdatePacket(digging.Position, 0));

var droppedItem = ItemsRegistry.Get(block.Material);
var droppedItem = ItemsRegistry.Get(block.Material);

if (droppedItem.Id == 0) { break; }
if (droppedItem.Id == 0) { break; }

var item = new ItemEntity
{
EntityId = player + player.World.GetTotalLoadedEntities() + 1,
Count = 1,
Id = droppedItem.Id,
Glowing = true,
World = player.World,
Position = digging.Position,
Server = this
};

TryAddEntity(player.World, item);

BroadcastPacket(new SpawnEntityPacket
{
EntityId = item.EntityId,
Uuid = item.Uuid,
Type = EntityType.Item,
Position = item.Position,
Pitch = 0,
Yaw = 0,
Data = 1,
Velocity = Velocity.FromVector(digging.Position + new VectorF(
(Globals.Random.NextFloat() * 0.5f) + 0.25f,
(Globals.Random.NextFloat() * 0.5f) + 0.25f,
(Globals.Random.NextFloat() * 0.5f) + 0.25f))
});

BroadcastPacket(new SetEntityMetadataPacket
{
EntityId = item.EntityId,
Entity = item
});
break;
}
var item = new ItemEntity
{
EntityId = player + player.World.GetTotalLoadedEntities() + 1,
Count = 1,
Id = droppedItem.Id,
Glowing = true,
World = player.World,
Position = digging.Position,
Server = this
};

TryAddEntity(player.World, item);

BroadcastPacket(new SpawnEntityPacket
{
EntityId = item.EntityId,
Uuid = item.Uuid,
Type = EntityType.Item,
Position = item.Position,
Pitch = 0,
Yaw = 0,
Data = 1,
Velocity = Velocity.FromVector(digging.Position + new VectorF(
(Globals.Random.NextFloat() * 0.5f) + 0.25f,
(Globals.Random.NextFloat() * 0.5f) + 0.25f,
(Globals.Random.NextFloat() * 0.5f) + 0.25f))
});

BroadcastPacket(new SetEntityMetadataPacket
{
EntityId = item.EntityId,
Entity = item
});
break;
}
}
}

Expand Down Expand Up @@ -735,6 +735,7 @@ private void RegisterDefaults()
{
this.RegisterWorldGenerator<SuperflatGenerator>();
this.RegisterWorldGenerator<OverworldGenerator>();
this.RegisterWorldGenerator<IslandGenerator>();
this.RegisterWorldGenerator<EmptyWorldGenerator>();
}

Expand Down
12 changes: 6 additions & 6 deletions Obsidian/WorldData/Chunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public Biome GetBiome(int x, int y, int z)
{
var i = SectionIndex(y);

x = NumericsHelper.Modulo(x, 16);
z = NumericsHelper.Modulo(z, 16);
y = (y + 64) % 16 / 4;
x = NumericsHelper.Modulo(x, 16) >> 2;
z = NumericsHelper.Modulo(z, 16) >> 2;
y = NumericsHelper.Modulo(y + 64, 16) >> 2;

return Sections[i].GetBiome(x, y, z);
}
Expand All @@ -87,9 +87,9 @@ public void SetBiome(int x, int y, int z, Biome biome)
{
int i = SectionIndex(y);

x = NumericsHelper.Modulo(x, 16);
y = (y + 64) % 16 / 4;
z = NumericsHelper.Modulo(z, 16);
x = NumericsHelper.Modulo(x, 16) >> 2;
y = NumericsHelper.Modulo(y + 64, 16) >> 2;
z = NumericsHelper.Modulo(z, 16) >> 2;

Sections[i].SetBiome(x, y, z, biome);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Obsidian.Registries;
using Obsidian.WorldData.Generators;

namespace Obsidian.WorldData.Generators.Overworld.Decorators;
namespace Obsidian.WorldData.Decorators;

public sealed class BadlandsDecorator : BaseDecorator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Obsidian.Registries;
using Obsidian.WorldData.Generators.Overworld.Features.Flora;
using Obsidian.WorldData.Generators.Overworld.Features.Trees;
using Obsidian.WorldData.Generators;
using Obsidian.WorldData.Features.Flora;
using Obsidian.WorldData.Features.Trees;

namespace Obsidian.WorldData.Generators.Overworld.Decorators;
namespace Obsidian.WorldData.Decorators;

public class BambooJungleDecorator : BaseDecorator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Obsidian.Registries;
using Obsidian.WorldData.Generators;
using Obsidian.WorldData.Generators.Overworld;

namespace Obsidian.WorldData.Generators.Overworld.Decorators;
namespace Obsidian.WorldData.Decorators;

public abstract class BaseDecorator : IDecorator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Obsidian.Registries;
using Obsidian.WorldData.Generators;

namespace Obsidian.WorldData.Generators.Overworld.Decorators;
namespace Obsidian.WorldData.Decorators;

public class BeachDecorator : BaseDecorator
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Obsidian.Registries;
using Obsidian.WorldData.Generators.Overworld.Features.Flora;
using Obsidian.WorldData.Generators.Overworld.Features.Trees;
using Obsidian.WorldData.Generators;
using Obsidian.WorldData.Features.Flora;
using Obsidian.WorldData.Features.Trees;

namespace Obsidian.WorldData.Generators.Overworld.Decorators;
namespace Obsidian.WorldData.Decorators;

public class BirchForestDecorator : BaseDecorator
{
Expand Down
Loading