Skip to content

Commit eb8c059

Browse files
committed
add debug commands for block walking (closes #9)
1 parent c486871 commit eb8c059

4 files changed

Lines changed: 133 additions & 25 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using Common.Mod.Extensions;
2+
using Vintagestory.API.Common;
3+
using Vintagestory.API.MathTools;
4+
using Vintagestory.API.Server;
5+
6+
namespace Common.Mod.Example.Commands;
7+
8+
public class DebugCommand
9+
{
10+
private readonly ICoreAPI _api;
11+
12+
public DebugCommand(ICoreAPI api)
13+
{
14+
_api = api;
15+
16+
var command = api.ChatCommands.Create("ex-debug")
17+
.RequiresPlayer()
18+
.RequiresPrivilege(Privilege.chat);
19+
20+
{
21+
var walkCommand = command.BeginSubCommand("walk");
22+
23+
walkCommand.BeginSubCommand("cuboid")
24+
.WithArgs(
25+
api.ChatCommands.Parsers.WorldPosition("position"),
26+
api.ChatCommands.Parsers.Vec3i("size")
27+
)
28+
.HandleWith(DebugWalkCuboid)
29+
.EndSubCommand();
30+
31+
walkCommand.BeginSubCommand("cube")
32+
.WithArgs(
33+
api.ChatCommands.Parsers.WorldPosition("position"),
34+
api.ChatCommands.Parsers.IntRange("size", 0, int.MaxValue)
35+
)
36+
.HandleWith(DebugWalkCube)
37+
.EndSubCommand();
38+
39+
walkCommand.BeginSubCommand("cylinder")
40+
.WithArgs(
41+
api.ChatCommands.Parsers.WorldPosition("position"),
42+
api.ChatCommands.Parsers.IntRange("radius", 0, int.MaxValue)
43+
)
44+
.HandleWith(DebugWalkCylinder)
45+
.EndSubCommand();
46+
47+
walkCommand.BeginSubCommand("sphere")
48+
.WithArgs(
49+
api.ChatCommands.Parsers.WorldPosition("position"),
50+
api.ChatCommands.Parsers.IntRange("radius", 0, int.MaxValue)
51+
)
52+
.HandleWith(DebugWalkSphere)
53+
.EndSubCommand();
54+
55+
walkCommand.EndSubCommand();
56+
}
57+
}
58+
59+
private TextCommandResult DebugWalkCuboid(TextCommandCallingArgs args)
60+
{
61+
var position = (Vec3d)args[0];
62+
var size = (Vec3i)args[1];
63+
64+
var halfSize = (size / 2)!;
65+
var minPos = position.SubCopy(halfSize.X, halfSize.Y, halfSize.Z);
66+
var maxPos = position.AddCopy(halfSize.X, halfSize.Y, halfSize.Z);
67+
68+
var devastationRock = _api.World.BlockAccessor.GetBlock(new AssetLocation("game", "drock"))!;
69+
_api.World.WalkBlocksCuboid(minPos.AsBlockPos, maxPos.AsBlockPos,
70+
(_, x, y, z) => { _api.World.BlockAccessor.SetBlock(devastationRock.Id, new Vec3i(x, y, z).AsBlockPos); });
71+
72+
return TextCommandResult.Success();
73+
}
74+
75+
private TextCommandResult DebugWalkCube(TextCommandCallingArgs args)
76+
{
77+
var position = (Vec3d)args[0];
78+
var size = (int)args[1];
79+
80+
var devastationRock = _api.World.BlockAccessor.GetBlock(new AssetLocation("game", "drock"))!;
81+
_api.World.WalkBlocksCube(position.AsBlockPos, size / 2,
82+
(_, x, y, z) => { _api.World.BlockAccessor.SetBlock(devastationRock.Id, new Vec3i(x, y, z).AsBlockPos); });
83+
84+
return TextCommandResult.Success();
85+
}
86+
87+
private TextCommandResult DebugWalkCylinder(TextCommandCallingArgs args)
88+
{
89+
var position = (Vec3d)args[0];
90+
var radius = (int)args[1];
91+
92+
const int minYPos = 0;
93+
var maxYPos = _api.World.BlockAccessor.MapSizeY;
94+
95+
var devastationRock = _api.World.BlockAccessor.GetBlock(new AssetLocation("game", "drock"))!;
96+
_api.World.WalkBlocksCylinder(position.AsBlockPos, radius, minYPos, maxYPos,
97+
(_, x, y, z) => { _api.World.BlockAccessor.SetBlock(devastationRock.Id, new Vec3i(x, y, z).AsBlockPos); });
98+
99+
return TextCommandResult.Success();
100+
}
101+
102+
private TextCommandResult DebugWalkSphere(TextCommandCallingArgs args)
103+
{
104+
var position = (Vec3d)args[0];
105+
var radius = (int)args[1];
106+
107+
var devastationRock = _api.World.BlockAccessor.GetBlock(new AssetLocation("game", "drock"))!;
108+
_api.World.WalkBlocksSphere(position.AsBlockPos, radius,
109+
(_, x, y, z) => { _api.World.BlockAccessor.SetBlock(devastationRock.Id, new Vec3i(x, y, z).AsBlockPos); });
110+
111+
return TextCommandResult.Success();
112+
}
113+
}

Common.Mod.Example/ExampleSystem.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Common.Mod.Common.Config;
22
using Common.Mod.Example.BlockBehaviors;
3+
using Common.Mod.Example.Commands;
4+
using DryIoc;
35
using JetBrains.Annotations;
46
using Vintagestory.API.Common;
57

@@ -14,6 +16,19 @@ public class ExampleSystem : System<ExampleSystem>
1416

1517
public override bool ShouldLoad(EnumAppSide forSide) => true;
1618

19+
public override void Start(ICoreAPI api)
20+
{
21+
base.Start(api);
22+
Container.Register<DebugCommand>();
23+
24+
// Initialize classes
25+
{
26+
// ReSharper disable UnusedVariable
27+
var debugCommand = Container.Resolve<DebugCommand>();
28+
// ReSharper restore UnusedVariable
29+
}
30+
}
31+
1732
protected override void RegisterConfigs(IConfigSystem configSystem)
1833
{
1934
configSystem.Register<ExampleCommonConfig>();

Common.Mod/Blocks/MultiblockBlock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ BlockCallDelegateBlock<T> onOtherwise
3434
)
3535
where TK : class
3636
{
37-
var block = blockAccessor.GetBlock((new Vec3i(x, y, z)).ToBlockPos());
37+
var block = blockAccessor.GetBlock((new Vec3i(x, y, z)).AsBlockPos);
3838
var blockInf = block as TK ?? block.GetBehavior(typeof(TK), true) as TK;
3939

4040
if (blockInf != null)

Common.Mod/Extensions/WorldAccessorExtensions.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ WalkBlocksHandler onBlock
3838
var maxPos = centerPos.AddCopy(radius, 0, radius);
3939
maxPos.Y = maxYPos;
4040

41-
bool Predicate(int x, int y, int z) => IsInCircle(centerPos.ToVec3i(), radius, x, z);
41+
bool Predicate(int x, int y, int z) => IsInCircle(centerPos.AsVec3i, radius, x, z);
4242
worldAccessor.WalkBlocks(minPos, maxPos, Predicate, onBlock);
4343
}
4444

@@ -65,7 +65,7 @@ WalkBlocksHandler onBlock
6565
var minPos = centerPos.SubCopy(radius, radius, radius);
6666
var maxPos = centerPos.AddCopy(radius, radius, radius);
6767

68-
bool Predicate(int x, int y, int z) => IsInSphere(centerPos.ToVec3i(), radius, x, y, z);
68+
bool Predicate(int x, int y, int z) => IsInSphere(centerPos.AsVec3i, radius, x, y, z);
6969
worldAccessor.WalkBlocks(minPos, maxPos, Predicate, onBlock);
7070
}
7171

@@ -145,17 +145,7 @@ private static bool IsInCircle(Vec3i center, int radius, int x, int z)
145145
var dX = Math.Abs(x - center.X);
146146
var dZ = Math.Abs(z - center.Z);
147147

148-
if (dX + dZ <= radius)
149-
{
150-
return true;
151-
}
152-
153-
if (dX > radius || dZ > radius)
154-
{
155-
return false;
156-
}
157-
158-
return Math.Pow(dX, 2) + Math.Pow(dX, 2) <= Math.Pow(radius, 2);
148+
return Math.Pow(dX, 2) + Math.Pow(dZ, 2) <= Math.Pow(radius, 2);
159149
}
160150

161151
private static bool IsInSphere(Vec3i center, int radius, int x, int y, int z)
@@ -164,16 +154,6 @@ private static bool IsInSphere(Vec3i center, int radius, int x, int y, int z)
164154
var dY = Math.Abs(y - center.Y);
165155
var dZ = Math.Abs(z - center.Z);
166156

167-
if (dX + dY + dZ <= radius)
168-
{
169-
return true;
170-
}
171-
172-
if (dX > radius || dY > radius || dZ > radius)
173-
{
174-
return false;
175-
}
176-
177-
return Math.Pow(dX, 2) + Math.Pow(dY, 2) + Math.Pow(dX, 2) <= Math.Pow(radius, 2);
157+
return Math.Pow(dX, 2) + Math.Pow(dY, 2) + Math.Pow(dZ, 2) <= Math.Pow(radius, 2);
178158
}
179159
}

0 commit comments

Comments
 (0)