Guest User

flex.lua

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