public Map(Vector2 position) { // layout Position = position; WidthInPixels = Statics.LeftColumnWidth - (int)Position.X * 2; HeightInPixels = Statics.CanvasHeight - Statics.LeftColumnPadding * 2 - (int)Position.Y * 2; UpdateDelta = TimeSpan.Zero; State = MAPSTATE.READY_FOR_BATTLE; // DEBUG MergeCount = 0; // END DEBUG // MapTiles holds only whether a map tile coordinate is still available MasterTileList = new Tile[WidthInTiles, HeightInTiles]; for (int x = 0; x < WidthInTiles; x++) { for (int y = 0; y < HeightInTiles; y++) { MasterTileList[x, y] = new Tile(new Point(x, y)); } } // now we have a grid of tiles, each of which states its availability // loop, creating regions, each of which will switch off a series of tiles int AvailableTileCount = WidthInTiles * HeightInTiles; int nCurrentRegionId = 0; while (AvailableTileCount > 0) { Region region = new Region(nCurrentRegionId++, MasterTileList); Regions.Add(region); AvailableTileCount -= region.Tiles.Count; } MergeRegions(MasterTileList); ReindexRegions(); }
private void MergeRegions(Tile[,] MasterTileList) { bool bRestart = true; while (bRestart) { bRestart = false; foreach(Region region in Regions) { if(region.Tiles.Count <= Statics.MergeThreshold) { Region neighbor = region.RandomNeighbor(); MergeCount++; MergeRegions(region, neighbor); bRestart = true; } } } //for (int i = Regions.Count - 1; i >= 0; i--) //{ // if (Regions[i].Tiles.Count <= Statics.MergeThreshold) // { // // determine merge direction // // pick random tile and check neighbors for a new region // int nMergeRegion = RandomNeighbor(i).ID; // MergeCount++; // MergeRegions(nMergeRegion, i); // } //} }
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++; } } }
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; }
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); } } } }