Advertisement
fatboychummy

blockCrafter.lua

Mar 4th, 2020
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. local EMC_PER_ITEM_IN = 28
  2. local EMC_PER_ITEM_OUT = 256
  3.  
  4. -- Suck, craft, then drop
  5.  
  6. local slots = {1, 2, 3, 5, 6, 7, 9, 10, 11}
  7. local cycle = 0
  8. local mx, my = term.getSize()
  9. local paused = false
  10.  
  11. local function writeOnLine(y, thing)
  12.   if y > my then
  13.     term.scroll(1)
  14.     y = my
  15.   end
  16.   local rep = string.rep(' ', mx)
  17.   term.setCursorPos(1, y)
  18.   io.write(rep)
  19.   term.setCursorPos(1, y)
  20.   io.write(thing)
  21. end
  22.  
  23. local function grabItems(count)
  24.   local _, cy = term.getCursorPos()
  25.   local grabbed = 0
  26.   local slot = turtle.getSelectedSlot()
  27.   while grabbed < count do
  28.     writeOnLine(cy, string.format("Grabbing items: %d / %d", grabbed, count))
  29.     turtle.suck(count - grabbed)
  30.     grabbed = turtle.getItemCount(slot)
  31.   end
  32.   print()
  33.   print("Done.")
  34. end
  35.  
  36. local function eachSlot(func)
  37.   for i = 1, 16 do
  38.     if turtle.getItemCount(i) > 0 then
  39.       turtle.select(i)
  40.       func()
  41.     end
  42.   end
  43. end
  44.  
  45. local function main()
  46.  
  47.   local emcGenerated = 0
  48.   local itemsGenerated = 0
  49.   while true do
  50.     print(string.format("Starting cycle %d.", cycle))
  51.     -- Get 64 items in each slot
  52.     for i = 1, 9 do
  53.       print(string.format("Grabbing slot %d (%d).", i, slots[i]))
  54.       turtle.select(slots[i])
  55.       grabItems(64)
  56.     end
  57.  
  58.     -- craft 64 items
  59.     print("Crafting.")
  60.     turtle.craft(64)
  61.  
  62.     -- drop everything
  63.     print("Dropping back in")
  64.     eachSlot(turtle.drop)
  65.     print()
  66.     print("Done.")
  67.     local genned = (64 * EMC_PER_ITEM_OUT) - (64 * EMC_PER_ITEM_IN * 9)
  68.     local items = math.floor(genned / EMC_PER_ITEM_IN + 0.5)
  69.     print(string.format("Generated %d EMC.", genned))
  70.     emcGenerated = emcGenerated + genned
  71.     print(string.format("Current total EMC generated: %d", emcGenerated))
  72.     print(string.format("Current total base items generated: %d", emcGenerated / EMC_PER_ITEM_IN))
  73.  
  74.     -- grab the amount of items we collected, stick them in the collector above us.
  75.     print(string.format("Grabbing %d items and placing them into the extras chest...", items))
  76.     grabItems(items)
  77.  
  78.     print("Dropping into inv below.")
  79.     eachSlot(turtle.dropDown)
  80.  
  81.     -- if paused, wait until unpaused.
  82.     if paused then
  83.       print("Paused. Waiting for unpause signal (spacebar).")
  84.       os.pullEvent("unpause")
  85.       print("Unpaused. Resuming.")
  86.     end
  87.  
  88.     print("Waiting 1 second.")
  89.  
  90.   end
  91. end
  92.  
  93. local function pauser()
  94.   while true do
  95.     local ev = {os.pullEvent("key")}
  96.     if ev[2] == keys.space then
  97.       if paused then
  98.         os.queueEvent("unpause")
  99.         print()
  100.         print("===========================")
  101.         print("Turtle set to unpause.")
  102.         print("===========================")
  103.         print()
  104.       else
  105.         print()
  106.         print("===========================")
  107.         print("At the end of this cycle, turtle will pause.")
  108.         print("===========================")
  109.         print()
  110.       end
  111.       paused = not paused
  112.     end
  113.   end
  114. end
  115.  
  116. parallel.waitForAny(main, pauser)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement