Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function move(direction, count)
- if ( direction == "forward" ) then
- for i=1,count do
- checkFuel()
- while ( not turtle.forward() ) do
- turtle.dig()
- checkInv()
- end
- end
- elseif ( direction == "up" ) then
- for i=1,count do
- checkFuel()
- while ( not turtle.up() ) do
- turtle.digUp()
- checkInv()
- end
- end
- elseif ( direction == "down" ) then
- for i=1,count do
- checkFuel()
- while ( not turtle.down() ) do
- turtle.digDown()
- checkInv()
- end
- end
- else
- io.write("Unknown direction specified: " .. direction .. "\n")
- os.exit()
- end
- end
- function turn(direction)
- if ( direction == "left" ) then
- turtle.turnLeft()
- elseif ( direction == "right" ) then
- turtle.turnRight()
- else
- io.write("Unknown turn Direction: " .. direction .. "\n")
- os.exit()
- end
- end
- function checkInv()
- full=true
- for i=freeSlot,16 do
- full=(turtle.getItemCount(i) > 0 and full)
- end
- return full
- end
- function checkFuel()
- if ( turtle.getFuelLevel() <= 1 ) then
- io.write("The turtle is very low on Fuel. Please place fuel in slot " .. freeSlot .. "\n")
- turtle.select(freeSlot)
- while ( turtle.getFuelLevel() <= 1 ) do
- turtle.refuel()
- sleep(1)
- end
- end
- end
- function getSlot()
- if ( turtle.getItemCount(currentSlot) > 0 ) then
- return
- else
- for i=1,#subs do
- if ( turtle.getItemCount(subs[i]) > 0 ) then
- currentSlot=i
- return
- end
- end
- io.write("There are no blocks available for building. Please place blocks in one of the following slots\n")
- for i=1,#subs do
- io.write(subs[i] .. "\n")
- end
- while ( true ) do
- for i=1,#subs do
- if ( turtle.getItemCount(subs[i]) > 0 ) then
- currentSlot=i
- return
- end
- end
- sleep(1)
- end
- end
- end
- function placeBlock()
- getSlot()
- turtle.select(currentSlot)
- if ( not turtle.compareDown() ) then
- turtle.digDown()
- turtle.placeDown()
- end
- turtle.select(freeSlot)
- end
- function checkBlocks(requiredNumber)
- j=2
- subs={}
- subs[1]=1
- turtle.select(1)
- if ( turtle.getItemCount(1) > 0 ) then
- blockCount=turtle.getItemCount(1)
- for i=2,16 do
- if ( turtle.compareTo(i) ) then
- subs[j]=i
- j=j+1
- blockCount = blockCount + turtle.getItemCount(i)
- else
- if ( freeSlot == 0 ) then
- freeSlot=i
- end
- end
- end
- else
- io.write("There are no blocks in slot 1. Please correct and run again\n")
- os.exit()
- end
- if ( blocksReq > blockCount ) then
- io.write("There are insufficient blocks in subsitute slots, a refill will be required\n")
- end
- end
- args={ ... }
- length=tonumber(args[1])
- width=tonumber(args[2])
- freeSlot=0
- currentSlot=1
- blocksReq=length*width
- checkBlocks()
- turnDirection="right"
- for i=1,width do
- for j=1,length do
- placeBlock()
- if ( j ~= length ) then
- move("forward",1)
- end
- end
- turn(turnDirection)
- move("forward", 1)
- turn(turnDirection)
- if ( i % 2 == 1 ) then
- turnDirection="left"
- else
- turnDirection="right"
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment