private RoomDescriptionGrid2D GetStartEndRoomDescription()
    {
        var roomsTemplates = new List <RoomTemplateGrid2D>();
        var doors          = new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 2);

        System.Random rnd = new System.Random();

        var transformations = new List <TransformationGrid2D>()
        {
            TransformationGrid2D.Identity
        };


        var roomTemplate = new RoomTemplateGrid2D(
            PolygonGrid2D.GetRectangle(roomMinWidth, roomMinHeight),
            doors,
            allowedTransformations: transformations);

        roomsTemplates.Add(roomTemplate);


        var roomDescription = new RoomDescriptionGrid2D
                              (
            isCorridor: false,
            roomTemplates: roomsTemplates
                              );

        return(roomDescription);
    }
        protected void DrawTextOntoPolygon(PolygonGrid2D polygon, string text, float penWidth)
        {
            var partitions        = polygonPartitioning.GetPartitions(polygon);
            var orderedRectangles = partitions.OrderBy(x => Vector2Int.ManhattanDistance(x.Center, polygon.BoundingRectangle.Center)).ToList();
            var targetRectangle   = orderedRectangles.First();

            if (orderedRectangles.Any(x => x.Width > 6 && x.Height > 3))
            {
                targetRectangle = orderedRectangles.First(x => x.Width > 6 && x.Height > 3);
            }

            using (var font = new Font("Baskerville Old Face", penWidth, FontStyle.Bold, GraphicsUnit.Pixel))
            {
                var rect = new RectangleF(
                    targetRectangle.A.X,
                    targetRectangle.A.Y,
                    targetRectangle.Width,
                    targetRectangle.Height
                    );

                var sf = new StringFormat
                {
                    LineAlignment = StringAlignment.Center,
                    Alignment     = StringAlignment.Center
                };

                graphics.DrawString(text, font, Brushes.Black, rect, sf);
            }
        }
        /// <summary>
        /// Checks the doors of the room template.
        /// </summary>
        /// <param name="outline"></param>
        /// <param name="doorMode"></param>
        /// <returns></returns>
        public static ActionResult CheckDoors(PolygonGrid2D outline, IDoorModeGrid2D doorMode)
        {
            var result = new ActionResult();

            try
            {
                var doors = doorMode.GetDoors(outline);

                if (doors.Count == 0)
                {
                    if (doorMode is SimpleDoorModeGrid2D)
                    {
                        result.AddError($"The simple door mode is used but there are no valid door positions. Try to decrease door length and/or corner distance.");
                    }
                    else
                    {
                        result.AddError($"The manual door mode is used but no doors were added.");
                    }
                }
            }
            // TODO: this is not optimal - the argument exception might be something different than invalid manual doors
            catch (ArgumentException e)
            {
                if (doorMode is ManualDoorModeGrid2D)
                {
                    result.AddError($"It seems like some of the manual doors are not located on the outline of the room template.");
                }
                else
                {
                    result.AddError(e.Message);
                }
            }

            return(result);
        }
        public void ComputeAverageRoomTemplatesEntropy_BasicTest()
        {
            var transformations = TransformationGrid2DHelper.GetAllTransformationsOld().ToList();
            var roomTemplate1   = new RoomTemplate(PolygonGrid2D.GetSquare(10), new SimpleDoorMode(1, 0), transformations);
            var roomTemplate2   = new RoomTemplate(PolygonGrid2D.GetRectangle(5, 10), new SimpleDoorMode(1, 0), transformations);

            var roomDescription1 = new BasicRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate1, roomTemplate2
            });

            var mapDescription = new MapDescription <int>();

            mapDescription.AddRoom(0, roomDescription1);
            mapDescription.AddRoom(1, roomDescription1);
            mapDescription.AddConnection(0, 1);

            var dungeonGenerator = new DungeonGenerator <int>(mapDescription);

            dungeonGenerator.InjectRandomGenerator(new Random(0));

            var layouts = new List <MapLayout <int> >();

            for (int i = 0; i < 10; i++)
            {
                layouts.Add(dungeonGenerator.GenerateLayout());
            }

            var entropy = entropyCalculator.ComputeAverageRoomTemplatesEntropy(mapDescription, layouts);

            Assert.That(entropy, Is.GreaterThanOrEqualTo(0));
            Assert.That(entropy, Is.LessThanOrEqualTo(1));
        }
Beispiel #5
0
        public void BoudingRectangle_ReturnsBoundingBox()
        {
            {
                var p = PolygonGrid2D.GetRectangle(2, 4);

                var boundingRectangle = p.BoundingRectangle;
                var expected          = new RectangleGrid2D(new Vector2Int(0, 0), new Vector2Int(2, 4));

                Assert.AreEqual(expected, boundingRectangle);
            }

            {
                var p = new PolygonGrid2DBuilder()
                        .AddPoint(0, 0)
                        .AddPoint(0, 6)
                        .AddPoint(3, 6)
                        .AddPoint(3, 3)
                        .AddPoint(7, 3)
                        .AddPoint(7, 0)
                        .Build();

                var boundingRectangle = p.BoundingRectangle;
                var expected          = new RectangleGrid2D(new Vector2Int(0, 0), new Vector2Int(7, 6));

                Assert.AreEqual(expected, boundingRectangle);
            }
        }
Beispiel #6
0
        public static List <RoomTemplate> GetCorridorRoomTemplates(List <int> offsets, int width = 1)
        {
            if (offsets == null)
            {
                return(null);
            }

            var roomTemplates   = new List <RoomTemplate>();
            var transformations = TransformationGrid2DHelper.GetAllTransformationsOld().ToList();

            foreach (var offset in offsets)
            {
                var length       = offset;
                var roomTemplate = new RoomTemplate(
                    PolygonGrid2D.GetRectangle(length, width),
                    new ManualDoorMode(new List <OrthogonalLineGrid2D>()
                {
                    new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(0, width)),
                    new OrthogonalLineGrid2D(new Vector2Int(length, 0), new Vector2Int(length, width)),
                }),
                    transformations
                    );

                roomTemplates.Add(roomTemplate);
            }

            return(roomTemplates);
        }
    private RoomDescriptionGrid2D GetRoomDescription()
    {
        var roomsTemplates = new List <RoomTemplateGrid2D>();
        var doors          = new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 2);

        System.Random rnd             = new System.Random();
        var           roomWidthArray  = Enumerable.Range(roomMinWidth, roomMaxWidth - roomMinWidth + 1).ToArray();
        var           roomHieghtArray = Enumerable.Range(roomMinHeight, roomMaxHeight - roomMinHeight + 1).ToArray();

        var transformations = new List <TransformationGrid2D>()
        {
            TransformationGrid2D.Identity
        };

        for (var i = 0; i <= numberOfRooms; i++)
        {
            var roomTemplate = new RoomTemplateGrid2D(
                PolygonGrid2D.GetRectangle(roomWidthArray.OrderBy(a => UnityEngine.Random.value).FirstOrDefault(), roomHieghtArray.OrderBy(a => UnityEngine.Random.value).FirstOrDefault()),
                doors,
                allowedTransformations: transformations);

            roomsTemplates.Add(roomTemplate);
        }

        var roomDescription = new RoomDescriptionGrid2D
                              (
            isCorridor: false,
            roomTemplates: roomsTemplates
                              );

        return(roomDescription);
    }
Beispiel #8
0
        protected void DrawRoomBackground(PolygonGrid2D polygon, Color color)
        {
            var polyPoints = polygon.GetPoints().Select(point => new Point(point.X, point.Y)).ToList();

            graphics.FillPolygon(new SolidBrush(color), polyPoints.ToArray());
            graphics.DrawPolygon(new Pen(color, 0.1f), polyPoints.ToArray());
        }
Beispiel #9
0
        public static List <RoomTemplateGrid2D> GetNewCorridorRoomTemplates(List <int> offsets, int width = 1)
        {
            if (offsets == null)
            {
                return(null);
            }

            var roomTemplates   = new List <RoomTemplateGrid2D>();
            var transformations = TransformationGrid2DHelper.GetAllTransformationsOld().ToList();

            foreach (var offset in offsets)
            {
                var length       = offset;
                var roomTemplate = new RoomTemplateGrid2D(
                    PolygonGrid2D.GetRectangle(length, width),
                    new ManualDoorModeGrid2D(new List <DoorGrid2D>()
                {
                    new DoorGrid2D(new Vector2Int(0, 0), new Vector2Int(0, width)),
                    new DoorGrid2D(new Vector2Int(length, 0), new Vector2Int(length, width)),
                }),
                    allowedTransformations: transformations,
                    repeatMode: RoomTemplateRepeatMode.AllowRepeat
                    );

                roomTemplates.Add(roomTemplate);
            }

            return(roomTemplates);
        }
        public void OverlapArea_NonTouching_ReturnsZero()
        {
            var r1 = PolygonGrid2D.GetSquare(6);
            var r2 = PolygonGrid2D.GetRectangle(2, 8);

            Assert.AreEqual(0, polygonOverlap.OverlapArea(r1, new Vector2Int(0, 0), r2, new Vector2Int(7, 2)));
        }
        public void OverlapArea_TwoSquares()
        {
            var r1 = PolygonGrid2D.GetSquare(6);
            var r2 = PolygonGrid2D.GetSquare(3);

            Assert.AreEqual(6, polygonOverlap.OverlapArea(r1, new Vector2Int(0, 0), r2, new Vector2Int(2, -1)));
        }
Beispiel #12
0
        private List <RoomTemplate> GetMediumRoomTemplates()
        {
            var roomTemplates = new List <RoomTemplate>();
            var doorMode      = new SimpleDoorMode(2, 2);

            roomTemplates.Add(new RoomTemplate(PolygonGrid2D.GetSquare(12), doorMode, transformations, name: "Square 12x12", repeatMode: repeatMode));
            roomTemplates.Add(new RoomTemplate(PolygonGrid2D.GetSquare(14), doorMode, transformations, name: "Square 14x14", repeatMode: repeatMode));
            roomTemplates.Add(new RoomTemplate(PolygonGrid2D.GetRectangle(10, 14), doorMode, transformations, name: "Rectangle 10x14", repeatMode: repeatMode));
            roomTemplates.Add(new RoomTemplate(PolygonGrid2D.GetRectangle(12, 15), doorMode, transformations, name: "Rectangle 12x15", repeatMode: repeatMode));

            //roomTemplates.Add(new RoomTemplate(
            //    new GridPolygonBuilder()
            //        .AddPoint(0, 0)
            //        .AddPoint(0, 16)
            //        .AddPoint(8, 16)
            //        .AddPoint(8, 8)
            //        .AddPoint(16, 8)
            //        .AddPoint(16, 0)
            //        .Build()
            //    , doorMode, transformations, name: "L-shape large", repeatMode: RepeatMode.NoRepeat));

            if (enhanceRoomTemplates)
            {
                roomTemplates.Add(new RoomTemplate(PolygonGrid2D.GetSquare(13), doorMode, transformations, name: "Square 13x13", repeatMode: repeatMode));
                roomTemplates.Add(new RoomTemplate(PolygonGrid2D.GetRectangle(10, 16), doorMode, transformations, name: "Rectangle 10x16", repeatMode: repeatMode));
            }

            return(roomTemplates);
        }
 private static List <RoomTemplate> GetRoomTemplates()
 {
     return(new List <RoomTemplate>()
     {
         new RoomTemplate(PolygonGrid2D.GetSquare(10), new SimpleDoorMode(1, 1)),
     });
 }
Beispiel #14
0
        public void Generate_BasicTest()
        {
            var roomTemplate1 = new RoomTemplate(PolygonGrid2D.GetSquare(10), new SimpleDoorMode(1, 0), TransformationGrid2DHelper.GetAllTransformationsOld().ToList());
            var roomTemplate2 = new RoomTemplate(PolygonGrid2D.GetRectangle(5, 10), new SimpleDoorMode(1, 0), TransformationGrid2DHelper.GetAllTransformationsOld().ToList());

            var roomDescription1 = new BasicRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate1
            });
            var roomDescription2 = new BasicRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate1, roomTemplate2
            });

            var mapDescription = new MapDescription <int>();

            mapDescription.AddRoom(0, roomDescription1);
            mapDescription.AddRoom(1, roomDescription2);
            mapDescription.AddConnection(0, 1);

            var configurationSpaces = generator.GetConfigurationSpaces <Configuration <CorridorsData> >(mapDescription);

            Assert.That(configurationSpaces.GetShapesForNode(0).Count, Is.EqualTo(1));
            Assert.That(configurationSpaces.GetShapesForNode(1).Count, Is.EqualTo(3));
            Assert.That(configurationSpaces.GetAllShapes().Count, Is.EqualTo(3));
        }
Beispiel #15
0
        /// <summary>
        /// Draws a given layout to an output using a given scale and offset.
        /// </summary>
        /// <remarks>
        /// All points are tranfosmer using the TransformPoint method.
        /// </remarks>
        /// <param name="layout">Layout do be drawn</param>
        /// <param name="scale">Scale factor</param>
        /// <param name="offset"></param>
        /// <param name="withNames">Whether names should be displayed</param>
        /// <param name="fixedFontSize"></param>
        protected void DrawLayout(MapLayout <TNode> layout, float scale, Vector2Int offset, bool withNames, int?fixedFontSize = null)
        {
            var polygons = layout.Rooms.Select(x => x.Shape + x.Position).ToList();
            var rooms    = layout.Rooms.ToList();
            var minWidth = layout.Rooms.Where(x => !x.IsCorridor).Select(x => x.Shape + x.Position).Min(x => x.BoundingRectangle.Width);

            for (var i = 0; i < rooms.Count; i++)
            {
                var room    = rooms[i];
                var outline = GetOutline(polygons[i], room.Doors?.ToList())
                              .Select(x => Tuple.Create(TransformPoint(x.Item1, scale, offset), x.Item2)).ToList();

                var transformedPoints = polygons[i].GetPoints().Select(point => TransformPoint(point, scale, offset)).ToList();

                if (transformedPoints.All(x => x == new Vector2Int(0, 0)))
                {
                    throw new InvalidOperationException("One of the polygons could not be drawn because the canvas size is too small.");
                }

                var polygon = new PolygonGrid2D(transformedPoints);
                DrawRoom(polygon, outline, 2);

                if (withNames && !room.IsCorridor)
                {
                    DrawTextOntoPolygon(polygon, room.Node.ToString(), fixedFontSize ?? 2.5f * minWidth);
                }
            }
        }
Beispiel #16
0
        public void Rectangle_LengthZeroCorners()
        {
            var polygon = PolygonGrid2D.GetRectangle(3, 5);
            var mode    = new ManualDoorMode(new List <OrthogonalLineGrid2D>()
            {
                new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(0, 0)),
                new OrthogonalLineGrid2D(new Vector2Int(0, 5), new Vector2Int(0, 5)),
                new OrthogonalLineGrid2D(new Vector2Int(3, 5), new Vector2Int(3, 5)),
                new OrthogonalLineGrid2D(new Vector2Int(3, 0), new Vector2Int(3, 0)),
            });
            var doorPositions = overlapModeHandler.GetDoorPositions(polygon, mode);

            var expectedPositions = new List <DoorLine>()
            {
                new DoorLine(new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(0, 0), OrthogonalLineGrid2D.Direction.Left), 0),
                new DoorLine(new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(0, 0), OrthogonalLineGrid2D.Direction.Top), 0),
                new DoorLine(new OrthogonalLineGrid2D(new Vector2Int(0, 5), new Vector2Int(0, 5), OrthogonalLineGrid2D.Direction.Top), 0),
                new DoorLine(new OrthogonalLineGrid2D(new Vector2Int(0, 5), new Vector2Int(0, 5), OrthogonalLineGrid2D.Direction.Right), 0),
                new DoorLine(new OrthogonalLineGrid2D(new Vector2Int(3, 5), new Vector2Int(3, 5), OrthogonalLineGrid2D.Direction.Right), 0),
                new DoorLine(new OrthogonalLineGrid2D(new Vector2Int(3, 5), new Vector2Int(3, 5), OrthogonalLineGrid2D.Direction.Bottom), 0),
                new DoorLine(new OrthogonalLineGrid2D(new Vector2Int(3, 0), new Vector2Int(3, 0), OrthogonalLineGrid2D.Direction.Bottom), 0),
                new DoorLine(new OrthogonalLineGrid2D(new Vector2Int(3, 0), new Vector2Int(3, 0), OrthogonalLineGrid2D.Direction.Left), 0),
            };

            Assert.IsTrue(doorPositions.SequenceEqualWithoutOrder(expectedPositions));
        }
Beispiel #17
0
        protected override void DrawRoom(PolygonGrid2D polygon, List <Tuple <Vector2Int, bool> > outline, float penWidth)
        {
            var polyPoints = polygon.GetPoints().Select(point => new Point(point.X, point.Y)).ToList();

            graphics.FillPolygon(Brushes.LightGray, polyPoints.ToArray());

            var lastPoint = outline[outline.Count - 1].Item1;
            var pen       = new Pen(Color.Black, penWidth)
            {
                EndCap   = LineCap.Flat,
                StartCap = LineCap.Flat
            };

            foreach (var pair in outline)
            {
                var point = pair.Item1;

                if (pair.Item2)
                {
                    graphics.DrawLine(pen, lastPoint.X, lastPoint.Y, point.X, point.Y);
                }

                lastPoint = point;
            }
        }
        public void OverlapArea_TwoRectangles()
        {
            var r1 = PolygonGrid2D.GetRectangle(4, 6);
            var r2 = PolygonGrid2D.GetRectangle(5, 3);

            Assert.AreEqual(9, polygonOverlap.OverlapArea(r1, new Vector2Int(0, 0), r2, new Vector2Int(1, 2)));
        }
Beispiel #19
0
        private List <Tuple <Vector2Int, bool> > GetOutlineOld(PolygonGrid2D polygon, List <OrthogonalLineGrid2D> doorLines)
        {
            var outline = new List <Tuple <Vector2Int, bool> >();

            doorLines = doorLines?.ToList();

            foreach (var line in polygon.GetLines())
            {
                AddToOutline(Tuple.Create(line.From, true));

                if (doorLines == null)
                {
                    continue;
                }

                var doorDistances = doorLines.Select(x =>
                                                     new Tuple <OrthogonalLineGrid2D, int>(x, Math.Min(line.Contains(x.From), line.Contains(x.To)))).ToList();
                doorDistances.Sort((x1, x2) => x1.Item2.CompareTo(x2.Item2));

                foreach (var pair in doorDistances)
                {
                    if (pair.Item2 == -1)
                    {
                        continue;
                    }

                    var doorLine = pair.Item1;

                    if (line.Contains(doorLine.From) != pair.Item2)
                    {
                        doorLine = doorLine.SwitchOrientation();
                    }

                    doorLines.Remove(pair.Item1);

                    AddToOutline(Tuple.Create(doorLine.From, true));
                    AddToOutline(Tuple.Create(doorLine.To, false));
                }
            }

            return(outline);

            void AddToOutline(Tuple <Vector2Int, bool> point)
            {
                if (outline.Count == 0)
                {
                    outline.Add(point);
                    return;
                }

                var lastPoint = outline[outline.Count - 1];

                if (!lastPoint.Item2 && point.Item2 && lastPoint.Item1 == point.Item1)
                {
                    return;
                }

                outline.Add(point);
            }
        }
Beispiel #20
0
        public void Run()
        {
            // Create square room template
            var squareRoomTemplate = new RoomTemplateGrid2D(
                PolygonGrid2D.GetSquare(8),
                new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 1)
                );

            // Create rectangle room template
            var rectangleRoomTemplate = new RoomTemplateGrid2D(
                PolygonGrid2D.GetRectangle(6, 10),
                new SimpleDoorModeGrid2D(doorLength: 1, cornerDistance: 1)
                );

            // Create a room description which says that the room is not a corridor and that it can use the two room templates
            var roomDescription = new RoomDescriptionGrid2D(
                isCorridor: false,
                roomTemplates: new List <RoomTemplateGrid2D>()
            {
                squareRoomTemplate, rectangleRoomTemplate
            }
                );

            // Create an instance of the level description
            var levelDescription = new LevelDescriptionGrid2D <int>();

            // Add 4 rooms to the level, use the room description that we created beforehand
            levelDescription.AddRoom(0, roomDescription);
            levelDescription.AddRoom(1, roomDescription);
            levelDescription.AddRoom(2, roomDescription);
            levelDescription.AddRoom(3, roomDescription);

            // Add connections between the rooms - the level graph will be a cycle with 4 vertices
            levelDescription.AddConnection(0, 1);
            levelDescription.AddConnection(0, 3);
            levelDescription.AddConnection(1, 2);
            levelDescription.AddConnection(2, 3);

            // Create an instance of the generate and generate a layout
            var generator = new GraphBasedGeneratorGrid2D <int>(levelDescription);
            var layout    = generator.GenerateLayout();

            // Export the resulting layout as PNG
            var drawer = new DungeonDrawer <int>();

            drawer.DrawLayoutAndSave(layout, "simple_layout.png", new DungeonDrawerOptions()
            {
                Width  = 2000,
                Height = 2000,
            });

            var layout2 = generator.GenerateLayout();

            // Export the resulting layout as PNG
            drawer.DrawLayoutAndSave(layout, "simple_layout_2.png", new DungeonDrawerOptions()
            {
                Width  = 2000,
                Height = 2000,
            });
        }
        public void GetRoomTemplatesDistribution_BasicTest()
        {
            var transformations = TransformationGrid2DHelper.GetAllTransformationsOld().ToList();
            var roomTemplate1   = new RoomTemplate(PolygonGrid2D.GetSquare(2), new SimpleDoorMode(1, 0), transformations);
            var roomTemplate2   = new RoomTemplate(PolygonGrid2D.GetSquare(4), new SimpleDoorMode(1, 0), transformations);
            var roomTemplate3   = new RoomTemplate(PolygonGrid2D.GetSquare(6), new SimpleDoorMode(1, 0), transformations);

            var data = new List <RoomTemplate>()
            {
                roomTemplate1,
                roomTemplate3,
                roomTemplate1,
                roomTemplate1
            };

            var availableRoomTemplates = new List <RoomTemplate>()
            {
                roomTemplate1,
                roomTemplate2,
                roomTemplate3,
            };

            var distribution = entropyCalculator.GetProbabilityDistribution(data, availableRoomTemplates);

            Assert.That(distribution.Count, Is.EqualTo(3));
            Assert.That(distribution[roomTemplate1], Is.EqualTo(3 / 4d));
            Assert.That(distribution[roomTemplate2], Is.EqualTo(0));
            Assert.That(distribution[roomTemplate3], Is.EqualTo(1 / 4d));
        }
        public void BasicTest()
        {
            var roomTemplate1 = new RoomTemplate(PolygonGrid2D.GetSquare(10), new SimpleDoorMode(1, 0));
            var roomTemplate2 = new RoomTemplate(PolygonGrid2D.GetRectangle(5, 10), new SimpleDoorMode(1, 0));

            var roomDescription1 = new BasicRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate1
            });
            var roomDescription2 = new BasicRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate2
            });

            var mapDescription = new MapDescription <string>();

            mapDescription.AddRoom("0", roomDescription1);
            mapDescription.AddRoom("1", roomDescription2);
            mapDescription.AddConnection("0", "1");

            var mapDescriptionMapping = new MapDescriptionMapping <string>(mapDescription);
            var mapping = mapDescriptionMapping.GetMapping();

            Assert.That(mapDescriptionMapping.GetRoomDescription(mapping["0"]), Is.EqualTo(roomDescription1));
            Assert.That(mapDescriptionMapping.GetRoomDescription(mapping["1"]), Is.EqualTo(roomDescription2));
            Assert.That(mapDescriptionMapping.GetGraph().VerticesCount, Is.EqualTo(2));
            Assert.That(mapDescriptionMapping.GetGraph().HasEdge(mapping["0"], mapping["1"]), Is.True);
        }
 public RoomTemplateInstance(RoomTemplate roomTemplate, PolygonGrid2D roomShape, List <TransformationGrid2D> transformations, List <DoorLine> doorLines)
 {
     RoomTemplate    = roomTemplate;
     RoomShape       = roomShape;
     Transformations = transformations;
     DoorLines       = doorLines;
 }
        public void SimpleMapDescriptionTest()
        {
            var roomTemplate1 = new RoomTemplate(PolygonGrid2D.GetSquare(10), new SimpleDoorMode(1, 0), TransformationGrid2DHelper.GetAllTransformationsOld().ToList());
            var roomTemplate2 = new RoomTemplate(PolygonGrid2D.GetRectangle(5, 10), new SimpleDoorMode(1, 0), TransformationGrid2DHelper.GetAllTransformationsOld().ToList());

            var roomDescription1 = new BasicRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate1
            });
            var roomDescription2 = new BasicRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate2
            });

            var mapDescription = new MapDescription <int>();

            mapDescription.AddRoom(0, roomDescription1);
            mapDescription.AddRoom(1, roomDescription2);
            mapDescription.AddConnection(0, 1);

            var dungeonGenerator = new DungeonGenerator <int>(mapDescription);

            dungeonGenerator.InjectRandomGenerator(new Random(0));

            var layout = dungeonGenerator.GenerateLayout();

            Assert.That(layout, Is.Not.Null);
            Assert.That(layout.Rooms.Count(), Is.EqualTo(2));
        }
Beispiel #25
0
        public void GetConfigurationSpaceOverCorridor_SquareRoomSquareCorridor()
        {
            var transformations = TransformationGrid2DHelper.GetAllTransformationsOld().ToList();

            var basicRoomTemplate         = new RoomTemplate(PolygonGrid2D.GetSquare(5), new SimpleDoorMode(1, 0), transformations);
            var basicRoomTemplateInstance = generator.GetRoomTemplateInstances(basicRoomTemplate).First();

            var corridorRoomTemplate = new RoomTemplate(PolygonGrid2D.GetSquare(2), new ManualDoorMode(new List <OrthogonalLineGrid2D>()
            {
                new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(1, 0)),
                new OrthogonalLineGrid2D(new Vector2Int(1, 0), new Vector2Int(2, 0)),
                new OrthogonalLineGrid2D(new Vector2Int(0, 2), new Vector2Int(1, 2)),
                new OrthogonalLineGrid2D(new Vector2Int(1, 2), new Vector2Int(2, 2)),
            }), transformations);
            var corridorRoomTemplateInstances = generator.GetRoomTemplateInstances(corridorRoomTemplate);

            var expectedLines = new List <OrthogonalLineGrid2D>()
            {
                new OrthogonalLineGrid2D(new Vector2Int(-7, -5), new Vector2Int(-7, 5)),
                new OrthogonalLineGrid2D(new Vector2Int(7, -5), new Vector2Int(7, 5)),
                new OrthogonalLineGrid2D(new Vector2Int(-5, 7), new Vector2Int(5, 7)),
                new OrthogonalLineGrid2D(new Vector2Int(-5, -7), new Vector2Int(5, -7)),
            };

            var expectedPoints = expectedLines
                                 .SelectMany(x => x.GetPoints())
                                 .Distinct()
                                 .ToList();

            var configurationSpace = generator.GetConfigurationSpaceOverCorridors(basicRoomTemplateInstance,
                                                                                  basicRoomTemplateInstance, corridorRoomTemplateInstances);

            Assert.That(configurationSpace.Lines.SelectMany(x => x.GetPoints()), Is.EquivalentTo(expectedPoints));
        }
Beispiel #26
0
        public void GetConfigurationSpaceOverCorridor_RoomShapesThatCannotBeCorrectlyConnected()
        {
            var roomShape1     = PolygonGrid2D.GetSquare(5);
            var roomDoorsMode1 = new SimpleDoorMode(1, 0);

            var roomShape2 = new PolygonGrid2DBuilder()
                             .AddPoint(0, 1)
                             .AddPoint(0, 2)
                             .AddPoint(2, 2)
                             .AddPoint(2, 0)
                             .AddPoint(1, 0)
                             .AddPoint(1, 1)
                             .Build();
            var roomDoorsMode2 = new ManualDoorMode(new List <OrthogonalLineGrid2D>()
            {
                new OrthogonalLineGrid2D(new Vector2Int(1, 1), new Vector2Int(0, 1)),
            });

            var corridor          = PolygonGrid2D.GetSquare(2);
            var corridorDoorsMode = new ManualDoorMode(new List <OrthogonalLineGrid2D>()
            {
                new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(1, 0)),
                new OrthogonalLineGrid2D(new Vector2Int(0, 2), new Vector2Int(1, 2)),
            });

            var configurationSpace = generator.GetConfigurationSpaceOverCorridor(roomShape2, roomDoorsMode2, roomShape1,
                                                                                 roomDoorsMode1, corridor, corridorDoorsMode);

            Assert.That(configurationSpace.Lines.SelectMany(x => x.GetPoints()), Is.Empty);
        }
Beispiel #27
0
        public void GetRoomTemplateInstances_RectangleAllRotations_ReturnsTwoInstances()
        {
            var roomShape = PolygonGrid2D.GetRectangle(5, 10);

            var doorsMode       = new SimpleDoorMode(1, 0);
            var transformations = new List <TransformationGrid2D>()
            {
                TransformationGrid2D.Identity, TransformationGrid2D.Rotate90, TransformationGrid2D.Rotate180, TransformationGrid2D.Rotate270
            };

            var roomTemplate = new RoomTemplate(roomShape, doorsMode, transformations);
            var instances    = generator.GetRoomTemplateInstances(roomTemplate);

            Assert.That(instances.Count, Is.EqualTo(2));

            var wideRectangle = instances[0];
            var tallRectangle = instances[1];

            if (wideRectangle.RoomShape.Equals(roomShape))
            {
                wideRectangle = instances[1];
                tallRectangle = instances[0];
            }

            Assert.That(tallRectangle.RoomShape, Is.EqualTo(PolygonGrid2D.GetRectangle(5, 10)));
            Assert.That(wideRectangle.RoomShape, Is.EqualTo(PolygonGrid2D.GetRectangle(10, 5)));
        }
        /// <inheritdoc />
        /// <summary>
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="outline"></param>
        /// <param name="penWidth"></param>
        protected override void DrawRoom(PolygonGrid2D polygon, List <Tuple <Vector2Int, bool> > outline, float penWidth)
        {
            // Draw polygon
            data.Append($"    <polygon points=\"");

            foreach (var point in polygon.GetPoints())
            {
                data.Append($"{GetX(point)},{GetY(point)} ");
            }

            data.Append($"\" style=\"fill:rgb(211,211,211);stroke:rgb(211,211,211)\" />");
            data.AppendLine();

            // Draw path
            data.Append($"    <path d=\"");

            var lastPoint = outline[outline.Count - 1].Item1;

            data.Append($"M {GetX(lastPoint)} {GetY(lastPoint)} ");

            foreach (var pair in outline)
            {
                var point = pair.Item1;

                data.Append(pair.Item2 ? $"L {GetX(point)} {GetY(point)} " : $"M {GetX(point)} {GetY(point)} ");
            }

            data.Append($"\" fill=\"none\" stroke=\"black\" stroke-linecap=\"square\" stroke-width=\"{penWidth}\" />");
            data.AppendLine();
        }
        /// <summary>
        /// Computes configuration space of given two polygons.
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="doorsMode"></param>
        /// <param name="fixedCenter"></param>
        /// <param name="fixedDoorsMode"></param>
        /// <param name="offsets"></param>
        /// <returns></returns>
        public ConfigurationSpaceGrid2D GetConfigurationSpace(PolygonGrid2D polygon, IDoorModeGrid2D doorsMode, PolygonGrid2D fixedCenter,
                                                              IDoorModeGrid2D fixedDoorsMode, List <int> offsets = null)
        {
            var doorLinesFixed = fixedDoorsMode.GetDoors(fixedCenter);
            var doorLines      = doorsMode.GetDoors(polygon);

            return(GetConfigurationSpace(polygon, doorLines, fixedCenter, doorLinesFixed, offsets));
        }
        /// <inheritdoc />
        /// <summary>
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="text"></param>
        /// <param name="penWidth"></param>
        protected override void DrawTextOntoPolygon(PolygonGrid2D polygon, string text, float penWidth)
        {
            var partitions       = polygonPartitioning.GetPartitions(polygon);
            var biggestRectangle = partitions.OrderByDescending(x => x.Width).First();

            data.AppendLine(
                $"    <text x=\"{GetX(biggestRectangle.Center)}\" y=\"{GetY(biggestRectangle.Center)}\" alignment-baseline=\"middle\" text-anchor=\"middle\" style=\"font-family: arial;\" font-size=\"{(int) (1.2 * penWidth)}\">{text}</text>");
        }