Consume stockpiled resources when purchasing constructions that require them

This commit is contained in:
yairm210
2024-08-29 10:22:21 +03:00
parent 2f36248484
commit 3c42d44ec4
2 changed files with 25 additions and 0 deletions

View File

@ -690,6 +690,17 @@ class CityConstructions : IsPartOfGameInfoSerialization {
) {
city.civ.civConstructions.boughtItemsWithIncreasingPrice.add(construction.name, 1)
}
// Consume stockpiled resources - usually consumed when construction starts, but not when bought
if (getWorkDone(construction.name) == 0){ // we didn't pay the resources when we started building
val costUniques = construction.getMatchingUniques(UniqueType.CostsResources, conditionalState)
for (unique in costUniques) {
val amount = unique.params[0].toInt()
val resourceName = unique.params[1]
city.civ.resourceStockpiles.add(resourceName, -amount)
}
}
}
if (queuePosition in 0 until constructionQueue.size)

View File

@ -304,4 +304,18 @@ class ResourceTests {
game.gameInfo.nextTurn()
assert(civInfo.getCivResourcesByName()[resource.name] == 1) // 1 was consumed because production started
}
@Test
fun stockpiledResourcesConsumedWhenConstructionPurchased() {
// given
val resource = game.createResource(UniqueType.Stockpiled.text)
val building = game.createBuilding("Instantly provides [2] [${resource.name}]")
city.cityConstructions.addBuilding(building)
assert(civInfo.getCivResourcesByName()[resource.name] == 2)
val consumingBuilding = game.createBuilding("Costs [1] [${resource.name}]")
assert(civInfo.getCivResourcesByName()[resource.name] == 2) // no change yet
city.cityConstructions.purchaseConstruction(consumingBuilding.name, -1, false)
assert(civInfo.getCivResourcesByName()[resource.name] == 1) // 1 was consumed because production started
}
}