AI only constructs Settler if it already has worker / improvements

This commit is contained in:
Yair Morgenstern 2023-06-12 11:27:27 +03:00
parent e5232494a0
commit 71828349df

View File

@ -8,8 +8,6 @@ import com.unciv.logic.battle.BattleDamage
import com.unciv.logic.battle.CityCombatant import com.unciv.logic.battle.CityCombatant
import com.unciv.logic.battle.MapUnitCombatant import com.unciv.logic.battle.MapUnitCombatant
import com.unciv.logic.city.City import com.unciv.logic.city.City
import com.unciv.models.ruleset.INonPerpetualConstruction
import com.unciv.models.ruleset.PerpetualConstruction
import com.unciv.logic.civilization.AlertType import com.unciv.logic.civilization.AlertType
import com.unciv.logic.civilization.Civilization import com.unciv.logic.civilization.Civilization
import com.unciv.logic.civilization.NotificationCategory import com.unciv.logic.civilization.NotificationCategory
@ -33,8 +31,10 @@ import com.unciv.models.Counter
import com.unciv.models.ruleset.Belief import com.unciv.models.ruleset.Belief
import com.unciv.models.ruleset.BeliefType import com.unciv.models.ruleset.BeliefType
import com.unciv.models.ruleset.Building import com.unciv.models.ruleset.Building
import com.unciv.models.ruleset.INonPerpetualConstruction
import com.unciv.models.ruleset.MilestoneType import com.unciv.models.ruleset.MilestoneType
import com.unciv.models.ruleset.ModOptionsConstants import com.unciv.models.ruleset.ModOptionsConstants
import com.unciv.models.ruleset.PerpetualConstruction
import com.unciv.models.ruleset.Policy import com.unciv.models.ruleset.Policy
import com.unciv.models.ruleset.PolicyBranch import com.unciv.models.ruleset.PolicyBranch
import com.unciv.models.ruleset.Victory import com.unciv.models.ruleset.Victory
@ -1042,17 +1042,33 @@ object NextTurnAutomation {
val settlerUnits = civInfo.gameInfo.ruleset.units.values val settlerUnits = civInfo.gameInfo.ruleset.units.values
.filter { it.hasUnique(UniqueType.FoundCity) && it.isBuildable(civInfo) } .filter { it.hasUnique(UniqueType.FoundCity) && it.isBuildable(civInfo) }
if (settlerUnits.isEmpty()) return if (settlerUnits.isEmpty()) return
if (civInfo.units.getCivUnits().none { it.hasUnique(UniqueType.FoundCity) } if (!civInfo.units.getCivUnits().none { it.hasUnique(UniqueType.FoundCity) }) return
&& civInfo.cities.none {
if (civInfo.cities.any {
val currentConstruction = it.cityConstructions.getCurrentConstruction() val currentConstruction = it.cityConstructions.getCurrentConstruction()
currentConstruction is BaseUnit && currentConstruction.hasUnique(UniqueType.FoundCity) currentConstruction is BaseUnit && currentConstruction.hasUnique(UniqueType.FoundCity)
}) { }) return
val bestCity = civInfo.cities.filterNot { it.isPuppet }.maxByOrNull { it.cityStats.currentCityStats.production }!! if (civInfo.units.getCivUnits().none { it.isMilitary() }) return // We need someone to defend him first
val workersBuildableForThisCiv = civInfo.gameInfo.ruleset.units.values.any {
it.hasUnique(UniqueType.BuildImprovements)
&& it.isBuildable(civInfo)
}
val bestCity = civInfo.cities.filterNot { it.isPuppet }
// If we can build workers, then we want AT LEAST 2 improvements, OR a worker nearby.
// Otherwise, AI tries to produce settlers when it can hardly sustain itself
.filter {
!workersBuildableForThisCiv
|| it.getCenterTile().getTilesInDistance(2).count { it.improvement!=null } > 1
|| it.getCenterTile().getTilesInDistance(3).any { it.civilianUnit?.hasUnique(UniqueType.BuildImprovements)==true }
}
.maxByOrNull { it.cityStats.currentCityStats.production }
?: return
if (bestCity.cityConstructions.builtBuildings.size > 1) // 2 buildings or more, otherwise focus on self first if (bestCity.cityConstructions.builtBuildings.size > 1) // 2 buildings or more, otherwise focus on self first
bestCity.cityConstructions.currentConstructionFromQueue = settlerUnits.minByOrNull { it.cost }!!.name bestCity.cityConstructions.currentConstructionFromQueue = settlerUnits.minByOrNull { it.cost }!!.name
} }
}
// Technically, this function should also check for civs that have liberated one or more cities // Technically, this function should also check for civs that have liberated one or more cities
// However, that can be added in another update, this PR is large enough as it is. // However, that can be added in another update, this PR is large enough as it is.