Advertisement
fatboychummy

turtleCobbleGeneratorSwarm

Aug 6th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 0 0
  1. local waitTime = 0.4
  2. local mX,mY = term.getSize()
  3.  
  4. local function getModem()
  5.   local h = peripheral.getNames()
  6.   local f = false
  7.   for i = 1,#h do
  8.     if h[i] == "top" or h[i] == "front" then
  9.       h = h[i]
  10.       f = true
  11.       break
  12.     end
  13.   end
  14.   if f then
  15.     return peripheral.wrap(h),h
  16.   end
  17.   return false
  18. end
  19.  
  20.  
  21. term.clear()
  22. local function writeAt(x,y,wr,fg,bg)
  23.   fg = fg or colors.black
  24.   bg = bg or colors.white
  25.   local clr = string.rep(" ",mX)
  26.   term.setCursorPos(1,y)
  27.   term.write(clr)
  28.   term.setCursorPos(x,y)
  29.   term.write(wr)
  30. end
  31.  
  32. local function checkInv()
  33.   local count = 0
  34.   for i = 1,16 do
  35.     if turtle.getItemCount(i) > 0 then
  36.       count = count + 1
  37.     end
  38.   end
  39.   return count
  40. end
  41.  
  42.  
  43. local modem,side = getModem()
  44. if modem then
  45.   writeAt(1,4,"Modem is connected.")
  46.   modem.open(1)
  47. else
  48.   writeAt(1,4,"Failed to find modem.")
  49. end
  50.  
  51.  
  52.  
  53.  
  54. local function f1()
  55.   while true do
  56.     if checkInv() >= 15 then
  57.       writeAt(1,2,"Inventory full",nil,colors.gray)
  58.       os.sleep(2)
  59.       os.shutdown()
  60.     end
  61.     for i = 1,100 do
  62.       writeAt(1,1,"Mining: " .. tostring(i))
  63.       while not turtle.digDown() do os.sleep(waitTime/2) end
  64.       os.sleep(waitTime)
  65.     end
  66.   end
  67. end
  68.  
  69.  
  70. local function f2()
  71.   while true do
  72.     local ev = {os.pullEvent("modem_message")}
  73.     assert(ev[1] == "modem_message","This should not happen...")
  74.     local mess = ev[5]
  75.     if mess == "update" then
  76.       writeAt(1,3,"UPDATING!!!")
  77.       os.sleep(2)
  78.       shell.run("pget")
  79.       term.clear()
  80.       writeAt(1,3,"DONE, REBOOTING")
  81.       os.sleep(2)
  82.       os.reboot()
  83.     elseif mess == "shutdown" then
  84.       writeAt(1,3,"Network shutdown.",nil,colors.gray)
  85.       os.sleep(2)
  86.       os.shutdown()
  87.     end
  88.   end
  89. end
  90.  
  91.  
  92. local ok,err = pcall(parallel.waitForAny,f1,f2)
  93. writeAt(1,6,"Something happened.",colors.gray)
  94. writeAt(1,7,err,colors.gray)
  95. if not err:find( "Terminated" ) then
  96.   os.sleep(3)
  97.   os.reboot()
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement