Skip to content

Commit

Permalink
Allow for city level stockpiles to actually function (#12710)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
SeventhM authored Jan 4, 2025
1 parent 919c4e3 commit 2e32ecc
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 23 deletions.
21 changes: 14 additions & 7 deletions core/src/com/unciv/logic/city/City.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions core/src/com/unciv/logic/city/CityConstructions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/com/unciv/logic/city/CityResources.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 5 additions & 4 deletions core/src/com/unciv/logic/civilization/Civilization.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/src/com/unciv/models/ruleset/tile/TileResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 2e32ecc

Please sign in to comment.