Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --file:flood
- --pastebin:B6YAtvQU
- targs = { ... }
- os.loadAPI("movement")
- local m = movement
- local fullSlot = 1
- local emptySlot = 2
- local reservedSlots = 2
- if (#targs ~= 2) then
- print("Usage : flood <length> <width>")
- print("Slot 1 : Full water bucket")
- print("Slot 2 : Empty bucket")
- print("Slot 3-16 : At least 2 full buckets, the rest may be empty")
- return
- end
- local length = tonumber(targs[1])
- local width = tonumber(targs[2])
- local function countBuckets(slot)
- turtle.select(slot)
- local bucketCount = 0;
- for i = 3, 16 do
- if (turtle.compareTo(i)) then
- bucketCount = bucketCount + 1
- end
- end
- return bucketCount
- end
- local function countFullBuckets()
- return countBuckets(fullSlot)
- end
- local function countEmptyBuckets()
- return countBuckets(emptySlot)
- end
- function selectMaterial(typeSlot)
- turtle.select(typeSlot)
- for otherSlot = 16, 1, -1 do
- if (turtle.compareTo(otherSlot)) then
- if otherSlot <= reservedSlots then
- if turtle.getItemCount(otherSlot) > 1 then
- turtle.select(otherSlot)
- return otherSlot
- end
- else
- turtle.select(otherSlot)
- return otherSlot
- end
- end
- end
- return -1
- end
- local function selectBucket(slot)
- local foundSlot = selectMaterial(slot)
- if foundSlot < 2 then
- return false
- else
- return true
- end
- end
- local function selectFullBucket()
- return selectBucket(fullSlot)
- end
- local function selectEmptyBucket()
- return selectBucket(emptySlot)
- end
- local function fillAllEmpty()
- while selectEmptyBucket() do
- turtle.placeDown()
- sleep(1)
- end
- end
- local function refill(x, y)
- print("Refilling")
- if (y == 1) then
- -- first row, since we started with at least 2 buckets, we can go back 2 steps and refill.
- m.turn(true)
- m.turn(true)
- m.turtleForward()
- m.turtleForward()
- fillAllEmpty()
- m.turn(true)
- m.turn(true)
- m.turtleForward()
- m.turtleForward()
- else
- -- at least second row. we can go 1 row back, and refull there.
- local goRight = (y % 2 == 0)
- if (x == 1) then
- m.turtleForward()
- end
- m.turn(goRight)
- m.turtleForward()
- fillAllEmpty()
- m.turn(goRight)
- m.turn(goRight)
- m.turtleForward()
- if (x == 1) then
- m.turn(not goRight)
- m.turtleForward()
- m.turn(goRight)
- m.turn(goRight)
- else
- m.turn(goRight)
- end
- end
- end
- local function main()
- local fullCount = countFullBuckets()
- print("Full buckets : ", fullCount)
- if fullCount < 2 then
- print("need at least 2 full buckets, apart from slot 1")
- return
- end
- local turnRight = true
- local alternate = (length % 2)
- for y = 1, width do
- local offset = (y % 2) * alternate
- for x = 1, length do
- turtle.digDown()
- if ((offset + x + 1) % 2 == 0) then
- local hasFull = selectFullBucket()
- if (not hasFull) then
- refill(x, y)
- end
- selectFullBucket()
- turtle.placeDown()
- end
- if (x < length) then
- m.turtleForward()
- end
- end
- if (y < width) then
- m.turn(turnRight)
- m.turtleForward()
- m.turn(turnRight)
- end
- turnRight = not turnRight
- end
- -- return to start position.
- if (not turnRight) then
- -- turtle is to the far right
- m.turn(false)
- m.turtleForwardX(width - 1)
- m.turn(false)
- m.turtleForwardX(length - 1)
- m.turn(false)
- m.turn(false)
- else
- -- turtle is to the close right
- m.turn(true)
- m.turtleForwardX(width - 1)
- m.turn(true)
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement