Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not turtle then
- error("impTurtle API requires device type 'turtle'.")
- end
- local moved = {
- north = vector.new( 0, 0,-1),
- east = vector.new( 1, 0, 0),
- south = vector.new( 0, 0, 1),
- west = vector.new(-1, 0, 0),
- }
- local faceName = {
- [0] = "north",
- [1] = "east",
- [2] = "south",
- [3] = "west",
- }
- local status = {
- bearing = nil,
- position = nil,
- fuel = turtle.getFuelLevel(),
- }
- local setup = function(bearing, posX, posY, posZ)
- bearing = string.lower(bearing or "")
- if bearing == "north" or bearing == "n" then
- status.bearing = 0
- elseif bearing == "east" or bearing == "e" then
- status.bearing = 1
- elseif bearing == "south" or bearing == "s" then
- status.bearing = 2
- elseif bearing == "west" or bearing == "w" then
- status.bearing = 3
- else
- error("Bad argument #1 setting up impTurtle. Expects string direction, e.g. 'north' or 'n'.")
- end
- if type(posX) == "number" and type(posY) == "number" and type(posZ) == "number" then
- status.position = vector.new(posX,posY,posZ)
- else
- status.bearing = nil
- error("Bad arguments #2 - #4 setting up impTurtle. Expects number for start co-ordinates, e.g. 0, 0, 0.")
- end
- end
- local getStatus = function(specified)
- local tmp = status.bearing
- status.bearing = faceName[tmp]
- gotStatus = textutils.serialise(status)
- status.bearing = tmp
- return textutils.unserialise(gotStatus)
- end
- local move = function(direction,times)
- local times = tonumber(times)
- local completed = 0
- if not times then times = 1 end
- for i = 1,times do
- if status.fuel == 0 then turtle.refuel() end
- if direction == "left" then
- turtle.turnLeft()
- if status.bearing then
- status.bearing = (status.bearing-1)%4
- end
- elseif direction == "right" then
- turtle.turnRight()
- if status.bearing then
- status.bearing = (status.bearing+1)%4
- end
- elseif direction == "up" then
- if turtle.up() then
- completed = completed + 1
- if status.position then
- status.position.y = status.position.y + 1
- end
- else
- break
- end
- elseif direction == "down" then
- if turtle.down() then
- completed = completed + 1
- if status.position then
- status.position.y = status.position.y - 1
- end
- else
- break
- end
- elseif direction == "forward" then
- if turtle.forward() then
- completed = completed + 1
- if status.position and status.bearing then
- status.position = status.position + moved[faceName[status.bearing]]
- end
- else
- break
- end
- elseif direction == "back" then
- if turtle.back() then
- completed = completed + 1
- if status.position and status.bearing then
- status.position = status.position - moved[faceName[status.bearing]]
- end
- else
- break
- end
- else
- return "Invalid Direction"
- end
- status.fuel = turtle.getFuelLevel()
- end
- return completed
- end
- local go = {
- fd = function(times)
- return move("forward",times)
- end,
- bk = function(times)
- return move("back",times)
- end,
- lt = function(times)
- return move("left",times)
- end,
- rt = function(times)
- return move("right",times)
- end,
- up = function(times)
- return move("up",times)
- end,
- dn = function(times)
- return move("down",times)
- end,
- }
- return {
- left = go.lt,
- right = go.rt,
- up = go.up,
- down = go.dn,
- forward = go.fd,
- back = go.bk,
- status = getStatus,
- setup = setup,
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement