Пример #1
0
        private void btnAccept_Click(object sender, System.EventArgs e)
        {
            Cell newCell = new Cell();
            newCell.Initialize();

            newCell.ID = Convert.ToInt32(this.tbxBlockId.Text);
            newCell.Location.X = Convert.ToInt32(this.tbxLocationX.Text);
            newCell.Location.Y = Convert.ToInt32(this.tbxLocationY.Text);
            newCell.Location.Z = Convert.ToInt32(this.tbxLocationZ.Text);
            newCell.Location.Level = Convert.ToInt32(this.tbxLocationLevel.Text);

            newCell.Type = 0;
            if (this.chkUp.Checked) newCell.Type += (uint)Directions.Up;
            if (this.chkLeft.Checked) newCell.Type += (uint)Directions.Left;
            if (this.chkDown.Checked) newCell.Type += (uint)Directions.Down;
            if (this.chkRight.Checked) newCell.Type += (uint)Directions.Right;

            newCell.Attribute = 0;
            if (this.chkStart.Checked) newCell.Attribute += (uint)CellAttributes.IsStart;
            if (this.chkFinish.Checked) newCell.Attribute += (uint)CellAttributes.IsFinish;
            if (this.chkDrop.Checked) newCell.Attribute += (uint)CellAttributes.HasDrop;

            newCell.Option = 0;
            if (this.chkPortal.Checked) newCell.Option += (uint)CellOptions.Portal;

            newCell.OptionValue = Convert.ToInt32(this.tbxOptionValue.Text);

            Program.EditorForm.AddCell(newCell);
            Program.EditorForm.Invalidate();
            this.Close();
        }
Пример #2
0
        /// <summary>
        /// Creates new random Maze with specified seed value
        /// </summary>
        /// <returns>Collection of the Maze Cells</returns>
        public List<Cell> Generate(ushort seed)
        {
            Random.Int(100);
            Random.Int(100);

            // Keeps maze size
            // Boundaries coords (for table creation)
            CoordBounds bounds;
            bounds.MaxX = 0;
            bounds.MaxY = 0;
            bounds.MinX = 0;
            bounds.MinY = 0;

            Point start;
            Point finish;
            Point currentPoint;

            start.X = 0;
            start.Y = 0;

            this.mainPath.Add(start);

            // Priority - in what direction finishPoint(relative of startPoint) will be located
            DecDirections priorityDirection = (DecDirections)(Random.Int(4) + 1);
            DecDirections currentDirection = (DecDirections)(Random.Int(4) + 1);

            //
            // Generate Main Path
            //
            currentPoint = start;

            // Main path length
            int pathLength = Random.Int(MAINPATH_LENGTH_MIN, MAINPATH_LENGTH_MAX);
            int currentLenth = 0;
            while (currentLenth < pathLength)
            {
                DecDirections oldDirection = currentDirection;
                do
                {
                    // Choose new direction (but NOT oppoite of current one)
                    // 60% chance - priority
                    // 30% chance - left or rights side of current
                    // 10% chance - opposite of priority direction
                    int rnd = Random.Int(100);
                    if (rnd >= 40)
                    {
                        currentDirection = priorityDirection;
                    }
                    else if (rnd > 10 && rnd < 40)
                    {
                        currentDirection = GetNeighborDirection(priorityDirection, Random.Int(100) > 50 ? 1 : 2);
                    }

                    else
                    {
                        currentDirection = GetOppositeDirection(priorityDirection);
                    }
                } while (GetOppositeDirection(oldDirection) == currentDirection);

                // Generate segment
                int segmentLenth = Random.Int(SEGMENT_LENGTH_MIN, SEGMENT_LENGTH_MAX);
                for (int j = 0; j < segmentLenth; ++j)
                {
                    ++currentLenth;
                    if (currentLenth > pathLength)
                        break;
                    // Next Point on direction
                    currentPoint = MoveTo(currentPoint, currentDirection);
                    // Recalc maze size
                    bounds.Recheck(currentPoint);

                    this.mainPath.Add(currentPoint);
                }

            }

            finish = currentPoint;

            // Add Main path to all maze points
            this.maze.AddRange(mainPath);

            //
            // Generate Branch Paths
            //
            int branchCount = pathLength / BRANCH_FREQUENCY - 1;
            for (int i = 0; i < branchCount; ++i)
            {
                // Total branches length relative to mainPath length
                int branchLenth = Random.Int(pathLength / 3, pathLength / 2);

                currentPoint = this.mainPath[(i + 1) * BRANCH_FREQUENCY];
                priorityDirection = (DecDirections)(Random.Int(4) + 1);

                int currentBranchLenth = 0;

                while (currentBranchLenth < branchLenth)
                {
                    DecDirections oldDirection = currentDirection;
                    do
                    {
                        // Choose new direction (but NOT oppoite of current one)
                        // 60% chance - priority
                        // 30% chance - left or rights side of current
                        // 10% chance - opposite of priority direction
                        int rnd = Random.Int(100);
                        if (rnd >= 40)
                        {
                            currentDirection = priorityDirection;
                        }
                        else if (rnd > 10 && rnd < 40)
                        {
                            currentDirection = GetNeighborDirection(priorityDirection, Random.Int(100) > 50 ? 1 : 2);
                        }
                        else
                        {
                            currentDirection = GetOppositeDirection(priorityDirection);
                        }
                    } while (GetOppositeDirection(oldDirection) == currentDirection);

                    int segmentLenth = Random.Int(BRANCH_SEGMENT_MIN, BRANCH_SEGMENT_MAX);
                    // Reduce the length of long straight segments
                    if (oldDirection == currentDirection)
                        segmentLenth /= 2;
                    for (int j = 0; j < segmentLenth; ++j)
                    {
                        currentPoint = MoveTo(currentPoint, currentDirection);

                        ++currentBranchLenth;
                        if (currentBranchLenth > branchLenth)
                            break;

                        bounds.Recheck(currentPoint);

                        this.maze.Add(currentPoint);
                    }

                }
            }

            //
            // Create Maze Table
            // 0 - Wall (no path)
            // 1 - Path;
            // 2 - Start Point
            // 3 - Finish Point
            int width = bounds.MaxX - bounds.MinX + 1 + 2;
            int height = bounds.MaxY - bounds.MinY + 1 + 2;
            int[,] mainPathMatrix = new int[width, height];

            for (int i = 0; i < width; ++i)
                for (int j = 0; j < height; ++j)
                    mainPathMatrix[i, j] = 0;

            int number;
            foreach(Point point in this.maze)
            {
                if (point.Equals(start))
                    number = 2;
                else if (point.Equals(finish))
                    number = 3;
                else
                    number = 1;

                mainPathMatrix[point.X - bounds.MinX + 1, point.Y - bounds.MinY + 1] = number;
            }

            // =====================
            // Test Part:
            // Record Maze into file
            if (IS_WRITE_MAZE_FILE)
            {
                StreamWriter fileStream = new StreamWriter("maze.txt", false);
                StringBuilder record;
                for (int j = 0; j < height; ++j)
                {
                    record = new StringBuilder(String.Empty);

                    for (int i = 0; i < width; ++i)
                    {
                        switch (mainPathMatrix[i, j])
                        {
                            case 1:
                                record.Append(" ");
                                break;
                            case 2:
                                record.Append("S");
                                break;
                            case 3:
                                record.Append("F");
                                break;
                            default:
                                record.Append("█");
                                break;
                        }
                    }
                    fileStream.WriteLine(record.ToString());

                }
                fileStream.Close();
            }
            // ======================

            //
            // FINAL: Map Cells Construct
            // Convert list of Points into list of cell blocks
            //
            List<Cell> mapBlocks = new List<Cell>();
            int idCounter = 1;
            foreach (Point point in this.maze)
            {
                Cell block = new Cell();
                block.Initialize();

                block.ID = idCounter;
                block.Location.X = point.X;
                block.Location.Y = point.Y;
                block.Location.Level = Map.Instance.CurrentLevel;

                block.Type = 0;
                // Up
                if (mainPathMatrix[point.X - bounds.MinX + 1, point.Y - bounds.MinY] > 0)
                    block.Type += (uint)Maze.Classes.Directions.Up;
                // Down
                if (mainPathMatrix[point.X - bounds.MinX + 1, point.Y - bounds.MinY + 2] > 0)
                    block.Type += (uint)Maze.Classes.Directions.Down;
                // Left
                if (mainPathMatrix[point.X - bounds.MinX, point.Y - bounds.MinY + 1] > 0)
                    block.Type += (uint)Maze.Classes.Directions.Left;
                // Right
                if (mainPathMatrix[point.X - bounds.MinX + 2, point.Y - bounds.MinY + 1] > 0)
                    block.Type += (uint)Maze.Classes.Directions.Right;

                // Start Point
                if (point.Equals(start))
                {
                    block.Attribute += (uint)CellAttributes.IsStart;
                    StartPoint = block;
                }
                // Finish Point
                if (point.Equals(finish))
                {
                    block.Attribute += (uint)CellAttributes.IsFinish;
                    FinishPoint = block;
                }

                ++idCounter;

                mapBlocks.Add(block);
            }

            return mapBlocks;
        }
Пример #3
0
        /// <summary>
        /// Gets a Cell of the map with the specified Location.
        /// </summary>
        /// <param name="location">Location where the Cell is expected to be.</param>
        /// <returns>Found Cell or default Cell value when no cells were found.</returns>
        public Cell GetCell(GridLocation location)
        {
            if (this.mapCells.ContainsKey(location))
                return this.mapCells[location];

            // return default cell
            Cell cell = new Cell();
            cell.Initialize();
            return cell;
        }
Пример #4
0
        /// <summary>
        /// Gets a Cell of the map with the specified ID.
        /// </summary>
        /// <param name="cellId">ID of the Cell.</param>
        /// <returns>Found Cell or default Cell value when no cells were found.</returns>
        public Cell GetCell(int cellId)
        {
            if (this.mapCellsIds.ContainsKey(cellId) && this.mapCells.ContainsKey(this.mapCellsIds[cellId]))
                return this.mapCells[this.mapCellsIds[cellId]];

            // return default cell
            Cell defaultCell = new Cell();
            defaultCell.Initialize();
            return defaultCell;
        }
Пример #5
0
        private Cell FindCell(List<CellParam> List, CellParam Cell)
        {
            Cell Block = new Cell();

            Block.Initialize();
            for (int i = 0; i < List.Count; ++i)
            {
                if (Cell.ID == List[i].ID)
                {
                    Block = Map.Instance.GetCell(List[i].ID);
                    break;
                }
            }
            return Block;
        }