From 9800eb35ba92a63d57cff6655b07a5aef9a98a7b Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 4 Jan 2024 16:37:41 +0700 Subject: [PATCH] Fixed energy consumption not being merged properly --- .../mc/otm/container/UpgradeContainer.kt | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt index 35abc5839..9b178fa01 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/UpgradeContainer.kt @@ -16,32 +16,38 @@ open class UpgradeContainer(slotCount: Int, open val allowedUpgrades: Set get() = setOf() - protected fun decimals(fn: (IMatteryUpgrade) -> Decimal, reducer: (Decimal, Decimal) -> Decimal): Decimal { + protected fun positiveDecimals(fn: (IMatteryUpgrade) -> Decimal, reducer: (Decimal, Decimal) -> Decimal): Decimal { return iterator() .map { it.getCapability(MatteryCapability.UPGRADE).map(fn).orElse(Decimal.ZERO).moreThanZero() * it.count } .reduce(Decimal.ZERO, reducer) } + protected fun anyDecimals(fn: (IMatteryUpgrade) -> Decimal, reducer: (Decimal, Decimal) -> Decimal): Decimal { + return iterator() + .map { it.getCapability(MatteryCapability.UPGRADE).map(fn).orElse(Decimal.ZERO) * it.count } + .reduce(Decimal.ZERO, reducer) + } + override val speedBonus: Double get() = iterator().map { it.getCapability(MatteryCapability.UPGRADE).map { it.speedBonus }.orElse(0.0) * it.count }.reduce(0.0) { a, b -> a + b } override val processingItems: Int get() = iterator().map { it.getCapability(MatteryCapability.UPGRADE).map { it.processingItems }.orElse(0).coerceAtLeast(0) * it.count }.reduce(0) { a, b -> a + b } override val energyStorageFlat: Decimal - get() = decimals(IMatteryUpgrade::energyStorageFlat, Decimal::plus) + get() = positiveDecimals(IMatteryUpgrade::energyStorageFlat, Decimal::plus) override val energyStorage: Decimal - get() = decimals(IMatteryUpgrade::energyStorage, Decimal::plus) + get() = positiveDecimals(IMatteryUpgrade::energyStorage, Decimal::plus) override val matterStorageFlat: Decimal - get() = decimals(IMatteryUpgrade::matterStorageFlat, Decimal::plus) + get() = positiveDecimals(IMatteryUpgrade::matterStorageFlat, Decimal::plus) override val matterStorage: Decimal - get() = decimals(IMatteryUpgrade::matterStorage, Decimal::plus) + get() = positiveDecimals(IMatteryUpgrade::matterStorage, Decimal::plus) override val energyConsumed: Decimal - get() = decimals(IMatteryUpgrade::energyConsumed, Decimal::plus) + get() = anyDecimals(IMatteryUpgrade::energyConsumed, Decimal::plus) override val failureMultiplier: Double get() = iterator().map { it.getCapability(MatteryCapability.UPGRADE).map { it.failureMultiplier }.orElse(1.0).coerceAtLeast(0.0).pow(it.count.toDouble()) }.reduce(1.0) { a, b -> a * b } override val energyThroughputFlat: Decimal - get() = decimals(IMatteryUpgrade::energyThroughputFlat, Decimal::plus) + get() = positiveDecimals(IMatteryUpgrade::energyThroughputFlat, Decimal::plus) override val energyThroughput: Decimal - get() = decimals(IMatteryUpgrade::energyThroughput, Decimal::plus) + get() = positiveDecimals(IMatteryUpgrade::energyThroughput, Decimal::plus) fun transform(values: EnergyBalanceValues): EnergyBalanceValues { return object : EnergyBalanceValues {