From e1645f37fc72e7733b64c51ffbee0370e13cbe29 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Wed, 19 Jul 2023 01:16:22 +1000 Subject: Support for player entity message commands --- assets/opensb/scripts/opensb/player/commands.lua | 30 ++++ assets/opensb/scripts/opensb/player/player.lua | 2 + .../opensb/universeclient/universeclient.lua | 2 + .../scripts/opensb/universeclient/voicemanager.lua | 164 ++++++++++++++++++ assets/opensb/scripts/opensb/util/modules.lua | 31 ++++ assets/opensb/scripts/universeClient/opensb.lua | 29 ---- .../universeClient/opensb/voice_manager.lua | 183 --------------------- 7 files changed, 229 insertions(+), 212 deletions(-) create mode 100644 assets/opensb/scripts/opensb/player/commands.lua create mode 100644 assets/opensb/scripts/opensb/player/player.lua create mode 100644 assets/opensb/scripts/opensb/universeclient/universeclient.lua create mode 100644 assets/opensb/scripts/opensb/universeclient/voicemanager.lua create mode 100644 assets/opensb/scripts/opensb/util/modules.lua delete mode 100644 assets/opensb/scripts/universeClient/opensb.lua delete mode 100644 assets/opensb/scripts/universeClient/opensb/voice_manager.lua (limited to 'assets/opensb/scripts') diff --git a/assets/opensb/scripts/opensb/player/commands.lua b/assets/opensb/scripts/opensb/player/commands.lua new file mode 100644 index 0000000..f62e2b8 --- /dev/null +++ b/assets/opensb/scripts/opensb/player/commands.lua @@ -0,0 +1,30 @@ +local module = {} +modules.commands = module + +local commands = {} +local function command(name, func) + commands[name] = func +end + +function module.init() + for name, func in pairs(commands) do + message.setHandler("/" .. name, function(isLocal, _, ...) + if not isLocal then + return + else + return func(...) + end + end) + end +end + + +command("run", function(src) + local success, result = pcall(loadstring, src, "/run") + if not success then + return "^#f00;compile error: " .. result + else + local success, result = pcall(result) + return not success and "^#f00;error: " .. result or sb.printJson(result) + end +end) \ No newline at end of file diff --git a/assets/opensb/scripts/opensb/player/player.lua b/assets/opensb/scripts/opensb/player/player.lua new file mode 100644 index 0000000..05cbfb9 --- /dev/null +++ b/assets/opensb/scripts/opensb/player/player.lua @@ -0,0 +1,2 @@ +require "/scripts/opensb/util/modules.lua" +modules("/scripts/opensb/player/", {"commands"}) \ No newline at end of file diff --git a/assets/opensb/scripts/opensb/universeclient/universeclient.lua b/assets/opensb/scripts/opensb/universeclient/universeclient.lua new file mode 100644 index 0000000..cad342b --- /dev/null +++ b/assets/opensb/scripts/opensb/universeclient/universeclient.lua @@ -0,0 +1,2 @@ +require "/scripts/opensb/util/modules.lua" +modules("/scripts/opensb/universeclient/", {"voicemanager"}) \ No newline at end of file diff --git a/assets/opensb/scripts/opensb/universeclient/voicemanager.lua b/assets/opensb/scripts/opensb/universeclient/voicemanager.lua new file mode 100644 index 0000000..c0aa309 --- /dev/null +++ b/assets/opensb/scripts/opensb/universeclient/voicemanager.lua @@ -0,0 +1,164 @@ +-- Manages the voice HUD indicators and click-to-mute/unmute. + +local fmt = string.format +local sqrt = math.sqrt + +local module = {} +modules.voice_manager = module + +--constants +local INDICATOR_PATH = "/interface/voicechat/indicator/" +local BACK_INDICATOR_IMAGE = INDICATOR_PATH .. "back.png" +local FRONT_INDICATOR_IMAGE = INDICATOR_PATH .. "front.png" +local FRONT_MUTED_INDICATOR_IMAGE = INDICATOR_PATH .. "front_muted.png" +local INDICATOR_SIZE = {300, 48} +local LINE_PADDING = 12 +local LINE_WIDTH = 296 +local LINE_WIDTH_PADDED = LINE_WIDTH - LINE_PADDING +local LINE_COLOR = {50, 210, 255, 255} +local FONT_DIRECTIVES = "?border=1;333;3337?border=1;333;3330" +local NAME_PREFIX = "^noshadow,white,set;" + +local linePaddingDrawable +do + local drawable = { image = BACK_INDICATOR_IMAGE, position = {0, 0}, color = LINE_COLOR, centered = false } + function linePaddingDrawable(a, b) + drawable.image = BACK_INDICATOR_IMAGE .. fmt("?crop=%i;%i;%i;%i?fade=fff;1", a, 0, b, INDICATOR_SIZE[2]) + drawable.position[1] = a + return drawable; + end +end + +local function line(pos, value) + local width = math.floor((LINE_WIDTH * value) + 0.5) + LINE_COLOR[4] = 255 * math.min(1, sqrt(width / 350)) + if width > 0 then + canvas:drawDrawable(linePaddingDrawable(0, math.min(12, width)), pos) + if width > 12 then + lineDrawable.line[2][1] = math.min(width, LINE_WIDTH_PADDED) + canvas:drawDrawable(lineDrawable, pos) + if width > LINE_WIDTH_PADDED then + canvas:drawDrawable(linePaddingDrawable(LINE_WIDTH_PADDED, width), pos) + end + end + end +end + +local canvas + +local drawable = { image = BACK_INDICATOR_IMAGE, centered = false } +local textPositioning = { position = {0, 0}, horizontalAnchor = "left", verticalAnchor = "mid" } + +local hoveredSpeaker = nil +local hoveredSpeakerIndex = 1 +local hoveredSpeakerPosition = {0, 0} +local function mouseOverSpeaker(mouse, pos, expand) + expand = tonumber(expand) or 0 + return (mouse[1] > pos[1] - expand and mouse[1] < pos[1] + 300 + expand) + and (mouse[2] > pos[2] - expand and mouse[2] < pos[2] + 48 + expand) +end + +local function drawSpeakerBar(mouse, pos, speaker, i) + drawable.image = BACK_INDICATOR_IMAGE + canvas:drawDrawable(drawable, pos) + line(pos, 1 - sqrt(math.min(1, math.max(0, speaker.loudness / -50)))) + local hovering = not speaker.isLocal and mouseOverSpeaker(mouse, pos) + + textPositioning.position = {pos[1] + 49, pos[2] + 24} + textPositioning.horizontalAnchor = "left" + local text = NAME_PREFIX .. + (hovering and (speaker.muted and "^#31d2f7;Unmute^reset; " or "^#f43030;Mute^reset; ") or "") + .. speaker.name + canvas:drawText(text, textPositioning, 16, nil, nil, nil, FONT_DIRECTIVES) + drawable.image = speaker.muted and FRONT_MUTED_INDICATOR_IMAGE or FRONT_INDICATOR_IMAGE + canvas:drawDrawable(drawable, pos) + + if hovering then + hoveredSpeaker = speaker + hoveredSpeakerIndex = i + hoveredSpeakerPosition = pos + --if input.key("LShift") then + -- textPositioning.position = {pos[1] + 288, pos[2] + 24} + -- textPositioning.horizontalAnchor = "right" + -- canvas:drawText("^#fff7;" .. tostring(speaker.speakerId), textPositioning, 16, nil, nil, nil, FONT_DIRECTIVES) + --end +-- + --if input.mouseDown("MouseLeft") then + -- local muted = not voice.muted(speaker.speakerId) + -- interface.queueMessage((muted and "^#f43030;Muted^reset; " or "^#31d2f7;Unmuted^reset; ") .. speaker.name, 4, 0.5) + -- voice.setMuted(speaker.speakerId, muted) + --end + end +end + +local function simulateSpeakers() + local speakers = {} + for i = 2, 5 + math.floor((math.sin(os.clock()) * 4) + .5) do + speakers[i] = { + speakerId = i, + entityId = -65536 * i, + name = "Player " .. i, + loudness = -48 + 48 * math.sin(os.clock() + (i * 0.5)), + muted = false + } + end + return speakers +end + +local function drawIndicators() + canvas:clear() + local screenSize = canvas:size() + local mousePosition = canvas:mousePosition() + local basePos = {screenSize[1] - 350, 50} + + local speakersRemaining, speakers = {}, {} + local hoveredSpeakerId = nil + if hoveredSpeaker then + if not mouseOverSpeaker(mousePosition, hoveredSpeakerPosition, 16) then + hoveredSpeaker = nil + else + hoveredSpeakerId = hoveredSpeaker.speakerId + end + end + + local speakerCount = 0 + for i, speaker in pairs(voice.speakers()) do + local speakerId = speaker.speakerId + speakersRemaining[speakerId] = true + if speakerId == hoveredSpeakerId then + hoveredSpeaker = speaker + else + speakerCount = speakerCount + 1 + speakers[speakerCount] = speaker + end + end + + if hoveredSpeaker then + local len = #speakers + if hoveredSpeakerIndex > len then + for i = len + 1, hoveredSpeakerIndex - 1 do + speakers[i] = false + end + speakers[hoveredSpeakerIndex] = hoveredSpeaker + else + table.insert(speakers, hoveredSpeakerIndex, hoveredSpeaker) + end + end + + for i, v in pairs(speakers) do + if v then + local entityId = v.entityId + local loudness = v.loudness + local pos = {basePos[1], basePos[2] + (i - 1) * 52} + drawSpeakerBar(mousePosition, pos, v, i) + end + end +end + +function module.init() + canvas = interface.bindCanvas("voice", true) +end + +function module.update() + drawIndicators() +end \ No newline at end of file diff --git a/assets/opensb/scripts/opensb/util/modules.lua b/assets/opensb/scripts/opensb/util/modules.lua new file mode 100644 index 0000000..2ce7086 --- /dev/null +++ b/assets/opensb/scripts/opensb/util/modules.lua @@ -0,0 +1,31 @@ +modules = setmetatable({}, {__call = function(this, path, names) + for i, name in pairs(names) do + require(path .. name .. ".lua") + end +end}) + +local modules, type = modules, type +local function call(func, ...) + if type(func) == "function" then + return func(...) + end +end + +function init(...) + script.setUpdateDelta(1) + for i, module in pairs(modules) do + call(module.init, ...) + end +end + +function update(...) + for i, module in pairs(modules) do + call(module.update, ...) + end +end + +function uninit(...) + for i, module in pairs(modules) do + call(module.uninit, ...) + end +end \ No newline at end of file diff --git a/assets/opensb/scripts/universeClient/opensb.lua b/assets/opensb/scripts/universeClient/opensb.lua deleted file mode 100644 index c1b4dea..0000000 --- a/assets/opensb/scripts/universeClient/opensb.lua +++ /dev/null @@ -1,29 +0,0 @@ -submodules = {} - -require "/scripts/universeClient/opensb/voice_manager.lua" - -local submodules, type = submodules, type -local function call(func, ...) - if type(func) == "function" then - return func(...) - end -end - -function init(...) - script.setUpdateDelta(1) - for i, module in pairs(submodules) do - call(module.init, ...) - end -end - -function update(...) - for i, module in pairs(submodules) do - call(module.update, ...) - end -end - -function uninit(...) - for i, module in pairs(submodules) do - call(module.uninit, ...) - end -end \ No newline at end of file diff --git a/assets/opensb/scripts/universeClient/opensb/voice_manager.lua b/assets/opensb/scripts/universeClient/opensb/voice_manager.lua deleted file mode 100644 index d1c540f..0000000 --- a/assets/opensb/scripts/universeClient/opensb/voice_manager.lua +++ /dev/null @@ -1,183 +0,0 @@ --- Manages the voice HUD indicators and click-to-mute/unmute. - -local fmt = string.format -local sqrt = math.sqrt - -local module = {} -submodules.voice_manager = module - ---constants -local INDICATOR_PATH = "/interface/voicechat/indicator/" -local BACK_INDICATOR_IMAGE = INDICATOR_PATH .. "back.png" -local FRONT_INDICATOR_IMAGE = INDICATOR_PATH .. "front.png" -local FRONT_MUTED_INDICATOR_IMAGE = INDICATOR_PATH .. "front_muted.png" -local INDICATOR_SIZE = {300, 48} -local LINE_PADDING = 12 -local LINE_WIDTH = 296 -local LINE_WIDTH_PADDED = LINE_WIDTH - LINE_PADDING -local LINE_COLOR = {50, 210, 255, 255} -local FONT_DIRECTIVES = "?border=1;333;3337?border=1;333;3330" -local NAME_PREFIX = "^noshadow,white,set;" - -local canvas - -local linePaddingDrawable = { - image = BACK_INDICATOR_IMAGE, - position = {0, 0}, - color = LINE_COLOR, - centered = false -} - -local function getLinePadding(a, b) - linePaddingDrawable.image = BACK_INDICATOR_IMAGE .. fmt("?crop=%i;%i;%i;%i?fade=fff;1", a, 0, b, INDICATOR_SIZE[2]) - linePaddingDrawable.position[1] = a - return linePaddingDrawable; -end - -local lineDrawable = { - line = {{LINE_PADDING, 24}, {10, 24}}, - width = 48, - color = LINE_COLOR -} - -local function drawTheLine(pos, value) - local width = math.floor((LINE_WIDTH * value) + 0.5) - LINE_COLOR[4] = 255 * math.min(1, sqrt(width / 350)) - if width > 0 then - canvas:drawDrawable(getLinePadding(0, math.min(12, width)), pos) - if width > 12 then - lineDrawable.line[2][1] = math.min(width, LINE_WIDTH_PADDED) - canvas:drawDrawable(lineDrawable, pos) - if width > LINE_WIDTH_PADDED then - canvas:drawDrawable(getLinePadding(LINE_WIDTH_PADDED, width), pos) - end - end - end -end - -local drawable = { - image = BACK_INDICATOR_IMAGE, - centered = false -} - -local textPositioning = { - position = {0, 0}, - horizontalAnchor = "left", - verticalAnchor = "mid" -} - -local hoveredSpeaker = nil -local hoveredSpeakerIndex = 1 -local hoveredSpeakerPosition = {0, 0} -local function mouseOverSpeaker(mouse, pos, expand) - expand = tonumber(expand) or 0 - return (mouse[1] > pos[1] - expand and mouse[1] < pos[1] + 300 + expand) - and (mouse[2] > pos[2] - expand and mouse[2] < pos[2] + 48 + expand) -end - -local function drawSpeakerBar(mouse, pos, speaker, i) - drawable.image = BACK_INDICATOR_IMAGE - canvas:drawDrawable(drawable, pos) - drawTheLine(pos, 1 - math.sqrt(math.min(1, math.max(0, speaker.loudness / -50)))) - local hovering = not speaker.isLocal and mouseOverSpeaker(mouse, pos) - - textPositioning.position = {pos[1] + 49, pos[2] + 24} - textPositioning.horizontalAnchor = "left" - local text = NAME_PREFIX .. - (hovering and (speaker.muted and "^#31d2f7;Unmute^reset; " or "^#f43030;Mute^reset; ") or "") - .. speaker.name - canvas:drawText(text, textPositioning, 16, nil, nil, nil, FONT_DIRECTIVES) - drawable.image = speaker.muted and FRONT_MUTED_INDICATOR_IMAGE or FRONT_INDICATOR_IMAGE - canvas:drawDrawable(drawable, pos) - - if hovering then - hoveredSpeaker = speaker - hoveredSpeakerIndex = i - hoveredSpeakerPosition = pos - --if input.key("LShift") then - -- textPositioning.position = {pos[1] + 288, pos[2] + 24} - -- textPositioning.horizontalAnchor = "right" - -- canvas:drawText("^#fff7;" .. tostring(speaker.speakerId), textPositioning, 16, nil, nil, nil, FONT_DIRECTIVES) - --end --- - --if input.mouseDown("MouseLeft") then - -- local muted = not voice.muted(speaker.speakerId) - -- interface.queueMessage((muted and "^#f43030;Muted^reset; " or "^#31d2f7;Unmuted^reset; ") .. speaker.name, 4, 0.5) - -- voice.setMuted(speaker.speakerId, muted) - --end - end -end - -local function simulateSpeakers() - local speakers = {} - for i = 2, 5 + math.floor((math.sin(os.clock()) * 4) + .5) do - speakers[i] = { - speakerId = i, - entityId = -65536 * i, - name = "Player " .. i, - loudness = -48 + 48 * math.sin(os.clock() + (i * 0.5)), - muted = false - } - end - return speakers -end - -local function drawIndicators() - canvas:clear() - local screenSize = canvas:size() - local mousePosition = canvas:mousePosition() - local basePos = {screenSize[1] - 350, 50} - - -- sort it ourselves for now - local speakersRemaining, speakers = {}, {} - local hoveredSpeakerId = nil - if hoveredSpeaker then - if not mouseOverSpeaker(mousePosition, hoveredSpeakerPosition, 16) then - hoveredSpeaker = nil - else - hoveredSpeakerId = hoveredSpeaker.speakerId - end - end - - local speakerCount = 0 - local now = os.clock() - for i, speaker in pairs(voice.speakers()) do - local speakerId = speaker.speakerId - speakersRemaining[speakerId] = true - if speakerId == hoveredSpeakerId then - hoveredSpeaker = speaker - else - speakerCount = speakerCount + 1 - speakers[speakerCount] = speaker - end - end - - if hoveredSpeaker then - local len = #speakers - if hoveredSpeakerIndex > len then - for i = len + 1, hoveredSpeakerIndex - 1 do - speakers[i] = false - end - speakers[hoveredSpeakerIndex] = hoveredSpeaker - else - table.insert(speakers, hoveredSpeakerIndex, hoveredSpeaker) - end - end - - for i, v in pairs(speakers) do - if v then - local entityId = v.entityId - local loudness = v.loudness - local pos = {basePos[1], basePos[2] + (i - 1) * 52} - drawSpeakerBar(mousePosition, pos, v, i) - end - end -end - -function module.init() - canvas = interface.bindCanvas("voice", true) -end - -function module.update() - drawIndicators() -end \ No newline at end of file -- cgit v1.2.3