Advertisement
krakaen

testing stuff and things

Feb 19th, 2019
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.82 KB | None | 0 0
  1. -- Modify blockLocation to change where the blocks are situated based on the computer
  2. local blockLocation = {storage = "right", altar = "back"}
  3.  
  4. local args = {...}
  5. local STATES = {crafting = "craft", orb = "orb", empty = "empty"}
  6. local chest = peripheral.wrap(blockLocation.storage)
  7. local altar = peripheral.wrap(blockLocation.altar)
  8. altar["state"] = STATES.empty
  9. altar["full"] = 0
  10.  
  11. local bloodOrb = {name = "bloodmagic:blood_orb"}
  12. local craftInstructions = {keepCrafting = true, step = 0, currentItemAmount = 0, amountToCraft = 1, baseItem = "", outcomeItem = ""}
  13.  
  14. local Recipes = {}
  15.  
  16. --********************************************--
  17. --           CRAFTING FUNCTIONS               --
  18. --********************************************--
  19.  
  20. function getFirstEmptySlot(storage)
  21.     for i = 1,storage.size(),1 do
  22.         if storage.list()[i] == nil then
  23.             return i
  24.         end
  25.     end
  26.  
  27.     return 0
  28. end
  29.  
  30. function craft()
  31.     local storageBaseInfo = getItemInfoFromStorage(craftInstructions["baseItem"], chest)
  32.     local storageOutcomeInfo = getItemInfoFromStorage(craftInstructions["outcomeItem"], chest)
  33.     if altar["state"] == STATES.crafting then
  34.         if getItemInAltar() == craftInstructions["outcomeItem"] then
  35.             local foundSpace = false
  36.             for name, data in pairs(storageOutcomeInfo["positions"]) do                
  37.                 if moveItemTo(1, data, altar, blockLocation.storage) ~= 0 and next(altar.list()) == nil then
  38.                     foundSpace = true
  39.                     break
  40.                 end
  41.             end
  42.             if foundSpace == false then
  43.                 moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
  44.             end
  45.             craftInstructions["step"] = craftInstructions["step"] + 1
  46.  
  47.             craftInstructions["currentItemAmount"] = craftInstructions["currentItemAmount"] + 1
  48.             term.clear()
  49.             write("\nTo close the program, press Q\n")
  50.             write("\nCrafted Items: " .. craftInstructions["currentItemAmount"])
  51.             if craftInstructions["amountToCraft"] ~= -1 then
  52.                 write("/" .. craftInstructions["amountToCraft"] .. "\n")
  53.             else
  54.                 write(" Crafted\n" .. getItemInfoFromStorage(craftInstructions["baseItem"], chest).nbrOfItems .. " left in Storage\n")
  55.             end
  56.  
  57.             if craftInstructions["step"] == bloodOrb["interval"] then
  58.                 moveBloodOrb()
  59.                 altar["state"] = STATES.orb
  60.                 craftInstructions["step"] = 0
  61.             else
  62.                 storageBaseInfo = getItemInfoFromStorage(craftInstructions["baseItem"], chest)
  63.                 if storageBaseInfo["nbrOfItems"] > 0 and craftInstructions["amountToCraft"] == -1 then
  64.                     moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
  65.                 elseif storageBaseInfo["nbrOfItems"] > 0 and craftInstructions["currentItemAmount"] < craftInstructions["amountToCraft"] then
  66.                     moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
  67.                 else
  68.                     moveBloodOrb()
  69.                     craftInstructions["keepCrafting"] = false                    
  70.                 end
  71.             end
  72.         elseif getItemInAltar() == "" then
  73.             storageBaseInfo = getItemInfoFromStorage(craftInstructions["baseItem"], chest)
  74.             if storageBaseInfo["nbrOfItems"] > 0 and craftInstructions["amountToCraft"] == -1 then
  75.                 moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
  76.             elseif storageBaseInfo["nbrOfItems"] > 0 and craftInstructions["currentItemAmount"] < craftInstructions["amountToCraft"] then
  77.                 moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
  78.             else
  79.                 moveBloodOrb()
  80.                 craftInstructions["keepCrafting"] = false                    
  81.             end
  82.         end        
  83.     elseif altar["state"] == STATES.orb then
  84.         if altar.getTanks()[1]["amount"]/ altar.getTanks()[1]["capacity"] > 0.90 and altar["full"] == 4 then
  85.             moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
  86.             moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
  87.             altar["state"] = STATES.crafting
  88.             altar["full"] = 0
  89.         elseif altar.getTanks()[1]["amount"]/ altar.getTanks()[1]["capacity"] > 0.90 and altar["full"] ~= 4 then
  90.             altar["full"] = altar["full"] + 1
  91.         end
  92.     elseif altar["state"] == STATES.empty then
  93.             moveItemTo(storageBaseInfo["positions"][1], 1, chest, blockLocation.altar)
  94.             altar["state"] = STATES.crafting
  95.             altar["full"] = 0
  96.     else
  97.         write("\nwth...")
  98.     end
  99.  
  100.     sleep(0.5)
  101. end
  102.  
  103. function moveBloodOrb()
  104.     if getItemInfoFromStorage(bloodOrb["name"], altar).nbrOfItems > 0  then
  105.         write("moving orb to storage")
  106.         moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
  107.     elseif getItemInfoFromStorage(bloodOrb["name"], chest).nbrOfItems > 0 then
  108.         write("moving orb to altar")
  109.         moveItemTo(getItemInfoFromStorage(bloodOrb["name"], chest).positions[1], 1, chest, blockLocation.altar)  
  110.     end
  111. end
  112.  
  113. function findOrbInStorage(storage)
  114.     local items = storage.list()
  115.     local positions = {}
  116.     local nbrOfItems = 0
  117.     for name, data in pairs(items) do
  118.         if data["name"] == "bloodmagic:blood_orb"  then
  119.             return name
  120.         end
  121.     end
  122.     return nil
  123. end
  124.  
  125. function getItemInAltar()
  126.     items = altar.list()
  127.     local item = next(altar.list())
  128.     if items[item] ~= nil then
  129.         return items[item].name .. ":" .. items[item].damage
  130.     end
  131.     return ""
  132. end
  133.  
  134. --*
  135. -- function that will return positions of the item in said storage with how many it contains
  136. -- item : string of item  ex: "minecraft:stone:0" or "bloodmagic:blood_orb"
  137. -- storage : peripheral in which we look for the item
  138. --*
  139. function getItemInfoFromStorage(item, storage)
  140.     local size = storage.size()
  141.     local itemList = storage.list()
  142.     local positions = {}
  143.     local nbrOfItems = 0
  144.  
  145.     local modName, itemName, itemDamage = item:match("([^,]+):([^,]+):([^,]+)")
  146.     if modName == nil and itemName == nil then
  147.         modName, itemName = item:match("([^,]+):([^,]+)")
  148.     end
  149.     if modName == nil and itemName == nil then
  150.         return {positions = positions, nbrOfItems = nbrOfItems}
  151.     end
  152.     if itemDamage == nil then
  153.         itemDamage = "0"
  154.     end
  155.     for i = 1,size,1 do
  156.         if itemList[i] ~= nil then
  157.             if itemList[i]["name"] == modName .. ":" .. itemName and tostring(itemList[i]["damage"]) == itemDamage then
  158.                 table.insert(positions, i)
  159.                 nbrOfItems = nbrOfItems + itemList[i]["count"]
  160.             end
  161.         end
  162.     end
  163.  
  164.     return {positions = positions, nbrOfItems = nbrOfItems}
  165. end
  166.  
  167. --*
  168. -- function that will move an item from one inventory to another
  169. -- positionFrom : position of the item to move in starting inventory
  170. -- positionTo : position of the item to move in starting inventory
  171. -- storage : starting inventory peripheral in which we look for the item
  172. -- direction : location of end inventory
  173. --*
  174. function moveItemTo(positionFrom, positionTo, storage, direction)
  175.     return storage.pushItems(direction, positionFrom, positionTo)
  176. end
  177.  
  178. function initializeCrafting()
  179.     if args[3] ~= nil and tonumber(args[3]) > -1 then
  180.         craftInstructions["amountToCraft"] = tonumber(args[3])
  181.     else
  182.         craftInstructions["amountToCraft"] = -1
  183.     end
  184.  
  185.     craftInstructions["baseItem"] = Recipes[args[2]]["base"]
  186.     craftInstructions["outcomeItem"] = Recipes[args[2]]["outcome"]
  187.  
  188.     if (verifyCraftingCommand() == false) then
  189.         return false
  190.     end
  191.  
  192.     if next(altar.list()) == nil then
  193.         altar["state"] = STATES.empty
  194.     elseif findOrbInStorage(altar) ~= nil then
  195.         altar["state"] = STATES.orb
  196.     elseif next(altar.list()) ~= nil then
  197.         moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
  198.         if (verifyCraftingCommand() == false) then
  199.             return false
  200.         end
  201.     else
  202.         local foundSpace = false
  203.         for name, data in pairs(storageOutcomeInfo["positions"]) do
  204.             if moveItemTo(1, data, altar, blockLocation.storage) ~= 0 and next(altar.list()) == nil then
  205.                 foundSpace = true
  206.                 break
  207.             end
  208.         end
  209.         if foundSpace == false then
  210.             moveItemTo(1, getFirstEmptySlot(chest), altar, blockLocation.storage)
  211.         end
  212.     end
  213.     return true
  214. end
  215.  
  216. function verifyCraftingCommand()
  217.     if tostring(args[3]) == "0" then
  218.         write("Done crafting 0 items.............\n")
  219.         return false
  220.     elseif getFirstEmptySlot(chest) == 0 then
  221.         write("You need an empty inventory spot to craft\n")
  222.         return false
  223.     elseif craftInstructions["amountToCraft"] ~= -1 then
  224.         if getItemInfoFromStorage(craftInstructions["baseItem"], chest).nbrOfItems < tonumber(args[3]) then
  225.             write("Not enough items in the inventory spot to craft\n")
  226.             return false
  227.         end
  228.     end
  229.     return true
  230. end
  231.  
  232. function exitProgramOnKey()
  233.     while true do
  234.         local eventList = { os.pullEvent() }
  235.         if eventList[1] == "key" then
  236.             if eventList[2] == 16 then  
  237.                 craftInstructions["keepCrafting"] = false    
  238.                 return
  239.             end      
  240.         end
  241.     end
  242. end
  243.  
  244. function activateCrafting()
  245.     bloodOrb["interval"] = 16;    
  246.     if (initializeCrafting() == false) then
  247.         return
  248.     end
  249.    
  250.     write("To close the program, press Q\n")
  251.     while craftInstructions["keepCrafting"] == true do
  252.         parallel.waitForAny(craft,exitProgramOnKey)
  253.     end
  254.     write("\nProgram Terminated\n")
  255. end
  256.  
  257. --- start check
  258. -- items to craft?
  259. -- place to put item?
  260. -- version of computercraft too old
  261. --
  262.  
  263.  
  264.  
  265. --********************************************--
  266. --            RECIPES FUNCTIONS               --
  267. --********************************************--
  268.  
  269. function listRecipes()
  270.     for name, data in pairs(Recipes) do
  271.         write(name .. " - base: " .. data["base"] .. ", outcome: " .. data["outcome"] .. "\n")
  272.     end
  273. end
  274.  
  275. function addRecipe()
  276.     write("Enter the name of the craft: ")
  277.     local name = read()
  278.     write("Enter the base of the craft: ")
  279.     local base = read()
  280.     write("Enter the outcome of the craft: ")
  281.     local outcome = read()
  282.     if setContains(Recipes, name) then
  283.         write("Recipe name already exists, replace? (y/n)")
  284.         local replacing = read()
  285.         if (string.sub(replacing,1,1) == "y") then
  286.             Recipes[name] = {base = base, outcome = outcome}
  287.             saveRecipeToFile()
  288.         else
  289.             write("Ending action..\n")            
  290.         end
  291.     else
  292.         Recipes[name] = {base = base, outcome = outcome}
  293.         saveRecipeToFile()
  294.     end
  295.  
  296. end
  297.  
  298. function modifyRecipe(name)
  299.     if setContains(Recipes, name) then
  300.         valid = false
  301.         while (valid == false)
  302.         do
  303.             write("Name: " .. name .. " - Base: " .. Recipes[name]["base"] .. " - Outcome: " .. Recipes[name]["outcome"] .. "\n")  
  304.             write("What would you like to change?\n")  
  305.             term.setTextColor(colors.green)
  306.             write ("name/base/outcome\n")
  307.             term.setTextColor(colors.white)
  308.             local elementToChange = read()
  309.             if elementToChange == "name" or elementToChange == "base" or elementToChange == "outcome" then
  310.                 write("Enter the new value of " .. elementToChange .. ":\n")
  311.                 local valueToChange = read()
  312.                 local fromElement = ""
  313.                 if elementToChange == "name" then
  314.                     fromElement = name
  315.                 else
  316.                     fromElement = Recipes[name][elementToChange]
  317.                 end
  318.                 writeColors({{"Are you sure you want to change the "}, {elementToChange, "green"}, {" from "}, {fromElement, "blue"}, {" to "}, {valueToChange, "lightBlue"}, {"?(y/n)"} })
  319.                 local confirmReplacement = read()
  320.                 if (string.sub(confirmReplacement,1,1) == "y") then
  321.                     local modifiedRecipe = {}    
  322.                     local newName = name                
  323.                     if elementToChange == "name" then
  324.                         modifiedRecipe[valueToChange] = {base = Recipes[name]["base"], outcome = Recipes[name]["outcome"]}
  325.                         newName = valueToChange
  326.                     elseif elementToChange == "base" then
  327.                         modifiedRecipe[name] = {base = valueToChange, outcome = Recipes[name]["outcome"]}
  328.                     elseif elementToChange == "outcome" then
  329.                         modifiedRecipe[name] = {base = Recipes[name]["base"], outcome = valueToChange}
  330.                     end
  331.                     if elementToChange == "name" and setContains(Recipes, newName) then
  332.                         writeColors({{"Recipe named "}, {newName, "green"}, {" already exists, replace?(y/n)"}})
  333.                         local replacing = read()
  334.                         if (string.sub(replacing,1,1) == "y") then
  335.                             Recipes[name] = nil
  336.                             Recipes[newName] = modifiedRecipe[newName]
  337.                             saveRecipeToFile()
  338.                             valid = true      
  339.                         end
  340.                     else
  341.                         Recipes[newName] = modifiedRecipe[newName]
  342.                         Recipes[name] = nil
  343.                         saveRecipeToFile()
  344.                         valid = true
  345.                     end
  346.                    
  347.                 else
  348.                     write("\n")            
  349.                 end
  350.                
  351.             else
  352.                 write("not a valid option\n")
  353.             end
  354.         end
  355.     else
  356.         write("There are no recipes with the name.\n")
  357.     end
  358. end
  359.  
  360. function deleteRecipe(name)
  361.     writeColors({{"Are you sure you want to delete the "}, {name, "green"}, {"?(y/n)"} })
  362.     local confirmDeleting = read()
  363.     if (string.sub(confirmDeleting,1,1) == "y") then
  364.         Recipes[name] = nil
  365.         writeColors({{name, "green"},{" has been deleted\n"}})
  366.         saveRecipeToFile()
  367.     end
  368. end
  369.  
  370. function saveRecipeToFile()
  371.     file = io.open("bloodmagic-recipes.txt","w")
  372.     for name, data in pairs(Recipes) do
  373.         file:write(name .. ";" .. data["base"] .. ";" .. data["outcome"] .. "\n")
  374.     end
  375.     file:flush()
  376.     file:close()
  377. end
  378.  
  379. function readRecipeFile()
  380.     file = io.open("bloodmagic-recipes.txt", "r")
  381.     if file then
  382.         for line in file:lines() do
  383.             if (line:match("([^,]+);([^,]+);([^,]+)")) then
  384.                 local name, base, outcome = line:match("([^,]+);([^,]+);([^,]+)")
  385.                 Recipes[name] = {base = base, outcome = outcome}
  386.             end
  387.         end
  388.     else
  389.         file = io.open("bloodmagic-recipes.txt","w")
  390.         file:close()
  391.     end
  392. end
  393.  
  394. function setContains(set, key)
  395.     return set[key] ~= nil
  396. end
  397.  
  398. function writeColors(input)
  399.     for name, data in pairs(input) do
  400.         if data[2] == nil then
  401.             data[2] = "white"
  402.         end
  403.         term.setTextColor(colors[data[2]])
  404.         write(data[1])
  405.         term.setTextColor(colors.white)
  406.     end
  407. end
  408.  
  409. --********************************************--
  410. --              HELP FUNCTIONS                --
  411. --********************************************--
  412. function help(sectionName)
  413.     if sectionName == "all" then  
  414.         writeColors({{"This program allows you to automate anything in the blood altar from blood magic, you can add, modify, delete recipes, craft a number of items or until they run out.\n"}})
  415.         writeColors({{"     "},{"help", "lightBlue"}, {" - get some help. can be done on all"}, {"\n     "}, {"functions\n"}})
  416.         writeColors({{"     "},{"craft", "lightBlue"}, {" - This option allows to craft items"}, {"\n     "}, {"automatically. For more info, type: "}, {"\n     "}, {shell.getRunningProgram() .. " craft help\n", "lime"}})
  417.         writeColors({{"     "},{"recipes", "lightBlue"}, {" - This option allows to add, modify,"}, {"\n     "}, {"delete or list recipes for more info, type:"}, {"\n     "}, {shell.getRunningProgram() .. " recipes help\n", "lime"}})
  418.     elseif sectionName == "craft" then
  419.         writeColors({{"     "}, {"The option craft allows you to craft items automatically"}})
  420.        
  421.     elseif sectionName == "recipes" then
  422.         writeColors({{"     "}, {"The option craft allows you to craft items automatically"}})
  423.     elseif sectionName == "add" then
  424.         writeColors({{"     "}, {"The option craft allows you to craft items automatically"}})
  425.     elseif sectionName == "modify" then
  426.         writeColors({{"     "}, {"The option craft allows you to craft items automatically"}})
  427.     elseif sectionName == "delete" then
  428.         writeColors({{"     "}, {"The option craft allows you to craft items automatically"}})
  429.     elseif sectionName == "list" then
  430.         writeColors({{"     "}, {"The option craft allows you to craft items automatically"}})
  431.     end
  432. end
  433.  
  434.  
  435. --********************************************--
  436. --             RUNNING STARTUP                --
  437. --********************************************--
  438.  
  439. readRecipeFile()
  440.  
  441. if args[1] == "recipes" then
  442.     if args[2] == "add" then
  443.         addRecipe()
  444.     elseif args[2] == "modify" then
  445.         if args[3] ~= nil then
  446.             modifyRecipe(args[3])
  447.         else
  448.             write({{"Missing 3rd argument, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
  449.         end
  450.     elseif args[2] == "delete" then
  451.         if args[3] ~= nil then
  452.             deleteRecipe(args[3])
  453.         else
  454.             writeColors({{"Missing 3rd argument, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
  455.         end
  456.     elseif args[2] == "list" then
  457.         write("---- Available Recipes ----\n")
  458.         term.setTextColor(colors.blue)
  459.         listRecipes()
  460.         term.setTextColor(colors.white)
  461.         write("---------------------------\n")
  462.     elseif args[2] == "help" then
  463.         help("recipes")
  464.     else
  465.         writeColors({{"Thats not how it works, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
  466.     end
  467. elseif args[1] == "craft" then
  468.     if args[2] ~= nil and Recipes[args[2]] ~= nil then
  469.         activateCrafting()
  470.     elseif args[2] == "help" then
  471.         help("craft")
  472.     else
  473.         writeColors({{"Thats not how it works, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
  474.     end
  475. elseif args[1] == "help" then
  476.     help("all")
  477. else
  478.     writeColors({{"Thats not how it works, use the "}, {"help", "lightBlue"}, {" command for more informations\n"}})
  479. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement