Advertisement
LaniusFNV

CC Base Monitor

Nov 19th, 2020 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.68 KB | None | 0 0
  1. local smeltery = peripheral.find("minecraft:tconstruct_smeltery_controller")
  2. local monitor = peripheral.find("monitor")
  3. local ingot_chest = peripheral.wrap("computer_0")
  4. local modem = peripheral.wrap("right")
  5.  
  6. local SERVER_CHANNEL = 1
  7. local CLIENT_CHANNEL = 2
  8.  
  9. -- Smeltery methods:
  10. --
  11. -- suck([slot:int[, limit:int]]):int
  12. -- Suck an item from the ground
  13. --
  14. -- pullItems(fromName:string, fromSlot:int[, limit:int[, toSlot:int]]):int
  15. -- Pull items to this inventory from another inventory. Returns the amount transferred.
  16. --
  17. -- drop(slot:int[, limit:int[, direction:string]]):int
  18. -- Drop an item on the ground. Returns the number of items dropped.
  19. --
  20. -- pushItems(toName:string, fromSlot:int[, limit:int[, toSlot:int]]):int
  21. -- Push items from this inventory to another inventory. Returns the amound transferred.
  22. --
  23. -- list():table
  24. -- List all items in this inventory.
  25. --
  26. -- size():int
  27. -- The size of the inventory.
  28. --
  29. -- getItemMeta(slot:int):table|nil
  30. -- The metadata of the item in the specified slot. The slot number starts from 1.
  31. --
  32. -- getItem(slot:int):table|nil
  33. -- The item in the specified slot. The slot number starts from 1.
  34. --
  35. -- getTemperature():number
  36. -- Get the internal temoerature of this structure.
  37. --
  38. -- getFuels():table
  39. -- Get a list of all fuels currenty used by the seared-bricks multiblock.
  40. --
  41. -- getMolten():table
  42. -- Get a list of all molten fluids within the smeltery.
  43. --
  44. -- selectMolten(fluid: number|string)
  45. -- Select which fluid will be extracted by drains in the smeltery. One can specify a fluid name or an index in list of molten fluids.
  46. --
  47. -- getMetadata():table
  48. -- Get metadata about this object
  49. --
  50. -- getTransferLocations([location:string]):table
  51. -- Get a list of all available objects which can be transferred to or from.
  52. --
  53. -- getDocs([name: string]):string|table
  54. -- Get the documentation for all functions or the function specified. Errors if the function cannot be found.
  55.  
  56.  
  57. -- Smeltery Drain Methods
  58. --
  59. -- getMetadata():table
  60. -- Get metadata about this object
  61. --
  62. -- getTransferLocations([location:string]):table
  63. -- Get a list of all available objects which can be transferred to or from
  64. --
  65. -- pullFluid(fromName:string[, limit:int[, fluid:string]]):int
  66. -- Pull fluid to this tank from another tank. Returns the amount transferred.
  67. --
  68. -- getDocs([name: string]):string|table
  69. -- Get the documentation for all functions or the function specified. Errors if the function cannot be found.
  70. --
  71. -- getTanks([side:string]):table
  72. -- Get a list of all tanks on this side.
  73. --
  74. -- pushFluid(toName:string[, limit:int], fluid:string):int
  75. -- Push fluid from this tank to another tank. Returns the amount transferred.
  76. --
  77. -- getController():table|nil
  78. -- Get the controller for this smeltery component.
  79.  
  80. local function mb_to_block_ingot_rest(millibuckets, displayName)
  81.     local block_mb = 1000
  82.     local ingot_mb = 144
  83.     local nugget_mb = 16
  84.  
  85.     if displayName == "Seared Stone" then
  86.         ingot_mb = 72
  87.         block_mb = 4 * ingot_mb
  88.     elseif displayName == "Liquid Glass" then
  89.         block_mb = 1000
  90.         return millibuckets / block_mb, nil, nil
  91.     else
  92.         nugget_mb = 16
  93.         ingot_mb = 144
  94.         block_mb = 9 * ingot_mb
  95.     end
  96.  
  97.     local num_blocks = math.floor(millibuckets / block_mb)
  98.     local num_ingots = math.floor((millibuckets - num_blocks * block_mb) / ingot_mb)
  99.     local num_nuggets = math.floor((millibuckets - num_blocks * block_mb - num_ingots * ingot_mb) / nugget_mb)
  100.     local rest = millibuckets - num_blocks * block_mb - num_ingots * ingot_mb - num_nuggets * nugget_mb
  101.  
  102.     return num_blocks, num_ingots, num_nuggets, rest
  103. end
  104.  
  105. local function notifySmelteryFuel()
  106.     local fuel = smeltery.getFuels()
  107.  
  108.     for index, value in ipairs(fuel) do
  109.         if value.amount < 250 then
  110.             term.clear()
  111.             term.setCursorPos(1, 1)
  112.  
  113.             term.write("Please refuel! Only "..value.amount.." mb of fuel left.")
  114.         end
  115.     end
  116. end
  117. local function maxStringLength(table, field)
  118.     local max = 0
  119.    
  120.     for k, v in ipairs(table) do
  121.         local string_len = string.len(v[field])
  122.         if string_len > max then
  123.             max = string_len
  124.         end
  125.     end
  126.  
  127.     return max
  128. end
  129.  
  130. local function header(window, max_material_length, max_count_length, column_spacing, kind)
  131.     window.setCursorPos(1, 1)
  132.     window.write("Material")
  133.  
  134.     window.setCursorPos(max_material_length + column_spacing - 1, 1)
  135.     window.write("|")
  136.  
  137.     if kind == "smeltery" then
  138.         window.setCursorPos(max_material_length + column_spacing + 1, 1)
  139.         window.write("Blocks")
  140.        
  141.         window.setCursorPos(max_material_length + max_count_length + 2 * column_spacing - 1, 1)
  142.         window.write("|")
  143.  
  144.         window.setCursorPos(max_material_length + max_count_length + 2 * column_spacing + 1, 1)
  145.         window.write("Ingots")
  146.  
  147.         window.setCursorPos(max_material_length + 2 * max_count_length + 3 * column_spacing - 1, 1)
  148.         window.write("|")
  149.  
  150.         window.setCursorPos(max_material_length + 2 * max_count_length + 3 * column_spacing + 1, 1)
  151.         window.write("Nuggets")
  152.  
  153.         window.setCursorPos(max_material_length + 3 * max_count_length + 4 * column_spacing - 1, 1)
  154.         window.write("|")
  155.  
  156.         window.setCursorPos(max_material_length + 3 * max_count_length + 4 * column_spacing + 1, 1)
  157.         window.write("Rest")
  158.     else
  159.         window.setCursorPos(max_material_length + column_spacing + 1, 1)
  160.         window.write("Ingots")
  161.     end
  162. end
  163.  
  164. local function sepRow(window, max_name_length, max_count_length, column_spacing, kind)
  165.     window.setCursorPos(1, 2)
  166.  
  167.     if kind == "smeltery" then
  168.         window.write(string.rep("-", max_name_length)..string.rep("-+"..string.rep("-", max_count_length + column_spacing - 2), 4))
  169.     else
  170.         window.write(string.rep("-", max_name_length)..string.rep("-+"..string.rep("-", max_count_length + column_spacing - 2), 1))
  171.     end
  172. end
  173.  
  174. local function tableContents(window, items, max_name_length, max_count_length, column_spacing, kind)
  175.     if kind == "smeltery" then
  176.         for k, v in ipairs(items) do
  177.             local num_blocks, num_ingots, num_nuggets, rest = mb_to_block_ingot_rest(v.amount, v.displayName)
  178.  
  179.             local display_height = k + 2
  180.  
  181.             window.setCursorPos(1, display_height)
  182.             window.write(v.displayName)
  183.  
  184.             window.setCursorPos(max_name_length + column_spacing - 1, display_height)
  185.             window.write("|")
  186.  
  187.             window.setCursorPos(max_name_length + column_spacing + 1, display_height)
  188.             if num_blocks ~= nil and num_blocks ~= 0 then
  189.                 window.write(num_blocks)
  190.             end
  191.  
  192.             window.setCursorPos(max_name_length + max_count_length + 2 * column_spacing - 1, display_height)
  193.             window.write("|")
  194.  
  195.             window.setCursorPos(max_name_length + max_count_length + 2 * column_spacing + 1, display_height)
  196.             if num_ingots ~= nil and num_ingots ~= 0 then
  197.             window.write(num_ingots)
  198.             end
  199.  
  200.             window.setCursorPos(max_name_length + 2 * max_count_length + 3 * column_spacing - 1, display_height)
  201.             window.write("|")
  202.  
  203.             window.setCursorPos(max_name_length + 2 * max_count_length + 3 * column_spacing + 1, display_height)
  204.             if num_nuggets ~= nil and num_nuggets ~= 0 then
  205.                 window.write(num_nuggets)
  206.             end
  207.  
  208.             window.setCursorPos(max_name_length + 3 * max_count_length + 4 * column_spacing - 1, display_height)
  209.             window.write("|")
  210.  
  211.             window.setCursorPos(max_name_length + 3 * max_count_length + 4 * column_spacing + 1, display_height)
  212.             if rest ~= nil and rest ~= 0 then
  213.                 window.write(rest)
  214.             end
  215.         end
  216.     else
  217.         for k, v in ipairs(items) do
  218.             local display_height = k + 2
  219.  
  220.             window.setCursorPos(1, display_height)
  221.             window.write(v.name)
  222.  
  223.             window.setCursorPos(max_name_length + column_spacing - 1, display_height)
  224.             window.write("|")
  225.  
  226.             window.setCursorPos(max_name_length + column_spacing + 1, display_height)
  227.             window.write(v.count)
  228.         end
  229.     end
  230. end
  231.  
  232. local function displayTable(window, items, column_spacing, sort_ascending, max_name_length, max_count_length, kind)
  233.     table.sort(items, function(a, b) if sort_ascending then return a.amount < b.amount else return a.amount > b.amount end end)
  234.  
  235.     window.clear()
  236.  
  237.     header(window, max_name_length, max_count_length, column_spacing, kind)
  238.     sepRow(window, max_name_length, max_count_length, column_spacing, kind)
  239.     tableContents(window, items, max_name_length, max_count_length, column_spacing, kind)
  240. end
  241.  
  242. local function updateDisplay(window, table, column_spacing, sort_ascending, max_material_length, max_count_length, kind)
  243.     displayTable(window, table, column_spacing, sort_ascending, max_material_length, max_count_length, kind)
  244. end
  245.  
  246. local function requestTable(kind)
  247.     modem.open(CLIENT_CHANNEL)
  248.  
  249.     if kind == "ingots" then
  250.         modem.transmit(SERVER_CHANNEL, CLIENT_CHANNEL, "req items")
  251.  
  252.         while true do
  253.             local event, peripheral_name, channel, reply_channel, message, distance = os.pullEvent("modem_message")
  254.  
  255.             return message
  256.         end
  257.     end
  258.  
  259.     modem.close("ingots")
  260. end
  261.  
  262. local function main()
  263.     local column_spacing = 3
  264.     local sort_ascending = false
  265.  
  266.     local molten = smeltery.getMolten()
  267.     --local ingots = requestTable("ingots")
  268.  
  269.     local smeltery_max_material_length = maxStringLength(molten, "displayName")
  270.     local smeltery_max_count_length = math.max(maxStringLength(molten, "amount"), string.len("Blocks"), string.len("Ingots"), string.len("Nuggets"), string.len("Rest"))
  271.  
  272.     --local ingots_max_material_length = maxStringLength(ingots, "name")
  273.     --local ingots_max_count_length = math.max(maxStringLength(ingots, "count"), string.len("Ingots"))
  274.  
  275.     local smeltery_table_width = smeltery_max_material_length + 4 * column_spacing + 1 + 3 * smeltery_max_count_length
  276.     --local ingots_table_width = ingots_max_material_length + column_spacing + 1 + ingots_max_count_length
  277.  
  278.     monitor.setTextScale(0.5)
  279.  
  280.     local mon_width, mon_height = monitor.getSize()
  281.     local smeltery_window = window.create(monitor, 1, 1, smeltery_table_width, mon_height)
  282.     --local ingots_window = window.create(monitor, smeltery_table_width + 1, 1, ingots_table_width, mon_height)
  283.  
  284.     while true do
  285.         notifySmelteryFuel()
  286.         updateDisplay(smeltery_window, molten, column_spacing, sort_ascending, smeltery_max_material_length, smeltery_max_count_length, "smeltery")
  287.         --updateDisplay(ingots_window, ingots, column_spacing, sort_ascending, ingots_max_material_length, ingots_max_count_length, "ingots")
  288.         sleep(5)
  289.  
  290.         molten = smeltery.getMolten()
  291.         --ingots = requestTable("ingots")
  292.  
  293.         smeltery_max_material_length = maxStringLength(molten, "displayName")
  294.         smeltery_max_count_length = math.max(maxStringLength(molten, "amount"), string.len("Blocks"), string.len("Ingots"), string.len("Nuggets"), string.len("Rest"))
  295.  
  296.         --ingots_max_material_length = maxStringLength(ingots, "name")
  297.         --ingots_max_count_length = math.max(maxStringLength(ingots, "count"), string.len("Ingots"))
  298.  
  299.         smeltery_table_width = smeltery_max_material_length + 4 * column_spacing + 1 + 3 * smeltery_max_count_length
  300.         --ingots_table_width = ingots_max_material_length + column_spacing + 1 + ingots_max_count_length
  301.  
  302.         smeltery_window.reposition(1, 1, smeltery_table_width, mon_height)
  303.         --ingots_window.reposition(smeltery_table_width + 1, 1, ingots_table_width, mon_height)
  304.     end
  305. end
  306.  
  307. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement