Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- An improved version of the go builtin command.
- -- Accepts a sequence of commands of the form "[repetitionNumber]<action>" where the
- -- repetitionNumber is optional and defaults to 1. Action can be
- -- f -> forward, b --> backwards, l -> move one to the left, r -> ...
- -- u -> up, d -> down
- -- tl -> turn left, tr -> turn right
- function terminate(errMsg)
- print(errMsg)
- os.sleep(5)
- os.queueEvent("terminate")
- end
- function execute(action, repetitionNumber)
- for i=1,repetitionNumber do
- if action == "f" then
- turtle.forward()
- elseif action == "b" then
- turtle.back()
- elseif action == "l" then
- if i == 1 then
- turtle.turnLeft()
- end
- turtle.forward()
- if i==repetitionNumber then
- turtle.turnRight()
- end
- elseif action == "r" then
- if i == 1 then
- turtle.turnRight()
- end
- turtle.forward()
- if i==repetitionNumber then
- turtle.turnLeft()
- end
- elseif action == "u" then
- turtle.up()
- elseif action == "d" then
- turtle.down()
- elseif action == "tl" then
- turtle.turnLeft()
- elseif action == "tr" then
- turtle.turnRight()
- else
- terminate(action .. " not a valid action. terminating...")
- end
- end
- end
- function split(command)
- local repetition = string.match(command, "%d*")
- if repetition == "" then
- repetition = 1
- end
- local action = string.match(command, "%a+")
- if action == "" then
- terminate(command .. " not a valid action. terminating...")
- end
- return tonumber(repetition), action
- end
- local args = {...}
- for i, command in ipairs(args) do
- local repetitionNumber, action = split(command)
- execute(action, repetitionNumber)
- end
Advertisement
Add Comment
Please, Sign In to add comment