Advertisement
mrkmg

Min count CC AE

Feb 1st, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.30 KB | None | 0 0
  1. ----- ME Network Minimum Count -----
  2. -- Created by: MrKMG   V.1
  3. -- Please keep this header
  4.  
  5.  
  6. --Sides of
  7. monitor_location = "left"
  8. chest_location = "top"
  9. menet_location = "bottom"
  10.  
  11. -- Warning!!!
  12. -- Nothing should be changed below
  13.  
  14.  
  15. -- Wrap the peripherals
  16. local monitor = peripheral.wrap("left")
  17. local chest = peripheral.wrap("top")
  18. local menet = peripheral.wrap("bottom")
  19.  
  20. -- Setup screen variables
  21. local monitor_width
  22. local monitor_height
  23. local screen_rows
  24. local screen_columns
  25. local screen_center_x
  26. local screen_center_y
  27. local left_padding
  28.  
  29. -- Other variables
  30. local items = {}
  31. local total_items
  32. local items_per_page
  33. -- local length_names
  34. local header_height = 2
  35. local length_qty = 4
  36. local length_cur = 4
  37. local is_popup = false
  38. local current_pop = 0
  39. local pages = 0
  40. local c_page = 1
  41. local item_pos = {}
  42.  
  43. --Setup and calculate information for monitor
  44. function setupMonitor()
  45.     monitor_width, monitor_height = monitor.getSize()
  46.     screen_columns = math.floor(monitor_width/18)
  47.     screen_rows = math.floor((monitor_height - header_height)/2)
  48.     left_padding = math.floor((monitor_width - (screen_columns * 18))/2)
  49.     screen_center_x = math.floor(monitor_width/2)
  50.     screen_center_y = math.floor(monitor_height/2)
  51.     items_per_page = screen_rows * screen_columns
  52.     monitor.setTextScale(0.5)
  53.     monitor.setBackgroundColor(colors.black)
  54.     monitor.clear()
  55. end
  56.  
  57. function printHeader()
  58.     monitor.setBackgroundColor(colors.gray)
  59.     monitor.setTextColor(colors.white)
  60.     monitor.setCursorPos(screen_center_x - 12,1)
  61.     monitor.clearLine()
  62.     monitor.write("MENetwork Minimum Counts")
  63. end
  64.  
  65. function printFooter()
  66.     monitor.setBackgroundColor(colors.gray)
  67.     monitor.setCursorPos(2,monitor_height)
  68.     monitor.clearLine()
  69.     monitor.setBackgroundColor(colors.green)
  70.     monitor.setTextColor(colors.white)
  71.     monitor.write("Prev")
  72.     monitor.setCursorPos(8,monitor_height)
  73.     monitor.write("Scan")
  74.     monitor.setCursorPos(14,monitor_height)
  75.     monitor.write("Save")
  76.     monitor.setCursorPos(20,monitor_height)
  77.     monitor.write("Load")
  78.     monitor.setCursorPos(26,monitor_height)
  79.     monitor.write("Next")
  80. end
  81.  
  82. -- TODO Use this to parse a more readable name
  83. function getNameOfItem(raw)
  84.     return raw
  85. end
  86.  
  87. -- Scan items in chest and add them to the system - Will also reset count to 1
  88. function scanItems()
  89.     local i, item, rawName
  90.     for i,item in pairs(chest.getAllStacks()) do
  91.         rawName = item.rawName
  92.         items[rawName] = {}
  93.         items[rawName].id = item.id
  94.         items[rawName].dmg = item.dmg
  95.         items[rawName].needed = 1
  96.         items[rawName].name = item.name
  97.         items[rawName].qty = 0
  98.         items[rawName].prev_qty = 0
  99.         items[rawName].is_error = false
  100.         items[rawName].did_request = false
  101.         items[rawName].request_timeout = 0
  102.     end
  103. end
  104.  
  105. function getItemCounts()
  106.     local i, item
  107.     for i,item in pairs(items) do
  108.         items[i].qty = menet.countOfItemType(item.id,item.dmg)
  109.     end
  110. end
  111.  
  112. function requestItem(rawName)
  113.     local requestItem = {}
  114.     requestItem.id = items[rawName].id
  115.     requestItem.dmg = items[rawName].dmg
  116.     requestItem.qty = 1
  117.     menet.requestCrafting(requestItem)
  118.     items[rawName].did_request = true
  119.     items[rawName].request_timeout = 60
  120.     items[rawName].prev_qty = items[rawName].qty
  121. end
  122.  
  123. function checkAllItems()
  124.     local i, item
  125.     for i,item in pairs(items) do
  126.         if item.did_request then
  127.             if item.qty ~= item.prev_qty then
  128.                 items[i].did_request = false
  129.             elseif item.request_timeout == 0 then
  130.                 items[i].did_request = false
  131.                 items[i].is_error = true
  132.             else
  133.                 items[i].request_timeout = item.request_timeout - 1
  134.             end
  135.         end
  136.         if item.qty < item.needed and item.did_request == false and item.is_error == false then
  137.             requestItem(i)
  138.         end
  139.     end
  140. end
  141.  
  142. function showPopup(rawName)
  143.     if items[rawName].did_request then
  144.         monitor.setBackgroundColor(colors.yellow)
  145.         monitor.setTextColor(colors.yellow)
  146.     elseif items[rawName].is_error then
  147.         monitor.setBackgroundColor(colors.red)
  148.         monitor.setTextColor(colors.red)
  149.     else
  150.         monitor.setBackgroundColor(colors.green)
  151.         monitor.setTextColor(colors.green)
  152.     end
  153.     monitor.setCursorPos(screen_center_x - 12,screen_center_y-2)
  154.     monitor.write("                        ")
  155.     monitor.setCursorPos(screen_center_x - 12,screen_center_y-1)
  156.     monitor.write("                        ")
  157.     monitor.setCursorPos(screen_center_x - 12,screen_center_y)
  158.     monitor.write("                        ")
  159.     monitor.setCursorPos(screen_center_x - 12,screen_center_y+1)
  160.     monitor.write("                        ")
  161.     monitor.setCursorPos(screen_center_x - 12,screen_center_y+2)
  162.     monitor.write("                        ")
  163.     monitor.setTextColor(colors.black)
  164.     monitor.setCursorPos(screen_center_x + 11,screen_center_y-2)
  165.     monitor.write("X")
  166.     monitor.setCursorPos(screen_center_x - 9,screen_center_y-1)
  167.     monitor.write(string.sub(items[rawName].name,-18))
  168.     monitor.setCursorPos(screen_center_x - 10,screen_center_y)
  169.     monitor.write(items[rawName].qty)
  170.     monitor.setCursorPos(screen_center_x + 1 ,screen_center_y)
  171.     monitor.write(items[rawName].needed)
  172.     monitor.setCursorPos(screen_center_x - 3, screen_center_y + 1)
  173.     monitor.write("<")
  174.     monitor.setCursorPos(screen_center_x - 7, screen_center_y + 1)
  175.     monitor.write("<<")
  176.     monitor.setCursorPos(screen_center_x - 11, screen_center_y + 1)
  177.     monitor.write("<<<")
  178.     monitor.setCursorPos(screen_center_x + 1,screen_center_y + 1)
  179.     monitor.write(">")
  180.     monitor.setCursorPos(screen_center_x + 4,screen_center_y + 1)
  181.     monitor.write(">>")
  182.     monitor.setCursorPos(screen_center_x + 8,screen_center_y + 1)
  183.     monitor.write(">>>")
  184.     is_popup = true
  185.     current_pop = rawName
  186. end
  187.  
  188. function calcTotalItems()
  189.     total_items = 0
  190.     local i,j
  191.     for i,j in pairs(items) do
  192.         total_items = total_items + 1
  193.     end
  194. end
  195.  
  196. function calcPages()
  197.     pages = math.floor(total_items / items_per_page)
  198. end
  199.  
  200. function clearPageArea()
  201.     monitor.setBackgroundColor(colors.black)
  202.     for i=header_height+1,monitor_height-1 do
  203.         monitor.setCursorPos(1,i)
  204.         monitor.clearLine()
  205.     end
  206. end
  207.  
  208. function printPage()
  209.     local starting_item = (c_page - 1) * items_per_page
  210.     local ending_item = (c_page) * items_per_page
  211.     local rawName, item
  212.     item_pos = {}
  213.     local i = 0
  214.     local r = 1
  215.     local c = 1
  216.     for rawName,item in pairs(items) do
  217.         if i >= starting_item and i < ending_item then
  218.             if c == 1 then
  219.                 item_pos[r] = {}
  220.                 monitor.setBackgroundColor(colors.black)
  221.                 monitor.setCursorPos(1,(r * 2) + header_height)
  222.                 monitor.clearLine()
  223.             end
  224.             if item.did_request then
  225.                 monitor.setBackgroundColor(colors.yellow)
  226.             elseif item.is_error then
  227.                 monitor.setBackgroundColor(colors.red)
  228.             else
  229.                 monitor.setBackgroundColor(colors.green)
  230.             end
  231.             monitor.setTextColor(colors.black)
  232.             monitor.setCursorPos(left_padding + ((c-1)*18) + 2,(r * 2) + header_height)
  233.             monitor.write(string.sub(item.name,-16))
  234.             item_pos[r][c] = rawName
  235.  
  236.             if c >= screen_columns then
  237.                 c = 1
  238.                 r = r + 1
  239.             else
  240.                 c = c + 1
  241.             end
  242.         end
  243.         i = i + 1
  244.     end
  245. end
  246.  
  247. function saveItems()
  248.     local file = io.open("MENetSave","w")
  249.     file:write(textutils.serialize(items))
  250.     file:close()
  251. end
  252.  
  253. function loadItems()
  254.     local file = io.open("MENetSave","r")
  255.     if file ~= nil then
  256.         items = textutils.unserialize(file:read("*a"))
  257.         file:close()
  258.     else
  259.         items = {}
  260.     end
  261.     refresh()
  262. end
  263.  
  264. function refresh()
  265.     calcTotalItems()
  266.     calcPages()
  267.     clearPageArea()
  268. end
  269.  
  270. function changePage()
  271.     clearPageArea()
  272. end
  273.  
  274. function onClick(x,y,side)
  275.     if is_popup then
  276.         if x == screen_center_x + 12 and y == screen_center_y - 2 then --check for close of popup
  277.             clearPageArea()
  278.             is_popup = false
  279.         elseif y == screen_center_y + 1 then -- Clicked an Arrow
  280.             if x == screen_center_x - 3 then -- Remove One
  281.                 items[current_pop].needed = items[current_pop].needed - 1
  282.                 showPopup(current_pop)
  283.             elseif x >= screen_center_x - 7 and x <= screen_center_x - 6 then -- Remove 10
  284.                 items[current_pop].needed = items[current_pop].needed - 10
  285.                 showPopup(current_pop)
  286.             elseif x >= screen_center_x - 11 and x <= screen_center_x - 9 then -- Remove 64
  287.                 items[current_pop].needed = items[current_pop].needed - 64
  288.                 showPopup(current_pop)
  289.             elseif x == screen_center_x + 2 then -- Add 1
  290.                 items[current_pop].needed = items[current_pop].needed + 1
  291.                 showPopup(current_pop)
  292.             elseif x >= screen_center_x + 5 and x <= screen_center_x + 6 then -- Add 10
  293.                 items[current_pop].needed = items[current_pop].needed + 10
  294.                 showPopup(current_pop)
  295.             elseif x >= screen_center_x + 9 and x <= screen_center_x + 11 then -- Add 64
  296.                 items[current_pop].needed = items[current_pop].needed + 64
  297.                 showPopup(current_pop)
  298.             end
  299.         end
  300.     else
  301.         if y == monitor_height then -- check if bottom row
  302.             if x >= 2 and x <= 5 then --check for Prev
  303.                 if c_page > 1 then c_page = c_page - 1 end
  304.             elseif x >= 8 and x <= 11 then --check for Scan
  305.                 print("Should Scan")
  306.                 scanItems()
  307.                 refresh()
  308.             elseif x >= 14 and x <= 17 then --check for Save
  309.                 saveItems()
  310.             elseif x >= 20 and x <= 23 then --check for Load
  311.                 loadItems()
  312.             elseif x >= 26 and x <= 29 then --check for Next
  313.                 if c_page < pages then c_page = c_page + 1 end
  314.             end
  315.         else
  316.             local isRow = (y - header_height) % 2 == 0
  317.             if isRow then
  318.                 local row = (y - header_height) / 2
  319.                 local col = (math.floor((x - left_padding) / 18) % screen_columns) + 1
  320.                 for c_row,cols in pairs(item_pos) do
  321.                     for c_col, rawname in pairs(cols) do
  322.                         if c_row == row and c_col == col then
  323.                             showPopup(rawname)
  324.                         end
  325.                     end
  326.                 end
  327.             end
  328.         end
  329.     end
  330. end
  331.  
  332. function WriteCorners()
  333.     monitor.setCursorPos(1,1)
  334.     monitor.write(" ")
  335.     monitor.setCursorPos(1,monitor_height-1)
  336.     monitor.write(" ")
  337.     monitor.setCursorPos(monitor_width-1,1)
  338.     monitor.write(" ")
  339.     monitor.setCursorPos(monitor_width-1,monitor_height-1)
  340.     monitor.write(" ")
  341. end
  342.  
  343. function showRunning()
  344.     print("Running")
  345.     monitor.setBackgroundColor(colors.red)
  346.     monitor.setTextColor(colors.red)
  347.     WriteCorners()
  348. end
  349. function showWaiting()
  350.     print("Waiting")
  351.     monitor.setBackgroundColor(colors.green)
  352.     monitor.setTextColor(colors.green)
  353.     WriteCorners()
  354. end
  355.  
  356. setupMonitor()
  357. printHeader()
  358. printFooter()
  359. loadItems()
  360. refresh()
  361. while true do
  362.     showWaiting()
  363.     os.startTimer(3)
  364.     local event,side,x,y = os.pullEvent()
  365.     showRunning()
  366.     if event == "monitor_touch" then
  367.         onClick(x,y,side)
  368.     else
  369.         getItemCounts()
  370.         checkAllItems()
  371.     end
  372.     if is_popup then
  373.         showPopup(current_pop)
  374.     else
  375.         printPage()
  376.     end
  377. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement