Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local robot = require('robot')
- local component = require('component')
- local rt = component.robot
- local tTurn = {
- ['N'] = {['E'] = 1, ['W'] = 2, ['S'] = 3},
- ['E'] = {['S'] = 1, ['N'] = 2, ['W'] = 3},
- ['S'] = {['W'] = 1, ['E'] = 2, ['N'] = 3},
- ['W'] = {['N'] = 1, ['S'] = 2, ['E'] = 3}
- }
- local function turn(a, b)
- if tTurn[a][b] == 1 then
- robot.turnRight()
- elseif tTurn[a][b] == 2 then
- robot.turnLeft()
- elseif tTurn[a][b] == 3 then
- robot.turnAround()
- end
- end
- local function pos()
- return {robot.getPos()}
- end
- local function move(d, c)
- for i = 1, c do
- if rt.detect(d) then
- if rt.swing(d) then
- os.sleep(0.1)
- while rt.detect(d) do
- rt.swing(d)
- end
- else
- return false
- end
- end
- os.sleep(0.1)
- if d == 3 then
- robot.forward()
- elseif d == 1 then
- robot.up()
- elseif d == 0 then
- robot.down()
- end
- end
- end
- local function goto_t(x, y, z)
- if pos()[1] > x then
- turn(pos()[4], 'W')
- move(3, pos()[1]-x)
- elseif pos()[1] < x then
- turn(pos()[4], 'E')
- move(3, x-pos()[1])
- end
- if pos()[2] > y then
- move(0, pos()[2]-y)
- elseif pos()[2] < y then
- move(1, y-pos()[2])
- end
- if pos()[3] > z then
- turn(pos()[4], 'N')
- move(3, pos()[3]-z)
- elseif pos()[3] < z then
- turn(pos()[4], 'S')
- move(3, z-pos()[3])
- end
- end
- local tArgs = {...}
- if #tArgs == 3 then
- for o = 1, 3 do
- tArgs[o] = tonumber(tArgs[o])
- end
- else
- print('Usage: goto <x> <y> <z>')
- os.exit()
- end
- goto_t(tArgs[1], tArgs[2], tArgs[3])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement