mirror of
https://github.com/yairm210/Unciv.git
synced 2025-03-03 22:22:51 +07:00
chore: Simplified hex coord math - part 1
This commit is contained in:
parent
78bd9693a1
commit
e390a4f579
@ -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)
|
||||
|
@ -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
|
||||
})
|
||||
|
||||
|
@ -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<Int, Int>() // Continent ID, civs added
|
||||
val continentFertility = HashMap<Int, Int>() // Continent ID, total fertility
|
||||
// Keep track of the even-q columns each continent is at, to figure out if they wrap
|
||||
val continentIsAtCol = HashMap<Int, HashSet<Int>>()
|
||||
val continentToColumnsItsIn = HashMap<Int, HashSet<Int>>()
|
||||
|
||||
// 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<Int>()
|
||||
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user