mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-23 11:34:54 -04:00
chore: Hexmath purity round 1
This commit is contained in:
parent
9c4e9f8877
commit
888a06cd0b
1
.gitignore
vendored
1
.gitignore
vendored
@ -156,6 +156,7 @@ android/assets/scenarios/
|
|||||||
SaveFiles/
|
SaveFiles/
|
||||||
|
|
||||||
/.kotlin/errors/*
|
/.kotlin/errors/*
|
||||||
|
/.kotlin/sessions/*
|
||||||
|
|
||||||
# Visual Studio Code
|
# Visual Studio Code
|
||||||
.vscode/
|
.vscode/
|
||||||
|
@ -37,7 +37,7 @@ plugins {
|
|||||||
// This is *with* gradle 8.2 downloaded according the project specs, no idea what that's about
|
// This is *with* gradle 8.2 downloaded according the project specs, no idea what that's about
|
||||||
kotlin("multiplatform") version "1.9.24"
|
kotlin("multiplatform") version "1.9.24"
|
||||||
kotlin("plugin.serialization") 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 {
|
allprojects {
|
||||||
|
@ -16,10 +16,12 @@ import kotlin.math.sqrt
|
|||||||
@Suppress("MemberVisibilityCanBePrivate", "unused") // this is a library offering optional services
|
@Suppress("MemberVisibilityCanBePrivate", "unused") // this is a library offering optional services
|
||||||
object HexMath {
|
object HexMath {
|
||||||
|
|
||||||
|
@Pure
|
||||||
fun getVectorForAngle(angle: Float): Vector2 {
|
fun getVectorForAngle(angle: Float): Vector2 {
|
||||||
return Vector2(sin(angle.toDouble()).toFloat(), cos(angle.toDouble()).toFloat())
|
return Vector2(sin(angle.toDouble()).toFloat(), cos(angle.toDouble()).toFloat())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Pure
|
||||||
private fun getVectorByClockHour(hour: Int): Vector2 {
|
private fun getVectorByClockHour(hour: Int): Vector2 {
|
||||||
return getVectorForAngle((2 * Math.PI * (hour / 12f)).toFloat())
|
return getVectorForAngle((2 * Math.PI * (hour / 12f)).toFloat())
|
||||||
}
|
}
|
||||||
@ -28,12 +30,14 @@ object HexMath {
|
|||||||
* @param size Radius around origin tile
|
* @param size Radius around origin tile
|
||||||
* @return The number of tiles in a hexagonal map including origin tile
|
* @return The number of tiles in a hexagonal map including origin tile
|
||||||
*/
|
*/
|
||||||
|
@Pure
|
||||||
fun getNumberOfTilesInHexagon(size: Int): Int {
|
fun getNumberOfTilesInHexagon(size: Int): Int {
|
||||||
if (size < 0) return 0
|
if (size < 0) return 0
|
||||||
return 1 + 6 * size * (size + 1) / 2
|
return 1 + 6 * size * (size + 1) / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Almost inverse of [getNumberOfTilesInHexagon] - get equivalent fractional Hexagon radius for an Area */
|
/** Almost inverse of [getNumberOfTilesInHexagon] - get equivalent fractional Hexagon radius for an Area */
|
||||||
|
@Pure
|
||||||
fun getHexagonalRadiusForArea(numberOfTiles: Int) =
|
fun getHexagonalRadiusForArea(numberOfTiles: Int) =
|
||||||
if (numberOfTiles < 1) 0f else ((sqrt(12f * numberOfTiles - 3) - 3) / 6)
|
if (numberOfTiles < 1) 0f else ((sqrt(12f * numberOfTiles - 3) - 3) / 6)
|
||||||
|
|
||||||
@ -54,6 +58,7 @@ object HexMath {
|
|||||||
* @param longitude As from [getLongitude].
|
* @param longitude As from [getLongitude].
|
||||||
* @return Hex coordinate. May need to be passed through [roundHexCoords] for further use.
|
* @return Hex coordinate. May need to be passed through [roundHexCoords] for further use.
|
||||||
* */
|
* */
|
||||||
|
@Pure
|
||||||
fun hexFromLatLong(latitude: Float, longitude: Float): Vector2 {
|
fun hexFromLatLong(latitude: Float, longitude: Float): Vector2 {
|
||||||
val y = (latitude - longitude) / 2f
|
val y = (latitude - longitude) / 2f
|
||||||
val x = longitude + y
|
val x = longitude + y
|
||||||
@ -71,6 +76,7 @@ object HexMath {
|
|||||||
|
|
||||||
/** returns a vector containing width and height a rectangular map should have to have
|
/** 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 */
|
* 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 {
|
fun getEquivalentRectangularSize(size: Int, ratio: Float = 0.65f): Vector2 {
|
||||||
if (size < 0)
|
if (size < 0)
|
||||||
return Vector2.Zero
|
return Vector2.Zero
|
||||||
@ -83,9 +89,10 @@ object HexMath {
|
|||||||
|
|
||||||
/** Returns a radius of a hexagonal map that has approximately the same number of
|
/** Returns a radius of a hexagonal map that has approximately the same number of
|
||||||
* tiles as a rectangular map of a given width/height */
|
* tiles as a rectangular map of a given width/height */
|
||||||
|
@Pure
|
||||||
fun getEquivalentHexagonalRadius(width: Int, height: Int) =
|
fun getEquivalentHexagonalRadius(width: Int, height: Int) =
|
||||||
getHexagonalRadiusForArea(width * height).roundToInt()
|
getHexagonalRadiusForArea(width * height).roundToInt()
|
||||||
|
|
||||||
fun getAdjacentVectors(origin: Vector2): ArrayList<Vector2> {
|
fun getAdjacentVectors(origin: Vector2): ArrayList<Vector2> {
|
||||||
val vectors = arrayListOf(
|
val vectors = arrayListOf(
|
||||||
Vector2(1f, 0f),
|
Vector2(1f, 0f),
|
||||||
|
@ -531,7 +531,7 @@ fun String.fillPlaceholders(vararg strings: String): String {
|
|||||||
return filledString
|
return filledString
|
||||||
}
|
}
|
||||||
|
|
||||||
@Pure @Suppress("purity") // calls .map{}, .toList()
|
@Pure @Suppress("purity")
|
||||||
fun String.getModifiers(): List<Unique> {
|
fun String.getModifiers(): List<Unique> {
|
||||||
if (!this.contains('<')) return emptyList()
|
if (!this.contains('<')) return emptyList()
|
||||||
return pointyBraceRegex.findAll(this).map { Unique(it.groups[1]!!.value) }.toList()
|
return pointyBraceRegex.findAll(this).map { Unique(it.groups[1]!!.value) }.toList()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user