View difference between Paste ID: 6cGEX6F4 and aeyf2zFS
SHOW: | | - or go back to the newest paste.
1
local actualPosition = {}
2
local homePos = {}
3
4
function offsetGoTo(x4,y4,z4)
5
	local offset = getHomePosition()
6
	local x5,y5,z5 = gps.locate()
7
	if (z5-offset["z"])<2 then
8
		goTo(x5,y5,offset["z"]+2,offset["facing"])
9-
    print(actualPosition["x"])
9+
10-
    print(x3)
10+
	goTo(offset["x"]+x4,offset["y"]+y4,offset["z"]+z4,offset["facing"])
11
end
12
13
function goTo(x3,y3,z3,facing3)
14
	if actualPosition["y"] == nil then
15
    	print("getting actual position")
16
		getActualPosition()
17
	end
18
    while actualPosition["y"]> y3 do
19
    	forceMoveDown()
20
    end
21
    while actualPosition["y"]< y3 do
22-
    	turnToFacing(4)
22+
23
    end
24
    while actualPosition["x"]< x3 do
25
    	turnToFacing(2)
26-
    	turnToFacing(3)
26+
27
    end
28
	while actualPosition["x"]> x3 do
29
    	turnToFacing(2)
30
        forceMoveBack()
31
    end
32
    while actualPosition["z"]< z3 do
33
    	turnToFacing(1)
34
        forceMoveBack()
35
    end
36
    while actualPosition["z"]> z3 do
37
    	turnToFacing(1)
38-
    fuelCheck()
38+
39
    end
40
    turnToFacing(facing3)
41
end
42
43
function forceMoveForward()
44
	turtle.dig()
45
    --fuelCheck()
46
    turtle.forward()
47
    if actualPosition["facing"]==1 then
48
    	actualPosition["z"]=actualPosition["z"]-1
49
    elseif actualPosition["facing"]==2 then
50
    	actualPosition["x"]=actualPosition["x"]+1
51
    elseif actualPosition["facing"]==3 then
52
    	actualPosition["z"]=actualPosition["z"]+1
53
	elseif actualPosition["facing"]==4 then
54
    	actualPosition["x"]=actualPosition["x"]-1
55
    end
56
end
57
58
function forceMoveBack()
59
	turtle.back()
60
	if actualPosition["facing"]==1 then
61
    	actualPosition["z"]=actualPosition["z"]+1
62
    elseif actualPosition["facing"]==2 then
63
    	actualPosition["x"]=actualPosition["x"]-1
64
    elseif actualPosition["facing"]==3 then
65
    	actualPosition["z"]=actualPosition["z"]-1
66
	elseif actualPosition["facing"]==4 then
67
    	actualPosition["x"]=actualPosition["x"]+1
68-
	for i=1,2 do moveTurnLeft() end
68+
69-
    forceMoveForward()
69+
70-
    for i=1,2 do moveTurnLeft() end
70+
71
function moveTurnLeft()
72
	turtle.turnLeft()
73
    actualPosition["facing"]=actualPosition["facing"]-1
74
    if actualPosition["facing"]==0 then
75-
    fuelCheck()
75+
76
    end
77
end
78
79
function moveTurnRight()
80
	turtle.turnRight()
81
    actualPosition["facing"]=actualPosition["facing"]+1
82-
    fuelCheck()
82+
83
    	actualPosition["facing"]=1
84
    end
85
end
86
87
88-
	while actualPosition["facing"]~=facing1 do
88+
89-
    	moveTurnLeft()
89+
90
	turtle.digUp()
91
    --fuelCheck()
92
    turtle.up()
93
    actualPosition["y"]=actualPosition["y"]+1
94
end
95-
    for i = 1,2 do
95+
96-
   		if turtle.forward() then
96+
97-
     		local x1,y1,z1 = gps.locate()
97+
98-
     		turtle.back()
98+
    --fuelCheck()
99-
     		if x0 > x1 then 
99+
100-
       			return 4
100+
101-
     		elseif x0 < x1 then
101+
102-
       			return 2
102+
103-
     		elseif z0 > z1 then
103+
104-
       			return 1
104+
	local diff = facing1 - actualPosition["facing"]
105-
     		elseif z0 < z1 then
105+
	if diff < 0 then diff = diff + 4 end
106-
       			return 3
106+
	if diff==1 then 
107-
     	end
107+
		moveTurnRight()
108-
   		elseif turtle.back() then
108+
	else
109-
     		x1,y1,z1 = gps.locate()
109+
		while actualPosition["facing"]~=facing1 do
110-
     		turtle.forward()
110+
    		moveTurnLeft()
111-
     		if x0 > x1 then 
111+
112-
       			return 2
112+
113-
     		elseif x0 < x1 then
113+
114-
       			return 4
114+
115-
     		elseif z0 > z1 then
115+
116-
       			return 3
116+
117-
     		elseif z0 < z1 then
117+
	if turtle.back() then
118-
       			return 1
118+
    	x1,y1,z1 = gps.locate()
119
     	turtle.forward()
120-
   		else  
120+
     	if x0 > x1 then 
121-
     		turtle.turnRight()
121+
       		return 2
122-
   		end
122+
     	elseif x0 < x1 then
123-
 	end
123+
       		return 4
124-
    print("unable to get facing")
124+
     	elseif z0 > z1 then
125-
    print("now executing force mode")
125+
       		return 3
126-
    turtle.turnLeft()
126+
     	elseif z0 < z1 then
127-
    turtle.turnLeft()
127+
       		return 1
128-
    forceMoveForward()
128+
129-
    local x1,y1,z1 = gps.locate()
129+
 	else
130-
    forceMoveBack()
130+
    	print("unable to get facing")
131-
    if x0 > x1 then 
131+
132-
    	return 4
132+
133-
    elseif x0 < x1 then
133+
134-
    	return 2
134+
135-
    elseif z0 > z1 then
135+
136-
    	return 1
136+
137-
    elseif z0 < z1 then
137+
138-
    	return 3
138+
139
    actualPosition["z"]=z
140
    actualPosition["facing"]=facing
141
    return actualPosition
142
end
143
144
function printActualPosition()
145
	print(textutils.serialize(actualPosition))
146
end
147
148
function setHomePosition()
149
	if fs.exists("homePosition") then
150
    	fs.delete("homePosition")
151
    end
152
    local file = fs.open("homePosition","w")
153
    file.write( textutils.serialize ( getActualPosition() ) )
154
    file.flush()
155
    file.close()
156
end
157
158
function getHomePosition()
159
	if fs.exists("homePosition") then
160
    	local file0 = fs.open("homePosition","r")
161
        local homePosition0 = {}
162
        homePosition0 = textutils.unserialize(file0.readAll())
163
        homePos = homePosition0
164
        print(textutils.serialize(homePosition0))
165
        return homePosition0
166
    else
167
    	print("no home position was set ...")
168
      	return
169
    end
170
end
171
172
function homePositionDialog()
173
	print("press any key to set current position to home position ...")
174
    local timer1 = os.startTimer(4)
175
	while true do
176
    	local event, param = os.pullEvent()
177
		if event=="timer" and param==timer1 then
178
			print("no new home position was set ...")
179
			break
180
        end
181
		if event=="key" then
182
			setHomePosition()
183
            print("a new home position was set...")
184
            break
185
		end
186
     end
187
end
188
189
function checkIfActualPositionIsHomePosition()
190
	actualPosition = getActualPosition()
191
    	homePos = getHomePosition()
192
    	if textutils.serialize(actualPosition)==textutils.serialize(homePos) then
193
    		print("i am at my home position")
194
    	else
195
    		print("i am going to my home position")
196
        	goTo(homePos["x"], homePos["y"], homePos["z"], homePos["facing"])
197
    	end
198
end
199
200
function fuelCheck()
201
	print("checking for fuel ...")
202
    if turtle.getFuelLevel()<10000 then
203
    	local detail3 = turtle.getItemDetail(16)
204
		if detail3~=nil and detail3["name"]=="EnderStorage:enderChest" then
205
			
206
			turtle.digUp()
207
			turtle.select(16)
208
			turtle.placeUp()
209
			turtle.select(12)
210
			turtle.drop()
211
			turtle.suckUp(64)
212
			turtle.refuel()
213
			turtle.select(16)
214
			turtle.digUp()
215
		else
216
			print("put a fuel chest in slot 16")
217
		end
218
    end
219
end
220
221
function craftRefuel()
222
	if turtle.getFuelLevel() < 8000 then
223
		while turtle.getFuelLevel() < 10000 do
224
			turtle.select(16)
225
			redstone.setOutput("top",true)
226
			sleep(1)
227
			redstone.setOutput("top",false)
228
			sleep(1)
229
			for i=1,16 do
230
				turtle.select(i)
231
				turtle.refuel()
232
			end
233
		end
234
	end
235
end
236
237
function itemCheck()
238
	print("checking for items ...")
239
   	if turtle.getItemCount()<=4 then
240
      local slot = turtle.getSelectedSlot()
241
    	local detail4 = turtle.getItemDetail(slot+12)
242
    	--print(detail4)
243
    	if detail4~=nil and detail4["name"]=="EnderStorage:enderChest" then
244
    		turtle.select(slot+12)
245
        	turtle.digUp()
246
        	turtle.placeUp()
247
        	turtle.select(slot)
248
            	while turtle.getItemCount(slot)<64 do
249
                	turtle.suckUp(64-turtle.getItemCount(slot))
250
                    sleep(1)
251
                end
252
        	turtle.select(slot+12)
253-
--checkForItems()
253+
254
            turtle.select(slot)
255-
--fuelCheck()
255+
256
    		print("no refill chest in slot " ..(slot+12))
257
        end
258
    end
259
end