Advertisement
Pirnogion

MiningTurtleClient

Jan 19th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.84 KB | None | 0 0
  1. os.loadAPI("GUIElements")
  2.  
  3. local args = {...}
  4. --Name, Text, BlinkText, FirstColor, SecondColor, FirstTextColor, SecondTextColor, PosX, PosY, Blink
  5. local StatusPoint = GUIElements.CreateBlinkPoint("SP", "Undefined", nil, colors.green, colors.white, nil, nil, 2, 1)
  6. local InfoPoint = GUIElements.CreateBlinkPoint("IP", "Press 'p' to pause/continue; Press 's' to stop.", ">", colors.green, colors.white, nil, nil, 2, 3)
  7. local FuelPoint = GUIElements.CreateBlinkPoint("FP", "Fuel: " .. turtle.getFuelLevel(), ">", colors.green, colors.white, nil, nil, 2, 6)
  8. local ConnectPoint = GUIElements.CreateBlinkPoint("CP", "No modem", ">", colors.red, colors.white, nil, nil, 2, 8)
  9.  
  10. --Define variable
  11. local isColor = term.isColor()
  12. local serveraddress = nil
  13. local running       = true
  14. local FUELSLOT      = 16
  15. local status        = "paused"
  16. local isConnect     = 0 -- '-1' - no modem; '0' - no connect; '1' - connect; '2' - connected.
  17. local statcolor     = {}
  18.       statcolor["mining"]       = colors.green
  19.       statcolor["paused"]       = colors.yellow
  20.       statcolor["no_fuel"]      = colors.red
  21.       statcolor["undefined"]    = colors.gray
  22.      
  23. --Utils
  24. local function CheckFuel()
  25.   if turtle.getFuelLevel() > 0 or turtle.getFuelLevel() == "unlimited" then
  26.     return true
  27.   end
  28.  
  29.   turtle.select(FUELSLOT)
  30.   if turtle.refuel() then
  31.     turtle.select(1)
  32.     os.queueEvent("status", "paused")
  33.     return true
  34.   end
  35.  
  36.   turtle.drop(FUELSLOT)
  37.   return false
  38. end
  39.  
  40. local function Exit()
  41.     running = false
  42.    
  43.     GUIElements.DestroyBlinkPoint(StatusPoint.name)
  44.     GUIElements.DestroyBlinkPoint(InfoPoint.name)
  45.     GUIElements.DestroyBlinkPoint(FuelPoint.name)
  46.     GUIElements.DestroyBlinkPoint(ConnectPoint.name)
  47.    
  48.     term.setBackgroundColor(colors.black)
  49.     term.setTextColor(colors.white)
  50.     term.setCursorPos(1, 1)
  51.     term.clear()
  52. end
  53.  
  54. --Program
  55. local function Mine(len, retback, core, list)
  56.   turtle.select(1)
  57.   while running do
  58.     if not CheckFuel() and status ~= "undefined" then
  59.       os.queueEvent("status", "no_fuel")
  60.     end
  61.     if (status == "mining") then
  62.       turtle.dig()
  63.       turtle.forward()
  64.       turtle.digDown()
  65.       turtle.digUp()
  66.     else os.sleep(2) end
  67.   end
  68. end
  69.  
  70. local function GUI()
  71.     term.setBackgroundColor(colors.white)
  72.     term.setTextColor(colors.gray)
  73.     term.clear()
  74.  
  75.     while running do
  76.         StatusPoint.text   = status
  77.         StatusPoint.fcolor = statcolor[status]
  78.         FuelPoint.text = "Fuel: " .. turtle.getFuelLevel()
  79.         GUIElements.DrawPoints()
  80.         os.sleep(1)
  81.     end
  82. end
  83.  
  84. local function ncGUI()
  85.     term.setBackgroundColor(colors.black)
  86.     term.setTextColor(colors.white)
  87.     term.clear()
  88.  
  89.     StatusPoint.fcolor  = colors.white
  90.     InfoPoint.fcolor    = colors.white
  91.     FuelPoint.fcolor    = colors.white
  92.     ConnectPoint.fcolor = colors.white
  93.    
  94.     StatusPoint.scolor  = colors.black
  95.     InfoPoint.scolor    = colors.black
  96.     FuelPoint.scolor    = colors.black
  97.     ConnectPoint.scolor = colors.black
  98.    
  99.     StatusPoint.ftcolor  = colors.white
  100.     InfoPoint.ftcolor    = colors.white
  101.     FuelPoint.ftcolor    = colors.white
  102.     ConnectPoint.ftcolor = colors.white
  103.    
  104.     StatusPoint.stcolor  = colors.black
  105.     InfoPoint.stcolor    = colors.black
  106.     FuelPoint.stcolor    = colors.black
  107.     ConnectPoint.stcolor = colors.black
  108.    
  109.     while running do
  110.         StatusPoint.text   = status
  111.         FuelPoint.text = "Fuel: " .. turtle.getFuelLevel()
  112.         GUIElements.DrawPoints()
  113.         os.sleep(1)
  114.     end
  115. end
  116.  
  117. local function UserControl()
  118.   while running do
  119.     local event, par1 = os.pullEvent()
  120.    
  121.     if (event == "char") then
  122.       if par1 == "p" and (status == "mining" or status == "paused") then
  123.         if status == "paused" then os.queueEvent("status", "mining") else os.queueEvent("status", "paused") end
  124.       elseif par1 == "s" then
  125.         rednet.send(address, "packet stop end", "TurtleControl1")
  126.         Exit()
  127.       end
  128.     elseif (event == "status") then
  129.         status = par1
  130.     end
  131.   end
  132. end
  133.  
  134. local function MessageHandler()
  135.     if not address then return end
  136.    
  137.     while running do
  138.         rednet.send(address, "packet " .. status .. " " .. turtle.getFuelLevel() .. " end", "TurtleControl1")
  139.         local id, msg = rednet.receive("TurtleControl1", 1)
  140.         if id == address then
  141.             if msg == "paused" then
  142.                 if status == "paused" then os.queueEvent("status", "mining") else os.queueEvent("status", "paused") end
  143.             elseif msg == "stop" then
  144.                 Exit()
  145.             end
  146.         end
  147.     end
  148. end
  149.  
  150. local sModemSide = nil
  151. for n, sSide in ipairs(rs.getSides()) do
  152.     if peripheral.getType(sSide) == "modem" and peripheral.call(sSide, "isWireless") then
  153.         sModemSide = sSide
  154.         break
  155.     end
  156. end
  157.  
  158. if not sModemSide then isConnect = -1 end
  159.  
  160. if args[1] and type(args[1] + 0) == "number" then
  161.     address = args[1] + 0
  162. end
  163.  
  164. if sModemSide and address then
  165.     rednet.open(sModemSide)
  166.     isConnect = 1
  167. end
  168.  
  169. if term.isColor() then
  170.     parallel.waitForAll(GUI, UserControl, Mine, MessageHandler)
  171. else
  172.     parallel.waitForAll(ncGUI, UserControl, Mine, MessageHandler)
  173. end
  174. rednet.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement