Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 3D-Offset-Driven Ship Jump & Mining-Laser Align
- -- Listens for { x, y, z } on Rednet protocol "3DOffsetBroadcast"
- -- and computes an offset jump point back toward the ship.
- -- 1) Configuration
- local offsetDistance = 25
- -- 2) Wrap your ship core
- local ship = peripheral.find("warpdriveShipCore")
- assert(ship, "No warpdriveShipCore detected!")
- -- 3) Pre-compute ship dimensions & status
- local ship_front, ship_right, ship_up = ship.dim_positive()
- local ship_back, ship_left, ship_down = ship.dim_negative()
- -- 4) Instructions
- print("Waiting for 3DOffsetBroadcast → Redstone 'front' must be ON to accept jumps")
- -- 5) Open Rednet on all modems
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- end
- end
- -- 6) Main loop: only react when redstone 'front' is high
- while true do
- if redstone.getInput("front") then
- -- wait up to 0.5s for a broadcast on 3DOffsetBroadcast
- local sender, message = rednet.receive("3DOffsetBroadcast", 0.5)
- if sender and message.x and message.y and message.z then
- local lastLx, lastLy, lastLz =
- tonumber(message.x),
- tonumber(message.y),
- tonumber(message.z)
- print(string.format(
- "Received target from %d → X:%d Y:%d Z:%d",
- sender, lastLx, lastLy, lastLz
- ))
- -- ship current position
- local mx, my, mz = ship.getLocalPosition()
- -- vector from laser target back toward ship
- local vx, vy, vz = mx - lastLx, my - lastLy, mz - lastLz
- local dist = math.sqrt(vx*vx + vy*vy + vz*vz)
- if dist > 0 then
- -- unit vector
- local ux, uy, uz = vx/dist, vy/dist, vz/dist
- -- offset point
- local dx = math.floor(lastLx + ux*offsetDistance + 0.5)
- local dy = math.floor(lastLy + uy*offsetDistance + 0.5)
- local dz = math.floor(lastLz + uz*offsetDistance + 0.5)
- print(string.format(
- "Jump coords → X:%d Y:%d Z:%d",
- dx, dy, dz
- ))
- -- orientation & thresholds
- local rx, ry, rz = ship.getOrientation()
- local minFB = math.abs(ship_front + ship_back + 1)
- local minLR = math.abs(ship_left + ship_right + 1)
- local minUD = math.abs(ship_up + ship_down + 1)
- -- desired move in world-space
- local jx, jy, jz = dx - mx, dy - my, dz - mz
- -- map to ship axes
- local fb, lr = 0, 0
- local ud = jy
- if rx == 1 then fb, lr = jx, jz
- elseif rx == -1 then fb, lr = -jx, -jz
- elseif rz == 1 then fb, lr = jz, -jx
- elseif rz == -1 then fb, lr = -jz, jx
- end
- -- threshold check
- if math.abs(fb) < minFB
- and math.abs(ud) < minUD
- and math.abs(lr) < minLR then
- print("Movement too small—skipping jump.")
- else
- ship.movement(fb, ud, lr)
- ship.rotationSteps(0)
- ship.command("MANUAL", true)
- end
- else
- print("Error: ship and target coincide—no movement.")
- end
- end
- else
- -- redstone is off, pause briefly
- os.sleep(0.1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment