Advertisement
hevohevo

ComputerCraft Tutorial: seichi_0_1

Jul 15th, 2014
2,771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. -- #############################
  2. -- seichi
  3. -- version 0.1a
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- #############################
  7. -- config (default parameters)
  8. DEPTH = 4 -- okuyuki
  9. WIDTH =4 -- haba
  10. HEIGHT = 4 -- takasa
  11.  
  12. -- #############################
  13. -- functions
  14. function surelyDigUp()
  15.   while turtle.digUp() do
  16.     os.sleep(0.4)
  17.   end
  18. end
  19.  
  20. function surelyUp()
  21.   surelyDigUp()
  22.   return turtle.up()
  23. end
  24.  
  25. function surelyDig()
  26.   while turtle.dig() do end
  27. end
  28.  
  29. function surelyFwd()
  30.   for i=1,4 do
  31.     local status, err = turtle.forward()
  32.     if status then
  33.       return true  -- success!
  34.     elseif err=="Out of fuel" then
  35.       return status, err
  36.     end
  37.  
  38.     surelyDig() -- face to a normal block or a sand(gravel) hill
  39.  
  40.     if turtle.detect() and not turtle.dig() then
  41.       return false, "bedrock!"
  42.     end
  43.  
  44.     if turtle.forward() then return true end -- success!
  45.  
  46.     if turtle.attack() then
  47.       -- face to monster-mob
  48.       while turtle.attack() do end
  49.     else
  50.       -- face to sand-blocks which is dropping long time
  51.       os.sleep(5) -- probably, adjustment is required
  52.     end
  53.   end
  54.   return turtle.forward()
  55. end
  56.  
  57. function seichiTwoStep()
  58.   for i=1,HEIGHT-1 do
  59.     surelyUp()
  60.   end
  61.   surelyFwd()  
  62.   for i=1,HEIGHT-1 do
  63.     turtle.digDown()
  64.     turtle.down()
  65.   end
  66. end
  67.  
  68. function seichiOneStep()
  69.   for i=1,HEIGHT-1 do
  70.     surelyUp()
  71.   end
  72.   for i=1,HEIGHT-1 do
  73.     turtle.down()
  74.   end
  75. end
  76.  
  77. function seichiOneLine(d)
  78.   local d = d or DEPTH
  79.   for i=1, d/2 do
  80.     seichiTwoStep()
  81.     if i<(d/2) then surelyFwd() end
  82.   end
  83.   if d%2==1 then
  84.     seichiOneStep()
  85.   end
  86. end
  87.  
  88. function countAllItems()
  89.   local count = 0
  90.   for i=1,16 do
  91.     count = count+turtle.getItemCount(i)
  92.   end
  93.   return count
  94. end
  95. -- #############################
  96. -- main
  97.  
  98. -- arguments setting
  99. local args = {...}
  100. if #args ~= 3 and #args ~=0 then error('seichi <DEPTH> <WIDTH> <HEIGHT>') end
  101. DEPTH = tonumber(args[1]) or DEPTH
  102. WIDTH = tonumber(args[2]) or WIDTH
  103. HEIGHT = tonumber(args[3]) or HEIGHT
  104. print(string.format("D/W/H = %d/%d/%d", DEPTH, WIDTH, HEIGHT))
  105.  
  106. -- fuel check
  107. local requiredFuelLevel = DEPTH*WIDTH*HEIGHT+(DEPTH%2)*WIDTH*HEIGHT
  108. if requiredFuelLevel > turtle.getFuelLevel() then
  109.   error('Not enough Fuel: ',turtle.getFuelLevel(),'/',requiredFUelLevel)
  110. else
  111.   print('currnet/required = ',turtle.getFuelLevel(),'/',requiredFuelLevel)
  112. end
  113.  
  114. -- start
  115. local qty_item = countAllItems() -- store qty of items
  116. surelyFwd()
  117.  
  118. for w= 1,WIDTH do
  119.   seichiOneLine()
  120.  
  121.   if WIDTH>w and w%2 == 1 then -- current width pos is odd number
  122.     turtle.turnRight()
  123.     surelyFwd()
  124.     turtle.turnRight()
  125.   elseif WIDTH>w and w%2 == 0 then -- current width pos is even number
  126.     turtle.turnLeft()
  127.     surelyFwd()
  128.     turtle.turnLeft()
  129.   end
  130. end
  131. print("current fuel: ",turtle.getFuelLevel())
  132. print("acquired items: ", countAllItems()-qty_item) -- calc qty of acquired items
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement