Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- /**
- * lzTurtle API
- *
- * Guaranteed movement functions for Turtles
- *
- * @copyright (C) LayZee 2014
- */
- --]]
- --[[ Local variables ]]--
- local VERSION = 0.2
- --[[ Local functions ]]--
- --[[
- /**
- * Verify that direction is supported in movement module
- *
- * @param string direction Direction to verify
- */
- --]]
- local verify = function(direction)
- local valid = {
- up = true,
- down = true,
- forward = true,
- back = true
- }
- assert(
- valid[direction],
- 'Invalid direction \''
- .. direction
- .. '\' passed to function move'
- )
- end
- --[[
- /**
- * Get move function
- *
- * @param string direction Direction to move
- */
- --]]
- local getMoveCallback = function(direction)
- local callback = {
- up = turtle.up,
- down = turtle.down,
- forward = turtle.forward,
- back = turtle.back
- }
- return callback[direction] or false
- end
- --[[
- /**
- * Get detect function
- *
- * @param string direction Direction to detect
- */
- --]]
- local getDetectCallback = function(direction)
- local callback = {
- up = turtle.detectUp,
- down = turtle.detectDown,
- forward = turtle.detect
- }
- return callback[direction] or false
- end
- --[[
- /**
- * Get dig function
- *
- * @param string direction Direction to dig
- */
- --]]
- local getDigCallback = function(direction)
- local callback = {
- up = turtle.digUp,
- down = turtle.digDown,
- forward = turtle.dig
- }
- return callback[direction] or false
- end
- --Define variable first to allow recursion
- local move
- --[[
- /**
- * Move turtle one step
- *
- * @param string direction Direction to move
- */
- --]]
- move = function(direction)
- verify(direction)
- local moving = getMoveCallback(direction)
- local detect = getDetectCallback(direction)
- local dig = getDigCallback(direction)
- local isSeeded = false
- local turtleId = os.getComputerID()
- --Determine which side to check for adjacent CC peripherals
- local sideToCheckForPeripherals = 'front' --Default
- if direction == 'up' then
- sideToCheckForPeripherals = 'top'
- elseif direction == 'down' then
- sideToCheckForPeripherals = 'bottom'
- end
- --Try to move
- while not moving() do
- local peripheralType = peripheral.getType(sideToCheckForPeripherals)
- --Handle CC peripherals (turtle, cable, monitors, etc.)
- while peripheralType do
- if peripheralType == 'turtle' then
- --Seed randomizer
- isSeeded = isSeeded or math.randomseed(turtleId) and true
- local waitingPeriod = math.random(1, 10)
- --Move up, wait for 1 to 10 seconds, then move back down
- move('up')
- sleep(waitingPeriod)
- move('down')
- else
- --TODO: Handle other peripherals appropriately
- break
- end
- peripheralType = peripheral.getType(sideToCheckForPeripherals)
- end
- --Check if a block is in the way
- if detect() then
- if not dig() then
- error('Unbreakable block detected')
- elseif detect() then
- --Sand/gravel falling
- sleep(0.8)
- else
- --Block broken
- end
- elseif turtle.getFuelLevel()
- ~= 'unlimited'
- and turtle.getFuelLevel() == 0
- then
- error('No more fuel')
- elseif turtle.attack() then
- --Attacking NPC/player
- end
- end
- end
- --[[ Module definition ]]--
- local module = {}
- module.VERSION = VERSION
- --[[
- /**
- * Move turtle up n times
- *
- * @param int n Number of moves
- */
- --]]
- module.up = function(n)
- for i = 0, n - 1 do
- move('up')
- end
- end
- --[[
- /**
- * Move turtle down n times
- *
- * @param int n Number of moves
- */
- --]]
- module.down = function(n)
- for i = 0, n - 1 do
- move('down')
- end
- end
- --[[
- /**
- * Move turtle forward n times
- *
- * @param int n Number of moves
- */
- --]]
- module.forward = function(n)
- for i = 0, n - 1 do
- move('forward')
- end
- end
- --[[ Aliases ]]
- module.u = function(n)
- module.up(n)
- end
- module.d = function(n)
- module.down(n)
- end
- module.f = function(n)
- module.forward(n)
- end
- return module
Advertisement
Add Comment
Please, Sign In to add comment