Allow for city level stockpiles to actually function (#12710)

* Allow for city level stockpiles to actually function

* Disallow civs to gain citywide stockpiles from gainStockpiledResource

* Give the correct resource amount from city.getAvailableResourceAmount

* Make getReserve for cities explicit for tileResources

* Remove city function for adding stockpiles via a string

* Remove civ function for adding stockpiles via a string
This commit is contained in:
SeventhM 2025-01-04 10:02:28 -08:00 committed by GitHub
parent 919c4e39d9
commit 2e32eccbce
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 36 additions and 23 deletions

View File

@ -16,6 +16,7 @@ import com.unciv.logic.map.TileMap
import com.unciv.logic.map.mapunit.MapUnit
import com.unciv.logic.map.tile.RoadStatus
import com.unciv.logic.map.tile.Tile
import com.unciv.models.Counter
import com.unciv.models.ruleset.Building
import com.unciv.models.ruleset.tile.TileResource
import com.unciv.models.ruleset.unique.StateForConditionals
@ -73,6 +74,8 @@ class City : IsPartOfGameInfoSerialization, INamed {
@Transient // CityStats has no serializable fields
var cityStats = CityStats(this)
var resourceStockpiles = Counter<String>()
/** All tiles that this city controls */
var tiles = HashSet<Vector2>()
@ -136,6 +139,7 @@ class City : IsPartOfGameInfoSerialization, INamed {
toReturn.tiles = tiles
toReturn.workedTiles = workedTiles
toReturn.lockedTiles = lockedTiles
toReturn.resourceStockpiles = resourceStockpiles.clone()
toReturn.isBeingRazed = isBeingRazed
toReturn.attackedThisTurn = attackedThisTurn
toReturn.foundingCiv = foundingCiv
@ -207,6 +211,11 @@ class City : IsPartOfGameInfoSerialization, INamed {
fun getGreatPersonPercentageBonus() = GreatPersonPointsBreakdown.getGreatPersonPercentageBonus(this)
fun getGreatPersonPoints() = GreatPersonPointsBreakdown(this).sum()
fun gainStockpiledResource(resource: TileResource, amount: Int) {
if (resource.isCityWide) resourceStockpiles.add(resource.name, amount)
else civ.resourceStockpiles.add(resource.name, amount)
}
fun addStat(stat: Stat, amount: Int) {
when (stat) {
Stat.Production -> cityConstructions.addProductionPoints(amount)
@ -218,8 +227,7 @@ class City : IsPartOfGameInfoSerialization, INamed {
fun addGameResource(stat: GameResource, amount: Int) {
if (stat is TileResource) {
if (!stat.isStockpiled) return
if (!stat.isCityWide) civ.gainStockpiledResource(stat.name, amount)
else { /*TODO*/ }
gainStockpiledResource(stat, amount)
return
}
when (stat) {
@ -238,11 +246,10 @@ class City : IsPartOfGameInfoSerialization, INamed {
}
fun getReserve(stat: GameResource): Int {
if (stat is TileResource && stat.isCityWide) {
return if (stat.isStockpiled) {
// TODO
0
} else 0
if (stat is TileResource) {
if (!stat.isStockpiled) return 0
if (stat.isCityWide) return resourceStockpiles[stat.name]
return civ.resourceStockpiles[stat.name]
}
return when (stat) {
Stat.Production -> cityConstructions.getWorkDone(cityConstructions.getCurrentConstruction().name)

View File

@ -435,7 +435,8 @@ class CityConstructions : IsPartOfGameInfoSerialization {
for (unique in costUniques) {
val amount = unique.params[0].toInt()
val resourceName = unique.params[1]
city.civ.gainStockpiledResource(resourceName, -amount)
val resource = city.civ.gameInfo.ruleset.tileResources[resourceName] ?: continue
city.gainStockpiledResource(resource, -amount)
}
if (construction !is Building) return
@ -705,7 +706,8 @@ class CityConstructions : IsPartOfGameInfoSerialization {
for (unique in costUniques) {
val amount = unique.params[0].toInt()
val resourceName = unique.params[1]
city.civ.gainStockpiledResource(resourceName, -amount)
val resource = city.civ.gameInfo.ruleset.tileResources[resourceName] ?: continue
city.gainStockpiledResource(resource, -amount)
}
}
}

View File

@ -69,9 +69,9 @@ object CityResources {
fun getAvailableResourceAmount(city: City, resourceName: String): Int {
val resource = city.getRuleset().tileResources[resourceName] ?: return 0
if (resource.isCityWide)
return getCityResourcesAvailableToCity(city).asSequence().filter { it.resource == resource }.sumOf { it.amount }
return city.civ.getResourceAmount(resourceName)
if (!resource.isCityWide) return city.civ.getResourceAmount(resourceName)
if (resource.isStockpiled) return city.resourceStockpiles[resourceName]
return getCityResourcesAvailableToCity(city).asSequence().filter { it.resource == resource }.sumOf { it.amount }
}
private fun addResourcesFromTiles(city: City, resourceModifer: HashMap<String, Float>, cityResources: ResourceSupplyList) {

View File

@ -866,7 +866,7 @@ class Civilization : IsPartOfGameInfoSerialization {
}
fun addGameResource(stat: GameResource, amount: Int) {
if (stat is TileResource && !stat.isCityWide && stat.isStockpiled) gainStockpiledResource(stat.name, amount)
if (stat is TileResource && stat.isStockpiled) gainStockpiledResource(stat, amount)
when (stat) {
Stat.Culture -> { policies.addCulture(amount)
if (amount > 0) totalCultureForContests += amount }
@ -880,9 +880,10 @@ class Civilization : IsPartOfGameInfoSerialization {
// Happiness cannot be added as it is recalculated again, use a unique instead
}
}
fun gainStockpiledResource(resourceName: String, amount: Int) {
resourceStockpiles.add(resourceName, amount)
fun gainStockpiledResource(resource: TileResource, amount: Int) {
if (resource.isCityWide) return
resourceStockpiles.add(resource.name, amount)
}
fun getStatReserve(stat: Stat): Int {

View File

@ -41,7 +41,7 @@ class TurnManager(val civInfo: Civilization) {
civInfo.cache.updateCivResources() // If you offered a trade last turn, this turn it will have been accepted/declined
for (stockpiledResource in civInfo.getCivResourceSupply().filter { it.resource.isStockpiled })
civInfo.gainStockpiledResource(stockpiledResource.resource.name, stockpiledResource.amount)
civInfo.gainStockpiledResource(stockpiledResource.resource, stockpiledResource.amount)
civInfo.civConstructions.startTurn()
civInfo.attacksSinceTurnStart.clear()

View File

@ -355,7 +355,7 @@ class CivInfoTransientCache(val civInfo: Civilization) {
newDetailedCivResources.subtractResourceRequirements(
unit.getResourceRequirementsPerTurn(), civInfo.gameInfo.ruleset, "Units")
newDetailedCivResources.removeAll { it.resource.hasUnique(UniqueType.CityResource) }
newDetailedCivResources.removeAll { it.resource.isCityWide }
// Check if anything has actually changed so we don't update stats for no reason - this uses List equality which means it checks the elements
if (civInfo.detailedCivResources == newDetailedCivResources) return

View File

@ -36,7 +36,7 @@ class TileResource : RulesetStatsObject(), GameResource {
var majorDepositAmount: DepositAmount = DepositAmount()
var minorDepositAmount: DepositAmount = DepositAmount()
val isCityWide by lazy { hasUnique(UniqueType.CityResource, StateForConditionals.IgnoreConditionals) }
val isStockpiled by lazy { hasUnique(UniqueType.Stockpiled, StateForConditionals.IgnoreConditionals) }

View File

@ -526,7 +526,8 @@ object UniqueTriggerActivation {
return {
val amount = unique.params[0].toInt()
civInfo.gainStockpiledResource(resourceName, amount)
if (city != null) city.gainStockpiledResource(resource, amount)
else civInfo.gainStockpiledResource(resource, amount)
val notificationText = getNotificationText(
notification, triggerNotificationText,
@ -545,7 +546,8 @@ object UniqueTriggerActivation {
return {
val amount = unique.params[0].toInt()
civInfo.gainStockpiledResource(resourceName, -amount)
if (city != null) city.gainStockpiledResource(resource, -amount)
else civInfo.gainStockpiledResource(resource, -amount)
val notificationText = getNotificationText(
notification, triggerNotificationText,

View File

@ -115,8 +115,9 @@ object UnitActionModifiers {
UniqueType.UnitActionStockpileCost -> {
val amount = conditional.params[0].toInt()
val resourceName = conditional.params[1]
if(unit.civ.getCivResourcesByName()[resourceName] != null)
unit.civ.gainStockpiledResource(resourceName, -amount)
val resource = unit.civ.gameInfo.ruleset.tileResources[resourceName]
if (resource != null && resource.isStockpiled)
unit.civ.gainStockpiledResource(resource, -amount)
}
UniqueType.UnitActionRemovingPromotion -> {
val promotionName = conditional.params[0]