natie3

crafting

Sep 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. os.loadAPI("recipes")
  2. os.loadAPI("moveAPI.lua")
  3. resources = {}
  4. position = 1
  5. regex = "([ri])(%d+)x?(%d*)"
  6.  
  7. function moveTo(spot)
  8.   local currentLevel = math.floor(position / 10)
  9.   local currentNumber = position % 10
  10.   local newLevel = math.floor(spot / 10)
  11.   local newNumber = spot % 10
  12.   moveAPI.moveRelative(newNumber - currentNumber, newLevel - currentLevel)
  13.   position = spot
  14. end
  15.  
  16. function findFreeSlot()
  17.   local freeSlot = -1
  18.   local check = 16
  19.   while freeSlot == -1 do
  20.     turtle.select(check)
  21.     if turtle.getItemCount() == 0 then
  22.       freeSlot = check
  23.     end
  24.     check = check - 1
  25.   end
  26.  
  27.   return freeSlot
  28. end
  29.  
  30. function gatherResources(recipe, times)
  31.   local list = listResources(recipe, 1)
  32.   for i = 1, #list do
  33.     if(resources["i" .. list[i].item] == nil) then
  34.        resources["i" .. list[i].item] = { item = list[i].item, amount = 0 }
  35.     end
  36.     resources["i" .. list[i].item].amount = resources["i" .. list[i].item].amount + list[i].amount
  37.   end
  38.   for key, value in pairs(resources) do
  39.     value.slot = findFreeSlot()
  40.     moveTo(value.item)
  41.     turtle.turnRight()
  42.     turtle.select(value.slot)
  43.     turtle.suck(value.amount * times)
  44.     turtle.turnLeft()
  45.     print(key .. ": " .. value.amount)
  46.   end
  47. end
  48.  
  49. function listResources(recipe, times)
  50.   local result = {}
  51.   for cur in recipes.list[recipe].recipe:gmatch("%S+") do
  52.     local _, _, type, pos, amount = string.find(cur, regex)
  53.     if amount == "" then
  54.       amount = "1"
  55.     end
  56.     if type == "i" then
  57.       result[#result + 1] = { item = pos, amount = amount * times }
  58.     else
  59.       local list = listResources(type .. pos, tonumber(amount) * times)
  60.       for i = 1, #list do
  61.         result[#result + 1] = { item = list[i].item, amount = list[i].amount }
  62.       end
  63.     end
  64.   end
  65.  
  66.   return result
  67. end
  68.  
  69. function craft(recipe, times)
  70.   for subRecipe in recipes.list[recipe].recipe:gmatch("r%d+x?%d*") do
  71.     craft(subRecipe, times)
  72.   end
  73.   moveTo(tonumber(recipe:match("%d+")))
  74.   turtle.turnLeft()
  75.   for req in recipes.list[recipe].recipe:gmatch("%S+") do
  76.     local _, _, type, pos, amount = string.find(req , regex)
  77.     turtle.select(resources[type .. pos].slot)
  78.     if type == "i" then
  79.       turtle.drop(tonumber(amount) * times)
  80.     else
  81.       turtle.drop()
  82.     end
  83.   end
  84.   resources[recipe] = { slot = findFreeSlot() }
  85.   turtle.select(1)
  86.   os.sleep(times + 2)
  87.   while turtle.getItemCount() < times do
  88.     os.sleep(0.5)
  89.   end
  90.   turtle.transferTo(resources[recipe].slot)
  91.   turtle.turnRight()
  92. end
  93.  
  94. function mainCraft(recipe, times)
  95.   gatherResources(recipe, times)
  96.   craft(recipe, times)
  97.   moveTo(1)
  98. end
  99.  
  100. function ask()
  101.   local recipeList = recipes.createTree()
  102.   local options = "["
  103.   for k, v in pairs(recipeList) do
  104.     options = options .. k .. ","
  105.   end
  106.   print("What do you want to make?")
  107.   print(options:sub(1, #options - 1) .. "]")
  108.   local chosenGroup = read()
  109.   local options = "["
  110.   for k, v in pairs(recipeList[chosenGroup]) do
  111.     options = options .. k .. ","
  112.   end
  113.   print("\nWhat do you want to make?")
  114.   print(options:sub(1, #options - 1) .. "]")
  115.   local chosenItem = read()
  116.   print("\nHow many times?")
  117.   local amount = tonumber(read())
  118.  
  119.   moveAPI.move("u b b dl d d d d")
  120.   mainCraft(recipeList[chosenGroup][chosenItem].iden, amount)
  121.   moveAPI.move("u u u u dr f f d")
  122. end
  123.  
  124. ask()
Add Comment
Please, Sign In to add comment