Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local actualPosition = {}
- local homePos = {}
- function goTo(x3,y3,z3,facing3)
- if actualPosition["y"] == nil then
- print("getting actual position")
- getActualPosition()
- end
- print(actualPosition["x"])
- print(x3)
- while actualPosition["y"]> y3 do
- forceMoveDown()
- end
- while actualPosition["y"]< y3 do
- forceMoveUp()
- end
- while actualPosition["x"]< x3 do
- turnToFacing(2)
- forceMoveForward()
- end
- while actualPosition["x"]> x3 do
- turnToFacing(4)
- forceMoveForward()
- end
- while actualPosition["z"]< z3 do
- turnToFacing(3)
- forceMoveForward()
- end
- while actualPosition["z"]> z3 do
- turnToFacing(1)
- forceMoveForward()
- end
- turnToFacing(facing3)
- end
- function forceMoveForward()
- turtle.dig()
- fuelCheck()
- turtle.forward()
- if actualPosition["facing"]==1 then
- actualPosition["z"]=actualPosition["z"]-1
- elseif actualPosition["facing"]==2 then
- actualPosition["x"]=actualPosition["x"]+1
- elseif actualPosition["facing"]==3 then
- actualPosition["z"]=actualPosition["z"]+1
- elseif actualPosition["facing"]==4 then
- actualPosition["x"]=actualPosition["x"]-1
- end
- end
- function moveTurnLeft()
- turtle.turnLeft()
- actualPosition["facing"]=actualPosition["facing"]-1
- if actualPosition["facing"]==0 then
- actualPosition["facing"]=4
- end
- end
- function moveTurnRight()
- turtle.turnRight()
- actualPosition["facing"]=actualPosition["facing"]+1
- if actualPosition["facing"]==5 then
- actualPosition["facing"]=1
- end
- end
- function forceMoveBack()
- for i=1,2 do moveTurnLeft() end
- forceMoveForward()
- for i=1,2 do moveTurnLeft() end
- end
- function forceMoveUp()
- turtle.digUp()
- fuelCheck()
- turtle.up()
- actualPosition["y"]=actualPosition["y"]+1
- end
- function forceMoveDown()
- turtle.digDown()
- fuelCheck()
- turtle.down()
- actualPosition["y"]=actualPosition["y"]-1
- end
- function turnToFacing(facing1)
- while actualPosition["facing"]~=facing1 do
- moveTurnLeft()
- end
- end
- function getFacing()
- local x0,y0,z0 = gps.locate()
- for i = 1,2 do
- if turtle.forward() then
- local x1,y1,z1 = gps.locate()
- turtle.back()
- if x0 > x1 then
- return 4
- elseif x0 < x1 then
- return 2
- elseif z0 > z1 then
- return 1
- elseif z0 < z1 then
- return 3
- end
- elseif turtle.back() then
- x1,y1,z1 = gps.locate()
- turtle.forward()
- if x0 > x1 then
- return 2
- elseif x0 < x1 then
- return 4
- elseif z0 > z1 then
- return 3
- elseif z0 < z1 then
- return 1
- end
- else
- turtle.turnRight()
- end
- end
- print("unable to get facing")
- print("now executing force mode")
- turtle.turnLeft()
- turtle.turnLeft()
- forceMoveForward()
- local x1,y1,z1 = gps.locate()
- forceMoveBack()
- if x0 > x1 then
- return 4
- elseif x0 < x1 then
- return 2
- elseif z0 > z1 then
- return 1
- elseif z0 < z1 then
- return 3
- end
- end
- function getActualPosition()
- local x,y,z = gps.locate()
- local facing = getFacing()
- actualPosition["x"]=x
- actualPosition["y"]=y
- actualPosition["z"]=z
- actualPosition["facing"]=facing
- return actualPosition
- end
- function printActualPosition()
- print(textutils.serialize(actualPosition))
- end
- function setHomePosition()
- if fs.exists("homePosition") then
- fs.delete("homePosition")
- end
- local file = fs.open("homePosition","w")
- file.write( textutils.serialize ( getActualPosition() ) )
- file.flush()
- file.close()
- end
- function getHomePosition()
- if fs.exists("homePosition") then
- local file0 = fs.open("homePosition","r")
- local homePosition0 = {}
- homePosition0 = textutils.unserialize(file0.readAll())
- homePos = homePosition0
- print(textutils.serialize(homePosition0))
- return homePosition0
- else
- print("no home position was set ...")
- return
- end
- end
- function homePositionDialog()
- print("press any key to set current position to home position ...")
- local timer1 = os.startTimer(4)
- while true do
- local event, param = os.pullEvent()
- if event=="timer" and param==timer1 then
- print("no new home position was set ...")
- break
- end
- if event=="key" then
- setHomePosition()
- print("a new home position was set...")
- break
- end
- end
- end
- function checkIfActualPositionIsHomePosition()
- actualPosition = getActualPosition()
- homePos = getHomePosition()
- if textutils.serialize(actualPosition)==textutils.serialize(homePos) then
- print("i am at my home position")
- else
- print("i am going to my home position")
- goTo(homePos["x"], homePos["y"], homePos["z"], homePos["facing"])
- end
- end
- function fuelCheck()
- print("checking for fuel ...")
- if turtle.getFuelLevel()<10000 then
- local detail3 = turtle.getItemDetail(16)
- if detail3~=nil and detail3["name"]=="EnderStorage:enderChest" then
- turtle.digUp()
- turtle.select(16)
- turtle.placeUp()
- turtle.select(12)
- turtle.drop()
- turtle.suckUp(64)
- turtle.refuel()
- turtle.select(16)
- turtle.digUp()
- else
- print("put a fuel chest in slot 16")
- end
- end
- end
- function itemCheck()
- print("checking for items ...")
- if turtle.getItemCount()<=4 then
- local slot = turtle.getSelectedSlot()
- local detail4 = turtle.getItemDetail(slot+12)
- --print(detail4)
- if detail4~=nil and detail4["name"]=="EnderStorage:enderChest" then
- turtle.select(slot+12)
- turtle.digUp()
- turtle.placeUp()
- turtle.select(slot)
- while turtle.getItemCount(slot)<64 do
- turtle.suckUp(64-turtle.getItemCount(slot))
- sleep(1)
- end
- turtle.select(slot+12)
- turtle.digUp()
- turtle.select(slot)
- else
- print("no refill chest in slot " ..(slot+12))
- end
- end
- end
- --checkForItems()
- --fuelCheck()
Advertisement
Add Comment
Please, Sign In to add comment