Repository navigation
Expand file tree
/
Copy pathMain.lean
More file actions
3008 lines (2819 loc) · 141 KB
/
Copy pathMain.lean
File metadata and controls
3008 lines (2819 loc) · 141 KB
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
import Std
namespace Lean64
-- Native code only owns the window, key polling, and framebuffer presentation.
@[extern "lean64_create_window"]
opaque createWindow (width height : UInt32) (visible : Bool) : IO Bool
@[extern "lean64_key_down"]
opaque keyDown (virtualKey : UInt32) : IO Bool
@[extern "lean64_mouse_delta"]
opaque mouseDelta : IO UInt64
@[extern "lean64_play_shot"]
opaque playShot : IO Unit
@[extern "lean64_play_chaingun"]
opaque playChaingun : IO Unit
@[extern "lean64_play_cue"]
opaque playCue (cue : UInt32) : IO Unit
@[extern "lean64_present"]
opaque present (pixels : @& Array UInt32) (width height : UInt32) : IO Bool
@[extern "lean64_present_gpu"]
opaque presentGpu (triangles : @& Array UInt64) (hud : @& Array UInt32)
(hudWidth hudHeight : UInt32) (cameraX cameraY cameraZ yaw pitch : UInt64) : IO Bool
@[extern "lean64_close_window"]
opaque closeWindow : IO Unit
def renderWidth : Nat := 128
def renderHeight : Nat := 72
def targetFrameMs : Nat := 16
def nearPlane : Float := 0.08
def focalLength : Float := 92.0
structure Vec3 where
x : Float
y : Float
z : Float
namespace Vec3
def add (a b : Vec3) : Vec3 := ⟨a.x + b.x, a.y + b.y, a.z + b.z⟩
def sub (a b : Vec3) : Vec3 := ⟨a.x - b.x, a.y - b.y, a.z - b.z⟩
def scale (a : Vec3) (s : Float) : Vec3 := ⟨a.x * s, a.y * s, a.z * s⟩
def dot (a b : Vec3) : Float := a.x * b.x + a.y * b.y + a.z * b.z
def length (a : Vec3) : Float := (dot a a).sqrt
def normalize (a : Vec3) : Vec3 :=
let length := a.length
if length < 0.0001 then ⟨0.0, 0.0, 0.0⟩ else a.scale (1.0 / length)
end Vec3
structure Color where
r : Nat
g : Nat
b : Nat
structure Triangle where
a : Vec3
b : Vec3
c : Vec3
color : Color
material : Nat
structure ScreenVertex where
x : Float
y : Float
depth : Float
structure Camera where
position : Vec3
yaw : Float
pitch : Float
structure Enemy where
position : Vec3
health : Nat
attackCooldown : Float := 0.0
phase : Float := 0.0
kind : Nat := 0
alerted : Bool := false
painTime : Float := 0.0
deathTime : Float := 0.0
attackWindup : Float := 0.0
countsForExit : Bool := true
structure Barrel where
position : Vec3
health : Nat := 1
blastTime : Float := 0.0
structure Projectile where
position : Vec3
velocity : Vec3
life : Float
damage : Nat := 12
structure Particle where
position : Vec3
velocity : Vec3
life : Float
kind : Nat := 0
structure ImpactMark where
position : Vec3
kind : Nat := 0
structure Pickup where
position : Vec3
kind : Nat
active : Bool := true
structure GameState where
camera : Camera
enemies : Array Enemy
barrels : Array Barrel
ammo : Nat
bullets : Nat
weapon : Nat
hasChaingun : Bool
hasRedKey : Bool
muzzleFlash : Nat
health : Nat
damageFlash : Nat
score : Nat
projectiles : Array Projectile
particles : Array Particle
marks : Array ImpactMark
weaponCooldown : Float
bobPhase : Float
bobStrength : Float
recoilKick : Float
hitMarker : Nat
pickupFlash : Nat
pickups : Array Pickup
doorOpen : Float
levelComplete : Bool
shotThisFrame : Bool
mapOpen : Bool
secretFound : Bool
secretFlash : Nat
keyFlash : Nat
cacheFound : Bool
cacheDoorOpen : Float
started : Bool
paused : Bool
playTime : Float
shotsFired : Nat
shotsHit : Nat
killsMade : Nat
difficulty : Nat
level : Nat
structure Input where
forward : Bool
backward : Bool
left : Bool
right : Bool
lookLeft : Bool
lookRight : Bool
lookUp : Bool
lookDown : Bool
mouseX : Float
mouseY : Float
sprint : Bool
shoot : Bool
use : Bool
weapon1 : Bool
weapon2 : Bool
weapon3 : Bool
map : Bool
confirm : Bool
pause : Bool
restart : Bool
quit : Bool
def noInput : Input := {
forward := false, backward := false, left := false, right := false
lookLeft := false, lookRight := false, lookUp := false, lookDown := false
mouseX := 0.0, mouseY := 0.0
sprint := false, shoot := false, use := false, weapon1 := false, weapon2 := false
weapon3 := false
map := false, restart := false, quit := false
confirm := false, pause := false
}
def floatToNat (value : Float) : Nat := value.floor.toUInt64.toNat
def clampedNat (value : Float) (limit : Nat) : Nat :=
if value < 0.0 then 0 else min limit (floatToNat value)
def colorScale (color : Color) (amount : Float) : Color :=
let channel (value : Nat) := min 255 (floatToNat (Float.ofNat value * amount))
⟨channel color.r, channel color.g, channel color.b⟩
def quadMaterial (a b c d : Vec3) (color : Color) (material : Nat) : Array Triangle := #[
{ a, b, c, color, material }, { a, b := c, c := d, color, material }
]
def quad (a b c d : Vec3) (color : Color) : Array Triangle :=
quadMaterial a b c d color 0
def boxMeshMaterial (minimum maximum : Vec3) (color : Color) (material : Nat) : Array Triangle :=
let p000 : Vec3 := ⟨minimum.x, minimum.y, minimum.z⟩
let p001 : Vec3 := ⟨minimum.x, minimum.y, maximum.z⟩
let p010 : Vec3 := ⟨minimum.x, maximum.y, minimum.z⟩
let p011 : Vec3 := ⟨minimum.x, maximum.y, maximum.z⟩
let p100 : Vec3 := ⟨maximum.x, minimum.y, minimum.z⟩
let p101 : Vec3 := ⟨maximum.x, minimum.y, maximum.z⟩
let p110 : Vec3 := ⟨maximum.x, maximum.y, minimum.z⟩
let p111 : Vec3 := ⟨maximum.x, maximum.y, maximum.z⟩
let dark := colorScale color 0.55
let side := colorScale color 0.78
let top := colorScale color 1.15
quadMaterial p001 p101 p111 p011 color material ++
quadMaterial p100 p000 p010 p110 dark material ++
quadMaterial p000 p001 p011 p010 side material ++
quadMaterial p101 p100 p110 p111 side material ++
quadMaterial p010 p011 p111 p110 top material ++
quadMaterial p000 p100 p101 p001 dark material
def boxMesh (minimum maximum : Vec3) (color : Color) : Array Triangle :=
boxMeshMaterial minimum maximum color 0
def floorMesh : Array Triangle := Id.run do
let mut triangles := #[]
for z in [0:4] do
for x in [0:4] do
let x0 := Float.ofNat x * 4.0 - 8.0
let z0 := Float.ofNat z * 4.0 - 8.0
let color : Color := if (x + z) % 2 == 0 then ⟨44, 48, 58⟩ else ⟨34, 38, 48⟩
triangles := triangles ++ quadMaterial
⟨x0, 0.0, z0⟩ ⟨x0 + 4.0, 0.0, z0⟩
⟨x0 + 4.0, 0.0, z0 + 4.0⟩ ⟨x0, 0.0, z0 + 4.0⟩ color 3
return triangles
def ceilingMesh : Array Triangle := Id.run do
let mut triangles := #[]
for z in [0:4] do
for x in [0:4] do
let x0 := Float.ofNat x * 4.0 - 8.0
let z0 := Float.ofNat z * 4.0 - 8.0
let color : Color := if (x + z) % 2 == 0 then ⟨32, 34, 43⟩ else ⟨25, 27, 35⟩
triangles := triangles ++ quadMaterial
⟨x0, 3.5, z0⟩ ⟨x0, 3.5, z0 + 4.0⟩
⟨x0 + 4.0, 3.5, z0 + 4.0⟩ ⟨x0 + 4.0, 3.5, z0⟩ color 2
return triangles
def levelOneWorld : Array Triangle :=
floorMesh ++ ceilingMesh ++
boxMeshMaterial ⟨-8.2, 0.0, -8.2⟩ ⟨-7.7, 3.5, 8.2⟩ ⟨90, 74, 72⟩ 1 ++
boxMeshMaterial ⟨7.7, 0.0, -8.2⟩ ⟨8.2, 3.5, 8.2⟩ ⟨90, 74, 72⟩ 1 ++
boxMeshMaterial ⟨-8.2, 0.0, -8.2⟩ ⟨8.2, 3.5, -7.7⟩ ⟨90, 74, 72⟩ 1 ++
boxMeshMaterial ⟨-8.2, 0.0, 7.7⟩ ⟨8.2, 3.5, 8.2⟩ ⟨90, 74, 72⟩ 1 ++
boxMeshMaterial ⟨-3.8, 0.0, -1.0⟩ ⟨-2.2, 2.6, 1.0⟩ ⟨74, 99, 126⟩ 2 ++
-- Hollow maintenance cache: four thick walls, split front, and a low roof.
boxMeshMaterial ⟨2.0, 0.0, 1.8⟩ ⟨2.25, 2.35, 3.2⟩ ⟨122, 73, 52⟩ 1 ++
boxMeshMaterial ⟨3.55, 0.0, 1.8⟩ ⟨3.8, 2.35, 3.2⟩ ⟨122, 73, 52⟩ 1 ++
boxMeshMaterial ⟨2.25, 0.0, 2.95⟩ ⟨3.55, 2.35, 3.2⟩ ⟨122, 73, 52⟩ 1 ++
boxMeshMaterial ⟨2.0, 0.0, 1.8⟩ ⟨2.55, 2.35, 2.05⟩ ⟨122, 73, 52⟩ 1 ++
boxMeshMaterial ⟨3.25, 0.0, 1.8⟩ ⟨3.8, 2.35, 2.05⟩ ⟨122, 73, 52⟩ 1 ++
boxMeshMaterial ⟨2.0, 2.15, 1.8⟩ ⟨3.8, 2.42, 3.2⟩ ⟨74, 82, 90⟩ 2 ++
boxMeshMaterial ⟨-0.8, 0.0, 4.2⟩ ⟨0.8, 3.2, 5.4⟩ ⟨65, 91, 72⟩ 2 ++
-- Low, hot wall lights and a toxic-green exit marker.
boxMeshMaterial ⟨-7.68, 0.65, -3.2⟩ ⟨-7.58, 1.55, -1.6⟩ ⟨235, 48, 22⟩ 4 ++
boxMeshMaterial ⟨7.58, 0.65, 2.0⟩ ⟨7.68, 1.55, 3.6⟩ ⟨235, 48, 22⟩ 4 ++
boxMeshMaterial ⟨-0.55, 2.25, 5.38⟩ ⟨0.55, 2.85, 5.48⟩ ⟨65, 240, 105⟩ 4 ++
-- A cross-room bulkhead leaves a central doorway between encounters.
boxMeshMaterial ⟨-8.0, 0.0, 0.45⟩ ⟨-1.28, 3.5, 0.78⟩ ⟨68, 76, 86⟩ 2 ++
boxMeshMaterial ⟨1.28, 0.0, 0.45⟩ ⟨8.0, 3.5, 0.78⟩ ⟨68, 76, 86⟩ 2 ++
boxMeshMaterial ⟨-1.72, 3.18, 0.38⟩ ⟨1.72, 3.46, 0.84⟩ ⟨95, 102, 110⟩ 2
def levelTwoWorld : Array Triangle :=
floorMesh ++ ceilingMesh ++
boxMeshMaterial ⟨-8.2, 0.0, -8.2⟩ ⟨-7.7, 3.5, 8.2⟩ ⟨52, 76, 78⟩ 2 ++
boxMeshMaterial ⟨7.7, 0.0, -8.2⟩ ⟨8.2, 3.5, 8.2⟩ ⟨52, 76, 78⟩ 2 ++
boxMeshMaterial ⟨-8.2, 0.0, -8.2⟩ ⟨8.2, 3.5, -7.7⟩ ⟨52, 76, 78⟩ 2 ++
boxMeshMaterial ⟨-8.2, 0.0, 7.7⟩ ⟨8.2, 3.5, 8.2⟩ ⟨52, 76, 78⟩ 2 ++
-- Alternating bulkheads force a right-left-right combat route.
boxMeshMaterial ⟨-8.0, 0.0, -2.75⟩ ⟨2.0, 2.72, -2.20⟩ ⟨70, 88, 96⟩ 2 ++
boxMeshMaterial ⟨-2.0, 0.0, 0.68⟩ ⟨8.0, 2.72, 1.23⟩ ⟨70, 88, 96⟩ 2 ++
boxMeshMaterial ⟨-8.0, 0.0, 4.00⟩ ⟨2.0, 2.72, 4.55⟩ ⟨70, 88, 96⟩ 2 ++
-- Emissive route beacons and exit-side pylons.
boxMeshMaterial ⟨1.55, 0.62, -2.82⟩ ⟨1.92, 1.62, -2.72⟩ ⟨35, 215, 205⟩ 4 ++
boxMeshMaterial ⟨-1.92, 0.62, 1.23⟩ ⟨-1.55, 1.62, 1.33⟩ ⟨35, 215, 205⟩ 4 ++
boxMeshMaterial ⟨1.55, 0.62, 3.90⟩ ⟨1.92, 1.62, 4.00⟩ ⟨35, 215, 205⟩ 4 ++
boxMeshMaterial ⟨5.45, 0.0, 5.45⟩ ⟨5.85, 2.25, 5.85⟩ ⟨85, 48, 94⟩ 1 ++
boxMeshMaterial ⟨6.95, 0.0, 5.45⟩ ⟨7.35, 2.25, 5.85⟩ ⟨85, 48, 94⟩ 1
def doorMesh (openAmount : Float) : Array Triangle :=
let bottom := openAmount * 3.25
if bottom >= 3.24 then #[]
else boxMeshMaterial ⟨-1.26, bottom, 0.47⟩ ⟨1.26, 3.28, 0.76⟩ ⟨110, 104, 88⟩ 2
def doorSwitchMesh (hasKey : Bool) (openAmount : Float) : Array Triangle :=
let color : Color := if openAmount >= 0.86 then ⟨60, 238, 108⟩
else if hasKey then ⟨250, 184, 48⟩ else ⟨238, 48, 35⟩
boxMeshMaterial ⟨1.38, 1.0, 0.38⟩ ⟨1.72, 1.52, 0.46⟩ color 4 ++
boxMeshMaterial ⟨1.45, 1.16, 0.36⟩ ⟨1.65, 1.36, 0.38⟩
(if hasKey then ⟨255, 232, 105⟩ else ⟨105, 18, 22⟩) 4
def cacheDoorMesh (openAmount : Float) : Array Triangle :=
let bottom := openAmount * 2.28
if bottom >= 2.26 then #[]
else boxMeshMaterial ⟨2.55, bottom, 1.79⟩ ⟨3.25, 2.30, 2.04⟩ ⟨118, 70, 50⟩ 1
def cacheSwitchMesh (found : Bool) : Array Triangle :=
let color : Color := if found then ⟨65, 238, 112⟩ else ⟨118, 52, 42⟩
boxMeshMaterial ⟨2.12, 0.82, 1.70⟩ ⟨2.43, 1.34, 1.82⟩ color 4 ++
boxMeshMaterial ⟨2.20, 0.98, 1.67⟩ ⟨2.35, 1.18, 1.70⟩
(if found then ⟨250, 210, 72⟩ else ⟨56, 24, 22⟩) 4
def exitMeshAt (centerX centerZ : Float) (unlocked complete : Bool) : Array Triangle :=
let color : Color := if complete then ⟨225, 235, 255⟩
else if unlocked then ⟨52, 245, 102⟩ else ⟨215, 35, 24⟩
boxMeshMaterial ⟨centerX - 0.78, 0.025, centerZ - 0.38⟩
⟨centerX + 0.78, 0.11, centerZ + 0.38⟩ color 4
def secretPanelMesh (found : Bool) : Array Triangle :=
let panel : Color := if found then ⟨65, 235, 112⟩ else ⟨76, 56, 50⟩
let rune : Color := if found then ⟨250, 205, 70⟩ else ⟨128, 44, 34⟩
-- A suspicious inset panel on the western wall. The lit rune changes when used.
boxMeshMaterial ⟨-7.68, 0.62, -5.0⟩ ⟨-7.57, 1.82, -3.72⟩ panel 2 ++
boxMeshMaterial ⟨-7.56, 0.92, -4.66⟩ ⟨-7.50, 1.52, -4.52⟩ rune 4 ++
boxMeshMaterial ⟨-7.56, 1.15, -4.90⟩ ⟨-7.50, 1.29, -4.28⟩ rune 4
def wardenMesh (enemy : Enemy) : Array Triangle :=
let p := enemy.position
let bob := enemy.phase.sin * 0.045
let armor : Color := if enemy.painTime > 0.0 then ⟨255, 210, 220⟩ else ⟨74, 42, 94⟩
let flesh : Color := if enemy.painTime > 0.0 then ⟨255, 175, 165⟩ else ⟨165, 58, 72⟩
let dark := colorScale armor 0.52
let eye : Color := if enemy.alerted then ⟨255, 92, 245⟩ else ⟨90, 42, 82⟩
-- Heavy plated torso, horned head, oversized fists, split armored legs, and reactor core.
boxMeshMaterial ⟨p.x - 0.55, 0.52 + bob, p.z - 0.38⟩
⟨p.x + 0.55, 1.55 + bob, p.z + 0.38⟩ armor 2 ++
boxMesh ⟨p.x - 0.62, 1.45 + bob, p.z - 0.44⟩
⟨p.x + 0.62, 2.18 + bob, p.z + 0.44⟩ flesh ++
boxMeshMaterial ⟨p.x - 0.92, 1.92 + bob, p.z - 0.18⟩
⟨p.x - 0.52, 2.38 + bob, p.z + 0.18⟩ dark 2 ++
boxMeshMaterial ⟨p.x + 0.52, 1.92 + bob, p.z - 0.18⟩
⟨p.x + 0.92, 2.38 + bob, p.z + 0.18⟩ dark 2 ++
boxMeshMaterial ⟨p.x - 0.92, 0.58 + bob, p.z - 0.30⟩
⟨p.x - 0.56, 1.55 + bob, p.z + 0.30⟩ armor 2 ++
boxMeshMaterial ⟨p.x + 0.56, 0.58 + bob, p.z - 0.30⟩
⟨p.x + 0.92, 1.55 + bob, p.z + 0.30⟩ armor 2 ++
boxMeshMaterial ⟨p.x - 0.48, 0.0, p.z - 0.30⟩
⟨p.x - 0.08, 0.62 + bob, p.z + 0.30⟩ dark 2 ++
boxMeshMaterial ⟨p.x + 0.08, 0.0, p.z - 0.30⟩
⟨p.x + 0.48, 0.62 + bob, p.z + 0.30⟩ dark 2 ++
boxMeshMaterial ⟨p.x - 0.36, 1.76 + bob, p.z - 0.47⟩
⟨p.x - 0.10, 1.94 + bob, p.z - 0.43⟩ eye 4 ++
boxMeshMaterial ⟨p.x + 0.10, 1.76 + bob, p.z - 0.47⟩
⟨p.x + 0.36, 1.94 + bob, p.z - 0.43⟩ eye 4 ++
boxMeshMaterial ⟨p.x - 0.22, 0.92 + bob, p.z - 0.41⟩
⟨p.x + 0.22, 1.30 + bob, p.z - 0.38⟩ ⟨200, 45, 235⟩ 4 ++
(if enemy.attackWindup > 0.0 then
let pulse := 0.18 + (0.30 - enemy.attackWindup) * 0.65
boxMeshMaterial ⟨p.x - pulse, 1.20 + bob - pulse, p.z - 0.65 - pulse⟩
⟨p.x + pulse, 1.20 + bob + pulse, p.z - 0.61 + pulse⟩ ⟨200, 42, 255⟩ 4 ++
boxMeshMaterial ⟨p.x - pulse * 0.45, 1.20 + bob - pulse * 0.45, p.z - 0.70⟩
⟨p.x + pulse * 0.45, 1.20 + bob + pulse * 0.45, p.z - 0.58⟩ ⟨255, 175, 255⟩ 4
else #[])
def enemyMesh (enemy : Enemy) : Array Triangle :=
if enemy.health == 0 then
let p := enemy.position
let fall := 1.0 - min 1.0 (enemy.deathTime / 0.62)
let boss := enemy.kind == 3
let torsoTop := if boss then 1.62 - fall * 1.34 else 1.18 - fall * 0.94
let headY := if boss then 1.72 - fall * 1.48 else 1.26 - fall * 1.08
let bodyWidth := if boss then 0.68 else 0.36
let bodyDepth := if boss then 0.46 else 0.28
let headWidth := if boss then 0.78 else 0.44
let corpseColor : Color := if boss then ⟨86, 24, 100⟩ else ⟨120, 34, 28⟩
boxMeshMaterial ⟨p.x - bodyWidth, 0.04, p.z - bodyDepth⟩
⟨p.x + bodyWidth, max 0.17 torsoTop, p.z + bodyDepth⟩ corpseColor 3 ++
boxMeshMaterial ⟨p.x - headWidth, max 0.05 headY, p.z - bodyDepth⟩
⟨p.x + headWidth, max 0.18 (headY + if boss then 0.46 else 0.34), p.z + bodyDepth⟩
(if boss then ⟨58, 18, 72⟩ else ⟨92, 24, 22⟩) 3
else if enemy.kind == 3 then wardenMesh enemy
else
let p := enemy.position
let bob := enemy.phase.sin * 0.07
let skin : Color := if enemy.kind % 2 == 0 then ⟨105, 154, 74⟩ else ⟨155, 74, 92⟩
let wounded : Color := if enemy.painTime > 0.0 then ⟨255, 205, 180⟩
else if enemy.health == 1 then ⟨235, 70, 42⟩ else skin
let dark := colorScale wounded 0.55
let eye : Color := if enemy.alerted then ⟨255, 225, 72⟩ else ⟨82, 72, 34⟩
-- Torso, oversized head, swinging arms, split legs, and bright inset eyes.
boxMesh ⟨p.x - 0.34, 0.48 + bob, p.z - 0.24⟩
⟨p.x + 0.34, 1.28 + bob, p.z + 0.24⟩ wounded ++
boxMesh ⟨p.x - 0.42, 1.22 + bob, p.z - 0.32⟩
⟨p.x + 0.42, 1.82 + bob, p.z + 0.32⟩ wounded ++
boxMesh ⟨p.x - 0.58, 0.52 + bob + enemy.phase.sin * 0.08, p.z - 0.17⟩
⟨p.x - 0.36, 1.30 + bob, p.z + 0.17⟩ dark ++
boxMesh ⟨p.x + 0.36, 0.52 + bob - enemy.phase.sin * 0.08, p.z - 0.17⟩
⟨p.x + 0.58, 1.30 + bob, p.z + 0.17⟩ dark ++
boxMesh ⟨p.x - 0.31, 0.0, p.z - 0.18⟩ ⟨p.x - 0.05, 0.52 + bob, p.z + 0.18⟩ dark ++
boxMesh ⟨p.x + 0.05, 0.0, p.z - 0.18⟩ ⟨p.x + 0.31, 0.52 + bob, p.z + 0.18⟩ dark ++
boxMeshMaterial ⟨p.x - 0.25, 1.48 + bob, p.z - 0.345⟩
⟨p.x - 0.08, 1.62 + bob, p.z - 0.32⟩ eye 4 ++
boxMeshMaterial ⟨p.x + 0.08, 1.48 + bob, p.z - 0.345⟩
⟨p.x + 0.25, 1.62 + bob, p.z - 0.32⟩ eye 4 ++
(if enemy.attackWindup > 0.0 then
let pulse := 0.10 + (0.35 - enemy.attackWindup) * 0.32
boxMeshMaterial ⟨p.x - pulse, 1.04 + bob - pulse, p.z - 0.48 - pulse⟩
⟨p.x + pulse, 1.04 + bob + pulse, p.z - 0.46 + pulse⟩ ⟨255, 112, 24⟩ 4 ++
boxMeshMaterial ⟨p.x - pulse * 0.45, 1.04 + bob - pulse * 0.45, p.z - 0.52⟩
⟨p.x + pulse * 0.45, 1.04 + bob + pulse * 0.45, p.z - 0.44⟩ ⟨255, 238, 125⟩ 4
else #[])
def projectileMesh (projectile : Projectile) : Array Triangle :=
let p := projectile.position
let radius := if projectile.damage > 12 then 0.22 else 0.13
let core := if projectile.damage > 12 then 0.11 else 0.06
boxMeshMaterial ⟨p.x - radius, p.y - radius, p.z - radius⟩
⟨p.x + radius, p.y + radius, p.z + radius⟩
(if projectile.damage > 12 then ⟨198, 42, 255⟩ else ⟨255, 82, 18⟩) 4 ++
boxMeshMaterial ⟨p.x - core, p.y - radius * 1.55, p.z - core⟩
⟨p.x + core, p.y + radius * 1.55, p.z + core⟩
(if projectile.damage > 12 then ⟨255, 155, 255⟩ else ⟨255, 210, 62⟩) 4
def particleMesh (particle : Particle) : Array Triangle :=
let p := particle.position
let radius := if particle.kind == 1 then 0.062 else if particle.kind == 2 then 0.082 else 0.086
let color : Color := if particle.kind == 0 then ⟨195, 24, 32⟩
else if particle.kind == 1 then ⟨255, 192, 54⟩ else ⟨62, 54, 48⟩
boxMeshMaterial ⟨p.x - radius, p.y - radius, p.z - radius⟩
⟨p.x + radius, p.y + radius, p.z + radius⟩ color
(if particle.kind == 1 then 4 else 3)
def impactMarkMesh (mark : ImpactMark) : Array Triangle :=
let p := mark.position
if mark.kind == 1 then
boxMeshMaterial ⟨p.x - 0.34, 0.012, p.z - 0.28⟩
⟨p.x + 0.34, 0.035, p.z + 0.28⟩ ⟨93, 14, 22⟩ 3 ++
boxMeshMaterial ⟨p.x - 0.17, 0.035, p.z - 0.13⟩
⟨p.x + 0.17, 0.048, p.z + 0.13⟩ ⟨145, 20, 28⟩ 3
else
boxMeshMaterial ⟨p.x - 0.085, p.y - 0.085, p.z - 0.085⟩
⟨p.x + 0.085, p.y + 0.085, p.z + 0.085⟩ ⟨34, 25, 24⟩ 3 ++
boxMeshMaterial ⟨p.x - 0.035, p.y - 0.035, p.z - 0.035⟩
⟨p.x + 0.035, p.y + 0.035, p.z + 0.035⟩ ⟨190, 74, 35⟩ 4
def barrelMesh (barrel : Barrel) : Array Triangle :=
let p := barrel.position
if barrel.health > 0 then
boxMeshMaterial ⟨p.x - 0.34, 0.05, p.z - 0.34⟩
⟨p.x + 0.34, 1.02, p.z + 0.34⟩ ⟨44, 112, 62⟩ 2 ++
boxMeshMaterial ⟨p.x - 0.38, 0.10, p.z - 0.38⟩
⟨p.x + 0.38, 0.23, p.z + 0.38⟩ ⟨52, 58, 61⟩ 2 ++
boxMeshMaterial ⟨p.x - 0.38, 0.82, p.z - 0.38⟩
⟨p.x + 0.38, 0.95, p.z + 0.38⟩ ⟨52, 58, 61⟩ 2 ++
boxMeshMaterial ⟨p.x - 0.16, 0.42, p.z - 0.355⟩
⟨p.x + 0.16, 0.68, p.z - 0.34⟩ ⟨235, 185, 42⟩ 4
else if barrel.blastTime > 0.0 then
let progress := 1.0 - barrel.blastTime / 0.28
let radius := 0.28 + progress * 1.28
boxMeshMaterial ⟨p.x - radius, 0.18, p.z - radius⟩
⟨p.x + radius, 0.58 + radius, p.z + radius⟩ ⟨255, 72, 18⟩ 4 ++
boxMeshMaterial ⟨p.x - radius * 0.55, 0.32, p.z - radius * 0.55⟩
⟨p.x + radius * 0.55, 0.52 + radius, p.z + radius * 0.55⟩ ⟨255, 230, 90⟩ 4
else
boxMeshMaterial ⟨p.x - 0.43, 0.02, p.z - 0.38⟩
⟨p.x + 0.43, 0.13, p.z + 0.38⟩ ⟨39, 34, 31⟩ 3
def pickupMesh (pickup : Pickup) : Array Triangle :=
if !pickup.active then #[]
else
let p := pickup.position
if pickup.kind == 0 then
-- Blue health vial with a bright glass neck.
boxMeshMaterial ⟨p.x - 0.16, 0.05, p.z - 0.16⟩
⟨p.x + 0.16, 0.36, p.z + 0.16⟩ ⟨38, 105, 245⟩ 4 ++
boxMeshMaterial ⟨p.x - 0.08, 0.36, p.z - 0.08⟩
⟨p.x + 0.08, 0.52, p.z + 0.08⟩ ⟨135, 215, 255⟩ 4
else if pickup.kind == 1 then
-- Brass-striped shell box.
boxMeshMaterial ⟨p.x - 0.28, 0.04, p.z - 0.20⟩
⟨p.x + 0.28, 0.28, p.z + 0.20⟩ ⟨102, 58, 34⟩ 1 ++
boxMeshMaterial ⟨p.x - 0.22, 0.28, p.z - 0.15⟩
⟨p.x + 0.22, 0.34, p.z + 0.15⟩ ⟨238, 177, 46⟩ 4
else if pickup.kind == 2 then
-- Chaingun pickup: heavy receiver, twin grips, and a long barrel cluster.
boxMeshMaterial ⟨p.x - 0.36, 0.18, p.z - 0.24⟩
⟨p.x + 0.36, 0.54, p.z + 0.24⟩ ⟨72, 77, 84⟩ 2 ++
boxMeshMaterial ⟨p.x - 0.18, 0.54, p.z - 0.13⟩
⟨p.x + 0.18, 1.12, p.z + 0.13⟩ ⟨155, 162, 164⟩ 2 ++
boxMeshMaterial ⟨p.x - 0.32, 0.02, p.z - 0.18⟩
⟨p.x - 0.12, 0.30, p.z + 0.18⟩ ⟨96, 56, 32⟩ 1 ++
boxMeshMaterial ⟨p.x + 0.12, 0.02, p.z - 0.18⟩
⟨p.x + 0.32, 0.30, p.z + 0.18⟩ ⟨96, 56, 32⟩ 1
else
-- Red access card with a bright brass contact stripe.
boxMeshMaterial ⟨p.x - 0.34, 0.10, p.z - 0.06⟩
⟨p.x + 0.34, 0.62, p.z + 0.06⟩ ⟨218, 38, 45⟩ 4 ++
boxMeshMaterial ⟨p.x - 0.22, 0.20, p.z - 0.075⟩
⟨p.x + 0.22, 0.30, p.z + 0.075⟩ ⟨250, 195, 58⟩ 4 ++
boxMeshMaterial ⟨p.x - 0.20, 0.42, p.z - 0.075⟩
⟨p.x - 0.02, 0.54, p.z + 0.075⟩ ⟨255, 120, 105⟩ 4 ++
boxMeshMaterial ⟨p.x - 0.06, 0.10, p.z - 0.34⟩
⟨p.x + 0.06, 0.62, p.z + 0.34⟩ ⟨218, 38, 45⟩ 4 ++
boxMeshMaterial ⟨p.x - 0.075, 0.20, p.z - 0.22⟩
⟨p.x + 0.075, 0.30, p.z + 0.22⟩ ⟨250, 195, 58⟩ 4
def sceneMesh (state : GameState) : Array Triangle :=
let cleared := state.enemies.all fun enemy => enemy.health == 0 || !enemy.countsForExit
let dynamicWorld := if state.level == 0 then
levelOneWorld ++ doorMesh state.doorOpen ++
doorSwitchMesh state.hasRedKey state.doorOpen ++
exitMeshAt 0.0 6.8 cleared state.levelComplete ++ secretPanelMesh state.secretFound ++
cacheDoorMesh state.cacheDoorOpen ++ cacheSwitchMesh state.cacheFound
else
levelTwoWorld ++ exitMeshAt 6.35 6.45 cleared state.levelComplete
let withBarrels := state.barrels.foldl
(fun triangles barrel => triangles ++ barrelMesh barrel) dynamicWorld
let withEnemies := state.enemies.foldl
(fun triangles enemy => triangles ++ enemyMesh enemy) withBarrels
let withProjectiles := state.projectiles.foldl
(fun triangles projectile => triangles ++ projectileMesh projectile) withEnemies
let withParticles := state.particles.foldl
(fun triangles particle => triangles ++ particleMesh particle) withProjectiles
let withMarks := state.marks.foldl
(fun triangles mark => triangles ++ impactMarkMesh mark) withParticles
state.pickups.foldl
(fun triangles pickup => triangles ++ pickupMesh pickup) withMarks
def illuminateTriangle (camera : Camera) (flash : Nat) (triangle : Triangle) : Triangle :=
if flash == 0 || triangle.material == 4 then triangle
else
let center : Vec3 := ⟨(triangle.a.x + triangle.b.x + triangle.c.x) / 3.0,
(triangle.a.y + triangle.b.y + triangle.c.y) / 3.0,
(triangle.a.z + triangle.b.z + triangle.c.z) / 3.0⟩
let distance := (center.sub camera.position).length
let intensity := Float.ofNat flash / 3.0 * max 0.0 (1.0 - distance / 10.0)
let addChannel (value : Nat) (boost : Float) : Nat :=
min 255 (value + floatToNat (boost * intensity))
{ triangle with color := ⟨addChannel triangle.color.r 82.0,
addChannel triangle.color.g 42.0, addChannel triangle.color.b 12.0⟩ }
def worldToCamera (camera : Camera) (point : Vec3) : Vec3 :=
let relative := point.sub camera.position
let cosYaw := camera.yaw.cos
let sinYaw := camera.yaw.sin
let horizontalX := cosYaw * relative.x - sinYaw * relative.z
let horizontalZ := sinYaw * relative.x + cosYaw * relative.z
let cosPitch := camera.pitch.cos
let sinPitch := camera.pitch.sin
⟨horizontalX,
cosPitch * relative.y - sinPitch * horizontalZ,
sinPitch * relative.y + cosPitch * horizontalZ⟩
def projectView (view : Vec3) : Option ScreenVertex :=
if view.z <= nearPlane then none
else some {
x := Float.ofNat renderWidth / 2.0 + view.x / view.z * focalLength
y := Float.ofNat renderHeight / 2.0 - view.y / view.z * focalLength
depth := view.z
}
def intersectNear (a b : Vec3) : Vec3 :=
let amount := (nearPlane - a.z) / (b.z - a.z)
a.add ((b.sub a).scale amount)
def clipNear (points : List Vec3) : List Vec3 :=
match points with
| [] => []
| first :: _ =>
let nextPoints := points.drop 1 ++ [first]
(points.zip nextPoints).flatMap fun (a, b) =>
let aInside := a.z > nearPlane
let bInside := b.z > nearPlane
if aInside && bInside then [b]
else if aInside && !bInside then [intersectNear a b]
else if !aInside && bInside then [intersectNear a b, b]
else []
structure Framebuffer where
pixels : Array UInt32
depth : Array Float
def packedColor (color : Color) : UInt32 :=
UInt32.ofNat (color.b + color.g * 256 + color.r * 65536 + 255 * 16777216)
def newFramebuffer : Framebuffer := Id.run do
let mut pixels : Array UInt32 := #[]
for y in [0:renderHeight] do
let t := Float.ofNat y / Float.ofNat renderHeight
let sky := if y < renderHeight / 2 then
colorScale ⟨24, 38, 68⟩ (1.15 - t * 0.35)
else ⟨18, 20, 28⟩
for _ in [0:renderWidth] do
pixels := pixels.push (packedColor sky)
return { pixels, depth := Array.replicate (renderWidth * renderHeight) 100000.0 }
def transparentFramebuffer : Framebuffer := {
pixels := Array.replicate (renderWidth * renderHeight) 0
depth := Array.replicate (renderWidth * renderHeight) 100000.0
}
def edge (a b : ScreenVertex) (x y : Float) : Float :=
(x - a.x) * (b.y - a.y) - (y - a.y) * (b.x - a.x)
def putPixel (frame : Framebuffer) (x y : Nat) (depth : Float) (color : Color) : Framebuffer :=
let index := y * renderWidth + x
if depth >= frame.depth[index]! then frame
else
let pixels := frame.pixels.set! index (packedColor color)
{ pixels, depth := frame.depth.set! index depth }
def materialColor (base : Color) (material x y : Nat) : Color :=
match material with
| 1 =>
let stagger := (x + (y / 7) * 8) % 18
if y % 7 == 0 || stagger == 0 then colorScale base 0.42
else if (x + y) % 5 == 0 then colorScale base 0.88
else base
| 2 =>
if x % 14 == 0 then colorScale base 0.38
else if x % 14 == 2 && y % 11 == 2 then colorScale base 1.55
else if y % 13 == 0 then colorScale base 0.72
else base
| 3 =>
if (x + y * 3) % 11 == 0 then colorScale base 0.62
else if (x / 4 + y / 3) % 2 == 0 then colorScale base 0.91
else base
| 4 => colorScale base 1.35
| _ => base
def rasterTriangle (frame : Framebuffer) (a b c : ScreenVertex) (color : Color)
(material : Nat) : Framebuffer := Id.run do
let area := edge a b c.x c.y
-- Clockwise triangles are back faces after the viewport's Y flip.
if area <= 0.001 then return frame
let minX := clampedNat (min a.x (min b.x c.x)) (renderWidth - 1)
let maxX := clampedNat (max a.x (max b.x c.x) + 1.0) (renderWidth - 1)
let minY := clampedNat (min a.y (min b.y c.y)) (renderHeight - 1)
let maxY := clampedNat (max a.y (max b.y c.y) + 1.0) (renderHeight - 1)
let mut output := frame
for y in [minY:maxY + 1] do
for x in [minX:maxX + 1] do
let px := Float.ofNat x + 0.5
let py := Float.ofNat y + 0.5
let w0 := edge b c px py / area
let w1 := edge c a px py / area
let w2 := edge a b px py / area
if w0 >= 0.0 && w1 >= 0.0 && w2 >= 0.0 then
let depth := w0 * a.depth + w1 * b.depth + w2 * c.depth
output := putPixel output x y depth (materialColor color material x y)
return output
def renderProjectedTriangle (frame : Framebuffer) (color : Color) (material : Nat)
(a b c : Vec3) : Framebuffer :=
match projectView a, projectView b, projectView c with
| some a, some b, some c =>
let averageDepth := (a.depth + b.depth + c.depth) / 3.0
let fog := if material == 4 then 1.0 else max 0.28 (min 1.0 (1.15 - averageDepth / 22.0))
rasterTriangle frame a b c (colorScale color fog) material
| _, _, _ => frame
def renderTriangle (camera : Camera) (frame : Framebuffer) (triangle : Triangle) : Framebuffer :=
let clipped := clipNear [worldToCamera camera triangle.a,
worldToCamera camera triangle.b, worldToCamera camera triangle.c]
match clipped with
| [a, b, c] => renderProjectedTriangle frame triangle.color triangle.material a b c
| [a, b, c, d] =>
let frame := renderProjectedTriangle frame triangle.color triangle.material a b c
renderProjectedTriangle frame triangle.color triangle.material a c d
| _ => frame
def drawRect (frame : Framebuffer) (left top width height : Nat) (color : Color) : Framebuffer := Id.run do
let mut output := frame
for y in [top:min renderHeight (top + height)] do
for x in [left:min renderWidth (left + width)] do
output := { output with
pixels := output.pixels.set! (y * renderWidth + x) (packedColor color) }
return output
-- Screen-space overlays intentionally bypass the z-buffer. Besides making HUD
-- pixels unconditional, updating only the color array is substantially cheaper
-- than copying both persistent arrays for every automap sample.
def overlayPixel (frame : Framebuffer) (x y : Nat) (color : Color) : Framebuffer :=
if x >= renderWidth || y >= renderHeight then frame
else { frame with pixels := frame.pixels.set! (y * renderWidth + x) (packedColor color) }
def drawOverlayRect (frame : Framebuffer) (left top width height : Nat)
(color : Color) : Framebuffer := Id.run do
let mut output := frame
for y in [top:min renderHeight (top + height)] do
for x in [left:min renderWidth (left + width)] do
output := overlayPixel output x y color
return output
def victory (state : GameState) : Bool :=
state.enemies.all fun enemy => enemy.health == 0 || !enemy.countsForExit
def mapX (x : Float) : Nat :=
clampedNat (10.0 + (x + 8.0) / 16.0 * 108.0) (renderWidth - 1)
def mapY (z : Float) : Nat :=
clampedNat (4.0 + (8.0 - z) / 16.0 * 54.0) (renderHeight - 12)
def drawMapLine (frame : Framebuffer) (ax ay bxPos byPos : Nat) (color : Color) : Framebuffer := Id.run do
let dx := if ax > bxPos then ax - bxPos else bxPos - ax
let dy := if ay > byPos then ay - byPos else byPos - ay
let steps := max 1 (max dx dy)
let mut output := frame
for step in [0:steps + 1] do
let t := Float.ofNat step / Float.ofNat steps
let x := clampedNat (Float.ofNat ax + (Float.ofNat bxPos - Float.ofNat ax) * t) (renderWidth - 1)
let y := clampedNat (Float.ofNat ay + (Float.ofNat byPos - Float.ofNat ay) * t) (renderHeight - 1)
output := overlayPixel output x y color
return output
def drawMapBox (frame : Framebuffer) (minX minZ maxX maxZ : Float)
(color : Color) : Framebuffer :=
let left := mapX minX
let right := mapX maxX
let top := mapY maxZ
let bottom := mapY minZ
let frame := drawMapLine frame left top right top color
let frame := drawMapLine frame right top right bottom color
let frame := drawMapLine frame right bottom left bottom color
drawMapLine frame left bottom left top color
def drawAutomap (state : GameState) (frame : Framebuffer) : Framebuffer := Id.run do
let wall : Color := ⟨185, 68, 55⟩
let metal : Color := ⟨96, 126, 148⟩
let mut output := drawOverlayRect frame 2 2 (renderWidth - 4) (renderHeight - 12) ⟨9, 11, 16⟩
output := drawMapBox output (-7.7) (-7.7) 7.7 7.7 wall
output := drawMapBox output (-3.8) (-1.0) (-2.2) 1.0 metal
output := drawMapBox output 2.0 1.8 3.8 3.2 wall
if state.cacheFound then
output := drawMapBox output 2.25 2.05 3.55 2.95 metal
let cacheColor : Color := if state.cacheDoorOpen >= 0.86 then ⟨55, 235, 108⟩ else ⟨250, 190, 55⟩
output := drawMapLine output (mapX 2.55) (mapY 1.94) (mapX 3.25) (mapY 1.94) cacheColor
output := drawMapBox output (-0.8) 4.2 0.8 5.4 metal
-- Bulkhead segments and live door aperture.
output := drawMapLine output (mapX (-7.7)) (mapY 0.62) (mapX (-1.28)) (mapY 0.62) metal
output := drawMapLine output (mapX 1.28) (mapY 0.62) (mapX 7.7) (mapY 0.62) metal
let doorColor : Color := if state.doorOpen >= 0.86 then ⟨58, 210, 102⟩
else if state.hasRedKey then ⟨238, 146, 38⟩ else ⟨238, 48, 45⟩
output := drawMapLine output (mapX (-1.26)) (mapY 0.62) (mapX 1.26) (mapY 0.62) doorColor
-- Exit and remaining supplies.
let exitColor : Color := if victory state then ⟨55, 245, 105⟩ else ⟨225, 45, 35⟩
output := drawMapBox output (-0.78) 6.42 0.78 7.18 exitColor
if state.secretFound then
output := drawOverlayRect output (mapX (-7.4)) (mapY (-4.36) - 1) 3 3 ⟨250, 205, 70⟩
for pickup in state.pickups do
let hiddenCacheItem := !state.cacheFound && pickup.position.x > 2.2 &&
pickup.position.x < 3.6 && pickup.position.z > 2.0 && pickup.position.z < 3.0
if pickup.active && !hiddenCacheItem then
let color : Color := if pickup.kind == 0 then ⟨55, 135, 255⟩
else if pickup.kind == 1 then ⟨238, 180, 52⟩
else if pickup.kind == 2 then ⟨210, 215, 220⟩ else ⟨250, 45, 52⟩
output := drawOverlayRect output (mapX pickup.position.x - 1) (mapY pickup.position.z - 1) 3 3 color
for barrel in state.barrels do
let color : Color := if barrel.health > 0 then ⟨58, 175, 82⟩
else if barrel.blastTime > 0.0 then ⟨255, 105, 28⟩ else ⟨64, 49, 40⟩
output := drawOverlayRect output (mapX barrel.position.x - 1)
(mapY barrel.position.z - 1) 3 3 color
-- Alerted monsters are visible as red contacts; corpses remain dark marks.
for enemy in state.enemies do
if enemy.health == 0 then
output := drawOverlayRect output (mapX enemy.position.x) (mapY enemy.position.z) 2 2 ⟨75, 25, 23⟩
else if enemy.alerted then
if enemy.kind == 3 then
output := drawOverlayRect output (mapX enemy.position.x - 2) (mapY enemy.position.z - 2) 5 5 ⟨205, 48, 240⟩
else
output := drawOverlayRect output (mapX enemy.position.x - 1) (mapY enemy.position.z - 1) 3 3 ⟨245, 45, 35⟩
let px := mapX state.camera.position.x
let py := mapY state.camera.position.z
output := drawOverlayRect output (px - 2) (py - 2) 5 5 ⟨245, 235, 90⟩
let lookX := state.camera.position.x + state.camera.yaw.sin * 1.35
let lookZ := state.camera.position.z + state.camera.yaw.cos * 1.35
output := drawMapLine output px py (mapX lookX) (mapY lookZ) ⟨255, 250, 170⟩
return output
def drawAutomapTwo (state : GameState) (frame : Framebuffer) : Framebuffer := Id.run do
let wall : Color := ⟨38, 170, 162⟩
let metal : Color := ⟨103, 116, 142⟩
let mut output := drawOverlayRect frame 2 2 (renderWidth - 4) (renderHeight - 12) ⟨7, 12, 18⟩
output := drawMapBox output (-7.7) (-7.7) 7.7 7.7 wall
output := drawMapBox output (-7.7) (-2.75) 2.0 (-2.20) metal
output := drawMapBox output (-2.0) 0.68 7.7 1.23 metal
output := drawMapBox output (-7.7) 4.0 2.0 4.55 metal
let exitColor : Color := if victory state then ⟨55, 245, 105⟩ else ⟨220, 58, 238⟩
output := drawMapBox output 5.57 6.07 7.13 6.83 exitColor
for pickup in state.pickups do
if pickup.active then
let color : Color := if pickup.kind == 0 then ⟨55, 135, 255⟩ else ⟨238, 180, 52⟩
output := drawOverlayRect output (mapX pickup.position.x - 1) (mapY pickup.position.z - 1) 3 3 color
for barrel in state.barrels do
let color : Color := if barrel.health > 0 then ⟨58, 175, 82⟩
else if barrel.blastTime > 0.0 then ⟨255, 105, 28⟩ else ⟨64, 49, 40⟩
output := drawOverlayRect output (mapX barrel.position.x - 1)
(mapY barrel.position.z - 1) 3 3 color
for enemy in state.enemies do
if enemy.health == 0 then
output := drawOverlayRect output (mapX enemy.position.x) (mapY enemy.position.z) 2 2 ⟨75, 25, 23⟩
else if enemy.alerted then
if enemy.kind == 3 then
output := drawOverlayRect output (mapX enemy.position.x - 2) (mapY enemy.position.z - 2) 5 5 ⟨205, 48, 240⟩
else
output := drawOverlayRect output (mapX enemy.position.x - 1) (mapY enemy.position.z - 1) 3 3 ⟨245, 45, 35⟩
let px := mapX state.camera.position.x
let py := mapY state.camera.position.z
output := drawOverlayRect output (px - 2) (py - 2) 5 5 ⟨245, 235, 90⟩
let lookX := state.camera.position.x + state.camera.yaw.sin * 1.35
let lookZ := state.camera.position.z + state.camera.yaw.cos * 1.35
output := drawMapLine output px py (mapX lookX) (mapY lookZ) ⟨255, 250, 170⟩
return output
def drawShotgun (state : GameState) (frame : Framebuffer) (centerX : Nat) : Framebuffer := Id.run do
let cooling := state.weaponCooldown
let smoking := state.muzzleFlash == 0 && cooling > 0.56
let breachOpen := cooling > 0.18 && cooling < 0.52
let inserting := cooling > 0.18 && cooling < 0.34
let recoil := if state.muzzleFlash > 0 then 4 else if cooling > 0.62 then 2 else 0
let reloadDrop := if breachOpen then 5 else 0
let barrelSpread := if breachOpen then 3 else 0
let stepBob := if state.bobStrength > 0.2 && (state.bobPhase * 2.0).sin > 0.0 then 1 else 0
let gunTop := renderHeight - 25 + recoil + reloadDrop + stepBob
let mut output := frame
-- A brief smoke curl bridges the flash into the mechanical reload.
if smoking then
output := drawRect output (centerX - 5) (gunTop - 13) 2 2 ⟨175, 180, 176⟩
output := drawRect output (centerX - 3) (gunTop - 16) 2 3 ⟨122, 130, 131⟩
output := drawRect output (centerX - 1) (gunTop - 18) 3 2 ⟨78, 86, 91⟩
output := drawRect output (centerX + 5) (gunTop - 12) 2 2 ⟨158, 164, 162⟩
output := drawRect output (centerX + 7) (gunTop - 15) 2 3 ⟨91, 99, 102⟩
output := drawRect output (centerX - 7 - barrelSpread) (gunTop - 5) 6 15 ⟨58, 63, 72⟩
output := drawRect output (centerX + 2 + barrelSpread) (gunTop - 5) 6 15 ⟨58, 63, 72⟩
output := drawRect output (centerX - 6 - barrelSpread) (gunTop - 4) 4 14 ⟨166, 174, 178⟩
output := drawRect output (centerX + 3 + barrelSpread) (gunTop - 4) 4 14 ⟨166, 174, 178⟩
output := drawRect output (centerX - 5 - barrelSpread) (gunTop - 3) 2 12 ⟨54, 59, 67⟩
output := drawRect output (centerX + 4 + barrelSpread) (gunTop - 3) 2 12 ⟨54, 59, 67⟩
output := drawRect output centerX (gunTop - 7) 1 5 ⟨190, 196, 194⟩
output := drawRect output (centerX - 15) (gunTop + 8) 31 11 ⟨66, 71, 79⟩
output := drawRect output (centerX - 13) (gunTop + 9) 27 7 ⟨148, 88, 44⟩
output := drawRect output (centerX - 11) (gunTop + 10) 23 2 ⟨205, 137, 70⟩
output := drawRect output (centerX - 9) (gunTop + 16) 19 8 ⟨70, 39, 25⟩
output := drawRect output (centerX - 14) (gunTop + 8) 3 9 ⟨164, 170, 166⟩
output := drawRect output (centerX + 12) (gunTop + 8) 3 9 ⟨164, 170, 166⟩
if breachOpen then
-- Brass breech faces and two chunky red shells sell the super-shotgun cycle.
output := drawRect output (centerX - 10) (gunTop + 3) 8 5 ⟨115, 77, 32⟩
output := drawRect output (centerX + 3) (gunTop + 3) 8 5 ⟨115, 77, 32⟩
output := drawRect output (centerX - 8) (gunTop + 4) 4 3 ⟨235, 184, 64⟩
output := drawRect output (centerX + 5) (gunTop + 4) 4 3 ⟨235, 184, 64⟩
if inserting then
output := drawRect output (centerX - 9) (gunTop - 3) 5 8 ⟨178, 30, 35⟩
output := drawRect output (centerX + 5) (gunTop - 3) 5 8 ⟨178, 30, 35⟩
output := drawRect output (centerX - 8) (gunTop - 3) 3 2 ⟨248, 196, 70⟩
output := drawRect output (centerX + 6) (gunTop - 3) 3 2 ⟨248, 196, 70⟩
if state.muzzleFlash > 0 then
output := drawRect output (centerX - 12) (gunTop - 13) 25 5 ⟨245, 92, 24⟩
output := drawRect output (centerX - 8) (gunTop - 18) 17 14 ⟨255, 172, 38⟩
output := drawRect output (centerX - 3) (gunTop - 22) 7 20 ⟨255, 239, 135⟩
return output
def drawChaingun (state : GameState) (frame : Framebuffer) (centerX : Nat) : Framebuffer := Id.run do
let recoil := if state.muzzleFlash > 0 then 2 else 0
let stepBob := if state.bobStrength > 0.2 && (state.bobPhase * 2.0).sin > 0.0 then 1 else 0
let gunTop := renderHeight - 30 + recoil + stepBob
let spin := state.bullets % 3
let mut output := frame
-- Three bright barrel jackets with alternating dark bores imply rotation.
output := drawRect output (centerX - 10) (gunTop - 3 + spin) 5 18 ⟨122, 130, 136⟩
output := drawRect output (centerX - 2) (gunTop - 5 + ((spin + 1) % 3)) 5 20 ⟨166, 174, 178⟩
output := drawRect output (centerX + 6) (gunTop - 3 + ((spin + 2) % 3)) 5 18 ⟨122, 130, 136⟩
output := drawRect output (centerX - 9) gunTop 3 14 ⟨35, 39, 46⟩
output := drawRect output (centerX - 1) (gunTop - 2) 3 16 ⟨35, 39, 46⟩
output := drawRect output (centerX + 7) gunTop 3 14 ⟨35, 39, 46⟩
-- Rotary housing, feed block, grips, and the marine's hands.
output := drawRect output (centerX - 17) (gunTop + 13) 35 12 ⟨52, 58, 67⟩
output := drawRect output (centerX - 13) (gunTop + 15) 27 6 ⟨91, 99, 104⟩
output := drawRect output (centerX - 5) (gunTop + 11) 11 12 ⟨31, 34, 40⟩
output := drawRect output (centerX - 20) (gunTop + 19) 7 10 ⟨139, 82, 48⟩
output := drawRect output (centerX + 14) (gunTop + 19) 7 10 ⟨139, 82, 48⟩
output := drawRect output (centerX - 18) (gunTop + 17) 5 4 ⟨204, 137, 84⟩
output := drawRect output (centerX + 14) (gunTop + 17) 5 4 ⟨204, 137, 84⟩
if state.muzzleFlash > 0 then
let flashX := centerX - 7 + spin * 7
output := drawRect output (flashX - 3) (gunTop - 12) 9 6 ⟨255, 112, 20⟩
output := drawRect output flashX (gunTop - 16) 3 12 ⟨255, 232, 105⟩
return output
def secretGlyphs : Array (Array Nat) := #[
#[7, 4, 7, 1, 7], -- S
#[7, 4, 6, 4, 7], -- E
#[7, 4, 4, 4, 7], -- C
#[6, 5, 6, 5, 5], -- R
#[7, 4, 6, 4, 7], -- E
#[7, 2, 2, 2, 2] -- T
]
def drawSecretWord (frame : Framebuffer) (left top : Nat) (color : Color) : Framebuffer := Id.run do
let mut output := frame
for glyphIndex in [0:secretGlyphs.size] do
let rows := secretGlyphs[glyphIndex]!
for row in [0:rows.size] do
for column in [0:3] do
let divisor := 2 ^ (2 - column)
if (rows[row]! / divisor) % 2 == 1 then
output := drawOverlayRect output (left + glyphIndex * 8 + column * 2)
(top + row * 2) 2 2 color
return output
def glyphRows (character : Char) : Array Nat :=
match character with
| 'A' => #[2, 5, 7, 5, 5] | 'B' => #[6, 5, 6, 5, 6]
| 'C' => #[7, 4, 4, 4, 7] | 'D' => #[6, 5, 5, 5, 6]
| 'E' => #[7, 4, 6, 4, 7] | 'F' => #[7, 4, 6, 4, 4]
| 'G' => #[7, 4, 5, 5, 7] | 'H' => #[5, 5, 7, 5, 5]
| 'I' => #[7, 2, 2, 2, 7] | 'J' => #[1, 1, 1, 5, 7]
| 'K' => #[5, 5, 6, 5, 5] | 'L' => #[4, 4, 4, 4, 7]
| 'M' => #[5, 7, 7, 5, 5] | 'N' => #[5, 7, 7, 7, 5]
| 'O' => #[7, 5, 5, 5, 7] | 'P' => #[6, 5, 6, 4, 4]
| 'Q' => #[7, 5, 5, 7, 1] | 'R' => #[6, 5, 6, 5, 5]
| 'S' => #[7, 4, 7, 1, 7] | 'T' => #[7, 2, 2, 2, 2]
| 'U' => #[5, 5, 5, 5, 7] | 'V' => #[5, 5, 5, 5, 2]
| 'W' => #[5, 5, 7, 7, 5] | 'X' => #[5, 5, 2, 5, 5]
| 'Y' => #[5, 5, 2, 2, 2] | 'Z' => #[7, 1, 2, 4, 7]
| '0' => #[7, 5, 5, 5, 7] | '1' => #[2, 6, 2, 2, 7]
| '2' => #[7, 1, 7, 4, 7] | '3' => #[7, 1, 7, 1, 7]
| '4' => #[5, 5, 7, 1, 1] | '5' => #[7, 4, 7, 1, 7]
| '6' => #[7, 4, 7, 5, 7] | '7' => #[7, 1, 2, 2, 2]
| '8' => #[7, 5, 7, 5, 7] | '9' => #[7, 5, 7, 1, 7]
| ':' => #[0, 2, 0, 2, 0] | '-' => #[0, 0, 7, 0, 0]
| '/' => #[1, 1, 2, 4, 4] | '%' => #[5, 1, 2, 4, 5]
| _ => #[0, 0, 0, 0, 0]
def textWidth (value : String) (scale : Nat) : Nat :=
if value.length == 0 then 0 else value.length * 4 * scale - scale
def drawText (frame : Framebuffer) (value : String) (left top scale : Nat)
(color : Color) : Framebuffer := Id.run do
let mut output := frame
for (character, index) in value.toList.zipIdx do
let rows := glyphRows character
for row in [0:5] do
for column in [0:3] do
let divisor := 2 ^ (2 - column)
if (rows[row]! / divisor) % 2 == 1 then
output := drawOverlayRect output (left + index * 4 * scale + column * scale)
(top + row * scale) scale scale color
return output
def drawTextCentered (frame : Framebuffer) (value : String) (top scale : Nat)
(color : Color) : Framebuffer :=
let width := textWidth value scale
drawText frame value ((renderWidth - min renderWidth width) / 2) top scale color
def difficultyName (difficulty : Nat) : String :=
if difficulty == 0 then "EASY" else if difficulty == 2 then "NIGHTMARE" else "HARD"
def drawTitle (state : GameState) (frame : Framebuffer) : Framebuffer := Id.run do
let mut output := drawOverlayRect frame 0 0 renderWidth renderHeight ⟨7, 8, 12⟩
-- Deep red industrial brick backdrop.
for row in [0:9] do
for column in [0:9] do
let offset := if row % 2 == 0 then 0 else 8
let left := column * 16 + offset
output := drawOverlayRect output left (row * 8) 14 6
(if (row + column) % 3 == 0 then ⟨55, 20, 25⟩ else ⟨41, 17, 22⟩)
output := drawOverlayRect output 5 1 (renderWidth - 10) 2 ⟨170, 35, 38⟩
output := drawTextCentered output "LEAN 64" 5 3 ⟨55, 10, 14⟩
output := drawTextCentered output "LEAN 64" 4 3 ⟨245, 70, 44⟩
output := drawTextCentered output "PURE LEAN 4 FPS" 21 1 ⟨195, 198, 190⟩
let modeColor : Color := if state.difficulty == 0 then ⟨75, 210, 125⟩
else if state.difficulty == 2 then ⟨240, 52, 72⟩ else ⟨250, 170, 55⟩
output := drawTextCentered output s!"MODE {difficultyName state.difficulty}" 27 1 modeColor
output := drawTextCentered output "PRESS ENTER" 34 2 ⟨250, 205, 70⟩
output := drawShotgun { state with muzzleFlash := 0 } output (renderWidth / 2)
return output
def drawPause (frame : Framebuffer) : Framebuffer := Id.run do
let mut output := drawOverlayRect frame 19 20 91 31 ⟨10, 11, 16⟩
output := drawOverlayRect output 21 22 87 2 ⟨175, 38, 45⟩
output := drawOverlayRect output 21 47 87 2 ⟨175, 38, 45⟩
output := drawTextCentered output "PAUSED" 27 2 ⟨245, 72, 62⟩
output := drawTextCentered output "P TO RESUME" 41 1 ⟨215, 215, 205⟩
return output
def drawIntermission (state : GameState) (frame : Framebuffer) : Framebuffer := Id.run do
let mut output := drawOverlayRect frame 0 0 renderWidth renderHeight ⟨8, 10, 14⟩
for row in [0:8] do
for column in [0:8] do
let left := column * 18 + (if row % 2 == 0 then 0 else 9)
output := drawOverlayRect output left (row * 9) 16 7
(if (row + column) % 2 == 0 then ⟨21, 28, 28⟩ else ⟨17, 22, 25⟩)
output := drawOverlayRect output 4 3 (renderWidth - 8) 14 ⟨10, 36, 27⟩