Example #1
0
        private void GenerateCorridors(ShaftBuilder mainShaft, int numberStories)
        {
            if (_corridorsByShaft.TryGetValue(mainShaft, out var corridors))
            {
                corridors.Clear();
            }
            else
            {
                corridors = new List <CorridorBuilder>();
            }

            var shaftLeftSide  = mainShaft.GetMaximalValue(Directions.Left);
            var shaftRightSide = mainShaft.GetMaximalValue(Directions.Right);

            var shaftY = mainShaft.GetMaximalValue(Directions.Down);

            for (var story = 0; story < Mathf.Abs(numberStories); story++)
            {
                var yPosition = GetYPositionForStory(shaftY, story);

                if (Chance.CoinFlip)
                {
                    var corridor = AddCorridor(new IntVector2(shaftLeftSide, yPosition), Directions.Left);
                    corridor.AddModifier(ModifierTypes.Laboratory);
                    var room = AddRoom(new IntVector2(corridor.GetMaximalValue(Directions.Left), yPosition), Directions.Left);
                    room.AddModifier(ModifierTypes.Laboratory);
                    corridors.Add(corridor);
                    _connectionsByCorridor.Add(corridor, room);
                }
                if (Chance.CoinFlip)
                {
                    var corridor = AddCorridor(new IntVector2(shaftRightSide, yPosition), Directions.Right);
                    corridor.AddModifier(ModifierTypes.Laboratory);
                    var room = AddRoom(new IntVector2(corridor.GetMaximalValue(Directions.Right), yPosition), Directions.Right);
                    room.AddModifier(ModifierTypes.Laboratory);
                    corridors.Add(corridor);
                    _connectionsByCorridor.Add(corridor, room);
                }
            }

            _corridorsByShaft[mainShaft] = corridors;

            OnSpaceBuilderChanged.Raise(this);
        }
Example #2
0
        private void GenerateSecondaryShafts(ShaftBuilder mainShaft, int height)
        {
            if (_secondaryShaftsByShaft.TryGetValue(mainShaft, out var shafts))
            {
                shafts.Clear();
            }
            else
            {
                shafts = new List <ShaftBuilder>();
            }

            var shaftY = mainShaft.GetMaximalValue(Directions.Down);
            var shaftX = mainShaft.GetMaximalValue(Directions.Left);

            for (var corridorSegment = -_maximumCorridorSegments; corridorSegment < _maximumCorridorSegments; corridorSegment++)
            {
                if (corridorSegment == 0)
                {
                    continue;                       // Main shaft...
                }
                var xPosition = (CORRIDOR_SEGMENT_LENGTH * corridorSegment) + shaftX;

                var validStories = new List <int>();

                for (var story = 0; story < Mathf.Abs(height); story++)
                {
                    var yPosition = GetYPositionForStory(shaftY, story);

                    var corridorPosition = new IntVector2(xPosition, yPosition);

                    if (_corridorsByShaft[mainShaft].Any(corridor => corridor.Contains(corridorPosition)))
                    {
                        validStories.Add(story);
                    }
                }

                if (validStories.Count > 0)
                {
                    if (corridorSegment == -_maximumCorridorSegments ||
                        corridorSegment == _maximumCorridorSegments)
                    {
                        var storyStartForShaft = validStories.RandomItem();
                        var yPosition          = GetYPositionForStory(shaftY, storyStartForShaft);
                        var newShaftHeight     = Chance.Range(-10, 10);

                        var shaft = AddShaft(storyStartForShaft, storyStartForShaft + newShaftHeight, new IntVector2(xPosition, yPosition));
                        RegisterNewMainShaft(shaft, newShaftHeight);

                        validStories.Remove(storyStartForShaft);
                    }
                    else if (validStories.Contains(0))
                    {
                        var newShaftHeight = Chance.Range(-10, -4);

                        var shaft = AddShaft(0, newShaftHeight, new IntVector2(xPosition, shaftY));
                        RegisterNewMainShaft(shaft, newShaftHeight);
                        validStories.Remove(0);
                    }
                    else if (validStories.Contains(height))
                    {
                        var yPosition      = GetYPositionForStory(shaftY, height);
                        var newShaftHeight = Chance.Range(4, 10);

                        var shaft = AddShaft(height, height + newShaftHeight, new IntVector2(xPosition, yPosition));
                        RegisterNewMainShaft(shaft, newShaftHeight);
                        validStories.Remove(height);
                    }

                    // At least two stories we can reach...
                    while (validStories.Count > 1)
                    {
                        var firstStory  = validStories.RandomItem();
                        var secondStory = validStories.RandomItem(firstStory);

                        var shaftPosition = new IntVector2(xPosition, GetYPositionForStory(shaftY, firstStory));

                        var shaft = AddShaft(Mathf.Min(firstStory, secondStory), Mathf.Max(firstStory, secondStory), shaftPosition);

                        if (!shafts.Any(otherShaft => otherShaft.IntersectsWith(shaft)))
                        {
                            shaft.AddModifier(ModifierTypes.Laboratory);
                            shafts.Add(shaft);
                        }

                        foreach (var corridor in _corridorsByShaft[mainShaft])
                        {
                            if (shaft.IntersectsWith(_connectionsByCorridor[corridor]))
                            {
                                _connectionsByCorridor[corridor] = null;
                            }
                        }

                        validStories.Remove(firstStory);
                        validStories.Remove(secondStory);
                    }
                }
            }

            _secondaryShaftsByShaft[mainShaft] = shafts;
            OnSpaceBuilderChanged.Raise(this);
        }