summaryrefslogtreecommitdiff
path: root/src/LivingVillage.Kernel
diff options
context:
space:
mode:
authorSomhairle H. Marisol <[email protected]>2026-09-20 10:50:44 +0800
committerSomhairle H. Marisol <[email protected]>2026-09-20 10:50:44 +0800
commit2f8ce45de7e4d9fb1aef1e35ef9d025b72f07b58 (patch)
treea9a17df7dccce5c3f38b9ef104dd5bf4f2898257 /src/LivingVillage.Kernel
parenta7ab5790d2d48be7b690dc099e4830b881e55401 (diff)
downloadliving-village-2f8ce45de7e4d9fb1aef1e35ef9d025b72f07b58.tar.gz
fix(m3): 保留关系记忆并避免立即重复聊天
[问题原因] - 常规记忆写入会挤出 Chatted 事件,导致关系权重失去长期输入。 - NPC 聊天选伴没有排除最近对象,可能立即重复同一聊天伙伴。 [问题根因] - 记忆列表只使用单一容量截断策略,没有区分关系记忆和常规记忆。 - 聊天伙伴搜索只按距离选择,没有读取最近聊天记忆。 [修复方案] - 为关系记忆保留容量,并继续限制总记忆容量。 - 优先选择非最近聊天伙伴;无替代对象时回退到原有选择逻辑。 - 增加记忆保留和重复聊天回归测试。 [影响范围] - 修改 LivingVillage.Kernel 的 NPC 记忆与聊天伙伴选择。 - 不改变 headless 参数或关系矩阵接口;现有构建与测试流程保持兼容。
Diffstat (limited to 'src/LivingVillage.Kernel')
-rw-r--r--src/LivingVillage.Kernel/Sim.fs56
1 files changed, 41 insertions, 15 deletions
diff --git a/src/LivingVillage.Kernel/Sim.fs b/src/LivingVillage.Kernel/Sim.fs
index 33f2b57..ba3fd37 100644
--- a/src/LivingVillage.Kernel/Sim.fs
+++ b/src/LivingVillage.Kernel/Sim.fs
@@ -161,8 +161,24 @@ module Sim =
| Chatted _ -> chatValence
let recordMemory (tick: int64) (kind: MemoryKind) (mem: MemoryEvent list) : MemoryEvent list =
- { Tick = tick; Kind = kind; Valence = valenceOfKind kind } :: mem
- |> List.truncate memoryCapacity
+ let updated = { Tick = tick; Kind = kind; Valence = valenceOfKind kind } :: mem
+ let isRelational (event: MemoryEvent) =
+ match event.Kind with
+ | Chatted _ -> true
+ | _ -> false
+ let relationalCount = updated |> List.sumBy (fun event -> if isRelational event then 1 else 0)
+ let relationalCapacity = min memoryCapacity relationalCount
+ let routineCapacity = memoryCapacity - relationalCapacity
+ let rec retain relationalLeft routineLeft remaining kept =
+ match remaining with
+ | [] -> List.rev kept
+ | event :: tail when isRelational event && relationalLeft > 0 ->
+ retain (relationalLeft - 1) routineLeft tail (event :: kept)
+ | event :: tail when not (isRelational event) && routineLeft > 0 ->
+ retain relationalLeft (routineLeft - 1) tail (event :: kept)
+ | _ :: tail -> retain relationalLeft routineLeft tail kept
+ // Relation weights are derived from this snapshot, so interaction memories must outlive routine churn.
+ retain relationalCapacity routineCapacity updated []
let actionMemoryKind (kind: NpcActionKind) : MemoryKind option =
match kind with
@@ -284,18 +300,28 @@ module Sim =
let chatableForChat (n: Npc) : bool =
n.Mind.Action <> Sleep && n.Mind.Action <> Chat
- let findChatPartner (self: NpcId) (pos: Vec2) (npcs: Npc[]) : NpcId option =
- let mutable best = None
- let mutable bestD2 = chatRangeSq
- for o in npcs do
- if o.Id <> self && chatableForChat o then
- let dx = o.Pos.X - pos.X
- let dy = o.Pos.Y - pos.Y
- let d2 = dx * dx + dy * dy
- if d2 < bestD2 then
- bestD2 <- d2
- best <- Some o.Id
- best
+ let findChatPartner (self: Npc) (pos: Vec2) (npcs: Npc[]) : NpcId option =
+ let lastChatPartner =
+ self.Mind.Memory
+ |> List.tryPick (fun event ->
+ match event.Kind with
+ | Chatted partner -> Some partner
+ | _ -> None)
+ let pick (allow: Npc -> bool) =
+ let mutable best = None
+ let mutable bestD2 = chatRangeSq
+ for o in npcs do
+ if o.Id <> self.Id && chatableForChat o && allow o then
+ let dx = o.Pos.X - pos.X
+ let dy = o.Pos.Y - pos.Y
+ let d2 = dx * dx + dy * dy
+ if d2 < bestD2 then
+ bestD2 <- d2
+ best <- Some o.Id
+ best
+ match pick (fun o -> Some o.Id <> lastChatPartner) with
+ | Some partner -> Some partner
+ | None -> pick (fun _ -> true)
let private stepNpc (tick: int64) (npcs: Npc[]) (pending: ResizeArray<InteractionEvent>) (npc: Npc) : Npc =
let decayed =
@@ -321,7 +347,7 @@ module Sim =
let switchTo (resetOnSame: bool) (mind: Mind) (needsNow: Needs) (pos: Vec2) : Npc =
let action = decideAction night needsNow mind.Personality
if action = Wander then
- match findChatPartner npc.Id pos npcs with
+ match findChatPartner npc pos npcs with
| Some partner ->
pending.Add { Tick = tick; Kind = ChatInit(npc.Id, partner) }
{ npc with Pos = pos; Mind = mind }