Skip to content

Commit

Permalink
Resolve misc. issues with commit 27a03bc
Browse files Browse the repository at this point in the history
  • Loading branch information
doublep committed May 21, 2022
1 parent 27a03bc commit 93d9fe9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
27 changes: 21 additions & 6 deletions core/src/com/unciv/logic/civilization/CivilizationInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ class CivilizationInfo {
* might include e.g. non-idle units, so must be filtered before being given to any outside code.
* (Should be small enough that using ArrayList is fine.)
*/
private var dueUnits = ArrayList<MapUnit>()
@Transient
private var dueUnits = mutableListOf<MapUnit>()
var hasMovedAutomatedUnits = false

@Transient
Expand Down Expand Up @@ -270,7 +271,7 @@ class CivilizationInfo {
toReturn.totalCultureForContests = totalCultureForContests
toReturn.totalFaithForContests = totalFaithForContests
toReturn.attacksSinceTurnStart = attacksSinceTurnStart.copy()
toReturn.dueUnits = ArrayList<MapUnit>(dueUnits)
toReturn.dueUnits = dueUnits.toMutableList()
toReturn.hasMovedAutomatedUnits = hasMovedAutomatedUnits
return toReturn
}
Expand Down Expand Up @@ -460,6 +461,8 @@ class CivilizationInfo {
newList.add(mapUnit)
units = newList

// Make sure it is initialized.
getDueUnits()
dueUnits.add(mapUnit)

if (updateCivInfo) {
Expand All @@ -474,26 +477,40 @@ class CivilizationInfo {
val newList = ArrayList(units)
newList.remove(mapUnit)
units = newList
dueUnits.remove(mapUnit)
updateStatsForNextTurn() // unit upkeep
updateDetailedCivResources()
}

fun getIdleUnits() = getCivUnits().filter { it.isIdle() }

// Drop all units that are not really 'due' anymore. We do it here to avoid caring how and where it happened.
fun getDueUnits() = dueUnits.filter { it.due && !it.isDestroyed && it.isIdle() }
// Internal side effect: if 'dueUnits' has never been initialized (new game, load game), do it here.
fun getDueUnits(): List<MapUnit> {
if (dueUnits.none())
dueUnits.addAll(units)
return dueUnits.filter { it.due && it.isIdle() }
}

fun shouldGoToDueUnit() = UncivGame.Current.settings.checkForDueUnits && getDueUnits().any()

// Callers should consider if cycleThroughDueUnits() is not a better choice.
fun getNextDueUnit() = getDueUnits().firstOrNull()

fun cycleThroughDueUnits(): MapUnit? {
fun cycleThroughDueUnits(unitToSkip: MapUnit?): MapUnit? {
var realDueUnits = getDueUnits();
if (realDueUnits.any()) {
var unit = realDueUnits.first();
// We shift the unit to the back of the queue. However, the caller may clear its 'due' state if it wants.
dueUnits.remove(unit);
dueUnits.add(unit);

if (unit == unitToSkip && realDueUnits.size > 1) {
unit = realDueUnits[1];
dueUnits.remove(unit);
dueUnits.add(unit);
}

return unit;
}
else return null;
Expand Down Expand Up @@ -842,8 +859,6 @@ class CivilizationInfo {
fun startTurn() {
civConstructions.startTurn()
attacksSinceTurnStart.clear()
dueUnits.clear()
dueUnits.addAll(getCivUnits())
updateStatsForNextTurn() // for things that change when turn passes e.g. golden age, city state influence

// Do this after updateStatsForNextTurn but before cities.startTurn
Expand Down
8 changes: 3 additions & 5 deletions core/src/com/unciv/ui/worldscreen/WorldScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,8 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Bas
// Don't select unit and change selectedCiv when centering as spectator
if (viewingCiv.isSpectator())
mapHolder.setCenterPosition(tileToCenterOn, immediately = true, selectUnit = false)
else {
else
mapHolder.setCenterPosition(tileToCenterOn, immediately = true, selectUnit = true)
if (viewingCiv.getNextDueUnit() == bottomUnitTable.selectedUnit)
viewingCiv.cycleThroughDueUnits()
}

tutorialController.allTutorialsShowedCallback = { shouldUpdate = true }

Expand Down Expand Up @@ -730,7 +727,8 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Bas
}

fun switchToNextUnit() {
val nextDueUnit = viewingCiv.cycleThroughDueUnits()
// Try to select something new if we already have the next pending unit selected.
val nextDueUnit = viewingCiv.cycleThroughDueUnits(bottomUnitTable.selectedUnit)
if (nextDueUnit != null) {
mapHolder.setCenterPosition(
nextDueUnit.currentTile.position,
Expand Down
4 changes: 1 addition & 3 deletions core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,8 @@ object UnitActions {
}

private fun addWaitAction(unit: MapUnit, actionList: ArrayList<UnitAction>, worldScreen: WorldScreen) {
// This is only for idle units.
if (!unit.isIdle()) return
// Don't add if there are no idle units we could switch to,
if (!worldScreen.viewingCiv.getDueUnits().any()) return
if (worldScreen.viewingCiv.getDueUnits().filter { it != unit }.none()) return
actionList += UnitAction(
type = UnitActionType.Wait,
action = {
Expand Down

0 comments on commit 93d9fe9

Please sign in to comment.