LezChap

Fluid Cow Sorter

May 28th, 2016
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.55 KB | None | 0 0
  1. os.loadAPI("touchpoint") --Personalized and modified Touchpoint API
  2. -- API Pastebin: 5QLmjqT1
  3. local storage = peripheral.wrap("right")
  4. local chestDir = "east"
  5. local chestSize = 54
  6. local mon = peripheral.wrap("top")
  7.  
  8. local totalCows = 0
  9. local cowList = {}
  10. local cowListNames = {}
  11. mon.setTextScale(1)
  12. local maxX, maxY = mon.getSize()
  13. local keyboardPos = {}
  14. local searchChars = {}
  15. local currPage = 1
  16. local lastPage = 1
  17. local tp
  18. local buttonList = {}
  19. local emptyLabel = {
  20.   " Empty Chest ",
  21.   "Into Storage ",
  22.   label = "emptyChest"
  23. }
  24. local refreshLabel = {
  25.   "  Refresh  ",
  26.   "  Screen   ",
  27.   label = "refreshScreen"
  28. }
  29. local searchLabel = {
  30.   " _ ",
  31.   "|_|",
  32.   "/  ",
  33.   label = "search"
  34. }
  35.  
  36. local pages = {}
  37. for i=1,5,1 do
  38.   pages[i] = touchpoint.new("top")
  39.   pages[i]:add(emptyLabel, emptyChest, 1, maxY-1, 14, maxY, colors.blue, colors.lightBlue)
  40.   pages[i]:add(refreshLabel, refreshScreen, maxX-10, maxY-1, maxX, maxY, colors.blue, colors.lightBlue)
  41.   pages[i]:add(searchLabel, searchCows, 20, maxY-2, 22, maxY, colors.purple, colors.magenta)
  42. end
  43. pages["search"] = touchpoint.new("top")
  44.  
  45. function refreshScreen(str)
  46.   --doesn't need to do anything else
  47. end
  48.  
  49. function emptyChest(str)
  50.   local cnt = 0
  51.   for i = 1, 54, 1 do
  52.     local num = storage.pullItem(chestDir, i)
  53.     cnt = cnt + num
  54.   end
  55.   print("Moved "..cnt.." items into storage.")
  56.   pollStorage()
  57.   makeCowList()
  58. end
  59.  
  60. function nextPage(str)
  61.   lastPage = currPage
  62.   currPage = currPage + 1
  63.   if currPage > 5 then
  64.     print("too many pages!")
  65.     currPage = 5
  66.   end
  67.   tp = pages[currPage]
  68. end
  69.  
  70. function prevPage(str)
  71.   lastPage = currPage
  72.   currPage = currPage - 1
  73.   if currPage < 1 then
  74.     print("Trying to go before page 1, ERROR!")
  75.     currPage = 1
  76.   end
  77.   tp = pages[currPage]
  78. end
  79.  
  80. function searchCows(str)
  81.   print("opening Search page?")
  82.   lastPage = currPage
  83.   currPage = "search"
  84.   tp = pages[currPage]
  85.   print("currPage: "..currPage)
  86. end
  87.  
  88. function testThis()
  89. end
  90.  
  91. function closeSearch(str)
  92.   currPage = lastPage
  93.   lastPage = "search"
  94.   tp = pages[currPage]
  95. end
  96.  
  97.  
  98. -- function drawBox(StartX, lengthX, StartY, lengthY, Text, Color, BkgColor) -- does what is says on the tin.
  99.   -- --local x, y = term.getSize()
  100.   -- local oldColor = mon.getTextColor()
  101.   -- local oldBG = mon.getBackgroundColor()
  102.   -- if Color then mon.setTextColor(Color) end
  103.   -- if BkgColor then mon.setBackgroundColor(BkgColor) end
  104.   -- if not Text then Text = " " end
  105.   -- lengthX = lengthX - 1
  106.   -- lengthY = lengthY - 1
  107.   -- EndX = StartX + lengthX  
  108.   -- EndY = StartY + lengthY
  109.   -- mon.setCursorPos(StartX, StartY)
  110.   -- mon.write(string.rep(Text, lengthX))
  111.   -- mon.setCursorPos(StartX, EndY)
  112.   -- mon.write(string.rep(Text, lengthX))
  113.   -- for i = StartY, EndY do
  114.     -- mon.setCursorPos(StartX, i)
  115.     -- mon.write(Text)
  116.     -- mon.setCursorPos(EndX, i)    
  117.     -- mon.write(Text)
  118.   -- end
  119.   -- mon.setTextColor(oldColor)
  120.   -- mon.setBackgroundColor(oldBG)
  121.   -- return true  
  122. -- end
  123.  
  124. function makeKeyboard(leftX, topY)
  125.   keyboardPos.x = leftX
  126.   keyboardPos.y = topY
  127.   tRow1 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "(", ")",}
  128.   tRow2 = {"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", ":", "_",}
  129.   tRow3 = {"A", "S", "D", "F", "G", "H", "J", "K", "L", "#", ";", "=",}
  130.   tRow4 = {"/", "Z", "X", "C", "V", "B", "N", "M", "@", "~", "'", "+",}
  131.   tRow5 = {"[", "]", "{", "}", "&", "$", "%", "-", "*", "!", "?", ".",}
  132.  
  133.   -- addBox(name, startX, Xsize, startY, Ysize, textColor, BGColor)
  134.   pages["search"]:addBox("box1", leftX, 30, topY, 10, colors.white, colors.blue)
  135.   pages["search"]:addBox("box2", leftX, 28, topY, 1, colors.black, colors.white)
  136.   pages["search"]:addBox("box3", leftX, 28, topY, 5, colors.black, colors.white)
  137.   spaceLabel = {
  138.     "  SPACE  ",
  139.     label = "SPACE"
  140.   }
  141.   pages["search"]:add(spaceLabel, pressKey, leftX+3, topY+9, leftX+11, topY+9, colors.black, colors.white)
  142.   backspaceLabel = {
  143.     "  <-BS-  ",
  144.     label = "<-"
  145.   }
  146.   pages["search"]:add(backspaceLabel, pressKey, leftX+13, topY+9, leftX+21, topY+9, colors.black, colors.white)
  147.   pages["search"]:add(">", nil, leftX+1, topY+1, leftX+1, topY+1, colors.gray, colors.white)
  148.  
  149.   i = leftX + 1
  150.   for _, v in ipairs(tRow1) do
  151.           pages["search"]:add(v, pressKey, i, topY+4, i, topY+4, colors.black, colors.white)
  152.           i = i + 2
  153.   end
  154.   i = leftX + 1
  155.   for _,v in ipairs(tRow2) do
  156.           pages["search"]:add(v, pressKey, i, topY+5, i, topY+5, colors.black, colors.white)
  157.           i = i + 2
  158.   end
  159.   i = leftX + 1
  160.   for _,v in ipairs(tRow3) do
  161.           pages["search"]:add(v, pressKey, i, topY+6, i, topY+6, colors.black, colors.white)
  162.           i = i + 2
  163.   end
  164.   i = leftX + 1
  165.   for _,v in ipairs(tRow4) do
  166.           pages["search"]:add(v, pressKey, i, topY+7, i, topY+7, colors.black, colors.white)
  167.           i = i + 2
  168.   end
  169.   i = leftX + 1
  170.   for _,v in ipairs(tRow5) do
  171.           pages["search"]:add(v, pressKey, i, topY+8, i, topY+8, colors.black, colors.white)
  172.           i = i + 2
  173.   end
  174. end
  175. local closeLabel = {
  176.   "X",
  177.   label = "CLOSE"
  178. }
  179. pages["search"]:add(closeLabel, closeSearch, maxX-1, 1, maxX, 1, colors.red, colors.green)
  180. makeKeyboard(1, 1)
  181.  
  182. function pressKey(str)
  183.   local searchString = ""
  184.   if str == "<-" then
  185.     table.remove(searchChars)
  186.   else
  187.     table.insert(searchChars, str)
  188.   end
  189.   for pos, searchChar in ipairs(searchChars) do
  190.     pages["search"]:add(searchChar, nil, keyboardPos.x+2+pos, keyboardPos.y+1, keyboardPos.x+2+pos, keyboardPos.y+1, colors.black, colors.white, colors.white, colors.black)
  191.     searchString = searchString..searchChar
  192.   end
  193.   searchString = string.upper(searchString)
  194. end
  195.  
  196.  
  197. local prevLabel = {
  198.   "   ",
  199.   " < ",
  200.   "   ",
  201.   label = "prevPage"
  202. }
  203. local nextLabel = {
  204.   "   ",
  205.   " > ",
  206.   "   ",
  207.   label = "nextPage"
  208. }
  209.  
  210.  
  211. function pollStorage()
  212.   local tbl = storage.getAllStacks()
  213.   totalCows = #tbl
  214.   cowList = {}
  215.   cowListNames = {}
  216.   for k,v in ipairs(tbl) do
  217.     local slot = v.all()
  218.     if cowList[slot.display_name] then
  219.       table.insert(cowList[slot.display_name].inSlots, k)
  220.     else
  221.       table.insert(cowListNames, slot.display_name)
  222.       cowList[slot.display_name] = {}
  223.       cowList[slot.display_name].inSlots = {}
  224.       cowList[slot.display_name].inSlots[1] = k
  225.     end
  226.   end
  227. end
  228.  
  229. function moveCows(str)
  230.   local line = tonumber(string.match(str, "%d+"))
  231.   for i = #cowList[cowListNames[line]].inSlots, 1, -1 do
  232.     local num = storage.pushItem(chestDir, cowList[cowListNames[line]].inSlots[i])
  233.     if num == 0 or num == nil then
  234.       print("error moving "..cowListNames[line].." from slot "..cowList[cowListNames[line]].inSlots[i])
  235.       break;
  236.     end
  237.   end
  238. end
  239.  
  240. function makeCowList()
  241.   for page,buttons in pairs(buttonList) do
  242.     temp = pages[page]
  243.     for k,button in pairs(buttons) do
  244.       temp:remove(button)
  245.     end
  246.   end
  247.   temp = pages[1]
  248.   local pageCnt = 1
  249.   buttonList = {}
  250.   buttonList[pageCnt] = {}
  251.   local line
  252.   for k,v in ipairs(cowListNames) do
  253.     if k % 16 == 0 then
  254.       pageCnt = math.floor(k/16) + 1
  255.       buttonList[pageCnt] = {}
  256.       temp:remove("nextPage")
  257.       temp:add(nextLabel, nextPage, 24, maxY-2, 26, maxY, colors.cyan, colors.lightBlue)
  258.       temp = pages[pageCnt]
  259.       temp:remove("prevPage")
  260.       temp:add(prevLabel, prevPage, 16, maxY-2, 18, maxY, colors.cyan, colors.lightBlue)
  261.     end
  262.      
  263.     if pageCnt > 1 then
  264.       line = k - ((pageCnt - 1) * 16) + 1
  265.     else
  266.       line = k
  267.     end
  268.     local str = k..". "..v..": "..tostring(#cowList[v].inSlots).." -----------------"
  269.     temp:add(str, nil, 1, line, maxX-7, line, colors.black, colors.black)
  270.     table.insert(buttonList[pageCnt], str)
  271.     temp:add("Move "..k, moveCows, maxX-6, line, maxX-1, line, colors.red, colors.green)
  272.     table.insert(buttonList[pageCnt], "Move "..k)
  273.     --print("Total "..k..": "..tostring(#v.inSlots))
  274.     --12. Nutrient Distillation total: 12
  275.   end
  276.   temp:add("Total Cows: "..totalCows, nil, 3, line+1, 22, line+1, colors.black, colors.black, colors.gray, colors.gray)
  277.   table.insert(buttonList[pageCnt], "Total Cows: "..totalCows)
  278. end
  279.  
  280. pollStorage()
  281. makeCowList()
  282. tp = pages[1]
  283. tp:draw()
  284. os.startTimer(5)
  285.  
  286. while true do
  287.  
  288.   local event, p1 = tp:handleEvents(os.pullEvent())   ---button_click, name
  289.   if event == "button_click" then
  290.     --remove toggling and simplify button running
  291.     if type(tp.buttonList[p1].func) == "function" then
  292.         tp:flash(p1)
  293.         tp.buttonList[p1].func(p1)
  294.     end
  295.     pollStorage()
  296.     makeCowList()
  297.     tp:draw()
  298.   end
  299.   if event == "timer" then
  300.     os.startTimer(5)
  301.   end
  302. end
Advertisement
Add Comment
Please, Sign In to add comment