Skip to content

Commit

Permalink
#35: cleanup, fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed Nov 17, 2017
1 parent 1479305 commit 5fc3d03
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 167 deletions.
4 changes: 2 additions & 2 deletions AppData.dat
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<soundDopplerFactor dataType="Float">1</soundDopplerFactor>
<speedOfSound dataType="Float">360</speedOfSound>
<startScene dataType="Struct" type="Duality.ContentRef`1[[Duality.Resources.Scene]]">
<contentPath dataType="String">Data\Examples\FlowFieldPathfinderExample.Scene.res</contentPath>
<contentPath dataType="String">Data\Examples\PotentialFieldPathfinderExample.Scene.res</contentPath>
</startScene>
<version dataType="UInt">1474</version>
<version dataType="UInt">1501</version>
<websiteUrl dataType="String">http://www.adamslair.net</websiteUrl>
</root>
<!-- XmlFormatterBase Document Separator -->

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void ICmpInitializable.OnInit(InitContext context)
void ICmpInitializable.OnShutdown(ShutdownContext context)
{
DualityApp.Mouse.ButtonDown -= Mouse_ButtonDown;
_dynamicPotentialField.RemovePotentialFunction(this);
_dynamicPotentialField?.RemovePotentialFunction(this);
}

void ICmpUpdatable.OnUpdate()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Collections.Generic;
using Duality.Editor;
using Pathfindax.Algorithms;
using Duality.Editor;
using Pathfindax.Factories;
using Pathfindax.Grid;
using Pathfindax.Nodes;
using Pathfindax.Utils;

namespace Duality.Plugins.Pathfindax.Components
Expand All @@ -21,22 +18,13 @@ public class AstarPathfinderComponent : PathfinderComponentBase<IDefinitionNodeN
/// </summary>
public int MaxClearance { get; set; } = 5;

/// <inheritdoc />
public override void OnInit(InitContext context)
{
if (context == InitContext.Activate && DualityApp.ExecContext == DualityApp.ExecutionContext.Game)
{
var sourceNodeNetwork = GetSourceNodeNetwork();
if (sourceNodeNetwork == null) return;
Pathfinder = PathfinderFactory.CreatePathfinder(sourceNodeNetwork, new AStarAlgorithm(), (definitionNodeNetwork, algorithm) =>
{
var nodeGenerators = new List<IPathfindNodeGenerator<AstarNode>>();
if (definitionNodeNetwork is IDefinitionNodeGrid sourceNodeGrid)
nodeGenerators.Add(new GridClearanceGenerator(sourceNodeGrid, MaxClearance));
var astarNodeNetwork = new AstarNodeNetwork(definitionNodeNetwork, nodeGenerators.ToArray());
return PathfinderFactory.CreateRequestProcesser(astarNodeNetwork, algorithm);
}, PathfinderId, AmountOfThreads);
Pathfinder.Start();
Pathfinder = PathfinderFactory.CreateAstarPathfinder(PathfinderId, sourceNodeNetwork, MaxClearance, AmountOfThreads);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Duality.Editor;
using Pathfindax.Algorithms;
using Pathfindax.Factories;
using Pathfindax.Grid;
using Pathfindax.Paths;
Expand All @@ -8,7 +7,7 @@
namespace Duality.Plugins.Pathfindax.Components
{
/// <summary>
/// Provides a way for other components to request a path from A to B. Uses the A* algorithm.
/// Provides a way for other components to request a path to B. Uses flowfields.
/// </summary>
[EditorHintCategory(PathfindaxStrings.Pathfindax)]
[RequiredComponent(typeof(IDefinitionNodeNetworkProvider<DefinitionNodeGrid>))]
Expand All @@ -25,19 +24,13 @@ public class FlowFieldPathfinderComponent : PathfinderComponentBase<DefinitionNo
/// </summary>
public int MaxCachedFlowFields { get; set; } = 100;

/// <inheritdoc />
public override void OnInit(InitContext context)
{
if (context == InitContext.Activate && DualityApp.ExecContext == DualityApp.ExecutionContext.Game)
{
var sourceNodeNetwork = GetSourceNodeNetwork();
if (sourceNodeNetwork == null) return;
Pathfinder = PathfinderFactory.CreatePathfinder(sourceNodeNetwork, new PotentialFieldAlgorithm(100), (definitionNodeGrid, algorithm) =>
{
var dijkstraNodeGrid = new DijkstraNodeGrid(definitionNodeGrid, MaxClearance);
return PathfinderFactory.CreateRequestProcesser(dijkstraNodeGrid, algorithm);
}, PathfinderId, AmountOfThreads);
Pathfinder.Start();
var definitionNodeGrid = GetSourceNodeNetwork();
if (definitionNodeGrid == null) return;
Pathfinder = PathfinderFactory.CreateFlowFieldPathfinder(PathfinderId, definitionNodeGrid, MaxClearance, MaxCachedFlowFields, AmountOfThreads);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Duality.Plugins.Pathfindax.Components
{
/// <summary>
/// Interface for duality pathfinders
/// </summary>
public interface IDualityPathfinderComponent
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@

namespace Duality.Plugins.Pathfindax.Components
{
public interface IDualityPathfinderComponent
{

}

/// <summary>
/// Base class for duality pathfinders
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Duality.Editor;
using Pathfindax.Factories;
using Pathfindax.Grid;
using Pathfindax.Paths;
using Pathfindax.Utils;

namespace Duality.Plugins.Pathfindax.Components
{
/// <summary>
/// Provides a way for other components to request a path to B. Uses potential fields.
/// </summary>
[EditorHintCategory(PathfindaxStrings.Pathfindax)]
[RequiredComponent(typeof(IDefinitionNodeNetworkProvider<DefinitionNodeGrid>))]
public class PotentialFieldPathfinderComponent : PathfinderComponentBase<DefinitionNodeGrid>
{
/// <summary>
/// The max calculated clearance. Any clearance value higher than will be set to this.
/// Try to keep this as low as possible to prevent wasting time calculating clearance values that will never be used.
/// </summary>
public int MaxClearance { get; set; } = 5;

/// <summary>
/// The maximum amount of cached <see cref="PotentialField"/>s
/// </summary>
public int MaxCachedFlowFields { get; set; } = 100;

public override void OnInit(InitContext context)
{
if (context == InitContext.Activate && DualityApp.ExecContext == DualityApp.ExecutionContext.Game)
{
var definitionNodeGrid = GetSourceNodeNetwork();
if (definitionNodeGrid == null) return;
Pathfinder = PathfinderFactory.CreatePotentialFieldPathfinder(PathfinderId, definitionNodeGrid, MaxClearance, MaxCachedFlowFields, AmountOfThreads);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@
<ItemGroup>
<Compile Include="Components\FlowFieldPathfinderComponent.cs" />
<Compile Include="Components\AstarPathfinderComponent.cs" />
<Compile Include="Components\Interfaces\IDualityPathfinderComponent.cs" />
<Compile Include="Components\Interfaces\IPathProvider.cs" />
<Compile Include="Components\PathVisualizer.cs" />
<Compile Include="Components\PathfinderComponentBase.cs" />
<Compile Include="Components\NodeNetworkVisualizer.cs" />
<Compile Include="Components\PotentialFieldPathfinderComponent.cs" />
<Compile Include="PathfindaxDualityCorePlugin.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
38 changes: 38 additions & 0 deletions Source/Code/Pathfindax/Factories/PathfinderFactory.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
using System;
using System.Collections.Generic;
using Pathfindax.Algorithms;
using Pathfindax.Grid;
using Pathfindax.Nodes;
using Pathfindax.PathfindEngine;
using Pathfindax.Paths;

namespace Pathfindax.Factories
{
public static class PathfinderFactory
{
public static IPathfinder CreateFlowFieldPathfinder(string pathfinderId, DefinitionNodeGrid nodeGrid, int maxClearance, int maxCachedFlowFields, int amountOfThreads)
{
var pathfinder = CreatePathfinder(nodeGrid, new FlowFieldAlgorithm(maxCachedFlowFields), (definitionNodeGrid, algorithm) =>
{
var dijkstraNodeGrid = new DijkstraNodeGrid(definitionNodeGrid, maxClearance);
return CreateRequestProcesser(dijkstraNodeGrid, algorithm);
}, pathfinderId, amountOfThreads);
pathfinder.Start();
return pathfinder;
}

public static IPathfinder CreatePotentialFieldPathfinder(string pathfinderId, DefinitionNodeGrid nodeGrid, int maxClearance, int maxCachedFlowFields, int amountOfThreads)
{
var pathfinder = CreatePathfinder(nodeGrid, new PotentialFieldAlgorithm(maxCachedFlowFields), (definitionNodeGrid, algorithm) =>
{
var dijkstraNodeGrid = new DijkstraNodeGrid(definitionNodeGrid, maxClearance);
return CreateRequestProcesser(dijkstraNodeGrid, algorithm);
}, pathfinderId, amountOfThreads);
pathfinder.Start();
return pathfinder;
}

public static IPathfinder CreateAstarPathfinder(string pathfinderId, IDefinitionNodeNetwork nodeNetwork, int maxClearance, int amountOfThreads)
{
var pathfinder = CreatePathfinder(nodeNetwork, new AStarAlgorithm(), (definitionNodeNetwork, algorithm) =>
{
var nodeGenerators = new List<IPathfindNodeGenerator<AstarNode>>();
if (definitionNodeNetwork is IDefinitionNodeGrid sourceNodeGrid)
nodeGenerators.Add(new GridClearanceGenerator(sourceNodeGrid, maxClearance));
var astarNodeNetwork = new AstarNodeNetwork(definitionNodeNetwork, nodeGenerators.ToArray());
return CreateRequestProcesser(astarNodeNetwork, algorithm);
}, pathfinderId, amountOfThreads);
pathfinder.Start();
return pathfinder;
}

public static Pathfinder<TSourceNodeNetwork, TThreadNodeNetwork, TPath> CreatePathfinder<TSourceNodeNetwork, TThreadNodeNetwork, TPath>(TSourceNodeNetwork sourceNodeNetwork, IPathFindAlgorithm<TThreadNodeNetwork, TPath> pathFindAlgorithm, Func<TSourceNodeNetwork, IPathFindAlgorithm<TThreadNodeNetwork, TPath>, PathRequestProcesser<TThreadNodeNetwork, TPath>> processerConstructor, string key, int threads = 1)
where TSourceNodeNetwork : IDefinitionNodeNetwork
where TThreadNodeNetwork : IPathfindNodeNetwork
Expand Down

0 comments on commit 5fc3d03

Please sign in to comment.