mirror of
https://github.com/yairm210/Unciv.git
synced 2025-02-09 10:29:02 +07:00
Resolved #9641 - Removed static wrapper functions from WorkerAutomation
This commit is contained in:
parent
b6aa177311
commit
b7e4ca358c
@ -59,11 +59,11 @@ object SpecificUnitAutomation {
|
||||
// try to revenge and capture their tiles
|
||||
val enemyCities = unit.civ.getKnownCivs()
|
||||
.filter { unit.civ.getDiplomacyManager(it).hasModifier(DiplomaticModifiers.StealingTerritory) }
|
||||
.flatMap { it.cities }.asSequence()
|
||||
.flatMap { it.cities }
|
||||
// find the suitable tiles (or their neighbours)
|
||||
val tileToSteal = enemyCities.flatMap { it.getTiles() } // City tiles
|
||||
.filter { it.neighbors.any { tile -> tile.getOwner() != unit.civ } } // Edge city tiles
|
||||
.flatMap { it.neighbors.asSequence() } // Neighbors of edge city tiles
|
||||
.flatMap { it.neighbors } // Neighbors of edge city tiles
|
||||
.filter {
|
||||
it in unit.civ.viewableTiles // we can see them
|
||||
&& it.neighbors.any { tile -> tile.getOwner() == unit.civ }// they are close to our borders
|
||||
@ -74,7 +74,7 @@ object SpecificUnitAutomation {
|
||||
// ...also get priorities to steal the most valuable for them
|
||||
val owner = it.getOwner()
|
||||
if (owner != null)
|
||||
distance - WorkerAutomation.getPriority(it, owner)
|
||||
distance - owner.getWorkerAutomation().getPriority(it)
|
||||
else distance
|
||||
}
|
||||
.firstOrNull { unit.movement.canReach(it) } // canReach is performance-heavy and always a last resort
|
||||
@ -87,7 +87,7 @@ object SpecificUnitAutomation {
|
||||
}
|
||||
|
||||
// try to build a citadel for defensive purposes
|
||||
if (WorkerAutomation.evaluateFortPlacement(unit.currentTile, unit.civ, true)) {
|
||||
if (unit.civ.getWorkerAutomation().evaluateFortPlacement(unit.currentTile, true)) {
|
||||
UnitActions.getImprovementConstructionActions(unit, unit.currentTile).firstOrNull()?.action?.invoke()
|
||||
return true
|
||||
}
|
||||
@ -114,7 +114,7 @@ object SpecificUnitAutomation {
|
||||
val tileForCitadel = cityToGarrison.getTilesInDistanceRange(3..4)
|
||||
.firstOrNull {
|
||||
reachableTest(it) &&
|
||||
WorkerAutomation.evaluateFortPlacement(it, unit.civ, true)
|
||||
unit.civ.getWorkerAutomation().evaluateFortPlacement(it, true)
|
||||
}
|
||||
if (tileForCitadel == null) {
|
||||
unit.movement.headTowards(cityToGarrison)
|
||||
@ -360,14 +360,14 @@ object SpecificUnitAutomation {
|
||||
val city =
|
||||
if (ourCitiesWithoutReligion.any())
|
||||
ourCitiesWithoutReligion.minByOrNull { it.getCenterTile().aerialDistanceTo(unit.getTile()) }
|
||||
else unit.civ.gameInfo.getCities().asSequence()
|
||||
else unit.civ.gameInfo.getCities()
|
||||
.filter { it.religion.getMajorityReligion() != unit.civ.religionManager.religion }
|
||||
.filter { it.civ.knows(unit.civ) && !it.civ.isAtWarWith(unit.civ) }
|
||||
.filterNot { it.religion.isProtectedByInquisitor(unit.religion) }
|
||||
.minByOrNull { it.getCenterTile().aerialDistanceTo(unit.getTile()) }
|
||||
|
||||
if (city == null) return
|
||||
val destination = city.getTiles().asSequence()
|
||||
val destination = city.getTiles()
|
||||
.filter { unit.movement.canMoveTo(it) || it == unit.getTile() }
|
||||
.sortedBy { it.aerialDistanceTo(unit.getTile()) }
|
||||
.firstOrNull { unit.movement.canReach(it) } ?: return
|
||||
@ -421,7 +421,7 @@ object SpecificUnitAutomation {
|
||||
if (destination == null) return
|
||||
|
||||
if (!unit.movement.canReach(destination)) {
|
||||
destination = destination.neighbors.asSequence()
|
||||
destination = destination.neighbors
|
||||
.filter { unit.movement.canMoveTo(it) || it == unit.getTile() }
|
||||
.sortedBy { it.aerialDistanceTo(unit.currentTile) }
|
||||
.firstOrNull { unit.movement.canReach(it) }
|
||||
|
@ -249,7 +249,7 @@ object UnitAutomation {
|
||||
return SpecificUnitAutomation.automateSettlerActions(unit, tilesWhereWeWillBeCaptured)
|
||||
|
||||
if (unit.cache.hasUniqueToBuildImprovements)
|
||||
return WorkerAutomation.automateWorkerAction(unit, tilesWhereWeWillBeCaptured)
|
||||
return unit.civ.getWorkerAutomation().automateWorkerAction(unit, tilesWhereWeWillBeCaptured)
|
||||
|
||||
if (unit.hasUnique(UniqueType.MayFoundReligion)
|
||||
&& unit.civ.religionManager.religionState < ReligionState.Religion
|
||||
|
@ -107,21 +107,6 @@ class WorkerAutomation(
|
||||
///////////////////////////////////////// Helpers /////////////////////////////////////////
|
||||
|
||||
companion object {
|
||||
/** Maps to instance [WorkerAutomation.automateWorkerAction] knowing only the MapUnit */
|
||||
fun automateWorkerAction(unit: MapUnit, tilesWhereWeWillBeCaptured: Set<Tile>) {
|
||||
unit.civ.getWorkerAutomation().automateWorkerAction(unit, tilesWhereWeWillBeCaptured)
|
||||
}
|
||||
|
||||
/** Convenience shortcut supports old calling syntax for [WorkerAutomation.getPriority] */
|
||||
fun getPriority(tile: Tile, civInfo: Civilization): Int {
|
||||
return civInfo.getWorkerAutomation().getPriority(tile)
|
||||
}
|
||||
|
||||
/** Convenience shortcut supports old calling syntax for [WorkerAutomation.evaluateFortPlacement] */
|
||||
fun evaluateFortPlacement(tile: Tile, civInfo: Civilization, isCitadel: Boolean): Boolean {
|
||||
return civInfo.getWorkerAutomation().evaluateFortPlacement(tile, isCitadel)
|
||||
}
|
||||
|
||||
/** For console logging only */
|
||||
private fun MapUnit.label() = toString() + " " + getTile().position.toString()
|
||||
}
|
||||
@ -135,7 +120,7 @@ class WorkerAutomation(
|
||||
val currentTile = unit.getTile()
|
||||
val tileToWork = findTileToWork(unit, tilesWhereWeWillBeCaptured)
|
||||
|
||||
if (getPriority(tileToWork, civInfo) < 3) { // building roads is more important
|
||||
if (civInfo.getWorkerAutomation().getPriority(tileToWork) < 3) { // building roads is more important
|
||||
if (tryConnectingCities(unit)) return
|
||||
}
|
||||
|
||||
@ -345,7 +330,7 @@ class WorkerAutomation(
|
||||
/**
|
||||
* Calculate a priority for improving a tile
|
||||
*/
|
||||
private fun getPriority(tile: Tile): Int {
|
||||
fun getPriority(tile: Tile): Int {
|
||||
var priority = 0
|
||||
if (tile.getOwner() == civInfo) {
|
||||
priority += 2
|
||||
@ -465,7 +450,7 @@ class WorkerAutomation(
|
||||
* @param isCitadel Controls within borders check - true also allows 1 tile outside borders
|
||||
* @return Yes please build a Fort here
|
||||
*/
|
||||
private fun evaluateFortPlacement(tile: Tile, isCitadel: Boolean): Boolean {
|
||||
fun evaluateFortPlacement(tile: Tile, isCitadel: Boolean): Boolean {
|
||||
//todo Is the Citadel code dead anyway? If not - why does the nearestTiles check not respect the param?
|
||||
|
||||
// build on our land only
|
||||
|
@ -2,7 +2,6 @@ package com.unciv.logic.civilization
|
||||
|
||||
import com.badlogic.gdx.math.Vector2
|
||||
import com.unciv.Constants
|
||||
import com.unciv.UncivGame
|
||||
import com.unciv.json.HashMapVector2
|
||||
import com.unciv.logic.GameInfo
|
||||
import com.unciv.logic.IsPartOfGameInfoSerialization
|
||||
@ -32,7 +31,7 @@ import com.unciv.logic.map.mapunit.MapUnit
|
||||
import com.unciv.logic.map.tile.Tile
|
||||
import com.unciv.logic.trade.TradeRequest
|
||||
import com.unciv.models.Counter
|
||||
import com.unciv.models.metadata.GameParameters // Kdoc only
|
||||
import com.unciv.models.metadata.GameParameters
|
||||
import com.unciv.models.ruleset.Building
|
||||
import com.unciv.models.ruleset.Policy
|
||||
import com.unciv.models.ruleset.Victory
|
||||
@ -74,9 +73,7 @@ class Civilization : IsPartOfGameInfoSerialization {
|
||||
/** Returns an instance of WorkerAutomation valid for the duration of the current turn
|
||||
* This instance carries cached data common for all Workers of this civ */
|
||||
fun getWorkerAutomation(): WorkerAutomation {
|
||||
val currentTurn = if (UncivGame.Current.isInitialized && UncivGame.Current.gameInfo != null) {
|
||||
UncivGame.Current.gameInfo!!.turns
|
||||
} else 0
|
||||
val currentTurn = gameInfo.turns
|
||||
if (workerAutomationCache == null || workerAutomationCache!!.cachedForTurn != currentTurn)
|
||||
workerAutomationCache = WorkerAutomation(this, currentTurn)
|
||||
return workerAutomationCache!!
|
||||
|
Loading…
Reference in New Issue
Block a user