summaryrefslogtreecommitdiff
path: root/src/LivingVillage.Kernel/Sim.fs
blob: 62ce1e11cc036d6271c5214a9a1f07aead14d5cb (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
namespace LivingVillage.Kernel

module Sim =

    [<Struct>]
    type Vec2 =
        { X: float32
          Y: float32 }

    [<Struct>]
    type Avatar =
        { Pos: Vec2 }

    type Input =
        { MoveX: float32
          MoveY: float32 }

    type TimeStep =
        { Input: Input }

    [<Struct>]
    type NoHost =
        { Reserved: uint64 }

    [<Struct>]
    type NpcId =
        | NpcId of int

    [<Struct>]
    type Needs =
        { Hunger: float32
          Energy: float32
          Social: float32
          Money: float32 }

    [<Struct>]
    type Personality =
        { Drive: float32
          Aggression: float32
          Extraversion: float32
          Honesty: float32
          Greed: float32 }

    type NpcActionKind =
        | Eat
        | Sleep
        | Wander
        | Work
        | Chat

    type MemoryKind =
        | Meal
        | Rest
        | Pay
        | Hungry
        | Chatted of NpcId

    type MemoryEvent =
        { Tick: int64
          Kind: MemoryKind
          Valence: float32 }

    type InteractionKind =
        | ChatInit of NpcId * NpcId

    type InteractionEvent =
        { Tick: int64
          Kind: InteractionKind }

    [<Struct>]
    type Mind =
        { Needs: Needs
          Personality: Personality
          Action: NpcActionKind
          Target: Vec2
          ActionAge: int64
          EffectDone: bool
          HungerFlagged: bool
          Memory: MemoryEvent list }

    [<Struct>]
    type Npc =
        { Id: NpcId
          Pos: Vec2
          Mind: Mind }

    [<Struct>]
    type World =
        { Tick: int64
          Time: float
          Rng: RngState
          Avatar: Avatar
          NoHost: NoHost
          Npcs: Npc[]
          Events: InteractionEvent list }

    let ticksPerSecond = 60L
    let secondsPerDay = 86400L
    let ticksPerDay = secondsPerDay * ticksPerSecond
    let dtSeconds = 1.0 / float ticksPerSecond
    let dtSecondsF = 1.0f / float32 ticksPerSecond

    let mapWidthTiles = 64
    let mapHeightTiles = 48
    let tilePixels = 32
    let avatarSpeed = 160.0f

    let npcSpeed = 80.0f
    let arriveEpsilon = 2.0f

    let memoryCapacity = 64
    let minActionTicks = 600L
    let urgentThreshold = 20.0f
    let nightSleepMultiplier = 3.0f
    let npcCount = 30
    let chatRangePx = 96.0f
    let chatRangeSq = chatRangePx * chatRangePx
    let chatTicks = 300L
    let chatSocialRestore = 40.0f
    let chatValence = 0.3f

    let clamp (v: float32) (lo: float32) (hi: float32) : float32 = if v < lo then lo elif v > hi then hi else v

    let hungerDecayPerTick = 0.0012f
    let energyDecayPerTick = 0.0010f
    let socialDecayPerTick = 0.0008f
    let moneyDecayPerTick = 0.0006f

    let private tileCenter (tx: int) (ty: int) : Vec2 =
        { X = float32 (tx * tilePixels + tilePixels / 2)
          Y = float32 (ty * tilePixels + tilePixels / 2) }

    let kitchenPoint = tileCenter 10 10
    let homePoint = tileCenter 52 10
    let plazaPoint = tileCenter 32 24
    let worksitePoint = tileCenter 52 38

    let actionTarget (kind: NpcActionKind) : Vec2 =
        match kind with
        | Eat -> kitchenPoint
        | Sleep -> homePoint
        | Wander -> plazaPoint
        | Work -> worksitePoint
        | Chat -> plazaPoint

    let isNightTick (tick: int64) : bool =
        let hourTicks = ticksPerDay / 24L
        let tod = tick % ticksPerDay
        tod >= 22L * hourTicks || tod < 6L * hourTicks

    let valenceOfKind (kind: MemoryKind) : float32 =
        match kind with
        | Meal -> 0.5f
        | Rest -> 0.4f
        | Pay -> 0.2f
        | Hungry -> -0.4f
        | 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 actionMemoryKind (kind: NpcActionKind) : MemoryKind option =
        match kind with
        | Eat -> Some Meal
        | Sleep -> Some Rest
        | Work -> Some Pay
        | Wander -> None
        | Chat -> None

    let personalityOfSeed (seed: uint64) : Personality =
        let a, r1 = Rng.nextFloat32 (Rng.ofSeed seed)
        let b, r2 = Rng.nextFloat32 r1
        let c, r3 = Rng.nextFloat32 r2
        let d, r4 = Rng.nextFloat32 r3
        let e, _ = Rng.nextFloat32 r4
        { Drive = a
          Aggression = b
          Extraversion = c
          Honesty = d
          Greed = e }

    let needsClamp (n: Needs) : Needs =
        { Hunger = clamp n.Hunger 0.0f 100.0f
          Energy = clamp n.Energy 0.0f 100.0f
          Social = clamp n.Social 0.0f 100.0f
          Money = clamp n.Money 0.0f 100.0f }

    let scoreAction (night: bool) (needs: Needs) (p: Personality) (kind: NpcActionKind) : float32 =
        let raw =
            match kind with
            | Eat -> (100.0f - needs.Hunger) * (0.25f + 2.0f * p.Drive)
            | Sleep -> (100.0f - needs.Energy) * (0.25f + 2.0f * (1.0f - p.Drive))
            | Wander -> (100.0f - needs.Social) * (0.25f + 2.0f * p.Extraversion)
            | Work -> (100.0f - needs.Money) * (0.25f + 2.0f * p.Greed)
            | Chat -> 0.0f
        if kind = Sleep && night then raw * nightSleepMultiplier else raw

    let decideAction (night: bool) (needs: Needs) (p: Personality) : NpcActionKind =
        let candidates = [ Eat; Sleep; Wander; Work ]
        candidates |> List.maxBy (scoreAction night needs p)

    let needsUrgent (n: Needs) : bool =
        n.Hunger < urgentThreshold
        || n.Energy < urgentThreshold
        || n.Social < urgentThreshold
        || n.Money < urgentThreshold

    let applyActionEffect (kind: NpcActionKind) (n: Needs) : Needs =
        match kind with
        | Eat -> { n with Hunger = n.Hunger + 40.0f; Money = n.Money - 5.0f }
        | Sleep -> { n with Energy = n.Energy + 60.0f }
        | Wander -> { n with Social = n.Social + 15.0f; Hunger = n.Hunger - 2.0f }
        | Work -> { n with Money = n.Money + 20.0f; Energy = n.Energy - 10.0f }
        | Chat -> { n with Social = n.Social + chatSocialRestore }
        |> needsClamp

    let private moveToward (pos: Vec2) (target: Vec2) (maxStep: float32) : struct (Vec2 * bool) =
        let dx = target.X - pos.X
        let dy = target.Y - pos.Y
        let len = sqrt (dx * dx + dy * dy)
        if len <= maxStep + arriveEpsilon then struct (target, true)
        else
            let inv = maxStep / len
            struct ({ X = pos.X + dx * inv; Y = pos.Y + dy * inv }, false)

    let initialWorld (seed: uint64) : World =
        let centerX = float32 (mapWidthTiles * tilePixels / 2 - tilePixels / 2)
        let centerY = float32 (mapHeightTiles * tilePixels / 2 - tilePixels / 2)
        let personality = personalityOfSeed seed
        let needs = { Hunger = 100.0f; Energy = 100.0f; Social = 100.0f; Money = 50.0f }
        let startAction = decideAction (isNightTick 0L) needs personality
        let npc =
            { Id = NpcId 0
              Pos = { X = centerX; Y = centerY }
              Mind =
                { Needs = needs
                  Personality = personality
                  Action = startAction
                  Target = actionTarget startAction
                  ActionAge = 0L
                  EffectDone = false
                  HungerFlagged = false
                  Memory = [] } }
        { Tick = 0L
          Time = 0.0
          Rng = Rng.ofSeed seed
          Avatar = { Pos = { X = centerX; Y = centerY } }
          NoHost = { Reserved = 0UL }
          Npcs = [| npc |]
          Events = [] }

    let initialWorldN (seed: uint64) (count: int) : World =
        let base_ = initialWorld seed
        let cols = 6
        let spacing = 64.0f
        let needs = { Hunger = 100.0f; Energy = 100.0f; Social = 100.0f; Money = 50.0f }
        let npcs =
            Array.init count (fun i ->
                let col = i % cols
                let row = i / cols
                let pos =
                    { X = plazaPoint.X + (float32 col - 2.5f) * spacing
                      Y = plazaPoint.Y + (float32 row - 2.0f) * spacing }
                let personality = personalityOfSeed (seed + uint64 i * 0x9E3779B97F4A7C15UL)
                let startAction = decideAction (isNightTick 0L) needs personality
                { Id = NpcId i
                  Pos = pos
                  Mind =
                    { Needs = needs
                      Personality = personality
                      Action = startAction
                      Target = actionTarget startAction
                      ActionAge = 0L
                      EffectDone = false
                      HungerFlagged = false
                      Memory = [] } })
        { base_ with Npcs = npcs }

    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 private stepNpc (tick: int64) (npcs: Npc[]) (pending: ResizeArray<InteractionEvent>) (npc: Npc) : Npc =
        let decayed =
            needsClamp
                { Hunger = npc.Mind.Needs.Hunger - hungerDecayPerTick
                  Energy = npc.Mind.Needs.Energy - energyDecayPerTick
                  Social = npc.Mind.Needs.Social - socialDecayPerTick
                  Money = npc.Mind.Needs.Money - moneyDecayPerTick }
        let night = isNightTick tick
        let age = npc.Mind.ActionAge + 1L
        let urgent = needsUrgent decayed
        let maxStep = npcSpeed * dtSecondsF
        let mind0 =
            { npc.Mind with
                Needs = decayed
                ActionAge = age
                HungerFlagged =
                    if decayed.Hunger < urgentThreshold then npc.Mind.HungerFlagged else false }
        let mind1 =
            if decayed.Hunger < urgentThreshold && not npc.Mind.HungerFlagged then
                { mind0 with Memory = recordMemory tick Hungry mind0.Memory; HungerFlagged = true }
            else mind0
        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
                | Some partner ->
                    pending.Add { Tick = tick; Kind = ChatInit(npc.Id, partner) }
                    { npc with Pos = pos; Mind = mind }
                | None ->
                    if action = mind.Action then
                        if resetOnSame then
                            { npc with Pos = pos; Mind = { mind with ActionAge = 0L; EffectDone = false } }
                        else { npc with Pos = pos; Mind = mind }
                    else
                        { npc with
                            Pos = pos
                            Mind = { mind with Action = action; Target = actionTarget action; ActionAge = 0L; EffectDone = false } }
            elif action = mind.Action then
                if resetOnSame then
                    { npc with Pos = pos; Mind = { mind with ActionAge = 0L; EffectDone = false } }
                else { npc with Pos = pos; Mind = mind }
            else
                { npc with
                    Pos = pos
                    Mind = { mind with Action = action; Target = actionTarget action; ActionAge = 0L; EffectDone = false } }
        if mind1.Action = Chat then
            let struct (nextPos, _) = moveToward npc.Pos mind1.Target maxStep
            if age >= chatTicks then
                let needs2 = applyActionEffect Chat mind1.Needs
                switchTo true { mind1 with Needs = needs2 } needs2 nextPos
            else
                { npc with Pos = nextPos; Mind = mind1 }
        else
            let target = actionTarget npc.Mind.Action
            let struct (nextPos, arrived) = moveToward npc.Pos target maxStep
            if arrived then
                let needs2, mem2 =
                    if mind1.EffectDone then mind1.Needs, mind1.Memory
                    else
                        let mem =
                            match actionMemoryKind mind1.Action with
                            | Some k -> recordMemory tick k mind1.Memory
                            | None -> mind1.Memory
                        applyActionEffect mind1.Action mind1.Needs, mem
                let mind2 = { mind1 with Needs = needs2; Memory = mem2; EffectDone = true }
                if age >= minActionTicks || urgent then
                    switchTo true mind2 needs2 nextPos
                else
                    { npc with Pos = nextPos; Mind = mind2 }
            elif urgent && age < minActionTicks then
                switchTo false mind1 decayed nextPos
            else
                { npc with Pos = nextPos; Mind = mind1 }

    let private applyChatInit (tick: int64) (a: NpcId) (b: NpcId) (npcs: Npc[]) : Npc[] =
        if a = b then npcs
        else
            let mutable ia = -1
            let mutable ib = -1
            for i in 0 .. npcs.Length - 1 do
                let id = npcs.[i].Id
                if id = a then ia <- i elif id = b then ib <- i
            if ia < 0 || ib < 0 then npcs
            else
                let na = npcs.[ia]
                let nb = npcs.[ib]
                if chatableForChat na && chatableForChat nb && ia <> ib then
                    npcs.[ia] <-
                        { na with
                            Mind =
                                { na.Mind with
                                    Action = Chat
                                    Target = nb.Pos
                                    ActionAge = 0L
                                    EffectDone = false
                                    Memory = recordMemory tick (Chatted b) na.Mind.Memory } }
                    npcs.[ib] <-
                        { nb with
                            Mind =
                                { nb.Mind with
                                    Action = Chat
                                    Target = na.Pos
                                    ActionAge = 0L
                                    EffectDone = false
                                    Memory = recordMemory tick (Chatted a) nb.Mind.Memory } }
                    npcs
                else npcs

    let step (ts: TimeStep) (world: World) : World =
        let tick = world.Tick + 1L
        let maxX = float32 (mapWidthTiles * tilePixels - tilePixels)
        let maxY = float32 (mapHeightTiles * tilePixels - tilePixels)
        let dx = ts.Input.MoveX * avatarSpeed * dtSecondsF
        let dy = ts.Input.MoveY * avatarSpeed * dtSecondsF
        let rngOut, rngNext = Rng.nextUInt64 world.Rng
        ignore rngOut
        let pending = ResizeArray<InteractionEvent> ()
        let oldNpcs = world.Npcs
        let newNpcs = Array.copy oldNpcs
        for i in 0 .. newNpcs.Length - 1 do
            newNpcs.[i] <- stepNpc tick oldNpcs pending newNpcs.[i]
        let queue = if pending.Count = 0 then world.Events else world.Events @ List.ofSeq pending
        if queue.Length > 0 then
            for ev in queue do
                match ev.Kind with
                | ChatInit (a, b) -> applyChatInit ev.Tick a b newNpcs |> ignore
        { Tick = tick
          Time = float tick * dtSeconds
          Rng = rngNext
          Avatar =
            { Pos =
                { X = clamp (world.Avatar.Pos.X + dx) 0.0f maxX
                  Y = clamp (world.Avatar.Pos.Y + dy) 0.0f maxY } }
          NoHost = world.NoHost
          Npcs = newNpcs
          Events = [] }