diff options
| author | Somhairle H. Marisol <[email protected]> | 2026-09-20 14:20:54 +0800 |
|---|---|---|
| committer | Somhairle H. Marisol <[email protected]> | 2026-09-20 14:20:54 +0800 |
| commit | 7e197babfbb68d7f7d1866d6a0ca47cf17d3af23 (patch) | |
| tree | abf951825edce69139f099a73dfb6f2575ff0259 /src/LivingVillage.Desktop/Interaction.fs | |
| parent | 4f8aca922e6cf47dce95aab79541065d487d5272 (diff) | |
| download | living-village-7e197babfbb68d7f7d1866d6a0ca47cf17d3af23.tar.gz | |
feat(m5): 增加可交互对话、需求观察与编年史
[变更性质]
- 本提交新增 M5 用户能力,不是缺陷修复。
[新增功能]
- 增加玩家六意图对话、需求面板、可见 NPC 观察、编年史与因果谣言冒烟验收。
[实现方案]
- 在 Kernel 中扩展 Avatar Mind、对话结果、观察数据、Annals 和确定性谣言链。
- 在 Desktop 中加入键盘交互、像素文本面板和运行时状态展示,并补充 Headless 与测试覆盖。
[影响范围]
- 覆盖 Kernel、Headless、Desktop 及测试工程;保留既有 M3/M4 确定性与交易、关系、谣言行为。
Diffstat (limited to 'src/LivingVillage.Desktop/Interaction.fs')
| -rw-r--r-- | src/LivingVillage.Desktop/Interaction.fs | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/src/LivingVillage.Desktop/Interaction.fs b/src/LivingVillage.Desktop/Interaction.fs new file mode 100644 index 0000000..6f175d6 --- /dev/null +++ b/src/LivingVillage.Desktop/Interaction.fs @@ -0,0 +1,211 @@ +namespace LivingVillage.Desktop + +open System +open LivingVillage.Kernel +open LivingVillage.Kernel.Sim + +type M5Panel = + | WorldPanel + | NeedsPanel + | ObservationPanel + | DialoguePanel + | ChroniclePanel + +type M5Command = + | Interact + | Intent1 + | Intent2 + | Intent3 + | Intent4 + | Intent5 + | Intent6 + | Observe + | ToggleNeeds + | ShowChronicle + | ClosePanel + +type M5View = + { Panel: M5Panel + Menu: DialogueMenu option + Needs: NeedsPanel option + Observations: Observation list + Chronicle: string + Status: string } + +module M5Interaction = + + let initial : M5View = + { Panel = WorldPanel + Menu = None + Needs = None + Observations = [] + Chronicle = "" + Status = "ready" } + + let private observationBounds (world: World) : VisibleBounds = + let radius = Sim.chatRangePx + { Min = { X = world.Avatar.Pos.X - radius; Y = world.Avatar.Pos.Y - radius } + Max = { X = world.Avatar.Pos.X + radius; Y = world.Avatar.Pos.Y + radius } } + + let private menuStatus (menu: DialogueMenu) : string = + sprintf "dialogue target=%A options=%d; press 1-6" menu.Target menu.Options.Length + + let private intentName (intent: DialogueIntent) : string = + match intent with + | SmallTalk -> "SMALLTALK" + | AskHelp -> "ASK HELP" + | OfferTrade -> "OFFER TRADE" + | Joke -> "JOKE" + | Apologize -> "APOLOGIZE" + | Provoke -> "PROVOKE" + + let private needName (kind: NeedKind) : string = + match kind with + | HungerNeed -> "HUNGER" + | EnergyNeed -> "ENERGY" + | SocialNeed -> "SOCIAL" + | MoneyNeed -> "MONEY" + + let private npcNumber (NpcId id) = id + + let private chooseIntent (index: int) (world: World) (view: M5View) : World * M5View = + match view.Menu with + | None -> world, { view with Status = "no dialogue menu; press E near an NPC" } + | Some menu -> + match List.tryItem index menu.Options with + | None -> world, { view with Status = "invalid dialogue option" } + | Some intent -> + match Sim.chooseDialogue menu.Target intent world with + | DialogueSucceeded(outcome, next) -> + next, + { view with + Panel = DialoguePanel + Menu = None + Chronicle = Sim.annalText next + Status = sprintf "dialogue intent=%A response=%A" outcome.Intent outcome.Response } + | DialogueRejected(failure, next) -> + next, { view with Status = sprintf "dialogue rejected: %A" failure } + + let apply (command: M5Command) (world: World) (view: M5View) : World * M5View = + match command with + | Interact -> + match Sim.openDialogueMenu world with + | Some menu -> + world, + { view with + Panel = DialoguePanel + Menu = Some menu + Status = menuStatus menu } + | None -> world, { view with Panel = WorldPanel; Menu = None; Status = "no nearby NPC available" } + | Intent1 -> chooseIntent 0 world view + | Intent2 -> chooseIntent 1 world view + | Intent3 -> chooseIntent 2 world view + | Intent4 -> chooseIntent 3 world view + | Intent5 -> chooseIntent 4 world view + | Intent6 -> chooseIntent 5 world view + | Observe -> + let observations = Sim.observeVisible (observationBounds world) world + world, + { view with + Panel = ObservationPanel + Observations = observations + Status = sprintf "observation visible_npcs=%d" observations.Length } + | ToggleNeeds -> + if view.Panel = NeedsPanel then + world, { view with Panel = WorldPanel; Needs = None; Status = "needs panel closed" } + else + world, + { view with + Panel = NeedsPanel + Needs = Some(Sim.needsPanel world) + Status = "needs panel open" } + | ShowChronicle -> + world, + { view with + Panel = ChroniclePanel + Chronicle = Sim.annalText world + Status = sprintf "chronicle entries=%d" world.Annals.Length } + | ClosePanel -> world, { view with Panel = WorldPanel; Menu = None; Status = "panel closed" } + + let panelName (view: M5View) : string = + match view.Panel with + | WorldPanel -> "world" + | NeedsPanel -> "needs" + | ObservationPanel -> "observation" + | DialoguePanel -> "dialogue" + | ChroniclePanel -> "chronicle" + + let titleText (view: M5View) : string = + match view.Panel with + | WorldPanel -> "E INTERACT | Q OBSERVE | TAB NEEDS | C CHRONICLE | L RELATIONS" + | NeedsPanel -> "NEEDS | TAB CLOSE | Q OBSERVE | C CHRONICLE" + | ObservationPanel -> "OBSERVATION | TAB NEEDS | C CHRONICLE | ESC CLOSE" + | DialoguePanel -> + match view.Menu with + | Some menu -> + let options = + menu.Options + |> List.mapi (fun index intent -> sprintf "%d %s" (index + 1) (intentName intent)) + |> String.concat " | " + sprintf "DIALOGUE TARGET=%A | %s | ESC CLOSE" menu.Target options + | None -> "DIALOGUE RECORDED | E INTERACT | ESC CLOSE" + | ChroniclePanel -> "CHRONICLE | TAB NEEDS | Q OBSERVE | ESC CLOSE" + + let private chronicleLines (text: string) : string list = + text.Split([| '\n' |], System.StringSplitOptions.RemoveEmptyEntries) |> Array.toList + + let panelLines (world: World) (view: M5View) : string list = + let status = if String.IsNullOrWhiteSpace view.Status then "READY" else view.Status.ToUpperInvariant() + match view.Panel with + | WorldPanel -> + [ "M5 VILLAGE" + "E INTERACT" + "Q OBSERVE" + "TAB NEEDS" + "C CHRONICLE" + "L RELATIONS" + status ] + | NeedsPanel -> + match view.Needs with + | None -> [ "NEEDS"; "NO DATA"; status ] + | Some panel -> + let topNeed = + panel.Ranked + |> List.tryHead + |> Option.map (fun score -> sprintf "TOP %s" (needName score.Kind)) + |> Option.defaultValue "TOP NONE" + [ "NEEDS" + sprintf "HUNGER %.0f" panel.Needs.Hunger + sprintf "ENERGY %.0f" panel.Needs.Energy + sprintf "SOCIAL %.0f" panel.Needs.Social + sprintf "MONEY %.0f" panel.Needs.Money + topNeed + status ] + | ObservationPanel -> + let rows = + view.Observations + |> List.truncate 8 + |> List.collect (fun observation -> + let urgency = + observation.Ranked + |> List.tryHead + |> Option.map (fun score -> score.Urgency) + |> Option.defaultValue 0.0f + [ sprintf "NPC %d U %.0f" (npcNumber observation.Id) urgency + sprintf "H %.0f E %.0f S %.0f M %.0f" + observation.Needs.Hunger + observation.Needs.Energy + observation.Needs.Social + observation.Needs.Money ]) + "OBSERVATION" :: (if rows.IsEmpty then [ "NO NPC IN RANGE" ] else rows) @ [ status ] + | DialoguePanel -> + match view.Menu with + | None -> [ "DIALOGUE RECORDED"; status ] + | Some menu -> + let options = + menu.Options + |> List.mapi (fun index intent -> sprintf "%d %s" (index + 1) (intentName intent)) + [ sprintf "DIALOGUE NPC %d" (npcNumber menu.Target) ] @ options @ [ "ESC CLOSE"; status ] + | ChroniclePanel -> + let entries = chronicleLines view.Chronicle |> List.truncate 10 + "CHRONICLE" :: (if entries.IsEmpty then [ "NO ENTRIES" ] else entries) @ [ status ] |
