From 780c4744cc26e3c12364c5cab0040055e375f8e7 Mon Sep 17 00:00:00 2001 From: SeventhM <127357473+SeventhM@users.noreply.github.com> Date: Thu, 25 Jan 2024 13:25:41 -0800 Subject: [PATCH] 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 --- .../civilization/NextTurnAutomation.kt | 31 ++++++++++++ .../managers/GreatPersonManager.kt | 16 +++++++ .../ruleset/unique/UniqueTriggerActivation.kt | 47 +++++-------------- .../com/unciv/ui/components/MayaCalendar.kt | 11 ++--- 4 files changed, 63 insertions(+), 42 deletions(-) diff --git a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt index b345c1fb2d..77bb465622 100644 --- a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt +++ b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt @@ -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) { diff --git a/core/src/com/unciv/logic/civilization/managers/GreatPersonManager.kt b/core/src/com/unciv/logic/civilization/managers/GreatPersonManager.kt index 22a983c830..f15f704bfa 100644 --- a/core/src/com/unciv/logic/civilization/managers/GreatPersonManager.kt +++ b/core/src/com/unciv/logic/civilization/managers/GreatPersonManager.kt @@ -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 { val greatPeople = civInfo.gameInfo.ruleset.units.values.asSequence() diff --git a/core/src/com/unciv/models/ruleset/unique/UniqueTriggerActivation.kt b/core/src/com/unciv/models/ruleset/unique/UniqueTriggerActivation.kt index 0ec01d01d4..691c12763e 100644 --- a/core/src/com/unciv/models/ruleset/unique/UniqueTriggerActivation.kt +++ b/core/src/com/unciv/models/ruleset/unique/UniqueTriggerActivation.kt @@ -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 -> { diff --git a/core/src/com/unciv/ui/components/MayaCalendar.kt b/core/src/com/unciv/ui/components/MayaCalendar.kt index 954f596d0e..03c6a77575 100644 --- a/core/src/com/unciv/ui/components/MayaCalendar.kt +++ b/core/src/com/unciv/ui/components/MayaCalendar.kt @@ -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