Fix crash in Tactical Analysis Map (#8441)

* Fix crash due to out of looking at index == array length

* Fix dimension and swap x and y

* Some more cleanup

* Rewrite loop

* Even more cleanup

* Set function back to private

* Half total number of iterations needed
This commit is contained in:
OptimizedForDensity 2023-01-23 04:09:54 -05:00 committed by GitHub
parent 2167f65c1b
commit c5667d1685
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 40 deletions

View File

@ -296,45 +296,21 @@ class TacticalAnalysisMap {
for (zone in zones)
zone.neighboringZones.clear()
val tileMatrix = game.tileMap.tileMatrix
val tileMap = game.tileMap
val directionsToCheck = setOf(12, 4, 8) // each tile only needs to check 3 directions for every possible neighboringZone to be identified
val gridH = tileMatrix.size-1
val gridW = tileMatrix.size-1
for (y in 0 until gridH) {
for (x in 0 until gridW) {
val tileA = tileMatrix[y][x]
val tileB = tileMatrix[y+1][x]
val tileC = tileMatrix[y][x+1]
val tileD = tileMatrix[y+1][x+1]
val zoneA = if (tileA == null) getZoneById("UNKNOWN") else getZoneByTile(tileA)
val zoneB = if (tileB == null) getZoneById("UNKNOWN") else getZoneByTile(tileB)
val zoneC = if (tileC == null) getZoneById("UNKNOWN") else getZoneByTile(tileC)
val zoneD = if (tileD == null) getZoneById("UNKNOWN") else getZoneByTile(tileD)
val zoneAId = zoneA!!.id
val zoneBId = zoneB!!.id
val zoneCId = zoneC!!.id
val zoneDId = zoneD!!.id
if (zoneAId != "UNKNOWN" && zoneBId != "UNKNOWN" && zoneAId != zoneBId) {
for (tile in tileMap.values) {
val zoneA = getZoneByTile(tile)
val zoneAId = zoneA!!.id
for (neighbor in directionsToCheck.mapNotNull { tileMap.getClockPositionNeighborTile(tile, it) }) {
val zoneB = getZoneByTile(neighbor)!!
val zoneBId = zoneB.id
if (zoneAId != zoneBId) {
zoneA.neighboringZones.add(zoneB.id)
zoneB.neighboringZones.add(zoneA.id)
}
if (zoneAId != "UNKNOWN" && zoneCId != "UNKNOWN" && zoneAId != zoneCId) {
zoneA.neighboringZones.add(zoneC.id)
zoneC.neighboringZones.add(zoneA.id)
}
if (zoneAId != "UNKNOWN" && zoneDId != "UNKNOWN" && zoneAId != zoneDId) {
zoneA.neighboringZones.add(zoneD.id)
zoneD.neighboringZones.add(zoneA.id)
}
}
}
}
fun debugOutput() {

View File

@ -153,13 +153,8 @@ class TileMap : IsPartOfGameInfoSerialization {
tileMatrix[x - leftX][y - bottomY]!!
/** @return tile at hex coordinates ([x],[y]) or null if they are outside the map. Does *not* respect world wrap, use [getIfTileExistsOrNull] for that. */
private fun getOrNull (x: Int, y: Int): Tile? {
val arrayXIndex = x - leftX
if (arrayXIndex < 0 || arrayXIndex >= tileMatrix.size) return null
val arrayYIndex = y - bottomY
if (arrayYIndex < 0 || arrayYIndex >= tileMatrix[arrayXIndex].size) return null
return tileMatrix[arrayXIndex][arrayYIndex]
}
private fun getOrNull (x: Int, y: Int): Tile? =
tileMatrix.getOrNull(x - leftX)?.getOrNull(y - bottomY)
//endregion
//region Pure Functions