Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Tower Builder by Robin | robinschoors.com
- -- Settings (You can change these (: )
- local fuel = true -- Change to false if your turtle doesn't use fuel
- local totalSlots = 16-1 -- Change to 9 if you have an older version! Leave the -1 alone please. :P
- local tightLoop = 0.25 -- Change this to a higher value if the server lags because of the program
- -- Initial variables
- local tArgs = {...}
- local width, length, height, inventory
- local totalBlocksPlaced = 0
- local success = false
- -- Check initial variables
- if tArgs[1] == nil or tArgs[2] == nil or tArgs[3] == nil then
- error("Error!\n\tUSAGE:\n\ttowerBuilder <width> <length> <height>\n\te.g. towerBuilder 4 4 8")
- else
- width = tonumber(tArgs[1])
- length = tonumber(tArgs[2])
- height = tonumber(tArgs[3])
- end
- -- Clever functions go here
- function hasFuel()
- if fuel == true then
- if turtle.getFuelLevel()<=0 then
- turtle.select(1)
- return false
- else
- return true
- end
- else
- return true
- end
- end
- function autoSelect() -- Automatically selects slot with items
- for i=1,totalSlots do
- if turtle.getItemCount(i)>0 then
- turtle.select(i)
- return true
- end
- i = i+1
- end
- return false
- end
- function clearInventory() -- Clear inventory from anything but the building blocks
- for i=1,totalSlots do
- turtle.select(totalSlots+1)
- if turtle.compareTo(i)==false then
- turtle.select(i)
- turtle.drop()
- end
- end
- end
- function totalBlocksNeeded() -- returns total blocks needed for build
- return width * length * height
- end
- function checkInventory() -- Count all blocks in inventory and stores it in inventory var
- inventory = 0
- for i=1,totalSlots do
- inventory = inventory + turtle.getItemCount(i)
- i = i+1
- end
- return inventory
- end
- function checkBlocksNeeded() -- Returns blocks that need to be added to complete the build
- return totalBlocksNeeded()-inventory-totalBlocksPlaced
- end
- function canPlaceBlock() -- Checks if Turtle can place a block
- checkInventory()
- if inventory == 0 then
- return false
- else
- return true
- end
- end
- function fuelCheckOnMove()
- if fuel==true then
- if hasFuel() == false then
- print "Out of fuel! Put some fuel in my first slot. I'll automatically turn it into energy! :)"
- while hasFuel()==false do
- turtle.refuel()
- hasFuel()
- os.sleep(tightLoop)
- end
- print "Resuming build in 5 seconds..."
- sleep(5)
- end
- end
- end
- function moveUp() -- Move up 1 level Checks for gravel or other players that may be obstructing the path
- fuelCheckOnMove()
- success = false
- while success==false do
- while turtle.detectUp() do
- turtle.digUp()
- end
- success=turtle.up()
- end
- end
- function forward() -- Move forward. Checks for gravel or other players that may be obstructing the path
- fuelCheckOnMove()
- success = false
- while success==false do
- while turtle.detect()==true do
- turtle.dig()
- clearInventory()
- end
- success=turtle.forward()
- end
- end
- function placedBlock() -- Doing some additional calculations after placing a block
- totalBlocksPlaced = totalBlocksPlaced + 1
- end
- function placeDown() -- place block beneath the turtle
- success = false
- while success == false do
- if canPlaceBlock()==false then
- print "Ran out of blocks to place. Will resume after blocks are added to inventory!"
- while inventory<=0 do
- checkInventory()
- sleep(tightLoop)
- end
- print "Resuming build in 5 seconds..."
- sleep(5)
- end
- autoSelect()
- success = turtle.placeDown()
- if turtle.detectDown()==true then
- success = true
- end
- end
- placedBlock()
- end
- function turnRight()
- success = false
- while success==false do
- success = turtle.turnRight()
- end
- end
- function turnLeft()
- success = false
- while success==false do
- success = turtle.turnLeft()
- end
- end
- function buildLength()
- for i=1,length do
- placeDown()
- forward()
- end
- end
- function buildWidth()
- for i=1,width do
- placeDown()
- forward()
- end
- end
- function buildLevel()
- moveUp()
- buildLength()
- turnLeft()
- buildWidth()
- turnLeft()
- buildLength()
- turnLeft()
- buildWidth()
- turnLeft()
- end
- function build()
- turnRight()
- for x=1,height do
- buildLevel()
- end
- end
- -- Doing the build
- print("I'm gonna need " .. totalBlocksNeeded() .. " to build this. Starting build...\n")
- build()
Advertisement
Add Comment
Please, Sign In to add comment