Skip to content

Commit

Permalink
Working scripting with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Oct 25, 2024
1 parent a973fff commit f522b95
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 13 deletions.
4 changes: 2 additions & 2 deletions CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ public void Update(GameTime gameTime, bool isActive, bool processMouse, bool pro
if (Client.Initialized)
{
Client.ResizeCache(ViewRange.Width * ViewRange.Height / 8);
Client.LoadBlocks(requested);
Client.RequestBlocks(requested);
}
}
if (Client.Initialized && AnimatedStatics)
Expand Down Expand Up @@ -1346,7 +1346,7 @@ public void ExportImage(string path, int widthPx, int heightPx, float zoom)
}

Client.ResizeCache(bounds.Width * bounds.Height / 8);
Client.LoadBlocks(requested);
Client.RequestBlocks(requested);
while(Client.WaitingForBlocks)
Client.Update();

Expand Down
23 changes: 23 additions & 0 deletions CentrEDSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "external", "external", "{6D
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FontStashSharp.FNA.Core", "external\FontStashSharp\src\XNA\FontStashSharp.FNA.Core.csproj", "{CEBAF20C-3334-4215-A33D-A96C03D95B16}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.ReplaceLand", "examples\Example.ReplaceLand\Example.ReplaceLand.csproj", "{6DCF98D1-4762-4EA2-B1DB-E003A75EA5B8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.AddLeaves", "examples\Example.AddLeaves\Example.AddLeaves.csproj", "{C96B28D6-76AC-4E84-B0B2-1535FBF6988D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.DeduplicateTiles", "examples\Example.DeduplicateTiles\Example.DeduplicateTiles.csproj", "{BB12D9B6-F8A9-4706-A1F1-B1B04F8D326F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{009EF43F-B3F9-45AA-A75A-50B53432B8D3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -53,6 +61,18 @@ Global
{CEBAF20C-3334-4215-A33D-A96C03D95B16}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CEBAF20C-3334-4215-A33D-A96C03D95B16}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CEBAF20C-3334-4215-A33D-A96C03D95B16}.Release|Any CPU.Build.0 = Release|Any CPU
{6DCF98D1-4762-4EA2-B1DB-E003A75EA5B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6DCF98D1-4762-4EA2-B1DB-E003A75EA5B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6DCF98D1-4762-4EA2-B1DB-E003A75EA5B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6DCF98D1-4762-4EA2-B1DB-E003A75EA5B8}.Release|Any CPU.Build.0 = Release|Any CPU
{C96B28D6-76AC-4E84-B0B2-1535FBF6988D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C96B28D6-76AC-4E84-B0B2-1535FBF6988D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C96B28D6-76AC-4E84-B0B2-1535FBF6988D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C96B28D6-76AC-4E84-B0B2-1535FBF6988D}.Release|Any CPU.Build.0 = Release|Any CPU
{BB12D9B6-F8A9-4706-A1F1-B1B04F8D326F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB12D9B6-F8A9-4706-A1F1-B1B04F8D326F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB12D9B6-F8A9-4706-A1F1-B1B04F8D326F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB12D9B6-F8A9-4706-A1F1-B1B04F8D326F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -61,6 +81,9 @@ Global
{BEE005A0-7FA5-493D-A887-64E633D65DF5} = {6DF4D008-497E-42DF-901C-57CF609725B9}
{ABC2B808-A581-4FB6-A390-659CA3FA154A} = {6DF4D008-497E-42DF-901C-57CF609725B9}
{CEBAF20C-3334-4215-A33D-A96C03D95B16} = {6DF4D008-497E-42DF-901C-57CF609725B9}
{C96B28D6-76AC-4E84-B0B2-1535FBF6988D} = {009EF43F-B3F9-45AA-A75A-50B53432B8D3}
{BB12D9B6-F8A9-4706-A1F1-B1B04F8D326F} = {009EF43F-B3F9-45AA-A75A-50B53432B8D3}
{6DCF98D1-4762-4EA2-B1DB-E003A75EA5B8} = {009EF43F-B3F9-45AA-A75A-50B53432B8D3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {139A7BAA-1455-4FB6-8B19-F663E2EB5EE7}
Expand Down
47 changes: 37 additions & 10 deletions Client/CentrEDClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,23 @@ public void Disconnect()
while (NetState.FlushPending)

Check warning on line 86 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, CentrED)

Dereference of a possibly null reference.

Check warning on line 86 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, CentrED)

Dereference of a possibly null reference.

Check warning on line 86 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (macos-13, CentrED)

Dereference of a possibly null reference.
NetState.Flush();
}
NetState?.Disconnect();
Running = false;
Landscape = null;
Initialized = false;
Disconnected?.Invoke();
Status = "Disconnected";
while (NetState.Receive())

Check warning on line 89 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, CentrED)

Dereference of a possibly null reference.

Check warning on line 89 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, CentrED)

Dereference of a possibly null reference.

Check warning on line 89 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (macos-13, CentrED)

Dereference of a possibly null reference.
{
//Let it read everything
}
}

public void Dispose()
{
NetState?.Disconnect();
Landscape = null;
Initialized = false;
Running = false;
while (NetState.FlushPending)

Check warning on line 101 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, CentrED)

Dereference of a possibly null reference.

Check warning on line 101 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, CentrED)

Dereference of a possibly null reference.

Check warning on line 101 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (macos-13, CentrED)

Dereference of a possibly null reference.
NetState.Flush();
NetState.Dispose();
Disconnected?.Invoke();
Status = "Disconnected";
}

public void Update()
Expand All @@ -112,16 +115,16 @@ public void Update()
{
Send(new NoOpPacket());
}

NetState.Receive();


if (NetState.FlushPending)
{
if (!NetState.Flush())
{
Disconnect();
}
}

NetState.Receive();
}
catch
{
Expand All @@ -132,8 +135,32 @@ public void Update()

public ushort Width => Landscape?.Width ?? 0;
public ushort Height => Landscape?.Height ?? 0;

public void LoadBlocks(AreaInfo areaInfo)
{
RequestBlocks(areaInfo);
while (WaitingForBlocks)
{
Update();
}
}

public void RequestBlocks(AreaInfo areaInfo)
{
List<BlockCoords> requested = new List<BlockCoords>();
for (var x = areaInfo.Left / 8; x <= areaInfo.Right / 8; x++)
{
for (var y = areaInfo.Top / 8; y <= areaInfo.Bottom / 8; y++)
{
requested.Add(new BlockCoords((ushort)x, (ushort)y));
}
}

ResizeCache(areaInfo.Width * areaInfo.Height / 8);
RequestBlocks(requested);
}

public void LoadBlocks(List<BlockCoords> blockCoords)
public void RequestBlocks(List<BlockCoords> blockCoords)
{
var filteredBlockCoords = blockCoords.FindAll
(b => !Landscape.BlockCache.Contains(Block.Id(b.X, b.Y)) && !RequestedBlocks.Contains(b) && IsValidX(b.X) && IsValidY(b.Y));

Check warning on line 166 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, CentrED)

Dereference of a possibly null reference.

Check warning on line 166 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, CentrED)

Dereference of a possibly null reference.

Check warning on line 166 in Client/CentrEDClient.cs

View workflow job for this annotation

GitHub Actions / build (macos-13, CentrED)

Dereference of a possibly null reference.
Expand Down
7 changes: 7 additions & 0 deletions Client/ConnectionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static ConnectionHandling()
Handlers[0x01] = new PacketHandler<CentrEDClient>(0, OnProtocolVersionPacket);
Handlers[0x03] = new PacketHandler<CentrEDClient>(0, OnLoginResponsePacket);
Handlers[0x04] = new PacketHandler<CentrEDClient>(0, OnServerStatePacket);
Handlers[0x05] = new PacketHandler<CentrEDClient>(0, OnQuitAckPacket);
}

public static void OnConnectionHandlerPacket(BinaryReader reader, NetState<CentrEDClient> ns)
Expand Down Expand Up @@ -98,4 +99,10 @@ private static void OnServerStatePacket(BinaryReader reader, NetState<CentrEDCli
ns.Parent.Status = reader.ReadStringNull();
}
}

private static void OnQuitAckPacket(BinaryReader reader, NetState<CentrEDClient> ns)
{
ns.LogDebug("Client OnQuitAckPacket");
ns.Parent.Dispose();
}
}
1 change: 1 addition & 0 deletions Server/ConnectionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private static void OnLoginRequestPacket(BinaryReader reader, NetState<CEDServer
private static void OnQuitPacket(BinaryReader reader, NetState<CEDServer> ns)
{
ns.LogDebug("Server OnQuitPacket");
ns.Send(new QuitAckPacket());
ns.Disconnect();
}
}
8 changes: 8 additions & 0 deletions Server/Packets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ public LoginResponsePacket(LoginState state, NetState<CEDServer>? ns = null) : b
}
}

public class QuitAckPacket : Packet
{
public QuitAckPacket() : base(0x02, 0)
{
Writer.Write((byte)0x05);
}
}

public class ServerStatePacket : Packet
{
public ServerStatePacket(ServerState state, string message = "") : base(0x02, 0)
Expand Down
3 changes: 3 additions & 0 deletions Shared/Network/AreaInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public AreaInfo(ushort left, ushort top, ushort right, ushort bottom)
Right = right;
Bottom = bottom;
}

public ushort Width => (ushort)(Right - Left);
public ushort Height => (ushort)(Bottom - Top);

public AreaInfo(BinaryReader reader)
{
Expand Down
2 changes: 1 addition & 1 deletion Shared/Network/NetState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public bool Flush()
try
{
_sendStream.Position = 0;
if (_sendStream.Length > 0)
while (_sendStream.Length > 0)
{
var buffer = new byte[_sendStream.Length];
var bytesCount = _sendStream.Read(buffer);
Expand Down
14 changes: 14 additions & 0 deletions examples/Example.AddLeaves/Example.AddLeaves.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Client\Client.csproj" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions examples/Example.AddLeaves/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using CentrED;
using CentrED.Client;
using CentrED.Network;

var start = DateTime.Now;

Dictionary<ushort, ushort> treeToLeaves = new Dictionary<ushort, ushort>
{
{0x0CCD, 0x0CCE}, {0x0CD0, 0x0CD1}, {0x0CD3, 0x0CD4}
};
ushort x1 = 0;
ushort y1 = 0;
ushort x2 = 100;
ushort y2 = 100;

CentrEDClient client = new CentrEDClient();
client.Connect("127.0.0.1", 2597, "user", "password");

client.LoadBlocks(new AreaInfo(x1, y1, x2, y2));
for (var x = x1; x < x2; x++)
{
for (var y = y1; y < y2; y++)
{
StaticTile tree = null;
StaticTile leaves = null;
foreach (var tile in client.GetStaticTiles(x, y))
{
if (treeToLeaves.ContainsKey(tile.Id))
{
tree = tile;
}else if (treeToLeaves.Values.Contains(tile.Id))
{
leaves = tile;
}
}
if (tree != null && leaves == null)
{
client.Add(new StaticTile(treeToLeaves[tree.Id], tree.X, tree.Y, tree.Z, 0));
}
client.Update();
}
}
client.Disconnect();

Console.WriteLine($"Elapsed: {(DateTime.Now - start).TotalMilliseconds}ms");
14 changes: 14 additions & 0 deletions examples/Example.DeduplicateTiles/Example.DeduplicateTiles.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Client\Client.csproj" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions examples/Example.DeduplicateTiles/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using CentrED.Client;
using CentrED.Network;

var start = DateTime.Now;

ushort[] duplicatedTiles = [0x1CD9, 0x1CDA, 0x1CDB, 0x1CDC];
ushort x1 = 0;
ushort y1 = 0;
ushort x2 = 100;
ushort y2 = 100;

CentrEDClient client = new CentrEDClient();
client.Connect("127.0.0.1", 2597, "user", "password");

client.LoadBlocks(new AreaInfo(x1, y1, x2, y2));
for (var x = x1; x < x2; x++)
{
for (var y = y1; y < y2; y++)
{
if(client.TryGetStaticTiles(x,y, out var statics))
{
var filtered = statics.Where(tile => duplicatedTiles.Contains(tile.Id)).ToArray();
for (var i = 1; i < filtered.Length; i++)
{
client.Remove(filtered[i]);
}
}
client.Update();
}
}
client.Disconnect();

Console.WriteLine($"Elapsed: {(DateTime.Now - start).TotalMilliseconds}ms");
14 changes: 14 additions & 0 deletions examples/Example.ReplaceLand/Example.ReplaceLand.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Client\Client.csproj" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions examples/Example.ReplaceLand/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using CentrED.Client;
using CentrED.Network;

var start = DateTime.Now;

ushort[] grassTiles = [0x3, 0x4, 0x5, 0x6];
ushort x1 = 0;
ushort y1 = 0;
ushort x2 = 100;
ushort y2 = 100;

CentrEDClient client = new CentrEDClient();
client.Connect("127.0.0.1", 2597, "user", "password");

client.LoadBlocks(new AreaInfo(x1, y1, x2, y2));
for (var x = x1; x < x2; x++)
{
for (var y = y1; y < y2; y++)
{
if(client.TryGetLandTile(x,y, out var landTile))
{
landTile.Id = grassTiles[Random.Shared.Next(grassTiles.Length)];
}
client.Update();
}
}
client.Disconnect();

Console.WriteLine($"Elapsed: {(DateTime.Now - start).TotalMilliseconds}ms");

0 comments on commit f522b95

Please sign in to comment.