Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- --- Generated by EmmyLua(https://github.com/EmmyLua)
- --- Created by Clor#D.
- --- DateTime: 18.05.2019 9:14
- ---
- local _, ops = require("shell").parse(...)
- local comp = require("component")
- local meInt = comp.me_interface
- local db
- if comp.isAvailable("database") then
- db = comp.database
- else
- db = nil
- end
- local gpu = comp.gpu
- local itemList
- local craftList
- local crftUpdtFlag = true
- local craftingThreadLimit = 2
- if ops ~= nil and ops.thrLim ~= nil and tonumber(ops.thrLim) > 0 then
- craftingThreadLimit = tonumber(ops.thrLim)
- end
- local craftingTreads = {}
- for i = 1, craftingThreadLimit do
- craftingTreads[i] = { slot = nil, preCraft = nil }
- end
- local line = 0
- local clear = " "
- local preCrafts = {
- { size = 1000, label = nil, alias = "SoC", craft = nil, progress = nil },
- { size = 1000, label = nil, alias = "ASoC", craft = nil, progress = nil }
- }
- if ops ~= nil and ops.extList ~= nil and ops.extList and ops.pclName ~= nil then
- dofile(ops.pclName)
- preCrafts = extList
- end
- if ops ~= nil and ops.int2ext ~= nil and ops.int2ext and ops.pclName ~= nil then
- if db == nil then
- print("no database")
- os.exit()
- end
- local fs = require("filesystem")
- fs.remove("/home/" .. ops.pclName)
- local f = fs.open("/home/" .. ops.pclName, "w")
- local s = ""
- f:write("extList = {" .. "\n")
- for k, v in ipairs(preCrafts) do
- s = ""
- s = s .. "\t{ size = " .. tostring(v.size) .. ", "
- if v.label == nil then
- v.label = db.get(k).label
- end
- s = s .. 'label = "' .. v.label .. '", '
- if v.alias == nil then
- v.alias = v.label
- end
- s = s .. 'alias = "' .. v.alias .. '", craft = nil, progress = nil },'
- if k == #preCrafts then
- s = string.sub(s, 1, string.len(s) - 1)
- end
- f:write(s .. "\n")
- end
- f:write("}")
- f:close()
- print("create data file /home/" .. ops.pclName)
- os.exit()
- end
- function initCraft()
- cPrint("Init Craft")
- local label
- for _, v in ipairs(craftList) do
- label = v.getItemStack().label
- for i = 1, #preCrafts do
- if label == preCrafts[i].label then
- preCrafts[i].craft = v
- cPrint(preCrafts[i].alias .. " init craft")
- break
- end
- end
- end
- crftUpdtFlag = false
- end
- function getItem(slot)
- local label = preCrafts[slot].label
- local count = 0
- local index = 0
- for k, v in ipairs(itemList) do
- if v.label == label then
- index = k
- count = count + v.size
- end
- end
- if count > 0 then
- itemList[index].size = count
- return itemList[index]
- end
- return { size = 0 }
- end
- function itemListUpdate()
- itemList = meInt.getItemsInNetwork()
- end
- function craftListUpdate()
- craftList = meInt.getCraftables()
- crftUpdtFlag = true
- end
- function termUpdate()
- for i = 1, craftingThreadLimit do
- ct = craftingTreads[i]
- if (ct.slot ~= nil and ct.preCraft ~= nil and not ct.preCraft.progress.isDone() and not ct.preCraft.progress.isCanceled()) then
- gpu.set(102, i + 1, "Crafting thread " .. tostring(i) .. " slot " .. tostring(ct.slot) .. " " .. ct.preCraft.alias .. " " .. tostring(ct.preCraft.size) .. clear)
- else
- gpu.set(102, i + 1, "Crafting thread " .. tostring(i) .. " free" .. clear)
- end
- end
- end
- function cPrint(str)
- local s = string.sub(str, 1, 99)
- if line < 50 then
- line = line + 1
- gpu.set(1, line, s)
- else
- gpu.copy(1, 1, 99, 50, 0, -1)
- gpu.set(1, line, clear)
- gpu.set(1, line, s)
- end
- termUpdate()
- end
- function initTerm()
- gpu.fill(1, 1, 160, 50, ' ')
- gpu.set(100, 1, "║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║", true)
- local title = "OC/ME Pre Craft System"
- gpu.set(101 + (60 - string.len(title)) / 2, 1, title)
- local s = {}
- for k, v in ipairs(preCrafts) do
- s[k] = tostring(v.size) .. " " .. v.alias
- s[k] = string.sub(s[k], 1, 28)
- end
- for k, v in ipairs(s) do
- if k % 2 ~= 0 then
- gpu.set(102, (k + 1) / 2 + craftingThreadLimit + 1, v)
- else
- gpu.set(132, k / 2 + craftingThreadLimit + 1, v)
- end
- end
- local author = "Desing by Clor#D"
- gpu.set(160 - string.len(author), 50, author)
- gpu.set(101, 45, "--thrLim=n [--thrLim=4] set craft thread lim lim 2 def")
- gpu.set(101, 46, "--int2ext create preCraft list database label auto paste")
- gpu.set(101, 47, "--extList run system from external preCraft list")
- gpu.set(101, 48, "database not required")
- gpu.set(101, 49, "--pclName = [name pre craft list]")
- if ops == nil or ops.extList == nil or not ops.extList then
- gpu.set(101, 50, "internal pre craft list mode")
- else
- gpu.set(101, 50, "external pre craft list mode")
- end
- end
- function init()
- if db ~= nil then
- local stack
- for i = 1, #preCrafts do
- stack = db.get(i)
- if stack ~= nil and preCrafts[i].label == nil then
- preCrafts[i].label = stack.label
- if (preCrafts[i].alias == nil) then
- preCrafts[i].alias = preCrafts[i].label
- end
- end
- end
- end
- itemListUpdate()
- craftListUpdate()
- initTerm()
- initCraft()
- end
- function loop()
- local slot = 1
- local currentPreCraft
- local item
- while true do
- currentPreCraft = preCrafts[slot]
- termUpdate()
- if currentPreCraft == nil and currentPreCraft.craft == nil then
- goto continue
- end
- for _, v in ipairs(craftingTreads) do
- if v ~= nil
- and v.slot == slot
- and v.preCraft ~= nil
- and v.preCraft.progress ~= nil
- and not v.preCraft.progress.isDone()
- and not v.preCraft.progress.isCanceled()
- then
- cPrint(tostring(slot) .. " " .. currentPreCraft.alias .. " already running")
- goto continue
- end
- end
- if currentPreCraft.craft ~= nil then
- item = getItem(slot)
- if item.size < currentPreCraft.size then
- repeat --wait free thread
- local flag = false
- for k, v in ipairs(craftingTreads) do
- if v.slot == nil or v.preCraft == nil or v.preCraft.progress == nil or v.preCraft.progress.isDone() or v.preCraft.progress.isCanceled() then
- flag = true
- v.slot = slot
- v.preCraft = currentPreCraft
- currentPreCraft.progress = currentPreCraft.craft.request(currentPreCraft.size - item.size)
- local b, err = currentPreCraft.progress.isCanceled()
- if b then
- cPrint(currentPreCraft.alias .. " request err : " .. err)
- else
- cPrint("Crafting thread " .. tostring(k) .. " running. " .. currentPreCraft.alias .. " request size " .. tostring(currentPreCraft.size - item.size))
- end
- break
- else
- --cPrint("Crafting thread " .. tostring(k) .. " is busy")
- end
- os.sleep(1)
- end
- until flag
- else
- cPrint(currentPreCraft.alias .. " " .. item.size .. " count" .. " is enough")
- end
- else
- cPrint("no craft " .. currentPreCraft.alias .. " in ME")
- end
- :: continue ::
- if slot < #preCrafts then
- slot = slot + 1
- else
- itemListUpdate()
- cPrint("item list update")
- slot = 1
- end
- termUpdate()
- os.sleep(1)
- end
- end
- init()
- loop()
Advertisement
Add Comment
Please, Sign In to add comment