예제 #1
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);
        }
    }
예제 #2
0
    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 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);
 }
 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);
 }
예제 #5
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);
    }
    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);
        }
    }
예제 #7
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);
        }
    }
예제 #8
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);
    }