View difference between Paste ID: EhBZHxvT and NU9EW3Q0
SHOW: | | - or go back to the newest paste.
1-
local cposition={0,0,0,0}
1+
os.loadAPI("utils")
2-
local home={0,0,0,0}
2+
3
local shaftmode=false
4
local positions = {}
5-
function modposition(axis, value)
5+
local current = {0,0,0,0}
6
7-
    cposition[axis] = ((cposition[axis] + value) % 4)
7+
local function modposition(axis, value)
8
  if (axis == 4) then
9-
    cposition[axis] = (cposition[axis] + value)
9+
    current[axis] = ((current[axis] + value) % 4)
10
  else
11
    current[axis] = (current[axis] + value)
12-
  setfilepos("aware/position")
12+
13
  setposition("current", current)
14
  reportpositions()
15
end
16
17
function setshaftmode(toggle)
18
  shaftmode=toggle
19
end
20
21-
    if     (cposition[4] == 0) then modposition(2,1)
21+
22-
    elseif (cposition[4] == 1) then modposition(1,-1)
22+
23-
    elseif (cposition[4] == 2) then modposition(2,-1)
23+
    if     (current[4] == 0) then modposition(2,1)
24-
    elseif (cposition[4] == 3) then modposition(1,1)
24+
    elseif (current[4] == 1) then modposition(1,-1)
25
    elseif (current[4] == 2) then modposition(2,-1)
26
    elseif (current[4] == 3) then modposition(1,1)
27
    end
28
    return true
29
  else
30
    return false
31
  end
32
end
33
34-
    if     (cposition[4] == 0) then modposition(2,-1)
34+
35-
    elseif (cposition[4] == 1) then modposition(1,1)
35+
36-
    elseif (cposition[4] == 2) then modposition(2,1)
36+
    if     (current[4] == 0) then modposition(2,-1)
37-
    elseif (cposition[4] == 3) then modposition(1,-1)
37+
    elseif (current[4] == 1) then modposition(1,1)
38
    elseif (current[4] == 2) then modposition(2,1)
39
    elseif (current[4] == 3) then modposition(1,-1)
40
    end
41
    return true
42
  else
43
    return false
44
  end
45
end
46
47
function moveup()
48
  if turtle.up() then
49
    modposition(3,1)
50
    return true
51
  else
52
    return false
53
  end
54
end
55
56
function movedown()
57
  if turtle.down() then
58
    modposition(3,-1)
59
    return true
60
  else
61
    return false
62
  end
63
end
64
65
function turnright()
66
  modposition(4,1)
67
  turtle.turnRight()
68
end
69
70
function turnleft()
71
  modposition(4,-1)
72
  turtle.turnLeft()
73
end
74-
  while cposition[4] ~= direction do
74+
75-
    diff = cposition[4] - direction
75+
76
  if (direction == nil) then
77
   return
78
  end
79
  while current[4] ~= direction do
80
    diff = current[4] - direction
81
    if ((diff == 1) or (diff == -3)) then
82
      turnleft()
83
    else
84
      turnright()
85
    end
86
  end
87
end
88
89
function digmoveforward()
90
  if turtle.detect() then
91
    turtle.dig()
92
  end
93
  if shaftmode then
94
    if turtle.detectUp() then
95
      turtle.digUp()
96
    end
97
  end
98
  if moveforward() then
99
    return true
100
  else
101
    return false
102
  end
103
end
104
105
function digmoveup()
106
  if turtle.detectUp() then
107
    turtle.digUp()
108
  end
109
  if shaftmode then
110
    if turtle.detect() then
111
      turtle.dig()
112
    end
113
  end
114
  if moveup() then
115
    return true
116
  else
117
    return false
118
  end
119
end
120
121
function digmovedown()
122
  if turtle.detectDown() then
123
    turtle.digDown()
124
  end
125
  if shaftmode then
126
    if turtle.detect() then
127
      turtle.dig()
128
    end
129
  end
130
  if movedown() then
131
    return true
132
  else
133-
  while(cposition[3] ~= tarz) do
133+
134-
    if cposition[3] < tarz then
134+
135
end
136
137
function movetoz(tarz)
138
  if (tarz == nil) then
139
   return true
140
  end
141
  while(current[3] ~= tarz) do
142
    if current[3] < tarz then
143-
  while(cposition[1] ~= tarx) do
143+
144-
    if cposition[1] < tarx then
144+
145
      digmovedown()
146
    end
147
  end
148
end
149
150
function movetox(tarx)
151
  while(current[1] ~= tarx) do
152
    if current[1] < tarx then
153
      turnto(3)
154-
  while(cposition[2] ~= tary) do
154+
155-
    if cposition[2] < tary then
155+
156
    end
157
    digmoveforward()
158
  end
159
end
160
161
function movetoy(tary)
162
  while(current[2] ~= tary) do
163
    if current[2] < tary then
164-
function moveto(tarx, tary, tarz, taro)
164+
165-
  if(cposition[3] > tarz) then
165+
166-
    movetoz(tarz)
166+
167-
    movetoy(tary)
167+
168-
    movetox(tarx)
168+
169
  end
170-
    movetox(tarx)
170+
171-
    movetoy(tary)
171+
172-
    movetoz(tarz)
172+
function moveto(targx, targy, targz, targo)
173
  if(targz == nil) then
174-
  turnto(taro)
174+
    movetox(targx)
175
    movetoy(targy)
176
  elseif(current[3] > targz) then
177
    movetoz(targz)
178-
  termwrite("going to"..textutils.serialize(destination))
178+
    movetoy(targy)
179
    movetox(targx)
180
  else
181
    movetox(targx)
182-
function gohome()
182+
    movetoy(targy)
183-
  goto(home)
183+
    movetoz(targz)
184
  end
185
  turnto(targo)
186-
function isfull()
186+
187
188
function goto(destination)
189-
      return false
189+
  utils.termwrite("going to"..textutils.serialize(destination))
190
  moveto(destination[1], destination[2], destination[3], destination[4])
191
end
192-
  return true
192+
193
function emptyslots()
194
  numslots = 0
195-
function isempty()
195+
196
    if turtle.getItemCount(i) == 0 then
197-
    if turtle.getItemCount(i) > 0 then
197+
      numslots = numslots + 1
198-
      return false
198+
199
  end
200
  return numslots
201-
  return true
201+
202
203
function dump()
204
  while (turtle.drop()) do
205
    sleep(.12)
206
  end
207
end
208
209
local function reportpositions()
210-
function setfilepos(filename, coords)
210+
  pushpos((os.getComputerID()+10000),"T",current)
211-
  file = fs.open(filename, "w")
211+
  pushpos((os.getComputerID()+100),"H",getposition("home"))
212-
    if (coords == null) then
212+
213-
      file.write(textutils.serialize(cposition))
213+
214
function pushpos(id, class, position)
215-
      file.write(textutils.serialize(coords))
215+
216
  formatted={id,class,fposition}
217-
  file:close()
217+
218
end
219
220-
function getfilepos(filename)
220+
function getaxis(axis)
221-
  file = fs.open(filename, "r")
221+
  return current[axis]
222-
  pos = textutils.unserialize(file.readLine())
222+
223-
  file:close()
223+
224-
  return pos
224+
function getposition(name)
225
  position = utils.varfromfile("aware."..name)
226
  if ( position == nil) then
227-
function termwrite(message)
227+
    return {0, 0, 0, 0}   
228-
  cx, cy = term.getCursorPos()
228+
229-
  mx, my = term.getSize()
229+
	return position
230-
  term.write(message)
230+
231-
  if(cy == my) then
231+
232-
    term.scroll(1)
232+
233-
    term.setCursorPos(1, cy)
233+
function setposition(name, coordinates)
234
  if (coordinates ~= nil) then
235-
    term.setCursorPos(1, cy + 1)
235+
    position = coordinates
236-
  end  
236+
237
    position = aware.getposition("current")
238
  end
239-
function reportpositions()
239+
  utils.vartofile(position, "aware."..name)
240-
  pushpos((os.getComputerID()+10000),"T",cposition)
240+
241-
  pushpos((os.getComputerID()+100),"H",home)
241+
242
local function init()
243
  rednet.open("right")
244
  current = getposition("current")
245
end
246
247
init()