aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/features/nether/CrimsonMinibossRespawnTimer.kt
blob: 3c5325697dbb4733e88920498127b92b9815e9b0 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package at.hannibal2.skyhanni.features.nether

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.data.mob.MobData
import at.hannibal2.skyhanni.events.DebugDataCollectEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.SecondPassedEvent
import at.hannibal2.skyhanni.features.nether.CrimsonMinibossRespawnTimer.MiniBoss.Companion.isSpawned
import at.hannibal2.skyhanni.features.nether.CrimsonMinibossRespawnTimer.MiniBoss.Companion.isSpawningSoon
import at.hannibal2.skyhanni.features.nether.CrimsonMinibossRespawnTimer.MiniBoss.Companion.isTimerKnown
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.EntityUtils
import at.hannibal2.skyhanni.utils.LocationUtils.isInside
import at.hannibal2.skyhanni.utils.LocationUtils.isPlayerInside
import at.hannibal2.skyhanni.utils.LorenzUtils.isInIsland
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderable
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.TimeUtils.format
import at.hannibal2.skyhanni.utils.renderables.Renderable
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import at.hannibal2.skyhanni.utils.toLorenzVec
import net.minecraft.tileentity.TileEntityBeacon
import net.minecraft.util.AxisAlignedBB
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

@SkyHanniModule
object CrimsonMinibossRespawnTimer {

    private val config get() = SkyHanniMod.feature.crimsonIsle

    private val patternGroup = RepoPattern.group("crimson.miniboss")

    /**
     * REGEX-TEST: §c§lBEWARE - Bladesoul Is Spawning.
     */
    private val spawnPattern by patternGroup.pattern(
        "spawn",
        "§c§lBEWARE - (?<name>.+) Is Spawning\\.",
    )

    /**
     * REGEX-TEST: §f                            §r§6§lBLADESOUL DOWN!
     */
    private val downPattern by patternGroup.pattern(
        "down",
        "§f\\s*§r§6§l(?<name>.+) DOWN!",
    )

    private var currentAreaBoss: MiniBoss? = null

    private var display: Renderable? = null

    @SubscribeEvent
    fun onChat(event: LorenzChatEvent) {
        if (!isEnabled()) return
        val message = event.message
        downPattern.matchMatcher(message) {
            val miniBoss = MiniBoss.fromName(group("name")) ?: return
            miniBoss.nextSpawnTime = SimpleTimeMark.now() + 2.minutes
            miniBoss.spawned = false
            miniBoss.possibleSpawnTime = null
            miniBoss.foundBeacon = null
            update()
            return
        }
        spawnPattern.matchMatcher(message) {
            val miniBoss = MiniBoss.fromName(group("name")) ?: return
            miniBoss.spawned = true
            miniBoss.possibleSpawnTime = null
            miniBoss.foundBeacon = null
            update()
            return
        }
    }

    @SubscribeEvent
    fun onRenderOverlay(event: GuiRenderEvent.GuiOverlayRenderEvent) {
        if (!isEnabled()) return
        val renderable = display ?: drawDisplay()
        config.minibossTimerPosition.renderRenderable(renderable, posLabel = "Miniboss Timer")
    }

    @SubscribeEvent
    fun onSecondPassed(event: SecondPassedEvent) {
        if (!isEnabled()) return
        updateArea()
        update()
    }

    private fun updateArea() {
        MiniBoss.entries.forEach {
            if (it.lastSeenArea.passedSince() > 2.minutes) {
                it.nextSpawnTime = null
                it.possibleSpawnTime = null
                it.foundBeacon = null
                it.spawned = null
            }
        }
        currentAreaBoss = MiniBoss.entries.firstOrNull {
            it.area.isPlayerInside()
        }
        val now = SimpleTimeMark.now()
        currentAreaBoss?.lastSeenArea = now
        val boss = currentAreaBoss ?: return
        if (boss.isTimerKnown()) return

        val isBossInArea = MobData.skyblockMobs.filter {
            it.name == boss.displayName
        }.any { boss.area.isInside(it.baseEntity.position.toLorenzVec()) }
        if (isBossInArea) {
            boss.spawned = true
            boss.foundBeacon = null
            boss.possibleSpawnTime = null
            return
        }
        boss.spawned = false

        val isThereBeacon = EntityUtils.getAllTileEntities().filter { it is TileEntityBeacon }.any {
            boss.area.isInside(it.pos.toLorenzVec())
        }
        if (boss.foundBeacon == true && !isThereBeacon) {
            boss.foundBeacon = false
            boss.possibleSpawnTime = null
            boss.nextSpawnTime = now + 1.minutes
            return
        }
        if (boss.possibleSpawnTime != null) return
        if (isThereBeacon && boss.foundBeacon == null) {
            boss.foundBeacon = true
            boss.possibleSpawnTime = now + 1.minutes to now + 2.minutes
            return
        }
        if (!isThereBeacon && boss.foundBeacon == null) {
            boss.foundBeacon = false
            boss.possibleSpawnTime = now to now + 1.minutes
        }
    }

    private fun update() {
        display = drawDisplay()
    }

    private fun drawDisplay(): Renderable {
        val lines = MiniBoss.entries.map {
            val timer = it.nextSpawnTime
            val possibleTimer = it.possibleSpawnTime
            Renderable.string(
                buildString {
                    append("§b${it.displayName}: ")
                    if (it.isSpawned()) append("§aSPAWNED!")
                    else when {
                        it.isSpawningSoon() -> append("§6Soon!")
                        it.isTimerKnown() -> append("§e${timer?.timeUntil()?.format()}")
                        possibleTimer != null -> {
                            val (start, end) = possibleTimer
                            if (start.timeUntil().isNegative()) append("§e~Now - ")
                            else append("§e~${start.timeUntil().format()} - ")
                            if (end.timeUntil().isNegative()) append("§eNow")
                            else append("§e${end.timeUntil().format()}")
                        }
                        else -> append("§cUnknown")
                    }
                },
            )
        }
        return Renderable.verticalContainer(lines)
    }

    @SubscribeEvent
    fun onWorldChange(event: LorenzWorldChangeEvent) {
        MiniBoss.entries.forEach {
            it.nextSpawnTime = null
            it.possibleSpawnTime = null
            it.foundBeacon = null
            it.spawned = null
            it.lastSeenArea = SimpleTimeMark.farPast()
        }
        currentAreaBoss = null
    }

    @SubscribeEvent
    fun onDebug(event: DebugDataCollectEvent) {
        event.title("Crimson Isle Miniboss")
        event.addIrrelevant {
            if (!isEnabled()) {
                add("Feature is Disabled")
                return@addIrrelevant
            }
            add("Current Area Boss: ${currentAreaBoss?.displayName}")
            MiniBoss.entries.forEach {
                add("")
                add(it.displayName)
                add("   Timer ${it.nextSpawnTime?.timeUntil()?.format()}")
                add(
                    "   Possible Timer ${it.possibleSpawnTime?.first?.timeUntil()?.format()} - " + "${
                        it.possibleSpawnTime?.second?.timeUntil()?.format()
                    }",
                )
                add("   Found Beacon ${it.foundBeacon}")
                add("   Spawned ${it.spawned}")
                add("   Last Seen Area ${it.lastSeenArea.passedSince().format()}")
            }
        }
    }

    enum class MiniBoss(
        val displayName: String,
        val area: AxisAlignedBB,
        var nextSpawnTime: SimpleTimeMark? = null,
        var possibleSpawnTime: Pair<SimpleTimeMark, SimpleTimeMark>? = null,
        var foundBeacon: Boolean? = null,
        var spawned: Boolean? = null,
        var lastSeenArea: SimpleTimeMark = SimpleTimeMark.farPast(),
    ) {
        BLADESOUL(
            "Bladesoul",
            LorenzVec(-330, 80, -486).axisAlignedTo(LorenzVec(-257, 107, -545)),
        ),
        MAGE_OUTLAW(
            "Mage Outlaw",
            LorenzVec(-200, 98, -843).axisAlignedTo(LorenzVec(-162, 116, -878)),