mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-30 15:30:43 -04:00
Created parameter-extracting functions for placeholder texts!
This commit is contained in:
parent
00983dd00e
commit
95e1e8279d
@ -5,6 +5,8 @@ import com.unciv.UniqueAbility
|
|||||||
import com.unciv.logic.map.MapUnit
|
import com.unciv.logic.map.MapUnit
|
||||||
import com.unciv.logic.map.TileInfo
|
import com.unciv.logic.map.TileInfo
|
||||||
import com.unciv.models.ruleset.unit.UnitType
|
import com.unciv.models.ruleset.unit.UnitType
|
||||||
|
import com.unciv.models.translations.equalsPlaceholderText
|
||||||
|
import com.unciv.models.translations.getPlaceholderParameters
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.collections.HashMap
|
import kotlin.collections.HashMap
|
||||||
import kotlin.collections.set
|
import kotlin.collections.set
|
||||||
@ -118,13 +120,13 @@ object BattleDamage {
|
|||||||
modifiers.putAll(getTileSpecificModifiers(attacker, defender.getTile()))
|
modifiers.putAll(getTileSpecificModifiers(attacker, defender.getTile()))
|
||||||
|
|
||||||
for (ability in attacker.unit.getUniques()) {
|
for (ability in attacker.unit.getUniques()) {
|
||||||
val regexResult = Regex(BONUS_AS_ATTACKER).matchEntire(ability) //to do: extend to defender, and penalyy
|
if(ability.equalsPlaceholderText("Bonus as Attacker []%")) {
|
||||||
if (regexResult == null) continue
|
val bonus = ability.getPlaceholderParameters()[0].toFloat() / 100
|
||||||
val bonus = regexResult.groups[1]!!.value.toFloat() / 100
|
|
||||||
if (modifiers.containsKey("Attacker Bonus"))
|
if (modifiers.containsKey("Attacker Bonus"))
|
||||||
modifiers["Attacker Bonus"] = modifiers["Attacker Bonus"]!! + bonus
|
modifiers["Attacker Bonus"] = modifiers["Attacker Bonus"]!! + bonus
|
||||||
else modifiers["Attacker Bonus"] = bonus
|
else modifiers["Attacker Bonus"] = bonus
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (attacker.unit.isEmbarked() && !attacker.unit.hasUnique("Amphibious"))
|
if (attacker.unit.isEmbarked() && !attacker.unit.hasUnique("Amphibious"))
|
||||||
modifiers["Landing"] = -0.5f
|
modifiers["Landing"] = -0.5f
|
||||||
|
@ -221,11 +221,8 @@ val curlyBraceRegex = Regex("""\{([^}]*)\}""")
|
|||||||
* but with placeholder or sentence brackets removed.
|
* but with placeholder or sentence brackets removed.
|
||||||
*/
|
*/
|
||||||
fun String.tr(): String {
|
fun String.tr(): String {
|
||||||
val activeMods = if (UncivGame.Current.isGameInfoInitialized()) {
|
val activeMods = if (UncivGame.Current.isGameInfoInitialized())
|
||||||
UncivGame.Current.gameInfo.gameParameters.mods
|
UncivGame.Current.gameInfo.gameParameters.mods else null
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
|
|
||||||
// There might still be optimization potential here!
|
// There might still be optimization potential here!
|
||||||
if (contains("[")) { // Placeholders!
|
if (contains("[")) { // Placeholders!
|
||||||
@ -245,7 +242,8 @@ fun String.tr(): String {
|
|||||||
// Convert "work on [building] has completed in [city]" to "work on [] has completed in []"
|
// Convert "work on [building] has completed in [city]" to "work on [] has completed in []"
|
||||||
val translationStringWithSquareBracketsOnly = this.replace(squareBraceRegex, "[]")
|
val translationStringWithSquareBracketsOnly = this.replace(squareBraceRegex, "[]")
|
||||||
// That is now the key into the translation HashMap!
|
// That is now the key into the translation HashMap!
|
||||||
val translationEntry = UncivGame.Current.translations.get(translationStringWithSquareBracketsOnly, UncivGame.Current.settings.language, activeMods)
|
val translationEntry = UncivGame.Current.translations
|
||||||
|
.get(translationStringWithSquareBracketsOnly, UncivGame.Current.settings.language, activeMods)
|
||||||
|
|
||||||
if (translationEntry == null ||
|
if (translationEntry == null ||
|
||||||
!translationEntry.containsKey(UncivGame.Current.settings.language)) {
|
!translationEntry.containsKey(UncivGame.Current.settings.language)) {
|
||||||
@ -253,7 +251,9 @@ fun String.tr(): String {
|
|||||||
return this.replace(eitherSquareBraceRegex, "")
|
return this.replace(eitherSquareBraceRegex, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
val termsInMessage = squareBraceRegex.findAll(this).map { it.groups[1]!!.value }.toList()
|
// Take the terms in the message, WITHOUT square brackets
|
||||||
|
val termsInMessage = this.getPlaceholderParameters()
|
||||||
|
// Take the term from the placeholder, INCLUDING the square brackets
|
||||||
val termsInTranslationPlaceholder = squareBraceRegex.findAll(translationEntry.entry).map { it.value }.toList()
|
val termsInTranslationPlaceholder = squareBraceRegex.findAll(translationEntry.entry).map { it.value }.toList()
|
||||||
if (termsInMessage.size != termsInTranslationPlaceholder.size)
|
if (termsInMessage.size != termsInTranslationPlaceholder.size)
|
||||||
throw Exception("Message $this has a different number of terms than the placeholder $translationEntry!")
|
throw Exception("Message $this has a different number of terms than the placeholder $translationEntry!")
|
||||||
@ -271,3 +271,10 @@ fun String.tr(): String {
|
|||||||
|
|
||||||
return UncivGame.Current.translations.getText(this, UncivGame.Current.settings.language, activeMods)
|
return UncivGame.Current.translations.getText(this, UncivGame.Current.settings.language, activeMods)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun String.equalsPlaceholderText(str:String): Boolean {
|
||||||
|
if (first() != str.first()) return false // for quick negative return 95% of the time
|
||||||
|
return this.replace(squareBraceRegex, "[]") == str
|
||||||
|
}
|
||||||
|
|
||||||
|
fun String.getPlaceholderParameters() = squareBraceRegex.findAll(this).map { it.groups[1]!!.value }.toList()
|
@ -9,6 +9,8 @@ import com.unciv.Constants
|
|||||||
import com.unciv.UncivGame
|
import com.unciv.UncivGame
|
||||||
import com.unciv.logic.map.MapUnit
|
import com.unciv.logic.map.MapUnit
|
||||||
import com.unciv.models.UnitAction
|
import com.unciv.models.UnitAction
|
||||||
|
import com.unciv.models.translations.equalsPlaceholderText
|
||||||
|
import com.unciv.models.translations.getPlaceholderParameters
|
||||||
import com.unciv.ui.utils.*
|
import com.unciv.ui.utils.*
|
||||||
import com.unciv.ui.worldscreen.WorldScreen
|
import com.unciv.ui.worldscreen.WorldScreen
|
||||||
import kotlin.concurrent.thread
|
import kotlin.concurrent.thread
|
||||||
@ -20,16 +22,16 @@ class UnitActionsTable(val worldScreen: WorldScreen) : Table() {
|
|||||||
|
|
||||||
private fun getIconAndKeyForUnitAction(unitAction: String): UnitIconAndKey {
|
private fun getIconAndKeyForUnitAction(unitAction: String): UnitIconAndKey {
|
||||||
when {
|
when {
|
||||||
unitAction.startsWith("Upgrade to") -> {
|
unitAction.equalsPlaceholderText("Upgrade to [] ([] gold)") -> {
|
||||||
// Regexplaination: start with a [, take as many non-] chars as you can, until you reach a ].
|
// Regexplaination: start with a [, take as many non-] chars as you can, until you reach a ].
|
||||||
// What you find between the first [ and the first ] that comes after it, will be group no. 1
|
// What you find between the first [ and the first ] that comes after it, will be group no. 1
|
||||||
val unitToUpgradeTo = Regex("""Upgrade to \[([^\]]*)\]""").find(unitAction)!!.groups[1]!!.value
|
val unitToUpgradeTo = unitAction.getPlaceholderParameters()[0]
|
||||||
return UnitIconAndKey(ImageGetter.getUnitIcon(unitToUpgradeTo), 'u')
|
return UnitIconAndKey(ImageGetter.getUnitIcon(unitToUpgradeTo), 'u')
|
||||||
}
|
}
|
||||||
unitAction.startsWith("Create ") -> {
|
unitAction.equalsPlaceholderText("Create []") -> {
|
||||||
// Regexplaination: start with a [, take as many non-] chars as you can, until you reach a ].
|
// Regexplaination: start with a [, take as many non-] chars as you can, until you reach a ].
|
||||||
// What you find between the first [ and the first ] that comes after it, will be group no. 1
|
// What you find between the first [ and the first ] that comes after it, will be group no. 1
|
||||||
val improvementName = Regex("""Create \[([^]]*)\]""").find(unitAction)!!.groups[1]!!.value
|
val improvementName = unitAction.getPlaceholderParameters()[0]
|
||||||
return UnitIconAndKey(ImageGetter.getImprovementIcon(improvementName), 'i')
|
return UnitIconAndKey(ImageGetter.getImprovementIcon(improvementName), 'i')
|
||||||
}
|
}
|
||||||
unitAction.startsWith("Sleep") -> return UnitIconAndKey(ImageGetter.getImage("OtherIcons/Sleep"), 'f')
|
unitAction.startsWith("Sleep") -> return UnitIconAndKey(ImageGetter.getImage("OtherIcons/Sleep"), 'f')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user