From e390a4f579411f953c19806ee5256ef569aaab45 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sun, 5 Feb 2023 00:29:25 +0200 Subject: [PATCH] chore: Simplified hex coord math - part 1 --- core/src/com/unciv/logic/map/HexMath.kt | 16 +++---- core/src/com/unciv/logic/map/TileMap.kt | 6 +-- .../logic/map/mapgenerator/MapRegions.kt | 43 ++++++++++--------- core/src/com/unciv/logic/map/tile/Tile.kt | 3 ++ 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/core/src/com/unciv/logic/map/HexMath.kt b/core/src/com/unciv/logic/map/HexMath.kt index 4358c6c389..31588793dd 100644 --- a/core/src/com/unciv/logic/map/HexMath.kt +++ b/core/src/com/unciv/logic/map/HexMath.kt @@ -120,6 +120,12 @@ object HexMath { return Vector2(x, y) } + // Both x - 10 o'clock - and y - 2 o'clock - increase the row by 1 + fun getRow(hexCoord: Vector2): Int = (hexCoord.x + hexCoord.y).toInt() + + // y is 2 o'clock - increases column by 1, x in 10 o'clock - decreases by 1 + fun getColumn(hexCoord: Vector2): Int = (hexCoord.y - hexCoord.x).toInt() + fun hex2CubicCoords(hexCoord: Vector2): Vector3 { return Vector3(hexCoord.y - hexCoord.x, hexCoord.x, -hexCoord.y) } @@ -128,9 +134,6 @@ object HexMath { return Vector2(cubicCoord.y, -cubicCoord.z) } - fun cubic2EvenQCoords(cubicCoord: Vector3): Vector2 { - return Vector2(cubicCoord.x, cubicCoord.z + (cubicCoord.x + (cubicCoord.x.toInt() and 1)) / 2) - } fun evenQ2CubicCoords(evenQCoord: Vector2): Vector3 { val x = evenQCoord.x @@ -146,13 +149,6 @@ object HexMath { cubic2HexCoords(evenQ2CubicCoords(evenQCoord)) } - fun hex2EvenQCoords(hexCoord: Vector2): Vector2 { - return if (hexCoord == Vector2.Zero) - Vector2.Zero - else - cubic2EvenQCoords(hex2CubicCoords(hexCoord)) - } - fun roundCubicCoords(cubicCoords: Vector3): Vector3 { var rx = round(cubicCoords.x) var ry = round(cubicCoords.y) diff --git a/core/src/com/unciv/logic/map/TileMap.kt b/core/src/com/unciv/logic/map/TileMap.kt index 4019e66858..7727c78769 100644 --- a/core/src/com/unciv/logic/map/TileMap.kt +++ b/core/src/com/unciv/logic/map/TileMap.kt @@ -115,10 +115,10 @@ class TileMap : IsPartOfGameInfoSerialization { // Even widths will have coordinates ranging -x..(x-1), not -x..x, which is always an odd-sized range // e.g. w=4 -> -2..1, w=5 -> -2..2, w=6 -> -3..2, w=7 -> -3..3 - for (x in -wrapAdjustedWidth / 2 .. (wrapAdjustedWidth-1) / 2) - for (y in -height / 2 .. (height-1) / 2) + for (row in -wrapAdjustedWidth / 2 .. (wrapAdjustedWidth-1) / 2) + for (column in -height / 2 .. (height-1) / 2) tileList.add(Tile().apply { - position = HexMath.evenQ2HexCoords(Vector2(x.toFloat(), y.toFloat())) + position = HexMath.evenQ2HexCoords(Vector2(row.toFloat(), column.toFloat())) baseTerrain = firstAvailableLandTerrain }) diff --git a/core/src/com/unciv/logic/map/mapgenerator/MapRegions.kt b/core/src/com/unciv/logic/map/mapgenerator/MapRegions.kt index 2a7e040316..ef94685ef0 100644 --- a/core/src/com/unciv/logic/map/mapgenerator/MapRegions.kt +++ b/core/src/com/unciv/logic/map/mapgenerator/MapRegions.kt @@ -4,7 +4,6 @@ import com.badlogic.gdx.math.Rectangle import com.badlogic.gdx.math.Vector2 import com.unciv.Constants import com.unciv.logic.civilization.Civilization -import com.unciv.logic.map.HexMath import com.unciv.logic.map.MapResources import com.unciv.logic.map.MapShape import com.unciv.logic.map.TileMap @@ -99,7 +98,7 @@ class MapRegions (val ruleset: Ruleset){ val civsAddedToContinent = HashMap() // Continent ID, civs added val continentFertility = HashMap() // Continent ID, total fertility // Keep track of the even-q columns each continent is at, to figure out if they wrap - val continentIsAtCol = HashMap>() + val continentToColumnsItsIn = HashMap>() // Calculate continent fertilities and columns for (tile in tileMap.values) { @@ -108,9 +107,10 @@ class MapRegions (val ruleset: Ruleset){ continentFertility[continent] = tile.getTileFertility(true) + (continentFertility[continent] ?: 0) - if (continentIsAtCol[continent] == null) - continentIsAtCol[continent] = HashSet() - continentIsAtCol[continent]!!.add(HexMath.hex2EvenQCoords(tile.position).x.toInt()) + if (continentToColumnsItsIn[continent] == null) + continentToColumnsItsIn[continent] = HashSet() + + continentToColumnsItsIn[continent]!!.add(tile.getColumn()) } } @@ -124,7 +124,7 @@ class MapRegions (val ruleset: Ruleset){ // Split up the continents for (continent in civsAddedToContinent.keys) { val continentRegion = Region(tileMap, Rectangle(mapRect), continent) - val cols = continentIsAtCol[continent]!! + val cols = continentToColumnsItsIn[continent]!! // Set origin at the rightmost column which does not have a neighbor on the left continentRegion.rect.x = cols.filter { !cols.contains(it - 1) }.maxOf { it }.toFloat() continentRegion.rect.width = cols.size.toFloat() @@ -1685,10 +1685,10 @@ class Region (val tileMap: TileMap, val rect: Rectangle, val continentID: Int = /** Recalculates tiles and fertility */ fun updateTiles(trim: Boolean = true) { totalFertility = 0 - var minX = 99999f - var maxX = -99999f - var minY = 99999f - var maxY = -99999f + var minColumn = 99999f + var maxColumn = -99999f + var minRow = 99999f + var maxRow = -99999f val columnHasTile = HashSet() @@ -1701,14 +1701,15 @@ class Region (val tileMap: TileMap, val rect: Rectangle, val continentID: Int = if (affectedByWorldWrap) - columnHasTile.add(HexMath.hex2EvenQCoords(tile.position).x.toInt()) + columnHasTile.add(tile.getColumn()) if (trim) { - val evenQCoords = HexMath.hex2EvenQCoords(tile.position) - minX = min(minX, evenQCoords.x) - maxX = max(maxX, evenQCoords.x) - minY = min(minY, evenQCoords.y) - maxY = max(maxY, evenQCoords.y) + val row = tile.getRow().toFloat() + val column = tile.getColumn().toFloat() + minColumn = min(minColumn, column) + maxColumn = max(maxColumn, column) + minRow = min(minRow, row) + maxRow = max(maxRow, row) } } @@ -1716,13 +1717,13 @@ class Region (val tileMap: TileMap, val rect: Rectangle, val continentID: Int = if (affectedByWorldWrap) // Need to be more thorough with origin longitude rect.x = columnHasTile.filter { !columnHasTile.contains(it - 1) }.maxOf { it }.toFloat() else - rect.x = minX // ez way for non-wrapping regions - rect.y = minY - rect.height = maxY - minY + 1 - if (affectedByWorldWrap && minX < rect.x) { // Thorough way + rect.x = minColumn // ez way for non-wrapping regions + rect.y = minRow + rect.height = maxRow - minRow + 1 + if (affectedByWorldWrap && minColumn < rect.x) { // Thorough way rect.width = columnHasTile.size.toFloat() } else { - rect.width = maxX - minX + 1 // ez way + rect.width = maxColumn - minColumn + 1 // ez way affectedByWorldWrap = false // also we're not wrapping anymore } } diff --git a/core/src/com/unciv/logic/map/tile/Tile.kt b/core/src/com/unciv/logic/map/tile/Tile.kt index a33499f357..1b9630ffc1 100644 --- a/core/src/com/unciv/logic/map/tile/Tile.kt +++ b/core/src/com/unciv/logic/map/tile/Tile.kt @@ -356,6 +356,9 @@ open class Tile : IsPartOfGameInfoSerialization { return tileMap.getClockPositionNeighborTile(this,(tileMap.getNeighborTileClockPosition(this, neighbor) + 2) % 12) } + fun getRow() = HexMath.getRow(position) + fun getColumn() = HexMath.getColumn(position) + @delegate:Transient val tileHeight : Int by lazy { // for e.g. hill+forest this is 2, since forest is visible above units if (terrainHasUnique(UniqueType.BlocksLineOfSightAtSameElevation)) unitHeight + 1