hevohevo

ComputerCraft Tutorial: seichi_0_3

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