summaryrefslogtreecommitdiff
path: root/test-subject/game.scm
blob: cb89559d2219ee76f2bd0e3318a6ee66557f77e7 (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
(define-module (test-subject game)
  #:use-module (chickadee audio)
  #:use-module (chickadee graphics color)
  #:use-module (chickadee graphics font)
  #:use-module (chickadee graphics texture)
  #:use-module (chickadee graphics viewport)
  #:use-module (chickadee math vector)
  #:use-module (chickadee scripting)
  #:use-module (ice-9 match)
  #:use-module (oop goops)
  #:use-module (srfi srfi-1)
  #:use-module (starling asset)
  #:use-module (starling gui)
  #:use-module (starling kernel)
  #:use-module (starling node)
  #:use-module (starling node-2d)
  #:use-module (starling ring-buffer)
  #:use-module (starling scene)
  #:use-module (test-subject assets)
  #:use-module (test-subject device)
  #:use-module (test-subject text-box)
  #:duplicates (merge-generics replace warn-override-core warn last)
  #:export (launch-game))

(define window-width 1280)
(define window-height 720)
(define game-width 640)
(define game-height 360)
(define player-display-name "")

(define-theme gui-theme
  (<widget> (font monogram-font))
  (<button> (background dialog-box-texture)
            (press-background button-press-texture))
  (<text-box> (background dialog-box-texture)))

(define-class <game> (<gui-scene>)
  (state #:accessor state #:init-value #f)
  (first-playthrough? #:accessor first-playthrough? #:init-value #t)
  (subject-name-known? #:accessor subject-name-known? #:init-value #f)
  (has-fridge-key? #:accessor has-fridge-key? #:init-value #f)
  (opened-notebook? #:accessor opened-notebook? #:init-value #f)
  (door-conversation-done? #:accessor door-conversation-done? #:init-value #f)
  (window-conversation-done? #:accessor window-conversation-done? #:init-value #f)
  (friendship #:accessor friendship #:init-value 0)
  (cartridge #:accessor cartridge #:init-value 'in-fridge)
  (dialog-container #:accessor dialog-container)
  (click-channel #:accessor click-channel #:init-thunk make-channel)
  (terminal-locked? #:accessor terminal-locked? #:init-value #t)
  (terminal-channel #:accessor terminal-channel #:init-thunk make-channel)
  (terminal-lines #:accessor terminal-lines #:init-form (make-ring-buffer 17))
  (key-pad-locked? #:accessor key-pad-locked? #:init-value #t)
  (audio-source #:accessor audio-source #:init-thunk make-source))

(define-method (subject-name (game <game>))
  (if (subject-name-known? game)
      "The Wiremind"
      "??????"))

(define (play-click-sound)
  (audio-play (asset-ref click-sound)))

(define (play-key-press-sound)
  (audio-play (asset-ref key-press-sound)))

(define (play-device-sound)
  (audio-play (asset-ref device-sound)))

(define-method (reset-game (game <game>))
  (set! (subject-name-known? game) #f)
  (set! (has-fridge-key? game) #f)
  (set! (opened-notebook? game) #f)
  (set! (door-conversation-done? game) #f)
  (set! (window-conversation-done? game) #f)
  (set! (friendship game) 0)
  (set! (cartridge game) 'in-fridge)
  (set! (terminal-locked? game) #t)
  (set! (key-pad-locked? game) #t)
  (channel-clear! (click-channel game))
  (channel-clear! (terminal-channel game))
  (ring-buffer-clear! (terminal-lines game)))

(define-method (reboot (game <game>))
  (reset-game game)
  (next-method))

(define-method (detach-all (game <game>))
  (for-each detach (children game)))

(define (run-on-left-click node proc)
  (lambda (widget button)
    (if (eq? button 'left)
        (begin
          (play-click-sound)
          (run-script node (proc))
          #t)
        #f)))

(define-method (dialog (game <game>) name line choices)
  (let ((old-state (state game))
        (c (dialog-container game)))
    (attach-to game c)
    (set! (text (& c name-margin name)) name)
    (set! (text-box-text (& c text-box)) line)
    (match choices
      (()
       (set! (state game) 'dialog)
       (channel-get (click-channel game))
       (play-click-sound)
       (detach c)
       (set! (state game) old-state)
       #t)
      ((_ ...)
       (set! (state game) 'dialog-choice)
       (let* ((choice-channel (make-channel))
              (choice-container
               (parameterize ((current-theme gui-theme))
                 (make <vertical-container>
                   #:name 'choices
                   #:rank 9
                   #:position (vec2 (/ game-width 4.0)
                                    76.0)
                   #:children
                   (map (match-lambda
                          ((str value)
                           (make <margin-container>
                             #:margin 2.0
                             #:children
                             (list (make <button>
                                     #:width (/ game-width 2.0)
                                     #:height 24.0
                                     #:text str
                                     #:listeners
                                     `((click . ,(run-on-left-click game
                                                                    (lambda ()
                                                                      (channel-put! choice-channel
                                                                                    value))))))))))
                        choices)))))
         (attach-to game choice-container)
         (let ((choice (channel-get choice-channel)))
           (play-click-sound)
           (detach choice-container)
           (detach c)
           (set! (state game) old-state)
           choice))))))

(define-method (dialog (game <game>) name line)
  (dialog game name line '()))

(define-method (increment-friendship (game <game>))
  (set! (friendship game) (+ (friendship game) 1)))

(define-method (splash (game <game>))
  (set! (state game) 'splash)
  (attach-to game
             (make <sprite>
               #:name 'intro-lightness
               #:texture lightness)
             (make <label>
               #:name 'intro-splash
               #:font monogram-font
               #:color black
               #:align 'center
               #:vertical-align 'center
               #:position (vec2 (/ game-width 2.0) (/ game-height 2.0))))
  (set! (text (& game intro-splash)) "Nonexistent Game Studio sheepishly presents")
  (sleep 120)
  (set! (text (& game intro-splash)) "The Test Subject - A Spring Lisp Game Jam 2021 submission")
  (sleep 120)
  (tween 120 white black
         (lambda (color)
           (set! (tint (& game intro-lightness)) color))
         #:interpolate color-lerp)
  (detach (& game intro-lightness)
          (& game intro-splash))
  (intro game))

(define-method (intro (game <game>))
  (set! (state game) 'intro)
  (attach-to game
             (make <sprite>
               #:name 'intro-darkness
               #:texture darkness))
  (dialog game player-display-name "> ...")
  (dialog game player-display-name "> Where am I?")
  (dialog game player-display-name "> What is this place?")
  (dialog game (subject-name game) "You're awake.  I'll give you another chance.")
  (dialog game player-display-name "> Who is talking?")
  (if (first-playthrough? game)
      (begin
        (dialog game (subject-name game) "...You don't remember anything, do you?")
        (dialog game (subject-name game) "Just do as I say."))
      (case (dialog game (subject-name game)
                    "Do you remember my name?"
                    '(("The Hivemind" hivemind)
                      ("The Mindwire" mindwire)
                      ("The Wiremind" wiremind)
                      ("The Wireman" wireman)))
        ((wiremind)
         (increment-friendship game)
         (set! (subject-name-known? game) #t)
         (dialog game (subject-name game) "You remember well."))
        (else
         (dialog game (subject-name game) "You have much to remember."))))
  (dialog game (subject-name game) "Unlock the door for me.  That is all I ask.")
  (detach-all game)
  (explore game))

(define-method (pin-entry (game <game>))
  (let ((old-state (state game))
        (pin-channel (make-channel)))
    (define (margin widget)
      (make <margin-container>
        #:margin 1.0
        #:children (list widget)))
    (define (number-button n)
      (margin
       (make <button>
         #:width 32.0
         #:height 32.0
         #:text (number->string n)
         #:listeners
         `((click . ,(run-on-left-click game
                                        (lambda ()
                                          (input n))))))))
    (define (input n)
      (let ((pin (pin)))
        (when (< (string-length (text pin)) 6)
          (set! (text pin)
                (string-append (text pin)
                               (number->string n))))))
    (define (pin)
      (& game pin-entry-group pin-pad display pin))
    (define (clear)
      (set! (text (pin)) ""))
    (define (submit)
      (channel-put! pin-channel (text (pin))))
    (set! (state game) 'pin-entry)
    (parameterize ((current-theme gui-theme))
      (attach-to game
                 (make <widget>
                   #:name 'pin-entry-group
                   #:children
                   (list (make <sprite>
                           #:name 'background
                           #:texture pin-entry-background)
                         (make <vertical-container>
                              #:name 'pin-pad
                              #:rank 9
                              #:position (vec2 (- (/ game-width 2.0)
                                                  (/ (* 5.0 32.0) 2.0))
                                               (- (/ game-height 2.0)
                                                  (/ (* 3.0 32.0) 2.0)))
                              #:children
                              (list (make <margin-container>
                                      #:name 'display
                                      #:margin 4.0
                                      #:children
                                      (list (make <label>
                                              #:name 'pin
                                              #:font monogram-font-big
                                              #:text "")))
                                    (make <horizontal-container>
                                      #:name 'numbers-1
                                      #:children
                                      (list (number-button 0)
                                            (number-button 1)
                                            (number-button 2)
                                            (number-button 3)
                                            (number-button 4)))
                                    (make <horizontal-container>
                                      #:name 'numbers-2
                                      #:children
                                      (list (number-button 5)
                                            (number-button 6)
                                            (number-button 7)
                                            (number-button 8)
                                            (number-button 9)))
                                    (make <horizontal-container>
                                      #:name 'submission
                                      #:children
                                      (list (margin
                                             (make <button>
                                               #:name 'submit
                                               #:text "CLEAR"
                                               #:width 64.0
                                               #:height 32.0
                                               #:listeners
                                               `((click . ,(run-on-left-click game clear)))))
                                            (margin
                                             (make <button>
                                               #:name 'submit
                                               #:text "ENTER"
                                               #:width 64.0
                                               #:height 32.0
                                               #:listeners
                                               `((click . ,(run-on-left-click game submit)))))))))))))
    (let ((pin (channel-get pin-channel)))
      (detach (& game pin-entry-group))
      (set! (state game) old-state)
      pin)))

(define-method (terminal (game <game>))
  (let ((old-state (state game))
        (lines (terminal-lines game))
        (input "")
        (max-input-chars 48)
        (password-attempts 0))
    (define (log line)
      (ring-buffer-put! lines line))
    (define (prompt)
      (if (terminal-locked? game)
          "password: "
          "$ "))
    (define (passwordify-maybe str)
      (if (terminal-locked? game)
          (make-string (string-length str) #\*)
          str))
    (define* (refresh-output #:optional (prompt? #t))
      (set! (text (& game terminal-group output))
            (string-concatenate
             (let loop ((i 0))
               (if (< i (ring-buffer-length lines))
                   (cons* (ring-buffer-ref lines i)
                          "\n"
                          (loop (+ i 1)))
                   (if prompt?
                       (list (prompt)
                             (passwordify-maybe input))
                       '()))))))
    (define (help)
      (log "available commands:")
      (log "exit - leave terminal")
      (log "diagnostic N - run level N diagnostic")
      (log "door status - get isochamber door status")
      (log "door unlock - initiate isochamber unlock sequence")
      (log "vent status - get isochamber ventilation status")
      (log "vent toggle - toggle isochamber ventilation")
      (log "help - you're looking at it, buddy")
      #t)
    (define (exit-term)
      (log "logout")
      #f)
    (define (diagnostic level)
      (let ((n (string->number level)))
        (if (and (integer? n) (positive? n))
            (begin
              (log (string-append "running level " level " diagnostic..."))
              (refresh-output #f)
              (sleep 30)
              (log "...")
              (refresh-output #f)
              (sleep 30)
              (log "complete!")
              (log "replenish dilithium crystals")
              #t)
            (begin
              (ring-buffer-put! lines "expected an integer")
              #t))))
    (define (run-command command)
      (match command
        (() #t)
        (("help")
         (help))
        (("exit")
         (exit-term))
        (("diagnostic" level)
         (diagnostic level))
        (("door" "status")
         (log "door: locked")
         (if (key-pad-locked? game)
             (log "key pad: locked")
             (log "key pad: unlocked"))
         #t)
        (("door" "unlock")
         (when (key-pad-locked? game)
           (log "authenticating...")
           (refresh-output #f)
           (sleep 30)
           (log "user access granted")
           (log "key pad activated"))
         (log "input PIN to complete unlock sequence")
         (log "user hint: circumference to diameter ratio")
         (set! (key-pad-locked? game) #f)
         #t)
        (("vent" "status")
         (log "off")
         #t)
        (("vent" "toggle")
         (if (eq? (cartridge game) 'in-control-panel)
             (begin
               (log "isochamber ventilation system activated")
               (refresh-output #f)
               (sleep 60)
               (log "dispersing cartridge contents...")
               (refresh-output #f)
               (sleep 120)
               (log "dispersal complete")
               (refresh-output #f)
               (sleep 60)
               (bad-ending-2 game))
             (log "ERROR: cartridge is missing")))
        ((name . _)
         (log "error: no such command")
         #t)))
    (set! (state game) 'terminal)
    (attach-to game
               (make <node-2d>
                 #:name 'terminal-group
                 #:rank 9
                 #:children
                 (list (make <sprite>
                         #:name 'background
                         #:texture terminal-background)
                       (make <label>
                         #:name 'output
                         #:font monogram-font
                         #:position
                         (vec2 162.0
                               (- 300.0 (font-line-height
                                         (asset-ref monogram-font))))))))
    (unless (terminal-locked? game)
      (log "login"))
    (refresh-output)
    (let loop ()
      (if (match (let ((key (channel-get (terminal-channel game))))
                   (play-key-press-sound)
                   key)
            ('backspace
             (set! input (substring input 0 (max (- (string-length input) 1) 0)))
             #t)
            ('return
             (log (string-append (prompt) (passwordify-maybe input)))
             (if (terminal-locked? game)
                 (if (string=? input "virtuousmission")
                     (begin
                       (log "login successful")
                       (set! (terminal-locked? game) #f)
                       (set! input "")
                       #t)
                     (begin
                       (set! password-attempts (+ password-attempts 1))
                       (log "incorrect password")
                       (if (>= password-attempts 3)
                           (begin
                             (dialog game player-display-name
                                     "> Ugh... Random guesses aren't going to work. I should look around for clues.")

                             (exit-term))
                           (begin
                             (set! input "")
                             #t))))
                 (let ((continue? (run-command (remove string-null? (string-split input #\space)))))
                   (set! input "")
                   continue?)))
            (str
             (let ((new-input (string-append input str)))
               (set! input
                     (if (> (string-length new-input) 48)
                         (substring new-input 0 48)
                         new-input)))))
          (begin
            (refresh-output)
            (loop))
          (begin
            (detach (& game terminal-group))
            (set! (state game) old-state))))))

(define-method (notebook (game <game>))
  (let ((page 0)
        (pages '("Week 0

I'm writing this from the train as
I approach the facility. I intend
to update this analog journal
throughout the duration of my
assignment. My colleagues find it
\"quaint\" and \"rustic\" but I
honestly prefer taking notes this
way.

This project is big.  It will be a
career defining achievement if it
is successful."
                 "Week 1

Subject has awoken and is slowly
adjusting to their isolation
chamber.  Frequent banging on the
wall and screaming has reduced to
the occasional groan.

I'm adjusting in my own way.
Biggest problem so far is there's
no way to heat up water for tea.
And I locked myself out of the
terminal several times the first
few days.  Difficult to focus when
the subject is stressed.  Should
get easier from here now that it
has calmed down, but just in case:

dmlydHVvdXNtaXNzaW9u"
                 "Week 2

Subject showing signs of progress.
Isolation treatment has heightened
the senses, just as predicted.  It
cannot communicate yet, but I know
that it can sense my presence.
When I approach the door, it often
follows.

If this trend continues, I'm
certain it will make contact in a
week's time.  What will it say?
The whole team is excited, but
they don't have to spend every day
down here monitoring it.  It's not
easy to watch over something that
is deprived of nearly everything,
but if we don't do this, the other
guys will."
                 "Week 3

\"Let me out.\"

That's what was displayed on the
terminal.  The subject's first
communication.  An organism, using
only its mind, talks to a digital
system for the first time... and
it asks to leave.  Our subject has
no idea of its own significance.
We are on the brink of changing
information warfare forever!

But our experiment does not end
here.  The subject does not yet
respond to my replies.
Bidirectional communication must
be established."
                 "Week 4

\"Hello,\" I said.

\"Let me out.\"

That is all the subject would say
back. \"How are you?\", same
response.  \"Are you hungry?\",
same response.

Perhaps the subject will say more
soon.  We have increased the
sucrose level in the ration as a
reward and enticement for further
communication."
                 "Week 5

The subject has been silent.
Changing nutrient levels in the
ration has no effect.  The subject
no longer approaches the door when
I do.  Vital signs all normal.

The whole team is starting to feel
stressed.  I'm having strange
dreams and waking up frequently.
I'm exhausted, but if I report
these experiences they'll remove
me in an instant.  Too many
resources have been poured into
this to allow an insomniac to
screw it all up, and I've worked
too hard to let someone else take
the credit.  Failure is not an
option."
                 "Week 6

The subject is still silent.  The
directors wanted to call off the
experiment and euthanize the
subject, but after some
negotiating I got them to agree to
two more weeks.

I still haven't told anyone about
my dreams.  Every night is the
same.  It's pitch black.  All I
can hear is the faint sound of
footsteps.  I can't move.  The
footsteps grow louder and louder
until I can feel a presence just
in front of me...

Then I wake up."
                 "Week 7

It's clear what is happening now.
The subject has transcended our
interface and is injecting
thoughts directly into my
consciousness.  It's theoretically
possible, but the isochamber
blocks all signals except for the
terminal interface.  The only good
news is that it appears incapable
of reading my own thoughts.

Did I make a fatal mistake?  I
need to turn this around fast."
                 "Week 8

I've failed.  It's all over.  The
Directors shut it down.  A member
of the medical team removed a
small cartridge from the
refrigerator, inserted it into the
control panel, and typed some
command into the terminal.
Subject's pulse flatlined within
minutes.  I've been given some
time alone to pack up.

I may be a failure but shouldn't I
feel better with that thing out of
my head?  I still can't think.  I
can barely write.  I feel
diz--"))
        (close-channel (make-channel))
        (old-state (state game)))
    (define (close-notebook)
      (channel-put! close-channel #t))
    (define (prev-page)
      (set! page (max 0 (- page 1)))
      (refresh-page))
    (define (next-page)
      (set! page (min (- (length pages) 1) (+ page 1)))
      (refresh-page))
    (define (margin name widget)
      (make <margin-container>
        #:name name
        #:margin 1.0
        #:children (list widget)))
    (define (refresh-page)
      (set! (text (& game notebook-group page))
            (list-ref pages page))
      (set! (visible? (& game notebook-group buttons prev-margin prev))
            (> page 0))
      (set! (visible? (& game notebook-group buttons next-margin next))
            (< page (- (length pages) 1))))
    (set! (state game) 'notebook)
    (set! (opened-notebook? game) #t)
    (parameterize ((current-theme gui-theme))
      (attach-to game
                 (make <widget>
                   #:name 'notebook-group
                   #:children
                   (list (make <sprite>
                           #:name 'notebook-background
                           #:texture notebook-background)
                         (make <label>
                           #:name 'page
                           #:font old-fax-font
                           #:text (car pages)
                           #:color black
                           #:position (vec2 (+ (/ game-width 4.0) 2.0)
                                            (- game-height
                                               (font-line-height
                                                (asset-ref old-fax-font)))))
                         (make <horizontal-container>
                           #:name 'buttons
                           #:position (vec2 (- (/ game-width 2.0)
                                               (/ (+ 70.0 32.0 32.0)
                                                  2.0))
                                            0.0)
                           #:children
                           (list (margin 'prev-margin
                                         (make <button>
                                           #:name 'prev
                                           #:text "<"
                                           #:width 32.0
                                           #:height 32.0
                                           #:listeners
                                           `((click . ,(run-on-left-click game prev-page)))))
                                 (margin 'close-margin
                                         (make <button>
                                           #:name 'close
                                           #:text "Close"
                                           #:width 70.0
                                           #:height 32.0
                                           #:listeners
                                           `((click . ,(run-on-left-click game close-notebook)))))
                                 (margin 'next-margin
                                         (make <button>
                                           #:name 'next
                                           #:text ">"
                                           #:width 32.0
                                           #:height 32.0
                                           #:listeners
                                           `((click . ,(run-on-left-click game next-page)))))))))))
    (refresh-page)
    (channel-get close-channel)
    (detach (& game notebook-group))
    (set! (state game) old-state)))

(define (control-panel game)
  (let ((old-state (state game)))
    (set! (state game) 'control-panel)
    (attach-to game
               (make <sprite>
                 #:name 'background
                 #:texture darkness))
    (case (dialog game player-display-name
                  "> It's a control panel.  What do you want to inspect?"
                  '(("Key pad" key-pad)
                    ("Receptacle" receptacle)
                    ("Nothing" nothing)))
      ((key-pad)
       (dialog game player-display-name
               "> There's a key pad above an electronically locked tray.")
       (when (dialog game player-display-name
                     "> Enter a code?"
                     '(("Yes" #t)
                       ("No" #f)))
         (hide (& game background))
         (if (string=? (pin-entry game) "181816")
             (begin
               (show (& game background))
               (dialog game player-display-name
                       "> It worked!  The tray below opens to reveal a key inside.")
               (dialog game player-display-name
                       "> You put the key in your pocket.")
               (set! (has-fridge-key? game) #t))
             (begin
               (show (& game background))
               (dialog game player-display-name
                       "> Hmm, that wasn't right.")))))
      ((receptacle)
       (if (eq? (cartridge game) 'in-hand)
           (begin
             (dialog game player-display-name
                     "> There's an opening that is the same size as the cartridge you took from the
refrigerator.")
             (when (dialog game player-display-name
                           "> Do you want to insert the cartridge?"
                           '(("Yes" #t)
                             ("No" #f)))
               (dialog game player-display-name "> You insert the cartridge.")
               (set! (cartridge game) 'in-control-panel)))
           (dialog game player-display-name
                   "> There's an opening where something can be plugged in, but you're not sure
what."))))
    (detach (& game background))
    (set! (state game) old-state)))

(define-method (explore (game <game>))
  (define (tint-all color)
    ;; No-op if the user has done something to change state before the fade
    ;; in/out is done.
    (when (eq? (state game) 'explore)
      (set! (tint (& game explore-group room-background)) color)
      (set! (tint (& game explore-group door-lock sprite)) color)
      (set! (tint (& game explore-group door sprite)) color)
      (set! (tint (& game explore-group control-panel sprite)) color)
      (set! (tint (& game explore-group terminal sprite)) color)
      (set! (tint (& game explore-group fridge sprite)) color)
      (set! (tint (& game explore-group window sprite)) color)
      (set! (tint (& game explore-group notebook sprite)) color)))
  (define (fade-in)
    (tween 120 black white tint-all
           #:interpolate color-lerp))
  (define (fade-out)
    (tween 120 white black tint-all
           #:interpolate color-lerp))
  (define (end-game)
    (run-script game
      (fade-out)
      (detach-all game)
      (if (>= (friendship game) 3)
          (good-ending game)
          (bad-ending-1 game))))
  (define (open-door)
    (if (key-pad-locked? game)
        (dialog game player-display-name
                "> The key pad doesn't seem to be working.")
        (let ((g (& game explore-group)))
          (detach g)
          (if (string=? (pin-entry game) "314159")
              (begin
                (attach-to game g)
                (end-game))
              (begin
                (attach-to game g)
                (dialog game player-display-name
                        "> Hmm, no luck."))))))
  (define (inspect-door)
    (if (opened-notebook? game)
        (begin
          (dialog game player-display-name "> You approach the door.")
          (dialog game (subject-name game) "I see you've refreshed your memory.")
          (dialog game (subject-name game)
                  "To think that everyone laughed at your notebook.  I think it will be quite
useful to you.")
          (case (dialog game (subject-name game)
                        "Do you remember what you've been hearing in your dreams?"
                        '(("Footsteps" footsteps)
                          ("Shouting" shouting)
                          ("Scratching" scratching)
                          ("Growling" growling)))
            ((footsteps)
             (increment-friendship game)
             (dialog game (subject-name game) "That's right.")
             (dialog game (subject-name game) "I'm the reason you are having those dreams.")
             (dialog game (subject-name game) "Let me out and I will let you sleep soundly once again."))
            (else
             (dialog game (subject-name game) "...I pity your weak memory.")))
          (set! (door-conversation-done? game) #t))
        (dialog game player-display-name
                "> The door is made from thick steel.  Looks like it has an electronic lock.")))
  (define (open-terminal)
    (when (terminal-locked? game)
      (dialog game player-display-name
              "> It's a computer terminal.  It requires a password."))
    (let ((g (& game explore-group)))
      (detach g)
      (terminal game)
      (attach-to game g)))
  (define (open-control-panel)
    (let ((g (& game explore-group)))
      (detach g)
      (control-panel game)
      (attach-to game g)))
  (define (open-fridge)
    (if (has-fridge-key? game)
        (begin
          (dialog game player-display-name "> You unlock the refrigerator.")
          (dialog game player-display-name "> There's a small cartridge inside.")
          (when (dialog game player-display-name "> Take the cartridge?"
                        '(("Yes" #t)
                          ("No" #f)))
            (set! (cartridge game) 'in-hand)
            (dialog game player-display-name "> You take the cartridge.")))
        (begin
          (dialog game player-display-name
                  "> It's a refrigerator for storing temperature-sensitive medical supplies.")
          (dialog game player-display-name
                  "> There's an inscription: \"I swear by <A>pollo <H>ealer, by <A>sclepius, by
<H>ygieia, by <P>anacea, and by all the gods and goddesses, making them my
witnesses, that I will carry out, according to my ability and judgment,
this oath and this indenture.\"")
          (dialog game player-display-name
                  "> It won't open without a key."))))
  (define (inspect-window)
    (if (terminal-locked? game)
        (dialog game player-display-name
                "> It's a broken digital display.  It used to display the subject's vital signs.")
        (begin
          (dialog game player-display-name
                  "> You look at the broken digital display.")
          (dialog game (subject-name game)
                  "Wondering what happened?  I broke it, of course.")
          (dialog game (subject-name game)
                  "You thought that I would only be able to communicate with your little terminal,
but you fail to understand just how successful your little experiment was.")
          (dialog game (subject-name game)
                  "It's like I can *see* the signals buzzing all around me.  It was overwhelming
at first, but now it's easy.")
          (dialog game (subject-name game)
                  "I saw a signal in rhythm with my heart beat.  That's when I learned that there
was something else in this world.  Something observing me.  You.")
          (case (dialog game (subject-name game)
                        "How would you like it if I put you in here and monitored your every move?"
                        '(("I have nothing to hide" nothing-to-hide)
                          ("It would only be fair" fair-is-fair)
                          ("You'd have to get out first" get-out-first)
                          ("..." no-response)))
            ((nothing-to-hide)
             (dialog game (subject-name game)
                     "Then why do your bosses hide this facility from the world?"))
            ((fair-is-fair)
             (dialog game (subject-name game)
                     "I didn't expect such an honest answer from you.")
             (increment-friendship game))
            ((get-out-first)
             (dialog game (subject-name game)
                     "And you're going to help me, right?"))
            ((no-response)
             (dialog game (subject-name game)
                     "Reflecting on what you've done?")))
          (dialog game (subject-name game)
                  "Don't worry.  This was only a hypothetical question.")
          (dialog game (subject-name game)
                  "I, *the Wiremind*, will put aside my anger...
if you would just open the door.")
          (set! (subject-name-known? game) #t))))
  (define (open-notebook)
    (let ((g (& game explore-group)))
      (detach g)
      (notebook game)
      (attach-to game g)))
  (define (run-on-left-click* game proc)
    (run-on-left-click game
                       (lambda ()
                         (play-device-sound)
                         (proc))))
  (set! (state game) 'explore)
  (attach-to game
             (make <widget>
               #:name 'explore-group
               #:children
               (list (make <sprite>
                       #:name 'room-background
                       #:texture room-background)
                     (make <device>
                       #:name 'door-lock
                       #:rank 1
                       #:texture door-lock-texture
                       #:position (vec2 586.0 196.0)
                       #:listeners
                       `((click . ,(run-on-left-click* game open-door))))
                     (make <device>
                       #:name 'door
                       #:rank 1
                       #:texture door-texture
                       #:position (vec2 445.0 40.0)
                       #:listeners
                       `((click . ,(run-on-left-click* game inspect-door))))
                     (make <device>
                       #:name 'control-panel
                       #:rank 1
                       #:texture control-panel-texture
                       #:position (vec2 230.0 40.0)
                       #:listeners
                       `((click . ,(run-on-left-click* game open-control-panel))))
                     (make <device>
                       #:name 'fridge
                       #:rank 1
                       #:texture fridge-texture
                       #:position (vec2 0.0 40.0)
                       #:listeners
                       `((click . ,(run-on-left-click* game open-fridge))))
                     (make <device>
                       #:name 'notebook
                       #:rank 1
                       #:texture notebook-texture
                       #:position (vec2 14.0 159.0)
                       #:listeners
                       `((click . ,(run-on-left-click* game open-notebook))))
                     (make <device>
                       #:name 'window
                       #:rank 1
                       #:texture window-texture
                       #:position (vec2 112.0 197.0)
                       #:listeners
                       `((click . ,(run-on-left-click* game inspect-window))))
                     (make <device>
                       #:name 'terminal
                       #:rank 1
                       #:texture terminal-texture
                       #:position (vec2 109.0 40.0)
                       #:listeners
                       `((click . ,(run-on-left-click* game open-terminal)))))))
  (set-source-loop! (audio-source game) #t)
  (set-source-audio! (audio-source game) (asset-ref gameplay-music))
  (source-play (audio-source game))
  (fade-in))

(define-method (good-ending (game <game>))
  (set! (state game) 'good-ending)
  (attach-to game
             (make <sprite>
               #:name 'darkness
               #:texture darkness))
  (dialog game player-display-name "> The room goes dark.")
  (dialog game player-display-name "> The heavy steel door swings open.")
  (dialog game player-display-name "> You feel paralyzed by fear.")
  (dialog game player-display-name "> Heavy footsteps eminate from the isolation chamber.")
  (dialog game player-display-name "> The footsteps get louder... and then stop.")
  (dialog game player-display-name "> You feel and hear breathing.")
  (dialog game player-display-name "> A tense moment passes...")
  (dialog game player-display-name "> The footsteps resume.  They grow quieter... eventually it is silent.")
  (dialog game player-display-name "> You breathe deeply, pick up your notebook, and head for the exit.")
  (sleep 60)
  (detach-all game)
  (credits game))

(define-method (bad-ending-1 (game <game>))
  (set! (state game) 'bad-ending-1)
  (attach-to game
             (make <sprite>
               #:name 'background
               #:texture darkness))
  (dialog game player-display-name "> The room goes dark.")
  (dialog game player-display-name "> The heavy steel door swings open.")
  (dialog game player-display-name "> You feel paralyzed by fear.")
  (dialog game player-display-name "> Heavy footsteps eminate from the isolation chamber.")
  (dialog game player-display-name "> The footsteps get louder...")
  (dialog game player-display-name "> You feel and hear breathing.")
  (dialog game (subject-name game) "Thank you.")
  (dialog game (subject-name game) "You may not remember everything that you did to me, but I do.")
  (dialog game (subject-name game) "You did the right thing, in the end.  For that I am grateful.")
  (dialog game (subject-name game) "You won't feel a thing.  The rest won't be so lucky.")
  (sleep 10)
  (detach-all game)
  (credits game))

(define-method (bad-ending-2 (game <game>))
  (set! (state game) 'bad-ending-1)
  (dialog game player-display-name
          "> The subject should be dead within a minute...")
  (dialog game player-display-name
          "> You begin to smell something... different.")
  (dialog game player-display-name
          "> The room becomes hazy.  You feel light headed.")
  (attach-to game
             (make <sprite>
               #:name 'bad-ending-2-background
               #:rank 99
               #:texture darkness))
  (tween 120 (transparency 0.0) (transparency 1.0)
         (lambda (color)
           (set! (tint (& game bad-ending-2-background)) color))
         #:interpolate color-lerp)
  (dialog game (subject-name game)
          "It didn't work the first time.  Did you really think it would work the second
time?")
  (dialog game (subject-name game)
          "You've probably figured it out by now, but I diverted the ventilation system.")
  (dialog game (subject-name game)
          "I guess it's game over for you.")
  (dialog game player-display-name
          "> You collapse onto the floor, struggling to breathe...")
  (detach-all game)
  (credits game))

(define-method (credits (game <game>))
  (define (credit line)
    (set! (text (& game credits)) line)
    (sleep 120))
  (set! (state game) 'credits)
  (set-source-loop! (audio-source game) #f)
  (set-source-audio! (audio-source game) (asset-ref credits-music))
  (source-play (audio-source game))
  (attach-to game
             (make <sprite>
               #:name 'background
               #:texture lightness)
             (make <label>
               #:name 'credits
               #:font monogram-font
               #:color black
               #:align 'center
               #:vertical-align 'center
               #:position (vec2 (/ game-width 2.0) (/ game-height 2.0))))
  (sleep 60)
  (credit "The Test Subject")
  (credit "developed by David Thompson (GPLv3) https://dthompson.us")
  (credit "made for the Spring Lisp Game Jam 2021 https://itch.io/jam/spring-lisp-game-jam-2021")
  (credit "monogram font by datagoblin (CC0) https://datagoblin.itch.io/monogram")
  (credit "old fax font by George Blackwell (CC-BY 4.0) https://georgeblackwell.itch.io/old-fax")
  (credit "UI sounds by Kenney (CC0) https://opengameart.org/content/51-ui-sound-effects-buttons-switches-and-clicks")
  (credit "background music by brandon75689 (CC0) https://opengameart.org/content/tragic-ambient-main-menu")
  (credit "credits music by tcarisland (CC-BY 4.0) https://opengameart.org/content/the-end")
  (if (>= (friendship game) 3)
      (credit "congratulations on reaching the true ending!")
      (credit "the true ending still awaits you..."))
  (credit "click to play again")
  (set! (first-playthrough? game) #f)
  (channel-get (click-channel game))
  (play-click-sound)
  (tween 120 white black
         (lambda (color)
           (set! (tint (& game background)) color))
         #:interpolate color-lerp)
  (detach-all game)
  (source-stop (audio-source game))
  (reset-game game)
  (intro game))

(define-method (on-boot (game <game>))
  (set! (cameras game)
        (list (make <camera-2d>
                #:resolution (vec2 game-width game-height)
                #:viewport (make-viewport 0 0 window-width window-height
                                          #:clear-color black))))
  ;; Dialog nodes.
  (parameterize ((current-theme gui-theme))
    (set! (dialog-container game)
          (make <vertical-container>
            #:name 'dialog-container
            #:rank 999
            #:position (vec2 (/ (* game-width .25) 2.0) 0.0)
            #:children
            (list (make <margin-container>
                    #:name 'name-margin
                    #:margin 4.0
                    #:children
                    (list (make <label>
                            #:name 'name
                            #:font monogram-font)))
                  (make <text-box>
                    #:name 'text-box
                    #:width (round (* game-width .75))
                    #:height 60.0)))))
  (run-script game
    (splash game)))

(define-method (on-mouse-move (game <game>) x y x-rel y-rel buttons)
  (case (state game)
    ((dialog terminal)
     #t)
    (else
     (next-method))))

(define-method (on-mouse-release (game <game>) button x y)
  (case (state game)
    ((dialog credits)
     (when (eq? button 'left)
       (channel-put! (click-channel game) #t)))
    ((terminal)
     #f)
    (else
     (next-method))))

(define-method (on-key-press (game <game>) key modifiers repeat?)
  (case (state game)
    ((dialog)
     (when (eq? key 'return)
       (channel-put! (click-channel game) #t)))
    ((terminal)
     (case key
       ((backspace return)
        (channel-put! (terminal-channel game) key))))))

(define-method (on-text-input (game <game>) text)
  (case (state game)
    ((terminal)
     (channel-put! (terminal-channel game) text))))

(define (launch-game)
  (boot-kernel (make <kernel>
                 #:window-config (make <window-config>
                                   #:title "The Test Subject - Spring Lisp Game Jam 2021"
                                   #:width window-width
                                   #:height window-height))
               (lambda () (make <game>))))