chore: Hexmath purity round 1

This commit is contained in:
yairm210 2025-07-16 11:53:31 +03:00
parent 9c4e9f8877
commit 888a06cd0b
4 changed files with 11 additions and 3 deletions

1
.gitignore vendored
View File

@ -156,6 +156,7 @@ android/assets/scenarios/
SaveFiles/
/.kotlin/errors/*
/.kotlin/sessions/*
# Visual Studio Code
.vscode/

View File

@ -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 {

View File

@ -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<Vector2> {
val vectors = arrayListOf(
Vector2(1f, 0f),

View File

@ -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<Unique> {
if (!this.contains('<')) return emptyList()
return pointyBraceRegex.findAll(this).map { Unique(it.groups[1]!!.value) }.toList()