/// <summary> /// Gets all the possible shapes based on repeat modes. /// If zero shapes are found and tryToFixEmpty is set to true, we try to lower the requirements and e.g. use /// only the NoImmediate mode instead of the NoRepeat mode. /// </summary> public List <RoomTemplateInstanceGrid2D> GetPossibleShapesForNode(ILayout <TNode, TConfiguration> layout, TNode node, bool tryToFixEmpty = false) { if (mapDescription.GetRoomDescription(node).IsCorridor) { return(shapesForNodes[node].Select(x => intAliasMapping.GetByValue(x.Shape)).ToList()); // return configurationSpaces.GetShapesForNode(node).Select(x => intAliasMapping.GetByValue(x)).ToList(); } var shapes = GetPossibleShapesForNode(layout, node, repeatModeOverride); if (shapes.Count == 0 && tryToFixEmpty) { // Try to lower our requirements and use NoImmediate instead of NoRepeat rather than returning an empty list if (repeatModeOverride == null || repeatModeOverride == RoomTemplateRepeatMode.NoRepeat) { shapes = GetPossibleShapesForNode(layout, node, RoomTemplateRepeatMode.NoImmediate); } // Try to lower our requirements and use AllowRepeat instead of returning an empty list if (shapes.Count == 0 && repeatModeOverride != RoomTemplateRepeatMode.AllowRepeat) { shapes = GetPossibleShapesForNode(layout, node, RoomTemplateRepeatMode.AllowRepeat); } } return(shapes); }
public void TwoWayDictionary_Add_ShouldAddItemToBothDictionaries() { int i = 1; string s = "one"; _sut.Add(i, s); _sut._forwardDict[i].Should().Be(s); _sut._reverseDict[s].Should().Be(i); _sut[i].Should().Be(s); _sut.GetByKey(i).Should().Be(s); _sut.GetByValue(s).Should().Be(i); }
private double[][] GetLayoutDistanceMatrix(TwoWayDictionary <TLayout, int> layouts, GetDistanceDelegate <TLayout> getDistance) { var distanceMatrix = new double[layouts.Count][]; for (int i = 0; i < layouts.Count; i++) { distanceMatrix[i] = new double[layouts.Count]; } for (int i = 0; i < layouts.Count - 1; i++) { for (int j = i + 1; j < layouts.Count; j++) { var distance = getDistance(layouts.GetByValue(i), layouts.GetByValue(j)); distanceMatrix[i][j] = distance; distanceMatrix[j][i] = distance; } } return(distanceMatrix); }
/// <summary> /// Gets room description from a given room template. /// </summary> /// <remarks> /// Returns cached result if a given room template was already processed. /// </remarks> /// <param name="roomTemplate"></param> /// <returns></returns> protected RoomDescription GetRoomDescription(GameObject roomTemplate) { if (RoomDescriptionsToRoomTemplates.ContainsValue(roomTemplate)) { return((RoomDescription)RoomDescriptionsToRoomTemplates.GetByValue(roomTemplate)); } var roomDescription = RoomShapesLoader.GetRoomDescription(roomTemplate); RoomDescriptionsToRoomTemplates.Add(roomDescription, roomTemplate); return(roomDescription); }
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); }
/// <inheritdoc /> public MapLayout <TNode> Convert(TLayout layout, bool addDoors) { var rooms = new List <Room <TNode> >(); var roomsDict = new Dictionary <TNode, Room <TNode> >(); var mapping = MapDescription.GetMapping(); foreach (var vertexAlias in layout.Graph.Vertices) { if (layout.GetConfiguration(vertexAlias, out var configuration)) { var vertex = mapping.GetByValue(vertexAlias); var roomTemplateInstance = IntAliasMapping.GetByValue(configuration.ShapeContainer); // Make sure that the returned shape has the same position as the original room template shape and is not moved to (0,0) // TODO: maybe make a unit/integration test? var transformation = roomTemplateInstance.Transformations.GetRandom(Random); var shape = configuration.Shape; var originalShape = roomTemplateInstance.RoomTemplate.Shape; var transformedShape = originalShape.Transform(transformation); var offset = transformedShape.BoundingRectangle.A - shape.BoundingRectangle.A; var room = new Room <TNode>(vertex, transformedShape, configuration.Position - offset, MapDescription.GetRoomDescription(vertexAlias) is CorridorRoomDescription, roomTemplateInstance.RoomTemplate, MapDescription.GetRoomDescription(vertexAlias), transformation, roomTemplateInstance.Transformations, roomTemplateInstance); rooms.Add(room); if (!addDoors) { continue; } var doors = new List <LayoutDoorGrid2D <TNode> >(); room.Doors = doors; roomsDict[vertex] = room; } } if (addDoors) { var generatedDoors = new HashSet <Tuple <TNode, TNode> >(); foreach (var vertexAlias in layout.Graph.Vertices) { var vertex = mapping.GetByValue(vertexAlias); if (layout.GetConfiguration(vertexAlias, out var configuration)) { var neighbours = layout.Graph.GetNeighbours(vertexAlias); foreach (var neighbourAlias in neighbours) { var neighbour = mapping.GetByValue(neighbourAlias); if (layout.GetConfiguration(neighbourAlias, out var neighbourConfiguration) && !generatedDoors.Contains(Tuple.Create(neighbour, vertex))) { var doorChoices = GetDoors(configuration, neighbourConfiguration); var randomChoice = doorChoices.GetRandom(Random); roomsDict[vertex].Doors.Add(new LayoutDoorGrid2D <TNode>(vertex, neighbour, randomChoice)); roomsDict[neighbour].Doors.Add(new LayoutDoorGrid2D <TNode>(neighbour, vertex, randomChoice)); generatedDoors.Add(Tuple.Create(vertex, neighbour)); } } } } } return(new MapLayout <TNode>(rooms)); }