-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWOLF3D-VZ200-NoMusic.asm
1655 lines (1444 loc) · 37.8 KB
/
WOLF3D-VZ200-NoMusic.asm
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
; WOLFENSTEIN FPS CODE. by Lawrence Kesteloot. e: lk@teamten.com
; POrted from original TRS80 model 1 to VZ by silly dave.
; https://github.com/lkesteloot/trs80/tree/master/apps/wolf
; https://en.wikipedia.org/wiki/Wolfenstein_3D$Development
;
; Found src code for trs80 on Lawrence github for the TRS80 Model 1.
; Grabbed it and ran with it.
; Q - forward Z - Show Map
; A - back X - Menu
; M - left C - Credits
; , - right
; s - straffe left
; d - straffe right
;
;
;
; WALL
; colour
; Number Colour Chr$()
; ------------------------------
; 0 green 143
; 16 yellow 159
; 32 blue 175
; 48 red 191
; 64 buff 207
; 80 cyan 223
; 96 magenta 239
; 112 orange 255
wallcolour equ 64 ; Buff
stride equ 32 ; Break my stride. TRS80=64. VZ=32.
walltexture1 equ 138 + wallcolour
walltexture2 equ 139 + wallcolour
ceilingborder equ 7 ; use 17 for mode(1)
floorborder equ 7 ; use 17 for mode(1)
ceilingtexture equ 223 ; cyan
floortexture equ 255 ; orange
topwalltexture1 equ 130 + wallcolour ; 130
topwalltexture2 equ 131 + wallcolour ; 129
bottomwalltexture1 equ 136+ wallcolour ;
bottomwalltexture2 equ 132+ wallcolour ;
SCREEN_WIDTH equ 32 ; TRS80=64
SCREEN_HEIGHT equ 16 ; NOT USED
MAZE_SIZE equ 8 ; NOT USED
latch equ $6800
video equ $7000
origin equ $7B00
org origin - 24
defb 'VZF1' ; file type ID
defb "WOLFENSTEIN 3D", 0,0,0 ; zero padded file name
defb $f1 ; Binary or BASIC?
defw START ; start address
org origin
START:
entry: ld sp, 0
call init
call init_introscreen
menuscr:call init_menuscreen
next_frame:
call get_key_update
call fill_buffer
call draw_screen
jp next_frame
; -------------------------------------------
init:
; -------------------------------------------
ei
call $01c9 ; VZ ROM CLS
ld a, 0 ; MODE(0)
ld ($6800), a
ret
; -------------------------------------------
init_introscreen:
; -------------------------------------------
di
; ld a, 8 ; Mode(1)
; ld ($6800), a
; ld hl, introscreen ; display intro screen
; ld de, $7000
; ld bc, 2048
; ldir
ld a, 0 ; MODE(0)
ld ($6800), a
ei
call $01c9 ; VZ ROM CLS
ret
; -----------------------------------------------------------
init_menuscreen:
; DISPLAY MENU
; PLAY MENU SONG
; PRESS <SPACE>.
; -----------------------------------------------------------
di
ld hl, menu0 ; -ROM String routine stuffs up
ld de, $7000 ;
ld b, 255 ;
msgloop0:ld a, (hl) ; have tried every bloody thing under the sun.
or 64 ; Read string, per each chr$ do an "OR 64", display it.
ld (de), a
inc hl
inc de
djnz msgloop0
ld b, 65+32
msgloop1:ld a, (hl)
or 64
ld (de), a
inc hl
inc de
djnz msgloop1
; jp skip_menu_song2
; Attempt to use the Lydon91 player once for both mode(0) and mode(1)
; Using self modified code. Failed first time. So player is currently in twice for each mode.
; May or may not come back to this if I can be bothered.
;
; ld hl, mask0
; ld (hl), $DD ;mask1 : 28 --> 20 MODE(1) then MODE(0)
; ld hl, mask1
; ld (hl), $21 ;mask1 : 28 --> 20 MODE(1) then MODE(0)
; ld hl, mask2
; ld (hl), $20 ;mask2 : 28 --> 20 MODE(1) then MODE(0)
; ld hl, mask3
; ld (hl), $20 ;mask3 : 28 --> 20 MODE(1) then MODE(0)
; ld hl, mask4
; ld (hl), $20 ;mask4 : 28 --> 20 MODE(1) then MODE(0)
; ld hl, mask5
; ld (hl), $20 ;mask5 : OR A, 28 --> 00 NOP : MODE(1) then MODE(0)
; inc hl
; ld (hl), $20 ;mask5 : OR A, 28 --> 00 NOP : MODE(1) then MODE(0)
;
; ld hl, mask6
; ld (hl), $20 ;mask5 : OR A, 28 --> 00 NOP : MODE(1) then MODE(0)
; inc hl
; ld (hl), $20 ;mask5 : OR A, 28 --> 00 NOP : MODE(1) then MODE(0)
;
; ld hl, mask7
; ld (hl), $20 ;mask5 : OR A, 28 --> 00 NOP : MODE(1) then MODE(0)
; inc hl
; ld (hl), $20 ;mask5 : OR A, 28 --> 00 NOP : MODE(1) then MODE(0)
;skip_menu_song2:
;hereas jp hereas
push af
push bc
push de
push hl
; di
;
; x20 x10 x8 x4 x2 x1
; ----------------------------
;68FE R Q E W T
;68FD F A D ctrl S G
;68FB V Z C SHFT X B
;68F7 4 1 3 2 5
;68EF M SPC , . N
;68DF 7 0 8 - 9 6
;68BF U P I RTN O Y
;687F J L K : L H
;
mkey_loop:
ld a, ($68f7) ; Key : 1
and $10
jr z, mselectmaze1 ; jump if <1> is pressed.
ld a, ($68f7) ; Key : 2
and $2
jr z, mselectmaze2 ; jump if <2> is pressed.
ld a, ($68f7) ; Key : 3
and $8
jr z, mselectmaze3 ; jump if <3> is pressed.
ld a, ($68f7) ; Key : 4
and $20
jr z, mselectmaze4 ; jump if <4> is pressed.
ld a, ($68f7) ; Key : 5
and $1
jr z, mselectmaze5 ; jump if <5> is pressed.
ld a, ($68df) ; Key : 6
and $1
jr z, mselectmaze6 ; jump if <6> is pressed.
ld a, ($68df) ; Key : 7
and $20
jr z, mselectmaze7 ; jump if <7> is pressed.
ld a, ($68df) ; Key : 8
and $8
jr z, mselectmaze8 ; jump if <8> is pressed.
ld a, ($68df) ; Key : 9
and $2
jr z, mselectmaze9 ; jump if <9> is pressed.
ld a, ($68df) ; Key : 0
and $10
jr z, mselectmaze0 ; jump if <0> is pressed.
ld a, ($68EF) ; Key : SPACE
and $10
jr nz, mkey_loop
pop hl ; Space is pressed. Play game.
pop de
pop bc
pop af
ret ; continue on with game!
mselectmaze1: ld hl, level1
jr copymaze
mselectmaze2: ld hl, level2
jr copymaze
mselectmaze3: ld hl, level3
jr copymaze
mselectmaze4: ld hl, level4
jr copymaze
mselectmaze5: ld hl, level5
jr copymaze
mselectmaze6: ld hl, level6
jr copymaze
mselectmaze7: ld hl, level7
jr copymaze
mselectmaze8: ld hl, level8
jr copymaze
mselectmaze9: ld hl, level9
jr copymaze
mselectmaze0: ld hl, level0
jr copymaze
copymaze:ld de, MAZE ; copy level into MAZE temp storage space
ld bc, 64 + 12 ; size of map + title length
ldir
call show_menu_map ; show map routine within Menu
jp mkey_loop
; -------------------------------------------
; Update model from inputs.
; https://www.trs-80.com/wordpress/zaps-patches-pokes-tips/keyboard-map/
; -------------------------------------------
get_key_update:
; -------------------------------------------
; Read keyboard
; Update position.
; Q - forward
; A - back
; M - left
; , - right
; s - straffe left
; d - straffe right
; x20 x10 x8 x4 x2 x1
; ----------------------------
;68FE R Q E W T
;68FD F A D ctrl S G
;68FB V Z C SHFT X B
;68F7 4 1 3 2 5
;68EF M SPC , . N
;68DF 7 0 8 - 9 6
;68BF U P I RTN O Y
;687F J L K : L H
;
KeyX: ld a, ($68FB) ; KEY: X MENU
and $2
jr nz, not_X
jp menuscr
not_X:
KeyM: ld a, ($68EF) ; KEY: M LEFT
and $20
jr nz, not_m
ld a, (dir)
add a, 1 ; POV turn left, screen goes right.
and 63
ld (dir), a
jp end_keyboard
not_m:
keyComma:ld a, ($68EF) ; KEY: comma RIGHT
and $08
jr nz, not_right
ld a, (dir)
sub 1 ; POV turn right, screen goes left.
and 63
ld (dir), a
jp end_keyboard
not_right:
keyQ: ld a, ($68FE) ; KEY: Q Forwards
and $10
jr nz, not_q
call premove
ld a, (posX)
ld hl, dir
ld l, (hl)
ld h, high (DIR_TABLE_X)
ld h, (hl)
sra h
add a, h
ld (posX), a
ld a, (posY)
ld h, high (DIR_TABLE_Y)
ld h, (hl)
sra h
add a, h
ld (posY), a
call postmove
jp end_keyboard
not_q:
keyA: ld a, ($68FD) ; KEY: A BACKWARDS
and $10
jr nz, not_a
call premove
ld a, (posX)
ld hl, dir
ld l, (hl)
ld h, high (DIR_TABLE_X)
ld h, (hl)
sra h
sub h
ld (posX), a
ld a, (posY)
ld h, high (DIR_TABLE_Y)
ld h, (hl)
sra h
sub h
ld (posY), a
call postmove
jp end_keyboard
not_a:
ld a, ($68fd) ; KEY: S Staffe left
and $02
jr nz, not_s
call premove
ld a, (posX)
ld hl, dir
ld l, (hl)
ld h, high (DIR_TABLE_Y)
ld h, (hl)
sra h
add a, h
ld (posX), a
ld a, (posY)
ld h, high (DIR_TABLE_X)
ld h, (hl)
sra h
sub h
ld (posY), a
call postmove
jp end_keyboard
not_s:
keyD: ld a, ($68fd) ; KEY: D Straffe right
and $08
jr nz, not_d
call premove
ld a, (posX)
ld hl, dir
ld l, (hl)
ld h, high (DIR_TABLE_Y)
ld h, (hl)
sra h
sub h
ld (posX), a
ld a, (posY)
ld h, high (DIR_TABLE_X)
ld h, (hl)
sra h
add a, h
ld (posY), a
call postmove
jp end_keyboard
not_d:
keyZ: ld a, ($68fB) ; KEY: Z SHOW MAP
and $10
jr nz, not_z
ld a, (maponoff)
cp 0
jr z, turnmapon ; map off. turn map on.
cp 1
jr z, turnmapoff ; map on. turn map off.
jr not_z
turnmapon:ld a, 1 ; map was off. turn map on.
ld (maponoff), a
jr turnmap
turnmapoff:ld a, 0 ; map was on. turn map off.
ld (maponoff), a
turnmap:call show_map
not_z: ld a, ($68FB) ; Key : C for credits
and $8
jr nz, not_c
call show_credits ; jump to call if <C> is pressed.
not_c:
end_keyboard:
ret
; ----------------------------------------
; Save the player position so that if we
; run into a wall, we can back off.
premove:ld a, (posX)
ld (savePosX), a
ld a, (posY)
ld (savePosY), a
ret
postmove: ; uint_8 mapX = posX >> 5;
ld a, (posX)
srl a
srl a
srl a
srl a
srl a
ld l, a
ld a, (posY) ; uint_8 mapY = (posY >> 5) << 3;
srl a
srl a
and $38 ; 3 bits
or l ; if (MAZE[mapY][mapX] != ' ')
ld h, high(MAZE)
ld l, a
ld a, (hl)
cp ' '
ret z ; not in wall
ld a, (savePosX) ; In wall, restore old position.
ld (posX), a
ld a, (savePosY)
ld (posY), a
ret
; -------------------------------------------
fill_buffer:
ld hl, BUFFER
ld c, 0
ld b, SCREEN_WIDTH
fill_loop:
call get_height
ld (hl), a
inc hl
inc c
djnz fill_loop
ret
; -------------------------------------------
draw_screen:
ld de, stride ; 64 ; Stride
ld bc, 0 ; Column
hloop: push bc
ld hl, BUFFER ; Load height from buffer.
add hl, bc
ld a, (hl)
bit 7, a ; Test top bit for texture.
jp z, texture_2
texture_1:
ld iyl, walltexture1 ; $80+1+4+16
and $7F
push af
ld h, high(TOP_TEXTURE_1)
ld l, a
ld a, (hl)
ld ixh, a
ld h, high(BOTTOM_TEXTURE_1)
ld a, (hl)
ld ixl, a
pop af
jp end_texture
texture_2:
ld iyl, walltexture2 ; $80+4
and $7F
push af
ld h, high(TOP_TEXTURE_2)
ld l, a
ld a, (hl)
ld ixh, a
ld h, high(BOTTOM_TEXTURE_2)
ld a, (hl)
ld ixl, a
pop af
end_texture:
; See if we're too tall and should
; remove the top/bottom character.
cp 24
jr nz, not_too_tall
push af
ld a, iyl
ld ixh, a
ld ixl, a
pop af
dec a
not_too_tall:
; Divide A by 3.
ld h, high(DIV3)
ld l, a
ld a, (hl)
; Height is now in A.
; Top of column.
ld hl, videobuffer ;video
add hl, bc
; Ceiling.
ld b, ceilingborder
jp toplooptest
toploop:
ld (hl), ceilingtexture ; 143 ;128
add hl, de
dec b
toplooptest:
cp b ; Stop at a
jp nz, toploop
; Border between ceiling and wall.
ld b, ixh
ld (hl), b
add hl, de
; Wall.
ld b, a
sla b
ld c, iyl
jp vlooptest
vloop:
ld (hl), c
add hl, de
dec b
vlooptest:
jp nz, vloop
; Border between wall and floor.
ld b, ixl
ld (hl), b
add hl, de
; Floor.
ld b, floorborder
jp bottomlooptest
bottomloop:
ld (hl), floortexture ; 143 + 16 ;128
add hl, de
dec b
bottomlooptest:
cp b ; Stop at a
jp nz, bottomloop
pop bc
inc bc
ld a, c
; cp 64
cp 32
; cp 16
jp nz, hloop
call show_map
screen_update: ; from video_buffer to video.
di
ld hl, videobuffer
ld de, video
ld bc, 512 ; MODE1 : 2048-784
screen_update_loop1: ; wait for first retrace
ld a, (0x6800)
rla
jr c,screen_update_loop1
ldir ; copy first slice during the retrace
; ld bc, 784 ; mode1: prepare for second slice. wait for second retrace
;screen_update_loop2: ; mode1:
; ld a, (0x6800) ; mode1:
; rla ; mode1:
; jr c,screen_update_loop2 ; mode1:
; ldir ; mode1: ; copy the second slice during the retrace
ret
; -------------------------------------------
; Given 0 <= C <= 63 column, return 0 <= A <= 23 height.
; Set high bit of A to use different texture.
get_height:
push hl
push bc
; int8_t cameraX = 2*x - SCREEN_WIDTH; // x-coordinate in camera space
sla c ; c *= 2
ld a, c
sub SCREEN_WIDTH ; c -= SCREEN_WIDTH
ld (cameraX), a
; int8_t dirX = DIR_TABLE_X[dir];
ld hl, dir
ld l, (hl)
ld h, high(DIR_TABLE_X)
ld a, (hl)
ld (dirX), a
; int8_t planeY = dirX;
ld (planeY), a
; int8_t dirY = DIR_TABLE_Y[dir];
ld h, high(DIR_TABLE_Y)
ld a, (hl)
ld (dirY), a
; int8_t planeX = -dirY;
neg
ld (planeX), a
; int8_t rayDirX = dirX + planeX * cameraX / SCREEN_WIDTH;
ld a, (planeX)
ld h, a
ld a, (cameraX)
ld e, a
call signed_mult_8
; Divide by SCREEN_WIDTH (64) by shifting left and using high byte.
add hl, hl
add hl, hl
ld a, (dirX)
add a, h
ld (rayDirX), a
; int8_t rayDirY = dirY + planeY * cameraX / SCREEN_WIDTH;
ld a, (planeY)
ld h, a
ld a, (cameraX)
ld e, a
call signed_mult_8
; Divide by SCREEN_WIDTH (64) by shifting left and using high byte.
add hl, hl
add hl, hl
ld a, (dirY)
add a, h
ld (rayDirY), a
; // which box of the map we're in
; uint_8 mapX = posX >> 5;
ld a, (posX)
srl a
srl a
srl a
srl a
srl a
ld (mapX), a
; uint_8 mapY = posY >> 5; (pre-shifted 3)
ld a, (posY)
srl a
srl a
and $38
ld (mapY), a
; See if we're aligned with one of the
; axes, in which case we'll get a
; divide-by-zero error. Special case
; both of those.
ld a, (rayDirX)
or a
jp nz, rayDirX_not_zero
; ----------------------------------------
; uint8_t deltaDistY = SIGNED_DIV_TABLE[(uint8_t) rayDirY];
ld h, high(SIGNED_DIV_TABLE)
ld a, (rayDirY)
ld l, a
ld a, (hl)
ld (deltaDistY), a
; if (rayDirY < 0) {
ld a, (rayDirY)
bit 7, a
jr z, rayDirYPos
; stepY = -1;
ld a, -8
ld (stepY), a
; sideDistY = (posY - mapY*32) * deltaDistY / 32;
ld a, (posY)
and $1F ; keep fractional part.
jp rayDirYEnd
rayDirYPos:
; } else {
; stepY = 1;
ld a, 8
ld (stepY), a
; sideDistY = ((mapY + 1)*32 - posY) * deltaDistY / 32;
ld a, (posY)
and $1F ; keep fractional part.
xor $1F ; subtract from 32.
rayDirYEnd:
ld h, a
ld a, (deltaDistY)
ld e, a
call mult8
; /= 32
xor a
add hl, hl
rla
add hl, hl
rla
add hl, hl
rla
ld l, h
ld h, a
ld (sideDistY), hl
; perform DDA
loop:
; sideDistY += deltaDistY;
ld de, (sideDistY)
ld a, (deltaDistY)
ld l, a
ld h, 0
add hl, de
ld (sideDistY), hl
; mapY += stepY;
ld a, (mapY)
ld hl, stepY
add a, (hl)
ld (mapY), a
; Check if ray has hit a wall
; if (MAZE[mapY][mapX] != ' ') hit = 1;
ld a, (mapX)
ld hl, mapY
or (hl)
ld h, high(MAZE)
ld l, a
ld a, (hl)
cp ' '
jp z, loop
; Calculate distance projected on camera direction (Euclidean
; distance would give fisheye effect!)
; perpWallDist = sideDistY - deltaDistY;
ld hl, (sideDistY)
ld a, (deltaDistY)
ld e, a
ld d, 0
or a
sbc hl, de
ld (dist), hl
; rayDirX = 0 -------------------------
ld b, $00 ; Hit Y side.
jp end_special_cases
rayDirX_not_zero:
ld a, (rayDirY)
or a
jp nz, rayDirY_not_zero
; ----------------------------------------
; uint8_t deltaDistX = SIGNED_DIV_TABLE[(uint8_t) rayDirX];
ld h, high(SIGNED_DIV_TABLE)
ld a, (rayDirX)
ld l, a
ld a, (hl)
ld (deltaDistX), a
; calculate step and initial sideDist
; if (rayDirX < 0) {
ld a, (rayDirX)
bit 7, a
jr z, rayDirXPos
; stepX = -1;
ld a, -1
ld (stepX), a
; sideDistX = (posX - mapX*32) * deltaDistX / 32;
ld a, (posX)
and $1F ; keep fractional part.
jp rayDirXEnd
rayDirXPos:
; } else {
; stepX = 1;
ld a, 1
ld (stepX), a
; sideDistX = ((mapX + 1)*32 - posX) * deltaDistX / 32;
ld a, (posX)
and $1F ; keep fractional part.
xor $1F ; subtract from 32.
rayDirXEnd:
ld h, a
ld a, (deltaDistX)
ld e, a
call mult8
; /= 32
xor a
add hl, hl
rla
add hl, hl
rla
add hl, hl
rla
ld l, h
ld h, a
ld (sideDistX), hl
; perform DDA
loop2:
; sideDistX += deltaDistX;
ld hl, (sideDistX)
ld a, (deltaDistX)
ld e, a
ld d, 0
add hl, de
ld (sideDistX), hl
; mapX += stepX;
ld a, (mapX)
ld hl, stepX
add a, (hl)
ld (mapX), a
; Check if ray has hit a wall
; if (MAZE[mapY][mapX] != ' ') hit = 1;
ld a, (mapX)
ld hl, mapY
or (hl)
ld h, high(MAZE)
ld l, a
ld a, (hl)
cp ' '
jp z, loop2
; Calculate distance projected on camera direction (Euclidean
; distance would give fisheye effect!)
; perpWallDist = sideDistX - deltaDistX;
ld hl, (sideDistX)
ld a, (deltaDistX)
ld e, a
ld d, 0
or a
sbc hl, de
ld (dist), hl
; rayDirY = zero -------------------------
ld b, $80 ; Hit X side.
jp end_special_cases
rayDirY_not_zero:
; neither is zero -------------------------
; uint8_t deltaDistX = SIGNED_DIV_TABLE[(uint8_t) rayDirX];
ld h, high(SIGNED_DIV_TABLE)
ld a, (rayDirX)
ld l, a
ld a, (hl)
ld (deltaDistX), a
; uint8_t deltaDistY = SIGNED_DIV_TABLE[(uint8_t) rayDirY];
ld a, (rayDirY)
ld l, a
ld a, (hl)
ld (deltaDistY), a
; calculate step and initial sideDist
; if (rayDirX < 0) {
ld a, (rayDirX)
bit 7, a
jr z, rayDirXPos1
; stepX = -1;
ld ixl, -1
; sideDistX = (posX - mapX*32) * deltaDistX / 32;
ld a, (posX)
and $1F ; keep fractional part
jp rayDirXEnd1
rayDirXPos1:
; } else {
; stepX = 1;
ld ixl, 1
; sideDistX = ((mapX + 1)*32 - posX) * deltaDistX / 32;
ld a, (posX)
and $1F ; keep fractional part
xor $1F ; subtract from 32
rayDirXEnd1:
ld h, a
ld a, (deltaDistX)
ld e, a
call mult8
; /= 32
xor a
add hl, hl
rla
add hl, hl
rla
add hl, hl
rla
ld l, h
ld h, a
ld (sideDistX), hl
; if (rayDirY < 0) {
ld a, (rayDirY)
bit 7, a
jr z, rayDirYPos1
; stepY = -1;
ld ixh, -8
; sideDistY = (posY - mapY*32) * deltaDistY / 32;
ld a, (posY)
and $1F
jp rayDirYEnd1
rayDirYPos1:
; } else {
; stepY = 1;
ld ixh, 8
; sideDistY = ((mapY + 1)*32 - posY) * deltaDistY / 32;
ld a, (posY)
and $1F ; keep fractional part
xor $1F ; subtract from 32
rayDirYEnd1:
ld h, a
ld a, (deltaDistY)
ld e, a
call mult8
; /= 32
xor a
add hl, hl
rla
add hl, hl
rla
add hl, hl
rla
ld l, h
ld h, a
ld (sideDistY), hl
; BC is pointer into map.
; IXL = stepX.
; IXH = stepY.
ld a, (mapX)
ld hl, mapY
or (hl)
ld b, high(MAZE)
ld c, a
; perform DDA
loop3:
; jump to next map square, either in x-direction, or in y-direction
; if (sideDistX < sideDistY) {
ld hl, (sideDistX)
ld de, (sideDistY)
or a ; clear carry
sbc hl, de
add hl, de
jr nc, moveY ; jump if de <= hl (y < x)
; sideDistX += deltaDistX;
ld a, (deltaDistX)
ld e, a
ld d, 0
add hl, de