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