Ejemplo n.º 1
0
    private List <Room> TryCreateBranch(Room roomToBranch, Room currentRoom, int numConnRooms)
    {
        int   tries;
        float angle;
        var   currentBranch = new List <Room>();

        for (int j = 0; j < numConnRooms; j++)
        {
            var connRoom = new ConnectionRoom();
            tries = 3;
            do
            {
                tries--;
                angle = AttemptToPlaceRoom(currentRoom, connRoom, SeededRandom.RandomDirection());
            } while (tries > 0 && angle == -1);

            if (angle == -1)    //If we failed to place the connecting room, remove all rooms on this branch
            {
                foreach (var branchRoom in currentBranch)
                {
                    branchRoom.Reset();
                    PlacedRooms.Remove(branchRoom);
                }
                currentBranch.Clear();
                return(currentBranch);
            }
            else
            {
                currentRoom = connRoom;
                currentBranch.Add(connRoom);
                PlacedRooms.Add(connRoom);
            }
        }

        tries = 10;
        do
        {
            tries--;
            angle = AttemptToPlaceRoom(currentRoom, roomToBranch, SeededRandom.RandomDirection());
        } while (tries > 0 && angle == -1);

        if (angle == -1)
        {
            roomToBranch.Reset();
            foreach (var branchRoom in currentBranch)
            {
                branchRoom.Reset();
                PlacedRooms.Remove(branchRoom);
            }
            currentBranch.Clear();
            return(currentBranch);
        }
        else
        {
            currentBranch.Add(roomToBranch);
            PlacedRooms.Add(roomToBranch);
        }
        return(currentBranch);
    }
Ejemplo n.º 2
0
    public override bool Build(List <Room> rooms)
    {
        this.Rooms = rooms;
        InitBuilder();

        if (entrance == null || exit == null)
        {
            return(false);
        }

        float direction = SeededRandom.RandomDirection();

        entrance.SetSize();
        entrance.Bounds.SetPosition(0, 0);
        PlacedRooms.Add(entrance);
        branchable.Add(entrance);

        int roomsOnPath = (int)(multiConnections.Count * pathLengthPercentage) + SeededRandom.PickRandom(pathLengthJitters);

        roomsOnPath = Math.Min(roomsOnPath, multiConnections.Count);
        float pathVariance = 15;

        var   curr = entrance;
        float res;
        int   rejected = 0;

        for (int i = 0; i < roomsOnPath; i++)
        {
            for (int j = 0; j < SeededRandom.PickRandom(pathConnectionLength); j++)
            {
                var connectionRoom = new ConnectionRoom();
                res = AttemptToPlaceRoom(curr, connectionRoom, direction + SeededRandom.Range(-pathVariance, pathVariance));
                if (res != -1)
                {
                    PlacedRooms.Add(connectionRoom);
                    branchable.Add(connectionRoom);
                    curr = connectionRoom;
                }
                else
                {
                    rejected++;
                }
            }
            var next = multiConnections[i];
            res = AttemptToPlaceRoom(curr, next, direction + SeededRandom.Range(-pathVariance, pathVariance));
            if (res != -1)
            {
                PlacedRooms.Add(next);
                branchable.Add(next);
                curr = next;
            }
            else
            {
                rejected++;
            }
        }
        for (int j = 0; j < SeededRandom.PickRandom(pathConnectionLength); j++)
        {
            var connectionRoom = new ConnectionRoom();
            res = AttemptToPlaceRoom(curr, connectionRoom, direction + SeededRandom.Range(-pathVariance, pathVariance));
            if (res != -1)
            {
                PlacedRooms.Add(connectionRoom);
                branchable.Add(connectionRoom);
                curr = connectionRoom;
            }
            else
            {
                rejected++;
            }
        }

        if (!PlaceExit(curr, direction, pathVariance))
        {
            return(false);
        }

        var roomsToBranch = GetRoomsToBranch(roomsOnPath);

        CreateBranches(roomsToBranch);

        FindNeighbours();

        return(true);
    }
Ejemplo n.º 3
0
    public override bool Build(List <Room> rooms)
    {
        this.Rooms = rooms;
        InitBuilder();

        if (entrance == null || exit == null)
        {
            return(false);
        }

        float startAngle = SeededRandom.RandomDirection();

        entrance.SetSize();
        entrance.Bounds.SetPosition(0, 0);
        PlacedRooms.Add(entrance);
        branchable.Add(entrance);

        var loop        = new List <Room>();
        int roomsOnPath = (int)(multiConnections.Count * pathLengthPercentage) + SeededRandom.PickRandom(pathLengthJitters);

        roomsOnPath = Math.Min(roomsOnPath, multiConnections.Count);

        for (int i = 0; i <= roomsOnPath; i++)
        {
            if (i == 0)
            {
                loop.Add(entrance);
            }
            else
            {
                loop.Add(multiConnections[0]);
                multiConnections.RemoveAt(0);
            }
            for (int j = 0; j < SeededRandom.PickRandom(pathConnectionLength); j++)
            {
                var newRoom = new ConnectionRoom();
                loop.Add(newRoom);
            }
        }

        loop.Insert((loop.Count + 1) / 2, exit);

        var   prev = entrance;
        float targetAngle;

        for (int i = 1; i < loop.Count; i++)
        {
            var curr = loop[i];
            targetAngle = startAngle + TargetAngle(i / (float)loop.Count);
            var res = AttemptToPlaceRoom(prev, curr, targetAngle);
            if (res != -1)
            {
                PlacedRooms.Add(curr);
                prev = curr;
                branchable.Add(curr);
            }
            else if (!(curr is ConnectionRoom))
            {
                //Little chance dependant, could be better
                return(false);
            }
        }

        //While not connected with the entrance, place connection rooms to connect
        //Also chance dependent. Should find better way to handle
        while (!prev.Connect(entrance))
        {
            var c = new ConnectionRoom();
            var angleToEntrance = GetAngleBetweenRooms(prev, entrance);
            if (AttemptToPlaceRoom(prev, c, angleToEntrance) == -1)
            {
                return(false);
            }
            loop.Add(c);
            PlacedRooms.Add(c);
            branchable.Add(c);
            prev = c;
        }

        UnityEngine.Debug.Log($"There are {loop.Count} rooms in the loop");

        //Pass in 0 because we remove the rooms from multiconnection
        var roomsToBranch = GetRoomsToBranch(0);

        CreateBranches(roomsToBranch);

        FindNeighbours();

        return(true);
    }