hevohevo

ComputerCraft Tutorial: seichi_0_4

Jul 22nd, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.51 KB | None | 0 0
  1. -- #############################
  2. -- seichi
  3. -- version 0.4
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- #############################
  7. -- config (default parameters)
  8. DEPTH = 4 -- okuyuki
  9. WIDTH =4 -- haba
  10. HEIGHT = 4 -- takasa
  11. TOARCH_SLOT = 15
  12. CHEST_SLOT = 16
  13. CHEST_TIMING = 'as_soon_as' -- 'regularly' or 'as_soon_as'
  14. CURRENT_HEIGHT=1
  15. REDUNDANCY =1 -- expect the hight of cave
  16.  
  17.  
  18. -- #############################
  19. -- functions
  20. function surelyDigUp()
  21.   while turtle.digUp() do
  22.     os.sleep(0.4)
  23.   end
  24. end
  25.  
  26. function surelyUp()
  27.   surelyDigUp()
  28.   local status, error_msg = turtle.up()
  29.   if status then CURRENT_HEIGHT = CURRENT_HEIGHT+1 end
  30.   return status, error_msg
  31. end
  32.  
  33. function surelyDown()
  34.   turtle.digDown()
  35.   local status, error_msg = turtle.down()
  36.   if status then CURRENT_HEIGHT = CURRENT_HEIGHT-1 end
  37.   return status, error_msg
  38. end
  39.  
  40. function surelyDig()
  41.   while turtle.dig() do end
  42. end
  43.  
  44. function surelyFwd()
  45.   for i=1,4 do
  46.     local status, err = turtle.forward()
  47.     if status then
  48.       return true  -- success!
  49.     elseif err=="Out of fuel" then
  50.       return status, err
  51.     end
  52.  
  53.     surelyDig() -- face to a normal block or a sand(gravel) hill
  54.  
  55.     if turtle.detect() and not turtle.dig() then
  56.       return false, "bedrock!"
  57.     end
  58.  
  59.     if turtle.forward() then return true end -- success!
  60.  
  61.     if turtle.attack() then
  62.       -- face to monster-mob
  63.       while turtle.attack() do end
  64.     else
  65.       -- face to sand-blocks which is dropping long time
  66.       os.sleep(5) -- probably, adjustment is required
  67.     end
  68.   end
  69.   return turtle.up()
  70. end
  71.  
  72. -- go to the top of a facing block tower
  73. function goTop()
  74.   for i=1,REDUNDANCY do
  75.     surelyUp()
  76.   end
  77.   while turtle.detectUp() or turtle.detect() do
  78.     surelyUp()
  79.     goTop()
  80.   end
  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(CHEST_TIMING, 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(putType, current_d, current_w)
  164.   if putType == "regularly" then
  165.     -- put items regularly
  166.     if current_w%4==1 and current_d<4 then
  167.       -- placeDown a chest to put items
  168.       if turtle.getItemCount(2)>0 and placeItem(CHEST_SLOT, turtle.placeUp) then
  169.         dropAllItems(turtle.dropUp)
  170.       elseif turtle.getItemCount(14)>0 then
  171.         error("Inventory is full!")
  172.       end
  173.     end
  174.   else
  175.     -- put items as soon as inventory becomes fill
  176.     if turtle.getItemCount(12)>0 then
  177.       if placeItem(CHEST_SLOT, turtle.placeUp) then
  178.         dropAllItems(turtle.dropUp)
  179.       elseif turtle.getItemCount(14)>0 then
  180.         error("Inventory is full!")
  181.       end
  182.     end
  183.   end
  184. end
  185.  
  186. -- turn around and place toarch at even intervals (5 spaces)
  187. function placeToarch(current_d, current_w)
  188.   if current_d%2==0 and current_w%6==1 then
  189.     print(string.format("D/W = %d/%d", current_d, current_w))
  190.     turtle.turnRight(); turtle.turnRight()
  191.     print('place a toarch: ', placeItem(TOARCH_SLOT))
  192.     turtle.turnRight(); turtle.turnRight()
  193.   end
  194. end
  195.  
  196. -- #############################
  197. -- main
  198.  
  199. -- arguments setting
  200. local args = {...}
  201. if #args == 3 or #args ==2 then
  202.   DEPTH = tonumber(args[1]) or DEPTH
  203.   WIDTH = tonumber(args[2]) or WIDTH
  204.   REDUNDANCY = tonumber(args[3]) or REDUNDANCY -- expect the hight of cave
  205. else
  206.   error('seichi <DEPTH> <WIDTH> (CAVE_HEIGHT)')
  207. end
  208. print(string.format("D/W = %d/%d, CaveHeight=%d", DEPTH, WIDTH, REDUNDANCY))
  209.  
  210. -- start
  211. surelyFwd()
  212.  
  213. for w= 1,WIDTH do
  214.   seichiOneLine(w)
  215.  
  216.   if WIDTH>w and w%2 == 1 then -- current width pos is odd number
  217.     turtle.turnRight()
  218.     surelyFwd()
  219.     turtle.turnRight()
  220.   elseif WIDTH>w and w%2 == 0 then -- current width pos is even number
  221.     turtle.turnLeft()
  222.     surelyFwd()
  223.     turtle.turnLeft()
  224.   end
  225. end
  226. print("current fuel: ",turtle.getFuelLevel())
Add Comment
Please, Sign In to add comment