Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dofile("bs/apis/basics")
- dofile("bs/apis/ui")
- -- general
- local posX = 0
- local posZ = 1
- local negX = 2
- local negZ = 3
- direction = { posX = posX, posZ = posZ, negX = negX, negZ = negZ }
- --pathList
- local Apath = {}
- pathList = {}
- local function addToPathList(path)
- local x = 1
- while pathList[x] ~= nil do
- x = x + 1
- end
- pathList[x] = path
- end
- local function getIndex(path)
- local x = 1
- while pathList[x] ~= nil do
- if pathList[x] == path then
- return x
- end
- x = x + 1
- end
- return nil
- end
- local function deleteFromPathList(path)
- local x = 1
- local trigger = false
- while pathList[x] ~= nil do
- if pathList[x] == path then
- trigger = true
- end
- if trigger == true then
- pathList[x] = pathList[x + 1]
- end
- x = x + 1
- end
- if trigger == true then
- return true
- else
- return false
- end
- end
- local function addToPath(action)
- local inner = {}
- inner[1] = 1
- inner[2] = action
- local x = 1
- while pathList[x] ~= nil do
- local y = 1
- while pathList[x][y] ~= nil do
- y = y + 1
- end
- pathList[x][y] = inner
- x = x + 1
- end
- end
- -- move
- local function left()
- turtle.turnLeft()
- addToPath("left")
- end
- local function right()
- turtle.turnRight()
- addToPath("right")
- end
- local function Fforward()
- if not turtle.forward() then
- print("something is obstructing the way")
- sleep(1)
- ui.clear()
- Fforward()
- end
- end
- local function forward()
- if turtle.getFuelLevel() == 0 then
- print("Out of fuel! Give me coal")
- ui.pressenter()
- basics.refuelanywhere()
- end
- if not turtle.forward() then
- ui.clear()
- sleep(1)
- Fforward()
- end
- addToPath("forward")
- end
- local function Fback()
- if not turtle.back() then
- print("something is obstucting the way")
- sleep(1)
- ui.clear()
- Fback()
- end
- end
- local function back()
- if turtle.getFuelLevel() == 0 then
- print("Out of fuel! Give me coal")
- ui.pressenter()
- basics.refuelanywhere()
- end
- if not turtle.back() then
- ui.clear()
- sleep(1)
- Fback()
- end
- addToPath("back")
- end
- local function Fup()
- if not turtle.up() then
- print("something is obstucting the way")
- sleep(1)
- ui.clear()
- Fup()
- end
- end
- local function up()
- if turtle.getFuelLevel() == 0 then
- print("Out of fuel! Give me coal")
- ui.pressenter()
- basics.refuelanywhere()
- end
- if not turtle.up() then
- ui.clear()
- sleep(1)
- Fup()
- end
- py = py + 1
- addToPath("up")
- end
- local function Fdown()
- if not turtle.down() then
- print("something is obstucting the way")
- sleep(1)
- ui.clear()
- Fdown()
- end
- end
- local function down()
- if turtle.getFuelLevel() == 0 then
- print("Out of fuel! Give me coal")
- ui.pressenter()
- basics.refuelanywhere()
- end
- if not turtle.down() then
- ui.clear()
- sleep(1)
- Fdown()
- end
- py = py - 1
- addToPath("down")
- end
- local function relativeTo(x,y,z)
- if x == nil or y == nil or z == nil then
- ui.printing("Enter x, y, z as parameters", colors.red)
- ui.pressenter()
- return false
- end
- if tonumber(x) == nil or tonumber(y) == nil or tonumber(z) == nil then
- ui.printing("One of the parameters you entered isnt a number", colors.red)
- ui.pressenter()
- return false
- end
- local FxNegative = false
- if x < 0 then
- x = math.abs(x)
- FxNegative = true
- left()
- left()
- end
- for I = 1, x do
- forward()
- end
- if y < 0 then
- y = math.abs(y)
- for I = 1, y do
- down()
- end
- else
- for I = 1, y do
- up()
- end
- end
- if z == 0 then return true end
- if z < 0 then
- if FxNegative == true then
- z = math.abs(z)
- right()
- else
- z = math.abs(z)
- left()
- end
- else
- if FxNegative == true then
- left()
- else
- right()
- end
- end
- for I = 1, z do
- forward()
- end
- end
- -- path
- local function getPos()
- local x, y, z = gps.locate()
- local Adirection = 0
- local turncounter = 0
- while not turtle.forward() do
- turtle.turnRight()
- turncounter = turncounter + 1
- if turncounter > 7 then
- print("unable to get direction")
- return nil
- end
- end
- local x2, y2, z2 = gps.locate()
- if x2 ~= x then
- if x2 > x then
- Adirection = direction.posX
- elseif x2 < x then
- Adirection = negX
- end
- else
- if z2 > z then
- Adirection = posZ
- elseif z2 < z then
- Adirection = negZ
- end
- end
- turtle.back()
- for I = 1, turncounter do
- if turncounter == 4 then
- break
- end
- turtle.turnLeft()
- if Adirection == 0 then
- Adirection = 3
- else
- Adirection = Adirection - 1
- end
- end
- if x < 0 then x = x + 1 end
- if z < 0 then z = z + 1 end
- local coordinates = {x, y, z, Adirection}
- return coordinates
- end
- -- bs.path.follow({{2, "back"}, {2, "left"}, {1, "forward"}, {1, "right"}, {2, "back"}})
- -- bs.path.follow(bs.path.inverse({{2, "back"}, {2, "left"}, {1, "forward"}, {1, "right"}, {2, "back"}}))
- local function followStatement(statement)
- if statement == "forward" then
- forward()
- elseif statement == "back" then
- back()
- elseif statement == "up" then
- up()
- elseif statement == "down" then
- down()
- elseif statement == "left" then
- left()
- elseif statement == "right" then
- right()
- else
- print("Error in -followStatement- invalid statement: '"..statement.."'")
- ui.pressenter()
- end
- end
- local function follow(path)
- local x = 1
- while path[x] ~= nil do
- for I= 1, path[x][1] do
- followStatement(path[x][2])
- end
- x = x + 1
- end
- end
- local function inverseStatement(statement)
- if statement == "forward" then
- return "back"
- elseif statement == "back" then
- return "forward"
- elseif statement == "up" then
- return "down"
- elseif statement == "down" then
- return "up"
- elseif statement == "left" then
- return "right"
- elseif statement == "right" then
- return "left"
- else
- print("Error in -inverseStatement- invalid statement: '"..statement.."'")
- ui.pressenter()
- end
- end
- local function inverse(path)
- local x = 1
- local MaxIndex = 0
- while path[x] ~= nil do
- MaxIndex = x
- x = x + 1
- end
- x = 1
- local returnPath = {}
- for I= MaxIndex, 1, -1 do
- local inner = {}
- inner[1] = path[I][1]
- inner[2] = inverseStatement(path[I][2])
- returnPath[x] = inner
- x = x + 1
- end
- return returnPath
- end
- -- tt = bs.path.make(bs.path.getPos(), {-11, 59, -9, 2})
- local function make(coordinates1, coordinates2)
- local tx = coordinates2[1] - coordinates1[1]
- local ty = coordinates2[2] - coordinates1[2]
- local tz = coordinates2[3] - coordinates1[3]
- local curDirection = coordinates1[4]
- local returnpath = {}
- if tx > 0 then
- if curDirection == 3 then
- returnpath[1] = {1, "right"}
- else
- returnpath[1] = {curDirection, "left"}
- end
- curDirection = 0
- elseif tx == 0 then
- returnpath[1] = {0, "right"}
- else
- if curDirection == 3 then
- returnpath[1] = {1, "left"}
- else
- returnpath[1] = {2 - curDirection, "right"}
- end
- tx = tx * -1
- curDirection = 2
- end
- returnpath[2] = {tx, "forward"}
- if ty > 0 then
- returnpath[3] = {ty, "up"}
- elseif ty == 0 then
- returnpath[3] = {0, "up"}
- else
- returnpath[3] = {ty * -1, "down"}
- end
- if tz > 0 then
- if curDirection == 0 then
- returnpath[4] = {1, "right"}
- else
- returnpath[4] = {curDirection - 1, "left"}
- end
- curDirection = 1
- elseif tz == 0 then
- returnpath[4] = {0, "forward"}
- else
- if curDirection == 0 then
- returnpath[4] = {1, "left"}
- else
- returnpath[4] = {3 - curDirection, "right"}
- end
- tz = tz * -1
- curDirection = 3
- end
- returnpath[5] = {tz, "forward"}
- if coordinates2[4] ~= curDirection then
- if coordinates2[4] > curDirection then
- returnpath[6] = {coordinates2[4] - curDirection, "right"}
- else
- returnpath[6] = {curDirection - coordinates2[4], "left"}
- end
- end
- return returnpath
- end
- --returning
- local startPos = {}
- local function setStart()
- startPos = getPos()
- Apath = {}
- if pathList[getIndex(Apath)] == nil then
- addToPathList(Apath)
- end
- end
- local function endRecording()
- if getIndex(Apath) ~= nil then
- deleteFromPathList(Apath)
- end
- end
- local function normalReturn()
- local backWay = make(getPos(), startPos)
- follow(backWay)
- setStart()
- end
- local function sameWayReturn()
- follow(inverse(Apath))
- setStart()
- end
- move = { up = up, down = down, left = left, right = right, forward = forward, back = back, relativeTo = relativeTo }
- path = { getPos = getPos, follow = follow, inverse = inverse, make = make }
- pathsList = { add = addToPathList, index = getIndex, delete = deleteFromPathList }
- returning = {sameWay = sameWayReturn, setStart = setStart, endRecording = endRecording, normalWay = normalReturn, Z = returnZ, Y = returnY, X = returnX }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement