mirror of
https://github.com/yairm210/Unciv.git
synced 2025-09-22 10:54:19 -04:00
Simplify method TileMap.getTilesInRectangle
(#8786)
This commit is contained in:
parent
6f7279d1ec
commit
085c5d73e8
@ -205,29 +205,17 @@ class TileMap : IsPartOfGameInfoSerialization {
|
|||||||
}.filterNotNull()
|
}.filterNotNull()
|
||||||
|
|
||||||
/** @return all tiles within [rectangle], respecting world edges and wrap.
|
/** @return all tiles within [rectangle], respecting world edges and wrap.
|
||||||
* If using row/column coordinates the rectangle will be "straight" ie parallel with rectangular map edges. */
|
* The rectangle will be "straight" ie parallel with rectangular map edges. */
|
||||||
fun getTilesInRectangle(rectangle: Rectangle, rowsAndColumns: Boolean = false): Sequence<Tile> =
|
fun getTilesInRectangle(rectangle: Rectangle) = sequence {
|
||||||
if (rectangle.width <= 0 || rectangle.height <= 0) {
|
val x = rectangle.x.toInt()
|
||||||
val tile = getIfTileExistsOrNull(rectangle.x.toInt(), rectangle.y.toInt())
|
val y = rectangle.y.toInt()
|
||||||
if (tile == null) sequenceOf()
|
for (worldColumnNumber in x until x + rectangle.width.toInt()) {
|
||||||
else sequenceOf(tile)
|
for (worldRowNumber in y until y + rectangle.height.toInt()) {
|
||||||
|
val hexCoords = HexMath.getTileCoordsFromColumnRow(worldColumnNumber, worldRowNumber)
|
||||||
|
yield(getIfTileExistsOrNull(hexCoords.x.toInt(), hexCoords.y.toInt()))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
}.filterNotNull()
|
||||||
sequence {
|
|
||||||
for (rectColumnNumber in 0 until rectangle.width.toInt()) {
|
|
||||||
for (rectRowNumber in 0 until rectangle.height.toInt()) {
|
|
||||||
val worldColumnNumber = rectangle.x.toInt() + rectColumnNumber
|
|
||||||
val worldRowNumber = rectangle.y.toInt() + rectRowNumber
|
|
||||||
|
|
||||||
if (rowsAndColumns) {
|
|
||||||
val hexCoords = HexMath.getTileCoordsFromColumnRow(worldColumnNumber, worldRowNumber)
|
|
||||||
yield(getIfTileExistsOrNull(hexCoords.x.toInt(), hexCoords.y.toInt()))
|
|
||||||
}
|
|
||||||
else
|
|
||||||
yield(getIfTileExistsOrNull(worldColumnNumber, worldRowNumber))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.filterNotNull()
|
|
||||||
|
|
||||||
/** @return tile at hex coordinates ([x],[y]) or null if they are outside the map. Respects map edges and world wrap. */
|
/** @return tile at hex coordinates ([x],[y]) or null if they are outside the map. Respects map edges and world wrap. */
|
||||||
fun getIfTileExistsOrNull(x: Int, y: Int): Tile? {
|
fun getIfTileExistsOrNull(x: Int, y: Int): Tile? {
|
||||||
|
@ -173,13 +173,11 @@ class MapRegions (val ruleset: Ruleset){
|
|||||||
val nextRect = if (widerThanTall)
|
val nextRect = if (widerThanTall)
|
||||||
splitOffRegion.tileMap.getTilesInRectangle(Rectangle(
|
splitOffRegion.tileMap.getTilesInRectangle(Rectangle(
|
||||||
splitOffRegion.rect.x + splitPoint - 1, splitOffRegion.rect.y,
|
splitOffRegion.rect.x + splitPoint - 1, splitOffRegion.rect.y,
|
||||||
1f, splitOffRegion.rect.height),
|
1f, splitOffRegion.rect.height))
|
||||||
rowsAndColumns = true)
|
|
||||||
else
|
else
|
||||||
splitOffRegion.tileMap.getTilesInRectangle(Rectangle(
|
splitOffRegion.tileMap.getTilesInRectangle(Rectangle(
|
||||||
splitOffRegion.rect.x, splitOffRegion.rect.y + splitPoint - 1,
|
splitOffRegion.rect.x, splitOffRegion.rect.y + splitPoint - 1,
|
||||||
splitOffRegion.rect.width, 1f),
|
splitOffRegion.rect.width, 1f))
|
||||||
rowsAndColumns = true)
|
|
||||||
|
|
||||||
cumulativeFertility += if (splitOffRegion.continentID == -1)
|
cumulativeFertility += if (splitOffRegion.continentID == -1)
|
||||||
nextRect.sumOf { it.getTileFertility(false) }
|
nextRect.sumOf { it.getTileFertility(false) }
|
||||||
@ -408,7 +406,7 @@ class MapRegions (val ruleset: Ruleset){
|
|||||||
val fallbackTiles = HashSet<Vector2>()
|
val fallbackTiles = HashSet<Vector2>()
|
||||||
|
|
||||||
// First check center
|
// First check center
|
||||||
val centerTiles = region.tileMap.getTilesInRectangle(centerRect, rowsAndColumns = true)
|
val centerTiles = region.tileMap.getTilesInRectangle(centerRect)
|
||||||
for (tile in centerTiles) {
|
for (tile in centerTiles) {
|
||||||
if (tileData[tile.position]!!.isTwoFromCoast)
|
if (tileData[tile.position]!!.isTwoFromCoast)
|
||||||
continue // Don't even consider tiles two from coast
|
continue // Don't even consider tiles two from coast
|
||||||
@ -436,7 +434,7 @@ class MapRegions (val ruleset: Ruleset){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now check middle donut
|
// Now check middle donut
|
||||||
val middleDonut = region.tileMap.getTilesInRectangle(middleRect, rowsAndColumns = true).filterNot { it in centerTiles }
|
val middleDonut = region.tileMap.getTilesInRectangle(middleRect).filterNot { it in centerTiles }
|
||||||
riverTiles.clear()
|
riverTiles.clear()
|
||||||
wetTiles.clear()
|
wetTiles.clear()
|
||||||
dryTiles.clear()
|
dryTiles.clear()
|
||||||
@ -467,7 +465,7 @@ class MapRegions (val ruleset: Ruleset){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Now check the outer tiles. For these we don't care about rivers, coasts etc
|
// Now check the outer tiles. For these we don't care about rivers, coasts etc
|
||||||
val outerDonut = region.tileMap.getTilesInRectangle(region.rect, rowsAndColumns = true).filterNot { it in centerTiles || it in middleDonut}
|
val outerDonut = region.tileMap.getTilesInRectangle(region.rect).filterNot { it in centerTiles || it in middleDonut}
|
||||||
dryTiles.clear()
|
dryTiles.clear()
|
||||||
for (tile in outerDonut) {
|
for (tile in outerDonut) {
|
||||||
if (region.continentID != -1 && region.continentID != tile.getContinent())
|
if (region.continentID != -1 && region.continentID != tile.getContinent())
|
||||||
@ -1153,13 +1151,11 @@ class MapRegions (val ruleset: Ruleset){
|
|||||||
regionTargetNumber = max(1, regionTargetNumber)
|
regionTargetNumber = max(1, regionTargetNumber)
|
||||||
for (region in regions) {
|
for (region in regions) {
|
||||||
val resource = ruleset.tileResources[region.luxury] ?: continue
|
val resource = ruleset.tileResources[region.luxury] ?: continue
|
||||||
if (isWaterOnlyResource(resource))
|
fun Tile.isShoreOfContinent(continent: Int) = isWater && neighbors.any { it.getContinent() == continent }
|
||||||
tryAddingResourceToTiles(resource, regionTargetNumber,
|
val candidates = if (isWaterOnlyResource(resource))
|
||||||
tileMap.getTilesInRectangle(region.rect).filter { it.isWater && it.neighbors.any { neighbor -> neighbor.getContinent() == region.continentID } }.shuffled(),
|
tileMap.getTilesInRectangle(region.rect).filter { it.isShoreOfContinent(region.continentID) }
|
||||||
0.4f, true, 4, 2)
|
else region.tiles.asSequence()
|
||||||
else
|
tryAddingResourceToTiles(resource, regionTargetNumber, candidates.shuffled(), 0.4f, true, 4, 2)
|
||||||
tryAddingResourceToTiles(resource, regionTargetNumber, region.tiles.asSequence().shuffled(), 0.4f,
|
|
||||||
true, 4, 2)
|
|
||||||
}
|
}
|
||||||
// Fourth add random luxuries
|
// Fourth add random luxuries
|
||||||
if (randomLuxuries.isNotEmpty()) {
|
if (randomLuxuries.isNotEmpty()) {
|
||||||
@ -1693,13 +1689,12 @@ class Region (val tileMap: TileMap, val rect: Rectangle, val continentID: Int =
|
|||||||
val columnHasTile = HashSet<Int>()
|
val columnHasTile = HashSet<Int>()
|
||||||
|
|
||||||
tiles.clear()
|
tiles.clear()
|
||||||
for (tile in tileMap.getTilesInRectangle(rect, rowsAndColumns = true).filter {
|
for (tile in tileMap.getTilesInRectangle(rect).filter {
|
||||||
continentID == -1 || it.getContinent() == continentID } ) {
|
continentID == -1 || it.getContinent() == continentID } ) {
|
||||||
val fertility = tile.getTileFertility(continentID != -1)
|
val fertility = tile.getTileFertility(continentID != -1)
|
||||||
tiles.add(tile)
|
tiles.add(tile)
|
||||||
totalFertility += fertility
|
totalFertility += fertility
|
||||||
|
|
||||||
|
|
||||||
if (affectedByWorldWrap)
|
if (affectedByWorldWrap)
|
||||||
columnHasTile.add(tile.getColumn())
|
columnHasTile.add(tile.getColumn())
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user