shirkit

Computer end

Feb 23rd, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.84 KB | None | 0 0
  1. -- This script is based on the code of Plorntus (http://turtlescripts.com/project/gjdh0m-Auto-multimining-bot-quarry)
  2.  
  3. -- This goes into the computer
  4.  
  5. print("Starting")
  6.  
  7. local port = "top"
  8. rednet.open(port)
  9.  
  10. -- Variables initialization, don't change
  11. local MainBotID = 0
  12. local quarryBots = {}
  13. local currentQuarry = {}
  14. local quarrySize = {0,0}
  15. local botServiceQueue = {}
  16. local currentBot = 0
  17. local doneFirstLane = false
  18. local startup = false
  19. local ending = true
  20. local endBots = 0
  21. local running = true
  22. local lasti = 1
  23.  
  24. -- Util split method
  25. function split(str, pat)
  26.    local t = {}
  27.    local fpat = "(.-)" .. pat
  28.    local last_end = 1
  29.    local s, e, cap = str:find(fpat, 1)
  30.    while s do
  31.       if s ~= 1 or cap ~= "" then
  32.          table.insert(t,cap)
  33.       end
  34.       last_end = e+1
  35.       s, e, cap = str:find(fpat, last_end)
  36.    end
  37.    if last_end <= #str then
  38.       cap = str:sub(last_end)
  39.       table.insert(t, cap)
  40.    end
  41.    return t
  42. end
  43.  
  44. -- Creates an empty quarry array which will hold if the lane is completed or not
  45. function createQuarryArray (x, y)
  46.     local tempArr = {}
  47.     for xt = 1, x, 1 do
  48.         tempArr[xt] = {}
  49.         for yt = 1, y, 1 do
  50.             tempArr[xt][yt] = false
  51.         end
  52.     end
  53.     return tempArr
  54. end
  55.  
  56. -- returns the first empty lane, or false if none
  57. function findEmptyQuarryLane (arr)
  58.     for x = 1, table.getn(arr), 1 do
  59.         for y = 1, table.getn(arr[x]), 1 do
  60.             if arr[x][y] == false then
  61.                 return {x, y}
  62.             end
  63.         end
  64.     end
  65.     return false
  66. end
  67.  
  68. -- sets a specific lane as busy with a bot
  69. function setWorkingOnColumn (arr, x, y, botID)
  70.     arr[x][y] = botID
  71. end
  72.  
  73. -- waits until a bot is at the back of a pc
  74. function waitForBotToStart ()
  75.     while peripheral.getType("back") ~= "turtle" do sleep(0.1) end
  76.     local bot = peripheral.wrap("back")
  77.     bot.turnOn()
  78. end
  79.  
  80. function table.contains(ta, element)
  81.   for ki = 1, table.getn(ta), 1 do
  82.     if ta[ki] == element then
  83.       return ki
  84.     end
  85.   end
  86.   return 0
  87. end
  88.  
  89. -- first, we wait for the main bot
  90. waitForBotToStart()
  91.  
  92. while running == true do
  93.     local id, message, distance = rednet.receive(0.1)
  94.     -- first, do the broadcasting queue positions
  95.     for i = lasti, math.min(table.getn(botServiceQueue), lasti + 3), 1 do
  96.         lasti = lasti + 1
  97.         rednet.send(botServiceQueue[i],"CCQuarry QUEUE "..i-1)
  98.     end
  99.     if lasti >= table.getn(botServiceQueue) then
  100.         lasti = 1
  101.     end
  102.    
  103.     if id ~= nil then
  104.         local splitMessage = split(message, " ")
  105.         -- only parse the messages from CCQuarry
  106.         if splitMessage[1] == "CCQuarry" then
  107.        
  108.             if splitMessage[2] == "LOCATE" then
  109.                 rednet.send(id, "CCQuarry PING")
  110.                
  111.             elseif splitMessage[2] == "SETUP" then 
  112.                 if MainBotID == 0 then
  113.                     MainBotID = id
  114.                     currentBot = id
  115.                     table.insert(botServiceQueue,id)
  116.                     print("SETING DEFAULT BOT AS MAIN BOT")
  117.                     rednet.send(id,"CCQuarry SETUP MAINBOT")
  118.                 else
  119.                     print("BOT CONNECTED. SENDING INFORMATION")
  120.                     rednet.send(id, "CCQuarry SETUP SLAVE")
  121.                 end
  122.                 table.insert(quarryBots, id)
  123.                
  124.             elseif splitMessage[2] == "LENGTH" then
  125.                 quarrySize[1] = splitMessage[3]
  126.                
  127.             elseif splitMessage[2] == "WIDTH" then
  128.                 if quarrySize[1] ~= 0 then
  129.                     quarrySize[2] = splitMessage[3] + 0
  130.                     currentQuarry = createQuarryArray(quarrySize[2],quarrySize[1])
  131.                     setWorkingOnColumn(currentQuarry, 1, 1, MainBotID)
  132.                 else
  133.                     print("ERROR RECIEVED WIDTH BEFORE LENGTH")
  134.                     exit()
  135.                 end
  136.                
  137.             elseif splitMessage[2] == "LANE" then
  138.                 local laneNeeded = findEmptyQuarryLane(currentQuarry)
  139.                 if laneNeeded ~= false then
  140.                     print("ASSIGNED TO LANE "..laneNeeded[1] .. " " .. laneNeeded[2])
  141.                     rednet.send(id, "CCQuarry LANE ".. laneNeeded[1] .. " " .. laneNeeded[2])
  142.                     setWorkingOnColumn(currentQuarry, laneNeeded[1], laneNeeded[2], id)
  143.                 else
  144.                     ending = true
  145.                     endBots = endBots + 1
  146.                     rednet.send(id,"CCQuarry LANE ENDLANE")
  147.                     if endBots == table.getn(quarryBots) then
  148.                         running = false
  149.                     end
  150.                 end
  151.                
  152.             elseif splitMessage[2] == "STARTUP" then
  153.                 startup = true
  154.                
  155.             elseif splitMessage[2] == "DONESTARTUP" then
  156.                 startup = false
  157.                
  158.             elseif splitMessage[2] == "TURNONBOT" then
  159.                 waitForBotToStart()
  160.                
  161.             elseif splitMessage[2] == "QUEUE" then
  162.                 local i = table.contains(botServiceQueue,id)
  163.                
  164.                 -- check if the turtle in on the queue
  165.                 if i > 0 then
  166.                     rednet.send(id,"CCQuarry QUEUE "..i-1)
  167.                 else
  168.                     table.insert(botServiceQueue,id)
  169.                     rednet.send(id, "CCQuarry QUEUE "..table.getn(botServiceQueue)-1)
  170.                 end
  171.                
  172.             elseif splitMessage[2] == "DONESERVICE" then
  173.                 if  currentBot == id then
  174.                     table.remove(botServiceQueue, 1)
  175.                     if table.getn(botServiceQueue) > 0 then
  176.                         rednet.send(botServiceQueue[1],"CCQuarry QUEUE 0")
  177.                         currentBot = botServiceQueue[1]
  178.                     else
  179.                         currentBot = 0
  180.                     end
  181.                 end
  182.             end
  183.         end
  184.     end
  185. end
  186.  
  187. print("Program ended")
Advertisement
Add Comment
Please, Sign In to add comment