Skip to content

Commit ff78db9

Browse files
committed
reintroduce custom eye pos
1 parent 2ba5f58 commit ff78db9

File tree

7 files changed

+53
-48
lines changed

7 files changed

+53
-48
lines changed

src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ import kotlinx.coroutines.Dispatchers
3232
import kotlinx.coroutines.joinAll
3333
import kotlinx.coroutines.launch
3434
import kotlinx.coroutines.runBlocking
35+
import net.minecraft.util.math.Vec3d
3536

3637
object BuildSimulator : SimChecker<PostSimResult>() {
3738
context(automatedSafeContext: AutomatedSafeContext)
38-
fun Blueprint.simulate(): Set<BuildResult> = runBlocking(Dispatchers.Default) {
39+
fun Blueprint.simulate(pov: Vec3d = automatedSafeContext.player.eyePos): Set<BuildResult> = runBlocking(Dispatchers.Default) {
3940
val concurrentSet = ConcurrentSet<BuildResult>()
4041
with(automatedSafeContext) {
4142
structure.entries
@@ -45,6 +46,7 @@ object BuildSimulator : SimChecker<PostSimResult>() {
4546
pos,
4647
blockState(pos),
4748
targetState,
49+
pov,
4850
concurrentSet
4951
) ?: return@launch
5052
with(simInfo) {

src/main/kotlin/com/lambda/interaction/construction/simulation/ISimInfo.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ import com.lambda.interaction.construction.result.Dependable
2525
import com.lambda.interaction.construction.verify.TargetState
2626
import net.minecraft.block.BlockState
2727
import net.minecraft.util.math.BlockPos
28+
import net.minecraft.util.math.Vec3d
2829
import java.util.*
2930

3031
interface ISimInfo : Automated {
3132
val pos: BlockPos
3233
val state: BlockState
3334
val targetState: TargetState
3435
val preProcessing: PreProcessingInfo
36+
val pov: Vec3d
3537
val concurrentResults: MutableSet<BuildResult>
3638
val dependencyStack: Stack<Dependable>
3739

@@ -41,21 +43,23 @@ interface ISimInfo : Automated {
4143
pos: BlockPos,
4244
state: BlockState,
4345
targetState: TargetState,
46+
pov: Vec3d,
4447
concurrentResults: MutableSet<BuildResult>,
4548
): SimInfo? {
4649
val preProcessingInfo = targetState.getProcessingInfo(pos) ?: return null
47-
return SimInfo(pos, state, targetState, preProcessingInfo, concurrentResults, this)
50+
return SimInfo(pos, state, targetState, preProcessingInfo, pov, concurrentResults, this)
4851
}
4952

5053
@SimCheckerDsl
5154
fun ISimInfo.simInfo(
5255
pos: BlockPos = this.pos,
5356
state: BlockState = this.state,
5457
targetState: TargetState = this.targetState,
58+
pov: Vec3d = this.pov,
5559
concurrentResults: MutableSet<BuildResult> = this.concurrentResults
5660
): SimInfo? {
5761
val preProcessingInfo = targetState.getProcessingInfo(pos) ?: return null
58-
return SimInfo(pos, state, targetState, preProcessingInfo, concurrentResults, this)
62+
return SimInfo(pos, state, targetState, preProcessingInfo, pov, concurrentResults, this)
5963
}
6064
}
6165
}
@@ -65,6 +69,7 @@ data class SimInfo(
6569
override val state: BlockState,
6670
override val targetState: TargetState,
6771
override val preProcessing: PreProcessingInfo,
72+
override val pov: Vec3d,
6873
override val concurrentResults: MutableSet<BuildResult>,
6974
val automated: Automated
7075
) : ISimInfo, Automated by automated {

src/main/kotlin/com/lambda/interaction/construction/simulation/SimChecker.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,15 @@
1717

1818
package com.lambda.interaction.construction.simulation
1919

20-
import com.lambda.context.SafeContext
2120
import com.lambda.interaction.construction.result.BuildResult
2221
import com.lambda.interaction.construction.result.Dependable
2322
import com.lambda.interaction.construction.result.results.GenericResult
24-
import net.minecraft.util.math.Vec3d
2523

2624
@DslMarker
2725
annotation class SimCheckerDsl
2826

2927
@SimCheckerDsl
3028
abstract class SimChecker<T : BuildResult> {
31-
val SafeContext.eye: Vec3d get() = player.eyePos
32-
3329
protected fun ISimInfo.checkDependent(caller: Dependable?) {
3430
if (caller == null) {
3531
dependencyStack.clear()

src/main/kotlin/com/lambda/interaction/construction/simulation/Simulation.kt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,20 @@ data class Simulation(
4343
private val cache: MutableMap<FastVector, Set<BuildResult>> = mutableMapOf()
4444
private fun FastVector.toView(): Vec3d = toVec3d().add(0.5, ClientPlayerEntity.DEFAULT_EYE_HEIGHT.toDouble(), 0.5)
4545

46-
fun simulate(
47-
pos: FastVector,
48-
) = cache.getOrPut(pos) {
49-
val view = pos.toView()
50-
val isOutOfBounds = blueprint.isOutOfBounds(view)
51-
val isTooFar = blueprint.getClosestPointTo(view).distanceTo(view) > 10.0
52-
return@getOrPut runSafeAutomated {
53-
if (isOutOfBounds && isTooFar) return@getOrPut emptySet()
54-
val blockPos = pos.toBlockPos()
55-
val isWalkable = blockState(blockPos.down()).isSideSolidFullSquare(world, blockPos, Direction.UP)
56-
if (!isWalkable) return@getOrPut emptySet()
57-
if (!playerFitsIn(blockPos)) return@getOrPut emptySet()
58-
blueprint.simulate()
59-
} ?: emptySet()
60-
}
46+
fun simulate(pos: FastVector) =
47+
cache.getOrPut(pos) {
48+
val pov = pos.toView()
49+
val isOutOfBounds = blueprint.isOutOfBounds(pov)
50+
val isTooFar = blueprint.getClosestPointTo(pov).distanceTo(pov) > 10.0
51+
return@getOrPut runSafeAutomated {
52+
if (isOutOfBounds && isTooFar) return@getOrPut emptySet()
53+
val blockPos = pos.toBlockPos()
54+
val isWalkable = blockState(blockPos.down()).isSideSolidFullSquare(world, blockPos, Direction.UP)
55+
if (!isWalkable) return@getOrPut emptySet()
56+
if (!playerFitsIn(blockPos)) return@getOrPut emptySet()
57+
blueprint.simulate(pov)
58+
} ?: emptySet()
59+
}
6160

6261
fun goodPositions() = cache
6362
.filter { entry -> entry.value.any { it.rank.ordinal < 4 } }

src/main/kotlin/com/lambda/interaction/construction/simulation/checks/BreakChecker.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
163163
)
164164

165165
/* the player is buried inside the block */
166-
if (boxes.any { it.contains(eye) }) {
167-
val currentCast = RotationManager.activeRotation.rayCast(buildConfig.interactReach, eye)
166+
if (boxes.any { it.contains(pov) }) {
167+
val currentCast = RotationManager.activeRotation.rayCast(buildConfig.interactReach, pov)
168168
currentCast?.blockResult?.let { blockHit ->
169169
val rotationRequest = RotationRequest(lookAtBlock(pos), this)
170170
val breakContext = BreakContext(
@@ -189,21 +189,21 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
189189
boxes.map { box ->
190190
launch {
191191
val sides = if (buildConfig.checkSideVisibility)
192-
box.getVisibleSurfaces(eye)
192+
box.getVisibleSurfaces(pov)
193193
else Direction.entries.toSet()
194194

195195
scanSurfaces(box, sides, buildConfig.resolution) { side, vec ->
196-
if (eye distSq vec > reachSq) {
196+
if (pov distSq vec > reachSq) {
197197
misses.add(vec)
198198
return@scanSurfaces
199199
}
200200

201-
val newRotation = eye.rotationTo(vec)
201+
val newRotation = pov.rotationTo(vec)
202202

203203
val hit = if (buildConfig.strictRayCast) {
204-
newRotation.rayCast(buildConfig.interactReach, eye)?.blockResult
204+
newRotation.rayCast(buildConfig.interactReach, pov)?.blockResult
205205
} else {
206-
val hitVec = newRotation.castBox(box, buildConfig.interactReach, eye)
206+
val hitVec = newRotation.castBox(box, buildConfig.interactReach, pov)
207207
BlockHitResult(hitVec, side, pos, false)
208208
} ?: return@scanSurfaces
209209

@@ -218,11 +218,11 @@ class BreakChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
218218

219219
if (validHits.isEmpty()) {
220220
if (misses.isNotEmpty()) {
221-
result(GenericResult.OutOfReach(pos, eye, misses))
221+
result(GenericResult.OutOfReach(pos, pov, misses))
222222
return true
223223
}
224224

225-
result(GenericResult.NotVisible(pos, pos, eye.distanceTo(pos.vec3d)))
225+
result(GenericResult.NotVisible(pos, pos, pov.distanceTo(pos.vec3d)))
226226
return true
227227
}
228228

src/main/kotlin/com/lambda/interaction/construction/simulation/checks/PlaceChecker.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import com.lambda.util.BlockUtils.blockState
5151
import com.lambda.util.item.ItemStackUtils.inventoryIndex
5252
import com.lambda.util.item.ItemUtils.blockItem
5353
import com.lambda.util.math.distSq
54+
import com.lambda.util.math.minus
5455
import com.lambda.util.math.vec3d
5556
import com.lambda.util.player.MovementUtils.sneaking
5657
import com.lambda.util.player.copyPlayer
@@ -153,34 +154,36 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
153154

154155
// ToDo: For each hand
155156
val fakePlayer = copyPlayer(player).apply {
157+
val newPos = pov - (this.eyePos - this.pos)
158+
setPos(newPos.x, newPos.y, newPos.z)
156159
if (testBlockState.block::class in BlockUtils.interactionBlocks) {
157160
input.sneaking = true
158161
updatePose()
159162
}
160163
}
161164

162-
val eye = fakePlayer.eyePos
165+
val pov = fakePlayer.eyePos
163166

164167
withContext(Dispatchers.Default) {
165168
boxes.map { box ->
166169
launch {
167170
val sides = if (buildConfig.checkSideVisibility || buildConfig.strictRayCast) {
168-
box.getVisibleSurfaces(eye).intersect(setOf(side))
171+
box.getVisibleSurfaces(pov).intersect(setOf(side))
169172
} else setOf(side)
170173

171174
scanSurfaces(box, sides, buildConfig.resolution, preProcessing.surfaceScan) { _, vec ->
172-
val distSquared = eye distSq vec
175+
val distSquared = pov distSq vec
173176
if (distSquared > reachSq) {
174177
misses.add(vec)
175178
return@scanSurfaces
176179
}
177180

178-
val newRotation = eye.rotationTo(vec)
181+
val newRotation = pov.rotationTo(vec)
179182

180183
val hit = if (buildConfig.strictRayCast) {
181-
newRotation.rayCast(buildConfig.interactReach, eye)?.blockResult ?: return@scanSurfaces
184+
newRotation.rayCast(buildConfig.interactReach, pov)?.blockResult ?: return@scanSurfaces
182185
} else {
183-
val hitVec = newRotation.castBox(box, buildConfig.interactReach, eye) ?: return@scanSurfaces
186+
val hitVec = newRotation.castBox(box, buildConfig.interactReach, pov) ?: return@scanSurfaces
184187
BlockHitResult(hitVec, side, pos, false)
185188
}
186189

@@ -195,11 +198,11 @@ class PlaceChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
195198

196199
if (validHits.isEmpty()) {
197200
if (misses.isNotEmpty()) {
198-
result(GenericResult.OutOfReach(pos, eye, misses))
201+
result(GenericResult.OutOfReach(pos, pov, misses))
199202
return
200203
}
201204

202-
result(GenericResult.NotVisible(pos, pos, eye.distanceTo(pos.offset(side).vec3d)))
205+
result(GenericResult.NotVisible(pos, pos, pov.distanceTo(pos.offset(side).vec3d)))
203206
return
204207
}
205208

src/main/kotlin/com/lambda/interaction/construction/simulation/checks/PostProcessingChecker.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class PostProcessingChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
126126

127127
boxes.forEach { box ->
128128
val refinedSides = if (buildConfig.checkSideVisibility) {
129-
box.getVisibleSurfaces(eye).let { visibleSides ->
129+
box.getVisibleSurfaces(pov).let { visibleSides ->
130130
sides?.let { specific ->
131131
visibleSides.intersect(specific)
132132
} ?: visibleSides.toSet()
@@ -139,29 +139,29 @@ class PostProcessingChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
139139
buildConfig.resolution,
140140
preProcessing.surfaceScan
141141
) { hitSide, vec ->
142-
val distSquared = eye distSq vec
142+
val distSquared = pov distSq vec
143143
if (distSquared > buildConfig.interactReach.pow(2)) {
144144
misses.add(vec)
145145
return@scanSurfaces
146146
}
147147

148-
val newRotation = eye.rotationTo(vec)
148+
val newRotation = pov.rotationTo(vec)
149149

150150
val hit = if (buildConfig.strictRayCast) {
151-
val rayCast = newRotation.rayCast(buildConfig.interactReach, eye)
151+
val rayCast = newRotation.rayCast(buildConfig.interactReach, pov)
152152
when {
153-
rayCast != null && (!airPlace || eye distSq rayCast.pos <= distSquared) ->
153+
rayCast != null && (!airPlace || pov distSq rayCast.pos <= distSquared) ->
154154
rayCast.blockResult
155155

156156
airPlace -> {
157-
val hitVec = newRotation.castBox(box, buildConfig.interactReach, eye)
157+
val hitVec = newRotation.castBox(box, buildConfig.interactReach, pov)
158158
BlockHitResult(hitVec, hitSide, pos, false)
159159
}
160160

161161
else -> null
162162
}
163163
} else {
164-
val hitVec = newRotation.castBox(box, buildConfig.interactReach, eye)
164+
val hitVec = newRotation.castBox(box, buildConfig.interactReach, pov)
165165
BlockHitResult(hitVec, hitSide, pos, false)
166166
} ?: return@scanSurfaces
167167

@@ -177,12 +177,12 @@ class PostProcessingChecker @SimCheckerDsl private constructor(simInfo: SimInfo)
177177

178178
if (validHits.isEmpty()) {
179179
if (misses.isNotEmpty()) {
180-
result(GenericResult.OutOfReach(pos, eye, misses))
180+
result(GenericResult.OutOfReach(pos, pov, misses))
181181
return
182182
}
183183

184184
//ToDo: Must clean up surface scan usage / renders. Added temporary direction until changes are made
185-
result(GenericResult.NotVisible(pos, pos, eye.distanceTo(pos.vec3d)))
185+
result(GenericResult.NotVisible(pos, pos, pov.distanceTo(pos.vec3d)))
186186
return
187187
}
188188

0 commit comments

Comments
 (0)