Пример #1
0
    public void PathFound(List <Node> nodePath, AgentPathfinder agent, Vector3 to)
    {
        if (nodePath == null)
        {
            PathfindResult result = new PathfindResult(agent, null);
            lock (results){ results.Enqueue(result); }
            return;
        }

        vector3Path.Clear();

        //invert path and convert to Vector3
        // i > 0 as we don't want to add last one, see below comment
        for (int i = nodePath.Count - 1; i > 0; i--)
        {
            vector3Path.Add(nodePath[i].pos);
        }

        //last one should be exact position, not node position.
        vector3Path.Add(to);

        PathfindResult successfulResult = new PathfindResult(agent, vector3Path);

        lock (results) { results.Enqueue(successfulResult); }
    }
    private TestResult RunPathfind(GridTile start, GridTile dest)
    {
        float      before, after;
        TestResult result = new TestResult();

        PathfindResult res = new PathfindResult();

        before   = Time.realtimeSinceStartup;
        res.Path = HierarchicalPathfinder.FindHierarchicalPath(graph, start, dest);
        after    = Time.realtimeSinceStartup;

        res.RunningTime = after - before;
        res.CalculatePathLength();
        result.HPAStarResult = res;

        res      = new PathfindResult();
        before   = Time.realtimeSinceStartup;
        res.Path = HierarchicalPathfinder.FindLowlevelPath(graph, start, dest);
        after    = Time.realtimeSinceStartup;

        res.RunningTime = after - before;
        res.CalculatePathLength();
        result.AStarResult = res;

        return(result);
    }
Пример #3
0
    public void MouseClicked(MouseEvent e)
    {
        var localCoordinates = this.GlobalToLocal(e.GlobalCoordinates);
        var gridX            = (int)((localCoordinates.x / this.tileSize) + 1) - 1;
        var gridY            = (int)((localCoordinates.y / this.tileSize) + 1) - 1;
        var gridVector       = new Vector2i(gridX, gridY);

        if (this.selectedActor != null)
        {
            if (this.pathfindResults != null && this.pathfindResults.VisitablePoints.Contains(gridVector))
            {
                this.UpdateLocation(this.selectedActor, gridVector);
                this.ClearSelection();
            }
        }
        else if (this.TryGetActor(gridVector, out this.selectedActor))
        {
            this.pathfindResults = Pathfinder.Pathfind(this, this.selectedActor);
            foreach (var item in this.pathfindResults.VisitablePoints)
            {
                this.highlights[item.X, item.Y].isVisible = true;
                this.highlightedIndicies.Add(item);
            }
        }
    }
Пример #4
0
 private void moveActor(Actor actor, Vector2i destination, PathfindResult pathfinding)
 {
     MoveActorAnimation move = new MoveActorAnimation(actor, destination, pathfinding);
     move.Delay = 10;
     move.AnimationStopped += new EventHandler<AnimationEventArgs>(anim_AnimationStopped);
     move.Start();
     AwayTeam.MissionController.AddChild(move);
     move.Play();
 }
Пример #5
0
    private void moveActor(Actor actor, Vector2i destination, PathfindResult pathfinding)
    {
        MoveActorAnimation move = new MoveActorAnimation(actor, destination, pathfinding);

        move.Delay             = 10;
        move.AnimationStopped += new EventHandler <AnimationEventArgs>(anim_AnimationStopped);
        move.Start();
        AwayTeam.MissionController.AddChild(move);
        move.Play();
    }
Пример #6
0
 public virtual string GetDoorCommand(Exit e)
 {
     if (!string.IsNullOrEmpty(e.DoorCommand))
     {
         return(e.DoorCommand);
     }
     if (e.HasFlag("door") && PathfindResult.IsDirectionCommand(e.Command) != 'x')
     {
         return("open " + e.Command);
     }
     return(string.Empty);
 }
Пример #7
0
    public override void Activate(Ability ability)
    {
        base.Activate(ability);
        this.MoveAbility = ability as BasicMoveAbility;
        if (this.MoveAbility == null)
        {
            throw new ArgumentException("Ability " + ability + " is not implemented by this controller.");
        }

        this.pathfinding = this.MoveAbility.GetDestinations(ParentController.Map, ParentController.SelectedActor);
        this.highlightDesinations();
        this.ParentController.TouchEnded += onTouchEnded;
    }
Пример #8
0
    public override void Activate(Ability ability)
    {
        base.Activate(ability);
        this.MoveAbility = ability as BasicMoveAbility;
        if (this.MoveAbility == null)
        {
            throw new ArgumentException("Ability " + ability + " is not implemented by this controller.");
        }

        this.pathfinding = this.MoveAbility.GetDestinations(ParentController.Map, ParentController.SelectedActor);
        this.highlightDesinations();
        this.ParentController.TouchEnded += onTouchEnded;
    }
Пример #9
0
    public static PathfindResult Pathfind(Map map, Actor actor)
    {
        var result        = new PathfindResult();
        var frontier      = new PriorityQueue <Vector2i>();
        var actorLocation = map.GetLocation(actor);

        if (actorLocation == null)
        {
            throw new InvalidOperationException("Actor " + actor + " does not exist on map " + map);
        }

        var start = actorLocation.Value;

        result.Distance.Add(new Vector2i(start.X, start.Y), 0);
        frontier.Insert(start, 0);

        Debug.Log("before loop");
        while (frontier.Count > 0)
        {
            var min = frontier.ExtractMin();

            Debug.Log("Greedy Step -- Location: " + min.Key + " Distance:" + min.Value);
            foreach (var adj in GetAdjacentCoordinates(map, min.Key))
            {
                if (map.GetActor(adj) == null)
                {
                    var tile = map[adj];
                    var cost = tile.Properties.MovementPenalty;
                    var cur  = result.Distance.ContainsKey(adj) ? result.Distance[adj] : int.MaxValue;
                    var alt  = min.Value + cost;

                    if (alt < cur && alt <= actor.Properties.MovementPoints)
                    {
                        AddOrUpdate(result.Distance, adj, alt);
                        AddOrUpdate(result.Previous, adj, min.Key);
                        if (frontier.ContainsKey(adj))
                        {
                            frontier.DecreaseKey(adj, alt);
                        }
                        else
                        {
                            frontier.Insert(adj, alt);
                        }
                    }
                }
            }
        }

        return(result);
    }
Пример #10
0
    public void HandleSingleTouchEnded(FTouch touch)
    {
        var gridVector = this.map.GlobalToGrid(touch.position);

        Debug.Log("Touch at: " + touch.position + ", map grid: " + gridVector);

        if (this.selectedActor != null)
        {
            var actorState = this.selectedActor.TurnState;
            if (actorState == ActorState.SelectingDestination)
            {
                if (this.pathfindResults != null && this.pathfindResults.VisitablePoints.Contains(gridVector))
                {
                    this.selectedActor.HasMovedThisTurn = true;
                    this.selectedActor.TurnState        = ActorState.AwaitingCommand;
                    this.map.UpdateLocation(this.selectedActor, gridVector);

                    // Recompute the set of attackable points, since the actor has moved.
                    var tempEnum = MoveAndAttackHelper.GetAttackablePoints(this.map, gridVector, 1, 6);
                    this.localAttackablePoints = new HashSet <Vector2i>(tempEnum);
                    this.showButtonStrip(this.selectedActor);
                    this.clearHighlights();
                }
            }
            else if (actorState == ActorState.SelectingEnemy)
            {
                if (this.localAttackablePoints.Contains(gridVector))
                {
                    Debug.Log("Attacking point: " + gridVector);
                    Vector2 source = new Vector2(this.selectedActor.x, this.selectedActor.y);
                    Vector2 dest   = this.map.GridToGlobal(gridVector);
                    this.ShootLaser(source, dest);
                }
            }
        }
        else if (this.map.TryGetActor(gridVector, out this.selectedActor))
        {
            var actorState = this.selectedActor.TurnState;
            if (actorState == ActorState.TurnStart)
            {
                this.selectedActor.TurnState = ActorState.AwaitingCommand;
                this.pathfindResults         = MoveAndAttackHelper.Pathfind(this.map, this.selectedActor);
                var tempEnum = MoveAndAttackHelper.GetAttackablePoints(this.map, gridVector, 1, 6);
                this.localAttackablePoints = new HashSet <Vector2i>(tempEnum);
                this.showButtonStrip(this.selectedActor);
            }
        }
    }
Пример #11
0
    // Update is called once per frame
    void Update()
    {
        //update objects when a thread has finished processing their path.
        lock (results)
        {
            while (results.Count > 0)
            {
                PathfindResult res = results.Dequeue();
                res.agent.NotifyPathAvailable(res.path);
            }
        }

#if UNITY_EDITOR
        drawOuterGrid();
#endif
    }
Пример #12
0
    public static PathfindResult Djikstra(
        Func<Vector2i, int> cost,
        Func<Vector2i, IEnumerable<Vector2i>> adjacent,
        Vector2i start,
        int range)
    {
        var result = new PathfindResult(start);
        var frontier = new PriorityQueue<Vector2i>();

        result.Distance.Add(start, 0);
        frontier.Insert(start, 0);

        while (frontier.Count > 0)
        {
            var min = frontier.ExtractMin();

            foreach (var adj in adjacent(min.Key))
            {
                var d = cost(adj);
                var cur = result.Distance.ContainsKey(adj) ? result.Distance[adj] : int.MaxValue;
                var alt = min.Value + d;

                // Wrap around!?
                if (alt < 0 && d > 0)
                {
                    alt = int.MaxValue;
                }

                if (alt < cur && alt <= range)
                {
                    AddOrUpdate(result.Distance, adj, alt);
                    AddOrUpdate(result.Previous, adj, min.Key);
                    if (frontier.ContainsKey(adj))
                    {
                        frontier.DecreaseKey(adj, alt);
                    }
                    else
                    {
                        frontier.Insert(adj, alt);
                    }
                }
            }
        }

        return result;
    }
Пример #13
0
    public MoveActorAnimation(Actor mover, Vector2i destination, PathfindResult pathMetrics)
    {
        this.actor = mover;
        this.dest = destination;
        this.done = false;
        this.path = new List<Vector2>();

        Vector2i p = destination;
        Vector2i origin = actor.GridPosition;
        while (p != origin)
        {
            var globalx = p.X * AwayTeam.TileSize + AwayTeam.TileSize / 2;
            var globaly = p.Y * AwayTeam.TileSize + AwayTeam.TileSize / 2;
            this.path.Insert(0, new Vector2(globalx, globaly));
            p = pathMetrics.Previous[p];
        }
        this.index = 0;
    }
Пример #14
0
    public MoveActorAnimation(Actor mover, Vector2i destination, PathfindResult pathMetrics)
    {
        this.actor = mover;
        this.dest  = destination;
        this.done  = false;
        this.path  = new List <Vector2>();

        Vector2i p      = destination;
        Vector2i origin = actor.GridPosition;

        while (p != origin)
        {
            var globalx = p.X * AwayTeam.TileSize + AwayTeam.TileSize / 2;
            var globaly = p.Y * AwayTeam.TileSize + AwayTeam.TileSize / 2;
            this.path.Insert(0, new Vector2(globalx, globaly));
            p = pathMetrics.Previous[p];
        }
        this.index = 0;
    }
Пример #15
0
    public void HandleSingleTouchEnded(FTouch touch)
    {
        var gridVector = this.map.GlobalToGrid(touch.position);
        Debug.Log("Touch at: " + touch.position + ", map grid: " + gridVector);

        if (this.selectedActor != null)
        {
            var actorState = this.selectedActor.TurnState;
            if (actorState == ActorState.SelectingDestination)
            {
                if (this.pathfindResults != null && this.pathfindResults.VisitablePoints.Contains(gridVector))
                {
                    this.selectedActor.HasMovedThisTurn = true;
                    this.selectedActor.TurnState = ActorState.AwaitingCommand;
                    this.map.UpdateLocation(this.selectedActor, gridVector);

                    // Recompute the set of attackable points, since the actor has moved.
                    var tempEnum = MoveAndAttackHelper.GetAttackablePoints(this.map, gridVector, 1, 6);
                    this.localAttackablePoints = new HashSet<Vector2i>(tempEnum);
                    this.showButtonStrip(this.selectedActor);
                    this.clearHighlights();
                }
            }
            else if(actorState == ActorState.SelectingEnemy)
            {
                if (this.localAttackablePoints.Contains(gridVector))
                {
                    Debug.Log("Attacking point: " + gridVector);
                    Vector2 source = new Vector2(this.selectedActor.x, this.selectedActor.y);
                    Vector2 dest = this.map.GridToGlobal(gridVector);
                    this.ShootLaser(source, dest);
                }
            }
        }
        else if (this.map.TryGetActor(gridVector, out this.selectedActor))
        {
            var actorState = this.selectedActor.TurnState;
            if (actorState == ActorState.TurnStart)
            {
                this.selectedActor.TurnState = ActorState.AwaitingCommand;
                this.pathfindResults = MoveAndAttackHelper.Pathfind(this.map, this.selectedActor);
                var tempEnum = MoveAndAttackHelper.GetAttackablePoints(this.map, gridVector, 1, 6);
                this.localAttackablePoints = new HashSet<Vector2i>(tempEnum);
                this.showButtonStrip(this.selectedActor);
            }
        }
    }
Пример #16
0
    public static PathfindResult Pathfind(Map map, Actor actor)
    {
        Vector2i start;

        if (!map.TryGetLocation(actor, out start))
        {
            throw new InvalidOperationException("Actor " + actor + " does not exist on map " + map);
        }

        var result   = new PathfindResult(start);
        var frontier = new PriorityQueue <Vector2i>();

        //var potentialAttackablePoints = new HashSet<Vector2i>();

        result.Distance.Add(new Vector2i(start.X, start.Y), 0);
        frontier.Insert(start, 0);

        while (frontier.Count > 0)
        {
            var min = frontier.ExtractMin();

            //foreach (var point in GetAttackablePoints(map, min.Key, 1, 2))
            //{
            //    potentialAttackablePoints.Add(point);
            //}

            foreach (var adj in GetAdjacentCoordinates(map, min.Key))
            {
                Actor tempActor;
                if (!map.TryGetActor(adj, out tempActor))
                {
                    var tile = map[adj];
                    var cost = tile.Properties.MovementPenalty;
                    var cur  = result.Distance.ContainsKey(adj) ? result.Distance[adj] : int.MaxValue;
                    var alt  = min.Value + cost;

                    if (alt < cur && alt <= actor.Properties.MovementPoints)
                    {
                        AddOrUpdate(result.Distance, adj, alt);
                        AddOrUpdate(result.Previous, adj, min.Key);
                        if (frontier.ContainsKey(adj))
                        {
                            frontier.DecreaseKey(adj, alt);
                        }
                        else
                        {
                            frontier.Insert(adj, alt);
                        }
                    }
                }
            }
        }

        //var attackablePoints = potentialAttackablePoints.Except(result.VisitablePoints);
        //foreach (var point in attackablePoints)
        //{
        //    result.AttackablePoints.Add(point);
        //}

        return(result);
    }
Пример #17
0
    public void MouseClicked(MouseEvent e)
    {
        var localCoordinates = this.GlobalToLocal(e.GlobalCoordinates);
        var gridX = (int)((localCoordinates.x / this.tileSize) + 1) - 1;
        var gridY = (int)((localCoordinates.y / this.tileSize) + 1) - 1;
        var gridVector = new Vector2i(gridX, gridY);

        if (this.selectedActor != null)
        {
            if (this.pathfindResults != null && this.pathfindResults.VisitablePoints.Contains(gridVector))
            {
                this.UpdateLocation(this.selectedActor, gridVector);
                this.ClearSelection();
            }
        }
        else if (this.TryGetActor(gridVector, out this.selectedActor))
        {
            this.pathfindResults = Pathfinder.Pathfind(this, this.selectedActor);
            foreach (var item in this.pathfindResults.VisitablePoints)
            {
                this.highlights[item.X, item.Y].isVisible = true;
                this.highlightedIndicies.Add(item);
            }
        }
    }