mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-18 11:49:19 +07:00
Refactoring of isTransported
flag for air units (#1742)
* Refactoring of `isTransported` flag for air units * Minor refactoring: canAirUnitMoveTo as a separate function * Redundant check is removed
This commit is contained in:

committed by
Yair Morgenstern

parent
4accfb594f
commit
617eea92cf
@ -63,7 +63,7 @@ class MapUnit {
|
||||
var attacksThisTurn = 0
|
||||
var promotions = UnitPromotions()
|
||||
var due: Boolean = true
|
||||
var isUnitInCity: Boolean = true
|
||||
var isTransported: Boolean = false
|
||||
|
||||
companion object {
|
||||
private const val ANCIENT_RUIN_MAP_REVEAL_OFFSET = 4
|
||||
@ -81,7 +81,7 @@ class MapUnit {
|
||||
toReturn.action=action
|
||||
toReturn.attacksThisTurn=attacksThisTurn
|
||||
toReturn.promotions=promotions.clone()
|
||||
toReturn.isUnitInCity=isUnitInCity
|
||||
toReturn.isTransported=isTransported
|
||||
return toReturn
|
||||
}
|
||||
|
||||
@ -465,7 +465,9 @@ class MapUnit {
|
||||
type.isCivilian() -> tile.civilianUnit=this
|
||||
else -> tile.militaryUnit=this
|
||||
}
|
||||
isUnitInCity = tile.isCityCenter() // prevent carriers from sailing away with air units explicitly assigned to city
|
||||
// this check is here in order to not load the fresh built unit into carrier right after the build
|
||||
isTransported = !tile.isCityCenter() &&
|
||||
type.isAirUnit() // not moving civilians
|
||||
moveThroughTile(tile)
|
||||
}
|
||||
|
||||
|
@ -219,6 +219,7 @@ class UnitMovementAlgorithms(val unit:MapUnit) {
|
||||
if(unit.type.isAirUnit()){ // they move differently from all other units
|
||||
unit.action=null
|
||||
unit.removeFromTile()
|
||||
unit.isTransported = false // it has left the carrier by own means
|
||||
unit.putInTile(destination)
|
||||
unit.currentMovement=0f
|
||||
return
|
||||
@ -242,6 +243,12 @@ class UnitMovementAlgorithms(val unit:MapUnit) {
|
||||
|
||||
unit.removeFromTile()
|
||||
unit.putInTile(destination)
|
||||
|
||||
for(payload in origin.getUnits().filter { it.isTransported }){ // bring along the payloads
|
||||
payload.removeFromTile()
|
||||
payload.putInTile(destination)
|
||||
payload.isTransported = true // restore the flag to not leave the payload in the city
|
||||
}
|
||||
|
||||
// Unit maintenance changed
|
||||
if (unit.canGarrison()
|
||||
@ -249,14 +256,6 @@ class UnitMovementAlgorithms(val unit:MapUnit) {
|
||||
&& unit.civInfo.policies.isAdopted("Oligarchy")
|
||||
) unit.civInfo.updateStatsForNextTurn()
|
||||
|
||||
if(unit.type.isAircraftCarrierUnit() || unit.type.isMissileCarrierUnit()){ // bring along the payloads
|
||||
for(airUnit in origin.airUnits.filter { !it.isUnitInCity }){
|
||||
airUnit.removeFromTile()
|
||||
airUnit.putInTile(destination)
|
||||
airUnit.isUnitInCity = false // don't leave behind payloads in the city if carrier happens to dock
|
||||
}
|
||||
}
|
||||
|
||||
// Move through all intermediate tiles to get ancient ruins, barb encampments
|
||||
// and to view tiles along the way
|
||||
// We only activate the moveThroughTile AFTER the putInTile because of a really weird bug -
|
||||
@ -277,19 +276,7 @@ class UnitMovementAlgorithms(val unit:MapUnit) {
|
||||
*/
|
||||
fun canMoveTo(tile: TileInfo): Boolean {
|
||||
if(unit.type.isAirUnit())
|
||||
if(tile.isCityCenter())
|
||||
return tile.airUnits.size<6 && tile.getCity()?.civInfo==unit.civInfo
|
||||
else if(tile.militaryUnit!=null) {
|
||||
val unitAtDestination = tile.militaryUnit!!
|
||||
|
||||
var unitCapacity = if (unitAtDestination.getUniques().contains("Can carry 2 aircraft")) 2 else 0
|
||||
// unitCapacity += unitAtDestination.getUniques().count { it == "Can carry 1 extra air unit" }
|
||||
|
||||
return ((unitAtDestination.type.isAircraftCarrierUnit() && !unit.type.isMissileUnit()) ||
|
||||
(unitAtDestination.type.isMissileCarrierUnit() && unit.type.isMissileUnit()))
|
||||
&& unitAtDestination.owner==unit.owner && tile.airUnits.size < unitCapacity
|
||||
} else
|
||||
return false
|
||||
return canAirUnitMoveTo(tile, unit)
|
||||
|
||||
if(!canPassThrough(tile))
|
||||
return false
|
||||
@ -299,6 +286,26 @@ class UnitMovementAlgorithms(val unit:MapUnit) {
|
||||
else return tile.militaryUnit==null && (tile.civilianUnit==null || tile.civilianUnit!!.owner==unit.owner)
|
||||
}
|
||||
|
||||
private fun canAirUnitMoveTo(tile: TileInfo, unit: MapUnit): Boolean {
|
||||
// landing in the city
|
||||
if (tile.isCityCenter()) {
|
||||
if (tile.airUnits.filter { !it.isTransported }.size < 6 && tile.getCity()?.civInfo == unit.civInfo)
|
||||
return true // if city is free - no problem, get in
|
||||
} // let's check whether it enters city on carrier now...
|
||||
|
||||
if (tile.militaryUnit != null) {
|
||||
val unitAtDestination = tile.militaryUnit!!
|
||||
|
||||
var unitCapacity = if (unitAtDestination.getUniques().contains("Can carry 2 aircraft")) 2 else 0
|
||||
// unitCapacity += unitAtDestination.getUniques().count { it == "Can carry 1 extra air unit" }
|
||||
|
||||
return ((unitAtDestination.type.isAircraftCarrierUnit() && !unit.type.isMissileUnit()) ||
|
||||
(unitAtDestination.type.isMissileCarrierUnit() && unit.type.isMissileUnit()))
|
||||
&& unitAtDestination.owner == unit.owner && tile.airUnits.filter { it.isTransported }.size < unitCapacity
|
||||
} else
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
// This is the most called function in the entire game,
|
||||
// so multiple callees of this function have been optimized,
|
||||
@ -397,4 +404,4 @@ class PathsToTilesWithinTurn : LinkedHashMap<TileInfo, UnitMovementAlgorithms.Pa
|
||||
}
|
||||
return reversePathList.reversed()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user