public override Map GenerateMap(Stairs incommingStairs, int level)
        {
            for (int i = 0; i < 5; ++i)
            {
                m_level = level;
                Map map = new Map(Width, Height);

                MapNode graphHead = m_graphGenerator.GenerateMapGraph();

                ParenthoodChain parentChain = new ParenthoodChain();

                GenerateMapFromGraph(graphHead, map, Point.Invalid, parentChain);

                Point upperLeft = new Point(m_smallestX, m_smallestY);
                Point lowerRight = new Point(m_largestX, m_largestY);

                map.TrimToSubset(upperLeft, lowerRight);

                if (!CheckConnectivity(map))
                    continue;

                if (m_placed < 30)
                    continue;

                GenerateUpDownStairs(map, incommingStairs);

                StripImpossibleDoors(map);

                return map;
            }
            throw new MapGenerationFailureException("Unable to generate Stitch map with multiple attempts.");
        }
 public ParenthoodChain(ParenthoodChain p)
 {
     // There is no Stack constructor that take a stack, just the enumerable one
     // Since we want a shallow copy, we'll just reverse the list and use that
     Chunks = new Stack<MapChunk>(p.Chunks.Reverse());
     UpperLefts = new Stack<Point>(p.UpperLefts.Reverse());
     Seams = new Stack<Point>(p.Seams.Reverse());
 }
 private void WalkNeighbors(MapNode currentNode, MapChunk currentChunk, Map map, Point upperLeft, ParenthoodChain parentChain)
 {
     while (currentNode.Neighbors.Count > 0)
     {
         MapNode nextNode = currentNode.Neighbors[0];
         nextNode.RemoveNeighbor(currentNode);
         currentNode.RemoveNeighbor(nextNode);
         Point seam = currentChunk.Seams[0];
         currentChunk.Seams.Remove(seam);
         GenerateMapFromGraph(nextNode, map, seam + upperLeft, parentChain);
     }
 }
 // Returns true if placed, false if we walled off seam
 private bool PlaceMapNode(MapNode current, MapChunk mapChunk, Map map, Point seam, ParenthoodChain parentChain)
 {
     Point placedUpperLeftCorner = mapChunk.PlaceChunkOnMap(map, seam, m_level);
     if (placedUpperLeftCorner == Point.Invalid)
     {
         map.SetTerrainAt(seam, TerrainType.Wall);
         return false;
     }
     else
     {
         PossiblyUpdateLargestSmallestPoint(placedUpperLeftCorner, mapChunk);
         map.SetTerrainAt(seam, TerrainType.Floor);
         parentChain.Push(mapChunk, placedUpperLeftCorner, seam);
         WalkNeighbors(current, mapChunk, map, placedUpperLeftCorner, parentChain);
         parentChain.Pop();
         return true;
     }
 }
 private void UnplacePossibleHallwayToNowhere(Map map, ParenthoodChain parentChain)
 {
     ParenthoodChain localChain = new ParenthoodChain(parentChain);
     ParenthoodElement top = localChain.Pop();
     Point seamToFill = Point.Invalid;
     if (top.Chunk.Type == MapNodeType.Hall)
     {
         do
         {
             if (top.Chunk.Type == MapNodeType.Hall)
             {
                 m_placed--;
                 top.Chunk.UnplaceChunkOnMapAtPosition(map, top.UpperLeft);
                 seamToFill = top.Seam;
                 top = localChain.Pop();
             }
             else
             {
                 if (seamToFill == Point.Invalid)
                     throw new MapGenerationFailureException("Trying to fill in invalid seam");
                 map.SetTerrainAt(seamToFill, TerrainType.Wall);
                 break;
             }
         }
         while (true);
     }
 }
        private void GenerateMapFromGraph(MapNode current, Map map, Point seam, ParenthoodChain parentChain)
        {
            if (current.Generated)
                return;

            current.Generated = true;
            bool placed = false;

            switch (current.Type)
            {
                case MapNodeType.Entrance:
                {
                    placed = true;
                    MapChunk entranceChunk = GetRandomChunkFromList(m_entrances);

                    // We need to place entrace so it's at our expected location
                    Point randomCenter = new Point(m_random.getInt(100, 150), m_random.getInt(100, 150));
                    Point entraceUpperLeftCorner = randomCenter - entranceChunk.PlayerPosition;

                    parentChain.Push(entranceChunk, entraceUpperLeftCorner, Point.Invalid);

                    entranceChunk.PlaceChunkOnMapAtPosition(map, entraceUpperLeftCorner, m_random);
                    PossiblyUpdateLargestSmallestPoint(entraceUpperLeftCorner, entranceChunk);
                    map.AddMapItem(MapObjectFactory.Instance.CreateMapObject("StairsUp", entranceChunk.PlayerPosition + entraceUpperLeftCorner));

                    if (current.Neighbors.Count != entranceChunk.Seams.Count)
                        throw new InvalidOperationException("Number of neighbors should equal number of seams.");
                    WalkNeighbors(current, entranceChunk, map, entraceUpperLeftCorner, parentChain);

                    parentChain.Pop();

                    break;
                }
                case MapNodeType.Hall:
                {
                    for (int i = 0; i < 10; i++)
                    {
                        placed = PlaceMapNode(current, GetRandomChunkFromList(m_halls), map, seam, parentChain);
                        if (placed)
                            break;
                    }
                    break;
                }
                case MapNodeType.None:
                {
                    // If we have nothing, see if we have any orphan nodes to try to place
                    if (m_unplacedDueToSpace.Count > 0)
                    {
                        // Grab the first unplaced node, and try again.
                        MapNode treeToGraphOn = m_unplacedDueToSpace.Dequeue();
                        treeToGraphOn.Generated = false;
                        GenerateMapFromGraph(treeToGraphOn, map, seam, parentChain);
                    }
                    else
                    {
                        map.SetTerrainAt(seam, TerrainType.Wall);
                        if (current.Neighbors.Count != 0)
                            throw new InvalidOperationException("None Node types should only have no neighbors");
                    }
                    placed = true;
                    break;
                }
                case MapNodeType.MainRoom:
                {
                    placed = PlaceMapNode(current, GetRandomChunkFromList(m_mainRooms), map, seam, parentChain);
                    break;
                }
                case MapNodeType.TreasureRoom:
                {
                    placed = PlaceMapNode(current, GetRandomChunkFromList(m_treasureRooms), map, seam, parentChain);
                    break;
                }
                case MapNodeType.SideRoom:
                {
                    placed = PlaceMapNode(current, GetRandomChunkFromList(m_sideRooms), map, seam, parentChain);
                    break;
                }
                case MapNodeType.NoneGivenYet:
                default:
                    throw new InvalidOperationException("Trying to generate MapNode from invalid node.");
            }
            if (!placed)
            {
                UnplacePossibleHallwayToNowhere(map, parentChain);
                m_unplacedDueToSpace.Enqueue(current);
            }
        }