· last year · Oct 20, 2023, 01:40 PM
1-- extends default turtle api with coord tracking
2-- now working
3
4-- used for testing in IDE
5if io.open("peripheral.lua", "r") then
6 require "peripheral"
7end
8
9local slurtle = turtle
10
11-- note: wont keep track of coords unless starting dir is initialized
12local turtle = {
13 x = 0,
14 y = 0,
15 z = 0,
16 dir = 1,
17 LEFT_EQUIP = 0,
18 RIGHT_EQUIP = 0,
19 TORCH_SLOT = 0,
20}
21setmetatable(turtle, slurtle)
22slurtle.__index = slurtle
23
24local DIRECTIONS = {"north","east","south","west"}
25
26-- maps direction name to attr affected when moving forward that direction
27local DIR_TO_ATTR = {
28 function(val) turtle.z = turtle.z-val turtle.save() end,
29 function(val) turtle.x = turtle.x+val turtle.save() end,
30 function(val) turtle.z = turtle.z+val turtle.save() end,
31 function(val) turtle.x = turtle.x-val turtle.save() end,
32}
33
34local DIR_TO_INDEX = {
35 north=1,
36 east=2,
37 south=3,
38 west=4
39}
40
41local function modulus_incr(num, amount, divisor)
42 local res = num-1
43 res = res + amount
44 res = res % divisor
45 res = res + 1
46 return res
47end
48
49function turtle.reset(x,y,z,dir)
50 assert(dir, "must specify direction at least")
51 if not tonumber(dir) then
52 dir = DIR_TO_INDEX[dir]
53 end
54 turtle.dir = dir
55 turtle.x = x or 0
56 turtle.y = y or 0
57 turtle.z = z or 0
58end
59
60function turtle.getDir()
61 return DIRECTIONS[turtle.dir]
62end
63
64function turtle.xyz()
65 return turtle.x, turtle.y, turtle.z, turtle.getDir()
66end
67
68function turtle.show_xyz()
69 local x,y,z,dir = turtle.xyz()
70 print("x: "..tostring(x))
71 print("y: "..tostring(y))
72 print("z: "..tostring(z))
73 print("dir: "..tostring(dir))
74end
75
76function turtle.forward()
77 if slurtle.forward() then
78 local func = DIR_TO_ATTR[turtle.dir]
79 func(1)
80 return true
81 end
82 return false
83end
84
85function turtle.back()
86 if slurtle.back() then
87 local func = DIR_TO_ATTR[turtle.dir]
88 func(-1)
89 return true
90 end
91 return false
92end
93
94function turtle.up()
95 if slurtle.up() then
96 turtle.y = turtle.y + 1
97 turtle.save()
98 return true
99 end
100 return false
101end
102
103function turtle.down()
104 if slurtle.down() then
105 turtle.y = turtle.y - 1
106 turtle.save()
107 return true
108 end
109 return false
110end
111
112function turtle.turnRight()
113 if slurtle.turnRight() then
114 turtle.dir = modulus_incr(turtle.dir, 1, #DIRECTIONS)
115 turtle.save()
116 return true
117 end
118 return false
119end
120
121function turtle.turnLeft()
122 if slurtle.turnLeft() then
123 turtle.dir = modulus_incr(turtle.dir, -1, #DIRECTIONS)
124 turtle.save()
125 return true
126 end
127 return false
128end
129
130function turtle.turnTo(dir)
131 --dir is a num
132 local num = dir - turtle.dir
133 if num == 0 then
134 return
135 elseif num == -3 or num == 1 then
136 turtle.turnRight()
137 elseif num == 3 or num == -1 then
138 turtle.turnLeft()
139 else
140 turtle.turnRight()
141 turtle.turnRight()
142 end
143end
144
145function turtle.forward_or_dig()
146 turtle.dig()
147 return turtle.forward()
148end
149
150function turtle.back()
151 return turtle.back()
152end
153
154function turtle.up_or_dig()
155 turtle.digUp()
156 return turtle.up()
157end
158function turtle.down_or_dig()
159 turtle.digDown()
160 return turtle.down()
161end
162
163function turtle.goToVar(dist, dir_pos, dir_neg)
164 if dist < 0 then
165 turtle.turnTo(dir_neg)
166 dist = dist * -1
167 else
168 turtle.turnTo(dir_pos)
169 end
170
171 for _=1,dist do
172 turtle.forward_or_dig()
173 end
174end
175
176function turtle.goToHeight(dist)
177 move_func = turtle.up_or_dig
178 if dist < 0 then
179 move_func = turtle.down_or_dig
180 dist = dist * -1
181 end
182
183 for _=1,dist do
184 move_func()
185 end
186end
187
188function turtle.goTo(x,y,z)
189 turtle.goToVar(z-turtle.z, 3, 1)
190 turtle.goToVar(x-turtle.x, 2, 4)
191 turtle.goToHeight(y-turtle.y)
192end
193
194function turtle.findItemSlot(itemName)
195 for i = 1,16 do
196 if turtle.getItemCount(i)>0 then
197 if itemName==turtle.getItemDetail(i).name then return i end
198 end
199 end
200 return 0
201end
202
203function turtle.swapRight(slot)
204 if slot then
205 turtle.select(slot)
206 end
207
208 if turtle.equipRight() and slot then
209 turtle.RIGHT_EQUIP = slot
210 end
211end
212
213function turtle.swapLeft(slot)
214 if slot then
215 turtle.select(slot)
216 end
217
218 if turtle.equipLeft() and slot then
219 turtle.LEFT_EQUIP = slot
220 end
221end
222
223function turtle.getRightEquipSlot()
224 return turtle.RIGHT_EQUIP
225end
226function turtle.getLeftEquipSlot()
227 return turtle.LEFT_EQUIP
228end
229
230function turtle.checkCompass()
231 local checkObs = true
232 local BLOCK_SLOT = 0
233 local torchesM = {["west"]="east",["east"]="west",["north"]="south",["south"]="north","Invalid"}
234 local items = {["minecraft:ladder"]={"Invalid","South","North","East","West"},
235 ["minecraft:wall_torch"]=torchesM,
236 ["minecraft:torch"]=torchesM,
237 ["minecraft:redstone_torch"]=torchesM}
238
239 while turtle.TORCH_SLOT == 0 do
240 turtle.TORCH_SLOT = turtle.findItemSlot("minecraft:torch")
241 if turtle.TORCH_SLOT == 0 then
242 print("Give me a Torch and press any key to continue...")
243 os.pullEvent("key")
244 end
245 end
246
247 if checkObs then
248 if turtle.detect() then
249 turtle.dig()
250 end
251 if turtle.detectDown() then
252 turtle.digDown()
253 end
254 end
255
256 if type(turtle.getFuelLevel()) == "number" and turtle.getFuelLevel() < 2 then
257 print("Not enough Fuel")
258 return
259 end
260
261 --Find a block in inventory
262 while BLOCK_SLOT == 0 do
263 for i = 1,16 do
264 if i ~= turtle.TORCH_SLOT then
265 turtle.select(i)
266 if turtle.place() then
267 BLOCK_SLOT = i
268 break
269 end
270 end
271 end
272 end
273
274 turtle.down()
275 turtle.select(turtle.TORCH_SLOT)
276 turtle.placeUp()
277 local inspect,data = turtle.inspectUp()
278 turtle.digUp()
279 turtle.up()
280 turtle.select(BLOCK_SLOT)
281 turtle.dig()
282 if not inspect then
283 print("Error getting direction")
284 return "Err"
285 end
286 return items[data.name][data.state.facing]
287end
288
289function turtle.checkInventory()
290 local blacklist = { "minecraft:cobblestone","minecraft:cobbled_deepslate", "minecraft:dirt", "minecraft:gravel", "minecraft:sand", "minecraft:tuff", "minecraft:mud", "byg:soapstone", }
291 local emptySlots = 0
292 for i=1,16 do
293 if i ~= turtle.LEFT_EQUIP and i ~= turtle.RIGHT_EQUIP and i ~= turtle.TORCH_SLOT then
294 turtle.select(i)
295 if turtle.getItemCount() > 0 then
296 for _,target in pairs(blacklist) do
297 if turtle.getItemDetail().name==target then
298 turtle.drop()
299 emptySlots = emptySlots + 1
300 break
301 end
302 end
303 else
304 emptySlots = emptySlots + 1
305 end
306 end
307 end
308 return emptySlots
309end
310
311function turtle.dumpInventory()
312 for i=1,16 do
313 if i ~= turtle.LEFT_EQUIP and i ~= turtle.RIGHT_EQUIP and i ~= turtle.TORCH_SLOT then
314 turtle.select(i)
315 turtle.dropUp()
316 end
317 end
318end
319
320function turtle.restore()
321 h = fs.open("restorevalues","r")
322 turtle.x = (tonumber(h.readLine()*1))
323 turtle.y = (tonumber(h.readLine()*1))
324 turtle.z = (tonumber(h.readLine()*1))
325 turtle.dir = (tonumber(h.readLine()*1))
326 turtle.LEFT_EQUIP = (tonumber(h.readLine()*1))
327 turtle.RIGHT_EQUIP = (tonumber(h.readLine()*1))
328 turtle.TORCH_SLOT = (tonumber(h.readLine()*1))
329 h.close()
330end
331
332function turtle.save()
333 h = fs.open("restorevalues","w")
334 h.writeLine(turtle.x)
335 h.writeLine(turtle.y)
336 h.writeLine(turtle.z)
337 h.writeLine(turtle.dir)
338 h.writeLine(turtle.LEFT_EQUIP)
339 h.writeLine(turtle.RIGHT_EQUIP)
340 h.writeLine(turtle.TORCH_SLOT)
341 h.close()
342end
343
344--for key, func in pairs(slurtle) do
345 --if not turtle[key] then
346 --turtle[key] = func
347 --end
348--end
349
350return turtle