public void PlayerReplyTurn()
    {
        var playerAgent = rpcList.Find(x => x.CharacterName == _chosenCharacter);


        var decidedList = playerAgent.Decide();
        var action      = decidedList.FirstOrDefault();

        IEnumerable <DialogueStateActionDTO> availableDialogs = new DialogueStateActionDTO[1];
        List <DialogueStateActionDTO>        dialogs          = new List <DialogueStateActionDTO>();

        stopTime = true;


        Name currentState = action.Parameters[0];
        Name nextState    = action.Parameters[1];
        Name meaning      = action.Parameters[2];

        Debug.Log(" meaning: " + meaning);
        Name style = Name.BuildName("*");

        dialogs = _iat.GetDialogueActions(currentState, nextState, meaning, style);
        //Debug.Log(" dialog: " + dialogs.Count +  " first: " + dialogs[0].Utterance);
        availableDialogs = dialogs;


        UpdateButtonTexts(false, availableDialogs);
    }
    private void UpdateButtonTexts(bool hide, List <IAction> decisions)
    {
        if (hide)
        {
            if (!m_buttonList.Any())
            {
                return;
            }
            foreach (var b in m_buttonList)
            {
                Destroy(b.gameObject);
            }
            m_buttonList.Clear();
        }
        else
        {
            var maxUtility      = decisions.Max(x => x.Utility);
            var playerDecisions = decisions.Where(x => x.Utility == maxUtility);
            int i = 0;
            foreach (var a in playerDecisions)
            {
                var dialogueOptions = _iat.GetDialogueActions(a.Parameters.ElementAt(0), a.Parameters.ElementAt(1), a.Parameters.ElementAt(2), a.Parameters.ElementAt(3));
                //     var block = new ColorBlock();
                //    block.normalColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
                //   block.highlightedColor = new Color(1.0f, 1.0f, 1.0f, 0.0f);
                //   block.pressedColor = new  Color(0.0f, 1.0f, 1.0f, 0.0f);
                //  block.disabledColor = new Color(1.0f, 1.0f, 1.0f, 0.0f);
                //    block.colorMultiplier = 1.0f;
                //   m_dialogButtonArchetype.colors = block;



                foreach (var d in dialogueOptions)
                {
                    if (isInButtonList(d.Utterance))
                    {
                        continue;
                    }
                    var b = Instantiate(m_dialogButtonArchetype);


                    b.GetComponent <Image>().color = Color.red;


                    var t = b.transform;
                    i += 1;
                    t.SetParent(m_dialogButtonZone, false);

                    b.GetComponentInChildren <Text>().text = i + ": " + "[Towards " + a.Target + "] - " + d.Utterance;
                    var id = d.Id;
                    b.onClick.AddListener((() => Reply(id, a.Target)));
                    m_buttonList.Add(b);
                }
            }
        }
    }
Example #3
0
    void ChooseDialogue(IAction action, Name initiator)
    {
        Debug.Log(" The agent " + initiator + " decided to perform " + action.Name + " towards " + action.Target);

        //                                          NTerm: 0     1     2     3     4
        // If it is a speaking action it is composed by Speak ( [ms], [ns] , [m}, [sty])
        var currentState = action.Name.GetNTerm(1);
        var nextState    = action.Name.GetNTerm(2);
        var meaning      = action.Name.GetNTerm(3);
        var style        = action.Name.GetNTerm(4);


        // Returns a list of all the dialogues given the parameters but in this case we only want the first element
        var dialog = _iat.GetDialogueActions(currentState, nextState, meaning, style).FirstOrDefault();


        if (dialog != null)
        {
            Reply(dialog.Id, initiator, action.Target);
        }
    }
Example #4
0
    private List <DialogueStateActionDTO> DeterminePlayerDialogues()
    {
        var actions = playerRPC.Decide().ToArray();
        var dOpt    = new List <DialogueStateActionDTO>();

        foreach (var action in actions)
        {
            if (action.Key.ToString().Equals(IATConsts.DIALOG_ACTION_KEY))
            {
                Name currentState = action.Parameters[0];
                Name nextState    = action.Parameters[1];
                Name meaning      = action.Parameters[2];
                Name style        = action.Parameters[3];
                var  dialogs      = iat.GetDialogueActions(currentState, nextState, meaning, style);
                dOpt.AddRange(dialogs);
            }
        }
        dOpt = dOpt.Distinct().ToList();
        var additional = iat.GetDialogueActionsByState("Any");

        dOpt.AddRange(additional);
        return(dOpt);
    }
Example #5
0
        private void decide(SuecaEvent ev)
        {
            //Console.WriteLine("DECING FOR EVENT: " + ev.Name);
            string[] tags     = ev.Tags.ToArray();
            string[] meanings = ev.Meanings.ToArray();

            IEnumerable <ActionLibrary.IAction> possibleActions = _rpc.Decide();

            if (possibleActions == null || possibleActions.IsEmpty())
            {
                //Console.WriteLine("No action");
                return;
            }
            else
            {
                ActionLibrary.IAction chosenAction = possibleActions.FirstOrDefault();
                saveToFile();

                switch (chosenAction.Key.ToString())
                {
                case "Speak":
                    Name currentState = chosenAction.Parameters[0];
                    Name nextState    = chosenAction.Parameters[1];
                    Name meaning      = chosenAction.Parameters[2];
                    Name style        = chosenAction.Parameters[3];

                    var possibleDialogs = _iat.GetDialogueActions(IATConsts.AGENT, currentState, nextState, meaning, style);
                    var dialog          = getUtterance(possibleDialogs);

                    Console.WriteLine(dialog);
                    EmotionalSuecaPlayer.SuecaPub.PerformUtteranceWithTags("", dialog, tags, meanings);
                    break;

                case "Animation":
                    Name state       = chosenAction.Parameters[0];
                    Name emotionName = chosenAction.Parameters[1];
                    Console.WriteLine("[ANIMATION] Soft reaction to " + state + " with the style " + emotionName);
                    break;

                default:
                    Console.WriteLine("Unknown Action");
                    break;
                }
            }
        }
Example #6
0
        public static Action ToAction(ActionLibrary.IAction a, IntegratedAuthoringToolAsset IAT)
        {
            Char[]   delimiters = { '(', ',', ' ', ')' };
            String[] splitted   = a.Name.ToString().Split(delimiters);

            switch (splitted[0])
            {
            case "Action":
                return(new DSTAction(splitted[0], a.Target.ToString(), splitted[1], splitted[3], splitted[5], splitted[7], splitted[9], a.Name.ToString()));

            case "Speak":
                var dialog = IAT.GetDialogueActions(
                    a.Parameters[0],
                    a.Parameters[1],
                    a.Parameters[2],
                    a.Parameters[3]).FirstOrDefault();
                return(new SpeakAction(splitted[0], a.Target.ToString(), splitted[1], splitted[3], splitted[5], splitted[7], a.Name.ToString(), dialog.Utterance));

            default:
                throw new Exception("This type of action (" + splitted[0] + ") is not recognized");
            }
        }
    // Update is called once per frame
    void UpdateFunction()
    {
        IAction finalDecision = null;
        String  agentName     = "";

        // a simple cycle to go through all the agents and get their decision
        foreach (var rpc in _rpcList)
        {
            // From all the decisions the rpc wants to perform we want the first one
            var decision = rpc.Decide().FirstOrDefault();

            if (decision != null)
            {
                agentName     = rpc.CharacterName.ToString();
                finalDecision = decision;
                break;
            }
        }

        //If there was a decision I want to print it

        if (finalDecision != null)

        {
            Debug.Log(" The agent " + agentName + " decided to perform " + finalDecision.Name);

            //                                          NTerm: 0     1     2     3     4
            // If it is a speaking action it is composed by Speak ( [ms], [ns] , [m}, [sty])
            var currentState = finalDecision.Name.GetNTerm(1);
            var nextState    = finalDecision.Name.GetNTerm(2);
            var meaning      = finalDecision.Name.GetNTerm(3);
            var style        = finalDecision.Name.GetNTerm(4);


            // Returns a list of all the dialogues given the parameters
            var dialog = _iat.GetDialogueActions(currentState, nextState, meaning, style).FirstOrDefault();

            //Let's print all available dialogues:

            if (dialog != null)
            {
                Debug.Log(agentName + " says: " + dialog.Utterance + " towards " + finalDecision.Target);
            }


            var actualCurrentState = dialog.CurrentState;
            var actualNextState    = dialog.NextState;
            var actualMeaning      = dialog.Meaning;
            var actualStyle        = dialog.Style;

            var actualActionName = "Speak(" + actualCurrentState + ", " + actualNextState + ", " + actualMeaning +
                                   ", " + actualStyle + ")";



            var eventName = EventHelper.ActionEnd((Name)agentName, (Name)actualActionName, finalDecision.Target);

            var consequences = _worldModel.Simulate(new Name[] { eventName });


            foreach (var eff in consequences)
            {
                Debug.Log("Effect: " + eff.PropertyName + " " + eff.NewValue + " " + eff.ObserverAgent);

                foreach (var rpc in _rpcList)
                {
                    if (eff.ObserverAgent == rpc.CharacterName || eff.ObserverAgent == (Name)"*")
                    {
                        rpc.Perceive(EventHelper.PropertyChange(eff.PropertyName, eff.NewValue, eff.ObserverAgent));
                        ;
                    }
                }
            }
        }

        // else there was no decision
    }
Example #8
0
        private void decide(SuecaEvent ev)
        {
            Console.WriteLine(_agentName + "---" + "DECING FOR EVENT: " + ev.Name);
            string[] tags     = ev.Tags.ToArray();
            string[] meanings = ev.Meanings.ToArray();

            try
            {
                IEnumerable <ActionLibrary.IAction> possibleActions = _rpc.Decide();

                if (possibleActions == null || possibleActions.IsEmpty())
                {
                    Console.WriteLine(_agentName + "---" + "No action");
                    saveToFile();
                    return;
                }
                else
                {
                    ActionLibrary.IAction chosenAction = possibleActions.FirstOrDefault();
                    saveToFile();

                    switch (chosenAction.Key.ToString())
                    {
                    case "Speak":

                        Name currentState    = chosenAction.Parameters[0];
                        Name nextState       = chosenAction.Parameters[1];
                        Name meaning         = chosenAction.Parameters[2];
                        Name style           = chosenAction.Parameters[3];
                        var  possibleDialogs = _iat.GetDialogueActions(IATConsts.AGENT, currentState, nextState, meaning, style);
                        var  dialog          = getUtterance(possibleDialogs);

                        Console.WriteLine(_agentName + "---" + dialog);
                        EmotionalSuecaPlayer.SuecaPub.StartedUtterance(_esp._id, ev.Name, "");
                        EmotionalSuecaPlayer.SuecaPub.PerformUtteranceWithTags("", dialog, tags, meanings);

                        break;

                    case "Animation":
                        Name   state       = chosenAction.Parameters[0];
                        string emotionName = chosenAction.Parameters[1].ToString().ToLower();
                        if (emotionName == "distress" || emotionName == "shame")
                        {
                            emotionName = "sadness";
                        }
                        else if (emotionName == "pride")
                        {
                            emotionName = "joy";
                        }
                        else if (emotionName == "reproach")
                        {
                            emotionName = "anger";
                        }
                        Console.WriteLine("[ANIMATION] reaction to " + state + " with the style " + emotionName);
                        EmotionalSuecaPlayer.SuecaPub.PlayAnimation("", emotionName + "3");
                        break;

                    default:
                        Console.WriteLine(_agentName + "---" + "Unknown Action");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(_agentName + "---" + e.ToString());
            }
        }
    // Update is called once per frame
    void Update()
    {
        if (_agentController == null)
        {
            return;
        }

        if (!_agentController.IsRunning)
        {
            return;
        }


        if (_agentController._body._speechController.IsPlaying)
        {
            return;
        }

        if (_agentController.getJustReplied())
        {
            var reply = _agentController.getReply();


            HandleEffects(new List <Name> {
                EventHelper.ActionEnd(_agentController.RPC.CharacterName, (Name)("Speak(" + reply.CurrentState.ToString() + "," + reply.NextState.ToString() + "," + reply.Meaning.ToString() + "," + reply.Style.ToString() + ")"), (Name)"Player")
            });



            waitingforReply = false;
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            if (Time.timeScale > 0)
            {
                Time.timeScale = 0;
            }
            else
            {
                Time.timeScale = 1;
            }
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            this.Restart();
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            this.SaveState();
        }

        _agentController.UpdateEmotionExpression();

        if (Player != null)
        {
            var decision = Player.Decide().FirstOrDefault();

            if (decision == null || waitingforReply)
            {
                return;
            }

            if (decision.Target != _agentController.RPC.CharacterName)
            {
                return;
            }
            var dialogActions = _iat.GetDialogueActions(decision.Parameters.ElementAt(0), Name.BuildName("*"), Name.BuildName("*"), Name.BuildName("*"));

            UpdateButtonTexts(false, dialogActions);

            waitingforReply = true;
        }
    }
Example #10
0
    public IEnumerator HandleSpeak(IAction speakAction)
    {
        //m_rpc.Perceive(new [] { EventHelper.ActionStart(m_rpc.CharacterName.ToString(), speakAction.Name.ToString(), IATConsts.PLAYER) });


        Name currentState = speakAction.Parameters[0];
        Name nextState    = speakAction.Parameters[1];
        Name meaning      = speakAction.Parameters[2];
        Name style        = speakAction.Parameters[3];
        var  Target       = speakAction.Target;

        var dialogs = m_iat.GetDialogueActions(currentState, nextState, meaning, style);

        //      Debug.Log("Here we go speaking: " + currentState.ToString() + " ns " + nextState.ToString() + " meaning " + meaning.ToString());

        var dialog = dialogs.Shuffle().FirstOrDefault();

        //         Debug.Log("Going to say: " + dialog.Utterance);

        if (dialog == null)
        {
            Debug.LogWarning("Unknown dialog action.");
            m_dialogController.AddDialogLine("... (unkown dialogue) ...");
        }
        else
        {
            this.setFloor(false);
            string subFolder = m_scenarioData.TTSFolder;
            if (subFolder != "<none>")
            {
                var path         = string.Format("/TTS-Dialogs/{0}/{1}/{2}", subFolder, m_rpc.VoiceName, dialog.UtteranceId);
                var absolutePath = Application.streamingAssetsPath;
#if UNITY_EDITOR || UNITY_STANDALONE
                absolutePath = "file://" + absolutePath;
#endif
                string audioUrl = absolutePath + path + ".wav";
                string xmlUrl   = absolutePath + path + ".xml";

                var audio = new WWW(audioUrl);
                var xml   = new WWW(xmlUrl);

                yield return(audio);

                yield return(xml);

                var xmlError   = !string.IsNullOrEmpty(xml.error);
                var audioError = !string.IsNullOrEmpty(audio.error);

                if (xmlError)
                {
                    Debug.LogError(xml.error);
                }
                if (audioError)
                {
                    Debug.LogError(audio.error);
                }

                m_dialogController.AddDialogLine(dialog.Utterance);

                if (xmlError || audioError)
                {
                    yield return(new WaitForSeconds(2));
                }
                else
                {
                    var clip = audio.GetAudioClip(false);
                    yield return(_body.PlaySpeech(clip, xml.text));

                    clip.UnloadAudioData();
                }
            }
            else
            {
                m_dialogController.AddDialogLine(dialog.Utterance);
                yield return(new WaitForSeconds(2));
            }

            if (RPC.GetBeliefValue("HasFloor(SELF)") != "True") //todo: replace with a constant
            {
                this.SetDialogueState(Target.ToString(), nextState.ToString());
                reply       = dialog;
                just_talked = true;
            }
        }


        if (nextState.ToString() == "Disconnect")
        {
            this.End();
        }
    }
        public IEnumerator HandleSpeak(IAction speakAction)
        {
            lastAction = speakAction;
            Name currentState = speakAction.Parameters[0];
            Name nextState    = speakAction.Parameters[1];
            Name meaning      = speakAction.Parameters[2];
            Name style        = speakAction.Parameters[3];

            m_rpc.SaveToFile(m_rpc.CharacterName + "-output" + ".rpc");

            var dialog = m_iat.GetDialogueActions(currentState, nextState, meaning, style).FirstOrDefault();

            if (dialog == null)
            {
                Debug.LogWarning("Unknown dialog action.");
                m_dialogController.AddDialogLine("... (unkown dialogue) ...");
            }
            else
            {
                string subFolder = m_scenarioData.TTSFolder;
                if (subFolder != "<none>")
                {
                    var provider = (AssetManager.Instance.Bridge as AssetManagerBridge)._provider;
                    var path     = string.Format("/TTS-Dialogs/{0}/{1}/{2}", subFolder, m_rpc.VoiceName, dialog.UtteranceId);

                    AudioClip clip = null; //Resources.Load<AudioClip>(path);
                    string    xml  = null; //Resources.Load<TextAsset>(path);

                    var xmlPath = path + ".xml";
                    if (provider.FileExists(xmlPath))
                    {
                        try
                        {
                            using (var xmlStream = provider.LoadFile(xmlPath, FileMode.Open, FileAccess.Read))
                            {
                                using (var reader = new StreamReader(xmlStream))
                                {
                                    xml = reader.ReadToEnd();
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.LogException(e);
                        }

                        if (!string.IsNullOrEmpty(xml))
                        {
                            var wavPath = path + ".wav";
                            if (provider.FileExists(wavPath))
                            {
                                try
                                {
                                    using (var wavStream = provider.LoadFile(wavPath, FileMode.Open, FileAccess.Read))
                                    {
                                        var wav = new WavStreamReader(wavStream);

                                        clip = AudioClip.Create("tmp", (int)wav.SamplesLength, wav.NumOfChannels, (int)wav.SampleRate, false);
                                        clip.SetData(wav.GetRawSamples(), 0);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Debug.LogException(e);
                                    if (clip != null)
                                    {
                                        clip.UnloadAudioData();
                                        clip = null;
                                    }
                                }
                            }
                        }
                    }

                    if (clip != null && xml != null)
                    {
                        yield return(_body.PlaySpeech(clip, xml));

                        clip.UnloadAudioData();
                    }
                    else
                    {
                        Debug.LogWarning("Could not found speech assets for a dialog");
                        yield return(new WaitForSeconds(2));
                    }
                }
                else
                {
                    yield return(nextframe);
                }

                if (nextState.ToString() != "-")                 //todo: replace with a constant
                {
                    AddEvent(string.Format("Event(Property-change,Suspect,DialogueState(Player),{0})", nextState));
                }
            }

            if (speakAction.Parameters[1].ToString() != "-")             //todo: replace with a constant
            {
                var dialogueStateUpdateEvent = string.Format("Event(Property-Change, Suspect ,DialogueState({0}),{1})", speakAction.Target, speakAction.Parameters[1]);
                AddEvent(dialogueStateUpdateEvent);
            }
            if (nextState.ToString() == "Disconnect")
            {
                this.End();
            }

            m_rpc.Perceive(new Name[] { EventHelper.ActionEnd(m_rpc.CharacterName.ToString(), speakAction.Name.ToString(), IATConsts.PLAYER) });

            yield return(new WaitForSeconds(0.1f));

            m_dialogController.AddDialogLine(dialog.Utterance);
            reply       = dialog;
            just_talked = true;
        }
Example #12
0
        private IEnumerator HandleSpeak(IAction speakAction)
        {
            Name currentState = speakAction.Parameters[0];
            Name nextState    = speakAction.Parameters[1];
            Name meaning      = speakAction.Parameters[2];
            Name style        = speakAction.Parameters[3];

            var dialogs = m_iat.GetDialogueActions(currentState, nextState, meaning, style);


            var dialog = dialogs.Shuffle().FirstOrDefault();


            if (dialog == null)
            {
                Debug.LogWarning("Unknown dialog action.");
                m_dialogController.AddDialogLine("... (unkown dialogue) ...");
            }
            else
            {
                string subFolder = m_scenarioData.TTSFolder;
                if (subFolder != "<none>")
                {
                    var path         = string.Format("/TTS-Dialogs/{0}/{1}/{2}", subFolder, m_rpc.VoiceName, dialog.UtteranceId);
                    var absolutePath = Application.streamingAssetsPath;
#if UNITY_EDITOR || UNITY_STANDALONE
                    absolutePath = "file://" + absolutePath;
#endif
                    string audioUrl = absolutePath + path + ".wav";
                    string xmlUrl   = absolutePath + path + ".xml";

                    var audio = new WWW(audioUrl);
                    var xml   = new WWW(xmlUrl);

                    yield return(audio);

                    yield return(xml);

                    var xmlError   = !string.IsNullOrEmpty(xml.error);
                    var audioError = !string.IsNullOrEmpty(audio.error);

                    if (xmlError)
                    {
                        Debug.LogError(xml.error);
                    }
                    if (audioError)
                    {
                        Debug.LogError(audio.error);
                    }

                    m_dialogController.AddDialogLine(dialog.Utterance);

                    if (xmlError || audioError)
                    {
                        yield return(new WaitForSeconds(2));
                    }
                    else
                    {
                        var clip = audio.GetAudioClip(false);
                        yield return(_body.PlaySpeech(clip, xml.text));

                        clip.UnloadAudioData();
                    }
                }
                else
                {
                    m_dialogController.AddDialogLine(dialog.Utterance);
                    yield return(new WaitForSeconds(2));
                }

                reply       = dialog;
                just_talked = true;
            }


            if (nextState.ToString() == "Disconnect")
            {
                this.End();
            }

            AddEvent(EventHelper.ActionEnd(m_rpc.CharacterName.ToString(), speakAction.Name.ToString(), IATConsts.PLAYER).ToString());
        }