From cbfd6584800d34844e0dac89653181c9e67c3817 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Fri, 23 Jul 2021 13:55:08 +0300 Subject: [PATCH] Part 1 of "generic Great Person points" - changed data type serialized in save to be stat-agnostic --- core/src/com/unciv/logic/GameInfo.kt | 7 ++++ .../logic/civilization/GreatPersonManager.kt | 35 +++++++++++++++---- core/src/com/unciv/models/Counter.kt | 14 ++++---- .../ui/overviewscreen/StatsOverviewTable.kt | 4 ++- 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/core/src/com/unciv/logic/GameInfo.kt b/core/src/com/unciv/logic/GameInfo.kt index e969f3196c..af23849fdf 100644 --- a/core/src/com/unciv/logic/GameInfo.kt +++ b/core/src/com/unciv/logic/GameInfo.kt @@ -316,6 +316,13 @@ class GameInfo { cityInfo.cityStats.update() } + + if(!civInfo.greatPeople.greatPersonPoints.isEmpty()) { + civInfo.greatPeople.greatPersonPointsCounter.add( + GreatPersonManager.statsToGreatPersonCounter(civInfo.greatPeople.greatPersonPoints) + ) + civInfo.greatPeople.greatPersonPoints.clear() + } } } diff --git a/core/src/com/unciv/logic/civilization/GreatPersonManager.kt b/core/src/com/unciv/logic/civilization/GreatPersonManager.kt index 1bb14c4d79..13ab4c9ae0 100644 --- a/core/src/com/unciv/logic/civilization/GreatPersonManager.kt +++ b/core/src/com/unciv/logic/civilization/GreatPersonManager.kt @@ -1,12 +1,16 @@ package com.unciv.logic.civilization +import com.unciv.models.Counter import com.unciv.models.stats.Stat import com.unciv.models.stats.Stats class GreatPersonManager { var pointsForNextGreatPerson = 100 var pointsForNextGreatGeneral = 30 + + @Deprecated("As of 3.15.15 - Should be converted to greatPersonPointsCounter") var greatPersonPoints = Stats() + var greatPersonPointsCounter = Counter() var greatGeneralPoints = 0 var freeGreatPeople = 0 @@ -17,12 +21,29 @@ class GreatPersonManager { Stat.Gold to "Great Merchant", Stat.Culture to "Great Artist", ) + + fun statsToGreatPersonCounter(stats: Stats): Counter { + val counter = Counter() + for ((key, value) in stats.toHashMap()) + if (statToGreatPersonMapping.containsKey(key)) + counter.add(statToGreatPersonMapping[key]!!, value.toInt()) + return counter + } + + fun greatPersonCounterToStats(counter: Counter): Stats { + val stats = Stats() + for ((key, value) in counter) { + val stat = statToGreatPersonMapping.entries.firstOrNull { it.value == key }?.key + if (stat != null) stats.add(stat, value.toFloat()) + } + return stats + } } fun clone(): GreatPersonManager { val toReturn = GreatPersonManager() toReturn.freeGreatPeople = freeGreatPeople - toReturn.greatPersonPoints = greatPersonPoints.clone() + toReturn.greatPersonPointsCounter = greatPersonPointsCounter.clone() toReturn.pointsForNextGreatPerson = pointsForNextGreatPerson toReturn.pointsForNextGreatGeneral = pointsForNextGreatGeneral toReturn.greatGeneralPoints = greatGeneralPoints @@ -38,19 +59,19 @@ class GreatPersonManager { return "Great General" } - val greatPersonPointsHashmap = greatPersonPoints.toHashMap() - for (entry in statToGreatPersonMapping) { - if (greatPersonPointsHashmap[entry.key]!! > pointsForNextGreatPerson) { - greatPersonPoints.add(entry.key, -pointsForNextGreatPerson.toFloat()) + for ((key, value) in greatPersonPointsCounter) { + if (value > pointsForNextGreatPerson) { + greatPersonPointsCounter.add(key, -pointsForNextGreatPerson) pointsForNextGreatPerson *= 2 - return entry.value + return key } } return greatPerson } fun addGreatPersonPoints(greatPersonPointsForTurn: Stats) { - greatPersonPoints.add(greatPersonPointsForTurn) + greatPersonPointsCounter.add(statsToGreatPersonCounter(greatPersonPointsForTurn)) } + } \ No newline at end of file diff --git a/core/src/com/unciv/models/Counter.kt b/core/src/com/unciv/models/Counter.kt index bad65ade8a..2e9f5e2c6a 100644 --- a/core/src/com/unciv/models/Counter.kt +++ b/core/src/com/unciv/models/Counter.kt @@ -19,15 +19,17 @@ open class Counter : LinkedHashMap() { } fun add(other: Counter) { - for (key in other.keys) { - add(key, other[key]!!) - } + for (key in other.keys) add(key, other[key]!!) } fun remove(other: Counter) { - for (key in other.keys) { - add(key, -other[key]!!) - } + for (key in other.keys) add(key, -other[key]!!) + } + + fun times(amount:Int): Counter { + val newCounter = Counter() + for (key in keys) newCounter[key] = this[key]!! * amount + return newCounter } override fun clone(): Counter { diff --git a/core/src/com/unciv/ui/overviewscreen/StatsOverviewTable.kt b/core/src/com/unciv/ui/overviewscreen/StatsOverviewTable.kt index 12a613ff38..688b160842 100644 --- a/core/src/com/unciv/ui/overviewscreen/StatsOverviewTable.kt +++ b/core/src/com/unciv/ui/overviewscreen/StatsOverviewTable.kt @@ -105,7 +105,9 @@ class StatsOverviewTable ( private fun getGreatPeopleTable(): Table { val greatPeopleTable = Table(CameraStageBaseScreen.skin) - val greatPersonPoints = viewingPlayer.greatPeople.greatPersonPoints.toHashMap() + val greatPersonPoints = GreatPersonManager + .greatPersonCounterToStats(viewingPlayer.greatPeople.greatPersonPointsCounter) + .toHashMap() val greatPersonPointsPerTurn = viewingPlayer.getGreatPersonPointsForNextTurn().toHashMap() val pointsToGreatPerson = viewingPlayer.greatPeople.pointsForNextGreatPerson