1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
namespace LivingVillage.Kernel.Tests
open System
open System.IO
open Microsoft.VisualStudio.TestTools.UnitTesting
open LivingVillage.Kernel
open LivingVillage.Kernel.Sim
module private M6aHarness =
let zeroInput = { Input = { MoveX = 0.0f; MoveY = 0.0f } }
let atNpc (id: int) (world: World) : World =
let npc = world.Npcs.[id]
{ world with Avatar = { world.Avatar with Pos = npc.Pos } }
let readyForChat (world: World) : World =
{ world with
Npcs =
world.Npcs
|> Array.map (fun npc ->
{ npc with
Mind =
{ npc.Mind with
Action = Wander
Target = actionTarget Wander
ActionAge = minActionTicks
EffectDone = true } }) }
let withDialogue (world: World) : World =
match Sim.chooseDialogue (NpcId 0) SmallTalk (atNpc 0 world) with
| DialogueSucceeded (_, next) -> next
| DialogueRejected (failure, _) -> Assert.Fail($"dialogue rejected: {failure}"); Unchecked.defaultof<World>
let withChat (world: World) : World =
match Sim.chat { Narrator = NpcId 0; Receiver = NpcId 1 } (readyForChat world) with
| ChatSucceeded (Some _, next) -> next
| ChatSucceeded (None, _) -> Assert.Fail("chat should create a rumor"); Unchecked.defaultof<World>
| ChatRejected (failure, _) -> Assert.Fail($"chat rejected: {failure}"); Unchecked.defaultof<World>
let withTrade (world: World) : World =
match Sim.trade { Buyer = NpcId 0; Seller = NpcId 1; Item = Food; Quantity = 1 } world with
| TradeSucceeded next -> next
| TradeRejected (failure, _) -> Assert.Fail($"trade rejected: {failure}"); Unchecked.defaultof<World>
let advance (inputs: Input list) (world: World) : World =
inputs |> List.fold (fun current input -> Sim.step { Input = input } current) world
let advanceTrace (inputs: Input list) (world: World) : World list =
inputs
|> List.scan (fun current input -> Sim.step { Input = input } current) world
|> List.tail
let withTempSave (action: string -> unit) =
let id = Guid.NewGuid().ToString("N")
let path = Path.Combine(Path.GetTempPath(), $"living-village-m6a-{id}.save")
try
action path
finally
if File.Exists path then File.Delete path
[<TestClass>]
type M6aTests () =
[<TestMethod>]
member _.SaveLoadPreservesWorldAndDeterministicContinuation () =
let world =
Sim.initialWorldN 7UL 4
|> M6aHarness.withDialogue
|> M6aHarness.withChat
|> M6aHarness.withTrade
|> fun current ->
let tick = 22L * (Sim.ticksPerDay / 24L)
{ current with Tick = tick; Time = float tick * Sim.dtSeconds }
M6aHarness.withTempSave (fun path ->
WorldSave.saveToFile path world
match WorldSave.loadFromFile path with
| Error failure -> Assert.Fail($"save should load: {failure}")
| Ok restored ->
Assert.AreEqual<string>(WorldSave.save world, WorldSave.save restored)
Assert.AreEqual<float>(world.Time, restored.Time)
Assert.IsTrue(Sim.isNightTick restored.Tick)
Assert.AreEqual<Needs>(world.Avatar.Mind.Needs, restored.Avatar.Mind.Needs)
Assert.AreEqual<float32>(world.Avatar.Mind.Needs.Money, restored.Avatar.Mind.Needs.Money)
CollectionAssert.AreEqual(world.Npcs, restored.Npcs)
Assert.AreEqual<Map<ItemKind, int>>(world.Npcs.[0].Inventory, restored.Npcs.[0].Inventory)
Assert.AreEqual<InteractionEvent list>(world.Events, restored.Events)
Assert.AreEqual<RumorEvent list>(world.Rumors, restored.Rumors)
Assert.AreEqual<AnnalEntry list>(world.Annals, restored.Annals)
Assert.AreEqual<uint64>(world.Rng.State, restored.Rng.State)
let inputs =
[ { MoveX = 0.5f; MoveY = -0.25f }
{ MoveX = 0.0f; MoveY = 0.0f }
{ MoveX = -1.0f; MoveY = 0.75f }
{ MoveX = 0.25f; MoveY = 0.0f } ]
let continued = M6aHarness.advanceTrace inputs world
let restoredContinued = M6aHarness.advanceTrace inputs restored
Assert.AreEqual<string list>(continued |> List.map WorldSave.save, restoredContinued |> List.map WorldSave.save)
Assert.AreEqual<uint64 list>(continued |> List.map (fun next -> next.Rng.State), restoredContinued |> List.map (fun next -> next.Rng.State))
Assert.AreEqual<InteractionEvent list list>(continued |> List.map (fun next -> next.Events), restoredContinued |> List.map (fun next -> next.Events))
Assert.AreEqual<string>(WorldSave.save (List.last continued), WorldSave.save (List.last restoredContinued)))
[<TestMethod>]
member _.RejectedFilesDoNotReplaceCurrentWorld () =
let currentWorld = Sim.initialWorldN 99UL 2
let before = WorldSave.save currentWorld
let mutable current = currentWorld
let reject path =
match WorldSave.loadFromFile path with
| Ok loaded ->
current <- loaded
Assert.Fail("invalid save must not load")
| Error _ -> ()
Assert.AreEqual<string>(before, WorldSave.save current)
M6aHarness.withTempSave (fun path ->
reject (path + ".missing")
File.WriteAllText(path, "LV_WORLD_SAVE_V0")
reject path
File.WriteAllText(path, WorldSave.save currentWorld + "|trailing-corruption")
reject path)
[<TestMethod>]
member _.CorruptNumericAndInventoryValuesAreRejected () =
let world = Sim.initialWorldN 7UL 2
M6aHarness.withTempSave (fun path ->
let nonFinite = { world with Time = Double.NaN }
WorldSave.saveToFile path nonFinite
match WorldSave.loadFromFile path with
| Ok _ -> Assert.Fail("non-finite time must be rejected")
| Error _ -> ()
let invalidInventory =
{ world with
Npcs =
[| { world.Npcs.[0] with Inventory = Map.ofList [ Food, -1 ] }
world.Npcs.[1] |] }
WorldSave.saveToFile path invalidInventory
match WorldSave.loadFromFile path with
| Ok _ -> Assert.Fail("negative inventory must be rejected")
| Error _ -> ())
[<TestMethod>]
member _.SimulationClockSupportsPauseAndRequestedSpeeds () =
let initial = SimulationControl.initial
Assert.AreEqual<int>(1, SimulationControl.stepsPerFrame initial)
Assert.AreEqual<int>(2, SimulationControl.stepsPerFrame (SimulationControl.setSpeed TwoX initial))
Assert.AreEqual<int>(5, SimulationControl.stepsPerFrame (SimulationControl.setSpeed FiveX initial))
let paused = SimulationControl.togglePause initial
Assert.IsTrue(paused.Paused)
Assert.AreEqual<int>(0, SimulationControl.stepsPerFrame paused)
let resumed = SimulationControl.togglePause paused
Assert.IsFalse(resumed.Paused)
Assert.AreEqual<SimulationSpeed>(OneX, resumed.Speed)
[<TestMethod>]
member _.ConfiguredLegacyStepCountsRemainExactUntilManualSpeedSelection () =
let control, legacySteps = SimulationControl.initialForConfiguredSteps 7
Assert.AreEqual<int option>(Some 7, legacySteps)
Assert.AreEqual<int>(7, SimulationControl.stepsPerFrameWithLegacy legacySteps control)
Assert.AreEqual<int>(0, SimulationControl.stepsPerFrameWithLegacy legacySteps (SimulationControl.togglePause control))
let manuallySelected = SimulationControl.setSpeed TwoX control
Assert.AreEqual<int>(2, SimulationControl.stepsPerFrameWithLegacy None manuallySelected)
|