Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- robot is an abstraction of turtle giving some more advanced functions involving gps
- -- default fuel value - reset to desired value
- -- notice that if below minFuelLevel intended behavior is for the turtle to continue what it's doing
- -- it is assumed that if you get below the minFuelLevel that the program will bring the turtle to where it can get fuel
- local minFuelLevel = 80
- function setFuelLevel(fuelLevel)
- if fuelLevel >= 100000 then
- fuelLevel = 100000
- else
- minFuelLevel = fuelLevel
- end
- end
- function getMinFuelLevel()
- return minFuelLevel
- end
- -- variable that determines which slots can be refueled from
- -- by default its all slots
- local refuelSlot = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
- function setRefuelSlots(var)
- refuelSlot = var
- end
- -- return refuelSlot
- function getRefuelSlots()
- return refuelSlot
- end
- -- function to return pos as a table of x, y, and z
- function getPos()
- local x, y, z = gps.locate()
- local pos = {
- x = x,
- y = y,
- z = z
- }
- return pos
- end
- -- return r1 - r2
- function getPosDiff(r1, r2)
- local diff = {
- x=nil,
- y=nil,
- z=nil
- }
- diff.x = r1.x - r2.x
- diff.y = r1.y - r2.y
- diff.z = r1.z - r2.z
- end
- local reservesPrinted = false -- if running on reserves print only once
- -- refuels until above minFuelLevel
- function refuel()
- local refueled = false
- local outPrinted = false
- while (turtle.getFuelLevel() < minFuelLevel) do
- refueled = false
- for k,v in pairs(refuelSlot) do
- if turtle.refuel(1) then
- refueled = true
- break
- end
- end
- if not refueled then
- if turtle.getFuelLevel() > 0 then
- if not reservesPrinted then
- reservesPrinted = true
- print("Running on reserves!")
- end
- return false
- else
- if not outPrinted then
- outPrinted = true
- print("Outta fuel!")
- end
- end
- end
- end
- reservesPrinted = false
- return true
- end
- -- function that checks fuel level before moving
- -- will try to refuel from 1st slot first then others
- -- returns false if it can't move
- -- gets stuck if out of fuel
- function forward()
- refuel()
- return turtle.forward()
- end
- function back()
- refuel()
- return turtle.back()
- end
- function up()
- refuel()
- return turtle.up()
- end
- function down()
- refuel()
- return turtle.down()
- end
- -- making taking turtle functions and calling them using robot api
- function turnLeft()
- turtle.turnLeft()
- end
- function turnRight()
- turtle.turnRight()
- end
- -- gets the turtle to move 1 block horizontally
- -- returns false if it cannot move
- -- tries to move all 4 directions
- -- if it can't then it will move up
- -- if it can no longer move up it will try to move all the way down
- -- if it can no longer move down it return to its starting pos and dir and returns false
- local function moveHor()
- local moved = false
- local yDiff = 0
- -- tries to move, if it can't move it goes up
- while not moved do
- for i = 1, 4 do
- moved = forward()
- if not moved then
- turnLeft()
- else
- break
- end
- end
- if not moved and up() then
- yDiff = yDiff + 1
- else
- break
- end
- end
- -- tries to move, if it can't move it goes down
- if not moved then
- -- goes to 1 position lower than where it started
- for i = 1, yDiff+1 do
- if down() then
- yDiff = yDiff - 1
- end
- end
- -- if it was able to go to 1 position lower than it started try to move forward again
- if yDiff == -1 then
- while not moved do
- for i = 1, 4 do
- moved = forward()
- if not moved then
- turnLeft()
- else
- break
- end
- end
- if not moved and down() then
- yDiff = yDiff - 1
- else
- break
- end
- end
- end
- end
- return moved
- end
- -- function to get the turtle's direction then returns to its startPos and direction
- -- return 0 for east, 1 for north, 2 for west, and 3 for south
- -- return false for a failed attempt
- function getDir()
- local startPos = getPos()
- -- try to move
- if not moveHor() then
- print("Door stuck! Please! I beg you!")
- return false
- end
- local newPos = getPos()
- local diffPos = getPosDiff(startPos, newPos)
- if diffPos.x == 1 then
- return 2
- elseif diffPos.x == -1 then
- return 0
- elseif diffPos.z == 1 then
- return 3
- elseif diffPos.z == -1 then
- return 1
- else
- print("Something went very wrong trying to get the direction!")
- return false
- end
- end
- -- function to move the turtle to a given x, y, z coordinate
- function moveTo(x, y, z)
- local dir = getDir()
- end
Add Comment
Please, Sign In to add comment