コード例 #1
0
    public void SetCost(RDGGridNode node, RDGRoom roomFrom, RDGRoom roomTo, RDGGrid grid)
    {
        hueristic = Mathf.Abs(x - Mathf.CeilToInt(roomTo.center.x)) + Mathf.Abs(y - Mathf.CeilToInt(roomTo.center.y));

        if ((roomFrom != null && grid[x, y] == roomFrom) || (roomTo != null && grid[x, y] == roomTo))
        {
            cost = 0;
        }
        else if (grid[x, y] != null)
        {
            if (grid[x, y].type == RoomType.CORRIDOR)
            {
                cost = 0;
            }
            else
            {
                cost = int.MaxValue - hueristic;                 //Subtract the huersitic so that we don't overflow when we call fullCost
            }
        }
        else
        {
            cost = parent.cost + 1;
            if (dir != node.dir)
            {
                cost += 2;
            }
        }
    }
コード例 #2
0
 public RDGRoom this[RDGGridNode node]
 {
     get
     {
         return(this[node.x, node.y]);
     }
 }
コード例 #3
0
    List <RDGGridNode> GetNeighbors(RDGGridNode fromNode, RDGRoom roomFrom = null, RDGRoom roomTo = null)
    {
        List <RDGGridNode> neighbors = new List <RDGGridNode>();

        RDGGridNode currNode;

        if (fromNode.x > minX)
        {
            currNode = new RDGGridNode(x: fromNode.x - 1, y: fromNode.y, dir: Direction.WEST, parent: fromNode);
            currNode.SetCost(fromNode, roomFrom, roomTo, this);
            neighbors.Add(currNode);
        }
        if (fromNode.x < maxX)
        {
            currNode = new RDGGridNode(x: fromNode.x + 1, y: fromNode.y, dir: Direction.EAST, parent: fromNode);
            currNode.SetCost(fromNode, roomFrom, roomTo, this);
            neighbors.Add(currNode);
        }
        if (fromNode.y > minY)
        {
            currNode = new RDGGridNode(x: fromNode.x, y: fromNode.y - 1, dir: Direction.SOUTH, parent: fromNode);
            currNode.SetCost(fromNode, roomFrom, roomTo, this);
            neighbors.Add(currNode);
        }
        if (fromNode.y < maxY)
        {
            currNode = new RDGGridNode(x: fromNode.x, y: fromNode.y + 1, dir: Direction.NORTH, parent: fromNode);
            currNode.SetCost(fromNode, roomFrom, roomTo, this);
            neighbors.Add(currNode);
        }

        return(neighbors);
    }
コード例 #4
0
 public RDGGridNode(int x = 0, int y = 0, Direction dir = Direction.NONE, int cost = 0, RDGGridNode parent = null)
 {
     this.x      = x;
     this.y      = y;
     this.dir    = dir;
     this.cost   = cost;
     this.parent = parent;
 }
コード例 #5
0
    void AddRoom(RDGGridNode initialNode, RDGGridNode finalNode)
    {
        RDGRoom room = new RDGRoom(Mathf.Min(initialNode.x, finalNode.x), Mathf.Min(initialNode.y, finalNode.y), Mathf.Abs(initialNode.x - finalNode.x) + 1, Mathf.Abs(initialNode.y - finalNode.y) + 1);

        room.type = RoomType.CORRIDOR;
        theGrid.AddRoom(room);
        rooms.Add(room);
    }
コード例 #6
0
    public IEnumerator Pathfind(RDGRoom roomFrom, RDGRoom roomTo)
    {
        List <RDGGridNode> open   = new List <RDGGridNode>();
        List <RDGGridNode> closed = new List <RDGGridNode>();

        RDGGridNode currNode = new RDGGridNode(x: Mathf.FloorToInt(roomFrom.center.x), y: Mathf.FloorToInt(roomFrom.center.y));

        open.Add(currNode);

        RDGGridNode otherNode;

        while (open.Count > 0)
        {
            currNode = open.OrderBy(x => x.fullCost).ElementAt(0);
            open.Remove(currNode);

            //Debug.Log("Testing node: " + currNode.x + ", " + currNode.y);

            if (currNode.x == Mathf.FloorToInt(roomTo.center.x) && currNode.y == Mathf.FloorToInt(roomTo.center.y))
            {
                lastPath = BuildPath(currNode);
                yield break;
            }

            foreach (var item in GetNeighbors(currNode, roomFrom, roomTo))
            {
                otherNode = open.Find(n => n.x == currNode.x && n.y == currNode.y);
                if (otherNode != null)
                {
                    if (otherNode.cost > item.cost)
                    {
                        open.Remove(otherNode);
                    }
                    else
                    {
                        continue;
                    }
                }
                otherNode = closed.Find(n => n.x == currNode.x && n.y == currNode.y);
                if (otherNode != null)
                {
                    if (otherNode.cost > item.cost)
                    {
                        closed.Remove(otherNode);
                    }
                    else
                    {
                        continue;
                    }
                }
                open.Add(item);
                //yield return null;
            }

            closed.Add(currNode);
        }
    }
コード例 #7
0
    Stack <RDGGridNode> BuildPath(RDGGridNode lastNode)
    {
        Stack <RDGGridNode> path = new Stack <RDGGridNode>();

        do
        {
            path.Push(lastNode);
            lastNode = lastNode.parent;
        }while(lastNode != null);

        return(path);
    }
コード例 #8
0
    IEnumerator BuildCorridors()
    {
        foreach (var roomFrom in minGraph.graph.Keys)
        {
            foreach (var roomTo in minGraph.graph[roomFrom])
            {
                if (roomFrom.id < roomTo.id)
                {
                    continue;
                }
                yield return(StartCoroutine(theGrid.Pathfind(roomFrom, roomTo)));

                Stack <RDGGridNode> path = theGrid.lastPath;
                RDGGridNode         initialNode = null, finalNode = null, currNode;
                while (path != null && path.Count > 0)
                {
                    currNode = path.Pop();
                    if (theGrid [currNode] != null)
                    {
                        if (initialNode != null)
                        {
                            AddRoom(initialNode, finalNode);
                            initialNode = null;
                            finalNode   = null;
                        }
                    }
                    else
                    {
                        if (initialNode == null)
                        {
                            initialNode = currNode;
                        }
                        else if (currNode.dir != initialNode.dir)
                        {
                            AddRoom(initialNode, finalNode);
                            initialNode = currNode;
                        }
                        finalNode = currNode;
                    }
                }
                yield return(null);
            }
        }
    }