C# Superior Pathfinder (ThreadSafe)
Hello RaGEZONE,
I'll show you my C# Pathfinder.
(not tested yet)
Why superior?
- This Pathfinder is fast.
- This Pathfinder uses Asynchronously Tasking, and won't lagg other services in your server.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Atomy.Maths.Pathfinding
{
public class Pathfinder : IDisposable
{
public static short[,] Rotations = new short[8, 2] { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 }, { -1, -1 }, { 1, -1 }, { 1, 1 }, { -1, 1 } };
public volatile ICollection<Matrix> Matrix;
public volatile Coordinate Location;
public volatile Coordinate Target;
public Pathfinder(ICollection<Matrix> Matrix, ref Coordinate Location, Coordinate Target)
{
this.Matrix = Matrix;
this.Location = Location;
this.Target = Target;
}
public async Task<ICollection<Coordinate>> Calculate()
{
ICollection<Coordinate> Output = new HashSet<Coordinate>();
await Threading.Threading.TaskFactory.StartNew(() =>
{
var CurrentTile = Location;
while (true)
{
ICollection<Coordinate> PossibleTiles = new HashSet<Coordinate>();
int CurrentCount = Output.Count;
for (int Obj = 0; Obj < Rotations.Length; Obj++)
{
var WorkingTile = new Coordinate(CurrentTile.X + Rotations[Obj, 0], CurrentTile.Y + Rotations[Obj, 1]);
var TileMatrix = this.Matrix.SingleOrDefault(obj => obj.Coordinate == WorkingTile);
if (TileMatrix != default(Matrix))
{
return;
}
if (TileMatrix.MatrixState.Equals(MatrixState.OPEN) || (TileMatrix.MatrixState == MatrixState.LASTSTEP &&
TileMatrix.Coordinate == Target))
{
PossibleTiles.Add(TileMatrix.Coordinate);
}
}
var BestTiles = PossibleTiles.OrderBy(obj => Coordinate.CalculateDistance(Target, obj));
if (BestTiles.Count() > 0)
{
Output.Add(BestTiles.First());
}
if (Output.Last() == Target || Output.Count == CurrentCount)
{
return;
}
}
});
return Output;
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
public class Matrix
{
public volatile Surface TileSize;
public volatile MatrixState MatrixState;
public volatile Coordinate Coordinate;
public Matrix(Surface TileSize, MatrixState MatrixState, Coordinate Coordinate)
{
this.TileSize = TileSize;
this.MatrixState = MatrixState;
this.Coordinate = Coordinate;
}
}
public enum MatrixState
{
OPEN, BLOCKED, LASTSTEP
}
public class Coordinate
{
public double X;
public double Y;
public Coordinate(double X, double Y)
{
this.X = X;
this.Y = Y;
}
public static bool operator ==(Coordinate Coordinate, Coordinate Comparer)
{
return (Coordinate.X == Comparer.X && Coordinate.Y == Comparer.Y);
}
public static bool operator !=(Coordinate Coordinate, Coordinate Comparer)
{
return (Coordinate.X != Comparer.X && Coordinate.Y != Comparer.Y);
}
public static double CalculateDistance(Coordinate Coordinate, Coordinate Comparer)
{
return Math.Sqrt((Coordinate.X - Comparer.X) * (Coordinate.X - Comparer.X) + (Coordinate.Y - Comparer.Y) * (Coordinate.Y - Comparer.Y));
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}
How to use?
Code:
using (Pathfinder Pathfinder = new Pathfinder(Room.Matrix, ref User.Location, TargetLocation))
{
ICollection<Coordinate> TilesToWalk = Pathfinder.Calculate();
}
Re: C# Superior Pathfinder (ThreadSafe)
Using the 3 Superior releases you made, thanks.
Re: C# Superior Pathfinder (ThreadSafe)
Re: C# Superior Pathfinder (ThreadSafe)
Nice to see your using the new Async features.
Re: C# Superior Pathfinder (ThreadSafe)
Re: C# Superior Pathfinder (ThreadSafe)
How to use this? :/
I use the ButterStorm? :(
Re: C# Superior Pathfinder (ThreadSafe)
Quote:
Originally Posted by
neto737
How to use this? :/
I use the ButterStorm? :(
If you are a noob then do not even bother using this, you will try to change the document and then everything will bug up. The implement this, Butterstorm/Butterfly will need a bit of it recoding. :P
Re: C# Superior Pathfinder (ThreadSafe)
Re: C# Superior Pathfinder (ThreadSafe)
What is Room.Matrix?
Why is the code so buggy.
In other words, more information is nice! I'm not mad, this release is good, but you didn't explain how to use it (well, you didn't explain much)