akihex

autoInfGUI

Aug 11th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.44 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local jarMaxAmount = 64
  4.  
  5. local wifi = peripheral.wrap("bottom")
  6. if not wifi then wifi = peripheral.wrap("back") end
  7. local channel = {45,46,47}
  8. --if #args > 0 then channel = tonumber(args[1]) end
  9. for i=1,3 do
  10.   channel[i] = #args >= i and tonumber(args[i]) or channel[i]
  11.   print("Opening wifi channel ",channel[i])
  12.   wifi.open(channel[i])
  13. end
  14.  
  15. print("Press and hold CTRL R to reboot if GUI doesn't show")
  16.  
  17. --wifi.transmit(channel,channel,"hello turtle")
  18.  
  19. local function oldprintRecipe(recipe)
  20.   print(math.floor(recipe[1])," ",recipe[14])
  21.   for i=2,13 do
  22.     term.write(math.floor(recipe[i]).." ")
  23.     if (i-1) % 4 == 0 then print("") end
  24.   end
  25. end
  26.  
  27. local function printRecipe(recipe)
  28.   print(math.floor(recipe[1][1]),":",math.floor(recipe[1][2]) or 0," ",recipe[14][1])
  29.   for i=2,13 do
  30.     term.write(math.floor(recipe[i][1])..":"..(recipe[i][2] and math.floor(recipe[i][2]) or 0).." ")
  31.     if (i-1) % 4 == 0 then print("") end
  32.   end
  33.   --print("")
  34. end
  35.  
  36. local function printAllRecipes(recipes)
  37.   local counter = 0
  38.   for _,recipe in pairs(recipes) do
  39.     --if counter > 0 and counter % 4 == 0 then print("press a key to continue") local x,y=term.getCursorPos() os.pullEvent("key") term.setCursorPos(x,y) print(" ") term.setCursorPos(x,y) end
  40.     if counter > 0 and counter % 4 == 0 then print("press a key to continue") local _,key = os.pullEvent("key") end
  41.     printRecipe(recipe)
  42.     counter = counter + 1
  43.   end
  44.   term.clearLine()
  45. end
  46.  
  47. -- http://lua-users.org/wiki/SplitJoin
  48. local function split(str, pat)
  49.    local t = {}
  50.    local fpat = "(.-)" .. pat
  51.    local last_end = 1
  52.    local s, e, cap = str:find(fpat, 1)
  53.    while s do
  54.       if s ~= 1 or cap ~= "" then
  55.          table.insert(t,cap)
  56.       end
  57.       last_end = e+1
  58.       s, e, cap = str:find(fpat, last_end)
  59.    end
  60.    if last_end <= #str then
  61.       cap = str:sub(last_end)
  62.       table.insert(t, cap)
  63.    end
  64.    return t
  65. end
  66.  
  67. local jars = {}
  68. local sumJars = {}
  69.  
  70. local function callJarHead(num)
  71.   wifi.transmit(channel[num],channel[num],"")
  72.   local _,_,_,_,message,_ = os.pullEvent("modem_message")
  73.   return message
  74. end
  75.  
  76. local function getJars()
  77.   sumJars=callJarHead(2)
  78.   for essentia,amount in pairs(callJarHead(3)) do
  79.     sumJars[essentia] = sumJars[essentia] and sumJars[essentia]+amount or amount
  80.   end
  81.   jars[1]=callJarHead(2)
  82.   jars[2]=callJarHead(3)
  83. end
  84.  
  85. local function printMissingEssentia()
  86.   for essentia,amount in pairs(sumJars) do
  87.     if amount < jarMaxAmount * 2 then print(essentia," ",amount) end
  88.   end
  89. end
  90.  
  91. if #args > 3 then
  92.   local input = nil
  93.   repeat
  94.     input = read()
  95.     if input ~= "bye" and input ~= "exit" then
  96.       local command = split(input,"|")
  97.       if command[1] == "getJars" then getJars() printMissingEssentia()
  98.       else
  99.         wifi.transmit(channel[1],channel[1],{["id"]=command[1],["info"]=command[2]})
  100.         if command[1] ~= "reboot" then
  101.           local _,_,_,_,message,_ = os.pullEvent("modem_message")
  102.           if message.id == "altar" then printRecipe(message.result)
  103.           elseif message.id == "recipes" then printAllRecipes(message.result)
  104.           else print(message.id," ",message.result) end
  105.         end
  106.       end
  107.     end
  108.   until input == "bye" or input == "exit"
  109. else
  110.  
  111.   -- GUI
  112.  
  113.   local function padStr(str, len)
  114.     if #str > len then do return string.sub(str,1,len) end
  115.     else do return str .. string.rep(' ', len - #str) end
  116.     end
  117.   end  
  118.  
  119.   local termX, termY = term.getSize()
  120.   local wind = window.create(term.current(), 1, 1, termX, termY, false)
  121.   local windPosX, windPosY = wind.getPosition()
  122.   local bgColor = colors.white
  123.   local fgColor = colors.blue
  124.   local altFgColor = colors.cyan
  125.   -- empty, > 10, > 32, > 50, full
  126.   local jarAlert = {0,10,32,50,64}
  127.   local jarColor = {colors.red, colors.orange, colors.yellow, colors.lime, colors.green}
  128.  
  129.   local button = {}
  130.  
  131.   local function colorJars(amount)
  132.     local color
  133.     if amount == jarAlert[5] then color = jarColor[5]
  134.     elseif amount > jarAlert[4] then color = jarColor[4]
  135.     elseif amount > jarAlert[3] then color = jarColor[3]
  136.     elseif amount > jarAlert[2] then color = jarColor[2]
  137.     else color = jarColor[1]
  138.     end
  139.     return color
  140.   end
  141.  
  142.   local jarWind = window.create(wind,1,4,termX,2,false)
  143.   local jarWindPosX, jarWindPosY = jarWind.getPosition()
  144.   jarWindPosX = jarWindPosX + windPosX - 1
  145.   jarWindPosY = jarWindPosY + windPosY - 1
  146.  
  147.   local function drawJars()
  148.     jarWind.setCursorPos(1,1)
  149.     local counter = 1
  150.     for name,amount in pairs(jars[1]) do
  151.       if counter > termX then break end
  152.       jarWind.setBackgroundColor(colorJars(amount))
  153.       jarWind.write(" ")
  154.       counter = counter + 1    
  155.     end
  156.     jarWind.setCursorPos(1,2)
  157.     counter = 1
  158.     for name,amount in pairs(jars[2]) do
  159.       if counter > termX then break end
  160.       jarWind.setBackgroundColor(colorJars(amount))
  161.       jarWind.write(" ")
  162.       counter = counter + 1
  163.     end
  164.     wind.setCursorPos(1,6)
  165.   end
  166.  
  167.   local function redrawJars()
  168.     jarWind.setVisible(false)
  169.     getJars()
  170.     drawJars()
  171.     jarWind.setVisible(true)
  172.     --wind.setCursorPos(1,7)
  173.     --wind.write("refreshed")
  174.   end
  175.  
  176.   altarWind = window.create(wind,1,7,termX,6,false)
  177.  
  178.   local function getAltar()
  179.     wifi.transmit(channel[1],channel[1],{id="altar",info=""})
  180.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  181.     return message.result
  182.   end
  183.  
  184.   local function drawRecipe(w,recipe)
  185.     if #recipe < 14 then return false end
  186.     w.setBackgroundColor(bgColor)
  187.     w.setTextColor(fgColor)
  188.     w.clear()
  189.     w.setCursorPos(11,1)
  190.     w.write(recipe[14][1] and recipe[14][1] or " ")
  191.  
  192. --    local map = {{1,1}, {21,2}, {31,2}, {31,3}, {31,4}, {31,5}, {21,5}, {11,5}, {1,5}, {1,4}, {1,3}, {1,2}, {11,2}}
  193.     local map = {{1,1}, {27,2}, {40,2}, {40,3}, {40,4}, {40,5}, {27,5}, {14,5}, {1,5}, {1,4}, {1,3}, {1,2}, {14,2}}
  194.     for i=1,13 do
  195.       w.setCursorPos(map[i][1]>1 and map[i][1]-1 or map[i][1],map[i][2])
  196.       if map[i][1] > 1 then w.write(" ") end
  197.       --w.write(math.floor(recipe[i][1])..(recipe[i][2] and (":"..math.floor(recipe[i][2])) or 0))
  198.       w.write(recipe[i][3] and ((recipe[i][3]==" " or recipe[i][3]=="") and "-" or recipe[i][3]) or (math.floor(recipe[i][1])..(recipe[i][2] and (":"..math.floor(recipe[i][2])) or 0)))
  199.     end
  200.   end
  201.  
  202.   local function drawAltar()
  203.     local altar = getAltar()
  204.     drawRecipe(altarWind,altar)
  205.   end
  206.  
  207.   local function redrawAltar()
  208.     drawAltar()
  209.   end
  210.  
  211.   local function redrawJarsNAltar()
  212.     redrawJars()
  213.     redrawAltar()
  214.   end
  215.  
  216.   local rWind = window.create(wind,1,13,termX,7,false)
  217.   local rWindPosX, rWindPosY = rWind.getPosition()
  218.  
  219.   local function getRecipes()
  220.     wifi.transmit(channel[1],channel[1],{id="recipes",info=""})
  221.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  222.     return message.result
  223.   end
  224.  
  225.   local function getRecipeNames()
  226.     local recipes = getRecipes()
  227.     local names = {}
  228.     for _,recipe in pairs(recipes) do
  229.       if #recipe >= 14 then
  230.         names[#names+1] = recipe[14][1] and recipe[14][1] or (type(recipe[14]) ~= "table" and recipe[14] or "-")
  231.       end
  232.     end
  233.     return names
  234.   end
  235.  
  236.   local function loadR(name)
  237.     wifi.transmit(channel[1],channel[1],{["id"]="loadR",["info"]=name})
  238.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  239.     redrawAltar()
  240.   end
  241.  
  242.   local function drawRecipes()
  243.     local names = getRecipeNames()
  244.     rWind.setCursorPos(1,1)
  245.     rWind.setBackgroundColor(bgColor)
  246.     rWind.setTextColor(altFgColor)
  247.     rWind.clear()
  248.     for i=1,#names do
  249.       rWind.setCursorPos(1,i)
  250.       rWind.write(names[i])
  251.       button[i+rWindPosY-1] = {{["x"]=0,["func"]=loadR,["data"]=names[i]}}
  252.     end
  253.   end
  254.  
  255.   local function redrawRecipes()
  256.     drawRecipes()
  257.   end
  258.  
  259.   local function refresh()
  260.     redrawJarsNAltar()
  261.     redrawRecipes()
  262.   end
  263.  
  264.   local function askForName()
  265.     altarWind.setBackgroundColor(fgColor)
  266.     altarWind.setTextColor(bgColor)
  267.     altarWind.clear()
  268.     altarWind.setCursorPos(1,2)
  269.     altarWind.write("Please enter a name to store the current recipe")
  270.     altarWind.setCursorPos(1,3)
  271.     local input = read()
  272.     redrawAltar()
  273.     return input
  274. --(input and #input > 0) and input or false
  275.   end
  276.  
  277.   local function addR()
  278.     local name = askForName()
  279.     if name then
  280.       wifi.transmit(channel[1],channel[1],{["id"]="add",["info"]=name})
  281.       local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  282.     end
  283.   end  
  284.  
  285.   local function prepareWindow()
  286.     wind.setVisible(false)
  287.     wind.setBackgroundColor(bgColor)
  288.     wind.setTextColor(fgColor)
  289.     wind.clear()
  290.     wind.setCursorPos(1,1)
  291.     wind.write("Infusion Altar Control Center")
  292.     wind.setCursorPos(1,3)
  293. --    wind.write(termX.." "..termY)
  294. --    wind.setCursorPos(1,1)
  295. --    wind.setBackgroundColor(bgColor)
  296. --    wind.setTextColor(fgColor)
  297.     wind.write(padStr("Essentia Storage",termX))
  298.     wind.setTextColor(altFgColor)
  299.     wind.setCursorPos(termX-10,3)
  300.     wind.write("Refresh")
  301.     local _,ypos = wind.getCursorPos()
  302.     button[ypos + windPosY - 1] = {{["x"]=0,["func"]=refresh,["data"]=""}}
  303.     wind.setCursorPos(termX-10,6)
  304.     wind.write("Add Recipe")
  305.     _,ypos = wind.getCursorPos()
  306.     button[ypos + windPosY - 1] = {}
  307.     for i=0,9 do
  308.       table.insert(button[ypos + windPosY - 1],{["x"]=termX-10+i,["func"]=addR,["data"]=""})
  309.     end
  310.     wind.setTextColor(fgColor)
  311.     wind.setCursorPos(1,4)
  312.     drawJars()
  313.     drawAltar()
  314.     drawRecipes()
  315.     wind.setVisible(true)
  316.     jarWind.setVisible(true)
  317.     altarWind.setVisible(true)
  318.     rWind.setVisible(true)
  319.   end
  320.  
  321.   local function drawUserInterface()
  322.     wind.redraw()
  323.   end
  324.  
  325.   getJars()
  326.   prepareWindow()
  327.  
  328.   while true do
  329.     drawUserInterface()
  330.     local event, mousebutton, xPos, yPos = os.pullEvent()
  331.     if event == "mouse_click" and mousebutton == 1 then
  332.       if button[yPos] then
  333.         for _,b in pairs(button[yPos]) do
  334.           if b.x == 0 or b.x == xPos then
  335.             b.func(b.data)
  336.             break
  337.           end
  338.         end
  339.       end
  340.     end
  341.   end
  342.   --wifi.transmit(channel[1],channel[1],{id="altar",info=""})
  343.   --local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  344.   --print(message.id," ",message.result[1])
  345. end
Advertisement
Add Comment
Please, Sign In to add comment