tClear_multi.lua

99 lines · 3.6 KB

Open raw

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
4local 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.")
11end
12
13local modemSide = findModem()
14rednet.open(modemSide)
15
16local wrapped = peripheral.wrap(modemSide)
17if wrapped and wrapped.isWireless and not wrapped.isWireless() then
18 print("Note: Wired modem detected. Ensure turtles are cabled to the same network.")
19end
20
21-- For two turtles; extend for more
22print("Enter number of turtles (supports 2):")
23local num = tonumber(read())
24if num ~= 2 then
25 print("Defaulting to 2 turtles.")
26 num = 2
27end
28
29print("Enter LEFT-corner turtle ID (use os.getComputerID() on turtle):")
30local id1 = tonumber(read())
31print("Enter RIGHT-corner turtle ID:")
32local id2 = tonumber(read())
33
34-- Ask for chunky turtle IDs
35print("Enter LEFT chunky turtle ID (or press Enter to skip):")
36local chunkyInput1 = read()
37local chunkyId1 = nil
38if chunkyInput1 ~= "" then
39 chunkyId1 = tonumber(chunkyInput1)
40end
41
42print("Enter RIGHT chunky turtle ID (or press Enter to skip):")
43local chunkyInput2 = read()
44local chunkyId2 = nil
45if chunkyInput2 ~= "" then
46 chunkyId2 = tonumber(chunkyInput2)
47end
48
49print("Enter total depth (positive):")
50local depth = tonumber(read())
51print("Enter total width (positive):")
52local totalWidth = tonumber(read())
53print("Enter height:")
54local height = tonumber(read())
55print("Enter options (space-separated, e.g. layerbylayer startwithin):")
56local options = read()
57
58-- Divide width (handle odd by giving extra to one side)
59local half1 = math.floor(totalWidth / 2)
60local half2 = totalWidth - half1
61
62-- Params strings
63local params1 = tostring(depth) .. " " .. tostring(half1) .. " " .. tostring(height) .. " " .. (options or "")
64local params2 = tostring(depth) .. " " .. tostring(-half2) .. " " .. tostring(height) .. " " .. (options or "")
65
66-- Also send a structured payload some listeners can parse directly
67local masterId = os.getComputerID()
68local payload1 = { command = "RUN", program = "tClear", args = params1, masterId = masterId, role = "left" }
69local payload2 = { command = "RUN", program = "tClear", args = params2, masterId = masterId, role = "right" }
70
71-- Start chunky turtles first if available
72if 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
78end
79
80if 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
86end
87
88-- Start main mining turtles
89print("Sending to left turtle (ID " .. id1 .. "): " .. params1)
90rednet.send(id1, payload1, "tclear-run")
91rednet.send(id1, params1, "tclear-run") -- fallback for simple listeners
92print("Sending to right turtle (ID " .. id2 .. "): " .. params2)
93rednet.send(id2, payload2, "tclear-run")
94rednet.send(id2, params2, "tclear-run") -- fallback for simple listeners
95
96print("Turtles should start digging now.")
97if chunkyId1 or chunkyId2 then
98 print("Chunky turtles are paired and following to keep chunks loaded.")
99end