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_BounceScale: 1
m_IndirectOutputScale: 1 m_IndirectOutputScale: 1
m_AlbedoBoost: 1 m_AlbedoBoost: 1
m_TemporalCoherenceThreshold: 1
m_EnvironmentLightingMode: 0 m_EnvironmentLightingMode: 0
m_EnableBakedLightmaps: 0 m_EnableBakedLightmaps: 0
m_EnableRealtimeLightmaps: 0 m_EnableRealtimeLightmaps: 0
m_LightmapEditorSettings: m_LightmapEditorSettings:
serializedVersion: 10 serializedVersion: 12
m_Resolution: 2 m_Resolution: 2
m_BakeResolution: 40 m_BakeResolution: 40
m_AtlasSize: 1024 m_AtlasSize: 1024
@@ -63,6 +62,7 @@ LightmapSettings:
m_AOMaxDistance: 1 m_AOMaxDistance: 1
m_CompAOExponent: 1 m_CompAOExponent: 1
m_CompAOExponentDirect: 0 m_CompAOExponentDirect: 0
m_ExtractAmbientOcclusion: 0
m_Padding: 2 m_Padding: 2
m_LightmapParameters: {fileID: 0} m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1 m_LightmapsBakeMode: 1
@@ -77,10 +77,16 @@ LightmapSettings:
m_PVRDirectSampleCount: 32 m_PVRDirectSampleCount: 32
m_PVRSampleCount: 500 m_PVRSampleCount: 500
m_PVRBounces: 2 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_PVRFilterTypeDirect: 0
m_PVRFilterTypeIndirect: 0 m_PVRFilterTypeIndirect: 0
m_PVRFilterTypeAO: 0 m_PVRFilterTypeAO: 0
m_PVRFilteringMode: 1 m_PVREnvironmentMIS: 0
m_PVRCulling: 1 m_PVRCulling: 1
m_PVRFilteringGaussRadiusDirect: 1 m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 5 m_PVRFilteringGaussRadiusIndirect: 5
@@ -89,6 +95,7 @@ LightmapSettings:
m_PVRFilteringAtrousPositionSigmaIndirect: 2 m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1 m_PVRFilteringAtrousPositionSigmaAO: 1
m_ShowResolutionOverlay: 1 m_ShowResolutionOverlay: 1
m_ExportTrainingData: 0
m_LightingDataAsset: {fileID: 0} m_LightingDataAsset: {fileID: 0}
m_UseShadowmask: 1 m_UseShadowmask: 1
--- !u!196 &4 --- !u!196 &4
@@ -116,9 +123,10 @@ NavMeshSettings:
--- !u!1 &519420028 --- !u!1 &519420028
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0} m_PrefabInstance: {fileID: 0}
serializedVersion: 5 m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component: m_Component:
- component: {fileID: 519420032} - component: {fileID: 519420032}
- component: {fileID: 519420031} - component: {fileID: 519420031}
@@ -133,20 +141,28 @@ GameObject:
--- !u!81 &519420029 --- !u!81 &519420029
AudioListener: AudioListener:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028} m_GameObject: {fileID: 519420028}
m_Enabled: 1 m_Enabled: 1
--- !u!20 &519420031 --- !u!20 &519420031
Camera: Camera:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028} m_GameObject: {fileID: 519420028}
m_Enabled: 1 m_Enabled: 1
serializedVersion: 2 serializedVersion: 2
m_ClearFlags: 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: m_NormalizedViewPortRect:
serializedVersion: 2 serializedVersion: 2
x: 0 x: 0
@@ -176,8 +192,9 @@ Camera:
--- !u!4 &519420032 --- !u!4 &519420032
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0} m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInternal: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 519420028} m_GameObject: {fileID: 519420028}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -10} m_LocalPosition: {x: 0, y: 0, z: -10}

View File

@@ -2,66 +2,67 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
using Algorithms;
public class AStar : Pathfinding { public class AStar : IPathfinding {
private Grid grid; private NodeGrid _nodeGrid;
private Stack<Node> path; private Stack<Node> _path;
private List<Node> openList; private List<Node> _openList;
private List<Node> closedList; private List<Node> _closedList;
public AStar(Grid grid) { public AStar(NodeGrid nodeGrid) {
this.grid = grid; this._nodeGrid = nodeGrid;
} }
public Tuple<List<Grid>, Stack<Node>> FindPath(Vector2 Start, Vector2 End) { public Tuple<List<NodeGrid>, 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); var start = new Node(Start, true);
Node end = new Node(new Vector2((int) (End.X / Node.NODE_SIZE), (int) (End.Y / Node.NODE_SIZE)), true); var end = new Node(End, true);
path = new Stack<Node>(); _path = new Stack<Node>();
openList = new List<Node>(); _openList = new List<Node>();
closedList = new List<Node>(); _closedList = new List<Node>();
Node current = start; Node current = start;
// add start node to Open List // add start node to Open List
openList.Add(start); _openList.Add(start);
while (openList.Count != 0 && !closedList.Exists(x => x.Position == end.Position)) { while (_openList.Count != 0 && !_closedList.Exists(x => x.Position == end.Position)) {
current = openList[0]; current = _openList[0];
openList.Remove(current); _openList.Remove(current);
closedList.Add(current); _closedList.Add(current);
List<Node> adjacencies = this.grid.GetAdjacentNodes(current); List<Node> adjacentNodes = this._nodeGrid.GetAdjacentNodes(current);
foreach (Node n in adjacencies) { foreach (Node n in adjacentNodes) {
if (!closedList.Contains(n) && n.Walkable) { if (!_closedList.Contains(n) && n.Walkable) {
if (!openList.Contains(n)) { if (!_openList.Contains(n)) {
n.Parent = current; n.Parent = current;
n.DistanceToTarget = Math.Abs(n.Position.X - end.Position.X) + n.DistanceToTarget = NodeGrid.Manhattan(n, end);
Math.Abs(n.Position.Y - end.Position.Y);
n.Cost = n.Weight + n.Parent.Cost; n.Cost = n.Weight + n.Parent.Cost;
openList.Add(n); _openList.Add(n);
openList = openList.OrderBy(node => node.F).ToList<Node>(); _openList = _openList.OrderBy(node => node.F).ToList<Node>();
} }
} }
} }
} }
// construct path, if end was not closed return null // 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; return null;
} }
// if all good, return path // if all good, return path
Node temp = closedList[closedList.IndexOf(current)]; Node temp = _closedList[_closedList.IndexOf(current)];
if (temp == null) return null; if (temp == null) return null;
do { do {
path.Push(temp); _path.Push(temp);
temp = temp.Parent; temp = temp.Parent;
} while (temp != start && temp != null); } 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,13 +1,12 @@
using System.Numerics; using System.Numerics;
public class Node { namespace Algorithms {
public class Node {
// Change this depending on what the desired size is for each element in the grid // 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 Node Parent;
public Vector2 Position; 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 DistanceToTarget;
public float Cost; public float Cost;
public float Weight; public float Weight;
@@ -16,7 +15,6 @@ public class Node {
get { get {
if (DistanceToTarget != -1 && Cost != -1) if (DistanceToTarget != -1 && Cost != -1)
return DistanceToTarget + Cost; return DistanceToTarget + Cost;
else
return -1; return -1;
} }
} }
@@ -31,4 +29,5 @@ public class Node {
Weight = weight; Weight = weight;
Walkable = walkable; 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Numerics; using System.Numerics;
using Algorithms;
/// <summary> /// <summary>
/// A general interface for implementing pathfinding algorithms. /// A general interface for implementing pathfinding algorithms.
/// </summary> /// </summary>
public interface Pathfinding { public interface IPathfinding {
/// <summary> /// <summary>
/// Finds a valid path between two Node objects. /// Finds a valid path between two Node objects.
/// </summary> /// </summary>
/// <param name="Start"></param> /// <param name="start">The position from which pathfinding begins</param>
/// <param name="End"></param> /// <param name="end">The position trying to be found via pathfinding</param>
/// <returns>A List of Grid objects representing the timeline of Pathfinding</returns> /// <returns>A List of NodeGridGrid objects representing the timeline of Pathfinding</returns>
Tuple<List<Grid>, Stack<Node>> FindPath(Vector2 Start, Vector2 End); Tuple<List<NodeGrid>, Stack<Node>> FindPath(Vector2 start, Vector2 end);
} }

View File

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

View File

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