Some improvement on ai.

This commit is contained in:
Duan Tao 2018-12-27 11:44:30 +08:00
parent 0e77d5b044
commit 6c51073112
2 changed files with 13 additions and 7 deletions

View File

@ -93,6 +93,7 @@ class NextTurnAutomation{
val rangedUnits = mutableListOf<MapUnit>() val rangedUnits = mutableListOf<MapUnit>()
val meleeUnits = mutableListOf<MapUnit>() val meleeUnits = mutableListOf<MapUnit>()
val civilianUnits = mutableListOf<MapUnit>() val civilianUnits = mutableListOf<MapUnit>()
val generals = mutableListOf<MapUnit>()
for (unit in civInfo.getCivUnits()) { for (unit in civInfo.getCivUnits()) {
if (unit.promotions.canBePromoted()) { if (unit.promotions.canBePromoted()) {
@ -101,10 +102,10 @@ class NextTurnAutomation{
unit.promotions.addPromotion(availablePromotions.getRandom().name) unit.promotions.addPromotion(availablePromotions.getRandom().name)
} }
val unitType = unit.type
when { when {
unitType.isRanged() -> rangedUnits.add(unit) unit.type.isRanged() -> rangedUnits.add(unit)
unitType.isMelee() -> meleeUnits.add(unit) unit.type.isMelee() -> meleeUnits.add(unit)
unit.name == "Great General" -> generals.add(unit) //generals move after military units
else -> civilianUnits.add(unit) else -> civilianUnits.add(unit)
} }
} }
@ -112,6 +113,7 @@ class NextTurnAutomation{
for (unit in civilianUnits) UnitAutomation().automateUnitMoves(unit) // They move first so that combat units can accompany a settler for (unit in civilianUnits) UnitAutomation().automateUnitMoves(unit) // They move first so that combat units can accompany a settler
for (unit in rangedUnits) UnitAutomation().automateUnitMoves(unit) for (unit in rangedUnits) UnitAutomation().automateUnitMoves(unit)
for (unit in meleeUnits) UnitAutomation().automateUnitMoves(unit) for (unit in meleeUnits) UnitAutomation().automateUnitMoves(unit)
for (unit in generals) UnitAutomation().automateUnitMoves(unit)
} }
private fun reassignWorkedTiles(civInfo: CivilizationInfo) { private fun reassignWorkedTiles(civInfo: CivilizationInfo) {

View File

@ -400,19 +400,23 @@ class SpecificUnitAutomation{
} }
fun automateGeneral(unit: MapUnit){ fun automateGeneral(unit: MapUnit){
//try to follow nearby units. Do not garrison in city if possible
val militantToCompany = unit.civInfo.getCivUnits() val militantToCompany = unit.civInfo.getCivUnits()
.firstOrNull { val tile = it.currentTile .firstOrNull { val tile = it.currentTile
it.type.isLandUnit() && it.getMaxMovement() <= 2.0f && tile.civilianUnit==null it.type.isLandUnit() && it.getMaxMovement() <= 2.0f && tile.civilianUnit==null
&& unit.canMoveTo(tile) && unit.movementAlgs().canReach(tile) } && unit.canMoveTo(tile) && unit.movementAlgs().canReach(tile) && !tile.isCityCenter() }
if(militantToCompany!=null) { if(militantToCompany!=null) {
unit.movementAlgs().headTowards(militantToCompany.currentTile) unit.movementAlgs().headTowards(militantToCompany.currentTile)
return return
} }
val cityToGarison = unit.civInfo.cities.filter {it.getCenterTile().civilianUnit == null} //if no unit to follow, take refuge in city.
.minBy { it.getCenterTile().arialDistanceTo(unit.currentTile) } val cityToGarison = unit.civInfo.cities.map {it.getCenterTile()}
.filter {it.civilianUnit == null && unit.canMoveTo(it)}
.minBy { it.arialDistanceTo(unit.currentTile) }
if (cityToGarison != null) { if (cityToGarison != null) {
unit.movementAlgs().headTowards(cityToGarison.getCenterTile()) unit.movementAlgs().headTowards(cityToGarison)
return return
} }
return return