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