View difference between Paste ID: 7vdAUbhS and V8yjxrhA
SHOW: | | - or go back to the newest paste.
1-
local ship = peripheral.find("warpdriveShipCore")
1+
local ship  = peripheral.find("warpdriveShipCore")
2
local miner = peripheral.find("warpdriveMiningLaser")
3-
local ship_front, ship_right, ship_up = ship.dim_positive()
3+
4-
local ship_back, ship_left, ship_down = ship.dim_negative()
4+
local ship_front, ship_right, ship_up   = ship.dim_positive()
5-
local ship_isInHyper = ship.isInHyperspace()
5+
local ship_back,  ship_left,  ship_down = ship.dim_negative()
6-
local ship_movement = { ship.movement() }
6+
7-
local ship_rotationSteps = ship.rotationSteps()
7+
-- Open rednet on the modem side (adjust "top" as needed)
8
rednet.open("top")
9-
-- Open rednet on the modem side (adjust "left" as needed)
9+
10
11
while true do
12
  if redstone.getInput("front") then
13
    -- listen for any protocol, 0.5s timeout
14
    local senderID, message, protocol = rednet.receive(nil, 0.5)
15-
    -- Only process messages when redstone signal on the "front" is on.
15+
    if senderID and message and message.x and message.y and message.z then
16-
    if redstone.getInput("front") then
16+
      local lastLx = tonumber(message.x)
17-
        -- Use a timeout (0.5 seconds) so the call doesn't block indefinitely and allows rechecking the redstone state.
17+
      local lastLy = tonumber(message.y)
18-
        local senderID, message, protocol = rednet.receive("HorizontalJumpBroadcast", 0.5)
18+
      local lastLz = tonumber(message.z)
19-
        
19+
20-
        if senderID then
20+
      -- choose reference point based on protocol
21-
            if message and message.x and message.y and message.z then
21+
      if protocol == "HorizontalJumpBroadcast"
22-
                print("Received coordinates from computer " .. senderID .. ":")
22+
      or protocol == "HorizontalJumpMinerBroadcast" then
23-
                print("X: " .. message.x .. ", Y: " .. message.y .. ", Z: " .. message.z)
23+
        local mx, my, mz
24
        if protocol == "HorizontalJumpBroadcast" then
25-
            local lastLx = tonumber(message.x)
25+
          mx, my, mz = ship.getLocalPosition()
26-
            local lastLy = tonumber(message.y)  -- if needed for future use
26+
        else
27-
            local lastLz = tonumber(message.z)
27+
          mx, my, mz = miner.getLocalPosition()
28-
            
28+
29-
            print("Jumping to X:" .. lastLx .. ", Z:" .. lastLz)
29+
30-
            
30+
        print(("Received %s → X:%d, Y:%d, Z:%d")
31-
            local rx, ry, rz = ship.getOrientation()
31+
          :format(protocol, lastLx, lastLy, lastLz))
32-
            local minForwardBack = math.abs(ship_front + ship_back + 1)
32+
33-
            local minLeftRight = math.abs(ship_left + ship_right + 1)
33+
        -- compute movement vector (X/Z plane only)
34-
            local mx, my, mz = ship.getLocalPosition()
34+
        local dx = lastLx - mx
35-
            
35+
        local dz = lastLz - mz
36-
            local dx = lastLx - mx
36+
37-
            local dz = lastLz - mz
37+
        local rx, ry, rz     = ship.getOrientation()
38-
            
38+
        local minFB          = math.abs(ship_front + ship_back + 1)
39-
            local forwardBackMov = 0
39+
        local minLR          = math.abs(ship_left  + ship_right + 1)
40-
            local leftRightMov = 0
40+
        local forwardBackMov = 0
41-
            
41+
        local leftRightMov   = 0
42-
            -- Determine movement based on ship's orientation.
42+
43-
            if rx == 1 then
43+
        if     rx ==  1 then forwardBackMov, leftRightMov =  dx,  dz
44-
                forwardBackMov = dx
44+
        elseif rx == -1 then forwardBackMov, leftRightMov = -dx, -dz
45-
                leftRightMov = dz
45+
        elseif rz ==  1 then forwardBackMov, leftRightMov =  dz, -dx
46-
            elseif rx == -1 then
46+
        elseif rz == -1 then forwardBackMov, leftRightMov = -dz,  dx
47-
                forwardBackMov = -dx
47+
48-
                leftRightMov = -dz
48+
49-
            elseif rz == 1 then
49+
        if math.abs(forwardBackMov) < minFB and math.abs(leftRightMov) < minLR then
50-
                forwardBackMov = dz
50+
          print("The movement is too small!")
51-
                leftRightMov = -dx
51+
        else
52-
            elseif rz == -1 then
52+
          ship.movement(forwardBackMov, 0, leftRightMov)
53-
                forwardBackMov = -dz
53+
          ship.rotationSteps(0)
54-
                leftRightMov = dx
54+
          ship.command("MANUAL", true)
55-
            end
55+
56-
            
56+
      end
57-
            if math.abs(forwardBackMov) < minForwardBack and math.abs(leftRightMov) < minLeftRight then
57+
58-
                print("The movement is too small!")
58+
  else
59-
            else
59+
    -- no redstone signal: wait before retrying
60-
                ship.movement(forwardBackMov, 0, leftRightMov)
60+
    os.sleep(0.1)
61-
                ship.rotationSteps(0)
61+
  end
62-
                ship.command("MANUAL", true)
62+
63-
            end
63+