Considerably improve upgrade container performance
This commit is contained in:
parent
17dfa953db
commit
faba736bd7
@ -26,28 +26,62 @@ class UpgradeContainer(
|
|||||||
override val upgradeTypes: Set<UpgradeType>
|
override val upgradeTypes: Set<UpgradeType>
|
||||||
get() = setOf()
|
get() = setOf()
|
||||||
|
|
||||||
private fun positiveDecimals(fn: (IMatteryUpgrade) -> Decimal, reducer: (Decimal, Decimal) -> Decimal): Decimal {
|
private inline fun positiveDecimals(fn: (IMatteryUpgrade) -> Decimal, reducer: (Decimal, Decimal) -> Decimal): Decimal {
|
||||||
if (isEmpty)
|
if (isEmpty)
|
||||||
return Decimal.ZERO
|
return Decimal.ZERO
|
||||||
|
|
||||||
return iterator()
|
var result = Decimal.ZERO
|
||||||
.map { (it.getCapability(MatteryCapability.UPGRADE)?.let(fn) ?: Decimal.ZERO).moreThanZero() * it.count }
|
|
||||||
.reduce(Decimal.ZERO, reducer)
|
for (it in iterator()) {
|
||||||
|
val cap = it.getCapability(MatteryCapability.UPGRADE) ?: continue
|
||||||
|
val value = fn(cap)
|
||||||
|
|
||||||
|
if (value > Decimal.ZERO) {
|
||||||
|
if (it.count == 1) {
|
||||||
|
result = reducer(result, value )
|
||||||
|
} else {
|
||||||
|
result = reducer(result, value * it.count)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun anyDecimals(fn: (IMatteryUpgrade) -> Decimal, reducer: (Decimal, Decimal) -> Decimal): Decimal {
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
private inline fun anyDecimals(fn: (IMatteryUpgrade) -> Decimal, reducer: (Decimal, Decimal) -> Decimal): Decimal {
|
||||||
if (isEmpty)
|
if (isEmpty)
|
||||||
return Decimal.ZERO
|
return Decimal.ZERO
|
||||||
|
|
||||||
return iterator()
|
var result = Decimal.ZERO
|
||||||
.map { (it.getCapability(MatteryCapability.UPGRADE)?.let(fn) ?: Decimal.ZERO) * it.count }
|
|
||||||
.reduce(Decimal.ZERO, reducer)
|
for (it in iterator()) {
|
||||||
|
val cap = it.getCapability(MatteryCapability.UPGRADE) ?: continue
|
||||||
|
val value = fn(cap)
|
||||||
|
|
||||||
|
if (it.count == 1) {
|
||||||
|
result = reducer(result, value)
|
||||||
|
} else {
|
||||||
|
result = reducer(result, value * it.count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
override val speedBonus: Double get() {
|
||||||
|
if (isEmpty)
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
return sumOf { (it.getCapability(MatteryCapability.UPGRADE)?.speedBonus ?: 0.0) * it.count }
|
||||||
|
}
|
||||||
|
|
||||||
|
override val processingItems: Int get() {
|
||||||
|
if (isEmpty)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
return sumOf { (it.getCapability(MatteryCapability.UPGRADE)?.processingItems ?: 0).coerceAtLeast(0) * it.count }
|
||||||
}
|
}
|
||||||
|
|
||||||
override val speedBonus: Double
|
|
||||||
get() = if (isEmpty) 0.0 else iterator().map { (it.getCapability(MatteryCapability.UPGRADE)?.speedBonus ?: 0.0) * it.count }.reduce(0.0) { a, b -> a + b }
|
|
||||||
override val processingItems: Int
|
|
||||||
get() = if (isEmpty) 0 else iterator().map { (it.getCapability(MatteryCapability.UPGRADE)?.processingItems ?: 0).coerceAtLeast(0) * it.count }.reduce(0) { a, b -> a + b }
|
|
||||||
override val energyStorageFlat: Decimal
|
override val energyStorageFlat: Decimal
|
||||||
get() = positiveDecimals(IMatteryUpgrade::energyStorageFlat, Decimal::plus)
|
get() = positiveDecimals(IMatteryUpgrade::energyStorageFlat, Decimal::plus)
|
||||||
override val energyStorage: Decimal
|
override val energyStorage: Decimal
|
||||||
@ -58,8 +92,28 @@ class UpgradeContainer(
|
|||||||
get() = positiveDecimals(IMatteryUpgrade::matterStorage, Decimal::plus)
|
get() = positiveDecimals(IMatteryUpgrade::matterStorage, Decimal::plus)
|
||||||
override val energyConsumed: Decimal
|
override val energyConsumed: Decimal
|
||||||
get() = anyDecimals(IMatteryUpgrade::energyConsumed, Decimal::plus)
|
get() = anyDecimals(IMatteryUpgrade::energyConsumed, Decimal::plus)
|
||||||
override val failureMultiplier: Double
|
|
||||||
get() = if (isEmpty) 1.0 else iterator().map { (it.getCapability(MatteryCapability.UPGRADE)?.failureMultiplier ?: 1.0).coerceAtLeast(0.0).pow(it.count.toDouble()) }.reduce(1.0) { a, b -> a * b }
|
override val failureMultiplier: Double get() {
|
||||||
|
if (isEmpty)
|
||||||
|
return 1.0
|
||||||
|
|
||||||
|
var result = 1.0
|
||||||
|
|
||||||
|
for (it in iterator()) {
|
||||||
|
val cap = it.getCapability(MatteryCapability.UPGRADE) ?: continue
|
||||||
|
val chance = cap.failureMultiplier.coerceAtLeast(0.0)
|
||||||
|
|
||||||
|
if (chance == 0.0)
|
||||||
|
return 0.0
|
||||||
|
else if (it.count == 1)
|
||||||
|
result *= chance
|
||||||
|
else
|
||||||
|
result *= chance.pow(it.count)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
override val energyThroughputFlat: Decimal
|
override val energyThroughputFlat: Decimal
|
||||||
get() = positiveDecimals(IMatteryUpgrade::energyThroughputFlat, Decimal::plus)
|
get() = positiveDecimals(IMatteryUpgrade::energyThroughputFlat, Decimal::plus)
|
||||||
override val energyThroughput: Decimal
|
override val energyThroughput: Decimal
|
||||||
|
Loading…
Reference in New Issue
Block a user