View difference between Paste ID: 8cpPXKjZ and MxdxD8Nn
SHOW: | | - or go back to the newest paste.
1
-- 3D-Offset-Driven Ship Jump & Mining-Laser Align
2-
local lasers = peripheral.getNames()
2+
-- Listens for { x, y, z } on Rednet protocol "3DOffsetBroadcast"
3
-- and computes an offset jump point back toward the ship.
4
5
-- 1) Configuration
6-
-- Filter for warpdriveLaserCamera peripherals and set beam frequency.
6+
7-
for i = #lasers, 1, -1 do
7+
8-
    if peripheral.getType(lasers[i]) ~= "warpdriveLaserCamera" then
8+
-- 2) Wrap your ship core
9-
        table.remove(lasers, i)
9+
10-
    else
10+
assert(ship, "No warpdriveShipCore detected!")
11-
        peripheral.wrap(lasers[i]).beamFrequency(1420)
11+
12
-- 3) Pre-compute ship dimensions & status
13
local ship_front, ship_right, ship_up   = ship.dim_positive()
14
local ship_back,  ship_left,  ship_down = ship.dim_negative()
15-
-- Get ship dimensions and status.
15+
16-
ship_front, ship_right, ship_up = ship.dim_positive()
16+
-- 4) Instructions
17-
ship_back, ship_left, ship_down = ship.dim_negative()
17+
print("Waiting for 3DOffsetBroadcast → Redstone 'front' must be ON to accept jumps")
18-
ship_isInHyper = ship.isInHyperspace()
18+
19-
ship_movement = { ship.movement() }
19+
-- 5) Open Rednet on all modems
20-
ship_rotationSteps = ship.rotationSteps()
20+
for _, side in ipairs(peripheral.getNames()) do
21
  if peripheral.getType(side) == "modem" then
22-
print("Emit Scanning Laser to Jump Ship and Aligning the Mining Laser")
22+
    rednet.open(side)
23
  end
24
end
25-
    local event, laserName, lx, ly, lz, block, _, _, _, type, metadata, resistance = os.pullEvent()
25+
26-
    
26+
-- 6) Main loop: only react when redstone 'front' is high
27-
    if event == "laserScanning" then
27+
28-
        -- Convert laser scanning coordinates to numbers.
28+
  if redstone.getInput("front") then
29-
        local lastLx = tonumber(lx)
29+
    -- wait up to 0.5s for a broadcast on 3DOffsetBroadcast
30-
        local lastLy = tonumber(ly)
30+
    local sender, message = rednet.receive("3DOffsetBroadcast", 0.5)
31-
        local lastLz = tonumber(lz)
31+
    if sender and message.x and message.y and message.z then
32-
        
32+
      local lastLx, lastLy, lastLz =
33-
        -- Get current ship position.
33+
        tonumber(message.x),
34-
        local mx, my, mz = ship.getLocalPosition()
34+
        tonumber(message.y),
35-
        
35+
        tonumber(message.z)
36-
        print("Laser target: X:" .. lastLx .. " Y:" .. lastLy .. " Z:" .. lastLz)
36+
37-
        
37+
      print(string.format(
38-
        -- Calculate vector from laser target (L) to ship (M)
38+
        "Received target from %d → X:%d  Y:%d  Z:%d",
39-
        local vx = mx - lastLx
39+
        sender, lastLx, lastLy, lastLz
40-
        local vy = my - lastLy
40+
      ))
41-
        local vz = mz - lastLz
41+
42-
        
42+
      -- ship current position
43-
        local dist = math.sqrt(vx*vx + vy*vy + vz*vz)
43+
      local mx, my, mz = ship.getLocalPosition()
44-
        
44+
45-
        if dist > 0 then
45+
      -- vector from laser target back toward ship
46-
            -- Normalize the vector.
46+
      local vx, vy, vz = mx - lastLx, my - lastLy, mz - lastLz
47-
            local ux = vx / dist
47+
      local dist = math.sqrt(vx*vx + vy*vy + vz*vz)
48-
            local uy = vy / dist
48+
49-
            local uz = vz / dist
49+
      if dist > 0 then
50
        -- unit vector
51-
            -- Calculate dx, dy, dz as the point offsetDistance away from the laser target along the vector toward the ship.
51+
        local ux, uy, uz = vx/dist, vy/dist, vz/dist
52-
            local dx = lastLx + ux * offsetDistance
52+
53-
            local dy = lastLy + uy * offsetDistance
53+
        -- offset point
54-
            local dz = lastLz + uz * offsetDistance
54+
        local dx = math.floor(lastLx + ux*offsetDistance + 0.5)
55-
            
55+
        local dy = math.floor(lastLy + uy*offsetDistance + 0.5)
56-
            -- Round the coordinates to whole numbers.
56+
        local dz = math.floor(lastLz + uz*offsetDistance + 0.5)
57-
            dx = math.floor(dx + 0.5)
57+
58-
            dy = math.floor(dy + 0.5)
58+
        print(string.format(
59-
            dz = math.floor(dz + 0.5)
59+
          "Jump coords → X:%d  Y:%d  Z:%d",
60-
            
60+
          dx, dy, dz
61-
            print("Calculated jump coordinates: (" .. dx .. ", " .. dy .. ", " .. dz .. ")")
61+
        ))
62-
            
62+
63-
            -- Retrieve ship orientation and compute minimum movement thresholds.
63+
        -- orientation & thresholds
64-
            local rx, ry, rz = ship.getOrientation()
64+
        local rx, ry, rz    = ship.getOrientation()
65-
            local minForwardBack = math.abs(ship_front + ship_back + 1)
65+
        local minFB         = math.abs(ship_front + ship_back + 1)
66-
            local minLeftRight = math.abs(ship_left + ship_right + 1)
66+
        local minLR         = math.abs(ship_left  + ship_right + 1)
67-
            local minUpDown = math.abs(ship_up + ship_down + 1)
67+
        local minUD         = math.abs(ship_up    + ship_down + 1)
68-
            
68+
69-
            -- Compute the movement difference between target jump point and ship.
69+
        -- desired move in world-space
70-
            local jx = dx - mx
70+
        local jx, jy, jz    = dx - mx, dy - my, dz - mz
71-
            local jy = dy - my
71+
72-
            local jz = dz - mz
72+
        -- map to ship axes
73-
            
73+
        local fb, lr = 0, 0
74-
            -- For horizontal movement, adjust based on ship orientation.
74+
        local ud      = jy
75-
            local forwardBackMov = 0
75+
        if     rx ==  1 then fb, lr = jx,  jz
76-
            local leftRightMov = 0
76+
        elseif rx == -1 then fb, lr = -jx, -jz
77-
            -- For vertical movement, use the raw difference.
77+
        elseif rz ==  1 then fb, lr = jz, -jx
78-
            local upDownMov = jy
78+
        elseif rz == -1 then fb, lr = -jz, jx
79-
            
79+
80-
            if rx == 1 then
80+
81-
                forwardBackMov = jx
81+
        -- threshold check
82-
                leftRightMov = jz
82+
        if math.abs(fb) < minFB
83-
            elseif rx == -1 then
83+
           and math.abs(ud) < minUD
84-
                forwardBackMov = -jx
84+
           and math.abs(lr) < minLR then
85-
                leftRightMov = -jz
85+
          print("Movement too small—skipping jump.")
86-
            elseif rz == 1 then
86+
87-
                forwardBackMov = jz
87+
          ship.movement(fb, ud, lr)
88-
                leftRightMov = -jx
88+
          ship.rotationSteps(0)
89-
            elseif rz == -1 then
89+
          ship.command("MANUAL", true)
90-
                forwardBackMov = -jz
90+
91-
                leftRightMov = jx
91+
      else
92-
            end
92+
        print("Error: ship and target coincide—no movement.")
93
      end
94-
            -- Check if movement is large enough in each axis.
94+
95-
            if math.abs(forwardBackMov) < minForwardBack and math.abs(upDownMov) < minUpDown and math.abs(leftRightMov) < minLeftRight then
95+
  else
96-
                print("The movement is too small!")
96+
    -- redstone is off, pause briefly
97-
            else
97+
    os.sleep(0.1)
98-
                ship.movement(forwardBackMov, upDownMov, leftRightMov)
98+
  end
99-
                ship.rotationSteps(0)
99+
100-
                ship.command("MANUAL", true)
100+