From 0ac87fa8c47bf14b42574d64e0c29d8c3e2a653b Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Wed, 8 Aug 2018 08:25:19 +0300 Subject: [PATCH] AI Performance improvement (added transient currentTile to unit) --- core/src/com/unciv/logic/map/MapUnit.kt | 7 ++++--- core/src/com/unciv/logic/map/TileMap.kt | 2 ++ .../com/unciv/ui/worldscreen/unit/UnitTable.kt | 18 ++++++------------ 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/core/src/com/unciv/logic/map/MapUnit.kt b/core/src/com/unciv/logic/map/MapUnit.kt index b7166da7f6..f33d4e388d 100644 --- a/core/src/com/unciv/logic/map/MapUnit.kt +++ b/core/src/com/unciv/logic/map/MapUnit.kt @@ -27,9 +27,9 @@ class MapUnit { fun getBaseUnit(): BaseUnit = GameBasics.Units[name]!! fun getMovementString(): String = DecimalFormat("0.#").format(currentMovement.toDouble()) + "/" + maxMovement - fun getTile(): TileInfo { - return civInfo.gameInfo.tileMap.values.first{it.militaryUnit==this || it.civilianUnit==this} - } + + @Transient private lateinit var currentTile :TileInfo + fun getTile(): TileInfo = currentTile fun getDistanceToTiles(): HashMap { val tile = getTile() @@ -187,6 +187,7 @@ class MapUnit { if(getBaseUnit().unitType== UnitType.Civilian) tile.civilianUnit=this else tile.militaryUnit=this + currentTile = tile } /** diff --git a/core/src/com/unciv/logic/map/TileMap.kt b/core/src/com/unciv/logic/map/TileMap.kt index 7ef0a52fe8..4edfb5b799 100644 --- a/core/src/com/unciv/logic/map/TileMap.kt +++ b/core/src/com/unciv/logic/map/TileMap.kt @@ -65,6 +65,8 @@ class TileMap { fun setTransients() { for (tileInfo in values){ tileInfo.tileMap = this + if(tileInfo.militaryUnit!=null) tileInfo.militaryUnit!!.currentTile = tileInfo + if(tileInfo.civilianUnit!=null) tileInfo.civilianUnit!!.currentTile = tileInfo } } diff --git a/core/src/com/unciv/ui/worldscreen/unit/UnitTable.kt b/core/src/com/unciv/ui/worldscreen/unit/UnitTable.kt index 9c88a29bb9..90dbf7c361 100644 --- a/core/src/com/unciv/ui/worldscreen/unit/UnitTable.kt +++ b/core/src/com/unciv/ui/worldscreen/unit/UnitTable.kt @@ -34,21 +34,15 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){ } fun update() { - if(selectedUnit!=null) - { - if(selectedUnit!!.civInfo != worldScreen.civInfo) { // The unit that was selected, was captured. It exists but is no longer ours. + if(selectedUnit!=null) { + if (selectedUnit!!.civInfo != worldScreen.civInfo) { // The unit that was selected, was captured. It exists but is no longer ours. + selectedUnit = null + currentlyExecutingAction = null + selectedUnitHasChanged = true + } else if (selectedUnit!! !in selectedUnit!!.getTile().getUnits()) { // The unit that was there no longer exists} selectedUnit = null currentlyExecutingAction = null selectedUnitHasChanged = true - } - else { - try { - selectedUnit!!.getTile() - } catch (ex: Exception) { // The unit that was there no longer exists} - selectedUnit = null - currentlyExecutingAction = null - selectedUnitHasChanged=true - } } }