Skip to content

Commit

Permalink
train balanced building check
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisc64 committed Jan 9, 2024
1 parent 4ce3365 commit 5c0f8ef
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 26 deletions.
78 changes: 62 additions & 16 deletions documentation/train balanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions language/Language/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, string> 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<string, string[]> GetResearches()
{
var research = new Dictionary<string, string[]> {
Expand Down
4 changes: 1 addition & 3 deletions language/Language/Rules/Train.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down Expand Up @@ -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}");
Expand Down
29 changes: 22 additions & 7 deletions language/Language/Rules/TrainBalanced.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -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)
{
Expand All @@ -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(
Expand All @@ -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));
}
Expand Down

0 comments on commit 5c0f8ef

Please sign in to comment.