Make CivilizationInfo.gold read-only (#4074)

* Make CivilizationInfo.gold read-only

* Make CivilizationInfo.gold read-only
This commit is contained in:
SomeTroglodyte 2021-06-08 05:40:17 +02:00 committed by GitHub
parent 8228d62d43
commit fadd1684ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 1 deletions

View File

@ -327,7 +327,7 @@ class CityConstructions {
if (construction is Building) { if (construction is Building) {
// Production put into wonders gets refunded // Production put into wonders gets refunded
if (construction.isWonder && getWorkDone(constructionName) != 0) { if (construction.isWonder && getWorkDone(constructionName) != 0) {
cityInfo.civInfo.gold += getWorkDone(constructionName) cityInfo.civInfo.addGold( getWorkDone(constructionName) )
val buildingIcon = "BuildingIcons/${constructionName}" val buildingIcon = "BuildingIcons/${constructionName}"
cityInfo.civInfo.addNotification("Excess production for [$constructionName] converted to [${getWorkDone(constructionName)}] gold", NotificationIcon.Gold, buildingIcon) cityInfo.civInfo.addNotification("Excess production for [$constructionName] converted to [${getWorkDone(constructionName)}] gold", NotificationIcon.Gold, buildingIcon)
} }

View File

@ -70,7 +70,9 @@ class CivilizationInfo {
/** Used in online multiplayer for human players */ /** Used in online multiplayer for human players */
var playerId = "" var playerId = ""
/** The Civ's gold reserves. Public get, private set - please use [addGold] method to modify. */
var gold = 0 var gold = 0
private set
var civName = "" var civName = ""
var tech = TechManager() var tech = TechManager()
var policies = PolicyManager() var policies = PolicyManager()
@ -543,7 +545,11 @@ class CivilizationInfo {
updateHasActiveGreatWall() updateHasActiveGreatWall()
} }
/** Modify gold by a given amount making sure it does neither overflow nor underflow.
* @param delta the amount to add (can be negative)
*/
fun addGold(delta: Int) { fun addGold(delta: Int) {
// not using Long.coerceIn - this stays in 32 bits
gold = when { gold = when {
delta > 0 && gold > Int.MAX_VALUE - delta -> Int.MAX_VALUE delta > 0 && gold > Int.MAX_VALUE - delta -> Int.MAX_VALUE
delta < 0 && gold < Int.MIN_VALUE - delta -> Int.MIN_VALUE delta < 0 && gold < Int.MIN_VALUE - delta -> Int.MIN_VALUE