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:
Oskar Niesen 2024-01-15 10:55:45 -06:00 committed by GitHub
parent c829caf51a
commit c3a4b5f7af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 7 deletions

View File

@ -647,13 +647,32 @@ class WorkerAutomation(
private fun getImprovementRanking(tile: Tile, unit: MapUnit, improvementName: String, localUniqueCache: LocalUniqueCache): Float {
val improvement = ruleSet.tileImprovements[improvementName]!!
var stats = Stats()
if (tile.getOwner() == unit.civ)
stats = tile.stats.getStatDiffForImprovement(improvement, civInfo, tile.getCity(), localUniqueCache)
// We could expand to this tile in the future
else if (ruleSet.tileImprovements[improvementName]!!.hasUnique(UniqueType.CanBuildOutsideBorders) &&
tile.neighbors.any {it.getOwner() == unit.civ && it.owningCity != null && tile.aerialDistanceTo(it.owningCity!!.getCenterTile()) <= 3})
stats = tile.stats.getStatDiffForImprovement(improvement, civInfo, tile.getCity(), localUniqueCache).div(3f)
// If this tile is not in our territory or neighboring it, it has no value
if (tile.getOwner() != unit.civ
// Check if it is not an unowned neighboring tile that can be in city range
&& !(ruleSet.tileImprovements[improvementName]!!.hasUnique(UniqueType.CanBuildOutsideBorders)
&& tile.neighbors.any { it.getOwner() == unit.civ && it.owningCity != null
&& 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)
// Calculate the bonus from gaining the resources, this isn't included in the stats above

View File

@ -54,6 +54,30 @@ internal class WorkerAutomationTest {
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
fun `should build improvement on unseen resource`() {
// Add the needed tech to construct the improvements below