Ejemplo n.º 1
0
    public static Location FindNextLocation(Location start, Location dest, IGameState state, List<Location> avoid, LayeredInfluenceMap heat)
    {
        if (start.Equals(dest) || !state.GetIsPassable(dest) || avoid.Contains(dest))
            return null;

        List<Location> list = FindPath(start, dest, state, avoid, heat);
        if (list != null)
            return list[0];
        else
            return null;
    }
Ejemplo n.º 2
0
    // Returns a list of tiles that form the shortest path between start and dest
    public static List<Location> FindPath(Location start, Location dest, IGameState state, List<Location> avoid, LayeredInfluenceMap heat = null, int maxDepth = int.MaxValue)
    {
        if (start.Equals(dest) || !state.GetIsPassable(dest) || avoid.Contains(dest))
            return null;

        HashSet<string> closed = new HashSet<string>();
        Dictionary<string, PathfindNode> locToNode = new Dictionary<string, PathfindNode>();
        List<PathfindNode> open = new List<PathfindNode>();

        List<Location> reachable;

        PathfindNode first = new PathfindNode(start, null, dest, state, heat[start]);
        open.Add(first);
        locToNode.Add(MyBot.LocationToKey(first.Position), first);

        // Repeat until the destination node is reached
        PathfindNode last = null;
        while (open.Count > 0)
        {
            if (state.TimeRemaining < 10)
            {
                MyBot.LogShit("timeout.txt", "stop B - " + state.TimeRemaining);
                return null;
            }

            // Search the best available tile (lowest cost to reach from start, closest to dest)

            PathfindNode best = null;
            foreach (PathfindNode next in open)
            {
                if (best == null)
                    best = next;

                if (next.F < best.F)
                    best = next;
            }

            if (best.G > maxDepth)
                return null;

            //PathfindNode best = open.Min;

            // Move to closed list
            open.Remove(best);
            locToNode.Remove(MyBot.LocationToKey(best.Position));
            closed.Add(MyBot.LocationToKey(best.Position));

            if (best.Position.Equals(dest)) // Destination added to closed list - almost done!
            {
                last = best;
                break;
            }

            // Find tiles adjacent to this tile
            reachable = GetNeighbours(best.Position, state);
            string lid;
            PathfindNode pfn;
            foreach (Location next in reachable)
            {
                if (!state.GetIsPassable(next) || avoid.Contains(next)) // Check if tile is blocked
                    continue;

                lid = MyBot.LocationToKey(next);

                if (closed.Contains(lid))
                    continue;

                if(locToNode.ContainsKey(lid))
                {
                    pfn = locToNode[lid];
                    if (best.G + 1 < pfn.G)
                        pfn.Parent = best;
                }
                else{
                    pfn = new PathfindNode(next, best, dest, state, heat[next]);
                    open.Add(pfn);
                    locToNode.Add(lid, pfn);
                }
            }
        }

        if (last == null)
            return null;

        // Trace the route from destination to start (using each node's parent property)
        List<PathfindNode> route = new List<PathfindNode>();
        while (last != first && last != null)
        {
            route.Add(last);
            last = last.Parent;
        }

        // Reverse route and convert to Points
        List<Location> path = new List<Location>();
        for (int i = route.Count - 1; i >= 0; i--)
        {
            path.Add(route[i].Position);
        }

        // Return the list of Points
        return path;
    }
Ejemplo n.º 3
0
    // Returns a list of tiles that form the shortest path between start and dest
    public static List <Location> FindPath(Location start, Location dest, IGameState state, List <Location> avoid, LayeredInfluenceMap heat = null, int maxDepth = int.MaxValue)
    {
        if (start.Equals(dest) || !state.GetIsPassable(dest) || avoid.Contains(dest))
        {
            return(null);
        }

        HashSet <string> closed = new HashSet <string>();
        Dictionary <string, PathfindNode> locToNode = new Dictionary <string, PathfindNode>();
        List <PathfindNode> open = new List <PathfindNode>();

        List <Location> reachable;

        PathfindNode first = new PathfindNode(start, null, dest, state, heat[start]);

        open.Add(first);
        locToNode.Add(MyBot.LocationToKey(first.Position), first);

        // Repeat until the destination node is reached
        PathfindNode last = null;

        while (open.Count > 0)
        {
            if (state.TimeRemaining < 10)
            {
                MyBot.LogShit("timeout.txt", "stop B - " + state.TimeRemaining);
                return(null);
            }

            // Search the best available tile (lowest cost to reach from start, closest to dest)

            PathfindNode best = null;
            foreach (PathfindNode next in open)
            {
                if (best == null)
                {
                    best = next;
                }

                if (next.F < best.F)
                {
                    best = next;
                }
            }

            if (best.G > maxDepth)
            {
                return(null);
            }

            //PathfindNode best = open.Min;

            // Move to closed list
            open.Remove(best);
            locToNode.Remove(MyBot.LocationToKey(best.Position));
            closed.Add(MyBot.LocationToKey(best.Position));

            if (best.Position.Equals(dest)) // Destination added to closed list - almost done!
            {
                last = best;
                break;
            }

            // Find tiles adjacent to this tile
            reachable = GetNeighbours(best.Position, state);
            string       lid;
            PathfindNode pfn;
            foreach (Location next in reachable)
            {
                if (!state.GetIsPassable(next) || avoid.Contains(next)) // Check if tile is blocked
                {
                    continue;
                }

                lid = MyBot.LocationToKey(next);

                if (closed.Contains(lid))
                {
                    continue;
                }

                if (locToNode.ContainsKey(lid))
                {
                    pfn = locToNode[lid];
                    if (best.G + 1 < pfn.G)
                    {
                        pfn.Parent = best;
                    }
                }
                else
                {
                    pfn = new PathfindNode(next, best, dest, state, heat[next]);
                    open.Add(pfn);
                    locToNode.Add(lid, pfn);
                }
            }
        }

        if (last == null)
        {
            return(null);
        }

        // Trace the route from destination to start (using each node's parent property)
        List <PathfindNode> route = new List <PathfindNode>();

        while (last != first && last != null)
        {
            route.Add(last);
            last = last.Parent;
        }

        // Reverse route and convert to Points
        List <Location> path = new List <Location>();

        for (int i = route.Count - 1; i >= 0; i--)
        {
            path.Add(route[i].Position);
        }

        // Return the list of Points
        return(path);
    }
Ejemplo n.º 4
0
    public static Location FindNextLocation(Location start, Location dest, IGameState state, List <Location> avoid, LayeredInfluenceMap heat)
    {
        if (start.Equals(dest) || !state.GetIsPassable(dest) || avoid.Contains(dest))
        {
            return(null);
        }

        List <Location> list = FindPath(start, dest, state, avoid, heat);

        if (list != null)
        {
            return(list[0]);
        }
        else
        {
            return(null);
        }
    }
Ejemplo n.º 5
0
        // Initialise at first step
        public void Init(IGameState state)
        {
            random = new Random();
            currentTasks = new Dictionary<string, CurrentTask>();
            nextTasks = new Dictionary<string, CurrentTask>();
            myHills = new List<Location>(state.MyHills);
            theirHills = new List<Location>();
            yummies = new List<Location>();
            timRobbins = new List<Location>();
            martinSheen = new List<Location>();
            viewRadius = (int)Math.Sqrt(state.ViewRadius2);
            heatMap = new HeatMap(viewRadius, state);

            influenceMaps = new LayeredInfluenceMap(state);
            influenceMaps["Enemies"] = new InfluenceMap(state, 4, 1);
            influenceMaps["Friends"] = new InfluenceMap(state, 1, 1);

            foreach (Location h in myHills)
            {
                heatMap.UpdateCell(h, -5f);

                Location tim;
                tim = h + new Location(-1, -1);
                if (state.GetIsPassable(tim))
                    timRobbins.Add(tim);

                tim = h + new Location(-1, 1);
                if (state.GetIsPassable(tim))
                    timRobbins.Add(tim);

                tim = h + new Location(1, -1);
                if (state.GetIsPassable(tim))
                    timRobbins.Add(tim);

                tim = h + new Location(1, 1);
                if (state.GetIsPassable(tim))
                    timRobbins.Add(tim);
            }
        }