private HexTile selectHexTile(int u, int v) { List <HexTile> neighbours = new List <HexTile>(); for (int q = -1; q <= 1; q++) { for (int p = -1; p <= 1; p++) { // Skip ourselves and the two outliers. if (p != q) { Coordinate neighbour = new Coordinate(u + p, v + q); if (hexTiles.ContainsKey(neighbour)) { neighbours.Add(hexTiles[neighbour]); } } } } if (neighbours.Count > 0) { // This is like performing a weighted selection (since it is uniformly distributed) but WAY easier. HexTile neighbour = ListRandom.select(neighbours); // Now the actual weighted selection (see how much easier it is now); return(neighbour.selectHexTile()); } else { float totalWeight = 0.0f; foreach (HexTileWeight hexTileWeight in hexTileWeights) { totalWeight += hexTileWeight.weight; } float value = Random.value * totalWeight; float cumulativeWeight = 0.0f; foreach (HexTileWeight hexTileWeight in hexTileWeights) { cumulativeWeight += hexTileWeight.weight; if (value < cumulativeWeight) { return(hexTileWeight.hexTile); } } // We canna get here cap'n! return(null); } }
public Vector3 getRandomPosition() { // Find a non-border hextile. HexTile hexTile; do { hexTile = ListRandom.select(new List <HexTile>(hexTiles.Values)); }while(hexTile.border); // Find the closest navigable point. Vector3?position = sampleSurface(hexTile.transform.position, radius * scale); if (position.HasValue) { return(position.Value); } else { Debug.Log("Could not sample NavMesh position."); return(hexTile.transform.position); } }