# SOURCE:
# https://lemmasoft.renai.us/forums/viewtopic.php?t=25399
# with modifications from CCL
# https://nothack-europa.itch.io/
init -10 python:
        
    # This is set to the name of the character that is speaking, or
    # None if no character is currently speaking.
    speaking = None
  
    # This returns speaking if the character is speaking, and done if the
    # character is not.
    def while_speaking(name, speak_d, done_d, st, at):
        if speaking == name:
            return speak_d, .1
        else:
            return done_d, None
  
    # Curried form of the above.
    curried_while_speaking = renpy.curry(while_speaking)
  
    # Displays speaking when the named character is speaking, and done otherwise.
    def WhileSpeaking(name, speaking_d, done_d=Null()):
        return DynamicDisplayable(curried_while_speaking(name, speaking_d, done_d))
  
    # This callback maintains the speaking variable.
    def speaker_callback(name, event, **kwargs):
        global speaking
        speaking = name
  
    # Curried form of the same.
    speaker = renpy.curry(speaker_callback)

    # This returns speaking if the character is speaking and audio is playing, and done if the
    # character is not.
    def while_speaking(name, speak_d, done_d, st, at):
        if speaking == name:
            return speak_d, .1
        else:
            return done_d, None
  
    # Curried form of the above.
    curried_while_speaking = renpy.curry(while_speaking)
  
    # Displays speaking when the named character is speaking, and done otherwise.
    def WhileSpeaking(name, speaking_d, done_d=Null()):
        return DynamicDisplayable(curried_while_speaking(name, speaking_d, done_d))
  
    # This callback maintains the speaking variable.
    def speaker_callback(name, event, **kwargs):
        global speaking

        if event == "show":
            speaking = name
        elif event == "slow_done" or event == "end":
            speaking = None
  
    # Curried form of the same.
    speaker = renpy.curry(speaker_callback)

    # Disable the current speaker.
    # No other function can set speaking to None anymore. This function fixes that.
    def disable_speaker():
        global speaking
        speaking = None
        #globals()['speaker'] = None
    
    # Set speaker to specific names. Useful if a character makes sounds without getting a textbox.
    def set_speaker(speaker):
        global speaking
        speaking = speaker
