From 499b5e5b2fd3128c630907d63ffb9a020e00773c Mon Sep 17 00:00:00 2001 From: Xander Lenstra <71121390+xlenstra@users.noreply.github.com> Date: Tue, 1 Feb 2022 08:41:57 +0100 Subject: [PATCH] Possibly fixed a crash accessing gameInfo before it is initialized (#6062) * Possibly fixed a crash accessing gameInfo before it is initialized * Inlined an otherwise unused variable * Alternative version using an extra constructor instead of weird getters/setters --- .../logic/map/mapgenerator/MapRegions.kt | 2 +- .../com/unciv/logic/trade/TradeEvaluation.kt | 1 - core/src/com/unciv/logic/trade/TradeLogic.kt | 5 +-- core/src/com/unciv/logic/trade/TradeOffer.kt | 34 ++++++++----------- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/core/src/com/unciv/logic/map/mapgenerator/MapRegions.kt b/core/src/com/unciv/logic/map/mapgenerator/MapRegions.kt index 0c6a904921..ece0e94a7f 100644 --- a/core/src/com/unciv/logic/map/mapgenerator/MapRegions.kt +++ b/core/src/com/unciv/logic/map/mapgenerator/MapRegions.kt @@ -1367,7 +1367,7 @@ class MapRegions (val ruleset: Ruleset){ // Third add some minor deposits to land tiles // Note: In G&K there is a bug where minor deposits are never placed on hills. We're not replicating that. val frequency = (baseMinorDepositFrequency * bonusMultiplier).toInt() - val minorDepositsToAdd = (landList.count() / frequency) + 1 + val minorDepositsToAdd = (landList.count() / frequency) + 1 // I sometimes have division by zero errors on this line var minorDepositsAdded = 0 for (tile in landList) { if (tile.resource != null || tileData[tile.position]!!.impacts.containsKey(ImpactType.Strategic)) diff --git a/core/src/com/unciv/logic/trade/TradeEvaluation.kt b/core/src/com/unciv/logic/trade/TradeEvaluation.kt index 194222fb85..76c45e3395 100644 --- a/core/src/com/unciv/logic/trade/TradeEvaluation.kt +++ b/core/src/com/unciv/logic/trade/TradeEvaluation.kt @@ -8,7 +8,6 @@ import com.unciv.logic.civilization.CivilizationInfo import com.unciv.logic.civilization.diplomacy.RelationshipLevel import com.unciv.models.ruleset.ModOptionsConstants import com.unciv.models.ruleset.Ruleset -import com.unciv.models.ruleset.tile.ResourceType import com.unciv.models.ruleset.unique.UniqueType import com.unciv.ui.utils.toPercent import com.unciv.ui.victoryscreen.RankingType diff --git a/core/src/com/unciv/logic/trade/TradeLogic.kt b/core/src/com/unciv/logic/trade/TradeLogic.kt index 022cd89a79..6dd2da6e9c 100644 --- a/core/src/com/unciv/logic/trade/TradeLogic.kt +++ b/core/src/com/unciv/logic/trade/TradeLogic.kt @@ -28,8 +28,9 @@ class TradeLogic(val ourCivilization:CivilizationInfo, val otherCivilization: Ci } for (entry in civInfo.getCivResourcesWithOriginsForTrade() - .filterNot { it.resource.resourceType == ResourceType.Bonus } - .filter { it.origin == "Tradable" } ) { + .filterNot { it.resource.resourceType == ResourceType.Bonus } + .filter { it.origin == "Tradable" } + ) { val resourceTradeType = if (entry.resource.resourceType == ResourceType.Luxury) TradeType.Luxury_Resource else TradeType.Strategic_Resource offers.add(TradeOffer(entry.resource.name, resourceTradeType, entry.amount)) diff --git a/core/src/com/unciv/logic/trade/TradeOffer.kt b/core/src/com/unciv/logic/trade/TradeOffer.kt index 21a22f7421..a2ff854209 100644 --- a/core/src/com/unciv/logic/trade/TradeOffer.kt +++ b/core/src/com/unciv/logic/trade/TradeOffer.kt @@ -6,28 +6,24 @@ import com.unciv.models.metadata.GameSpeed import com.unciv.models.translations.tr import com.unciv.ui.utils.Fonts import com.unciv.logic.trade.TradeType.TradeTypeNumberType -import com.unciv.models.ruleset.tile.ResourceSupply -data class TradeOffer(val name:String, val type:TradeType, var amount:Int = 1, var duration: Int = -1) { +data class TradeOffer(val name: String, val type: TradeType, var amount: Int = 1, var duration: Int) { - init { - // Duration needs to be part of the variables defined in the primary constructor, - // so that it will be copied over with the automatically generated copy() - - duration = - if (type.isImmediate) -1 // -1 for offers that are immediate (e.g. gold transfer) - else { - // Do *not* access UncivGame.Current.gameInfo in the default constructor! - val gameSpeed = UncivGame.Current.gameInfo.gameParameters.gameSpeed - when { - name == Constants.peaceTreaty -> 10 - gameSpeed == GameSpeed.Quick -> 25 - else -> (30 * gameSpeed.modifier).toInt() - } - } + constructor( + name: String, + type: TradeType, + amount: Int = -1, + gameSpeed: GameSpeed = UncivGame.Current.gameInfo.gameParameters.gameSpeed + ) : this(name, type, amount, duration = -1) { + duration = when { + type.isImmediate -> -1 // -1 for offers that are immediate (e.g. gold transfer) + name == Constants.peaceTreaty -> 10 + gameSpeed == GameSpeed.Quick -> 25 + else -> (30 * gameSpeed.modifier).toInt() + } } - - constructor() : this("", TradeType.Gold) // so that the json deserializer can work + + constructor() : this("", TradeType.Gold, duration = -1) // so that the json deserializer can work @Suppress("CovariantEquals") // This is an overload, not an override of the built-in equals(Any?) fun equals(offer: TradeOffer): Boolean {