Advertisement
Pirnogion

MiningTurtleServer

Jan 19th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 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", "Commands: 'p', 's', 'b'.", ">", colors.green, colors.white, nil, nil, 2, 3)
  7. local FuelPoint = GUIElements.CreateBlinkPoint("FP", "Fuel: " .. 0, ">", 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 serveraddress = nil
  12. local running       = true
  13. local status        = "paused"
  14. local isConnect     = 0 -- '-1' - no modem; '0' - connect; '1' - connected.
  15. local fuel          = 0
  16. local statcolor     = {}
  17.       statcolor["mining"]       = colors.green
  18.       statcolor["paused"]       = colors.yellow
  19.       statcolor["no_fuel"]      = colors.red
  20.       statcolor["undefined"]    = colors.gray
  21.      
  22. --Utils
  23. local function Exit()
  24.     running = false
  25.    
  26.     GUIElements.DestroyBlinkPoint(StatusPoint.name)
  27.     GUIElements.DestroyBlinkPoint(InfoPoint.name)
  28.     GUIElements.DestroyBlinkPoint(FuelPoint.name)
  29.     GUIElements.DestroyBlinkPoint(ConnectPoint.name)
  30.    
  31.     term.setBackgroundColor(colors.black)
  32.     term.setTextColor(colors.white)
  33.     term.setCursorPos(1, 1)
  34.     term.clear()
  35. end
  36.  
  37. --message status fuel ... end
  38. local function ParseMessage(msg)
  39.     local _msg = msg
  40.     local strings = {}
  41.    
  42.     while _msg ~= "end" do
  43.         local si = string.find(_msg, "%s")
  44.         table.insert(strings, string.sub(_msg, 1, si-1))
  45.         _msg = string.sub(_msg, si+1, -1)
  46.     end
  47.    
  48.     if strings[1] == "packet" then
  49.         status = strings[2]
  50.         fuel = strings[3]
  51.     end
  52. end
  53.  
  54. --Program
  55. local function GUI()
  56.     term.setBackgroundColor(colors.white)
  57.     term.setTextColor(colors.gray)
  58.     term.clear()
  59.  
  60.     while running do
  61.         StatusPoint.text   = status
  62.         StatusPoint.fcolor = statcolor[status]
  63.         FuelPoint.text = "Fuel: " .. fuel
  64.         GUIElements.DrawPoints()
  65.         os.sleep(1)
  66.     end
  67. end
  68.  
  69. local function ncGUI()
  70.     term.setBackgroundColor(colors.black)
  71.     term.setTextColor(colors.white)
  72.     term.clear()
  73.  
  74.     StatusPoint.fcolor  = colors.white
  75.     InfoPoint.fcolor    = colors.white
  76.     FuelPoint.fcolor    = colors.white
  77.     ConnectPoint.fcolor = colors.white
  78.    
  79.     StatusPoint.scolor  = colors.black
  80.     InfoPoint.scolor    = colors.black
  81.     FuelPoint.scolor    = colors.black
  82.     ConnectPoint.scolor = colors.black
  83.    
  84.     StatusPoint.ftcolor  = colors.white
  85.     InfoPoint.ftcolor    = colors.white
  86.     FuelPoint.ftcolor    = colors.white
  87.     ConnectPoint.ftcolor = colors.white
  88.    
  89.     StatusPoint.stcolor  = colors.black
  90.     InfoPoint.stcolor    = colors.black
  91.     FuelPoint.stcolor    = colors.black
  92.     ConnectPoint.stcolor = colors.black
  93.    
  94.     while running do
  95.         StatusPoint.text   = status
  96.         FuelPoint.text = "Fuel: " .. fuel
  97.         GUIElements.DrawPoints()
  98.         os.sleep(1)
  99.     end
  100. end
  101.  
  102. local function UserControl()
  103.   while running do
  104.     local event, par1, par2, par3 = os.pullEvent()
  105.    
  106.     if (event == "char") then
  107.       if par1 == "p" then
  108.         rednet.send(address, "paused", "TurtleControl1")
  109.       elseif par1 == "s" then
  110.         rednet.send(address, "stop", "TurtleControl1")
  111.         Exit()
  112.       end
  113.     elseif (event == "rednet_message" and par1 == address and par3 == "TurtleControl1") then
  114.         ParseMessage(par2)
  115.         if status == "stop" then Exit() end
  116.     end
  117.   end
  118. end
  119.  
  120. local sModemSide = nil
  121. for n, sSide in ipairs(rs.getSides()) do
  122.     if peripheral.getType(sSide) == "modem" and peripheral.call(sSide, "isWireless") then
  123.         sModemSide = sSide
  124.         break
  125.     end
  126. end
  127.  
  128. if not sModemSide then Exit() end
  129.  
  130. if args[1] and type(args[1] + 0) == "number" then
  131.     address = args[1] + 0
  132. else Exit() end
  133.  
  134. if sModemSide and address then
  135.     rednet.open(sModemSide)
  136.     isConnect = 0
  137. end
  138.  
  139. if term.isColor() then
  140.     parallel.waitForAll(GUI, UserControl)
  141. else
  142.     parallel.waitForAll(ncGUI, UserControl)
  143. end
  144. rednet.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement