Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local term = require("term")
- local sides = require("sides")
- local gpu = component.gpu
- local meInterface = component.me_interface
- local meExport = component.me_exportbus
- local db = component.database
- local Comp_Drive = nil
- local Comp_Num = 128.0
- local oreDict = {
- "-- MineCraft --",
- "Iron",
- "Gold",
- "Diamond",
- "Emerald",
- "Coal",
- "Redstone",
- "Nether Quartz",
- "-- Thermal --",
- "Aluminum",
- "Copper",
- "Lead",
- "Tin",
- "Silver",
- "Nickel",
- "-- Nuclear --",
- "Boron",
- "Lithium",
- "Magnesium",
- "Thorium"
- }
- local oreNum = {}
- local g_Table_MovedOreNum = {}
- local isFoundData = {}
- local g_CurrentProcess = 0
- local g_RestExportCycle = 0
- local g_const_TransportPerTrigger = 8
- local g_const_MaxCyclePerOre = 8
- local Text_Name_X = 2
- local Text_Num_X = Text_Name_X + 20
- local Text_Offset_X_CycleInfo = Text_Num_X + 10
- local Test_Offset_X_TotalMovedOre = Text_Offset_X_CycleInfo + 10
- local Text_Height_Start = 2
- local Text_Title_Y_Offset = 0
- local Text_MainText_Y_Offset = 2 + Text_Title_Y_Offset
- function Report_Component()
- if gpu ~= nil then
- print("Get GPU Succeed")
- else
- print("Get GPU Failed!!!")
- end
- if meInterface ~= nil then
- print("Get Interface Succeed")
- else
- print("Get Interface Failed!!!")
- end
- if meExport ~= nil then
- print("Get ExportBus Succeed")
- else
- print("Get ExportBus Failed!!!")
- end
- if db ~= nil then
- print("Get Database Succeed")
- else
- print("Get Database Failed!!!")
- end
- end
- function Add_Suffix(table_ore)
- for key,value in ipairs(table_ore) do
- if string.find(value, "-") == nil then
- table_ore[key] = value .. " Ore"
- end
- end
- print("Add Suffix Proccess done")
- end
- function Adjust_Resolution()
- if gpu == nil then
- print("GPU Component is not set!!!")
- else
- gpu.setResolution(60,30)
- end
- end
- function Set_FontColor(ItemNum)
- local Num_Ratio = ItemNum / Comp_Num
- if Num_Ratio > 1 then
- Num_Ratio = 1
- end
- local Num_Mult = math.floor(255 * Num_Ratio)
- local Color_Red = 255 - Num_Mult
- local Color_Green = Num_Mult
- local Color_Value = tonumber(string.format("0X%2.2X%2.2X00",Color_Red,Color_Green))
- gpu.setForeground(Color_Value)
- end
- function Refresh_Database()
- for key,value in ipairs(oreDict) do
- --reset old data
- isFoundData[key] = false
- oreNum[key] = 0
- if string.find(value, "-") == nil then
- local label_item = {label = value}
- local table_item = nil
- table_item = meInterface.getItemsInNetwork(label_item)
- if table_item.n == 1 then
- db.clear(key)
- local isSuccessStore = meInterface.store(table_item[1], db.address, key, 1)
- if isSuccessStore == true then
- isFoundData[key] = true
- oreNum[key] = table_item[1].size
- end
- end
- end
- end
- --print("Refresh Database proccess done")
- end
- function Cycle_CurrentProccess()
- g_CurrentProcess = g_CurrentProcess + 1
- if oreDict[g_CurrentProcess] == nil then
- g_CurrentProcess = 1
- end
- meExport.setExportConfiguration(sides.down, 1, db.address, g_CurrentProcess)
- end
- function Calculate_ExportCycle()
- local target_OreTotal = oreNum[g_CurrentProcess]
- if target_OreTotal == nil or target_OreTotal <= Comp_Num then
- g_RestExportCycle = 0
- else
- local target_ExcessOreNum = target_OreTotal - Comp_Num
- if target_ExcessOreNum < g_const_TransportPerTrigger then
- g_RestExportCycle = 0
- else
- local DivideNum = target_ExcessOreNum / g_const_TransportPerTrigger
- local CycleNum = math.floor(DivideNum)
- if CycleNum > g_const_MaxCyclePerOre then
- g_RestExportCycle = g_const_MaxCyclePerOre
- else
- g_RestExportCycle = CycleNum
- end
- end
- end
- end
- function Print_Title()
- local Height = Text_Height_Start + Text_Title_Y_Offset
- gpu.setForeground(0xAAAAFF)
- gpu.set(Text_Name_X, Height, "Ore Name", false)
- gpu.set(Text_Num_X, Height, "Quantity", false)
- gpu.set(Text_Offset_X_CycleInfo, Height, "Remain", false)
- gpu.set(Text_Offset_X_CycleInfo, Height + 1, "Cycle", false)
- gpu.set(Test_Offset_X_TotalMovedOre, Height, "Total", false)
- gpu.set(Test_Offset_X_TotalMovedOre, Height + 1, "Moved Ore", false)
- gpu.setForeground(0xFFFFFF)
- end
- function Print_OreInfo()
- for key, value in pairs(oreDict) do
- local Height = Text_Height_Start + Text_MainText_Y_Offset + key
- gpu.set(Text_Name_X, Height, value, false)
- if isFoundData[key] ~= true then
- Set_FontColor(0)
- gpu.set(Text_Num_X, Height, "0", false)
- else
- local table_Item = db.get(key)
- local itemNum = oreNum[key]
- Set_FontColor(itemNum)
- gpu.set(Text_Num_X, Height, tostring(itemNum), false)
- end
- gpu.setForeground(0xFFFFFF)
- end
- end
- function Print_CycleInfo()
- local StringPos_X = Text_Offset_X_CycleInfo
- local StringPos_Y = Text_Height_Start + Text_MainText_Y_Offset + g_CurrentProcess
- gpu.set(StringPos_X, StringPos_Y, tostring(g_RestExportCycle), false)
- end
- function Print_TotalMovedOre()
- for key, value in pairs(oreDict) do
- local StringPos_X = Test_Offset_X_TotalMovedOre
- local Height = Text_Height_Start + Text_MainText_Y_Offset + key
- if g_Table_MovedOreNum[key] == nil then
- gpu.set(StringPos_X, Height, "0", false)
- else
- gpu.set(StringPos_X, Height, tostring(g_Table_MovedOreNum[key]), false)
- end
- end
- end
- function Trigger_Export()
- local MovedOreNum = meExport.exportIntoSlot(sides.down)
- g_RestExportCycle = g_RestExportCycle - 1
- if MovedOreNum ~= nil then
- oreNum[g_CurrentProcess] = oreNum[g_CurrentProcess] - MovedOreNum
- if g_Table_MovedOreNum[g_CurrentProcess] == nil then
- g_Table_MovedOreNum[g_CurrentProcess] = 0
- end
- g_Table_MovedOreNum[g_CurrentProcess] = g_Table_MovedOreNum[g_CurrentProcess] + MovedOreNum
- end
- end
- --Storage Related Function
- function Initialize_Drive()
- for key,value in pairs(component.list("filesystem")) do
- local CompProxy = component.proxy(key)
- if CompProxy.getLabel() == "CDrive" then
- Comp_Drive = CompProxy
- end
- end
- end
- function Drive_Load()
- if Comp_Drive ~= nil then
- if Comp_Drive.exists("/home/OreManagerSaveFile") == true then
- local FileHandle = Comp_Drive.open("/home/OreManagerSaveFile", 'r')
- if FileHandle ~= nil then
- local CurrentIndex = 1
- while true do
- local ReadData = Comp_Drive.read(FileHandle, 10)
- if ReadData == nil then
- Comp_Drive.close(FileHandle)
- return
- end
- local oreNum = tonumber(ReadData)
- g_Table_MovedOreNum[CurrentIndex] = oreNum
- CurrentIndex = CurrentIndex + 1
- end
- end
- end
- end
- end
- function Drive_Save()
- if Comp_Drive ~= nil then
- local FileHandle = Comp_Drive.open("/home/OreManagerSaveFile", 'w')
- if FileHandle ~= nil then
- for key,value in ipairs(oreDict) do
- if g_Table_MovedOreNum[key] == nil then
- Comp_Drive.write(FileHandle, string.format("0X%08X", 0))
- else
- Comp_Drive.write(FileHandle, string.format("0X%08X", g_Table_MovedOreNum[key]))
- end
- end
- Comp_Drive.close(FileHandle)
- gpu.set(1,1,"Saved",false)
- end
- end
- end
- do -- Main Function --
- Adjust_Resolution()
- Report_Component()
- Add_Suffix(oreDict)
- Initialize_Drive()
- Drive_Load()
- while true do
- Refresh_Database()
- if g_RestExportCycle == 0 then
- if g_CurrentProcess == 1 then
- Drive_Save()
- os.sleep(1)
- end
- Cycle_CurrentProccess()
- Calculate_ExportCycle()
- else
- Trigger_Export()
- end
- term.clear()
- Print_Title()
- Print_OreInfo()
- Print_CycleInfo()
- Print_TotalMovedOre()
- os.sleep(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement