public void Generate_BasicTest()
        {
            // This test cannot check if the generated configuration spaces are valid
            var mapDescription = new MapDescription <int>();
            var squareRoom     = new RoomDescription(GridPolygon.GetSquare(3), new OverlapMode(1, 0));
            var rectangleRoom  = new RoomDescription(GridPolygon.GetRectangle(4, 5), new OverlapMode(1, 1));

            mapDescription.AddRoomShapes(squareRoom);
            mapDescription.AddRoomShapes(rectangleRoom, probability: 0.5d);

            mapDescription.AddRoom(0);
            mapDescription.AddRoom(1);
            mapDescription.AddPassage(0, 1);

            mapDescription.AddRoomShapes(1, rectangleRoom, new List <Transformation>()
            {
                Transformation.Identity
            });

            // var configurationSpaces = generator.Generate(mapDescription);
            Assert.IsTrue(false);             // TODO: repair

            //Assert.AreEqual(3, configurationSpaces.GetShapesForNode(0).Count);
            //Assert.AreEqual(1, configurationSpaces.GetShapesForNode(1).Count);
        }
        protected override void DrawRoom(GridPolygon polygon, List <Tuple <IntVector2, 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 MapDescription <int> GetMapDescription()
        {
            var mapDescription = new MapDescription <int>();

            // Add rooms ( - you would normally use a for cycle)
            mapDescription.AddRoom(0);
            mapDescription.AddRoom(1);
            mapDescription.AddRoom(2);
            mapDescription.AddRoom(3);

            // Add passages
            mapDescription.AddPassage(0, 1);
            mapDescription.AddPassage(0, 3);
            mapDescription.AddPassage(1, 2);
            mapDescription.AddPassage(2, 3);

            // Add room shapes
            var doorMode = new OverlapMode(1, 1);

            var squareRoom = new RoomDescription(
                GridPolygon.GetSquare(8),
                doorMode
                );
            var rectangleRoom = new RoomDescription(
                GridPolygon.GetRectangle(6, 10),
                doorMode
                );

            mapDescription.AddRoomShapes(squareRoom);
            mapDescription.AddRoomShapes(rectangleRoom);

            return(mapDescription);
        }
Ejemplo n.º 4
0
        static void GenerateMap(SpaceGraph graph, int seed = 0)
        {
            var mapDescription = new MapDescription <int>();

            //Add rooms
            graph.AllNodes.ForEach(node => mapDescription.AddRoom(node.Id));

            //Add connections
            List <List <int> > connections = graph.ConvertToAdjList(false);

            for (int node = 0; node < connections.Count; node++)
            {
                for (int link = 0; link < connections[node].Count; link++)
                {
                    mapDescription.AddPassage(node, connections[node][link]);
                }
            }
            //Add room descriptions
            using (var reader = new StreamReader(@"Resources\Rooms\SMB.yml"))
            {
                var roomLoader = cfloader.LoadRoomDescriptionsSetModel(reader);
                foreach (var roomDescription in roomLoader.RoomDescriptions)
                {
                    GridPolygon     shape     = new GridPolygon(roomDescription.Value.Shape);
                    RoomDescription roomShape = new RoomDescription(shape, (roomDescription.Value.DoorMode == null) ? roomLoader.Default.DoorMode : roomDescription.Value.DoorMode);
                    mapDescription.AddRoomShapes(roomShape);
                }
            }

            // Generate bitmap
            SaveBitmap(mapDescription, seed);
        }
        public void BoudingRectangle_ReturnsBoundingBox()
        {
            {
                var p = GridPolygon.GetRectangle(2, 4);

                var boundingRectangle = p.BoundingRectangle;
                var expected          = new GridRectangle(new IntVector2(0, 0), new IntVector2(2, 4));

                Assert.AreEqual(expected, boundingRectangle);
            }

            {
                var p = new GridPolygonBuilder()
                        .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 GridRectangle(new IntVector2(0, 0), new IntVector2(7, 6));

                Assert.AreEqual(expected, boundingRectangle);
            }
        }
        public void OverlapArea_TwoRectangles()
        {
            var r1 = GridPolygon.GetRectangle(4, 6);
            var r2 = GridPolygon.GetRectangle(5, 3);

            Assert.AreEqual(9, polygonOverlap.OverlapArea(r1, new IntVector2(0, 0), r2, new IntVector2(1, 2)));
        }
        public void OverlapArea_NonTouching_ReturnsZero()
        {
            var r1 = GridPolygon.GetSquare(6);
            var r2 = GridPolygon.GetRectangle(2, 8);

            Assert.AreEqual(0, polygonOverlap.OverlapArea(r1, new IntVector2(0, 0), r2, new IntVector2(7, 2)));
        }
Ejemplo n.º 8
0
        /// <inheritdoc />
        /// <summary>
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="outline"></param>
        /// <param name="penWidth"></param>
        protected override void DrawRoom(GridPolygon polygon, List <Tuple <IntVector2, bool> > outline, float penWidth)
        {
            // Draw polygon
            data.Append($"    <polygon points=\"");

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

            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 {lastPoint.X} {lastPoint.Y} ");

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

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

            data.Append($"\" fill=\"none\" stroke=\"black\" stroke-linecap=\"square\" stroke-width=\"{penWidth}\" />");
            data.AppendLine();
        }
        /// <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(IMapLayout <TNode> layout, float scale, IntVector2 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 IntVector2(0, 0)))
                {
                    throw new InvalidOperationException("One of the polygons could not be drawn because the canvas size is too small.");
                }

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

                if (withNames && !room.IsCorridor)
                {
                    DrawTextOntoPolygon(polygon, room.Node.ToString(), fixedFontSize ?? 2.5f * minWidth);
                }
            }
        }
Ejemplo n.º 10
0
        public void TransformPointToNewPosition_RectanglePoints()
        {
            // Create a rectangular room shape
            // Move it away from (0, 0) so that we can properly test the functionality
            var rectangleShape = GridPolygon.GetRectangle(10, 4) + new IntVector2(10, 10);

            // Create points to be transformed
            // These could be for example traps, spawn points, etc.
            // We use points of the rectangle here because it is easy to check that the transformation is correct.
            var pointsToTransform = rectangleShape.GetPoints();

            var mapDescription = new MapDescription <int>();

            // Create simple graph with 2 vertices and 1 edge
            mapDescription.AddRoom(0);
            mapDescription.AddRoom(1);
            mapDescription.AddPassage(0, 1);

            // Add the rectangle shape
            mapDescription.AddRoomShapes(new RoomDescription(rectangleShape, new OverlapMode(1, 0)));

            var layoutGenerator = LayoutGeneratorFactory.GetDefaultChainBasedGenerator <int>();
            var layout          = layoutGenerator.GetLayouts(mapDescription, 1)[0];

            foreach (var room in layout.Rooms)
            {
                // The points were chosen to be the points of the polygon, so after transforming them, they should
                // be equal to the room.Shape + room.Position points
                var transformedPoints = pointsToTransform.Select(x => room.TransformPointToNewPosition(x));
                var expectedPoints    = (room.Shape + room.Position).GetPoints();

                Assert.That(transformedPoints, Is.EquivalentTo(expectedPoints));
            }
        }
 public RoomInfo(RoomDescription roomDescription, GridPolygon roomShape, List <Transformation> transformations, List <IDoorLine> doorLines)
 {
     RoomDescription = roomDescription;
     RoomShape       = roomShape;
     Transformations = transformations;
     DoorLines       = doorLines;
 }
		public MapDescription<int> GetMapDescription()
		{
			var mapDescription = new MapDescription<int>();
			mapDescription.SetupWithGraph(GraphsDatabase.GetExample5());

			// Add room shapes
			var doorMode = new OverlapMode(1, 1);

			var squareRoomBig = new RoomDescription(
				GridPolygon.GetSquare(8),
				doorMode
			);
			var squareRoomSmall = new RoomDescription(
				GridPolygon.GetSquare(6),
				doorMode
			);
			var rectangleRoomBig = new RoomDescription(
				GridPolygon.GetRectangle(8, 12),
				doorMode
			);
			var rectangleRoomSmall = new RoomDescription(
				GridPolygon.GetRectangle(6, 10),
				doorMode
			);

			mapDescription.AddRoomShapes(squareRoomBig, probability: 10);
			mapDescription.AddRoomShapes(squareRoomSmall);
			mapDescription.AddRoomShapes(rectangleRoomBig);
			mapDescription.AddRoomShapes(rectangleRoomSmall);

			return mapDescription;
		}
        public void Rectangle_LengthZeroCorners()
        {
            var polygon = GridPolygon.GetRectangle(3, 5);
            var mode    = new SpecificPositionsMode(new List <OrthogonalLine>()
            {
                new OrthogonalLine(new IntVector2(0, 0), new IntVector2(0, 0)),
                new OrthogonalLine(new IntVector2(0, 5), new IntVector2(0, 5)),
                new OrthogonalLine(new IntVector2(3, 5), new IntVector2(3, 5)),
                new OrthogonalLine(new IntVector2(3, 0), new IntVector2(3, 0)),
            });
            var doorPositions = overlapModeHandler.GetDoorPositions(polygon, mode);

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

            Assert.IsTrue(doorPositions.SequenceEqualWithoutOrder(expectedPositions));
        }
        public void OverlapArea_TwoSquares()
        {
            var r1 = GridPolygon.GetSquare(6);
            var r2 = GridPolygon.GetSquare(3);

            Assert.AreEqual(6, polygonOverlap.OverlapArea(r1, new IntVector2(0, 0), r2, new IntVector2(2, -1)));
        }
        /// <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 ConfigurationSpace GetConfigurationSpace(GridPolygon polygon, IDoorMode doorsMode, GridPolygon fixedCenter,
                                                        IDoorMode fixedDoorsMode, List <int> offsets = null)
        {
            var doorLinesFixed = doorHandler.GetDoorPositions(fixedCenter, fixedDoorsMode);
            var doorLines      = doorHandler.GetDoorPositions(polygon, doorsMode);

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

            data.AppendLine(
                $"    <text x=\"{biggestRectangle.Center.X}\" y=\"{biggestRectangle.Center.Y}\" alignment-baseline=\"middle\" text-anchor=\"middle\" style=\"font-family: arial;\" font-size=\"{(int) (1.2 * penWidth)}\">{text}</text>");
        }
        /// <summary>
        /// Computes the outline of a given polygon and its door lines.
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="doorLines"></param>
        /// <returns></returns>
        protected List <Tuple <IntVector2, bool> > GetOutline(GridPolygon polygon, List <IDoorInfo <TNode> > doorLines)
        {
            var outline = new List <Tuple <IntVector2, bool> >();

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

                if (doorLines == null)
                {
                    continue;
                }

                var doorDistances = doorLines.Select(x =>
                                                     new Tuple <IDoorInfo <TNode>, int>(x, Math.Min(line.Contains(x.DoorLine.From), line.Contains(x.DoorLine.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.DoorLine;

                    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 <IntVector2, 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);
            }
        }
Ejemplo n.º 18
0
        /// <inheritdoc />
        /// <remarks>
        /// Gets door positions by by returning an output of a registered door handler.
        /// </remarks>
        /// <param name="polygon"></param>
        /// <param name="doorMode"></param>
        /// <returns></returns>
        public List <IDoorLine> GetDoorPositions(GridPolygon polygon, IDoorMode doorMode)
        {
            if (handlers.TryGetValue(doorMode.GetType(), out var handler))
            {
                return(handler.GetDoorPositions(polygon, doorMode));
            }

            throw new InvalidOperationException("Handler not found");
        }
Ejemplo n.º 19
0
        public GridPolygon GetGridPolygon()
        {
            if (gridPolygon == null)
            {
                gridPolygon = new GridPolygon(points.Select(x => x.ToCustomIntVector2()));
            }

            return(gridPolygon);
        }
        public void DoTouch_TwoSquares()
        {
            var r1 = GridPolygon.GetSquare(6);
            var r2 = GridPolygon.GetSquare(3);

            Assert.AreEqual(true, polygonOverlap.DoTouch(r1, new IntVector2(0, 0), r2, new IntVector2(6, 0)));
            Assert.AreEqual(false, polygonOverlap.DoTouch(r1, new IntVector2(0, 0), r2, new IntVector2(6, -3)));
            Assert.AreEqual(true, polygonOverlap.DoTouch(r1, new IntVector2(0, 0), r2, new IntVector2(6, -2)));
        }
Ejemplo n.º 21
0
 private Room <int> GetRoom(IntVector2 position, IntVector2 roomShapeOrigin, Transformation transformation, List <Transformation> transformations)
 {
     return(new Room <int>(0,
                           GridPolygon.GetSquare(10),
                           position,
                           false,
                           new RoomDescription(GridPolygon.GetSquare(10) + roomShapeOrigin, new OverlapMode(1, 0)),
                           transformation,
                           transformations));
 }
        public void OverlapAlongLine_Rectangles_OverlapStart2()
        {
            var p1   = GridPolygon.GetSquare(5);
            var p2   = GridPolygon.GetRectangle(2, 3) + new IntVector2(0, -3);
            var line = new OrthogonalLine(new IntVector2(0, 0), new IntVector2(0, 10));

            var result = polygonOverlap.OverlapAlongLine(p1, p2, line);

            Assert.AreEqual(0, result.Count);
        }
        public void OverlapArea_PlusShapeAndSquare()
        {
            var p1 = GetPlusShape();
            var p2 = GridPolygon.GetSquare(3);

            foreach (var degrees in GridPolygon.PossibleRotations)
            {
                Assert.AreEqual(5, polygonOverlap.OverlapArea(p1.Rotate(degrees), new IntVector2(0, 0), p2.Rotate(degrees), new IntVector2(3, 3).RotateAroundCenter(degrees)));
            }
        }
Ejemplo n.º 24
0
 public Room(TNode node, GridPolygon shape, IntVector2 position, bool isCorridor, IRoomDescription roomDescription, Transformation transformation, IList <Transformation> transformations)
 {
     Node            = node;
     Shape           = shape;
     Position        = position;
     IsCorridor      = isCorridor;
     RoomDescription = roomDescription;
     Transformation  = transformation;
     Transformations = transformations;
     Doors           = null;
 }
        public void GetAllTransformations_ReturnsCorrectCount()
        {
            var square    = GridPolygon.GetSquare(1);
            var rectangle = GridPolygon.GetRectangle(1, 2);

            var transformedSquares    = square.GetAllTransformations();
            var transformedRectangles = rectangle.GetAllTransformations();

            Assert.That(transformedSquares.Count(), Is.EqualTo(8));
            Assert.That(transformedRectangles.Count(), Is.EqualTo(8));
        }
Ejemplo n.º 26
0
        public List <GridRectangle> GetPartitions(GridPolygon polygon)
        {
            if (this.partitions.TryGetValue(polygon, out var partitions))
            {
                return(partitions);
            }

            partitions = polygonPartitioning.GetPartitions(polygon);
            this.partitions.Add(polygon, partitions);

            return(partitions);
        }
Ejemplo n.º 27
0
        public void Rectangle_TwoOverlap()
        {
            var polygon           = GridPolygon.GetRectangle(3, 5);
            var mode              = new OverlapMode(1, 2);
            var doorPositions     = overlapModeHandler.GetDoorPositions(polygon, mode);
            var expectedPositions = new List <IDoorLine>()
            {
                new DoorLine(new OrthogonalLine(new IntVector2(0, 2), new IntVector2(0, 2), OrthogonalLine.Direction.Top), 1),
                new DoorLine(new OrthogonalLine(new IntVector2(3, 3), new IntVector2(3, 3), OrthogonalLine.Direction.Bottom), 1),
            };

            Assert.IsTrue(doorPositions.SequenceEqualWithoutOrder(expectedPositions));
        }
        public void OverlapAlongLine_SquareAndL2()
        {
            var p1   = GridPolygon.GetSquare(6);
            var p2   = GetLShape();
            var line = new OrthogonalLine(new IntVector2(3, 5), new IntVector2(3, -2));

            var result   = polygonOverlap.OverlapAlongLine(p1, p2, line);
            var expected = new List <Tuple <IntVector2, bool> >()
            {
                Tuple.Create(new IntVector2(3, 2), true),
            };

            Assert.IsTrue(expected.SequenceEqual(result));
        }
        public void Rotate_Square_ReturnsRotated()
        {
            var square         = GridPolygon.GetSquare(4);
            var rotatedSquare  = square.Rotate(180);
            var expectedPoints = new List <IntVector2>()
            {
                new IntVector2(0, 0),
                new IntVector2(0, -4),
                new IntVector2(-4, -4),
                new IntVector2(-4, 0),
            };

            Assert.IsTrue(expectedPoints.SequenceEqual(rotatedSquare.GetPoints()));
        }
        public void Rotate_Rectangle_ReturnsRotated()
        {
            var polygon        = GridPolygon.GetRectangle(2, 5);
            var rotatedPolygon = polygon.Rotate(270);
            var expectedPoints = new List <IntVector2>()
            {
                new IntVector2(0, 0),
                new IntVector2(-5, 0),
                new IntVector2(-5, 2),
                new IntVector2(0, 2),
            };

            Assert.IsTrue(expectedPoints.SequenceEqual(rotatedPolygon.GetPoints()));
        }