hevohevo

ComputerCraft Tutorial: cutland_0_2

Aug 2nd, 2014
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.37 KB | None | 0 0
  1. -- #############################
  2. -- cutland
  3. -- version 0.2
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- #############################
  7. -- config (default parameters)
  8. DEPTH = 4 -- okuyuki
  9. WIDTH = 1 -- haba
  10. CAVE_HEIGHT = 0 -- expect the hight of cave
  11. TOARCH_SLOT = 15
  12. CHEST_SLOT = 16
  13.  
  14. -- #############################
  15. -- functions
  16. local current_height = 1
  17.  
  18. function surelyDigUp()
  19.   while turtle.digUp() do
  20.     os.sleep(0.4)
  21.   end
  22. end
  23.  
  24. function surelyUp()
  25.   surelyDigUp()
  26.   local status, error_msg = turtle.up()
  27.   if status then current_height = current_height + 1 end
  28.   return status, error_msg
  29. end
  30.  
  31. function surelyDown()
  32.   turtle.digDown()
  33.   local status, error_msg = turtle.down()
  34.   if status then current_height = current_height - 1 end
  35.   return status, error_msg
  36. end
  37.  
  38. function surelyDig()
  39.   while turtle.dig() do end
  40. end
  41.  
  42. function surelyFwd()
  43.   for i = 1, 4 do
  44.     local status, err = turtle.forward()
  45.     if status then
  46.       return true -- success!
  47.     elseif err == "Out of fuel" then
  48.       return status, err
  49.     end
  50.     surelyDig() -- face to a normal block or a sand(gravel) hill
  51.     if turtle.detect() and not turtle.dig() then
  52.       return false, "bedrock!"
  53.     end
  54.     if turtle.forward() then return true end -- success!
  55.     if turtle.attack() then
  56.       -- face to monster-mob
  57.       while turtle.attack() do end
  58.     else
  59.       -- face to sand-blocks which is dropping long time
  60.       os.sleep(5) -- probably, adjustment is required
  61.     end
  62.   end
  63.   return turtle.up()
  64. end
  65.  
  66. -- go to the top of a facing block tower
  67. -- sadly a turtle can only detect neighbor blocks
  68. -- the turtle go up for touching tha celling of cave
  69. function goTop()
  70.   repeat
  71.     for i=1,(CAVE_HEIGHT-1) do
  72.       surelyUp()
  73.     end
  74.     local flag_rose= true
  75.    
  76.     while turtle.detectUp() or turtle.detect() do
  77.       surelyUp()
  78.       flag_rose= false -- execute from beginning, if detects a block
  79.     end
  80.   until flag_rose
  81. end
  82.  
  83. -- down() to the initial height
  84. -- if argument exists, down() and dig() to facing block
  85. function goBottom(surelyDig_flag)
  86.   while current_height > 1 do
  87.     surelyDown()
  88.     if surelyDig_flag then surelyDig() end
  89.   end
  90. end
  91.  
  92. function seichiThreeStep(current_d, current_w)
  93.   goTop()
  94.   surelyFwd()
  95.   goTop()
  96.   goBottom("surelyDig")
  97.  
  98.   -- place toarch
  99.   placeToarch(current_d, current_w)
  100.   -- place chest and put items
  101.   putItems(current_d, current_w)
  102.  
  103.   surelyFwd()
  104. end
  105.  
  106. function seichiTwoStep()
  107.   goTop()
  108.   surelyFwd()
  109.   goBottom(false)
  110. end
  111.  
  112. function seichiOneStep()
  113.   goTop()
  114.   goBottom(false)
  115. end
  116.  
  117. -- use seichiThreeStep() as much as possible
  118. function seichiOneLine(current_w)
  119.   local div3 = math.floor(DEPTH / 3)
  120.   local mod3 = DEPTH % 3
  121.   local current_d = 1
  122.   for i = 1, div3 do
  123.     seichiThreeStep(current_d, current_w)
  124.     current_d = current_d + 2
  125.     if current_d < DEPTH then
  126.       surelyFwd()
  127.       current_d = current_d + 1
  128.     end
  129.   end
  130.   if mod3 == 1 then
  131.     seichiOneStep()
  132.   elseif mod3 == 2 then
  133.     seichiTwoStep()
  134.   end
  135. end
  136.  
  137. -- placeItem(TOARCH_SLOT): place() TOARCH_SLOT's item
  138. -- placeItem(CHEST_SLOT, turtle.placeUp): use placeUp()
  139. function placeItem(slot, place_func)
  140.   local place_func = place_func or turtle.place
  141.   if turtle.getItemCount(slot) == 0 then return false end
  142.   turtle.select(slot)
  143.   local status, error_msg = place_func()
  144.   turtle.select(1)
  145.   return status, error_msg
  146. end
  147.  
  148. -- dropAllItems(): dropDown items within 1-14 slots
  149. -- dropAllItems(turtle.dropUp): use dropUp() function
  150. function dropAllItems(drop_func)
  151.   local drop_func = drop_func or turtle.dropDown
  152.   for i = 1, 14 do
  153.     if turtle.getItemCount(i) > 0 then
  154.       turtle.select(i)
  155.       drop_func()
  156.     end
  157.   end
  158.   turtle.select(1)
  159. end
  160.  
  161. -- two types of put items function:
  162. --  'regulary' without investigating inventory and 'as_soon_as' it becomes fill
  163. function putItems(current_d, current_w)
  164.   -- put items regularly
  165.   if current_w % 4 == 1 and current_d < 4 then
  166.     -- placeDown a chest to put items
  167.     if turtle.getItemCount(2) > 0 and placeItem(CHEST_SLOT, turtle.placeUp) then
  168.       dropAllItems(turtle.dropUp)
  169.     elseif turtle.getItemCount(14) > 0 then
  170.       error("Inventory is full!")
  171.     end
  172.   end
  173. end
  174.  
  175. -- turn around and place toarch at even intervals (5 spaces)
  176. function placeToarch(current_d, current_w)
  177.   if current_d % 2 == 0 and current_w % 6 == 1 then
  178.     print(string.format("D/W = %d/%d", current_d, current_w))
  179.     turtle.turnRight(); turtle.turnRight()
  180.     print('place a toarch: ', placeItem(TOARCH_SLOT))
  181.     turtle.turnRight(); turtle.turnRight()
  182.   end
  183. end
  184.  
  185. -- #############################
  186. -- main
  187.  
  188. -- arguments setting
  189. local args = { ... }
  190. if #args == 3 or #args == 2 or #args==0 then
  191.   DEPTH = tonumber(args[1]) or DEPTH
  192.   WIDTH = tonumber(args[2]) or WIDTH
  193.   CAVE_HEIGHT = tonumber(args[3]) or CAVE_HEIGHT -- expect the height of cave
  194. else
  195.   error('seichi <DEPTH> <WIDTH> (CAVE_HEIGHT)')
  196. end
  197. print(string.format("D/W = %d/%d, CaveHeight=%d", DEPTH, WIDTH, CAVE_HEIGHT))
  198.  
  199. -- start
  200. surelyFwd()
  201.  
  202. for w = 1, WIDTH do
  203.   seichiOneLine(w)
  204.  
  205.   if WIDTH > w and w % 2 == 1 then -- current width pos is odd number
  206.     turtle.turnRight()
  207.     surelyFwd()
  208.     turtle.turnRight()
  209.   elseif WIDTH > w and w % 2 == 0 then -- current width pos is even number
  210.     turtle.turnLeft()
  211.     surelyFwd()
  212.     turtle.turnLeft()
  213.   end
  214. end
  215. print("current fuel: ", turtle.getFuelLevel())
Add Comment
Please, Sign In to add comment