Barnet

playnbs.lua

Jun 9th, 2021 (edited)
645
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.   playMonitor
  3.   By SoniEx2
  4. --]]
  5.  
  6. -- fastq is no better than CCland other than for table/function support,
  7. -- so we don't use it by default.
  8. -- REQUIRES FASTQ
  9. local use_fastq = false
  10.  
  11. local x = function(...)
  12.   local args = {...} -- args table
  13.   if not args[4] then
  14.     print("Usage: newPlayMonitor.lua <hasColor> <NBS file> <monitor> <slave...>")
  15.     print("(Example: newPlayMonitor.lua 1 whatislove.nbs top left right)")
  16.     return
  17.   end
  18.  
  19.   local monitorBaseSettings = {
  20.     hasColor = (args[1] == "true" or args[1] == "on" or args[1] == "1"),
  21.     title = args[2],
  22.     -- avoid errors
  23.     footerLeft = "",
  24.     footerRight = "",
  25.   }
  26.  
  27.   local playerslave = dofile("player_slave.lua")
  28.   local playermonitor = dofile("player_monitor.lua")
  29.   local playernbs = dofile("player_nbs.lua")
  30.  
  31.   local function play(filename, monside, ...)
  32.     local id = os.clock() -- heh
  33.     local subid = 1
  34.     local maxsubid = 1
  35.  
  36.     local threads = {}
  37.  
  38.     local monitor = peripheral.wrap(monside)
  39.     if peripheral.getType(monside) == "modem" then
  40.       print(monitor.getNamesRemote())
  41.       for k,v in pairs(monitor.getNamesRemote()) do
  42.         print(k,v)
  43.         table.insert(threads, playermonitor.new(peripheral.wrap(v), id, subid, monitorBaseSettings))
  44.       end
  45.     else
  46.       table.insert(threads, playermonitor.new(monitor, id, subid, monitorBaseSettings))
  47.     end
  48.  
  49.     for i, side in ipairs({...}) do  
  50.       table.insert(threads, playerslave.new(side, id, subid))    
  51.     end
  52.  
  53.     table.insert(threads, playernbs.new(id, maxsubid, filename, monitorBaseSettings))
  54.  
  55.     table.insert(threads, function()
  56.         local count = 0
  57.         while true do
  58.           local evt, p1_id, p2_subid, p3_side, p4_count = os.pullEvent("player:slaveinfo")
  59.           if p1_id == id and p2_subid == subid then
  60.             count = count + p4_count
  61.           end
  62.           monitorBaseSettings.footerLeft = count .. " note blocks connected"
  63.         end
  64.       end)
  65.  
  66.     parallel.waitForAny(unpack(threads))
  67.  
  68.     while not rs.getInput("front") do
  69.       os.pullEvent("redstone")
  70.     end
  71.   end
  72.  
  73.   play(select(3, ...))
  74. end
  75.  
  76. if use_fastq then
  77.   dofile("fastq.lua").init(x, ...)
  78. else
  79.   x(...)
  80. end
RAW Paste Data