From 5c0f8ef981e30c3f7d0daf5585cbbc926ce22fb6 Mon Sep 17 00:00:00 2001 From: lewisc64 Date: Tue, 9 Jan 2024 05:46:04 +0000 Subject: [PATCH] train balanced building check --- documentation/train balanced.md | 78 +++++++++++++++++++----- language/Language/Game.cs | 19 ++++++ language/Language/Rules/Train.cs | 4 +- language/Language/Rules/TrainBalanced.cs | 29 ++++++--- 4 files changed, 104 insertions(+), 26 deletions(-) diff --git a/documentation/train balanced.md b/documentation/train balanced.md index b35d5b6..34f7a7e 100644 --- a/documentation/train balanced.md +++ b/documentation/train balanced.md @@ -13,17 +13,40 @@ train balanced 1 archer-line 1 militiaman-line (true) => (set-goal 1 0) + (set-goal 4 0) + (up-get-object-type-data c: archer-line 56 3) +) +(defrule + (unit-available archer-line) + (up-object-type-count g: 3 c:>= 1) +=> (up-get-fact unit-type-count-total archer-line 2) (up-modify-goal 1 g:+ 2) + (up-modify-goal 4 c:+ 1) +) +(defrule + (true) +=> + (up-get-object-type-data c: militiaman-line 56 3) +) +(defrule + (unit-available militiaman-line) + (up-object-type-count g: 3 c:>= 1) +=> (up-get-fact unit-type-count-total militiaman-line 2) (up-modify-goal 1 g:+ 2) + (up-modify-goal 4 c:+ 1) +) +(defrule + (true) +=> (up-get-fact unit-type-count-total archer-line 2) - (up-modify-goal 3 g:= 1) - (up-modify-goal 3 c:* 1) - (up-modify-goal 3 c:/ 2) + (up-modify-goal 5 g:= 1) + (up-modify-goal 5 c:* 1) + (up-modify-goal 5 g:/ 4) ) (defrule - (up-compare-goal 2 g:<= 3) + (up-compare-goal 2 g:<= 5) (can-train archer-line) => (train archer-line) @@ -32,12 +55,12 @@ train balanced 1 archer-line 1 militiaman-line (true) => (up-get-fact unit-type-count-total militiaman-line 2) - (up-modify-goal 3 g:= 1) - (up-modify-goal 3 c:* 1) - (up-modify-goal 3 c:/ 2) + (up-modify-goal 5 g:= 1) + (up-modify-goal 5 c:* 1) + (up-modify-goal 5 g:/ 4) ) (defrule - (up-compare-goal 2 g:<= 3) + (up-compare-goal 2 g:<= 5) (can-train militiaman-line) => (train militiaman-line) @@ -53,17 +76,40 @@ train balanced 10 scout-cavalry-line 3 spearman-line (true) => (set-goal 1 0) + (set-goal 4 0) + (up-get-object-type-data c: scout-cavalry-line 56 3) +) +(defrule + (unit-available scout-cavalry-line) + (up-object-type-count g: 3 c:>= 1) +=> (up-get-fact unit-type-count-total scout-cavalry-line 2) (up-modify-goal 1 g:+ 2) + (up-modify-goal 4 c:+ 10) +) +(defrule + (true) +=> + (up-get-object-type-data c: spearman-line 56 3) +) +(defrule + (unit-available spearman-line) + (up-object-type-count g: 3 c:>= 1) +=> (up-get-fact unit-type-count-total spearman-line 2) (up-modify-goal 1 g:+ 2) + (up-modify-goal 4 c:+ 3) +) +(defrule + (true) +=> (up-get-fact unit-type-count-total scout-cavalry-line 2) - (up-modify-goal 3 g:= 1) - (up-modify-goal 3 c:* 10) - (up-modify-goal 3 c:/ 13) + (up-modify-goal 5 g:= 1) + (up-modify-goal 5 c:* 10) + (up-modify-goal 5 g:/ 4) ) (defrule - (up-compare-goal 2 g:<= 3) + (up-compare-goal 2 g:<= 5) (can-train scout-cavalry-line) => (train scout-cavalry-line) @@ -72,12 +118,12 @@ train balanced 10 scout-cavalry-line 3 spearman-line (true) => (up-get-fact unit-type-count-total spearman-line 2) - (up-modify-goal 3 g:= 1) - (up-modify-goal 3 c:* 3) - (up-modify-goal 3 c:/ 13) + (up-modify-goal 5 g:= 1) + (up-modify-goal 5 c:* 3) + (up-modify-goal 5 g:/ 4) ) (defrule - (up-compare-goal 2 g:<= 3) + (up-compare-goal 2 g:<= 5) (can-train spearman-line) => (train spearman-line) diff --git a/language/Language/Game.cs b/language/Language/Game.cs index 22fbbb8..783c3df 100644 --- a/language/Language/Game.cs +++ b/language/Language/Game.cs @@ -19,6 +19,10 @@ public static class Game public static readonly int ObjectDataDistance = 44; + public static readonly int ObjectDataTrainSite = 56; + + public static readonly int ObjectDataTrainTime = 57; + public static readonly int StatusResource = 3; public static readonly int StatusDown = 4; @@ -67,6 +71,21 @@ public static class Game public static readonly int[] AllClosedGateIds = new int[] { 789, 793, 797, 801, 64, 88, 659, 667, 63, 85, 660, 668 }; + public static readonly Dictionary UnitSets = new() + { + { "monk", "monk-set" }, + { "trebuchet", "trebuchet-set" }, + { "huskarl-line", "huskarl-set" }, + { "konnik-line", "konnik-set" }, + { "krepost-konnik-line", "konnik-set" }, + { "tarkan-line", "tarkan-set" }, + { "kipchak-line", "kipchak-set" }, + { "serjeant-line", "serjeant-set" }, + { "donjon-serjeant-line", "serjeant-set" }, + { "flemish-pikeman", "flemish-militia-set" }, + { "ratha-line", "ratha-set" }, + }; + public static Dictionary GetResearches() { var research = new Dictionary { diff --git a/language/Language/Rules/Train.cs b/language/Language/Rules/Train.cs index b01495d..5326bbf 100644 --- a/language/Language/Rules/Train.cs +++ b/language/Language/Rules/Train.cs @@ -7,8 +7,6 @@ namespace Language.Rules [ActiveRule(-1)] public class Train : RuleBase { - private static readonly string[] SetUnits = new[] { "monk", "trebuchet" }; - public override string Name => "train"; public override string Help => "Trains a unit using the specified parameters."; @@ -55,7 +53,7 @@ public override void Parse(string line, TranspilerContext context) if (!string.IsNullOrEmpty(amount)) { - conditions.Add($"unit-type-count-total {unit + (SetUnits.Contains(unit) ? "-set" : "")} < {amount}"); + conditions.Add($"unit-type-count-total {(Game.UnitSets.ContainsKey(unit) ? Game.UnitSets[unit] : unit)} < {amount}"); } actions.Add($"train {unit}"); diff --git a/language/Language/Rules/TrainBalanced.cs b/language/Language/Rules/TrainBalanced.cs index dcc606a..2ec36ae 100644 --- a/language/Language/Rules/TrainBalanced.cs +++ b/language/Language/Rules/TrainBalanced.cs @@ -35,11 +35,12 @@ public override void Parse(string line, TranspilerContext context) var militaryCountGoal = context.CreateVolatileGoal(); var unitCountGoal = context.CreateVolatileGoal(); - var unitThresholdGoal = context.CreateVolatileGoal(); + var unitBuildingGoal = context.CreateVolatileGoal(); + var totalWeightGoal = context.CreateVolatileGoal(); - rules.Add(new Defrule(new[] { "true" }, new[] { $"set-goal {militaryCountGoal} 0" })); + rules.Add(new Defrule(new[] { "true" }, new[] { $"set-goal {militaryCountGoal} 0", $"set-goal {totalWeightGoal} 0" })); - foreach (var (unit, _) in unitData) + foreach (var (unit, weight) in unitData) { rules.Add(new Defrule( new[] @@ -48,12 +49,24 @@ public override void Parse(string line, TranspilerContext context) }, new[] { - $"up-get-fact unit-type-count-total {unit} {unitCountGoal}", + $"up-get-object-type-data c: {unit} {Game.ObjectDataTrainSite} {unitBuildingGoal}", + })); + + rules.Add(new Defrule( + new[] + { + $"unit-available {unit}", + $"up-object-type-count g: {unitBuildingGoal} c:>= 1", + }, + new[] + { + $"up-get-fact unit-type-count-total {(Game.UnitSets.ContainsKey(unit) ? Game.UnitSets[unit] : unit)} {unitCountGoal}", $"up-modify-goal {militaryCountGoal} g:+ {unitCountGoal}", + $"up-modify-goal {totalWeightGoal} c:+ {weight}", })); } - var totalWeight = unitData.Select(x => x.Item2).Sum(); + var unitThresholdGoal = context.CreateVolatileGoal(); foreach (var (unit, weight) in unitData) { @@ -64,10 +77,10 @@ public override void Parse(string line, TranspilerContext context) }, new[] { - $"up-get-fact unit-type-count-total {unit} {unitCountGoal}", + $"up-get-fact unit-type-count-total {(Game.UnitSets.ContainsKey(unit) ? Game.UnitSets[unit] : unit)} {unitCountGoal}", $"up-modify-goal {unitThresholdGoal} g:= {militaryCountGoal}", $"up-modify-goal {unitThresholdGoal} c:* {weight}", - $"up-modify-goal {unitThresholdGoal} c:/ {totalWeight}", + $"up-modify-goal {unitThresholdGoal} g:/ {totalWeightGoal}", })); rules.Add(new Defrule( @@ -84,7 +97,9 @@ public override void Parse(string line, TranspilerContext context) context.FreeVolatileGoal(militaryCountGoal); context.FreeVolatileGoal(unitCountGoal); + context.FreeVolatileGoal(unitBuildingGoal); context.FreeVolatileGoal(unitThresholdGoal); + context.FreeVolatileGoal(totalWeightGoal); context.AddToScript(context.ApplyStacks(rules)); }