From 888a06cd0b914f512e3b9bb2ae0ea60653e77456 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Wed, 16 Jul 2025 11:53:31 +0300 Subject: [PATCH] chore: Hexmath purity round 1 --- .gitignore | 1 + build.gradle.kts | 2 +- core/src/com/unciv/logic/map/HexMath.kt | 9 ++++++++- core/src/com/unciv/models/translations/Translations.kt | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2cf04306ce..e4098e294f 100644 --- a/.gitignore +++ b/.gitignore @@ -156,6 +156,7 @@ android/assets/scenarios/ SaveFiles/ /.kotlin/errors/* +/.kotlin/sessions/* # Visual Studio Code .vscode/ diff --git a/build.gradle.kts b/build.gradle.kts index 9548d3a0ec..5afd9ea7a4 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.22" apply(false) + id("io.github.yairm210.purity-plugin") version "0.0.23" apply(false) } allprojects { diff --git a/core/src/com/unciv/logic/map/HexMath.kt b/core/src/com/unciv/logic/map/HexMath.kt index 843ec07ca9..44e15dee10 100644 --- a/core/src/com/unciv/logic/map/HexMath.kt +++ b/core/src/com/unciv/logic/map/HexMath.kt @@ -16,10 +16,12 @@ import kotlin.math.sqrt @Suppress("MemberVisibilityCanBePrivate", "unused") // this is a library offering optional services object HexMath { + @Pure fun getVectorForAngle(angle: Float): Vector2 { return Vector2(sin(angle.toDouble()).toFloat(), cos(angle.toDouble()).toFloat()) } + @Pure private fun getVectorByClockHour(hour: Int): Vector2 { return getVectorForAngle((2 * Math.PI * (hour / 12f)).toFloat()) } @@ -28,12 +30,14 @@ object HexMath { * @param size Radius around origin tile * @return The number of tiles in a hexagonal map including origin tile */ + @Pure fun getNumberOfTilesInHexagon(size: Int): Int { if (size < 0) return 0 return 1 + 6 * size * (size + 1) / 2 } /** Almost inverse of [getNumberOfTilesInHexagon] - get equivalent fractional Hexagon radius for an Area */ + @Pure fun getHexagonalRadiusForArea(numberOfTiles: Int) = if (numberOfTiles < 1) 0f else ((sqrt(12f * numberOfTiles - 3) - 3) / 6) @@ -54,6 +58,7 @@ object HexMath { * @param longitude As from [getLongitude]. * @return Hex coordinate. May need to be passed through [roundHexCoords] for further use. * */ + @Pure fun hexFromLatLong(latitude: Float, longitude: Float): Vector2 { val y = (latitude - longitude) / 2f val x = longitude + y @@ -71,6 +76,7 @@ object HexMath { /** returns a vector containing width and height a rectangular map should have to have * approximately the same number of tiles as an hexagonal map given a height/width ratio */ + @Pure fun getEquivalentRectangularSize(size: Int, ratio: Float = 0.65f): Vector2 { if (size < 0) return Vector2.Zero @@ -83,9 +89,10 @@ object HexMath { /** Returns a radius of a hexagonal map that has approximately the same number of * tiles as a rectangular map of a given width/height */ + @Pure fun getEquivalentHexagonalRadius(width: Int, height: Int) = getHexagonalRadiusForArea(width * height).roundToInt() - + fun getAdjacentVectors(origin: Vector2): ArrayList { val vectors = arrayListOf( Vector2(1f, 0f), diff --git a/core/src/com/unciv/models/translations/Translations.kt b/core/src/com/unciv/models/translations/Translations.kt index d0023a4027..e8c622686c 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 @Suppress("purity") // calls .map{}, .toList() +@Pure @Suppress("purity") fun String.getModifiers(): List { if (!this.contains('<')) return emptyList() return pointyBraceRegex.findAll(this).map { Unique(it.groups[1]!!.value) }.toList()