public void SimpleEngineConMath()
        {
            var player = new Actor();

            player.Ammo = 3;

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have more than half health",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = ">",
                            Right = "(/ player.maxHealth 2)"
                        }
                    }
                },
                new DialogRule()
                {
                    Name       = "I am the king of health and ammo",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = "=",
                            Right = "player.maxHealth"
                        },
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.ammo",
                            Op    = ">",
                            Right = "25"
                        }
                    }
                }
            };

            var attributes = new ObjectDialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                new ObjectDialogAttribute(player, "player", "maxHealth"),
                new ObjectDialogAttribute(player, "player", "ammo"),
            };

            var engine = new DialogEngine();

            rules.ToList().ForEach(r => engine.AddRule(r));
            attributes.ToList().ForEach(a => engine.AddAttribute(a));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have more than half health", best.Name);
        }
        public void SimpleTemplateInterp()
        {
            var player = new Actor();

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have full health!",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = "=",
                            Right = "player.maxHealth"
                        }
                    },
                    Dialog = new DialogRule.DialogPart[]
                    {
                        new DialogRule.DialogPart()
                        {
                            Speaker      = "player",
                            Content      = "I have health of {player health} which and {player maxHealth + player health}",
                            ContentParts = new string[]
                            {
                                "'I have health of '",
                                "player.health",
                                "' which and '",
                                "(+ player.maxHealth player.health)"
                            }
                        }
                    }
                },
            };

            var attributes = new ObjectDialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                new ObjectDialogAttribute(player, "player", "maxHealth"),
                new ObjectDialogAttribute(player, "player", "ammo"),
            };

            var engine = new DialogEngine();

            rules.ToList().ForEach(r => engine.AddRule(r));
            attributes.ToList().ForEach(a => engine.AddAttribute(a));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have full health!", best.Name);

            var dialogs = engine.ExecuteRuleDialogs(best);

            Assert.AreEqual(1, dialogs.Length);
            Assert.AreEqual("I have health of 50 which and 100", dialogs[0]);
        }
        public void BoolBag2()
        {
            var player = new
            {
                health = 10,
                flags  = new BagBoolElement[]
                {
                    new BagBoolElement()
                    {
                        name  = "a.part",
                        value = true
                    }
                }.ToList()
            };

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have full health!",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.flags.a.part",
                            Op    = "=",
                            Right = "true"
                        },
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.flags.b",
                            Op    = "=",
                            Right = "false"
                        }
                    }
                },
            };

            var engine = new DialogEngine()
                         .AddHandler(new BagBoolHandler());
            var attributes = new DialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                DialogAttribute.New("player.flags", false, player.flags).UpdateElements(engine)
                //new BagDialogAttribute<bool>("player.flags", player.flags).UpdateElements(engine)
            };

            attributes.ToList().ForEach(a => engine.AddAttribute(a));



            rules.ToList().ForEach(r => engine.AddRule(r));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have full health!", best.Name);
        }
        public void SimpleEngineOutcomeSetter()
        {
            var player = new Actor();

            player.MaxHealth = 100;
            player.Health    = 100;
            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have full health!",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = "=",
                            Right = "player.maxHealth"
                        }
                    },
                    Outcomes = new DialogRule.DialogOutcome[]
                    {
                        new DialogRule.DialogOutcome()
                        {
                            Command   = "set",
                            Target    = "player.health",
                            Arguments = new Dictionary <string, string>()
                            {
                                { "", "(/ player.maxHealth 2)" }
                            }
                        }
                    }
                }
            };

            var attributes = new ObjectDialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                new ObjectDialogAttribute(player, "player", "maxHealth"),
                new ObjectDialogAttribute(player, "player", "ammo"),
            };

            var engine = new DialogEngine();

            rules.ToList().ForEach(r => engine.AddRule(r));
            attributes.ToList().ForEach(a => engine.AddAttribute(a));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have full health!", best.Name);

            engine.ExecuteRuleOutcomes(best);

            Assert.AreEqual(50, player.Health);
        }
        public void TemplateInterpStrings()
        {
            var player = new Actor();

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have full health!",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = "=",
                            Right = "player.maxHealth"
                        }
                    },
                    Dialog = new DialogRule.DialogPart[]
                    {
                        new DialogRule.DialogPart()
                        {
                            Speaker      = "player",
                            Content      = "my health is <color='red'> great </color>",
                            ContentParts = new string[]
                            {
                                "'my health is <color='red'> great </color>'",
                            }
                        }
                    }
                },
            };

            var attributes = new ObjectDialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                new ObjectDialogAttribute(player, "player", "maxHealth"),
                new ObjectDialogAttribute(player, "player", "ammo"),
            };

            var engine = new DialogEngine();

            rules.ToList().ForEach(r => engine.AddRule(r));
            attributes.ToList().ForEach(a => engine.AddAttribute(a));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have full health!", best.Name);

            var dialogs = engine.ExecuteRuleDialogs(best);

            Assert.AreEqual(1, dialogs.Length);
            Assert.AreEqual("my health is <color='red'> great </color>", dialogs[0]);
        }
        public void IntBag()
        {
            var player = new
            {
                health = 10,
                ints   = new BagIntElement[]
                {
                }.ToList()
            };

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have full health!",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.ints.a",
                            Op    = "=",
                            Right = "10"
                        },
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.ints.hidaldo.rep",
                            Op    = ">",
                            Right = "6"
                        }
                    }
                },
            };

            var engine = new DialogEngine()
                         .AddHandler(new BagIntHandler());
            var attributes = new DialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                DialogAttribute.New("player.ints", 10, player.ints).UpdateElements(engine)
                //new BagDialogAttribute<bool>("player.flags", player.flags).UpdateElements(engine)
            };

            attributes.ToList().ForEach(a => engine.AddAttribute(a));



            rules.ToList().ForEach(r => engine.AddRule(r));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have full health!", best.Name);
        }
        public void SimpleEngineTransform()
        {
            var player = new Actor();

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have full health!",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "dialog.target.health",
                            Op    = "=",
                            Right = "player.maxHealth"
                        }
                    }
                }
            };

            var attributes = new ObjectDialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                new ObjectDialogAttribute(player, "player", "maxHealth"),
                new ObjectDialogAttribute(player, "player", "ammo"),
            };

            var engine = new DialogEngine()
                         .AddTransform("dialog.target", () => "player");

            rules.ToList().ForEach(r => engine.AddRule(r));
            attributes.ToList().ForEach(a => engine.AddAttribute(a));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have full health!", best.Name);
        }
        public void SimpleEngineOutcomeRunner()
        {
            var player = new Actor();

            player.MaxHealth = 100;
            player.Health    = 100;
            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have full health!",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = "=",
                            Right = "player.maxHealth"
                        }
                    },
                    Outcomes = new DialogRule.DialogOutcome[]
                    {
                        new DialogRule.DialogOutcome()
                        {
                            Command   = "run",
                            Target    = "player.sampleFunc",
                            Arguments = new Dictionary <string, string>()
                            {
                                { "h", "(/ player.maxHealth 2)" },

                                { "ammo", "55" }
                            }
                        }
                    }
                }
            };

            // new OFDA(player, "setAll", new Dictionary<string, string>(){

            var attributes = new DialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                new ObjectDialogAttribute(player, "player", "maxHealth"),
                new ObjectDialogAttribute(player, "player", "ammo"),
                new ObjectFunctionDialogAttribute("player.sampleFunc", new Action <Dictionary <string, object> >(vars =>
                {
                    var health    = (int)vars["h"];
                    var maxHealth = (int)vars["mx"];
                    var ammo      = (int)vars["ammo"];

                    player.SetAll(health, maxHealth, ammo);
                }), new Dictionary <string, object> {
                    { "mx", 200 }
                })
            };

            var engine = new DialogEngine();

            rules.ToList().ForEach(r => engine.AddRule(r));
            attributes.ToList().ForEach(a => engine.AddAttribute(a));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have full health!", best.Name);

            engine.ExecuteRuleOutcomes(best);

            Assert.AreEqual(50, player.Health);
            Assert.AreEqual(200, player.MaxHealth);
            Assert.AreEqual(55, player.Ammo);
        }
        public void SimpleEngineWithConditionSetLoopDetected()
        {
            var player = new Actor();

            player.Health = player.MaxHealth;
            var sets = new DialogConditionSet[]
            {
                new DialogConditionSet()
                {
                    Name       = "test",
                    Conditions = new DialogConditionSet.DialogCondition[]
                    {
                        new DialogConditionSet.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = ">",
                            Right = "1"
                        },
                        new DialogConditionSet.DialogCondition()
                        {
                            Left  = "__.conditions.egg",
                            Op    = "=",
                            Right = "true"
                        }
                    }
                },
                new DialogConditionSet()
                {
                    Name       = "egg",
                    Conditions = new DialogConditionSet.DialogCondition[]
                    {
                        new DialogConditionSet.DialogCondition()
                        {
                            Left  = "__.conditions.test",
                            Op    = "=",
                            Right = "true"
                        },
                        new DialogConditionSet.DialogCondition()
                        {
                            Left  = "player.ammo",
                            Op    = ">",
                            Right = "5"
                        }
                    }
                }
            };

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have full health!",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = "=",
                            Right = "player.maxHealth"
                        },
                        new DialogRule.DialogCondition()
                        {
                            Left  = "__.conditions.egg",
                            Op    = "=",
                            Right = "true"
                        }
                    }
                },
                new DialogRule()
                {
                    Name       = "I am less specific",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = "=",
                            Right = "player.maxHealth"
                        },
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.health",
                            Op    = "=",
                            Right = "player.maxHealth"
                        }
                    }
                },
            };

            var attributes = new ObjectDialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                new ObjectDialogAttribute(player, "player", "maxHealth"),
                new ObjectDialogAttribute(player, "player", "ammo"),
            };

            var engine = new DialogEngine().AddHandler(new ConditionSetEvalHandler());

            sets.ToList().ForEach(r => engine.AddConditionSet(r));
            rules.ToList().ForEach(r => engine.AddRule(r));
            attributes.ToList().ForEach(a => engine.AddAttribute(a));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have full health!", best.Name);
        }
Beispiel #10
0
        public void StringAndIntBags()
        {
            var player = new
            {
                health = 10,
                nums   = new BagIntElement[]
                {
                    new BagIntElement()
                    {
                        name  = "a",
                        value = 5
                    }
                }.ToList(),
                strs = new BagStringElement[]
                {
                    new BagStringElement()
                    {
                        name  = "a",
                        value = "tuna"
                    }
                }.ToList()
            };

            var rules = new DialogRule[]
            {
                new DialogRule()
                {
                    Name       = "I have full health!",
                    Conditions = new DialogRule.DialogCondition[]
                    {
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.nums.a",
                            Op    = "=",
                            Right = "5"
                        },
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.nums.b",
                            Op    = ">",
                            Right = "6"
                        },
                        new DialogRule.DialogCondition()
                        {
                            Left  = "player.strs.a",
                            Op    = "=!",
                            Right = "player.strs.b"
                        }
                    }
                },
            };

            var engine = new DialogEngine()
                         .AddHandler(new BagIntHandler())
                         .AddHandler(new BagStringHandler());
            var attributes = new DialogAttribute[]
            {
                new ObjectDialogAttribute(player, "player", "health"),
                DialogAttribute.New("player.nums", 10, player.nums).UpdateElements(engine),
                DialogAttribute.New("player.strs", "eggs", player.strs).UpdateElements(engine)
                //new BagDialogAttribute<bool>("player.flags", player.flags).UpdateElements(engine)
            };

            attributes.ToList().ForEach(a => engine.AddAttribute(a));



            rules.ToList().ForEach(r => engine.AddRule(r));

            var best = engine.GetBestValidDialog();

            Assert.IsNotNull(best);
            Assert.AreEqual("I have full health!", best.Name);
        }