summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/garden/composter/ComposterAPI.kt
blob: a0044066799385e720245b3c884021ff39fb02fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package at.hannibal2.skyhanni.features.garden.composter

import at.hannibal2.skyhanni.data.model.ComposterUpgrade
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.utils.NumberUtil.formatLong
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.TimeUtils
import kotlin.math.floor
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes

object ComposterAPI {

    var tabListData = mapOf<ComposterDisplay.DataType, String>()
    val composterUpgrades: MutableMap<ComposterUpgrade, Int>? get() = GardenAPI.storage?.composterUpgrades

    fun ComposterUpgrade.getLevel(addOne: ComposterUpgrade?) =
        (composterUpgrades?.get(this) ?: 0) + if (addOne == this) 1 else 0

    fun estimateEmptyTimeFromTab(): Duration? {
        if (composterUpgrades.isNullOrEmpty()) {
            return null
        }

        val nextCompostTime = tabListData[ComposterDisplay.DataType.TIME_LEFT]?.removeColor()?.let {
            if (it != "INACTIVE") TimeUtils.getDuration(it) else null
        } ?: Duration.ZERO

        val timePerCompost = timePerCompost(null)
        val fractionRemaining = nextCompostTime / timePerCompost

        val remainingTimeByOrganicMatter = getDurationUntilEndOfResource(
            getOrganicMatter(), fractionRemaining, organicMatterRequiredPer(null), timePerCompost
        )

        val remainingTimeByFuel = getDurationUntilEndOfResource(
            getFuel(), fractionRemaining, fuelRequiredPer(null), timePerCompost
        )

        return nextCompostTime + minOf(remainingTimeByOrganicMatter, remainingTimeByFuel)
    }

    private fun getDurationUntilEndOfResource(
        amount: Long,
        fractionOfCompostRemaining: Double,
        requiredPer: Double,
        timePerCompost: Duration,
    ): Duration {
        val resourceConsumedByNextCompost = fractionOfCompostRemaining * requiredPer
        val resourceRemainingAfterNextCompostFinishes = amount - resourceConsumedByNextCompost
        val compostRemainingAfterNextCompostFinishes = floor(resourceRemainingAfterNextCompostFinishes / requiredPer)
        return timePerCompost * compostRemainingAfterNextCompostFinishes
    }

    fun getFuel() = tabListData[ComposterDisplay.DataType.FUEL]?.removeColor()?.formatLong() ?: 0

    fun getOrganicMatter() = tabListData[ComposterDisplay.DataType.ORGANIC_MATTER]?.removeColor()?.formatLong() ?: 0

    fun maxOrganicMatter(addOne: ComposterUpgrade?) =
        40_000 + ComposterUpgrade.ORGANIC_MATTER_CAP.getLevel(addOne) * 20_000

    fun multiDropChance(addOne: ComposterUpgrade?) = ComposterUpgrade.MULTI_DROP.getLevel(addOne) * 0.03

    fun maxFuel(addOne: ComposterUpgrade?) = 100_000 + ComposterUpgrade.FUEL_CAP.getLevel(addOne) * 30_000

    fun timePerCompost(addOne: ComposterUpgrade?): Duration {
        val speedUpgrade = ComposterUpgrade.COMPOSTER_SPEED.getLevel(addOne)
        val speedFactor = 1 + speedUpgrade * 0.2
        val baseDuration = 10.minutes
        return baseDuration / speedFactor
    }

    fun organicMatterRequiredPer(addOne: ComposterUpgrade?): Double {
        val costReduction = ComposterUpgrade.COST_REDUCTION.getLevel(addOne)
        val costFactor = 1.0 - costReduction.toDouble() / 100
        return 4_000.0 * costFactor
    }

    fun fuelRequiredPer(addOne: ComposterUpgrade?): Double {
        val costReduction = ComposterUpgrade.COST_REDUCTION.getLevel(addOne)
        val costFactor = 1.0 - costReduction.toDouble() / 100
        return 2_000.0 * costFactor
    }
}