public static Terrain NewTerrain(int sizeX, int sizeY)
        {
            Terrain result = new Terrain(sizeX, sizeY);
            ClearTerrain(result, Tile.TileType.TilePit);

            TerrainNumber++;

            _roachQueenCurrentCount = 0;
            _brainYellowCount = 0;

            CalculatePercents ();
            CalculateSpecialRoom ();
            GenerateArtificialTerrain(result);

            return result;
        }
Example #2
0
        public Room(int mStartX, int mStartY, int mEndX, int mEndY, Terrain mTerrain)
        {
            _startX = mStartX;
            _startY = mStartY;
            _endX = mEndX;
            _endY = mEndY;
            _terrain = mTerrain;
            SizeX = _endX - _startX;
            SizeY = _endY - _startY;
            _tiles = new List<Tile> ();

            for (int iY = _startY; iY < _endY; iY++)
            {
                for (int iX = _startX; iX < _endX; iX++)
                {
                    _tiles.Add(_terrain.Tiles[iX, iY]);
                }
            }
        }
        private static void Populate(Terrain terrain)
        {
            int dX = terrain.Tiles.GetLength(0) - 1;
            int dY = terrain.Tiles.GetLength(1) - 1;

            for (int iY = 0; iY < dY; iY++)
            {
                for (int iX = 0; iX < dX; iX++)
                {
                    Tile tempTile = terrain.GetRandomTile ();

                    if (tempTile.X <= 1 || tempTile.X >= terrain.SizeX - 1 || tempTile.Y <= 1 ||
                        tempTile.Y >= terrain.SizeY - 1) continue;
                    #region "Doors"
                    if (tempTile.Type == Tile.TileType.TileFloor)
                    {
                        if (tempTile.GetCountAdjacent(Tile.TileType.TileWall) == 6)
                        {
                            if (CalculatePercent(_doorPercent))
                            {
                                terrain.Tiles[tempTile.X - 1, tempTile.Y + 1].Type = Tile.TileType.TileFloor;
                                terrain.Tiles[tempTile.X + 1, tempTile.Y - 1].Type = Tile.TileType.TileFloor;

                                int tempX = 0;
                                int tempY = 0;
                                int rnd = 0;

                                tempX = 1;
                                tempY = 1;

                                rnd = Helper.Random.Next(0, 2);

                                if (rnd == 0)
                                {
                                    tempX = -tempX;
                                }

                                Tile orbTile1 = terrain.Tiles[tempTile.X - tempX, tempTile.Y + tempY];
                                orbTile1.Type = Tile.TileType.TileOrb;
                                orbTile1.OrbFlagX = tempTile.X;
                                orbTile1.OrbFlagY = tempTile.Y;
                                Tile orbTile2 = terrain.Tiles[tempTile.X + tempX, tempTile.Y - tempY];
                                orbTile2.Type = Tile.TileType.TileOrb;
                                orbTile2.OrbFlagX = tempTile.X;
                                orbTile2.OrbFlagY = tempTile.Y;

                                tempTile.Type = Tile.TileType.TileClosedDoor;
                            }
                        }
                    }
                    #endregion
                    #region "Pits"
                    switch (tempTile.Type)
                    {
                        case Tile.TileType.TileWall:
                            if (tempTile.GetCountAdjacent(Tile.TileType.TilePit) > 2)
                            {
                                if (CalculatePercent(_pitPercent))
                                {
                                    tempTile.Type = Tile.TileType.TilePit;
                                }
                            }
                            break;
                        case Tile.TileType.TileFloor:
                            if (tempTile.GetCountAdjacent(Tile.TileType.TileFloor) > 6)
                            {
                                if (CalculatePercent(_pitPercent))
                                {
                                    tempTile.Type = Tile.TileType.TilePit;
                                }
                            }
                            break;
                    }
                    #endregion
                    #region "Normal Room"
                    if (_currentSpecial == SpecialRoom.SrNormal ||
                        _currentSpecial == SpecialRoom.SrOrthosquare ||
                        _currentSpecial == SpecialRoom.SrBrokenwall)
                    {
                        if (tempTile.Type == Tile.TileType.TileFloor)
                        {
                            #region "Brain Yellow"
                            if (tempTile.Type == Tile.TileType.TileFloor)
                            {
                                if (TerrainNumber >= BrainYellowStart)
                                {
                                    if (_brainYellowCount < _brainYellowNumber)
                                    {
                                        tempTile.Type = Tile.TileType.TileBrainYellow;
                                        _brainYellowCount++;
                                    }
                                }
                            }
                            #endregion
                            #region "Evil Eye"
                            if (tempTile.Type == Tile.TileType.TileFloor)
                            {
                                if (TerrainNumber >= EvilEyeStart)
                                {
                                    if (_evilEyeNumber > 0)
                                    {
                                        tempTile.Type = Tile.TileType.TileEvileye;
                                        _evilEyeNumber--;

                                        if (TerrainNumber >= EvilEyeSentryStart &&
                                            _evilEyeSentryNumber > 0)
                                        {
                                            tempTile.Type = Tile.TileType.TileEvileyesentry;
                                            _evilEyeSentryNumber--;
                                        }
                                    }
                                }
                            }
                            #endregion
                            #region "Roach / Roach Queen / Wraithwings / Gel Baby / Zombie"
                            if (tempTile.Type == Tile.TileType.TileFloor)
                            {
                                if (TerrainNumber >= RoachStart)
                                {
                                    if (_roachNumber > 0)
                                    {
                                        tempTile.Type = Tile.TileType.TileRoach;
                                        _roachNumber--;

                                        if (TerrainNumber >= RoachQueenStart)
                                        {
                                            if (_zombieNumber > 0 &&
                                                TerrainNumber >= ZombieStart)
                                            {
                                                tempTile.Type = Tile.TileType.TileZombie;
                                                _zombieNumber--;
                                            }
                                            else if (_roachQueenNumber > 0 &&
                                                     _roachQueenCurrentCount < RoachQueenLimit)
                                            {
                                                tempTile.Type = Tile.TileType.TileRoachqueen;
                                                _roachQueenCurrentCount++;
                                                _roachQueenNumber--;
                                            }
                                            else
                                            {
                                                if (TerrainNumber >= WraithwingStart &&
                                                    _wraithwingNumber > 0)
                                                {
                                                    tempTile.Type = Tile.TileType.TileWraithwing;
                                                    _wraithwingNumber--;
                                                }
                                                else
                                                {
                                                    if (TerrainNumber >= GelBabyStart)
                                                    {
                                                        if (_gelBabyNumber > 0)
                                                        {
                                                            tempTile.Type = Tile.TileType.TileGelbaby;
                                                            _gelBabyNumber--;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                    #endregion
                    #region "Broken Walls"
                    if (tempTile.Type == Tile.TileType.TileWall)
                    {
                        if (CalculatePercent(_brokenWallPercent))
                        {
                            tempTile.Type = Tile.TileType.TileBrokenwall;
                        }
                    }
                    #endregion
                    #region "Trapdoors"
                    if (tempTile.Type == Tile.TileType.TileFloor &&
                        tempTile.GetCountAdjacent(Tile.TileType.TileWall) < 3)
                    {
                        if (TerrainNumber >= TrapDoorStart)
                        {
                            if (CalculatePercent(_trapDoorPercent))
                            {
                                tempTile.Type = Tile.TileType.TileTrapdoor;
                            }
                        }
                    }
                    #endregion
                    #region "Special Zombie"
                    if (_currentSpecial == SpecialRoom.SrZombie)
                    {
                        if (tempTile.Type == Tile.TileType.TileFloor)
                        {
                            if (_zombieNumber > 0)
                            {
                                tempTile.Type = Tile.TileType.TileZombie;
                                _zombieNumber--;
                            }
                        }
                    }

                    #endregion
                    #region "Special Evil Eye"
                    if (_currentSpecial == SpecialRoom.SrEvileye)
                    {
                        if (tempTile.Type == Tile.TileType.TileFloor)
                        {
                            if (_evilEyeNumber > 0)
                            {
                                tempTile.Type = Tile.TileType.TileEvileye;
                                _evilEyeNumber--;

                                if (TerrainNumber >= EvilEyeSentryStart && _evilEyeSentryNumber > 0)
                                {
                                    tempTile.Type = Tile.TileType.TileEvileyesentry;
                                    _evilEyeSentryNumber--;
                                }
                            }
                            else if (tempTile.GetCountAdjacent(Tile.TileType.TileFloor) > 7)
                            {
                                tempTile.Type = Tile.TileType.TileWall;
                            }
                        }
                    }
                    #endregion
                    #region "Special Roach Queen Queen"
                    if (_currentSpecial == SpecialRoom.SrRoachqueen)
                    {
                        if (tempTile.Type == Tile.TileType.TileFloor)
                        {
                            if (_roachQueenNumber > 0)
                            {
                                tempTile.Type = Tile.TileType.TileRoachqueen;
                                tempTile.Flags += "spawns roach queens";
                                _roachQueenNumber--;
                            }
                        }
                    }
                    #endregion
                    #region "Special Gel Baby"
                    if (_currentSpecial == SpecialRoom.SrGelbaby)
                    {
                        if (tempTile.Type == Tile.TileType.TileFloor)
                        {
                            if (_gelBabyNumber > 0)
                            {
                                tempTile.Type = Tile.TileType.TileGelbaby;
                                _gelBabyNumber--;
                            }
                        }
                    }
                    #endregion
                    #region "Special Trapdoor"
                    if (_currentSpecial == SpecialRoom.SrTrapdoor)
                    {
                        if (tempTile.Type != Tile.TileType.TileFloor) continue;
                        tempTile.Type = Tile.TileType.TileTrapdoor;

                        if (_wraithwingNumber > 0)
                        {
                            tempTile.Type = Tile.TileType.TileWraithwing;
                            _wraithwingNumber--;
                        }
                        #endregion
                    }
                }
            }

            #region "Orthogonal Squares"
            foreach (Tile tempTile in from tempRoom in terrain.Rooms
                where TerrainNumber >= OrthoSquareStart
                where CalculatePercent(_orthoSquarePercent)
                from tempTile in tempRoom.Tiles
                select tempTile)
            {
                if(tempTile.Type != Tile.TileType.TileOrb)
                tempTile.Flags += "orthosquare";
            }
            #endregion

            for (int iY = 0; iY < dY; iY++)
            {
                for (int iX = 0; iX < dX; iX++)
                {
                    Tile tempTile = terrain.Tiles[iX, iY];

                    if (tempTile.X <= 1 || tempTile.X >= terrain.SizeX - 1 || tempTile.Y <= 1 ||
                        tempTile.Y >= terrain.SizeY - 1) continue;
                    #region "Special Orthosquares"
                    if (_currentSpecial == SpecialRoom.SrOrthosquare)
                    {
                        if (tempTile.Type == Tile.TileType.TileFloor && tempTile.Type != Tile.TileType.TileOrb)
                        {
                            tempTile.Flags += "orthosquare";
                        }
                    }
                    #endregion
                    #region "Special Broken Walls"
                    if (_currentSpecial != SpecialRoom.SrBrokenwall) continue;
                    if (tempTile.Type != Tile.TileType.TileFloor || tempTile.Type == Tile.TileType.TileOrb) continue;
                    if (CalculatePercent(66))
                    {
                        tempTile.Type = Tile.TileType.TileBrokenwall;
                    }
                    #endregion
                }
            }

            terrain.GetRandomTile(Tile.TileType.TileWall).Type = Tile.TileType.TileFloor;
            terrain.GetRandomTile(Tile.TileType.TileWall).Type = Tile.TileType.TileFloor;
            terrain.GetRandomTile(Tile.TileType.TileWall).Type = Tile.TileType.TileFloor;

            if (_shop)
            {
                terrain.GetRandomTile(Tile.TileType.TileFloor).Type = Tile.TileType.TileShop;
            }
        }
        private static void GenerateArtificialTerrain(Terrain terrain)
        {
            var startPoints = new List<Point> ();
            const int offset = 7;

            int roomsToCreate = _roomNumber - 3 > 0 ? Helper.Random.Next(_roomNumber - 3, _roomNumber) : _roomNumber;

            for (int i = 0; i < roomsToCreate; i++)
            {
                startPoints.Add(new Point(Helper.Random.Next(offset, terrain.SizeX - offset),
                    Helper.Random.Next(offset, terrain.SizeY - offset)));
            }

            foreach (Point tempPoint in startPoints)
            {
                int roomWidth = Helper.Random.Next(2, 6);
                int roomHeight = Helper.Random.Next(2, 6);

                var tempRoom = new Room(tempPoint.X - roomWidth, tempPoint.Y - roomHeight, tempPoint.X + roomWidth,
                    tempPoint.Y + roomHeight, terrain);
                terrain.Rooms.Add(tempRoom);

                for (int iY = -roomHeight; iY < roomHeight; iY++)
                {
                    for (int iX = -roomWidth; iX < roomWidth; iX++)
                    {
                        terrain.Tiles[tempPoint.X + iX, tempPoint.Y + iY].Type = Tile.TileType.TileFloor;
                    }
                }
            }

            for (int i = 0; i < startPoints.Count - 1; i++)
            {
                ConnectPoints(terrain, startPoints[i], startPoints[i + 1]);
            }

            CoverPits(terrain);
            Populate(terrain);
            FinalizeTerrain ();
        }
 private static void CoverPits(Terrain terrain)
 {
     foreach (Tile tempTile in terrain.Tiles)
     {
         if (terrain.Tiles[tempTile.X, tempTile.Y].Type != Tile.TileType.TileFloor) continue;
         for (int iY = -1; iY < 2; iY++)
         {
             for (int iX = -1; iX < 2; iX++)
             {
                 if ((iX == 0 || iY == 0) || (tempTile.X + iX - 1 <= -1) || (tempTile.Y + iY - 1 <= -1) ||
                     (tempTile.X + iX + 1 >= terrain.SizeX) || (tempTile.Y + iY + 1 >= terrain.SizeY)) continue;
                 if (terrain.Tiles[tempTile.X + iX, tempTile.Y + iY].Type == Tile.TileType.TilePit)
                 {
                     terrain.Tiles[tempTile.X + iX, tempTile.Y + iY].Type = Tile.TileType.TileWall;
                 }
             }
         }
     }
 }
        private static void ConnectPoints(Terrain terrain, Point a, Point b)
        {
            int tempAX = a.X;
            int tempAY = a.Y;
            int tempBX = b.X;
            int tempBY = b.Y;

            while (tempAX != tempBX)
            {
                if (tempAX > tempBX)
                {
                    tempAX--;
                }
                else if (tempAX < tempBX)
                {
                    tempAX++;
                }

                terrain.Tiles[tempAX, tempAY].Type = Tile.TileType.TileFloor;
            }

            while (tempAY != tempBY)
            {
                if (tempAY > tempBY)
                {
                    tempAY--;
                }
                else if (tempAY < tempBY)
                {
                    tempAY++;
                }

                terrain.Tiles[tempAX, tempAY].Type = Tile.TileType.TileFloor;
            }
        }
 private static void ClearTerrain(Terrain terrain, Tile.TileType tileType)
 {
     for (int iY = 0; iY < terrain.SizeY; iY++)
     {
         for (int iX = 0; iX < terrain.SizeX; iX++)
         {
             terrain.Tiles[iX, iY] = new Tile(iX, iY, terrain, tileType);
         }
     }
 }