示例#1
0
        public ConnectivityMap GenerateConnectivityMapNoCycles(int numberNodes, double branchingRatio)
        {
            var map = new ConnectivityMap();

            var terminalNodes = new HashSet <int> {
                0, 1
            };
            var nextNodeIndex = 2;

            map.AddRoomConnection(0, 1);

            while (nextNodeIndex < numberNodes)
            {
                var randomChance = rand.NextDouble();

                int sourceVertex;

                if (randomChance < branchingRatio)
                {
                    sourceVertex = rand.Next(nextNodeIndex);
                }
                else
                {
                    sourceVertex = terminalNodes.RandomElementUsing(rand);
                }

                map.AddRoomConnection(sourceVertex, nextNodeIndex);
                terminalNodes.Remove(sourceVertex);
                terminalNodes.Add(nextNodeIndex);
                nextNodeIndex++;
            }

            return(map);
        }
示例#2
0
        private MapInfoBuilder GetStandardMapInfoBuilderForTemplates()
        {
            var builder = new MapInfoBuilder();

            var l1ConnectivityMap = new ConnectivityMap();

            l1ConnectivityMap.AddRoomConnection(1, 2);
            l1ConnectivityMap.AddRoomConnection(2, 3);

            var corridor1 = new RoomTemplateTerrain[1, 3];
            var corridor2 = new RoomTemplateTerrain[3, 1];
            var room1     = new RoomTemplateTerrain[4, 4];

            var l1RoomList = new List <TemplatePositioned>();

            l1RoomList.Add(new TemplatePositioned(1, 1, 0, new RoomTemplate(corridor1), 0));
            l1RoomList.Add(new TemplatePositioned(1, 1, 0, new RoomTemplate(corridor2), 1));
            l1RoomList.Add(new TemplatePositioned(1, 1, 0, new RoomTemplate(room1), 2));

            var l1DoorDict = new Dictionary <Connection, Point>();

            l1DoorDict.Add(new Connection(2, 3), new Point(5, 5));

            builder.AddConstructedLevel(0, l1ConnectivityMap, l1RoomList, l1DoorDict, 1);

            return(builder);
        }
示例#3
0
        public void AddingSameEdgeTwiceDoesntMakeTwoEdges()
        {
            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(1, 2);

            Assert.AreEqual(1, newMap.RoomConnectionGraph.EdgeCount);
        }
示例#4
0
        public void CyclesOnSeparateLevelsCanBeReturned()
        {
            var builder = new MapInfoBuilder();

            var l1ConnectivityMap = new ConnectivityMap();

            //Cycle in level 1
            l1ConnectivityMap.AddRoomConnection(1, 2);
            l1ConnectivityMap.AddRoomConnection(2, 3);
            l1ConnectivityMap.AddRoomConnection(3, 1);

            var l1RoomList = new List <TemplatePositioned>();
            var room1      = new TemplatePositioned(1, 1, 0, null, 1);

            l1RoomList.Add(room1);
            l1RoomList.Add(new TemplatePositioned(1, 1, 0, null, 2));
            l1RoomList.Add(new TemplatePositioned(1, 1, 0, null, 3));

            var l2ConnectivityMap = new ConnectivityMap();

            //Cycle in level 2
            l2ConnectivityMap.AddRoomConnection(5, 6);
            l2ConnectivityMap.AddRoomConnection(6, 7);
            l2ConnectivityMap.AddRoomConnection(7, 5);

            var l2RoomList = new List <TemplatePositioned>();
            var room5      = new TemplatePositioned(1, 1, 0, null, 5);

            l2RoomList.Add(room5);
            l2RoomList.Add(new TemplatePositioned(1, 1, 0, null, 6));
            l2RoomList.Add(new TemplatePositioned(1, 1, 0, null, 7));

            builder.AddConstructedLevel(0, l1ConnectivityMap, l1RoomList, new Dictionary <Connection, Point>(), 1);
            builder.AddConstructedLevel(1, l2ConnectivityMap, l2RoomList, new Dictionary <Connection, Point>(), new Connection(3, 5));

            var mapInfo = new MapInfo(builder);

            var cyclesOnLevel0 = mapInfo.GetCyclesOnLevel(0).ToList();

            Assert.AreEqual(1, cyclesOnLevel0.Count());
            CollectionAssert.AreEquivalent(cyclesOnLevel0[0], new List <Connection> {
                new Connection(1, 2),
                new Connection(2, 3),
                new Connection(3, 1)
            });

            var cyclesOnLevel1 = mapInfo.GetCyclesOnLevel(1).ToList();

            Assert.AreEqual(1, cyclesOnLevel1.Count());
            CollectionAssert.AreEquivalent(cyclesOnLevel1[0], new List <Connection> {
                new Connection(5, 6),
                new Connection(6, 7),
                new Connection(7, 5)
            });
        }
示例#5
0
        public void MapCycleReducerMapsContainsAMappingForEachEdgeInReducedMap()
        {
            //Build a graph with one nested cycles

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            //Exterior connections

            newMap.AddRoomConnection(5, 6);
            newMap.AddRoomConnection(4, 7);
            newMap.AddRoomConnection(4, 8);
            newMap.AddRoomConnection(3, 9);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            Assert.AreEqual(6, cycleReducer.edgeMappingNoCycleToFullMap.Count());
        }
示例#6
0
        public void MapWithTwoNonConnectedCyclesGivesTwoCyclesInAllCycles()
        {
            //Build a graph with one nested cycle

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle 1

            newMap.AddRoomConnection(3, 5);
            newMap.AddRoomConnection(5, 4);
            newMap.AddRoomConnection(4, 3);

            newMap.AddRoomConnection(5, 6);

            //Cycle 2

            newMap.AddRoomConnection(6, 7);
            newMap.AddRoomConnection(7, 8);
            newMap.AddRoomConnection(8, 6);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            var allCycles = cycleReducer.AllCycles;

            Assert.AreEqual(2, allCycles.Count);
        }
示例#7
0
        public void DoesNotContainEdgeNotInMap()
        {
            //Build a simple graph

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);
            newMap.AddRoomConnection(3, 4);

            newMap.GetEdgeBetweenRooms(2, 4);
        }
示例#8
0
        public void ContainEdgeInMap()
        {
            //Build a simple graph

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            var edge = newMap.GetEdgeBetweenRooms(1, 2);

            Assert.AreEqual(edge.Source, 1);
            Assert.AreEqual(edge.Target, 2);
        }
示例#9
0
        public void ConnectionCanBeAddedFromAnotherMap()
        {
            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            ConnectivityMap newMap2 = new ConnectivityMap();

            newMap2.AddRoomConnection(3, 4);
            newMap2.AddRoomConnection(4, 5);

            newMap.AddAllConnections(newMap2);

            CollectionAssert.AreEquivalent(new List <Connection>(new Connection[] { new Connection(1, 2), new Connection(2, 3),
                                                                                    new Connection(3, 4), new Connection(4, 5) }), newMap.GetAllConnections().ToList());
        }
示例#10
0
        public void MapCycleReducerMapsContainsCorrectMappingForUnchangedEdgesInReducedMap()
        {
            //Build a graph with one nested cycles

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            //Exterior connections

            newMap.AddRoomConnection(5, 6);
            newMap.AddRoomConnection(6, 7);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);
            var             edgeMapping  = cycleReducer.edgeMappingNoCycleToFullMap;

            Assert.AreEqual(new Connection(1, 2), edgeMapping[new Connection(1, 2)].Ordered);
            Assert.AreEqual(new Connection(2, 3), edgeMapping[new Connection(2, 3)].Ordered);
            Assert.AreEqual(new Connection(6, 7), edgeMapping[new Connection(6, 7)].Ordered);
        }
示例#11
0
        private MapInfoBuilder GetStandardMapInfoBuilder()
        {
            var builder = new MapInfoBuilder();

            var l1ConnectivityMap = new ConnectivityMap();

            l1ConnectivityMap.AddRoomConnection(1, 2);
            l1ConnectivityMap.AddRoomConnection(2, 3);

            var l1RoomList = new List <TemplatePositioned>();
            var room1      = new TemplatePositioned(1, 1, 0, null, 1);

            l1RoomList.Add(room1);
            l1RoomList.Add(new TemplatePositioned(1, 1, 0, null, 2));
            l1RoomList.Add(new TemplatePositioned(1, 1, 0, null, 3));

            var l2ConnectivityMap = new ConnectivityMap();

            l2ConnectivityMap.AddRoomConnection(5, 6);
            l2ConnectivityMap.AddRoomConnection(6, 7);

            var l2RoomList = new List <TemplatePositioned>();
            var room5      = new TemplatePositioned(1, 1, 0, null, 5);

            l2RoomList.Add(room5);
            l2RoomList.Add(new TemplatePositioned(1, 1, 0, null, 6));
            l2RoomList.Add(new TemplatePositioned(1, 1, 0, null, 7));

            var l1DoorDict = new Dictionary <Connection, Point>();

            l1DoorDict.Add(new Connection(2, 3), new Point(5, 5));

            var l2DoorDict = new Dictionary <Connection, Point>();

            l2DoorDict.Add(new Connection(5, 6), new Point(8, 8));

            builder.AddConstructedLevel(0, l1ConnectivityMap, l1RoomList, l1DoorDict, 1);
            builder.AddConstructedLevel(1, l2ConnectivityMap, l2RoomList, l2DoorDict, new Connection(3, 5));

            return(builder);
        }
示例#12
0
        public void CluesCanBePlacedInRoomIfOnlyOneRoomPossible()
        {
            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);

            var mapNoCycles = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);
            var mapMST      = new MapMST(mapNoCycles.mapNoCycles.Edges);
            var manager     = new DoorAndClueManager(mapNoCycles, 1);

            Assert.IsNotNull(manager.PlaceDoorAndClue(new DoorRequirements(new Connection(1, 2), "lock0"), 1));
        }
示例#13
0
        public void MapCycleReducerRemovesOneCycleInInputMap()
        {
            //Build a graph with one nested cycle

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            newMap.AddRoomConnection(5, 6);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            var roomMapping = cycleReducer.roomMappingFullToNoCycleMap;

            //Confirm that all the cycle nodes are mapped to the first node
            Assert.AreEqual(roomMapping[3], 3);
            Assert.AreEqual(roomMapping[4], 3);
            Assert.AreEqual(roomMapping[5], 3);
        }
示例#14
0
        public void MapCycleReducerMapsReducedCycleNodeBackToOriginalNodes()
        {
            //Build a graph with one nested cycles

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            newMap.AddRoomConnection(5, 6);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            var roomMapping = cycleReducer.roomMappingNoCycleToFullMap;

            //Confirm that all the first node in the cycle maps back to all the collapsed nodes
            CollectionAssert.AreEquivalent(new List <int> {
                3, 4, 5
            }, roomMapping[3]);
        }
示例#15
0
        public void SingleCycleInMapCanBeRetrievedWhenCycleSpecifiedInADifferentOrder()
        {
            //Build a graph with one nested cycle

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 5);
            newMap.AddRoomConnection(5, 4);
            newMap.AddRoomConnection(4, 3);

            newMap.AddRoomConnection(5, 6);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            var allCycles      = cycleReducer.AllCycles;
            var expectedCycles = new List <List <Connection> > {
                new List <Connection> {
                    new Connection(3, 5),
                    new Connection(5, 4),
                    new Connection(4, 3)
                }
            };

            //Can't know whether the list will be returned in 'clockwise' or 'anticlockwise' ordering
            Assert.AreEqual(allCycles.Count, expectedCycles.Count);
            CollectionAssert.AreEquivalent(allCycles[0], expectedCycles[0]);
        }
示例#16
0
        public void MapCycleReducerMapsNonReducedNodeBackToThemselves()
        {
            //Build a graph with one nested cycles

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            newMap.AddRoomConnection(5, 6);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            var roomMapping = cycleReducer.roomMappingNoCycleToFullMap;

            //Confirm that all the first node in the cycle maps back to all the collapsed nodes
            CollectionAssert.AreEquivalent(new List <int>(new int[] { 1 }), roomMapping[1]);
            CollectionAssert.AreEquivalent(new List <int>(new int[] { 2 }), roomMapping[2]);
            CollectionAssert.AreEquivalent(new List <int>(new int[] { 6 }), roomMapping[6]);
        }
示例#17
0
        /// <summary>
        /// Add second or subsequent level.
        /// </summary>
        public void AddConstructedLevel(int levelNo, ConnectivityMap levelMap, List <TemplatePositioned> roomsInLevelCoords, Dictionary <Connection, Point> doorsInLevel, Connection connectionBetweenLevels)
        {
            if (connectivityMap.Count == 0)
            {
                throw new ApplicationException("Need to add first level before using this method");
            }

            AddConstructedLevelItems(levelNo, levelMap, roomsInLevelCoords, doorsInLevel);

            //Combine into full map
            fullMap.AddAllConnections(levelMap);
            fullMap.AddRoomConnection(connectionBetweenLevels);
        }
示例#18
0
        public void MapsFromDifferentLevelsCanBeConnected()
        {
            var mapInfo = new MapInfoBuilder();

            var l1ConnectivityMap = new ConnectivityMap();

            l1ConnectivityMap.AddRoomConnection(1, 2);
            l1ConnectivityMap.AddRoomConnection(2, 3);

            var l2ConnectivityMap = new ConnectivityMap();

            l2ConnectivityMap.AddRoomConnection(5, 6);
            l2ConnectivityMap.AddRoomConnection(6, 7);

            mapInfo.AddConstructedLevel(0, l1ConnectivityMap, new List <TemplatePositioned>(), new Dictionary <Connection, Point>(), 0);
            mapInfo.AddConstructedLevel(1, l2ConnectivityMap, new List <TemplatePositioned>(), new Dictionary <Connection, Point>(), new Connection(3, 5));

            ConnectivityMap fullMap = mapInfo.FullConnectivityMap;

            CollectionAssert.AreEquivalent(new List <Connection>(new Connection[] { new Connection(1, 2), new Connection(2, 3),
                                                                                    new Connection(3, 5), new Connection(5, 6), new Connection(6, 7) }), fullMap.GetAllConnections().ToList());
        }
示例#19
0
        public void DeadEndNodesAreFoundInSinglePathGraphs()
        {
            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            var mapNoCycles = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);
            var mapH        = new MapHeuristics(mapNoCycles, 1);

            var expectedConnections = new Dictionary <int, List <Connection> > {
                { 0, new List <Connection>(new Connection[] {
                        new Connection(1, 2),
                        new Connection(2, 3)
                    }) }
            };

            var terminalConnectionsFound = mapH.GetTerminalBranchConnections();

            //TODO: We have slightly pathological behaviour that all non-terminal node connections
            //will be double counted in the
            CollectionAssert.AreEquivalent(expectedConnections[0], terminalConnectionsFound[0]);
            CollectionAssert.AreEquivalent(expectedConnections.Keys, terminalConnectionsFound.Keys);
        }
示例#20
0
        public void MapCycleReducerMapsExterorEdgesOfCycleToOriginalConnections()
        {
            //Build a graph with one nested cycles

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            //Exterior connections

            newMap.AddRoomConnection(5, 6);
            newMap.AddRoomConnection(4, 7);
            newMap.AddRoomConnection(4, 8);
            newMap.AddRoomConnection(3, 9);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            var edgeMapping = cycleReducer.edgeMappingNoCycleToFullMap;

            //Exterior connections from start node
            Assert.AreEqual(new Connection(2, 3), edgeMapping[new Connection(2, 3)].Ordered);
            Assert.AreEqual(new Connection(3, 9), edgeMapping[new Connection(3, 9)].Ordered);

            //Exterior connections from collasped nodes
            Assert.AreEqual(new Connection(4, 7), edgeMapping[new Connection(3, 7)].Ordered);
            Assert.AreEqual(new Connection(4, 8), edgeMapping[new Connection(3, 8)].Ordered);

            Assert.AreEqual(new Connection(5, 6), edgeMapping[new Connection(3, 6)].Ordered);
        }
示例#21
0
        public void RoomsCanBeRetrievedByIndex()
        {
            var newTemplate    = new TemplatePositioned(9, 9, 0, null, 100);
            var mapInfoBuilder = new MapInfoBuilder();

            var templateList = new List <TemplatePositioned>();

            templateList.Add(newTemplate);

            var map = new ConnectivityMap();

            map.AddRoomConnection(new Connection(100, 101));

            mapInfoBuilder.AddConstructedLevel(0, map, templateList, new Dictionary <Connection, Point>(), 100);

            var mapInfo = new MapInfo(mapInfoBuilder);

            Assert.AreEqual(new Point(9, 9), mapInfo.GetRoom(100).Location);
        }
示例#22
0
        public void MapWithTwoNestedCyclesGivesCorrectTwoCyclesInAllCycles()
        {
            //Build a graph with one nested cycle

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle 1
            newMap.AddRoomConnection(3, 5);
            newMap.AddRoomConnection(5, 4);
            newMap.AddRoomConnection(4, 3);

            //Nested cycle
            newMap.AddRoomConnection(4, 6);
            newMap.AddRoomConnection(5, 6);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            //Relying on the fact that the DFS hits 3,5 first
            var allCycles      = cycleReducer.AllCycles;
            var expectedCycles = new List <List <Connection> > {
                new List <Connection> {
                    new Connection(3, 5),
                    new Connection(5, 4),
                    new Connection(4, 3)
                },
                new List <Connection> {
                    new Connection(4, 6),
                    new Connection(6, 5),
                    new Connection(5, 4)
                }
            };

            Assert.AreEqual(2, allCycles.Count);
            CollectionAssert.AreEquivalent(expectedCycles[0], allCycles[0]);
            CollectionAssert.AreEquivalent(expectedCycles[1], allCycles[1]);
        }
示例#23
0
        public void SquashedEdgeIsNotInMapAfterCycleReduction()
        {
            //Build a graph with one nested cycle

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            newMap.AddRoomConnection(5, 6);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            Assert.IsFalse(cycleReducer.IsEdgeInRoomsNoCycles(3, 4));
        }
示例#24
0
        public void RemovedEdgeNotFoundInMapCycleReducer()
        {
            //Build a graph with one nested cycle

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            newMap.AddRoomConnection(5, 6);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            cycleReducer.GetEdgeBetweenRoomsNoCycles(4, 5);
        }
示例#25
0
        public void EdgeBetweenNonCycleAndCycleMapsToBottleneckEdge()
        {
            //Build a graph with one nested cycle

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            newMap.AddRoomConnection(5, 6);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            //In the reduced graph, we have the edge between the first node of the cycle and the next node
            //In the full graph, we have the bottleneck edge between the cycle and the next node

            Assert.AreEqual(cycleReducer.edgeMappingNoCycleToFullMap[new Connection(3, 6)], new Connection(5, 6));
        }
示例#26
0
        private ConnectivityMap BuildStandardTestMap()
        {
            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Branch

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(3, 5);
            newMap.AddRoomConnection(5, 6);

            //Cycle

            newMap.AddRoomConnection(2, 8);
            newMap.AddRoomConnection(2, 7);
            newMap.AddRoomConnection(8, 9);
            newMap.AddRoomConnection(7, 9);

            //Post-cycle

            newMap.AddRoomConnection(9, 10);
            newMap.AddRoomConnection(10, 11);
            newMap.AddRoomConnection(11, 12);
            newMap.AddRoomConnection(11, 13);

            //2-way branches
            newMap.AddRoomConnection(13, 14);
            newMap.AddRoomConnection(13, 15);

            //Save to disk
            GraphvizExport.OutputUndirectedGraph(newMap.RoomConnectionGraph, "standard-test-map");

            return(newMap);
        }
示例#27
0
        /// <summary>
        /// Join 2 doors with a corridor. They must be on the opposite sides of their parent rooms (for now)
        /// </summary>
        public bool JoinDoorsWithCorridor(DoorInfo firstDoor, DoorInfo secondDoor, RoomTemplate corridorTemplate)
        {
            try
            {
                if (connectionDoors.ContainsKey(new Connection(firstDoor.OwnerRoomIndex, secondDoor.OwnerRoomIndex).Ordered))
                {
                    LogFile.Log.LogEntryDebug("No allowing 2nd connection between rooms for now - revisit past 7DRL", LogDebugLevel.High);
                    return(false);
                }

                var firstDoorLoc  = RoomTemplateUtilities.GetDoorLocation(firstDoor.OwnerRoom.Room, firstDoor.DoorIndexInRoom);
                var secondDoorLoc = RoomTemplateUtilities.GetDoorLocation(secondDoor.OwnerRoom.Room, secondDoor.DoorIndexInRoom);

                var firstDoorCoord  = firstDoor.MapCoords;
                var secondDoorCoord = secondDoor.MapCoords;

                var corridorTermini = RoomTemplateUtilities.CorridorTerminalPointsBetweenDoors(firstDoor.MapCoords, firstDoor.DoorLocation, secondDoor.MapCoords, secondDoor.DoorLocation);

                bool canDoLSharedCorridor  = RoomTemplateUtilities.CanBeConnectedWithLShapedCorridor(firstDoorCoord, firstDoorLoc, secondDoorCoord, secondDoorLoc);
                bool canDoBendCorridor     = RoomTemplateUtilities.CanBeConnectedWithBendCorridor(firstDoorCoord, firstDoorLoc, secondDoorCoord, secondDoorLoc);
                bool canDoStraightCorridor = RoomTemplateUtilities.CanBeConnectedWithStraightCorridor(firstDoorCoord, firstDoorLoc, secondDoorCoord, secondDoorLoc);
                bool areAdjacent           = corridorTermini.Item1 == secondDoorCoord && corridorTermini.Item2 == firstDoorCoord;
                bool areOverlapping        = firstDoorCoord == secondDoorCoord;

                if (!canDoLSharedCorridor && !canDoBendCorridor && !canDoStraightCorridor && !areAdjacent && !areOverlapping)
                {
                    throw new ApplicationException("No corridor available to connect this type of door");
                }

                if (areAdjacent || areOverlapping)
                {
                    //Add a direct connection in the connectivity graph
                    connectivityMap.AddRoomConnection(firstDoor.OwnerRoomIndex, secondDoor.OwnerRoomIndex);
                    connectionDoors.Add(new Connection(firstDoor.OwnerRoomIndex, secondDoor.OwnerRoomIndex).Ordered, firstDoor);
                }
                else
                {
                    //Create template

                    var horizontal = false;
                    if (firstDoorLoc == RoomTemplate.DoorLocation.Left || firstDoorLoc == RoomTemplate.DoorLocation.Right)
                    {
                        horizontal = true;
                    }

                    int xOffset = corridorTermini.Item2.x - corridorTermini.Item1.x;
                    int yOffset = corridorTermini.Item2.y - corridorTermini.Item1.y;

                    RoomTemplate expandedCorridor;
                    Point        corridorTerminus1InTemplate;

                    if (canDoBendCorridor)
                    {
                        int transition = (int)Math.Floor(yOffset / 2.0);
                        if (horizontal == true)
                        {
                            transition = (int)Math.Floor(xOffset / 2.0);
                        }
                        var expandedCorridorAndPoint = RoomTemplateUtilities.ExpandCorridorTemplateBend(xOffset, yOffset, transition, horizontal, corridorTemplate);
                        expandedCorridor            = expandedCorridorAndPoint.Item1;
                        corridorTerminus1InTemplate = expandedCorridorAndPoint.Item2;
                    }
                    else if (canDoLSharedCorridor)
                    {
                        var expandedCorridorAndPoint = RoomTemplateUtilities.ExpandCorridorTemplateLShaped(xOffset, yOffset, horizontal, corridorTemplate);
                        expandedCorridor            = expandedCorridorAndPoint.Item1;
                        corridorTerminus1InTemplate = expandedCorridorAndPoint.Item2;
                    }
                    else
                    {
                        var offsetToUse = horizontal ? xOffset : yOffset;
                        var expandedCorridorAndPoint = RoomTemplateUtilities.ExpandCorridorTemplateStraight(offsetToUse, horizontal, corridorTemplate);
                        expandedCorridor            = expandedCorridorAndPoint.Item1;
                        corridorTerminus1InTemplate = expandedCorridorAndPoint.Item2;
                    }

                    //Place corridor

                    //Match corridor tile to location of door
                    Point topLeftCorridor = corridorTermini.Item1 - corridorTerminus1InTemplate;

                    var corridorRoomIndex  = NextRoomIndex();
                    var positionedCorridor = new TemplatePositioned(topLeftCorridor.x, topLeftCorridor.y, 0, expandedCorridor, corridorRoomIndex);

                    if (!mapBuilder.CanBePlacedWithoutOverlappingOtherTemplates(positionedCorridor))
                    {
                        return(false);
                    }

                    //Place the corridor
                    mapBuilder.AddPositionedTemplate(positionedCorridor);
                    templates[corridorRoomIndex] = positionedCorridor;
                    IncreaseNextRoomIndex();

                    //Add connections to the old and new rooms
                    connectivityMap.AddRoomConnection(firstDoor.OwnerRoomIndex, corridorRoomIndex);
                    connectivityMap.AddRoomConnection(corridorRoomIndex, secondDoor.OwnerRoomIndex);

                    connectionDoors.Add(new Connection(firstDoor.OwnerRoomIndex, corridorRoomIndex).Ordered, firstDoor);
                    connectionDoors.Add(new Connection(corridorRoomIndex, secondDoor.OwnerRoomIndex).Ordered, secondDoor);
                }

                //Remove both doors from the potential list
                potentialDoors.Remove(firstDoor);
                potentialDoors.Remove(secondDoor);

                return(true);
            }
            catch (ApplicationException ex)
            {
                LogFile.Log.LogEntryDebug("Failed to join doors: " + ex.Message, LogDebugLevel.Medium);
                return(false);
            }
        }
示例#28
0
        public void MapCycleReducerRemovesMultipleCyclesInInputMap()
        {
            //Build a graph with one two nested cycles

            ConnectivityMap newMap = new ConnectivityMap();

            newMap.AddRoomConnection(1, 2);
            newMap.AddRoomConnection(2, 3);

            //Cycle

            newMap.AddRoomConnection(3, 4);
            newMap.AddRoomConnection(4, 5);
            newMap.AddRoomConnection(3, 5);

            newMap.AddRoomConnection(5, 6);

            //Cycle

            newMap.AddRoomConnection(6, 7);
            newMap.AddRoomConnection(7, 8);
            newMap.AddRoomConnection(8, 9);
            newMap.AddRoomConnection(9, 10);
            newMap.AddRoomConnection(10, 11);
            newMap.AddRoomConnection(11, 6);
            newMap.AddRoomConnection(9, 6);

            newMap.AddRoomConnection(11, 12);

            MapCycleReducer cycleReducer = new MapCycleReducer(newMap.RoomConnectionGraph.Edges);

            var roomMapping = cycleReducer.roomMappingFullToNoCycleMap;

            //Confirm that all the first cycle nodes are mapped to the first node in the cycle
            Assert.AreEqual(3, roomMapping[3]);
            Assert.AreEqual(3, roomMapping[4]);
            Assert.AreEqual(3, roomMapping[5]);

            //Confirm that all the second cycle nodes are mapped to the first node in the second cycle
            Assert.AreEqual(6, roomMapping[6]);
            Assert.AreEqual(6, roomMapping[7]);
            Assert.AreEqual(6, roomMapping[8]);
            Assert.AreEqual(6, roomMapping[9]);
            Assert.AreEqual(6, roomMapping[10]);
            Assert.AreEqual(6, roomMapping[11]);
        }