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

Countable comparison conditional uniques #11308

Merged
merged 3 commits into from
Apr 1, 2024
Merged
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
77 changes: 77 additions & 0 deletions core/src/com/unciv/models/ruleset/unique/Conditionals.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,57 @@ object Conditionals {
return compare(statReserve, lowerLimit * gameSpeedModifier, upperLimit * gameSpeedModifier)
}

fun getCountableAmount(countable: String): Float? {
if (countable.toFloatOrNull() != null) return countable.toFloat()

val relevantStat = Stat.safeValueOf(countable)

if (relevantStat != null) {
return if (relevantCity != null) {
relevantCity!!.getStatReserve(relevantStat).toFloat()
} else if (relevantStat in Stat.statsWithCivWideField && relevantCiv != null) {
relevantCiv!!.getStatReserve(relevantStat).toFloat()
} else {
null
}
}

if (gameInfo == null) return null

if (countable == "year") return gameInfo!!.getYear(gameInfo!!.turns).toFloat()
if (gameInfo!!.ruleset.tileResources.containsKey(countable))
return getResourceAmount(countable).toFloat()

return null
}

fun compareCountables(
first: String,
second: String,
compare: (first: Float, second: Float) -> Boolean): Boolean {

val firstNumber = getCountableAmount(first)
val secondNumber = getCountableAmount(second)

return if (firstNumber != null && secondNumber != null)
compare(firstNumber, secondNumber)
else
false
}

fun compareCountables(first: String, second: String, third: String,
compare: (first: Float, second: Float, third: Float) -> Boolean): Boolean {

val firstNumber = getCountableAmount(first)
val secondNumber = getCountableAmount(second)
val thirdNumber = getCountableAmount(third)

return if (firstNumber != null && secondNumber != null && thirdNumber != null)
compare(firstNumber, secondNumber, thirdNumber)
else
false
}

return when (condition.type) {
// These are 'what to do' and not 'when to do' conditionals
UniqueType.ConditionalTimedUnique -> true
Expand Down Expand Up @@ -286,6 +337,32 @@ object Conditionals {
&& it.policies.isAdopted(unique.sourceObjectName!!) // guarded by the sourceObjectType check
} }

UniqueType.ConditionalCountableEqualTo ->
compareCountables(condition.params[0], condition.params[1]) {
first, second -> first == second
}

UniqueType.ConditionalCountableDifferentThan ->
compareCountables(condition.params[0], condition.params[1]) {
first, second -> first != second
}

UniqueType.ConditionalCountableGreaterThan ->
compareCountables(condition.params[0], condition.params[1]) {
first, second -> first > second
}

UniqueType.ConditionalCountableLessThan ->
compareCountables(condition.params[0], condition.params[1]) {
first, second -> first < second
}

UniqueType.ConditionalCountableBetween ->
compareCountables(condition.params[0], condition.params[1], condition.params[2]) {
first, second, third ->
first in second..third
}

else -> false
}
}
Expand Down
22 changes: 22 additions & 0 deletions core/src/com/unciv/models/ruleset/unique/UniqueParameterType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ enum class UniqueParameterType(
}
},

Countable("countable", "1000", "This indicate a number or a numeric variable") {
// todo add more countables
private val knownValues = setOf(
"year"
)

override fun isKnownValue(parameterText: String, ruleset: Ruleset): Boolean {
if (parameterText in knownValues) return true
if (parameterText.toIntOrNull() != null) return true
if (parameterText.toFloatOrNull() != null) return true
if (Stat.isStat(parameterText)) return true
if (parameterText in ruleset.tileResources) return true
return false
}

override fun getErrorSeverity(
parameterText: String, ruleset: Ruleset): UniqueType.UniqueParameterErrorSeverity? {
return if (isKnownValue(parameterText, ruleset)) null
else UniqueType.UniqueParameterErrorSeverity.RulesetSpecific
}
},

// todo potentially remove if OneTimeRevealSpecificMapTiles changes
KeywordAll("'all'", "All") {
override fun getErrorSeverity(parameterText: String, ruleset: Ruleset) =
Expand Down
7 changes: 7 additions & 0 deletions core/src/com/unciv/models/ruleset/unique/UniqueType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,13 @@ enum class UniqueType(
ConditionalInRegionOfType("in [regionType] Regions", UniqueTarget.Conditional),
ConditionalInRegionExceptOfType("in all except [regionType] Regions", UniqueTarget.Conditional),

/////// countables conditionals
ConditionalCountableEqualTo("when number of [countable] is equal to [countable]", UniqueTarget.Conditional),
ConditionalCountableDifferentThan("when number of [countable] is different than [countable]", UniqueTarget.Conditional),
ConditionalCountableGreaterThan("when number of [countable] is greater than [countable]", UniqueTarget.Conditional),
ConditionalCountableLessThan("when number of [countable] is less than [countable]", UniqueTarget.Conditional),
ConditionalCountableBetween("when number of [countable] is between [countable] and [countable]", UniqueTarget.Conditional),

//endregion

///////////////////////////////////////// region 09 TRIGGERED ONE-TIME /////////////////////////////////////////
Expand Down
26 changes: 26 additions & 0 deletions docs/Modders/uniques.md
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,31 @@ Simple unique parameters are explained by mouseover. Complex parameters are expl

Applicable to: Conditional

??? example "&lt;when number of [countable] is equal to [countable]&gt;"
Example: "&lt;when number of [1000] is equal to [1000]&gt;"

Applicable to: Conditional

??? example "&lt;when number of [countable] is different than [countable]&gt;"
Example: "&lt;when number of [1000] is different than [1000]&gt;"

Applicable to: Conditional

??? example "&lt;when number of [countable] is greater than [countable]&gt;"
Example: "&lt;when number of [1000] is greater than [1000]&gt;"

Applicable to: Conditional

??? example "&lt;when number of [countable] is less than [countable]&gt;"
Example: "&lt;when number of [1000] is less than [1000]&gt;"

Applicable to: Conditional

??? example "&lt;when number of [countable] is between [countable] and [countable]&gt;"
Example: "&lt;when number of [1000] is between [1000] and [1000]&gt;"

Applicable to: Conditional

## TriggerCondition uniques
!!! note ""

Expand Down Expand Up @@ -2388,6 +2413,7 @@ Simple unique parameters are explained by mouseover. Complex parameters are expl
*[civWideStat]: All the following stats have civ-wide fields: `Gold`, `Science`, `Culture`, `Faith`.
*[combatantFilter]: This indicates a combatant, which can either be a unit or a city (when bombarding). Must either be `City` or a `mapUnitFilter`.
*[costOrStrength]: `Cost` or `Strength`.
*[countable]: This indicate a number or a numeric variable.
*[era]: The name of any era.
*[event]: The name of any event.
*[foundingOrEnhancing]: `founding` or `enhancing`.
Expand Down
Loading