tClear_multi.lua
99 lines · 3.6 KB
Master script to launch multiple turtles with divided params
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/tClear/tClear_multi.lua
| 1 | -- Master script to launch multiple turtles with divided params |
| 2 | |
| 3 | -- Auto-detect a modem and open rednet |
| 4 | local function findModem() |
| 5 | for _, p in pairs(rs.getSides()) do |
| 6 | if peripheral.isPresent(p) and peripheral.getType(p) == "modem" then |
| 7 | return p |
| 8 | end |
| 9 | end |
| 10 | error("No modem attached to this computer.") |
| 11 | end |
| 12 | |
| 13 | local modemSide = findModem() |
| 14 | rednet.open(modemSide) |
| 15 | |
| 16 | local wrapped = peripheral.wrap(modemSide) |
| 17 | if wrapped and wrapped.isWireless and not wrapped.isWireless() then |
| 18 | print("Note: Wired modem detected. Ensure turtles are cabled to the same network.") |
| 19 | end |
| 20 | |
| 21 | -- For two turtles; extend for more |
| 22 | print("Enter number of turtles (supports 2):") |
| 23 | local num = tonumber(read()) |
| 24 | if num ~= 2 then |
| 25 | print("Defaulting to 2 turtles.") |
| 26 | num = 2 |
| 27 | end |
| 28 | |
| 29 | print("Enter LEFT-corner turtle ID (use os.getComputerID() on turtle):") |
| 30 | local id1 = tonumber(read()) |
| 31 | print("Enter RIGHT-corner turtle ID:") |
| 32 | local id2 = tonumber(read()) |
| 33 | |
| 34 | -- Ask for chunky turtle IDs |
| 35 | print("Enter LEFT chunky turtle ID (or press Enter to skip):") |
| 36 | local chunkyInput1 = read() |
| 37 | local chunkyId1 = nil |
| 38 | if chunkyInput1 ~= "" then |
| 39 | chunkyId1 = tonumber(chunkyInput1) |
| 40 | end |
| 41 | |
| 42 | print("Enter RIGHT chunky turtle ID (or press Enter to skip):") |
| 43 | local chunkyInput2 = read() |
| 44 | local chunkyId2 = nil |
| 45 | if chunkyInput2 ~= "" then |
| 46 | chunkyId2 = tonumber(chunkyInput2) |
| 47 | end |
| 48 | |
| 49 | print("Enter total depth (positive):") |
| 50 | local depth = tonumber(read()) |
| 51 | print("Enter total width (positive):") |
| 52 | local totalWidth = tonumber(read()) |
| 53 | print("Enter height:") |
| 54 | local height = tonumber(read()) |
| 55 | print("Enter options (space-separated, e.g. layerbylayer startwithin):") |
| 56 | local options = read() |
| 57 | |
| 58 | -- Divide width (handle odd by giving extra to one side) |
| 59 | local half1 = math.floor(totalWidth / 2) |
| 60 | local half2 = totalWidth - half1 |
| 61 | |
| 62 | -- Params strings |
| 63 | local params1 = tostring(depth) .. " " .. tostring(half1) .. " " .. tostring(height) .. " " .. (options or "") |
| 64 | local params2 = tostring(depth) .. " " .. tostring(-half2) .. " " .. tostring(height) .. " " .. (options or "") |
| 65 | |
| 66 | -- Also send a structured payload some listeners can parse directly |
| 67 | local masterId = os.getComputerID() |
| 68 | local payload1 = { command = "RUN", program = "tClear", args = params1, masterId = masterId, role = "left" } |
| 69 | local payload2 = { command = "RUN", program = "tClear", args = params2, masterId = masterId, role = "right" } |
| 70 | |
| 71 | -- Start chunky turtles first if available |
| 72 | if chunkyId1 then |
| 73 | print("Starting left chunky turtle (ID " .. chunkyId1 .. ")...") |
| 74 | local chunkyPayload1 = { command = "RUN", program = "tClearChunky", masterId = os.getComputerID(), role = "left_chunky" } |
| 75 | rednet.send(chunkyId1, chunkyPayload1, "tclear-run") |
| 76 | rednet.send(chunkyId1, chunkyPayload1, "tclear-chunky") |
| 77 | sleep(1) -- Give chunky turtle time to start |
| 78 | end |
| 79 | |
| 80 | if chunkyId2 then |
| 81 | print("Starting right chunky turtle (ID " .. chunkyId2 .. ")...") |
| 82 | local chunkyPayload2 = { command = "RUN", program = "tClearChunky", masterId = os.getComputerID(), role = "right_chunky" } |
| 83 | rednet.send(chunkyId2, chunkyPayload2, "tclear-run") |
| 84 | rednet.send(chunkyId2, chunkyPayload2, "tclear-chunky") |
| 85 | sleep(1) -- Give chunky turtle time to start |
| 86 | end |
| 87 | |
| 88 | -- Start main mining turtles |
| 89 | print("Sending to left turtle (ID " .. id1 .. "): " .. params1) |
| 90 | rednet.send(id1, payload1, "tclear-run") |
| 91 | rednet.send(id1, params1, "tclear-run") -- fallback for simple listeners |
| 92 | print("Sending to right turtle (ID " .. id2 .. "): " .. params2) |
| 93 | rednet.send(id2, payload2, "tclear-run") |
| 94 | rednet.send(id2, params2, "tclear-run") -- fallback for simple listeners |
| 95 | |
| 96 | print("Turtles should start digging now.") |
| 97 | if chunkyId1 or chunkyId2 then |
| 98 | print("Chunky turtles are paired and following to keep chunks loaded.") |
| 99 | end |