public PathFinding(GameObject _Worker, aStarPoint _Location, aStarPoint _Destination) { Worker = _Worker; Location = _Location; Destination = _Destination; endedTest = false; }
public List <aStarPoint> findValidNeighbours(aStarPoint Node) { List <aStarPoint> neighbours = new List <aStarPoint> (); for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { if (x == 0 && y == 0) { continue; } if (mc.mapStarMap [Node.y + y, Node.x - x].isTraversable) { if (x != 0 && y != 0) { if (!mc.mapStarMap [Node.y, Node.x - x].isTraversable || !mc.mapStarMap [Node.y + y, Node.x].isTraversable) { } else { neighbours.Add(mc.mapStarMap [Node.y + y, Node.x - x]); } } else { neighbours.Add(mc.mapStarMap [Node.y + y, Node.x - x]); } } } } return(neighbours); }
protected override void OnFinished() { Pathfound = SimplifyPath(Pathfound); Worker.GetComponent <Worker_AI2> ().destinationPath = Pathfound; Worker = null; Location = null; Destination = null; endedTest = true; }
public List <Vector3> PathFind(aStarPoint NodeStart, aStarPoint NodeFinish) { Heap <aStarPoint> openList = new Heap <aStarPoint>(mc.mapStarMap.GetLength(0) * mc.mapStarMap.GetLength(1)); List <aStarPoint> closedList = new List <aStarPoint> (); openList.Add(NodeStart); int t = 0; while (openList.Count > 0) { t++; aStarPoint currentNode = openList.RemoveFirst(); closedList.Add(currentNode); if (currentNode == NodeFinish) { List <Vector3> Path = new List <Vector3> (); aStarPoint Node = NodeFinish; while (Node != NodeStart) { Path.Add(Node.Location); Node = Node.parent; } foreach (aStarPoint a in mc.mapStarMap) { a.reset(); } Path.Reverse(); return(Path); } List <aStarPoint> neighbours = findValidNeighbours(currentNode); foreach (aStarPoint node in neighbours) { if (closedList.Contains(node)) { continue; } int g = Mathf.RoundToInt(Vector2.Distance(node.gridPosition(), currentNode.gridPosition()) * 10); int h = GetDistance(node, NodeFinish); int f = g + h; if (f < node.f || !openList.Contains(node)) { node.f = f; node.h = h; node.g = g; node.parent = currentNode; if (!openList.Contains(node)) { openList.Add(node); openList.UpdateItem(node); } } } } return(null); }
public void assignJob(Map_Cell cell, string job, int cellNumber) { jobTarget = cell; State = job; astardestination = jobTarget.assignPosition(gameObject); destinationPoint = getClosetPoint(astardestination); if (job.Equals("ClaimGround")) { jobTarget.Claimer = true; } }
public int GetDistance(aStarPoint nodeA, aStarPoint nodeB) { int x = Mathf.Abs(nodeA.x - nodeB.x); int y = Mathf.Abs(nodeA.y - nodeB.y); if (x < y) { return(14 * y + 10 * (x - y)); } return(14 * x + 10 * (y - x)); }
aStarPoint getClosetPoint(Vector3 currentPos) { aStarPoint bestTarget = new aStarPoint(); float closestDist = Mathf.Infinity; foreach (aStarPoint v in mapStarMap) { float dist = Vector2.Distance(new Vector2(v.Location.x, v.Location.z), (new Vector2(currentPos.x, currentPos.z))); if (dist < closestDist) { closestDist = dist; bestTarget = v; } } return(bestTarget); }
aStarPoint getClosetPoint(Vector3 currentPos) { aStarPoint bestTarget = new aStarPoint(); float closestDist = -1f; //List<aStarPoint> liststar = new List<aStarPoint> (); foreach (aStarPoint v in mc.mapStarMap) { //* float dist = Vector2.Distance(new Vector2(v.Location.x, v.Location.z), (new Vector2(currentPos.x, currentPos.z))); if (dist < closestDist || closestDist == -1f) { closestDist = dist; bestTarget = v; } //*/ //liststar.Add (v); } //bestTarget = liststar [0]; //liststar.Clear (); return(bestTarget); }
void Update() { /* Debug Code */ if (Input.GetKeyDown("l")) { toggle = !toggle; } if (toggle) { speed = 1; mineSpeed = 1; } else { speed = 100; mineSpeed = 100; } /* End Of Debug Code */ if (State.Equals("Mine")) { //UnityEngine.Debug.Log (destinationPath); if (jobTarget.Selected == false) { State = "Idle"; jobTarget = null; astardestination = transform.position; } else { if (destinationPath == null && transform.position != new Vector3(astardestination.x, transform.position.y, astardestination.z)) { HiveMind.RequestPath(new PathRequest(this.gameObject, getClosetPoint(transform.position), getClosetPoint(astardestination))); } //destinationPath = PathFind (getClosetPoint (transform.position), getClosetPoint (astardestination)); //astardestination = jobTarget.GetComponent<Map_CellControl>().assignPosition(gameObject); if (Vector3.Distance(transform.position, new Vector3(destinationPoint.Location.x, transform.position.y, destinationPoint.Location.z)) <= 8) { MineCell(jobTarget); } } if (transform.position != astardestination) { if (destinationPath != null) { try{ if (transform.position.Equals(new Vector3(destinationPath [pathPosition].x, transform.position.y, destinationPath [pathPosition].z))) { if (pathPosition < destinationPath.Count - 1) { pathPosition++; } } } catch (System.ArgumentOutOfRangeException ex) { Debug.Log(ex); } if (pathPosition >= destinationPath.Count) { pathPosition = destinationPath.Count - 1; } //Debug.Log (pathPosition + " : " + (destinationPath.Count-1)); transform.position = Vector3.MoveTowards(transform.position, new Vector3(destinationPath [pathPosition].x, transform.position.y, destinationPath [pathPosition].z), speed); } // transform.position = Vector3.MoveTowards (transform.position, new Vector3 (astardestination.x, transform.position.y, astardestination.z), 1f); } else { destinationPath = null; pathPosition = 0; } } else if (State.Equals("ClaimGround")) { if (jobTarget.Cell == 2) { State = "Idle"; jobTarget = null; astardestination = transform.position; } else { astardestination = new Vector3(jobTarget.position.x, transform.position.y, jobTarget.position.z); if (destinationPath == null && transform.position != new Vector3(astardestination.x, transform.position.y, astardestination.z)) { HiveMind.RequestPath(new PathRequest(this.gameObject, getClosetPoint(transform.position), getClosetPoint(astardestination))); } if (Vector3.Distance(transform.position, new Vector3(destinationPoint.Location.x, transform.position.y, destinationPoint.Location.z)) <= 8) { ClaimCell(jobTarget); } } if (transform.position != astardestination) { if (destinationPath != null) { if (transform.position.Equals(new Vector3(destinationPath [pathPosition].x, transform.position.y, destinationPath [pathPosition].z))) { if (pathPosition < destinationPath.Count - 1) { pathPosition++; } } if (pathPosition >= destinationPath.Count) { pathPosition = destinationPath.Count - 1; } transform.position = Vector3.MoveTowards(transform.position, new Vector3(destinationPath [pathPosition].x, transform.position.y, destinationPath [pathPosition].z), speed); } // transform.position = Vector3.MoveTowards (transform.position, new Vector3 (astardestination.x, transform.position.y, astardestination.z), 1f); } else { destinationPath = null; pathPosition = 0; } } else if (State.Equals("Idle")) { destinationPath = null; destinationPoint = null; astardestination = Vector3.zero; } }
public PathRequest(GameObject _Worker, aStarPoint _Position, aStarPoint _Destination) { Worker = _Worker; Position = _Position; Destination = _Destination; }
protected override void ThreadFunction() { Heap <aStarPoint> openList = new Heap <aStarPoint>(mc.mapStarMap.GetLength(0) * mc.mapStarMap.GetLength(1)); List <aStarPoint> closedList = new List <aStarPoint> (); openList.Add(Location); int t = 0; while (openList.Count > 0) { t++; aStarPoint currentNode = openList.RemoveFirst(); closedList.Add(currentNode); /* * if (currentNode == Destination) { * List <Vector3> Path = new List<Vector3> (); * aStarPoint Node = Destination; * while(Node != Location){ * Path.Add (Node.Location); * Node = Node.parent; * } * foreach(aStarPoint a in mc.mapStarMap){ * a.reset (); * } * Path = SimplifyPath (Path); * Path.Reverse(); * if(!Path.Contains(Destination.Location)){ * Path.Add (Destination.Location); * } * * //sw.Stop (); * //UnityEngine.Debug.Log (sw.ElapsedMilliseconds+"ms"); * //sw.Reset (); * Pathfound = Path; * } */ if (currentNode == Destination) { List <Vector3> Path = new List <Vector3> (); aStarPoint Node = Destination; while (Node != Location) { Path.Add(Node.Location); Node = Node.parent; } foreach (aStarPoint a in mc.mapStarMap) { a.reset(); } Path = CleanRoute(Path); Path.Reverse(); if (!Path.Contains(Destination.Location)) { Path.Add(Destination.Location); } Path.Insert(0, Location.Location); //SimplifyPath (Path); //sw.Stop (); //UnityEngine.Debug.Log (sw.ElapsedMilliseconds+"ms"); //sw.Reset (); Pathfound = Path; } List <aStarPoint> neighbours = findValidNeighbours(currentNode); foreach (aStarPoint node in neighbours) { if (closedList.Contains(node)) { continue; } int g = Mathf.RoundToInt(Vector2.Distance(node.gridPosition(), currentNode.gridPosition()) * 10); int h = GetDistance(node, Destination); int f = g + h; if (f < node.f || !openList.Contains(node)) { node.f = f; node.h = h; node.g = g; node.parent = currentNode; if (!openList.Contains(node)) { //CalculatePosition (node,openList); openList.Add(node); openList.UpdateItem(node); } } } } }