LezChap

FluidCow Sorter 2

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