Advertisement
AssortedBrunoz

Target practice

Sep 8th, 2024 (edited)
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. local modem = peripheral.wrap("bottom")
  2.  
  3. local monitors = {}
  4.  
  5. local routers = {}
  6.  
  7. local speakers = {}
  8.  
  9. for i, v in pairs(modem.getNamesRemote()) do
  10.     if string.find(v,"monitor") then
  11.         table.insert(monitors, v)
  12.     elseif string.find(v,"redrouter") then
  13.         table.insert(routers, v)
  14.     elseif string.find(v, "speaker") then
  15.         table.insert(speakers, v)
  16.    end
  17. end
  18.  
  19. local won = false
  20.  
  21. term.clear()
  22.  
  23. io.write("Target Practice program by @the_orion.5, enjoy!\n")
  24.  
  25. io.write("Please input amount of players:\n")
  26.  
  27. local input_players = tonumber(io.read())
  28.  
  29. io.write("Please input amount of seconds per player:\n")
  30.  
  31. local input_time = tonumber(io.read())
  32.  
  33. io.write("Match starting..")
  34.  
  35. function cursor_down()
  36.     for _, monitor in pairs(monitors) do
  37.         local x,y = modem.callRemote(monitor, "getCursorPos")
  38.         modem.callRemote(monitor, "setCursorPos", 1, y+1)
  39.     end
  40. end
  41.  
  42. function play_speakers(instrument, pitch)
  43.     for _, speaker in pairs(speakers) do
  44.         modem.callRemote(speaker, "playNote", instrument, 3, pitch)
  45.     end
  46. end
  47.  
  48. function ding(pitch)
  49.     play_speakers("harp", pitch)
  50. end
  51.  
  52. function monitor_write(text)
  53.     for _, monitor in pairs(monitors) do
  54.         modem.callRemote(monitor,"write", text)
  55.     end
  56. end
  57.  
  58. function monitor_clear()
  59.     for _, monitor in pairs(monitors) do
  60.         modem.callRemote(monitor, "clear")
  61.     end
  62. end
  63.  
  64. function cursor_reset()
  65.     for _, monitor in pairs(monitors) do
  66.         modem.callRemote(monitor, "setCursorPos", 1,1)
  67.     end
  68. end
  69. cursor_reset()
  70.  
  71. local fired = {}
  72.  
  73. local function find_in_table(item, tb)
  74.     for _, v in pairs(tb) do
  75.         if item == v then
  76.             return true
  77.         end
  78.     end
  79. end
  80.  
  81. function check_routers()
  82.     local points = 0
  83.     for _, router in pairs(routers) do
  84.         result = modem.callRemote(router, "getAnalogInput", "top")
  85.         if result > 0 and not find_in_table(router, fired) then
  86.             points = points + result
  87.             table.insert(fired, router)
  88.         end
  89.     end
  90.     return points
  91. end
  92.  
  93. function buzz(pitch)
  94.     play_speakers("cow_bell", pitch)
  95. end
  96.  
  97. local scores = {}
  98.  
  99. for player = 1, input_players, 1 do
  100.    
  101.     local points = 0
  102.    
  103.     local start = os.time(os.date("*t"))
  104.    
  105.     local _end = start + input_time
  106.    
  107.     local cycles = 0
  108.    
  109.     ding(15)
  110.     os.sleep(1)
  111.     ding(16)
  112.     os.sleep(1)
  113.     ding(17)
  114.     os.sleep(1)
  115.     ding(24)
  116.    
  117.     repeat
  118.         if cycles == 380 then
  119.             table.remove(fired, 1)
  120.             cycles = 0
  121.         end
  122.         cycles = cycles + 1
  123.    
  124.         monitor_clear()
  125.         cursor_reset()
  126.        
  127.         monitor_write("Player "..player.."'s turn!")
  128.         cursor_down()
  129.         monitor_write(points.." points")
  130.        
  131.         points = points + check_routers()
  132.        
  133.         os.sleep(0.01)
  134.        
  135.     until os.time(os.date("*t")) >= _end
  136.    
  137.     fired = {}
  138.    
  139.     table.insert(scores, {player, points})
  140.    
  141.     monitor_clear()
  142.     cursor_reset()
  143.    
  144.     monitor_write("Time's up!")
  145.     cursor_down()
  146.     monitor_write("Player "..player.." got "..points.." points!")
  147.  
  148.     buzz(12)
  149.  
  150. end
  151.  
  152. monitor_clear()
  153. cursor_reset()
  154.  
  155. table.sort(scores, function(a,b)
  156.     return a[2] > b[2]
  157. end)
  158.  
  159. ding(24)
  160.  
  161. for position, pack in pairs(scores) do
  162.     local player, score = pack[1], pack[2]
  163.    
  164.     monitor_write(position..". Player ".. player..": "..score.." points,")
  165.     cursor_down()
  166. end
  167.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement