Skip to content

Commit

Permalink
scuffed "working" version
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunderblade73 committed Oct 19, 2024
1 parent 5d48ff1 commit fee26a2
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzRenderWorldEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzToolTipEvent
import at.hannibal2.skyhanni.events.ServerBlockChangeEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.BlockUtils.getBlockAt
Expand All @@ -13,7 +12,7 @@ import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.getSkullTexture
import at.hannibal2.skyhanni.utils.LocationUtils.distanceToPlayer
import at.hannibal2.skyhanni.utils.LorenzUtils.round
import at.hannibal2.skyhanni.utils.LorenzColor
import at.hannibal2.skyhanni.utils.LorenzVec
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RegexUtils.matches
Expand Down Expand Up @@ -100,6 +99,7 @@ object FruitDigging {
)

private fun LorenzVec.convertCords(): Pair<Int, Int> = add(112, 0, 11).let { it.x.toInt() to it.z.toInt() }
private fun Pair<Int, Int>.convertCords(): LorenzVec = LorenzVec(first - 112, 72, second - 11)

@SubscribeEvent
fun onBlockChange(event: ServerBlockChangeEvent) {
Expand Down Expand Up @@ -178,7 +178,7 @@ object FruitDigging {
//println(itemsOnGround)
//println(mineBlocks)
}
if (ticksSinceLastFound > (fruitStack.count { itemsOnGround[it]!!.type == DropType.WATERMELON } * 5 + 2) * 3) {
if (ticksSinceLastFound > (fruitStack.count { itemsOnGround[it]!!.type == DropType.WATERMELON } * 5 + 3) * 3) {
if (lastPos == fruitStack.firstOrNull()) {
fruitStack.clear()
}
Expand Down Expand Up @@ -216,7 +216,7 @@ object FruitDigging {
}
if (result != null) {
MyFruitDigging.onDig(
lastMined.last().convertCords(),
lastMined.get(lastMined.size - 1 - watermeloned.size).convertCords(),
drop,
if (rummed) null else MyFruitDigging.ShovelType.active,
result,
Expand Down Expand Up @@ -311,13 +311,7 @@ object FruitDigging {
}
}

@SubscribeEvent
fun onLorenzToolTip(event: LorenzToolTipEvent) {
if (event.itemStack != InventoryUtils.getItemInHand()) return
event.toolTip = MyFruitDigging.printBoard().replace("(\\[(?!\\[)[^\\]]*\\])".toRegex(), "$1\n").split("\n").toMutableList()
}

@SubscribeEvent
/* @SubscribeEvent
fun onRenderWorld(event: LorenzRenderWorldEvent) {
if (!isEnabled()) return
Expand Down Expand Up @@ -394,6 +388,48 @@ object FruitDigging {
if (text == null || color == null) return@forEach
event.drawWaypointFilled(loc, color, minimumAlpha = 0.5F)
event.drawDynamicText(loc.add(0.0, 1.5, 0.0), text, 1.0)
}
} */

@SubscribeEvent
fun onRenderWorld(event: LorenzRenderWorldEvent) {
if (!isEnabled()) return

for (info in MyFruitDigging.getBoardState()) {
val text: String
val color: Color
when {
info.isBombed() -> {
text = if (info.possibilities.size <= 2) info.fancyPrint() else ""
color = LorenzColor.GRAY.toColor()
}

!info.diggable -> {
text = info.fancyPrint()
color = config.uncovered.toColor()
}

info.isOnlyBombOrRum() -> {
text = info.fancyPrint()
color = config.mine.toColor()
}

info.isOnlyFruit() -> {
text = info.fancyPrint()
color = config.safe.toColor()
}

info.possibilities.size <= 3 -> {
text = info.fancyPrint()
color = LorenzColor.BLUE.toColor()
}

else -> continue
}
val loc = info.pos.convertCords()

event.drawWaypointFilled(loc, color, minimumAlpha = 0.5F)
event.drawDynamicText(loc.add(0.0, 1.5, 0.0), text, 1.0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ object MyFruitDigging {

var diggable = true

var bombed = false

init {
clear()
}
Expand All @@ -39,6 +41,7 @@ object MyFruitDigging {
),
)
diggable = true
bombed = false
}

fun setResult(drop: DropType) {
Expand All @@ -54,12 +57,117 @@ object MyFruitDigging {

fun setBombed() {
diggable = false
bombed = true
}

override fun toString(): String {
return (if (diggable) "" else "!") + possibilities.toString()
}

var area: AreaInfo? = null

fun isResolvedTo(drop: DropType): Boolean = diggable && possibilities.contains(drop)

fun fancyPrint() = possibilities.joinToString(" or ") { it.display }

fun isOnlyFruit() = possibilities.all { it.isFruit() }

fun isOnlyBombOrRum() = possibilities.all { it == DropType.BOMB || it == DropType.RUM }

fun isBombed() = bombed
}

interface AreaInfo {
fun interact(other: AreaInfo)
val pos: Pair<Int, Int>
val fields: Set<Pair<Int, Int>>
get() = directions.map { (it.first + pos.first) to (it.second + pos.second) }.toSet()

fun overlap(other: AreaInfo): Pair<Set<Pair<Int, Int>>, OverlapInfo> {
val myFields = fields
val otherFields = other.fields
val intersect = myFields.intersect(otherFields)
//val relativeIntersect = intersect.map { (it.first - pos.first) to (it.second - pos.second) }.toSet()
val relativePos = (other.pos.first - pos.first) to (other.pos.second - pos.second)
val info: OverlapInfo = OverlapInfo.entries.firstOrNull { it.relation.contains(relativePos) } ?: OverlapInfo.NONE
return intersect to info
}

enum class OverlapInfo(val relation: Set<Pair<Int, Int>>) {
CORNER(
setOf(
2 to 2,
-2 to -2,
2 to -2,
-2 to 2,
),
),
PARALLEL(
setOf(
2 to 0,
0 to 2,
-2 to 0,
0 to -2,
),
),
DIAGONAL(
setOf(
1 to 1,
-1 to -1,
1 to -1,
-1 to 1,
),
),
TWO(
setOf(
1 to 2,
-1 to -2,
1 to -2,
-1 to 2,
2 to 1,
-2 to -1,
2 to -1,
-2 to 1,
),
),
NEIGHBOUR(
setOf(
1 to 0,
0 to 1,
-1 to 0,
0 to -1,
),
),
NONE(setOf()),
}
}

private class NoFruitInfo(override val pos: Pair<Int, Int>) : AreaInfo {
override fun interact(other: AreaInfo) {
return
}
}

private class FruitInfo(override val pos: Pair<Int, Int>, val fruit: DropType) : AreaInfo {

val foundNearby get() = fields.any { board.getInfo(it).isResolvedTo(fruit) }

val possibleSpace get() = fields.filter { board.getInfo(it).possibilities.contains(fruit) }

override fun interact(other: AreaInfo) {
if (other !is FruitInfo) return
if (other.fruit != fruit) return
val (overlap, info) = overlap(other)
if (!foundNearby && overlap.size == 1 && board.getRemaining(fruit) == 1) {
board.setResolve(overlap.first(), fruit)
}
}
}

private class BombInfo(override val pos: Pair<Int, Int>, var bombGuess: Int) : AreaInfo {
override fun interact(other: AreaInfo) {
if (other is NoFruitInfo) return
}
}

enum class ShovelType {
Expand Down Expand Up @@ -94,10 +202,12 @@ object MyFruitDigging {

ChatUtils.chat("Digged $drop @${pos.first} ${pos.second} with $ability $result")
board.setResult(pos, drop)
val areaInfo: AreaInfo?
when (ability) {
ShovelType.MINES -> {
if (result !is Int) throw IllegalStateException("Expected Int as result type for MINES")
val neighbours = board.getNeighbors(pos)
areaInfo = BombInfo(pos, result)
if (result == 0) {
neighbours.forEach { it.possibilities.remove(DropType.BOMB) }
} else {
Expand All @@ -114,10 +224,13 @@ object MyFruitDigging {
result.first as? DropType ?: throw IllegalStateException("Expected Triple<DropType,Int,Int> as result type for ANCHOR")
val nPos = (result.second as? Int)?.let { f -> (result.third as? Int)?.let { f to it } }
?: throw IllegalStateException("Expected Triple<DropType,Int,Int> as result type for ANCHOR")
val neighbours = board.getNeighbors(nPos)
val neighbours = board.getNeighbors(pos)
if (item == DropType.NONE) {
areaInfo = NoFruitInfo(pos)
neighbours.forEach { it.possibilities.removeIf { it != DropType.RUM && it != DropType.BOMB } }
} else {
areaInfo = null
board.setResolve(nPos, item)
val notPossible = item.below
neighbours.forEach {
if (it.possibilities.size != 1) {
Expand All @@ -131,8 +244,10 @@ object MyFruitDigging {
if (result !is DropType) throw IllegalStateException("Expected DropType as result type for TREASURE")
val neighbours = board.getNeighbors(pos)
if (result == DropType.NONE) {
areaInfo = NoFruitInfo(pos)
neighbours.forEach { it.possibilities.removeIf { it != DropType.RUM && it != DropType.BOMB } }
} else {
areaInfo = FruitInfo(pos, result)
val amount = neighbours.count { it.possibilities.contains(drop) }
if (amount == 1) {
neighbours.first { it.possibilities.contains(drop) }.let {
Expand All @@ -149,8 +264,11 @@ object MyFruitDigging {
}
}

else -> {} // RUM
else -> {
areaInfo = null
} // RUM
}
board.setAreaInfo(pos, areaInfo)
println(board)
}

Expand All @@ -164,7 +282,7 @@ object MyFruitDigging {
board.setResult(pos, drop)
}

private val board = object {
private val board = object : Iterable<BlockInfo> {

private val GRID_SIZE = 7

Expand Down Expand Up @@ -201,6 +319,14 @@ object MyFruitDigging {
foundLogic(drop)
}

fun setAreaInfo(pos: Pair<Int, Int>, areaInfo: AreaInfo?) {
if (areaInfo == null) return
cells[pos.first][pos.second].area = areaInfo
getFarNeighbors(pos).forEach {
it.area?.interact(areaInfo)
}
}

private fun foundLogic(drop: DropType) {
found.addOrPut(drop, 1)
if (found[drop] == amountOnTheBoard[drop]) {
Expand All @@ -221,11 +347,22 @@ object MyFruitDigging {
fun getNeighbors(pos: Pair<Int, Int>): List<BlockInfo> =
directions.mapNotNull { cells.getOrNull(it.first + pos.first)?.getOrNull(it.second + pos.second) }

fun getFarNeighbors(pos: Pair<Int, Int>): List<BlockInfo> =
farDirections.mapNotNull { cells.getOrNull(it.first + pos.first)?.getOrNull(it.second + pos.second) }

override fun iterator(): Iterator<BlockInfo> = cells.flatten().iterator()

override fun toString(): String {
return "$found ${cells.contentDeepToString()}"
}

fun getInfo(pos: Pair<Int, Int>) = cells[pos.first][pos.second]

fun getRemaining(fruit: DropType) = amountOnTheBoard[fruit]?.minus(found[fruit] ?: 0) ?: 0
}

fun getBoardState() = board.iterator()

val directions = listOf(
1 to 0,
-1 to 0,
Expand All @@ -237,6 +374,26 @@ object MyFruitDigging {
-1 to -1,
)

val farDirections = listOf(
*directions.toTypedArray(),
2 to -2,
2 to -1,
2 to 0,
2 to 1,
2 to 2,
1 to 2,
0 to 2,
-1 to 2,
-2 to 2,
-2 to 1,
-2 to 0,
-2 to -1,
-2 to -2,
-1 to -2,
0 to -2,
1 to -2,
)

interface Strategy {
fun getNextBlock(): Pair<Int, Int>
}
Expand Down

0 comments on commit fee26a2

Please sign in to comment.