Skip to content

Commit

Permalink
refactor: attempt to optimize Ice Path solver by removing bruteforce
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff committed Dec 14, 2023
1 parent 0a0dbf5 commit 0ccf0f4
Showing 1 changed file with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.awt.Color
import java.awt.Point
import java.util.*
import kotlin.math.abs

object IcePathSolver {
private val steps: MutableList<Point?> = ArrayList()
Expand All @@ -53,7 +54,7 @@ object IcePathSolver {
if (!Utils.inDungeons || !Skytils.config.icePathSolver || mc.thePlayer == null || "Ice Path" !in DungeonListener.missingPuzzles) return@tickTimer
if (silverfishChestPos != null && roomFacing != null) {
if (grid == null) {
grid = layout
grid = getGridLayout()
silverfishPos = getGridPointFromPos(silverfish!!.position)
steps.clear()
if (silverfishPos != null) {
Expand Down Expand Up @@ -140,47 +141,51 @@ object IcePathSolver {
}

private fun getVec3RelativeToGrid(column: Int, row: Int): Vec3? {
return if (silverfishChestPos == null || roomFacing == null) null else Vec3(
silverfishChestPos!!
.offset(roomFacing!!.opposite, 4)
.offset(roomFacing!!.rotateYCCW(), 8)
.offset(roomFacing!!.rotateY(), column)
.offset(roomFacing!!.opposite, row)
)
return silverfishChestPos?.let { chestPos ->
roomFacing?.let { facing ->
Vec3(chestPos.offset(facing.opposite, 4)
.offset(facing.rotateYCCW(), 8)
.offset(facing.rotateY(), column)
.offset(facing.opposite, row))
}
}
}

private fun getGridPointFromPos(pos: BlockPos): Point? {
if (silverfishChestPos == null || roomFacing == null) return null
for (row in 0..16) {
for (column in 0..16) {
if (BlockPos(getVec3RelativeToGrid(column, row)) == pos) {
return Point(column, row)
}
}
val topLeft = silverfishChestPos!!.offset(roomFacing!!.opposite, 4).offset(roomFacing!!.rotateYCCW(), 8)
val diff = pos.subtract(topLeft)

return Point(abs(diff.getValueOnAxis(roomFacing!!.rotateY().axis)), abs(diff.getValueOnAxis(roomFacing!!.opposite.axis)))
}

private fun BlockPos.getValueOnAxis(axis: EnumFacing.Axis): Int {
return when (axis) {
EnumFacing.Axis.X -> this.x
EnumFacing.Axis.Y -> this.y
EnumFacing.Axis.Z -> this.z
}
return null
}

private val layout: Array<IntArray>?
get() {
if (silverfishChestPos == null || roomFacing == null) return null
val grid = Array(17) { IntArray(17) }
for (row in 0..16) {
for (column in 0..16) {
grid[row][column] = if (mc.theWorld.getBlockState(
BlockPos(
getVec3RelativeToGrid(
column,
row
)
fun getGridLayout(): Array<IntArray>? {
if (silverfishChestPos == null || roomFacing == null) return null
val grid = Array(17) { IntArray(17) }
for (row in 0..16) {
for (column in 0..16) {
grid[row][column] = if (mc.theWorld.getBlockState(
BlockPos(
getVec3RelativeToGrid(
column,
row
)
).block !== Blocks.air
) 1 else 0
}
if (row == 16) return grid
)
).block !== Blocks.air
) 1 else 0
}
return null
if (row == 16) return grid
}
return null
}

/**
* This code was modified into returning an ArrayList and was taken under CC BY-SA 4.0
Expand Down

0 comments on commit 0ccf0f4

Please sign in to comment.