void AddNeighbourNode(int xOffset, int yOffset, NodePosition parentPos, AStarPathfinder aStarSearch)
 {
     if (ValidNeigbour(xOffset, yOffset) &&
         !(parentPos.x == position.x + xOffset && parentPos.y == position.y + yOffset))
     {
         NodePosition  neighbourPos = new NodePosition(position.x + xOffset, position.y + yOffset);
         MapSearchNode newNode      = pathfinder.AllocateMapSearchNode(neighbourPos);
         aStarSearch.AddSuccessor(newNode);
     }
 }
Exemple #2
0
        public bool GetSuccessors(AStarPathfinder astarsearch, MapSearchNode parent_node)
        {
            MapSearchNode n;
            MapNode       left  = null;
            MapNode       right = null;
            bool          ret;

            if (parent_node != null && mapNode.floorNode != null)
            {
                // Start node which is also a floor node.
                // Find and add nearest transport nodes to the left/right (based on start_point, which should be populated)
                List <MapNode> nodesOnFloor = mapNode.nodesOnFloor;
                //assert(nodesOnFloor != NULL);
                foreach (var nodex in nodesOnFloor)
                //for (List<MapNode> const_iterator i = nodesOnFloor->begin(); i != nodesOnFloor->end(); i++)
                {
                    //MapNode node = *i;
                    if (nodex.position.X <= start_point.X && canTransfer(this, nodex, Direction.LEFT))
                    {
                        left = nodex;
                    }
                    else if (canTransfer(this, nodex, Direction.RIGHT))
                    {
                        right = nodex;
                        break;
                    }
                }

                if (left != null)
                {
                    createNode(out n, left);
                    ret = astarsearch.AddSuccessor(n);
                    if (!ret)
                    {
                        return(false);
                    }
                }

                if (right != null)
                {
                    createNode(out n, right);
                    ret = astarsearch.AddSuccessor(n);
                    if (!ret)
                    {
                        return(false);
                    }
                }

                return(true);
            }

            //if (mapNode.position.Y == end_point.Y && astarsearch.GetSolutionEnd().mapNode.floorNode != null)
            //{
            //  // Reached the same floor as end node, and end node is a floor node.
            //  // Add destination floor node
            //  if (mapNode.floorNode != null) return false; // ERROR: All transport nodes must contain pointer to their respective floor node.

            //  createNode(out n, mapNode.floorNode);
            //  ret = astarsearch.AddSuccessor(n);
            //  if (!ret) return false;
            //  else return true;
            //}

            MapNode node = mapNode.neighbours[(int)Direction.LEFT];

            while (node != null && left == null)
            {
                if (!canTransfer(this, node, Direction.LEFT))
                {
                    node = node.neighbours[(int)Direction.LEFT];
                    continue;
                }

                left = node;
            }

            node = mapNode.neighbours[(int)Direction.RIGHT];
            while (node != null && right == null)
            {
                if (!canTransfer(this, node, Direction.RIGHT))
                {
                    node = node.neighbours[(int)Direction.RIGHT];
                    continue;
                }

                right = node;
            }

            if (left != null)
            {
                createNode(out n, left);
                ret = astarsearch.AddSuccessor(n);
                if (!ret)
                {
                    return(false);
                }
            }

            if (right != null)
            {
                createNode(out n, right);
                ret = astarsearch.AddSuccessor(n);
                if (!ret)
                {
                    return(false);
                }
            }

            if (mapNode.neighbours[(int)Direction.UP] != null)
            {
                node = mapNode.neighbours[(int)Direction.UP];
                if (mapNode.HasElevator)
                {
                    while (node != null)
                    {
                        createNode(out n, node);
                        ret = astarsearch.AddSuccessor(n);
                        if (!ret)
                        {
                            return(false);
                        }
                        node = node.neighbours[(int)Direction.UP];
                    }
                }
                else if (canTransfer(this, node, Direction.UP))
                {
                    createNode(out n, node);
                    ret = astarsearch.AddSuccessor(n);
                    if (!ret)
                    {
                        return(false);
                    }
                }
            }

            if (mapNode.neighbours[(int)Direction.DOWN] != null)
            {
                node = mapNode.neighbours[(int)Direction.DOWN];
                if (mapNode.HasElevator)
                {
                    while (node != null)
                    {
                        createNode(out n, node);
                        ret = astarsearch.AddSuccessor(n);
                        if (!ret)
                        {
                            return(false);
                        }
                        node = node.neighbours[(int)Direction.DOWN];
                    }
                }
                else if (canTransfer(this, node, Direction.DOWN))
                {
                    createNode(out n, node);
                    ret = astarsearch.AddSuccessor(n);
                    if (!ret)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }