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
This commit is contained in:
Xander Lenstra
2022-02-01 08:41:57 +01:00
committed by GitHub
parent 16855f66f6
commit 499b5e5b2f
4 changed files with 19 additions and 23 deletions

View File

@ -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))

View File

@ -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

View File

@ -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))

View File

@ -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 {