Skip to content

Commit

Permalink
renamed helper methods to clarify, added MoveWentWrongException
Browse files Browse the repository at this point in the history
  • Loading branch information
psu-de committed Mar 13, 2024
1 parent ae60fb4 commit 500220d
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 29 deletions.
10 changes: 10 additions & 0 deletions Plugins/MineSharp.Pathfinder/Exceptions/MoveWentWrongException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using MineSharp.Core.Exceptions;

namespace MineSharp.Pathfinder.Exceptions;

/// <summary>
/// Exception thrown when a <see cref="Moves.Move"/> could not be done.
/// </summary>
/// <param name="message"></param>
public class MoveWentWrongException(string message) : MineSharpException(message)
{ }
6 changes: 3 additions & 3 deletions Plugins/MineSharp.Pathfinder/Moves/DirectMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements
{
await physics.WaitForTick();

if (CollisionHelper.IsPositionInBlock(entity.Position, targetBlock))
if (CollisionHelper.IsPointInBlockBb(entity.Position, targetBlock))
{
break;
}
}
physics.InputControls.Reset();

if (!CollisionHelper.IsPositionInBlock(entity.Position, targetBlock))
if (!CollisionHelper.IsPointInBlockBb(entity.Position, targetBlock))
{
throw new Exception("move went wrong"); // TODO: Move exception
}

await MovementUtils.MoveToBlockCenter(entity, physics);
await MovementUtils.MoveInsideBlock(entity, targetBlock, physics);
}
}
6 changes: 3 additions & 3 deletions Plugins/MineSharp.Pathfinder/Moves/FallDownMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements
if (!stopNextTick)
{
var vec = MovementUtils.GetXZPositionNextTick(player.Entity);
if (CollisionHelper.IsXZPositionInBlock(vec, targetBlock))
if (CollisionHelper.IsXzPointInBlockBb(vec, targetBlock))
{
stopNextTick = true;
}
Expand All @@ -72,11 +72,11 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements
physics.InputControls.Reset();
await physics.WaitForOnGround();

if (!CollisionHelper.IsPositionInBlock(entity.Position, targetBlock))
if (!CollisionHelper.IntersectsBbWithBlock(entity.GetBoundingBox(), targetBlock))
{
throw new Exception("move went wrong."); // TODO: Better exception
}

await MovementUtils.MoveToBlockCenter(entity, physics);
await MovementUtils.MoveInsideBlock(entity, targetBlock, physics);
}
}
12 changes: 5 additions & 7 deletions Plugins/MineSharp.Pathfinder/Moves/JumpOneBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,12 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements
0,
this.Motion.Z / 2);
var targetBlock = (Position)target;

await physics.Look(0, 0);
await MovementUtils.MoveToBlockCenter(entity, physics);

MovementUtils.SetHorizontalMovementsFromVector(this.Motion, physics.InputControls);

while (true)
{
if (CollisionHelper.IsPositionInBlock(entity.Position, jumpBlock))
if (CollisionHelper.IsPointInBlockBb(entity.Position, jumpBlock))
{
physics.InputControls.JumpingKeyDown = true;
await physics.WaitForTick();
Expand All @@ -77,7 +74,7 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements

while (true)
{
if (CollisionHelper.IsPositionInBlock(entity.Position.Plus(entity.Velocity), jumpBlock))
if (CollisionHelper.IsPointInBlockBb(entity.Position.Plus(entity.Velocity), jumpBlock))
{
physics.InputControls.Reset();
break;
Expand All @@ -88,11 +85,12 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements

await physics.WaitForOnGround();

if (!CollisionHelper.IsPositionInBlock(entity.Position, targetBlock))
if (!CollisionHelper.IntersectsBbWithBlock(entity.GetBoundingBox(), targetBlock))
{
throw new Exception("move went wrong."); // TODO: Better exception
}

await MovementUtils.MoveToBlockCenter(entity, physics);
Console.WriteLine($"target={target}, block={targetBlock}");
await MovementUtils.MoveInsideBlock(entity, targetBlock, physics);
}
}
4 changes: 2 additions & 2 deletions Plugins/MineSharp.Pathfinder/Moves/JumpUpMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements
{
await physics.WaitForTick();

if (CollisionHelper.IsXZPositionInBlock(entity.Position, targetBlock))
if (CollisionHelper.IsXzPointInBlockBb(entity.Position, targetBlock))
{
break;
}
Expand All @@ -71,6 +71,6 @@ protected override async Task PerformMove(MineSharpBot bot, int count, Movements
physics.InputControls.Reset();
await physics.WaitForOnGround();

await MovementUtils.MoveToBlockCenter(entity, physics);
await MovementUtils.MoveInsideBlock(entity, targetBlock, physics);
}
}
12 changes: 6 additions & 6 deletions Plugins/MineSharp.Pathfinder/Moves/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class Move
/// </summary>
public abstract bool IsMovePossible(Position position, IWorld world);

internal async Task Perform(MineSharpBot bot, int count, Movements movements)
internal async Task Perform(MineSharpBot bot, int count, Position startPosition, Movements movements)
{
if (!this.CanBeLinked && count > 1)
{
Expand All @@ -47,20 +47,20 @@ internal async Task Perform(MineSharpBot bot, int count, Movements movements)
var physics = bot.GetPlugin<PhysicsPlugin>();
var entity = player.Entity ?? throw new NullReferenceException("player is not initialized");

if (entity.Velocity.HorizontalLengthSquared() > 0.03)
if (entity.Velocity.HorizontalLengthSquared() > 0.15 * 0.15)
await MovementUtils.SlowDown(entity, physics);

await physics.Look(0, 0);
await MovementUtils.MoveToBlockCenter(entity, physics);
await MovementUtils.MoveInsideBlock(entity, startPosition, physics);

await PerformMove(bot, count, movements);
}

/// <summary>
/// Perform the move. Before PerformMove() is called, it is made sure that:
/// Perform the move. Before PerformMove() is called, it is made guaranteed that:
/// - The bot is looking at (0, 0)
/// - The bos hitbox is completely on the block
/// - The bots velocity is lower than 0.03
/// - The bots hitbox is completely inside the block
/// - The bots velocity is lower than 0.15^2
/// - The input controls are reset
/// </summary>
protected abstract Task PerformMove(MineSharpBot bot, int count, Movements movements);
Expand Down
11 changes: 9 additions & 2 deletions Plugins/MineSharp.Pathfinder/Utils/CollisionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,25 @@ public static bool CollidesWithWord(AABB aabb, IWorld world)
return false;
}

public static bool IsPositionInBlock(Vector3 position, Position block)
public static bool IsPointInBlockBb(Vector3 position, Position block)
{
var pos = position.Minus(block.X, block.Y, block.Z);
return BlockBb.Contains(pos);
}

public static bool IsXZPositionInBlock(Vector3 position, Position block)
public static bool IsXzPointInBlockBb(Vector3 position, Position block)
{
var pos = position.Minus(block.X, position.Y, block.Z);
return BlockBb.Contains(pos);
}

public static bool IntersectsBbWithBlock(AABB bb, Position block)
{
// TODO: not correct, (min.x, max.z) can be on block, but is not detected
// return IsPositionInBlock(bb.Min, block) || IsPositionInBlock(bb.Max, block);
return BlockBb.Intersects(bb.Clone().Offset(-block.X, -block.Y, -block.Z));
}

public static AABB[] GetBoundingBoxes(Block block, MinecraftData data)
{
return data.BlockCollisionShapes.GetForBlock(block)
Expand Down
21 changes: 15 additions & 6 deletions Plugins/MineSharp.Pathfinder/Utils/MovementUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MineSharp.Core.Common;
using MineSharp.Core.Common.Entities;
using MineSharp.Core.Geometry;
using MineSharp.Pathfinder.Exceptions;
using MineSharp.Physics.Input;

namespace MineSharp.Pathfinder.Utils;
Expand Down Expand Up @@ -48,12 +49,20 @@ public static Vector3 GetXZPositionNextTick(Entity entity)
return entity.Position.Clone().Add(entity.Velocity.X, 0, entity.Velocity.Z);
}

public static async Task MoveToBlockCenter(Entity entity, PhysicsPlugin physics)
/// <summary>
/// Makes sure the whole xz-hitbox of <paramref name="entity"/> is inside the block at <paramref name="blockPosition"/>
/// </summary>
public static async Task MoveInsideBlock(Entity entity, Position blockPosition, PhysicsPlugin physics)
{
var target = (Position)entity.Position;
var toTarget = new Vector3(0.5, 0, 0.5).Add(target);
// assert entity's bb intersects block position
var bb = entity.GetBoundingBox();

var bb = CollisionHelper.SetAABBToPlayerBB(entity.Position);
if (!CollisionHelper.IntersectsBbWithBlock(bb, 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);

while (true)
{
Expand All @@ -63,8 +72,8 @@ public static async Task MoveToBlockCenter(Entity entity, PhysicsPlugin physics)

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

if (CollisionHelper.IsPositionInBlock(bb.Min, target)
&& CollisionHelper.IsXZPositionInBlock(bb.Max, target))
if (CollisionHelper.IsPointInBlockBb(bb.Min, blockPosition)
&& CollisionHelper.IsXzPointInBlockBb(bb.Max, blockPosition))
{
break;
}
Expand Down

0 comments on commit 500220d

Please sign in to comment.