· 4 years ago · Mar 17, 2021, 12:14 PM
1-- Misc Useful Functions
2-- Required by most other programs
3-- <Flexico64@gmail.com>
4
5---------------------------------------------
6-- |\/|[¯¯]/¯¯\ /¯] /¯¯\[¯¯]|| |||¯¯]|¯¯] --
7-- | | ][ \_¯\| [ \_¯\ || ||_||| ] | ] --
8-- ||||[__]\__/ \_] \__/ || \__||| || --
9---------------------------------------------
10
11local log_file = "log.txt"
12local options_file = "flex_options.cfg"
13
14-- Defaults; can be changed in config file
15local modem_channel = 6464
16local name_color = "lightGray"
17
18
19function getPeripheral(name)
20 local x,sides
21 sides = { "top", "bottom", "left",
22 "right", "front", "back" }
23 local periph = {}
24 for x=1,#sides do
25 if peripheral.getType(sides[x]) == name then
26 periph[#periph+1] = sides[x]
27 end --if
28 end --for
29 return periph
30end --function
31
32
33local modem
34local hasModem = false
35local x = getPeripheral("modem")
36if #x > 0 then
37 hasModem = true
38 modem = peripheral.wrap(x[1])
39 modem.open(modem_channel)
40end --if
41
42function modemOff()
43 local x = getPeripheral("modem")
44 if #x > 0 then
45 modem.close(modem_channel)
46 end --if
47end --function
48
49
50local file
51if not fs.exists(log_file) then
52 file = fs.open(log_file,"w")
53 file.close()
54end --if
55
56
57
58function optionsExport()
59 if fs.exists(options_file) then
60 fs.delete(options_file)
61 end
62 local file
63 while file == nil do
64 file = fs.open(options_file,"w")
65 end
66 file.writeLine("# Flex API Options File #\n")
67 file.writeLine("modem_channel="
68 ..tostring(modem_channel))
69 file.writeLine("name_color="..name_color)
70 file.writeLine("")
71 file.close()
72 return true
73end --function
74
75
76function optionsImport()
77 if not fs.exists(options_file) then
78 return false
79 end
80 local file
81 while file == nil do
82 file = fs.open(options_file, "r")
83 end
84
85 local x = file.readLine()
86 while x ~= nil do
87 if string.find(x,"modem_channel")==1 then
88 modem_channel = tonumber(string.sub(x,15))
89 elseif string.find(x,"name_color")==1 then
90 name_color = string.sub(x,12)
91 end --if/else
92 x = file.readLine()
93 end --while
94
95 file.close()
96 return true
97end --function
98
99
100if not optionsImport() then
101 optionsExport()
102end --if
103
104
105
106--==============================--
107
108
109-- Inventory Condense
110function condense(n)
111 n = n or 1
112 n = math.floor(n)
113 if n < 1 or n > 16 then
114 n = 1
115 end --if
116
117 local x,y,slot
118 slot = turtle.getSelectedSlot()
119 for x=n+1,16 do
120 if turtle.getItemCount(x) > 0 then
121
122 turtle.select(x)
123 for y=n,x-1 do
124 turtle.transferTo(y)
125 if turtle.getItemCount(x) == 0 then
126 break
127 end --if
128 end --for
129
130 end --if
131 end --for
132 turtle.select(slot)
133end --function
134
135
136-- Round to Integer
137function round(n)
138 local m = n - math.floor(n)
139 if m < 0.5 then return math.floor(n)
140 else return math.ceil(n)
141 end --if/else
142end --function
143
144
145-- Evaluate Expression
146function eval(expression)
147 local solution, err = loadstring(
148 "return "..expression)
149 if err then error(err,2) end
150 local sol = pcall(solution)
151 if not sol then
152 error("Invalid Expression",2)
153 end
154 return solution()
155end --function
156
157
158-- Press any Key
159function getKey()
160 local event, key_code = os.pullEvent("key")
161 return key_code
162end --function
163function keyPress() return getKey() end
164
165
166
167------------------------------
168-- [¯¯] |¯¯] \\// [¯¯] --
169-- || | ] >< || --
170-- || |__] //\\ || --
171------------------------------
172-- /¯] /¯\ || /¯\ |¯\ --
173--| [ | O | ||_ | O | | / --
174-- \_] \_/ |__] \_/ | \ --
175------------------------------
176
177hexchars = "0123456789ABCDEF"
178
179-- Start with named value, get hex char
180function getHex(x)
181 if x == nil then
182 error("Number expected, got nil", 2)
183 end
184 x = round(math.log(x)/math.log(2))
185 if x < 0 or x > 15 then
186 -- Default to Light Gray if input is invalid
187 x = 8
188 end --if
189 return string.sub(hexchars,x+1,x+1)
190end --function
191
192-- Start with hex char, get named value
193function getVal(x)
194 local z = string.find(hexchars,x)
195 if z == nil then return nil end
196 return math.pow(2,z-1)
197end --function
198
199local send_depth, print_depth = 0, 0
200
201
202-------------------------------
203-- Multicolor Print Function --
204-------------------------------
205
206function printColors(message,textColor)
207 local x,y,z,t,skip,margin
208 local xmax,ymax = term.getSize()
209 local oldcolor = term.getTextColor()
210 local textColor = textColor or oldcolor
211
212 margin = ""
213 for x=1,print_depth do
214 margin = margin.." "
215 end --for
216
217 if type(message) == "table" then
218 if print_depth == 0 then
219 printColors("#0{")
220 end --if
221 print_depth = print_depth + 1
222
223 for x,y in pairs(message) do
224 if type(y) == "table" then
225 printColors(margin.." "..tostring(x).." #0= {",textColor)
226 printColors(y,textColor)
227 else
228 printColors(margin.." "..tostring(x).." #0= #"..
229 getHex(textColor)..tostring(y),textColor)
230 end --if/else
231 end --for
232
233 print_depth = print_depth - 1
234 printColors(margin.."#0}")
235 return
236
237 end --if
238
239
240 if type(message) ~= "table" then
241 message = "#"..getHex(textColor)..tostring(message)
242 end --if
243
244 for t=1,string.len(message) do
245
246 skip = false
247 while string.sub(message,t,t) == "#" and
248 not skip do
249
250 -- Found legit "#"
251 if string.sub(message,t+1,t+1) == "#" then
252 message = string.sub(message,1,t)..
253 string.sub(message,t+2)..""
254 skip = true
255
256 else
257 textColor = getVal(string.sub(message,t+1,t+1))
258
259 if textColor == nil then
260 textColor = colors.white
261 end --if
262
263 -- This bit clears out # escapes
264 if t == 1 then
265 message = string.sub(message,3)..""
266 elseif t < string.len(message) then
267 message = string.sub(message,1,t-1)..
268 string.sub(message,t+2)..""
269 elseif t == string.len(message) then
270 message = string.sub(message,1,t-1)..""
271 end --if/else
272
273 end --if
274
275 if t > string.len(message) then
276 break
277 end --if
278
279 end --while (is escape char)
280
281 if t > string.len(message) then
282 break
283 end --if
284
285 -- Actually Print Character
286 x,y = term.getCursorPos()
287 term.setTextColor(textColor)
288
289 if textColor == colors.gray then
290 term.setBackgroundColor(colors.lightGray)
291
292 elseif textColor == colors.black then
293 term.setBackgroundColor(colors.white)
294
295 end --if/else
296 term.write(string.sub(message,t,t))
297 term.setBackgroundColor(colors.black)
298
299 if t >= string.len(message) then
300 break
301 end --if
302
303 -- Loop Around to Next Row
304 xmax,ymax = term.getSize()
305 if string.sub(message,t,t) == "\n" or x >= xmax then
306 x = 1
307 if y < ymax-1 then
308 y = y + 1
309 else
310 print("")
311 end --if/else
312 else
313 x = x + 1
314 end --if/else
315 term.setCursorPos(x,y)
316
317 end --for
318
319 term.setTextColor(oldcolor)
320 print("")
321
322end --function
323
324
325
326-----------------------------
327--Print/Broadcast Function --
328-----------------------------
329
330function send(message,textColor)
331 local x,y,z,id,nameColor
332 local oldColor = term.getTextColor()
333
334 local margin = ""
335 for x=1,send_depth do
336 margin = margin.." "
337 end --for
338
339 if type(message) == "table" then
340 if send_depth == 0 then
341 send("#0{")
342 end --if
343 send_depth = send_depth + 1
344
345 for x,y in pairs(message) do
346 if type(y) == "table" then
347 send(margin.." "..tostring(x).." #0= {",textColor)
348 send(y,textColor)
349 else
350 send(margin.." "..tostring(x)..
351 " #0= #"..getHex(textColor)..tostring(y),textColor)
352 end --if/else
353 end --for
354
355 send_depth = send_depth - 1
356 send(margin.."#0}")
357 return
358
359 end --if
360
361
362 if message == nil then
363 message = "nil"
364 end --if
365
366 message = tostring(message)
367 textColor = textColor or oldColor
368 nameColor = eval("colors."..name_color)
369
370 printColors(message,textColor)
371
372 file = fs.open(log_file,"a")
373 file.writeLine(message)
374 file.close()
375
376 if hasModem then
377 id = "#"..getHex(nameColor)..
378 tostring(os.getComputerID()).."#0"
379
380 if os.getComputerLabel() ~= nil then
381 id = id.."|#"..getHex(nameColor)..
382 os.getComputerLabel().."#0"
383 end --if
384
385 id = id..": #"..getHex(textColor)..
386 message..""
387
388 modem.transmit(modem_channel,
389 modem_channel,id)
390 sleep(0.1)
391 end --if (hasModem)
392
393 term.setTextColor(oldColor)
394 sleep(0.02)
395end --funtion
396
397
398
399args = {...}
400
401if args[1]=="color" or args[1]=="colors" then
402 z = ""
403 for x=0,15 do
404 y = string.sub(hexchars,x+1,x+1)..""
405 z = z.."#"..y..y.."#0 "
406 end --for
407 printColors(z)
408 return
409
410elseif args[1] == "edit" then
411 shell.run("edit "..options_file)
412 optionsImport()
413
414end --if/else
415
416
417
418-------------------------------------------
419-- /¯¯] |¯¯] [¯¯] |¯\ /\ [¯¯] /\ --
420--| [¯| | ] || | | | | || | | --
421-- \__| |__] || |_/ |||| || |||| --
422-------------------------------------------
423
424
425function getBlock(dir)
426 dir = dir or "fwd"
427 local block,meta
428
429 if dir=="fwd" then
430 block,meta = turtle.inspect()
431 elseif dir=="up" then
432 block,meta = turtle.inspectUp()
433 elseif dir=="down" then
434 block,meta = turtle.inspectDown()
435 end
436
437 if block then
438 block = meta["name"]
439 meta = meta["metadata"]
440 return block,meta
441 else
442 return "minecraft:air",nil
443 end --if
444
445end --function
446
447function getBlockUp()
448 return getBlock("up")
449end
450
451function getBlockDown()
452 return getBlock("down")
453end
454
455
456function isBlock(key,dir)
457 if type(key) == "string" then
458 key = { key }
459 end --if
460 if type(key) ~= "table" then
461 error("Expected string or table, got "
462 ..type(key), 2)
463 return false
464 end --if
465
466 local block = getBlock(dir)
467 local x
468 for x=1,#key do
469
470 if string.find(key[x],":") ~= nil then
471 if block == key[x] then
472 return true
473 end --if
474
475 else
476 if string.find(block,key[x]) ~= nil then
477 return true
478 end --if
479
480 end --if/else
481 end --for
482
483 return false
484end --function
485
486function isBlockUp(key)
487 return isBlock(key, "up")
488end
489
490function isBlockDown(key)
491 return isBlock(key, "down")
492end
493
494
495
496local fluid = { "air", "water", "lava",
497 "acid", "blood", "poison" }
498
499function isFluid(dir)
500 return isBlock(fluid, "fwd")
501end
502
503function isFluidUp()
504 return isBlock(fluid, "up")
505end
506
507function isFluidDown()
508 return isBlock(fluid, "down")
509end
510
511
512
513function isItem(key,slot)
514 local slot_old = turtle.getSelectedSlot()
515 if type(slot) ~= "number" then
516 slot = slot_old
517 end --if
518
519 if type(key) == "table" then
520 local x
521 for x=1,#key do
522 if isItem(key[x],slot) then
523 return true
524 end --if
525 end --for
526 return false
527 end --if
528
529 if turtle.getItemCount(slot) == 0 then
530 return false
531 end --if
532
533 local name = turtle.getItemDetail(slot)["name"]
534
535 return ( string.find(name,key) ~= nil )
536end --function
537
538
539