Advertisement
Clor

PreCraft System OC/AE

May 25th, 2019
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.35 KB | None | 0 0
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by Clor#D.
  4. --- DateTime: 18.05.2019 9:14
  5. ---
  6. local _, ops = require("shell").parse(...)
  7. local comp = require("component")
  8. local meInt = comp.me_interface
  9. local db
  10. if comp.isAvailable("database") then
  11.     db = comp.database
  12. else
  13.     db = nil
  14. end
  15. local gpu = comp.gpu
  16.  
  17. local itemList
  18. local craftList
  19. local crftUpdtFlag = true
  20.  
  21. local craftingThreadLimit = 2
  22. if ops ~= nil and ops.thrLim ~= nil and tonumber(ops.thrLim) > 0 then
  23.     craftingThreadLimit = tonumber(ops.thrLim)
  24. end
  25. local craftingTreads = {}
  26. for i = 1, craftingThreadLimit do
  27.     craftingTreads[i] = { slot = nil, preCraft = nil }
  28. end
  29. local line = 0
  30. local clear = "                                                                                                   "
  31.  
  32. local preCrafts = {
  33.     { size = 1000, label = nil, alias = "SoC", craft = nil, progress = nil },
  34.     { size = 1000, label = nil, alias = "ASoC", craft = nil, progress = nil }
  35. }
  36.  
  37. if ops ~= nil and ops.extList ~= nil and ops.extList and ops.pclName ~= nil then
  38.     dofile(ops.pclName)
  39.     preCrafts = extList
  40. end
  41.  
  42. if ops ~= nil and ops.int2ext ~= nil and ops.int2ext and ops.pclName ~= nil then
  43.     if db == nil then
  44.         print("no database")
  45.         os.exit()
  46.     end
  47.     local fs = require("filesystem")
  48.     fs.remove("/home/" .. ops.pclName)
  49.     local f = fs.open("/home/" .. ops.pclName, "w")
  50.     local s = ""
  51.     f:write("extList = {" .. "\n")
  52.     for k, v in ipairs(preCrafts) do
  53.         s = ""
  54.         s = s .. "\t{ size = " .. tostring(v.size) .. ", "
  55.         if v.label == nil then
  56.             v.label = db.get(k).label
  57.         end
  58.         s = s .. 'label =  "' .. v.label .. '", '
  59.         if v.alias == nil then
  60.             v.alias = v.label
  61.         end
  62.         s = s .. 'alias =  "' .. v.alias .. '", craft = nil, progress = nil },'
  63.         if k == #preCrafts then
  64.             s = string.sub(s, 1, string.len(s) - 1)
  65.         end
  66.         f:write(s .. "\n")
  67.     end
  68.     f:write("}")
  69.     f:close()
  70.     print("create data file /home/" .. ops.pclName)
  71.     os.exit()
  72. end
  73.  
  74. function initCraft()
  75.  
  76.     cPrint("Init Craft")
  77.  
  78.     local label
  79.     for _, v in ipairs(craftList) do
  80.         label = v.getItemStack().label
  81.         for i = 1, #preCrafts do
  82.             if label == preCrafts[i].label then
  83.                 preCrafts[i].craft = v
  84.                 cPrint(preCrafts[i].alias .. " init craft")
  85.                 break
  86.             end
  87.         end
  88.     end
  89.     crftUpdtFlag = false
  90. end
  91.  
  92. function getItem(slot)
  93.     local label = preCrafts[slot].label
  94.     local count = 0
  95.     local index = 0
  96.     for k, v in ipairs(itemList) do
  97.         if v.label == label then
  98.             index = k
  99.             count = count + v.size
  100.         end
  101.     end
  102.     if count > 0 then
  103.         itemList[index].size = count
  104.         return itemList[index]
  105.     end
  106.     return { size = 0 }
  107. end
  108.  
  109. function itemListUpdate()
  110.     itemList = meInt.getItemsInNetwork()
  111. end
  112.  
  113. function craftListUpdate()
  114.     craftList = meInt.getCraftables()
  115.     crftUpdtFlag = true
  116. end
  117.  
  118. function termUpdate()
  119.     for i = 1, craftingThreadLimit do
  120.         ct = craftingTreads[i]
  121.         if (ct.slot ~= nil and ct.preCraft ~= nil and not ct.preCraft.progress.isDone() and not ct.preCraft.progress.isCanceled()) then
  122.             gpu.set(102, i + 1, "Crafting thread " .. tostring(i) .. " slot " .. tostring(ct.slot) .. " " .. ct.preCraft.alias .. " " .. tostring(ct.preCraft.size) .. clear)
  123.         else
  124.             gpu.set(102, i + 1, "Crafting thread " .. tostring(i) .. " free" .. clear)
  125.         end
  126.     end
  127. end
  128.  
  129. function cPrint(str)
  130.     local s = string.sub(str, 1, 99)
  131.     if line < 50 then
  132.         line = line + 1
  133.         gpu.set(1, line, s)
  134.     else
  135.         gpu.copy(1, 1, 99, 50, 0, -1)
  136.         gpu.set(1, line, clear)
  137.         gpu.set(1, line, s)
  138.     end
  139.     termUpdate()
  140. end
  141.  
  142. function initTerm()
  143.     gpu.fill(1, 1, 160, 50, ' ')
  144.     gpu.set(100, 1, "║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║", true)
  145.     local title = "OC/ME Pre Craft System"
  146.     gpu.set(101 + (60 - string.len(title)) / 2, 1, title)
  147.     local s = {}
  148.     for k, v in ipairs(preCrafts) do
  149.         s[k] = tostring(v.size) .. " " .. v.alias
  150.         s[k] = string.sub(s[k], 1, 28)
  151.     end
  152.     for k, v in ipairs(s) do
  153.         if k % 2 ~= 0 then
  154.             gpu.set(102, (k + 1) / 2 + craftingThreadLimit + 1, v)
  155.         else
  156.             gpu.set(132, k / 2 + craftingThreadLimit + 1, v)
  157.         end
  158.     end
  159.     local author = "Desing by Clor#D"
  160.     gpu.set(160 - string.len(author), 50, author)
  161.     gpu.set(101, 45, "--thrLim=n [--thrLim=4] set craft thread lim  lim 2 def")
  162.     gpu.set(101, 46, "--int2ext create preCraft list database label auto paste")
  163.     gpu.set(101, 47, "--extList run system from external preCraft list")
  164.     gpu.set(101, 48, "database not required")
  165.     gpu.set(101, 49, "--pclName = [name pre craft list]")
  166.     if ops == nil or ops.extList == nil or not ops.extList then
  167.         gpu.set(101, 50, "internal pre craft list mode")
  168.     else
  169.         gpu.set(101, 50, "external pre craft list mode")
  170.     end
  171. end
  172.  
  173. function init()
  174.  
  175.     if db ~= nil then
  176.         local stack
  177.         for i = 1, #preCrafts do
  178.             stack = db.get(i)
  179.             if stack ~= nil and preCrafts[i].label == nil then
  180.                 preCrafts[i].label = stack.label
  181.                 if (preCrafts[i].alias == nil) then
  182.                     preCrafts[i].alias = preCrafts[i].label
  183.                 end
  184.             end
  185.         end
  186.     end
  187.  
  188.     itemListUpdate()
  189.     craftListUpdate()
  190.     initTerm()
  191.     initCraft()
  192. end
  193.  
  194. function loop()
  195.     local slot = 1
  196.     local currentPreCraft
  197.     local item
  198.     while true do
  199.         currentPreCraft = preCrafts[slot]
  200.         termUpdate()
  201.         if currentPreCraft == nil and currentPreCraft.craft == nil then
  202.             goto continue
  203.         end
  204.         for _, v in ipairs(craftingTreads) do
  205.             if v ~= nil
  206.                     and v.slot == slot
  207.                     and v.preCraft ~= nil
  208.                     and v.preCraft.progress ~= nil
  209.                     and not v.preCraft.progress.isDone()
  210.                     and not v.preCraft.progress.isCanceled()
  211.             then
  212.                 cPrint(tostring(slot) .. " " .. currentPreCraft.alias .. " already running")
  213.                 goto continue
  214.             end
  215.         end
  216.  
  217.         if currentPreCraft.craft ~= nil then
  218.             item = getItem(slot)
  219.             if item.size < currentPreCraft.size then
  220.                 repeat --wait free thread
  221.                     local flag = false
  222.                     for k, v in ipairs(craftingTreads) do
  223.                         if v.slot == nil or v.preCraft == nil or v.preCraft.progress == nil or v.preCraft.progress.isDone() or v.preCraft.progress.isCanceled() then
  224.                             flag = true
  225.                             v.slot = slot
  226.                             v.preCraft = currentPreCraft
  227.                             currentPreCraft.progress = currentPreCraft.craft.request(currentPreCraft.size - item.size)
  228.                             local b, err = currentPreCraft.progress.isCanceled()
  229.                             if b then
  230.                                 cPrint(currentPreCraft.alias .. " request err : " .. err)
  231.                             else
  232.                                 cPrint("Crafting thread " .. tostring(k) .. " running. " .. currentPreCraft.alias .. " request size " .. tostring(currentPreCraft.size - item.size))
  233.                             end
  234.                             break
  235.                         else
  236.                             --cPrint("Crafting thread " .. tostring(k) .. " is busy")
  237.                         end
  238.                         os.sleep(1)
  239.                     end
  240.                 until flag
  241.             else
  242.                 cPrint(currentPreCraft.alias .. " " .. item.size .. " count" .. " is enough")
  243.             end
  244.         else
  245.             cPrint("no craft " .. currentPreCraft.alias .. " in ME")
  246.         end
  247.  
  248.         :: continue ::
  249.         if slot < #preCrafts then
  250.             slot = slot + 1
  251.         else
  252.             itemListUpdate()
  253.             cPrint("item list update")
  254.             slot = 1
  255.         end
  256.         termUpdate()
  257.         os.sleep(1)
  258.     end
  259. end
  260.  
  261. init()
  262. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement