Advertisement
soee

flood

Oct 2nd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. --file:flood
  2. --pastebin:B6YAtvQU
  3.  
  4. targs = { ... }
  5. os.loadAPI("movement")
  6.  
  7. local m = movement
  8. local fullSlot = 1
  9. local emptySlot = 2
  10. local reservedSlots = 2
  11.  
  12. if (#targs ~= 2) then
  13.     print("Usage : flood <length> <width>")
  14.     print("Slot 1 : Full water bucket")
  15.     print("Slot 2 : Empty bucket")
  16.     print("Slot 3-16 : At least 2 full buckets, the rest may be empty")
  17.     return
  18. end
  19.  
  20. local length = tonumber(targs[1])
  21. local width = tonumber(targs[2])
  22.  
  23.  
  24. local function countBuckets(slot)
  25.     turtle.select(slot)
  26.     local bucketCount = 0;
  27.     for i = 3, 16 do
  28.         if (turtle.compareTo(i)) then
  29.             bucketCount = bucketCount + 1
  30.         end
  31.     end
  32.     return bucketCount
  33. end
  34.  
  35. local function countFullBuckets()
  36.     return countBuckets(fullSlot)
  37. end
  38.  
  39. local function countEmptyBuckets()
  40.     return countBuckets(emptySlot)
  41. end
  42.  
  43. function selectMaterial(typeSlot)
  44.     turtle.select(typeSlot)
  45.     for otherSlot = 16, 1, -1 do
  46.         if (turtle.compareTo(otherSlot)) then
  47.             if otherSlot <= reservedSlots then
  48.                 if turtle.getItemCount(otherSlot) > 1 then
  49.                     turtle.select(otherSlot)
  50.                     return otherSlot
  51.                 end
  52.             else
  53.                 turtle.select(otherSlot)
  54.                 return otherSlot
  55.             end
  56.         end
  57.     end
  58.     return -1
  59. end
  60.  
  61. local function selectBucket(slot)
  62.     local foundSlot = selectMaterial(slot)
  63.     if foundSlot < 2 then
  64.         return false
  65.     else
  66.         return true
  67.     end
  68. end
  69.  
  70. local function selectFullBucket()
  71.     return selectBucket(fullSlot)
  72. end
  73.  
  74. local function selectEmptyBucket()
  75.     return selectBucket(emptySlot)
  76. end
  77.  
  78. local function fillAllEmpty()
  79.     while selectEmptyBucket() do
  80.         turtle.placeDown()
  81.         sleep(1)
  82.     end
  83. end
  84.  
  85. local function refill(x, y)
  86.     print("Refilling")
  87.     if (y == 1) then
  88.         -- first row, since we started with at least 2 buckets, we can go back 2 steps and refill.
  89.         m.turn(true)
  90.         m.turn(true)
  91.         m.turtleForward()
  92.         m.turtleForward()
  93.         fillAllEmpty()
  94.         m.turn(true)
  95.         m.turn(true)
  96.         m.turtleForward()
  97.         m.turtleForward()
  98.     else
  99.         -- at least second row. we can go 1 row back, and refull there.
  100.         local goRight = (y % 2 == 0)
  101.         if (x == 1) then
  102.             m.turtleForward()
  103.         end
  104.         m.turn(goRight)
  105.         m.turtleForward()
  106.         fillAllEmpty()
  107.         m.turn(goRight)
  108.         m.turn(goRight)
  109.         m.turtleForward()
  110.         if (x == 1) then
  111.             m.turn(not goRight)
  112.             m.turtleForward()
  113.             m.turn(goRight)
  114.             m.turn(goRight)
  115.         else
  116.             m.turn(goRight)
  117.         end
  118.     end
  119. end
  120.  
  121.  
  122.  
  123. local function main()
  124.     local fullCount = countFullBuckets()
  125.     print("Full buckets : ", fullCount)
  126.     if fullCount < 2 then
  127.         print("need at least 2 full buckets, apart from slot 1")
  128.         return
  129.     end
  130.  
  131.     local turnRight = true
  132.     local alternate = (length % 2)
  133.     for y = 1, width do
  134.         local offset = (y % 2) * alternate
  135.         for x = 1, length do
  136.             turtle.digDown()
  137.             if ((offset + x + 1) % 2 == 0) then
  138.                 local hasFull = selectFullBucket()
  139.                 if (not hasFull) then
  140.                     refill(x, y)
  141.                 end
  142.                 selectFullBucket()
  143.                 turtle.placeDown()
  144.             end
  145.  
  146.             if (x < length) then
  147.                 m.turtleForward()
  148.             end
  149.         end
  150.         if (y < width) then
  151.             m.turn(turnRight)
  152.             m.turtleForward()
  153.             m.turn(turnRight)
  154.         end
  155.         turnRight = not turnRight
  156.     end
  157.  
  158.     -- return to start position.
  159.     if (not turnRight) then
  160.         -- turtle is to the far right
  161.         m.turn(false)
  162.         m.turtleForwardX(width - 1)
  163.         m.turn(false)
  164.         m.turtleForwardX(length - 1)
  165.         m.turn(false)
  166.         m.turn(false)
  167.     else
  168.         -- turtle is to the close right
  169.         m.turn(true)
  170.         m.turtleForwardX(width - 1)
  171.         m.turn(true)
  172.     end
  173. end
  174.  
  175. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement