Advertisement
Ganeesya

sorting for quarry

Mar 29th, 2015
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.78 KB | None | 0 0
  1. function working( func , ... )
  2.     local co = coroutine.create(func)
  3.     local ok,filter = coroutine.resume(co, ...)
  4.     local eve = {}
  5.  
  6.     while coroutine.status(co) ~= "dead" do
  7.  
  8.         eve = {os.pullEventRaw()}
  9.         --------------------------------
  10.         -- retuen to func
  11.         --------------------------------
  12.         if filter == nil or filter == eve[1] or eve[1] == "terminate" then
  13.  
  14.             local param
  15.             ok,param = coroutine.resume( co, unpack(eve) )
  16.             if not ok then
  17.                 error( param )
  18.             else
  19.                 filter = param
  20.             end
  21.         end
  22.  
  23.         if eve[1] == "key" and eve[2] == 20 then -- push "T" is stoping prossce
  24.             break
  25.         end
  26.     end
  27. end
  28.  
  29. function chestMatome(chesto)
  30.     local r ={}
  31.     for k, v in pairs(chesto.getAllStacks()) do
  32.         if r[v.all().raw_name] then
  33.             r[v.all().raw_name] = r[v.all().raw_name] + v.all().qty
  34.         else
  35.             r[v.all().raw_name] = v.all().qty
  36.         end
  37.     end
  38.     return r
  39. end
  40.  
  41. function OnList(targt,list)
  42.     for k, v in pairs(list) do
  43.         if targt == k then return true end
  44.     end
  45.     return false
  46. end
  47.  
  48. function getList(book)
  49.     local r = {}
  50.     for k, v in pairs(book) do
  51.         if v.mat then
  52.             for l, x in pairs(v.mat) do
  53.                 if not r[x] then
  54.                     r[x] = 1
  55.                 end
  56.             end
  57.         end
  58.     end
  59.     return r
  60. end
  61.  
  62. function sorting(chest)
  63.     local chestSide = {top="down",bottom="up"}
  64.     local data = chest.getAllStacks()
  65.     for k, v in pairs(data) do
  66.         for l, x in pairs(data) do
  67.             if k < l and v.all().raw_name == x.all().raw_name
  68.                     and v.all().raw_name ~= "" and v.all().qty < v.all().max_size
  69.                     and x.all().qty < x.all().max_size
  70.                     and not v.out and not x.out then
  71.                 chest.pushItem(chestSide[chest.side],l)
  72.                 chest.pushItem(chestSide[chest.side],k)
  73.                 data[k].out = "out"
  74.                 data[l].out = "out"
  75.                 if chest.side == "top" then
  76.                     turtle.dropUp()
  77.                 elseif chest.side == "bottom" then
  78.                     turtle.dropDown()
  79.                 end
  80.             end
  81.         end
  82.     end
  83. end
  84.  
  85. function recipeMatome(recipe)
  86.     local r ={}
  87.     for k, v in pairs(recipe.mat) do
  88.         if r[v] then
  89.             r[v] = r[v] + 1
  90.         else
  91.             r[v] = 1
  92.         end
  93.     end
  94.     return r
  95. end
  96.  
  97. function haveMat(rmatome,cmatome,many)
  98.     for k, v in pairs(rmatome) do
  99.         if cmatome[k] == nil then return k,v * many end
  100.         if cmatome[k] < v * many then
  101.             return k, v * many - cmatome[k]
  102.         end
  103.     end
  104.     return nil,0
  105. end
  106.  
  107. function findRecipe(craft,book)
  108.     for k, v in pairs(book) do
  109.         if v.craft == craft then
  110.             return v
  111.         end
  112.     end
  113.     return nil
  114. end
  115.  
  116. function askMake(recipe,chest,book,many)
  117.  
  118.     local mrec = recipeMatome(recipe)
  119.     for k, v in pairs(mrec) do
  120.         if chest[k] and chest[k] >= v then
  121.             chest[k] = chest[k] - v * many
  122.         else
  123.             local ask = findRecipe(k,book)
  124.             if ask then
  125.                 local copychest = {}
  126.                 for l, x in pairs(chest) do
  127.                     copychest[l] = x
  128.                 end
  129.                 local m = 0
  130.                 if chest[k] == nil then
  131.                     m = v * many
  132.                 else
  133.                     m = v * many - chest[k]
  134.                 end
  135.                 if askMake(ask,copychest,book, math.ceil(m / ask.craft_many)) then
  136.                     for l, x in pairs(copychest) do
  137.                         chest[l] = x
  138.                     end
  139.                     chest[k] = math.ceil(m / ask.craft_many) * ask.craft_many - m
  140.                 else
  141.                     return false
  142.                 end
  143.             else
  144.                 return false
  145.             end
  146.         end
  147.     end
  148.     return true
  149. end
  150.  
  151. function canMake(recipe,chest,book)
  152.     local mchs = chestMatome(chest)
  153.  
  154.     local can = 0
  155.     while true do
  156.         if not askMake(recipe,mchs,book,1) then
  157.             break
  158.         end
  159.         can = can + 1
  160.     end
  161.     return can
  162. end
  163.  
  164. function AllDrop(chesto)
  165.     for i = 1, 16 do
  166.         if turtle.getItemCount(i) > 0 then
  167.             turtle.select(i)
  168.             if chesto.side == "top" then
  169.                 turtle.dropUp()
  170.             elseif chesto.side == "bottom" then
  171.                 turtle.dropDown()
  172.             end
  173.         end
  174.     end
  175.     turtle.select(1)
  176. end
  177.  
  178. function shipping(chesto,shiplist)
  179.     local itemlist = chesto.getAllStacks()
  180.  
  181.     turtle.select(1)
  182.     for k, v in pairs(itemlist) do
  183.         if not OnList(v.all().raw_name,shiplist) then
  184.             chesto.pushItem("up",k)
  185.             if peripheral.getType("front") and peripheral.wrap("front").getInventorySize() > 20 then
  186.                 turtle.drop()
  187.             end
  188.             if turtle.getItemCount(1) > 0 and peripheral.getType("top") and peripheral.wrap("top").getInventorySize() > 20 then
  189.                 turtle.dropUp()
  190.             end
  191.             if turtle.getItemCount(1) > 0 then
  192.                 turtle.dropDown()
  193.             end
  194.         end
  195.     end
  196. end
  197.  
  198. function autoCraft(chesto,book)
  199.     local nonshiplist = getList(book)
  200.     while true do
  201.         shipping(chesto,nonshiplist)
  202.         for k, v in pairs(book) do
  203.             if v.code == "recipe" then
  204.                 local ans , ansm  = haveMat(recipeMatome(v), chestMatome(chesto),1)
  205.                 if ans == nil then
  206.                     crafting(v,chesto,book,canMake(v,chesto,book))
  207.                 end
  208.             end
  209.             --sorting(chesto)
  210.             chesto.condenseItems()
  211.         end
  212.         sleep(0.1)
  213.     end
  214. end
  215.  
  216. function castCraft( recipe,chesto,book )
  217.     crafting( recipe,chesto,book,canMake(recipe,chesto,book) )
  218. end
  219.  
  220. function crafting( recipe,chesto,book,many )
  221.     --------------mat check------------------
  222.     AllDrop(chesto)
  223.  
  224.     local lmany = many
  225.     local nokori = 0
  226.     if lmany > math.floor( 64 / recipe.craft_many ) then
  227.         nokori = many - math.floor( 64 / recipe.craft_many )
  228.         lmany = math.floor( 64 / recipe.craft_many )
  229.     end
  230.  
  231.     while true do
  232.         local ans,ansm = haveMat( recipeMatome(recipe), chestMatome(chesto) ,lmany)
  233.         if ans == nil then break end
  234.  
  235.         local find = false
  236.         for k, v in pairs(book) do
  237.             if v.craft == ans then
  238.                 if not crafting(v,chesto,book,math.ceil(ansm/v.craft_many)) then return false end
  239.                 find = true
  240.                 break
  241.             end
  242.         end
  243.         if not find then return false end
  244.     end
  245.  
  246.     local turtleTable = {1,2,3, 5,6,7, 9,10,11}
  247.     local chestSide = {top="down",bottom="up"}
  248.     for k, v in pairs(recipe.mat) do
  249.         loading = lmany
  250.         for l, x in pairs(chesto.getAllStacks()) do
  251.             if v == x.all().raw_name then
  252.                 loading = loading - chesto.pushItem(chestSide[chesto.side],l,loading,turtleTable[k])
  253.                 if loading == 0 then break end
  254.             end
  255.         end
  256.     end
  257.  
  258.     turtle.craft()
  259.    
  260.     AllDrop(chesto)
  261.  
  262.     if nokori > 0 then crafting( recipe,chesto,book,nokori ) end
  263.     return true
  264. end
  265.  
  266. function writeColor(str,color)
  267.     term.setTextColor(color)
  268.     write(str)
  269.     term.setTextColor(colours.white)
  270. end
  271.  
  272. function isChest( side )
  273.     if side == "bottom" then
  274.         if peripheral.wrap(side).getInventorySize() >= 20 then
  275.             return true
  276.         end
  277.     end
  278.     return false
  279. end
  280.  
  281. function isTable(side)
  282.     if peripheral.getType(side) == "crafters_craftingstation" then
  283.         return true
  284.     end
  285.     return false
  286. end
  287.  
  288. function init()
  289.     local periList = peripheral.getNames()
  290.     local craft = nil
  291.     for k, v in pairs(periList) do
  292.         if isChest(v) then
  293.             chest = peripheral.wrap(v)
  294.             chest["side"] = v
  295.         end
  296.     end
  297.     if not chest then
  298.         write("no have under chest")
  299.         return false,nil
  300.     end
  301.     return true,chest
  302. end
  303.  
  304. function checkNewRecipe( table,book )
  305.     local tabledata = table.getAllStacks()
  306.     if tabledata[1] == nil then
  307.         return false,"not recipe"
  308.     end
  309.     local newrecipe = getNewRecipe(table)
  310.     for k, v in pairs(book) do
  311.         if v.craft == newrecipe.craft then
  312.             return false,"i know"
  313.         end
  314.     end
  315.     return true
  316. end
  317.  
  318. function getNewRecipe( table )
  319.     local tabledata = table.getAllStacks()
  320.     local r = {code = "recipe",mat = {},dis_mat={},craft = "",dis_craft=""}
  321.     for k, v in pairs(tabledata) do
  322.         if k == 1 then
  323.             r.craft = v.all().raw_name
  324.             r.dis_craft = v.all().display_name
  325.             r.craft_many = v.all().qty
  326.         else
  327.             r.mat[k-1] = v.all().raw_name
  328.             r.dis_mat[k-1] = v.all().display_name
  329.         end
  330.     end
  331.     return r
  332. end
  333.  
  334. function writeMatLine(recipe,pos,chest,book)
  335.     for k, v in pairs(chest) do
  336.         if recipe.mat[pos] == v.raw_name then
  337.             writeColor(recipe.dis_mat[pos].."\n",colours.white)
  338.             return
  339.         end
  340.     end
  341.  
  342.     for k, v in pairs(book) do
  343.         if recipe.mat[pos] == v.craft then
  344.             writeColor(recipe.dis_mat[pos].."\n",colors.yellow)
  345.             return
  346.         end
  347.     end
  348.     writeColor(recipe.dis_mat[pos].."\n",colors.red)
  349. end
  350.  
  351. function writeRecipe( recipe,chest,book )
  352.     write("craft > ".. recipe.dis_craft.."\n")
  353.     local i = 1
  354.     ----------------------------123------------------------------------
  355.     if recipe.mat[i] then writeMatLine(recipe,i,chest,book) end
  356.     i = i + 1
  357.     if recipe.mat[i] then
  358.         write("   ")
  359.         writeMatLine(recipe,i,chest,book)
  360.     end
  361.     i = i + 1
  362.     if recipe.mat[i] then
  363.         write("      ")
  364.         writeMatLine(recipe,i,chest,book)
  365.     end
  366.     i = i + 1
  367.  
  368.     ----------------------------456------------------------------------
  369.     if recipe.mat[i] then writeMatLine(recipe,i,chest,book) end
  370.     i = i + 1
  371.     if recipe.mat[i] then
  372.         write("   ")
  373.         writeMatLine(recipe,i,chest,book)
  374.     end
  375.     i = i + 1
  376.     if recipe.mat[i] then
  377.         write("      ")
  378.         writeMatLine(recipe,i,chest,book)
  379.     end
  380.     i = i + 1
  381.  
  382.     ----------------------------789------------------------------------
  383.     if recipe.mat[i] then writeMatLine(recipe,i,chest,book) end
  384.     i = i + 1
  385.     if recipe.mat[i] then
  386.         write("   ")
  387.         writeMatLine(recipe,i,chest,book)
  388.     end
  389.     i = i + 1
  390.     if recipe.mat[i] then
  391.         write("      ")
  392.         writeMatLine(recipe,i,chest,book)
  393.     end
  394. end
  395.  
  396. function addRecipeToFile(recipe)
  397.     local f = fs.open("recipes","a")
  398.     local w = ""
  399.  
  400.     for i = 1, 9 do
  401.         if recipe.mat[i] then
  402.             w = w .. recipe.mat[i] .. ","
  403.         else
  404.             w = w .. "nil,"
  405.         end
  406.     end
  407.  
  408.     for i = 1, 9 do
  409.         if recipe.mat[i] then
  410.             w = w .. recipe.dis_mat[i] .. ","
  411.         else
  412.             w = w .. "nil,"
  413.         end
  414.     end
  415.  
  416.     w = w .. recipe.craft .. "," .. recipe.dis_craft .. "," .. recipe.craft_many
  417.     f.writeLine(w)
  418.     f.close()
  419. end
  420.  
  421. function split(str)
  422.     local r = {}
  423.     -- local buff = ""
  424.     -- for i = 1, string.len(str) do
  425.     --  if string.sub(str,i,i+1) == "," then
  426.     --      r[#r+1] = buff
  427.     --      buff = ""
  428.     --  else
  429.     --      buff = buff .. string.sub(str,i,i+1)
  430.     --  end
  431.     -- end
  432.     -- write(#r.."//"..buff)
  433.     for v in string.gmatch(str,"[^,]+") do
  434.         r[#r+1] = v
  435.     end
  436.     write(#r)
  437.     return r
  438. end
  439.  
  440. function loadRecipe()
  441.     local r = {{code="addrecipe"},{code="reset"},{code="autocraft"}}
  442.     local f = fs.open("qrecipes","r")  
  443.     if f == nil then return r end
  444.  
  445.     local line = f.readLine()
  446.     while line do
  447.         local adds = {code = "recipe",mat = {},dis_mat={},craft = "",dis_craft=""}
  448.         local datas = split(line,"[^\,]+")
  449.         local l = 1
  450.         for i = 1, 9 do
  451.             if datas[l] ~= "nil" then
  452.                 adds.mat[i] = datas[l]
  453.             end
  454.             l = l + 1
  455.         end
  456.         for i = 1, 9 do
  457.             if datas[l] ~= "nil" then
  458.                 adds.dis_mat[i] = datas[l]
  459.             end
  460.             l = l + 1
  461.         end
  462.         adds.craft = datas[l]
  463.         l = l + 1
  464.         adds.dis_craft = datas[l]
  465.         l = l + 1
  466.         adds.craft_many = tonumber(datas[l])
  467.         r[#r+1] = adds
  468.  
  469.         line = f.readLine()
  470.     end
  471.  
  472.     f.close()
  473.     return r
  474. end
  475.  
  476. -- {code="recipe",mat={"1","2","3", "4","5","6", "7","8","9"}
  477. --      ,dis_mat={"1","2","3", "4","5","6", "7","8","9"}
  478. --      ,craft="craftans",dis_craft}
  479. rcode = "wkA7XrBs"
  480. local args = {...}
  481. if args[1] ~= nil then
  482.     rcode = args[1]
  483. end
  484. fs.delete("qrecipes")
  485. shell.run("pastebin get " .. rcode .. " qrecipes")
  486. if not fs.exists("qrecipes") then shell.exit() end
  487. local recipes = loadRecipe()
  488. local initans,chest = init()
  489.  
  490. if initans then
  491.     write("\nqsort mode")
  492.     working(autoCraft,chest,recipes)
  493. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement