Resolved #3076 - automation now happens at the end of turn rather than the beginning

This commit is contained in:
Yair Morgenstern 2020-09-02 23:05:24 +03:00
parent def96071e7
commit 1b3469ea2e
4 changed files with 48 additions and 59 deletions

View File

@ -27,7 +27,7 @@ class WorkerAutomation(val unit: MapUnit) {
if (tileToWork != currentTile) {
val reachedTile = unit.movement.headTowards(tileToWork)
if (reachedTile != currentTile) unit.doPreTurnAction() // otherwise, we get a situation where the worker is automated, so it tries to move but doesn't, then tries to automate, then move, etc, forever. Stack overflow exception!
if (reachedTile != currentTile) unit.doAction() // otherwise, we get a situation where the worker is automated, so it tries to move but doesn't, then tries to automate, then move, etc, forever. Stack overflow exception!
return
}
@ -52,7 +52,7 @@ class WorkerAutomation(val unit: MapUnit) {
if (mostUndevelopedCity != null && mostUndevelopedCity != unit.currentTile.owningCity) {
val reachedTile = unit.movement.headTowards(mostUndevelopedCity.getCenterTile())
if (reachedTile != currentTile) unit.doPreTurnAction() // since we've moved, maybe we can do something here - automate
if (reachedTile != currentTile) unit.doAction() // since we've moved, maybe we can do something here - automate
return
}

View File

@ -86,14 +86,12 @@ object Battle {
}
if (attacker is MapUnitCombatant) {
if (attacker.getUnitType()==UnitType.Missile) {
if (attacker.getUnitType() == UnitType.Missile)
attacker.unit.destroy()
} else if (attacker.unit.action != null
&& attacker.unit.action!!.startsWith("moveTo")) {
else if (attacker.unit.isMoving())
attacker.unit.action = null
}
}
}
private fun takeDamage(attacker: ICombatant, defender: ICombatant) {
var damageToDefender = BattleDamage.calculateDamageToDefender(attacker, attacker.getTile(), defender)

View File

@ -179,13 +179,11 @@ class MapUnit {
civInfo.updateViewableTiles() // for the civ
}
fun isFortified(): Boolean {
return action?.startsWith("Fortify") == true
}
fun isFortified() = action?.startsWith("Fortify") == true
fun isSleeping(): Boolean {
return action?.startsWith("Sleep") == true
}
fun isSleeping() = action?.startsWith("Sleep") == true
fun isMoving() = action?.startsWith("moveTo") == true
fun getFortificationTurns(): Int {
if (!isFortified()) return 0
@ -203,7 +201,7 @@ class MapUnit {
if (hasUnique("Can construct roads") && currentTile.improvementInProgress == "Road") return false
if (isFortified()) return false
if (action == Constants.unitActionExplore || isSleeping()
|| action == Constants.unitActionAutomation) return false
|| action == Constants.unitActionAutomation || isMoving()) return false
return true
}
@ -284,13 +282,9 @@ class MapUnit {
return true
}
fun fortify() {
action = "Fortify 0"
}
fun fortify() { action = "Fortify 0" }
fun fortifyUntilHealed() {
action = "Fortify 0 until healed"
}
fun fortifyUntilHealed() { action = "Fortify 0 until healed" }
fun fortifyIfCan() {
if (canFortify()) {
@ -323,9 +317,8 @@ class MapUnit {
if(currentMovement<0) currentMovement = 0f
}
fun doPreTurnAction() {
fun doAction() {
if (action == null) return
val currentTile = getTile()
if (currentMovement == 0f) return // We've already done stuff this turn, and can't do any more stuff
val enemyUnitsInWalkingDistance = movement.getDistanceToTiles().keys
@ -338,16 +331,17 @@ class MapUnit {
mapUnitAction?.doPreTurnAction()
if (action != null && action!!.startsWith("moveTo")) {
val currentTile = getTile()
if (isMoving()) {
val destination = action!!.replace("moveTo ", "").split(",").dropLastWhile { it.isEmpty() }.toTypedArray()
val destinationVector = Vector2(Integer.parseInt(destination[0]).toFloat(), Integer.parseInt(destination[1]).toFloat())
val destinationVector = Vector2(destination[0].toFloat(), destination[1].toFloat())
val destinationTile = currentTile.tileMap[destinationVector]
if (!movement.canReach(destinationTile)) return // That tile that we were moving towards is now unreachable
val gotTo = movement.headTowards(destinationTile)
if (gotTo == currentTile) // We didn't move at all
return
if (gotTo.position == destinationVector) action = null
if (currentMovement > 0) doPreTurnAction()
if (currentMovement > 0) doAction()
return
}
@ -356,18 +350,6 @@ class MapUnit {
if (action == Constants.unitActionExplore) UnitAutomation.automatedExplore(this)
}
private fun doPostTurnAction() {
if (hasUnique(Constants.workerUnique) && getTile().improvementInProgress != null) workOnImprovement()
if(hasUnique("Can construct roads") && currentTile.improvementInProgress=="Road") workOnImprovement()
if(currentMovement == getMaxMovement().toFloat()
&& isFortified()){
val currentTurnsFortified = getFortificationTurns()
if(currentTurnsFortified<2)
action = action!!.replace(currentTurnsFortified.toString(),(currentTurnsFortified+1).toString(), true)
}
if (hasUnique("Heal adjacent units for an additional 15 HP per turn"))
currentTile.neighbors.flatMap{ it.getUnits() }.forEach{ it.healBy(15) }
}
private fun workOnImprovement() {
val tile = getTile()
@ -462,11 +444,21 @@ class MapUnit {
}
fun endTurn() {
doPostTurnAction()
if (currentMovement == getMaxMovement().toFloat() // didn't move this turn
|| hasUnique("Unit will heal every turn, even if it performs an action")){
heal()
doAction()
if (hasUnique(Constants.workerUnique) && getTile().improvementInProgress != null) workOnImprovement()
if (hasUnique("Can construct roads") && currentTile.improvementInProgress == "Road") workOnImprovement()
if (currentMovement == getMaxMovement().toFloat() && isFortified()) {
val currentTurnsFortified = getFortificationTurns()
if (currentTurnsFortified < 2)
action = action!!.replace(currentTurnsFortified.toString(), (currentTurnsFortified + 1).toString(), true)
}
if (hasUnique("Heal adjacent units for an additional 15 HP per turn"))
currentTile.neighbors.flatMap { it.getUnits() }.forEach { it.healBy(15) }
if (currentMovement == getMaxMovement().toFloat() // didn't move this turn
|| hasUnique("Unit will heal every turn, even if it performs an action")) heal()
if (action != null && health > 99)
if (action!!.endsWith(" until healed")) {
action = null // wake up when healed
@ -490,7 +482,6 @@ class MapUnit {
val tileOwner = getTile().getOwner()
if (tileOwner != null && !civInfo.canEnterTiles(tileOwner) && !tileOwner.isCityState()) // if an enemy city expanded onto this tile while I was in it
movement.teleportToClosestMoveableTile()
doPreTurnAction()
}
fun destroy(){

View File

@ -32,7 +32,7 @@ object UnitActions {
val unitTable = worldScreen.bottomUnitTable
val actionList = ArrayList<UnitAction>()
if (unit.action != null && unit.action!!.startsWith("moveTo")) {
if (unit.isMoving()) {
actionList += UnitAction(
type = UnitActionType.StopMovement,
action = { unit.action = null }