Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Unified Ship Jump Controller
- -- - “3DOffsetBroadcast” → 3D-offset jump + mining-laser align
- -- - “HorizontalJumpBroadcast” → horizontal jump (ship position)
- -- - “HorizontalJumpMinerBroadcast” → horizontal jump (miner position)
- -- 1) Configuration
- local offsetDistance = 25
- local frontLever = "front"
- -- 2) Wrap peripherals
- local ship = peripheral.find("warpdriveShipCore")
- assert(ship, "No warpdriveShipCore found")
- local miner = peripheral.find("warpdriveMiningLaser")
- assert(miner, "No warpdriveMiningLaser found")
- -- 3) Precompute ship dims
- local ship_front, ship_right, ship_up = ship.dim_positive()
- local ship_back, ship_left, ship_down = ship.dim_negative()
- -- 4) Open rednet on every modem
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- rednet.open(side)
- end
- end
- print("Listening for 3DOffsetBroadcast, HorizontalJumpBroadcast, HorizontalJumpMinerBroadcast")
- print(" (front-side redstone must be ON to accept jumps)")
- -- 5) Helper: perform the 3D-offset jump logic
- local function do3DOffsetJump(x,y,z)
- local mx, my, mz = ship.getLocalPosition()
- local vx, vy, vz = mx - x, my - y, mz - z
- local dist = math.sqrt(vx*vx + vy*vy + vz*vz)
- if dist <= 0 then
- print("3DJump: target == ship; skipping")
- return
- end
- local ux, uy, uz = vx/dist, vy/dist, vz/dist
- local dx = math.floor(x + ux*offsetDistance + 0.5)
- local dy = math.floor(y + uy*offsetDistance + 0.5)
- local dz = math.floor(z + uz*offsetDistance + 0.5)
- print(string.format("3DJump coords → X:%d Y:%d Z:%d", dx,dy,dz))
- -- orient + thresholds
- local rx, _, 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)
- -- world-space desired move
- 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
- if math.abs(fb) < minFB and math.abs(ud) < minUD and math.abs(lr) < minLR then
- print("3DJump: Movement too small; skipping")
- else
- ship.movement(fb, ud, lr)
- ship.rotationSteps(0)
- ship.command("MANUAL", true)
- end
- end
- -- 6) Helper: perform the horizontal-only jump logic
- local function doHorizontalJump(x,y,z, useMiner)
- local mx, my, mz
- if useMiner then
- mx,my,mz = miner.getLocalPosition()
- else
- mx,my,mz = ship.getLocalPosition()
- end
- print(string.format(
- "%s → Target X:%d Y:%d Z:%d",
- useMiner and "MinerJump" or "ShipJump", x,y,z
- ))
- -- only forward/back + left/right
- local dx, dz = x - mx, z - mz
- local rx, _, rz = ship.getOrientation()
- local minFB = math.abs(ship_front + ship_back + 1)
- local minLR = math.abs(ship_left + ship_right + 1)
- local fb, lr = 0, 0
- if rx == 1 then fb,lr = dx, dz
- elseif rx == -1 then fb,lr = -dx, -dz
- elseif rz == 1 then fb,lr = dz, -dx
- elseif rz == -1 then fb,lr = -dz, dx
- end
- if math.abs(fb) < minFB and math.abs(lr) < minLR then
- print("HorizontalJump: Movement too small; skipping")
- else
- ship.movement(fb, 0, lr)
- ship.rotationSteps(0)
- ship.command("MANUAL", true)
- end
- end
- -- 7) Main event loop
- while true do
- if redstone.getInput(frontLever) then
- -- wait 0.5s for any protocol
- local sender, message, protocol = rednet.receive(nil, 0.5)
- if sender and message and message.x and message.y and message.z then
- local x,y,z = tonumber(message.x), tonumber(message.y), tonumber(message.z)
- if protocol == "3DOffsetBroadcast" then
- do3DOffsetJump(x,y,z)
- elseif protocol == "HorizontalJumpBroadcast" then
- doHorizontalJump(x,y,z, false)
- elseif protocol == "HorizontalJumpMinerBroadcast" then
- doHorizontalJump(x,y,z, true)
- else
- -- unknown protocol: ignore entirely
- end
- end
- else
- os.sleep(0.1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement