Skip to content

Commit

Permalink
perf(memory): Save stateForConditionals on tile for single-create
Browse files Browse the repository at this point in the history
  • Loading branch information
yairm210 committed Nov 5, 2024
1 parent ba37be8 commit 6cc5a87
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions core/src/com/unciv/logic/map/tile/Tile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,17 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable {

fun isRoughTerrain() = allTerrains.any { it.isRough() }

private val stateThisTile: StateForConditionals = StateForConditionals(tile = this)
/** Checks whether any of the TERRAINS of this tile has a certain unique */
fun terrainHasUnique(uniqueType: UniqueType, state: StateForConditionals = StateForConditionals(tile = this)) =
fun terrainHasUnique(uniqueType: UniqueType, state: StateForConditionals = stateThisTile) =
terrainUniqueMap.getMatchingUniques(uniqueType, state).any()
/** Get all uniques of this type that any TERRAIN on this tile has */
fun getTerrainMatchingUniques(uniqueType: UniqueType, stateForConditionals: StateForConditionals = StateForConditionals(tile = this) ): Sequence<Unique> {
fun getTerrainMatchingUniques(uniqueType: UniqueType, stateForConditionals: StateForConditionals = stateThisTile ): Sequence<Unique> {
return terrainUniqueMap.getMatchingUniques(uniqueType, stateForConditionals)
}

/** Get all uniques of this type that any part of this tile has: terrains, improvement, resource */
fun getMatchingUniques(uniqueType: UniqueType, stateForConditionals: StateForConditionals = StateForConditionals(tile=this)): Sequence<Unique> {
fun getMatchingUniques(uniqueType: UniqueType, stateForConditionals: StateForConditionals = stateThisTile): Sequence<Unique> {
var uniques = getTerrainMatchingUniques(uniqueType, stateForConditionals)
if (getUnpillagedImprovement() != null) {
val tileImprovement = getTileImprovement()
Expand Down Expand Up @@ -549,7 +550,7 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable {
if (naturalWonder != null) bonus += getNaturalWonder().defenceBonus
val tileImprovement = getUnpillagedTileImprovement()
if (tileImprovement != null && includeImprovementBonus) {
for (unique in tileImprovement.getMatchingUniques(UniqueType.DefensiveBonus, StateForConditionals(tile = this)))
for (unique in tileImprovement.getMatchingUniques(UniqueType.DefensiveBonus, stateThisTile))
bonus += unique.params[0].toFloat() / 100
}
return bonus
Expand Down Expand Up @@ -766,7 +767,7 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable {
return
}

for (unique in newResource.getMatchingUniques(UniqueType.ResourceAmountOnTiles, StateForConditionals(tile = this))) {
for (unique in newResource.getMatchingUniques(UniqueType.ResourceAmountOnTiles, stateThisTile)) {
if (matchesTerrainFilter(unique.params[0])) {
resourceAmount = unique.params[1].toInt()
return
Expand Down

1 comment on commit 6cc5a87

@EmperorPinguin
Copy link
Contributor

Choose a reason for hiding this comment

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

@yairm210 things are failing for me just after this was rolled out, could there be a causal relationship?

Please sign in to comment.