Ejemplo n.º 1
0
 /// <summary>
 /// Constructor for the layout cell
 /// </summary>
 /// <param name="x">the current x</param>
 /// <param name="y">the current y</param>
 /// <param name="type">the cell type</param>
 /// <param name="node">the corresponding mission graph node</param>
 public Cell(int x, int y, CellType type, MissionGraph.Node node)
 {
     this.x         = x;
     this.y         = y;
     this.type      = type;
     this.node      = node;
     this.doorTypes = new DoorType[4];
     this.parent    = null;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Start the map by adding the first cell that correspond to the starting node of the mission graph
        /// </summary>
        /// <param name="node">the starting cell that has only to be connected randomly from one direction</param>
        public void initializeCell(MissionGraph.Node node)
        {
            Cell start = new Cell(0, 0, CellType.Normal, node);

            this.usedSpaces.Add(start.getLocationString(), start);
            this.openLocations.Add("0,0", new List <OpenNode>());
            int[] randDir = this.directions[this.random.Next(this.directions.Count)];
            this.openLocations["0,0"].Add(new OpenNode(randDir[0], randDir[1], start));
            // foreach(int[] dir in this.directions){
            //     this.openLocations["0,0"].Add(new OpenNode(dir[0], dir[1], start));
            // }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Add a new cell to the layout that correspond to a certain node in the mission graph
 /// </summary>
 /// <param name="node">corresponding node in the mission graph</param>
 /// <param name="parentID">the id of the parent that the new cell should be connected to</param>
 /// <returns>True if it succeed and False otherwise</returns>
 public bool addCell(MissionGraph.Node node, int parentID)
 {
     if (node.type == MissionGraph.NodeType.Lock)
     {
         OpenNode selected = this.getWorkingLocation(parentID, node.accessLevel - 1);
         if (selected == null)
         {
             return(false);
         }
         Cell newCell = new Cell(selected.x, selected.y, CellType.Normal, node);
         newCell.connectCells(selected.parent, DoorType.KeyLock);
         newCell.parent = selected.parent;
         this.addNewNode(newCell, node.accessLevel, node.id);
     }
     else if (node.type == MissionGraph.NodeType.Puzzle)
     {
         OpenNode selected = this.getWorkingLocation(parentID, node.accessLevel);
         if (selected == null)
         {
             return(false);
         }
         Cell newCell = new Cell(selected.x, selected.y, CellType.Normal, node);
         newCell.connectCells(selected.parent, DoorType.Open);
         newCell.parent = selected.parent;
         this.addNewNode(newCell, node.accessLevel + 1, node.id);
     }
     else if (node.type == MissionGraph.NodeType.Lever)
     {
         OpenNode selected = this.getWorkingLocation(parentID, node.accessLevel);
         if (selected == null)
         {
             return(false);
         }
         Cell newCell = new Cell(selected.x, selected.y, CellType.Normal, node);
         newCell.connectCells(selected.parent, DoorType.Open);
         newCell.parent = selected.parent;
         this.usedSpaces.Add(newCell.getLocationString(), newCell);
     }
     else
     {
         OpenNode selected = this.getWorkingLocation(parentID, node.accessLevel);
         if (selected == null)
         {
             return(false);
         }
         Cell newCell = new Cell(selected.x, selected.y, CellType.Normal, node);
         if (selected.parent.node.type == MissionGraph.NodeType.Puzzle)
         {
             newCell.connectCells(selected.parent, DoorType.PuzzleLock);
         }
         else if (selected.parent.node.type == MissionGraph.NodeType.Lever)
         {
             newCell.connectCells(selected.parent, DoorType.LeverLock);
         }
         else
         {
             newCell.connectCells(selected.parent, DoorType.Open);
         }
         if (node.getChildren().Count == 0)
         {
             this.usedSpaces.Add(newCell.getLocationString(), newCell);
         }
         else
         {
             this.addNewNode(newCell, node.accessLevel, parentID);
         }
         newCell.parent = selected.parent;
     }
     return(true);
 }
        /// <summary>
        /// Generate a map layout that correspond to the input mission graph
        /// </summary>
        /// <param name="graph">the mission graph that need to be mapped to a 2D layout</param>
        /// <returns>a 2D layout of the mission graph</returns>
        public Map GenerateDungeon(MissionGraph.Graph graph)
        {
            Map result = new Map(this.random);

            result.InitializeCell(graph.nodes[0]);

            #region  make initial dungeon

            List <MissionGraph.Node>            open      = new List <MissionGraph.Node>();
            Dictionary <MissionGraph.Node, int> parentIDs = new Dictionary <MissionGraph.Node, int>();
            foreach (MissionGraph.Node child in graph.nodes[0].GetChildren())
            {
                open.Add(child);
                parentIDs.Add(child, 0);
            }

            HashSet <MissionGraph.Node> nodes = new HashSet <MissionGraph.Node>();
            nodes.Add(graph.nodes[0]);
            while (open.Count > 0)
            {
                MissionGraph.Node current = open[0];
                open.RemoveAt(0);
                if (nodes.Contains(current))
                {
                    continue;
                }

                nodes.Add(current);
                if (!result.AddCell(current, parentIDs[current]))
                {
                    return(null);
                }

                foreach (MissionGraph.Node child in current.GetChildren())
                {
                    if (!parentIDs.ContainsKey(child))
                    {
                        if (current.type == MissionGraph.NodeType.Lock ||
                            current.type == MissionGraph.NodeType.Puzzle)
                        {
                            parentIDs.Add(child, current.id);
                        }
                        else
                        {
                            parentIDs.Add(child, parentIDs[current]);
                        }
                    }

                    open.Add(child);
                }
            }

            #endregion

            #region make lever connections

            open.Clear();
            nodes.Clear();
            open.Add(graph.nodes[0]);
            while (open.Count > 0)
            {
                MissionGraph.Node current = open[0];
                open.RemoveAt(0);
                if (nodes.Contains(current))
                {
                    continue;
                }

                nodes.Add(current);
                foreach (MissionGraph.Node child in current.GetChildren())
                {
                    Cell from = result.GetCell(current.id);
                    Cell to   = result.GetCell(child.id);
                    if (current.type == MissionGraph.NodeType.Lever)
                    {
                        if (!result.MakeConnection(from, to, nodes.Count * nodes.Count))
                        {
                            return(null);
                        }
                    }

                    open.Add(child);
                }
            }

            #endregion

            return(result);
        }