hevohevo

ComputerCraft Tutorial: cutland_0_1

Aug 2nd, 2014
5,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.72 KB | None | 0 0
  1. -- #############################
  2. -- cutland
  3. -- version 0.1
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- #############################
  7. -- config (default parameters)
  8. DEPTH = 4 -- okuyuki
  9. WIDTH = 4 -- haba
  10.  
  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. function goTop()
  68.   while turtle.detectUp() or turtle.detect() do
  69.     surelyUp()
  70.   end
  71. end
  72.  
  73. -- down() to the initial height
  74. -- if argument exists, down() and dig() to facing block
  75. function goBottom(func)
  76.   while current_height > 1 do
  77.     if func then func() end -- if func isn't nil, dig foward
  78.     surelyDown()
  79.   end
  80. end
  81.  
  82. function seichiThreeStep(current_d, current_w)
  83.   goTop()
  84.   surelyFwd()
  85.   goTop()
  86.   goBottom(surelyDig)
  87.  
  88.   -- place toarch
  89.   placeToarch(current_d, current_w)
  90.   -- place chest and put items
  91.   putItems()
  92.  
  93.   surelyFwd()
  94. end
  95.  
  96. function seichiTwoStep()
  97.   goTop()
  98.   surelyFwd()
  99.   goBottom()
  100. end
  101.  
  102. function seichiOneStep()
  103.   goTop()
  104.   goBottom()
  105. end
  106.  
  107. -- use seichiThreeStep() as much as possible
  108. function seichiOneLine(current_w)
  109.   local div3 = math.floor(DEPTH / 3)
  110.   local mod3 = DEPTH % 3
  111.   local current_d = 1
  112.   for i = 1, div3 do
  113.     seichiThreeStep(current_d, current_w)
  114.     current_d = current_d + 2
  115.     if current_d < DEPTH then
  116.       surelyFwd()
  117.       current_d = current_d + 1
  118.     end
  119.   end
  120.   if mod3 == 1 then
  121.     seichiOneStep()
  122.   elseif mod3 == 2 then
  123.     seichiTwoStep()
  124.   end
  125. end
  126.  
  127. -- placeItem(TOARCH_SLOT): place() TOARCH_SLOT's item
  128. -- placeItem(CHEST_SLOT, turtle.placeUp): use placeUp()
  129. function placeItem(slot, place_func)
  130.   local place_func = place_func or turtle.place
  131.   if turtle.getItemCount(slot) == 0 then return false end
  132.   turtle.select(slot)
  133.   local status, error_msg = place_func()
  134.   turtle.select(1)
  135.   return status, error_msg
  136. end
  137.  
  138. -- dropAllItems(): dropDown items within 1-14 slots
  139. -- dropAllItems(turtle.dropUp): use dropUp() function
  140. function dropAllItems(drop_func)
  141.   local drop_func = drop_func or turtle.dropDown
  142.   for i = 1, 14 do
  143.     if turtle.getItemCount(i) > 0 then
  144.       turtle.select(i)
  145.       drop_func()
  146.     end
  147.   end
  148.   turtle.select(1)
  149. end
  150.  
  151. -- put items as soon as inventory becomes fill (slot 12)
  152. function putItems()
  153.   if turtle.getItemCount(12)>0 then
  154.     if placeItem(CHEST_SLOT, turtle.placeUp) then
  155.       dropAllItems(turtle.dropUp)
  156.     elseif turtle.getItemCount(14)>0 then
  157.       error("Inventory is full!") -- terminate if can't put
  158.     end
  159.   end
  160. end
  161.  
  162. -- turn around and place toarch at even intervals (5 spaces)
  163. function placeToarch(current_d, current_w)
  164.   if current_d % 2 == 0 and current_w % 6 == 1 then
  165.     print(string.format("D/W = %d/%d", current_d, current_w))
  166.     turtle.turnRight(); turtle.turnRight()
  167.     print('place a toarch: ', placeItem(TOARCH_SLOT))
  168.     turtle.turnRight(); turtle.turnRight()
  169.   end
  170. end
  171.  
  172. -- #############################
  173. -- main
  174.  
  175. -- arguments setting
  176. local args = { ... }
  177. if #args == 2 or #args == 0 then
  178.   DEPTH = tonumber(args[1]) or DEPTH
  179.   WIDTH = tonumber(args[2]) or WIDTH
  180. else
  181.   error('seichi <DEPTH> <WIDTH>')
  182. end
  183. print(string.format("D/W = %d/%d", DEPTH, WIDTH))
  184.  
  185. -- start
  186. surelyFwd()
  187.  
  188. for w = 1, WIDTH do
  189.   seichiOneLine(w)
  190.  
  191.   if WIDTH > w and w % 2 == 1 then -- current width pos is odd number
  192.     turtle.turnRight()
  193.     surelyFwd()
  194.     turtle.turnRight()
  195.   elseif WIDTH > w and w % 2 == 0 then -- current width pos is even number
  196.     turtle.turnLeft()
  197.     surelyFwd()
  198.     turtle.turnLeft()
  199.   end
  200. end
  201. print("current fuel: ", turtle.getFuelLevel())
Add Comment
Please, Sign In to add comment