public static void PackRoom(Room room, int floor, Dungeon building)
    {
        room.xSize = (int)Mathf.Sqrt(room.size);
        room.ySize = (int)(room.size / (float)room.xSize);
        int swapRoll = Random.Range(0, 2);

        if (swapRoll == 0)
        {
            var temp = room.xSize;
            room.xSize = room.ySize;
            room.ySize = temp;
        }
        for (int x = 0; x < building.maxDimensions - room.xSize; x++)
        {
            for (int y = 0; y < building.maxDimensions - room.ySize; y++)
            {
                if (LevelGenValidationUtils.RoomFits(room, floor, x, y, building.grid))
                {
                    room.x     = x;
                    room.y     = y;
                    room.floor = floor;
                    LevelGenGridUtils.FillRoom(room, floor, x, y, building.grid);
                    return;
                }
            }
        }
        building.maxDimensions = (int)(building.maxDimensions * 1.2);
        PackRoom(room, floor, building);
    }
    private static void InstantiateRoomDoor(int x, int y, int floor, string block, Dungeon layout)
    {
        if (block != "X")
        {
            return;
        }
        var room = LevelGenRoomUtils.GetRoomFromCoords(x, y, floor, layout);

        if (room is CommonSpace)
        {
            return;
        }
        if (LevelGenRoomUtils.IsRoomEntrance(x, y, floor, layout, 3) && LevelGenGridUtils.IsCorridor(x - 1, y, floor, layout.grid))
        {
            MakeRoomDoor(x - 0.5f, y, 90);
        }
        if (LevelGenRoomUtils.IsRoomEntrance(x, y, floor, layout, 1) && LevelGenGridUtils.IsCorridor(x + 1, y, floor, layout.grid))
        {
            MakeRoomDoor(x + 0.5f, y, 270);
        }
        if (LevelGenRoomUtils.IsRoomEntrance(x, y, floor, layout, 0) && LevelGenGridUtils.IsCorridor(x, y - 1, floor, layout.grid))
        {
            MakeRoomDoor(x, y - 0.5f, 180);
        }
        if (LevelGenRoomUtils.IsRoomEntrance(x, y, floor, layout, 2) && LevelGenGridUtils.IsCorridor(x, y + 1, floor, layout.grid))
        {
            MakeRoomDoor(x, y + 0.5f, 0);
        }
    }
Beispiel #3
0
    public static bool AddCrookedConnectorToBlock(Room room, Coordinates start, Coordinates end, Dungeon building)
    {
        Graph g      = new Graph(room.floor, building.maxDimensions, building.maxDimensions, building.grid);
        Astar astar  = new Astar();
        var   values = astar.SearchUnfilled(g, start, end);
        var   chain  = new List <Coordinates>();
        var   cursor = end;

        while (values.ContainsKey(cursor) && values[cursor] != cursor)
        {
            chain.Add(cursor);
            cursor = values[cursor];
        }
        if (values.ContainsKey(cursor))
        {
            chain.Add(values[cursor]);
        }
        if (chain.Count > 0)
        {
            chain.Reverse();
        }
        var works = true;

        if (chain.Count == 0 || chain[0].x != start.x || chain[0].y != start.y)
        {
            works = false;
        }
        foreach (var entry in chain)
        {
            if (entry.x < 0 || entry.y < 0 || entry.x >= 120 || entry.y >= 120)
            {
                works = false;
                break;
            }
        }
        if (works)
        {
            foreach (var entry in chain)
            {
                if (LevelGenGridUtils.IsFloor(entry.x, entry.y, room.floor, building.grid))
                {
                    continue;
                }
                building.grid[room.floor, entry.x, entry.y] = "x";
            }
            return(true);
        }
        else
        {
            return(false);
        }
    }
    public override void LayoutRooms()
    {
        LevelGenGridUtils.FillGrid(building.grid, building.numFloors, 120, 120);
        var roomsBySize = LevelGenRoomUtils.SortRoomsBySize(building.rooms);

        building.numFloors = Random.Range(1, 6);
        var roomsByFloor = ChunkRoomsByFloors(roomsBySize);

        for (int i = 0; i < building.numFloors; i++)
        {
            LayoutFloor(roomsByFloor[i], i);
        }
    }
    public bool SetupVault()
    {
        LevelGenGridUtils.FillGrid(building.grid, building.numFloors, 120, 120);
        var success = true;
        var rooms   = new List <Room>();

        foreach (var room in building.rooms)
        {
            rooms.Add(room);
        }
        success = LevelGenRoomUtils.PackPreConnectedRooms(building, rooms);
        return(success);
    }
 private static bool PlaceStartingRoom(Dungeon building, Room room)
 {
     room.size  = 25;
     room.xSize = 5;
     room.ySize = 5;
     room.x     = (building.maxDimensions / 2) - 2;                                           // 58;
     room.y     = (building.maxDimensions / 2) - 2;                                           // 58;
     room.SetEntrance((building.maxDimensions / 2) - 2, (building.maxDimensions / 2) - 2, 3); //room.SetEntrance(58, 58, 3);
     LevelGenGridUtils.FillRoom(room, 0, room.x, room.y, building.grid);
     building.grid[0, (building.maxDimensions / 2) - 3, (building.maxDimensions / 2) - 2] = "x";
     building.grid[0, (building.maxDimensions / 2) - 4, (building.maxDimensions / 2) - 2] = "E";
     return(true);
 }
 private void ConnectOrphanedStairs(int floor)
 {
     for (int x = 0; x < building.maxDimensions; x++)
     {
         for (int y = 0; y < building.maxDimensions; y++)
         {
             if (LevelGenGridUtils.IsStairs(x, y, floor, building.grid))
             {
                 ConnectOrphanedStaircase(x, y, floor);
             }
         }
     }
 }
    private static void AddTorches(Room room, string[,,] grid)
    {
        var  floor     = room.floor;
        int  torchRoll = Random.Range(0, 2);
        bool hasTorch;

        if (torchRoll == 0)
        {
            hasTorch = true;
        }
        else
        {
            hasTorch = false;
        }
        for (int x = room.x; x < room.x + room.xSize; x++)
        {
            for (int y = room.y; y < room.y + room.ySize; y++)
            {
                hasTorch = !hasTorch;
                foreach (var location in room.dressingLocations)
                {
                    if (location.x == x && location.y == y)
                    {
                        hasTorch = false;
                    }
                }
                if (!hasTorch)
                {
                    continue;
                }
                if (LevelGenGridUtils.IsFloor(x, y, floor, grid) && !LevelGenGridUtils.IsFloor(x - 1, y, floor, grid))
                {
                    MakeTorch(x - 0.5f, y, 0);
                }
                if (LevelGenGridUtils.IsFloor(x, y, floor, grid) && !LevelGenGridUtils.IsFloor(x + 1, y, floor, grid))
                {
                    MakeTorch(x + 0.5f, y, 180);
                }
                if (LevelGenGridUtils.IsFloor(x, y, floor, grid) && !LevelGenGridUtils.IsFloor(x, y - 1, floor, grid))
                {
                    MakeTorch(x, y - 0.5f, 270);
                }
                if (LevelGenGridUtils.IsFloor(x, y, floor, grid) && !LevelGenGridUtils.IsFloor(x, y + 1, floor, grid))
                {
                    MakeTorch(x, y + 0.5f, 90);
                }
            }
        }
    }
    public static List <Vector2> GetPotentialSideDressingSpots(Room room, string[,,] grid)
    {
        var output = new List <Vector2>();

        for (int x = room.x; x < room.x + room.xSize; x++)
        {
            for (int y = room.y; y < room.y + room.ySize; y++)
            {
                if (LevelGenGridUtils.IsWallEast(x, y, room.floor, grid) || LevelGenGridUtils.IsWallNorth(x, y, room.floor, grid) || LevelGenGridUtils.IsWallSouth(x, y, room.floor, grid) || LevelGenGridUtils.IsWallWest(x, y, room.floor, grid))
                {
                    output.Add(new Vector2(x, y));
                }
            }
        }
        return(output);
    }
    private static void AddCommonSpaceSideFurnishing(CommonSpace room, string[,,] grid)
    {
        var population = room.inhabitants.Count;
        var size       = Mathf.Sqrt(DesignedBuilding.monsterRoomSizes[room.inhabitants[0].generalType]);
        int bedType    = ActiveCastleLivingQuartersGen.GetBedType(room);

        if (bedType == 1)
        {
            population /= 2;
        }
        bool fancy = (bedType >= 6);

        population /= 2;
        Dictionary <string, GameObject> furniture;
        Dictionary <string, float>      furnitureProbs;

        if (fancy)
        {
            furniture      = prefabs["castle"]["commonSpaceSideFurnitureFancy"];
            furnitureProbs = prefabProbability["castle"]["commonSpaceSideFurnitureFancy"];
        }
        else
        {
            furniture      = prefabs["castle"]["commonSpaceSideFurniture"];
            furnitureProbs = prefabProbability["castle"]["commonSpaceSideFurniture"];
        }
        var potentialSpots = LevelGenRoomUtils.GetPotentialSideDressingSpots(room, grid);

        foreach (var spot in potentialSpots)
        {
            var roll = Random.Range(0, 100);
            if (roll < 100 * population / (float)potentialSpots.Count)
            {
                var xFudge        = Random.Range(-0.05f, 0.05f);
                var yFudge        = Random.Range(-0.05f, 0.05f);
                var rotationFudge = Random.Range(-2.5f, 2.5f);
                var wallRotation  = LevelGenGridUtils.GetWallRotation((int)spot.x, (int)spot.y, room.floor, grid);
                var xAdjust       = LevelGenGridUtils.GetXSideDressingAdjustment((int)spot.x, (int)spot.y, room.floor, grid);
                var yAdjust       = LevelGenGridUtils.GetYSideDressingAdjustment((int)spot.x, (int)spot.y, room.floor, grid);
                var obj           = LevelGenInstantiationUtils.InstantiateBlockObject(furniture, furnitureProbs, xAdjust + xFudge + spot.x, yAdjust + yFudge + spot.y);
                room.dressingLocations.Add(spot);
                obj.transform.localScale = new Vector3(size, size, size);
                obj.transform.Rotate(0, rotationFudge + wallRotation, 0);
            }
        }
    }
 public static bool ValidVerticalCorridor(int floor, int start, int finish, int xBase, string[,,] grid)
 {
     for (int y = start; y < finish; y++)
     {
         for (int x = xBase - 1; x <= xBase + 1; x++)
         {
             if (x <= 0 || x >= 120 || y <= 0 || y >= 120)
             {
                 continue;
             }
             if (LevelGenGridUtils.IsFloor(x, y, floor, grid))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
 public static bool ValidHorizontalCorridor(int floor, int start, int finish, int yBase, Dungeon building)
 {
     for (int x = start; x < finish; x++)
     {
         for (int y = yBase - 1; y <= yBase + 1; y++)
         {
             if (x <= 0 || x >= 120 || y <= 0 || y >= 120)
             {
                 continue;
             }
             if (LevelGenGridUtils.IsFloor(x, y, floor, building.grid))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Beispiel #13
0
    public string PrintGridFloor(int floor)
    {
        var output = "Floor " + (floor + 1).ToString() + "\n";
        int maxX   = maxDimensions; //GetMaxUsedGridX(floor);
        int maxY   = maxDimensions; //GetMaxUsedGridY(floor);

        for (int y = 0; y <= maxY; y++)
        {
            for (int x = 0; x <= maxX; x++)
            {
                var spot = grid[floor, x, y];
                if (!LevelGenGridUtils.IsFloor(x, y, floor, grid) && spot != "R" && spot != "*")
                {
                    spot = ".";
                }
                output += spot + "  ";
            }
            output += "\n";
        }
        output += "-----------------------------\n";
        return(output);
    }
 private static void InstantiateRoomWall(int x, int y, int floor, string block, Dungeon layout)
 {
     if (block != "X")
     {
         return;
     }
     if (!LevelGenRoomUtils.IsRoomEntrance(x, y, floor, layout, 3) && LevelGenGridUtils.IsCorridor(x - 1, y, floor, layout.grid))
     {
         MakeRoomWall(x - 0.5f, y, 0);
     }
     if (!LevelGenRoomUtils.IsRoomEntrance(x, y, floor, layout, 1) && LevelGenGridUtils.IsCorridor(x + 1, y, floor, layout.grid))
     {
         MakeRoomWall(x + 0.5f, y, 180);
     }
     if (!LevelGenRoomUtils.IsRoomEntrance(x, y, floor, layout, 0) && LevelGenGridUtils.IsCorridor(x, y - 1, floor, layout.grid))
     {
         MakeRoomWall(x, y - 0.5f, 90);
     }
     if (!LevelGenRoomUtils.IsRoomEntrance(x, y, floor, layout, 2) && LevelGenGridUtils.IsCorridor(x, y + 1, floor, layout.grid))
     {
         MakeRoomWall(x, y + 0.5f, 270);
     }
 }
    private void ApplyRuinToFloor(int floor, float ruinLevel)
    {
        int ruinedArea = 0;
        int areaToRuin = 0;

        foreach (var room in building.rooms)
        {
            if (room.floor == floor)
            {
                areaToRuin += room.xSize * room.ySize;
            }
        }
        areaToRuin = (int)(areaToRuin * ruinLevel);
        int maxRuinAreaSize = (int)Mathf.Sqrt(areaToRuin / 4);

        while (ruinedArea < areaToRuin)
        {
            int newRuinAreaSizeX = Random.Range(1, maxRuinAreaSize + 1);
            int newRuinAreaSizeY = Random.Range(1, maxRuinAreaSize + 1);
            int posX             = Random.Range(0, building.maxDimensions - 1 - newRuinAreaSizeX);
            int posY             = Random.Range(0, building.maxDimensions - 1 - newRuinAreaSizeY);
            for (int x = posX; x < posX + newRuinAreaSizeX; x++)
            {
                for (int y = posY; y < posY + newRuinAreaSizeY; y++)
                {
                    if (LevelGenGridUtils.IsNonStairsFloor(x, y, floor, building.grid))
                    {
                        building.grid[floor, x, y] = "R";
                        ruinedArea++;
                    }
                }
            }
        }
        ConnectOrphanedStairs(floor);
        EnsureStairsConnect(floor);
    }
    public static void InstantiateWall(string type, int x, int y, int floor, string block, string[,,] grid)
    {
        var    castleParts         = LevelGenPrefabs.prefabs[type];
        var    castleProbabilities = LevelGenPrefabs.prefabProbability[type];
        string blockType;
        var    northCovered = false;
        var    southCovered = false;
        var    eastCovered  = false;
        var    westCovered  = false;

        if (!LevelGenGridUtils.IsFloor(x, y, floor, grid))
        {
            return;
        }
        if (block == "E")
        {
            blockType = "entrance";
            if (LevelGenGridUtils.IsFloor(x + 1, y, floor, grid) && !LevelGenGridUtils.IsFloor(x - 1, y, floor, grid))
            {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x - 1, y);
                obj.transform.Rotate(0, 180, 0);
                westCovered = true;
                LevelGen.instance.entranceLocation = new Vector3((x + 1) * 5, 0, y * 5);
                LevelGen.instance.entranceAngle    = 180;
            }
            else if (LevelGenGridUtils.IsFloor(x - 1, y, floor, grid) && !LevelGenGridUtils.IsFloor(x + 1, y, floor, grid))
            {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x + 1, y);
                obj.transform.Rotate(0, 0, 0);
                eastCovered = true;
                LevelGen.instance.entranceLocation = new Vector3((x - 1) * 5, 0, y * 5);
                LevelGen.instance.entranceAngle    = 0;
            }
            else if (LevelGenGridUtils.IsFloor(x, y + 1, floor, grid) && !LevelGenGridUtils.IsFloor(x, y - 1, floor, grid))
            {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y - 1);
                obj.transform.Rotate(0, 90, 0);
                northCovered = true;
                LevelGen.instance.entranceLocation = new Vector3(x * 5, 0, (y + 1) * 5);
                LevelGen.instance.entranceAngle    = 90;
            }
            else if (LevelGenGridUtils.IsFloor(x, y - 1, floor, grid) && !LevelGenGridUtils.IsFloor(x, y + 1, floor, grid))
            {
                //else {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y + 1);
                obj.transform.Rotate(0, 270, 0);
                southCovered = true;
                LevelGen.instance.entranceLocation = new Vector3(x * 5, 0, (y - 1) * 5);
                LevelGen.instance.entranceAngle    = 270;
            }
        }
        else if (block == ">")
        {
            blockType = "stairsToNext";
            if (LevelGenGridUtils.IsFloor(x + 1, y, floor, grid) && !LevelGenGridUtils.IsFloor(x - 1, y, floor, grid))
            {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y);
                obj.transform.Rotate(0, 180, 0);
                westCovered = true;
                LevelGen.instance.exitLocation = new Vector3((x + 1) * 5, 0, y * 5);
                LevelGen.instance.exitAngle    = 180;
            }
            else if (LevelGenGridUtils.IsFloor(x - 1, y, floor, grid) && !LevelGenGridUtils.IsFloor(x + 1, y, floor, grid))
            {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y);
                obj.transform.Rotate(0, 0, 0);
                eastCovered = true;
                LevelGen.instance.exitLocation = new Vector3((x - 1) * 5, 0, y * 5);
                LevelGen.instance.exitAngle    = 0;
            }
            else if (LevelGenGridUtils.IsFloor(x, y + 1, floor, grid) && !LevelGenGridUtils.IsFloor(x, y - 1, floor, grid))
            {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y);
                obj.transform.Rotate(0, 90, 0);
                northCovered = true;
                LevelGen.instance.exitLocation = new Vector3(x * 5, 0, (y + 1) * 5);
                LevelGen.instance.exitAngle    = 90;
            }
            else if (LevelGenGridUtils.IsFloor(x, y - 1, floor, grid) && !LevelGenGridUtils.IsFloor(x, y + 1, floor, grid))
            {
                //else {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y);
                obj.transform.Rotate(0, 270, 0);
                LevelGen.instance.exitLocation = new Vector3(x * 5, 0, (y - 1) * 5);
                LevelGen.instance.exitAngle    = 270;
                southCovered = true;
            }
        }
        else if (block == "<")
        {
            blockType = "stairsToLast";
            if (LevelGenGridUtils.IsFloor(x + 1, y, floor, grid) && !LevelGenGridUtils.IsFloor(x - 1, y, floor, grid))
            {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x - 1.5f, y);
                obj.transform.Rotate(0, 0, 0);
                westCovered = true;
                LevelGen.instance.entranceLocation = new Vector3(x * 5, 0, y * 5);
                LevelGen.instance.entranceAngle    = 180;
            }
            else if (LevelGenGridUtils.IsFloor(x - 1, y, floor, grid) && !LevelGenGridUtils.IsFloor(x + 1, y, floor, grid))
            {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x + 1.5f, y);
                obj.transform.Rotate(0, 180, 0);
                eastCovered = true;
                LevelGen.instance.entranceLocation = new Vector3(x * 5, 0, y * 5);
                LevelGen.instance.entranceAngle    = 0;
            }
            else if (LevelGenGridUtils.IsFloor(x, y + 1, floor, grid) && !LevelGenGridUtils.IsFloor(x, y - 1, floor, grid))
            {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y - 1.5f);
                obj.transform.Rotate(0, 270, 0);
                northCovered = true;
                LevelGen.instance.entranceLocation = new Vector3(x * 5, 0, y * 5);
                LevelGen.instance.entranceAngle    = 90;
            }
            else if (LevelGenGridUtils.IsFloor(x, y - 1, floor, grid) && !LevelGenGridUtils.IsFloor(x, y + 1, floor, grid))
            {
                //else {
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y + 1.5f);
                obj.transform.Rotate(0, 90, 0);
                southCovered = true;
                LevelGen.instance.entranceLocation = new Vector3(x * 5, 0, y * 5);
                LevelGen.instance.entranceAngle    = 270;
            }
        }
        if (LevelGenGridUtils.IsWallNorth(x, y, floor, grid) && LevelGenGridUtils.IsFloor(x, y - 2, floor, grid) && !northCovered && grid[floor, x, y - 1] != "w")
        {
            blockType = "oneTileWall";
            var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y - 1);
            obj.transform.Rotate(0, 90, 0);
            northCovered          = true;
            grid[floor, x, y - 1] = "w";
        }
        if (LevelGenGridUtils.IsWallSouth(x, y, floor, grid) && LevelGenGridUtils.IsFloor(x, y + 2, floor, grid) && !southCovered)
        {
            southCovered = true;
        }
        if (LevelGenGridUtils.IsWallWest(x, y, floor, grid) && LevelGenGridUtils.IsFloor(x - 2, y, floor, grid) && !westCovered && grid[floor, x - 1, y] != "w")
        {
            blockType = "oneTileWall";
            var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x - 1, y);
            obj.transform.Rotate(0, 0, 0);
            westCovered           = true;
            grid[floor, x - 1, y] = "w";
        }
        if (LevelGenGridUtils.IsWallEast(x, y, floor, grid) && LevelGenGridUtils.IsFloor(x + 2, y, floor, grid) && !eastCovered)
        {
            eastCovered = true;
        }
        if (LevelGenGridUtils.IsFullTileTarget(x + 1, y, floor, grid))
        {
            eastCovered = true;
        }
        if (LevelGenGridUtils.IsFullTileTarget(x - 1, y, floor, grid))
        {
            westCovered = true;
        }
        if (LevelGenGridUtils.IsFullTileTarget(x, y + 1, floor, grid))
        {
            southCovered = true;
        }
        if (LevelGenGridUtils.IsFullTileTarget(x, y - 1, floor, grid))
        {
            northCovered = true;
        }
        if (LevelGenGridUtils.IsReverseCornerTopLeft(x, y, floor, grid))
        {
            blockType = "reverseCornerWall";
            var rotation = 0;
            var obj      = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x - 1.12f, y - 1.12f);
            obj.transform.Rotate(0, rotation, 0);
        }
        if (LevelGenGridUtils.IsReverseCornerTopRight(x, y, floor, grid))
        {
            blockType = "reverseCornerWall";
            var rotation = 270;
            var obj      = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x + 1.12f, y - 1.12f);
            obj.transform.Rotate(0, rotation, 0);
        }
        if (LevelGenGridUtils.IsReverseCornerBottomLeft(x, y, floor, grid))
        {
            blockType = "reverseCornerWall";
            var rotation = 90;
            var obj      = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x - 1.12f, y + 1.12f);
            obj.transform.Rotate(0, rotation, 0);
        }
        if (LevelGenGridUtils.IsReverseCornerBottomRight(x, y, floor, grid))
        {
            blockType = "reverseCornerWall";
            var rotation = 180;
            var obj      = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x + 1.12f, y + 1.12f);
            obj.transform.Rotate(0, rotation, 0);
        }
        if (LevelGenGridUtils.IsCoveredByReverseCorner(x, y, floor, grid))
        {
            if (LevelGenGridUtils.IsWestCoveredByReverseCorner(x, y, floor, grid))
            {
                westCovered = true;
            }
            if (LevelGenGridUtils.IsEastCoveredByReverseCorner(x, y, floor, grid))
            {
                eastCovered = true;
            }
            if (LevelGenGridUtils.IsNorthCoveredByReverseCorner(x, y, floor, grid))
            {
                northCovered = true;
            }
            if (LevelGenGridUtils.IsSouthCoveredByReverseCorner(x, y, floor, grid))
            {
                southCovered = true;
            }
        }

        if (LevelGenGridUtils.IsCorner(x, y, floor, grid, northCovered, eastCovered, southCovered, westCovered))
        {
            var rotation = LevelGenGridUtils.GetCornerRotation(x, y, floor, grid);
            if (rotation == 180)
            {
                westCovered  = true;
                northCovered = true;
            }
            else if (rotation == 270)
            {
                westCovered  = true;
                southCovered = true;
            }
            else if (rotation == 90)
            {
                eastCovered  = true;
                northCovered = true;
            }
            else if (rotation == 0)
            {
                eastCovered  = true;
                southCovered = true;
            }
            blockType = "cornerWall";
            var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y);
            obj.transform.Rotate(0, rotation, 0);
        }
        if (LevelGenGridUtils.IsDoubleWallTarget(x, y, floor, grid))
        {
            blockType = "doubleWall";
            if (LevelGenGridUtils.IsDoubleWallTargetNorth(x, y, floor, grid) && !northCovered)
            {
                northCovered = true;
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x + 0.5f, y);
                obj.transform.Rotate(0, 270, 0);
            }
            if (LevelGenGridUtils.IsDoubleWallTargetEast(x, y, floor, grid) && !eastCovered)
            {
                eastCovered = true;
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y + 0.5f);
                obj.transform.Rotate(0, 180, 0);
            }
            if (LevelGenGridUtils.IsDoubleWallTargetSouth(x, y, floor, grid) && !southCovered)
            {
                southCovered = true;
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x - 0.5f, y);
                obj.transform.Rotate(0, 90, 0);
            }
            if (LevelGenGridUtils.IsDoubleWallTargetWest(x, y, floor, grid) && !westCovered)
            {
                westCovered = true;
                var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y - 0.5f);
                obj.transform.Rotate(0, 0, 0);
            }
        }
        if (LevelGenGridUtils.IsDoubleWallSecondaryTarget(x, y, floor, grid))
        {
            if (LevelGenGridUtils.IsDoubleWallSecondaryTargetNorth(x, y, floor, grid))
            {
                northCovered = true;
            }
            if (LevelGenGridUtils.IsDoubleWallSecondaryTargetSouth(x, y, floor, grid))
            {
                southCovered = true;
            }
            if (LevelGenGridUtils.IsDoubleWallSecondaryTargetEast(x, y, floor, grid))
            {
                eastCovered = true;
            }
            if (LevelGenGridUtils.IsDoubleWallSecondaryTargetWest(x, y, floor, grid))
            {
                westCovered = true;
            }
        }

        blockType = "wall";
        if (LevelGenGridUtils.IsWallNorth(x, y, floor, grid) && !northCovered)
        {
            var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y - 0.7f);
            obj.transform.Rotate(0, 90, 0);
        }
        if (LevelGenGridUtils.IsWallEast(x, y, floor, grid) && !eastCovered)
        {
            var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x + 0.7f, y);
            obj.transform.Rotate(0, 180, 0);
        }
        if (LevelGenGridUtils.IsWallSouth(x, y, floor, grid) && !southCovered)
        {
            var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x, y + 0.7f);
            obj.transform.Rotate(0, 270, 0);
        }
        if (LevelGenGridUtils.IsWallWest(x, y, floor, grid) && !westCovered)
        {
            var obj = InstantiateBlockObject(castleParts[blockType], castleProbabilities[blockType], x - 0.7f, y);
            obj.transform.Rotate(0, 0, 0);
        }
    }
Beispiel #17
0
    private static bool AddCrookedConnector(Room room, Room room2, Coordinates start, Coordinates end, Dungeon building)
    {
        Graph g      = new Graph(room.floor, building.maxDimensions, building.maxDimensions, building.grid);
        Astar astar  = new Astar();
        var   values = astar.SearchUnfilled(g, start, end);
        var   chain  = new List <Coordinates>();
        var   cursor = end;

        while (values.ContainsKey(cursor) && values[cursor] != cursor)
        {
            chain.Add(cursor);
            cursor = values[cursor];
        }
        if (values.ContainsKey(cursor))
        {
            chain.Add(values[cursor]);
        }
        if (chain.Count > 0)
        {
            chain.Reverse();
        }
        var works = true;

        if (chain.Count == 0 || chain[0].x != start.x || chain[0].y != start.y)
        {
            works = false;
        }
        foreach (var entry in chain)
        {
            if (entry.x < 0 || entry.y < 0 || entry.x >= 120 || entry.y >= 120)
            {
                works = false;
                break;
            }
        }
        if (works)
        {
            Corridor newCorridor = null;
            bool     first       = true;
            foreach (var entry in chain)
            {
                if (LevelGenGridUtils.IsFloor(entry.x, entry.y, room.floor, building.grid))
                {
                    continue;
                }
                building.grid[room.floor, entry.x, entry.y] = "x";
                newCorridor = new Corridor();
                if (first)
                {
                    newCorridor.connectedRooms.Add(room);
                    int entranceDirection;
                    int entranceX = room.ClampEntranceX(end.x);
                    int entranceY = room.ClampEntranceY(end.y);
                    if (end.y < entranceY)
                    {
                        entranceDirection = 0;
                    }
                    else if (end.x > entranceX)
                    {
                        entranceDirection = 1;
                    }
                    else if (end.y > entranceY)
                    {
                        entranceDirection = 2;
                    }
                    else
                    {
                        entranceDirection = 3;
                    }
                    room.SetEntrance(end.x, end.y, entranceDirection);
                }
                newCorridor.x     = entry.x;
                newCorridor.y     = entry.y;
                newCorridor.xSize = 1;
                newCorridor.ySize = 1;
                newCorridor.floor = room.floor;
                building.rooms.Add(newCorridor);
                first = false;
            }
            if (newCorridor != null)
            {
                newCorridor.connectedRooms.Add(room2);
            }
            return(true);
        }
        else
        {
            return(false);
        }
    }
Beispiel #18
0
    internal static bool ConnectTwoRooms(int floor, Dungeon building, Room room1, Room room2)
    {
        Graph g              = new Graph(floor, building.maxDimensions, building.maxDimensions, building.grid);
        Astar astar          = new Astar();
        var   startPositions = GetSurrounding(room1);
        var   endPositions   = GetSurrounding(room2);
        var   start          = startPositions[Random.Range(0, startPositions.Count)];
        var   end            = endPositions[Random.Range(0, endPositions.Count)];
        var   values         = astar.SearchUnfilled(g, start, end);
        var   chain          = new List <Coordinates>();
        var   cursor         = end;

        while (values.ContainsKey(cursor) && values[cursor] != cursor)
        {
            chain.Add(cursor);
            cursor = values[cursor];
        }
        if (values.ContainsKey(cursor))
        {
            chain.Add(values[cursor]);
        }
        if (chain.Count > 0)
        {
            chain.Reverse();
        }
        if (chain.Count == 0 || chain[0] != start)
        {
            return(false);
        }
        foreach (var entry in chain)
        {
            if (entry.x < 0 || entry.y < 0 || entry.x >= 120 || entry.y >= 120)
            {
                return(false);
            }
        }
        Corridor newCorridor = null;

        foreach (var entry in chain)
        {
            bool first = true;
            if (LevelGenGridUtils.IsFloor(entry.x, entry.y, room1.floor, building.grid))
            {
                continue;
            }
            building.grid[floor, entry.x, entry.y] = "x";
            newCorridor = new Corridor();
            if (first)
            {
                newCorridor.connectedRooms.Add(room1);
                int entranceDirection;
                int entranceX = room1.ClampEntranceX(end.x);
                int entranceY = room1.ClampEntranceY(end.y);
                if (end.y < entranceY)
                {
                    entranceDirection = 0;
                }
                else if (end.x > entranceX)
                {
                    entranceDirection = 1;
                }
                else if (end.y > entranceY)
                {
                    entranceDirection = 2;
                }
                else
                {
                    entranceDirection = 3;
                }
                room1.SetEntrance(end.x, end.y, entranceDirection);
            }
            newCorridor.x     = entry.x;
            newCorridor.y     = entry.y;
            newCorridor.xSize = 1;
            newCorridor.ySize = 1;
            newCorridor.floor = floor;
            building.rooms.Add(newCorridor);
            first = false;
        }
        if (newCorridor != null)
        {
            newCorridor.connectedRooms.Add(room2);
        }
        return(true);
    }