Part 1 of "generic Great Person points" - changed data type serialized in save to be stat-agnostic

This commit is contained in:
Yair Morgenstern
2021-07-23 13:55:08 +03:00
parent 6791dd3340
commit cbfd658480
4 changed files with 46 additions and 14 deletions

View File

@ -316,6 +316,13 @@ class GameInfo {
cityInfo.cityStats.update() cityInfo.cityStats.update()
} }
if(!civInfo.greatPeople.greatPersonPoints.isEmpty()) {
civInfo.greatPeople.greatPersonPointsCounter.add(
GreatPersonManager.statsToGreatPersonCounter(civInfo.greatPeople.greatPersonPoints)
)
civInfo.greatPeople.greatPersonPoints.clear()
}
} }
} }

View File

@ -1,12 +1,16 @@
package com.unciv.logic.civilization package com.unciv.logic.civilization
import com.unciv.models.Counter
import com.unciv.models.stats.Stat import com.unciv.models.stats.Stat
import com.unciv.models.stats.Stats import com.unciv.models.stats.Stats
class GreatPersonManager { class GreatPersonManager {
var pointsForNextGreatPerson = 100 var pointsForNextGreatPerson = 100
var pointsForNextGreatGeneral = 30 var pointsForNextGreatGeneral = 30
@Deprecated("As of 3.15.15 - Should be converted to greatPersonPointsCounter")
var greatPersonPoints = Stats() var greatPersonPoints = Stats()
var greatPersonPointsCounter = Counter<String>()
var greatGeneralPoints = 0 var greatGeneralPoints = 0
var freeGreatPeople = 0 var freeGreatPeople = 0
@ -17,12 +21,29 @@ class GreatPersonManager {
Stat.Gold to "Great Merchant", Stat.Gold to "Great Merchant",
Stat.Culture to "Great Artist", Stat.Culture to "Great Artist",
) )
fun statsToGreatPersonCounter(stats: Stats): Counter<String> {
val counter = Counter<String>()
for ((key, value) in stats.toHashMap())
if (statToGreatPersonMapping.containsKey(key))
counter.add(statToGreatPersonMapping[key]!!, value.toInt())
return counter
}
fun greatPersonCounterToStats(counter: Counter<String>): 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 { fun clone(): GreatPersonManager {
val toReturn = GreatPersonManager() val toReturn = GreatPersonManager()
toReturn.freeGreatPeople = freeGreatPeople toReturn.freeGreatPeople = freeGreatPeople
toReturn.greatPersonPoints = greatPersonPoints.clone() toReturn.greatPersonPointsCounter = greatPersonPointsCounter.clone()
toReturn.pointsForNextGreatPerson = pointsForNextGreatPerson toReturn.pointsForNextGreatPerson = pointsForNextGreatPerson
toReturn.pointsForNextGreatGeneral = pointsForNextGreatGeneral toReturn.pointsForNextGreatGeneral = pointsForNextGreatGeneral
toReturn.greatGeneralPoints = greatGeneralPoints toReturn.greatGeneralPoints = greatGeneralPoints
@ -38,19 +59,19 @@ class GreatPersonManager {
return "Great General" return "Great General"
} }
val greatPersonPointsHashmap = greatPersonPoints.toHashMap() for ((key, value) in greatPersonPointsCounter) {
for (entry in statToGreatPersonMapping) { if (value > pointsForNextGreatPerson) {
if (greatPersonPointsHashmap[entry.key]!! > pointsForNextGreatPerson) { greatPersonPointsCounter.add(key, -pointsForNextGreatPerson)
greatPersonPoints.add(entry.key, -pointsForNextGreatPerson.toFloat())
pointsForNextGreatPerson *= 2 pointsForNextGreatPerson *= 2
return entry.value return key
} }
} }
return greatPerson return greatPerson
} }
fun addGreatPersonPoints(greatPersonPointsForTurn: Stats) { fun addGreatPersonPoints(greatPersonPointsForTurn: Stats) {
greatPersonPoints.add(greatPersonPointsForTurn) greatPersonPointsCounter.add(statsToGreatPersonCounter(greatPersonPointsForTurn))
} }
} }

View File

@ -19,15 +19,17 @@ open class Counter<K> : LinkedHashMap<K, Int>() {
} }
fun add(other: Counter<K>) { fun add(other: Counter<K>) {
for (key in other.keys) { for (key in other.keys) add(key, other[key]!!)
add(key, other[key]!!)
}
} }
fun remove(other: Counter<K>) { fun remove(other: Counter<K>) {
for (key in other.keys) { for (key in other.keys) add(key, -other[key]!!)
add(key, -other[key]!!) }
}
fun times(amount:Int): Counter<K> {
val newCounter = Counter<K>()
for (key in keys) newCounter[key] = this[key]!! * amount
return newCounter
} }
override fun clone(): Counter<K> { override fun clone(): Counter<K> {

View File

@ -105,7 +105,9 @@ class StatsOverviewTable (
private fun getGreatPeopleTable(): Table { private fun getGreatPeopleTable(): Table {
val greatPeopleTable = Table(CameraStageBaseScreen.skin) 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 greatPersonPointsPerTurn = viewingPlayer.getGreatPersonPointsForNextTurn().toHashMap()
val pointsToGreatPerson = viewingPlayer.greatPeople.pointsForNextGreatPerson val pointsToGreatPerson = viewingPlayer.greatPeople.pointsForNextGreatPerson