Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for city level stockpiles to actually function #12710

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 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 @@ -206,6 +210,16 @@ class City : IsPartOfGameInfoSerialization, INamed {

fun getGreatPersonPercentageBonus() = GreatPersonPointsBreakdown.getGreatPersonPercentageBonus(this)
fun getGreatPersonPoints() = GreatPersonPointsBreakdown(this).sum()

fun gainStockpiledResource(resourceName: String, amount: Int) {

resourceStockpiles.add(resourceName, amount)
}

fun gainStockpiledResource(resource: TileResource, amount: Int) {
if (resource.isCityWide) resourceStockpiles.add(resource.name, amount)
else civ.gainStockpiledResource(resource.name, amount)
}

fun addStat(stat: Stat, amount: Int) {
when (stat) {
Expand All @@ -218,8 +232,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 +251,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]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we do when it's civ-wide? Fall down to "civ.getReserve(stat)" below?
I'd rather this was explicit in the "stat is TileResource", that it become a "return when()" so we see that we treat all TileResources cases exhaustively

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it like that originally, but wasn't sure if it should be explicit

else 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)
Comment on lines +438 to +439
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, to me, indicates that the gainStockpiledResource(String) should do the resource lookup, and then call the gainStockpiledResource(Resource) function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not entirely ideal unless we decide to add additional logic here, since the counter itself only cares about the resource name (for the sake of serialization)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, if this is what's wanted, the actual thing is to delete the function for strings (I added it for clarity, but the counter is still public rn, actually). Currently, the city version for strings can already be safely deleted

}

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
7 changes: 6 additions & 1 deletion 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.name, amount)
when (stat) {
Stat.Culture -> { policies.addCulture(amount)
if (amount > 0) totalCultureForContests += amount }
Expand All @@ -880,6 +880,11 @@ class Civilization : IsPartOfGameInfoSerialization {
// Happiness cannot be added as it is recalculated again, use a unique instead
}
}

fun gainStockpiledResource(resource: TileResource, amount: Int) {
if (resource.isCityWide) return
resourceStockpiles.add(resource.name, amount)
}

fun gainStockpiledResource(resourceName: String, amount: Int) {
resourceStockpiles.add(resourceName, amount)
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
2 changes: 2 additions & 0 deletions core/src/com/unciv/models/ruleset/tile/TileResource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class TileResource : RulesetStatsObject(), GameResource {
var majorDepositAmount: DepositAmount = DepositAmount()
var minorDepositAmount: DepositAmount = DepositAmount()

@delegate:Transient
val isCityWide by lazy { hasUnique(UniqueType.CityResource, StateForConditionals.IgnoreConditionals) }

@delegate:Transient
val isStockpiled by lazy { hasUnique(UniqueType.Stockpiled, StateForConditionals.IgnoreConditionals) }

private var improvementsInitialized = false
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
Loading