private static bool CreateBrush(
            Vector2 minBrushSize,
            Vector2 maxBrushSize,
            Vector2 startingPosition,
            Vector2 corridorDirection,
            Dungeon dungeon,
            Room room,
            ref Rect brush)
        {
            //make sure a segment with the given dimensions won't go over the room bounds
            brush.width = Mathf.RoundToInt(Dungeon.Random.Range(minBrushSize.x, maxBrushSize.x - Mathf.Min(0, brush.x - startingPosition.x)));
            brush.height = Mathf.RoundToInt(Dungeon.Random.Range(minBrushSize.y, maxBrushSize.y - Mathf.Min(0, brush.y - startingPosition.y)));

            Rect brushPerimeter = new Rect(brush);
            Vector2 offset = corridorDirection.YX();
            brushPerimeter.min -= offset;
            brushPerimeter.max += offset;

            bool hadToExpandBrush = !room.Size.IsZero() && !room.Bounds.Overlaps(brushPerimeter);
            //make sure this new part will be connected to the room!
            while (!corridorDirection.IsZero() && hadToExpandBrush && !dungeon.OverlapsCorridor(brush.position, brush.size))
            {
                brush.width += corridorDirection.y;
                brush.height += corridorDirection.x;
                hadToExpandBrush = !room.Bounds.Overlaps(brush);
            }

            //make sure this new part won't go over a corridor!
            while (!corridorDirection.IsZero()
                    && brush.width > minBrushSize.x
                    && brush.height > minBrushSize.y
                    && dungeon.OverlapsCorridor(brush.position, brush.size))
            {
                if (hadToExpandBrush) //there's no point in reducing the brush if it had to be expanded to begin with!
                    return false;
                brush.width -= corridorDirection.y;
                brush.height -= corridorDirection.x;
            }

            brush.width = Mathf.Min(brush.width, maxBrushSize.x);
            brush.height = Mathf.Min(brush.height, maxBrushSize.y);

            return true;
        }
        public static bool CarveRoom(Dungeon dungeon, Corridor corridor, Vector4 brushSizeVariation, Vector2 maxRoomSize, int maxBrushStrokes, out Room room)
        {
            Vector2 startingPosition = corridor ? corridor.Bounds.max : Vector2.zero;
            Vector2 corridorDirection = corridor ? corridor.Direction : Vector2.zero;
            Vector2 minBrushSize = brushSizeVariation.XY();
            Vector2 maxBrushSize = brushSizeVariation.ZW();

            Rect brush = new Rect(startingPosition, Vector2.zero);

            //move the room one up or to the left so it aligns properly to the corridor
            brush.x -= corridorDirection.y;
            brush.y -= corridorDirection.x;

            room = Room.CreateInstance(brush.position, Dungeon.Random);

            if (brush.x < 0 || brush.y < 0
                || !AdjustCoordinate(corridorDirection, minBrushSize, dungeon.Size, ref brush))
            {
                return false;
            }

            bool enoughSpaceForRoom = !dungeon.OverlapsCorridor(brush.position, minBrushSize);

            while (!corridorDirection.IsZero()
                    && !enoughSpaceForRoom //if the room is currently intersecting a corridor
                    && ((corridorDirection.y != 0 && brush.x >= 0 && brush.x + minBrushSize.x - 1 > room.Bounds.x)  //and it can be moved to the left (orUp)
                    || (corridorDirection.x != 0 && brush.y >= 0 && brush.y + minBrushSize.y - 1 > room.Bounds.y))) //while still being attached to the corridor
            {
                //move the room and check again
                brush.x -= corridorDirection.y;
                brush.y -= corridorDirection.x;
                enoughSpaceForRoom = !dungeon.OverlapsCorridor(brush.position, minBrushSize);//!dungeon.SearchInArea(pos, minSize, CellType.corridor);
            }

            if (!enoughSpaceForRoom) //if a room with the minimum size possible would still intersect a corridor, stop trying to make it
                return false;

            brush.x = Mathf.Clamp(brush.x, 0, dungeon.Width);
            brush.y = Mathf.Clamp(brush.y, 0, dungeon.Height);

            bool roomCarved = false;
            //mark cells at random locations within the room, until the maximum tries is reached
            for (int tries = 0; tries < maxBrushStrokes; tries++)
            {

                if (CreateBrush(
                    minBrushSize,
                    maxBrushSize,
                    startingPosition,
                    corridorDirection,
                    dungeon,
                    room,
                    ref brush))
                {
                    if (!brush.size.IsZero())
                    {
                        if (!dungeon.OverlapsCorridor(brush.position, brush.size))
                        {
                            if (AddCells(brush, dungeon, room) > 0)
                                roomCarved = true;
                        }
                    }
                }
                MoveBrush(dungeon, startingPosition, corridorDirection, room, minBrushSize, maxBrushSize, ref brush);
            }

            if (room.Position.x < 0 || room.Position.y < 0)
                throw new ArgumentOutOfRangeException("Room was carved outside of dungeon!\n" + room);

            return roomCarved;
        }