Move goal weights into BehaviorDef so a pack can rebalance decisions without a fork.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Leonid Pershin
2026-08-20 00:27:52 +03:00
co-authored by Cursor
parent fcbc5bb22c
commit e1a0f06017
10 changed files with 293 additions and 50 deletions
+123 -3
View File
@@ -265,6 +265,94 @@ public class DecisionPlannerTests
Assert.Equal(GoalKind.Duty, decision.Intent.Kind);
}
/// <summary>
/// Vanilla numbers after the move must keep the same winners: a toilet at zero leaves class,
/// a need just under the threshold does not, a sitting beats a walk but not a lesson.
/// </summary>
[Fact]
public void VanillaWeights_MatchTheKnownDecisionTable()
{
var (catalog, map, walks) = World();
var classroom = map.Rooms.First(room => room.Def == "Classroom").Id;
var corridor = map.Rooms.First(room => room.Def == "Corridor").Id;
var zeroToilet = Decide(catalog, map, walks, Actor(classroom, boundToLesson: true, classroom, Needs(toilet: 0f)));
Assert.Equal(GoalKind.Need, zeroToilet.Intent.Kind);
Assert.Equal("Toilet", zeroToilet.Intent.Id);
Assert.Equal("UseToilet", zeroToilet.Intent.ActionId);
Assert.Equal(DecisionPlanner.NeedWeightAtZero, zeroToilet.Intent.Weight);
var justBelow = Decide(catalog, map, walks, Actor(classroom, boundToLesson: true, classroom, Needs(toilet: 0.34f)));
Assert.Equal(GoalKind.Duty, justBelow.Intent.Kind);
Assert.Equal(classroom, justBelow.Intent.Id);
Assert.Equal(DecisionPlanner.DutyLessonWeight, justBelow.Intent.Weight);
var peckishSitting = Decide(
catalog, map, walks,
Actor(corridor, boundToLesson: false, corridor, Needs(hunger: 0.6f), lunchWindowOpen: true));
Assert.Equal(GoalKind.Need, peckishSitting.Intent.Kind);
Assert.Equal("EatLunch", peckishSitting.Intent.ActionId);
Assert.Equal(DecisionPlanner.LunchWeight, peckishSitting.Intent.Weight);
var peckishLesson = Decide(
catalog, map, walks,
Actor(classroom, boundToLesson: true, classroom, Needs(hunger: 0.6f), lunchWindowOpen: true));
Assert.Equal(GoalKind.Duty, peckishLesson.Intent.Kind);
Assert.Equal(DecisionPlanner.DutyLessonWeight, peckishLesson.Intent.Weight);
Assert.NotEqual("EatLunch", peckishLesson.Intent.ActionId);
}
[Fact]
public void PackWithLunchAboveLesson_PullsTheClassToTheCafeteria()
{
var (catalog, map, walks) = WorldWithLunchFirst();
var classroom = map.Rooms.First(room => room.Def == "Classroom").Id;
var rules = catalog.BehaviorRules;
Assert.NotNull(rules);
Assert.True(rules.LunchWeight > rules.DutyLessonWeight);
var decision = Decide(
catalog, map, walks,
Actor(classroom, boundToLesson: true, classroom, Needs(hunger: 0.6f), lunchWindowOpen: true));
Assert.Equal(GoalKind.Need, decision.Intent.Kind);
Assert.Equal("EatLunch", decision.Intent.ActionId);
Assert.Equal("Cafeteria", map.Rooms.First(room => room.Id == decision.WalkTo).Def);
}
[Fact]
public void CatalogWithoutBehaviorDef_UsesCodeDefaults()
{
var (catalog, map, walks) = WorldWithoutBehavior();
Assert.Null(catalog.BehaviorRules);
var classroom = map.Rooms.First(room => room.Def == "Classroom").Id;
var corridor = map.Rooms.First(room => room.Def == "Corridor").Id;
var zeroToilet = Decide(catalog, map, walks, Actor(classroom, boundToLesson: true, classroom, Needs(toilet: 0f)));
Assert.Equal(GoalKind.Need, zeroToilet.Intent.Kind);
Assert.Equal("UseToilet", zeroToilet.Intent.ActionId);
Assert.Equal(DecisionPlanner.NeedWeightAtZero, zeroToilet.Intent.Weight);
var peckishSitting = Decide(
catalog, map, walks,
Actor(corridor, boundToLesson: false, corridor, Needs(hunger: 0.6f), lunchWindowOpen: true));
Assert.Equal("EatLunch", peckishSitting.Intent.ActionId);
Assert.Equal(DecisionPlanner.LunchWeight, peckishSitting.Intent.Weight);
var peckishLesson = Decide(
catalog, map, walks,
Actor(classroom, boundToLesson: true, classroom, Needs(hunger: 0.6f), lunchWindowOpen: true));
Assert.Equal(GoalKind.Duty, peckishLesson.Intent.Kind);
Assert.Equal(DecisionPlanner.DutyLessonWeight, peckishLesson.Intent.Weight);
}
private static Decision Decide(
DefCatalog catalog,
MapLayout map,
WalkGraph walks,
ActorState state) =>
DecisionPlanner.Decide(catalog, map, walks, state, (_, _) => 0);
private static ActorState Actor(
string node,
bool boundToLesson,
@@ -287,10 +375,12 @@ public class DecisionPlannerTests
intent ?? Intent.None,
lunchWindowOpen);
private static Dictionary<string, float> FullNeeds() => new(StringComparer.Ordinal)
private static Dictionary<string, float> FullNeeds() => Needs();
private static Dictionary<string, float> Needs(float toilet = 1f, float hunger = 1f) => new(StringComparer.Ordinal)
{
["Toilet"] = 1f,
["Hunger"] = 1f,
["Toilet"] = toilet,
["Hunger"] = hunger,
["Social"] = 1f,
["Sleep"] = 1f,
};
@@ -300,4 +390,34 @@ public class DecisionPlannerTests
var (catalog, map) = Fixtures.Vanilla();
return (catalog, map, WalkGraph.Build(catalog, map));
}
private static (DefCatalog Catalog, MapLayout Map, WalkGraph Walks) WorldWithoutBehavior()
{
var root = Path.Combine(AppContext.BaseDirectory, "vanilla");
var documents = PackDocuments.FromDirectory(CatalogLoader.CorePackId, root)
.Where(document =>
{
var path = document.RelativePath.Replace('\\', '/');
return path.IndexOf("defs/behavior/", StringComparison.OrdinalIgnoreCase) < 0;
})
.ToList();
var catalog = new CatalogLoader().Load([CatalogLoader.CorePackId], documents);
var map = CatalogLoader.LastDefaultMap([CatalogLoader.CorePackId], documents);
Assert.NotNull(map);
return (catalog, map, WalkGraph.Build(catalog, map));
}
private static (DefCatalog Catalog, MapLayout Map, WalkGraph Walks) WorldWithLunchFirst()
{
var root = Path.Combine(AppContext.BaseDirectory, "vanilla");
var documents = PackDocuments.FromDirectory(CatalogLoader.CorePackId, root).ToList();
documents.Add(new ContentDocument(
"addon",
"patches/lunch-first.jsonc",
"""{ "target": "Behavior", "ops": [ { "op": "replace", "path": "/lunchWeight", "value": 11 } ] }"""));
var catalog = new CatalogLoader().Load([CatalogLoader.CorePackId, "addon"], documents);
var map = CatalogLoader.LastDefaultMap([CatalogLoader.CorePackId], documents);
Assert.NotNull(map);
return (catalog, map, WalkGraph.Build(catalog, map));
}
}
@@ -0,0 +1,50 @@
namespace HSchool.Content.Tests;
public class BehaviorDefTests
{
private readonly CatalogLoader _loader = new();
[Theory]
[InlineData("dutyLessonWeight")]
[InlineData("dutyTravelWeight")]
[InlineData("needWeightAtZero")]
[InlineData("lunchWeight")]
public void NegativeGoalWeight_FailsTheCatalog(string field)
{
var error = Assert.Throws<ContentLoadException>(() => _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(
CatalogLoader.CorePackId,
"behavior",
"rules",
$$"""{ "defName": "Behavior", "{{field}}": -1 }"""),
]));
Assert.Contains("negative", error.Message, StringComparison.OrdinalIgnoreCase);
Assert.Contains("Behavior", error.Message, StringComparison.Ordinal);
}
[Fact]
public void LunchHeavierThanLesson_WarnsAndStillLoads()
{
var log = new RecordingLog();
var catalog = _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(
CatalogLoader.CorePackId,
"behavior",
"rules",
"""{ "defName": "Behavior", "dutyLessonWeight": 10, "lunchWeight": 11 }"""),
],
log);
Assert.NotNull(catalog.BehaviorRules);
Assert.Equal(11f, catalog.BehaviorRules.LunchWeight);
Assert.Contains(
log.Warnings,
warning => warning.Contains("lunchWeight", StringComparison.Ordinal)
&& warning.Contains("dutyLessonWeight", StringComparison.Ordinal));
}
}
@@ -81,6 +81,10 @@ public class VanillaCoreTests
Assert.Equal(0, catalog.BehaviorRules.CommuteSlackMin);
Assert.Equal(6, catalog.BehaviorRules.CommuteSlackMax);
Assert.Equal(0.35f, catalog.BehaviorRules.NeedThreshold);
Assert.Equal(10f, catalog.BehaviorRules.DutyLessonWeight);
Assert.Equal(5f, catalog.BehaviorRules.DutyTravelWeight);
Assert.Equal(20f, catalog.BehaviorRules.NeedWeightAtZero);
Assert.Equal(6f, catalog.BehaviorRules.LunchWeight);
}
/// <summary>