aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/profileviewer/hotm/HotmTreeScreen.kt
blob: c64b470645e48050fd29e819b4b6b8db0cda8bee (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
/*
 * Copyright (C) 2024 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.profileviewer.hotm

import com.google.gson.GsonBuilder
import com.google.gson.JsonElement
import io.github.moulberry.notenoughupdates.NotEnoughUpdates
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
import io.github.moulberry.notenoughupdates.core.GlScissorStack
import io.github.moulberry.notenoughupdates.core.util.StringUtils
import io.github.moulberry.notenoughupdates.events.RepositoryReloadEvent
import io.github.moulberry.notenoughupdates.util.ItemUtils
import io.github.moulberry.notenoughupdates.util.Utils
import io.github.moulberry.notenoughupdates.util.kotlin.KotlinTypeAdapterFactory
import moe.nea.lisp.*
import moe.nea.lisp.bind.AutoBinder
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.ScaledResolution
import net.minecraft.init.Items
import net.minecraft.item.ItemStack
import net.minecraft.util.ResourceLocation
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.math.floor
import kotlin.math.roundToInt

class HotmTreeRenderer(val hotmLayout: HotmTreeLayout, val prelude: List<String>) {
    val lec = LispExecutionContext()

    init {
        lec.setupStandardBindings()
        AutoBinder().bindTo(ExtraLispMethods(), lec.rootStackFrame)
        prelude.forEachIndexed { index, it ->
            lec.executeProgram(lec.rootStackFrame, LispParser.parse("hotmlayout.json:prelude[$index]", it))
        }
    }

    @NEUAutoSubscribe
    companion object {

        val gson = GsonBuilder()
            .registerTypeAdapterFactory(KotlinTypeAdapterFactory)
            .registerTypeAdapterFactory(LoreLineSerializer)
            .registerTypeAdapterFactory(LispProgramSerializer)
            .create()

        var renderer: HotmTreeRenderer? = null

        @SubscribeEvent
        fun onRepoReload(event: RepositoryReloadEvent) {
            renderer = runCatching {
                val hotmLayoutFile = NotEnoughUpdates.INSTANCE.manager.repoLocation
                    .resolve("constants/hotmlayout.json")
                val hotmLayout = gson.fromJson(hotmLayoutFile.readText(), HotmTreeLayoutFile::class.java)
                HotmTreeRenderer(hotmLayout.hotm, hotmLayout.prelude)
            }.onFailure {
                Utils.showOutdatedRepoNotification("constants/hotmlayout.json", it)
            }.getOrNull()
        }

        val perkBackground = ResourceLocation("notenoughupdates:profile_viewer/mining/perk_background.png")
        val perkConnectionX = ResourceLocation("notenoughupdates:profile_viewer/mining/perk_connection_x.png")
        val perkConnectionY = ResourceLocation("notenoughupdates:profile_viewer/mining/perk_connection_y.png")
    }

    val gridNodes = hotmLayout.perks.map { (it.value.x to it.value.y) to it }.toMap()
    val ySize = hotmLayout.perks.maxOf { it.value.y }
    val xSize = hotmLayout.perks.maxOf { it.value.x }

    fun renderPerks(
        levels: Map<String, JsonElement>,
        x: Int, y: Int,
        mouseX: Int, mouseY: Int,
        renderTooltip: Boolean,
        gridSize: Int,
        gridSpacing: Int,
    ) {
        val sr = ScaledResolution(Minecraft.getMinecraft())
        val relX = mouseX - x
        val relY = mouseY - y
        val gridOffset = (gridSize - 16) / 2
        for ((key, perk) in hotmLayout.perks) {
            val level = levels[key]?.asInt ?: 0
            val (values, bindings) = calculatePerkProperties(perk, level, levels)
            val tooltip = createPerkTooltip(perk, level, values, bindings)
            val perkItem = getPerkItem(perk, level, values, bindings, tooltip) ?: ItemStack(Items.painting, 1, 10)
            Minecraft.getMinecraft().textureManager.bindTexture(perkBackground)
            Utils.drawTexturedRect(
                (perk.x * gridSize + x + gridSpacing / 2).toFloat(),
                (perk.y * gridSize + y + gridSpacing / 2).toFloat(),
                gridSize.toFloat() - gridSpacing, gridSize.toFloat() - gridSpacing,
                0F, 1f, 0f, 1f
            )
            if (Pair(perk.x - 1, perk.y) in gridNodes) {
                Minecraft.getMinecraft().textureManager.bindTexture(perkConnectionX)
                Utils.drawTexturedRect(
                    (perk.x * gridSize + x - gridSpacing / 2).toFloat(),
                    (perk.y * gridSize + y).toFloat(),
                    gridSpacing.toFloat(), gridSize.toFloat(),
                    0F, 1f, 0f, 1f
                )
            }
            if (Pair(perk.x, perk.y - 1) in gridNodes) {
                Minecraft.getMinecraft().textureManager.bindTexture(perkConnectionY)
                Utils.drawTexturedRect(
                    (perk.x * gridSize + x).toFloat(),
                    (perk.y * gridSize + y - gridSpacing / 2).toFloat(),
                    gridSize.toFloat(), gridSpacing.toFloat(),
                    0F, 1f, 0f, 1f
                )
            }
            if (renderTooltip &&
                relX in (perk.x * gridSize + gridSpacing / 2 .. perk.x * gridSize + gridSize - gridSpacing / 2) &&
                relY in (perk.y * gridSize + gridSpacing / 2 .. perk.y * gridSize + gridSize - gridSpacing / 2)) {
                GlScissorStack.disableTemporary()
                Utils.drawHoveringText(
                    tooltip,
                    mouseX, mouseY,
                    sr.scaledWidth, sr.scaledHeight, -1
                )
                GlScissorStack.refresh(sr)
            }
            Utils.drawItemStack(perkItem, perk.x * gridSize + x + gridOffset, perk.y * gridSize + y + gridOffset)
        }
    }

    fun getPerkItem(
        perk: LayoutedHotmPerk,
        level: Int,
        values: Map<String, LispData?>,
        bindings: StackFrame,
        tooltip: List<String>
    ): ItemStack? {
        val itemId = values["item"] ?: LispData.LispNil

        val repoId = when (itemId) {
            is LispData.Atom -> itemId.label
            is LispData.LispString -> itemId.string
            else -> return null
        }
        val item = NotEnoughUpdates.INSTANCE.manager.createItem(repoId)
        ItemUtils.setLore(item, tooltip)
        return item
    }

    fun calculatePerkProperties(
        perk: LayoutedHotmPerk,
        level: Int,
        levels: Map<String, JsonElement>
    ): Pair<Map<String, LispData?>, StackFrame> {
        val bindings = lec.genBindings()
        bindings.setValueLocal("potm", LispData.LispNumber((levels["special_0"]?.asInt ?: 0).toDouble()))
        bindings.setValueLocal("level", LispData.LispNumber(if (level == 0) 1.0 else level.toDouble()))
        bindings.setValueLocal("maxLevel", LispData.LispNumber(perk.maxLevel.toDouble()))
        bindings.setValueLocal("level0", LispData.LispNumber(level.toDouble()))
        val values = perk.compiledFunctions.mapValues {
            lec.executeProgram(bindings.fork(), it.value)
        }
        return values to bindings
    }

    private fun createPerkTooltip(
        perk: LayoutedHotmPerk,
        level: Int,
        values: Map<String, LispData?>,
        bindings: StackFrame
    ): List<String> {
        val perkTitle = when (level) {
            perk.maxLevel -> "§a${perk.name}"
            0 -> "§c${perk.name}"
            else -> "§e${perk.name}"
        }
        val begin =
            if (perk.maxLevel == 1) listOf(perkTitle)
            else listOf(
                perkTitle,
                if (level != perk.maxLevel) "§7Level $level§8/${perk.maxLevel}"
                else "§7Level $level",
                ""
            )
        val end: List<String> = if (level == 0 || level == perk.maxLevel) listOf() else listOf(
            "",
            hotmLayout.powders[(lec.executeProgram(bindings.fork().also {
                for (powder in hotmLayout.powders) {
                    it.setValueLoc