View difference between Paste ID: ehbX2LcT and DRPYudWC
SHOW: | | - or go back to the newest paste.
1
local tArgs = {...}
2
xDest, yDest, zDest = tArgs[1], tArgs[2], tArgs[3]
3
4
face={}
5
face[0]="north"
6-
    print("Correct usage: goTo <X> <Y> <Z>")
6+
face[1]="west"
7-
    print("NOTE: Requires GPS API")
7+
face[2]="south"
8
face[3]="east"
9
10
for k,v in pairs(face) do
11
  if tArgs[4] == v then
12
    dirDest = k
13
  elseif tonumber(tArgs[4]) ~= nil then
14
    dirDest = tArgs[4]
15
  else
16
    print("Direction Destination Required")
17
  end
18
end
19
20
function correctUsage()
21
  if #tArgs ~= 3 then
22
    print("Correct usage: goTo <X> <Y> <Z> <Dir>")
23
  end
24
local a,b,c = gps.locate(5,true)
25
  if a==nil then
26
    print("GPS Array out of range")
27
  end
28
end
29
30
function direction()
31
  xPos, yPos, zPos = gps.locate()
32
  turtle.forward()
33
  xPos2, yPos2, zPos2 = gps.locate()
34
  turtle.back()
35
  if xPos ~= xPos2 then
36
    if xPos > xPos2 then
37
      fDir = 1 
38
    else
39
      fDir = 3
40
    end
41
  else
42
    if zPos > zPos2 then
43
      fDir = 2
44
    else
45
      fDir = 0
46
    end
47
  end
48
  print(fDir)
49
end
50
51
function forward()
52
  turtle.forward()
53
  if fDir == 0 then zPos = zPos + 1 end
54
  if fDir == 2 then zPos = zPos - 1 end
55
  if fDir == 1 then xPos = xPos - 1 end
56
  if fDir == 3 then xPos = xPos + 1 end
57
end
58
59
function left()
60
  turtle.turnLeft()
61
  fDir = fDir - 1
62
  if fDir < 0 then fDir = 3 end
63
end
64
65
function right()
66
  turtle.turnRight()
67
  fDir = fDir + 1
68
  if fDir > 3 then fDir = 0 end
69
end
70
71
function goToX()
72
  if xDest < xPos then
73
    while fDir ~= 1 do
74
      left()
75
    end
76
  elseif xDest > xPos then
77
    while fDir ~= 3 do
78
      left()
79
    end
80
  end
81
  while xPos ~= xDest do
82
    turtle.dig()
83
    forward()
84
  end
85
end
86
87
function goToZ()
88
  if zDest < zPos then
89
    while fDir ~= 2 do
90
      left()
91
    end
92
  elseif zDest > zPos then
93
    while fDir ~= 0 do
94
      left()
95
    end
96
  end
97
  while zPos ~= zDest do
98
    turtle.dig()
99
    forward()
100
  end
101
end
102
103
function goToY()
104-
goToY()
104+
105
    while yDest ~= yPos do
106
      turtle.digDown()
107
      turtle.down()
108
      yPos = yPos - 1
109
    end
110
  elseif yDest > yPos then
111
    while yDest ~= yPos do
112
      turtle.digUp()
113
      turtle.Up()
114
      yPos = yPos + 1
115
    end
116
  end
117
end
118
119
function goToDir()
120
  while dirDest ~= fDir do
121
    turtle.turnRight()
122
  end
123
end
124
125
correctUsage()
126
direction()
127
goToX()
128
goToZ()
129
goToY()
130
direction()
131
goToDir()