Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ship = peripheral.find("warpdriveShipCore")
- local lasers = peripheral.getNames()
- -- Filter for warpdriveLaserCameras and set their 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
- -- Define the offset distance (in blocks).
- local offsetDistance = 18
- -- Set invert to true if your testing shows the ship moves in the opposite direction
- -- than expected along the X and Z axes.
- local invert = true
- print("Auto Navigation System Online")
- print("Offset Distance = " .. offsetDistance)
- if invert then
- print("Using inverted offset calculation (moving to the opposite side from the ship).")
- else
- print("Using standard offset calculation (moving from target toward ship).")
- end
- while true do
- local event, laserName, lx, ly, lz = os.pullEvent("laserScanning")
- -- Convert target coordinates to numbers.
- local targetX = tonumber(lx)
- local targetY = tonumber(ly)
- local targetZ = tonumber(lz)
- print("LaserScanning event received:")
- print("Target: (" .. targetX .. ", " .. targetY .. ", " .. targetZ .. ")")
- -- Get current ship core position.
- local currentX, currentY, currentZ = ship.getLocalPosition()
- -- Calculate the vector from the target to the ship.
- local dx = currentX - targetX
- local dy = currentY - targetY
- local dz = currentZ - targetZ
- local distance = math.sqrt(dx * dx + dy * dy + dz * dz)
- if distance == 0 then
- print("Warning: The ship and target are at the same location. Skipping movement.")
- else
- -- Compute the unit vector from target to ship.
- local ux = dx / distance
- local uy = dy / distance
- local uz = dz / distance
- local newX, newY, newZ
- if invert then
- -- If invert is true, choose the point on the opposite side of the target.
- newX = targetX - ux * offsetDistance
- newY = targetY - uy * offsetDistance
- newZ = targetZ - uz * offsetDistance
- else
- -- Otherwise, choose the point from the target toward the ship.
- newX = targetX + ux * offsetDistance
- newY = targetY + uy * offsetDistance
- newZ = targetZ + uz * offsetDistance
- end
- -- Round the new coordinates to whole numbers.
- newX = math.floor(newX + 0.5)
- newY = math.floor(newY + 0.5)
- newZ = math.floor(newZ + 0.5)
- print("New ship target position (rounded): (" .. newX .. ", " .. newY .. ", " .. newZ .. ")")
- -- Determine the movement delta.
- local moveX = newX - currentX
- local moveY = newY - currentY
- local moveZ = newZ - currentZ
- ship.movement(moveX, moveY, moveZ)
- ship.rotationSteps(0)
- ship.command("MANUAL", true)
- print("Ship commanded to move by (" .. moveX .. ", " .. moveY .. ", " .. moveZ .. ")")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement