1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

added a time calculation to the pathfinder

This commit is contained in:
Ellpeck 2019-09-01 10:54:25 +02:00
parent 6c253744a6
commit 3603de78d0
3 changed files with 14 additions and 7 deletions

View file

@ -17,7 +17,7 @@
<AndroidSupportedAbis>armeabi-v7a;x86</AndroidSupportedAbis> <AndroidSupportedAbis>armeabi-v7a;x86</AndroidSupportedAbis>
<AndroidStoreUncompressedFileExtensions>.m4a</AndroidStoreUncompressedFileExtensions> <AndroidStoreUncompressedFileExtensions>.m4a</AndroidStoreUncompressedFileExtensions>
<MandroidI18n /> <MandroidI18n />
<TargetFrameworkVersion>v7.1</TargetFrameworkVersion> <TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<MonoGamePlatform>Android</MonoGamePlatform> <MonoGamePlatform>Android</MonoGamePlatform>
<AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest> <AndroidManifest>Properties\AndroidManifest.xml</AndroidManifest>
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk> <AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
@ -105,8 +105,8 @@
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Content\Fonts\TestFont.spritefont" /> <None Include="Content\Fonts\TestFont.spritefont" />
<Content Include="Content\Textures\Test.png" /> <None Include="Content\Textures\Test.png" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" /> <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" /> <Import Project="$(MSBuildExtensionsPath)\MonoGame\v3.0\MonoGame.Content.Builder.targets" />

View file

@ -48,7 +48,7 @@ namespace Demos {
this.path = foundPath != null ? foundPath.ToList() : null; this.path = foundPath != null ? foundPath.ToList() : null;
// print out some info // 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) if (this.path == null)
Console.WriteLine("Couldn't find a path, press the left mouse button to try again"); Console.WriteLine("Couldn't find a path, press the left mouse button to try again");
} }

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace MLEM.Pathfinding { namespace MLEM.Pathfinding {
@ -10,6 +11,7 @@ namespace MLEM.Pathfinding {
public int DefaultMaxTries; public int DefaultMaxTries;
public bool DefaultAllowDiagonals; public bool DefaultAllowDiagonals;
public int LastTriesNeeded { get; private set; } 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) { protected AStar(T[] allDirections, T[] adjacentDirections, GetCost defaultCostFunction, bool defaultAllowDiagonals, float defaultCost = 1, int defaultMaxTries = 10000) {
this.AllDirections = allDirections; 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) { 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 getCost = costFunction ?? this.DefaultCostFunction;
var diags = allowDiagonals ?? this.DefaultAllowDiagonals; var diags = allowDiagonals ?? this.DefaultAllowDiagonals;
var tries = maxTries ?? this.DefaultMaxTries; 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)); open.Add(new PathPoint<T>(start, this.GetManhattanDistance(start, goal), null, 0, defCost));
var count = 0; var count = 0;
Stack<T> ret = null;
while (open.Count > 0) { while (open.Count > 0) {
PathPoint<T> current = null; PathPoint<T> current = null;
var lowestF = float.MaxValue; var lowestF = float.MaxValue;
@ -46,8 +51,8 @@ namespace MLEM.Pathfinding {
closed.Add(current); closed.Add(current);
if (current.Pos.Equals(goal)) { if (current.Pos.Equals(goal)) {
this.LastTriesNeeded = count; ret = CompilePath(current);
return CompilePath(current); break;
} }
var dirsUsed = diags ? this.AllDirections : this.AdjacentDirections; var dirsUsed = diags ? this.AllDirections : this.AdjacentDirections;
@ -75,8 +80,10 @@ namespace MLEM.Pathfinding {
if (count >= tries) if (count >= tries)
break; break;
} }
this.LastTriesNeeded = count; this.LastTriesNeeded = count;
return null; this.LastTimeNeeded = DateTime.UtcNow - startTime;
return ret;
} }
protected abstract T AddPositions(T first, T second); protected abstract T AddPositions(T first, T second);