Grow lesson skills only when the assigned teacher is standing in the room.

The timetable already named a teacher; learning ignored whether they were in the toilet, still walking, or not a person at all.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Leonid Pershin
2026-08-20 08:33:28 +03:00
co-authored by Cursor
parent 936d6fb04f
commit fbc32f2bfc
9 changed files with 312 additions and 18 deletions
@@ -191,5 +191,6 @@
"ActionEnded": "finished: {0}",
"ApparelReplaced": "got a new {0}",
"ApparelChanged": "changed clothes: {0}",
"LessonNoTeacher": "lesson without a teacher: {0}",
"core": "Core",
}
@@ -191,5 +191,6 @@
"ActionEnded": "закончил: {0}",
"ApparelReplaced": "получил новую {0}",
"ApparelChanged": "переоделся: {0}",
"LessonNoTeacher": "урок без учителя: {0}",
"core": "Базовая игра",
}
+32 -2
View File
@@ -8,10 +8,14 @@ namespace HSchool.Simulation;
/// <summary>
/// Grows skills for people who are actually in the lesson: at the room, not walking, not off
/// doing something else. The formula lives in <see cref="HSchool.Ai.LessonLearning"/>.
/// doing something else, and with the assigned teacher standing there. The formula lives in
/// <see cref="HSchool.Ai.LessonLearning"/>.
/// </summary>
internal static class LessonLearningSystem
{
private static readonly QueryDescription Places =
new QueryDescription().WithAll<PersonIdentity, Presence>();
private static readonly QueryDescription People =
new QueryDescription().WithAll<PersonIdentity, PersonSkills, PersonTraits, PersonNeeds, PersonRoles, Presence, PersonActivity>();
@@ -38,11 +42,19 @@ internal static class LessonLearningSystem
var weekday = SchoolDay.WeekdayIndex(school.Clock.Time);
var catalog = school.Catalog;
var world = school.World;
var places = new Dictionary<string, Presence>(StringComparer.Ordinal);
world.Query(
in Places,
(ref PersonIdentity identity, ref Presence presence) =>
{
places[identity.Id] = presence;
});
world.Query(
in People,
(ref PersonIdentity identity, ref PersonSkills skills, ref PersonTraits traits, ref PersonNeeds needs, ref PersonRoles roles, ref Presence presence, ref PersonActivity activity) =>
{
if (activity.IsActive || presence.NodeId is null || presence.Path.Length > 0 || presence.RemainingMinutes > 0)
if (activity.IsActive || !IsStanding(presence))
{
return;
}
@@ -56,12 +68,19 @@ internal static class LessonLearningSystem
var lesson = CurrentLesson(school, person, weekday, slot.Index);
if (lesson is null
|| presence.NodeId is null
|| !presence.NodeId.Equals(lesson.RoomId, StringComparison.Ordinal)
|| !catalog.Subjects.TryGetValue(lesson.Subject, out var subject))
{
return;
}
if (!TeacherStandingIn(places, lesson.TeacherId, lesson.RoomId))
{
school.TryLogLessonOnce(personId, PersonLogTypes.LessonNoTeacher, lesson.Subject);
return;
}
var hunger = needs.Values.GetValueOrDefault("Hunger", 1f);
foreach (var share in subject.Skills)
{
@@ -87,6 +106,17 @@ internal static class LessonLearningSystem
});
}
private static bool IsStanding(Presence presence) =>
presence.NodeId is not null && presence.Path.Length == 0 && presence.RemainingMinutes <= 0;
private static bool TeacherStandingIn(
Dictionary<string, Presence> places,
string teacherId,
string roomId) =>
places.TryGetValue(teacherId, out var teacher) && IsStanding(teacher)
&& teacher.NodeId is not null
&& teacher.NodeId.Equals(roomId, StringComparison.Ordinal);
private static LessonPlacement? CurrentLesson(School school, Person person, int weekday, int period)
{
foreach (var lesson in Duty.LessonsToday(person, ClassOf(school, person), school.Timetable, weekday))
+9
View File
@@ -14,6 +14,7 @@ public static class PersonLogTypes
public const string ActionEnded = "action-ended";
public const string ApparelReplaced = "apparel-replaced";
public const string ApparelChanged = "apparel-changed";
public const string LessonNoTeacher = "lesson-no-teacher";
}
/// <summary>
@@ -58,6 +59,14 @@ public sealed record PersonLogEvent(string PersonId, DateTime Time, string Type,
return string.Format(CultureInfo.InvariantCulture, catalog.Text(locale, key), name);
}
if (Type.Equals(PersonLogTypes.LessonNoTeacher, StringComparison.Ordinal))
{
var name = catalog.Subjects.TryGetValue(ThingDef, out var subject)
? catalog.Label(locale, subject)
: ThingDef;
return string.Format(CultureInfo.InvariantCulture, catalog.Text(locale, "LessonNoTeacher"), name);
}
return Type;
}
}
+17
View File
@@ -17,6 +17,7 @@ public sealed class School : IDisposable
private bool _disposed;
private readonly List<PersonLogEvent> _dayLog = [];
private readonly HashSet<string> _lessonLogOnce = new(StringComparer.Ordinal);
internal School(int id, string name, DateTime startDate, DefCatalog? catalog, MapLayout? map)
{
@@ -135,10 +136,26 @@ public sealed class School : IDisposable
{
_dayLog.Clear();
LoggedActivity.Clear();
_lessonLogOnce.Clear();
}
internal void AppendDayLog(PersonLogEvent row) => _dayLog.Add(row);
/// <summary>
/// Lesson-quality rows fire every tick the condition holds. One key per person per day is enough.
/// </summary>
internal bool TryLogLessonOnce(string personId, string type, string subject)
{
var key = string.Concat(personId, "\0", type, "\0", subject);
if (!_lessonLogOnce.Add(key))
{
return false;
}
AppendDayLog(new PersonLogEvent(personId, Clock.Time, type, subject));
return true;
}
public void QueueDecision(string personId)
{
ObjectDisposedException.ThrowIf(_disposed, this);