public override object Evaluate(
            List <string> args,
            ExpressionEvaluator evaluator,
            Creature player,
            Target target,
            CastedSpell spell,
            RollResults dice = null)
        {
            ExpectingArguments(args, 0, 1);

            WhatSide whatSide = WhatSide.All;

            if (args.Count > 0)
            {
                string   whatSideStr = args[0].Trim();
                string[] parts       = whatSideStr.Split('|');            // Union enum elements, e.g., "Friendly | Neutral"
                whatSide = WhatSide.None;
                foreach (string part in parts)
                {
                    whatSide |= DndUtils.ToWhatSide(part);
                }
            }

            OnTargetCreaturesInVolumeRequest(player, new WhatSideEventArgs(whatSide));

            return(null);
        }
Exemplo n.º 2
0
 private void TriggerEvent(Magic magic, string eventCode, RollResults dice = null)
 {
     if (string.IsNullOrWhiteSpace(eventCode))
     {
         return;
     }
     Expressions.Do(eventCode, magic.Caster as Character, Target.FromMagic(magic), magic.CastedSpell, dice, magic);
 }
Exemplo n.º 3
0
        private static void ExpressionEvaluator_EvaluateFunction(object sender, FunctionEvaluationEventArg e)
        {
            Creature    player      = GetPlayer(e.Evaluator.Variables);
            CastedSpell castedSpell = GetCastedSpell(e.Evaluator.Variables);
            Target      target      = GetTargetCreature(e.Evaluator.Variables);
            RollResults dice        = GetDiceStoppedRollingData(e.Evaluator.Variables);
            DndFunction function    = functions.FirstOrDefault(x => x.Handles(e.Name, player, castedSpell));

            if (function != null)
            {
                try
                {
                    e.Value = function.Evaluate(e.Args, e.Evaluator, player, target, castedSpell, dice);
                    Log($"  {e.Name}({GetArgsStr(e.Args)}) => {GetValueStr(e.Value)}");
                }
                catch                 //(Exception ex)
                {
                    e.Value = null;
                    Log($"  Exception thrown trying to evaluate {e.Name}({GetArgsStr(e.Args)})");
                }
            }
        }
Exemplo n.º 4
0
        public static double GetDouble(string expression, Creature player = null, Target target = null, CastedSpell spell = null, RollResults dice = null, object customData = null)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                return(0);
            }
            StartEvaluation(player, $"GetDouble({expression})", target, spell, dice, customData);
            try
            {
                object result = 0.0;

                try
                {
                    result = expressionEvaluator.Evaluate(Clean(expression));
                    Log($"  returns {result};");
                }
                catch (Exception ex)
                {
                    OnExceptionThrown(null, new DndCoreExceptionEventArgs(ex));
                    return(0);
                }

                if (result is int)
                {
                    return((int)result);
                }

                if (result is decimal)
                {
                    return((double)((decimal)result));
                }

                if (result is double)
                {
                    return((double)result);
                }

                if (result is string resultStr)
                {
                    if (double.TryParse(resultStr, out double resultDbl))
                    {
                        return(resultDbl);
                    }
                    else                      // Support fractions...
                    {
                        const string divideSymbol = "/";
                        if (resultStr.Contains(divideSymbol))
                        {
                            string numeratorStr   = resultStr.EverythingBefore(divideSymbol).Trim();
                            string denominatorStr = resultStr.EverythingAfter(divideSymbol).Trim();
                            if (double.TryParse(numeratorStr, out double numerator) && double.TryParse(denominatorStr, out double denominator) && denominator != 0)
                            {
                                return(numerator / denominator);
                            }
                        }
                    }
                }

                return(double.MinValue);
            }
            finally
            {
                FinishEvaluation(player);
            }
        }
Exemplo n.º 5
0
        public static bool GetBool(string expression, Creature player = null, Target target = null, CastedSpell spell = null, RollResults dice = null, object customData = null)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                return(false);
            }
            StartEvaluation(player, $"GetBool({expression})", target, spell, dice, customData);
            try
            {
                object result = false;

                try
                {
                    result = expressionEvaluator.Evaluate(Clean(expression));
                }
                catch (Exception ex)
                {
                    OnExceptionThrown(null, new DndCoreExceptionEventArgs(ex));
                    return(false);
                }

                bool resultBool;
                if (result is int)
                {
                    resultBool = (int)result == 1;
                }
                else if (result is string)
                {
                    string compareStr = ((string)result).Trim().ToLower();
                    resultBool = compareStr == "x" || compareStr == "true";
                }
                else if (result is bool)
                {
                    resultBool = (bool)result;
                }
                else
                {
                    resultBool = false;
                }

                Log($"  returns {resultBool};");

                return(resultBool);
            }
            finally
            {
                FinishEvaluation(player);
            }
        }
 public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
 {
     if (function == null)
     {
         return(null);
     }
     return(evaluator.Evaluate(DndUtils.InjectParameters(function.Expression, function.Parameters, args)));
 }
Exemplo n.º 7
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target = null, CastedSpell spell = null, RollResults dice = null)
        {
            ExpectingArguments(args, 1, 3);
            if (player != null)
            {
                int hue = Expressions.GetInt(args[0], player, target, spell);

                int saturation = 100;
                if (args.Count == 2)
                {
                    saturation = Expressions.GetInt(args[1], player, target, spell);
                }

                int brightness = 100;
                if (args.Count == 3)
                {
                    brightness = Expressions.GetInt(args[2], player, target, spell);
                }

                player.AddSpellHitEffect(hue: hue, saturation: saturation, brightness: brightness);
            }

            return(null);
        }
Exemplo n.º 8
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target = null, CastedSpell spell = null, RollResults dice = null)
        {
            ExpectingArguments(args, 1, 2);
            if (player != null)
            {
                string fileName = Expressions.GetStr(args[0], player, target, spell);

                int timeOffsetMs = 0;
                if (args.Count > 1)
                {
                    timeOffsetMs = Expressions.GetInt(args[1], player, target, spell);
                }

                player.AddSpellHitSoundEffect(fileName, timeOffsetMs);
            }

            return(null);
        }
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target = null, CastedSpell spell = null, RollResults dice = null)
        {
            ExpectingArguments(args, 1, 15);
            if (player != null)
            {
                string effectName;
                int    hue, saturation, brightness, timeOffset, secondaryHue, secondarySaturation, secondaryBrightness, yOffset, xOffset;
                double scale, rotation, autoRotation, velocityX, velocityY;
                AddSpellCastEffect.GetVisualEffectParameters(args, player, target, spell, out effectName, out hue, out saturation, out brightness, out scale, out rotation, out autoRotation, out timeOffset, out secondaryHue, out secondarySaturation, out secondaryBrightness, out xOffset, out yOffset, out velocityX, out velocityY);

                player.AddSpellHitEffect(effectName, hue, saturation, brightness,
                                         scale, rotation, autoRotation, timeOffset,
                                         secondaryHue, secondarySaturation, secondaryBrightness, xOffset, yOffset, velocityX, velocityY);
            }

            return(null);
        }
Exemplo n.º 10
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target = null, CastedSpell spell = null, RollResults dice = null)
        {
            string        question = Expressions.GetStr(args[0], player, target, spell);
            List <string> answers  = args.Skip(1).ToList();
            AskEventArgs  ea       = new AskEventArgs(question, answers);

            OnAskQuestion(player, ea);
            return(ea.Result);
        }
Exemplo n.º 11
0
 private static void AddPlayerVariables(Creature player, Target target, CastedSpell spell, RollResults dice = null, object customData = null)
 {
     variableStack.Push(expressionEvaluator.Variables);
     expressionEvaluator.Variables = new Dictionary <string, object>()
     {
         { STR_Player, player },
         { STR_Target, target },
         { STR_Dice, dice },
         { STR_CastedSpell, spell },
         { STR_CustomData, customData }
     };
 }
Exemplo n.º 12
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            ExpectingArguments(args, 0);

            CreaturePlusModId recipient = Expressions.GetCustomData <CreaturePlusModId>(evaluator.Variables);

            if (recipient == null)
            {
                return(null);
            }
            OnRequestDispelMagic(new DispelMagicEventArgs(recipient));
            return(null);
        }
Exemplo n.º 13
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            if (player == null)
            {
                return(null);
            }

            ExpectingArguments(args, 2);

            string variableName = args[0];
            object rawValue     = Expressions.Get <object>(args[1], player, target, spell);
            double valueDouble;

            if (rawValue == null)
            {
                valueDouble = 0;
            }
            else
            {
                valueDouble = MathUtils.GetDouble(rawValue.ToString());
            }
            int valueInt = (int)Math.Round(valueDouble);

            // TODO: Wil says Convert.ConvertTo() can simplify this.

            FieldInfo field = typeof(Character).GetField(variableName);

            if (field != null)
            {
                if (field.FieldType.FullName == "System.Int32")
                {
                    field.SetValue(player, MathUtils.GetInt(field.GetValue(player).ToString()) + valueInt);
                }
                else
                {
                    field.SetValue(player, MathUtils.GetDouble(field.GetValue(player).ToString()) + valueDouble);
                }
                return(null);
            }

            PropertyInfo property = typeof(Character).GetProperty(variableName);

            if (property != null)
            {
                if (property.PropertyType.FullName == "System.Int32")
                {
                    property.SetValue(player, MathUtils.GetInt(property.GetValue(player).ToString()) + valueInt);
                }
                else
                {
                    property.SetValue(player, MathUtils.GetDouble(property.GetValue(player).ToString()) + valueDouble);
                }
                return(null);
            }

            object existingValueRaw = player.GetState(variableName);

            if (existingValueRaw != null)
            {
                if (existingValueRaw is int)
                {
                    player.SetState(variableName, MathUtils.GetInt(existingValueRaw.ToString()) + valueInt);
                }
                else
                {
                    player.SetState(variableName, MathUtils.GetDouble(existingValueRaw.ToString()) + valueDouble);
                }
                return(null);
            }

            throw new Exception($"Variable \"{variableName}\" not found.");
        }
Exemplo n.º 14
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            ExpectingArguments(args, 4);

            string tableName   = evaluator.Evaluate <string>(args[0]);
            string fieldLookup = evaluator.Evaluate <string>(args[1]);
            string matchColumn = evaluator.Evaluate <string>(args[2]);
            object matchValue  = evaluator.Evaluate(Expressions.Clean(args[3]));

            return(AllTables.GetData(tableName, fieldLookup, matchColumn, matchValue));
        }
Exemplo n.º 15
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            ExpectingArguments(args, 1);

            string diceStr = evaluator.Evaluate <string>(args[0]);

            player.AddDice(diceStr);

            return(null);
        }
Exemplo n.º 16
0
 public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target = null, CastedSpell spell = null, RollResults dice = null)
 {
     ExpectingArguments(args, 1, 2);
     if (args.Count > 1)
     {
         player = AllPlayers.GetFromName(args[1].Trim());
     }
     if (player != null)
     {
         player.SetNextAnswer(Expressions.GetStr(args[0], player, target, spell));
     }
     return(null);
 }
Exemplo n.º 17
0
        public static string GetStr(string expression, Creature player = null, Target target = null, CastedSpell spell = null, RollResults dice = null, object customData = null)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                return(string.Empty);
            }

            StartEvaluation(player, $"GetStr({expression})", target, spell, dice, customData);
            try
            {
                object result = string.Empty;

                try
                {
                    result = expressionEvaluator.Evaluate(Clean(expression));
                }
                catch (Exception ex)
                {
                    OnExceptionThrown(null, new DndCoreExceptionEventArgs(ex));
                    return(string.Empty);
                }

                string resultStr = string.Empty;
                if (result is string)
                {
                    resultStr = (string)result;
                }
                else if (result == null)
                {
                    resultStr = string.Empty;
                }
                else
                {
                    resultStr = result.ToString();
                }

                Log($"  returns \"{resultStr}\";");
                return(resultStr);
            }
            finally
            {
                FinishEvaluation(player);
            }
        }
Exemplo n.º 18
0
        // TODO: Add customData parameter?
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            ExpectingArguments(args, 1, 2);

            string shortcutName    = evaluator.Evaluate <string>(args[0]);
            bool   rollImmediately = false;

            if (args.Count > 1)
            {
                rollImmediately = Expressions.GetBool(args[1], player, target, spell, dice);
            }

            player.AddShortcutToQueue(shortcutName, rollImmediately);

            return(null);
        }
Exemplo n.º 19
0
 private static void StartEvaluation(Creature player, string callingProc, Target target = null, CastedSpell spell = null, RollResults dice = null, object customData = null)
 {
     //historyStack.Push(history);
     //history = new List<string>();
     LogCallingProc(callingProc);
     AddPlayerVariables(player, target, spell, dice, customData);
     if (player != null)
     {
         player.StartingExpressionEvaluation();
     }
     BeginUpdate();
 }
Exemplo n.º 20
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            ExpectingArguments(args, 0);
            CastedSpell castedSpell = Expressions.GetCastedSpell(evaluator.Variables);

            if (castedSpell != null && player != null)
            {
                player.Dispel(castedSpell);
            }

            return(null);
        }
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            ExpectingArguments(args, 2);

            string propertyName = Expressions.GetStr(args[0], player, target, spell, dice);
            double deltaValue   = Expressions.GetDouble(args[1], player, target, spell, dice);

            if (target == null)
            {
                // Make sure this Spell has values in the MinTargetsToCast and MaxTargetsToCast columns.
                System.Diagnostics.Debugger.Break();
                return(null);
            }

            foreach (Creature creature in target.Creatures)
            {
                OnRequestPropertyChange(creature, propertyName, deltaValue);
            }

            if (player != null)
            {
                foreach (int playerId in target.PlayerIds)
                {
                    Character playerFromId = player.Game.GetPlayerFromId(playerId);
                    if (playerFromId != null)
                    {
                        OnRequestPropertyChange(playerFromId, propertyName, deltaValue);
                    }
                }
            }


            return(null);
        }
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target = null, CastedSpell spell = null, RollResults dice = null)
        {
            ExpectingArguments(args, 2);
            string reminder         = Expressions.GetStr(args[0], player, target, spell);
            string fromNowDuration  = Expressions.GetStr(args[1], player, target, spell);
            AddReminderEventArgs ea = new AddReminderEventArgs(reminder, fromNowDuration);

            AddReminder(player, ea);
            return(null);
        }
Exemplo n.º 23
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target = null, CastedSpell spell = null, RollResults dice = null)
        {
            ExpectingArguments(args, 1);
            // TODO: Get the condition to set.
            // TODO: Set the target's condition.

            return(null);
        }
Exemplo n.º 24
0
 public static T Get <T>(string expression, Creature player = null, Target target = null, CastedSpell spell = null, RollResults dice = null, object customData = null)
 {
     if (string.IsNullOrWhiteSpace(expression))
     {
         return(default(T));
     }
     StartEvaluation(player, $"Get<{typeof(T).ToString()}>({expression})", target, spell, dice, customData);
     try
     {
         try
         {
             T result = (T)expressionEvaluator.Evaluate(Clean(expression));
             Log($"  returns {result};");
             return(result);
         }
         catch (Exception ex)
         {
             OnExceptionThrown(null, new DndCoreExceptionEventArgs(ex));
             return(default(T));
         }
     }
     finally
     {
         FinishEvaluation(player);
     }
 }
Exemplo n.º 25
0
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            CreaturePlusModId recipient = Expressions.GetCustomData <CreaturePlusModId>(evaluator.Variables);

            if (recipient == null)
            {
                throw new Exception($"CreaturePlusModId recipient must be specified before evaluating expressions containing RemovePropertyMod.");
            }

            ExpectingArguments(args, 0);

            recipient.Creature.RemoveVantageMod(recipient.ID);
            return(null);
        }
Exemplo n.º 26
0
        public static void Do(string expression, Creature player = null, Target target = null, CastedSpell castedSpell = null, RollResults dice = null, object customData = null)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                return;
            }
            StartEvaluation(player, $"Do({expression})", target, castedSpell, dice, customData);
            try
            {
                string script = Clean(expression);
                if (!script.EndsWith(";") && !script.EndsWith("}"))
                {
                    script += ";";
                }

                string   compactScript = string.Empty;
                string[] splitLines    = script.Split('\r', '\n');
                foreach (string line in splitLines)
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }
                    if (line.Trim().StartsWith("//"))
                    {
                        continue;
                    }
                    compactScript += line + Environment.NewLine;
                }

                if (string.IsNullOrWhiteSpace(compactScript))
                {
                    return;
                }
                try
                {
                    expressionEvaluator.ScriptEvaluate(compactScript);
                }
                catch (ExpressionEvaluatorSyntaxErrorException ex)
                {
                    OnExceptionThrown(null, new DndCoreExceptionEventArgs(ex));
                }
            }
            finally
            {
                FinishEvaluation(player);
            }
        }
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            ExpectingArguments(args, 1);
            string featureName = args[0];

            if (featureName.StartsWith("\""))
            {
                featureName = Expressions.GetStr(featureName);
            }
            Feature feature = AllFeatures.Get(featureName);

            if (feature != null)
            {
                feature.Deactivate("", player);
            }

            return(null);
        }
Exemplo n.º 28
0
        public static int GetInt(string expression, Creature player = null, Target target = null, CastedSpell spell = null, RollResults dice = null, object customData = null)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                return(0);
            }
            StartEvaluation(player, $"GetInt({expression})", target, spell, dice, customData);
            try
            {
                object result = 0;
                try
                {
                    result = expressionEvaluator.Evaluate(Clean(expression));
                    Log($"  returns {result};");
                }
                catch (Exception ex)
                {
                    OnExceptionThrown(null, new DndCoreExceptionEventArgs(ex));
                    return(0);
                }

                if (result is int)
                {
                    return((int)result);
                }
                if (result is double)
                {
                    return((int)Math.Round((double)result));
                }

                if (result is decimal)
                {
                    return((int)Math.Round((decimal)result));
                }
                if (result is Enum)
                {
                    return((int)result);
                }
                return(int.MinValue);
            }
            finally
            {
                FinishEvaluation(player);
            }
        }
        public override object Evaluate(List <string> args, ExpressionEvaluator evaluator, Creature player, Target target, CastedSpell spell, RollResults dice = null)
        {
            ExpectingArguments(args, 1);

            string message = evaluator.Evaluate <string>(args[0]);

            player.Game.TellDungeonMaster(message);

            return(null);
        }
Exemplo n.º 30
0
 public static float GetFloat(string expression, Creature player = null, Target target = null, CastedSpell spell = null, RollResults dice = null, object customData = null)
 {
     return((float)GetDouble(expression, player, target, spell, dice, customData));
 }