From 6cd78ca9264d75257b94f7dd02d495d653b97a81 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Tue, 1 May 2018 21:42:13 +0300 Subject: [PATCH] Minor performance enhancements for unit movement --- core/src/com/unciv/logic/WorkerAutomation.kt | 16 ++++++++++++---- .../unciv/logic/map/UnitMovementAlgorithms.kt | 4 ++++ core/src/com/unciv/ui/utils/ImageGetter.kt | 8 ++++++-- .../com/unciv/ui/worldscreen/unit/UnitActions.kt | 3 --- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/core/src/com/unciv/logic/WorkerAutomation.kt b/core/src/com/unciv/logic/WorkerAutomation.kt index 85d5985669..18665048b0 100644 --- a/core/src/com/unciv/logic/WorkerAutomation.kt +++ b/core/src/com/unciv/logic/WorkerAutomation.kt @@ -32,10 +32,18 @@ public class WorkerAutomation(){ && it.improvement == null && it.canBuildImprovement(chooseImprovement(it), civInfo) && {val city=it.getCity(); city==null || it.getCity()?.civInfo == civInfo}() // don't work tiles belonging to another civ - && UnitMovementAlgorithms(currentTile.tileMap) // the tile is actually reachable - more difficult than it seems! - .getShortestPath(currentTile.position, it.position, 2f, 2, civInfo).isNotEmpty() - } - val selectedTile = workableTiles.maxBy { getPriority(it, civInfo) } + }.sortedByDescending { getPriority(it, civInfo) }.toMutableList() + + // the tile needs to be actually reachable - more difficult than it seems, + // which is why we DON'T calculate this for every possible tile in the radius, + // but only for the tile that's about to be chosen. + while (workableTiles.isNotEmpty() + && UnitMovementAlgorithms(currentTile.tileMap) + .getShortestPath(currentTile.position, workableTiles.first().position,2f, 2, civInfo) + .isEmpty()) + workableTiles.removeAt(0) + + val selectedTile = workableTiles.firstOrNull() if (selectedTile != null && getPriority(selectedTile, civInfo)>1 && (!workableTiles.contains(currentTile) diff --git a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt index 844b21bcad..f49039e9dc 100644 --- a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt +++ b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt @@ -81,7 +81,11 @@ class UnitMovementAlgorithms(val tileMap: TileMap){ if(newTilesToCheck.isEmpty()) return emptyList() // there is NO PATH (eg blocked by enemy units) + // no need to check tiles that are surrounded by reachable tiles, only need to check the edgemost tiles. + // Because anything we can reach from intermediate tiles, can be more easily reached by the edgemost tiles, + // since we'll have to pass through an edgemost tile in order to reach the diestination anyway tilesToCheck = newTilesToCheck.filterNot {tile -> tile.neighbors.all{newTilesToCheck.contains(it) || tilesToCheck.contains(it) } } + distance++ } } diff --git a/core/src/com/unciv/ui/utils/ImageGetter.kt b/core/src/com/unciv/ui/utils/ImageGetter.kt index 1243f5ce49..f65464a916 100644 --- a/core/src/com/unciv/ui/utils/ImageGetter.kt +++ b/core/src/com/unciv/ui/utils/ImageGetter.kt @@ -24,8 +24,12 @@ object ImageGetter { private fun getTextureRegion(fileName: String): TextureRegion { try { - if (!textureRegionByFileName.containsKey(fileName)) - textureRegionByFileName[fileName] = TextureRegion(Texture(Gdx.files.internal(fileName))) + if (!textureRegionByFileName.containsKey(fileName)) { + val texture = Texture(Gdx.files.internal(fileName)) + texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear) + textureRegionByFileName[fileName] = TextureRegion(texture) + } + } catch (ex: Exception) { print("File $fileName not found!") throw ex diff --git a/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt b/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt index 3ad26290b9..d2bc91d515 100644 --- a/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt +++ b/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt @@ -51,7 +51,6 @@ class UnitActions { UnitAction("Stop movement", { unitTable.currentlyExecutingAction = null unit.action=null - tileMapHolder.updateTiles() },unit.currentMovement != 0f) } @@ -63,7 +62,6 @@ class UnitActions { unit.civInfo.addCity(tile.position) unitTable.currentlyExecutingAction = null // In case the settler was in the middle of doing something and we then founded a city with it tile.unit = null // Remove settler! - worldScreen.update() }, unit.currentMovement != 0f && !tile.getTilesInDistance(2).any { it.isCityCenter() }) @@ -109,7 +107,6 @@ class UnitActions { { unit.civInfo.goldenAges.enterGoldenAge() tile.unit = null// destroy! - worldScreen.update() },unit.currentMovement != 0f ) actionList += UnitAction("Construct Landmark",