Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local x = 0
- local y = 0
- local z = 0
- local direction = 0
- DIRECTION_SOUTH = 0
- DIRECTION_WEST = 1
- DIRECTION_NORTH = 2
- DIRECTION_EAST = 3
- local CONFIG_PATH = "robot_config"
- function load()
- if fs.exists(CONFIG_PATH) then
- handle = fs.open(CONFIG_PATH, 'r')
- x = tonumber(handle.readLine())
- y = tonumber(handle.readLine())
- z = tonumber(handle.readLine())
- direction = tonumber(handle.readLine())
- handle.close()
- else
- initGPS()
- end
- end
- function save()
- handle = fs.open(CONFIG_PATH, 'w')
- handle.writeLine(x)
- handle.writeLine(y)
- handle.writeLine(z)
- handle.writeLine(direction)
- handle.close()
- end
- function log()
- print("x: " .. x)
- print("y: " .. y)
- print("z: " .. z)
- print("dir: " .. direction)
- end
- function initGPS()
- term.write("x-pos: ")
- x = tonumber(read())
- term.write("y-pos: ")
- y = tonumber(read())
- term.write("z-pos: ")
- z = tonumber(read())
- term.write("direction (0-3)")
- direction = tonumber(read())
- save()
- end
- function getX()
- return x
- end
- function getY()
- return y
- end
- function getZ()
- return z
- end
- function getDirection()
- return direction
- end
- function moveX(count, cb)
- if count > 0 then
- turnTo(3)
- elseif count < 0 then
- turnTo(1)
- end
- forward(math.abs(count), cb)
- end
- function moveToX(c, cb)
- moveX(c - x, cb)
- end
- function moveY(count, cb)
- if count > 0 then
- up(count, cb)
- elseif count < 0 then
- down(math.abs(count), cb)
- end
- end
- function moveToY(c, cb)
- moveY(c - y, cb)
- end
- function moveZ(count, cb)
- if count > 0 then
- turnTo(0)
- elseif count < 0 then
- turnTo(2)
- end
- forward(math.abs(count), cb)
- end
- function moveToZ(c, cb)
- moveZ(c - z, cb)
- end
- function move(cx, cy, cz, cb)
- moveX(cx, cb)
- moveY(cy, cb)
- moveZ(cz, cb)
- end
- function moveTo(cx, cy, cz, cb)
- moveToX(cx, cb)
- moveToY(cy, cb)
- moveToZ(cz, cb)
- end
- function forward(count, cb)
- count = count or 1
- for i = 1, count do
- while not turtle.forward() do
- turtle.dig()
- end
- if direction == DIRECTION_SOUTH then
- z = z + 1
- elseif direction == DIRECTION_NORTH then
- z = z - 1
- elseif direction == DIRECTION_WEST then
- x = x - 1
- elseif direction == DIRECTION_EAST then
- x = x + 1
- end
- if cb then cb() end
- end
- end
- function back(count, cb)
- count = count or 1
- for i = 1, count do
- if turtle.back() then
- if direction == DIRECTION_SOUTH then
- z = z + 1
- elseif direction == DIRECTION_NORTH then
- z = z - 1
- elseif direction == DIRECTION_WEST then
- x = x - 1
- elseif direction == DIRECTION_EAST then
- x = x + 1
- end
- end
- if cb then cb() end
- end
- end
- function down(count, cb)
- count = count or 1
- for i = 1, count do
- while not turtle.down() do
- turtle.digDown()
- end
- y = y - 1
- if cb then cb() end
- end
- end
- function up(count, cb)
- count = count or 1
- for i = 1, count do
- while not turtle.up() do
- turtle.digUp()
- end
- y = y + 1
- if cb then cb() end
- end
- end
- function turnleft(count, cb)
- count = count or 1
- for i = 1, count do
- if turtle.turnLeft() then
- if direction <= 0 then
- direction = 3
- else
- direction = direction - 1
- end
- if cb then cb() end
- end
- end
- end
- function turnright(count, cb)
- count = count or 1
- for i = 1, count do
- if turtle.turnRight() then
- if direction >= 3 then
- direction = 0
- else
- direction = direction + 1
- end
- if cb then cb() end
- end
- end
- end
- function turnTo(dir, cb)
- count = direction - dir
- dif = math.abs(count)
- if dif > 2 then
- count = -(count / dif)
- end
- if count > 0 then
- turnleft(count)
- elseif count < 0 then
- turnright(-count)
- end
- if cb then cb() end
- end
- function dropAmount(slot, amount)
- if amount == nil then
- return turtle.getItemCount(slot)
- elseif amount < 0 then
- return turtle.getItemCount(slot) + amount
- else return amount
- end
- end
- function drop(slot, amount)
- amount = dropAmount(slot, amount)
- turtle.select(slot)
- turtle.drop(amount)
- end
- function dropUp(slot, amount)
- amount = dropAmount(slot, amount)
- turtle.select(slot)
- turtle.dropUp(amount)
- end
- function dropDown(slot, amount)
- amount = dropAmount(slot, amount)
- turtle.select(slot)
- turtle.dropDown(amount)
- end
- function compareItems(s1, s2)
- turtle.select(s1)
- return turtle.compareTo(s2)
- end
- function compareFrontTo(slot)
- turtle.select(slot)
- return turtle.compare()
- end
- function compareDownTo(slot)
- turtle.select(slot)
- return turtle.compareDown()
- end
- function compareUpTo(slot)
- turtle.select(slot)
- return turtle.compareUp()
- end
Advertisement
Add Comment
Please, Sign In to add comment