Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- move = {}
- require("action")
- local robot = require("robot")
- local component = require("component")
- local inventory = component.inventory_controller
- function move(movementCommands, isBreak)
- if movementCommands == nil then
- return false
- end
- local moves = {}
- local steps = {}
- --Check the commands
- if #movementCommands % 2 == 1 then
- return false
- end
- local numberOfMoves = #movementCommands / 2
- --Check the strings
- for i = 1, numberOfMoves, 2 do
- local sub = string.lower(string.sub(movementCommands, i, i))
- if sub ~= "f" and
- sub ~= "b" and
- sub ~= "l" and
- sub ~= "r" and
- sub ~= "u" and
- sub ~= "d" then
- return false
- end
- end
- --Check the numbers
- for i = 2, numberOfMoves, 2 do
- local sub = tonumber(string.sub(movementCommands, i, i))
- if sub == nil then
- return false
- end
- end
- local moveNumber = 1
- --Create the movement arrays
- for i = 1, #movementCommands, 2 do
- moves[moveNumber] = string.lower(string.sub(movementCommands, i, i))
- if moveNumber < #movementCommands / 2 then
- steps[moveNumber] = tonumber(string.sub(movementCommands, i + 1, i + 1))
- else
- steps[moveNumber] = tonumber(string.sub(movementCommands, i + 1))
- end
- moveNumber = moveNumber + 1
- end
- for i = 1, numberOfMoves do
- if moves[i] == "f" then
- moveForward(steps[i], isBreak)
- elseif moves[i] == "b" then
- moveBack(steps[i], isBreak)
- elseif moves[i] == "l" then
- moveLeft(steps[i], isBreak)
- elseif moves[i] == "r" then
- moveRight(steps[i], isBreak)
- elseif moves[i] == "u" then
- moveUp(steps[i], isBreak)
- elseif moves[i] == "d" then
- moveDown(steps[i], isBreak)
- end
- end
- return true
- end
- function moveDown(steps, isBreak)
- if steps == nil then
- steps = 1
- end
- if isBreak == nil then
- for i = 1, steps do
- while not robot.down() do
- if not swing("bottom") then
- return i - 1
- end
- end
- end
- elseif isBreak == true then
- for i = 1, steps do
- while not robot.down() do
- if not robot.swingDown() then
- return i - 1
- end
- end
- end
- else
- for i = 1, steps do
- if not robot.down() then
- return i - 1
- end
- end
- end
- end
- function moveForward(steps, isBreak)
- if steps == nil then
- steps = 1
- end
- if isBreak == nil then
- for i = 1, steps do
- while not robot.forward() do
- if not swing("forward") then
- return i - 1
- end
- end
- end
- elseif isBreak == true then
- for i = 1, steps do
- while not robot.forward() do
- if not robot.swing() then
- return i - 1
- end
- end
- end
- else
- for i = 1, steps do
- if not robot.forward() then
- return i - 1
- end
- end
- end
- return steps
- end
- function moveBack(steps, isBreak)
- robot.turnAround()
- return moveForward(steps, isBreak)
- end
- function moveLeft(steps, isBreak)
- robot.turnLeft()
- return moveForward(steps, isBreak)
- end
- function moveRight(steps, isBreak)
- robot.turnRight()
- return moveForward(steps, isBreak)
- end
- function moveUp(steps, isBreak)
- if steps == nil then
- steps = 1
- end
- if isBreak == nil then
- for i = 1, steps do
- while not robot.up() do
- if not swing("top") then
- return i - 1
- end
- end
- end
- elseif isBreak == true then
- for i = 1, steps do
- while not robot.up() do
- if not robot.swingUp() then
- return i - 1
- end
- end
- end
- else
- for i = 1, steps do
- if not robot.up() then
- return i - 1
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement