Advertisement
hevohevo

ComputerCraft: block_program

Aug 17th, 2019
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. -- Turtle always keeps moving forward.
  2. -- You can program physically by setting some blocks!
  3. local max_step = 10 -- for your safety
  4. local min_fuel_level = 1000 -- maintain this minimum fuel levels
  5. local sleep_time = 10 -- define "os.sleep(sleep_time)" in your block_ruls
  6. local wait_time = 1 -- wait [wait_time] seconds for each step
  7.  
  8. -- define your block_rules. Turtle can't braek these blocks.
  9. local front_rule = {}
  10. front_rule["minecraft:cobblestone"] = turtle.turnRight
  11. front_rule["minecraft:stonebrick"] = turtle.turnLeft
  12. front_rule["minecraft:stone_brick_stairs"] = turtle.up
  13. front_rule["minecraft:stone_stairs"] = turtle.down -- cobblestone stairs
  14. front_rule["minecraft:chest"] = os.shutdown
  15.  
  16. local top_rule = {}
  17. top_rule["minecraft:glass"] = function() os.sleep(sleep_time) end
  18. top_rule["minecraft:chest"] = function()
  19.   while turtle.suckUp(1) and (turtle.getFuelLevel() < min_fuel_level) do
  20.     if turtle.getFuelLevel() < min_fuel_level then os.shutdown() end  -- the fuel level is low, and shutdown
  21.   end
  22. end
  23.  
  24. local bottom_rule = {}
  25. bottom_rule["minecraft:glass"] = function() os.sleep(sleep_time) end
  26. bottom_rule["minecraft:chest"] = function()
  27.   for i=16,1,-1 do
  28.     turtle.select(i)
  29.     local success, msg = turtle.dropDown()
  30.     if not(success) and string.find(msg, "space") then os.shutdown() end -- the chest is full, and shutdown
  31.   end
  32. end
  33.  
  34. -- sub functions
  35. function get_func(inspect_func, rule)
  36.   inspect_func = inspect_func or turtle.inspect
  37.   local success, info = inspect_func()
  38.   local bname = info["name"]
  39.   if success then
  40.     return rule[ bname ], bname
  41.   else
  42.     return nil
  43.   end
  44. end
  45.  
  46. function exec_func(inspect_func, rule)
  47.   local func, bname = get_func(inspect_func, rule)
  48.   if func then
  49.     print("  ",bname)
  50.     return func()
  51.   end
  52.   return nil
  53. end
  54.  
  55. function safe_dig(dig_func, inspect_func, rule)
  56.   if get_func(inspect_func, rule) then
  57.     print("decline to dig"); return false
  58.   else
  59.     local success, msg = dig_func()
  60.     print("  dig ->",success,"(",msg,")")
  61.     return success, msg
  62.   end
  63. end
  64.  
  65. function go_fwd() -- dig the top,bottom,front. and go forward.
  66.   while safe_dig(turtle.digUp, turtle.inspectUp, top_rule) do os.sleep() end -- prepare for a sand or a gravel
  67.   safe_dig(turtle.digDown, turtle.inspectDown, bottom_rule)
  68.   while safe_dig(turtle.dig, turtle.inspect, front_rule) do os.sleep() end -- prepare for a sand or a gravel
  69.   return turtle.forward()
  70. end
  71.  
  72. -- Main
  73. turtle.select(1)
  74. for step=1,max_step do
  75.   print(step,":")
  76.   print(" -top"); exec_func(turtle.inspectUp, top_rule)
  77.   print(" -bottom"); exec_func(turtle.inspectDown, bottom_rule)
  78.   print(" -front"); exec_func(turtle.inspect, front_rule)
  79.   go_fwd()
  80.   os.sleep(wait_time)
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement