Exemple #1
0
    public static void Main(string[] args)
    {
        Console.SetWindowSize(240, 80);

        Random      rand    = new Random();
        SegmentMask maskSet = new SegmentMask(10, 10);

        while (true)
        {
            Console.Clear();

            MazeGenerator mazeGen = new MazeGenerator(rand);
            mazeGen.SetLoopChance(0.1F);
            mazeGen.SetStraightBias(0.2F);
            MazeMap mazeMap = mazeGen.Generate(12, 8);

            CellularGenerator cellGen = new CellularGenerator(rand);
            cellGen.SetPasses(8);
            cellGen.SetFillWeight(0.5F);
            cellGen.SetThresholds(4, 4);
            CellMap cellMap = cellGen.Generate(mazeMap, maskSet);

            for (int j = 0; j < cellMap.Height(); j++)
            {
                for (int i = 0; i < cellMap.Width(); i++)
                {
                    Console.Write(GetCharFromCell(cellMap.GetSegment(new MapPos(i, j))) + " ");
                }
            }

            Console.Write("Press enter to regenerate...");
            Console.ReadLine();
        }
    }
    void OnLevelWasLoaded()
    {
        //if(GetComponent<NetworkView>().isMine){
        bool works = false;
        int  i     = 0;

        while (!works)
        {
            map = ScriptableObject.CreateInstance("MazeMap") as MazeMap;
            map.initializeMap(step, tileSet);
            created = true;
            works   = defineRooms();
        }
        foreach (LinkedRoom r in map.rooms)
        {
            //map.initializeNeighborRooms (r);
            if (r.getRoom().getState() != "null")
            {
                GetComponent <NetworkView>().RPC("loadMap", RPCMode.AllBuffered, r.getRoom().toString(), i);
                i++;
            }
        }
        //}
        GameObject[] players = GameObject.FindGameObjectsWithTag("Player") as GameObject[];
        foreach (GameObject p in players)
        {
            playerController control = p.GetComponent <playerController>() as playerController;
            control.enabled = true;
        }
    }
Exemple #3
0
    public List <EnumDirection> GetEmptySides(MazeMap map, MapPos pos, EnumDirection?lastDir)
    {
        List <EnumDirection> list = new List <EnumDirection>();
        int curMask = map.GetSegment(pos);

        Predicate <EnumDirection> dirPre = delegate(EnumDirection d)
        {
            MapPos off = pos.Offset(d);

            if ((curMask & d.BitMask()) != 0)
            {
                return(false);
            }
            else if (map.IsValid(off) && (map.GetSegment(off) <= 0 || rand.NextDouble() < lpChance))
            {
                return(true);
            }

            return(false);
        };

        list.AddAll(MethodExtensions.AllDirections(), dirPre);

        if (lastDir.HasValue && rand.NextDouble() < strBias && list.Contains(lastDir.Value))
        {
            for (int n = list.Count - 1; n >= 0; n--)
            {
                list[n] = lastDir.Value;
            }
        }

        return(list);
    }
    void Start()
    {
        bool works = false;

        Camera.main.enabled = true;
        //if(GetComponent<NetworkView>().isMine){
        while (!works)
        {
            map = ScriptableObject.CreateInstance("MazeMap") as MazeMap;
            map.initializeMap(step, tileSet);
            map.onEnable();
            //created = true;
            works = defineRooms();
        }
        created = true;
        int i = 0;

        foreach (LinkedRoom r in map.rooms)
        {
            if (r.getRoom().getState() != "null")
            {
                //GetComponent<NetworkView>().RPC ("loadMap", RPCMode.AllBuffered, r.getRoom ().toString(),i);
                i++;
            }
        }
        //}
//		GameObject[] players = GameObject.FindGameObjectsWithTag ("Player") as GameObject[];
//		foreach(GameObject p in players){
//			playerController control = p.GetComponent<playerController>() as playerController;
//			control.enabled = true;
//		}
    }
Exemple #5
0
        public void Setup()
        {
            _rooms = new List <Room>
            {
                new Room
                {
                    Id          = 0,
                    IsEntrance  = true,
                    IsVisited   = true,
                    HasTreasure = false,
                    HasTrap     = false,
                    X           = 0,
                    Y           = 0,
                    Description = Constants.RoomConstants.EntranceRoomDescription
                },
                new Room
                {
                    Id          = 1,
                    IsEntrance  = false,
                    IsVisited   = false,
                    HasTreasure = false,
                    HasTrap     = true,
                    X           = 0,
                    Y           = 1,
                    Description = Constants.RoomConstants.TrapRoomDescription
                },
                new Room
                {
                    Id          = 2,
                    IsEntrance  = false,
                    IsVisited   = false,
                    HasTreasure = true,
                    HasTrap     = false,
                    X           = 1,
                    Y           = 0,
                    Description = Constants.RoomConstants.TreasureRoomDescription
                },
                new Room
                {
                    Id          = 3,
                    IsEntrance  = false,
                    IsVisited   = false,
                    HasTreasure = false,
                    HasTrap     = false,
                    X           = 1,
                    Y           = 1,
                    Description = Constants.RoomConstants.NormalRoomDescription
                },
            };

            _mazeMap = new MazeMap
            {
                mazeRooms = _rooms,
                mazeSize  = _sizeOfMaze
            };
        }
Exemple #6
0
    public CellMap Generate(MazeMap maze, SegmentMask masks)
    {
        CellMap map = new CellMap(maze.Width() * masks.SegmentWidth(), maze.Height() * masks.SegmentHeight());

        BakeMask(map, maze, masks);
        SeedMap(map);

        for (int n = 0; n < passes; n++)
        {
            DoPass(map);
        }

        return(map);
    }
Exemple #7
0
    public MazeMap Generate(int sizeX, int sizeY)
    {
        MazeMap map = new MazeMap(sizeX, sizeY);

        List <MapPos> pending = new List <MapPos>();

        pending.Add(new MapPos(map.Width() / 2, map.Height() / 2));

        EnumDirection?lastDir = null;

        while (pending.Count > 0)
        {
            MapPos curPos             = pending[0];
            int    curSeg             = map.GetSegment(curPos);
            List <EnumDirection> dirs = GetEmptySides(map, curPos, lastDir);

            if (lastDir.HasValue)
            {
                curSeg |= lastDir.Value.Opposite().BitMask();
                map.SetSegment(curPos, curSeg);
            }

            if (dirs.Count <= 0)
            {
                lastDir = null;
                pending.RemoveAt(0);
                pending.Shuffle(rand);
            }
            else
            {
                EnumDirection nxtDir = dirs[rand.Next(dirs.Count)];
                curSeg |= nxtDir.BitMask();
                map.SetSegment(curPos, curSeg);
                lastDir = nxtDir;

                if (dirs.Count == 1)
                {
                    pending.RemoveAt(0);
                }

                pending.Insert(0, curPos.Offset(nxtDir));
            }
        }

        return(map);
    }
Exemple #8
0
        public MazeMap GetMazeMapInstance()
        {
            if (_mazeMapInstance == null)
            {
                lock (_Lock)
                {
                    if (_mazeMapInstance == null)
                    {
                        _mazeMapInstance = new MazeMap {
                            mazeRooms = new List <Room>()
                        };
                    }
                }
            }

            return(_mazeMapInstance);
        }
Exemple #9
0
    private void BakeMask(CellMap map, MazeMap maze, SegmentMask masks)
    {
        for (int i1 = 0; i1 < maze.Width(); i1++)
        {
            for (int j1 = 0; j1 < maze.Height(); j1++)
            {
                int segID = maze.GetSegment(new MapPos(i1, j1));

                for (int i2 = 0; i2 < masks.SegmentWidth(); i2++)
                {
                    for (int j2 = 0; j2 < masks.SegmentHeight(); j2++)
                    {
                        MapPos pos = new MapPos(i1 * masks.SegmentWidth() + i2, j1 * masks.SegmentHeight() + j2);
                        map.SetSegment(pos, masks.GetMask(segID).GetSegment(new MapPos(i2, j2)));
                    }
                }
            }
        }
    }
Exemple #10
0
        public char GetCharAtPosition(Position position)
        {
            if (position.X < 0 || position.Y < 0)
            {
                throw new ArgumentOutOfRangeException("Arguments 'Position' coordinates must be a positive number");
            }
            if (position.X > Height || position.Y > Width)
            {
                throw new IndexOutOfRangeException("Position does not exist on Maz's map");
            }

            var positionToFind = MazeMap.FirstOrDefault(m => m.Item1.X == position.X && m.Item1.Y == position.Y);

            if (positionToFind == null)
            {
                throw new IndexOutOfRangeException("Position does not exist on Maz's map");
            }

            return(positionToFind.Item2);
        }
Exemple #11
0
        public void CreateMazeMap(string[] lines)
        {
            if (lines == null && lines.Length == 0)
            {
                throw new ArgumentException("The 'lines' argument should not be null");
            }

            Width  = lines[0].Length;
            Height = lines.Length;
            MazeMap.Clear();

            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i];
                for (int j = 0; j < line.Length; j++)
                {
                    MazeMap.Add(new Tuple <Position, char>(new Position()
                    {
                        Y = i, X = j
                    }, line[j]));
                    if (line[j] == StartSymbol)
                    {
                        StartPosition = new Position()
                        {
                            Y = i, X = j
                        }
                    }
                    ;
                    else if (line[j] == FinishSymbol)
                    {
                        FinishPosition = new Position()
                        {
                            Y = i, X = j
                        }
                    }
                    ;
                }
            }
        }
    //-------------------------


    public void GenerateMaze(MazeMap maze, bool calledFromEditor = false)
    {
        string holderName = "Maze";

        if (GameObject.Find(holderName))
        {
            DestroyImmediate(GameObject.Find(holderName));
        }

        Renderer   tileRender     = tilePrefab.GetComponent <Renderer>();
        Vector2    tileSize       = new Vector2(tileRender.bounds.size.x, tileRender.bounds.size.z);
        GameObject mazeGameObject = new GameObject(holderName);

        if (!calledFromEditor)
        {
            TilesMatrix = new Material[(int)maze.Size.x, (int)maze.Size.y];
        }
        for (int row = 0; row < maze.Size.x; row++)
        {
            for (int colum = 0; colum < maze.Size.y; colum++)
            {
                Vector3   tilePosition = new Vector3(-maze.Size.x / 2 + (row * tileSize.x), 0, -maze.Size.y / 2 + (colum * tileSize.y));
                Transform tile         = Instantiate(tilePrefab, tilePosition, Quaternion.identity);
                tile.localScale = tile.localScale * (1 - outlinePercet);
                tile.SetParent(mazeGameObject.transform);

                Material tileMat;
                if (!calledFromEditor)
                {
                    tileMat = tile.GetComponent <Renderer>().material;
                    TilesMatrix[row, colum] = tileMat;
                    tileMat.color           = GetTileColor(maze.MazeMatrix[row, colum]);
                }
            }
        }
    }
Exemple #13
0
 public virtual bool IsFinishingPoint(Position position)
 {
     return(MazeMap.Exists(m => m.Item1.Equals(position) && m.Item2 == FinishSymbol));
 }
Exemple #14
0
 public virtual bool IsWall(Position position)
 {
     return(MazeMap.Exists(m => m.Item1.Equals(position) && m.Item2 == WallSymbol));
 }
Exemple #15
0
 public int TotalWalls()
 {
     return(MazeMap.FindAll(m => m.Item2 == WallSymbol).Count);
 }
Exemple #16
0
 private void Start()
 {
     maze = new MazeMap(mazeMatrix);
     mazeGenerator.GenerateMaze(maze);
     StartCoroutine(StartAlgorithm());
 }
Exemple #17
0
        public string[,] Generate2DMazeMatrixSymbols(MazeMap mazeMap, Player player)
        {
            var sizeOfARow = mazeMap.mazeSize * _sizeOfATile; // Each Room is 5 points in the maze map

            string[,] mazeMapSymbols = new string[sizeOfARow, sizeOfARow];

            // Generate symbols for each room
            foreach (var room in mazeMap.mazeRooms)
            {
                // Each room has it's own 2D axys
                for (var x = 0; x < _sizeOfATile; x++)
                {
                    for (var y = 0; y < _sizeOfATile; y++)
                    {
                        // If room was not visited we do not show any information about it
                        if (!room.IsVisited)
                        {
                            mazeMapSymbols[room.X * _sizeOfATile + x, room.Y *_sizeOfATile + y] =
                                Constants.RenderSymbolConstants.UnExploredSymbol;
                        }
                        else
                        {
                            // Mark floor of tiles
                            mazeMapSymbols[room.X * _sizeOfATile + x, room.Y *_sizeOfATile + y] =
                                Constants.RenderSymbolConstants.FloorTileSymbol;

                            // Mark walls
                            if (x == 0 || y == 0 || x == _sizeOfATile - 1 || y == _sizeOfATile - 1)
                            {
                                // If x or y are on the edges of tiles mark all of them as walls.
                                mazeMapSymbols[room.X * _sizeOfATile + x, room.Y *_sizeOfATile + y] =
                                    Constants.RenderSymbolConstants.WallSymbol;
                            }
                            // Mark gates
                            if ((x == 2 && (y == _sizeOfATile - 1 || y == 0)) || (y == 2 && (x == _sizeOfATile - 1 || x == 0)))
                            {
                                mazeMapSymbols[(room.X * _sizeOfATile) + x, (room.Y * _sizeOfATile) + y] = Constants.RenderSymbolConstants.GateSymbol;
                            }
                            // Mark entrance
                            if (room.IsEntrance)
                            {
                                // Mark the conrner of the tile as entrance
                                if (x == 0)
                                {
                                    mazeMapSymbols[room.X * _sizeOfATile + x, (room.Y * _sizeOfATile)] =
                                        Constants.RenderSymbolConstants.EntranceSymbol;
                                }
                            }
                            // Mark traps
                            if (room.HasTrap)
                            {
                                // Find tile position (room.X * _sizeOfATile). Find center of room  (_sizeOfATile / 2)
                                mazeMapSymbols[(room.X * _sizeOfATile) + (_sizeOfATile / 2), (room.Y * _sizeOfATile) + (_sizeOfATile / 2)] =
                                    Constants.RenderSymbolConstants.TrapSymbol;
                            }
                            // Mark treasure
                            if (room.HasTreasure)
                            {
                                // Find tile position (room.X * _sizeOfATile). Find center of room  (_sizeOfATile / 2)
                                mazeMapSymbols[(room.X * _sizeOfATile) + (_sizeOfATile / 2), (room.Y * _sizeOfATile) + (_sizeOfATile / 2)] =
                                    Constants.RenderSymbolConstants.TreasureSymbol;
                            }
                            //Draw player on the map
                            if (player.CurrentRoom.Id.Equals(room.Id))
                            {
                                // Find tile position (room.X * _sizeOfATile). Find center of room  (_sizeOfATile / 2)
                                mazeMapSymbols[(room.X * _sizeOfATile) + (_sizeOfATile / 2), (room.Y * _sizeOfATile) + (_sizeOfATile / 2)] =
                                    Constants.RenderSymbolConstants.PlayerSymbol;
                            }
                        }
                    }
                }
            }

            return(mazeMapSymbols);
        }
Exemple #18
0
 public int TotalSpaces()
 {
     return(MazeMap.FindAll(m => m.Item2 == SpaceSymbol).Count);
 }