multi.lua
121 lines · 3.6 KB
Send coordinated eDig jobs to multiple turtles
Copy & run
wget https://perlytiara.github.io/turtles.tips/raw/programs/perlytiara/eDig/multi.lua
| 1 | -- multi.lua - Send coordinated eDig jobs to multiple turtles |
| 2 | local function findModem() |
| 3 | for _, side in pairs(rs.getSides()) do |
| 4 | if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then |
| 5 | return side |
| 6 | end |
| 7 | end |
| 8 | error("No modem found!") |
| 9 | end |
| 10 | |
| 11 | rednet.open(findModem()) |
| 12 | |
| 13 | print("Multi-Turtle eDig Coordinator") |
| 14 | print("Supports simultaneous row-based coordination") |
| 15 | |
| 16 | write("Tunnel type (straight/dome/size2) [straight]: ") |
| 17 | local shape = string.lower(read()) |
| 18 | if shape ~= "dome" and shape ~= "size2" then |
| 19 | shape = "straight" |
| 20 | end |
| 21 | |
| 22 | local height, width, length |
| 23 | if shape == "straight" then |
| 24 | write("Tunnel height (blocks) [3]: ") |
| 25 | height = tonumber(read()) or 3 |
| 26 | |
| 27 | write("Tunnel width (blocks) [3]: ") |
| 28 | width = tonumber(read()) or 3 |
| 29 | else |
| 30 | if shape == "size2" then |
| 31 | height = 3 |
| 32 | width = 7 |
| 33 | print("Dome shape: size2 (7 wide, max 7 turtles)") |
| 34 | else |
| 35 | height = 3 |
| 36 | width = 5 |
| 37 | print("Dome shape: custom (5 wide, max 5 turtles)") |
| 38 | end |
| 39 | end |
| 40 | |
| 41 | write("Tunnel length (blocks) [32]: ") |
| 42 | length = tonumber(read()) or 32 |
| 43 | |
| 44 | write("Place floor blocks? (y/n) [n]: ") |
| 45 | local place = string.lower(read()) == "y" and "place" or "" |
| 46 | |
| 47 | write("Turtle IDs (space-separated): ") |
| 48 | local idStr = read() |
| 49 | local ids = {} |
| 50 | for id in idStr:gmatch("%S+") do |
| 51 | local num = tonumber(id) |
| 52 | if num then table.insert(ids, num) end |
| 53 | end |
| 54 | |
| 55 | if #ids == 0 then |
| 56 | print("No valid turtle IDs") |
| 57 | return |
| 58 | end |
| 59 | |
| 60 | -- Limit turtles based on shape |
| 61 | local maxTurtles = shape == "straight" and width or (shape == "size2" and 7 or 5) |
| 62 | if #ids > maxTurtles then |
| 63 | print("Warning: " .. shape .. " shape supports max " .. maxTurtles .. " turtles") |
| 64 | print("Using first " .. maxTurtles .. " turtles") |
| 65 | while #ids > maxTurtles do |
| 66 | table.remove(ids) |
| 67 | end |
| 68 | end |
| 69 | |
| 70 | write("Coordination mode:") |
| 71 | print("1. Row-based (turtles work on different rows simultaneously)") |
| 72 | print("2. Segment-based (turtles work on different segments)") |
| 73 | write("Choose (1/2) [1]: ") |
| 74 | local coordMode = read() |
| 75 | if coordMode ~= "2" then coordMode = "1" end |
| 76 | |
| 77 | local segmentLength = 0 |
| 78 | if coordMode == "2" then |
| 79 | write("Segment length (blocks per turtle) [8]: ") |
| 80 | segmentLength = tonumber(read()) or 8 |
| 81 | end |
| 82 | |
| 83 | -- Build command |
| 84 | local cmd = tostring(height) .. " " .. tostring(length) .. " " .. tostring(width) |
| 85 | if place ~= "" then cmd = cmd .. " " .. place end |
| 86 | if shape ~= "straight" then cmd = cmd .. " " .. shape end |
| 87 | |
| 88 | -- Send to turtles with coordination |
| 89 | print("\nSending coordinated jobs to " .. #ids .. " turtles...") |
| 90 | |
| 91 | if coordMode == "2" then |
| 92 | -- Segment-based coordination |
| 93 | print("Segment-based coordination") |
| 94 | local totalLength = length |
| 95 | local segments = math.ceil(totalLength / segmentLength) |
| 96 | |
| 97 | for i = 1, #ids do |
| 98 | local turtleId = ids[i] |
| 99 | local segmentStart = (i - 1) * segmentLength |
| 100 | local segmentEnd = math.min(segmentStart + segmentLength, totalLength) |
| 101 | local segmentLengthActual = segmentEnd - segmentStart |
| 102 | |
| 103 | if segmentLengthActual > 0 then |
| 104 | local segmentCmd = cmd .. " " .. tostring(i) |
| 105 | print("Turtle " .. turtleId .. " (Segment " .. i .. "): " .. segmentLengthActual .. " blocks") |
| 106 | rednet.send(turtleId, {command = "RUN", args = segmentCmd}) |
| 107 | end |
| 108 | end |
| 109 | else |
| 110 | -- Row-based coordination (simultaneous) |
| 111 | print("Row-based coordination - all turtles start simultaneously") |
| 112 | for i = 1, #ids do |
| 113 | local turtleId = ids[i] |
| 114 | local rowCmd = cmd .. " " .. tostring(i) .. " " .. tostring(width) |
| 115 | print("Turtle " .. turtleId .. " (Row " .. i .. "): " .. width .. " blocks wide") |
| 116 | rednet.send(turtleId, {command = "RUN", args = rowCmd}) |
| 117 | end |
| 118 | end |
| 119 | |
| 120 | print("\nJobs sent! All turtles starting simultaneously...") |
| 121 | print("Monitor individual turtle progress for coordination status.") |