Fix errors when starting games as Maya (#11001)

* Split FreeGreatPerson and MayanGainGreatPerson

* Add null check to mayan great person

* Move to NextTurnAutomation to be easier to edit

* Move code fromUniqueTriggerActivation to GreatPersonManager

* Remove unnecessary civInfo.greatPeople
This commit is contained in:
SeventhM 2024-01-25 13:25:41 -08:00 committed by GitHub
parent 5f102b4159
commit 780c4744cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 63 additions and 42 deletions

View File

@ -310,6 +310,37 @@ object NextTurnAutomation {
}
}
fun chooseGreatPerson(civInfo: Civilization) {
if (civInfo.greatPeople.freeGreatPeople == 0) return
val mayanGreatPerson = civInfo.greatPeople.mayaLimitedFreeGP > 0
val greatPeople =
if (mayanGreatPerson)
civInfo.greatPeople.getGreatPeople().filter { it.name in civInfo.greatPeople.longCountGPPool }
else civInfo.greatPeople.getGreatPeople()
if (greatPeople.isEmpty()) return
var greatPerson = greatPeople.random()
if (civInfo.wantsToFocusOn(Victory.Focus.Culture)) {
val culturalGP =
greatPeople.firstOrNull { it.uniques.contains("Great Person - [Culture]") }
if (culturalGP != null) greatPerson = culturalGP
}
if (civInfo.wantsToFocusOn(Victory.Focus.Science)) {
val scientificGP =
greatPeople.firstOrNull { it.uniques.contains("Great Person - [Science]") }
if (scientificGP != null) greatPerson = scientificGP
}
civInfo.units.addUnit(greatPerson, civInfo.cities.firstOrNull { it.isCapital() })
civInfo.greatPeople.freeGreatPeople--
if (mayanGreatPerson){
civInfo.greatPeople.longCountGPPool.remove(greatPerson.name)
civInfo.greatPeople.mayaLimitedFreeGP--
}
}
/** If we are able to build a spaceship but have already spent our resources, try disbanding
* a unit and selling a building to make room. Can happen due to trades etc */
private fun freeUpSpaceResources(civInfo: Civilization) {

View File

@ -2,9 +2,12 @@ package com.unciv.logic.civilization.managers
import com.unciv.logic.IsPartOfGameInfoSerialization
import com.unciv.logic.civilization.Civilization
import com.unciv.logic.civilization.MayaLongCountAction
import com.unciv.logic.civilization.NotificationCategory
import com.unciv.models.Counter
import com.unciv.models.ruleset.unique.UniqueType
import com.unciv.models.ruleset.unit.BaseUnit
import com.unciv.ui.components.MayaCalendar
// todo: Great Admiral?
@ -85,6 +88,19 @@ class GreatPersonManager : IsPartOfGameInfoSerialization {
greatPersonPointsCounter.add(getGreatPersonPointsForNextTurn())
}
fun triggerMayanGreatPerson() {
if (civInfo.isSpectator()) return
val greatPeople = getGreatPeople()
if (longCountGPPool.isEmpty())
longCountGPPool = greatPeople.map { it.name }.toHashSet()
freeGreatPeople++
mayaLimitedFreeGP++
// Anyone an idea for a good icon?
val notification = "{A new b'ak'tun has just begun!}\n{A Great Person joins you!}"
civInfo.addNotification(notification, MayaLongCountAction(), NotificationCategory.General, MayaCalendar.notificationIcon)
}
fun getGreatPeople(): HashSet<BaseUnit> {
val greatPeople = civInfo.gameInfo.ruleset.units.values.asSequence()

View File

@ -3,13 +3,13 @@ package com.unciv.models.ruleset.unique
import com.badlogic.gdx.math.Vector2
import com.unciv.Constants
import com.unciv.UncivGame
import com.unciv.logic.automation.civilization.NextTurnAutomation
import com.unciv.logic.battle.MapUnitCombatant
import com.unciv.logic.city.City
import com.unciv.logic.civilization.CivFlags
import com.unciv.logic.civilization.Civilization
import com.unciv.logic.civilization.LocationAction
import com.unciv.logic.civilization.MapUnitAction
import com.unciv.logic.civilization.MayaLongCountAction
import com.unciv.logic.civilization.NotificationAction
import com.unciv.logic.civilization.NotificationCategory
import com.unciv.logic.civilization.NotificationIcon
@ -20,12 +20,10 @@ import com.unciv.logic.map.mapunit.MapUnit
import com.unciv.logic.map.tile.Tile
import com.unciv.models.UpgradeUnitAction
import com.unciv.models.ruleset.BeliefType
import com.unciv.models.ruleset.Victory
import com.unciv.models.stats.Stat
import com.unciv.models.stats.Stats
import com.unciv.models.translations.fillPlaceholders
import com.unciv.models.translations.hasPlaceholderParameters
import com.unciv.ui.components.MayaCalendar
import com.unciv.ui.components.extensions.addToMapOfSets
import com.unciv.ui.screens.worldscreen.unit.actions.UnitActionsUpgrade
import kotlin.math.roundToInt
@ -232,41 +230,18 @@ object UniqueTriggerActivation {
return true
}
UniqueType.OneTimeFreeGreatPerson, UniqueType.MayanGainGreatPerson -> {
UniqueType.OneTimeFreeGreatPerson -> {
if (civInfo.isSpectator()) return false
val greatPeople = civInfo.greatPeople.getGreatPeople()
if (unique.type == UniqueType.MayanGainGreatPerson && civInfo.greatPeople.longCountGPPool.isEmpty())
civInfo.greatPeople.longCountGPPool = greatPeople.map { it.name }.toHashSet()
if (civInfo.isHuman() && !UncivGame.Current.settings.autoPlay.isAutoPlayingAndFullAI()) {
civInfo.greatPeople.freeGreatPeople++
// Anyone an idea for a good icon?
if (unique.type == UniqueType.MayanGainGreatPerson) {
civInfo.greatPeople.mayaLimitedFreeGP++
civInfo.addNotification(notification!!, MayaLongCountAction(), NotificationCategory.General, MayaCalendar.notificationIcon)
} else if (notification != null)
civInfo.addNotification(notification, NotificationCategory.General)
return true
} else {
if (unique.type == UniqueType.MayanGainGreatPerson)
greatPeople.removeAll { it.name !in civInfo.greatPeople.longCountGPPool }
if (greatPeople.isEmpty()) return false
var greatPerson = greatPeople.random()
if (civInfo.wantsToFocusOn(Victory.Focus.Culture)) {
val culturalGP =
greatPeople.firstOrNull { it.uniques.contains("Great Person - [Culture]") }
if (culturalGP != null) greatPerson = culturalGP
}
if (civInfo.wantsToFocusOn(Victory.Focus.Science)) {
val scientificGP =
greatPeople.firstOrNull { it.uniques.contains("Great Person - [Science]") }
if (scientificGP != null) greatPerson = scientificGP
}
if (unique.type == UniqueType.MayanGainGreatPerson)
civInfo.greatPeople.longCountGPPool.remove(greatPerson.name)
return civInfo.units.addUnit(greatPerson.name, chosenCity) != null
civInfo.greatPeople.freeGreatPeople++
// Anyone an idea for a good icon?
if (notification != null)
civInfo.addNotification(notification, NotificationCategory.General)
if (civInfo.isAI() || UncivGame.Current.settings.autoPlay.isAutoPlayingAndFullAI()) {
NextTurnAutomation.chooseGreatPerson(civInfo)
}
return true
}
UniqueType.OneTimeGainPopulation -> {

View File

@ -2,6 +2,8 @@ package com.unciv.ui.components
import com.badlogic.gdx.graphics.Color
import com.unciv.Constants
import com.unciv.UncivGame
import com.unciv.logic.automation.civilization.NextTurnAutomation
import com.unciv.logic.civilization.Civilization
import com.unciv.models.ruleset.unique.UniqueTriggerActivation
import com.unciv.models.ruleset.unique.UniqueType
@ -73,12 +75,9 @@ object MayaCalendar {
val game = civInfo.gameInfo
val year = game.getYear()
if (!isNewCycle(year, game.getYear(-1))) return
for (unique in civInfo.getMatchingUniques(UniqueType.MayanGainGreatPerson)) {
UniqueTriggerActivation.triggerCivwideUnique(
unique, civInfo,
notification = "{A new b'ak'tun has just begun!}\n{A Great Person joins you!}"
)
}
civInfo.greatPeople.triggerMayanGreatPerson()
if (civInfo.isAI() || UncivGame.Current.settings.autoPlay.isAutoPlayingAndFullAI())
NextTurnAutomation.chooseGreatPerson(civInfo)
}
// User interface to explain changed year display