Advertisement
kaibochan

greeter.lua

Mar 6th, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --different possible groupings and their
  2. --corresponding greetings
  3.  
  4. --default text displayed when no players
  5. local defaultText = "Welcome to Biopunk"
  6.  
  7. local defaultSettings = {
  8.     ["scale"] = 1.5,
  9.     ["textColor"] = "lime",
  10.     ["bgColor"] = "black",
  11. }
  12.  
  13. function findGroup(players, groupings)
  14.     local foundGroup
  15.            
  16.     --iterate through all different groups
  17.     for i, group in ipairs(groupings) do
  18.         foundGroup = true
  19.        
  20.         --iterate through names within the
  21.         --current group, checking for a
  22.         --match, then printing the
  23.         --corresponding greeting
  24.         if #players ==  #group then
  25.             for j, name in ipairs(group) do
  26.                 if players[j] ~= name then
  27.                     foundGroup = false
  28.                     break
  29.                 end
  30.             end
  31.         else
  32.             foundGroup = false
  33.         end
  34.  
  35.         if foundGroup then
  36.             return i
  37.         end
  38.     end
  39. end
  40.  
  41. function loadGreetingFiles()
  42.     --load greeting data from files
  43.     local fileGroupings = fs.open("groupings.txt", "r")
  44.     local groupings = textutils.unserialise(fileGroupings.readAll())
  45.     fileGroupings.close()
  46.    
  47.     local fileGreetings = fs.open("greetings.txt", "r")
  48.     local greetings = textutils.unserialise(fileGreetings.readAll())
  49.     fileGreetings.close()
  50.    
  51.     local fileGreetFormat = fs.open("greetFormat.txt", "r")
  52.     local settings = textutils.unserialise(fileGreetFormat.readAll())
  53.     fileGreetFormat.close()
  54.  
  55.     return groupings, greetings, settings
  56. end
  57.  
  58. function main()
  59.     local groupings, greetings, settings = loadGreetingFiles()
  60.  
  61.     --sort each grouping for easy comparison
  62.     for i, group in ipairs(groupings) do
  63.         table.sort(group)
  64.     end
  65.  
  66.     --find peripherals
  67.     monitor = peripheral.find("monitor")
  68.     playerDetector = peripheral.find("playerDetector")
  69.  
  70.     while true do
  71.         --only activate once every 1/8 of a second
  72.         sleep(1/8)
  73.        
  74.         monitor.setCursorPos(1, 1)
  75.  
  76.         --get a sorted list of nearby players
  77.         local players = playerDetector.getPlayersInCubic(3, 3, 3)
  78.         table.sort(players)
  79.        
  80.         local group = findGroup(players, groupings)
  81.         if group then
  82.             monitor.setTextScale(settings[group].scale)
  83.             monitor.setTextColor(colors[settings[group].textColor])
  84.             monitor.setBackgroundColor(colors[settings[group].bgColor])
  85.             monitor.clear()
  86.  
  87.             monitor.write(greetings[group])
  88.         else
  89.             --if no players nearby or no matching group found
  90.             --display default text
  91.  
  92.             monitor.setTextScale(defaultSettings.scale)
  93.             monitor.setTextColor(colors[defaultSettings.textColor])
  94.             monitor.setBackgroundColor(colors[defaultSettings.bgColor])
  95.             monitor.clear()
  96.  
  97.             monitor.write(defaultText)
  98.         end
  99.     end
  100. end
  101.  
  102. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement