Example #1
0
        public Region(int id, Tile[,] MasterTileList)
        {
            Name = Statics.RandomRegionType();

            int TileCountX = MasterTileList.GetLength(0);
            int TileCountY = MasterTileList.GetLength(1);

            FailedExpansionCount = 0;
            ID = id;
            Color = Statics.RandomColor();

            // grab random point as starting tile
            int x = Statics.Random.Next(TileCountX);
            int y = Statics.Random.Next(TileCountY);
            while (!MasterTileList[x, y].Available)
            {
                x = Statics.Random.Next(TileCountX);
                y = Statics.Random.Next(TileCountY);
            }

            // create this tile
            Tile startingTile = MasterTileList[x, y];
            Tiles.Add(startingTile);
            startingTile.Available = false;
            startingTile.Region = ID;

            // if able, grow until minimum size reached, then possibly grow more
            while (((Tiles.Count < Statics.MinimumRegionSize) || (Statics.Random.Next(100) < Statics.ProbabilityOfExpansion)) && HasAvailableNeighboringTile(MasterTileList))
            {
                // pick a random tile from the region set and attempt to grow
                Tile currentTile = Tiles.RandomItem();
                x = (int)currentTile.Coordinates.X;
                y = (int)currentTile.Coordinates.Y;

                int nExpansionDirection = Statics.Random.Next(4);
                int nExpansionAttempts = 0;

                Tile expansionTile = null;
                while (expansionTile == null && nExpansionAttempts < 4)
                {
                    nExpansionAttempts++;
                    nExpansionDirection = (nExpansionDirection + 1) % 4;
                    switch (nExpansionDirection)
                    {
                        case 0:
                            // try to grow left
                            if (x > 0 && MasterTileList[x - 1, y].Available)
                            {
                                expansionTile = MasterTileList[x - 1, y];
                            }
                            break;
                        case 1:
                            // try to grow right
                            if (x < TileCountX - 1 && MasterTileList[x + 1, y].Available)
                            {
                                expansionTile = MasterTileList[x + 1, y];
                            }
                            break;
                        case 2:
                            // try to grow up
                            if (y > 0 && MasterTileList[x, y - 1].Available)
                            {
                                expansionTile = MasterTileList[x, y - 1];
                            }
                            break;
                        case 3:
                            // try to grow down
                            if (y < TileCountY - 1 && MasterTileList[x, y + 1].Available)
                            {
                                expansionTile = MasterTileList[x, y + 1];
                            }
                            break;
                    }
                }

                if (expansionTile != null)
                {
                    Tiles.Add(expansionTile);
                    expansionTile.Available = false;
                    expansionTile.Region = ID;
                }
                else
                {
                    FailedExpansionCount++;
                }
            }
        }
Example #2
0
        private bool HasAvailableNeighboringTile(Tile[,] MasterTileList)
        {
            int TileCountX = MasterTileList.GetLength(0);
            int TileCountY = MasterTileList.GetLength(1);

            foreach (Tile tile in Tiles)
            {
                // check left
                if (tile.Coordinates.X > 0 && MasterTileList[(int)tile.Coordinates.X - 1, (int)tile.Coordinates.Y].Available) { return true; }

                // check right
                if (tile.Coordinates.X < TileCountX - 1 && MasterTileList[(int)tile.Coordinates.X + 1, (int)tile.Coordinates.Y].Available) { return true; }

                // check up
                if (tile.Coordinates.Y > 0 && MasterTileList[(int)tile.Coordinates.X, (int)tile.Coordinates.Y - 1].Available) { return true; }

                // check down
                if (tile.Coordinates.Y < TileCountY - 1 && MasterTileList[(int)tile.Coordinates.X, (int)tile.Coordinates.Y + 1].Available) { return true; }
            }

            return false;
        }
Example #3
0
        public void FindNeighbors(Tile[,] MasterTileList)
        {
            int TileCountX = MasterTileList.GetLength(0);
            int TileCountY = MasterTileList.GetLength(1);

            foreach (Tile tile in Tiles)
            {
                int x = (int)tile.Coordinates.X;
                int y = (int)tile.Coordinates.Y;

                if (x > 0)
                {
                    // analyze left neighbor
                    Tile neighbor = MasterTileList[x - 1, y];
                    if (neighbor.Region != null && neighbor.Region.ID != ID)
                    {
                        NeighboringRegions.Add(neighbor.Region);
                    }
                }

                if (x < TileCountX - 1)
                {
                    // analyze right neighbor
                    Tile neighbor = MasterTileList[x + 1, y];
                    if (neighbor.Region != null && neighbor.Region.ID != ID)
                    {
                        NeighboringRegions.Add(neighbor.Region);
                    }
                }

                if (y > 0)
                {
                    // analyze up neighbor
                    Tile neighbor = MasterTileList[x, y - 1];
                    if (neighbor.Region != null && neighbor.Region.ID != ID)
                    {
                        NeighboringRegions.Add(neighbor.Region);
                    }
                }

                if (y < TileCountY - 1)
                {
                    // analyze down neighbor
                    Tile neighbor = MasterTileList[x, y + 1];
                    if (neighbor.Region != null && neighbor.Region.ID != ID)
                    {
                        NeighboringRegions.Add(neighbor.Region);
                    }
                }
            }
        }