DemonicArcher

ME Crafting Tracker

Dec 8th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.89 KB | None | 0 0
  1. --this program uses a table provided by another
  2. --program that is saved to a common disk drive used
  3. --by the 2 computers.  use pastebin code ctaTjUpj
  4. --to retrieve the table provided program
  5. crafting = {}
  6. ---------------------------------------------------
  7. -- assigns the name or side for the 3 needed peri-
  8. -- pherals for this program; a monitor, a disk
  9. -- drive, and an Applied Energistics ME system
  10. -- you can assign their network name or a side
  11.  
  12. -- example apiName.setPeripheral("top","
  13. -- right","back")
  14.  
  15. -- example2 apiName.setPeripheral("monitor_2",
  16. -- "disk_1","appeng_me_tilecontroller_3"
  17.  
  18. function setPeripheral(monitor, disk, ME)
  19.  
  20. mon = peripheral.wrap(monitor)
  21. disk = peripheral.wrap(disk)
  22. app = peripheral.wrap(ME)
  23.  
  24. end -- set Peripheral
  25. ---------------------------------------------------
  26.  
  27. ---------------------------------------------------
  28. function RoundNumber(num,idp)
  29.   local mult = 10^(idp or 0)
  30.   return math.floor(num * mult +.05) / mult
  31. end --
  32. ---------------------------------------------------
  33.  
  34. ---------------------------------------------------
  35. -- collects the table of items to track from the
  36. -- disk drive. call this function after the
  37. -- setPeripheral function.  And place it somewhere
  38. -- in your main loop.  It does not have to be in
  39. -- every pass as it can slow your program, but it
  40. -- should be run every few seconds
  41.  
  42. -- example apiName.collecTable()
  43.  
  44. function collectTable()
  45. if fs.exists("disk/supplies") then
  46.   fyle = fs.open("disk/supplies","r")
  47.   items = textutils.unserialize(fyle.readAll())
  48.   fyle.close()
  49. else
  50.   term.clear()
  51.   term.setCursorPos(12,8)
  52.   term.write("Missing Esential File")
  53.   sleep(2)
  54.   collectTable()
  55. end -- if fs.exists
  56.  
  57. for name,data in pairs(items) do
  58.   if crafting[name] == nil then
  59.     crafting[name] = false
  60.   end
  61. end
  62.  
  63. end -- collectTable()
  64. ---------------------------------------------------
  65.  
  66. ---------------------------------------------------
  67. -- assigns a color to your list
  68. -- mainColor is the color than your item names will
  69. -- be
  70. -- secondColor is the color the item name will be
  71. -- while it's count is below the number you assign
  72. -- to keep in your ME in the other program
  73. -- countColor is the color that the number of each
  74. -- item will be displayed in
  75. -- dustColor is the color that the number of each
  76. -- items subItem will be displayed in
  77.  
  78. -- example (apiName.assignColors(colors.blue,
  79. -- colors.cyan,colors.green,colors.purple)
  80.  
  81. function assignColors(mainColor,secondColor,countColor,dustColor)
  82.  
  83. defColor = mainColor
  84. craftColor = secondColor
  85. invColor = countColor
  86. subColor = dustColor
  87.  
  88. end -- assignColors
  89. ---------------------------------------------------
  90.  
  91. ---------------------------------------------------
  92. -- Draw the headline at the top of the monitor
  93. -- run this only once
  94.  
  95. function headLine()
  96.   mon.clear()
  97.   mon.setTextColor(colors.orange)
  98. local w,h = mon.getSize()
  99.   p = w/4
  100.   mon.setCursorPos((2*p - string.len("Item Name"))/2+1,1)
  101.   mon.write("Item Name")
  102.   mon.setCursorPos((p-string.len("Count"))/2+(2*p)+1,1)
  103.   mon.write("Count")
  104.   mon.setCursorPos((p-string.len("Dust"))/2+(3*p)+1,1)
  105.   mon.write("Dust")
  106. end -- headline()  
  107. ---------------------------------------------------
  108.  
  109. ---------------------------------------------------
  110. function useData()
  111. local currentColor
  112.   for name,date in pairs(items) do
  113.  
  114.  
  115. local contained = app.countOfItemType(items[name]["id"],items[name]["damage"])
  116.   line = items[name]["line"] + 2
  117.  
  118.   target = tonumber(items[name]["target"])
  119.   lastCount = tonumber(items[name]["lastCount"])
  120.  
  121. if items[name]["target"] ~= 0 then
  122.   diff = contained - items[name]["target"]
  123.   if diff < 0 and target > lastCount then
  124.     if crafting[name] == false then
  125.        ident = tonumber(items[name]["id"])
  126.        dam = tonumber(items[name]["damage"])
  127.        needed = math.abs(diff)
  128.     itemStack = {id = ident, dmg = dam, qty = needed}
  129.       app.requestCrafting(itemStack)
  130.     crafting[name] = true
  131.     print("Crafting "..needed.." "..items[name]["Name"])
  132.     end -- if
  133.   elseif contained >= target then
  134.     crafting[name] = false
  135.   end -- if
  136. else
  137.   diff = 1
  138. end -- if
  139.  
  140. if crafting[name] == true then
  141.   currentColor = craftColor
  142. else
  143.   currentColor = defColor
  144. end
  145.  
  146. if items[name]["subID"] ~= "nil" then
  147.   subItem = app.countOfItemType(items[name]["subID"],items[name]["subDamage"])
  148. else
  149.   subItem = nil
  150. end
  151.  
  152. fillLine(name,currentColor,contained,diff,subItem,line)
  153. items[name]["lastCount"] = contained
  154. end -- for name,data
  155. end -- useData()
  156. ---------------------------------------------------
  157.  
  158. ---------------------------------------------------  
  159. function fillLine(iName,iColor,iCount,difference,iSubItem,iLine)
  160.   mon.setTextColor(iColor)
  161.   mon.setCursorPos(1,iLine)
  162.   mon.write("                                                   ")
  163.   mon.setCursorPos(1,iLine)
  164.   mon.write(iName)
  165.  
  166. if iCount >=0 then
  167.   mon.setTextColor(defColor)
  168. else
  169.   mon.setTextColor(colors.red)
  170. end
  171.          
  172.   if iCount > 1000000 then
  173.     iCount = RoundNumber(iCount/1000000,2).."M"
  174.   elseif iCount > 1000 then
  175.     iCount = RoundNumber(iCount/1000,2).."K"
  176.   end
  177.   mon.setTextColor(invColor)
  178.   mon.setCursorPos((p-string.len("iCount"))/2+(2*p)+1,iLine)
  179.   mon.write(tostring(iCount))
  180.  
  181.   if iSubItem ~= nil then
  182.     mon.setTextColor(subColor)
  183.     if iSubItem >1000000 then
  184.       iSubItem = RoundNumber(iSubItem/1000000,2).."M"
  185.     elseif iSubItem >1000 then
  186.       iSubItem = RoundNumber(iSubItem/1000,2).."K"
  187.     end
  188.     mon.setTextColor(subColor)
  189.     mon.setCursorPos((p-string.len("iSubItem"))/2+(3*p)+2,iLine)
  190.     mon.write(tostring(iSubItem))
  191.   end
  192. end -- fillLine()
  193. ---------------------------------------------------
  194. setPeripheral("monitor_31","drive_9","appeng_me_tilecontroller_2")
  195. assignColors(colors.blue,colors.cyan,colors.green,colors.purple)
  196.  
  197. headLine()
  198. collectTable()
  199.  
  200. while true do
  201.  
  202.   collectTable()
  203.   for t = 1,20 do
  204.   useData()
  205.   end
  206. end
Add Comment
Please, Sign In to add comment