/** * @param targetType see {@link ActionTarget} * @return goal node to restore path from */ private List <Vector3Int> search(Node initialNode, Vector3Int target, ActionTargetTypeEnum targetType) { var openSet = new BinaryHeap(); var closedSet = new HashSet <Vector3Int>(); var fetchedNodes = new Dictionary <Vector3Int, Vector3Int?>(); var finishCondition = new PathFinishCondition(target, targetType); openSet.push(initialNode); while (openSet.Count > 0) { if (!openSet.tryPop(out var currentNode)) { return(null); // get node from open set or return not found } if (finishCondition.check(currentNode.position)) { return(getPath(currentNode, fetchedNodes)); //check if path is complete } var vectors = getSuccessors(currentNode.position, closedSet); var pathLength = currentNode.pathLength + 1; vectors.ForEach(vector => { // iterate passable near positions openSet.tryGet(vector, out var oldNode); if (oldNode == null || oldNode.Value.pathLength > pathLength) // if successor node is newly found, or has shorter path { openSet.push(new Node(vector, currentNode.position, getH(target, vector), pathLength)); // replace old node } }); closedSet.Add(currentNode.position); // node processed fetchedNodes[currentNode.position] = currentNode.parent; } Debug.Log("No path found"); return(null); }
public PathFinishCondition(Vector3Int target, ActionTargetTypeEnum targetType) { if (targetType == EXACT || targetType == ANY) { acceptable.Add(target); } if (targetType == NEAR || targetType == ANY) // add near tiles { LocalMap map = GameModel.localMap; PositionUtil.allNeighbour .Select(delta => target + delta) .Where(pos => map.inMap(pos)) .Where(pos => map.passageMap.passage.get(pos) == PASSABLE.VALUE) .Apply(pos => acceptable.Add(pos)); PositionUtil.allNeighbour .Select(delta => target + delta) .Select(pos => pos.add(0, 0, -1)) .Where(pos => map.inMap(pos)) .Where(pos => map.blockType.get(pos) == RAMP.CODE) .Apply(pos => acceptable.Add(pos)); } if (targetType != NEAR && targetType != ANY) { return; } }
public ActionTarget(ActionTargetTypeEnum type) { this.type = type; }
public List <Vector3Int> makeShortestPath(Vector3Int start, Vector3Int target, ActionTargetTypeEnum targetType) { localMap = GameModel.localMap; Debug.Log("searching path from " + start + " to " + target); return(search(new Node(start, null, getH(target, start), 0), target, targetType)); }
public PositionActionTarget(Vector3Int targetPosition, ActionTargetTypeEnum placement) : base(placement) { this.targetPosition = targetPosition; }
public EntityActionTarget(EcsEntity entity, ActionTargetTypeEnum placement) : base(placement) { this.entity = entity; }