Try to fix #5729 as concurrency issue. (#5862)

* Try to fix #5729 as concurrency issue.

* Oh, remove comment about crash, I guess.
This commit is contained in:
will-ca 2021-12-27 23:15:34 -08:00 committed by GitHub
parent 005c465ee4
commit f6d186445c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 21 deletions

View File

@ -35,27 +35,29 @@ class CivInfoStats(val civInfo: CivilizationInfo) {
// Handle unit maintenance discounts
// Have to capture global and per-unit
// Free Garrison already removed above from sequence
// Initialize maintenance cost per unit, default 1 aka 100%
// Initialize base maintenance cost empire-wide, default 1 aka 100%
// Then calculate per-unit discounrts
// Note all discounts are in the form of -X%, such as -25 for 25% reduction
for (unit in unitsToPayFor){
unit.maintenance = 1f
for (unique in unit.getMatchingUniques(UniqueType.UnitMaintenanceDiscount)){
unit.maintenance *= unique.params[0].toPercent()
}
}
// Apply global discounts
var civWideMaintenance = 1f
// Calculate global discounts
for (unique in civInfo.getMatchingUniques(UniqueType.UnitMaintenanceDiscountGlobal, StateForConditionals(civInfo))) {
for (unit in unitsToPayFor.filter { it.matchesFilter(unique.params[1]) }) {
unit.maintenance *= unique.params[0].toPercent()
}
civWideMaintenance *= unique.params[0].toPercent()
}
val costsToPay = ArrayList<Float>()
for (unit in unitsToPayFor) {
var unitMaintenance = civWideMaintenance
for (unique in unit.getMatchingUniques(UniqueType.UnitMaintenanceDiscount)){
unitMaintenance *= unique.params[0].toPercent()
}
costsToPay.add(unitMaintenance)
}
// Sort by descending maintenance, then drop most expensive X units to make them free
// If more free than units left, returns empty sequence
// There's something here that causes a bug and I'm not sure where, so let's try taking this apart piece by piece
// We tried toInt()ing, didn't help. Let's try converting to a final list before sorting.
unitsToPayFor = unitsToPayFor.toList().sortedByDescending { it.maintenance }.asSequence()
unitsToPayFor = unitsToPayFor.drop(freeUnits)
val numberOfUnitsToPayFor = max(0.0, unitsToPayFor.sumOf { it.maintenance.toDouble() }).toFloat()
// If more free than units left, runs sum on empty sequence
costsToPay.sortDescending()
val numberOfUnitsToPayFor = max(0.0, costsToPay.asSequence().drop(freeUnits).sumOf { it.toDouble() } ).toFloat()
val turnLimit =
BASE_GAME_DURATION_TURNS * civInfo.gameInfo.gameParameters.gameSpeed.modifier
@ -344,4 +346,4 @@ class CivInfoStats(val civInfo: CivilizationInfo) {
return statMap
}
}
}

View File

@ -43,9 +43,6 @@ class MapUnit {
@Transient
val movement = UnitMovementAlgorithms(this)
@Transient
var maintenance = 1f
@Transient
var isDestroyed = false