SilentWarrior

autoCrafting

Jan 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.52 KB | None | 0 0
  1. --### Program Config
  2. local turtleFromAEDirection = "down"
  3.  
  4. local programRestartInterval = 1.00             -- Program run interval
  5.  
  6. --### ONLY MODFIY BELOW IF YOU KNOW WHAT YOU ARE DOING
  7. ---------------------------------------------------------------------------------------------------------------
  8. --### Recipe functions
  9. local woolStock = 100
  10. local woolName = "minecraft:wool"
  11. local cottonName = "Natura:barleyFood:3"
  12.  
  13. local dyeStock = 100
  14. local dyeName = "Botania:dye"
  15. local petalName = "Botania:petal"
  16. local mortarName = "Botania:pestleAndMortar"
  17.  
  18. local recipe = {
  19.   {name = "minecraft:chest", stock = 10, ingredient = {"minecraft:planks","minecraft:planks","minecraft:planks","minecraft:planks",nil,"minecraft:planks","minecraft:planks","minecraft:planks","minecraft:planks"}}
  20. }
  21.  
  22. function addNaturaWoolRecipe()
  23.   local whiteWool = {name = woolName, stock = woolStock, ingredient = {cottonName,cottonName,cottonName,cottonName,cottonName,cottonName,cottonName,cottonName,cottonName}}
  24.   table.insert(recipe, whiteWool)
  25. end
  26.  
  27. function generateWoolRecipes()
  28.   for c=1,16 do
  29.     local r = {}
  30.     r.name = woolName..":"..c
  31.     r.stock = woolStock
  32.     r.ingredient = {dyeName..":"..c, woolName}
  33.     table.insert(recipe, r)
  34.   end
  35. end
  36.  
  37. function generateDyeRecipes()
  38.   table.insert(recipe, {name = dyeName, stock = dyeStock, ingredient = {petalName,mortarName}})
  39.   for c=1,16 do
  40.     local r = {}
  41.     r.name = dyeName..":"..c
  42.     r.stock = dyeStock
  43.     r.ingredient = {petalName..":"..c, mortarName}
  44.     table.insert(recipe, r)
  45.   end
  46. end
  47.  
  48. generateDyeRecipes()
  49. addNaturaWoolRecipe()
  50. generateWoolRecipes()
  51.  
  52. function getRecipe()
  53.   for _,r in pairs(recipe) do
  54.     if r.stock > getAmountInAE(r.name) then
  55.       return r
  56.     end
  57.   end
  58.   return nil
  59. end
  60. --### Recipe functions
  61. ---------------------------------------------------------------------------------------------------------------
  62. --### Crafting functions
  63. local c  = peripheral.find("workbench")
  64.  
  65. if not c then
  66.   error("\n\nNo Crafting Table equiped")
  67.   return
  68. end
  69.  
  70. --### Crafting functions
  71. ---------------------------------------------------------------------------------------------------------------
  72. --### Helper Functions
  73. local gridSlotToTurtleSlot = {
  74.   [1] = 1,
  75.   [2] = 2,
  76.   [3] = 3,
  77.   [4] = 5,
  78.   [5] = 6,
  79.   [6] = 7,
  80.   [7] = 9,
  81.   [8] = 10,
  82.   [9] = 11,
  83. }
  84.  
  85. function debug(text)
  86.   local debug = true
  87.   if text ~= nil and debug then
  88.     term.setTextColor(colors.blue)
  89.     print(">>> "..text)
  90.     term.setTextColor(colors.white)
  91.   end
  92. end
  93.  
  94. function status(text)
  95.   if text ~= nil then
  96.     term.setTextColor(colors.yellow)
  97.     print("  "..text)
  98.     term.setTextColor(colors.white)
  99.   end
  100. end
  101.  
  102. function getItemId(stack)
  103.   if stack then
  104.     if stack.dmg ~= nil and stack.dmg ~= 0 then
  105.       return stack.id .. ":" .. stack.dmg
  106.     else
  107.       return stack.id
  108.     end
  109.   else
  110.     return nil
  111.   end
  112. end
  113.  
  114. function getFingerprint(itemString)
  115.   itemString = itemString..":"
  116.   local tbl = {}
  117.   for v in itemString:gmatch("(.-):") do
  118.     table.insert(tbl,v)
  119.   end
  120.  
  121.   local id = tbl[1]..":"..tbl[2]
  122.   local dmg = tbl[3] or 0
  123.   return {["id"] = id, ["dmg"] = tostring(dmg)}
  124. end
  125.  
  126. function resetTerminal()
  127.   term.clear()
  128.   term.setCursorPos(1,1)
  129. end
  130.  
  131. function printTable(tbl)
  132.   for i,v in pairs(tbl) do
  133.     write(i)
  134.     write(":")
  135.     write(type(v))
  136.     write(":")
  137.     print(v)
  138.   end
  139. end
  140.  
  141. --### Helper Functions
  142. ---------------------------------------------------------------------------------------------------------------
  143. --### AE Functions
  144. local ae = peripheral.find("tileinterface")
  145.  
  146. if not ae then
  147.   error("\n\nNo AE Interface found.\nAttack to any side.")
  148.   return
  149. end
  150.  
  151. function checkAE()
  152.   if not ae.canExport(turtleFromAEDirection) then
  153.     error("\nWrong AE Setup.\n\nInterface can not export to: "..turtleFromAEDirection)
  154.   end
  155. end
  156.  
  157. function getAmountInAE(itemString)
  158.   local fp = getFingerprint(itemString)
  159.   printTable(fp)
  160.   local info = ae.getItemDetail(fp)
  161.   return info.basic()["qty"] or 0
  162. end
  163.  
  164. function clearTurtle()
  165.   for i=1,16 do
  166.     ae.pullItem(turtleFromAEDirection,i)
  167.   end
  168. end
  169.  
  170. --### AE Functions
  171. ---------------------------------------------------------------------------------------------------------------
  172. --### Program Functions
  173.  
  174. function craft(r)
  175.   for cslot,i in pairs(r.ingredient) do
  176.     local fingerprint = getFingerprint(i)
  177.     local toSlot = gridSlotToTurtleSlot[cslot]
  178.     local success = ae.exportItem(fingerprint, turtleFromAEDirection, 1, toSlot)
  179.     --print("exported "..success.fingerprint.id.. " to slot "..toSlot)
  180.     if not success then
  181.       status("Not enough resources: "..i)
  182.       return
  183.     end
  184.   end
  185.   local cSuccess, msg = c.craft()
  186.   if cSuccess then
  187.     print("Crafting done")
  188.   else
  189.     print(msg)
  190.   end
  191. end
  192.  
  193. --### Program Functions
  194. ---------------------------------------------------------------------------------------------------------------
  195. --### Default Main Program Functions
  196. function preRunProgram()
  197.  
  198. end
  199.  
  200. function runProgram()
  201.   checkAE()
  202.  
  203.   local r = getRecipe()
  204.   if r ~= nil then
  205.     status("Crafting: "..r.name)
  206.     status("("..getAmountInAE(r.name).."/"..r.stock..")")
  207.     craft(r)
  208.     clearTurtle()
  209.   else
  210.     status("No further recipe")
  211.   end
  212.   --resetTerminal()
  213. end
  214.  
  215.  
  216. function postRunProgram()
  217.  
  218. end
  219.  
  220. local STOP = false
  221. local PROGRAM_RUN_TIMER = nil
  222. function handleEvent(event)
  223.   if event[1] == "timer" and event[2] == PROGRAM_RUN_TIMER then
  224.     preRunProgram()
  225.     runProgram()
  226.     postRunProgram()
  227.     PROGRAM_RUN_TIMER = os.startTimer(programRestartInterval)
  228.     return
  229.   end
  230.   if event[1] == "char" and event[2] == "t" then
  231.     STOP = true
  232.     print("Termination Key detected.\n!!! Terminating the program !!!")
  233.     return
  234.   end
  235.  
  236.   local ignoreList = {"char", "key", "key_up", "mouse_click", "mouse_up", "mouse_drag", "mouse_scroll", "rednet", "turtle_inventory"}
  237.   for i,v in pairs(ignoreList) do
  238.     if event[1] == v then
  239.       return
  240.     end
  241.   end
  242.  
  243.   print("unhandeled event:")
  244.   for i,v in pairs(event) do
  245.     write(i)
  246.     write(":")
  247.     print(v)
  248.   end
  249. end
  250.  
  251. function preLoop()
  252.   resetTerminal()
  253.   print(shell.getRunningProgram().." started")
  254.   clearTurtle()
  255.   PROGRAM_RUN_TIMER = os.startTimer(0)
  256. end
  257.  
  258. function loop()
  259.   while not STOP do
  260.     handleEvent({os.pullEvent()})
  261.   end
  262. end
  263.  
  264. function postLoop()
  265.  
  266. end
  267. --### Default Main Program Functions
  268. ---------------------------------------------------------------------------------------------------------------
  269. preLoop()
  270. loop()
  271. postLoop()
Advertisement
Add Comment
Please, Sign In to add comment