Minecraft-Console-Client/MinecraftClient/Mapping/Movement.cs

734 lines
33 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace MinecraftClient.Mapping
{
/// <summary>
/// Allows moving through a Minecraft world
/// </summary>
public static class Movement
{
/* ========= PATHFINDING METHODS ========= */
/// <summary>
/// Handle movements due to gravity
/// </summary>
/// <param name="world">World the player is currently located in</param>
/// <param name="location">Location the player is currently at</param>
/// <param name="motionY">Current vertical motion speed</param>
/// <returns>Updated location after applying gravity</returns>
public static Location HandleGravity(World world, Location location, ref double motionY)
{
2022-10-05 15:02:30 +08:00
if (Settings.InternalConfig.GravityEnabled)
{
Location onFoots = new(location.X, Math.Floor(location.Y), location.Z);
Location belowFoots = Move(location, Direction.Down);
if (location.Y > Math.Truncate(location.Y) + 0.0001)
{
belowFoots = location;
belowFoots.Y = Math.Truncate(location.Y);
}
2023-05-16 23:19:31 +02:00
if (!IsOnGround(world, location) && !IsSwimming(world, location))
{
while (!IsOnGround(world, belowFoots) && belowFoots.Y >= 1 + World.GetDimension().minY)
belowFoots = Move(belowFoots, Direction.Down);
location = Move2Steps(location, belowFoots, ref motionY, true).Dequeue();
}
2023-05-16 23:19:31 +02:00
else if (!world.GetBlock(onFoots).Type.IsSolid())
location = Move2Steps(location, onFoots, ref motionY, true).Dequeue();
}
2023-05-16 23:19:31 +02:00
return location;
}
/// <summary>
/// Return a list of possible moves for the player
/// </summary>
/// <param name="world">World the player is currently located in</param>
2023-05-16 23:19:31 +02:00
/// <param name="originLocation">Location the player is currently at</param>
/// <param name="allowUnsafe">Allow possible but unsafe locations</param>
/// <returns>A list of new locations the player can move to</returns>
2023-05-16 23:19:31 +02:00
public static IEnumerable<Location> GetAvailableMoves(World world, Location originLocation,
bool allowUnsafe = false)
{
2022-09-09 16:13:25 +08:00
Location location = originLocation.ToCenter();
List<Location> availableMoves = new();
if (IsOnGround(world, location) || IsSwimming(world, location))
{
foreach (Direction dir in Enum.GetValues(typeof(Direction)))
2022-09-09 16:13:25 +08:00
{
Location dest = Move(location, dir);
if (CanMove(world, location, dir) && (allowUnsafe || IsSafe(world, dest)))
availableMoves.Add(dest);
}
}
else
{
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
foreach (Direction dir in new[] { Direction.East, Direction.West, Direction.North, Direction.South })
2023-05-16 23:19:31 +02:00
if (CanMove(world, location, dir) && IsOnGround(world, Move(location, dir)) &&
(allowUnsafe || IsSafe(world, Move(location, dir))))
availableMoves.Add(Move(location, dir));
availableMoves.Add(Move(location, Direction.Down));
}
2023-05-16 23:19:31 +02:00
return availableMoves;
}
/// <summary>
/// Decompose a single move from a block to another into several steps
/// </summary>
/// <remarks>
/// Allows moving by little steps instead or directly moving between blocks,
/// which would be rejected by anti-cheat plugins anyway.
/// </remarks>
/// <param name="start">Start location</param>
/// <param name="goal">Destination location</param>
/// <param name="motionY">Current vertical motion speed</param>
/// <param name="falling">Specify if performing falling steps</param>
/// <param name="stepsByBlock">Amount of steps by block</param>
/// <returns>A list of locations corresponding to the requested steps</returns>
2023-05-16 23:19:31 +02:00
public static Queue<Location> Move2Steps(Location start, Location goal, ref double motionY,
bool falling = false, int stepsByBlock = 8)
{
if (stepsByBlock <= 0)
stepsByBlock = 1;
if (falling)
{
//Use MC-Like falling algorithm
2023-05-16 23:19:31 +02:00
double y = start.Y;
Queue<Location> fallSteps = new();
fallSteps.Enqueue(start);
motionY -= 0.08D;
motionY *= 0.9800000190734863D;
2023-05-16 23:19:31 +02:00
y += motionY;
if (y < goal.Y)
return new Queue<Location>(new[] { goal });
2023-05-16 23:19:31 +02:00
return new Queue<Location>(new[] { new Location(start.X, y, start.Z) });
}
else
{
//Regular MCC moving algorithm
motionY = 0; //Reset motion speed
double totalStepsDouble = start.Distance(goal) * stepsByBlock;
int totalSteps = (int)Math.Ceiling(totalStepsDouble);
Location step = (goal - start) / totalSteps;
if (totalStepsDouble >= 1)
{
2022-09-09 16:13:25 +08:00
Queue<Location> movementSteps = new();
for (int i = 1; i <= totalSteps; i++)
movementSteps.Enqueue(start + step * i);
return movementSteps;
}
else
2022-09-09 16:13:25 +08:00
return new Queue<Location>(new[] { goal });
}
}
/// <summary>
/// Calculate a path from the start location to the destination location
/// </summary>
/// <remarks>
/// Based on the A* pathfinding algorithm described on Wikipedia
/// </remarks>
/// <see href="https://en.wikipedia.org/wiki/A*_search_algorithm#Pseudocode"/>
2023-05-16 23:19:31 +02:00
/// <param name="world">World</param>
/// <param name="start">Start location</param>
/// <param name="goal">Destination location</param>
/// <param name="allowUnsafe">Allow possible but unsafe locations</param>
/// <param name="maxOffset">If no valid path can be found, also allow locations within specified distance of destination</param>
/// <param name="minOffset">Do not get closer of destination than specified distance</param>
/// <param name="timeout">How long to wait before stopping computation</param>
/// <remarks>When location is unreachable, computation will reach timeout, then optionally fallback to a close location within maxOffset</remarks>
/// <returns>A list of locations, or null if calculation failed</returns>
2023-05-16 23:19:31 +02:00
public static Queue<Location>? CalculatePath(World world, Location start, Location goal, bool allowUnsafe,
int maxOffset, int minOffset, TimeSpan timeout)
{
CancellationTokenSource cts = new();
2023-05-16 23:19:31 +02:00
Task<Queue<Location>?> pathfindingTask = Task.Factory.StartNew(() =>
CalculatePath(world, start, goal, allowUnsafe, maxOffset, minOffset, cts.Token));
pathfindingTask.Wait(timeout);
if (!pathfindingTask.IsCompleted)
{
cts.Cancel();
pathfindingTask.Wait();
}
2023-05-16 23:19:31 +02:00
return pathfindingTask.Result;
}
/// <summary>
/// Calculate a path from the start location to the destination location
/// </summary>
/// <remarks>
/// Based on the A* pathfinding algorithm described on Wikipedia
/// </remarks>
/// <see href="https://en.wikipedia.org/wiki/A*_search_algorithm#Pseudocode"/>
2023-05-16 23:19:31 +02:00
/// <param name="world">World</param>
/// <param name="start">Start location</param>
/// <param name="goal">Destination location</param>
/// <param name="allowUnsafe">Allow possible but unsafe locations</param>
/// <param name="maxOffset">If no valid path can be found, also allow locations within specified distance of destination</param>
/// <param name="minOffset">Do not get closer of destination than specified distance</param>
/// <param name="ct">Token for stopping computation after a certain time</param>
/// <returns>A list of locations, or null if calculation failed</returns>
2023-05-16 23:19:31 +02:00
public static Queue<Location>? CalculatePath(World world, Location start, Location goal, bool allowUnsafe,
int maxOffset, int minOffset, CancellationToken ct)
{
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// This is a bad configuration
if (minOffset > maxOffset)
throw new ArgumentException("minOffset must be lower or equal to maxOffset", nameof(minOffset));
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Round start coordinates for easier calculation
2022-09-09 16:13:25 +08:00
Location startLower = start.ToFloor();
Location goalLower = goal.ToFloor();
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// We always use distance squared so our limits must also be squared.
minOffset *= minOffset;
maxOffset *= maxOffset;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Prepare variables and datastructures for A*
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Dictionary that contains the relation between all coordinates and resolves the final path
2023-05-16 23:19:31 +02:00
Dictionary<Location, Location> cameFrom = new();
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Create a Binary Heap for all open positions => Allows fast access to Nodes with lowest scores
BinaryHeap openSet = new();
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Dictionary to keep track of the G-Score of every location
Dictionary<Location, int> gScoreDict = new();
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Set start values for variables
openSet.Insert(0, (int)startLower.DistanceSquared(goalLower), startLower);
gScoreDict[startLower] = 0;
BinaryHeap.Node? current = null;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Start of A*
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Execute while we have nodes to process and we are not cancelled
while (openSet.Count() > 0 && !ct.IsCancellationRequested)
{
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Get the root node of the Binary Heap
// Node with the lowest F-Score or lowest H-Score on tie
current = openSet.GetRootLocation();
// Return if goal found and no maxOffset was given OR current node is between minOffset and maxOffset
2023-05-16 23:19:31 +02:00
if ((current.Location == goalLower && maxOffset <= 0) ||
(maxOffset > 0 && current.HScore >= minOffset && current.HScore <= maxOffset))
return ReconstructPath(cameFrom, current.Location, start, goal);
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Discover neighbored blocks
foreach (Location neighbor in GetAvailableMoves(world, current.Location, allowUnsafe))
{
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// If we are cancelled: break
if (ct.IsCancellationRequested)
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
break;
2023-05-16 23:19:31 +02:00
// tentative_GScore is the distance from start to the neighbor through current
int tentativeGScore = current.GScore + (int)current.Location.DistanceSquared(neighbor);
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
2023-05-16 23:19:31 +02:00
// If the neighbor is not in the GScoreDict OR its current tentativeGScore is lower than the previously saved one:
if (!gScoreDict.ContainsKey(neighbor) ||
(gScoreDict.ContainsKey(neighbor) && tentativeGScore < gScoreDict[neighbor]))
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
{
// Save the new relation between the neighbored block and the current one
2023-05-16 23:19:31 +02:00
cameFrom[neighbor] = current.Location;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
gScoreDict[neighbor] = tentativeGScore;
// If this location is not already included in the Binary Heap: save it
if (!openSet.ContainsLocation(neighbor))
openSet.Insert(tentativeGScore, (int)neighbor.DistanceSquared(goalLower), neighbor);
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
}
}
}
2023-05-16 23:19:31 +02:00
// Goal could not be reached. Set the path to the closest location if close enough
if (current != null && openSet.MinHScoreNode != null &&
(maxOffset == int.MaxValue || openSet.MinHScoreNode.HScore <= maxOffset))
return ReconstructPath(cameFrom, openSet.MinHScoreNode.Location, start, goal);
return null;
}
/// <summary>
/// Helper function for CalculatePath(). Backtrack from goal to start to reconstruct a step-by-step path.
/// </summary>
2023-05-16 23:19:31 +02:00
/// <param name="cameFrom">The collection of Locations that leads back to the start</param>
/// <param name="current">Endpoint of our later walk</param>
2023-05-16 23:19:31 +02:00
/// <param name="start">Start location</param>
/// <param name="end">End location</param>
/// <returns>the path that leads to current from the start position</returns>
2023-05-16 23:19:31 +02:00
private static Queue<Location> ReconstructPath(Dictionary<Location, Location> cameFrom, Location current,
Location start, Location end)
{
2022-09-09 16:13:25 +08:00
int midPathCnt = 0;
2023-05-16 23:19:31 +02:00
List<Location> totalPath = new();
// Move from the center of the block to the final position
2022-09-09 16:13:25 +08:00
if (current != end && current == end.ToFloor())
2023-05-16 23:19:31 +02:00
totalPath.Add(end);
// Generate intermediate paths
2023-05-16 23:19:31 +02:00
totalPath.Add(current.ToCenter());
while (cameFrom.ContainsKey(current))
{
2022-09-09 16:13:25 +08:00
++midPathCnt;
2023-05-16 23:19:31 +02:00
current = cameFrom[current];
totalPath.Add(current.ToCenter());
}
2022-09-09 16:13:25 +08:00
if (midPathCnt <= 2 && start.DistanceSquared(end) < 2.0)
2023-05-16 23:19:31 +02:00
return new Queue<Location>(new[] { end });
2022-09-09 16:13:25 +08:00
else
{
// Move to the center of the block first
if (current != start && current == start.ToFloor())
2023-05-16 23:19:31 +02:00
totalPath.Add(start.ToCenter());
2023-05-16 23:19:31 +02:00
totalPath.Reverse();
return new Queue<Location>(totalPath);
2022-09-09 16:13:25 +08:00
}
}
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
/// <summary>
/// A datastructure to store Locations as Nodes and provide them in sorted and queued order.
/// !!!
/// CAN BE REPLACED WITH PriorityQueue IN .NET-6
/// https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.priorityqueue-2?view=net-6.0
/// !!!
/// </summary>
public class BinaryHeap
{
/// <summary>
/// Represents a location and its attributes
/// </summary>
public class Node
{
// Distance to start
2023-05-16 23:19:31 +02:00
public int GScore;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Distance to Goal
2023-05-16 23:19:31 +02:00
public int HScore;
public int FScore
{
get { return HScore + GScore; }
}
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
public Location Location;
2023-05-16 23:19:31 +02:00
public Node(int gScore, int hScore, Location loc)
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
{
2023-05-16 23:19:31 +02:00
this.GScore = gScore;
this.HScore = hScore;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
Location = loc;
}
}
// List which contains all nodes in form of a Binary Heap
private readonly List<Node> heapList;
2023-05-16 23:19:31 +02:00
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Hashset for quick checks of locations included in the heap
private readonly HashSet<Location> locationList;
2023-05-16 23:19:31 +02:00
public Node? MinHScoreNode;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
public BinaryHeap()
{
heapList = new List<Node>();
locationList = new HashSet<Location>();
2023-05-16 23:19:31 +02:00
MinHScoreNode = null;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
}
/// <summary>
/// Insert a new location in the heap
/// </summary>
2023-05-16 23:19:31 +02:00
/// <param name="newGScore">G-Score of the location</param>
/// <param name="newHScore">H-Score of the location</param>
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
/// <param name="loc">The location</param>
2023-05-16 23:19:31 +02:00
public void Insert(int newGScore, int newHScore, Location loc)
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
{
// Begin at the end of the list
int i = heapList.Count;
// Temporarily save the node created with the parameters to allow comparisons
2023-05-16 23:19:31 +02:00
Node newNode = new(newGScore, newHScore, loc);
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Add new note to the end of the list
heapList.Add(newNode);
locationList.Add(loc);
// Save node with the smallest H-Score => Distance to goal
2023-05-16 23:19:31 +02:00
if (MinHScoreNode == null || newNode.HScore < MinHScoreNode.HScore)
MinHScoreNode = newNode;
if (i == 0)
return;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// There is no need of sorting for one node.
2023-05-16 23:19:31 +02:00
// Go up the heap from child to parent and move parent down...
// while we are not looking at the root node AND the new node has better attributes than the parent node ((i - 1) / 2)
while (i > 0 && FirstNodeBetter(newNode /* Current Child */,
heapList[(i - 1) / 2] /* Corresponding Parent */))
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
{
2023-05-16 23:19:31 +02:00
// Move parent down and replace current child -> New free space is created
heapList[i] = heapList[(i - 1) / 2];
// Select the next parent to check
i = (i - 1) / 2;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
}
2023-05-16 23:19:31 +02:00
// Nodes were moved down at position I there is now a free space at the correct position for our new node:
// Insert new node in position
heapList[i] = newNode;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
}
/// <summary>
/// Obtain the root which represents the node the the best attributes currently
/// </summary>
/// <returns>node with the best attributes currently</returns>
/// <exception cref="InvalidOperationException"></exception>
public Node GetRootLocation()
{
// The heap is empty. There is nothing to return.
if (heapList.Count == 0)
throw new InvalidOperationException("The heap is empty.");
// Save the root node
2023-05-16 23:19:31 +02:00
var rootNode = heapList[0];
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
locationList.Remove(rootNode.Location);
// Temporarirly store the last item's value.
2023-05-16 23:19:31 +02:00
var lastNode = heapList[^1];
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Remove the last value.
heapList.RemoveAt(heapList.Count - 1);
if (heapList.Count > 0)
{
// Start at the first index.
2023-05-16 23:19:31 +02:00
var currentParentPos = 0;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
2023-05-16 23:19:31 +02:00
// Go through the heap from root to bottom...
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Continue until the halfway point of the heap.
while (currentParentPos < heapList.Count / 2)
{
// Select the left child of the current parent
2023-05-16 23:19:31 +02:00
var currentChildPos = (2 * currentParentPos) + 1;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// If the currently selected child is not the last entry of the list AND right child has better attributes
2023-05-16 23:19:31 +02:00
if ((currentChildPos < heapList.Count - 1) && FirstNodeBetter(heapList[currentChildPos + 1],
heapList[currentChildPos]))
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
{
// Select the right child
currentChildPos++;
}
// If the last item is smaller than both siblings at the
// current height, break.
if (FirstNodeBetter(lastNode, heapList[currentChildPos]))
break;
// Move the item at index j up one level.
heapList[currentParentPos] = heapList[currentChildPos];
// Move index i to the appropriate branch.
currentParentPos = currentChildPos;
}
2023-05-16 23:19:31 +02:00
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Insert the last node into the currently free position
heapList[currentParentPos] = lastNode;
}
return rootNode;
}
/// <summary>
/// Compares two nodes and evaluates their position to the goal.
/// </summary>
/// <param name="firstNode">First node to compare</param>
/// <param name="secondNode">Second node to compare</param>
2023-05-16 23:19:31 +02:00
/// <returns>True if the first node has a more promising position to the goal than the second</returns>
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
private static bool FirstNodeBetter(Node firstNode, Node secondNode)
{
2023-05-16 23:19:31 +02:00
// Is the FScore smaller?
return (firstNode.FScore < secondNode.FScore) ||
// If FScore is equal, evaluate the h-score
(firstNode.FScore == secondNode.FScore && firstNode.HScore < secondNode.HScore);
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
}
/// <summary>
/// Get the size of the heap
/// </summary>
/// <returns>size of the heap</returns>
public int Count()
{
return heapList.Count;
}
/// <summary>
/// Check if the heap contains a node with a certain location
/// </summary>
/// <param name="loc">Location to check</param>
/// <returns>true if a node with the given location is in the heap</returns>
public bool ContainsLocation(Location loc)
{
return locationList.Contains(loc);
}
}
/* ========= LOCATION PROPERTIES ========= */
// TODO: Find a way to remove this Hack for Vines here.
/// <summary>
/// Check if the specified location is on the ground
/// </summary>
/// <param name="world">World for performing check</param>
/// <param name="location">Location to check</param>
/// <returns>True if the specified location is on the ground</returns>
public static bool IsOnGround(World world, Location location)
{
2022-09-07 17:25:06 +08:00
ChunkColumn? chunkColumn = world.GetChunkColumn(location);
if (chunkColumn == null || chunkColumn.FullyLoaded == false)
2022-07-25 03:19:24 +08:00
return true; // avoid moving downward in a not loaded chunk
2022-09-07 17:25:06 +08:00
Location down = Move(location, Direction.Down);
Material currentMaterial = world.GetBlock(down).Type;
2023-05-16 23:19:31 +02:00
var result = currentMaterial.IsSolid()
|| currentMaterial == Material.TwistingVines || currentMaterial == Material.TwistingVinesPlant
|| currentMaterial == Material.WeepingVines || currentMaterial == Material.WeepingVinesPlant
|| currentMaterial == Material.Vine;
2022-09-07 17:25:06 +08:00
2023-05-16 23:19:31 +02:00
var northCheck = 1 + Math.Floor(down.Z) - down.Z > 0.7;
var eastCheck = down.X - Math.Floor(down.X) > 0.7;
var southCheck = down.Z - Math.Floor(down.Z) > 0.7;
var westCheck = 1 + Math.Floor(down.X) - down.X > 0.7;
2022-09-07 17:25:06 +08:00
if (!result && northCheck)
2023-05-16 23:19:31 +02:00
result |= IsSolidOrVine(world, Move(down, Direction.North));
2022-09-07 17:25:06 +08:00
if (!result && northCheck && eastCheck)
2023-05-16 23:19:31 +02:00
result |= IsSolidOrVine(world, Move(down, Direction.NorthEast));
2022-09-07 17:25:06 +08:00
if (!result && eastCheck)
2023-05-16 23:19:31 +02:00
result |= IsSolidOrVine(world, Move(down, Direction.East));
2022-09-07 17:25:06 +08:00
if (!result && eastCheck && southCheck)
2023-05-16 23:19:31 +02:00
result |= IsSolidOrVine(world, Move(down, Direction.SouthEast));
2022-09-07 17:25:06 +08:00
if (!result && southCheck)
2023-05-16 23:19:31 +02:00
result |= IsSolidOrVine(world, Move(down, Direction.South));
2022-09-07 17:25:06 +08:00
if (!result && southCheck && westCheck)
2023-05-16 23:19:31 +02:00
result |= IsSolidOrVine(world, Move(down, Direction.SouthWest));
2022-09-07 17:25:06 +08:00
if (!result && westCheck)
2023-05-16 23:19:31 +02:00
result |= IsSolidOrVine(world, Move(down, Direction.West));
2022-09-07 17:25:06 +08:00
if (!result && westCheck && northCheck)
2023-05-16 23:19:31 +02:00
result |= IsSolidOrVine(world, Move(down, Direction.NorthWest));
2022-09-07 17:25:06 +08:00
return result && (location.Y <= Math.Truncate(location.Y) + 0.0001);
}
2023-05-16 23:19:31 +02:00
private static bool IsSolidOrVine(World world, Location location)
{
var block = world.GetBlock(location);
return block.Type.IsSolid()
|| block.Type == Material.TwistingVines
|| block.Type == Material.TwistingVinesPlant
|| block.Type == Material.WeepingVines
|| block.Type == Material.WeepingVinesPlant
|| block.Type == Material.Vine;
}
/// <summary>
/// Check if the specified location implies swimming
/// </summary>
/// <param name="world">World for performing check</param>
/// <param name="location">Location to check</param>
/// <returns>True if the specified location implies swimming</returns>
2023-05-16 23:19:31 +02:00
private static bool IsSwimming(World world, Location location)
{
return world.GetBlock(location).Type.IsLiquid();
}
/// <summary>
/// Check if the specified location can be climbed on
/// </summary>
/// <param name="world">World for performing check</param>
/// <param name="location">Location to check</param>
/// <returns>True if the specified location can be climbed on</returns>
2023-05-16 23:19:31 +02:00
private static bool IsClimbing(World world, Location location)
{
return world.GetBlock(location).Type.CanBeClimbedOn();
}
/// <summary>
/// Check if the specified location is safe
/// </summary>
/// <param name="world">World for performing check</param>
/// <param name="location">Location to check</param>
/// <returns>True if the destination location won't directly harm the player</returns>
2023-05-16 23:19:31 +02:00
private static bool IsSafe(World world, Location location)
{
return
2023-05-16 23:19:31 +02:00
//No block that can harm the player
!world.GetBlock(location).Type.CanHarmPlayers()
&& !world.GetBlock(Move(location, Direction.Up)).Type.CanHarmPlayers()
&& !world.GetBlock(Move(location, Direction.Down)).Type.CanHarmPlayers()
//No fall from a too high place
2023-05-16 23:19:31 +02:00
&& (world.GetBlock(Move(location, Direction.Down)).Type.IsSolid() ||
IsClimbing(world, Move(location, Direction.Down))
|| world.GetBlock(Move(location, Direction.Down, 2)).Type.IsSolid() ||
IsClimbing(world, Move(location, Direction.Down, 2))
|| world.GetBlock(Move(location, Direction.Down, 3)).Type.IsSolid() ||
IsClimbing(world, Move(location, Direction.Down, 3)))
//Not an underwater location
&& !(world.GetBlock(Move(location, Direction.Up)).Type.IsLiquid());
}
/* ========= SIMPLE MOVEMENTS ========= */
/// <summary>
/// Check if the player can move in the specified direction
/// </summary>
/// <param name="world">World the player is currently located in</param>
/// <param name="location">Location the player is currently at</param>
/// <param name="direction">Direction the player is moving to</param>
/// <returns>True if the player can move in the specified direction</returns>
public static bool CanMove(World world, Location location, Direction direction)
{
switch (direction)
{
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Move vertical
case Direction.Down:
return IsClimbing(world, Move(location, Direction.Down)) || !IsOnGround(world, location);
case Direction.Up:
2023-05-16 23:19:31 +02:00
bool nextTwoBlocks =
!world.GetBlock(Move(Move(location, Direction.Up), Direction.Up)).Type.IsSolid();
// Check if the current block can be climbed on
if (IsClimbing(world, location))
2023-05-16 23:19:31 +02:00
// Check if next block after the next one can be climbed upon
return IsClimbing(world, Move(location, Direction.Up)) || nextTwoBlocks;
return (IsOnGround(world, location) || IsSwimming(world, location)) && nextTwoBlocks;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Move horizontal
case Direction.East:
case Direction.West:
case Direction.South:
case Direction.North:
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
return PlayerFitsHere(world, Move(location, direction));
// Move diagonal
case Direction.NorthEast:
2023-05-16 23:19:31 +02:00
return PlayerFitsHere(world, Move(location, Direction.North)) &&
PlayerFitsHere(world, Move(location, Direction.East)) &&
PlayerFitsHere(world, Move(location, direction));
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
case Direction.SouthEast:
2023-05-16 23:19:31 +02:00
return PlayerFitsHere(world, Move(location, Direction.South)) &&
PlayerFitsHere(world, Move(location, Direction.East)) &&
PlayerFitsHere(world, Move(location, direction));
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
case Direction.SouthWest:
2023-05-16 23:19:31 +02:00
return PlayerFitsHere(world, Move(location, Direction.South)) &&
PlayerFitsHere(world, Move(location, Direction.West)) &&
PlayerFitsHere(world, Move(location, direction));
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
case Direction.NorthWest:
2023-05-16 23:19:31 +02:00
return PlayerFitsHere(world, Move(location, Direction.North)) &&
PlayerFitsHere(world, Move(location, Direction.West)) &&
PlayerFitsHere(world, Move(location, direction));
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
default:
2022-09-09 16:13:25 +08:00
throw new ArgumentException("Unknown direction", nameof(direction));
}
}
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
/// <summary>
/// Evaluates if a player fits in this location
/// </summary>
/// <param name="world">Current world</param>
/// <param name="location">Location to check</param>
/// <returns>True if a player is able to stand in this location</returns>
public static bool PlayerFitsHere(World world, Location location)
{
2023-05-16 23:19:31 +02:00
var canClimb = IsClimbing(world, location) && IsClimbing(world, Move(location, Direction.Up));
var isNotSolid = !world.GetBlock(location).Type.IsSolid() &&
!world.GetBlock(Move(location, Direction.Up)).Type.IsSolid();
// Handle slabs
if (!isNotSolid && world.GetBlock(Move(location, Direction.Up))
.IsTopSlab(McClient.Instance!.GetProtocolVersion()))
isNotSolid = true;
return canClimb || isNotSolid;
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
}
/// <summary>
/// Get an updated location for moving in the specified direction
/// </summary>
/// <param name="location">Current location</param>
/// <param name="direction">Direction to move to</param>
/// <param name="length">Distance, in blocks</param>
/// <returns>Updated location</returns>
public static Location Move(Location location, Direction direction, int length = 1)
{
return location + Move(direction) * length;
}
/// <summary>
/// Get a location delta for moving in the specified direction
/// </summary>
/// <param name="direction">Direction to move to</param>
/// <returns>A location delta for moving in that direction</returns>
2023-05-16 23:19:31 +02:00
private static Location Move(Direction direction)
{
2023-05-16 23:19:31 +02:00
return direction switch
{
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Move vertical
2023-05-16 23:19:31 +02:00
Direction.Down => new Location(0, -1, 0),
Direction.Up => new Location(0, 1, 0),
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Move horizontal straight
2023-05-16 23:19:31 +02:00
Direction.East => new Location(1, 0, 0),
Direction.West => new Location(-1, 0, 0),
Direction.South => new Location(0, 0, 1),
Direction.North => new Location(0, 0, -1),
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
// Move horizontal diagonal
2023-05-16 23:19:31 +02:00
Direction.NorthEast => Move(Direction.North) + Move(Direction.East),
Direction.SouthEast => Move(Direction.South) + Move(Direction.East),
Direction.SouthWest => Move(Direction.South) + Move(Direction.West),
Direction.NorthWest => Move(Direction.North) + Move(Direction.West),
Rework of MineCube.cs and further improvements to CalculatePath() (#2014) * Add function to determine if the client is executing a walking process * Add comments * Remove test bot entry * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add funtion to approach a block as close as possible * Add comment to function in McClient.cs * Improve concurrency and reduce potential calculation power * Apply code suggestions * Apply code suggestions * Improve CalculatePath() function to allow approaching * Fix typo in MinecraftClient/ChatBot.cs * Add comments to Chatbot fucntion * Add break to for loop to exit quicker * Allow to give a maxOffset to the goal * Comment the sample bot again. * Add parameter for calculation timeout * Remove TestBot again * Implement timeout in Chatbot class * Remove test commands * Update comment in Chatbot.cs * Set timeout to default 5 sec * Change order of parameters back * Add suggested improvements * Move task and fix missing methods in .NET 4.0 * Create switch for tool handling * Remove unused function * Improve movement * Improve performance of CalculatePath() - Replace Hashset OpenSet with a Binary Heap - Temporary remove maxOffset / minOffset features - Round start location for easier calculation - Add 0.5 to each location in reconstruct path to avoid getting stuck on edges * Add diagonal movement * Remove direct block movement - causes kick for invalid packet movement if moving on the block you are currently standing on * Floor start in A* and improve diagonal walking check * Add helperfunctions to McClient.cs * Prevent client from falling into danger * Add comment to function and remove dependencies * Add comments * Remove debug settings Co-authored-by: ORelio <ORelio@users.noreply.github.com>
2022-08-15 18:26:40 +02:00
2023-05-16 23:19:31 +02:00
_ => throw new ArgumentException("Unknown direction", nameof(direction))
};
}
/// <summary>
/// Check that the chunks at both the start and destination locations have been loaded
/// </summary>
/// <param name="world">Current world</param>
/// <param name="start">Start location</param>
/// <param name="dest">Destination location</param>
/// <returns>Is loading complete</returns>
public static bool CheckChunkLoading(World world, Location start, Location dest)
{
2023-05-16 23:19:31 +02:00
var chunkColumn = world.GetChunkColumn(dest);
if (chunkColumn == null || chunkColumn.FullyLoaded == false)
return false;
chunkColumn = world.GetChunkColumn(start);
if (chunkColumn == null || chunkColumn.FullyLoaded == false)
return false;
return true;
}
}
2023-05-16 23:19:31 +02:00
}