View difference between Paste ID: dZUW3WH3 and sXRNjAjy
SHOW: | | - or go back to the newest paste.
1
local function getFaceTo()
2
  local pos0 = vector.new(gps.locate())
3
  turtle.forward()
4
  local pos1 = vector.new(gps.locate())
5
  local dist = pos1 - pos0
6
  turtle.back()
7
  local faceto = ""
8-
mon = peripheral.wrap("top")
8+
  if dist.x > 0 then
9-
fuel = turtle.getFuelLevel()
9+
    faceto = "west"
10
  elseif dist.x < 0 then
11-
pos0 = vector.new(gps.locate())
11+
    faceto = "east"
12-
turtle.forward()
12+
  elseif dist.z > 0 then
13-
pos1 = vector.new(gps.locate())
13+
    faceto = "north"
14-
dist = pos1 - pos0
14+
  else
15-
turtle.back()
15+
    faceto = "south"
16-
face = ""
16+
  end
17-
if dist.x > 0 then
17+
  return faceto
18-
  face = "west"
18+
19-
elseif dist.x < 0 then
19+
20-
  face = "east"
20+
local function forBack(dist)
21-
elseif dist.z > 0 then
21+
  d = dist
22-
  face = "north"
22+
  if d < 0 then
23-
else
23+
    d = -d
24-
  face = "south"
24+
  end  
25
  for i=1, d do
26
    if dist > 0 then
27-
if mon ~= nil then
27+
      turtle.forward()
28-
  mon.setTextScale(1.5)
28+
    else
29-
  mon.clear()
29+
      turtle.back()
30-
  mon.setCursorPos(1, 1)
30+
    end
31-
  mon.write("id:"..os.getComputerID())
31+
  end
32-
  mon.setCursorPos(1, 2)
32+
33-
  mon.write("fuel:"..fuel)
33+
34-
  mon.setCursorPos(1, 3)
34+
local function upDown(dist)
35-
  mon.write("pos:"..tostring(pos0))
35+
  d = dist
36-
  mon.setCursorPos(1, 4)
36+
  if d < 0 then
37-
  mon.write("faceTo:"..face)
37+
    d = -d
38
  end  
39-
print("pos:"..tostring(pos0))
39+
  for i=1, d do
40-
print("faceTo:"..face)
40+
    if dist > 0 then
41-
print("dist:"..tostring(target - pos0))
41+
      turtle.up()
42
    else
43-
43+
      turtle.down()
44
    end
45
  end
46
end
47
48
local function moveAxisX(faceto, curpos, target)
49
  local dist = target - curpos
50
  if faceto == "north" then
51
    turtle.turnLeft()
52
    forBack(dist.x)
53
    turtle.turnRight()
54
  elseif faceto == "south" then
55
    turtle.turnRight()
56
    forBack(dist.x)
57
    turtle.turnLeft()
58
  elseif faceto == "east" then
59
    turtle.turnLeft()
60
    turtle.turnLeft()
61
    forBack(dist.x)
62
    turtle.turnRight()
63
    turtle.turnRight()
64
  else
65
    forBack(dist.x)
66
  end
67
end
68
69
local function moveAxisZ(faceto, curpos, target)
70
  local dist = target - curpos
71
  if faceto == "north" then
72
    forBack(dist.z)
73
  elseif faceto == "south" then
74
    turtle.turnLeft()
75
    turtle.turnLeft()
76
    forBack(dist.z)
77
    turtle.turnRight()
78
    turtle.turnRight()
79
  elseif faceto == "east" then
80
    turtle.turnLeft()
81
    forBack(dist.z)
82
    turtle.turnRight()
83
  else
84
    turtle.turnRight()
85
    forBack(dist.z)
86
    turtle.turnLeft()
87
  end
88
end
89
90
local function moveAxisY(curpos, target)
91
  local dist = target - curpos
92
  upDown(dist.y)
93
end
94
95
local function dispStatus(pos, target, faceto)
96
  print("pos:"..tostring(pos))
97
  print("faceTo:"..faceto)
98
  print("dist:"..tostring(target - pos))
99
end
100
101
args = {...}
102
x = tonumber(args[1])
103
y = tonumber(args[2])
104
z = tonumber(args[3])
105
target = vector.new(x, y, z)
106
107
rednet.open("left")
108
curpos = vector.new(gps.locate())
109
110
faceto = getFaceTo()
111
dispStatus(curpos, target, faceto)
112
113
moveAxisZ(faceto, curpos, target)
114
moveAxisY(curpos, target)
115
moveAxisX(faceto, curpos, target)