-
Notifications
You must be signed in to change notification settings - Fork 41
Feat:Adds Line Lock Module #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PoTheMagicDragon
wants to merge
4
commits into
lambda-client:1.21.11
Choose a base branch
from
PoTheMagicDragon:line-lock
base: 1.21.11
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
165
src/main/kotlin/com/lambda/module/modules/player/LineLock.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| /* | ||
| * Copyright 2026 Lambda | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.lambda.module.modules.player | ||
|
|
||
| import com.lambda.config.ConfigEditor.editSetting | ||
| import com.lambda.config.ConfigEditor.hideAllExcept | ||
| import com.lambda.config.automation.AutomationConfig.Companion.setDefaultAutomationConfig | ||
| import com.lambda.config.withEdits | ||
| import com.lambda.context.SafeContext | ||
| import com.lambda.event.events.TickEvent | ||
| import com.lambda.event.listener.SafeListener.Companion.listen | ||
| import com.lambda.graphics.mc.renderer.ImmediateRenderer.Companion.immediateRenderer | ||
| import com.lambda.interaction.managers.rotating.IRotationRequest.Companion.rotationRequest | ||
| import com.lambda.interaction.managers.rotating.RotationMode | ||
| import com.lambda.module.Module | ||
| import com.lambda.module.tag.ModuleTag | ||
| import com.lambda.threading.runSafe | ||
| import net.minecraft.util.math.Vec3d | ||
| import java.awt.Color | ||
| import kotlin.math.abs | ||
| import kotlin.math.atan2 | ||
| import kotlin.math.cos | ||
| import kotlin.math.max | ||
| import kotlin.math.roundToInt | ||
| import kotlin.math.sin | ||
|
|
||
| @Suppress("unused") | ||
| object LineLock : Module( | ||
| name = "LineLock", | ||
| description = "Locks the player's yaw to a line and steers toward a point ahead on the line", | ||
| tag = ModuleTag.PLAYER, | ||
| ) { | ||
| private const val YAW_SNAP_INCREMENT = 45.0 // snap the axis to the nearest 45° heading | ||
| private const val YAW_SNAP_BIAS = 1.0 // nudge before rounding so exact half-steps snap consistently | ||
| private const val FULL_CIRCLE_DEGREES = 360.0 | ||
| private const val YAW_DEGREES_OFFSET = 90.0 // atan2 (east = 0°) to Minecraft yaw (south = 0°) | ||
| private const val MIN_LOOKAHEAD = 2.0 // floor for the look-ahead distance so steering stays stable at low speed | ||
| private const val ON_LINE_THRESHOLD = 0.1 // how close to the line counts as "on it" | ||
| private const val TOLERANCE = 0.1 // tolerance for classifying a direction as diagonal vs. straight | ||
| private const val RENDER_LINE_HALF_LENGTH = 500.0 | ||
|
|
||
| private val distance by setting("Distance", 5.0, 0.0..10.0, 0.1, "Look-ahead distance along the axis to steer toward") | ||
| private val renderLine by setting("Render Line", true, "Render the axis line the yaw is snapping to") | ||
| private val lineColor by setting("Line Color", Color(0, 255, 0, 255)) { renderLine } | ||
|
|
||
| private var startingYaw = 0.0 | ||
| private var lineOrigin: Vec3d = Vec3d.ZERO | ||
| private var lineDirection: Vec3d = Vec3d.ZERO | ||
|
|
||
| init { | ||
| setDefaultAutomationConfig() | ||
| .withEdits { | ||
| hideAllExcept(::rotationConfig) | ||
| rotationConfig::rotationMode.editSetting { defaultValue(RotationMode.Lock) } | ||
| } | ||
|
|
||
| // When enabled we lock onto a straight "rail" and steer the camera down it: | ||
| // | ||
| // lineOrigin ●─────────●──────────► lineDirection (the way the rail points) | ||
| // ╱ closestPoint (rail spot nearest you) | ||
| // you ●──╯ → we aim a bit further down the rail | ||
| // (lookaheadPoint) so you curve back on | ||
| onEnable { | ||
| // Snap our facing to the nearest 45° compass direction (N, NE, E, SE, ...) | ||
| // and lock the rail in from where we're standing. | ||
| val nearestNotch = ((player.yaw + YAW_SNAP_BIAS) / YAW_SNAP_INCREMENT).roundToInt() // which 45° notch we're closest to | ||
| startingYaw = nearestNotch * YAW_SNAP_INCREMENT % FULL_CIRCLE_DEGREES // back to degrees, kept in 0–360 | ||
| lineOrigin = player.pos | ||
| lineDirection = directionForHeading(startingYaw) | ||
| adjustYaw() | ||
| } | ||
|
|
||
| // Every tick: find the rail spot nearest you, pick a point a little ahead of it, | ||
| // and turn to face that point. | ||
| listen<TickEvent.Pre> { | ||
| if (player.velocity == Vec3d.ZERO) return@listen | ||
| adjustYaw() | ||
| } | ||
|
|
||
| immediateRenderer("LineLock Renderer") { | ||
| if (!renderLine) return@immediateRenderer | ||
| runSafe { | ||
| val playerPos = Vec3d(player.x, lineOrigin.y, player.z) | ||
| val closestPoint = closestPointOnLine(playerPos, lineOrigin, lineDirection) | ||
| val lineStart = closestPoint.subtract(lineDirection.multiply(RENDER_LINE_HALF_LENGTH)) | ||
| val lineEnd = closestPoint.add(lineDirection.multiply(RENDER_LINE_HALF_LENGTH)) | ||
| line(lineStart, lineEnd, lineColor) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun SafeContext.adjustYaw() { | ||
| val playerPos = Vec3d(player.x, lineOrigin.y, player.z) | ||
| val closestPoint = closestPointOnLine(playerPos, lineOrigin, lineDirection) | ||
| val distanceToLine = Vec3d(closestPoint.x, playerPos.y, closestPoint.z).distanceTo(playerPos) | ||
| val targetYaw = if (distanceToLine < ON_LINE_THRESHOLD) { | ||
| // Already on the line - hold the snapped axis yaw. | ||
| startingYaw | ||
| } else { | ||
| // Off the line - steer toward a point ahead so we converge back onto it. | ||
| yawTowardsLine(closestPoint) | ||
| } | ||
| rotationRequest { yaw(targetYaw) }.submit() | ||
| } | ||
|
|
||
| private fun SafeContext.yawTowardsLine(closestPoint: Vec3d): Double { | ||
| // Aim a little further down the rail than the nearest spot, so we curve back onto it. | ||
| val lookaheadPoint = closestPoint.add(lineDirection.multiply(max(MIN_LOOKAHEAD, player.velocity.length() * distance))) | ||
| return headingToward(lookaheadPoint) | ||
| } | ||
|
|
||
| // What compass heading (Minecraft yaw) points from the player toward this spot on the map? | ||
| private fun SafeContext.headingToward(point: Vec3d): Double { | ||
| val eastOffset = point.x - player.pos.x | ||
| val southOffset = point.z - player.pos.z | ||
| return Math.toDegrees(atan2(southOffset, eastOffset)) - YAW_DEGREES_OFFSET | ||
| } | ||
|
|
||
| // Turn a compass heading (degrees) into a unit arrow pointing that way on the map. | ||
| private fun directionForHeading(heading: Double): Vec3d { | ||
| val radians = Math.toRadians(heading) | ||
| return Vec3d(-sin(radians), 0.0, cos(radians)).normalize() | ||
| } | ||
|
|
||
| private fun closestPointOnLine(point: Vec3d, lineOrigin: Vec3d, lineDirection: Vec3d): Vec3d { | ||
| val originToPoint = point.subtract(lineOrigin) | ||
| val projection = originToPoint.dotProduct(lineDirection.normalize()) | ||
| return snapToAxis(lineOrigin.add(lineDirection.multiply(projection)), lineOrigin, lineDirection) | ||
| } | ||
|
|
||
| private fun snapToAxis(point: Vec3d, lineOrigin: Vec3d, lineDirection: Vec3d): Vec3d { | ||
| var offset = point.subtract(lineOrigin) | ||
|
|
||
| if (abs(abs(lineDirection.x) - abs(lineDirection.z)) < TOLERANCE) { // diagonal | ||
| val magnitude = (abs(offset.x) + abs(offset.z)) / 2 | ||
| val x = if (lineDirection.x > 0) magnitude else -magnitude | ||
| val z = if (lineDirection.z > 0) magnitude else -magnitude | ||
| offset = Vec3d(x, 0.0, z) | ||
| } else { // straight | ||
| val x = if (abs(lineDirection.x) < TOLERANCE) 0.0 else offset.x | ||
| val z = if (abs(lineDirection.z) < TOLERANCE) 0.0 else offset.z | ||
| if (x == 0.0) { | ||
| offset = Vec3d(0.0, 0.0, z) | ||
| } else if (z == 0.0) { | ||
| offset = Vec3d(x, 0.0, 0.0) | ||
| } | ||
| } | ||
| return lineOrigin.add(offset) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it'd be nice to have some of these as settings. For example, bounce efly has the step as a setting so you can ensure that no matter what angle the highway is built at, it will always be configurable to snap in the right direction
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which ones? I left a lot of these out to keep the interface simple so there's a shallow learning curve for using it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbh i think all the constants that could be configurable should be settings. Perhaps the simple settings would be at base level, and then you could have an Advanced group under then base settings for more niche changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the way you think