Skip to content

Commit 8df56d8

Browse files
committed
VisibilityChecker documentation
1 parent 25c7d85 commit 8df56d8

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

common/src/main/kotlin/com/lambda/interaction/visibilty/VisibilityChecker.kt

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,20 @@ import net.minecraft.util.math.Vec3d
3939
import java.util.*
4040
import kotlin.math.pow
4141

42+
/**
43+
* Object for handling visibility checks, rotation calculations, and hit detection.
44+
*/
4245
object VisibilityChecker {
46+
47+
/**
48+
* Attempts to rotate the player to look at a specified entity.
49+
* The function calculates the best rotation to center the player's view on the bounding box of the given entity.
50+
*
51+
* @param rotationConfig Specifies the rotation configuration settings.
52+
* @param interactionConfig Specifies interaction settings, such as range and resolution.
53+
* @param entity The entity to be looked at.
54+
* @return A [RotationContext] if a valid rotation was found; otherwise, null.
55+
*/
4356
fun SafeContext.lookAtEntity(
4457
rotationConfig: IRotationConfig,
4558
interactionConfig: InteractionConfig,
@@ -48,6 +61,16 @@ object VisibilityChecker {
4861
entityResult?.entity == entity
4962
}
5063

64+
/**
65+
* Attempts to rotate the player to look at a specific block position.
66+
* The function computes the best rotation to focus on a target block's position and side.
67+
*
68+
* @param blockPos The position of the block to look at.
69+
* @param rotationConfig Specifies rotation configuration settings.
70+
* @param interactionConfig Specifies interaction settings, such as range and resolution.
71+
* @param sides Specifies the set of block sides to consider for targeting.
72+
* @return A [RotationContext] if a valid rotation was found; otherwise, null.
73+
*/
5174
fun SafeContext.lookAtBlock(
5275
blockPos: BlockPos,
5376
rotationConfig: IRotationConfig = TaskFlow.rotation,
@@ -62,6 +85,18 @@ object VisibilityChecker {
6285
}
6386
}
6487

88+
/**
89+
* Finds a rotation that intersects with one of the specified bounding boxes, allowing the player to look at entities or blocks.
90+
*
91+
* @param boxes List of bounding boxes for potential targets.
92+
* @param rotationConfig Specifies rotation configuration settings.
93+
* @param interact Specifies interaction settings, such as range and resolution.
94+
* @param sides Set of block sides to consider for targeting.
95+
* @param reach The maximum reach distance for the interaction.
96+
* @param eye The player's eye position.
97+
* @param verify A lambda to verify if a [HitResult] meets the desired criteria.
98+
* @return A [RotationContext] if a valid rotation was found; otherwise, null.
99+
*/
65100
fun SafeContext.findRotation(
66101
boxes: List<Box>,
67102
rotationConfig: IRotationConfig,
@@ -109,6 +144,16 @@ object VisibilityChecker {
109144
return null
110145
}
111146

147+
/**
148+
* Scans the visible surfaces of a given box, identifying points on each surface within a defined resolution.
149+
* The surface is subdivided to hits the corners of the pixels
150+
*
151+
* @param eyes The player's eye position.
152+
* @param box The bounding box of the target.
153+
* @param sides Set of block sides to consider for visibility.
154+
* @param resolution The number of points to sample along each axis of the box.
155+
* @param check A lambda to check each visible point for a hit.
156+
*/
112157
inline fun scanVisibleSurfaces(
113158
eyes: Vec3d,
114159
box: Box,
@@ -134,11 +179,20 @@ object VisibilityChecker {
134179
}
135180
}
136181

182+
/**
183+
* Computes the optimum point by averaging the vectors in a set of points.
184+
*/
137185
val Set<Vec3d>.optimum: Vec3d?
138186
get() = reduceOrNull { acc, vec3d ->
139187
acc.add(vec3d)
140188
}?.multiply(1.0 / size.toDouble())
141189

190+
/**
191+
* Gets the bounding coordinates of a box's side, specifying min and max values for each axis.
192+
*
193+
* @param side The side of the box to calculate bounds for.
194+
* @return An array of doubles representing the side's bounds.
195+
*/
142196
fun Box.bounds(side: Direction) =
143197
when (side) {
144198
Direction.DOWN -> doubleArrayOf(minX, minY, minZ, maxX, minY, maxZ)
@@ -149,12 +203,21 @@ object VisibilityChecker {
149203
Direction.EAST -> doubleArrayOf(maxX, minY, minZ, maxX, maxY, maxZ)
150204
}
151205

206+
/**
207+
* Determines which surfaces of the box are visible from a specific position, typically the player's eyes.
208+
*
209+
* @param eyes The position to determine visibility from.
210+
* @return A set of directions corresponding to visible sides.
211+
*/
152212
fun Box.getVisibleSurfaces(eyes: Vec3d) =
153213
EnumSet.noneOf(Direction::class.java)
154214
.checkAxis(eyes.x - center.x, lengthX / 2, Direction.WEST, Direction.EAST)
155215
.checkAxis(eyes.y - center.y, lengthY / 2, Direction.DOWN, Direction.UP)
156216
.checkAxis(eyes.z - center.z, lengthZ / 2, Direction.NORTH, Direction.SOUTH)
157217

218+
/**
219+
* Helper function to add visible sides to an EnumSet based on positional differences.
220+
*/
158221
private fun EnumSet<Direction>.checkAxis(
159222
diff: Double,
160223
limit: Double,

0 commit comments

Comments
 (0)