Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This script is based on the code of Plorntus (http://turtlescripts.com/project/gjdh0m-Auto-multimining-bot-quarry)
- -- This goes into the computer
- print("Starting")
- local port = "top"
- rednet.open(port)
- -- Variables initialization, don't change
- local MainBotID = 0
- local quarryBots = {}
- local currentQuarry = {}
- local quarrySize = {0,0}
- local botServiceQueue = {}
- local currentBot = 0
- local doneFirstLane = false
- local startup = false
- local ending = true
- local endBots = 0
- local running = true
- local lasti = 1
- -- Util split method
- function split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- -- Creates an empty quarry array which will hold if the lane is completed or not
- function createQuarryArray (x, y)
- local tempArr = {}
- for xt = 1, x, 1 do
- tempArr[xt] = {}
- for yt = 1, y, 1 do
- tempArr[xt][yt] = false
- end
- end
- return tempArr
- end
- -- returns the first empty lane, or false if none
- function findEmptyQuarryLane (arr)
- for x = 1, table.getn(arr), 1 do
- for y = 1, table.getn(arr[x]), 1 do
- if arr[x][y] == false then
- return {x, y}
- end
- end
- end
- return false
- end
- -- sets a specific lane as busy with a bot
- function setWorkingOnColumn (arr, x, y, botID)
- arr[x][y] = botID
- end
- -- waits until a bot is at the back of a pc
- function waitForBotToStart ()
- while peripheral.getType("back") ~= "turtle" do sleep(0.1) end
- local bot = peripheral.wrap("back")
- bot.turnOn()
- end
- function table.contains(ta, element)
- for ki = 1, table.getn(ta), 1 do
- if ta[ki] == element then
- return ki
- end
- end
- return 0
- end
- -- first, we wait for the main bot
- waitForBotToStart()
- while running == true do
- local id, message, distance = rednet.receive(0.1)
- -- first, do the broadcasting queue positions
- for i = lasti, math.min(table.getn(botServiceQueue), lasti + 3), 1 do
- lasti = lasti + 1
- rednet.send(botServiceQueue[i],"CCQuarry QUEUE "..i-1)
- end
- if lasti >= table.getn(botServiceQueue) then
- lasti = 1
- end
- if id ~= nil then
- local splitMessage = split(message, " ")
- -- only parse the messages from CCQuarry
- if splitMessage[1] == "CCQuarry" then
- if splitMessage[2] == "LOCATE" then
- rednet.send(id, "CCQuarry PING")
- elseif splitMessage[2] == "SETUP" then
- if MainBotID == 0 then
- MainBotID = id
- currentBot = id
- table.insert(botServiceQueue,id)
- print("SETING DEFAULT BOT AS MAIN BOT")
- rednet.send(id,"CCQuarry SETUP MAINBOT")
- else
- print("BOT CONNECTED. SENDING INFORMATION")
- rednet.send(id, "CCQuarry SETUP SLAVE")
- end
- table.insert(quarryBots, id)
- elseif splitMessage[2] == "LENGTH" then
- quarrySize[1] = splitMessage[3]
- elseif splitMessage[2] == "WIDTH" then
- if quarrySize[1] ~= 0 then
- quarrySize[2] = splitMessage[3] + 0
- currentQuarry = createQuarryArray(quarrySize[2],quarrySize[1])
- setWorkingOnColumn(currentQuarry, 1, 1, MainBotID)
- else
- print("ERROR RECIEVED WIDTH BEFORE LENGTH")
- exit()
- end
- elseif splitMessage[2] == "LANE" then
- local laneNeeded = findEmptyQuarryLane(currentQuarry)
- if laneNeeded ~= false then
- print("ASSIGNED TO LANE "..laneNeeded[1] .. " " .. laneNeeded[2])
- rednet.send(id, "CCQuarry LANE ".. laneNeeded[1] .. " " .. laneNeeded[2])
- setWorkingOnColumn(currentQuarry, laneNeeded[1], laneNeeded[2], id)
- else
- ending = true
- endBots = endBots + 1
- rednet.send(id,"CCQuarry LANE ENDLANE")
- if endBots == table.getn(quarryBots) then
- running = false
- end
- end
- elseif splitMessage[2] == "STARTUP" then
- startup = true
- elseif splitMessage[2] == "DONESTARTUP" then
- startup = false
- elseif splitMessage[2] == "TURNONBOT" then
- waitForBotToStart()
- elseif splitMessage[2] == "QUEUE" then
- local i = table.contains(botServiceQueue,id)
- -- check if the turtle in on the queue
- if i > 0 then
- rednet.send(id,"CCQuarry QUEUE "..i-1)
- else
- table.insert(botServiceQueue,id)
- rednet.send(id, "CCQuarry QUEUE "..table.getn(botServiceQueue)-1)
- end
- elseif splitMessage[2] == "DONESERVICE" then
- if currentBot == id then
- table.remove(botServiceQueue, 1)
- if table.getn(botServiceQueue) > 0 then
- rednet.send(botServiceQueue[1],"CCQuarry QUEUE 0")
- currentBot = botServiceQueue[1]
- else
- currentBot = 0
- end
- end
- end
- end
- end
- end
- print("Program ended")
Advertisement
Add Comment
Please, Sign In to add comment