SHARE
TWEET

ComputerCraft Tutorial: obsidian_stone_generator_0_1

hevohevo Mar 5th, 2014 858 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ################################
  2. -- Obsidian/Stone Generator
  3. -- version 0.1b
  4. -- http://hevohevo.hatenablog.com/
  5. -- If you want to get not Cobble-stones but Stones,
  6. --  require a SilkTouch Mining Turtle by "More Turtles" mod.
  7. -- See the CC forum.
  8. --  http://www.computercraft.info/forums2/index.php?/topic/16465-mc164152-more-turtles-v112/
  9.  
  10. -- ChangeLog:
  11. -- 0.1b. bugfix: calculate second/piece
  12. -- 0.1a. bugfix: generate stone with silktouch mining turtle
  13.  
  14. -- How to use:
  15. --  0. Download and install.
  16. --      > pastebin get ZknGLgxE obsigen
  17. --  1. Build a facility: x_size=4, z_size=4, y_size=3
  18. --      > obsigen build
  19. --  2a. Generate Obsidians (Insert redstones into a right-side chest before executing)
  20. --      > obsigen obsidian
  21. --  2b. Generate Cobble-stones
  22. --      > obsigen cobble
  23. --  2c. Generate Stones (requires a SilkTouch mining turtle and fuel)
  24. --      > obsigen stone
  25.  
  26. -- config
  27. LAVA_SLOT = 1
  28. GET_BLOCK_SLOT = 2
  29. REDSTONE_SLOT = 3
  30. BLOCK_SLOT = 4
  31. GLASS_SLOT = 13
  32. WATER_SLOT = 14
  33. CHEST_SLOT = 15
  34. FUEL_SLOT = 16
  35. MIN_FUEL_LEVEL = 60
  36. DEFAULT_GENERATE_QTY = 64
  37.  
  38. -- functions
  39. function waitForEnoughItems(itemName, n, slot)
  40.   turtle.select(slot)
  41.   while turtle.getItemCount(slot) < n do
  42.     if itemName then print(string.format("Insert %s into slot %d",itemName,slot)) end
  43.     os.sleep(0.5)
  44.     os.pullEvent("turtle_inventory")
  45.   end
  46. end
  47.  
  48. function waitForEnoughFuel(minLevel, slot)
  49.   local qty = 0
  50.   local refuel = function()
  51.     turtle.select(slot)
  52.     turtle.refuel()
  53.     qty = turtle.getFuelLevel()
  54.     print("fuel: ",qty)
  55.     return qty
  56.   end
  57.  
  58.   while refuel() < minLevel do
  59.     print(string.format("Insert fuel-items into slot %d: %d/%d", slot, qty, minLevel))
  60.     os.sleep(1)
  61.     os.pullEvent("turtle_inventory")
  62.   end
  63. end
  64.  
  65. function assertByErrorMsg(func, error_msg)
  66.   local status, msg = func()
  67.   if not status and msg == error_msg then
  68.     assert(status, msg)
  69.   end
  70. end
  71.  
  72. function getBlock(_digFunc)
  73.   local digFunc = _digFunc or turtle.dig
  74.  
  75.   turtle.select(GET_BLOCK_SLOT)
  76.   digFunc()
  77.   assertByErrorMsg(turtle.dropDown, "Inventory full")
  78. end
  79.  
  80. function placeItem(slot, _place_func)
  81.   local place_func = _place_func or turtle.place
  82.   turtle.select(slot)
  83.   place_func()
  84. end
  85.  
  86. function placeLava(sec)
  87.   turtle.select(LAVA_SLOT)
  88.   turtle.placeUp()
  89.   if sec and type(sec)=="number" then
  90.     os.sleep(sec)
  91.     turtle.placeUp()
  92.   end
  93. end
  94.  
  95. function switchWaterDirection(key)
  96.   if key == "obsidian" then
  97.     turtle.turnLeft()
  98.     turtle.select(BLOCK_SLOT)
  99.     turtle.dig()
  100.     turtle.turnRight()
  101.     getBlock()
  102.   else
  103.     turtle.turnLeft()
  104.     placeItem(BLOCK_SLOT)
  105.     turtle.turnRight()
  106.     getBlock()
  107.   end
  108. end
  109. function isInclude(tbl, value)
  110.   local status = false
  111.   for k,v in pairs(tbl) do
  112.     if v == value then
  113.       status = true
  114.       break
  115.     end
  116.   end
  117.   return status
  118. end
  119.  
  120. local sTime = 0
  121. function generateStone(maxCount, digFunc)
  122.   local count = 0
  123.   placeLava()
  124.   sTime = os.clock()
  125.   while count < maxCount do
  126.     local unDetect = 0
  127.     if turtle.detect() then
  128.       getBlock(digFunc)
  129.       count = count + 1
  130.       print(count,'/',maxCount,': ',(os.clock() - sTime)/count,' sec/piece')
  131.     else
  132.       unDetect = unDetect + 1
  133.       os.sleep(0.1)
  134.     end
  135.     if unDetect > 20 then
  136.       getBlock()
  137.       unDetect = 0
  138.     end
  139.   end
  140.   placeLava()
  141. end
  142.  
  143. function generateObsidian()
  144.   local count = 0
  145.   local function suckRedstone()
  146.     turtle.turnRight()
  147.     turtle.select(REDSTONE_SLOT)
  148.     local status = turtle.suck()
  149.     turtle.turnLeft()
  150.     return status
  151.   end
  152.   sTime = os.clock()
  153.   while turtle.getItemCount(REDSTONE_SLOT) > 0 or suckRedstone() do
  154.     while turtle.getItemCount(REDSTONE_SLOT) > 0 do
  155.       getBlock()
  156.       placeItem(REDSTONE_SLOT)
  157.       placeLava(2.5)
  158.       turtle.select(BLOCK_SLOT)
  159.       turtle.placeUp()
  160.       turtle.digUp()
  161.  
  162.       getBlock()
  163.       count = count + 1
  164.       print(count,': ',(os.clock() - sTime)/count,' sec/piece')
  165.     end
  166.   end
  167.   return count
  168. end
  169.  
  170. -- functions for building
  171. function fwdAndPlaceDown(slot, _count)
  172.   local count = _count or 1
  173.   for i=1, count do
  174.     turtle.forward()
  175.     turtle.select(slot)
  176.     turtle.placeDown()
  177.   end
  178. end
  179. function loop(n, func)
  180.   for i=1,n do func() end
  181. end
  182. function goRight()
  183.   turtle.turnRight()
  184.   turtle.forward()
  185. end
  186. function goLeft()
  187.   turtle.turnLeft()
  188.   turtle.forward()
  189. end
  190.  
  191. function build1()
  192.   turtle.up()
  193.   fwdAndPlaceDown(BLOCK_SLOT, 4)
  194.   turtle.turnRight()
  195.   fwdAndPlaceDown(BLOCK_SLOT, 3)
  196.   turtle.turnRight()
  197.   fwdAndPlaceDown(BLOCK_SLOT, 1)
  198.   turtle.turnRight()
  199.   fwdAndPlaceDown(BLOCK_SLOT, 2)
  200.   goLeft()
  201.   turtle.turnLeft()
  202.   fwdAndPlaceDown(CHEST_SLOT, 2)
  203.   loop(2, goRight)
  204.   fwdAndPlaceDown(BLOCK_SLOT, 1)
  205. end
  206. function build2()
  207.   turtle.up()
  208.   placeItem(BLOCK_SLOT, turtle.placeDown)
  209.   fwdAndPlaceDown(BLOCK_SLOT, 1)
  210.   turtle.turnRight()
  211.   fwdAndPlaceDown(BLOCK_SLOT, 3)
  212.   turtle.turnRight()
  213.   fwdAndPlaceDown(BLOCK_SLOT, 3)
  214.   turtle.turnRight()
  215.   fwdAndPlaceDown(BLOCK_SLOT, 1)
  216.   fwdAndPlaceDown(CHEST_SLOT, 1)
  217.   turtle.back()
  218.   goRight()
  219.   fwdAndPlaceDown(WATER_SLOT, 1)
  220.   goLeft()
  221. end
  222. function build3()
  223.   turtle.up()
  224.   fwdAndPlaceDown(BLOCK_SLOT, 1)
  225.   turtle.turnRight()
  226.   fwdAndPlaceDown(BLOCK_SLOT, 1)
  227.   turtle.turnRight()
  228.   fwdAndPlaceDown(BLOCK_SLOT, 3)
  229.   turtle.turnRight()
  230.   fwdAndPlaceDown(BLOCK_SLOT, 3)
  231.   turtle.turnRight()
  232.   fwdAndPlaceDown(BLOCK_SLOT, 1)
  233.   fwdAndPlaceDown(GLASS_SLOT, 1)
  234.   fwdAndPlaceDown(BLOCK_SLOT, 1)
  235.   turtle.turnRight()
  236.   fwdAndPlaceDown(BLOCK_SLOT, 2)
  237.   turtle.turnRight()
  238.   fwdAndPlaceDown(GLASS_SLOT, 2)
  239.   loop(2, goRight)
  240.   loop(2, turtle.down)
  241.   loop(2, turtle.turnRight)
  242. end
  243.  
  244. -- main
  245. local args = {...}
  246. local targets = "cobble"
  247. if args and isInclude({"build", "cobble", "obsidian", "stone"}, args[1]) then
  248.   target = args[1]
  249. else
  250.   print("program-name arg1 arg2")
  251.   print(" arg1: build/cobble/obsidian/stone")
  252.   print(" arg2: quantity (optional)")
  253.   assert(false, "Invalid argument(s)")
  254. end
  255.  
  256. if target == "build" then
  257.   print('Start: ',target)
  258.   waitForEnoughFuel(MIN_FUEL_LEVEL, FUEL_SLOT)
  259.   waitForEnoughItems("40 Blocks", 40, BLOCK_SLOT)
  260.   waitForEnoughItems("3 Glasses", 3, GLASS_SLOT)
  261.   waitForEnoughItems("a Water-Bucket", 1, WATER_SLOT)
  262.   waitForEnoughItems("3 Chests", 3, CHEST_SLOT)
  263.   build1()
  264.   build2()
  265.   build3()
  266. else
  267.   local maxCount = tonumber(args[2]) or DEFAULT_GENERATE_QTY
  268.   waitForEnoughItems("a Lava-Bucket", 1, LAVA_SLOT)
  269.   waitForEnoughItems("a BLOCK", 1, BLOCK_SLOT)
  270.   switchWaterDirection(target)
  271.  
  272.   if target == 'stone' then
  273.     print('Start: ',target,' ',maxCount)
  274.     local p = peripheral.wrap("right")
  275.     assert((p and p.digSilkTouch), "required: SilkTouch Turtle")
  276.     waitForEnoughFuel(maxCount,FUEL_SLOT)
  277.     generateStone(maxCount, p.digSilkTouch)
  278.   elseif target == 'cobble' then
  279.     print('Start: ',target,' ',maxCount)
  280.     generateStone(maxCount, turtle.dig)
  281.   elseif target == 'obsidian' then
  282.     print('Start: ',target)
  283.     maxCount = generateObsidian()
  284.   end
  285.  
  286.   print('Generate ',target,': ',maxCount)
  287.   if maxCount > 0 then
  288.     print((os.clock() - sTime)/maxCount,'(sec/piece)')
  289.   end
  290. end
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top