Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ship = peripheral.find("warpdriveShipCore")
- local lasers = peripheral.getNames()
- local offsetDistance = 25
- -- Filter for warpdriveLaserCamera peripherals and set beam frequency.
- for i = #lasers, 1, -1 do
- if peripheral.getType(lasers[i]) ~= "warpdriveLaserCamera" then
- table.remove(lasers, i)
- else
- peripheral.wrap(lasers[i]).beamFrequency(1420)
- end
- end
- -- Get ship dimensions and status.
- ship_front, ship_right, ship_up = ship.dim_positive()
- ship_back, ship_left, ship_down = ship.dim_negative()
- ship_isInHyper = ship.isInHyperspace()
- ship_movement = { ship.movement() }
- ship_rotationSteps = ship.rotationSteps()
- print("Emit Scanning Laser to Jump Ship and Aligning the Mining Laser")
- while true do
- local event, laserName, lx, ly, lz, block, _, _, _, type, metadata, resistance = os.pullEvent()
- if event == "laserScanning" then
- -- Convert laser scanning coordinates to numbers.
- local lastLx = tonumber(lx)
- local lastLy = tonumber(ly)
- local lastLz = tonumber(lz)
- -- Get current ship position.
- local mx, my, mz = ship.getLocalPosition()
- print("Laser target: X:" .. lastLx .. " Y:" .. lastLy .. " Z:" .. lastLz)
- -- Calculate vector from laser target (L) to ship (M)
- local vx = mx - lastLx
- local vy = my - lastLy
- local vz = mz - lastLz
- local dist = math.sqrt(vx*vx + vy*vy + vz*vz)
- if dist > 0 then
- -- Normalize the vector.
- local ux = vx / dist
- local uy = vy / dist
- local uz = vz / dist
- -- Calculate dx, dy, dz as the point offsetDistance away from the laser target along the vector toward the ship.
- local dx = lastLx + ux * offsetDistance
- local dy = lastLy + uy * offsetDistance
- local dz = lastLz + uz * offsetDistance
- -- Round the coordinates to whole numbers.
- dx = math.floor(dx + 0.5)
- dy = math.floor(dy + 0.5)
- dz = math.floor(dz + 0.5)
- print("Calculated jump coordinates: (" .. dx .. ", " .. dy .. ", " .. dz .. ")")
- -- Retrieve ship orientation and compute minimum movement thresholds.
- local rx, ry, rz = ship.getOrientation()
- local minForwardBack = math.abs(ship_front + ship_back + 1)
- local minLeftRight = math.abs(ship_left + ship_right + 1)
- local minUpDown = math.abs(ship_up + ship_down + 1)
- -- Compute the movement difference between target jump point and ship.
- local jx = dx - mx
- local jy = dy - my
- local jz = dz - mz
- -- For horizontal movement, adjust based on ship orientation.
- local forwardBackMov = 0
- local leftRightMov = 0
- -- For vertical movement, use the raw difference.
- local upDownMov = jy
- if rx == 1 then
- forwardBackMov = jx
- leftRightMov = jz
- elseif rx == -1 then
- forwardBackMov = -jx
- leftRightMov = -jz
- elseif rz == 1 then
- forwardBackMov = jz
- leftRightMov = -jx
- elseif rz == -1 then
- forwardBackMov = -jz
- leftRightMov = jx
- end
- -- Check if movement is large enough in each axis.
- if math.abs(forwardBackMov) < minForwardBack and math.abs(upDownMov) < minUpDown and math.abs(leftRightMov) < minLeftRight then
- print("The movement is too small!")
- else
- ship.movement(forwardBackMov, upDownMov, leftRightMov)
- ship.rotationSteps(0)
- ship.command("MANUAL", true)
- end
- else
- print("Error: Ship and target are at the same location!")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement