View difference between Paste ID: HuuCNQ7B and FAsxpDXB
SHOW: | | - or go back to the newest paste.
1
-- goto.lua
2
3
--(( Variables ))--
4
5
x,y,z = 0,0,0
6
dir = 0
7
local init = false
8
9
--(( Methods ))--
10
11
function up()
12
  while not turtle.up() do
13
    if not turtle.attackUp() then
14
      print("ERROR: Failed to move up!")
15
      return false
16
    end
17
    sleep(1)
18
  end
19
  y = y + 1
20
  return true
21
end
22
23
function down()
24
  while not turlte.down() do
25
    if not turtle.attackDown() then
26
      print("ERROR: Failed to move down!")
27
      return false
28
    end
29
    sleep(1)
30
  end
31
  y = y - 1
32
  return true
33
end
34
35
function forward()
36
  while not turtle.forward() do
37
    if not turtle.attack() then
38
      print("ERROR: Failed to go forwards!")
39
      return false
40
    end
41
    sleep(1)
42
  end
43
  if dir == 0 then z = z + 1
44
  elseif dir == 1 then x = x - 1
45
  elseif dir == 2 then z = z - 1
46
  elseif dir == 3 then x = x + 1 end
47
  return true
48
end
49
50
function right()
51
  turtle.turnRight()
52
  dir = dir + 1
53
  if dir == 4 then dir = 0 end
54
end
55
56
function left()
57
  turtle.turnLeft()
58
  dir = dir - 1
59
  if dir == -1 then dir = 3 end
60
end
61
62
local function deltaDir(a,b)
63
  local delta = b - a
64
  if delta == 3 then return -1 end
65
  if delta == -3 then return 1 end
66
  return delta
67
end
68
69
function turn(towards)
70
  local delta = deltaDir(dir,towards)
71
  for x = 1,delta,1 do right() end
72
  for x = -1,delta,-1 do left() end
73
end
74
75
local function whereAmI()
76
  rednet.open("right")
77
  -- find start pos
78
  local nx,_,nz = gps.locate(.5)
79
  if not nx then error("ERROR: Failed to get my location!",0) end
80
  
81
  for count = 1,4 do
82
    if forward() then break
83
    else right() end
84
    if count == 4 then error("ERROR: Failed to get my direction!",0) end
85
  end
86
  
87
  x,y,z = gps.locate(.5)
88
  if not x then error("ERROR: Failed to get my location!",0) end
89
  
90
  if nx < x then dir = 3
91
  elseif nx > x then dir = 1
92
  elseif nz < z then dir = 0
93
  elseif nz > z then dir = 2
94
  else
95
    error("ERROR: Invalid direction!",0)
96
  end
97
  
98
  rednet.close("right")
99
  init = true
100
end
101
102
function goto(nx,ny,nz)
103
  if not init then whereAmI() end
104
  
105
  while x~=nx or y~=ny or z~=nz do
106
    print("Heading to x"..nx.." y"..ny.." z"..nz.."!")
107
    
108
    -- X axis
109
    if nx > x then turn(3) end
110
    if nx < x then turn(1) end
111
    repeat until nx == x or not forward()
112
    
113
    -- Z axis
114
    if nz > z then turn(0) end
115
    if nz < z then turn(2) end
116
    repeat until nz == z or not forward()
117
    
118
    -- Up
119
    while ny > y do up() end
120
    
121
    -- Down
122
    while ny < y do down() end
123
  end
124
end
125
126
-- EOF