mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-22 10:54:19 -04: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)
|
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 {
|
fun hex2CubicCoords(hexCoord: Vector2): Vector3 {
|
||||||
return Vector3(hexCoord.y - hexCoord.x, hexCoord.x, -hexCoord.y)
|
return Vector3(hexCoord.y - hexCoord.x, hexCoord.x, -hexCoord.y)
|
||||||
}
|
}
|
||||||
@ -128,9 +134,6 @@ object HexMath {
|
|||||||
return Vector2(cubicCoord.y, -cubicCoord.z)
|
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 {
|
fun evenQ2CubicCoords(evenQCoord: Vector2): Vector3 {
|
||||||
val x = evenQCoord.x
|
val x = evenQCoord.x
|
||||||
@ -146,13 +149,6 @@ object HexMath {
|
|||||||
cubic2HexCoords(evenQ2CubicCoords(evenQCoord))
|
cubic2HexCoords(evenQ2CubicCoords(evenQCoord))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun hex2EvenQCoords(hexCoord: Vector2): Vector2 {
|
|
||||||
return if (hexCoord == Vector2.Zero)
|
|
||||||
Vector2.Zero
|
|
||||||
else
|
|
||||||
cubic2EvenQCoords(hex2CubicCoords(hexCoord))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun roundCubicCoords(cubicCoords: Vector3): Vector3 {
|
fun roundCubicCoords(cubicCoords: Vector3): Vector3 {
|
||||||
var rx = round(cubicCoords.x)
|
var rx = round(cubicCoords.x)
|
||||||
var ry = round(cubicCoords.y)
|
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
|
// 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
|
// 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 (row in -wrapAdjustedWidth / 2 .. (wrapAdjustedWidth-1) / 2)
|
||||||
for (y in -height / 2 .. (height-1) / 2)
|
for (column in -height / 2 .. (height-1) / 2)
|
||||||
tileList.add(Tile().apply {
|
tileList.add(Tile().apply {
|
||||||
position = HexMath.evenQ2HexCoords(Vector2(x.toFloat(), y.toFloat()))
|
position = HexMath.evenQ2HexCoords(Vector2(row.toFloat(), column.toFloat()))
|
||||||
baseTerrain = firstAvailableLandTerrain
|
baseTerrain = firstAvailableLandTerrain
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import com.badlogic.gdx.math.Rectangle
|
|||||||
import com.badlogic.gdx.math.Vector2
|
import com.badlogic.gdx.math.Vector2
|
||||||
import com.unciv.Constants
|
import com.unciv.Constants
|
||||||
import com.unciv.logic.civilization.Civilization
|
import com.unciv.logic.civilization.Civilization
|
||||||
import com.unciv.logic.map.HexMath
|
|
||||||
import com.unciv.logic.map.MapResources
|
import com.unciv.logic.map.MapResources
|
||||||
import com.unciv.logic.map.MapShape
|
import com.unciv.logic.map.MapShape
|
||||||
import com.unciv.logic.map.TileMap
|
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 civsAddedToContinent = HashMap<Int, Int>() // Continent ID, civs added
|
||||||
val continentFertility = HashMap<Int, Int>() // Continent ID, total fertility
|
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
|
// 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
|
// Calculate continent fertilities and columns
|
||||||
for (tile in tileMap.values) {
|
for (tile in tileMap.values) {
|
||||||
@ -108,9 +107,10 @@ class MapRegions (val ruleset: Ruleset){
|
|||||||
continentFertility[continent] = tile.getTileFertility(true) +
|
continentFertility[continent] = tile.getTileFertility(true) +
|
||||||
(continentFertility[continent] ?: 0)
|
(continentFertility[continent] ?: 0)
|
||||||
|
|
||||||
if (continentIsAtCol[continent] == null)
|
if (continentToColumnsItsIn[continent] == null)
|
||||||
continentIsAtCol[continent] = HashSet()
|
continentToColumnsItsIn[continent] = HashSet()
|
||||||
continentIsAtCol[continent]!!.add(HexMath.hex2EvenQCoords(tile.position).x.toInt())
|
|
||||||
|
continentToColumnsItsIn[continent]!!.add(tile.getColumn())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ class MapRegions (val ruleset: Ruleset){
|
|||||||
// Split up the continents
|
// Split up the continents
|
||||||
for (continent in civsAddedToContinent.keys) {
|
for (continent in civsAddedToContinent.keys) {
|
||||||
val continentRegion = Region(tileMap, Rectangle(mapRect), continent)
|
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
|
// 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.x = cols.filter { !cols.contains(it - 1) }.maxOf { it }.toFloat()
|
||||||
continentRegion.rect.width = cols.size.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 */
|
/** Recalculates tiles and fertility */
|
||||||
fun updateTiles(trim: Boolean = true) {
|
fun updateTiles(trim: Boolean = true) {
|
||||||
totalFertility = 0
|
totalFertility = 0
|
||||||
var minX = 99999f
|
var minColumn = 99999f
|
||||||
var maxX = -99999f
|
var maxColumn = -99999f
|
||||||
var minY = 99999f
|
var minRow = 99999f
|
||||||
var maxY = -99999f
|
var maxRow = -99999f
|
||||||
|
|
||||||
val columnHasTile = HashSet<Int>()
|
val columnHasTile = HashSet<Int>()
|
||||||
|
|
||||||
@ -1701,14 +1701,15 @@ class Region (val tileMap: TileMap, val rect: Rectangle, val continentID: Int =
|
|||||||
|
|
||||||
|
|
||||||
if (affectedByWorldWrap)
|
if (affectedByWorldWrap)
|
||||||
columnHasTile.add(HexMath.hex2EvenQCoords(tile.position).x.toInt())
|
columnHasTile.add(tile.getColumn())
|
||||||
|
|
||||||
if (trim) {
|
if (trim) {
|
||||||
val evenQCoords = HexMath.hex2EvenQCoords(tile.position)
|
val row = tile.getRow().toFloat()
|
||||||
minX = min(minX, evenQCoords.x)
|
val column = tile.getColumn().toFloat()
|
||||||
maxX = max(maxX, evenQCoords.x)
|
minColumn = min(minColumn, column)
|
||||||
minY = min(minY, evenQCoords.y)
|
maxColumn = max(maxColumn, column)
|
||||||
maxY = max(maxY, evenQCoords.y)
|
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
|
if (affectedByWorldWrap) // Need to be more thorough with origin longitude
|
||||||
rect.x = columnHasTile.filter { !columnHasTile.contains(it - 1) }.maxOf { it }.toFloat()
|
rect.x = columnHasTile.filter { !columnHasTile.contains(it - 1) }.maxOf { it }.toFloat()
|
||||||
else
|
else
|
||||||
rect.x = minX // ez way for non-wrapping regions
|
rect.x = minColumn // ez way for non-wrapping regions
|
||||||
rect.y = minY
|
rect.y = minRow
|
||||||
rect.height = maxY - minY + 1
|
rect.height = maxRow - minRow + 1
|
||||||
if (affectedByWorldWrap && minX < rect.x) { // Thorough way
|
if (affectedByWorldWrap && minColumn < rect.x) { // Thorough way
|
||||||
rect.width = columnHasTile.size.toFloat()
|
rect.width = columnHasTile.size.toFloat()
|
||||||
} else {
|
} else {
|
||||||
rect.width = maxX - minX + 1 // ez way
|
rect.width = maxColumn - minColumn + 1 // ez way
|
||||||
affectedByWorldWrap = false // also we're not wrapping anymore
|
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)
|
return tileMap.getClockPositionNeighborTile(this,(tileMap.getNeighborTileClockPosition(this, neighbor) + 2) % 12)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getRow() = HexMath.getRow(position)
|
||||||
|
fun getColumn() = HexMath.getColumn(position)
|
||||||
|
|
||||||
@delegate:Transient
|
@delegate:Transient
|
||||||
val tileHeight : Int by lazy { // for e.g. hill+forest this is 2, since forest is visible above units
|
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
|
if (terrainHasUnique(UniqueType.BlocksLineOfSightAtSameElevation)) unitHeight + 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user