Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------
- -- Warning ! WORK IN PROGRESS ! --
- -- Has not been fully tested ! --
- ----------------------------------
- --
- -- Copyright 2013 MigukNamja
- --
- -- MigukNamja's Platform Builder
- --
- --
- -- Globals
- gForward = 0 -- Set by program arg
- gRight = 0 -- Set by program arg
- --
- -- Place a platform block
- --
- gSlot = 1
- gTotalPlaced = 0
- function place()
- while turtle.getItemCount(gSlot) == 0 do
- gSlot = gSlot + 1
- end
- turtle.select(gSlot)
- if not turtle.compareDown() then -- don't bother digging and placing if platform already there
- turtle.digDown()
- turtle.placeDown()
- gTotalPlaced = gTotalPlaced + 1
- end
- end
- --
- -- Return to the starting position and orientation
- --
- function returnToStart()
- -- odd number of columns will have turtle at the far end (top) of the platform
- -- so, turtle should return to bottom row
- if gRight % 2 == 1 then
- local y = gForward - 1
- turtle.turnLeft()
- turtle.turnLeft()
- assert( moveForward( y ) == y )
- end
- local x = gRight - 1
- turtle.turnRight()
- assert( moveForward( x ) == x )
- turtle.turnRight()
- end
- -- Move forward 'blocks' number of blocks, digging when necessary
- -- Hitting bedrock or something undiggable like an entity (creature) will
- -- prevent forward movement
- --
- -- Return actual blocks moved forward
- function moveForward(blocks)
- if blocks < 0 then return moveBack(0-blocks) end
- if blocks == 0 then return 0 end
- local moved = 0
- while moved < blocks do
- if turtle.forward() then -- air, nothing to dig, but go down
- moved = moved + 1
- elseif turtle.dig() then -- had to dig to go forwards
- if not turtle.forward() then
- local attempts = 0
- while turtle.dig() and attempts < 20 do
- attempts = attempts + 1 -- take care water won't cause infinite digging
- sleep(0.25) -- might have to chew through a stack of sand or gravel
- end
- if turtle.forward() then
- moved = moved + 1
- end
- else
- moved = moved + 1
- end
- else -- undiggable, give up (for now)
- return moved
- end
- sleep(0.25) -- sleep to avoid yielding error
- end
- return moved
- end
- -- Move backwards, digging when necessary
- function moveBack(blocks)
- if blocks < 0 then return moveForward(0-blocks) end
- if blocks == 0 then return 0 end
- local moved = 0
- turtle.turnRight()
- turtle.turnRight()
- moved = moveForward(blocks)
- turtle.turnRight()
- turtle.turnRight()
- return moved
- end
- function usage()
- print( "Usage: platform <blocks_forward> <blocks_right>" )
- print( " Both values must be a least 1" )
- end
- function parseArgs(args)
- local validSyntax = false
- if #args ~= 2 then
- return false
- elseif args[1] == "help" then
- return false
- elseif args[1] == nil or args[2] == nil then
- return false
- else
- gForward = tonumber( args[1] )
- gRight = tonumber( args[2] )
- if gForward <= 0 or gRight <= 0 then
- validSyntax = false
- else
- validSyntax = true
- end
- end
- return validSyntax
- end
- local tArgs = { ... }
- validSyntax = parseArgs(tArgs)
- if not validSyntax then
- usage()
- return
- end
- print( "Making platform ", gForward, " blocks forward and ", gRight, " blocks rightwards." )
- local x = 1
- local y = 1
- turtle.select(1)
- for x=1,gRight do
- for y=1,gForward do
- place()
- if y < gForward then
- assert( moveForward(1) == 1 )
- end
- end
- if x < gRight then
- if (x % 2) == 1 then -- odd numbered row, turn rightwards and get lined up for next row
- turtle.turnRight()
- assert( moveForward(1) == 1 )
- turtle.turnRight()
- else -- even numbered row, turn leftwards and get lined up for next row
- turtle.turnLeft()
- assert( moveForward(1) == 1 )
- turtle.turnLeft()
- end
- end
- end
- --returnToStart()
- print( "I have finished the assigned task, my master !" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement