Beispiel #1
0
        public static DiceStat Parse(int entity, string label, string expression)
        {
            string cleanExpression = _whitespacePattern.Replace(expression.ToLower(), "");

            cleanExpression = cleanExpression.Replace("+-", "-");

            var parseValues = new ParseValues().Init();
            var dice        = new DiceStat(entity, label);

            for (int i = 0; i < cleanExpression.Length; ++i)
            {
                char c = cleanExpression[i];
                if (char.IsDigit(c))
                {
                    parseValues.Constant += c;
                }
                else if (c == '*')
                {
                    parseValues.Multiply *= int.Parse(parseValues.Constant);
                    parseValues.Constant  = "";
                }
                else if (c == 'd')
                {
                    if (parseValues.Constant == "")
                    {
                        parseValues.Constant = "1";
                    }
                    parseValues.DiceRolls = int.Parse(parseValues.Constant);
                    parseValues.Constant  = "";
                }
                // else if (c == 'k') {
                //     string chooseAccum = "";
                //     while (i + 1 < cleanExpression.Length && char.IsDigit(cleanExpression[i + 1])) {
                //         chooseAccum += cleanExpression[i + 1];
                //         ++i;
                //     }
                //     parseValues.Choose = int.Parse(chooseAccum);
                // }
                else if (c == '+')
                {
                    Append(dice, parseValues);
                    parseValues = new ParseValues().Init();
                }
                else if (c == '-')
                {
                    Append(dice, parseValues);
                    parseValues          = new ParseValues().Init();
                    parseValues.Multiply = -1;
                }
                else
                {
                    throw new ArgumentException("Invalid character in dice expression", expression);
                }
            }
            Append(dice, parseValues);

            return(dice);
        }
        public void SetupEntity(Entity entity, SimpleDataLine lineData, DataEntry allData)
        {
            var damageStat = DiceStat.Parse(entity, Stats.Damage, lineData.Config);

            if (damageStat != null)
            {
                entity.Get <StatsContainer>().Add(damageStat);
            }
        }
Beispiel #3
0
        private static void Append(DiceStat dice, ParseValues parseValues)
        {
            int constant = int.Parse(parseValues.Constant);

            if (parseValues.DiceRolls == 0)
            {
                dice.ChangeBase(parseValues.Multiply * constant);
            }
            else
            {
                dice.DiceSides      = constant;
                dice.DiceRolls      = parseValues.DiceRolls;
                dice.MultiplyResult = parseValues.Multiply;
            }
        }