Ejemplo n.º 1
0
        public static void init(string filename)
        {
            nodes = new Dictionary<Tuple<int, int>, WaypointNode>();
            XDocument xdoc = XDocument.Load("Resources/TestWaypointNeighbors.xml");

            //add all the waypoints to the graph
            foreach (var item in xdoc.Element("Waypoints").Elements("Waypoint"))
            {
                int x = int.Parse(item.Attribute("X").Value);
                int y = int.Parse(item.Attribute("Y").Value);
                nodes[new Tuple<int, int>(x, y)] = new WaypointNode(x, y, new List<WaypointNode>());
            }

            //find the neigbhors for every waypoint
            foreach (var item in xdoc.Element("Waypoints").Elements("Waypoint"))
            {
                int x = int.Parse(item.Attribute("X").Value);
                int y = int.Parse(item.Attribute("Y").Value);
                Tuple<int, int> thisPoint = new Tuple<int, int>(x, y);
                foreach (var neighbor in item.Elements("Neighbor"))
                {
                    int xn = int.Parse(neighbor.Attribute("X").Value);
                    int yn = int.Parse(neighbor.Attribute("Y").Value);
                    Tuple<int, int> neighborPoint = new Tuple<int, int>(xn, yn);
                    nodes[thisPoint].Neighbors.Add(nodes[neighborPoint]);
                }
            }
        }
Ejemplo n.º 2
0
        public override DoublePoint GetGoal()
        {
            bool player1 = Game.Player1.Minions.ContainsValue(_owner);
            //List<WaypointNode> target_nodes = new List<WaypointNode>();
            /*      These are guesses for base nodes: ...?
                    <Waypoint X="231" Y="1586" />
                    <Waypoint X="326" Y="1760" />
                    <Waypoint X="495" Y="1777" />

                    <Waypoint X="160" Y="47" />
                    <Waypoint X="499" Y="44" />
                    <Waypoint X="810" Y="37" />
                 * */
            if (target == null || WaypointGraph.GetClosestWaypoint((int)_owner.Pos.X, (int)_owner.Pos.Y) == target)
            {
                if (player1)
                {
                    Random random = new Random();
                    int randX = random.Next(0, 1000);
                    int randY = random.Next(0, 900);

                    target = WaypointGraph.GetClosestWaypoint(randX, randY);
                }
                else
                {
                    Random random = new Random();
                    int randX = random.Next(0, 1000);
                    int randY = random.Next(900, 1800);

                    target = WaypointGraph.GetClosestWaypoint(randX, randY);
                }
            }
            WaypointNode myNode = WaypointGraph.GetClosestWaypoint((int)_owner.Pos.X, (int)_owner.Pos.Y);
            List<WaypointNode> path = new List<WaypointNode>();
            /*
             * int length = Int32.MaxValue;
            foreach(WaypointNode target in target_nodes){
                List<WaypointNode> p = WaypointGraph.pathfindDijkstra(myNode, target);
                if (p.Count < length){
                    path = p;
                    length = p.Count;
                }
            }
             * */
            path = WaypointGraph.pathfindDijkstra(myNode, target);
            //path.Add(new WaypointNode((int)closest.Pos.X, (int)closest.Pos.Y, new List<WaypointNode>()));
            FollowPath fp = new FollowPath(_owner, path);

            return fp.GetGoal(); ;
        }
Ejemplo n.º 3
0
        public static List<WaypointNode> pathfindDijkstra(WaypointNode start, WaypointNode end)
        {
            NodeRecord startRecord = new NodeRecord(start);

            List<NodeRecord> open = new List<NodeRecord>();
            open.Add(startRecord);
            List<NodeRecord> closed = new List<NodeRecord>();
            NodeRecord current = new NodeRecord();

            while (open.Count > 0)
            {
                current = open.Where(e => e.CostSoFar == open.Min(f => f.CostSoFar)).First();

                if (current.Node == end)
                    break;

                // connections = graph.getConnections(current);
                List<Connection> connections = new List<Connection>();
                foreach (WaypointNode n in current.Node.Neighbors)
                {
                    WaypointNode fromNode = current.Node;
                    Connection c = new Connection(getDistance(current.Node, n), current.Node, n);
                    connections.Add(c);
                }

                foreach (Connection connection in connections)
                {
                    WaypointNode endNode = connection.getToNode();
                    double endNodeCost = current.CostSoFar + connection.getCost();
                    NodeRecord endNodeRecord = closed.Where(e => e.Node == endNode).FirstOrDefault();
                    if (endNodeRecord != null)
                    {
                        continue;
                    }
                    endNodeRecord = open.Where(e => e.Node == endNode).FirstOrDefault();
                    if (endNodeRecord != null)
                    {
                        if (endNodeRecord.CostSoFar <= endNodeCost)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        endNodeRecord = new NodeRecord(endNode);
                    }

                    endNodeRecord.CostSoFar = endNodeCost;
                    endNodeRecord.Connection = connection;

                    if (!open.Contains(endNodeRecord))
                    {
                        open.Add(endNodeRecord);
                    }
                }
                open.Remove(current);
                closed.Add(current);
            }
            if (current.Node != end)
            {
                return null;
            }
            else
            {
                if (current.Connection == null)
                {
                    return new List<WaypointNode>();
                }

                List<WaypointNode> path = new List<WaypointNode>();
                path.Add(current.Connection.getFromNode());
                while (current.Node != start)
                {
                    // path += current.connection;
                    path.Add(current.Connection.getToNode());
                    // current = current.connection.getFromNode();
                    current = closed.Where(e => e.Node == current.Connection.getFromNode()).First();
                }
                path.Reverse();
                return path;
            }
        }
Ejemplo n.º 4
0
 private static double getDistance(WaypointNode node1, WaypointNode node2)
 {
     return getDistance(node1.X, node2.X, node1.Y, node2.Y);
 }
Ejemplo n.º 5
0
 public static void ConnectNodes(WaypointNode aNode, WaypointNode bNode)
 {
     aNode.Neighbors.Add(bNode);
     bNode.Neighbors.Add(aNode);
 }
Ejemplo n.º 6
0
 public void getPath(WaypointNode curNode, WaypointNode target)
 {
     _path = WaypointGraph.pathfindDijkstra(curNode, target);
 }