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 OnList(targt,list)
- for k, v in pairs(list) do
- if targt == k then return true end
- end
- return false
- end
- function getList(book)
- local r = {}
- for k, v in pairs(book) do
- if v.mat then
- for l, x in pairs(v.mat) do
- if not r[x] then
- r[x] = 1
- end
- end
- 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 = "out"
- data[l].out = "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 ={}
- for k, v in pairs(recipe.mat) do
- if r[v] then
- r[v] = r[v] + 1
- else
- r[v] = 1
- 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 then
- chest[k] = chest[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 = 0
- if chest[k] == nil then
- m = v * many
- else
- m = v * many - chest[k]
- end
- if askMake(ask,copychest,book, math.ceil(m / ask.craft_many)) then
- for l, x in pairs(copychest) do
- chest[l] = x
- end
- chest[k] = math.ceil(m / ask.craft_many) * ask.craft_many - m
- 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 shipping(chesto,shiplist)
- local itemlist = chesto.getAllStacks()
- turtle.select(1)
- for k, v in pairs(itemlist) do
- if not OnList(v.all().raw_name,shiplist) then
- chesto.pushItem("up",k)
- if peripheral.getType("front") and peripheral.wrap("front").getInventorySize() > 20 then
- turtle.drop()
- end
- if turtle.getItemCount(1) > 0 and peripheral.getType("top") and peripheral.wrap("top").getInventorySize() > 20 then
- turtle.dropUp()
- end
- if turtle.getItemCount(1) > 0 then
- turtle.dropDown()
- end
- end
- end
- end
- function autoCraft(chesto,book)
- local nonshiplist = getList(book)
- while true do
- shipping(chesto,nonshiplist)
- 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( 64 / recipe.craft_many ) then
- nokori = many - math.floor( 64 / recipe.craft_many )
- lmany = math.floor( 64 / 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 == "bottom" then
- if peripheral.wrap(side).getInventorySize() >= 20 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
- for k, v in pairs(periList) do
- if isChest(v) then
- chest = peripheral.wrap(v)
- chest["side"] = v
- end
- end
- if not chest then
- write("no have under chest")
- return false,nil
- end
- return true,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
- 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.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
- f.writeLine(w)
- f.close()
- end
- function split(str)
- local r = {}
- -- local buff = ""
- -- for i = 1, string.len(str) do
- -- if string.sub(str,i,i+1) == "," then
- -- r[#r+1] = buff
- -- buff = ""
- -- else
- -- buff = buff .. string.sub(str,i,i+1)
- -- end
- -- end
- -- write(#r.."//"..buff)
- 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"}}
- local f = fs.open("qrecipes","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])
- r[#r+1] = adds
- line = f.readLine()
- end
- f.close()
- return r
- 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}
- rcode = "wkA7XrBs"
- local args = {...}
- if args[1] ~= nil then
- rcode = args[1]
- end
- fs.delete("qrecipes")
- shell.run("pastebin get " .. rcode .. " qrecipes")
- if not fs.exists("qrecipes") then shell.exit() end
- local recipes = loadRecipe()
- local initans,chest = init()
- if initans then
- write("\nqsort mode")
- working(autoCraft,chest,recipes)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement