View difference between Paste ID: 0LdzPykn and e6uQxa8J
SHOW: | | - or go back to the newest paste.
1
local ship = peripheral.find("warpdriveShipController")
2
3
local ship_front, ship_right, ship_up = ship.dim_positive()
4
local ship_back, ship_left, ship_down = ship.dim_negative()
5
local ship_isInHyper = ship.isInHyperspace()
6
local ship_movement = { ship.movement() }
7
local ship_rotationSteps = ship.rotationSteps()
8
9
-- Open rednet on the modem side (adjust "left" as needed)
10
rednet.open("top")
11
12
print("Waiting for coordinate broadcasts when redstone is active on the front...")
13
14
while true do
15
    -- Only process messages when redstone signal on the "front" is on.
16
    if redstone.getInput("front") then
17
        -- Use a timeout (0.5 seconds) so the call doesn't block indefinitely and allows rechecking the redstone state.
18
        local senderID, message, protocol = rednet.receive("coordBroadcast", 0.5)
19
        
20
        if senderID then
21
            if message and message.x and message.y and message.z then
22
                print("Received coordinates from computer " .. senderID .. ":")
23
                print("X: " .. message.x .. ", Y: " .. message.y .. ", Z: " .. message.z)
24
25
            local lastLx = tonumber(message.x)
26
            local lastLy = tonumber(message.y)  -- if needed for future use
27
            local lastLz = tonumber(message.z)
28
            
29
            print("Jumping to X:" .. lastLx .. ", Z:" .. lastLz)
30
            
31
            local rx, ry, rz = ship.getOrientation()
32
            local minForwardBack = math.abs(ship_front + ship_back + 1)
33
            local minLeftRight = math.abs(ship_left + ship_right + 1)
34
            local mx, my, mz = ship.getLocalPosition()
35
            
36
            local dx = lastLx - mx
37
            local dz = lastLz - mz
38
            
39
            local forwardBackMov = 0
40
            local leftRightMov = 0
41
            
42
            -- Determine movement based on ship's orientation.
43
            if rx == 1 then
44
                forwardBackMov = dx
45
                leftRightMov = dz
46
            elseif rx == -1 then
47
                forwardBackMov = -dx
48
                leftRightMov = -dz
49
            elseif rz == 1 then
50
                forwardBackMov = dz
51
                leftRightMov = -dx
52
            elseif rz == -1 then
53
                forwardBackMov = -dz
54
                leftRightMov = dx
55
            end
56
            
57
            if math.abs(forwardBackMov) < minForwardBack and math.abs(leftRightMov) < minLeftRight then
58
                print("The movement is too small!")
59
            else
60
                ship.movement(forwardBackMov, 0, leftRightMov)
61
                ship.rotationSteps(0)
62
                ship.command("MANUAL", true)
63
            end
64
65
            
66
            
67
            else
68
                print("Received invalid data from computer " .. senderID)
69
            end
70
        end
71
    else
72
        -- Optionally, you can print a message when redstone is off or simply sleep quietly.
73
        -- print("Redstone is off on the front; not receiving messages.")
74
        sleep(0.1)
75
    end
76
end
77