Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Adapted from Marval's pastebin b2fc4912 but converted to English via ChatGPT
- -- All credit goes to Marval. This v.5 is untested as of yet, and likely needs some post-AI tweaking.
- -- This version was ran through GPTv3.5 'improvement' as it works with the code as a whole, and GPTv4 wants to break everything
- -- into small chunks.
- local peripheralAnalyzer = peripheral.wrap("right")
- local craftingSlots = {1, 2, 3, 5, 6, 7, 9, 10, 11}
- local tCraft = {}
- local function dropAllItems()
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- local function countItems(craftingGrid)
- local countList = {}
- for i = 1, 9 do
- countList[i] = {ID = nil, META = nil, COUNT = 0}
- end
- local startIndex = 0
- for i = 1, 9 do
- if craftingGrid[i][1] then
- countList[1].ID = craftingGrid[i][1]
- countList[1].META = craftingGrid[i][2]
- countList[1].COUNT = 1
- startIndex = i
- break
- end
- end
- local itemCount = 1
- for i = startIndex + 1, 9 do
- if craftingGrid[i][1] then
- local itemId = craftingGrid[i][1]
- local itemMeta = craftingGrid[i][2]
- local itemFound = false
- for j = 1, itemCount do
- if itemId == countList[j].ID and itemMeta == countList[j].META then
- countList[j].COUNT = countList[j].COUNT + 1
- itemFound = true
- break
- end
- end
- if not itemFound then
- itemCount = itemCount + 1
- countList[itemCount].ID = itemId
- countList[itemCount].META = itemMeta
- countList[itemCount].COUNT = 1
- end
- end
- end
- return countList, itemCount
- end
- local function checkAvailability(items, recordCount)
- local isAvailable = true
- local missingItem = nil
- for i = 1, recordCount do
- local slot = 0
- local itemId = 0
- local itemMeta = 0
- local itemCount = 0
- repeat
- slot = slot + 1
- itemId = peripheralAnalyzer.getBlockIdAt(slot)
- itemMeta = peripheralAnalyzer.getBlockMetadataAt(slot)
- if itemId == items[i].ID and (items[i].META == nil or itemMeta == items[i].META) then
- itemCount = itemCount + peripheralAnalyzer.getBlockCountAt(slot)
- end
- until slot == 109
- if itemCount < items[i].COUNT then
- isAvailable = false
- missingItem = items[i].META == nil and items[i].ID or (items[i].ID .. ":" .. items[i].META)
- print("Missing item ID: " .. missingItem .. " in quantity: " .. items[i].COUNT - itemCount)
- break
- end
- end
- return isAvailable, missingItem
- end
- local function craft(items)
- for i = 1, 9 do
- if items[i][1] then
- local slot = 0
- repeat
- slot = slot + 1
- local itemId = peripheralAnalyzer.getBlockIdAt(slot)
- local itemMeta = peripheralAnalyzer.getBlockMetadataAt(slot)
- until (itemId == items[i][1] and (items[i][2] == nil or itemMeta == items[i][2]))
- local itemCount = peripheralAnalyzer.getBlockCountAt(slot)
- turtle.select(craftingSlots[i])
- peripheralAnalyzer.takeAt(slot)
- turtle.drop(itemCount - 1)
- end
- end
- turtle.craft()
- dropAllItems()
- end
- local function manageCrafting(recipe)
- print("Crafting " .. b)
- local isReadyToCraft = false
- while not isReadyToCraft do
- local countList, itemCount = countItems(recipe)
- isReadyToCraft, missingItem = checkAvailability(countList, itemCount)
- if not isReadyToCraft then
- print("Missing " .. missingItem)
- missingItem = tostring(missingItem)
- crafting()
- else
- craft(recipe)
- end
- end
- end
- local function crafting(b)
- if tCraft[b] then
- manageCrafting(tCraft[b])
- else
- print("Missing: " .. b)
- os.pullEvent()
- end
- end
- local function startCrafting()
- local file = io.open("receptury", "r")
- while true do
- local line = file:read()
- if line == nil then break end
- local item, recipeParts = string.match(line, "([%w%:]+)=(.+)")
- tCraft[item] = {}
- for i = 1, 9 do
- local part = select(i, string.match(recipeParts, "([^;]+)"))
- if part == "0" then
- tCraft[item][i] = {nil}
- elseif string.find(part, ":") then
- local id, meta = string.match(part, "(%d+):(%d+)")
- tCraft[item][i] = {tonumber(id), tonumber(meta)}
- else
- tCraft[item][i] = {tonumber(part)}
- end
- end
- end
- file:close()
- print("What to craft? Enter ID or ID:META")
- local b = read()
- local originalItem = b
- print("How many?")
- local quantity = tonumber(read())
- for zz = 1, quantity do
- crafting(b)
- b = originalItem
- end
- end
- local function displayMenu()
- term.clear()
- local option = 2
- term.setCursorPos(1, 1)
- term.write("Ultimate Crafting Program")
- local menuOptions = {
- "Start Crafting",
- "Add Recipe by Scanning",
- "Add Recipe Manually"
- }
- for i, optionText in ipairs(menuOptions) do
- term.setCursorPos(1, i + 1)
- term.write("[ ] " .. optionText)
- end
- end
- local function refreshMenu(option)
- for i = 2, 4 do
- term.setCursorPos(2, i)
- term.write(" ")
- end
- term.setCursorPos(2, option)
- term.write("X")
- end
- local function saveScannedRecipe()
- print("Arrange the recipe in the turtle's inventory")
- os.pullEvent()
- local recipe = {}
- for i = 1, 9 do
- turtle.select(craftingSlots[i])
- local id = peripheralAnalyzer.getBlockId()
- if id then
- recipe[i] = {ID = id, META = peripheralAnalyzer.getBlockMetadata()}
- else
- recipe[i] = {ID = nil}
- end
- end
- local recipeString = {}
- for i = 1, 9 do
- if recipe[i].ID == nil then
- recipeString[i] = "0"
- elseif recipe[i].META == nil then
- recipeString[i] = tostring(recipe[i].ID)
- else
- recipeString[i] = recipe[i].ID .. ":" .. recipe[i].META
- end
- end
- turtle.craft()
- turtle.select(11)
- local craftedItem = peripheralAnalyzer.getBlockId() .. ":" .. peripheralAnalyzer.getBlockMetadata()
- local file = io.open("receptury", "a")
- file:write(string.format("%s=%s;%s;%s;%s;%s;%s;%s;%s;%s\n", craftedItem, unpack(recipeString)))
- file:close()
- end
- local function inputAndSaveRecipe()
- term.clear()
- print("Enter the recipe in the format: ")
- print("ID:META=ID:META;ID:META; ... ;ID:META")
- print("You can also input IDs without metadata")
- local newRecipe = read()
- local file = io.open("receptury", "a")
- file:write(newRecipe .. "\n")
- file:close()
- end
- local function main()
- displayMenu()
- local option = 2
- refreshMenu(option)
- while true do
- local event, keyCode = os.pullEvent("key")
- if keyCode == 208 and option < 4 then
- option = option + 1
- elseif keyCode == 200 and option > 2 then
- option = option - 1
- end
- if keyCode == 28 then
- term.clear()
- term.setCursorPos(1, 1)
- if option == 2 then
- startCrafting()
- elseif option == 3 then
- saveScannedRecipe()
- elseif option == 4 then
- inputAndSaveRecipe()
- end
- displayMenu()
- refreshMenu(option)
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement