float CalcCityValue(HexRef xref) { if (ownershipArray[xref.Index] != 0) { return(-1); // if the hex is claimed then we can't use it; } float value = CalcTileBaseValue(xref); int cityDistanceTheshold = 4; int distanceToNearestCity = cityDistanceTheshold; foreach (City city in cities) { int dist = Hex.Distance(xref.Hex, city.Hex); distanceToNearestCity = Mathf.Min(distanceToNearestCity, dist); } if (distanceToNearestCity <= 1) { return(-1); // city too close } else { value *= distanceToNearestCity / cityDistanceTheshold; } value *= xref.isCoastal? 1f : 0.5f; return(value); }
private bool TryGetNewCityLocation(Civilization civ, out HexRef hexRef) { //City sourceCity = civ.Cities.PickRandom(); List <Hex> possibleHexes = new List <Hex>(); foreach (City sourceCity in civ.Cities) { for (int radius = 4; radius <= 6; radius++) // for each hex circle around city form 4 to 6. { Hex[] hexCircle = Hex.HexCircle(sourceCity.Hex, radius); hexCircle = gameGrid.ValidHexes(hexCircle); possibleHexes.AddRange(hexCircle); } } if (possibleHexes.Count > 0) { Hex targetHex = possibleHexes.OrderByDescending((o => CalcCityValue(gameGrid.getHexRef(o), civ.capital))).First(); // get best hex int targetIndex = gameGrid.HexToIndex(targetHex); int targetID = ownershipArray [targetIndex]; hexRef = gameGrid.getHexRef(targetHex); if (targetID == 0 && hexRef != null && hexRef.Biome != Biomes.Oceanic) // if true cell is valid { return(true); // hexRef is already set to the correct value; } } hexRef = null; return(false); }
void SpawnCivs(int count, int maxIterations = 100) { int curCount = 0; for (int i = 0; i < maxIterations; i++) { if (curCount >= count) { break; } float tolerance = Mathf.Max(0.9f * (i / (float)maxIterations), 0.05f); HexRef xref = gameGrid.RandomLandHexRef(); float value = CalcCityValue(xref); if (value > tolerance && value > cityValueTreshold) { City newCity = spawnCity(xref); StartNewCiv(newCity); // Civilization newCiv = new Civilization(this, civIndex, newCity); // civilizaitons.Add(newCiv); curCount++; } else { tolerance -= 0.03f; //tolerance = Mathf.Max(tolerance,0); } } }
public City(HexRef xref, CityDisplay display) { name = NameGeneration.RandomCityName(); //color = Random.ColorHSV(0,1,1,1,0.99f,0.99f); this.hexRef = xref; this.cityDispaly = display; this.cityDispaly.Name = name; //this.display.SetColor(color); }
public HexRef getHexRef(Hex hex) { if (IsHexValid(hex)) { HexRef hexRef = new HexRef(HexToIndex(hex), this); return(hexRef); } else { return(null); } }
float CalcCityValue(HexRef xref, City Capital) { float value = CalcCityValue(xref); if (xref.Terrain == Terrain.Mountains) { value *= 0.6f; } int dist = Hex.Distance(xref.Hex, Capital.Hex); value *= 1f / dist; return(value); }
City spawnCity(HexRef xref) { Vector3 position = xref.WorldPosition; position.z = -0.3f; GameObject city = Instantiate(cityPrefab, position, cityPrefab.transform.localRotation, transform) as GameObject; cityObjs.Add(city); CityDisplay disp = city.GetComponent <CityDisplay>(); City newCity = new City(xref, disp); cities.Add(newCity); return(newCity); }
public HexRef RandomLandHexRef() { bool searching = true; while (searching) { HexRef h = RandomHexRef(); if (h.Biome != Biomes.Oceanic) { searching = false; return(h); } } return(null); }
private bool TryGetNewCityTileLocation(City city, out HexRef hexRef, int maxDistance = 3) { Hex[] hexArea = gameGrid.HexesInRange(city.Hex, maxDistance); //foreach(Hex targetHex in gameGrid.HexesInRange(city.Hex,3) ) List <HexRef> adjacentHexes = new List <HexRef>(); int rOffset = Random.Range(0, hexArea.Length - 1); for (int i = 0; i < hexArea.Length; i++) { int sindex = (i + rOffset) % hexArea.Length; Hex targetHex = hexArea[sindex]; int targetIndex = gameGrid.HexToIndex(targetHex); int targetID = ownershipArray [targetIndex]; if (targetID == 0) { HexRef xref = gameGrid.getHexRef(targetHex); if (xref != null && xref.Biome != Biomes.Oceanic) // if true cell is valid { Hex[] neighbors = gameGrid.Neighbors(targetHex); bool isAdjacent = false; foreach (Hex h in neighbors) { if (city.ownerID == ownershipArray[gameGrid.HexToIndex(h)]) { isAdjacent = true; break; } } if (isAdjacent) { adjacentHexes.Add(xref); //SetOwnership (targetIndex, civ.civID); //break; } } } // end if hex unclaimed } // end for each hex in cityArea // sort hexes by priority if (adjacentHexes.Count > 0) { hexRef = adjacentHexes.OrderByDescending(o => CalcCityTileValue(o, city)).First(); return(true); //SetOwnership(bestHexRef.Index, civ.civID); } hexRef = null; return(false); }
float CalcTileBaseValue(HexRef xref) { float value = 0; switch (xref.Biome) { case Biomes.Grassland: value = 1f; break; case Biomes.TropicalRainForest: value = 0.5f; break; case Biomes.Forrest: value = 0.8f; break; case Biomes.Taiga: value = 0.75f; break; case Biomes.Tundra: value = 0.5f; break; case Biomes.Desert: value = 0.5f; break; case Biomes.Snow: value = 0.3f; break; default: //Debug.LogError(string.Format("Illegal enum value {0}", biome)); value = 0; break; } // if (xref.Terrain == Terrain.Mountains) // value *= 0.8f; return(value); }
void SetupOverlay() { Overlay = new GameObject("Overlay"); Overlay.transform.SetParent(transform); Transform overlayXform = Overlay.transform; overlayCells = new List <HexCell>(); for (int x = 0; x < gameGrid.Columns; x++) { for (int y = 0; y < gameGrid.Rows; y++) { HexRef xref = gameGrid.getHexRef(gameGrid.OffsetToHex(x, y)); Vector3 pos = xref.WorldPosition; pos.z = -0.1f; GameObject cellObj = Instantiate(singleHex, pos, singleHex.transform.localRotation, overlayXform); HexCell cell = cellObj.GetComponent <HexCell>(); cell.SetColor(Color.clear); overlayCells.Add(cell); } } }
float CalcCityTileValue(HexRef xref, City city) { if (ownershipArray[xref.Index] != 0) { return(-1); // if the hex is claimed then we can't use it; } float value = CalcTileBaseValue(xref); if (xref.Terrain == Terrain.Mountains) { value *= 0.8f; } int dist = Hex.Distance(xref.Hex, city.Hex); value *= 1f / dist; //value *= xref.isCoastal? 1f : 0.3f; return(value); }
// private Hex SearchForOpenLand(Hex startPoint, int OwnerID) // { // // } private int GetValidExpansionTarget(List <int> civBorders, Hex capital, int maxDist) { int totalCount = civBorders.Count; int fisherCount = totalCount; //int targetIndex = -1; for (int j = 0; j < totalCount; j++) { // fisher yates shuffel int randomIndex = Random.Range(0, fisherCount); fisherCount--; int selectedIndex = civBorders[randomIndex]; civBorders [randomIndex] = civBorders [fisherCount]; // set selectionIndex to last unused value civBorders [fisherCount] = selectedIndex; // set last index to selected value ( may not be needed). Hex selectedHex = gameGrid.IndexToHex(selectedIndex); int distance = Hex.Distance(selectedHex, capital); if (distance > maxDist) { continue; // hex is to far away } Hex dir = Hex.directions.PickRandom(); Hex targetHex = selectedHex + dir; if (!gameGrid.IsHexValid(targetHex)) { continue; } // hex is not valid int targetIndex = gameGrid.HexToIndex(targetHex); int targetID = ownershipArray [targetIndex]; if (targetID == 0) { HexRef xref = gameGrid.getHexRef(targetHex); if (xref != null && xref.Biome != Biomes.Oceanic) // if true cell is valid { return(targetIndex); } } } return(-1); }