public override void Think(World world) { if (world.Player?.IsAlive ?? false) { var path = PathFinder.AStar(world.Level, Position, world.Player.Position); if (path.Any()) { var next = path.First(); var act = world.Level.GetActorAt(next); if (act == null) { Action = new MoveAction(this, (next - Position).Direction()); } else if (act is Player) { Action = new AttackAction(this, act); } } } if (Action == null) { Action = new WaitAction(); } }
public void SetSelectedPlayerCharacterPath(Vector3 mousePosition) { var clickPos = Camera.main.ScreenToWorldPoint(mousePosition); var x = Mathf.RoundToInt(clickPos.x); var y = Mathf.RoundToInt(clickPos.y); var targetPos = new Vector3(x, y); var target = FindObjectOfType <MapComponent>().GetComponentAt <FloorComponent>(targetPos); if (target == null || target.CanEnter == false) { return; } if (SelectedPlayerCharacter.GameObject.transform.position == SelectedPlayerCharacter.NextVertice.FloorComponent.gameObject.transform.position) { var startVertice = FindObjectOfType <MapComponent>().GetComponentAt <FloorComponent>(SelectedPlayerCharacter.GameObject.transform.position).Vertice; var endVertice = FindObjectOfType <MapComponent>().GetComponentAt <FloorComponent>(targetPos).Vertice; SelectedPlayerCharacter.Path = PathFinder.AStar(startVertice, endVertice); } else { var startVertice = FindObjectOfType <MapComponent>().GetComponentAt <FloorComponent>(SelectedPlayerCharacter.NextVertice.Position).Vertice; var endVertice = FindObjectOfType <MapComponent>().GetComponentAt <FloorComponent>(targetPos).Vertice; SelectedPlayerCharacter.Path = PathFinder.AStar(startVertice, endVertice); } }
private void SetRoamState(Floor parent) { var myPosition = parent.Entities.Reverse[this]; CurrentState = State.Roaming; var visible = VisiblePoints.ToArray(); if (visible.Length != 0) { var roamPoint = visible[RandomHelper.Random.Next(0, visible.Length)]; var path = PathFinder.AStar(parent, myPosition, roamPoint); if (path != null) { Path = path; return; } } RandomMoveDirection.Shuffle(); foreach (var direction in RandomMoveDirection) { if (parent.IsPointPassable(NewPosition(direction, myPosition))) { MoveTo(parent, direction); return; } } SetWaitState(); }
public override bool this[int x, int y] { get { Stack <Vector2Int> path; return(base[x, y] && PathFinder.AStar(Center, new Vector2Int(x, y), out path, MaxVerticalDistance, MaxRadius)); } }
void UpdatePath() { startVec = new Vector2Int((int)start.position.x, (int)start.position.z); targetVec = new Vector2Int((int)target.position.x, (int)target.position.z); Stopwatch sw = new Stopwatch(); sw.Start(); path = PathFinder.AStar(startVec, targetVec); sw.Stop(); UnityEngine.Debug.Log("Path length: " + path.Count + ", in " + sw.ElapsedMilliseconds + " ms."); }
void FindPath() { if (destination != null) { path = PathFinder.AStar(this.gameObject, destination.gameObject); //if (path==null) { UnityEngine.Debug.Log("Path could not be found from :" + transform.position + " to:" + destination.position); } } else { UnityEngine.Debug.Log("Please drag a gameobject into destination."); } }
public override IEnumerator <Vector2Int> GetEnumerator() { IEnumerator <Vector2Int> e = base.GetEnumerator(); while (e.MoveNext()) { Stack <Vector2Int> path; if (PathFinder.AStar(Center, e.Current, out path, MaxVerticalDistance, MaxRadius)) { yield return(e.Current); } } yield break; }
private void Move(char[,] map, List <Unit> units, char[] blockItems) { var openPositions = GetAllOpenPositions(Type == UnitType.Elf ? UnitType.Goblin : UnitType.Elf, units, map, blockItems); var nodes = openPositions.Select(x => PathFinder.AStar(map, this.Point.X, this.Point.Y, x.X, x.Y, blockItems)); var selectedNode = nodes.Where(x => x != null).OrderBy(x => PathFinder.GetStepsNum(x)).ThenBy(x => x.y).ThenBy(x => x.x).FirstOrDefault(); if (selectedNode != null) { var step = PathFinder.GetFirstStep(selectedNode); //replace 'G' or 'E' after the move on the map with '.'; move the unit to the new position on the map map[this.Point.X, this.Point.Y] = freeItem; this.Point.X = step.x; this.Point.Y = step.y; map[this.Point.X, this.Point.Y] = ownSign; } }
// Calculates path to target & repurposes navigator if successful, in // which case we return true. If we can't do it, we deactivate the // navigator and return false: public bool CalculatePathToTarget(GameObject targ) { activated = false; ArrayList path = PathFinder.AStar(this.gameObject, targ); if (path.Count == 0) { // Happens when path can't be found. This is currently occuring as the objects never // die. We also need to put the "Unit" objects in a layer to not be considered for // waypoint calculation: Debug.Log("A* path was null."); } currentPath = path; currentTarget = targ; lastCalcedTarget = currentTarget; //searchTime=Time.time; activated = true; return(activated); }
public override void Execute(BattleManager manager, BattleQueueTime time) { // Find a path using A* Stack <Vector2Int> steps; PathFinder.AStar(m_Agent.Coordinates, destination, out steps, m_Agent["Jump"]); // Push each step in path BattleGrid grid = manager.grid; while (steps.Count > 1) { Vector2Int current = steps.Pop(); if (grid[current].Height != grid[steps.Peek()].Height) { manager.Add(new BattleJump(current, steps.Peek() - current, time - steps.Count - 2)); } else { manager.Add(new BattleWalk(current, steps.Peek() - current, time - steps.Count - 2)); } } }
public override double[] value(int[] state, List <actionType> actions) { if (currentGoal.goalState == null) { currentGoal = selectGoal(state, models.Length - 1, actions); subgoals[currentGoal.level].Clear(); subgoals[currentGoal.level].Add(currentGoal); } for (int l = currentGoal.level - 1; l >= 0; l--) { if (subgoals[l].Count == 0) { // plan the route to the goal List <int[]> goalStates = stateTree.GetChildren(subgoals[l + 1][0].goalState, l + 1); int[] startState = stateTree.GetParentState(state, l); List <Tuple <int[], actionType, double> > path = pathFinder.AStar(startState, goalStates, models[l], actions, false);// l!=0); subgoals[l] = path2subgoals(path, l, models[l]); if (subgoals[l].Count == 0) // if no path is known, pass control to lowest level { goalStates = stateTree.GetChildren(subgoals[l + 1][0].goalState, l + 1); int[] currentGoalLevelState = stateTree.GetParentState(state, currentGoal.level); Console.WriteLine("couldn't find a path to level " + currentGoal.level + ": " + String.Join(",", currentGoal.goalState) + " at level " + l); throw new ApplicationException("Pathfinding failed"); currentGoal.goalState = null; for (int i = 0; i < subgoals.Length; i++) { subgoals[i].Clear(); } currentGoal = selectGoal(state, 0, actions); subgoals[0].Clear(); subgoals[0].Add(currentGoal); break; } // no local path should be more than 2 steps if (subgoals[l].Count > 3) { int[] thisOldState = subgoals[l + 1][0].startState; actionType thisAction = subgoals[l + 1][0].action; int[] thisNewState = subgoals[l + 1][0].goalState; StateTransition <int[], actionType> t = new StateTransition <int[], actionType>(thisOldState, thisAction, -10, thisNewState); // size of negative reward? models[l + 1].update(t); } } //System.IO.StreamWriter w = new System.IO.StreamWriter("log.txt", false); ////w.WriteLine("Goal: Level " + currentGoal.level + ", at " + String.Join(",", currentGoal.goalState)); //for (int k = 0; k <= currentGoal.level; k++) //{ // foreach (Goal<int[], actionType> step in subgoals[k]) // { // if (step.goalState != null) // { // foreach (int[] s in stateTree.GetLevel0Children(step.goalState, k)) // { // w.WriteLine(string.Join(",", s) + "," + (k == currentGoal.level ? "g" : "p")); // } // //w.WriteLine("Goal: Level " + Math.Max(0, l) + ", at " + String.Join(",", step.goalState)); // } // } //} //w.Flush(); w.Close(); } if (minLevel > 0) // for simulating dH lesion { double[] vals = new double[availableActions.Count]; if (currentGoal.level >= minLevel) { vals[availableActions.IndexOf(currentGoal.action)] = 1; } else { return(models[0].value(state, availableActions)); } return(vals); } double[] response = new double[actions.Count()]; for (int i = 0; i < availableActions.Count; i++) { if (actionComparer.Equals(subgoals[0][0].action, availableActions.ElementAt(i))) { response[i] = 1; } } return(response); }