· 7 years ago · Oct 10, 2018, 05:30 PM
1-- Valvates library for cordanite management and stuff.
2-- If you have an idea for feature make an issue or
3-- Create a pull request if you're a coder.
4-- A lot of stuff here is unfinished so
5-- Be careful and tell me how to make it better :DD
6
7-- WARNING!
8-- Since the turtle writes his coordanites to a file you should
9-- run fs.delete(t.cordsfile) when your program finishes!
10
11local t = {}
12
13
14-- Debug level of messages to display. (see log function)
15-- I use level 1 as error 2 as Warning 3 as Info and 4 for debug.
16t.debug_level = 4
17
18-- Files
19t.logfile = 'logfile.log'
20t.cordsfile = 'cords'
21t.posfile = 'savedPosistions'
22
23local file -- Used for file management
24t.saved_positions = {}-- Table for saving posisions
25
26t.blocks_dug = 0 -- Blocks dug
27
28-- Diference in cords with diferent orientations
29local zDiff = {
30 [0] = -1,
31 [1] = 0,
32 [2] = 1,
33 [3] = 0
34}
35
36local xDiff = {
37 [0] = 0,
38 [1] = 1,
39 [2] = 0,
40 [3] = -1
41}
42
43-- Needed for human readable input/output
44t.orientations = {
45 [0] = "north",
46 [1] = "east",
47 [2] = "south",
48 [3] = "west"
49}
50
51-- Unwanted items for clean inventory function.
52t.unWantedItems = {
53 'minecraft:cobblestone',
54 'minecraft:stone',
55 'minecraft:flint',
56 'minecraft:dirt',
57 'minecraft:sandstone',
58 'minecraft:sand'
59}
60
61-- Checks if a value is in a table.
62function t.inTable(value, table)
63 for key, v in ipairs(table) do
64 if value == value then
65 return true
66 end
67 end
68 -- if its not in the table then
69 return false
70end
71
72function t.cleanInventory()
73 local item
74 local prevSlot = turtle.getSelectedSlot()
75
76 for i=1,16 do
77 item = turtle.getItemDetail(i)
78 -- Makes sure item exists to avoid nil errors.
79 if item and t.inTable(item.name, t.unWantedItems) then
80 turtle.select(i)
81 turtle.dropDown(item.count) -- Drops all of the unwanted item
82 end
83 turtle.select(prevSlot) -- Leave no trace!
84 end
85end
86
87function t.writeToFile(msg, file)
88 -- Function used by logging function.
89 -- i felt it was cleaner this way.
90
91 if file == nil then
92 file = 'log' -- default
93 end
94
95 file = fs.open(file, 'a')
96 file.write(msg..'\n') -- Adds newline
97 file.close()
98end
99
100function t.log(msg, msg_debug_level)
101 -- Logging function
102
103 if msg_debug_level == nil then
104 t.writeToFile('[WARNING] msg_debug_level is nil, defaulting to level 3 message info.', t.logfile)
105 -- As a param this is already local.
106 msg_debug_level = 3
107 end
108
109 if msg_debug_level <= t.debug_level then
110 t.writeToFile(msg)
111 end
112end
113
114local function updatePositions()
115 -- Writes saved positions to file.
116 file = fs.open(t.posfile, 'w')
117 file.write(textutils.serialize(t.saved_positions))
118 file.close()
119end
120
121-- Get cords from file
122function t.getCords()
123 local cords
124 local contents
125 if t.cordsfile == nil then
126 t.log('[ERROR] cordsfile is nil', 1)
127 t.log('[WARNING] Without a cords file persistance will fail', 2)
128 return -- Breaks from this function
129 end
130 if not fs.exists(t.cordsfile) then
131 t.log('[WARNING] t.cordsfile does not exist', 2)
132 t.log('[INFO] Creating cordsfile...', 3)
133 -- Creates cords file with 0,0,0,0 as values.
134 t.writeToFile(textutils.serialize({x = 0, y = 0, z = 0, orientation = 0}), t.cordsfile)
135 end
136 file = fs.open(t.cordsfile, 'r') -- Opens cordsfile for reading.
137 contents = file.readAll()
138 cords = textutils.unserialize(contents)
139 -- Sets coordanites
140 t.x = cords.x
141 t.y = cords.y
142 t.z = cords.z
143
144 -- Sets orientation
145 t.orientation = cords.orientation
146
147 -- Not going to return a value since i will just change the varables.
148end
149
150-- Saves coordanites to file
151local function saveCords()
152 local cords = {
153 x = t.x,
154 y = t.y,
155 z = t.z,
156 }
157 -- Testing to see if i need the temporary cords varable.
158 t.log('[DEBUG] Trying to write cords to file.', 4)
159 t.log('[DEBUG] t.x = '..t.x..' t.y = '..t.y..' t.z = '..t.z)
160 writeToFile(textutils.serialize({x = t.x,y = t.y, z = t.z}), t.cordsfile)
161end
162
163local function orientationToNumber(orientationStr)
164 -- Turns an orientation string into an Orientation number.
165 for i=0,#t.orientations do
166 if orientationStr == t.orientations[i] then
167 return i
168 end
169 end
170end
171
172-- Turns an orientation number into an t.orientation string.
173local function orientationToString(orientationInt)
174 -- Checks to see if orientationInt is a number
175 if type(orientationInt) ~= 'number' then
176 t.log('[ERROR] orientationInt is not a number', 1)
177 error('[ERROR] OrientationInt is not a number')
178 end
179 if orientations[orientationInt] then
180 return t.orientations[orientationInt]
181 else
182 print('[ERROR] Orientation is invalid', 1)
183 print('orientationInt = '..orientationInt)
184 error()
185 end
186end
187
188-- Turning functions
189function t.turnRight()
190 turtle.turnRight()
191 -- This "magic" math adds one to t.orientation unless t.orientation is 3, then it moves to 0.
192 -- This could also be done with an if statement but this is cleaner imo
193 t.orientation = (t.orientation + 1) % 4
194end
195
196function t.turnLeft()
197 turtle.turnLeft()
198 t.orientation = (t.orientation - 1) % 4
199end
200
201-- Looks to a direction, can be passed a string or a number
202function t.look(direction)
203 -- makes sure the value passed is valid.
204 if type(direction) == 'string' then
205 direction = orientationToNumber(direction)
206 elseif type(direction) ~= 'number' then
207 error('Direction is not a number')
208 end
209
210 -- Thanks to Incin for this bit of code :)
211 if direction == t.orientation then return end
212
213 if (direction - t.orientation) % 2 == 0 then
214 t.turnLeft()
215 t.turnLeft()
216 elseif (direction - t.orientation) % 4 == 1 then
217 t.turnRight()
218 else
219 t.turnLeft()
220 end
221end
222
223function t.forward()
224 t.log('[DEBUG] t.forward called', 4)
225
226 if turtle.forward() then
227 t.log('[DEBUG] turtle.forward() returned true, changing cords...', 4)
228 -- Change t.x and t.z cords
229 t.x = t.x + xDiff[t.orientation]
230 t.z = t.z + zDiff[t.orientation]
231 saveCords()
232 return true
233 else
234 -- If he failed to move return false and don't change the cords.
235 return false
236 end
237end
238
239function t.up()
240 t.log('[DEBUG] t.up function called', 4)
241 if turtle.up() then
242 t.y = t.y + 1
243 t.log('[DEBUG] Trying to save cords to file after going up', 4)
244 saveCords()
245 return true
246 else
247 return false
248 end
249end
250
251function t.down()
252 if turtle.down() then
253 t.y = t.y - 1
254 saveCords()
255 return true
256 else
257 return false
258 end
259end
260
261function t.digDown()
262 if turtle.digDown() then
263 t.blocks_dug = t.blocks_dug + 1
264 return true
265 else
266 return false
267 end
268end
269function t.dig()
270 if turtle.dig() then
271 t.blocks_dug = t.blocks_dug + 1
272 return true
273 else
274 return false
275 end
276end
277function t.digUp()
278 if turtle.digUp() then
279 t.blocks_dug = t.blocks_dug + 1
280 return true
281 else
282 return false
283 end
284end
285
286-- This function saves the turtles posision so it can be returned to later.
287function t.saveCurrentPos(name)
288 if type(name) ~= 'string' then
289 error('Position name must be a string.')
290 end
291
292 -- Creates a new table entry with "name" key
293 t.saved_positions[name] = {
294 x = t.x,
295 y = t.y,
296 z = t.z,
297 orientation = t.orientation
298 }
299end
300
301function t.savePosisionsToFile()
302 file = fs.open(t.savedPositions, 'w')
303 file.write(textutils.serialize(t.saved_positions))
304 file.close()
305end
306
307function t.getPos()
308 if fs.exists(t.savedPositions) then
309 file = fs.open(t.savedPositions, 'r')
310 t.saved_positions = textutils.unserialize(file.readAll())
311 file.close()
312 else
313 error('No file to get positions from.')
314 end
315end
316
317function t.gotoPos(name)
318 t.goto(t.saved_positions[name].x, t.saved_posisions[name].y, t.saved_posisions[name].z, t.saved_posisions[name].orientation)
319end
320
321-- Careful this breaks blocks.
322function t.goto(xTarget, yTarget, zTarget, orientationTarget)
323 if not xTarget or not yTarget or not zTarget or not orientationTarget then
324 t.log('Here are all the params for the goto function:', 1)
325 t.log(xTarget, yTarget, zTarget, orientationTarget, 1)
326 error('Invalid params, read log for more info.')
327 end
328 -- Moves to t.y
329 while yTarget < t.y do
330 t.digDown()
331 t.down()
332 end
333
334 while yTarget > t.y do
335 t.digUp()
336 t.up()
337 end
338
339 -- Turns to correct t.orientation then moves forward until its at the right t.x cord
340 if xTarget < t.x then
341 t.look('west')
342 while xTarget < t.x do
343 t.dig()
344 t.forward()
345 end
346 end
347
348 if xTarget > t.x then
349 t.look('east')
350 while xTarget > t.x do
351 t.dig()
352 t.forward()
353 end
354 end
355
356 -- Turns to correct t.orientation then moves forward until its at the right t.z cord
357 if zTarget < t.z then
358 t.look('north')
359 while zTarget < t.z do
360 t.dig()
361 t.forward()
362 end
363 end
364 if zTarget > t.z then
365 t.look('south')
366 while zTarget > t.z do
367 t.dig()
368 t.forward()
369 end
370 end
371 -- Look to correct orientation
372 t.look(orientationTarget)
373end
374-- Because its not defined at the top
375t.getCords()
376return t