· 7 years ago · Oct 10, 2018, 05:18 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 = 3
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 writeToFile(textutils.serialize({x = t.x,y = t.y, z = t,z}), t.cordsfile)
160end
161
162local function orientationToNumber(orientationStr)
163 -- Turns an orientation string into an Orientation number.
164 for i=0,#t.orientations do
165 if orientationStr == t.orientations[i] then
166 return i
167 end
168 end
169end
170
171-- Turns an orientation number into an t.orientation string.
172local function orientationToString(orientationInt)
173 -- Checks to see if orientationInt is a number
174 if type(orientationInt) ~= 'number' then
175 t.log('[ERROR] orientationInt is not a number', 1)
176 error('[ERROR] OrientationInt is not a number')
177 end
178 if orientations[orientationInt] then
179 return t.orientations[orientationInt]
180 else
181 print('[ERROR] Orientation is invalid', 1)
182 print('orientationInt = '..orientationInt)
183 error()
184 end
185end
186
187-- Turning functions
188function t.turnRight()
189 turtle.turnRight()
190 -- This "magic" math adds one to t.orientation unless t.orientation is 3, then it moves to 0.
191 -- This could also be done with an if statement but this is cleaner imo
192 t.orientation = (t.orientation + 1) % 4
193end
194
195function t.turnLeft()
196 turtle.turnLeft()
197 t.orientation = (t.orientation - 1) % 4
198end
199
200-- Looks to a direction, can be passed a string or a number
201function t.look(direction)
202 -- makes sure the value passed is valid.
203 if type(direction) == 'string' then
204 direction = orientationToNumber(direction)
205
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 if turtle.forward() then
225 -- Change t.x and t.z cords
226 t.x = t.x + xDiff[t.orientation]
227 t.z = t.z + zDiff[t.orientation]
228 saveCords()
229 return true
230 else
231 -- If he failed to move return false and don't change the cords.
232 return false
233 end
234end
235
236function t.up()
237 if turtle.up() then
238 t.y = t.y + 1
239 t.log('[DEBUG] Trying to save cords to file after going up', 4)
240 saveCords()
241 return true
242 else
243 return false
244 end
245end
246
247function t.down()
248 if turtle.down() then
249 t.y = t.y - 1
250 saveCords()
251 return true
252 else
253 return false
254 end
255end
256
257function t.digDown()
258 if turtle.digDown() then
259 t.blocks_dug = t.blocks_dug + 1
260 return true
261 else
262 return false
263 end
264end
265function t.dig()
266 if turtle.dig() then
267 t.blocks_dug = t.blocks_dug + 1
268 return true
269 else
270 return false
271 end
272end
273function t.digUp()
274 if turtle.digUp() then
275 t.blocks_dug = t.blocks_dug + 1
276 return true
277 else
278 return false
279 end
280end
281
282-- This function saves the turtles posision so it can be returned to later.
283function t.saveCurrentPos(name)
284 if type(name) ~= 'string' then
285 error('Position name must be a string.')
286 end
287
288 -- Creates a new table entry with "name" key
289 t.saved_positions[name] = {
290 x = t.x,
291 y = t.y,
292 z = t.z,
293 orientation = t.orientation
294 }
295end
296
297function t.savePosisionsToFile()
298 file = fs.open(t.savedPositions, 'w')
299 file.write(textutils.serialize(t.saved_positions))
300 file.close()
301end
302
303function t.getPos()
304 if fs.exists(t.savedPositions) then
305 file = fs.open(t.savedPositions, 'r')
306 t.saved_positions = textutils.unserialize(file.readAll())
307 file.close()
308 else
309 error('No file to get positions from.')
310 end
311end
312
313function t.gotoPos(name)
314 t.goto(t.saved_positions[name].x, t.saved_posisions[name].y, t.saved_posisions[name].z, t.saved_posisions[name].orientation)
315end
316
317-- Careful this breaks blocks.
318function t.goto(xTarget, yTarget, zTarget, orientationTarget)
319 if not xTarget or not yTarget or not zTarget or not orientationTarget then
320 t.log('Here are all the params for the goto function:', 1)
321 t.log(xTarget, yTarget, zTarget, orientationTarget, 1)
322 error('Invalid params, read log for more info.')
323 end
324 -- Moves to t.y
325 while yTarget < t.y do
326 t.digDown()
327 t.down()
328 end
329
330 while yTarget > t.y do
331 t.digUp()
332 t.up()
333 end
334
335 -- Turns to correct t.orientation then moves forward until its at the right t.x cord
336 if xTarget < t.x then
337 t.look('west')
338 while xTarget < t.x do
339 t.dig()
340 t.forward()
341 end
342 end
343
344 if xTarget > t.x then
345 t.look('east')
346 while xTarget > t.x do
347 t.dig()
348 t.forward()
349 end
350 end
351
352 -- Turns to correct t.orientation then moves forward until its at the right t.z cord
353 if zTarget < t.z then
354 t.look('north')
355 while zTarget < t.z do
356 t.dig()
357 t.forward()
358 end
359 end
360 if zTarget > t.z then
361 t.look('south')
362 while zTarget > t.z do
363 t.dig()
364 t.forward()
365 end
366 end
367 -- Look to correct orientation
368 t.look(orientationTarget)
369end
370-- Because its not defined at the top
371t.getCords()
372return t