Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ------------------------------------------------------
- --
- -- Wall builder
- -- Author: KROM
- -- goes forward and down. then steps back and places block in front
- -- end of row, move up, turn around and continue.
- --
- -- simple and untested, use at own risk ;p
- --
- -- ------------------------------------------------------
- local nStepsBackHome = 0
- local bDone = false
- local bAtHome = true
- local nEnergy = 0
- local nActiveSlot = 0
- local nItemsLeft = 0
- local vOffset = vector.new(0,0,0)
- local vDir = vector.new(0,0,1)
- -- ------------------------------------------------------
- local function goForward()
- if turtle.forward() == true then
- if vDir.z == 1 then vOffset.z = vOffset.z + 1 else vOffset.z = vOffset.z - 1 end
- nEnergy = nEnergy - 1;
- return true
- end
- return false
- end
- -- ------------------------------------------------------
- local function goBack()
- if turtle.back() == true then
- if vDir.z == 1 then vOffset.z = vOffset.z - 1 else vOffset.z = vOffset.z + 1 end
- nEnergy = nEnergy - 1;
- return true
- end
- return false
- end
- -- ------------------------------------------------------
- local function goUp()
- if turtle.up() == true then
- vOffset.y = vOffset.y + 1
- nEnergy = nEnergy - 1;
- return true
- end
- return false
- end
- -- ------------------------------------------------------
- local function goDown()
- if turtle.down() == true then
- vOffset.y = vOffset.y - 1
- nEnergy = nEnergy - 1;
- return true
- end
- return false
- end
- -- ------------------------------------------------------
- local function goHome()
- print("Going home")
- while vOffset.y > 0 do
- if goUp() == false then
- print("ERROR: Return home: no way up")
- end
- end
- if not vDir.z == 1 then
- turtle.turnLeft()
- turtle.turnLeft()
- nEnergy = nEnergy - 2;
- vDir.z = 1
- end
- while vOffset.z > 0 do
- if goForward() == false then
- print("ERROR: Return home: no way forward")
- end
- end
- if vOffset.y == 0 and vOffset.z == 0 then
- bHome = true
- print("Arrived at home")
- end
- end
- -- ------------------------------------------------------
- local function refuel(amount)
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- nEnergy = 9999
- return true
- end
- --refuel
- local needed = amount or 600
- if turtle.getFuelLevel() < needed then
- for n=1,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- if turtle.refuel(1) then
- needed = needed - 1
- while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
- turtle.refuel(1)
- end
- if turtle.getFuelLevel() >= needed then
- turtle.select(1)
- nEnergy = turtle.getFuelLevel()
- return true
- end
- end
- end
- end
- nEnergy = turtle.getFuelLevel()
- turtle.select(1)
- return false
- end
- nEnergy = turtle.getFuelLevel()
- turtle.select(1)
- return true
- end
- -- ------------------------------------------------------
- local function getBlock()
- for n=2,16 do
- if turtle.getItemCount(n) > 0 then
- turtle.select(n)
- nItemsLeft = turtle.getItemCount(n)
- nActiveSlot = n
- return true
- end
- end
- return false
- end
- -- ------------------------------------------------------
- local function placeBlock()
- if nItemsLeft > 0 then
- turtle.place()
- nItemsLeft = nItemsLeft - 1
- return true
- else
- if getBlock() == true then
- return placeBlock()
- else
- return false
- end
- end
- end
- -- ------------------------------------------------------
- local function placeBlockDown()
- if nItemsLeft > 0 then
- turtle.placeDown()
- nItemsLeft = nItemsLeft - 1
- return true
- else
- if getBlock() == true then
- return placeBlock()
- else
- return false
- end
- end
- end
- -- ------------------------------------------------------
- while not bDone do
- if bAtHome == true then
- -- if at home
- -- check energy
- if not refuel() or nEnergy < 60 then
- print "Fuel less than 60 and no fuel left. Abort."
- bDone = true
- break
- end
- -- check inventory
- if not getBlock() then
- print "No blocks to place. Abort."
- bDone = true
- break
- end
- while goForward() == true do
- end
- while goDown() == true do
- end
- bAtHome = false
- else
- -- check energy
- if not refuel() or nEnergy < 60 then
- print "Fuel less than 60 and no fuel left. Abort."
- goHome()
- bDone = true
- break
- end
- -- check inventory
- if not getBlock() then
- print "No blocks to place. Abort."
- bDone = true
- goHome()
- break
- end
- -- if on duty
- if goBack() == true then
- if not placeBlock() then
- print("could not place block")
- goHome()
- bDone = true
- break
- end
- else
- if goUp() == true then
- if not placeBlockDown() then
- print("could not place block below")
- goHome()
- bDone = true
- break
- else
- -- check if top layer
- if vOffset.y == 0 then
- print("Reached home layer. Returning")
- goHome()
- bDone = true
- break
- end
- -- check if we can move backwards now
- if goBack() == true then
- if not placeBlock() then
- print("could not place block")
- goHome()
- bDone = true
- break
- end
- else
- -- no we can not. turn around
- turtle.turnLeft()
- turtle.turnLeft()
- nEnergy = nEnergy - 2;
- vDir.z = 1 - vDir.z
- end
- end
- else
- print("FATAL ERROR: could not go up")
- bDone = true
- break
- end
- end
- end
- end
- print("Wall: All done!")
- -- ------------------------------------------------------
- -- ------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment