Moved changeImprovement to TileInfoImprovementFunctions

This commit is contained in:
Yair Morgenstern 2023-08-08 22:06:25 +03:00
parent 3cbd447620
commit 7b778f7535
3 changed files with 45 additions and 40 deletions

View File

@ -178,9 +178,10 @@ class UnitTurnManager(val unit: MapUnit) {
if (unit.civ.isCurrentPlayer())
UncivGame.Current.settings.addCompletedTutorialTask("Construct an improvement")
val improvementInProgress = tile.improvementInProgress ?: return
when {
tile.improvementInProgress!!.startsWith(Constants.remove) -> {
val removedFeatureName = tile.improvementInProgress!!.removePrefix(Constants.remove)
improvementInProgress.startsWith(Constants.remove) -> {
val removedFeatureName = improvementInProgress.removePrefix(Constants.remove)
val tileImprovement = tile.getTileImprovement()
if (tileImprovement != null
&& tile.terrainFeatures.any {
@ -192,7 +193,7 @@ class UnitTurnManager(val unit: MapUnit) {
tile.removeImprovement()
if (tile.resource != null) unit.civ.cache.updateCivResources() // unlikely, but maybe a mod makes a resource improvement dependent on a terrain feature
}
if (RoadStatus.values().any { tile.improvementInProgress == it.removeAction }) {
if (RoadStatus.values().any { improvementInProgress == it.removeAction }) {
tile.removeRoad()
} else {
val removedFeatureObject = tile.ruleset.terrains[removedFeatureName]
@ -202,11 +203,11 @@ class UnitTurnManager(val unit: MapUnit) {
tile.removeTerrainFeature(removedFeatureName)
}
}
tile.improvementInProgress == RoadStatus.Road.name -> tile.addRoad(RoadStatus.Road, unit.civ)
tile.improvementInProgress == RoadStatus.Railroad.name -> tile.addRoad(RoadStatus.Railroad, unit.civ)
tile.improvementInProgress == Constants.repair -> tile.setRepaired()
improvementInProgress == RoadStatus.Road.name -> tile.addRoad(RoadStatus.Road, unit.civ)
improvementInProgress == RoadStatus.Railroad.name -> tile.addRoad(RoadStatus.Railroad, unit.civ)
improvementInProgress == Constants.repair -> tile.setRepaired()
else -> {
tile.changeImprovement(tile.improvementInProgress, unit.civ)
tile.changeImprovement(improvementInProgress, unit.civ)
}
}

View File

@ -8,11 +8,11 @@ import com.unciv.logic.city.City
import com.unciv.logic.civilization.Civilization
import com.unciv.logic.civilization.PlayerType
import com.unciv.logic.map.HexMath
import com.unciv.logic.map.MapParameters // Kdoc only
import com.unciv.logic.map.MapParameters
import com.unciv.logic.map.MapResources
import com.unciv.logic.map.TileMap
import com.unciv.logic.map.mapunit.MapUnit
import com.unciv.logic.map.mapunit.UnitMovement // Kdoc only
import com.unciv.logic.map.mapunit.UnitMovement
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.tile.ResourceType
import com.unciv.models.ruleset.tile.Terrain
@ -25,7 +25,6 @@ import com.unciv.models.ruleset.unique.UniqueMap
import com.unciv.models.ruleset.unique.UniqueType
import com.unciv.ui.components.extensions.withItem
import com.unciv.ui.components.extensions.withoutItem
import com.unciv.ui.screens.worldscreen.unit.actions.UnitActions
import com.unciv.utils.DebugUtils
import kotlin.math.abs
import kotlin.math.min
@ -310,37 +309,11 @@ open class Tile : IsPartOfGameInfoSerialization {
}
/** Does not remove roads */
fun removeImprovement() = changeImprovement(null)
fun removeImprovement() =
improvementFunctions.changeImprovement(null)
fun changeImprovement(improvementStr: String?, civToHandleCompletion:Civilization? = null) {
improvementIsPillaged = false
improvement = improvementStr
val improvementObject = getTileImprovement()
if (improvementObject != null && improvementObject.hasUnique(UniqueType.RemovesFeaturesIfBuilt)) {
// Remove terrainFeatures that a Worker can remove
// and that aren't explicitly allowed under the improvement
val removableTerrainFeatures = terrainFeatures.filter { feature ->
val removingAction = "${Constants.remove}$feature"
removingAction in ruleset.tileImprovements // is removable
&& !improvementObject.isAllowedOnFeature(feature) // cannot coexist
}
setTerrainFeatures(terrainFeatures.filterNot { it in removableTerrainFeatures })
}
if (civToHandleCompletion != null && improvementObject != null
&& improvementObject.hasUnique(UniqueType.TakesOverAdjacentTiles)
)
UnitActions.takeOverTilesAround(civToHandleCompletion, this)
if (owningCity != null) {
owningCity!!.civ.cache.updateCivResources()
owningCity!!.reassignPopulationDeferred()
}
}
fun changeImprovement(improvementStr: String, civToHandleCompletion:Civilization? = null) =
improvementFunctions.changeImprovement(improvementStr, civToHandleCompletion)
// function handling when adding a road to the tile
fun addRoad(roadType: RoadStatus, unitCivInfo: Civilization) {

View File

@ -5,6 +5,7 @@ import com.unciv.logic.civilization.Civilization
import com.unciv.models.ruleset.tile.TileImprovement
import com.unciv.models.ruleset.unique.StateForConditionals
import com.unciv.models.ruleset.unique.UniqueType
import com.unciv.ui.screens.worldscreen.unit.actions.UnitActions
enum class ImprovementBuildingProblem {
@ -163,6 +164,36 @@ class TileInfoImprovementFunctions(val tile: Tile) {
}
fun changeImprovement(improvementStr: String?, civToHandleCompletion:Civilization? = null) {
tile.improvementIsPillaged = false
tile.improvement = improvementStr
val improvementObject = tile.getTileImprovement()
if (improvementObject != null && improvementObject.hasUnique(UniqueType.RemovesFeaturesIfBuilt)) {
// Remove terrainFeatures that a Worker can remove
// and that aren't explicitly allowed under the improvement
val removableTerrainFeatures = tile.terrainFeatures.filter { feature ->
val removingAction = "${Constants.remove}$feature"
removingAction in tile.ruleset.tileImprovements // is removable
&& !improvementObject.isAllowedOnFeature(feature) // cannot coexist
}
tile.setTerrainFeatures(tile.terrainFeatures.filterNot { it in removableTerrainFeatures })
}
if (civToHandleCompletion != null && improvementObject != null
&& improvementObject.hasUnique(UniqueType.TakesOverAdjacentTiles)
)
UnitActions.takeOverTilesAround(civToHandleCompletion, tile)
if (tile.owningCity != null) {
tile.owningCity!!.civ.cache.updateCivResources()
tile.owningCity!!.reassignPopulationDeferred()
}
}
/** Marks tile as target tile for a building with a [UniqueType.CreatesOneImprovement] unique */
fun markForCreatesOneImprovement(improvement: String) {