· 7 years ago · Oct 10, 2018, 05:02 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 saveCords()
240 return true
241 else
242 return false
243 end
244end
245
246function t.down()
247 if turtle.down() then
248 t.y = t.y - 1
249 saveCords()
250 return true
251 else
252 return false
253 end
254end
255
256function t.digDown()
257 if turtle.digDown() then
258 t.blocks_dug = t.blocks_dug + 1
259 return true
260 else
261 return false
262 end
263end
264function t.dig()
265 if turtle.dig() then
266 t.blocks_dug = t.blocks_dug + 1
267 return true
268 else
269 return false
270 end
271end
272function t.digUp()
273 if turtle.digUp() then
274 t.blocks_dug = t.blocks_dug + 1
275 return true
276 else
277 return false
278 end
279end
280
281-- This function saves the turtles posision so it can be returned to later.
282function t.saveCurrentPos(name)
283 if type(name) ~= 'string' then
284 error('Position name must be a string.')
285 end
286
287 -- Creates a new table entry with "name" key
288 t.saved_positions[name] = {
289 x = t.x,
290 y = t.y,
291 z = t.z,
292 orientation = t.orientation
293 }
294end
295function t.savePosisionsToFile()
296 file = fs.open(t.savedPositions, 'w')
297 file.write(textutils.serialize(t.saved_positions))
298 file.close()
299end
300
301function t.getPos()
302 if fs.exists(t.savedPositions) then
303 file = fs.open(t.savedPositions, 'r')
304 t.saved_positions = textutils.unserialize(file.readAll())
305 file.close()
306 else
307 error('No file to get positions from.')
308 end
309end
310
311function t.gotoPos(name)
312 t.goto(t.saved_positions[name].x, t.saved_posisions[name].y, t.saved_posisions[name].z, t.saved_posisions[name].orientation)
313end
314
315-- Careful this breaks blocks.
316function t.goto(xTarget, yTarget, zTarget, orientationTarget)
317 if not xTarget or not yTarget or not zTarget or not orientationTarget then
318 t.log('Here are all the params for the goto function:', 1)
319 t.log(xTarget, yTarget, zTarget, orientationTarget, 1)
320 error('Invalid params, read log for more info.')
321 end
322 -- Moves to t.y
323 while yTarget < t.y do
324 t.digDown()
325 t.down()
326 end
327
328 while yTarget > t.y do
329 t.digUp()
330 t.up()
331 end
332
333 -- Turns to correct t.orientation then moves forward until its at the right t.x cord
334 if xTarget < t.x then
335 t.look('west')
336 while xTarget < t.x do
337 t.dig()
338 t.forward()
339 end
340 end
341
342 if xTarget > t.x then
343 t.look('east')
344 while xTarget > t.x do
345 t.dig()
346 t.forward()
347 end
348 end
349
350 -- Turns to correct t.orientation then moves forward until its at the right t.z cord
351 if zTarget < t.z then
352 t.look('north')
353 while zTarget < t.z do
354 t.dig()
355 t.forward()
356 end
357 end
358 if zTarget > t.z then
359 t.look('south')
360 while zTarget > t.z do
361 t.dig()
362 t.forward()
363 end
364 end
365 -- Look to correct orientation
366 t.look(orientationTarget)
367end
368-- Because its not defined at the top
369t.getCords()
370return t