public PathData(PathNode[] nodes, PathFollowerTypes targetType, int cost) { Nodes = nodes; var length = nodes.Length; VectorPath = new Vector3[length]; for (int i = 0; i < length; i++) { VectorPath[i] = nodes[i].Trans.position; } TargetFollowerType = targetType; Cost = cost; }
private void DeserializeWeightData(string data) { Dictionary <string, object> dict = (Dictionary <string, object>)Json.Deserialize(data); weights = new Dictionary <PathFollowerTypes, Dictionary <PathType, int> >(); foreach (KeyValuePair <string, object> kvp in dict) { PathFollowerTypes t = (PathFollowerTypes)Enum.Parse(typeof(PathFollowerTypes), kvp.Key); Dictionary <string, object> wObjects = (Dictionary <string, object>)kvp.Value; Dictionary <PathType, int> w = new Dictionary <PathType, int>(); foreach (KeyValuePair <string, object> d in wObjects) { var key = (PathType)Enum.Parse(typeof(PathType), d.Key); w.Add(key, (int)((long)d.Value)); } weights.Add(t, w); } }
public override bool Walkable(PathFollowerTypes follower) { switch (follower) { case PathFollowerTypes.flying: return(true); case PathFollowerTypes.foot: return(true); case PathFollowerTypes.mech: return(true); case PathFollowerTypes.track: return(false); case PathFollowerTypes.wheel: return(false); } return(false); }
public void GetPath(PathNode start, PathNode end, PathFollowerTypes type) { var openSet = new List <PathNode>(); openSet.Add(start); var closedSet = new List <PathNode>(); var cameFrom = new Dictionary <PathNode, PathNode>(); var globalScore = new Dictionary <PathNode, float>(); globalScore.Add(start, 0); var fScore = new Dictionary <PathNode, float>(); fScore.Add(start, globalScore[start] + Heuristics.ManhattanDistance(start.GridPosition, end.GridPosition)); var iWeights = PathWeights.GetWeights(type); float[] weights = new float[iWeights.Count]; for (int w = 0; w < weights.Length; w++) { weights[w] = (float)iWeights[(PathType)w] * gridNodeSize; } while (openSet.Count > 0) { float lowestF = -1; PathNode current = null; foreach (PathNode node in openSet) { var score = fScore[node]; if (score < lowestF || lowestF == -1) { lowestF = score; current = node; } } if (current == end) { Debug.Log("Path found! With a cost of " + fScore[current]); RecreatePath(current, cameFrom); start.SetColor(Color.green); end.SetColor(Color.red); return; } openSet.Remove(current); closedSet.Add(current); var connections = current.GetConnections(); float tentativeScore = 0; foreach (PathNode node in connections) { if (node == null || !node.Walkable(type)) { continue; } tentativeScore = globalScore[current] + weights[(int)node.Type]; //Heuristics.ManhattanDistance(current.GridPosition, node.GridPosition); if (closedSet.Contains(node) && tentativeScore >= globalScore[node]) { continue; } else { if (cameFrom.ContainsKey(node)) { cameFrom[node] = current; } else { cameFrom.Add(node, current); } globalScore[node] = tentativeScore; fScore[node] = tentativeScore + Heuristics.ManhattanDistance(node.GridPosition, end.GridPosition); if (!openSet.Contains(node)) { openSet.Add(node); } } } } Debug.Log("Failure"); }
public void GetPath(PathNode start, PathNode end, PathFollowerTypes type, Action <PathData> callback) { Debug.Log("Start = " + start.name + " End = " + end.name); var endPos = end.GridPosition; var closedSet = new List <PathNode>(); var openSet = new List <PathNode>(); openSet.Add(start); var navigatedSet = new List <PathNode>(); var pathWeight = new List <int>(); List <float> score = new List <float>(); float estimatedScore = 0; var openConnections = new PathNode[0]; var iWeights = PathWeights.GetWeights(type); float[] weights = new float[iWeights.Count]; for (int i = 0; i < weights.Length; i++) { weights[i] = iWeights[(PathType)i] * gridNodeSize; } bool endIsFound = false; for (int i = 0; i < openSet.Count; i++) { if (openSet[i] == end) { endIsFound = true; break; } Debug.Log("Checking connections of: " + openSet[i].name, openSet[i].gameObject); openConnections = GetConnections(openSet[i]); int index = 0; float distance = 100000; bool foundNewNode = false; bool[] newConnections = new bool[4]; bool hasConnectionsAvailable = false; for (int c = 0; c < 4; c++) { if (navigatedSet.Contains(openConnections[c])) { continue; } newConnections[c] = true; hasConnectionsAvailable = true; } if (!hasConnectionsAvailable) { continue; } for (int c = 0; c < 4; c++) { if (openConnections[c] == null || !newConnections[c]) { continue; } if (openConnections[c] == end) { distance = 0; index = c; foundNewNode = true; break; } //Debug.Log((openConnections[c] == null) +" "+c, openSet[i].gameObject); estimatedScore = Heuristics.ManhattanDistance(openConnections[c].GridPosition, endPos) + weights[(int)openConnections[c].Type]; if (estimatedScore < distance) { distance = estimatedScore; index = c; foundNewNode = true; } } if (!foundNewNode || !openConnections[index].Walkable(type)) { //openSet.RemoveAt(i); i = -1; openSet.Clear(); openSet.Add(start); if (openSet.Count == 0) { Debug.Log("No path found"); return; } } else { Debug.Log("Next = " + openConnections[index].name); openSet.Add(openConnections[index]); navigatedSet.Add(openConnections[index]); } } if (!endIsFound) { Debug.Log("Impossible to create a path!"); } foreach (PathNode p in openSet) { p.SetColor(Color.cyan); } }
public static int GetWeight(PathFollowerTypes followerType, PathType pathType) { return(Instance.weights[followerType][pathType]); }
public static Dictionary <PathType, int> GetWeights(PathFollowerTypes followerType) { return(Instance.weights[followerType]); }
virtual public bool Walkable(PathFollowerTypes follower) { return(false); }
public bool Walkable(PathFollowerTypes follower) { return(NodeType.Walkable(follower)); }