Dev: added "type" property to MapUnit to reflect baseUnit.unitType, since that's used EVERYWHERE

This commit is contained in:
Yair Morgenstern 2018-11-17 21:57:52 +02:00
parent f6f8b53ab1
commit af88d287c3
15 changed files with 38 additions and 34 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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 {

View file

@ -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()

View file

@ -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)

View file

@ -24,7 +24,7 @@ class UnitPromotions{
fun getAvailablePromotions(): List<Promotion> {
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 } }
}

View file

@ -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) {

View file

@ -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

View file

@ -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<TileInfo> = 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){

View file

@ -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")

View file

@ -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

View file

@ -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)

View file

@ -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)