Skip to content

Commit

Permalink
chore: StateForConditionals accepts GameInfo, HiddenBySettings -> Una…
Browse files Browse the repository at this point in the history
…vailableBySettings for pending changes
  • Loading branch information
yairm210 committed Oct 20, 2024
1 parent 9e1d29f commit 3191974
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class GreatPersonManager : IsPartOfGameInfoSerialization {
fun getGreatPeople() = civInfo.gameInfo.ruleset.units.values.asSequence()
.filter { it.isGreatPerson }
.map { civInfo.getEquivalentUnit(it.name) }
.filterNot { it.isHiddenBySettings(civInfo.gameInfo) }
.filterNot { it.isUnavailableBySettings(civInfo.gameInfo) }
.toHashSet()

fun getGreatPersonPointsForNextTurn(): Counter<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ class QuestManager : IsPartOfGameInfoSerialization {
building.isWonder
&& challenger.tech.isResearched(building)
// Can't be disabled
&& !building.isHiddenBySettings(civ.gameInfo)
&& !building.isUnavailableBySettings(civ.gameInfo)
// Can't be a unique wonder
&& building.uniqueTo == null
// Big loop last: Exists or more than 25% built anywhere
Expand Down Expand Up @@ -825,7 +825,7 @@ class QuestManager : IsPartOfGameInfoSerialization {
.distinct()
// The hidden test is already done by getGreatPeople for the civ-specific units,
// repeat for the replaced one we'll be asking for
.filterNot { it in existingGreatPeople || it.isHiddenBySettings(civ.gameInfo) }
.filterNot { it in existingGreatPeople || it.isUnavailableBySettings(civ.gameInfo) }
.toList()

return greatPeople.randomOrNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RuinsManager(

private fun isPossibleReward(ruinReward: RuinReward, unit: MapUnit): Boolean {
if (ruinReward.name in lastChosenRewards) return false
if (ruinReward.isHiddenBySettings(civInfo.gameInfo)) return false
if (ruinReward.isUnavailableBySettings(civInfo.gameInfo)) return false
val stateForConditionals = StateForConditionals(civInfo, unit = unit, tile = unit.getTile())
if (ruinReward.hasUnique(UniqueType.Unavailable, stateForConditionals)) return false
if (ruinReward.getMatchingUniques(UniqueType.OnlyAvailable, StateForConditionals.IgnoreConditionals)
Expand Down
6 changes: 3 additions & 3 deletions core/src/com/unciv/models/ruleset/Building.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Building : RulesetStatsObject(), INonPerpetualConstruction {
fun getDescription(city: City, showAdditionalInfo: Boolean) = BuildingDescriptions.getDescription(this, city, showAdditionalInfo)
override fun getCivilopediaTextLines(ruleset: Ruleset) = BuildingDescriptions.getCivilopediaTextLines(this, ruleset)

override fun isHiddenBySettings(gameInfo: GameInfo): Boolean {
if (super<INonPerpetualConstruction>.isHiddenBySettings(gameInfo)) return true
override fun isUnavailableBySettings(gameInfo: GameInfo): Boolean {
if (super<INonPerpetualConstruction>.isUnavailableBySettings(gameInfo)) return true
if (!gameInfo.gameParameters.nuclearWeaponsEnabled && hasUnique(UniqueType.EnablesNuclearWeapons)) return true
return isHiddenByStartingEra(gameInfo)
}
Expand Down Expand Up @@ -264,7 +264,7 @@ class Building : RulesetStatsObject(), INonPerpetualConstruction {
if (cityConstructions.isBuilt(name))
yield(RejectionReasonType.AlreadyBuilt.toInstance())

if (isHiddenBySettings(civ.gameInfo)) {
if (isUnavailableBySettings(civ.gameInfo)) {
// Repeat the starting era test isHiddenBySettings already did to change the RejectionReasonType
if (isHiddenByStartingEra(civ.gameInfo))
yield(RejectionReasonType.WonderDisabledEra.toInstance())
Expand Down
4 changes: 2 additions & 2 deletions core/src/com/unciv/models/ruleset/RuinReward.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class RuinReward : RulesetObject() {

override fun makeLink() = "" //No own category on Civilopedia screen

override fun isHiddenBySettings(gameInfo: GameInfo) =
gameInfo.difficulty in excludedDifficulties || super.isHiddenBySettings(gameInfo)
override fun isUnavailableBySettings(gameInfo: GameInfo) =
gameInfo.difficulty in excludedDifficulties || super.isUnavailableBySettings(gameInfo)
}
6 changes: 3 additions & 3 deletions core/src/com/unciv/models/ruleset/unique/IHasUniques.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ interface IHasUniques : INamed {
* - Default implementation checks disabling by Religion, Espionage or Victory types.
* - Overrides need to deal with e.g. Era-specific wonder disabling, no-nukes, ruin rewards by difficulty, and so on!
*/
fun isHiddenBySettings(gameInfo: GameInfo): Boolean {
fun isUnavailableBySettings(gameInfo: GameInfo): Boolean {
if (!gameInfo.isReligionEnabled() && hasUnique(UniqueType.HiddenWithoutReligion)) return true
if (!gameInfo.isEspionageEnabled() && hasUnique(UniqueType.HiddenWithoutEspionage)) return true
if (getMatchingUniques(UniqueType.HiddenWithoutVictoryType).any { it.params[0] !in gameInfo.gameParameters.victoryTypes }) return true
Expand All @@ -106,7 +106,7 @@ interface IHasUniques : INamed {
* Is this ruleset object hidden from Civilopedia?
*
* - Obviously, the [UniqueType.HiddenFromCivilopedia] test is done here (and nowhere else - exception TranslationFileWriter for Tutorials).
* - Includes the [isHiddenBySettings] test if [gameInfo] is known, otherwise existence of Religion is guessed from [ruleset], and all victory types are assumed enabled.
* - Includes the [isUnavailableBySettings] test if [gameInfo] is known, otherwise existence of Religion is guessed from [ruleset], and all victory types are assumed enabled.
* - Note: RulesetObject-type specific overrides should not be necessary unless (TODO) we implement support for conditionals on the 'Hidden*' Uniques.
* @param gameInfo Defaults to [UncivGame.getGameInfoOrNull]. Civilopedia must also be able to run from MainMenu without a game loaded. In that case only this parameter can be `null`. So if you know it - provide!
* @param ruleset required if [gameInfo] is null, otherwise optional.
Expand All @@ -117,7 +117,7 @@ interface IHasUniques : INamed {
ruleset: Ruleset? = null
): Boolean {
if (hasUnique(UniqueType.HiddenFromCivilopedia)) return true
if (gameInfo != null && isHiddenBySettings(gameInfo)) return true
if (gameInfo != null && isUnavailableBySettings(gameInfo)) return true
if (gameInfo == null && hasUnique(UniqueType.HiddenWithoutReligion) && ruleset!!.beliefs.isEmpty()) return true
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.unciv.models.ruleset.unique

import com.unciv.logic.GameInfo
import com.unciv.logic.battle.CityCombatant
import com.unciv.logic.battle.CombatAction
import com.unciv.logic.battle.ICombatant
Expand All @@ -23,6 +24,7 @@ data class StateForConditionals(
val combatAction: CombatAction? = null,

val region: Region? = null,
val gameInfo: GameInfo? = civInfo?.gameInfo,

val ignoreConditionals: Boolean = false,
) {
Expand Down Expand Up @@ -72,8 +74,6 @@ data class StateForConditionals(
relevantUnit?.civ
}

val gameInfo by lazy { relevantCiv?.gameInfo }

fun getResourceAmount(resourceName: String): Int {
return when {
relevantCity != null -> relevantCity!!.getAvailableResourceAmount(resourceName)
Expand Down
6 changes: 3 additions & 3 deletions core/src/com/unciv/models/ruleset/unit/BaseUnit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
override fun getCivilopediaTextLines(ruleset: Ruleset): List<FormattedLine> =
BaseUnitDescriptions.getCivilopediaTextLines(this, ruleset)

override fun isHiddenBySettings(gameInfo: GameInfo) =
super<INonPerpetualConstruction>.isHiddenBySettings(gameInfo) ||
override fun isUnavailableBySettings(gameInfo: GameInfo) =
super<INonPerpetualConstruction>.isUnavailableBySettings(gameInfo) ||
(!gameInfo.gameParameters.nuclearWeaponsEnabled && isNuclearWeapon())

fun getUpgradeUnits(stateForConditionals: StateForConditionals = StateForConditionals.EmptyState): Sequence<String> {
Expand Down Expand Up @@ -212,7 +212,7 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
if (civ.cache.uniqueUnits.any { it.replaces == name })
yield(RejectionReasonType.ReplacedByOurUnique.toInstance("Our unique unit replaces this"))

if (isHiddenBySettings(civ.gameInfo))
if (isUnavailableBySettings(civ.gameInfo))
yield(RejectionReasonType.DisabledBySetting.toInstance())

if (hasUnique(UniqueType.Unbuildable, stateForConditionals))
Expand Down

0 comments on commit 3191974

Please sign in to comment.