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

Provide a default unitType for units to gain global uniques #12690

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions core/src/com/unciv/models/ruleset/Ruleset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class Ruleset {
val greatGeneralUnits by lazy {
units.values.filter { it.hasUnique(UniqueType.GreatPersonFromCombat, StateForConditionals.IgnoreConditionals) }
}

val allUnitTypes by lazy { unitTypes["All"] }

val tileRemovals by lazy { tileImprovements.values.filter { it.name.startsWith(Constants.remove) } }
val nonRoadTileRemovals by lazy { tileRemovals.filter { rulesetImprovement ->
Expand Down
28 changes: 19 additions & 9 deletions core/src/com/unciv/models/ruleset/unit/BaseUnit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,21 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {


override fun hasUnique(uniqueType: UniqueType, state: StateForConditionals?): Boolean {
return super<RulesetObject>.hasUnique(uniqueType, state) || ::ruleset.isInitialized && type.hasUnique(uniqueType, state)
return super<RulesetObject>.hasUnique(uniqueType, state) ||
::ruleset.isInitialized && type.hasUnique(uniqueType, state) ||
::ruleset.isInitialized && ruleset.allUnitTypes?.hasUnique(uniqueType, state) == true
}

override fun hasUnique(uniqueTag: String, state: StateForConditionals?): Boolean {
return super<RulesetObject>.hasUnique(uniqueTag, state) || ::ruleset.isInitialized && type.hasUnique(uniqueTag, state)
return super<RulesetObject>.hasUnique(uniqueTag, state) ||
::ruleset.isInitialized && type.hasUnique(uniqueTag, state) ||
::ruleset.isInitialized && ruleset.allUnitTypes?.hasUnique(uniqueTag, state) == true
}

override fun hasTagUnique(tagUnique: String): Boolean {
return super<RulesetObject>.hasTagUnique(tagUnique) || ::ruleset.isInitialized && type.hasTagUnique(tagUnique)
return super<RulesetObject>.hasTagUnique(tagUnique) ||
::ruleset.isInitialized && type.hasTagUnique(tagUnique) ||
::ruleset.isInitialized && ruleset.allUnitTypes?.hasTagUnique(tagUnique) == true
}

/** Allows unique functions (getMatchingUniques, hasUnique) to "see" uniques from the UnitType */
Expand All @@ -136,11 +142,13 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
return ourUniques
}
val typeUniques = type.getMatchingUniques(uniqueType, state)
val globalTypeUniques = ruleset.allUnitTypes?.getMatchingUniques(uniqueType, state) ?: emptySequence()
// Memory optimization - very rarely do we actually get uniques from both sources,
// and sequence addition is expensive relative to the rare case that we'll actually need it
if (ourUniques.none()) return typeUniques
if (typeUniques.none()) return ourUniques
return ourUniques + type.getMatchingUniques(uniqueType, state)
if (ourUniques.none() && globalTypeUniques.none()) return typeUniques
if (typeUniques.none() && globalTypeUniques.none()) return ourUniques
if (typeUniques.none() && ourUniques.none()) return globalTypeUniques
return ourUniques + typeUniques + globalTypeUniques
}

/** Allows unique functions (getMatchingUniques, hasUnique) to "see" uniques from the UnitType */
Expand All @@ -150,11 +158,13 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
return ourUniques
}
val typeUniques = type.getMatchingUniques(uniqueTag, state)
val globalTypeUniques = ruleset.allUnitTypes?.getMatchingUniques(uniqueTag, state) ?: emptySequence()
// Memory optimization - very rarely do we actually get uniques from both sources,
// and sequence addition is expensive relative to the rare case that we'll actually need it
if (ourUniques.none()) return typeUniques
if (typeUniques.none()) return ourUniques
return ourUniques + type.getMatchingUniques(uniqueTag, state)
if (ourUniques.none() && globalTypeUniques.none()) return typeUniques
if (typeUniques.none() && globalTypeUniques.none()) return ourUniques
if (typeUniques.none() && ourUniques.none()) return globalTypeUniques
return ourUniques + typeUniques + globalTypeUniques
}

override fun getProductionCost(civInfo: Civilization, city: City?): Int = costFunctions.getProductionCost(civInfo, city)
Expand Down
Loading