Advertisement
ben_mkiv

deH.lua

Oct 30th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 28.47 KB | None | 0 0
  1. blocks = {}
  2.  
  3. blocks.storage = {
  4.     { label = "ender chest", name = "enderstorage:ender_storage" },
  5.     { label = "ender chest", name = "minecraft:ender_chest" },
  6.     { label = "modular storage", name = "rftools:modular_storage" }
  7. }
  8.  
  9. blocks.input = {
  10.     { label = "[DE] Injector", name = "draconicevolution:crafting_injector" },
  11.     { label = "[AA] Display Stand", name = "actuallyadditions:block_display_stand" },
  12.     { label = "[EC] Pedestal", name = "extendedcrafting:pedestal" }
  13. }
  14.  
  15. blocks.core = {
  16.     { label = "[DE] Crafting Core", name = "draconicevolution:fusion_crafting_core" },
  17.     { label = "[AA] Empowerer", name = "actuallyadditions:block_empowerer" },
  18.     { label = "[EC] Crafting Core", name = "extendedcrafting:crafting_core" }
  19. }
  20.  
  21. cachedRecipe = false
  22. recipeOutput = false
  23.  
  24. craftingDone = false
  25. craftWaitUser = false
  26.  
  27. function hazeUI:drawTools()
  28.     self:addButton(32, 3, 20, 2, "read recipe", "tools", 0xFFB000, 0x282828, "left", "readRecipe")
  29.     self:addButton(32, 6, 20, 2, "start crafting", "tools", 0xFFB000, 0x282828, "left", function()
  30.         cachedRecipe = self:getRecipe()
  31.         self:startCrafting()
  32.         self:drawMain()
  33.         end)
  34.     self:addButton(32, 9, 20, 2, "sync inventory", "tools", 0xFFB000, 0x282828, "left", "readInventory")
  35.     self:addButton(32, 12, 20, 2, "cleanup", "tools", 0xFFB000, 0x282828, "left", "cleanup")
  36.  
  37.     self:addButton(2, 23, 20, 3, "back", "tools", 0xFFA200, 0x282828, "center", "drawMain")
  38.    
  39.     self:drawScreen("tools")   
  40. end
  41.  
  42. function hazeUI:drawMain()
  43.     self:flushElements(true)
  44.     self:setElement({index = titleBar, text = getCraftingType()})
  45.  
  46.     self:addButton(2, 3, 20, 2, "tools", "main", 0xFFB000, 0x282828, "left", "drawTools")
  47.  
  48.     if config.devices.transposer.crafter.type == "draconicevolution:fusion_crafting_core" then
  49.         self:addButton(2, 12, 20, 1, "upgrades", "main", 0x282828, 0xFFB000, "left")
  50.         for i=1,#upgradeKeys do
  51.             self:addButton(2, 12+i, 20, 1, upgradeKeys[i].nameShort, "main", 0xFFAD00, 0x4A4A4A, "left", "showUpgrade", i) end
  52.     end
  53.  
  54.     self:addButton(40, 3, 40, 1, "crafting recipes", "main", 0xFFB000, 0x282828, "left")
  55.     for i=1,#recipes do
  56.         local bgColor = 0xFFB000
  57.         if self:checkItems(i) ~= false then bgColor = 0x3AA700 end
  58.         self:addButton(40, 3+i, 40, 1, recipes[i].output.label, "main", 0x282828, bgColor, "left", "showRecipe", i)
  59.     end
  60.    
  61.     self:drawScreen("main")    
  62. end
  63.  
  64. function hazeUI:getRecipe()
  65.     self:drawScreen("clear")
  66.     local recipe = {}
  67.     recipe.materials = {}
  68.  
  69.     local crafter = component.proxy(config.devices.transposer.crafter.address)
  70.     recipe.input = crafter.getStackInSlot(config.devices.transposer.crafter.sideCrafter, 1)
  71.     recipe.output = crafter.getStackInSlot(config.devices.transposer.crafter.sideOutput, config.devices.transposer.crafter.slotOutput)
  72.     local s = #config.devices.transposer.injectors
  73.     for i=1,s do
  74.         self:drawStatusbar(5, 5, 40, 1, 1+s, i, "reading recipe")
  75.         local injector = component.proxy(config.devices.transposer.injectors[i].address)
  76.         local tmp = injector.getStackInSlot(config.devices.transposer.injectors[i].sideInjector, 1)
  77.         if tmp and tmp ~= nil then
  78.             table.insert(recipe.materials, tmp)
  79.         end
  80.         os.sleep(0)
  81.     end
  82.  
  83.     return recipe
  84. end
  85.  
  86. function hazeUI:storeRecipe(recipe)
  87.     table.insert(recipes, clone(recipe))
  88.     self:saveRecipesConfig()
  89. end
  90.  
  91. function checkRecipe(recipe)
  92.     local problems = {}
  93.     if not recipe.output then table.insert(problems, "no recipe output found") end
  94.     if not recipe.input then table.insert(problems, "no primary recipe input found") end
  95.     return problems
  96. end
  97.  
  98. function hazeUI:readRecipe()
  99.     self:drawScreen("clear")
  100.     local recipe = self:getRecipe()
  101.  
  102.     local recipeErrors = checkRecipe(recipe)
  103.  
  104.     if #errors > 0 then
  105.         for i=1,#recipeErrors do
  106.             self:addButton(2, (i * 4), 70, 3, "reading recipe failed, " .. recipeErrors[i], "readRecipeFailed", 0x282828, 0xFFA200, "left")
  107.         end
  108.         self:drawScreen("readRecipeFailed")
  109.         return
  110.     end
  111.  
  112.     self:storeRecipe()
  113.     return self:showRecipe(#recipes)
  114. end
  115.  
  116. function hazeUI:loadRecipesConfig()
  117.   local ser = require("serialization")
  118.   local fs = require("filesystem")
  119.   if not fs.exists("/etc/de_infusioncrafting.conf") then
  120.     self:saveRecipesConfig()   
  121.   end
  122.  
  123.   local cf = io.open("/etc/de_infusioncrafting.conf", "r")
  124.   local serData = cf:read("*a")
  125.   cf:close()
  126.   recipes = ser.unserialize(serData)
  127.   sortRecipes()
  128.  
  129.   if not fs.exists("/etc/de_upgradeRecipes.conf") then
  130.     local cf = io.open("/etc/de_upgradeRecipes.conf", "w")
  131.     cf:write(ser.serialize(upgradeRecipes))
  132.     cf:close() 
  133.   end
  134.  
  135.   local cf = io.open("/etc/de_upgradeRecipes.conf", "r")
  136.   local serData = cf:read("*a")
  137.   cf:close()
  138.   upgradeRecipes = ser.unserialize(serData)
  139.   self:compressRecipes()
  140. end
  141.  
  142. function hazeUI:saveRecipesConfig()
  143.     local ser = require("serialization")
  144.     local cf = io.open("/etc/de_infusioncrafting.conf", "w")
  145.     cf:write(ser.serialize(recipes))
  146.     cf:close() 
  147.     self:compressRecipes()
  148. end
  149.  
  150. function loadConfig()
  151.   local ser = require("serialization")
  152.   local fs = require("filesystem")
  153.   if not fs.exists("/etc/de.conf") then
  154.     return
  155.   end
  156.  
  157.   local cf = io.open("/etc/de.conf", "r")
  158.   local serData = cf:read("*a")
  159.   cf:close()
  160.   config = ser.unserialize(serData)
  161. end
  162.  
  163. function saveConfig()
  164.     local ser = require("serialization")
  165.     local cf = io.open("/etc/de.conf", "w")
  166.     cf:write(ser.serialize(config))
  167.     cf:close()
  168. end
  169.  
  170. function hazeUI:getRecipeForOutput(name)
  171.     for i=1,#recipes do
  172.         if recipes[i].output == name then return recipes[i]; end
  173.     end
  174.     return false
  175. end
  176.  
  177. function storeCachedRecipe(data)
  178.     if data.label == "yes" then
  179.         if config.devices.transposer.crafter.type ~= "draconicevolution:fusion_crafting_core" then
  180.             gui:addButton(2, 3, 70, 3, "please put the primary input on the core now", "storeCachedRecipe", 0x282828, 0xFFA200, "left")
  181.             gui:drawScreen("storeCachedRecipe")
  182.  
  183.             local crafter = component.proxy(config.devices.transposer.crafter.address)
  184.             cachedRecipe.input = false
  185.  
  186.             while not cachedRecipe.input do
  187.                 stackCore = crafter.getStackInSlot(config.devices.transposer.crafter.sideCrafter, config.devices.transposer.crafter.slotOutput)
  188.                 if stackCore ~= nil and stackCore ~= cachedRecipe.output then
  189.                     cachedRecipe.input = stackCore
  190.                     cleanCrafter()
  191.                 else
  192.                     os.sleep(0.25)
  193.                 end
  194.             end
  195.         end
  196.  
  197.         gui:storeRecipe(cachedRecipe)
  198.  
  199.         cachedRecipe = false
  200.         return self:showRecipe(#recipes)
  201.     end
  202.  
  203.     cachedRecipe = false
  204. end
  205.  
  206. function hazeUI:updateCraftingStatusFromRedstoneSignal()
  207.     local rs = component.proxy(config.devices.redstone.status.address)
  208.     local status = rs.getComparatorInput(config.devices.redstone.status.side)
  209.     local oldStatus = -1
  210.  
  211.     while status < 15 do
  212.         status = rs.getComparatorInput(config.devices.redstone.status.side)
  213.         if status ~= oldStatus then
  214.             oldStatus = status
  215.             self:drawStatusbar(5, 5, 40, 1, 15, status, "crafting...")
  216.         end
  217.         os.sleep(0.5)
  218.     end
  219.  
  220.     craftingDone = true
  221. end
  222.  
  223. function hazeUI:updateCraftingStatusFuzzy()
  224.     local status = 1
  225.     local crafter = component.proxy(config.devices.transposer.crafter.address)
  226.  
  227.     if recipeOutput then while not craftingDone do
  228.         self:drawStatusbar(5, 5, 40, 1, 15, status, "crafting...")
  229.         stackOutput = crafter.getStackInSlot(config.devices.transposer.crafter.sideCrafter, config.devices.transposer.crafter.slotOutput)
  230.  
  231.         -- replace by something like compareStacks
  232.         if stackOutput.name == recipeOutput.name and stackOutput.damage == recipeOutput.damage then
  233.             craftingDone = true
  234.             component.gpu.set(3, 10, "DONE!")
  235.             return
  236.         else
  237.             status = status + 1
  238.             if status > 15 then status = 1 end
  239.             os.sleep(0.2)
  240.         end
  241.     end end
  242.  
  243.     craftingDone = true
  244. end
  245.  
  246. function hazeUI:craftingStatus()
  247.     local crafter = component.proxy(config.devices.transposer.crafter.address)
  248.  
  249.     local status = 1
  250.     while crafter.getStackInSlot(config.devices.transposer.crafter.sideCrafter, 1) == nil do
  251.         self:drawStatusbar(5, 5, 40, 1, 15, status, "waiting for primary input...")
  252.         status = status + 1
  253.         if status > 15 then status = 1 end
  254.         os.sleep(0.2)
  255.     end
  256.  
  257.     craftingDone = false
  258.  
  259.     if config.devices.redstone.crafter.address then
  260.         self:updateCraftingStatusFromRedstoneSignal()
  261.     else
  262.         self:updateCraftingStatusFuzzy()
  263.     end
  264.  
  265.     while not craftingDone do os.sleep(0.2) end
  266.  
  267.     if not recipeOutput then waitIsJobDone() end
  268.  
  269.     self:drawStatusbar(5, 5, 40, 1, 15, 15, "crafting. [done]")
  270.  
  271.     recipeOutput = false
  272.  
  273.     return true
  274. end
  275.  
  276. function waitIsJobDone()
  277.     gui:addButton(2, 3, 70, 3, "waiting for user input", "craftWaitUser", 0x282828, 0xFFA200, "left")
  278.     gui:drawScreen("craftWaitUser")
  279.     craftWaitUser = true
  280.     gui:list("crafting unknown recipe", "crafting done?", {"yes", "no"}, { f = finishCraft, p = {}}, nil, nil, nil)
  281.     while craftWaitUser do os.sleep(0.5) end
  282. end
  283.  
  284. function finishCraft()
  285.     local crafter = component.proxy(config.devices.transposer.crafter.address)
  286.     cachedRecipe.output = crafter.getStackInSlot(config.devices.transposer.crafter.sideCrafter, config.devices.transposer.crafter.slotOutput)
  287.     craftWaitUser = false
  288.     cleanCrafter()
  289.  
  290.     gui:addButton(2, 3, 70, 3, "put the input in the core now if you want to store the recipe", "finishCraft", 0x282828, 0xFFA200, "left")
  291.     gui:drawScreen("finishCraft")
  292.     gui:list("new recipe detected", "store new recipe?", {"yes", "no"}, { f = storeCachedRecipe, p = {}}, nil, nil, nil)
  293.     while cachedRecipe ~= false do os.sleep(0.5) end
  294. end
  295.  
  296. function hazeUI:startCrafting()
  297.     self:drawScreen("craftingStatus")
  298.     self:drawStatusbar(5, 5, 40, 1, 100, 0, "starting crafting...")
  299.  
  300.     local rs = false
  301.  
  302.     if config.devices.redstone.crafter.address then
  303.         rs = component.proxy(config.devices.redstone.crafter.address)
  304.         rs.setOutput(config.devices.redstone.crafter.side, 15)
  305.     end
  306.  
  307.     os.sleep(0.5)
  308.  
  309.     self:drawStatusbar(5, 5, 40, 1, 100, 100, "starting crafting. [done]")
  310.  
  311.     if rs then rs.setOutput(config.devices.redstone.crafter.side, 0) end
  312.  
  313.     return self:craftingStatus()
  314. end
  315.  
  316. function hazeUI:craftRecipeLoop(data)
  317.     for i=1,data.count do self:craftRecipe(data.recipe) end
  318.     self:drawMain()
  319. end
  320.  
  321. function hazeUI:getStockForRecipe(i, printOutput)
  322.     local maxCraftable = math.huge
  323.  
  324.     for j=1,#recipes_compressed[i] do
  325.         local bgColor = 0x3AA700
  326.         local stockCnt = self:checkItem(recipes_compressed[i][j])
  327.         if stockCnt == false then
  328.             bgColor = 0x9D0000
  329.             maxCraftable = 0
  330.         elseif math.floor(stockCnt / recipes_compressed[i][j].size) < maxCraftable then
  331.             maxCraftable = math.floor(stockCnt / recipes_compressed[i][j].size)
  332.         end
  333.         if printOutput == true then
  334.             local cnt = " "
  335.             if recipes_compressed[i][j].size > 1 then cnt = " "..recipes_compressed[i][j].size .. "x " end
  336.             self:addButton(30, 17+j, 40, 1, cnt .. recipes_compressed[i][j].label, "showRecipe", 0x0, bgColor, "left")
  337.         end
  338.     end
  339.  
  340.     return maxCraftable
  341. end
  342.  
  343. function hazeUI:getStockPrefix(material)
  344.     if self:checkItem(material) then
  345.         return "[x]"
  346.     else
  347.         return "[ ]"
  348.     end
  349. end
  350.  
  351. function hazeUI:showRecipe(i)
  352.     local recipe = clone(recipes[i])
  353.    
  354.     local cnt = " "
  355.     if recipe.output.size > 1 then cnt = " ".. recipe.output.size .. "x " end      
  356.     self:addButton(30, 3, 40, 1, "output:"..cnt..recipe.output.label, "showRecipe", 0x282828, 0xFFA200, "left")
  357.    
  358.     local cnt = " "
  359.     if recipe.input.size > 1 then cnt = " ".. recipe.input.size .. "x " end        
  360.     self:addButton(30, 4, 40, 1,  self:getStockPrefix(recipe.input) .. " input:"..cnt..recipe.input.label, "showRecipe", 0x282828, 0xFFA200, "left")
  361.  
  362.     for j=1,#recipe.materials do
  363.         local cnt = " "
  364.         if recipe.materials[j].size > 1 then cnt = " "..recipe.materials[j].size.."x " end
  365.         self:addButton(30, 5+j, 40, 1, self:getStockPrefix(recipe.materials[j]) .. " #"..j..cnt..recipe.materials[j].label, "showRecipe", 0xFFA200, 0x484848, "left")
  366.     end
  367.  
  368.     local maxCraftable = self:getStockForRecipe(i, false)
  369.    
  370.     self:addButton(2, 23, 20, 3, "back", "showRecipe", 0xFFA200, 0x282828, "center", "drawMain")
  371.     self:addButton(63, 25, 18, 1, "remove recipe", "showRecipe", 0x0, 0x9A1200, "center", "removeRecipe", recipe)
  372.    
  373.     if maxCraftable >= 1 then self:addButton(2, 8, 20, 3, "craft recipe", "showRecipe", 0x282828, 0xFFA200, "center", "craftRecipeLoop", {recipe = i, count = 1}) end  
  374.     if maxCraftable > 1 then self:addButton(2, 12, 20, 3, "craft recipe "..maxCraftable.."x", "showRecipe", 0x282828, 0xFFA200, "center", "craftRecipeLoop", {recipe = i, count = maxCraftable}) end   
  375.     self:drawScreen("showRecipe")  
  376. end
  377.  
  378. function hazeUI:removeRecipe(recipe)
  379.   for i=1,#recipes do
  380.     if recipes[i].output.name == recipe.output.name and recipes[i].output.label == recipe.output.label then
  381.         table.remove(recipes, i) end
  382.   end
  383.   self:saveRecipesConfig()
  384.   self:drawMain()
  385. end
  386.  
  387. function hazeUI:compressRecipes()
  388.     recipes_compressed = {}
  389.     for d=1,#recipes do
  390.         recipes_compressed[d] = {}     
  391.         self:addCompressedRecipeItem(d, recipes[d].input)      
  392.         for i=1,#recipes[d].materials do
  393.             self:addCompressedRecipeItem(d, recipes[d].materials[i])
  394.     end end    
  395.     upgradeRecipes_compressed = {}
  396.     for d=1,#upgradeRecipes do
  397.         upgradeRecipes_compressed[d] = {}
  398.         for i=1,#upgradeRecipes[d] do
  399.             self:addCompressedUpgradeRecipeItem(d, upgradeRecipes[d][i])
  400.     end end    
  401. end
  402.  
  403. function hazeUI:addCompressedUpgradeRecipeItem(d, item)
  404.     for i=1,#upgradeRecipes_compressed[d] do
  405.         if upgradeRecipes_compressed[d][i].label == item.label and upgradeRecipes_compressed[d][i].name == item.name then
  406.             upgradeRecipes_compressed[d][i].size = upgradeRecipes_compressed[d][i].size + item.size
  407.             return true
  408.     end end
  409.     table.insert(upgradeRecipes_compressed[d], clone(item))
  410. end
  411.  
  412. function hazeUI:addCompressedRecipeItem(d, item)
  413.     for i=1,#recipes_compressed[d] do
  414.         if recipes_compressed[d][i].label == item.label and recipes_compressed[d][i].name == item.name then
  415.             recipes_compressed[d][i].size = recipes_compressed[d][i].size + item.size
  416.             return true
  417.     end end
  418.     table.insert(recipes_compressed[d], clone(item))
  419. end
  420.  
  421. function hazeUI:checkItems(recipe) 
  422.     for i=1,#recipes_compressed[recipe] do
  423.         if self:checkItem(recipes_compressed[recipe][i]) == false then
  424.             return false
  425.         end end
  426.     return true
  427. end
  428.  
  429. function hazeUI:checkItem(item)
  430.   for i=1,#inventory do
  431.     if item.label == inventory[i].label and item.name == inventory[i].name then
  432.       if item.size <= inventory[i].size then
  433.         return inventory[i].size
  434.   end end end
  435.   return false
  436. end
  437.  
  438. function hazeUI:addCompressedInventory(item)   
  439.     for i=1,#inventory do
  440.         if inventory[i].label == item.label and inventory[i].name == item.name then
  441.             inventory[i].size = inventory[i].size + item.size
  442.             return true
  443.     end end
  444.     table.insert(inventory, clone(item))
  445. end
  446.  
  447. function hazeUI:readInventory()
  448.     self:drawScreen("clear")
  449.     upgradeKeys = {}
  450.     inventoryAll = {}
  451.     local crafter = component.proxy(config.devices.transposer.crafter.address)
  452.     local s = crafter.getInventorySize(config.devices.transposer.crafter.sideInput)
  453.     for i=1,s do
  454.         self:drawStatusbar(5, 5, 40, 1, s, i, "syncing inventory...")
  455.         local tmp = crafter.getStackInSlot(config.devices.transposer.crafter.sideInput, i)
  456.         if tmp ~= nil then
  457.             tmp.invSlot = i
  458.                
  459.             if string.find(tmp.label, "Upgrade Key") then
  460.                 tmp.nameShort = string.gsub(tmp.label, 'Upgrade Key ', "")
  461.                 tmp.nameShort = string.gsub(tmp.nameShort, "[-()]+", "")
  462.                table.insert(upgradeKeys, tmp)              
  463.             else
  464.                 table.insert(inventoryAll, tmp)
  465.             end end end
  466.  
  467.     sortUpgradeKeys()
  468.  
  469.     self:compressInventory()
  470.    
  471.     self:drawMain()
  472. end
  473.  
  474. function hazeUI:compressInventory()
  475.     inventory = {} 
  476.     for i=1,#inventoryAll do self:addCompressedInventory(inventoryAll[i]) end
  477. end
  478.  
  479. function hazeUI:transferItemToCrafter(item, size)
  480.   local i = self:getFromInventory(item)
  481.   if inventoryAll[i].size < item.size then
  482.     size = inventoryAll[i].size
  483.   end
  484.   local crafter = component.proxy(config.devices.transposer.crafter.address)
  485.   crafter.transferItem(config.devices.transposer.crafter.sideInput, config.devices.transposer.crafter.sideCrafter, size, inventoryAll[i].invSlot)
  486.   inventoryAll[i].size = (inventoryAll[i].size - size)
  487.   return size
  488. end
  489.  
  490. function hazeUI:transferItemToInjector(injec, item, size)
  491.   local i = self:getFromInventory(item)
  492.   if inventoryAll[i].size < size then
  493.     size = inventoryAll[i].size
  494.   end  
  495.   if size > 0 then
  496.     index = getTransposerIndex(injec.address)
  497.     injec.transferItem(config.devices.transposer.injectors[index].sideInventory, config.devices.transposer.injectors[index].sideInjector, size, inventoryAll[i].invSlot)
  498.     inventoryAll[i].size = (inventoryAll[i].size - size)
  499.   end
  500.   return size
  501. end
  502.  
  503. function hazeUI:moveCraftingOutputToInputChest()
  504.     local crafter = component.proxy(config.devices.transposer.crafter.address)
  505.     crafter.transferItem(config.devices.transposer.crafter.sideCrafter, config.devices.transposer.crafter.sideInput)
  506.     self:readInventory()
  507. end
  508.  
  509. function hazeUI:craftRecipe(d)
  510.   self:drawScreen("craftRecipe")
  511.  
  512.   --self:setElement({index = titleBar, text = "draconic infusion crafting, crafting: "..recipes[d].output.label})
  513.   --self:drawElement(titleBar)
  514.  
  515.   self:drawStatusbar(5, 5, 40, 1, 10, 1, "moving items to crafter...")
  516.    
  517.   local tmpSize = recipes[d].input.size
  518.   while tmpSize > 0 do
  519.     tmpSize = tmpSize - self:transferItemToCrafter(recipes[d].input, tmpSize)
  520.   end
  521.  
  522.   for j=1,#recipes[d].materials do
  523.     self:drawStatusbar(5, 5, 40, 1, 10, 1+j, "moving items to injectors...")
  524.     local tmpSize = recipes[d].materials[j].size
  525.     while tmpSize > 0 do
  526.       tmpSize = tmpSize - self:transferItemToInjector(component.proxy(config.devices.transposer.injectors[j].address), recipes[d].materials[j], tmpSize)
  527.     end
  528.   end  
  529.  
  530.   recipeOutput = recipes[d].output
  531.   self:startCrafting()
  532.  
  533.   --self:setElement({index = titleBar, text = "draconic infusion crafting, "..recipes[d].output.label})
  534.   --self:drawElement(titleBar)
  535.  
  536.   --self:drawElement(self:addButton(3, 15, 50, 3, "move items to input chest", "craftRecipe", 0xFFA200, 0x282828, "left", "moveCraftingOutputToInputChest"))
  537.  
  538.   --for j=1,10 do
  539. --  self:drawStatusbar(3, 10, 50, 1, 10, j, "moving output to storage after timeout")
  540. --  os.sleep(0.3)
  541.  -- end
  542.   local crafter = component.proxy(config.devices.transposer.crafter.address)
  543.   if crafter.getStackInSlot(config.devices.transposer.crafter.sideCrafter, config.devices.transposer.crafter.slotOutput) ~= nil then
  544.     crafter.transferItem(config.devices.transposer.crafter.sideCrafter, config.devices.transposer.crafter.sideInput, 64, config.devices.transposer.crafter.slotOutput)
  545.   end
  546. end
  547.  
  548. function hazeUI:flushInventory()
  549.   for i=1,#inventoryAll do if inventoryAll[i].size < 1 then
  550.     table.remove(inventoryAll, i)
  551.     return self:flushInventory()
  552.   end end
  553.  
  554.   self:compressInventory()
  555.   return true
  556. end
  557.  
  558. function hazeUI:getFromInventory(item)
  559.   self:flushInventory()  
  560.   for i=1,#inventoryAll do
  561.     if inventoryAll[i].name == item.name and inventoryAll[i].label == item.label then
  562.         return i
  563.   end end  
  564.   return nil
  565. end
  566.  
  567.  
  568. selectedUpgrades = {}
  569.  
  570. function hazeUI:isUpgradeSelected(tier)
  571.     for d=1,#selectedUpgrades do
  572.         if selectedUpgrades[d] == tier then
  573.             return true
  574.         end
  575.     end
  576.  
  577.     return false
  578. end
  579.  
  580. function hazeUI:selectUpgrade(data)
  581.     for d=1,#selectedUpgrades do
  582.         if selectedUpgrades[d] == data[2] then
  583.             table.remove(selectedUpgrades, d)
  584.             self:showUpgrade(data[1])
  585.             return
  586.         end
  587.     end
  588.  
  589.     table.insert(selectedUpgrades, data[2])
  590.     self:showUpgrade(data[1])
  591. end
  592.  
  593. function hazeUI:showUpgrade(i)
  594.     local tierNames = { "Basic", "Wyvern", "Draconic", "Chaotic" }
  595.    
  596.     self:addButton(25, 3, 55, 1, "upgrade: "..upgradeKeys[i].nameShort, "showUpgrade", 0xFFA200, 0x282828, "left")
  597.    
  598.     --self:setElement({index = titleBar, text = "draconic infusion crafting, upgrade: "..upgradeKeys[i].nameShort.." ("..item.label..")"})
  599.     --self:drawElement(titleBar)   
  600.     local crafter = component.proxy(config.devices.transposer.crafter.address)
  601.     local item = crafter.getStackInSlot(1, 1)
  602.    
  603.     if item ~= nil then
  604.         self:addButton(25, 4, 55, 1, "item: "..item.label, "showUpgrade", 0xFFA200, 0x282828, "left")  
  605.     end
  606.    
  607.     local row = 1
  608.     local col = 1
  609.     for tier=1,#upgradeRecipes_compressed do
  610.         if math.ceil(tier/2) > col then
  611.             col = math.ceil(tier/2)
  612.             row = 1
  613.         end
  614.        
  615.         local xoffset = 25+((col-1)*28)
  616.        
  617.         local maxCraftable = math.huge
  618.         self:addButton(xoffset, 5+row, 27, 1, tierNames[tier], "showUpgrade", 0x282828, 0xFFA200, "left", "selectUpgrade", {i, tier})
  619.            
  620.         for j=1,#upgradeRecipes_compressed[tier] do
  621.             local bgColor = 0x3AA700
  622.             local stockCnt = self:checkItem(upgradeRecipes_compressed[tier][j])
  623.             if stockCnt == false then
  624.               bgColor = 0x9D0000
  625.               maxCraftable = 0
  626.             elseif math.floor(stockCnt / upgradeRecipes_compressed[tier][j].size) < maxCraftable then
  627.               maxCraftable = math.floor(stockCnt / upgradeRecipes_compressed[tier][j].size)
  628.             end
  629.  
  630.             local bgColorBox = 0x282828
  631.             if self:isUpgradeSelected(tier) then bgColorBox = 0x316236 end
  632.                    
  633.             local cnt = ""
  634.             if upgradeRecipes_compressed[tier][j].size > 1 then cnt = ""..upgradeRecipes_compressed[tier][j].size .. "x " end
  635.             self:addButton(xoffset, 6+row, 2, 1, "", "showUpgrade", bgColor, bgColor, "left", "selectUpgrade", {i, tier})
  636.             self:addButton(xoffset+2, 6+row, 25, 1, cnt .. upgradeRecipes_compressed[tier][j].label, "showUpgrade", 0xFFA200, bgColorBox, "left", "selectUpgrade", {i, tier})
  637.             row = row + 1
  638.     end
  639.         row = row+2
  640.     end
  641.  
  642.     self:addButton(25, 23, 30, 3, "start upgrade", "showUpgrade", 0xFFA200, 0x282828, "center", "startUpgrade", { i, false })
  643.  
  644.     self:addButton(2, 23, 20, 3, "back", "showUpgrade", 0xFFA200, 0x282828, "center", "drawMain")
  645.     self:drawScreen("showUpgrade") 
  646. end
  647.  
  648. function hazeUI:startUpgrade(data)
  649.     local upgradeIndex = data[1]
  650.     local d = data[2]
  651.  
  652.     if not d then
  653.         --todo: check stock before starting batch craftingjobs
  654.         for tier=1,#upgradeRecipes_compressed do
  655.             if self:isUpgradeSelected(tier) then
  656.                 self:startUpgrade({ upgradeIndex, tier })
  657.             end
  658.         end
  659.         return
  660.     end
  661.  
  662.     local crafter = component.proxy(config.devices.transposer.crafter.address)
  663.     local item = crafter.getStackInSlot(1, 1)
  664.    
  665.     if item == nil then
  666.         self:addButton(2, 3, 70, 3, " put the item you want to upgrade in the crafter", "startUpgradeFailed", 0x282828, 0xFFA200, "left")
  667.         self:drawScreen("startUpgradeFailed")
  668.         while item == nil do
  669.             item = crafter.getStackInSlot(1, 1)
  670.             os.sleep(1)
  671.         end
  672.     end
  673.    
  674.     --self:setElement({index = titleBar, text = "draconic infusion crafting, upgrading: "..upgradeKeys[upgradeIndex].nameShort.." ("..item.label..")"})
  675.     --self:drawElement(titleBar)
  676.    
  677.     self:addButton(2, 3, 70, 1, " upgrade: "..upgradeKeys[upgradeIndex].nameShort.." ("..item.label..")", "craftUpgrade", 0xFFA200, 0x282828, "left")
  678.     self:drawScreen("craftUpgrade")
  679.    
  680.     self:drawStatusbar(5, 5, 40, 1, 13, 1, "moving updatekey to injector...")
  681.    
  682.     local injector = component.proxy(config.devices.transposer.injectors[9].address)
  683.    
  684.     injector.transferItem(config.devices.transposer.injectors[9].sideInventory, config.devices.transposer.injectors[9].sideInjector, 1, upgradeKeys[upgradeIndex].invSlot)
  685.    
  686.     for j=1,#upgradeRecipes[d] do
  687.         self:drawStatusbar(5, 5, 40, 1, 13, 1+j, "moving items to injectors...")
  688.         local tmpSize = upgradeRecipes[d][j].size
  689.         while tmpSize > 0 do
  690.             tmpSize = tmpSize - self:transferItemToInjector(component.proxy(config.devices.transposer.injectors[j].address), upgradeRecipes[d][j], tmpSize)
  691.         end
  692.     end  
  693.  
  694.     recipeOutput = "upgradeRecipe" -- need to set something to not confuse the new recipe detection
  695.     self:startCrafting()
  696.     self:drawStatusbar(5, 5, 40, 1, 13, 11, "moving updatekey back...")
  697.     injector.transferItem(config.devices.transposer.injectors[9].sideInjector, config.devices.transposer.injectors[9].sideInventory, 1, 1, upgradeKeys[upgradeIndex].invSlot)
  698.    
  699.     self:drawStatusbar(5, 5, 40, 1, 13, 12, "moving item back to input")
  700.    
  701.      crafter.transferItem(config.devices.transposer.crafter.sideCrafter, config.devices.transposer.crafter.sideCrafter, 1, 2, 1)
  702.      self:drawStatusbar(5, 5, 40, 1, 13, 13, "done")
  703.      
  704.      self:drawMain()   
  705. end
  706.  
  707. function cleanCrafter()
  708.     local crafter = component.proxy(config.devices.transposer.crafter.address)
  709.     --crafter
  710.     for i=1,crafter.getInventorySize(config.devices.transposer.crafter.sideCrafter) do
  711.         crafter.transferItem(config.devices.transposer.crafter.sideCrafter, config.devices.transposer.crafter.sideInput, 64, i)
  712.     end
  713. end
  714.  
  715. function cleanInjectors()
  716.     --injectors
  717.     for i=1,#config.devices.transposer.injectors do component.proxy(config.devices.transposer.injectors[i].address).transferItem(config.devices.transposer.injectors[i].sideInjector, config.devices.transposer.injectors[i].sideInventory) end
  718. end
  719.  
  720. function hazeUI:cleanup()
  721.     cleanCrafter()
  722.     cleanInjectors()
  723.     self:readInventory()
  724. end
  725.  
  726. redstoneInitDone = false
  727. function setRedstoneDevice(data)
  728.     if data.label == "crafter" then
  729.         config.devices.redstone.crafter.address = data.e.address
  730.     elseif data.label == "comparator" then
  731.         config.devices.redstone.status.address = data.e.address
  732.     end
  733.     redstoneInitDone = true
  734. end
  735.  
  736. function setRedstoneSide(data)
  737.     -- dont make this an elseif, as this SHOULD set both if theres only one redstone device
  738.     if config.devices.redstone.crafter.address == data.e.address then
  739.         config.devices.redstone.crafter.side = (data.value - 1)
  740.     end
  741.  
  742.     if config.devices.redstone.status.address == data.e.address then
  743.         config.devices.redstone.status.side = (data.value - 1)
  744.     end
  745.        
  746.     redstoneInitDone = true
  747. end
  748.  
  749. function configureRedstoneDevice(address)
  750.     gui:addButton(2, 3, 70, 3, "found redstone interface "..address, "initRedstone", 0x282828, 0xFFA200, "left")
  751.     gui:drawScreen("initRedstone")
  752.        
  753.     --redstoneInitDone = false
  754.     --gui:list("set redstone mode for "..address, "please select the connected device from the list", {"crafter", "comparator"}, { f = setRedstoneDevice, p = { address = address }}, nil, nil)
  755.     --while not redstoneInitDone do os.sleep(0.1); end
  756.  
  757.     redstoneInitDone = false
  758.     gui:list("set connected side for "..address, "please select the connected redstone side from the list", {"bottom", "top", "north", "south", "west", "east"}, { f = setRedstoneSide, p = { address = address }}, nil, nil, nil)
  759.     while not redstoneInitDone do os.sleep(0.1); end   
  760. end
  761.  
  762. function getTransposerIndex(address)
  763.     for i=1,#config.devices.transposer.injectors do
  764.         if config.devices.transposer.injectors[i].address == address then
  765.             return i
  766.         end
  767.     end
  768. end
  769.  
  770. function isOneOf(name, data)
  771.     for c=1,#data do if data[c].name == name then return data[c]; end end
  772.     return false
  773. end
  774.  
  775. function hasBlock(address, data)
  776.     for i=0,#sides-1 do
  777.         local data = isOneOf(component.proxy(address).getInventoryName(i), data)
  778.         if data then return { true, i, data }; end
  779.     end
  780.     return { false }
  781. end
  782.  
  783. function getCraftingType()
  784.     local name = config.devices.transposer.crafter.type
  785.     if name == "draconicevolution:fusion_crafting_core" then return "[draconic evolution] infusion crafting";
  786.     elseif name == "extendedcrafting:crafting_core" then return "[extended crafting] crafting core";
  787.     elseif name == "actuallyadditions:block_empowerer" then return "[actually additions] empowering";
  788.     end
  789. end
  790.  
  791. function configureTransposerDevice(address)
  792.     local sides = require("sides")
  793.     local storage = hasBlock(address, blocks.storage)
  794.  
  795.     if not storage[1] then
  796.         print("couldnt find a storage at transposer " .. address)
  797.         return
  798.     end
  799.  
  800.     local data = hasBlock(address, blocks.core)
  801.  
  802.     if data[1] ~= false then
  803.         print(data[3].label .. " (".. sides[data[2]] .."), storage (".. sides[storage[2]] ..")")
  804.         config.devices.transposer.crafter.address = address
  805.         config.devices.transposer.crafter.sideInput = storage[2]
  806.         config.devices.transposer.crafter.sideOutput = storage[2]
  807.         config.devices.transposer.crafter.sideCrafter = data[2]
  808.         config.devices.transposer.crafter.type = data[3].name
  809.  
  810.         if data[3].name == "draconicevolution:fusion_crafting_core" then
  811.             config.devices.transposer.crafter.slotOutput = 2
  812.         else
  813.             config.devices.transposer.crafter.slotOutput = 1
  814.         end
  815.  
  816.     else
  817.         data = hasBlock(address, blocks.input)
  818.         if data[1] == false then return; end
  819.         print(data[3].label .. " (".. sides[data[2]] .."), storage (".. sides[storage[2]] ..")")
  820.         table.insert(config.devices.transposer.injectors, { address = address, sideInjector = data[2], sideInventory = storage[2] })
  821.     end
  822. end
  823.  
  824. function compareOutputName(a, b)
  825.     return a.output.label < b.output.label
  826. end
  827.  
  828. function sortRecipes()
  829.     if #recipes > 1 then
  830.         table.sort(recipes, compareOutputName)
  831.     end
  832. end
  833.  
  834. function compareUpgradeKey(a, b)
  835.     return a.label < b.label
  836. end
  837.  
  838. function sortUpgradeKeys()
  839.     if #upgradeKeys > 1 then
  840.         table.sort(upgradeKeys, compareUpgradeKey)
  841.     end
  842. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement