mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
added a time calculation to the pathfinder
This commit is contained in:
parent
6c253744a6
commit
3603de78d0
3 changed files with 14 additions and 7 deletions
|
@ -17,7 +17,7 @@
|
|||
<AndroidSupportedAbis>armeabi-v7a;x86</AndroidSupportedAbis>
|
||||
<AndroidStoreUncompressedFileExtensions>.m4a</AndroidStoreUncompressedFileExtensions>
|
||||
<MandroidI18n />
|
||||
<TargetFrameworkVersion>v7.1</TargetFrameworkVersion>
|
||||
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
|
||||
<MonoGamePlatform>Android</MonoGamePlatform>
|
||||
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
|
||||
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
|
||||
|
@ -105,8 +105,8 @@
|
|||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="Content\Fonts\TestFont.spritefont" />
|
||||
<Content Include="Content\Textures\Test.png" />
|
||||
<None Include="Content\Fonts\TestFont.spritefont" />
|
||||
<None Include="Content\Textures\Test.png" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
|
||||
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" />
|
||||
|
|
|
@ -48,7 +48,7 @@ namespace Demos {
|
|||
this.path = foundPath != null ? foundPath.ToList() : null;
|
||||
|
||||
// print out some info
|
||||
Console.WriteLine("Pathfinding took " + this.pathfinder.LastTriesNeeded + " tries");
|
||||
Console.WriteLine("Pathfinding took " + this.pathfinder.LastTriesNeeded + " tries and "+this.pathfinder.LastTimeNeeded.TotalSeconds+" seconds");
|
||||
if (this.path == null)
|
||||
Console.WriteLine("Couldn't find a path, press the left mouse button to try again");
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MLEM.Pathfinding {
|
||||
|
@ -10,6 +11,7 @@ namespace MLEM.Pathfinding {
|
|||
public int DefaultMaxTries;
|
||||
public bool DefaultAllowDiagonals;
|
||||
public int LastTriesNeeded { get; private set; }
|
||||
public TimeSpan LastTimeNeeded { get; private set; }
|
||||
|
||||
protected AStar(T[] allDirections, T[] adjacentDirections, GetCost defaultCostFunction, bool defaultAllowDiagonals, float defaultCost = 1, int defaultMaxTries = 10000) {
|
||||
this.AllDirections = allDirections;
|
||||
|
@ -21,6 +23,8 @@ namespace MLEM.Pathfinding {
|
|||
}
|
||||
|
||||
public Stack<T> FindPath(T start, T goal, GetCost costFunction = null, float? defaultCost = null, int? maxTries = null, bool? allowDiagonals = null) {
|
||||
var startTime = DateTime.UtcNow;
|
||||
|
||||
var getCost = costFunction ?? this.DefaultCostFunction;
|
||||
var diags = allowDiagonals ?? this.DefaultAllowDiagonals;
|
||||
var tries = maxTries ?? this.DefaultMaxTries;
|
||||
|
@ -31,6 +35,7 @@ namespace MLEM.Pathfinding {
|
|||
open.Add(new PathPoint<T>(start, this.GetManhattanDistance(start, goal), null, 0, defCost));
|
||||
|
||||
var count = 0;
|
||||
Stack<T> ret = null;
|
||||
while (open.Count > 0) {
|
||||
PathPoint<T> current = null;
|
||||
var lowestF = float.MaxValue;
|
||||
|
@ -46,8 +51,8 @@ namespace MLEM.Pathfinding {
|
|||
closed.Add(current);
|
||||
|
||||
if (current.Pos.Equals(goal)) {
|
||||
this.LastTriesNeeded = count;
|
||||
return CompilePath(current);
|
||||
ret = CompilePath(current);
|
||||
break;
|
||||
}
|
||||
|
||||
var dirsUsed = diags ? this.AllDirections : this.AdjacentDirections;
|
||||
|
@ -75,8 +80,10 @@ namespace MLEM.Pathfinding {
|
|||
if (count >= tries)
|
||||
break;
|
||||
}
|
||||
|
||||
this.LastTriesNeeded = count;
|
||||
return null;
|
||||
this.LastTimeNeeded = DateTime.UtcNow - startTime;
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected abstract T AddPositions(T first, T second);
|
||||
|
|
Loading…
Reference in a new issue