2022-07-28 17:28:11 +00:00
--@name DECTalk
--@author Minori
--@include https://gist.githubusercontent.com/sr229/5e6f3a5b03181704a871207c14706499/raw/2ee5b6cfb3a875a5ad2024a38d0131cfb5278785/url-encode.lua as henke.txt
2022-11-25 17:11:36 +00:00
--@shared
2022-12-12 17:56:13 +00:00
local references = { }
local DEBUG = true
2022-07-28 17:28:11 +00:00
require ( " henke.txt " )
2022-11-25 17:11:36 +00:00
if SERVER then
-- haaugh
2022-12-12 17:56:13 +00:00
--chip():setNoDraw(true)
2022-11-25 17:11:36 +00:00
hook.add ( " PlayerSay " , " tts_msg " , function ( ply , txt )
-- instead of limiting to owner()
-- we do a little trolling and give it to everyone
if ply and string.sub ( txt , 1 , 1 ) == " ; " then
2022-12-12 17:56:13 +00:00
local plyAuthor = ply : getUserID ( )
local cleanedMsg = string.sub ( txt , 2 )
if # cleanedMsg > 1024 then
-- Ignore message more than 1024 characters
if DEBUG then
print ( string.format ( " SERVER: Ignoring message from %s, msglen %i is greater than limit (1024) " , ply : getName ( ) , # cleanedMsg ) )
end
else
net.start ( " aeiou " )
net.writeInt ( plyAuthor , 32 )
net.writeString ( cleanedMsg )
net.send ( )
end
2022-11-25 17:11:36 +00:00
-- do not broadcast original msg to client
-- BUG: only works for owner()
--return ""
end
end )
end
if CLIENT then
if not hasPermission ( " bass.loadURL " , " https://tts.cyzon.us/tts " ) then return end
print ( " DECTalk loaded on client. Type ;<text> to use it. " )
2022-07-28 17:28:11 +00:00
2022-11-25 17:11:36 +00:00
net.receive ( " aeiou " , function ( )
local plyAuthor = player ( net.readInt ( 32 ) )
local msg = net.readString ( )
2022-07-28 17:28:11 +00:00
2022-12-12 17:56:13 +00:00
if DEBUG then
print ( string.format ( " CLIENT: Playing sound from %s (%i), msg len: %i " , plyAuthor : getName ( ) , plyAuthor : getUserID ( ) , # msg ) )
end
2022-07-28 17:28:11 +00:00
2022-12-12 17:56:13 +00:00
bass.loadURL ( " https://tts.cyzon.us/tts?text= " .. urlencode ( msg ) , " 3d noblock " , function ( a , e , n )
-- since Starfall has no way to keep track of objects
-- we will have to manage the lifecycle oureselves via a local table
table.insert ( references , {
ref = a ,
timestamp = os.time ( )
} )
-- we may have ended up with a unsorted table, let's fix that
-- sort table by oldest at the top to most recent at the bottom.
table.sort ( references , function ( entryA , entryB ) return entryA.timestamp < entryB.timestamp end )
2022-07-28 17:28:11 +00:00
2022-11-25 17:11:36 +00:00
hook.add ( " Think " , " followSound " , function ( )
2022-12-12 17:56:13 +00:00
a : setPos ( plyAuthor : getPos ( ) )
2022-11-25 17:11:36 +00:00
end )
2022-07-28 17:28:11 +00:00
2022-12-12 17:56:13 +00:00
a : setVolume ( 1.5 )
a : play ( )
-- let's dispose the oldest ref
-- but make sure we keep two references alive
-- so we don't end up disposing too early :(
if # references ~= 0 and # references > 2 then
if DEBUG then
print ( string.format ( " Destroying oldest reference, ts: %i " , references [ 1 ] . timestamp ) )
end
references [ 1 ] . ref : destroy ( )
table.remove ( references , 1 )
end
2022-11-25 17:11:36 +00:00
end )
2022-07-28 17:28:11 +00:00
end )
2022-11-25 17:11:36 +00:00
end