Created parameter-extracting functions for placeholder texts!

This commit is contained in:
Yair Morgenstern 2020-07-20 00:04:21 +03:00
parent 00983dd00e
commit 95e1e8279d
3 changed files with 40 additions and 29 deletions

View File

@ -5,6 +5,8 @@ import com.unciv.UniqueAbility
import com.unciv.logic.map.MapUnit
import com.unciv.logic.map.TileInfo
import com.unciv.models.ruleset.unit.UnitType
import com.unciv.models.translations.equalsPlaceholderText
import com.unciv.models.translations.getPlaceholderParameters
import java.util.*
import kotlin.collections.HashMap
import kotlin.collections.set
@ -118,13 +120,13 @@ object BattleDamage {
modifiers.putAll(getTileSpecificModifiers(attacker, defender.getTile()))
for (ability in attacker.unit.getUniques()) {
val regexResult = Regex(BONUS_AS_ATTACKER).matchEntire(ability) //to do: extend to defender, and penalyy
if (regexResult == null) continue
val bonus = regexResult.groups[1]!!.value.toFloat() / 100
if(ability.equalsPlaceholderText("Bonus as Attacker []%")) {
val bonus = ability.getPlaceholderParameters()[0].toFloat() / 100
if (modifiers.containsKey("Attacker Bonus"))
modifiers["Attacker Bonus"] = modifiers["Attacker Bonus"]!! + bonus
else modifiers["Attacker Bonus"] = bonus
}
}
if (attacker.unit.isEmbarked() && !attacker.unit.hasUnique("Amphibious"))
modifiers["Landing"] = -0.5f

View File

@ -221,11 +221,8 @@ val curlyBraceRegex = Regex("""\{([^}]*)\}""")
* but with placeholder or sentence brackets removed.
*/
fun String.tr(): String {
val activeMods = if (UncivGame.Current.isGameInfoInitialized()) {
UncivGame.Current.gameInfo.gameParameters.mods
} else {
null
}
val activeMods = if (UncivGame.Current.isGameInfoInitialized())
UncivGame.Current.gameInfo.gameParameters.mods else null
// There might still be optimization potential here!
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 []"
val translationStringWithSquareBracketsOnly = this.replace(squareBraceRegex, "[]")
// 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 ||
!translationEntry.containsKey(UncivGame.Current.settings.language)) {
@ -253,7 +251,9 @@ fun String.tr(): String {
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()
if (termsInMessage.size != termsInTranslationPlaceholder.size)
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)
}
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()

View File

@ -9,6 +9,8 @@ import com.unciv.Constants
import com.unciv.UncivGame
import com.unciv.logic.map.MapUnit
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.worldscreen.WorldScreen
import kotlin.concurrent.thread
@ -20,16 +22,16 @@ class UnitActionsTable(val worldScreen: WorldScreen) : Table() {
private fun getIconAndKeyForUnitAction(unitAction: String): UnitIconAndKey {
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 ].
// 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')
}
unitAction.startsWith("Create ") -> {
unitAction.equalsPlaceholderText("Create []") -> {
// 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
val improvementName = Regex("""Create \[([^]]*)\]""").find(unitAction)!!.groups[1]!!.value
val improvementName = unitAction.getPlaceholderParameters()[0]
return UnitIconAndKey(ImageGetter.getImprovementIcon(improvementName), 'i')
}
unitAction.startsWith("Sleep") -> return UnitIconAndKey(ImageGetter.getImage("OtherIcons/Sleep"), 'f')