Advertisement
HHM1573

Minecraft - OpenComputers - AE2OreMonitoring Program V.1

Jun 2nd, 2019
1,594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.52 KB | None | 0 0
  1. local component = require("component")
  2. local term = require("term")
  3. local sides = require("sides")
  4.  
  5. local gpu = component.gpu
  6.  
  7. local meInterface = component.me_interface
  8. local meExport = component.me_exportbus
  9.  
  10. local db = component.database
  11.  
  12. local Comp_Drive = nil
  13.  
  14.  
  15.  
  16. local Comp_Num = 128.0
  17.  
  18. local oreDict = {
  19.     "-- MineCraft --",
  20.     "Iron",
  21.     "Gold",
  22.     "Diamond",
  23.     "Emerald",
  24.     "Coal",
  25.     "Redstone",
  26.     "Nether Quartz",
  27.     "-- Thermal --",
  28.     "Aluminum",
  29.     "Copper",
  30.     "Lead",
  31.     "Tin",
  32.     "Silver",
  33.     "Nickel",
  34.     "-- Nuclear --",
  35.     "Boron",
  36.     "Lithium",
  37.     "Magnesium",
  38.     "Thorium"
  39. }
  40.    
  41. local oreNum = {}
  42. local g_Table_MovedOreNum = {}
  43.  
  44. local isFoundData = {}
  45.  
  46. local g_CurrentProcess = 0
  47. local g_RestExportCycle = 0
  48. local g_const_TransportPerTrigger = 8
  49. local g_const_MaxCyclePerOre = 8
  50.  
  51. local Text_Name_X = 2
  52. local Text_Num_X = Text_Name_X + 20
  53. local Text_Offset_X_CycleInfo = Text_Num_X + 10
  54. local Test_Offset_X_TotalMovedOre = Text_Offset_X_CycleInfo + 10
  55. local Text_Height_Start = 2
  56. local Text_Title_Y_Offset = 0
  57. local Text_MainText_Y_Offset = 2 + Text_Title_Y_Offset
  58.  
  59. function Report_Component()
  60.     if gpu ~= nil then
  61.         print("Get GPU Succeed")
  62.     else
  63.         print("Get GPU Failed!!!")
  64.     end
  65.    
  66.     if meInterface ~= nil then
  67.         print("Get Interface Succeed")
  68.     else
  69.         print("Get Interface Failed!!!")
  70.     end
  71.    
  72.     if meExport ~= nil then
  73.         print("Get ExportBus Succeed")
  74.     else
  75.         print("Get ExportBus Failed!!!")
  76.     end
  77.    
  78.     if db ~= nil then
  79.         print("Get Database Succeed")
  80.     else
  81.         print("Get Database Failed!!!")
  82.     end
  83. end
  84.    
  85. function Add_Suffix(table_ore)
  86.     for key,value in ipairs(table_ore) do
  87.         if string.find(value, "-") == nil then
  88.             table_ore[key] = value .. " Ore"
  89.         end
  90.     end
  91.     print("Add Suffix Proccess done")
  92. end
  93.  
  94. function Adjust_Resolution()
  95.     if gpu == nil then
  96.         print("GPU Component is not set!!!")
  97.     else
  98.         gpu.setResolution(60,30)
  99.     end
  100. end
  101.  
  102. function Set_FontColor(ItemNum)
  103.     local Num_Ratio = ItemNum / Comp_Num
  104.     if Num_Ratio > 1 then
  105.         Num_Ratio = 1
  106.     end
  107.     local Num_Mult = math.floor(255 * Num_Ratio)
  108.     local Color_Red = 255 - Num_Mult
  109.     local Color_Green = Num_Mult
  110.    
  111.     local Color_Value = tonumber(string.format("0X%2.2X%2.2X00",Color_Red,Color_Green))
  112.    
  113.     gpu.setForeground(Color_Value)
  114. end
  115.  
  116. function Refresh_Database()
  117.     for key,value in ipairs(oreDict) do
  118.         --reset old data
  119.         isFoundData[key] = false
  120.         oreNum[key] = 0
  121.         if string.find(value, "-") == nil then
  122.             local label_item = {label = value}
  123.             local table_item = nil
  124.             table_item = meInterface.getItemsInNetwork(label_item)
  125.            
  126.             if table_item.n == 1 then
  127.                 db.clear(key)
  128.                 local isSuccessStore = meInterface.store(table_item[1], db.address, key, 1)
  129.                 if isSuccessStore == true then
  130.                     isFoundData[key] = true
  131.                     oreNum[key] = table_item[1].size
  132.                 end
  133.             end
  134.            
  135.         end
  136.     end
  137.     --print("Refresh Database proccess done")
  138. end
  139.  
  140. function Cycle_CurrentProccess()
  141.     g_CurrentProcess = g_CurrentProcess + 1
  142.     if oreDict[g_CurrentProcess] == nil then
  143.         g_CurrentProcess = 1
  144.     end
  145.    
  146.     meExport.setExportConfiguration(sides.down, 1, db.address, g_CurrentProcess)
  147. end
  148.  
  149. function Calculate_ExportCycle()
  150.     local target_OreTotal = oreNum[g_CurrentProcess]
  151.    
  152.     if target_OreTotal == nil or target_OreTotal <= Comp_Num then
  153.         g_RestExportCycle = 0
  154.     else
  155.         local target_ExcessOreNum = target_OreTotal - Comp_Num
  156.         if target_ExcessOreNum < g_const_TransportPerTrigger then
  157.             g_RestExportCycle = 0
  158.         else
  159.             local DivideNum = target_ExcessOreNum / g_const_TransportPerTrigger
  160.             local CycleNum = math.floor(DivideNum)
  161.             if CycleNum > g_const_MaxCyclePerOre then
  162.                 g_RestExportCycle = g_const_MaxCyclePerOre
  163.             else
  164.                 g_RestExportCycle = CycleNum
  165.             end
  166.         end
  167.     end
  168. end
  169.  
  170. function Print_Title()
  171.     local Height = Text_Height_Start + Text_Title_Y_Offset
  172.     gpu.setForeground(0xAAAAFF)
  173.     gpu.set(Text_Name_X, Height, "Ore Name", false)
  174.    
  175.     gpu.set(Text_Num_X, Height, "Quantity", false)
  176.    
  177.     gpu.set(Text_Offset_X_CycleInfo, Height, "Remain", false)
  178.     gpu.set(Text_Offset_X_CycleInfo, Height + 1, "Cycle", false)
  179.    
  180.     gpu.set(Test_Offset_X_TotalMovedOre, Height, "Total", false)
  181.     gpu.set(Test_Offset_X_TotalMovedOre, Height + 1, "Moved Ore", false)
  182.    
  183.     gpu.setForeground(0xFFFFFF)
  184. end
  185.  
  186. function Print_OreInfo()
  187.     for key, value in pairs(oreDict) do
  188.         local Height = Text_Height_Start + Text_MainText_Y_Offset + key
  189.         gpu.set(Text_Name_X, Height, value, false)
  190.        
  191.         if isFoundData[key] ~= true then
  192.             Set_FontColor(0)
  193.             gpu.set(Text_Num_X, Height, "0", false)
  194.         else
  195.             local table_Item = db.get(key)
  196.             local itemNum = oreNum[key]
  197.             Set_FontColor(itemNum)
  198.             gpu.set(Text_Num_X, Height, tostring(itemNum), false)
  199.         end
  200.         gpu.setForeground(0xFFFFFF)
  201.     end
  202. end
  203.  
  204. function Print_CycleInfo()
  205.     local StringPos_X = Text_Offset_X_CycleInfo
  206.     local StringPos_Y = Text_Height_Start + Text_MainText_Y_Offset + g_CurrentProcess
  207.     gpu.set(StringPos_X, StringPos_Y, tostring(g_RestExportCycle), false)
  208. end
  209.  
  210. function Print_TotalMovedOre()
  211.     for key, value in pairs(oreDict) do
  212.         local StringPos_X = Test_Offset_X_TotalMovedOre
  213.         local Height = Text_Height_Start + Text_MainText_Y_Offset + key
  214.        
  215.         if g_Table_MovedOreNum[key] == nil then
  216.             gpu.set(StringPos_X, Height, "0", false)
  217.         else
  218.             gpu.set(StringPos_X, Height, tostring(g_Table_MovedOreNum[key]), false)
  219.         end
  220.        
  221.     end
  222. end
  223.  
  224.  
  225.  
  226. function Trigger_Export()
  227.     local MovedOreNum = meExport.exportIntoSlot(sides.down)
  228.     g_RestExportCycle = g_RestExportCycle - 1
  229.    
  230.     if MovedOreNum ~= nil then
  231.         oreNum[g_CurrentProcess] = oreNum[g_CurrentProcess] - MovedOreNum
  232.        
  233.         if g_Table_MovedOreNum[g_CurrentProcess] == nil then
  234.             g_Table_MovedOreNum[g_CurrentProcess] = 0
  235.         end
  236.        
  237.         g_Table_MovedOreNum[g_CurrentProcess] = g_Table_MovedOreNum[g_CurrentProcess] + MovedOreNum
  238.     end
  239. end
  240.  
  241.  
  242.  
  243. --Storage Related Function
  244. function Initialize_Drive()
  245.     for key,value in pairs(component.list("filesystem")) do
  246.         local CompProxy = component.proxy(key)
  247.         if CompProxy.getLabel() == "CDrive" then
  248.             Comp_Drive = CompProxy
  249.         end
  250.     end
  251. end
  252.  
  253. function Drive_Load()
  254.  
  255.     if Comp_Drive ~= nil then
  256.    
  257.         if Comp_Drive.exists("/home/OreManagerSaveFile") == true then
  258.        
  259.             local FileHandle = Comp_Drive.open("/home/OreManagerSaveFile", 'r')
  260.             if FileHandle ~= nil then
  261.                 local CurrentIndex = 1
  262.                 while true do
  263.                     local ReadData = Comp_Drive.read(FileHandle, 10)
  264.                     if ReadData == nil then
  265.                         Comp_Drive.close(FileHandle)
  266.                         return
  267.                     end
  268.                     local oreNum = tonumber(ReadData)
  269.                     g_Table_MovedOreNum[CurrentIndex] = oreNum
  270.                     CurrentIndex = CurrentIndex + 1
  271.                 end
  272.                
  273.             end
  274.         end
  275.     end
  276. end
  277.  
  278. function Drive_Save()
  279.     if Comp_Drive ~= nil then
  280.         local FileHandle = Comp_Drive.open("/home/OreManagerSaveFile", 'w')
  281.         if FileHandle ~= nil then
  282.             for key,value in ipairs(oreDict) do
  283.                 if g_Table_MovedOreNum[key] == nil then
  284.                     Comp_Drive.write(FileHandle, string.format("0X%08X", 0))
  285.                 else
  286.                     Comp_Drive.write(FileHandle, string.format("0X%08X", g_Table_MovedOreNum[key]))
  287.                 end
  288.             end
  289.             Comp_Drive.close(FileHandle)
  290.             gpu.set(1,1,"Saved",false)
  291.         end
  292.     end
  293. end
  294.  
  295.  
  296.  
  297.  
  298.  
  299. do  -- Main Function --
  300.     Adjust_Resolution()
  301.     Report_Component()
  302.     Add_Suffix(oreDict)
  303.     Initialize_Drive()
  304.     Drive_Load()
  305.     while true do
  306.    
  307.         Refresh_Database()
  308.        
  309.         if g_RestExportCycle == 0 then
  310.             if g_CurrentProcess == 1 then
  311.                 Drive_Save()
  312.                 os.sleep(1)
  313.             end
  314.             Cycle_CurrentProccess()
  315.             Calculate_ExportCycle()
  316.         else
  317.             Trigger_Export()
  318.         end
  319.        
  320.         term.clear()
  321.        
  322.         Print_Title()
  323.         Print_OreInfo()
  324.         Print_CycleInfo()
  325.         Print_TotalMovedOre()
  326.        
  327.         os.sleep(1)
  328.     end
  329. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement