fatboychummy

craftingAPI

Sep 28th, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.00 KB | None | 0 0
  1. --What this will do:
  2. --Craft utilities
  3. --Easier crafting of items
  4. --easier item placement for crafting
  5.  
  6. local recipesFile = "craftRecipes"
  7. local mv = 16 --Recommended not to change this
  8. local recipes = false
  9. local ourTurtle = false
  10.  
  11. function turtleName(name)
  12.   ourTurtle = name
  13. end
  14.  
  15. local function pEr(func,i,exp,got,i2)
  16.   if i2 then
  17.     return func..":"..tostring(i).." (Table Index "..tostring(i2).."): Expected "..exp..", got "..type(got)
  18.   else
  19.     return func..":"..tostring(i)..": Expected "..exp..", got "..type(got)
  20.   end
  21. end
  22.  
  23. function rI(i)
  24.   if i <= 3 then return i
  25.   elseif i > 3 and i <= 6 then return i+1
  26.   else return i + 2
  27.   end
  28.   return false
  29. end
  30.  
  31. local function dumpContents()
  32.   for i = 1,16 do
  33.  
  34.   end
  35. end
  36.  
  37. function swap(a,b)
  38.   turtle.select(a)
  39.   turtle.transferTo(mv)
  40.   turtle.select(b)
  41.   turtle.transferTo(a)
  42.   turtle.select(mv)
  43.   turtle.transferTo(b)
  44. end
  45.  
  46.  
  47. function moveItemsForRecipe(recipe)
  48.   assert(type(recipe) == "string",pEr("moveItemsForRecipe",1,"string",recipe))
  49.   local cRecipe = false
  50.   for k,v in pairs(recipes) do
  51.     if k == recipe then
  52.       cRecipe = v
  53.     end
  54.   end
  55.   if not cRecipe then
  56.     return false,"Failed to find recipe!"
  57.   end
  58.   moveToWall()
  59.   for i = 1,9 do
  60.  
  61.   end
  62. end
  63.  
  64. function grabItemsForRecipe(recipe,chests)
  65.   assert(type(recipe) == "string",pEr("grabItemsForRecipe",1,"string",recipe))
  66.   assert(type(chests) == "table",pEr("grabItemsForRecipe",2,"table",chests))
  67.   assert(type(ourTurtle) == "string",pEr("ourTurtle",0,"string",ourTurtle).."| Make sure to use the function \"<APINAME>.turtleName()\"")
  68.   assert(type(recipes) == "table",pEr("grabItemsForRecipe,",0,"table",recipes))
  69.   local cRecipe = false
  70.   for k,v in pairs(recipes) do
  71.     if k == recipe then
  72.       cRecipe = v
  73.     end
  74.   end
  75.   if not cRecipe then
  76.     return false,"Failed to find recipe!"
  77.   end
  78.   if #chests == 0 then
  79.     return false,"No chests to choose from!"
  80.   end
  81.   local totalGrabbed = {}
  82.   for i = 1,#chests do
  83.     assert(type(chests[i]) == "string",pEr("grabItemsForRecipe",2,"string",chests[i],i))
  84.     local cChest = peripheral.wrap(chests[i])
  85.     if cChest then
  86.       local ls = cChest.list()
  87.       for i = 1,cChest.size() do
  88.         if ls[i] then
  89.           for k,v in pairs(cRecipe.totals) do
  90.             for k2,v2 in pairs(v) do
  91.               if ls[i].name == k and ls[i].damage == k2 then
  92.                 if not totalGrabbed[k] then totalGrabbed[k] = {k2 = 0} end
  93.                 if not totalGrabbed[k][k2] then totalGrabbed[k][k2] = 0 end
  94.                 if totalGrabbed[k][k2] < v2 then
  95.                   totalGrabbed[k][k2] = cChest.pushItems(ourTurtle,i,v2-totalGrabbed[k][k2])
  96.                 end
  97.               end
  98.             end
  99.           end
  100.         end
  101.       end
  102.     end
  103.   end
  104.   --
  105.   for k,v in pairs(cRecipe.totals) do
  106.     for k2,v2 in pairs(v) do
  107.       local switch = false
  108.       for i = 1,16 do
  109.         local dat = turtle.getItemDetail(i)
  110.         if dat then
  111.           if dat.name == k and dat.damage == k2 and dat.count >= v2 then
  112.             switch = true
  113.           end
  114.         end
  115.       end
  116.       if not switch then dumpContents() return false,k.." not found (damage "..k2..")" end
  117.     end
  118.   end
  119.   return true
  120. end
  121.  
  122.  
  123.  
  124. function readRecipesRaw()
  125.   local hndl = fs.open(recipesFile,"r")
  126.   if hndl then
  127.     local all = hndl.readAll()
  128.     hndl.close()
  129.     return all
  130.   end
  131.   return {}
  132. end
  133.  
  134. function readRecipes()
  135.   local hndl = fs.open(recipesFile,"r")
  136.   if hndl then
  137.     local all = textutils.unserialize(hndl.readAll())
  138.     hndl.close()
  139.     return all or {}
  140.   end
  141.   return {}
  142. end
  143.  
  144. function loadRecipes()
  145.   recipes = readRecipes()
  146. end
  147.  
  148. function saveRecipe(name)
  149.   if type(name) ~= "string" then
  150.     printError(pEr("saveRecipe",1,"string",name))
  151.     return false
  152.   end
  153.   recipes = readRecipes()
  154.   if recipes[name] then
  155.     print("Recipe exists, overwrite?")
  156.     local ans = string.lower(io.read())
  157.     if ans ~= "y" then
  158.       print("Cancelling")
  159.       return
  160.     end
  161.   end
  162.   if turtle.craft(0) then
  163.     print("Recipe is savable.")
  164.   else
  165.     error("Recipe is not a valid recipe.")
  166.   end
  167.   local data = {locs = {},totals = {}}
  168.  
  169.   for i = 1,9 do
  170.     local dat = turtle.getItemDetail(rI(i))
  171.     if dat then
  172.       print(tostring(i)..":",dat.name,dat.count,dat.damage)
  173.       data.locs[rI(i)] = {}
  174.       data.locs[rI(i)].name = dat.name
  175.       data.locs[rI(i)].damage = dat.damage
  176.       if not data.totals[dat.name] then
  177.         data.totals[dat.name] = {}
  178.         data.totals[dat.name][dat.damage] = 0
  179.       end
  180.       data.totals[dat.name][dat.damage] = data.totals[dat.name][dat.damage] + 1
  181.     end
  182.   end
  183.   recipes[name] = data
  184.   local handle = fs.open(recipesFile,"w")
  185.   handle.write(textutils.serialise(recipes))
  186.   handle.close()
  187. end
  188.  
  189.  
  190. function help(func)
  191.   local function printUsage(str_func,tab_args,tab_info)
  192.     term.clear()
  193.     term.setCursorPos(1,1)
  194.     local old = term.getTextColor()
  195.     --print(str_func)
  196.     term.setTextColor(colors.yellow)
  197.     print("Usage:")
  198.     term.setTextColor(old)
  199.     local args = ""
  200.     for i = 1,#tab_args do
  201.       if i ~= 1 then
  202.         args = args..",\n"
  203.       end
  204.       if tab_args[i][1] then
  205.         args = args.."<"..tab_args[i][2]..">"
  206.       else
  207.         args = args.."["..tab_args[i][2].."]"
  208.       end
  209.     end
  210.     print(str_func.."("..args..")")
  211.     print("-----")
  212.     for i = 1,#tab_info do
  213.       print(tab_info[i])
  214.     end
  215.   end
  216.  
  217.  
  218.   func = func or "help"
  219.   func = string.lower(func)
  220.   if func == "help" then
  221.     printUsage(func,
  222.     {
  223.       {
  224.         [1]=false,
  225.         [2]="string functionName"
  226.       }
  227.     },
  228.     {
  229.       "Prints information on how to use the function specified"
  230.     })
  231.   elseif func == "saverecipe" then
  232.     printUsage("saveRecipe",
  233.     {
  234.       {
  235.         [1]=true,
  236.         [2]="string saveName"
  237.       }
  238.     },
  239.     {
  240.       "Saves a recipe to storage for use later.",
  241.       "Requires items to be in the turtle, in the orientation they would be used for crafting the item."
  242.     })
  243.   elseif func == "loadrecipes" then
  244.     printUsage("loadRecipes",
  245.     {},
  246.     {
  247.       "Loads all recipes from the save file into RAM",
  248.       "This is required in order to use functions like \'getItemsForRecipe\'"
  249.     })
  250.   elseif func == "readrecipes" then
  251.     printUsage("readRecipes",
  252.     {
  253.       {
  254.       }
  255.     },
  256.     {
  257.       "Reads the recipes in the savefile, then returns them as a table."
  258.     })
  259.   elseif func == "readrecipesraw" then
  260.     printUsage("readRecipesRaw",
  261.     {
  262.     },
  263.     {
  264.       "Reads the recipes in the savefile, then returns them as a string."
  265.     })
  266.   elseif func == "grabitemsforrecipe" then
  267.     printUsage("grabItemsForRecipe",
  268.     {
  269.       {
  270.         [1]=true,
  271.         [2]="string recipeName"
  272.       },
  273.       {
  274.         [1]=true,
  275.         [2]="table [strings] listOfChestNamesConnectedToStorageNetwork"
  276.       }
  277.     },
  278.     {
  279.       "Grabs the required items for recipeName from the chests listed"
  280.     })
  281.   elseif func == "moveitemsforrecipe" then
  282.     printUsage("moveItemsForRecipe",
  283.     {
  284.       {
  285.         [1]=true,
  286.         [2]="string recipeName"
  287.       }
  288.     },
  289.     {
  290.       "Moves items in the turtle to the recipe locations.",
  291.       "NOTE: THIS WILL BE DEPRECATED"
  292.     })
  293.   elseif func == "swap" then
  294.     printUsage(func,
  295.     {
  296.       {
  297.         [1]=true,
  298.         [2]="number index1"
  299.       },
  300.       {
  301.         [1]=true,
  302.         [2]="number index2",
  303.       }
  304.     },
  305.     {
  306.       "Swaps two items in the turtle, using the 16th slot as an extra slot",
  307.       "Useful for sorting."
  308.     })
  309.   elseif func == "ri" then
  310.     printUsage("rI",
  311.     {
  312.       {
  313.         [1]=true,
  314.         [2]="number index"
  315.       }
  316.     },
  317.     {
  318.       "Converts a crafting grid index (from 1-9) to the turtle's native index, useful for moving items into place."
  319.     })
  320.   elseif func == "turtlename" then
  321.     printUsage("turtleName",
  322.     {
  323.     },
  324.     {
  325.       "Sets the turtle's name, for pushing items to the turtle from chests.",
  326.       "This is required in order to recieve items."
  327.     })
  328.   end
  329. end
Add Comment
Please, Sign In to add comment