Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...}
- local targetX = tonumber(tArgs[1])
- local targetZ = tonumber(tArgs[2])
- if not targetX and not targetZ then
- term.write("No X and Z given.")
- return
- end
- function moveForward(times)
- for i = 1,times do
- while turtle.forward() == false do
- turtle.dig()
- end
- end
- end
- function moveUp(times)
- for i = 1,times do
- while turtle.up() == false do
- turtle.digUp()
- end
- end
- end
- function moveDown(times)
- for i = 1,times do
- while turtle.down() == false do
- turtle.digDown()
- end
- end
- end
- function CheckRail(location,startingPosition)
- local requiredRails = (math.abs(location.x - startingPosition.x) + math.abs(location.z-startingPosition.z))
- return requiredRails
- end
- function FindRails()
- for i = 1,16 do
- local a = turtle.getItemDetail(i)
- if a then
- if a.name == "minecraft:rail" or a.name == "minecraft:powered_rail" then
- return i
- end
- end
- end
- return false
- end
- function neededFuel(location,startingPosition)
- local currentFuel = turtle.getFuelLevel()
- local requiredFuel = (math.abs(location.x - startingPosition.x) + math.abs(location.z-startingPosition.z))* 2
- return requiredFuel
- end
- function MoveToLocation(location)
- local startingPosition = 0
- local currentPosition = 0
- if gps.locate(2, false) then
- startingPosition = vector.new(gps.locate(2,false))
- currentPosition = startingPosition
- moveForward(1)
- currentPosition = vector.new(gps.locate(2,false))
- local difference = startingPosition - currentPosition
- local moving = ""
- if difference.x == -1 then
- moving = 3 -- east
- elseif difference.x == 1 then
- moving = 1 -- west
- end
- if difference.z == -1 then
- moving = 0 -- south
- elseif difference.z == 1 then
- moving = 2 -- north
- end
- if currentPosition.x > location.x then
- if moving == 0 then
- turtle.turnRight()
- elseif moving == 2 then
- turtle.turnLeft()
- elseif moving == 3 then
- turtle.turnRight()
- turtle.turnRight()
- end
- moving = 1
- while currentPosition.x ~= location.x do
- moveForward(1)
- turtle.digDown()
- if FindRails() then
- turtle.select(FindRails())
- turtle.placeDown()
- end
- currentPosition = vector.new(gps.locate(2, false))
- end
- else
- if moving == 0 then
- turtle.turnLeft()
- elseif moving == 2 then
- turtle.turnRight()
- elseif moving == 1 then
- turtle.turnRight()
- turtle.turnRight()
- end
- moving = 3
- while currentPosition.x ~= location.x do
- moveForward(1)
- turtle.digDown()
- if FindRails() then
- turtle.select(FindRails())
- turtle.placeDown()
- end
- currentPosition = vector.new(gps.locate(2, false))
- end
- end
- if currentPosition.z > location.z then -- needs to go north
- if moving == 0 then
- turtle.turnRight()
- turtle.turnRight()
- elseif moving == 1 then
- turtle.turnRight()
- elseif moving == 3 then
- turtle.turnLeft()
- end
- moving = 2
- while currentPosition.z ~= location.z do
- moveForward(1)
- turtle.digDown()
- if FindRails() then
- turtle.select(FindRails())
- turtle.placeDown()
- end
- currentPosition = vector.new(gps.locate(2, false))
- end
- else -- needs to go south
- if moving == 2 then
- turtle.turnRight()
- turtle.turnRight()
- elseif moving == 1 then
- turtle.turnLeft()
- elseif moving == 3 then
- turtle.turnRight()
- end
- moving = 0
- while currentPosition.z ~= location.z do
- moveForward(1)
- turtle.digDown()
- if FindRails() then
- turtle.select(FindRails())
- turtle.placeDown()
- end
- currentPosition = vector.new(gps.locate(2, false))
- end
- end
- end
- end
- if targetX and targetZ then
- local location = vector.new(targetX,0,targetZ)
- local currentPosition = vector.new(gps.locate(2, false))
- print("No. Rails: " .. CheckRail(location,currentPosition))
- if neededFuel(location,currentPosition) > turtle.getFuelLevel() then
- print("Cur Fuel: " .. turtle.getFuelLevel())
- print("Req Fuel: " .. neededFuel(location,currentPosition))
- print("Needed Fuel: " .. neededFuel(location,currentPosition) - turtle.getFuelLevel())
- return
- end
- MoveToLocation(vector.new(targetX,0,targetZ))
- end
Add Comment
Please, Sign In to add comment