private void PlayerReplyAction(string replyActionName, string nextState)
    {
        var events = new List <Name>();

        events.Add(EventHelper.ActionEnd(IATConsts.PLAYER, replyActionName, _agentController.RPC.CharacterName.ToString()));

        var effects = _wm.Simulate(events);

        foreach (var eff in effects)
        {
            var ef = eff.ToPropertyChangeEvent();

            if (eff.ObserverAgent == _agentController.RPC.CharacterName)
            {
                _agentController.AddEvent(ef);
            }
            else if (eff.ObserverAgent == Player.CharacterName)
            {
                Player.Perceive(ef);
            }
            else
            {
                _agentController.AddEvent(ef);
                Player.Perceive(ef);
            }
        }

        _agentController.canSpeak = true;
        waitingForReply           = true;
    }
Exemplo n.º 2
0
    void HandleEffects(Name _event)
    {
        var consequences = _worldModel.Simulate(new Name[] { _event });

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

            // For each Role Play Character
            foreach (var rpc in _rpcList)
            {
                //If the "Observer" part of the effect corresponds to the name of the agent or if it is a universal symbol
                if (eff.ObserverAgent != rpc.CharacterName && eff.ObserverAgent != (Name)"*")
                {
                    continue;
                }
                //Apply that consequence to the agent
                rpc.Perceive(EventHelper.PropertyChange(eff.PropertyName, eff.NewValue, rpc.CharacterName));
            }
        }

        _waitingForPlayer = false;
    }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var iat = IntegratedAuthoringToolAsset.LoadFromFile("../../../Examples/CiF-Tutorial/JobInterview.iat");

            rpcList = new List <RolePlayCharacterAsset>();
            var wm = new WorldModelAsset();

            wm.AddActionEffect((Name)"Event(Action-End, *, Speak(*,*,*,*), [t])", new EffectDTO()
            {
                PropertyName  = (Name)"Has(Floor)",
                NewValue      = (Name)"[t]",
                ObserverAgent = (Name)"*"
            });

            wm.AddActionEffect((Name)"Event(Action-End, [i], Speak([cs],[ns],*,*), SELF)", new EffectDTO()
            {
                PropertyName  = (Name)"DialogueState([i])",
                NewValue      = (Name)"[ns]",
                ObserverAgent = (Name)"[t]"
            });


            wm.AddActionEffect((Name)"Event(Action-End, SELF, Speak([cs],[ns],*,*), [t])", new EffectDTO()
            {
                PropertyName  = (Name)"DialogueState([t])",
                NewValue      = (Name)"[ns]",
                ObserverAgent = (Name)"[i]"
            });

            wm.SaveToFile("../../../Examples/WM-Tutorial/WorldModel.wm");
            foreach (var source in iat.GetAllCharacterSources())
            {
                var rpc = RolePlayCharacterAsset.LoadFromFile(source.Source);


                //rpc.DynamicPropertiesRegistry.RegistDynamicProperty(Name.BuildName("Volition"),cif.VolitionPropertyCalculator);
                rpc.LoadAssociatedAssets();

                iat.BindToRegistry(rpc.DynamicPropertiesRegistry);

                rpcList.Add(rpc);
            }


            foreach (var actor in rpcList)
            {
                foreach (var anotherActor in rpcList)
                {
                    if (actor != anotherActor)
                    {
                        var changed = new[] { EventHelper.ActionEnd(anotherActor.CharacterName.ToString(), "Enters", "Room") };
                        actor.Perceive(changed);
                    }
                }
                //         actor.SaveToFile("../../../Examples/" + actor.CharacterName + "-output1" + ".rpc");
            }



            List <Name>    _events  = new List <Name>();
            List <IAction> _actions = new List <IAction>();

            IAction action = null;

            while (true)
            {
                _actions.Clear();
                foreach (var rpc in rpcList)
                {
                    rpc.Tick++;

                    Console.WriteLine("Character perceiving: " + rpc.CharacterName + " its mood: " + rpc.Mood);

                    rpc.Perceive(_events);
                    var decisions = rpc.Decide();
                    _actions.Add(decisions.FirstOrDefault());

                    foreach (var act in decisions)
                    {
                        Console.WriteLine(rpc.CharacterName + " has this action: " + act.Name);
                    }
                    rpc.SaveToFile("../../../Examples/WM-Tutorial/" + rpc.CharacterName + "-output" + ".rpc");
                }

                _events.Clear();

                var randomGen = new Random(Guid.NewGuid().GetHashCode());

                var pos = randomGen.Next(rpcList.Count);
                int i   = 0;

                var initiator = rpcList[pos];


                action = _actions.ElementAt(pos);

                Console.WriteLine();
                if (action == null)
                {
                    Console.WriteLine(initiator.CharacterName + " does not have any action to do ");
                }

                if (action != null)
                {
                    var initiatorName = initiator.CharacterName.ToString();
                    var targetName    = action.Target.ToString();
                    var nextState     = action.Parameters[1].ToString();
                    var currentState  = action.Parameters[0].ToString();

                    Console.WriteLine("Action: " + initiatorName + " does " + action.Name + " to " +
                                      targetName + "\n" + action.Parameters[1]);

                    _events.Add(EventHelper.ActionEnd(initiatorName, action.Name.ToString(),
                                                      action.Target.ToString()));



                    Console.WriteLine();
                    Console.WriteLine("Dialogue:");
                    Console.WriteLine("Current State: " + currentState);
                    if (iat.GetDialogueActions(action.Parameters[0], action.Parameters[1], action.Parameters[2], action.Parameters[3]).FirstOrDefault() != null)
                    {
                        Console.WriteLine(initiator.CharacterName + " says: ''" +
                                          iat.GetDialogueActions(action.Parameters[0], action.Parameters[1], action.Parameters[2], action.Parameters[3]).FirstOrDefault().Utterance + "'' to " + targetName);
                    }
                    else
                    {
                        Console.WriteLine(initiator.CharacterName + " says: " + "there is no dialogue suited for this action");
                    }


                    // WORLD MODEL
                    var effects = wm.Simulate(_events.ToArray());
                    foreach (var ef in effects)
                    {
                        if (ef.ObserverAgent == (Name)"*")
                        {
                            foreach (var rpc in rpcList)
                            {
                                var proChange =
                                    EventHelper.PropertyChange(ef.PropertyName.ToString(), ef.NewValue.ToString(), "World");
                                rpc.Perceive(proChange);
                            }
                        }

                        else
                        {
                            var proChange =
                                EventHelper.PropertyChange(ef.PropertyName.ToString(), ef.NewValue.ToString(), "World");
                            rpcList.Find(x => x.CharacterName == ef.ObserverAgent).Perceive(proChange);
                        }
                    }

                    Console.WriteLine("Next State: " + nextState);
                }
                Console.WriteLine();
                Console.ReadKey();
            }
        }
    // 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
    }