mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-13 09:18:43 +07:00
* Implement a waiting command (#6884) * Resolve misc. issues with commit27a03bca8
* Resolve misc. issues with commit93d9fe9cc
* Resolve misc. issues with commit2ca7ed154
Co-authored-by: Paul Pogonyshev <pogonyshev@gmail.com>
This commit is contained in:
@ -74,6 +74,12 @@ class CivilizationInfo {
|
|||||||
@Transient
|
@Transient
|
||||||
private var units = listOf<MapUnit>()
|
private var units = listOf<MapUnit>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Index in the unit list above of the unit that is potentially due and is next up for button "Next unit".
|
||||||
|
*/
|
||||||
|
@Transient
|
||||||
|
private var nextPotentiallyDueAt = 0
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
var viewableTiles = setOf<TileInfo>()
|
var viewableTiles = setOf<TileInfo>()
|
||||||
|
|
||||||
@ -448,10 +454,19 @@ class CivilizationInfo {
|
|||||||
fun getCivUnits(): Sequence<MapUnit> = units.asSequence()
|
fun getCivUnits(): Sequence<MapUnit> = units.asSequence()
|
||||||
fun getCivGreatPeople(): Sequence<MapUnit> = getCivUnits().filter { mapUnit -> mapUnit.isGreatPerson() }
|
fun getCivGreatPeople(): Sequence<MapUnit> = getCivUnits().filter { mapUnit -> mapUnit.isGreatPerson() }
|
||||||
|
|
||||||
|
// Similar to getCivUnits(), but the returned list is rotated so that the
|
||||||
|
// 'nextPotentiallyDueAt' unit is first here.
|
||||||
|
private fun getCivUnitsStartingAtNextDue(): Sequence<MapUnit> = sequenceOf(units.subList(nextPotentiallyDueAt, units.size) + units.subList(0, nextPotentiallyDueAt)).flatten()
|
||||||
|
|
||||||
fun addUnit(mapUnit: MapUnit, updateCivInfo: Boolean = true) {
|
fun addUnit(mapUnit: MapUnit, updateCivInfo: Boolean = true) {
|
||||||
val newList = ArrayList(units)
|
// Since we create a new list anyway (otherwise some concurrent modification
|
||||||
|
// exception will happen), also rearrange existing units so that
|
||||||
|
// 'nextPotentiallyDueAt' becomes 0. This way new units are always last to be due
|
||||||
|
// (can be changed as wanted, just have a predictable place).
|
||||||
|
var newList = getCivUnitsStartingAtNextDue().toMutableList()
|
||||||
newList.add(mapUnit)
|
newList.add(mapUnit)
|
||||||
units = newList
|
units = newList
|
||||||
|
nextPotentiallyDueAt = 0
|
||||||
|
|
||||||
if (updateCivInfo) {
|
if (updateCivInfo) {
|
||||||
// Not relevant when updating TileInfo transients, since some info of the civ itself isn't yet available,
|
// Not relevant when updating TileInfo transients, since some info of the civ itself isn't yet available,
|
||||||
@ -462,27 +477,46 @@ class CivilizationInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun removeUnit(mapUnit: MapUnit) {
|
fun removeUnit(mapUnit: MapUnit) {
|
||||||
val newList = ArrayList(units)
|
// See comment in addUnit().
|
||||||
|
var newList = getCivUnitsStartingAtNextDue().toMutableList()
|
||||||
newList.remove(mapUnit)
|
newList.remove(mapUnit)
|
||||||
units = newList
|
units = newList
|
||||||
|
nextPotentiallyDueAt = 0
|
||||||
|
|
||||||
updateStatsForNextTurn() // unit upkeep
|
updateStatsForNextTurn() // unit upkeep
|
||||||
updateDetailedCivResources()
|
updateDetailedCivResources()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getIdleUnits() = getCivUnits().filter { it.isIdle() }
|
fun getIdleUnits() = getCivUnits().filter { it.isIdle() }
|
||||||
|
|
||||||
private fun getDueUnits() = getCivUnits().filter { it.due && it.isIdle() }
|
fun getDueUnits(): Sequence<MapUnit> = getCivUnitsStartingAtNextDue().filter { it.due && it.isIdle() }
|
||||||
|
|
||||||
fun shouldGoToDueUnit() = UncivGame.Current.settings.checkForDueUnits && getDueUnits().any()
|
fun shouldGoToDueUnit() = UncivGame.Current.settings.checkForDueUnits && getDueUnits().any()
|
||||||
|
|
||||||
fun getNextDueUnit(): MapUnit? {
|
// Return the next due unit, but preferably not 'unitToSkip': this is returned only if it is the only remaining due unit.
|
||||||
val dueUnits = getDueUnits()
|
fun cycleThroughDueUnits(unitToSkip: MapUnit? = null): MapUnit? {
|
||||||
if (dueUnits.any()) {
|
if (units.none()) return null
|
||||||
val unit = dueUnits.first()
|
|
||||||
unit.due = false
|
var returnAt = nextPotentiallyDueAt
|
||||||
return unit
|
var fallbackAt = -1
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (units[returnAt].due && units[returnAt].isIdle()) {
|
||||||
|
if (units[returnAt] != unitToSkip) {
|
||||||
|
nextPotentiallyDueAt = (returnAt + 1) % units.size
|
||||||
|
return units[returnAt]
|
||||||
}
|
}
|
||||||
return null
|
else fallbackAt = returnAt
|
||||||
|
}
|
||||||
|
|
||||||
|
returnAt = (returnAt + 1) % units.size
|
||||||
|
} while (returnAt != nextPotentiallyDueAt)
|
||||||
|
|
||||||
|
if (fallbackAt >= 0) {
|
||||||
|
nextPotentiallyDueAt = (fallbackAt + 1) % units.size
|
||||||
|
return units[fallbackAt]
|
||||||
|
}
|
||||||
|
else return null
|
||||||
}
|
}
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
|
@ -137,6 +137,8 @@ enum class UnitActionType(
|
|||||||
{ ImageGetter.getImage("OtherIcons/DisbandUnit") }, KeyCharAndCode.DEL),
|
{ ImageGetter.getImage("OtherIcons/DisbandUnit") }, KeyCharAndCode.DEL),
|
||||||
GiftUnit("Gift unit",
|
GiftUnit("Gift unit",
|
||||||
{ ImageGetter.getImage("OtherIcons/Present") }, UncivSound.Silent),
|
{ ImageGetter.getImage("OtherIcons/Present") }, UncivSound.Silent),
|
||||||
|
Wait("Wait",
|
||||||
|
null, 'z', UncivSound.Silent),
|
||||||
ShowAdditionalActions("Show more",
|
ShowAdditionalActions("Show more",
|
||||||
{ imageGetShowMore() }, KeyCharAndCode(Input.Keys.PAGE_DOWN)),
|
{ imageGetShowMore() }, KeyCharAndCode(Input.Keys.PAGE_DOWN)),
|
||||||
HideAdditionalActions("Back",
|
HideAdditionalActions("Back",
|
||||||
|
@ -187,7 +187,6 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Bas
|
|||||||
else
|
else
|
||||||
mapHolder.setCenterPosition(tileToCenterOn, immediately = true, selectUnit = true)
|
mapHolder.setCenterPosition(tileToCenterOn, immediately = true, selectUnit = true)
|
||||||
|
|
||||||
|
|
||||||
tutorialController.allTutorialsShowedCallback = { shouldUpdate = true }
|
tutorialController.allTutorialsShowedCallback = { shouldUpdate = true }
|
||||||
|
|
||||||
onBackButtonClicked { backButtonAndESCHandler() }
|
onBackButtonClicked { backButtonAndESCHandler() }
|
||||||
@ -723,6 +722,22 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Bas
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun switchToNextUnit() {
|
||||||
|
// 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,
|
||||||
|
immediately = false,
|
||||||
|
selectUnit = false
|
||||||
|
)
|
||||||
|
bottomUnitTable.selectUnit(nextDueUnit)
|
||||||
|
shouldUpdate = true
|
||||||
|
// Unless 'wait' action is chosen, the unit will not be considered due anymore.
|
||||||
|
nextDueUnit.due = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun updateNextTurnButton(isSomethingOpen: Boolean) {
|
private fun updateNextTurnButton(isSomethingOpen: Boolean) {
|
||||||
nextTurnButton.update(isSomethingOpen, isPlayersTurn, waitingForAutosave, getNextTurnAction())
|
nextTurnButton.update(isSomethingOpen, isPlayersTurn, waitingForAutosave, getNextTurnAction())
|
||||||
nextTurnButton.setPosition(stage.width - nextTurnButton.width - 10f, topBar.y - nextTurnButton.height - 10f)
|
nextTurnButton.setPosition(stage.width - nextTurnButton.width - 10f, topBar.y - nextTurnButton.height - 10f)
|
||||||
@ -737,18 +752,7 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Bas
|
|||||||
NextTurnAction("Waiting for other players...",Color.GRAY) {}
|
NextTurnAction("Waiting for other players...",Color.GRAY) {}
|
||||||
|
|
||||||
viewingCiv.shouldGoToDueUnit() ->
|
viewingCiv.shouldGoToDueUnit() ->
|
||||||
NextTurnAction("Next unit", Color.LIGHT_GRAY) {
|
NextTurnAction("Next unit", Color.LIGHT_GRAY) { switchToNextUnit() }
|
||||||
val nextDueUnit = viewingCiv.getNextDueUnit()
|
|
||||||
if (nextDueUnit != null) {
|
|
||||||
mapHolder.setCenterPosition(
|
|
||||||
nextDueUnit.currentTile.position,
|
|
||||||
immediately = false,
|
|
||||||
selectUnit = false
|
|
||||||
)
|
|
||||||
bottomUnitTable.selectUnit(nextDueUnit)
|
|
||||||
shouldUpdate = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
viewingCiv.cities.any { it.cityConstructions.currentConstructionFromQueue == "" } ->
|
viewingCiv.cities.any { it.cityConstructions.currentConstructionFromQueue == "" } ->
|
||||||
NextTurnAction("Pick construction", Color.CORAL) {
|
NextTurnAction("Pick construction", Color.CORAL) {
|
||||||
|
@ -68,6 +68,7 @@ object UnitActions {
|
|||||||
addTriggerUniqueActions(unit, actionList)
|
addTriggerUniqueActions(unit, actionList)
|
||||||
addAddInCapitalAction(unit, actionList, tile)
|
addAddInCapitalAction(unit, actionList, tile)
|
||||||
|
|
||||||
|
addWaitAction(unit, actionList, worldScreen);
|
||||||
|
|
||||||
addToggleActionsAction(unit, actionList, unitTable)
|
addToggleActionsAction(unit, actionList, unitTable)
|
||||||
|
|
||||||
@ -821,6 +822,18 @@ object UnitActions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun addWaitAction(unit: MapUnit, actionList: ArrayList<UnitAction>, worldScreen: WorldScreen) {
|
||||||
|
if (!unit.isIdle()) return
|
||||||
|
if (worldScreen.viewingCiv.getDueUnits().filter { it != unit }.none()) return
|
||||||
|
actionList += UnitAction(
|
||||||
|
type = UnitActionType.Wait,
|
||||||
|
action = {
|
||||||
|
unit.due = true
|
||||||
|
worldScreen.switchToNextUnit()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
private fun addToggleActionsAction(unit: MapUnit, actionList: ArrayList<UnitAction>, unitTable: UnitTable) {
|
private fun addToggleActionsAction(unit: MapUnit, actionList: ArrayList<UnitAction>, unitTable: UnitTable) {
|
||||||
actionList += UnitAction(
|
actionList += UnitAction(
|
||||||
type = if (unit.showAdditionalActions) UnitActionType.HideAdditionalActions
|
type = if (unit.showAdditionalActions) UnitActionType.HideAdditionalActions
|
||||||
@ -831,5 +844,4 @@ object UnitActions {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user