fix many errors, warnings and more, refactoring of naming, remove NODE_SIZE constant from original AStar algorithm

This commit is contained in:
Xevion
2020-11-08 17:58:25 -06:00
parent b4e281f158
commit 335a3d7b7f
10 changed files with 152 additions and 108 deletions

View File

@@ -50,12 +50,11 @@ LightmapSettings:
m_BounceScale: 1
m_IndirectOutputScale: 1
m_AlbedoBoost: 1
m_TemporalCoherenceThreshold: 1
m_EnvironmentLightingMode: 0
m_EnableBakedLightmaps: 0
m_EnableRealtimeLightmaps: 0
m_LightmapEditorSettings:
serializedVersion: 10
serializedVersion: 12
m_Resolution: 2
m_BakeResolution: 40
m_AtlasSize: 1024
@@ -63,6 +62,7 @@ LightmapSettings:
m_AOMaxDistance: 1
m_CompAOExponent: 1
m_CompAOExponentDirect: 0
m_ExtractAmbientOcclusion: 0
m_Padding: 2
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
@@ -77,10 +77,16 @@ LightmapSettings:
m_PVRDirectSampleCount: 32
m_PVRSampleCount: 500
m_PVRBounces: 2
m_PVREnvironmentSampleCount: 500
m_PVREnvironmentReferencePointCount: 2048
m_PVRFilteringMode: 2
m_PVRDenoiserTypeDirect: 0
m_PVRDenoiserTypeIndirect: 0
m_PVRDenoiserTypeAO: 0
m_PVRFilterTypeDirect: 0
m_PVRFilterTypeIndirect: 0
m_PVRFilterTypeAO: 0
m_PVRFilteringMode: 1
m_PVREnvironmentMIS: 0
m_PVRCulling: 1
m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 5
@@ -89,6 +95,7 @@ LightmapSettings:
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_ShowResolutionOverlay: 1
m_ExportTrainingData: 0
m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 1
--- !u!196 &4
@@ -116,9 +123,10 @@ NavMeshSettings:
--- !u!1 &519420028
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 519420032}
- component: {fileID: 519420031}
@@ -133,20 +141,28 @@ GameObject:
--- !u!81 &519420029
AudioListener:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028}
m_Enabled: 1
--- !u!20 &519420031
Camera:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028}
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 2
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
m_BackGroundColor: {r: 0.13207549, g: 0.13207549, b: 0.13207549, a: 0}
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_FocalLength: 50
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
@@ -176,8 +192,9 @@ Camera:
--- !u!4 &519420032
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -10}

View File

@@ -2,66 +2,67 @@
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Algorithms;
public class AStar : Pathfinding {
private Grid grid;
public class AStar : IPathfinding {
private NodeGrid _nodeGrid;
private Stack<Node> path;
private List<Node> openList;
private List<Node> closedList;
private Stack<Node> _path;
private List<Node> _openList;
private List<Node> _closedList;
public AStar(Grid grid) {
this.grid = grid;
public AStar(NodeGrid nodeGrid) {
this._nodeGrid = nodeGrid;
}
public Tuple<List<Grid>, Stack<Node>> FindPath(Vector2 Start, Vector2 End) {
Node start = new Node(new Vector2((int) (Start.X / Node.NODE_SIZE), (int) (Start.Y / Node.NODE_SIZE)), true);
Node end = new Node(new Vector2((int) (End.X / Node.NODE_SIZE), (int) (End.Y / Node.NODE_SIZE)), true);
public Tuple<List<NodeGrid>, Stack<Node>> FindPath(Vector2 Start, Vector2 End) {
var start = new Node(Start, true);
var end = new Node(End, true);
path = new Stack<Node>();
openList = new List<Node>();
closedList = new List<Node>();
_path = new Stack<Node>();
_openList = new List<Node>();
_closedList = new List<Node>();
Node current = start;
// add start node to Open List
openList.Add(start);
_openList.Add(start);
while (openList.Count != 0 && !closedList.Exists(x => x.Position == end.Position)) {
current = openList[0];
openList.Remove(current);
closedList.Add(current);
List<Node> adjacencies = this.grid.GetAdjacentNodes(current);
while (_openList.Count != 0 && !_closedList.Exists(x => x.Position == end.Position)) {
current = _openList[0];
_openList.Remove(current);
_closedList.Add(current);
List<Node> adjacentNodes = this._nodeGrid.GetAdjacentNodes(current);
foreach (Node n in adjacencies) {
if (!closedList.Contains(n) && n.Walkable) {
if (!openList.Contains(n)) {
foreach (Node n in adjacentNodes) {
if (!_closedList.Contains(n) && n.Walkable) {
if (!_openList.Contains(n)) {
n.Parent = current;
n.DistanceToTarget = Math.Abs(n.Position.X - end.Position.X) +
Math.Abs(n.Position.Y - end.Position.Y);
n.DistanceToTarget = NodeGrid.Manhattan(n, end);
n.Cost = n.Weight + n.Parent.Cost;
openList.Add(n);
openList = openList.OrderBy(node => node.F).ToList<Node>();
_openList.Add(n);
_openList = _openList.OrderBy(node => node.F).ToList<Node>();
}
}
}
}
// construct path, if end was not closed return null
if (!closedList.Exists(x => x.Position == end.Position)) {
if (!_closedList.Exists(x => x.Position == end.Position)) {
return null;
}
// if all good, return path
Node temp = closedList[closedList.IndexOf(current)];
Node temp = _closedList[_closedList.IndexOf(current)];
if (temp == null) return null;
do {
path.Push(temp);
_path.Push(temp);
temp = temp.Parent;
} while (temp != start && temp != null);
return path;
return _path;
}
private StateGrid compileState() {}
private StateGrid compileState() {
}
}

View File

@@ -1,28 +0,0 @@
using System.Collections.Generic;
using NUnit.Framework.Constraints;
public class Grid {
private List<List<Node>> grid;
public Grid(int width, int height) {
this.grid = new List<List<Node>>();
}
public int GridRows => this.grid[0].Count;
public int GridCols => this.grid.Count;
public List<Node> GetAdjacentNodes(Node node) {
List<Node> temp = new List<Node>();
int row = (int) node.Position.Y;
int col = (int) node.Position.X;
if (row + 1 < GridRows) temp.Add(this.grid[col][row + 1]);
if (row - 1 >= 0) temp.Add(this.grid[col][row - 1]);
if (col - 1 >= 0) temp.Add(this.grid[col - 1][row]);
if (col + 1 < GridCols) temp.Add(this.grid[col + 1][row]);
return temp;
}
}

View File

@@ -1,34 +1,33 @@
using System.Numerics;
public class Node {
// Change this depending on what the desired size is for each element in the grid
public static int NODE_SIZE = 32;
public Node Parent;
public Vector2 Position;
namespace Algorithms {
public class Node {
// Change this depending on what the desired size is for each element in the grid
public Node Parent;
public Vector2 Position;
public Vector2 Center => new Vector2(Position.X + NODE_SIZE / 2, Position.Y + NODE_SIZE / 2);
// A* Algorithm variables
public float DistanceToTarget;
public float Cost;
public float Weight;
public float DistanceToTarget;
public float Cost;
public float Weight;
public float F {
get {
if (DistanceToTarget != -1 && Cost != -1)
return DistanceToTarget + Cost;
else
public float F {
get {
if (DistanceToTarget != -1 && Cost != -1)
return DistanceToTarget + Cost;
return -1;
}
}
public bool Walkable;
public Node(Vector2 pos, bool walkable, float weight = 1) {
Parent = null;
Position = pos;
DistanceToTarget = -1;
Cost = 1;
Weight = weight;
Walkable = walkable;
}
}
public bool Walkable;
public Node(Vector2 pos, bool walkable, float weight = 1) {
Parent = null;
Position = pos;
DistanceToTarget = -1;
Cost = 1;
Weight = weight;
Walkable = walkable;
}
}

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Algorithms;
using NUnit.Framework.Constraints;
using UnityEngine.SocialPlatforms;
public class NodeGrid {
private List<List<Node>> grid;
private readonly int _width;
private readonly int _height;
public NodeGrid(int width, int height) {
if(width <= 0)
throw new ArgumentOutOfRangeException(nameof(width), $"The width of the grid must be a positive non-zero integer.");
if (height <= 0)
throw new ArgumentOutOfRangeException(nameof(height), $"The height of the grid must be a positive non-zero integer.");
this.grid = new List<List<Node>>(width);
for(int _ in Enumerable.Range(0, height - 1)) {}
this._width = width;
this._height = height;
}
public NodeGrid(List<List<Node>> grid) {
this.grid = grid;
this._height = this.grid[0].Count;
this._width = this.grid.Count;
}
public List<Node> GetAdjacentNodes(Node node) {
List<Node> temp = new List<Node>();
int row = (int) node.Position.Y;
int col = (int) node.Position.X;
if (row + 1 < _height) temp.Add(this.grid[col][row + 1]);
if (row - 1 >= 0) temp.Add(this.grid[col][row - 1]);
if (col - 1 >= 0) temp.Add(this.grid[col - 1][row]);
if (col + 1 < _width) temp.Add(this.grid[col + 1][row]);
return temp;
}
public static float Manhattan(Node first, Node second) {
return Math.Abs(first.Position.X - second.Position.X) + Math.Abs(first.Position.Y - second.Position.Y);
}
}

View File

@@ -1,16 +1,17 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using Algorithms;
/// <summary>
/// A general interface for implementing pathfinding algorithms.
/// </summary>
public interface Pathfinding {
public interface IPathfinding {
/// <summary>
/// Finds a valid path between two Node objects.
/// </summary>
/// <param name="Start"></param>
/// <param name="End"></param>
/// <returns>A List of Grid objects representing the timeline of Pathfinding</returns>
Tuple<List<Grid>, Stack<Node>> FindPath(Vector2 Start, Vector2 End);
/// <param name="start">The position from which pathfinding begins</param>
/// <param name="end">The position trying to be found via pathfinding</param>
/// <returns>A List of NodeGridGrid objects representing the timeline of Pathfinding</returns>
Tuple<List<NodeGrid>, Stack<Node>> FindPath(Vector2 start, Vector2 end);
}

View File

@@ -6,6 +6,6 @@ EditorBuildSettings:
serializedVersion: 2
m_Scenes:
- enabled: 1
path: Assets/Scenes/SampleScene.unity
path: Assets/Scenes/Default.unity
guid: 2cda990e2423bbf4892e6590ba056729
m_configObjects: {}

View File

@@ -8,14 +8,18 @@ EditorSettings:
m_SerializationMode: 2
m_LineEndingsForNewScripts: 2
m_DefaultBehaviorMode: 1
m_PrefabRegularEnvironment: {fileID: 0}
m_PrefabUIEnvironment: {fileID: 0}
m_SpritePackerMode: 4
m_SpritePackerPaddingPower: 1
m_EtcTextureCompressorBehavior: 1
m_EtcTextureFastCompressor: 1
m_EtcTextureNormalCompressor: 2
m_EtcTextureBestCompressor: 4
m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd
m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd;asmdef
m_ProjectGenerationRootNamespace:
m_UserGeneratedProjectSuffix:
m_CollabEditorSettings:
inProgressEnabled: 1
m_EnableTextureStreamingInEditMode: 1
m_EnableTextureStreamingInPlayMode: 1
m_AsyncShaderCompilation: 1