From af88d287c3ac662e8cbf8dd5df5d58e896b56beb Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sat, 17 Nov 2018 21:57:52 +0200 Subject: [PATCH] Dev: added "type" property to MapUnit to reflect baseUnit.unitType, since that's used EVERYWHERE --- .../com/unciv/logic/automation/Automation.kt | 2 +- .../logic/automation/NextTurnAutomation.kt | 4 ++-- .../unciv/logic/automation/UnitAutomation.kt | 10 ++++---- .../com/unciv/logic/battle/BattleDamage.kt | 2 +- .../unciv/logic/battle/MapUnitCombatant.kt | 2 +- .../logic/civilization/CivilizationInfo.kt | 2 +- core/src/com/unciv/logic/map/MapUnit.kt | 24 +++++++++++-------- .../src/com/unciv/logic/map/UnitPromotions.kt | 2 +- .../ui/pickerscreens/PromotionPickerScreen.kt | 2 +- .../com/unciv/ui/tilegroups/WorldTileGroup.kt | 2 +- .../com/unciv/ui/worldscreen/TileMapHolder.kt | 6 ++--- .../com/unciv/ui/worldscreen/WorldScreen.kt | 2 +- .../ui/worldscreen/bottombar/BattleTable.kt | 2 +- .../unciv/ui/worldscreen/unit/UnitActions.kt | 6 ++--- .../unciv/ui/worldscreen/unit/UnitTable.kt | 4 ++-- 15 files changed, 38 insertions(+), 34 deletions(-) diff --git a/core/src/com/unciv/logic/automation/Automation.kt b/core/src/com/unciv/logic/automation/Automation.kt index f8480e54..bbd45b6e 100644 --- a/core/src/com/unciv/logic/automation/Automation.kt +++ b/core/src/com/unciv/logic/automation/Automation.kt @@ -54,7 +54,7 @@ class Automation { val buildableWonders = getBuildableBuildings().filter { it.isWonder } val civUnits = cityInfo.civInfo.getCivUnits() - val militaryUnits = civUnits.filter { !it.baseUnit().unitType.isCivilian()}.size + val militaryUnits = civUnits.filter { !it.type.isCivilian()}.size val workers = civUnits.filter { it.name == CityConstructions.Worker }.size val cities = cityInfo.civInfo.cities.size diff --git a/core/src/com/unciv/logic/automation/NextTurnAutomation.kt b/core/src/com/unciv/logic/automation/NextTurnAutomation.kt index d2996b0b..63c909ad 100644 --- a/core/src/com/unciv/logic/automation/NextTurnAutomation.kt +++ b/core/src/com/unciv/logic/automation/NextTurnAutomation.kt @@ -70,7 +70,7 @@ class NextTurnAutomation(){ private fun declareWar(civInfo: CivilizationInfo) { if (civInfo.cities.isNotEmpty() && civInfo.diplomacy.isNotEmpty() && !civInfo.isAtWar() - && civInfo.getCivUnits().filter { !it.baseUnit.unitType.isCivilian() }.size > civInfo.cities.size * 2) { + && civInfo.getCivUnits().filter { !it.type.isCivilian() }.size > civInfo.cities.size * 2) { val enemyCivsByDistanceToOurs = civInfo.diplomacy.values.map { it.otherCiv() } .filterNot { it == civInfo || it.cities.isEmpty() || !civInfo.diplomacy[it.civName]!!.canDeclareWar() } @@ -101,7 +101,7 @@ class NextTurnAutomation(){ unit.promotions.addPromotion(availablePromotions.getRandom().name) } - val unitType = unit.baseUnit().unitType + val unitType = unit.type if (unitType.isRanged()) rangedUnits.add(unit) else if (unitType.isMelee()) meleeUnits.add(unit) else civilianUnits.add(unit) diff --git a/core/src/com/unciv/logic/automation/UnitAutomation.kt b/core/src/com/unciv/logic/automation/UnitAutomation.kt index 3b66e264..a3cfb74a 100644 --- a/core/src/com/unciv/logic/automation/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/UnitAutomation.kt @@ -127,7 +127,7 @@ class UnitAutomation{ fun containsAttackableEnemy(tile: TileInfo, unit: MapUnit): Boolean { if(unit.isEmbarked()){ - if(unit.baseUnit.unitType.isRanged()) return false + if(unit.type.isRanged()) return false if(tile.isWater()) return false // can't attack water units while embarked, only land } val tileCombatant = Battle(unit.civInfo.gameInfo).getMapCombatantOfTile(tile) @@ -167,7 +167,7 @@ class UnitAutomation{ // this can be sped up if we check each layer separately var closeEnemies = unit.getTile().getTilesInDistance(5) .filter{ containsAttackableEnemy(it, unit) && unit.movementAlgs().canReach(it)} - if(unit.baseUnit().unitType.isRanged()) + if(unit.type.isRanged()) closeEnemies = closeEnemies.filterNot { it.isCityCenter() && it.getCity()!!.health==1 } val closestEnemy = closeEnemies.minBy { it.arialDistanceTo(unit.getTile()) } @@ -213,7 +213,7 @@ class UnitAutomation{ .filter { it.location in unit.civInfo.exploredTiles } .map { it.getCenterTile() }.toList() - if(unit.baseUnit().unitType.isRanged()) // ranged units don't harm capturable cities, waste of a turn + if(unit.type.isRanged()) // ranged units don't harm capturable cities, waste of a turn enemyCities = enemyCities.filterNot { it.getCity()!!.health==1 } val closestReachableEnemyCity = enemyCities @@ -245,7 +245,7 @@ class UnitAutomation{ val cityWithHealthLeft = cityTilesToAttack.filter { it.tileToAttack.getCity()!!.health != 1 } // don't want ranged units to attack defeated cities .minBy { it.tileToAttack.getCity()!!.health } - if (unit.baseUnit().unitType.isMelee() && capturableCity!=null) + if (unit.type.isMelee() && capturableCity!=null) enemyTileToAttack = capturableCity // enter it quickly, top priority! else if (nonCityTilesToAttack.isNotEmpty()) // second priority, units @@ -266,7 +266,7 @@ class UnitAutomation{ } private fun tryGarrisoningUnit(unit: MapUnit): Boolean { - if(unit.baseUnit().unitType.isMelee()) return false // don't garrison melee units, they're not that good at it + if(unit.type.isMelee()) return false // don't garrison melee units, they're not that good at it val reachableCitiesWithoutUnits = unit.civInfo.cities.filter { val centerTile = it.getCenterTile() centerTile.militaryUnit==null diff --git a/core/src/com/unciv/logic/battle/BattleDamage.kt b/core/src/com/unciv/logic/battle/BattleDamage.kt index 8ca9aa7f..6f4dd1fa 100644 --- a/core/src/com/unciv/logic/battle/BattleDamage.kt +++ b/core/src/com/unciv/logic/battle/BattleDamage.kt @@ -45,7 +45,7 @@ class BattleDamage{ if(combatant.getCivilization().policies.isAdopted("Discipline") && combatant.isMelee() && combatant.getTile().neighbors.flatMap { it.getUnits() } - .any { it.civInfo==combatant.getCivilization() && !it.baseUnit.unitType.isCivilian()}) + .any { it.civInfo==combatant.getCivilization() && !it.type.isCivilian()}) modifiers["Discipline"] = 0.15f val requiredResource = combatant.unit.baseUnit.requiredResource diff --git a/core/src/com/unciv/logic/battle/MapUnitCombatant.kt b/core/src/com/unciv/logic/battle/MapUnitCombatant.kt index e765c576..54f2b9f8 100644 --- a/core/src/com/unciv/logic/battle/MapUnitCombatant.kt +++ b/core/src/com/unciv/logic/battle/MapUnitCombatant.kt @@ -28,7 +28,7 @@ class MapUnitCombatant(val unit: MapUnit) : ICombatant { } override fun getUnitType(): UnitType { - return unit.baseUnit().unitType + return unit.type } override fun toString(): String { diff --git a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt index 0f927940..d11c4cda 100644 --- a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt +++ b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt @@ -283,7 +283,7 @@ class CivilizationInfo { // disband units until there are none left OR the gold values are normal if(!isBarbarianCivilization() && gold < -100 && nextTurnStats.gold.toInt() < 0) { for (i in 1 until (gold / -100)) { - var civMilitaryUnits = getCivUnits().filter { !it.baseUnit().unitType.isCivilian() } + var civMilitaryUnits = getCivUnits().filter { !it.type.isCivilian() } if (civMilitaryUnits.isNotEmpty()) { val unitToDisband = civMilitaryUnits.first() unitToDisband.destroy() diff --git a/core/src/com/unciv/logic/map/MapUnit.kt b/core/src/com/unciv/logic/map/MapUnit.kt index 5e8b6983..5480ae93 100644 --- a/core/src/com/unciv/logic/map/MapUnit.kt +++ b/core/src/com/unciv/logic/map/MapUnit.kt @@ -7,6 +7,7 @@ import com.unciv.logic.automation.WorkerAutomation import com.unciv.logic.civilization.CivilizationInfo import com.unciv.models.gamebasics.GameBasics import com.unciv.models.gamebasics.unit.BaseUnit +import com.unciv.models.gamebasics.unit.UnitType import com.unciv.ui.utils.getRandom import java.text.DecimalFormat import java.util.* @@ -38,6 +39,9 @@ class MapUnit { return toReturn } + val type:UnitType + get()=baseUnit.unitType + fun baseUnit(): BaseUnit = baseUnit fun getMovementString(): String = DecimalFormat("0.#").format(currentMovement.toDouble()) + "/" + getMaxMovement() fun getTile(): TileInfo = currentTile @@ -47,7 +51,7 @@ class MapUnit { var movement = baseUnit.movement movement += getUniques().count{it=="+1 Movement"} - if(baseUnit.unitType.isWaterUnit() && !baseUnit.unitType.isCivilian() + if(type.isWaterUnit() && !type.isCivilian() && civInfo.getBuildingUniques().contains("All military naval units receive +1 movement and +1 sight")) movement += 1 @@ -83,7 +87,7 @@ class MapUnit { if(hasUnique("Limited Visibility")) visibilityRange-=1 if(civInfo.getNation().unique=="All land military units have +1 sight, 50% discount when purchasing tiles") visibilityRange += 1 - if(baseUnit.unitType.isWaterUnit() && !baseUnit.unitType.isCivilian() + if(type.isWaterUnit() && !type.isCivilian() && civInfo.getBuildingUniques().contains("All military naval units receive +1 movement and +1 sight")) visibilityRange += 1 val tile = getTile() @@ -108,14 +112,14 @@ class MapUnit { fun canPassThrough(tile: TileInfo):Boolean{ val tileOwner = tile.getOwner() - if(tile.isWater() && baseUnit.unitType.isLandUnit()){ + if(tile.isWater() && type.isLandUnit()){ val techUniques = civInfo.tech.getUniques() if(!techUniques.contains("Enables embarkation for land units")) return false if(tile.baseTerrain == "Ocean" && !techUniques.contains("Enables embarked units to enter ocean tiles")) return false } - if(tile.isLand() && baseUnit.unitType.isWaterUnit()) + if(tile.isLand() && type.isWaterUnit()) return false if(tile.baseTerrain=="Ocean" && baseUnit.uniques.contains("Cannot enter ocean tiles until Astronomy") && !civInfo.tech.isResearched("Astronomy")) @@ -132,7 +136,7 @@ class MapUnit { fun canMoveTo(tile: TileInfo): Boolean { if(!canPassThrough(tile)) return false - if (baseUnit().unitType.isCivilian()) + if (type.isCivilian()) return tile.civilianUnit==null && (tile.militaryUnit==null || tile.militaryUnit!!.owner==owner) else return tile.militaryUnit==null && (tile.civilianUnit==null || tile.civilianUnit!!.owner==owner) } @@ -154,7 +158,7 @@ class MapUnit { } fun getRange(): Int { - if(baseUnit().unitType.isMelee()) return 1 + if(type.isMelee()) return 1 var range = baseUnit().range if(hasUnique("+1 Range")) range++ return range @@ -162,7 +166,7 @@ class MapUnit { fun isEmbarked(): Boolean { - if(!baseUnit.unitType.isLandUnit()) return false + if(!type.isLandUnit()) return false return currentTile.baseTerrain=="Ocean"||currentTile.baseTerrain=="Coast" } @@ -298,13 +302,13 @@ class MapUnit { } fun removeFromTile(){ - if (baseUnit().unitType.isCivilian()) getTile().civilianUnit=null + if (type.isCivilian()) getTile().civilianUnit=null else getTile().militaryUnit=null } fun putInTile(tile:TileInfo){ if(!canMoveTo(tile)) throw Exception("I can't go there!") - if(baseUnit().unitType.isCivilian()) + if(type.isCivilian()) tile.civilianUnit=this else tile.militaryUnit=this currentTile = tile @@ -336,7 +340,7 @@ class MapUnit { civInfo.addNotification("A [$chosenUnit] has joined us!",null, Color.BLUE) } - if(!baseUnit.unitType.isCivilian()) + if(!type.isCivilian()) actions.add { promotions.XP+=10 civInfo.addNotification("An ancient tribe trains our [$name] in their ways of combat!",null, Color.RED) diff --git a/core/src/com/unciv/logic/map/UnitPromotions.kt b/core/src/com/unciv/logic/map/UnitPromotions.kt index 7cfc3cac..6d4dafa4 100644 --- a/core/src/com/unciv/logic/map/UnitPromotions.kt +++ b/core/src/com/unciv/logic/map/UnitPromotions.kt @@ -24,7 +24,7 @@ class UnitPromotions{ fun getAvailablePromotions(): List { return GameBasics.UnitPromotions.values - .filter { unit.baseUnit().unitType.toString() in it.unitTypes && it.name !in promotions } + .filter { unit.type.toString() in it.unitTypes && it.name !in promotions } .filter { it.prerequisites.isEmpty() || it.prerequisites.any { p->p in promotions } } } diff --git a/core/src/com/unciv/ui/pickerscreens/PromotionPickerScreen.kt b/core/src/com/unciv/ui/pickerscreens/PromotionPickerScreen.kt index 93cee8da..02cedefa 100644 --- a/core/src/com/unciv/ui/pickerscreens/PromotionPickerScreen.kt +++ b/core/src/com/unciv/ui/pickerscreens/PromotionPickerScreen.kt @@ -26,7 +26,7 @@ class PromotionPickerScreen(mapUnit: MapUnit) : PickerScreen() { val availablePromotionsGroup = VerticalGroup() availablePromotionsGroup.space(10f) - val unitType = mapUnit.baseUnit().unitType + val unitType = mapUnit.type val promotionsForUnitType = GameBasics.UnitPromotions.values.filter { it.unitTypes.contains(unitType.toString()) } val unitAvailablePromotions = mapUnit.promotions.getAvailablePromotions() for (promotion in promotionsForUnitType) { diff --git a/core/src/com/unciv/ui/tilegroups/WorldTileGroup.kt b/core/src/com/unciv/ui/tilegroups/WorldTileGroup.kt index 4bc9874f..42f33193 100644 --- a/core/src/com/unciv/ui/tilegroups/WorldTileGroup.kt +++ b/core/src/com/unciv/ui/tilegroups/WorldTileGroup.kt @@ -24,7 +24,7 @@ class WorldTileGroup(tileInfo: TileInfo) : TileGroup(tileInfo) { val whiteHalo = if(unit.isFortified()) ImageGetter.getImage("OtherIcons/Shield.png") else ImageGetter.getImage("OtherIcons/Circle.png") whiteHalo.setSize(30f,30f) - val unitImage = if(unit.baseUnit().unitType.isCivilian()) civilianUnitImage + val unitImage = if(unit.type.isCivilian()) civilianUnitImage else militaryUnitImage if(unitImage==null) //Stuff has changed since we requested this, the unit is no longer here... return diff --git a/core/src/com/unciv/ui/worldscreen/TileMapHolder.kt b/core/src/com/unciv/ui/worldscreen/TileMapHolder.kt index f7eef03d..54cba564 100644 --- a/core/src/com/unciv/ui/worldscreen/TileMapHolder.kt +++ b/core/src/com/unciv/ui/worldscreen/TileMapHolder.kt @@ -153,7 +153,7 @@ class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap: if(unit.canMoveTo(tile)) tileGroups[tile]!!.showCircle(colorFromRGB(0, 120, 215)) - val unitType = unit.baseUnit().unitType + val unitType = unit.type val attackableTiles: List = when{ unitType.isCivilian() -> unit.getDistanceToTiles().keys.toList() else -> UnitAutomation().getAttackableEnemies(unit, unit.getDistanceToTiles()).map { it.tileToAttack } @@ -164,14 +164,14 @@ class TileMapHolder(internal val worldScreen: WorldScreen, internal val tileMap: it.getUnits().isNotEmpty() && it.getUnits().first().owner != unit.owner && (playerViewableTilePositions.contains(it.position) || UnCivGame.Current.viewEntireMapForDebug)}) { - if(unit.baseUnit().unitType.isCivilian()) tileGroups[tile]!!.hideCircle() + if(unit.type.isCivilian()) tileGroups[tile]!!.hideCircle() else { tileGroups[tile]!!.showCircle(colorFromRGB(237, 41, 57)) tileGroups[tile]!!.showCrosshair() } } - val fadeout = if(unit.baseUnit.unitType.isCivilian()) 1f + val fadeout = if(unit.type.isCivilian()) 1f else 0.5f for(tile in tileGroups.values){ diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt index 06ba3ecc..53802772 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt @@ -280,7 +280,7 @@ class WorldScreen : CameraStageBaseScreen() { displayTutorials("EnemyCity") if("Enables construction of Spaceship parts" in civInfo.getBuildingUniques()) displayTutorials("ApolloProgram") - if(civInfo.getCivUnits().any { it.baseUnit.unitType == UnitType.Siege }) + if(civInfo.getCivUnits().any { it.type == UnitType.Siege }) displayTutorials("SiegeUnitTrained") if(civInfo.tech.getUniques().contains("Enables embarkation for land units")) displayTutorials("CanEmbark") diff --git a/core/src/com/unciv/ui/worldscreen/bottombar/BattleTable.kt b/core/src/com/unciv/ui/worldscreen/bottombar/BattleTable.kt index 3fee95a1..6f940124 100644 --- a/core/src/com/unciv/ui/worldscreen/bottombar/BattleTable.kt +++ b/core/src/com/unciv/ui/worldscreen/bottombar/BattleTable.kt @@ -31,7 +31,7 @@ class BattleTable(val worldScreen: WorldScreen): Table() { fun update() { val unitTable = worldScreen.bottomBar.unitTable if (unitTable.selectedUnit == null - || unitTable.selectedUnit!!.baseUnit().unitType.isCivilian()){ + || unitTable.selectedUnit!!.type.isCivilian()){ hide() return } // no attacker diff --git a/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt b/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt index 29bdc073..27d7f16d 100644 --- a/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt +++ b/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt @@ -41,7 +41,7 @@ class UnitActions { },true) } - if(!unit.baseUnit().unitType.isCivilian() && !unit.isEmbarked() && !unit.baseUnit.unitType.isWaterUnit() + if(!unit.type.isCivilian() && !unit.isEmbarked() && !unit.type.isWaterUnit() && !unit.hasUnique("No defensive terrain bonus") && !unit.isFortified()) { actionList += UnitAction("Fortify", { unit.action = "Fortify 0" }, unit.currentMovement != 0f) } @@ -50,7 +50,7 @@ class UnitActions { actionList += UnitAction("Sleep", { unit.action = "Sleep" }, unit.currentMovement != 0f) } - if(unit.baseUnit.unitType == UnitType.Scout){ + if(unit.type == UnitType.Scout){ if(unit.action != "explore") actionList += UnitAction("Explore", { UnitAutomation().automatedExplore(unit); unit.action = "explore" }, unit.currentMovement != 0f) @@ -58,7 +58,7 @@ class UnitActions { actionList += UnitAction("Stop exploration", { unit.action = null }, true) } - if(!unit.baseUnit().unitType.isCivilian() && unit.promotions.canBePromoted()){ + if(!unit.type.isCivilian() && unit.promotions.canBePromoted()){ actionList += UnitAction("Promote", {UnCivGame.Current.screen = PromotionPickerScreen(unit)}, unit.currentMovement != 0f) diff --git a/core/src/com/unciv/ui/worldscreen/unit/UnitTable.kt b/core/src/com/unciv/ui/worldscreen/unit/UnitTable.kt index c92c7ee8..bf2ab961 100644 --- a/core/src/com/unciv/ui/worldscreen/unit/UnitTable.kt +++ b/core/src/com/unciv/ui/worldscreen/unit/UnitTable.kt @@ -64,13 +64,13 @@ class UnitTable(val worldScreen: WorldScreen) : Table(){ unitNameLabel.setText(nameLabelText) var unitLabelText = "Movement".tr()+": " + unit.getMovementString() - if (!unit.baseUnit().unitType.isCivilian()) + if (!unit.type.isCivilian()) unitLabelText += "\n"+"Strength".tr()+": " + unit.baseUnit().strength if (unit.baseUnit().rangedStrength!=0) unitLabelText += "\n"+"Ranged strength".tr()+": "+unit.baseUnit().rangedStrength - if (!unit.baseUnit().unitType.isCivilian()) + if (!unit.type.isCivilian()) unitLabelText += "\n"+"XP".tr()+": "+unit.promotions.XP+"/"+unit.promotions.xpForNextPromotion() if(unit.isFortified() && unit.getFortificationTurns()>0)