I also added a little marker that goes on the map where the agression happened to trigger the music (F3 wasn't always helpful) ; it gets removed if peace music comeback.
>I wasn't able to find how to recognize .ogg and .wav with VFS.DirList é_è
Distributed with the TA original music it would have been very neat (for me at least, I looked for this widget). Althought I guess it's not okay for right stuff reasons :/ (even if the whole BA thing is quite totally similar in design, I guess this is the limit ?)
Code: Select all
function widget:GetInfo()
return {
name = "simple BA music",
desc = "War and peace music",
author = "Black Drake Inn",
date = "Aug 13, 2015",
license = "GNU GPL, v3 or later",
layer = 0,
enabled = true
}
end
--Can be changed
local VERBOSE=false --tell you what he is playing
local flagOnDamage=true --get (or not) a little marker on the unit damaged that trigger the war (does not stack)
local flagString='*' --what's on the marker
local BaseVolume = 1.0 --default at a fairly loud volume
local initWarTime=25 --time of war after agression (seconds) ; need to be > than the initFadingTime
local initFadingTime=5 --transition fading time between war and peace (seconds)
local warDirpath="music/TA/WAR/" --path to your music (can contrain anything *.ogg) TODO : *.wav
local peaceDirpath="music/TA/PEACE/" --path to your music (can contrain anything *.ogg)
--The above is filled automatically
local numwarTracks=0 -- number of tracks
local numpeaceTracks=0 -- number of tracks
local warTracks={} -- contains path of music
local peaceTracks={} -- contains path of music
local warSongtimes={} -- stores the duration in seconds of each song
local peaceSongtimes={} -- stores the duration in seconds of each song
local peace = true --wether is peace or not
local pause = true --wether is paused or not
local songnum=0 --song number in the list
local songtime=0 --song timer (0 = end)
local warTime=0 --war song timer (0 = end)
local fadingTime=0 --fading timer (0 = end)
local fading=false --is fading ?
local xF=0 --flag pos (all useless if flagOnDamage=false)
local yF=0 --flag pos
local zF=0 --flag pos
local waspaused=false
--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
function setsong(peace)
--local a,b=Spring.GetSoundStreamTime()--TODO experiment
--if(not(a==0) and not(b==0)) then
-- Spring.Echo("DEBUG : "..a.." <- played | total -> "..b)
--end
--Spring.StopSoundStream() --should not be of use (cracking noise causing too)
if(peace) then
songnum = math.random(1,numpeaceTracks);
songtime=peaceSongtimes[songnum]
Spring.PlaySoundStream(peaceTracks[songnum],BaseVolume)
else
songnum = math.random(1,numwarTracks);
songtime=warSongtimes[songnum]
Spring.PlaySoundStream(warTracks[songnum],BaseVolume)
end
if(VERBOSE) then
if(peace) then
Spring.Echo("start "..peaceTracks[songnum])
else
Spring.Echo("start "..warTracks[songnum])
end
end
end
function widget:Initialize()
--LOAD MUSIC DIR
warTracks=VFS.DirList(warDirpath,'*.ogg') --TODO : *.wav
peaceTracks=VFS.DirList(peaceDirpath,'*.ogg')
--SET MUSIC QTT
numwarTracks=#warTracks -- number of war tracks
numpeaceTracks=#peaceTracks -- number of peace tracks
--SET MUSIC TIMERS
Spring.SetSoundStreamVolume(0) --Cut the sound time to import the music lenght
local i=1
while(i<=numwarTracks) do
Spring.PlaySoundStream(warTracks[i])
local playedTime, totalTime = Spring.GetSoundStreamTime()
Spring.StopSoundStream()
warSongtimes[i]=totalTime
i=i+1
end
i=1
while(i<=numpeaceTracks) do
Spring.PlaySoundStream(peaceTracks[i])
local playedTime, totalTime = Spring.GetSoundStreamTime(peaceTracks[i])
Spring.StopSoundStream()
peaceSongtimes[i]=totalTime
i=i+1
end
Spring.SetSoundStreamVolume(BaseVolume) --Sound it back to normal
songtime=1 --workaround (sound seem to fail if launched too soon)
--IF SPEC, no flag (because yup.)
if(Spring.GetSpectatingState()) then
flagOnDamage=false
end
end
function widget:Update(dt)
--PAUSED?
local _, _, paused = Spring.GetGameSpeed()
if(paused) then
if(waspaused) then
--rien pause continue
else
--begin pause
Spring.PauseSoundStream()
waspaused=true
end
else
if(waspaused) then
--end pause
Spring.PauseSoundStream()
waspaused=false
else
--no pause
songtime=songtime-dt
--NEXT SONG
if(songtime<=0) then
Spring.StopSoundStream() --if not set, got two paralele stream doing strange stuff
setsong(peace)
end
--WAR IS HERE FOR A MOMENT
if(not(peace)) then
warTime=warTime-dt
if(warTime<=0) then
peace=true
fading=true
fadingTime=initFadingTime
end
else
if(fading) then
--FADDING TO PEACE
if(fadingTime>0) then
fadingTime=fadingTime-dt
Spring.SetSoundStreamVolume((BaseVolume/initFadingTime)*fadingTime)
else
--FADDING ENDED : PEACE
if(VERBOSE) then
Spring.Echo("end "..warTracks[songnum])
end
Spring.StopSoundStream()
Spring.SetSoundStreamVolume(BaseVolume)
fading=false
if(flagOnDamage) then
Spring.MarkerErasePosition(xF,yF,zF) --remove last marker
end
setsong(peace)
end
end
end
end
end
end
function widget:Shutdown()
Spring.StopSoundStream()
if(flagOnDamage) then
Spring.MarkerErasePosition(xF,yF,zF) --remove last marker
end
end
function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer, weaponDefID, projectileID, attackerID, attackerDefID, attackerTeam)
if(peace) then
if(flagOnDamage) then
--TODO team ally
if(not(unitTeam==Spring.GetMyPlayerID())) then
Spring.Echo("toggled by ally ?")
end
xF,yF,zF=Spring.GetUnitPosition(unitID)
Spring.MarkerAddPoint(xF,yF,zF,flagString,true)
end
peace=false
if(fading) then
--FADDING INTERRUPTED : WAR
fading=false
if(VERBOSE) then
Spring.Echo("back to "..peaceTracks[songnum])
end
Spring.SetSoundStreamVolume(BaseVolume) --Sound it back to normal
else
--WAR BEGINS
if(VERBOSE) then
Spring.Echo("end "..peaceTracks[songnum])
end
Spring.StopSoundStream()
setsong(peace)
end
end
warTime=initWarTime
end