Esempio n. 1
0
        public override void Parse(string line, TranspilerContext context)
        {
            if (line.StartsWith("#repeat"))
            {
                var data   = GetData(line);
                var amount = int.Parse(data["amount"].Value);
                var unit   = data["unit"].Value.TrimEnd('s');

                switch (unit)
                {
                case "minute": amount *= 60; break;

                case "hour": amount *= 3600; break;
                }

                var timerNumber = context.CreateTimer();

                context.AddToScript(context.ApplyStacks(new Defrule(new[] { "true" }, new[] { $"enable-timer {timerNumber} {amount}", "disable-self" })));

                context.ConditionStack.Push(new Condition($"timer-triggered {timerNumber}"));
                context.DataStack.Push(timerNumber);
                context.DataStack.Push(amount);
            }
            else
            {
                var amount      = context.DataStack.Pop();
                var timerNumber = context.DataStack.Pop();
                context.ConditionStack.Pop();

                context.AddToScript(context.ApplyStacks(new Defrule(new[] { $"timer-triggered {timerNumber}" }, new[] { $"disable-timer {timerNumber}", $"enable-timer {timerNumber} {amount}" })));
            }
        }
Esempio n. 2
0
        public override void Parse(string line, TranspilerContext context)
        {
            context.UsingVolatileGoal(gathererGoal =>
            {
                context.UsingVolatileGoal(farmGoal =>
                {
                    var infoRule = new Defrule(
                        new[]
                    {
                        "true",
                    },
                        new[]
                    {
                        $"up-get-fact unit-type-count villager {gathererGoal}",
                        $"up-modify-goal {gathererGoal} s:* sn-food-gatherer-percentage",
                        $"up-modify-goal {gathererGoal} c:/ 100",
                        $"up-get-fact building-type-count-total farm {farmGoal}",
                    });

                    var buildRule = new Defrule(
                        new[]
                    {
                        $"up-compare-goal {farmGoal} g:< {gathererGoal}",
                        "can-build farm",
                    },
                        new[]
                    {
                        "build farm"
                    });

                    context.AddToScript(context.ApplyStacks(infoRule));
                    context.AddToScript(context.ApplyStacks(buildRule));
                });
            });
        }
Esempio n. 3
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data = GetData(line);

            if (data["playerwildcard"].Success)
            {
                context.AddToScript(context.ApplyStacks(new Defrule(new[] { "true" }, new[] { $"chat-to-{data["playerwildcard"].Value} \"{data["message"].Value}\"".Replace("to-self", "local-to-self") })));
            }
            else
            {
                context.AddToScript(context.ApplyStacks(new Defrule(new[] { "true" }, new[] { $"chat-to-player {data["player"].Value} \"{data["message"].Value}\"" })));
            }
        }
Esempio n. 4
0
        public override void Parse(string line, TranspilerContext context)
        {
            var location = GetData(line)["location"].Value;
            var rule     = new Defrule(new[] { "true" }, new[] { $"up-send-scout group-type-land-explore scout-{location}" });

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 5
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data         = GetData(line);
            var enemyAmount  = data["enemyamount"].Value.ReplaceIfNullOrEmpty("1");
            var enemyName    = data["enemyname"].Value;
            var enemyType    = data["enemytype"].Value.ReplaceIfNullOrEmpty("unit");
            var player       = data["player"].Value.ReplaceIfNullOrEmpty("any-enemy");
            var createAmount = data["createamount"].Value;
            var createName   = data["createname"].Value;
            var createType   = data["createtype"].Value.ReplaceIfNullOrEmpty("unit");

            var action = createType == "unit" ? "train" : "build";

            var conditions = new List <string>();
            var actions    = new List <string>();

            conditions.Add($"players-{enemyType}-type-count {player} {enemyName} >= {enemyAmount}");

            if (!string.IsNullOrEmpty(createAmount))
            {
                conditions.Add($"{createType}-type-count-total {createName} < {createAmount}");
            }

            conditions.Add($"can-{action} {createName}");

            actions.Add($"{action} {createName}");

            context.AddToScript(context.ApplyStacks(new Defrule(conditions, actions)));
        }
Esempio n. 6
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data      = GetData(line);
            var resources = data["resourcelist"].Value
                            .ReplaceIfNullOrEmpty("wood and food and gold")
                            .Replace("all", "wood and food and gold and stone")
                            .Split(" and ");
            var threshold = data["threshold"].Value.ReplaceIfNullOrEmpty("300");
            var interval  = data["time"].Value.ReplaceIfNullOrEmpty("60");

            var rules = new List <Defrule>();
            var timer = context.CreateTimer();

            rules.Add(new Defrule(new[] { "true" }, new[] { $"enable-timer {timer} {interval}", "disable-self" }));

            (var escrowRules, var goalToResourceMap) = CreateNonEscrowedResourceGoals(context, resources);
            rules.AddRange(escrowRules);

            foreach (var rule in GetBalanceRules(8, int.Parse(threshold), goalToResourceMap, goalToResourceMap.Keys))
            {
                rule.Conditions.Add(new Condition($"timer-triggered {timer}"));
                rules.Add(rule);
            }

            rules.Add(new Defrule(new[] { $"timer-triggered {timer}" }, new[] { $"disable-timer {timer}", $"enable-timer {timer} {interval}" }));

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

            context.FreeVolatileGoals(goalToResourceMap.Keys);
        }
Esempio n. 7
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data     = GetData(line);
            var style    = data["style"].Value;
            var headroom = data["headroom"].Value.ReplaceIfNullOrEmpty(DefaultHeadroom.ToString());

            var building = style == "houses" ? "house" : Game.YurtId.ToString();

            var conditions = new[]
            {
                "population-headroom != 0",
                $"up-pending-objects c: {building} < 2",
                $"can-build {building}",
                $"housing-headroom < {headroom}",
            };

            var actions = new[]
            {
                $"build {building}",
            };

            var rule = new Defrule(conditions, actions);

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 8
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data       = GetData(line);
            var amount     = data["amount"].Value;
            var unit       = data["unit"].Value;
            var escrowList = data["escrowlist"].Value;

            var conditions = new List <string>();
            var actions    = new List <string>();

            if (!string.IsNullOrEmpty(escrowList))
            {
                conditions.Add($"can-train-with-escrow {unit}");
                foreach (var resource in escrowList.Split(" and "))
                {
                    actions.Add($"release-escrow {resource}");
                }
            }
            else
            {
                conditions.Add($"can-train {unit}");
            }

            if (!string.IsNullOrEmpty(amount))
            {
                conditions.Add($"unit-type-count-total {unit + (SetUnits.Contains(unit) ? "-set" : "")} < {amount}");
            }

            actions.Add($"train {unit}");

            context.AddToScript(context.ApplyStacks(new Defrule(conditions, actions)));
        }
Esempio n. 9
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data      = GetData(line);
            var material  = data["material"].Value;
            var type      = data["type"].Value;
            var perimeter = data["perimeter"].Value;

            var building = material == "stone" ? "stone-wall-line" : "palisade-wall";

            var conditions = new List <string>();
            var actions    = new List <string>();

            if (type == "walls")
            {
                conditions.Add($"can-build-wall {perimeter} {building}");

                actions.Add($"build-wall {perimeter} {building}");
            }
            else
            {
                conditions.Add($"building-type-count-total {building} > 0");
                conditions.Add($"can-build-gate {perimeter}");
                conditions.Add("building-type-count-total gate < 5");

                actions.Add($"build-gate {perimeter}");
            }

            var rule = new Defrule(conditions, actions);

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 10
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data     = GetData(line);
            var amount   = int.Parse(data["amount"].Value);
            var fromList = data["fromlist"].Value.Split(" and ");
            var toList   = data["tolist"].Value.Split(" and ");

            var conditions = new List <string>()
            {
                "true"
            };
            var actions = new List <string>()
            {
                "do-nothing"
            };

            foreach (var resource in fromList)
            {
                conditions.Add($"strategic-number sn-{resource}-gatherer-percentage >= {amount / fromList.Length}");
                actions.Add($"up-modify-sn sn-{resource}-gatherer-percentage c:- {amount / fromList.Length}");
            }

            foreach (var resource in toList)
            {
                conditions.Add($"strategic-number sn-{resource}-gatherer-percentage <= {100 - amount / toList.Length}");
                actions.Add($"up-modify-sn sn-{resource}-gatherer-percentage c:+ {amount / toList.Length}");
            }

            var rule = new Defrule(conditions, actions);

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 11
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data     = GetData(line);
            var amount   = data["amount"].Value;
            var resource = data["resource"].Value;

            context.AddToScript(context.ApplyStacks(new Defrule(new[] { "true" }, new[] { $"cc-add-resource {resource} {amount}" })));
        }
Esempio n. 12
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data = GetData(line);
            var type = data["type"].Value;
            var name = data["name"].Value;

            var rule = new Defrule(new[] { "true" }, new[] { $"delete-{type} {name}" });

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 13
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data     = GetData(line);
            var amount   = data["amount"].Value;
            var building = data["building"].Value;

            var rule = new Defrule(new[] { "true" }, new[] { $"up-assign-builders c: {building} c: {amount}" });

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 14
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data     = GetData(line);
            var amount   = data["amount"].Value;
            var resource = data["resource"].Value;

            var rule = new Defrule(new[] { "true" }, new[] { $"set-escrow-percentage {resource} {amount}" });

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 15
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data = GetData(line);

            var amount   = data["amount"].Value;
            var resource = data["resource"].Value;
            var player   = data["player"].Value;

            var rule = new Defrule(new[] { "true" }, new[] { $"tribute-to-player {player} {resource} {amount}" });

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 16
0
        public override void Parse(string line, TranspilerContext context)
        {
            var templateName = GetData(line)["name"].Value;

            if (!context.Templates.ContainsKey(templateName))
            {
                throw new System.InvalidOperationException($"Template '{templateName}' is not defined.");
            }

            var parameters = new Dictionary <string, string>();

            foreach (var match in ParamRegex.Matches(line).Cast <Match>())
            {
                var name  = match.Groups["name"].Value;
                var value = match.Groups["value"].Value;

                if (value.StartsWith("\"") && value.EndsWith("\""))
                {
                    value = value.Trim('"');
                }
                parameters[name] = value.Replace("\\\"", "\"");
            }

            var template = context.Templates[templateName];

            foreach ((string key, string value) in parameters)
            {
                template = template.Replace($"{{{key}}}", value);
            }

            Script rules = null;

            context.UsingSubcontext(subcontext =>
            {
                subcontext.CurrentFileName = $"template '{templateName}'";

                var transpiler = new Transpiler();
                rules          = transpiler.Transpile(template, subcontext);
            });

            if (context.ConditionStack.Any())
            {
                foreach (var rule in rules)
                {
                    (rule as Defrule)?.Actions.AddRange(context.ActionStack);
                }
                context.AddToScriptWithJump(rules, Condition.JoinConditions("and", context.ConditionStack).Invert());
            }
            else
            {
                context.AddToScript(context.ApplyStacks(rules));
            }
        }
Esempio n. 17
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data         = GetData(line);
            var amount       = data["amount"].Value;
            var resourceList = data["resourcelist"].Value
                               .ReplaceIfNullOrEmpty("wood and food and gold and stone")
                               .Split(" and ");

            var rules = new List <Defrule>();

            (var escrowRules, var goalToResourceMap) = CreateNonEscrowedResourceGoals(context, resourceList);

            foreach (var(resourceGoal, resource) in goalToResourceMap)
            {
                if (resource == "gold")
                {
                    continue;
                }

                rules.Add(new Defrule(
                              new[] { $"up-compare-goal {resourceGoal} c:> {amount}", $"can-sell-commodity {resource}" },
                              new[] { $"sell-commodity {resource}" }));
            }

            if (resourceList.Contains("gold"))
            {
                var goldGoal = goalToResourceMap.First(x => x.Value == "gold").Key;

                foreach (var rule in rules)
                {
                    rule.Conditions.Add(new Condition($"up-compare-goal {goldGoal} c:< {amount}"));
                }

                foreach (var(resourceGoal, resource) in goalToResourceMap)
                {
                    if (resource == "gold")
                    {
                        continue;
                    }

                    rules.Add(new Defrule(
                                  new[] { $"up-compare-goal {resourceGoal} c:< {amount}", $"up-compare-goal {goldGoal} c:> {amount}", $"can-buy-commodity {resource}" },
                                  new[] { $"buy-commodity {resource}" }));
                }
            }

            rules.InsertRange(0, escrowRules);

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

            context.FreeVolatileGoals(goalToResourceMap.Keys);
        }
Esempio n. 18
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();
            }
        }
Esempio n. 19
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data       = GetData(line);
            var forward    = data["forward"].Success;
            var amount     = data["amount"].Value;
            var building   = data["building"].Value;
            var near       = data["near"].Value;
            var escrowList = data["escrowlist"].Value;

            var conditions = new List <string>();
            var actions    = new List <string>();

            if (!string.IsNullOrEmpty(escrowList))
            {
                conditions.Add($"can-build-with-escrow {building}");
                foreach (var resource in escrowList.Split(" and "))
                {
                    actions.Add($"release-escrow {resource}");
                }
            }
            else
            {
                conditions.Add($"can-build {building}");
            }

            var compressable = true;

            conditions.Add($"up-pending-objects c: {building} < {MaxConcurrentBuilds}");
            if (!string.IsNullOrEmpty(amount))
            {
                conditions.Add($"building-type-count-total {building} < {amount}");
            }

            if (string.IsNullOrEmpty(near))
            {
                actions.Add($"build{(forward ? "-forward" : "")} {building}");
            }
            else
            {
                actions.Add($"up-set-placement-data {(forward ? "any-enemy" : "my-player-number")} {near} c: 4");
                actions.Add($"up-build place-{(forward ? "forward" : "control")} 0 c: {building}");
                compressable = false;
            }

            context.AddToScript(context.ApplyStacks(new Defrule(conditions, actions)
            {
                Compressable = compressable
            }));
        }
Esempio n. 20
0
        public override void Parse(string line, TranspilerContext context)
        {
            var perimeter = GetData(line)["perimeter"].Value;

            var rule = new Defrule(
                new[]
            {
                "true",
            },
                new[]
            {
                $"enable-wall-placement {perimeter}",
                "disable-self",
            });

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 21
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data      = GetData(line);
            var operation = data["operation"].Value;
            var amount    = data["amount"].Value;

            var rule = new Defrule();

            if (operation == "set")
            {
                rule.Actions.Add(new Action($"set-strategic-number sn-maximum-town-size {amount}"));
            }
            else
            {
                rule.Actions.Add(new Action($"up-modify-sn sn-maximum-town-size c:{(operation == "increase" ? "+" : "-")} {amount}"));
            }

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 22
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));
        }
Esempio n. 23
0
        public override void Parse(string line, TranspilerContext context)
        {
            if (line.StartsWith("#reply"))
            {
                var data        = GetData(line);
                var playerType  = data["playertype"].Value;
                var tauntNumber = data["tauntnumber"].Value;

                context.ConditionStack.Push(new Condition($"taunt-detected any-{playerType} {tauntNumber}"));
                context.DataStack.Push(playerType);
                context.DataStack.Push(tauntNumber);
            }
            else
            {
                var tauntNumber = context.DataStack.Pop();
                var playerType  = context.DataStack.Pop();
                context.AddToScript(context.ApplyStacks(new Defrule(new[] { "true" }, new[] { $"acknowledge-taunt this-any-{playerType} {tauntNumber}" })));
                context.ConditionStack.Pop();
            }
        }
Esempio n. 24
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))
            {
                rule.Actions.Add(new Action($"set-strategic-number {name} {value}"));
            }
            else
            {
                rule.Actions.Add(new Action($"up-modify-sn {name} c:{mathOp} {value}"));
            }

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 25
0
        public override void Parse(string line, TranspilerContext context)
        {
            var amount = GetData(line)["amount"].Value;

            Defrule rule;

            if (string.IsNullOrEmpty(amount))
            {
                rule = new Defrule(
                    new[] { "true" },
                    new[] { "attack-now" });
            }
            else
            {
                rule = new Defrule(
                    new[] { $"military-population >= {amount}" },
                    new[] { "attack-now" });
            }

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 26
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data         = GetData(line);
            var action       = data["action"].Value;
            var resource     = data["resource"].Value;
            var testResource = data["testresource"].Value;
            var comparison   = data["comparison"].Value;
            var amount       = data["amount"].Value;

            var rule = new Defrule(
                new[]
            {
                $"{testResource}-amount {comparison} {amount}",
                $"can-{action}-commodity {resource}",
            },
                new[]
            {
                $"{action}-commodity {resource}",
            });

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 27
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data  = GetData(line);
            var wood  = data["wood"].Value;
            var food  = data["food"].Value;
            var gold  = data["gold"].Value;
            var stone = data["stone"].Value;

            var rule = new Defrule(
                new[]
            {
                "true",
            },
                new[]
            {
                $"set-strategic-number sn-wood-gatherer-percentage {wood}",
                $"set-strategic-number sn-food-gatherer-percentage {food}",
                $"set-strategic-number sn-gold-gatherer-percentage {gold}",
                $"set-strategic-number sn-stone-gatherer-percentage {stone}",
            });

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 28
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data       = GetData(line);
            var findType   = data["findtype"].Value;
            var playerType = data["playertype"].Value;

            var rule = new Defrule();

            if (findType != "winning")
            {
                context.UsingVolatileGoal(goal =>
                {
                    rule.Actions.Add(new Action($"up-find-player {playerType} find-{FindTypes[findType]} {goal}"));
                    rule.Actions.Add(new Action($"up-modify-sn sn-target-player-number g:= {goal}"));
                });
            }
            else
            {
                rule.Actions.Add(new Action("set-strategic-number sn-target-player-number 0"));
            }

            context.AddToScript(context.ApplyStacks(rule));
        }
Esempio n. 29
0
        public override void Parse(string line, TranspilerContext context)
        {
            var data       = GetData(line);
            var item       = data["item"].Value;
            var category   = data["category"].Value;
            var escrowList = data["escrowlist"].Value;

            var conditions = new List <string>();
            var actions    = new List <string>();

            if (!string.IsNullOrEmpty(escrowList))
            {
                conditions.Add("can-research-with-escrow {0}");
                foreach (var resource in escrowList.Split(" and "))
                {
                    actions.Add($"release-escrow {resource}");
                }
            }
            else
            {
                conditions.Add("can-research {0}");
            }

            actions.Add("research {0}");

            var rules = new List <Defrule>();

            foreach (var research in AllResearch.Keys.Contains(category) ? AllResearch[category] : new[] { item })
            {
                rules.Add(new Defrule(
                              conditions.Select(x => string.Format(x, research)),
                              actions.Select(x => string.Format(x, research))));
            }

            context.AddToScript(context.ApplyStacks(rules));
        }
Esempio n. 30
0
        public override void Parse(string line, TranspilerContext context)
        {
            var rule = new Defrule(new[] { "true" }, new[] { GetData(line)["action"].Value });

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