public override bool Process(AStarContext context) { PathFindingRequest request = context.Request as PathFindingRequest; ProcessingNode startNode = context.GetStartNode(); ProcessingNode endNode = context.GetTargetNode(); if (startNode == null || endNode == null) { return(false); } FindPath(startNode, endNode, context); List <Int3> path = context.rawPathPoints; if (path.Count > 0) { // set the first and last point to 'from' and 'to'. path[0] = request.fromPosition; path[path.Count - 1] = request.toPosition; // then optimize NavMeshPathOptimizer.Optimize(ref context.rawPathNodeCache, ref path); } return(context.rawPathPoints.Count >= 2); }
public bool FindPath(Int3 from, Int3 to, ref List <Int3> result, TwGame.Team team) { PathFindingRequest req = new PathFindingRequest(from, to, navgationGraph, pathPlanner, (int)team); //astarEngine.AddRequest(req); bool ret = astarEngine.ProcessRequest(req); if (ret) { result.Clear(); result.AddRange(astarEngine.Context.rawPathPoints); } return(ret); }
/// <summary> /// calculate tactical G /// </summary> private int TacticalCost(NavMeshNode prevNode, NavMeshNode currentNode, AStarContext context) { int distCost = prevNode.GetConnectionCost(currentNode.id); int tacCost = 0; PathFindingRequest request = context.Request as PathFindingRequest; // doer's team TwGame.Team team = (TwGame.Team)request.extend1; switch (team) { case TwGame.Team.Neutral: tacCost = 0; break; case TwGame.Team.Team_1: case TwGame.Team.Team_2: { int MaxInfluence = TwGame.ComInfluenceMap.MaxTeamStrengthValue; int cur = TwGame.AIUtil.GetTeamStrength(currentNode.position, team); int pre = TwGame.AIUtil.GetTeamStrength(prevNode.position, team); // avarage influence between current node's position and previous node's position. int infl = (cur + pre) >> 1; if (infl > 0) { tacCost = System.Math.Max(-distCost + 1, -distCost * infl / MaxInfluence); } else if (infl < 0) { tacCost = -distCost * infl / MaxInfluence * 2; } } break; } return(distCost + tacCost); }
public void QueueJob(PathFindingRequest request) { requests.Enqueue(request); }
private void Update() { framesProcessed++; if (currentRequest != null) { //jobHandle.Complete(); if (jobHandle.IsCompleted || framesProcessed > 3) { jobHandle.Complete(); //make path Path path = new Path(); if (job.result.Length == 0 || Vector3.Distance(currentRequest.dst, job.grid.GetNodePosition(job.result[0])) > 3) { path.failed = true; } else { path.nodes = new List <Vector3>(job.result.Length); for (int i = job.result.Length - 1; i >= 0; i--) { path.nodes.Add(job.grid.GetNodePosition(job.result[i].x, job.result[i].y)); } } currentRequest.result = path; currentRequest.done = true; //Dispos job structs job.grid.Dispose(); job.result.Dispose(); job.open.Dispose(); job.closed.Dispose(); currentRequest = null; } } //Queue a new job if there are requests if (currentRequest == null && requests.Count > 0 && this.grid.nodeSize > 0) { currentRequest = requests.Dequeue(); job = new ProcessPathJob() { srcPosition = currentRequest.src, dstPosition = currentRequest.dst, grid = grid.Copy(Allocator.TempJob), result = new NativeList <int2>(Allocator.TempJob), open = new NativeBinaryHeap <ProcessPathJob.NodeCost>((int)(grid.width * grid.height / (grid.nodeSize) / 2), Allocator.TempJob), closed = new NativeHashMap <int2, ProcessPathJob.NodeCost>(128, Allocator.TempJob) }; jobHandle = job.Schedule(); framesProcessed = 0; } }