Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pos = {
- x=0,
- y=0,
- z=0,
- facing = 0 --clockwise
- }
- Ydir = true
- function Y()
- Ydir = not Ydir
- end
- wasPlacedUp = 0
- wasPlacedDown = 0
- function sendAlarm(text)
- term.clear()
- term.setCursorPos(1,1)
- print(text)
- print("try to resolve the issue and press Enter to continue")
- read()
- term.clear()
- term.setCursorPos(1,1)
- end
- function sortInv()
- for i=1,16 do
- itemCount = turtle.getItemCount(i)
- if turtle.getItemSpace(i) ~= 0 then
- for j=16,i+1,-1 do
- turtle.select(i)
- if turtle.getItemCount(j)~=0 and (turtle.getItemCount(i) == 0 or turtle.compareTo(j)) then
- turtle.select(j)
- turtle.transferTo(i)
- if turtle.getItemSpace(i) == 0 then break end
- end
- end
- end
- if turtle.getItemCount(i) == 0 then break end
- end
- end
- function cantPlace(n)--side, 0=up, 1=down, 2=mid
- side = ""
- isBlocked=false
- if n==0 then isBlocked = turtle.detectUp()
- side = "up"
- elseif n==1 then isBlocked = turtle.detectDown()
- side = "down"
- else isBlocked = turtle.detect()
- side = "front" end
- if isBlocked == true then
- sendAlarm("I cant place the block in " .. side .. " direction!")
- else
- sortInv()
- turtle.select(1)
- if turtle.getItemCount(1) == 0 then toChest() end
- end
- end
- function forward(n)
- for i=1,n do
- turtle.forward()
- if pos.facing == 0 then pos.x = pos.x + 1
- elseif pos.facing == 1 then pos.y = pos.y + 1
- elseif pos.facing == 2 then pos.x = pos.x - 1
- else pos.y = pos.y - 1 end
- end
- end
- function up(n)
- print("up("..n..")")
- for i=1,n do
- print("up()")
- turtle.up()
- pos.z = pos.z + 1
- end
- end
- function down(n)
- for i=1,n do
- turtle.down()
- pos.z = pos.z - 1
- end
- end
- function turnLeft()
- turtle.turnLeft()
- pos.facing = pos.facing - 1
- if pos.facing < 0 then pos.facing = 3 end
- end
- function turnRight()
- turtle.turnRight()
- pos.facing = pos.facing + 1
- if pos.facing > 3 then pos.facing = 0 end
- end
- function placeUp()
- if turtle.placeUp() == false then cantPlace(0) turtle.placeUp() end
- end
- function placeDown()
- if turtle.placeDown() == false then cantPlace(1) turtle.placeDown() end
- end
- function place()
- if turtle.place() == false then cantPlace(2) turtle.place() end
- end
- function placeMid()
- forward(1)
- turnLeft()
- turnLeft()
- place()
- end
- function setFacing(n)
- k = n - pos.facing
- if k ~= 0 then
- if k < 0 then k = k + 4 end
- if k < 3 then
- while n ~= pos.facing do turnRight() end
- else
- while n ~= pos.facing do turnLeft() end
- end
- end
- end
- function setFacingY()
- if Ydir == true then setFacing(1) else setFacing(3) end
- end
- function setFacingNotY()
- if Ydir == false then setFacing(1) else setFacing(3) end
- end
- function toChest()
- oldPos = pos
- if wasPlacedUp == 1 and wasPlacedMid == 0 then setFacingY() forward(1) end
- up(2)
- setFacing(3)
- forward(pos.y)
- setFacing(2)
- forward(pos.x + 1)
- down(pos.z - 1)
- useChest()
- up(oldPos.z+1)
- setFacing(1)
- if wasPlacedUp == 1 and wasPlacedMid == 0 then
- if Ydir == true then forward(oldPos.y+1)
- else forward(oldPos.y-1) end
- else forward(oldPos.y) end
- setFacing(4)
- forward(oldPos.x+1)
- down(2)
- if wasPlacedUp == 1 and wasPlacedMid == 0 then setFacingNotY() forward(1) end
- setFacing(oldPos.facing)
- end
- useChest()
- end
- function g(x,y,z,_placeUp,_placeDown,_placeMid)
- --print("need to go["..x..","..y..","..z.."], im in ["..pos.x..","..pos.y..","..pos.z.."]")
- if pos.z ~= z then
- if wasPlacedUp == 1 and wasPlacedMid == 0 then forward(1) end
- up(z-pos.z)
- end
- if pos.x ~= x then
- if pos.x < x then setFacing(0) else setFacing(2) end
- forward(math.abs(x - pos.x))
- end
- if pos.y ~= y then
- if pos.y < y then setFacing(1) else setFacing(3) end
- forward(math.abs(y - pos.y))
- end
- setFacingY()
- if _placeUp == 1 then placeUp() end
- if _placeDown == 1 then placeDown() end
- if _placeMid == 1 then placeMid() end
- wasPlacedUp = _placeUp
- wasPlacedMid = _placeMid
- end
- --CODE
- placeUp()
Advertisement
Add Comment
Please, Sign In to add comment