From 3d85b2fad30fcca398e3f74023920a9fb2e6a48a Mon Sep 17 00:00:00 2001 From: yairm210 Date: Tue, 15 Jul 2025 11:35:38 +0300 Subject: [PATCH] chore: update purity --- build.gradle.kts | 12 ++---------- core/src/com/unciv/logic/MultiFilter.kt | 2 +- core/src/com/unciv/logic/map/TileMap.kt | 11 +++++++++++ .../com/unciv/models/translations/Translations.kt | 2 +- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 7c4d06080e..6aa3038445 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,7 +37,7 @@ plugins { // This is *with* gradle 8.2 downloaded according the project specs, no idea what that's about kotlin("multiplatform") version "1.9.24" kotlin("plugin.serialization") version "1.9.24" - id("io.github.yairm210.purity-plugin") version "0.0.18" apply(false) + id("io.github.yairm210.purity-plugin") version "0.0.21" apply(false) } allprojects { @@ -48,22 +48,14 @@ allprojects { apply(plugin = "io.github.yairm210.purity-plugin") configure{ wellKnownPureFunctions = setOf( - "kotlin.let", - "kotlin.run", - "kotlin.also", - "kotlin.apply", - "kotlin.takeIf", - "kotlin.takeUnless", - "kotlin.ranges.coerceIn", - "kotlin.ranges.coerceAtLeast", "com.unciv.logic.civilization.diplomacy.RelationshipLevel.compareTo", ) wellKnownReadonlyFunctions = setOf( // Looks like the Collection.contains is not considered overridden :thunk: "java.util.AbstractCollection.contains", + "java.util.AbstractList.get", ) wellKnownPureClasses = setOf( - "kotlin.enums.EnumEntries", ) } diff --git a/core/src/com/unciv/logic/MultiFilter.kt b/core/src/com/unciv/logic/MultiFilter.kt index c3d2e2771c..9d77db3ded 100644 --- a/core/src/com/unciv/logic/MultiFilter.kt +++ b/core/src/com/unciv/logic/MultiFilter.kt @@ -37,7 +37,7 @@ object MultiFilter { return filterFunction(input) } - @Pure + @Pure @Suppress("purity") fun getAllSingleFilters(input: String): Sequence = when { input.hasSurrounding(andPrefix, andSuffix) && input.contains(andSeparator) -> // Resolve "AND" filters diff --git a/core/src/com/unciv/logic/map/TileMap.kt b/core/src/com/unciv/logic/map/TileMap.kt index 435dbce7f5..34160b89e5 100644 --- a/core/src/com/unciv/logic/map/TileMap.kt +++ b/core/src/com/unciv/logic/map/TileMap.kt @@ -18,6 +18,7 @@ import com.unciv.models.ruleset.unique.UniqueType import com.unciv.models.ruleset.unit.BaseUnit import com.unciv.utils.addToMapOfSets import com.unciv.utils.contains +import yairm210.purity.annotations.Readonly import java.lang.Integer.max import java.util.concurrent.ConcurrentHashMap import kotlin.math.abs @@ -206,19 +207,24 @@ class TileMap(initialCapacity: Int = 10) : IsPartOfGameInfoSerialization { return toReturn } + @Readonly operator fun contains(vector: Vector2) = contains(vector.x.toInt(), vector.y.toInt()) + @Readonly operator fun get(vector: Vector2) = get(vector.x.toInt(), vector.y.toInt()) + @Readonly fun contains(x: Int, y: Int) = getOrNull(x, y) != null + @Readonly operator fun get(x: Int, y: Int) = tileMatrix[x - leftX][y - bottomY]!! /** @return tile at hex coordinates ([x],[y]) or null if they are outside the map. Does *not* respect world wrap, use [getIfTileExistsOrNull] for that. */ + @Readonly private fun getOrNull (x: Int, y: Int): Tile? = tileMatrix.getOrNull(x - leftX)?.getOrNull(y - bottomY) @@ -230,16 +236,19 @@ class TileMap(initialCapacity: Int = 10) : IsPartOfGameInfoSerialization { /** @return All tiles in a hexagon of radius [distance], including the tile at [origin] and all up to [distance] steps away. * Respects map edges and world wrap. */ + @Readonly fun getTilesInDistance(origin: Vector2, distance: Int): Sequence = getTilesInDistanceRange(origin, 0..distance) /** @return All tiles in a hexagonal ring around [origin] with the distances in [range]. Excludes the [origin] tile unless [range] starts at 0. * Respects map edges and world wrap. */ + @Readonly fun getTilesInDistanceRange(origin: Vector2, range: IntRange): Sequence = range.asSequence().flatMap { getTilesAtDistance(origin, it) } /** @return All tiles in a hexagonal ring 1 tile wide around [origin] with the [distance]. Contains the [origin] if and only if [distance] is <= 0. * Respects map edges and world wrap. */ + @Readonly @Suppress("purity") fun getTilesAtDistance(origin: Vector2, distance: Int): Sequence = if (distance <= 0) // silently take negatives. sequenceOf(get(origin)) @@ -287,6 +296,7 @@ class TileMap(initialCapacity: Int = 10) : IsPartOfGameInfoSerialization { }.filterNotNull() /** @return tile at hex coordinates ([x],[y]) or null if they are outside the map. Respects map edges and world wrap. */ + @Readonly fun getIfTileExistsOrNull(x: Int, y: Int): Tile? { val tile = getOrNull(x, y) if (tile != null) return tile @@ -366,6 +376,7 @@ class TileMap(initialCapacity: Int = 10) : IsPartOfGameInfoSerialization { * Returns the closest position to (0, 0) outside the map which can be wrapped * to the position of the given vector */ + fun getUnWrappedPosition(position: Vector2): Vector2 { if (!contains(position)) return position //The position is outside the map so its unwrapped already diff --git a/core/src/com/unciv/models/translations/Translations.kt b/core/src/com/unciv/models/translations/Translations.kt index 6a09861d5c..a0acf3e480 100644 --- a/core/src/com/unciv/models/translations/Translations.kt +++ b/core/src/com/unciv/models/translations/Translations.kt @@ -531,7 +531,7 @@ fun String.fillPlaceholders(vararg strings: String): String { return filledString } -@Pure +@Pure @Suppress("purity") fun String.getModifiers(): List { if (!this.contains('<')) return emptyList() return pointyBraceRegex.findAll(this).map { Unique(it.groups[1]!!.value) }.toList()