Guest User

receive.lua

a guest
Jul 22nd, 2021
6,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.35 KB | None | 0 0
  1. -- Program to receive messages from computers/
  2. -- turtles using flex.lua "send" function
  3.  
  4. --------------------------------------
  5. -- |¯\|¯¯] /¯]|¯¯][¯¯]\\  //|¯¯]|¯\ --
  6. -- | /| ] | [ | ]  ][  \\// | ] | / --
  7. -- | \|__] \_]|__][__]  \/  |__]| \ --
  8. --------------------------------------
  9.  
  10. local log_file = "log.txt"
  11. local options_file = "flex_options.cfg"
  12. os.loadAPI("flex.lua")
  13. local modem_channel = 6464
  14.  
  15.  
  16. if fs.exists(options_file) then
  17.  local file = fs.open("flex_options.cfg", "r")
  18.  local line = file.readLine()
  19.  while line ~= nil do
  20.   if string.find(line, "modem_channel=") == 1 then
  21.    modem_channel = tonumber( string.sub(
  22.          line, 15, string.len(line) ) )
  23.    break
  24.   end --if
  25.   line = file.readLine()
  26.  end --while
  27.  file.close()
  28. end --if
  29.  
  30.  
  31. local modem
  32. local p = flex.getPeripheral("modem")
  33. if #p > 0 then
  34.  modem = peripheral.wrap(p[1])
  35.  modem.open(modem_channel)
  36. else
  37.  flex.printColors("Please attach a wireless"
  38.    .." or ender modem\n", colors.red)
  39.  sleep(2)
  40.  return
  41. end --if/else
  42.  
  43. local monitor
  44. p = flex.getPeripheral("monitor")
  45. if #p > 0 then
  46.  monitor = peripheral.wrap(p[1])
  47.  term.redirect(monitor)
  48.  monitor.clear()
  49.  monitor.setCursorPos(1,1)
  50.  monitor.setTextScale(0.5)
  51. end --if
  52. local lcd_x,lcd_y = monitor.getSize()
  53.  
  54.  
  55. local file, line
  56. local filelist = {}
  57. if fs.exists(log_file) then
  58.  file = fs.open(log_file, "r")
  59.  line = file.readLine()
  60.  
  61.  while line ~= nil do
  62.  
  63.   if line ~= "" or ( line == "" and
  64.      filelist[#filelist] ~= "" ) then
  65.    filelist[#filelist+1] = line
  66.   end --if
  67.  
  68.   line = file.readLine()
  69.  end --while
  70.  file.close()
  71.  file = fs.open(log_file, "a")
  72.  
  73. else
  74.  -- Log file does not exist: make one!
  75.  file = fs.open(log_file, "w")
  76.  
  77. end --if/else
  78.  
  79.  
  80. local x, y
  81. y = math.max(1,#filelist-lcd_y)
  82. for x=y, #filelist do
  83.  flex.printColors(filelist[x])
  84. end --for
  85.  
  86. if filelist[#filelist] ~= "" then
  87.  file.writeLine("")
  88. end --if
  89. file.close()
  90.  
  91.  
  92. term.setTextColor(colors.white)
  93. print("Waiting for message on channel "
  94.       ..tostring(modem_channel).."...")
  95.  
  96. while true do
  97.  local event, modemSide, senderChannel,
  98.     replyChannel, message, senderDistance =
  99.     os.pullEvent("modem_message")
  100.  
  101.  file = fs.open(log_file, "a")
  102.  file.writeLine(message)
  103.  file.close()
  104.  
  105.  flex.printColors(message)
  106.  
  107.  sleep(0.01)
  108. end --while
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment