Example #1
0
 public RoomPivot(DMapNode _MapNode, GameObject _RoomPiv, Vector3 _PosInWorld, Vector2Int _PosInMap, int _HPos, String _Name)
 {
     MapNode    = _MapNode;
     RoomPiv    = _RoomPiv;
     PosInMap   = _PosInMap;
     HPos       = _HPos;
     Name       = _Name;
     PosInWorld = _PosInWorld;
 }
Example #2
0
 // resets map
 void ResetMap()
 {
     for (int y = 0; y < Height; y++)
     {
         for (int x = 0; x < Width; x++)
         {
             Map[x, y] = new DMapNode(0, new Vector2(), "X00", new Vector2Int());
         }
     }
 }
Example #3
0
    // utility function for a star pathfinding
    int GetDistance(DMapNode _NodeA, DMapNode _NodeB)
    {
        int dstX = (int)Mathf.Abs(_NodeA.GirdPosition.x - _NodeB.GirdPosition.x);
        int dstY = (int)Mathf.Abs(_NodeA.GirdPosition.y - _NodeB.GirdPosition.y);

        if (dstX > dstY)
        {
            return(14 * dstY + 10 * (dstX - dstY));
        }
        return(14 * dstX + 10 * (dstY - dstX));
    }
Example #4
0
    // utility function for a star pathfinding
    Vector2Int[] RetracePath(DMapNode _StartNode, DMapNode _EndNode)
    {
        List <DMapNode> path        = new List <DMapNode>();
        DMapNode        currentNode = _EndNode;

        while (currentNode != _StartNode)
        {
            path.Add(currentNode);
            currentNode = currentNode.parent;
        }
        Vector2Int[] waypoints = ListToVector(path);
        Array.Reverse(waypoints);
        return(waypoints);
    }
Example #5
0
    // utility function for a star pathfinding
    List <DMapNode> GetNeighboursFourAxis(DMapNode _Node)
    {
        List <DMapNode> Neighbours = new List <DMapNode>();

        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                if (x == 0 && y == 0)
                {
                    continue;
                }
                if (x == -1 && y == -1)
                {
                    continue;
                }
                if (x == -1 && y == 1)
                {
                    continue;
                }
                if (x == 1 && y == 1)
                {
                    continue;
                }
                if (x == 1 && y == -1)
                {
                    continue;
                }

                int CheckX = Mathf.RoundToInt(_Node.GirdPosition.x + x);
                int CheckY = Mathf.RoundToInt(_Node.GirdPosition.y + y);

                if (CheckX >= 0 && CheckX < Width && CheckY >= 0 && CheckY < Height)
                {
                    Neighbours.Add(Map[CheckX, CheckY]);
                }
            }
        }
        return(Neighbours);
    }
Example #6
0
    // path finding function using a star path finding
    public void FindPath()
    {
        waypoints   = new Vector2Int[0];
        pathSuccess = false;

        DMapNode startNode  = DMapStartNode;
        DMapNode targetNode = DMapEndNode;

        startNode.parent = startNode;

        Heap <DMapNode>    openSet   = new Heap <DMapNode>(MaxSize);
        HashSet <DMapNode> closedSet = new HashSet <DMapNode>();

        openSet.Add(startNode);

        while (openSet.Count > 0)
        {
            DMapNode currentNode = openSet.RemoveFirst();
            closedSet.Add(currentNode);

            if (currentNode.GirdPosition == targetNode.GirdPosition)
            {
                targetNode.parent = currentNode.parent;
                pathSuccess       = true;
                break;
            }

            if (TargetDifficultyMin == 0 && TargetDifficultyMax == 0)
            {
                foreach (DMapNode neighbour in GetNeighboursFourAxis(currentNode))
                {
                    if (closedSet.Contains(neighbour))
                    {
                        continue;
                    }

                    int newMovementCostToNeighbour = currentNode.GCost + GetDistance(currentNode, neighbour) + neighbour.Difficulty;
                    if (newMovementCostToNeighbour < neighbour.GCost || !openSet.Contains(neighbour))
                    {
                        neighbour.GCost  = newMovementCostToNeighbour;
                        neighbour.HCost  = GetDistance(neighbour, targetNode);
                        neighbour.parent = currentNode;

                        if (!openSet.Contains(neighbour))
                        {
                            openSet.Add(neighbour);
                        }
                        else
                        {
                            openSet.UpdateItem(neighbour);
                        }
                    }
                }
            }
            else
            {
                foreach (DMapNode neighbour in GetNeighboursFourAxis(currentNode))
                {
                    if (closedSet.Contains(neighbour))
                    {
                        continue;
                    }


                    if (neighbour.Difficulty <= TargetDifficultyMax && neighbour.Difficulty >= TargetDifficultyMin)
                    {
                        int newMovementCostToNeighbour = currentNode.GCost + GetDistance(currentNode, neighbour) + neighbour.Difficulty;
                        if (newMovementCostToNeighbour < neighbour.GCost || !openSet.Contains(neighbour))
                        {
                            neighbour.GCost  = newMovementCostToNeighbour;
                            neighbour.HCost  = GetDistance(neighbour, targetNode);
                            neighbour.parent = currentNode;

                            if (!openSet.Contains(neighbour))
                            {
                                openSet.Add(neighbour);
                            }
                            else
                            {
                                openSet.UpdateItem(neighbour);
                            }
                        }
                    }
                }
            }
        }

        if (pathSuccess)
        {
            waypoints   = RetracePath(startNode, targetNode);
            pathSuccess = waypoints.Length > 0;
        }
    }
Example #7
0
    // adds rooms so that they all fit with no spaces between them inside the spacified size of the map square
    void FillMapRandomRooms()
    {
        // initialize  nodes in map including start room nodes and end rooms nodes in map
        int h1 = 0;

        for (int y1 = 0; y1 < Height; y1++)
        {
            for (int x1 = 0; x1 < Width; x1++)
            {
                if (x1 == StartNode.Position.x && y1 == StartNode.Position.y)
                {
                    DMapStartNode                       = new DMapNode(0, StartNode.Size, "S00", StartNode.Position);
                    roomPivots[h1].Name                 = "Start";
                    roomPivots[h1].MapNode.Room         = StartNode.RoomObj;
                    roomPivots[h1].MapNode.Room.name    = StartNode.RoomObj.name;
                    roomPivots[h1].MapNode.Size         = StartNode.Size;
                    roomPivots[h1].MapNode.Symbol       = "S00";
                    roomPivots[h1].MapNode.IsInMap      = true;
                    roomPivots[h1].MapNode.GirdPosition = StartNode.Position;
                    AddtoMap(StartNode.Size, StartNode.Position, "S00", 0);
                }
                else if (x1 == EndNode.Position.x && y1 == EndNode.Position.y)
                {
                    DMapEndNode                         = new DMapNode(0, EndNode.Size, "E00", EndNode.Position);
                    roomPivots[h1].Name                 = "End";
                    roomPivots[h1].MapNode.Room         = EndNode.RoomObj;
                    roomPivots[h1].MapNode.Room.name    = EndNode.RoomObj.name;
                    roomPivots[h1].MapNode.Size         = EndNode.Size;
                    roomPivots[h1].MapNode.Symbol       = "E00";
                    roomPivots[h1].MapNode.IsInMap      = true;
                    roomPivots[h1].MapNode.GirdPosition = EndNode.Position;
                    AddtoMap(EndNode.Size, EndNode.Position, "E00", 0);
                }

                roomPivots[h1].PosInWorld = new Vector3(x1 + this.transform.position.x, 0.0f + this.transform.position.y, -y1 + this.transform.position.z);
                h1++;
            }
        }

        // if user has chosen a random seed initialize seed here
        if (UsingRandom)
        {
            Seed = Time.time.ToString() + UnityEngine.Random.Range(0, 1000);
        }

        System.Random RandSeed = new System.Random(Seed.GetHashCode());



        int   i           = 0;
        float z           = 0;
        int   h           = 0;
        int   y           = 0;
        int   x           = 0;
        int   number      = 0;
        bool  first       = true;
        bool  b_hit       = false;
        bool  MadeSmaller = false;

        // begins loop to add all rooms into map square with no spaces
        // rooms are compacted in, can handle all room sizes
        // the generator goes node by node from the top left of the map
        // to the bottom of the map from collum by collum then row by row
        // do this until the whole map has been filled
        // **** THERE HAVE NOT BEEN ANY GAME OBJECTS MADE YET THIS IS ALL ON AN 2D ARRAY IN SYSTEM VERY LIGHT WEIGHT ***
        while (y < Height)
        {
            while (x < Width)
            {
                // choose a room randomly from list of rooms provided
                room Room;
                i    = RandSeed.Next(0, Rooms.Count);
                Room = Rooms[i];

                // begin checks to make sure room can be fit into map
                // check y
                if (Room.Size.y > ((Height) - y))
                {
                    // if it cannot be fit in because it is too tall grab a shorter room from list of rooms
                    Room        = GetSmallerRoomY(((Height) - y));
                    MadeSmaller = true;
                }
                // check x
                if (Room.Size.x > ((Width) - z))
                {
                    if (MadeSmaller)
                    {
                        // if it already has been made shorter because it was too tall
                        // and now it is too wide get the smallest rooms in all ways possible
                        // with in list of rooms
                        Room = GetRoom(new Vector2Int((int)((Width) - z), ((Height) - y)));
                    }
                    else
                    {
                        // if it cannot be fit in because it is too wide grab a thinner room from list of rooms
                        Room = GetSmallerRoomX((int)((Width) - z));
                    }
                }

                // if room being put in is not first room (apart from start and end) being put into map
                if (!first)
                {
                    // check if room is colliding with another room in the position generator is tryign to put it in
                    // if it is not colliding with another room then place it down
                    if (!CheckHitMap(new Vector2(Room.Size.x, Room.Size.y), new Vector2Int((int)z, y)))
                    {
                        if (number <= 9)
                        {
                            AddtoMap(new Vector2(Room.Size.x, Room.Size.y), new Vector2Int((int)z, y), "O0" + number, Room.Difficulty);
                            roomPivots[h].MapNode.Room         = Room.RoomObj;
                            roomPivots[h].MapNode.Room.name    = Room.RoomObj.name;
                            roomPivots[h].MapNode.Size         = new Vector2(Room.Size.x, Room.Size.y);
                            roomPivots[h].MapNode.Symbol       = "O0" + number;
                            roomPivots[h].MapNode.GirdPosition = roomPivots[h].PosInMap;
                        }
                        else
                        {
                            roomPivots[h].MapNode.Size      = new Vector2(Room.Size.x, Room.Size.y);
                            roomPivots[h].MapNode.Room      = Room.RoomObj;
                            roomPivots[h].MapNode.Room.name = Room.RoomObj.name;
                            roomPivots[h].MapNode.Symbol    = "O" + number;
                            AddtoMap(new Vector2(Room.Size.x, Room.Size.y), new Vector2Int((int)z, y), "O" + number, Room.Difficulty);
                            roomPivots[h].MapNode.GirdPosition = roomPivots[h].PosInMap;
                        }
                        z += Room.Size.x;
                        roomPivots[h].Name = "Room Pivot" + h;
                        h += (int)Room.Size.x;
                        x++;
                        number++;
                    }
                    else
                    {
                        // if it does collide with another room try to move a node to its right and place it again
                        // continue trying to do this until the room is too wide to be place on that row
                        // if it is too wide try another room
                        while (!placed)
                        {
                            if (z >= Width || Room.Size.x > ((Width) - z))
                            {
                                h    += (int)((Width) - z);
                                b_hit = true;
                                break;
                            }
                            else
                            {
                                if (!CheckHitMap(new Vector2(Room.Size.x, Room.Size.y), new Vector2Int((int)z, y)))
                                {
                                    if (number <= 9)
                                    {
                                        AddtoMap(new Vector2(Room.Size.x, Room.Size.y), new Vector2Int((int)z, y), "O0" + number, Room.Difficulty);
                                        roomPivots[h].MapNode.Room         = Room.RoomObj;
                                        roomPivots[h].MapNode.Room.name    = Room.RoomObj.name;
                                        roomPivots[h].MapNode.Size         = new Vector2(Room.Size.x, Room.Size.y);
                                        roomPivots[h].MapNode.Symbol       = "O0" + number;
                                        roomPivots[h].MapNode.GirdPosition = roomPivots[h].PosInMap;
                                    }
                                    else
                                    {
                                        roomPivots[h].MapNode.Size   = new Vector2(Room.Size.x, Room.Size.y);
                                        roomPivots[h].MapNode.Symbol = "O" + number;
                                        AddtoMap(new Vector2(Room.Size.x, Room.Size.y), new Vector2Int((int)z, y), "O" + number, Room.Difficulty);
                                        roomPivots[h].MapNode.Room         = Room.RoomObj;
                                        roomPivots[h].MapNode.Room.name    = Room.RoomObj.name;
                                        roomPivots[h].MapNode.GirdPosition = roomPivots[h].PosInMap;
                                    }
                                    z += Room.Size.x;
                                    roomPivots[h].Name = "Room Pivot" + h;
                                    placed             = true;
                                    h += (int)Room.Size.x;
                                    x++;
                                    number++;
                                }
                                else
                                {
                                    z += Room.Size.x;
                                    h += (int)Room.Size.x;
                                    x++;
                                }
                            }
                        }
                    }
                }
                else
                {
                    // if the room is the first room to be placed check if it is colliding with eaither the
                    // starting room or ending room
                    // if it is move across row until it can be placed
                    if (!CheckHitMap(new Vector2(Room.Size.x, Room.Size.y), new Vector2Int((int)z, y)))
                    {
                        if (number <= 9)
                        {
                            AddtoMap(new Vector2(Room.Size.x, Room.Size.y), new Vector2Int((int)z, y), "O0" + number, Room.Difficulty);
                            roomPivots[h].MapNode.Room         = Room.RoomObj;
                            roomPivots[h].MapNode.Room.name    = Room.RoomObj.name;
                            roomPivots[h].MapNode.Size         = new Vector2(Room.Size.x, Room.Size.y);
                            roomPivots[h].MapNode.Symbol       = "O0" + number;
                            roomPivots[h].MapNode.GirdPosition = roomPivots[h].PosInMap;
                        }
                        else
                        {
                            roomPivots[h].MapNode.Size      = new Vector2(Room.Size.x, Room.Size.y);
                            roomPivots[h].MapNode.Room      = Room.RoomObj;
                            roomPivots[h].MapNode.Room.name = Room.RoomObj.name;
                            roomPivots[h].MapNode.Symbol    = "O" + number;
                            AddtoMap(new Vector2(Room.Size.x, Room.Size.y), new Vector2Int((int)z, y), "O" + number, Room.Difficulty);
                            roomPivots[h].MapNode.GirdPosition = roomPivots[h].PosInMap;
                        }

                        roomPivots[h].Name = "Room Pivot" + h;
                        z += Room.Size.x;
                        x++;
                        h += (int)Room.Size.x;
                        number++;
                    }
                    first = false;
                }
                if (b_hit)
                {
                    break;
                }
                placed = false;
                if (z == Width)
                {
                    break;
                }
            }
            MadeSmaller = false;
            b_hit       = false;
            z           = 0;
            x           = 0;
            y++;
        }
    }