Skip to content

Commit 30a2dee

Browse files
committed
chore: update heuristic
1 parent 57e7f7a commit 30a2dee

1 file changed

Lines changed: 16 additions & 15 deletions

File tree

src/main/kotlin/org/cobalt/pathfinder/goal/GoalBlock.kt

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ class GoalBlock(
1414
) : Goal {
1515

1616
override fun heuristic(x: Int, y: Int, z: Int): Double {
17-
var heuristic = 0.0
17+
val dx = abs(x - goalX).toDouble()
18+
val dz = abs(z - goalZ).toDouble()
1819

19-
val xDiff = x - goalX
20-
val yDiff = y - goalY
21-
val zDiff = z - goalZ
20+
val diagonal = min(dx, dz)
21+
val straight = max(dx, dz) - diagonal
2222

23-
if (yDiff > 0) {
24-
heuristic += (ctx.costs.fallNBlocksCost[2] / 2.0) * yDiff
25-
} else if (yDiff < 0) {
26-
heuristic += -yDiff * ctx.costs.jumpOneBlockCost
27-
}
23+
val horizontal =
24+
diagonal * (ctx.costs.oneBlockWalkCost * SQRT_2) +
25+
straight * ctx.costs.oneBlockWalkCost
26+
27+
val dy = goalY - y
2828

29-
val absX = abs(xDiff.toDouble())
30-
val absZ = abs(zDiff.toDouble())
31-
val diagonal = min(absX, absZ)
32-
val straight = max(absX, absZ) - diagonal
29+
val vertical = when {
30+
dy > 0 -> dy * ctx.costs.jumpOneBlockCost
31+
dy < 0 -> -dy * (ctx.costs.fallNBlocksCost[1] * 0.5)
32+
else -> 0.0
33+
}
3334

34-
heuristic += (diagonal * SQRT_2 + straight) * 3.563
35-
return heuristic
35+
return horizontal + vertical
3636
}
3737

3838
override fun isAtGoal(x: Int, y: Int, z: Int): Boolean {
@@ -42,4 +42,5 @@ class GoalBlock(
4242
companion object {
4343
private val SQRT_2 = sqrt(2.0)
4444
}
45+
4546
}

0 commit comments

Comments
 (0)