-
Notifications
You must be signed in to change notification settings - Fork 9
/
game.hs
1054 lines (912 loc) · 35.6 KB
/
game.hs
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
{----------------------------------------
raycasting game in Haskell
Miloslav Číž, 2017
----------------------------------------}
import System.IO
import System.Timeout
import Data.Fixed
import Data.Char
import Debug.Trace
import Control.Concurrent
import System.CPUTime
import Data.List
-- key mapping:
keyForward = 'w'
keyBackward = 's'
keyTurnLeft = 'a'
keyTurnRight = 'd'
keyStrafeLeft = 'q'
keyStrafeRight = 'e'
keyFire = 'p'
keyWeapon1 = '1'
keyWeapon2 = '2'
keyWeapon3 = '3'
keyQuit = 'x'
disableAI = False -- for debug
frameDelayMs = 16 -- in milliseconds
frameDelayUs = frameDelayMs * 1000 -- in microseconds
stepLength = 0.1
zombieStepLength = 0.01
demonStepLength = 0.09
rotationStep = 0.06
mapSize = (15,15)
infoBarHeight = 5
screenSize = (150,45)
viewSize = ( (fst screenSize) , (snd screenSize) - infoBarHeight )
fieldOfView = pi / 2
focalLength = 0.5
maxRaycastIterations = 20
spriteSize = (15,10)
spriteScale = fromIntegral (snd viewSize) / fromIntegral (snd spriteSize) * 2
totalMapSquares = (fst mapSize) * (snd mapSize)
rayAngleStep = fieldOfView / fromIntegral (fst viewSize)
infinity = 1.0 / 0.0
animationFrameStep = 4
spriteDepthBias = 1 -- so that sprites don't disappear in walls
backgroundChar = ' '
transparentChar = 'X' -- marks transparency in sprites
emptyLines = 15 -- number of empty lines added before each rendered frame
emptyLineString = ['\n' | i <- [1..emptyLines]]
recomputeAIin = 64
aimAccuracy = 0.32 -- this constant is used in fire function to determine if a monster was hit
knifeAttackDistance = 1.5
weaponDamage = 20 -- damage of all weapons
fireRateKnife = 6
fireRateGun = 10
fireRateUzi = 4
monsterHealthZombie = 100 -- initial health amounts
monsterHealthDemon = 50
weaponSpritePosition = ((fst viewSize) - (fst viewSize) `div` 3,1 + snd viewSize - snd spriteSize)
type Position2D = (Double,Double) -- in squares, starting top left
data MonsterType = Zombie | Demon deriving(Show, Eq)
data Normal = NormalNorth | NormalWest | NormalSouth | NormalEast deriving(Show, Eq)
data Weapon = Knife | Gun | Uzi deriving(Show, Eq)
data MapSquare = SquareEmpty | SquareWall deriving(Show, Eq)
sE = SquareEmpty -- short aliases
sW = SquareWall
gameMap1 :: [MapSquare]
gameMap1 =
[
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sW,sW,sE,sE,
sE,sE,sE,sW,sE,sE,sW,sE,sE,sE,sE,sE,sE,sE,sE,
sE,sE,sE,sW,sE,sE,sW,sE,sE,sE,sE,sE,sE,sE,sE,
sE,sE,sE,sW,sE,sE,sW,sE,sE,sE,sE,sE,sE,sE,sE,
sE,sE,sE,sW,sE,sE,sW,sE,sE,sE,sE,sE,sW,sE,sW,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sW,sE,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sW,sW,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sW,sW,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,
sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE,sE
]
type SpriteType = Int
spriteNone = -1
spriteTree = 0
spriteZombie = 1 -- animated, 2 frames
-- skip
spriteDemon = 3 -- animated, 2 frames
-- skip
spriteGun = 5
spriteUzi = 6
spriteMedkit = 7
-- skip
spriteFPKnife = 8 -- animated, 2 frames
-- skip
spriteFPGun = 10 -- animated, 2 frames
-- skip
spriteFPUzi = 12 -- animated, 2 frames
grayscaleMap = ['M','$','o','?','/','!',';',':','\'','.','-'] -- characters sorted by brigtness
animatedSpriteIds = [1,3,8,10,12] -- list of sprite IDs that are animated
spriteList =
[
[ -- 0
"XXXXXXXXXXXXXXX",
"XXXX/'''''\\XXXX",
"XX/' ''\\X",
"X| O |",
"X| O |",
"XX\\_ O /X",
"XXXX\\_ ____/XX",
"XXXXXX||XXXXXXX",
"XXXXXX||XXXXXXX",
"XX-=#/__\\#=-XXX"],
[ -- 1
"XXXXXXXXXXXXXXX",
"XXXXX/```\\XXXXX",
"XXXXX[o.o]XXXXX",
"XXXXX\\II]/XXXXX",
"XXX/^^````(III)",
"X(III] /XXXX",
"XXXX(_____)XXXX",
"XXXX( _ )XXXX",
"XXXX| |X\\ |XXXX",
"X--(__]=|__)--X"],
[ -- 2
"XXXXXXXXXXXXXXXX",
"XXXXX/```\\XXXXX",
"XXXXX[o.o]XXXXX",
"XXXXX\\[II/XXXXX",
"X(III]```^^\\XXX",
"XXXX\\ (III)X",
"XXXX|_____)XXXX",
"XXXX( _ )XXXX",
"XXXX/ | | |XXXX",
"X--(__/=|__)--X"],
[ -- 3
"X|\\X-''`''-X/|X",
"X\\;` `;/X",
"X/ ,_ _, \\X",
"X( \\0) , (0/ )X",
"XX\\_: ^ :_/XX",
"XXXX\\ |^| /XXXX",
"XXXX| [_] |XXXX",
"XXXXX\\___/XXXXX",
"XXXXXXXXXXXXXXX",
"X--==#####==--X"],
[ -- 4
"|\\XX-'```'-XX/|",
"\\ ;` `; /",
"X/ ,_ \\ / _, \\X",
"X( (0) (0) )X",
"XX\\_ A _/XX",
"XXXX) ___ (XXXX",
"XXXX( ' ' )XXXX",
"XXXXX'---'XXXXX",
"XXXXXXXXXXXXXXX",
"X--==#####==--X"],
[ -- 5
"XXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXX",
"Xl_/_\\\\\\^^^^^^]",
"X/ P\\_______/X",
"/ ({_)XXXXXX",
"| /XXXXXXXXXX",
"(___)XXXXXXXXXX",
"XXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXX",
"X--==#####==--X"],
[ -- 6
"|^\\XXXXXXXXX/^|",
"| ^^^^^^^^^ _|",
"| -o-- [[[(_o)",
"|___________ |",
"XX\\ __|X[_]\\_|",
"XX/ /_]XX| |XXX",
"X/ /XXXXX| |XXX",
"|_/XXXXXX|_|XXX",
"XXXXXXXXXXXXXXX",
"--===#####===--"],
[ -- 7
"XXXXXXXXXXXXXXX",
"XX/`````````\\XX",
"X| .. _ .. |X",
"X| _| |_ |X",
"X| [ + ] |X",
"X| `|_|` |X",
"X| .. .. |X",
"XX\\_________/XX",
"XXXXXXXXXXXXXXX",
" --==#####==-- "],
[ -- 8
"XXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXX",
"XXXXXXXXX/|XXXX",
"XXXXXXXX/ |XXXX",
"XXXXXXX( ,|XXXX",
"XXXXXXX| |<XXXX",
"XXXXXXX| |<XXXX",
"XXXXXXX| |<XXXX",
"XXXXXXX| |<XXXX"],
[ -- 9
"XXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXX",
"XX|\\XXXXXXXXXXX",
"XX| \\XXXXXXXXXX",
"XX( ,\\XXXXXXXXX",
"XXX\\ \\LXXXXXXXX",
"XXXX\\ \\LXXXXXXX",
"XXXXX\\ \\LX/\\XXX",
"XXXXXX\\ \\V /XXX",
"XXXX\\^^ , \\XXX"],
[ -- 10
"X/^^\\XXXXXXXXXX",
"[:\\ Y\\XXXXXXXXX",
"X\\:\\__`\\XXXXXXX",
"XX\\:\\ \\ \\XXXXXX",
"XXX\\:\\` `\\XXXX",
"XXX/\\:\\____\\XXX",
"XX(_|::\\ P |\\XX",
"XX( \\_:| | )X",
"XX(\\_ )-___/ /X",
"XXX\\ \\ |XX"],
[ -- 11
"XWWWWWWWXXXXXXX",
"WW/^^\\WWXXXXXXX",
"WW|:) Y\\WXXXXXX",
"XW(:( _ \\XXXXXX",
"XXX|:( \\ \\XXXXX",
"XXX\\::)` \\XXXX",
"XXXX\\:( \\XXX",
"XXXX(::\\____|\\X",
"XXX/|::| P | |",
"XX( \\_| | /"],
[ -- 12
"X[\\^^\\XXXXXXXXX",
"X|:\\__\\XXXXXXXX",
"C|::|__|XXXXXXX",
"X|: :\\ \\XXXXXXX",
"X|: :\\ \\XXXXXX",
"X|::\\ :\\ `\\XXXX",
"XX\\_:\\ :\\ \\XXX",
"XXX|\\:\\ :\\__\\XX",
"XX/|:\\:O :\\ `\\",
"X( |:|\\:::|^^^|"],
[ -- 13
"WXXXXXXXXXXXXXX",
"WWXXXXXXXXXXXXX",
"WWW[\\^^\\XXXXXXX",
"XWW|:\\__\\XXXXXX",
"XWC|::|__|XXXXX",
"XXW|: :\\ \\XXXXX",
"XXX|: :\\ \\XXXX",
"XXX|::\\ :\\ `\\XX",
"XXXX\\_:\\ :\\ \\X",
"XXXX/ \\:\\ :\\__\\"
]
]
data GameState = GameState
{
playerPos :: Position2D,
playerRot :: Double, -- rotation in radians, CCW, 0 = facing right
frameNumber :: Int,
currentWeapon :: Weapon,
currentLevel :: Int,
currentScore :: Int,
gameMap :: [MapSquare],
monsters :: [Monster], -- list of monsters
sprites :: [Sprite], -- list of sprites
fireCountdown :: Int -- counter for implementing different fire rates
} deriving (Show)
data Sprite = Sprite
{
spriteType :: SpriteType,
spritePos :: Position2D
} deriving (Show)
data Monster = Monster
{
monsterType :: MonsterType,
monsterPos :: Position2D,
health :: Int,
countdownAI :: Int,
monsterRot :: Double
} deriving (Show)
newMonster :: MonsterType -> Position2D -> Monster
newMonster monsterType initialPosition = Monster
{
monsterType = monsterType,
monsterPos = initialPosition,
health =
if monsterType == Zombie
then monsterHealthZombie
else monsterHealthDemon,
countdownAI = 0,
monsterRot = 0
}
initialGameState :: GameState
initialGameState = GameState
{
playerPos = (7.5,8.5),
playerRot = 0.0,
frameNumber = 0,
currentWeapon = Knife,
currentLevel = 1,
currentScore = 0,
gameMap = gameMap1,
monsters =
[
newMonster Zombie (6,7),
newMonster Demon (8,5),
newMonster Demon (10,2),
newMonster Demon (2,10)
],
sprites = [],
fireCountdown = 0
}
----------------------------------------------- Functions for 3-item tuples.
fst3 (x, _, _) = x
snd3 (_, x, _) = x
thd3 (_, _, x) = x
----------------------------------------------- Splits given list to chunks of size n.
splitChunks _ [] = []
splitChunks n list = first : (splitChunks n rest)
where
(first,rest) = splitAt n list
----------------------------------------------- Fills given string with spaces to given length.
toLength :: String -> Int -> String
toLength what outputLength =
what ++ [' ' | i <- [1.. outputLength - length(what)]]
----------------------------------------------- Alternative version of trace for debugging.
trace2 :: a -> (a -> String) -> a
trace2 what func =
trace (func what) what
----------------------------------------------- Ensures given values is in given interval by clamping it.
clamp :: (Ord a) => a -> (a, a) -> a
clamp value (minimum, maximum) =
(min maximum . max minimum) value
----------------------------------------------- Adds two 2-item pair tuples, itemwise.
addPairs :: (Num a) => (Num b) => (a, b) -> (a, b) -> (a, b)
addPairs (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
-----------------------------------------------
substractPairs :: (Num a) => (Num b) => (a, b) -> (a, b) -> (a, b)
substractPairs (x1, y1) (x2, y2) = (x1 - x2, y1 - y2)
----------------------------------------------- Applies floor function to both items of a pair.
floorPair :: (RealFrac a) => (RealFrac b) => (a, b) -> (Int, Int)
floorPair couple =
(floor (fst couple),floor (snd couple))
----------------------------------------------- Makes the angle safe for tan function.
tanSafeAngle :: Double -> Double
tanSafeAngle angle
| mod' angle (pi / 2) == 0.0 = angle + 0.00001
| otherwise = angle
-----------------------------------------------
vectorAngle :: Position2D -> Double
vectorAngle vector =
atan2 (-1 * (snd vector)) (fst vector)
----------------------------------------------- Returns the result of angle1 - angle2 closest to 0.
angleAngleDifference :: Double -> Double -> Double
angleAngleDifference angle1 angle2
| difference > pi = difference - 2 * pi
| otherwise = difference
where
difference = angleTo02Pi (angle1 - angle2)
-----------------------------------------------
angleTo02Pi :: Double -> Double
angleTo02Pi angle =
mod' angle (2 * pi)
----------------------------------------------- Gets distance of two points.
pointPointDistance :: Position2D -> Position2D -> Double
pointPointDistance point1 point2 =
let
dx = (fst point1) - (fst point2)
dy = (snd point1) - (snd point2)
in
sqrt (dx * dx + dy * dy)
----------------------------------------------- Converts 2D map coords to 1D array coords.
mapToArrayCoords :: (Int, Int) -> Int
mapToArrayCoords coords =
snd coords * (fst mapSize) + fst coords
----------------------------------------------- Converts 1D array coords to 2D map coords.
arrayToMapCoords :: Int -> (Int, Int)
arrayToMapCoords coords =
(mod coords (fst mapSize),div coords (fst mapSize))
----------------------------------------------- Computes an intersection point of two lines.
lineLineIntersection :: Position2D -> Double -> Position2D -> Double -> Position2D
lineLineIntersection (x1,y1) angle1 (x2,y2) angle2 = (x,y)
where
tan1 = tan (tanSafeAngle angle1)
tan2 = tan (tanSafeAngle angle2)
x = (y2 - tan2 * x2 - y1 + tan1 * x1) / (tan1 - tan2)
y = if abs tan1 < abs tan2 then tan1 * x + (y1 - tan1 * x1) else tan2 * x + (y2 - tan2 * x2)
----------------------------------------------- Maps normalized intensity to ASCII character.
intensityToChar :: Double -> Char
intensityToChar intensity =
let
safeIndex = clamp (floor (intensity * fromIntegral (length grayscaleMap))) (0,(length grayscaleMap) - 1)
in
grayscaleMap !! safeIndex
----------------------------------------------- Returns an intensity addition (possibly negative) cause by distance.
distanceToIntensity :: Double -> Double
distanceToIntensity distance =
(min (distance / 7.0) 1.0) * (-0.3)
----------------------------------------------- Maps worldspace distance to normalized screenspace size (caused by perspective).
distanceToSize :: Double -> Double
distanceToSize distance =
1.0 / (distance + 1.0)
----------------------------------------------- Projects sprites to screen space, returns a list representing screen, each
-- pixel has (sprite id,sprite x pixel,distance), sprite id = -1 => empty.
projectSprites :: GameState -> [(SpriteType,Int,Double)]
projectSprites gameState =
let
-- project all sprites to screenspace first:
screenspaceSprites = -- [(sprite id,sprite x pixel,distance)]
[
(
spriteType sprite,
0.5 + -- sprite center in screenspace, normalized
(
angleAngleDifference (playerRot gameState) ( vectorAngle ( fst (spritePos sprite) - fst (playerPos gameState), snd (spritePos sprite) - snd (playerPos gameState) ) )
)
/ fieldOfView
,
(pointPointDistance (playerPos gameState) (spritePos sprite)) - spriteDepthBias -- sprite distance
)
| sprite <- (sprites gameState)
]
-- projects one sprite (sprite,x,y) to a screen list [(sprite id,sprite x pixel,distance)]
projectOneSprite :: (SpriteType,Double,Double) -> [(SpriteType,Int,Double)] -> [(SpriteType,Int,Double)]
projectOneSprite = -- projects a single sprite to screen list
(
\spriteInfo screenList ->
let
spritePos = (snd3 spriteInfo) * fromIntegral ((length screenList) - 1)
spriteLength = (distanceToSize (thd3 spriteInfo)) * fromIntegral (fst spriteSize) * spriteScale
spriteInterval = ( floor (spritePos - spriteLength / 2) , floor (spritePos + spriteLength / 2) )
in
map
(
\item ->
if (snd item) >= (fst spriteInterval) && (snd item) <= (snd spriteInterval)
then
(
(fst3 spriteInfo),
round $ ((fromIntegral ( (snd item) - (fst spriteInterval) )) / spriteLength) * fromIntegral ((fst spriteSize) - 1),
(thd3 spriteInfo)
)
else (fst item)
)
(zip screenList [0..])
)
emptyScreenlList = [(spriteNone,0,infinity) | i <- [0..(fst viewSize) - 1]]
in
foldl
(
\screenList1 screenList2 ->
map
(
\itemPair -> -- compare depths
if (thd3 (fst itemPair)) <= (thd3 (snd itemPair))
then (fst itemPair)
else (snd itemPair)
)
(zip screenList1 screenList2)
)
emptyScreenlList
[projectOneSprite spriteItem emptyScreenlList | spriteItem <- screenspaceSprites]
----------------------------------------------- Samples given sprite.
sampleSprite :: SpriteType -> (Int,Int) -> Int -> Char
sampleSprite spriteId coordinates animationFrame =
let
safeCoords =
(
clamp (fst coordinates) (0,(fst spriteSize) - 1),
clamp (snd coordinates) (0,(snd spriteSize) - 1)
)
in
((spriteList !! (spriteId + animationFrame)) !! (snd safeCoords)) !! (fst safeCoords)
----------------------------------------------- Gets animation frame for current frame number.
animationFrameForSprite :: SpriteType -> Int -> Int
animationFrameForSprite spriteId frameNumber
| ((frameNumber `div` animationFrameStep) `mod` 2 == 1) && (spriteId `elem` animatedSpriteIds) = 1
| otherwise = 0
----------------------------------------------- Renders the 3D player view (no bar or weapon) into String.
render3Dview :: [(Double, Normal)] -> [(SpriteType,Int,Double)] -> Int -> Int -> String
render3Dview wallMap spriteMap height frameNumber =
let
middle = div height 2 + 1 -- middle line of the view
heightDouble = (fromIntegral height)
in
concat
[
let
distanceFromMiddle = middle - i
absDistanceFromMiddle = abs distanceFromMiddle
in
map
(
\item ->
let
normal = (snd (fst item))
distance = (fst (fst item))
columnHeight = floor ((distanceToSize distance) * heightDouble)
spriteInfo = (snd item)
wallSample =
if absDistanceFromMiddle < columnHeight
then
if normal == NormalNorth then intensityToChar $ 0.25 + distanceToIntensity distance
else if normal == NormalEast then intensityToChar $ 0.50 + distanceToIntensity distance
else if normal == NormalSouth then intensityToChar $ 0.75 + distanceToIntensity distance
else intensityToChar $ 1.00 + distanceToIntensity distance
else backgroundChar
spriteHalfHeight = floor ( spriteScale * distanceToSize (thd3 spriteInfo) * fromIntegral (snd spriteSize) / 2 )
sampleX = snd3 spriteInfo
sampleY = round (((1 - (1 + (fromIntegral distanceFromMiddle) / (fromIntegral spriteHalfHeight)) / 2)) * fromIntegral ((snd spriteSize) - 1))
spriteSample = sampleSprite (fst3 spriteInfo) (sampleX,sampleY) (animationFrameForSprite (fst3 spriteInfo) frameNumber)
in
if (thd3 spriteInfo) >= distance -- is wall closer than sprite?
then wallSample
else -- sprite is closer
if absDistanceFromMiddle <= spriteHalfHeight
then
if spriteSample /= transparentChar
then spriteSample
else wallSample
else wallSample
)
(zip wallMap spriteMap) ++ "\n"
| i <- [1..height]
]
----------------------------------------------- Renders the lower info bar to String.
renderInfoBar :: GameState -> String
renderInfoBar gameState =
let
separatorPositions = [0,15,31,63]
separator = "+" ++ [if i `elem` separatorPositions then '+' else '~' | i <- [3..(fst viewSize)]] ++ "+"
emptyLine = "|" ++ [if i `elem` separatorPositions then '|' else ' ' | i <- [3..(fst viewSize)]] ++ "|\n"
infoLine = "| level: " ++ (toLength (show (currentLevel gameState)) 3) ++ "| score: " ++ (toLength (show (currentScore gameState)) 6) ++ "| health: 100/100 ########## | ammo: 100/100"
in
separator ++ "\n" ++
emptyLine ++
(toLength infoLine ((fst screenSize) - 1)) ++ "|\n" ++
emptyLine ++
separator
----------------------------------------------- Overlays a string image over another
overlay :: String -> String -> (Int,Int) -> (Int,Int) -> (Int,Int) -> Char -> String
overlay background foreground position backgroundResolution foregroundResolution transparentChar =
let
backgroundLines = splitChunks (fst backgroundResolution) background
(firstLines,restLines) = splitAt (snd position) backgroundLines
(secondLines,thirdLines) = splitAt (snd foregroundResolution) restLines
foregroundLines =
[
take (fst position) (snd item) ++
[
if (fst chars) == transparentChar then (snd chars) else (fst chars)
| chars <- zip (fst item) ( take (fst foregroundResolution) (drop (fst position) (snd item)))
] ++
drop (fst position + fst foregroundResolution) (snd item)
| item <- zip (splitChunks (fst foregroundResolution) foreground) secondLines
]
in
concat (firstLines) ++
concat (foregroundLines) ++
concat (thirdLines)
-----------------------------------------------
weaponFireRate :: Weapon -> Int
weaponFireRate weaponId
| weaponId == Knife = fireRateKnife
| weaponId == Gun = fireRateGun
| weaponId == Uzi = fireRateUzi
| otherwise = 1
-----------------------------------------------
weaponSprite :: Weapon -> Int
weaponSprite weaponId
| weaponId == Knife = spriteFPKnife
| weaponId == Gun = spriteFPGun
| weaponId == Uzi = spriteFPUzi
| otherwise = spriteFPKnife
----------------------------------------------- Renders the game in 3D.
renderGameState :: GameState -> String
renderGameState gameState =
let
wallDrawInfo = castRays gameState
gunSprite = weaponSprite (currentWeapon gameState) +
if (fireCountdown gameState) /= 0
then 1
else 0
in
(
overlay
(render3Dview wallDrawInfo (projectSprites gameState) (snd viewSize) (frameNumber gameState))
(concat (spriteList !! gunSprite))
weaponSpritePosition
(addPairs viewSize (1,0))
spriteSize
transparentChar
)
++
renderInfoBar gameState
----------------------------------------------- Renders the game state into string, simple version.
renderMap :: GameState -> String
renderMap gameState =
concat
(
map
(
\square ->
(
if mod (snd square) (fst mapSize) == 0
then "\n"
else ""
)
++
(
if floor (fst (playerPos gameState)) == fst (arrayToMapCoords (snd square)) &&
floor (snd (playerPos gameState)) == snd (arrayToMapCoords (snd square))
then
case round (4.0 * (playerRot gameState) / pi) of
0 -> "->"
1 -> "/^"
2 -> "|^"
3 -> "^\\"
4 -> "<-"
5 -> "./"
6 -> ".|"
7 -> "\\."
8 -> "->"
else if fst square == SquareEmpty
then " "
else "[]"
)
) (zip (gameMap gameState) [0..])
)
----------------------------------------------- Gets the distance from projection origin to projection plane.
distanceToProjectionPlane :: Double -> Double -> Double
distanceToProjectionPlane focalDistance angleFromCenter =
focalDistance * (cos angleFromCenter)
----------------------------------------------- Casts all rays needed to render player's view, returns a list of ray cast results.
castRays :: GameState -> [(Double, Normal)]
castRays gameState =
[
let
rayDirection = (playerRot gameState) + fieldOfView / 2 - (fromIntegral x) * rayAngleStep
rayResult = castRay gameState (playerPos gameState) (floorPair (playerPos gameState)) rayDirection maxRaycastIterations
in
(
max
( (fst rayResult) - (distanceToProjectionPlane focalLength (abs $ (playerRot gameState) - rayDirection)) )
0.0,
snd rayResult
)
| x <- [0..(fst viewSize) - 1]
]
----------------------------------------------- Casts a ray and returns an information (distance, normal) about a wall it hits.
castRay :: GameState -> Position2D -> (Int, Int) -> Double -> Int -> (Double, Normal)
castRay gameState rayOrigin square rayDirection maxIterations =
let
squareCoords = floorPair rayOrigin
angle = angleTo02Pi rayDirection
in
if (mapSquareAt gameState square) /= SquareEmpty || maxIterations == 0
then (0,NormalNorth)
else
let
squareCastResult = castRaySquare square rayOrigin angle
recursionResult = castRay gameState (fst squareCastResult) (addPairs square (snd squareCastResult)) angle (maxIterations - 1)
in
(
pointPointDistance rayOrigin (fst squareCastResult) + (fst recursionResult),
if (fst recursionResult) /= 0
then (snd recursionResult)
else
case (snd squareCastResult) of
(1,0) -> NormalEast
(0,1) -> NormalSouth
(-1,0) -> NormalWest
_ -> NormalNorth
)
----------------------------------------------- Casts a ray inside a single square, returns (intersection point with square bounds,next square offset)
castRaySquare :: (Int, Int) -> Position2D -> Double -> (Position2D,(Int, Int))
castRaySquare squareCoords rayPosition rayAngle =
let
angle = 2 * pi - rayAngle
boundX = (fst squareCoords) + if angle < (pi / 2) || angle > (pi + pi / 2) then 1 else 0
boundY = (snd squareCoords) + if angle < pi then 1 else 0
intersection1 = lineLineIntersection rayPosition angle (fromIntegral boundX,fromIntegral (snd squareCoords)) (pi / 2)
intersection2 = lineLineIntersection rayPosition angle (fromIntegral (fst squareCoords),fromIntegral boundY) 0
in
if (pointPointDistance rayPosition intersection1) <= (pointPointDistance rayPosition intersection2)
then (intersection1,(if boundX == (fst squareCoords) then -1 else 1,0))
else (intersection2,(0,if boundY == (snd squareCoords) then -1 else 1))
----------------------------------------------- Returns map square at given coords.
mapSquareAt :: GameState -> (Int, Int) -> MapSquare
mapSquareAt gameState coords
| (fst coords) < (fst mapSize) && (fst coords) >= 0 && (snd coords) < (snd mapSize) && (snd coords) >= 0 = (gameMap gameState) !! (mapToArrayCoords coords)
| otherwise = SquareWall
----------------------------------------------- Checks if given player position is valid (collisions).
positionIsWalkable :: GameState -> Position2D -> Bool
positionIsWalkable gameState position =
(mapSquareAt gameState (floorPair position)) == SquareEmpty
-----------------------------------------------
monsterSprite :: MonsterType -> Int
monsterSprite monsterId
| monsterId == Zombie = spriteZombie
| otherwise = spriteDemon
-----------------------------------------------
monsterStepLength :: MonsterType -> Double
monsterStepLength monsterId
| monsterId == Zombie = zombieStepLength
| otherwise = demonStepLength
----------------------------------------------- Creates sprites and places them on the map depending on current state of things.
updateSprites :: GameState -> GameState
updateSprites gameState =
gameState
{
sprites =
[
Sprite {spriteType = monsterSprite (monsterType monster), spritePos = (monsterPos monster)}
| monster <- (monsters gameState)
]
}
-----------------------------------------------
monsterAI :: GameState -> Monster -> Monster
monsterAI gameState whatMonster =
let
rotation =
if (monsterType whatMonster) == Zombie
then vectorAngle $ substractPairs (playerPos gameState) (monsterPos whatMonster) -- zombie walks towards the player
else
if (countdownAI whatMonster) == 0
then angleTo02Pi ((fst (monsterPos whatMonster)) + (snd (monsterPos whatMonster)) + (fromIntegral (frameNumber gameState)) / 100.0)
else (monsterRot whatMonster)
in
whatMonster
{
monsterPos = moveWithCollision gameState (monsterPos whatMonster) (monsterRot whatMonster) (monsterStepLength (monsterType whatMonster))
}
{
monsterRot = rotation
}
{
countdownAI =
if (countdownAI whatMonster) <= 0
then recomputeAIin
else (countdownAI whatMonster) - 1
}
----------------------------------------------- Runs the AI for each monster, updating their positions etc.
updateMonsters :: GameState -> GameState
updateMonsters gameState =
if disableAI
then
gameState
else
gameState
{
monsters =
[
monsterAI gameState monster
| monster <- filter (\m -> (health m) > 0) (monsters gameState) -- health = 0 => monster is dead, filter it out
]
}
-----------------------------------------------
moveWithCollision :: GameState -> Position2D -> Double -> Double -> Position2D
moveWithCollision gameState positionFrom angle distance =
let
plusX = cos angle * distance
plusY = -1 * (sin angle * distance)
in
(
(fst positionFrom) +
if positionIsWalkable gameState ((fst positionFrom) + plusX,snd positionFrom)
then plusX
else 0,
(snd positionFrom) +
if positionIsWalkable gameState (fst positionFrom,(snd positionFrom) + plusY)
then plusY
else 0
)
----------------------------------------------- Moves player by given distance in given direction, with collisions.
movePlayerInDirection :: GameState -> Double -> Double -> GameState
movePlayerInDirection previousGameState angle distance =
let
plusX = cos angle * distance
plusY = -1 * (sin angle * distance)
in
previousGameState
{
playerPos =
(
fst (playerPos previousGameState) +
if positionIsWalkable previousGameState ((fst (playerPos previousGameState)) + plusX,snd (playerPos previousGameState))
then plusX
else 0,
snd (playerPos previousGameState) +
if positionIsWalkable previousGameState (fst (playerPos previousGameState),(snd (playerPos previousGameState)) + plusY)
then plusY
else 0
)
}
----------------------------------------------- Moves the player forward by given distance, with collisions.
movePlayerForward :: GameState -> Double -> GameState
movePlayerForward previousGameState distance =
previousGameState
{
playerPos = moveWithCollision previousGameState (playerPos previousGameState) (playerRot previousGameState) distance
}
----------------------------------------------- Strafes the player left by given distance (with collisions).
strafePlayer :: GameState -> Double -> GameState
strafePlayer previousGameState distance =
previousGameState
{
playerPos = moveWithCollision previousGameState (playerPos previousGameState) (angleTo02Pi ((playerRot previousGameState) + pi / 2)) distance
}
-----------------------------------------------
fire :: GameState -> GameState
fire gameState =
if (fireCountdown gameState) == 0
then
gameState
{
fireCountdown = weaponFireRate (currentWeapon gameState)
}
{
monsters =
filter
(\m -> (health m) > 0)
(
map
(\m ->
let
angleDifference = abs $ angleAngleDifference (playerRot gameState) (vectorAngle $ substractPairs (monsterPos m) (playerPos gameState))
monsterDistance = pointPointDistance (playerPos gameState) (monsterPos m)
angleRange = 1.0 / (monsterDistance + aimAccuracy)
wallDistance = fst $ castRay gameState (playerPos gameState) (floorPair (playerPos gameState)) (playerRot gameState) maxRaycastIterations
maxDistance = if (currentWeapon gameState) == Knife then knifeAttackDistance else infinity
hit = angleDifference < angleRange / 2 && monsterDistance <= wallDistance && monsterDistance <= maxDistance
in
m
{
health = if hit then (health m) - weaponDamage else (health m)
}
)
(monsters gameState)
)
}
else gameState
----------------------------------------------- Computes the next game state.
nextGameState :: GameState -> Char -> GameState
nextGameState previousGameState inputChar =
let
newGameState =
case () of _ -- case with expressions hack
| inputChar == keyForward -> movePlayerForward previousGameState stepLength
| inputChar == keyBackward -> movePlayerForward previousGameState (-1 * stepLength)
| inputChar == keyTurnLeft -> previousGameState { playerRot = angleTo02Pi ((playerRot previousGameState) + rotationStep) }
| inputChar == keyTurnRight -> previousGameState { playerRot = angleTo02Pi ((playerRot previousGameState) - rotationStep) }
| inputChar == keyStrafeLeft -> strafePlayer previousGameState stepLength
| inputChar == keyStrafeRight -> strafePlayer previousGameState (-1 * stepLength)
| inputChar == keyFire -> fire previousGameState
| inputChar == keyWeapon1 -> previousGameState { currentWeapon = Knife }
| inputChar == keyWeapon2 -> previousGameState { currentWeapon = Gun }
| inputChar == keyWeapon3 -> previousGameState { currentWeapon = Uzi }
| otherwise -> previousGameState
in
(
updateMonsters $ updateSprites newGameState
)
{
frameNumber = (frameNumber newGameState) + 1
}