Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function working( func , ... )
- local co = coroutine.create(func)
- local ok,filter = coroutine.resume(co, ...)
- local eve = {}
- while coroutine.status(co) ~= "dead" do
- eve = {os.pullEventRaw()}
- --------------------------------
- -- retuen to func
- --------------------------------
- if filter == nil or filter == eve[1] or eve[1] == "terminate" then
- local param
- ok,param = coroutine.resume( co, unpack(eve) )
- if not ok then
- error( param )
- else
- filter = param
- end
- end
- if eve[1] == "key" and eve[2] == 20 then -- push "T" is stoping prossce
- break
- end
- end
- end
- function chestMatome(chesto)
- local r ={}
- for k, v in pairs(chesto.getAllStacks()) do
- if r[v.all().raw_name] then
- r[v.all().raw_name] = r[v.all().raw_name] + v.all().qty
- else
- r[v.all().raw_name] = v.all().qty
- end
- end
- return r
- end
- function sorting(chest)
- local chestSide = {top="down",bottom="up"}
- local data = chest.getAllStacks()
- for k, v in pairs(data) do
- for l, x in pairs(data) do
- if k < l and v.all().raw_name == x.all().raw_name
- and v.all().raw_name ~= "" and v.all().qty < v.all().max_size
- and x.all().qty < x.all().max_size
- and not v.out and not x.out then
- chest.pushItem(chestSide[chest.side],l)
- chest.pushItem(chestSide[chest.side],k)
- data[k].out = ""
- data[l].out = ""
- if chest.side == "top" then
- turtle.dropUp()
- elseif chest.side == "bottom" then
- turtle.dropDown()
- end
- end
- end
- end
- end
- function recipeMatome(recipe)
- local r ={}
- if recipe.mat then
- for k, v in pairs(recipe.mat) do
- if r[v] then
- r[v] = r[v] + 1
- else
- r[v] = 1
- end
- end
- end
- return r
- end
- function haveMat(rmatome,cmatome,many)
- for k, v in pairs(rmatome) do
- if cmatome[k] == nil then return k,v * many end
- if cmatome[k] < v * many then
- return k, v * many - cmatome[k]
- end
- end
- return nil,0
- end
- function findRecipe(craft,book)
- for k, v in pairs(book) do
- if v.craft == craft then
- return v
- end
- end
- return nil
- end
- function askMake(recipe,chest,book,many)
- local mrec = recipeMatome(recipe)
- for k, v in pairs(mrec) do
- if chest[k] and chest[k] >= v * many then
- chest[k] = chest[k] - v * many
- --write(k.."-"..v * many.." ")
- else
- local ask = findRecipe(k,book)
- if ask then
- -- local copychest = {}
- -- for l, x in pairs(chest) do
- -- copychest[l] = x
- -- end
- local m = v * many
- -- if chest[k] then
- -- m = m - chest[k]
- -- end
- if askMake(ask,chest,book, math.ceil(m / ask.craft_many)) then
- -- for l, x in pairs(copychest) do
- -- chest[l] = x
- -- end
- -- chest = copychest
- if chest[k] then
- chest[k] = chest[k] + math.ceil(m / ask.craft_many) * ask.craft_many - m
- else
- chest[k] = math.ceil(m / ask.craft_many) * ask.craft_many - m
- end
- --write(k.."n"..m.."="..chest[k].." ")
- else
- return false
- end
- else
- return false
- end
- end
- end
- return true
- end
- function canMake(recipe,chest,book)
- local mchs = chestMatome(chest)
- local can = 0
- while true do
- if not askMake(recipe,mchs,book,1) then
- break
- end
- can = can + 1
- end
- return can
- end
- function AllDrop(chesto)
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if chesto.side == "top" then
- turtle.dropUp()
- elseif chesto.side == "bottom" then
- turtle.dropDown()
- end
- end
- end
- turtle.select(1)
- end
- function autoCraft(chesto,book)
- while true do
- for k, v in pairs(book) do
- if v.code == "recipe" then
- local ans , ansm = haveMat(recipeMatome(v), chestMatome(chesto),1)
- if ans == nil then
- crafting(v,chesto,book,canMake(v,chesto,book))
- end
- end
- --sorting(chesto)
- chesto.condenseItems()
- end
- sleep(0.1)
- end
- end
- function castCraft( recipe,chesto,book )
- crafting( recipe,chesto,book,canMake(recipe,chesto,book) )
- end
- function crafting( recipe,chesto,book,many )
- --------------mat check------------------
- AllDrop(chesto)
- local lmany = many
- local nokori = 0
- if lmany > math.floor( recipe.craft_max / recipe.craft_many ) then
- nokori = many - math.floor( recipe.craft_max / recipe.craft_many )
- lmany = math.floor( recipe.craft_max / recipe.craft_many )
- end
- while true do
- local ans,ansm = haveMat( recipeMatome(recipe), chestMatome(chesto) ,lmany)
- if ans == nil then break end
- local find = false
- for k, v in pairs(book) do
- if v.craft == ans then
- if not crafting(v,chesto,book,math.ceil(ansm/v.craft_many)) then return false end
- find = true
- break
- end
- end
- if not find then return false end
- end
- local turtleTable = {1,2,3, 5,6,7, 9,10,11}
- local chestSide = {top="down",bottom="up"}
- for k, v in pairs(recipe.mat) do
- loading = lmany
- for l, x in pairs(chesto.getAllStacks()) do
- if v == x.all().raw_name then
- loading = loading - chesto.pushItem(chestSide[chesto.side],l,loading,turtleTable[k])
- if loading == 0 then break end
- end
- end
- end
- turtle.craft()
- AllDrop(chesto)
- if nokori > 0 then crafting( recipe,chesto,book,nokori ) end
- return true
- end
- function writeColor(str,color)
- term.setTextColor(color)
- write(str)
- term.setTextColor(colours.white)
- end
- function isChest( side )
- if side == "top" or side == "bottom" then
- if peripheral.wrap(side).getInventorySize() >= 27 then
- return true
- end
- end
- return false
- end
- function isTable(side)
- if peripheral.getType(side) == "crafters_craftingstation" then
- return true
- end
- return false
- end
- function init()
- local periList = peripheral.getNames()
- local craft = nil
- local chest = nil
- for k, v in pairs(periList) do
- if isChest(v) then
- chest = peripheral.wrap(v)
- chest["side"] = v
- end
- if isTable(v) then
- craft = peripheral.wrap(v)
- end
- end
- if not craft or not chest then
- return false,nil,nil
- end
- return true,craft,chest
- end
- function checkNewRecipe( table,book )
- local tabledata = table.getAllStacks()
- if tabledata[1] == nil then
- return false,"not recipe"
- end
- local newrecipe = getNewRecipe(table)
- for k, v in pairs(book) do
- if v.craft == newrecipe.craft then
- return false,"i know"
- end
- end
- return true
- end
- function getNewRecipe( table )
- local tabledata = table.getAllStacks()
- local r = {code = "recipe",mat = {},dis_mat={},craft = "",dis_craft=""}
- for k, v in pairs(tabledata) do
- if k == 1 then
- r.craft = v.all().raw_name
- r.dis_craft = v.all().display_name
- r.craft_many = v.all().qty
- r.craft_max = v.all().max_size
- else
- r.mat[k-1] = v.all().raw_name
- r.dis_mat[k-1] = v.all().display_name
- end
- end
- return r
- end
- function writeMatLine(recipe,pos,chest,book)
- for k, v in pairs(chest) do
- if recipe.mat[pos] == v.all().raw_name then
- writeColor(recipe.dis_mat[pos].."\n",colours.white)
- return
- end
- end
- for k, v in pairs(book) do
- if recipe.mat[pos] == v.craft then
- writeColor(recipe.dis_mat[pos].."\n",colors.yellow)
- return
- end
- end
- writeColor(recipe.dis_mat[pos].."\n",colors.red)
- end
- function writeRecipe( recipe,chest,book )
- write("craft > ".. recipe.dis_craft.."\n")
- local i = 1
- ----------------------------123------------------------------------
- if recipe.mat[i] then writeMatLine(recipe,i,chest,book) end
- i = i + 1
- if recipe.mat[i] then
- write(" ")
- writeMatLine(recipe,i,chest,book)
- end
- i = i + 1
- if recipe.mat[i] then
- write(" ")
- writeMatLine(recipe,i,chest,book)
- end
- i = i + 1
- ----------------------------456------------------------------------
- if recipe.mat[i] then writeMatLine(recipe,i,chest,book) end
- i = i + 1
- if recipe.mat[i] then
- write(" ")
- writeMatLine(recipe,i,chest,book)
- end
- i = i + 1
- if recipe.mat[i] then
- write(" ")
- writeMatLine(recipe,i,chest,book)
- end
- i = i + 1
- ----------------------------789------------------------------------
- if recipe.mat[i] then writeMatLine(recipe,i,chest,book) end
- i = i + 1
- if recipe.mat[i] then
- write(" ")
- writeMatLine(recipe,i,chest,book)
- end
- i = i + 1
- if recipe.mat[i] then
- write(" ")
- writeMatLine(recipe,i,chest,book)
- end
- end
- function addRecipeToFile(recipe)
- local f = fs.open("recipes","a")
- local w = ""
- for i = 1, 9 do
- if recipe.mat[i] then
- w = w .. recipe.mat[i] .. ","
- else
- w = w .. "nil,"
- end
- end
- for i = 1, 9 do
- if recipe.mat[i] then
- w = w .. recipe.dis_mat[i] .. ","
- else
- w = w .. "nil,"
- end
- end
- w = w .. recipe.craft .. "," .. recipe.dis_craft .. "," .. recipe.craft_many..","..recipe.craft_max
- f.writeLine(w)
- f.close()
- end
- function split(str)
- local r = {}
- for v in string.gmatch(str,"[^,]+") do
- r[#r+1] = v
- end
- write(#r)
- return r
- end
- function loadRecipe()
- local r = {{code="addrecipe"},{code="reset"},{code="autocraft"},{code="search"}}
- local f = fs.open("recipes","r")
- if f == nil then return r end
- local line = f.readLine()
- while line do
- local adds = {code = "recipe",mat = {},dis_mat={},craft = "",dis_craft=""}
- local datas = split(line,"[^\,]+")
- local l = 1
- for i = 1, 9 do
- if datas[l] ~= "nil" then
- adds.mat[i] = datas[l]
- end
- l = l + 1
- end
- for i = 1, 9 do
- if datas[l] ~= "nil" then
- adds.dis_mat[i] = datas[l]
- end
- l = l + 1
- end
- adds.craft = datas[l]
- l = l + 1
- adds.dis_craft = datas[l]
- l = l + 1
- adds.craft_many = tonumber(datas[l])
- l = l + 1
- if datas[l] then
- adds.craft_max = tonumber(datas[l])
- else
- adds.craft_max = 64
- end
- r[#r+1] = adds
- line = f.readLine()
- end
- f.close()
- return r
- end
- function isHit( recipe, code )
- ----------- T return for roll end hit -------------
- if not recipe or recipe.code ~= "recipe" or code == "" then return true end
- if string.find( string.lower(recipe.dis_craft), string.lower(code) ) ~= nil then return true end
- for k, v in pairs(recipe.dis_mat) do
- if string.find( string.lower(v), string.lower(code) ) ~= nil then return true end
- end
- return false
- end
- -- {code="recipe",mat={"1","2","3", "4","5","6", "7","8","9"}
- -- ,dis_mat={"1","2","3", "4","5","6", "7","8","9"}
- -- ,craft="craftans",dis_craft,craft_many,craft_max}
- local recipes = loadRecipe()
- local initans,craft,chest = init()
- local args = {...}
- local searchCode = ""
- if initans then
- local cursol = 1
- if args[1] == "auto" then
- write("startup auto crafting. PUSH T Stop.\n")
- working(autoCraft,chest,recipes )
- cursol = 3
- end
- while true do
- ----------------view----------------
- term.clear()
- term.setCursorPos(1, 1)
- if recipes[cursol].code == "addrecipe" then
- local ncheck,nans = checkNewRecipe( craft, recipes )
- writeColor("ADD MODE\n",colors.brown)
- if ncheck then
- writeColor("ADD RECIPE : PUSH A\n",colors.green)
- writeRecipe( getNewRecipe(craft), chest.getAllStacks(), recipes )
- else
- if nans == "not recipe" then
- writeColor("Its not recipe\n",colors.red)
- elseif nans == "i know" then
- writeColor("i know it\n",colors.pink)
- writeRecipe( getNewRecipe(craft), chest.getAllStacks(), recipes )
- writeColor("canmake x"..canMake(getNewRecipe(craft),chest,recipes),colors.purple)
- writeColor("\nF:Full SPACE,ENTER:Single M:Select",colours.cyan)
- end
- end
- elseif recipes[cursol].code == "reset" then
- writeColor("RESET ALL RECIPE : PUSH R\n",colors.red)
- elseif recipes[cursol].code == "autocraft" then
- writeColor("AUTO CRAFTING MODE\n",colours.magenta)
- writeColor("SPACE,ENTER:Start Auto Craft\n",colours.magenta)
- elseif recipes[cursol].code == "search" then
- writeColor("SEARCH MODE : PUSH ENTER\n",colours.cyan)
- if searchCode ~= "" then
- writeColor("now code:*"..searchCode.."*\n",colors.green)
- end
- else
- writeColor("recipe Mode ",colors.lime)
- if searchCode == "" then
- write("\n")
- else
- writeColor("(kw:"..searchCode..")\n",colors.lime)
- end
- writeRecipe( recipes[cursol], chest.getAllStacks(), recipes )
- writeColor("canmake x"..canMake(recipes[cursol],chest,recipes),colors.purple)
- writeColor("\nF:Full SPACE,ENTER:Single M:Select",colours.cyan)
- end
- local eve = {os.pullEvent()}
- if eve[1] == "key" then
- if eve[2] == 205 then -- left key
- repeat
- cursol = cursol + 1
- if cursol > #recipes then
- cursol = 1
- end
- until isHit(recipes[cursol],searchCode)
- end
- if eve[2] == 203 then -- right key
- repeat
- cursol = cursol - 1
- if cursol < 1 then
- cursol = #recipes
- end
- until isHit(recipes[cursol],searchCode)
- end
- if eve[2] == 28 and recipes[cursol].code == "search" then -- space key
- write(">")
- searchCode = read()
- writeColor("add search code :"..searchCode.."\n",colors.lime)
- sleep(1)
- end
- ------------------------------------------------------------------------
- ---------------------- recipe mode ---------------------
- ------------------------------------------------------------------------
- if ( eve[2] == 57 or eve[2] == 28 ) and recipes[cursol].code == "recipe" then -- space key
- crafting( recipes[cursol],chest,recipes,1 )
- term.clear()
- term.setCursorPos(1, 1)
- writeColor("single crafting "..recipes[cursol].dis_craft.."\n",colors.lime)
- sleep(1)
- end
- if eve[2] == 33 and recipes[cursol].code == "recipe" then -- f key
- working(castCraft, recipes[cursol],chest,recipes )
- term.clear()
- term.setCursorPos(1, 1)
- writeColor("full crafting "..recipes[cursol].dis_craft.."\n",colors.lime)
- sleep(1)
- end
- if eve[2] == 50 and recipes[cursol].code == "recipe" then -- m key
- write("\ninput num>")
- sleep(0)
- local craftnum = tonumber(read())
- working(crafting, recipes[cursol],chest,recipes,craftnum)
- term.clear()
- term.setCursorPos(1, 1)
- writeColor("crafting "..recipes[cursol].dis_craft.."\n",colors.lime)
- sleep(1)
- end
- ------------------------------------------------------------------------
- ---------------------- add mode ---------------------
- ------------------------------------------------------------------------
- if ( eve[2] == 57 or eve[2] == 28 ) and recipes[cursol].code == "addrecipe" and craft.getAllStacks()[1] ~= nil then -- space key
- writeColor("single crafting start\n",colors.lime)
- crafting( getNewRecipe(craft),chest,recipes,1 )
- term.clear()
- term.setCursorPos(1, 1)
- writeColor("single crafting end"..getNewRecipe(craft).dis_craft.."\n",colors.lime)
- sleep(1)
- end
- if eve[2] == 33 and recipes[cursol].code == "addrecipe" and craft.getAllStacks()[1] ~= nil then -- f key
- writeColor("full crafting start\n",colors.lime)
- working(castCraft, getNewRecipe(craft),chest,recipes )
- term.clear()
- term.setCursorPos(1, 1)
- writeColor("full crafting end"..getNewRecipe(craft).dis_craft.."\n",colors.lime)
- sleep(1)
- end
- if eve[2] == 50 and recipes[cursol].code == "addrecipe" then -- m key
- write("\ninput num>")
- sleep(0)
- local craftnum = tonumber(read())
- working(crafting, getNewRecipe(craft),chest,recipes,craftnum)
- term.clear()
- term.setCursorPos(1, 1)
- writeColor("crafting "..getNewRecipe(craft).dis_craft.."\n",colors.lime)
- sleep(1)
- end
- ------------------------------------------------------------------------------
- if ( eve[2] == 57 or eve[2] == 28 ) and recipes[cursol].code == "autocraft" then -- space key
- writeColor("crafting start\n",colors.lime)
- working(autoCraft,chest,recipes )
- sleep(1)
- term.clear()
- term.setCursorPos(1, 1)
- writeColor("crafting all\n",colors.lime)
- sleep(1)
- end
- -- A key
- if eve[2] == 30 and recipes[cursol].code == "addrecipe" and checkNewRecipe(craft,recipes) then
- recipes[#recipes+1] = getNewRecipe(craft)
- addRecipeToFile(getNewRecipe(craft))
- writeColor("\n\nADD RECIPE\n",colors.blue)
- cursol = #recipes
- sleep(2)
- end
- if eve[2] == 19 and recipes[cursol].code == "reset" then -- R key
- recipes = {{code="addrecipe"},{code="reset"}}
- fs.delete("recipes")
- cursol = 1
- writeColor("\n\nRESET RECIPE\n",colors.white)
- sleep(2)
- end
- end
- end
- else
- write("need crafting station and chest, top and bottom.\n")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement