Example #1
0
        public override void Parse(string line, TranspilerContext context)
        {
            if (line.StartsWith("#when"))
            {
                var goalNumber = context.CreateGoal();

                context.AddToScript(new Defrule(new[] { "true" }, new[] { $"set-goal {goalNumber} 0", "disable-self" }));

                context.ActionStack.Push(new Action($"set-goal {goalNumber} 1"));
                context.DataStack.Push(goalNumber);
            }
            else if (line.StartsWith("#then"))
            {
                var goalNumber = context.DataStack.Peek();

                context.DataStack.Push(GetData(line)["always"].Success);
                context.ActionStack.Pop();
                context.ConditionStack.Push(new Condition($"goal {goalNumber} 1"));
            }
            else
            {
                var always     = (bool)context.DataStack.Pop();
                var goalNumber = context.DataStack.Pop();
                context.ConditionStack.Pop();

                if (!always)
                {
                    context.AddToScript(new Defrule(new[] { "true" }, new[] { $"set-goal {goalNumber} 0" }));
                }
            }
        }
Example #2
0
        public override void Parse(string line, TranspilerContext context)
        {
            var segments   = line.Split("=>");
            var transpiler = new Transpiler();
            var goalNumber = context.CreateGoal();

            var items = new List <IScriptItem>();

            for (var i = 0; i < segments.Length; i++)
            {
                var segment = segments[i];

                Script segmentItems = null;

                context.UsingSubcontext(subcontext =>
                {
                    subcontext.CurrentFileName = $"{subcontext.CurrentFileName} -> order expression component '{segment}'";
                    segmentItems = transpiler.Transpile(segment, subcontext, suppressStackWarnings: true);
                });

                if (segmentItems.Count(x => x is Defrule) >= 2)
                {
                    throw new System.InvalidOperationException($"'{segment}' transpiles to more than one defrule, which order does not support.");
                }
                else if (segmentItems.Count(x => x is Defrule) == 0)
                {
                    throw new System.InvalidOperationException($"'{segment}' does not transpile to any defrules.");
                }

                foreach (var segmentItem in segmentItems)
                {
                    (segmentItem as Defrule)?.Conditions.Add(new Condition($"goal {goalNumber} {i}"));
                    (segmentItem as Defrule)?.Actions.Add(new Action($"set-goal {goalNumber} {(i + 1) % segments.Length}"));
                    items.Add(segmentItem);
                }
            }

            items.Reverse();
            items.Insert(0, new Defrule(new[] { "true" }, new[] { $"set-goal {goalNumber} 0", "disable-self" }));

            context.AddToScript(context.ApplyStacks(items));
        }
Example #3
0
        public override void Parse(string line, TranspilerContext context)
        {
            if (line.StartsWith("#stages"))
            {
                var stagesGoal = context.CreateGoal();

                var currentStage = 0;

                var rule = new Defrule(new[] { "true" }, new[] { $"set-goal {stagesGoal} 0", "disable-self" });
                context.AddToScript(context.ApplyStacks(rule));

                context.ConditionStack.Push(new Condition($"goal {stagesGoal} {currentStage}"));
                context.DataStack.Push(stagesGoal);
                context.DataStack.Push(currentStage);
            }
            else if (line.StartsWith("#advance"))
            {
                var condition = GetData(line)["condition"].Value;

                var previousStage = (int)context.DataStack.Pop();
                var stagesGoal    = context.DataStack.Pop();

                context.ConditionStack.Pop();

                var rule = new Defrule(new[] { $"goal {stagesGoal} {previousStage}", condition }, new[] { $"set-goal {stagesGoal} {previousStage + 1}" });
                context.AddToScript(context.ApplyStacks(rule));

                context.ConditionStack.Push(new Condition($"goal {stagesGoal} {previousStage + 1}"));

                context.DataStack.Push(stagesGoal);
                context.DataStack.Push(previousStage + 1);
            }
            else
            {
                context.DataStack.Pop();
                context.DataStack.Pop();
                context.ConditionStack.Pop();
            }
        }
Example #4
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data   = GetData(line);
            var name   = data["name"].Value;
            var mathOp = data["mathop"].Value;
            var value  = data["value"].Value;

            var rule = new Defrule();

            if (string.IsNullOrEmpty(mathOp))
            {
                if (!context.Goals.Contains(name))
                {
                    context.CreateGoal(name);
                }
                rule.Actions.Add(new Action($"set-goal {name} {value}"));
            }
            else
            {
                rule.Actions.Add(new Action($"up-modify-goal {name} c:{mathOp} {value}"));
            }

            context.AddToScript(context.ApplyStacks(rule));
        }
Example #5
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data = GetData(line);

            if (line.StartsWith("#select"))
            {
                var generateRule = new Defrule();

                var persistant = data["persistant"].Success;
                var goalNumber = context.CreateGoal();

                context.DataStack.Push(persistant);
                context.DataStack.Push(2);
                context.DataStack.Push(generateRule.Id);
                context.DataStack.Push(goalNumber);

                generateRule.Actions.Add(new Action($"set-goal {goalNumber} 0"));
                if (persistant)
                {
                    generateRule.Actions.Add(new Action("disable-self"));
                }

                context.AddToScript(context.ApplyStacks(generateRule));

                context.ConditionStack.Push(new Condition($"goal {goalNumber} 1"));
            }
            else if (line.StartsWith("#end"))
            {
                context.ConditionStack.Pop();

                var goalNumber     = (int)context.DataStack.Pop();
                var generateRuleId = (string)context.DataStack.Pop();
                var numberOfBlocks = (int)context.DataStack.Pop() - 1;
                var persistant     = (bool)context.DataStack.Pop();

                for (var i = 0; i < context.Script.Items.Count; i++)
                {
                    var rule = context.Script.Items[i] as Defrule;
                    if (rule != null && rule.Id == generateRuleId)
                    {
                        rule.Actions.Add(new Action($"generate-random-number {numberOfBlocks}"));
                        rule.Actions.Add(new Action($"up-get-fact random-number 0 {goalNumber}"));
                        if (persistant)
                        {
                            rule.Actions.Add(new Action("disable-self"));
                        }
                    }
                }
            }
            else
            {
                var goalNumber     = (int)context.DataStack.Pop();
                var generateRuleId = (string)context.DataStack.Pop();
                var numberOfBlocks = (int)context.DataStack.Pop();

                context.ConditionStack.Pop();
                context.ConditionStack.Push(new Condition($"goal {goalNumber} {numberOfBlocks}"));

                context.DataStack.Push(numberOfBlocks + 1);
                context.DataStack.Push(generateRuleId);
                context.DataStack.Push(goalNumber);
            }
        }
Example #6
0
        public override void Parse(string line, TranspilerContext context)
        {
            var maximum = GetData(line)["maximum"].Value.ReplaceIfNullOrEmpty(DefaultMaximum.ToString());

            var hasBuiltBuildingGoal = context.CreateGoal();
            var didResetGoal         = context.CreateGoal();

            foreach (var item in context.Script)
            {
                if (item is Defrule rule && rule.Actions.Any(x => x.Text.StartsWith("build ") && !IgnoredBuildings.Any(y => x.Text.EndsWith($" {y}"))))
                {
                    rule.Actions.Add(new Action($"set-goal {hasBuiltBuildingGoal} 1"));
                }
            }

            var rules = new List <Defrule>()
            {
                new Defrule(
                    new[] {
                    "true"
                },
                    new[]
                {
                    $"set-strategic-number sn-maximum-town-size {maximum}",
                    $"set-strategic-number sn-minimum-town-size {MinimumTownSize}",
                    $"set-strategic-number sn-safe-town-size {maximum}",
                    $"set-strategic-number sn-maximum-food-drop-distance {maximum}",
                    $"set-goal {didResetGoal} 0",
                    "disable-self"
                }),
                new Defrule(
                    new[]
                {
                    "true",
                },
                    new[]
                {
                    $"up-modify-sn sn-maximum-town-size c:+ {Increment}",
                    $"up-modify-sn sn-maximum-town-size c:min {maximum}",
                    $"set-strategic-number sn-minimum-town-size {MinimumTownSize}",
                    $"set-strategic-number sn-safe-town-size {maximum}",
                    $"set-strategic-number sn-maximum-food-drop-distance {maximum}",
                }),
                new Defrule(
                    new[]
                {
                    $"goal {hasBuiltBuildingGoal} 0",
                },
                    new[]
                {
                    $"set-goal {didResetGoal} 0",
                }),
                new Defrule(
                    new[]
                {
                    $"goal {hasBuiltBuildingGoal} 1",
                    $"goal {didResetGoal} 0",
                },
                    new[]
                {
                    "up-modify-sn sn-maximum-town-size s:= sn-minimum-town-size",
                    $"set-goal {didResetGoal} 1",
                }),
                new Defrule(
                    new[]
                {
                    "true",
                },
                    new[]
                {
                    $"set-goal {hasBuiltBuildingGoal} 0",
                }),
            };

            context.AddToScript(context.ApplyStacks(rules));
        }