123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- local pos = {x = 0, y = 0, z = 0, face = 0}
-
- local function addToPos(n)
- if pos.face == Faces.FORWARD then
- pos.z = pos.z + n
- elseif pos.face == Faces.BACK then
- pos.z = pos.z - n
- elseif pos.face == Faces.RIGHT then
- pos.x = pos.x + n
- elseif pos.face == Faces.LEFT then
- pos.x = pos.x - n
- end
- end
-
- local function addToFace(n)
- pos.face = (pos.face + n) % 4
- if pos.face < 0 then
- pos.face = 4 + pos.face
- end
- end
-
- Faces = {
- FORWARD = 0;
- RIGHT = 1;
- BACK = 2;
- LEFT = 3;
- };
-
- function getPos()
- return {
- x = pos.x;
- y = pos.y;
- z = pos.z;
- face = pos.face;
- }
- end
-
- function dig()
- while turtle.detect() do turtle.dig() end
- end
- function digUp()
- while turtle.detectUp() do turtle.digUp() end
- end
- function digDown()
- while turtle.detectDown() do turtle.digDown() end
- end
-
- function right()
- turtle.turnRight()
- addToFace(1)
- end
- function left()
- turtle.turnLeft()
- addToFace(-1)
- end
-
- function forward()
- while not turtle.forward() do end
- addToPos(1)
- end
- function back()
- while not turtle.back() do end
- addToPos(-1)
- end
- function up()
- while not turtle.up() do end
- pos.y = pos.y + 1
- end
- function down()
- while not turtle.down() do end
- pos.y = pos.y - 1
- end
-
- function digForwards()
- dig()
- forward()
- digUp()
- digDown()
- end
-
- function alterturn(i)
- if i % 2 == 1 then
- right()
- else
- left()
- end
- end
-
- function selectItem()
- if turtle.getItemCount(turtle.getSelectedSlot()) > 0 then
- return
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- return true
- end
- end
- turtle.select(1)
- return false
- end
-
- function turnTo(face)
- if pos.face == 0 and face == 3 or
- pos.face == 1 and face == 0 or
- pos.face == 2 and face == 1 or
- pos.face == 3 and face == 2 then
- left()
- else
- while pos.face ~= face do
- right()
- end
- end
- end
-
- function goTo(dest)
- -- Correct X value
- if dest.x > pos.x then
- turnTo(Faces.RIGHT)
- elseif dest.x < pos.x then
- turnTo(Faces.LEFT)
- end
- for i = 1, math.abs(dest.x - pos.x) do
- forward()
- end
-
- -- Correct Z value
- if dest.z > pos.z then
- turnTo(Faces.FORWARD)
- elseif dest.z < pos.z then
- turnTo(Faces.BACK)
- end
- for i = 1, math.abs(dest.z - pos.z) do
- forward()
- end
-
- -- Correct Y value
- while dest.y > pos.y do up() end
- while dest.y < pos.y do down() end
-
- -- Correct facing
- turnTo(dest.face)
- end
|