#****************************************************************************
# UserMusic
# Copyright © 2006 Gas Powered Games, Inc.  All rights reserved.
#
#****************************************************************************

#****************************************************************************
# Config options
#****************************************************************************

# List of battle cues to cycle through
local BattleCues = {
    Sound({Cue = 'Battle_01', Bank = 'Music'}),
    Sound({Cue = 'Battle_02', Bank = 'Music'}),
    Sound({Cue = 'Battle_03', Bank = 'Music'}),
    Sound({Cue = 'Battle_04', Bank = 'Music'}),
    Sound({Cue = 'Battle_05', Bank = 'Music'}),
    Sound({Cue = 'Battle_06', Bank = 'Music'}),
    Sound({Cue = 'Battle_07', Bank = 'Music'}),
    Sound({Cue = 'Battle_08', Bank = 'Music'}),
}

# List of peace cues to cycle through
local PeaceCues = {
    Sound( { Cue = 'Base_Building_01', Bank = 'Music' } ),
    Sound( { Cue = 'Base_Building_02', Bank = 'Music' } ),
    Sound( { Cue = 'Base_Building_03', Bank = 'Music' } ),
    Sound( { Cue = 'Base_Building_04', Bank = 'Music' } ),
    Sound( { Cue = 'Base_Building_05', Bank = 'Music' } ),
    Sound( { Cue = 'Base_Building_06', Bank = 'Music' } ),
    Sound( { Cue = 'Base_Building_07', Bank = 'Music' } ),
    Sound( { Cue = 'Base_Building_08', Bank = 'Music' } ),
}

# How many battle events do we receive before switching to battle music
local BattleEventThreshold = 20

# Current count of battle events
local BattleEventCounter = 0

# How many ticks can elapse between NotifyBattle events before we reset the
# BattlceEventCounter (only used in peace time)
local BattleCounterReset = 30 # 3 seconds

# How many ticks of battle inactivity until we switch back to peaceful music
local PeaceTimer = 200 # 20 seconds

#****************************************************************************
# Internal
#****************************************************************************

# The last tick in which we got a battle notification
local LastBattleNotify = 0

# Current music loop if music is active
local Music = false

# Watcher thread
local MusicThread = false

local BattleCueIndex = 1
local PeaceCueIndex = 1

function NotifyBattle()

    if not MusicThread then
        #LOG("*** CREATING MUSIC THREAD")
        MusicThread = ForkThread( MusicWatcher )
    end

    LastBattleNotify = GameTick()
    BattleEventCounter = BattleEventCounter + 1
    #LOG("*** Battle Counter : ", BattleEventCounter)
end

function MusicWatcher()

    # Tick when battle started
    local BattleStart = 0

    while true do
        local tick = GameTick()

        if BattleStart > 0 then
            if tick - LastBattleNotify > PeaceTimer then
                BattleStart = 0
                BattleEventCounter = 0

                StartPeaceMusic()
                #LOG('*** Peace Time')
            end
        else
            if BattleEventCounter > BattleEventThreshold then
                BattleStart = tick

                StartBattleMusic()
                #LOG('*** Battle Time!')
            else
                if BattleEventCounter > 0 and tick - LastBattleNotify > BattleCounterReset then
                    #LOG('*** counter reset')
                    BattleEventCounter = 0
                end
            end
        end

        WaitSeconds(1)
    end
end

function StartBattleMusic()
    #LOG("*** StartBattleMusic()")
    if Music then
        StopSound(Music,true) # true means stop immediately
        Music = false
    end
    #LOG('*** PLAYING BATTLE ', BattleCueIndex)
    Music = PlaySound( BattleCues[BattleCueIndex] )
    BattleCueIndex = math.mod(BattleCueIndex,table.getn(BattleCues)) + Random(1, 8)
    
end

function StartPeaceMusic()
    #LOG("*** StartPeaceMusic()")
    ForkThread(
        function()
            if Music then
                #LOG('*** FADING MUSIC')
                StopSound(Music) # fade out
                WaitFor(Music)
                #LOG('**** DONE FADE')
                Music = false
            end

            WaitSeconds(3)

            # It's possible Music got set while we were waiting
            if not Music then
                #LOG('*** PLAYING PEACE ', PeaceCueIndex)
                Music = PlaySound( PeaceCues[PeaceCueIndex] )
                PeaceCueIndex = math.mod(PeaceCueIndex,table.getn(PeaceCues)) + Random(1, 8)
            end
        end
    )
end

