Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --roadBuilder
- currentFacing = 0
- currentlySelectedSlot = 1
- fuelLevelToRefuelAt = 5
- initBlocksCount = 0
- minSlot = 1
- maxSlot = 16
- args = { ... }
- minimumDown = 2
- BLOCK_PILLAR = 1
- BLOCK_GLASS = 3
- BLOCK_WALL = 1
- BLOCK_WALLSTAIRS = 2
- BLOCK_LAMP = 4
- BLOCK_FENCE = 3
- BLOCK_TORCH = 4
- function main()
- if #args > 1 then
- local length = tonumber(args[1])
- local roadType = args[2]
- initBlocks()
- for i=1,length,1 do
- currentY = 0
- print("Building part: "..i.." of "..length);
- if roadType=="tun" then
- tun_makePart()
- elseif roadType=="optun" then
- optun_makePart()
- elseif roadType=="village" then
- village_makePart()
- elseif roadType=="susp" then
- susp_makePart()
- else
- print("Avail types: tun,optun,susp,village")
- end
- end
- else
- print("Usage: road <length> [type=tun|optun|susp|village]")
- end
- end
- function susp_makePart()
- currentY = 0
- turnRight()
- go("forward",true)
- turnLeft()
- for i=1,6 do
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- end
- replaceBlock(BLOCK_WALL,"down")
- turnLeft()
- go("forward",true)
- turnLeft()
- for i=1,6 do
- replaceBlock(BLOCK_FENCE,"down")
- go("forward",true)
- end
- replaceBlock(BLOCK_FENCE,"down")
- turnRight()
- go("forward",true)
- turnRight()
- for i=1,6 do
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- end
- replaceBlock(BLOCK_WALL,"down")
- turnRight()
- go("forward",true)
- turnRight()
- go("up",true)
- for i=1,6 do
- if i==2 or i==6 then
- replaceBlock(BLOCK_TORCH,"down")
- end
- if i==4 then
- replaceBlock(BLOCK_FENCE,"down")
- end
- go("forward",true)
- end
- turnLeft()
- turnLeft()
- go("up",true)
- goX(3,"forward",true)
- replaceBlock(BLOCK_FENCE,"down")
- go("up",true)
- replaceBlock(BLOCK_FENCE,"down")
- go("up",true)
- replaceBlock(BLOCK_PILLAR,"down")
- turnLeft()
- go("forward",true)
- replaceBlock(BLOCK_PILLAR,"down")
- go("forward",true)
- go("forward",true)
- turnLeft()
- turnLeft()
- go("down",true)
- replaceBlock(BLOCK_WALLSTAIRS,"forward")
- goX(3,"down",true)
- for i=1,minimumDown,1 do
- currentY = currentY - 1
- go("down",true)
- end
- repeat
- currentY = currentY - 1
- go("down",true)
- until not isEmpty("down")
- while currentY < (0 + 2) do
- go("up",true)
- replaceBlock(BLOCK_PILLAR,"down")
- currentY = currentY + 1
- end
- go("up",true)
- replaceBlock(BLOCK_WALLSTAIRS,"down")
- turnRight()
- go("forward",true)
- turnLeft()
- go("forward",true)
- go("down",true)
- go("down",true)
- turnLeft()
- go("forward",true)
- turnLeft()
- replaceBlock(BLOCK_WALLSTAIRS,"up")
- turnRight()
- goX(4,"forward",true)
- go("down",true)
- turnRight()
- goX(2,"forward",true)
- turnLeft()
- --next
- end
- function village_makePart()
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- go("forward",true)
- village_pillar()
- go("forward",true)
- go("forward",true)
- replaceBlock(BLOCK_WALL,"down")
- turnLeft()
- go("forward",true)
- turnLeft()
- for j=1,2 do
- for i=1,5,1 do
- replaceBlock(BLOCK_FENCE,"down")
- if i==3 then
- go("up",true)
- replaceBlock(BLOCK_TORCH,"down")
- go("forward",true)
- go("down",true)
- else
- go("forward",true)
- end
- end
- turnLeft()
- go("forward",true)
- if j==1 then
- go("forward",true)
- turnLeft()
- go("forward",true)
- else
- turnRight()
- end
- end
- end
- function optun_makePart()
- for i=1,3,1 do
- optun_case(false)
- end
- go("forward",true)
- go("down",true)
- tun_pillar()
- go("up",true)
- turnLeft()
- turnLeft()
- go("forward",true)
- turnLeft()
- turnLeft()
- optun_case(false)
- optun_case(true)
- optun_case(false)
- for i=1,3,1 do
- optun_case(false)
- end
- end
- function tun_makePart()
- for i=1,3,1 do
- tun_case(false)
- end
- go("forward",true)
- go("down",true)
- tun_pillar()
- go("up",true)
- turnLeft()
- turnLeft()
- go("forward",true)
- turnLeft()
- turnLeft()
- tun_case(false)
- tun_case(true)
- tun_case(false)
- for i=1,3,1 do
- tun_case(false)
- end
- end
- function optun_case(lamp)
- turnLeft()
- go("forward",true)
- turnRight()
- replaceBlock(BLOCK_WALL,"down")
- turnRight()
- go("forward",true)
- if lamp then
- replaceBlock(BLOCK_LAMP,"down")
- else
- replaceBlock(BLOCK_WALL,"down")
- end
- go("forward",true)
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- go("up",true)
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- go("down",true)
- go("down",true)
- turnRight()
- turnRight()
- go("down",true)
- go("forward",true)
- replaceBlock(BLOCK_WALLSTAIRS,"up")
- turnRight()
- turnRight()
- go("forward",true)
- turnRight()
- turnRight()
- go("up",true)
- go("up",true)
- go("up",true)
- go("forward",true)
- go("forward",true)
- go("forward",true)
- go("forward",true)
- go("forward",true)
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- turnRight()
- turnRight()
- go("down",true)
- go("down",true)
- go("down",true)
- go("forward",true)
- replaceBlock(BLOCK_WALLSTAIRS,"up")
- turnLeft()
- turnLeft()
- go("forward",true)
- go("up",true)
- turnRight()
- go("forward",true)
- go("up",true)
- turnRight()
- go("forward",true)
- go("forward",true)
- go("forward",true)
- turnLeft()
- end
- function tun_case(lamp)
- if lamp then
- replaceBlock(BLOCK_LAMP,"down")
- else
- replaceBlock(BLOCK_WALL,"down")
- end
- turnRight()
- go("forward",true)
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- go("up",true)
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- go("down",true)
- go("down",true)
- turnRight()
- turnRight()
- go("down",true)
- go("forward",true)
- replaceBlock(BLOCK_WALLSTAIRS,"up")
- turnRight()
- turnRight()
- go("forward",true)
- turnRight()
- turnRight()
- go("up",true)
- go("up",true)
- go("up",true)
- go("forward",true)
- go("up",true)
- replaceBlock(BLOCK_GLASS,"down")
- go("up",true)
- replaceBlock(BLOCK_WALL,"down")
- go("up",true)
- replaceBlock(BLOCK_WALLSTAIRS,"down")
- go("forward",true)
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- replaceBlock(BLOCK_GLASS,"down")
- go("forward",true)
- replaceBlock(BLOCK_WALL,"down")
- go("forward",true)
- go("forward",true)
- turnRight()
- turnRight()
- go("down",true)
- replaceBlock(BLOCK_WALLSTAIRS,"forward")
- go("down",true)
- replaceBlock(BLOCK_WALL,"forward")
- go("down",true)
- replaceBlock(BLOCK_GLASS,"forward")
- go("down",true)
- replaceBlock(BLOCK_WALL,"forward")
- go("down",true)
- go("forward",true)
- replaceBlock(BLOCK_WALL,"forward")
- turnRight()
- turnRight()
- go("forward",true)
- turnRight()
- turnRight()
- go("down",true)
- go("forward",true)
- replaceBlock(BLOCK_WALLSTAIRS,"up")
- turnLeft()
- turnLeft()
- go("forward",true)
- go("up",true)
- turnRight()
- go("forward",true)
- go("up",true)
- turnRight()
- go("forward",true)
- go("forward",true)
- go("forward",true)
- turnLeft()
- end
- function tun_pillar()
- for i=1,minimumDown,1 do
- currentY = currentY - 1
- go("down",true)
- end
- repeat
- currentY = currentY - 1
- go("down",true)
- until not isEmpty("down")
- while currentY < 0 do
- go("up",true)
- replaceBlock(BLOCK_PILLAR,"down")
- turnLeft()
- go("forward",true)
- replaceBlock(BLOCK_PILLAR,"down")
- turnLeft()
- turnLeft()
- go("forward",true)
- go("forward",true)
- replaceBlock(BLOCK_PILLAR,"down")
- turnRight()
- turnRight()
- go("forward",true)
- turnRight()
- currentY = currentY + 1
- end
- turnRight()
- go("forward",true)
- turnLeft()
- go("forward",true)
- turnLeft()
- replaceBlock(BLOCK_PILLAR,"down")
- go("forward",true)
- replaceBlock(BLOCK_PILLAR,"down")
- go("forward",true)
- replaceBlock(BLOCK_PILLAR,"down")
- turnLeft()
- go("forward",true)
- go("forward",true)
- turnLeft()
- replaceBlock(BLOCK_PILLAR,"down")
- go("forward",true)
- replaceBlock(BLOCK_PILLAR,"down")
- go("forward",true)
- replaceBlock(BLOCK_PILLAR,"down")
- turnLeft()
- turnLeft()
- go("forward",true)
- turnRight()
- go("forward",true)
- end
- function village_pillar()
- for i=1,minimumDown,1 do
- currentY = currentY - 1
- go("down",true)
- end
- repeat
- currentY = currentY - 1
- go("down",true)
- until not isEmpty("down")
- while currentY < 0 do
- go("up",true)
- replaceBlock(BLOCK_PILLAR,"down")
- currentY = currentY + 1
- end
- turnRight()
- go("forward",true)
- go("down",true)
- go("down",true)
- turnLeft()
- go("forward",true)
- turnLeft()
- go("forward",true)
- turnLeft()
- replaceBlock(BLOCK_WALL,"up")
- go("down",true)
- replaceBlock(BLOCK_WALLSTAIRS,"up")
- turnLeft()
- go("forward",true)
- go("up",true)
- turnRight()
- go("forward",true)
- go("forward",true)
- turnRight()
- go("forward",true)
- turnRight()
- replaceBlock(BLOCK_WALL,"up")
- go("down",true)
- replaceBlock(BLOCK_WALLSTAIRS,"up")
- turnRight()
- go("forward",true)
- turnLeft()
- go("forward",true)
- go("up",true)
- go("up",true)
- go("up",true)
- turnLeft()
- go("forward",true)
- turnRight()
- end
- function replaceBlock(id, direction)
- if not isEmpty(direction) then
- dig(direction)
- end
- placeBlock(id, direction)
- end
- function placeBlock(id, direction)
- local slotNum = GetBlock(id)
- local result = false
- while slotNum == 0 do
- selectSlot(id)
- awaitFix("I have no blocks as in selected slot")
- slotNum = GetBlock(id)
- end
- ensureFuel()
- selectSlot(slotNum)
- if direction=="up" then
- result = turtle.placeUp()
- elseif direction=="down" then
- result = turtle.placeDown()
- else
- result = turtle.place()
- end
- return result
- end
- function GetBlock(id)
- if turtle.getItemCount(id) > 1 then
- return id
- else
- selectSlot(id)
- for i=minSlot,maxSlot,1 do
- if turtle.getItemCount(i) > 0 then
- if turtle.compareTo(i) then
- return i
- end
- end
- end
- end
- return 0
- end
- function ___initBlocks()
- if initBlocksCount==0 then
- selectSlot(1)
- while turtle.getItemCount(currentlySelectedSlot)~=0 do
- initBlocksCount = initBlocksCount + 1
- selectSlot(currentlySelectedSlot + 1)
- end
- end
- if initBlocksCount==0 then
- return false
- end
- minSlot = initBlocksCount + 1
- maxSlot = 15
- end
- function initBlocks()
- initBlocksCount = 4
- minSlot = initBlocksCount
- maxSlot = 15
- end
- function isEmpty(side)
- local result = false
- if side=="up" then
- result = not turtle.detectUp()
- elseif side=="down" then
- result = not turtle.detectDown()
- else
- result = not turtle.detect()
- end
- return result
- end
- --select slot
- function selectSlot(x)
- if x<1 then
- x = 1
- elseif x > 16 then
- x = 16
- end
- turtle.select(x)
- currentlySelectedSlot = x
- end
- --Check fuel, refueling from inventory special slot or not
- function ensureFuel()
- -- Determine whether a refuel is required
- local fuelLevel = turtle.getFuelLevel()
- if (fuelLevel ~= "unlimited") then
- if (fuelLevel < fuelLevelToRefuelAt) then
- -- Need to refuel
- selectSlot(16)
- local fuelItems = turtle.getItemCount(16)
- -- Do we need to impact the emergency fuel to continue? (always
- -- keep one fuel item in slot 16)
- if (fuelItems == 0) then
- awaitFix("I want more fuel!")
- elseif (fuelItems == 1) then
- --load fuel from another slot
- local fueled = false
- local fromSlot = initBlocksCount + 1
- for i=fromSlot,15,1 do
- selectSlot(i)
- if (turtle.compareTo(16)) then
- print("Refueling from not fuel slot: "+i)
- turtle.refuel(1)
- turtle.transferTo(16)
- fueled = true
- end
- end
- if not fueled then
- print("completely out of fuel!")
- end
- else --fuelItems > 1
- print("Time to refueling!")
- turtle.refuel(1)
- end
- end
- end
- end
- --turns
- function turnFacing(facing)
- while facing ~= currentFacing do
- turnRight()
- end
- end
- --turns
- function turnLeft()
- ensureFuel()
- turtle.turnLeft()
- currentFacing = currentFacing - 1
- if currentFacing < 0 then
- currentFacing = 3
- end
- end
- --turns
- function turnRight()
- ensureFuel()
- turtle.turnRight()
- currentFacing = currentFacing + 1
- if currentFacing >= 4 then
- currentFacing = 0
- end
- end
- -- BOOL dig direction with retries
- function dig(direction)
- local result = false
- local tries = 0
- selectSlot(1)
- while (not result) and (tries < 10) do
- ensureFuel()
- if direction=="up" then
- result = turtle.digUp()
- elseif direction=="down" then
- result = turtle.digDown()
- else
- result = turtle.dig()
- end
- tries = tries + 1
- end
- if not result then
- print ("can't dig in "..direction)
- end
- return result
- end
- function goX(blocks,direction,allowDig)
- for i=1,blocks do
- go(direction,allowDig)
- end
- end
- --go direction with breaking blocks and refueling
- function go(direction, allowDig)
- if direction=="right" then
- turnRight()
- direction="forward"
- end
- if direction=="left" then
- turnLeft()
- direction="forward"
- end
- if direction=="forward" then
- while turtle.detect() do
- if allowDig then
- ensureFuel()
- turtle.dig()
- else
- awaitFix("can't forward")
- end
- end
- while not turtle.forward() do
- ensureFuel()
- turtle.dig()
- end
- end
- if direction=="up" then
- while turtle.detectUp() do
- if allowDig then
- ensureFuel()
- turtle.digUp()
- else
- awaitFix("can't up")
- end
- end
- while not turtle.up() do
- ensureFuel()
- turtle.digUp()
- end
- end
- if direction=="down" then
- while turtle.detectDown() do
- if allowDig then
- ensureFuel()
- turtle.digDown()
- else
- awaitFix("can't down")
- end
- end
- while not turtle.down() do
- ensureFuel()
- turtle.digDown()
- end
- end
- end
- --wait for user action
- function awaitFix(message)
- print("I have problem: " .. message)
- print("Please fix it and press Enter to continue")
- io.read()
- end
- main() --execute ALL
Advertisement
Add Comment
Please, Sign In to add comment