Ejemplo n.º 1
0
        protected virtual IRoomDescription GetCorridorRoomDescription(List <int> corridorOffsets, int width = 1)
        {
            var corridorRoomTemplates   = MapDescriptionUtils.GetCorridorRoomTemplates(corridorOffsets, width);
            var corridorRoomDescription = new CorridorRoomDescription(corridorRoomTemplates);

            return(corridorRoomDescription);
        }
Ejemplo n.º 2
0
        private CorridorRoomDescription GetCorridors(MapDescriptionModel mapDescriptionModel, Dictionary <string, RoomDescriptionsSetModel> roomDescriptionsSets)
        {
            if (mapDescriptionModel.Corridors == null)
            {
                return(null);
            }

            var corridors = mapDescriptionModel.Corridors;
            var enable    = corridors.Enable ?? true;

            if (!enable)
            {
                return(null);
            }

            if (enable && (corridors.CorridorShapes == null || corridors.CorridorShapes.Count == 0))
            {
                throw new InvalidOperationException("There must be at least one shape for corridors if they are enabled.");
            }

            var roomTemplates   = GetRoomTemplates(corridors.CorridorShapes, roomDescriptionsSets, mapDescriptionModel.CustomRoomDescriptionsSet);
            var roomDescription = new CorridorRoomDescription(roomTemplates);

            return(roomDescription);
        }
Ejemplo n.º 3
0
        public void GetGraph_WhenCorridorTooManyNeighbors_Throws()
        {
            var mapDescription = new MapDescription <int>();

            var basicRoomDescription    = new BasicRoomDescription(GetRoomTemplates());
            var corridorRoomDescription = new CorridorRoomDescription(GetRoomTemplates());

            mapDescription.AddRoom(0, corridorRoomDescription);
            mapDescription.AddRoom(1, basicRoomDescription);
            mapDescription.AddRoom(2, basicRoomDescription);
            mapDescription.AddRoom(3, basicRoomDescription);

            mapDescription.AddConnection(0, 1);
            mapDescription.AddConnection(0, 2);
            mapDescription.AddConnection(0, 3);

            Assert.Throws <ArgumentException>(() => mapDescription.GetGraph());
            Assert.Throws <ArgumentException>(() => mapDescription.GetStageOneGraph());
        }
Ejemplo n.º 4
0
        private CorridorRoomDescription GetCorridorRoomDescription(List <GameObject> roomTemplatePrefabs)
        {
            foreach (var existingRoomDescription in corridorRoomDescriptions)
            {
                var existingPrefabs = existingRoomDescription
                                      .RoomTemplates
                                      .Select(x => prefabToRoomTemplateMapping.GetByValue(x))
                                      .ToList();

                if (existingPrefabs.SequenceEqual(roomTemplatePrefabs))
                {
                    return(existingRoomDescription);
                }
            }

            var corridorRoomDescription = new CorridorRoomDescription(roomTemplatePrefabs.Select(GetRoomTemplate).ToList());

            corridorRoomDescriptions.Add(corridorRoomDescription);

            return(corridorRoomDescription);
        }
        public void BasicCorridorsTest()
        {
            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 corridorDescription = new CorridorRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate2
            });

            var mapDescription = new MapDescription <string>();

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

            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(corridorDescription));
            Assert.That(mapDescriptionMapping.GetRoomDescription(mapping["2"]), Is.EqualTo(roomDescription2));

            Assert.That(mapDescriptionMapping.GetGraph().VerticesCount, Is.EqualTo(3));
            Assert.That(mapDescriptionMapping.GetStageOneGraph().VerticesCount, Is.EqualTo(2));
            Assert.That(mapDescriptionMapping.GetGraph().HasEdge(mapping["0"], mapping["1"]), Is.True);
            Assert.That(mapDescriptionMapping.GetGraph().HasEdge(mapping["1"], mapping["2"]), Is.True);
            Assert.That(mapDescriptionMapping.GetStageOneGraph().HasEdge(mapping["0"], mapping["2"]), Is.True);
        }
Ejemplo n.º 6
0
        public static IMapDescription <TNode> GetMapDescription <TNode>(this LevelDescriptionGrid2D <TNode> levelDescription)
        {
            var mapDescription = new MapDescription <TNode>();
            var graph          = levelDescription.GetGraph();

            var corridorRoomDescriptions = new Dictionary <RoomDescriptionGrid2D, CorridorRoomDescription>();
            var roomTemplateMapping      = new Dictionary <RoomTemplateGrid2D, RoomTemplate>();

            foreach (var room in graph.Vertices)
            {
                var roomDescription = levelDescription.GetRoomDescription(room);

                if (roomDescription.IsCorridor)
                {
                    if (corridorRoomDescriptions.TryGetValue(roomDescription, out var cached))
                    {
                        mapDescription.AddRoom(room, cached);
                    }
                    else
                    {
                        var corridorRoomDescription = new CorridorRoomDescription(roomDescription.RoomTemplates.Select(x => GetOldRoomTemplate(x, roomTemplateMapping)).ToList());
                        corridorRoomDescriptions[roomDescription] = corridorRoomDescription;
                        mapDescription.AddRoom(room, corridorRoomDescription);
                    }
                }
                else
                {
                    mapDescription.AddRoom(room, new BasicRoomDescription(roomDescription.RoomTemplates.Select(x => GetOldRoomTemplate(x, roomTemplateMapping)).ToList()));
                }
            }

            foreach (var edge in graph.Edges)
            {
                mapDescription.AddConnection(edge.From, edge.To);
            }

            return(mapDescription);
        }
Ejemplo n.º 7
0
        private MapDescription <int> GetMapDescription(BasicRoomDescription roomDescription, CorridorRoomDescription corridorRoomDescription = null)
        {
            var mapDescription = new MapDescription <int>();

            var graph = new UndirectedAdjacencyListGraph <int>();

            graph.AddVertex(0);
            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);

            foreach (var vertex in graph.Vertices)
            {
                mapDescription.AddRoom(vertex, roomDescription);
            }

            var corridorCounter = graph.VerticesCount;

            foreach (var edge in graph.Edges)
            {
                if (corridorRoomDescription != null)
                {
                    mapDescription.AddRoom(corridorCounter, corridorRoomDescription);
                    mapDescription.AddConnection(corridorCounter, edge.From);
                    mapDescription.AddConnection(corridorCounter, edge.To);
                    corridorCounter++;
                }
                else
                {
                    mapDescription.AddConnection(edge.From, edge.To);
                }
            }

            return(mapDescription);
        }
Ejemplo n.º 8
0
        public void NoImmediateWithCorridors()
        {
            var roomTemplate1   = GetRoomTemplate(RoomTemplateRepeatMode.NoImmediate);
            var roomTemplate2   = GetRoomTemplate(RoomTemplateRepeatMode.NoImmediate);
            var roomTemplate3   = GetRoomTemplate(RoomTemplateRepeatMode.NoImmediate);
            var roomDescription = new BasicRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate1, roomTemplate2, roomTemplate3
            });
            var corridorRoomDescription = new CorridorRoomDescription(new List <RoomTemplate>()
            {
                roomTemplate1, roomTemplate2, roomTemplate3
            });

            var mapDescription      = GetMapDescription(roomDescription, corridorRoomDescription);
            var configurationSpaces = GetConfigurationSpaces(mapDescription);

            var roomShapesHandler = new RoomShapesHandler <int, Configuration <CorridorsData> >(
                configurationSpaces,
                configurationSpaces.GetIntAliasMapping(),
                mapDescription
                );

            var roomShapes = configurationSpaces.GetIntAliasMapping().Values.ToList();
            var layout     = new Layout <Configuration <CorridorsData> >(mapDescription.GetGraph());

            SetConfiguration(layout, 0, roomShapes[0]);
            SetConfiguration(layout, 1, roomShapes[1]);
            SetConfiguration(layout, 2, roomShapes[2]);

            {
                // Node 0
                var shapes         = roomShapesHandler.GetPossibleShapesForNode(layout, 0);
                var expectedShapes = new List <IntAlias <PolygonGrid2D> >()
                {
                    roomShapes[0], roomShapes[2]
                };
                Assert.That(shapes, Is.EquivalentTo(expectedShapes));
            }

            {
                // Node 1
                var shapes         = roomShapesHandler.GetPossibleShapesForNode(layout, 1);
                var expectedShapes = new List <IntAlias <PolygonGrid2D> >()
                {
                    roomShapes[1]
                };
                Assert.That(shapes, Is.EquivalentTo(expectedShapes));
            }

            {
                // Node 2
                var shapes         = roomShapesHandler.GetPossibleShapesForNode(layout, 2);
                var expectedShapes = new List <IntAlias <PolygonGrid2D> >()
                {
                    roomShapes[0], roomShapes[2]
                };
                Assert.That(shapes, Is.EquivalentTo(expectedShapes));
            }

            foreach (var corridorRoom in mapDescription.GetGraph().Vertices.Where(x => mapDescription.GetRoomDescription(x) is CorridorRoomDescription))
            {
                var shapes         = roomShapesHandler.GetPossibleShapesForNode(layout, corridorRoom);
                var expectedShapes = configurationSpaces.GetShapesForNode(corridorRoom);
                Assert.That(shapes, Is.EquivalentTo(expectedShapes));
            }
        }
Ejemplo n.º 9
0
        public MapDescription <int> GetMapDescription()
        {
            const bool useLongCorridors    = false;
            const bool useLShapedCorridors = false;
            const bool useWideCorridors    = false;

            // Create basic room templates and room description
            var doorMode = new SimpleDoorMode(1, 1);

            var squareRoom = new RoomTemplate(
                PolygonGrid2D.GetSquare(8),
                doorMode
                );

            var rectangleRoom = new RoomTemplate(
                PolygonGrid2D.GetRectangle(6, 10),
                doorMode,
                new List <TransformationGrid2D>()
            {
                TransformationGrid2D.Identity, TransformationGrid2D.Rotate90
            }
                );

            var basicRoomDescription = new BasicRoomDescription(new List <RoomTemplate>()
            {
                squareRoom, rectangleRoom
            });

            // Basic corridor shape
            var corridorRoom1x2 = new RoomTemplate(
                PolygonGrid2D.GetRectangle(1, 2),
                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))
            }),
                new List <TransformationGrid2D>()
            {
                TransformationGrid2D.Identity, TransformationGrid2D.Rotate90
            }
                );

            var corridorRoomDescription = new CorridorRoomDescription(new List <RoomTemplate>()
            {
                corridorRoom1x2
            });

            // Add longer corridor
            if (useLongCorridors)
            {
                var corridorRoom1x4 = new RoomTemplate(
                    PolygonGrid2D.GetRectangle(1, 4),
                    new ManualDoorMode(new List <OrthogonalLineGrid2D>()
                {
                    new OrthogonalLineGrid2D(new Vector2Int(0, 0), new Vector2Int(1, 0)),
                    new OrthogonalLineGrid2D(new Vector2Int(0, 4), new Vector2Int(1, 4))
                }),
                    new List <TransformationGrid2D>()
                {
                    TransformationGrid2D.Identity, TransformationGrid2D.Rotate90
                }
                    );

                corridorRoomDescription.RoomTemplates.Add(corridorRoom1x4);
            }

            // Add l-shaped corridor
            if (useLShapedCorridors)
            {
                var corridorRoomLShaped = new RoomTemplate(
                    new PolygonGrid2DBuilder()
                    .AddPoint(0, 2)
                    .AddPoint(0, 3)
                    .AddPoint(3, 3)
                    .AddPoint(3, 0)
                    .AddPoint(2, 0)
                    .AddPoint(2, 2)
                    .Build(),
                    new ManualDoorMode(new List <OrthogonalLineGrid2D>()
                {
                    new OrthogonalLineGrid2D(new Vector2Int(0, 2), new Vector2Int(0, 3)),
                    new OrthogonalLineGrid2D(new Vector2Int(2, 0), new Vector2Int(3, 0))
                }),
                    TransformationGrid2DHelper.GetAllTransformationsOld().ToList()
                    );

                corridorRoomDescription.RoomTemplates.Add(corridorRoomLShaped);
            }

            // Add wide corridor
            if (useWideCorridors)
            {
                var corridorWide = new RoomTemplate(
                    new PolygonGrid2DBuilder()
                    .AddPoint(1, 0)
                    .AddPoint(1, 1)
                    .AddPoint(0, 1)
                    .AddPoint(0, 4)
                    .AddPoint(1, 4)
                    .AddPoint(1, 5)
                    .AddPoint(2, 5)
                    .AddPoint(2, 4)
                    .AddPoint(3, 4)
                    .AddPoint(3, 1)
                    .AddPoint(2, 1)
                    .AddPoint(2, 0)
                    .Build(),
                    new ManualDoorMode(new List <OrthogonalLineGrid2D>()
                {
                    new OrthogonalLineGrid2D(new Vector2Int(1, 0), new Vector2Int(2, 0)),
                    new OrthogonalLineGrid2D(new Vector2Int(1, 5), new Vector2Int(2, 5))
                }),
                    TransformationGrid2DHelper.GetAllTransformationsOld().ToList()
                    );

                corridorRoomDescription.RoomTemplates.Add(corridorWide);
            }

            // Create map description
            var mapDescription = new MapDescription <int>();
            var graph          = GraphsDatabase.GetExample1();

            // Add non-corridor rooms
            foreach (var room in graph.Vertices)
            {
                mapDescription.AddRoom(room, basicRoomDescription);
            }

            // We need to somehow identify our corridor rooms
            // Here we simply number them and keep track which was the last used number
            var counter = graph.VerticesCount;

            foreach (var connection in graph.Edges)
            {
                if (true)
                {
                    if (true)
                    {
                        if (connection.From % 2 == 0 && connection.To % 2 == 0)
                        {
                            // We manually insert a new node between each neighboring nodes in the graph
                            mapDescription.AddRoom(counter, corridorRoomDescription);

                            // And instead of connecting the rooms directly, we connect them to the corridor room
                            mapDescription.AddConnection(connection.From, counter);
                            mapDescription.AddConnection(connection.To, counter);
                            counter++;
                        }
                        else
                        {
                            mapDescription.AddConnection(connection.From, connection.To);
                        }
                    }
                    else
                    {
                        var desc1 = new CorridorRoomDescription(new List <RoomTemplate>()
                        {
                            corridorRoomDescription.RoomTemplates[0]
                        });
                        var desc2 = new CorridorRoomDescription(new List <RoomTemplate>()
                        {
                            corridorRoomDescription.RoomTemplates[1]
                        });

                        // We manually insert a new node between each neighboring nodes in the graph
                        mapDescription.AddRoom(counter, connection.From % 2 == 0 && connection.To % 2 == 0 ? desc2 : desc1);

                        // And instead of connecting the rooms directly, we connect them to the corridor room
                        mapDescription.AddConnection(connection.From, counter);
                        mapDescription.AddConnection(connection.To, counter);
                        counter++;
                    }
                }
                else
                {
                    // We manually insert a new node between each neighboring nodes in the graph
                    mapDescription.AddRoom(counter, corridorRoomDescription);

                    // And instead of connecting the rooms directly, we connect them to the corridor room
                    mapDescription.AddConnection(connection.From, counter);
                    mapDescription.AddConnection(connection.To, counter);
                    counter++;
                }
            }

            return(mapDescription);
        }