mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-12 08:49:22 +07:00
Worker remove feature AI fix (#10917)
* Workers now value the improvement that they want to build after removing a terrain feature * Restructured getImprovementRanking
This commit is contained in:
@ -647,13 +647,32 @@ class WorkerAutomation(
|
|||||||
|
|
||||||
private fun getImprovementRanking(tile: Tile, unit: MapUnit, improvementName: String, localUniqueCache: LocalUniqueCache): Float {
|
private fun getImprovementRanking(tile: Tile, unit: MapUnit, improvementName: String, localUniqueCache: LocalUniqueCache): Float {
|
||||||
val improvement = ruleSet.tileImprovements[improvementName]!!
|
val improvement = ruleSet.tileImprovements[improvementName]!!
|
||||||
var stats = Stats()
|
|
||||||
if (tile.getOwner() == unit.civ)
|
// If this tile is not in our territory or neighboring it, it has no value
|
||||||
stats = tile.stats.getStatDiffForImprovement(improvement, civInfo, tile.getCity(), localUniqueCache)
|
if (tile.getOwner() != unit.civ
|
||||||
// We could expand to this tile in the future
|
// Check if it is not an unowned neighboring tile that can be in city range
|
||||||
else if (ruleSet.tileImprovements[improvementName]!!.hasUnique(UniqueType.CanBuildOutsideBorders) &&
|
&& !(ruleSet.tileImprovements[improvementName]!!.hasUnique(UniqueType.CanBuildOutsideBorders)
|
||||||
tile.neighbors.any {it.getOwner() == unit.civ && it.owningCity != null && tile.aerialDistanceTo(it.owningCity!!.getCenterTile()) <= 3})
|
&& tile.neighbors.any { it.getOwner() == unit.civ && it.owningCity != null
|
||||||
stats = tile.stats.getStatDiffForImprovement(improvement, civInfo, tile.getCity(), localUniqueCache).div(3f)
|
&& tile.aerialDistanceTo(it.owningCity!!.getCenterTile()) <= 3 } ))
|
||||||
|
return 0f
|
||||||
|
|
||||||
|
val stats = tile.stats.getStatDiffForImprovement(improvement, civInfo, tile.getCity(), localUniqueCache)
|
||||||
|
|
||||||
|
if (improvementName.startsWith("Remove ")) {
|
||||||
|
// We need to look beyond what we are doing right now and at the final improvement that will be on this tile
|
||||||
|
val terrainName = improvementName.replace("Remove ", "")
|
||||||
|
if (ruleSet.terrains.containsKey(terrainName)) { // Otherwise we get an infinite loop with remove roads
|
||||||
|
tile.removeTerrainFeature(terrainName)
|
||||||
|
val wantedFinalImprovement = chooseImprovement(unit, tile)
|
||||||
|
if (wantedFinalImprovement != null)
|
||||||
|
stats.add(tile.stats.getStatDiffForImprovement(wantedFinalImprovement, civInfo, tile.getCity(), localUniqueCache))
|
||||||
|
tile.addTerrainFeature(terrainName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the tile is a neighboring tile it has a lower value
|
||||||
|
if (tile.getOwner() != unit.civ)
|
||||||
|
stats.div(3f)
|
||||||
|
|
||||||
var value = Automation.rankStatsValue(stats, unit.civ)
|
var value = Automation.rankStatsValue(stats, unit.civ)
|
||||||
// Calculate the bonus from gaining the resources, this isn't included in the stats above
|
// Calculate the bonus from gaining the resources, this isn't included in the stats above
|
||||||
|
@ -54,6 +54,30 @@ internal class WorkerAutomationTest {
|
|||||||
assertTrue(currentTile.turnsToImprovement > 0)
|
assertTrue(currentTile.turnsToImprovement > 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `should remove terrain feature enable resource`() {
|
||||||
|
// Add the needed tech to construct the improvements below
|
||||||
|
for (improvement in listOf("Remove Forest", "Plantation")) {
|
||||||
|
civInfo.tech.techsResearched.add(testGame.ruleset.tileImprovements[improvement]!!.techRequired!!)
|
||||||
|
}
|
||||||
|
|
||||||
|
testGame.addCity(civInfo, testGame.tileMap[0,0])
|
||||||
|
|
||||||
|
val currentTile = testGame.tileMap[1,1]
|
||||||
|
currentTile.addTerrainFeature("Hill")
|
||||||
|
currentTile.addTerrainFeature("Forest")
|
||||||
|
currentTile.resource = "Citrus"
|
||||||
|
currentTile.resourceAmount = 1
|
||||||
|
|
||||||
|
val mapUnit = testGame.addUnit("Worker", civInfo, currentTile)
|
||||||
|
|
||||||
|
workerAutomation.automateWorkerAction(mapUnit, hashSetOf())
|
||||||
|
|
||||||
|
assertEquals("Worker should begun removing the forest to clear a luxury resource but didn't",
|
||||||
|
"Remove Forest", currentTile.improvementInProgress)
|
||||||
|
assertTrue(currentTile.turnsToImprovement > 0)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should build improvement on unseen resource`() {
|
fun `should build improvement on unseen resource`() {
|
||||||
// Add the needed tech to construct the improvements below
|
// Add the needed tech to construct the improvements below
|
||||||
|
Reference in New Issue
Block a user