Skip to content

Commit

Permalink
update pathfinder to use the new mutable types
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Mar 27, 2024
1 parent 72acc76 commit 3efa7b9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
8 changes: 4 additions & 4 deletions Plugins/MineSharp.Pathfinder/Moves/JumpMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override bool IsMovePossible(Position position, IWorld world)
var x = this.Motion.X == 0 ? 0 : this.Motion.X < 0 ? -1 : 1;
var z = this.Motion.Z == 0 ? 0 : this.Motion.Z < 0 ? -1 : 1;

var pos = new Vector3(0.5, 0, 0.5).Add(position);
var pos = new MutableVector3(0.5, 0, 0.5).Add(position);

var motionBelow = this.Motion
.Clone()
Expand All @@ -53,7 +53,7 @@ public override bool IsMovePossible(Position position, IWorld world)
if (CollisionHelper.CollidesWithWord(bb, world))
return false;

bb = CollisionHelper.SetAABBToPlayerBB(pos, ref bb)
bb = CollisionHelper.SetAABBToPlayerBB(pos, bb)
.Offset(this.Motion.X, this.Motion.Y, this.Motion.Z);

return !CollisionHelper.CollidesWithWord(bb, world);
Expand Down Expand Up @@ -98,7 +98,7 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements
while (true)
{
await physics.WaitForTick();
CollisionHelper.SetAABBToPlayerBB(MovementUtils.GetPositionNextTick(entity), ref bb);
CollisionHelper.SetAABBToPlayerBB(MovementUtils.GetPositionNextTick(entity), bb);

if (bb.Min.Y - target.Y + this.Motion.Y < -1)
{
Expand All @@ -125,7 +125,7 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements
{
await physics.WaitForTick();

CollisionHelper.SetAABBToPlayerBB(MovementUtils.GetPositionNextTick(entity), ref bb);
CollisionHelper.SetAABBToPlayerBB(MovementUtils.GetPositionNextTick(entity), bb);
if (CollisionHelper.IntersectsBbWithBlockXz(bb, targetBlock))
{
break;
Expand Down
23 changes: 10 additions & 13 deletions Plugins/MineSharp.Pathfinder/Utils/CollisionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static bool CollidesWithWord(AABB aabb, IWorld world)

foreach (var box in boxes)
{
box.Offset(block.Position.X, block.Position.Y, block.Position.Z);
box.Clone().Offset(block.Position.X, block.Position.Y, block.Position.Z);

if (aabb.Intersects(box))
{
Expand Down Expand Up @@ -57,31 +57,28 @@ public static bool IntersectsBbWithBlock(AABB bb, Position block)

public static bool IntersectsBbWithBlockXz(AABB bb, Position block)
{
return BlockBb.Intersects(bb.Clone().Offset(-block.X, -bb.MinY, -block.Z));
return BlockBb.Intersects(bb.Clone().Offset(-block.X, -bb.Min.Y, -block.Z));
}

public static AABB[] GetBoundingBoxes(Block block, MinecraftData data)
public static MutableAABB[] GetBoundingBoxes(Block block, MinecraftData data)
{
return data.BlockCollisionShapes.GetForBlock(block)
.Select(x => x.Offset(block.Position.X, block.Position.Y, block.Position.Z))
.Select(x => x.Clone().Offset(block.Position.X, block.Position.Y, block.Position.Z))
.ToArray();
}

public static AABB GetAabbForPlayer(Vector3 pos)
public static MutableAABB GetAabbForPlayer(Vector3 pos)
{
var bb = new AABB(-0.3, 0, -0.3, 0.3, 1.8, 0.3)
var bb = new MutableAABB(-0.3, 0, -0.3, 0.3, 1.8, 0.3)
.Offset(pos.X, pos.Y, pos.Z);
return bb;
}

public static AABB SetAABBToPlayerBB(Vector3 pos, ref AABB aabb)
public static MutableAABB SetAABBToPlayerBB(Vector3 pos, MutableAABB aabb)
{
aabb.Min.X = -0.3;
aabb.Min.Y = 0;
aabb.Min.Z = -0.3;
aabb.Max.X = 0.3;
aabb.Max.Y = 1.8;
aabb.Max.Z = 0.3;
aabb.Min.Set(-0.3, 0, -0.3);
aabb.Max.Set(0.3, 1.8, 0.3);

return aabb.Offset(pos.X, pos.Y, pos.Z);
}
}
13 changes: 6 additions & 7 deletions Plugins/MineSharp.Pathfinder/Utils/MovementUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static async Task SetRotationForMotion(Vector3 movement, PhysicsPlugin ph
public static void SetHorizontalMovementsFromVector(Vector3 movement, double yaw, InputControls controls)
{
var vec = movement.Clone();
RotateVector(ref vec, -yaw);
RotateVector(vec, -yaw);

controls.Reset();

Expand Down Expand Up @@ -54,7 +54,7 @@ public static async Task SlowDown(Entity entity, PhysicsPlugin physics)
physics.InputControls.Reset();
}

private static void RotateVector(ref Vector3 vec, double yaw)
private static void RotateVector(MutableVector3 vec, double yaw)
{
var sin = Math.Sin(yaw * (Math.PI / 180.0));
var cos = Math.Cos(yaw * (Math.PI / 180.0));
Expand All @@ -66,9 +66,8 @@ private static void RotateVector(ref Vector3 vec, double yaw)
x = 0;
if (Math.Abs(z) < 0.02)
z = 0;

vec.X = x;
vec.Z = z;

vec.Set(x, vec.Y, z);
}

public static Vector3 GetPositionNextTick(Entity entity)
Expand All @@ -94,15 +93,15 @@ public static async Task MoveInsideBlock(Entity entity, Position blockPosition,
throw new MoveWentWrongException($"Cannot move to block center of {blockPosition}, because entity is at {entity.Position}");
}

var toTarget = new Vector3(0.5, 0, 0.5).Add(blockPosition);
var toTarget = new MutableVector3(0.5, 0, 0.5).Add(blockPosition);

while (true)
{
var vec = toTarget.Minus(entity.Position);
SetHorizontalMovementsFromVector(vec, entity.Yaw, physics.InputControls);
await physics.WaitForTick();

CollisionHelper.SetAABBToPlayerBB(GetXZPositionNextTick(entity), ref bb);
CollisionHelper.SetAABBToPlayerBB(GetXZPositionNextTick(entity), bb);

if (CollisionHelper.IsPointInBlockBb(bb.Min, blockPosition)
&& CollisionHelper.IsXzPointInBlockBb(bb.Max, blockPosition))
Expand Down

0 comments on commit 3efa7b9

Please sign in to comment.