summaryrefslogtreecommitdiff
path: root/src/LivingVillage.Kernel/WorldSave.fs
diff options
context:
space:
mode:
Diffstat (limited to 'src/LivingVillage.Kernel/WorldSave.fs')
-rw-r--r--src/LivingVillage.Kernel/WorldSave.fs46
1 files changed, 39 insertions, 7 deletions
diff --git a/src/LivingVillage.Kernel/WorldSave.fs b/src/LivingVillage.Kernel/WorldSave.fs
index 7f724a3..3e11810 100644
--- a/src/LivingVillage.Kernel/WorldSave.fs
+++ b/src/LivingVillage.Kernel/WorldSave.fs
@@ -494,6 +494,24 @@ module WorldSave =
DueTick = dueTick
State = state }
+ /// v3 backpack 尾段(design §2.4-1):玩家背包侧车,按声明顺序写入;旧 v3(无此段)读回
+ /// 由 kind 的 `profileOf` 默认值补齐。
+ let private writeBackpack tokens (items: (ItemKind * int) list) =
+ add tokens "backpack"
+ addInt tokens items.Length
+ for item, quantity in items do
+ writeItemKind tokens item
+ addInt tokens quantity
+
+ let private readBackpack (reader: TokenReader) : (ItemKind * int) list =
+ let count = readCount reader "backpack.count"
+ [ for index in 0 .. count - 1 do
+ let item = readItemKind reader (sprintf "backpack[%d].item" index)
+ let quantity = readInt reader (sprintf "backpack[%d].quantity" index)
+ if quantity < 0 then
+ invalid (sprintf "invalid backpack[%d].quantity" index)
+ yield item, quantity ]
+
let private writeMind tokens (value: Mind) =
writeNeeds tokens value.Needs
writePersonality tokens value.Personality
@@ -598,6 +616,7 @@ module WorldSave =
match state.Today with
| Some task -> writeDailyTask tokens task
| None -> ()
+ writeBackpack tokens state.Backpack
| None -> ()
String.Join("|", tokens)
@@ -634,7 +653,14 @@ module WorldSave =
| "peddler" -> Some Occupation.Peddler
| "scholar" -> Some Occupation.Scholar
| _ -> None
- occupationOption <- kind |> Option.map (fun kind -> { Profile = Occupation.profileOf kind; TaskToken = task; Today = None; StoryStage = stage })
+ occupationOption <-
+ kind
+ |> Option.map (fun kind ->
+ { Profile = Occupation.profileOf kind
+ TaskToken = task
+ Today = None
+ StoryStage = stage
+ Backpack = (Occupation.profileOf kind).InitialInventory })
| _ -> ()
let tick = readInt64 reader "tick"
let time = readFloat reader "time"
@@ -650,17 +676,23 @@ module WorldSave =
let rumorStats = rumorWorkingSetStats rumors
let annalCount = readCount reader "annals"
let annals = List.init annalCount (fun index -> readAnnal reader (sprintf "annal[%d]" index))
- let todayTask =
- if reader.Remaining > 0 then
- match reader.Take("task.today") with
- | "task-today" -> Some(readDailyTask reader)
+ let tail =
+ let mutable task : Occupation.DailyTask option = None
+ let mutable backpack : (ItemKind * int) list option = None
+ while reader.Remaining > 0 do
+ match reader.Take("occupation.tail") with
+ | "task-today" -> task <- Some(readDailyTask reader)
+ | "backpack" -> backpack <- Some(readBackpack reader)
| _ -> invalid "trailing save data"
- else None
+ task, backpack
if reader.Remaining <> 0 then
invalid "trailing save data"
let occupation =
occupationOption
- |> Option.map (fun state -> { state with Today = todayTask })
+ |> Option.map (fun state ->
+ { state with
+ Today = fst tail
+ Backpack = snd tail |> Option.defaultValue state.Backpack })
Ok
({ Tick = tick
Time = time