forked from lantus/NGRayEx
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathraycast.c
More file actions
1133 lines (1049 loc) · 40.2 KB
/
Copy pathraycast.c
File metadata and controls
1133 lines (1049 loc) · 40.2 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
/* raycast.c - fixed-point DDA raycaster mapping wall slices onto the Neo
* Geo's hardware sprite shrinker */
#include "raycast.h"
#include "config.h"
#include "map.h"
#if DOOM_RIPDOOM_RENDER
#include "ripdoom_runtime.h"
#endif
/* ---- 16.16 fixed point ---------------------------------------------- */
typedef s32 fix;
#define FBITS 16
#define FONE (1 << FBITS)
#define FIX(x) ((fix)((x) * (double)FONE)) /* constant initializers only */
static inline fix fmul(fix a, fix b) { return (fix)(((int64_t)a * b) >> FBITS); }
static inline fix fdiv(fix a, fix b) { return (fix)(((int64_t)a << FBITS) / b); }
static inline fix fabsx(fix a) { return a < 0 ? -a : a; }
#define RECIP_SHIFT 9
#define RECIP_LUT_SIZE 2048
#define RECIP_MIN (FONE >> 8)
static fix recip_lut[RECIP_LUT_SIZE];
static void init_recip_lut(void) {
for (int i = 0; i < RECIP_LUT_SIZE; i++) {
u32 ab = (u32)(i << RECIP_SHIFT);
if (ab < (u32)RECIP_MIN) ab = RECIP_MIN;
recip_lut[i] = (fix)(0x100000000ULL / ab);
}
}
static inline fix recip(fix b) {
u32 ab = (b < 0) ? (u32)(-b) : (u32)b;
u32 idx;
if (ab < (u32)RECIP_MIN) return recip_lut[0];
idx = ab >> RECIP_SHIFT;
if (idx < RECIP_LUT_SIZE) return recip_lut[idx];
/* The old 256-entry LUT saturated every distance past ~2 map cells to
* one reciprocal, so far walls/sprites stopped shrinking and then popped
* out instead of preserving the perspective falloff. Keep the Neo Geo
* fast table for the common near/mid range, but fall back to one exact
* reciprocal for true long corridors. */
return (fix)(0x100000000ULL / ab);
}
#define FBIG (1 << 28)
#define FMIN (FONE >> 6) /* clamp tiny distances */
#define PLAYER_RADIUS ((FONE / 5) * MAP_RENDER_SCALE) /* Doom-ish collision body */
#ifndef PORTAL_SPAN_DRAW_MIN_H
#define PORTAL_SPAN_DRAW_MIN_H 3
#endif
#ifndef PORTAL_SPAN_OCCLUDE_MIN_H
#define PORTAL_SPAN_OCCLUDE_MIN_H 5
#endif
#ifndef PORTAL_SPAN_REPLACE_MIN_H
#define PORTAL_SPAN_REPLACE_MIN_H 18
#endif
#ifndef PORTAL_SPAN_REPLACE_FAR_DIV
#define PORTAL_SPAN_REPLACE_FAR_DIV 2
#endif
static fix posX, posY; /* world position (1.0 == one map cell) */
static fix dirX, dirY; /* facing direction (unit) */
static fix planeX, planeY; /* camera plane (sets FOV; |plane|~0.66) */
static fix invDet; /* inverse camera matrix determinant */
static fix cameraXbuf[NUM_COLS]; /* constant camera x in [-1,+1] per column */
static fix rayXbuf[NUM_COLS]; /* cached ray directions for current view */
static fix rayYbuf[NUM_COLS];
static fix rayDdXbuf[NUM_COLS]; /* DDA delta distances for cached rays */
static fix rayDdYbuf[NUM_COLS];
static signed char rayStepXbuf[NUM_COLS];
static signed char rayStepYbuf[NUM_COLS];
static u8 rays_dirty = 1;
static u16 scb2buf[WALL_SPRITE_COUNT]; /* (HSHRINK<<8)|vshrink */
static u16 scb3buf[WALL_SPRITE_COUNT]; /* Y/size word */
static u16 curscb2[WALL_SPRITE_COUNT]; /* control words currently in VRAM */
static u16 curscb3[WALL_SPRITE_COUNT];
static u8 palbuf[WALL_SPRITE_COUNT]; /* desired palette this frame */
static u8 curpal[WALL_SPRITE_COUNT]; /* palette currently in VRAM */
static u8 texbuf[WALL_SPRITE_COUNT]; /* wall texture atlas column */
static u8 curtex[WALL_SPRITE_COUNT]; /* atlas column currently in VRAM */
static u8 kindbuf[WALL_SPRITE_COUNT]; /* 0 = wall, 1..N = alt wall, door */
static u8 curkind[WALL_SPRITE_COUNT];
static u8 closebuf[WALL_SPRITE_COUNT]; /* reserved for coarse-wall mips */
static u8 curclose[WALL_SPRITE_COUNT];
static fix distbuf[NUM_COLS]; /* perpendicular wall distance */
static u8 wall_full_cover[NUM_COLS]; /* wall column fully hides backdrop */
static u16 wall_tiles[TILE_WALL_ATLAS_COLS][WALL_WIN];
static u16 wall_alt_tiles[TILE_WALL_ALT_COUNT][TILE_WALL_ATLAS_COLS][WALL_WIN];
static u16 door_tiles[TILE_WALL_ATLAS_COLS][WALL_WIN];
static u8 view_dirty = 1;
static u8 wall_upload_dirty = 1;
static u8 wall_upload_scan = 0;
static u8 wall_render_scan = 0;
static u8 wall_first_upload = 1;
static u8 wall_frame_overrun = 0;
static u8 render_motion_active = 0;
static u8 moved_last_input = 0;
#if DOOM_RIPDOOM_RENDER
static long ripdoom_start_global_x_q8 = 0;
static long ripdoom_start_global_y_q8 = 0;
static short ripdoom_view_start_x = 0;
static short ripdoom_view_start_y = 0;
static u8 ripdoom_view_ready = 0;
#endif
static inline int projected_height_from_inv(fix inv_dist) {
int h = (int)((((s32)WALLH * MAP_RENDER_SCALE) * inv_dist) >> FBITS);
return h < 1 ? 1 : h;
}
static inline int projected_height(fix dist) {
return projected_height_from_inv(recip(dist));
}
static inline int projected_span_height(fix dist, u8 span_height) {
int full_h = projected_height(dist);
int h = (full_h * span_height + 63) / 128;
return h < 2 ? 2 : h;
}
static inline u8 depth_palette(u8 kind, int side, int h) {
int band = ((MAX_H - h) * DEPTH_BANDS) / MAX_H;
if (band < 0) band = 0;
if (band >= DEPTH_BANDS) band = DEPTH_BANDS - 1;
return (u8)(((kind > TILE_WALL_ALT_COUNT)
? PAL_DOOR_DEPTH_BASE
: (kind ? PAL_WALL_ALT_DEPTH_BASE + (kind - 1) * PAL_WALL_ALT_DEPTH_STRIDE : PAL_DEPTH_BASE))
+ (side ? DEPTH_BANDS : 0) + band);
}
static inline int wall_slot(int layer, int column) {
return layer * NUM_COLS + column;
}
static void hide_wall_slot(int slot) {
if (slot < 0 || slot >= WALL_SPRITE_COUNT) return;
scb2buf[slot] = (u16)((HSHRINK << 8) | 0);
scb3buf[slot] = scb3_word(SCRH + 32, 0, 1);
palbuf[slot] = PAL_WALL_LIT;
texbuf[slot] = 0;
kindbuf[slot] = 0;
closebuf[slot] = 0;
}
#if DOOM_RIPDOOM_RENDER
static long ripdoom_global_start_x_q8(void) {
#if DOOM_SIMPLE_MAP && DOOM_CHUNKED_SIMPLE_MAP
long chunk_x = DOOM_CHUNK_START_CHUNK % DOOM_CHUNK_COLS;
return (chunk_x * DOOM_CHUNK_SIZE * 256L) + DOOM_CHUNK_START_X_Q8;
#else
return (long)(FIX(DOOM_START_X) >> (FBITS - 8));
#endif
}
static long ripdoom_global_start_y_q8(void) {
#if DOOM_SIMPLE_MAP && DOOM_CHUNKED_SIMPLE_MAP
long chunk_y = DOOM_CHUNK_START_CHUNK / DOOM_CHUNK_COLS;
return (chunk_y * DOOM_CHUNK_SIZE * 256L) + DOOM_CHUNK_START_Y_Q8;
#else
return (long)(FIX(DOOM_START_Y) >> (FBITS - 8));
#endif
}
static void ripdoom_start_coord_from_chunk(short *x, short *y) {
#if DOOM_SIMPLE_MAP && DOOM_CHUNKED_SIMPLE_MAP
long global_x_q8 = ripdoom_global_start_x_q8();
long global_y_q8 = ripdoom_global_start_y_q8();
*x = (short)(DOOM_CHUNK_ORIGIN_X + ((global_x_q8 * DOOM_CHUNK_CELL_DOOM_UNITS + 128) >> 8));
*y = (short)(DOOM_CHUNK_ORIGIN_Y - ((global_y_q8 * DOOM_CHUNK_CELL_DOOM_UNITS + 128) >> 8));
#else
*x = (short)0;
*y = (short)0;
#endif
}
static void ripdoom_init_view_anchor(void) {
ripdoom_view_ready = 0;
#if DOOM_SIMPLE_MAP && DOOM_CHUNKED_SIMPLE_MAP
ripdoom_start_global_x_q8 = ripdoom_global_start_x_q8();
ripdoom_start_global_y_q8 = ripdoom_global_start_y_q8();
ripdoom_start_coord_from_chunk(&ripdoom_view_start_x, &ripdoom_view_start_y);
ripdoom_view_ready = 1;
return;
#else
int i;
for (i = 0; i < NG_RIP_THING_COUNT; i++) {
if (g_rip_things[i].type == 1) {
ripdoom_start_global_x_q8 = ripdoom_global_start_x_q8();
ripdoom_start_global_y_q8 = ripdoom_global_start_y_q8();
ripdoom_view_start_x = g_rip_things[i].x;
ripdoom_view_start_y = g_rip_things[i].y;
ripdoom_view_ready = 1;
return;
}
}
#endif
}
static void ripdoom_visual_pose(short *x, short *y) {
#if DOOM_SIMPLE_MAP && DOOM_CHUNKED_SIMPLE_MAP
long active_chunk_x = SIMPLE_ACTIVE_CHUNK % DOOM_CHUNK_COLS;
long active_chunk_y = SIMPLE_ACTIVE_CHUNK / DOOM_CHUNK_COLS;
long pos_x_q8 = (posX >> (FBITS - 8)) + active_chunk_x * DOOM_CHUNK_SIZE * 256L;
long pos_y_q8 = (posY >> (FBITS - 8)) + active_chunk_y * DOOM_CHUNK_SIZE * 256L;
long dx = ((pos_x_q8 - ripdoom_start_global_x_q8) * DOOM_RIPDOOM_RENDER_UNITS_PER_CELL) >> 8;
long dy = ((pos_y_q8 - ripdoom_start_global_y_q8) * DOOM_RIPDOOM_RENDER_UNITS_PER_CELL) >> 8;
#else
long dx = ((long)(posX - FIX(DOOM_START_X)) * DOOM_RIPDOOM_RENDER_UNITS_PER_CELL) >> FBITS;
long dy = ((long)(posY - FIX(DOOM_START_Y)) * DOOM_RIPDOOM_RENDER_UNITS_PER_CELL) >> FBITS;
#endif
*x = (short)(ripdoom_view_start_x + dx);
*y = (short)(ripdoom_view_start_y - dy);
}
static void ripdoom_coord_from_local_q8(short x_q8, short y_q8, short *x, short *y) {
#if DOOM_SIMPLE_MAP && DOOM_CHUNKED_SIMPLE_MAP
long active_chunk_x = SIMPLE_ACTIVE_CHUNK % DOOM_CHUNK_COLS;
long active_chunk_y = SIMPLE_ACTIVE_CHUNK / DOOM_CHUNK_COLS;
long global_x_q8 = (long)x_q8 + active_chunk_x * DOOM_CHUNK_SIZE * 256L;
long global_y_q8 = (long)y_q8 + active_chunk_y * DOOM_CHUNK_SIZE * 256L;
long dx = ((global_x_q8 - ripdoom_start_global_x_q8) * DOOM_RIPDOOM_RENDER_UNITS_PER_CELL) >> 8;
long dy = ((global_y_q8 - ripdoom_start_global_y_q8) * DOOM_RIPDOOM_RENDER_UNITS_PER_CELL) >> 8;
#else
long dx = (((long)x_q8 - ripdoom_start_global_x_q8) * DOOM_RIPDOOM_RENDER_UNITS_PER_CELL) >> 8;
long dy = (((long)y_q8 - ripdoom_start_global_y_q8) * DOOM_RIPDOOM_RENDER_UNITS_PER_CELL) >> 8;
#endif
*x = (short)(ripdoom_view_start_x + dx);
*y = (short)(ripdoom_view_start_y - dy);
}
static int ripdoom_floor_height_for_local_q8(short x_q8, short y_q8) {
short rip_x;
short rip_y;
int sector;
if (!ripdoom_view_ready) return map_cell_floor_height(x_q8 >> 8, y_q8 >> 8);
ripdoom_coord_from_local_q8(x_q8, y_q8, &rip_x, &rip_y);
sector = ripdoom_point_sector(rip_x, rip_y);
if (sector < 0 || sector >= NG_RIP_SECTOR_COUNT) return map_cell_floor_height(x_q8 >> 8, y_q8 >> 8);
return g_rip_sectors[sector].floor_height;
}
static int rc_ripdoom_floor_height_q8(short x_q8, short y_q8) {
#if DOOM_MULTIFLOOR_RENDER
return ripdoom_floor_height_for_local_q8(x_q8, y_q8);
#else
return map_cell_floor_height(x_q8 >> 8, y_q8 >> 8);
#endif
}
static u8 ripdoom_can_move_to_local_q8(short from_x_q8, short from_y_q8, short to_x_q8, short to_y_q8) {
#if DOOM_MULTIFLOOR_RENDER
short from_rip_x;
short from_rip_y;
short to_rip_x;
short to_rip_y;
int from_sector;
int to_sector;
int from_floor;
int to_floor;
int to_ceil;
if (!ripdoom_view_ready) return 1;
ripdoom_coord_from_local_q8(from_x_q8, from_y_q8, &from_rip_x, &from_rip_y);
ripdoom_coord_from_local_q8(to_x_q8, to_y_q8, &to_rip_x, &to_rip_y);
from_sector = ripdoom_point_sector(from_rip_x, from_rip_y);
to_sector = ripdoom_point_sector(to_rip_x, to_rip_y);
if (from_sector < 0 || to_sector < 0 || from_sector >= NG_RIP_SECTOR_COUNT || to_sector >= NG_RIP_SECTOR_COUNT) return 0;
from_floor = g_rip_sectors[from_sector].floor_height;
to_floor = g_rip_sectors[to_sector].floor_height;
to_ceil = g_rip_sectors[to_sector].ceiling_height;
if (to_floor - from_floor > DOOM_PLAYER_STEP_HEIGHT) return 0;
if (from_floor - to_floor > DOOM_PLAYER_DROP_HEIGHT) return 0;
if (to_ceil - to_floor < DOOM_PLAYER_HEIGHT) return 0;
#else
(void)from_x_q8;
(void)from_y_q8;
(void)to_x_q8;
(void)to_y_q8;
#endif
return 1;
}
static u8 ripdoom_kind_for_hit(const NgRipRayHit *hit) {
if (hit->flags & NG_RIP_SEG_DOOR) return (u8)(TILE_WALL_ALT_COUNT + 1);
if (hit->texture_kind >= 1 && hit->texture_kind <= TILE_WALL_ALT_COUNT) return hit->texture_kind;
return 0;
}
static u8 ripdoom_kind_for_span(const NgRipRaySpan *span) {
if (span->flags & NG_RIP_SEG_DOOR) return (u8)(TILE_WALL_ALT_COUNT + 1);
if (span->texture_kind >= 1 && span->texture_kind <= TILE_WALL_ALT_COUNT) return span->texture_kind;
return 0;
}
#if DOOM_MULTIFLOOR_RENDER
static u8 rc_project_ripdoom_span_to_slot(int slot, const NgRipRaySpan *span, int eye_z, u8 full_cover_candidate, fix *out_perp) {
fix perp;
int full_h;
int top;
int bottom;
int h;
int palette_h;
int vsh;
int view_h = GAME_H;
u8 kind;
if (!span) return 0;
perp = (fix)((((long)span->distance_q8) << FBITS) / DOOM_RIPDOOM_RENDER_UNITS_PER_CELL);
if (perp < FMIN) perp = FMIN;
if (out_perp) *out_perp = perp;
full_h = projected_height(perp);
top = HORIZON - (((int)span->z_top - eye_z) * full_h) / 128;
bottom = HORIZON - (((int)span->z_bottom - eye_z) * full_h) / 128;
if (bottom <= 0 || top >= view_h) return 0;
if (top < 0) top = 0;
if (bottom > view_h) bottom = view_h;
#if DOOM_MULTIFLOOR_SOLID_FULL_HEIGHT
if (full_cover_candidate) {
top = 0;
bottom = view_h;
}
#endif
h = bottom - top;
if (h < PORTAL_SPAN_DRAW_MIN_H && !full_cover_candidate) return 0;
if (h < 1) h = 1;
if (h > MAX_H) h = MAX_H;
palette_h = full_h;
if (palette_h < 1) palette_h = 1;
if (palette_h > MAX_H) palette_h = MAX_H;
vsh = h - 1;
if (vsh < 0) vsh = 0;
if (vsh > 255) vsh = 255;
kind = ripdoom_kind_for_span(span);
kindbuf[slot] = kind;
texbuf[slot] = (u8)(((int)span->tex_u * TILE_WALL_ATLAS_COLS) >> 8);
texbuf[slot] &= (TILE_WALL_ATLAS_COLS - 1);
closebuf[slot] = 0;
scb2buf[slot] = (u16)((HSHRINK << 8) | (vsh & 0xFF));
scb3buf[slot] = scb3_word(top, 0, WALL_WIN);
palbuf[slot] = depth_palette(kind, span->side, palette_h);
return (u8)(full_cover_candidate && top <= 0 && bottom >= view_h);
}
#endif
static u8 rc_render_ripdoom_column(int column, fix ray_x, fix ray_y) {
#if DOOM_MULTIFLOOR_RENDER
NgRipRaySpanSet spans;
#else
NgRipRayHit hit;
#endif
short rip_x;
short rip_y;
short rip_dir_x;
short rip_dir_y;
fix perp;
int full_h;
int h;
int top;
int vsh;
int view_h;
if (!ripdoom_view_ready) return 0;
ripdoom_visual_pose(&rip_x, &rip_y);
rip_dir_x = (short)(ray_x >> (FBITS - 8));
rip_dir_y = (short)((-ray_y) >> (FBITS - 8));
#if DOOM_MULTIFLOOR_RENDER
for (int layer = 0; layer < DOOM_WALL_LAYERS; layer++) hide_wall_slot(wall_slot(layer, column));
distbuf[column] = FBIG;
wall_full_cover[column] = 0;
if (!ripdoom_cast_local_ray_spans(rip_x, rip_y, rip_dir_x, rip_dir_y, DOOM_RIPDOOM_RENDER_BLOCK_RADIUS, &spans)) return 0;
{
int sector = ripdoom_point_sector(rip_x, rip_y);
int eye_z = DOOM_PLAYER_EYE_HEIGHT;
if (sector >= 0 && sector < NG_RIP_SECTOR_COUNT) eye_z += g_rip_sectors[sector].floor_height;
if (spans.has_solid) {
wall_full_cover[column] = rc_project_ripdoom_span_to_slot(wall_slot(0, column), &spans.solid, eye_z, 1, &perp);
distbuf[column] = perp;
}
#if DOOM_RIPDOOM_FOREGROUND_SPANS
for (int i = 0; i < spans.foreground_count && i + 1 < DOOM_WALL_LAYERS; i++) {
(void)rc_project_ripdoom_span_to_slot(wall_slot(i + 1, column), &spans.foreground[i], eye_z, 0, 0);
}
#endif
}
return 1;
#else
if (!ripdoom_cast_local_ray(rip_x, rip_y, rip_dir_x, rip_dir_y, DOOM_RIPDOOM_RENDER_BLOCK_RADIUS, &hit)) return 0;
if (hit.span && hit.span_height < 96) {
NgRipRayHit far_hit;
unsigned short min_dist = hit.distance_q8 > 0xFFF0 ? 0xFFFF : (unsigned short)(hit.distance_q8 + 16);
if (ripdoom_cast_local_ray_after(rip_x, rip_y, rip_dir_x, rip_dir_y, DOOM_RIPDOOM_RENDER_BLOCK_RADIUS, min_dist, &far_hit)) {
hit = far_hit;
}
}
perp = (fix)((((long)hit.distance_q8) << FBITS) / DOOM_RIPDOOM_RENDER_UNITS_PER_CELL);
if (perp < FMIN) perp = FMIN;
full_h = projected_height(perp);
h = full_h;
if (hit.span && hit.span_height) h = projected_span_height(perp, hit.span_height);
if (h < 1) h = 1;
if (h > MAX_H) h = MAX_H;
kindbuf[column] = ripdoom_kind_for_hit(&hit);
texbuf[column] = (u8)(((int)hit.tex_u * TILE_WALL_ATLAS_COLS) >> 8);
texbuf[column] &= (TILE_WALL_ATLAS_COLS - 1);
closebuf[column] = 0;
distbuf[column] = perp;
#if DOOM_SIMPLE_MAP
view_h = SCRH;
#else
view_h = GAME_H;
#endif
top = (view_h - h) / 2;
if (hit.span == 1) {
int bottom = (view_h + full_h) / 2;
if (bottom > view_h) bottom = view_h;
top = bottom - h;
} else if (hit.span == 2) {
top = (view_h - full_h) / 2;
}
if (top < 0) top = 0;
if (top > view_h - 1) top = view_h - 1;
vsh = h - 1;
if (vsh < 0) vsh = 0;
if (vsh > 255) vsh = 255;
scb2buf[column] = (u16)((HSHRINK << 8) | (vsh & 0xFF));
scb3buf[column] = scb3_word(top, 0, WALL_WIN);
wall_full_cover[column] = (u8)(!hit.span && top <= 0 && h >= view_h);
palbuf[column] = depth_palette(kindbuf[column], hit.side, h);
return 1;
#endif
}
#endif
int rc_floor_height_q8(short x_q8, short y_q8) {
#if DOOM_RIPDOOM_RENDER && DOOM_MULTIFLOOR_RENDER
return rc_ripdoom_floor_height_q8(x_q8, y_q8);
#else
return map_cell_floor_height(x_q8 >> 8, y_q8 >> 8);
#endif
}
static void update_projection_cache(void) {
fix det = fmul(planeX, dirY) - fmul(dirX, planeY);
if (det > -FMIN && det < FMIN) invDet = 0;
else invDet = fdiv(FONE, det);
rays_dirty = 1;
}
static void update_ray_cache(void) {
if (!rays_dirty) return;
for (int c = 0; c < NUM_COLS; c++) {
fix cameraX = cameraXbuf[c];
fix rayX = dirX + fmul(planeX, cameraX);
fix rayY = dirY + fmul(planeY, cameraX);
rayXbuf[c] = rayX;
rayYbuf[c] = rayY;
rayDdXbuf[c] = recip(rayX);
rayDdYbuf[c] = recip(rayY);
rayStepXbuf[c] = rayX < 0 ? -1 : 1;
rayStepYbuf[c] = rayY < 0 ? -1 : 1;
}
rays_dirty = 0;
}
void rc_init(void) {
init_recip_lut();
#if DOOM_RIPDOOM_RENDER
ripdoom_init_view_anchor();
#endif
for (int tx = 0; tx < TILE_WALL_ATLAS_COLS; tx++) {
for (int row = 0; row < WALL_WIN; row++) {
int ty = (row * TILE_WALL_ATLAS_ROWS) / WALL_WIN;
wall_tiles[tx][row] = (u16)(TILE_WALL_ATLAS_BASE + ty * TILE_WALL_ATLAS_COLS + tx);
for (int alt = 0; alt < TILE_WALL_ALT_COUNT; alt++) {
wall_alt_tiles[alt][tx][row] = (u16)(TILE_WALL_ALT_ATLAS_BASE + alt * TILE_WALL_ATLAS_TILES + ty * TILE_WALL_ATLAS_COLS + tx);
}
door_tiles[tx][row] = (u16)(TILE_DOOR_ATLAS_BASE + ty * TILE_WALL_ATLAS_COLS + tx);
}
}
for (int c = 0; c < NUM_COLS; c++) {
cameraXbuf[c] = (fix)(((int64_t)2 * FONE * c) / (NUM_COLS - 1)) - FONE;
}
posX = FIX(DOOM_START_X);
posY = FIX(DOOM_START_Y);
dirX = FIX(DOOM_DIR_X);
dirY = FIX(DOOM_DIR_Y);
planeX = FIX(DOOM_PLANE_X);
planeY = FIX(DOOM_PLANE_Y);
update_projection_cache();
for (int c = 0; c < WALL_SPRITE_COUNT; c++) {
curpal[c] = 0xFF; /* force first write */
curtex[c] = 0xFF;
curkind[c] = 0xFF;
curclose[c] = 0xFF;
curscb2[c] = 0xFFFF;
curscb3[c] = 0xFFFF;
hide_wall_slot(c);
}
for (int c = 0; c < NUM_COLS; c++) {
wall_full_cover[c] = 0;
distbuf[c] = FBIG;
}
rc_invalidate_view();
}
void rc_invalidate_view(void) {
view_dirty = 1;
}
void rc_set_pose_q8(short x_q8, short y_q8, short dir_x_q8, short dir_y_q8) {
posX = ((fix)x_q8) << (FBITS - 8);
posY = ((fix)y_q8) << (FBITS - 8);
dirX = ((fix)dir_x_q8) << (FBITS - 8);
dirY = ((fix)dir_y_q8) << (FBITS - 8);
planeX = -fmul(dirY, FIX(0.66));
planeY = fmul(dirX, FIX(0.66));
update_projection_cache();
rc_invalidate_view();
}
void rc_shift_player_q8(short dx_q8, short dy_q8) {
posX += ((fix)dx_q8) << (FBITS - 8);
posY += ((fix)dy_q8) << (FBITS - 8);
rc_invalidate_view();
}
static void rotate(int sign) {
fix cs = FIX(ROT_COS);
fix sn = sign < 0 ? -FIX(ROT_SIN) : FIX(ROT_SIN);
fix ox = dirX;
dirX = fmul(dirX, cs) - fmul(dirY, sn);
dirY = fmul(ox, sn) + fmul(dirY, cs);
fix opx = planeX;
planeX = fmul(planeX, cs) - fmul(planeY, sn);
planeY = fmul(opx, sn) + fmul(planeY, cs);
update_projection_cache();
rc_invalidate_view();
}
static u8 player_can_occupy(fix x, fix y) {
int cx = x >> FBITS;
int cy = y >> FBITS;
if (map_at(cx, cy)) return 0;
if (map_at((x - PLAYER_RADIUS) >> FBITS, cy)) return 0;
if (map_at((x + PLAYER_RADIUS) >> FBITS, cy)) return 0;
if (map_at(cx, (y - PLAYER_RADIUS) >> FBITS)) return 0;
if (map_at(cx, (y + PLAYER_RADIUS) >> FBITS)) return 0;
if (rc_dynamic_blocked_q8((short)(x >> (FBITS - 8)), (short)(y >> (FBITS - 8)))) return 0;
#if DOOM_RIPDOOM_RENDER && DOOM_MULTIFLOOR_RENDER
if (!ripdoom_can_move_to_local_q8(
(short)(posX >> (FBITS - 8)),
(short)(posY >> (FBITS - 8)),
(short)(x >> (FBITS - 8)),
(short)(y >> (FBITS - 8)))) return 0;
#endif
return 1;
}
static u8 try_move(fix dx, fix dy) {
fix nx = posX + dx, ny = posY + dy;
fix ox = posX, oy = posY;
if (player_can_occupy(nx, posY)) posX = nx;
if (player_can_occupy(posX, ny)) posY = ny;
if (posX != ox || posY != oy) {
rc_invalidate_view();
return 1;
}
return 0;
}
static inline signed char input_axis(u8 pressed, u8 neg, u8 pos) {
u8 n = pressed & neg;
u8 p = pressed & pos;
if (n == p) return 0;
return n ? -1 : 1;
}
u8 rc_input(u8 pressed) {
enum { UP=1, DOWN=2, LEFT=4, RIGHT=8, A=16 };
fix spd = FIX(MOVE_SPEED * MAP_RENDER_SCALE);
signed char forward = input_axis(pressed, DOWN, UP);
signed char side = 0;
signed char turn = 0;
u8 camera_changed = 0;
moved_last_input = 0;
if (pressed & A) { /* strafe with A held */
side = input_axis(pressed, LEFT, RIGHT);
} else {
turn = input_axis(pressed, LEFT, RIGHT);
}
if (forward || side) {
fix dx = 0;
fix dy = 0;
if (forward && side) spd = (fix)(((s32)spd * 181) >> 8); /* ~1/sqrt(2) */
if (forward) {
fix step_x = fmul(dirX, spd);
fix step_y = fmul(dirY, spd);
if (forward > 0) { dx += step_x; dy += step_y; }
else { dx -= step_x; dy -= step_y; }
}
if (side) {
fix step_x = fmul(planeX, spd);
fix step_y = fmul(planeY, spd);
if (side > 0) { dx += step_x; dy += step_y; }
else { dx -= step_x; dy -= step_y; }
}
moved_last_input = try_move(dx, dy);
camera_changed |= moved_last_input;
}
if (turn) {
rotate(turn);
camera_changed = 1;
}
render_motion_active = camera_changed;
return camera_changed;
}
u8 rc_moved_last_input(void) {
return moved_last_input;
}
void rc_player_cell(int *cx, int *cy) {
*cx = posX >> FBITS;
*cy = posY >> FBITS;
}
void rc_player_q8(int *x_q8, int *y_q8) {
*x_q8 = posX >> (FBITS - 8);
*y_q8 = posY >> (FBITS - 8);
}
void rc_dir_q8(int *view_dir_x, int *view_dir_y) {
*view_dir_x = dirX >> (FBITS - 8);
*view_dir_y = dirY >> (FBITS - 8);
}
void rc_view_q8(int *view_dir_x, int *view_dir_y, int *view_plane_x, int *view_plane_y) {
*view_dir_x = dirX >> (FBITS - 8);
*view_dir_y = dirY >> (FBITS - 8);
*view_plane_x = planeX >> (FBITS - 8);
*view_plane_y = planeY >> (FBITS - 8);
}
int rc_project_point(int world_x_q8, int world_y_q8, int *screen_x, int *height, int *dist_q8) {
fix spriteX = ((fix)world_x_q8 << (FBITS - 8)) - posX;
fix spriteY = ((fix)world_y_q8 << (FBITS - 8)) - posY;
if (!invDet) return 0;
fix transformX = fmul(invDet, fmul(dirY, spriteX) - fmul(dirX, spriteY));
fix transformY = fmul(invDet, -fmul(planeY, spriteX) + fmul(planeX, spriteY));
if (transformY < (FONE >> 3)) return 0;
fix invY = recip(transformY);
fix scaleX = fmul(transformX, invY);
int sx = (SCRW / 2) + (int)(((s32)(SCRW / 2) * scaleX) >> FBITS);
int h = projected_height_from_inv(invY);
if (h < 1) return 0;
if (h > GAME_H) h = GAME_H;
if (sx >= 0 && sx < SCRW) {
int col = sx / COLW;
if (col >= 0 && col < NUM_COLS) {
u8 visible = 0;
for (int dc = -2; dc <= 2; dc++) {
int sample_col = col + dc;
if (sample_col < 0 || sample_col >= NUM_COLS) continue;
if (transformY <= distbuf[sample_col] + (FONE >> 3)) {
visible = 1;
break;
}
}
if (!visible) return 0;
}
}
*screen_x = sx;
*height = h;
*dist_q8 = (int)(transformY >> (FBITS - 8));
return 1;
}
u8 rc_sprite_strip_visible(int left, int right, int dist_q8) {
fix sprite_dist;
int first_col;
int last_col;
if (right < 0 || left >= SCRW) return 0;
if (left < 0) left = 0;
if (right >= SCRW) right = SCRW - 1;
if (right < left) return 0;
first_col = left / COLW;
last_col = right / COLW;
if (first_col < 0) first_col = 0;
if (last_col >= NUM_COLS) last_col = NUM_COLS - 1;
sprite_dist = ((fix)dist_q8) << (FBITS - 8);
for (int c = first_col; c <= last_col; c++) {
if (sprite_dist <= distbuf[c] + (FONE >> 3)) return 1;
}
return 0;
}
u8 rc_background_column_hidden(u8 col) {
int left;
int right;
int first_col;
int last_col;
if (col >= BG_COUNT) return 0;
left = (int)col * 16;
right = left + 15;
first_col = left / COLW;
last_col = right / COLW;
if (first_col < 0) first_col = 0;
if (last_col >= NUM_COLS) last_col = NUM_COLS - 1;
for (int c = first_col; c <= last_col; c++) {
if (!wall_full_cover[c]) return 0;
}
return 1;
}
static u8 rc_refine_render_line_hit(fix rayX, fix rayY, int cell_x, int cell_y, u8 accept_mode, fix *dist, u8 *kind, u8 *tex, int *side, u8 *span, u8 *span_height) {
#if DOOM_RENDER_LINES
unsigned char cell_count = g_render_cell_count[cell_y][cell_x];
unsigned short cell_start;
if (!cell_count) return 0;
int pos_x_q4 = (int)(posX >> (FBITS - 4));
int pos_y_q4 = (int)(posY >> (FBITS - 4));
int ray_x_q4 = (int)(rayX >> (FBITS - 4));
int ray_y_q4 = (int)(rayY >> (FBITS - 4));
int best_t_q8 = 0x7FFFFFFF;
int best_u_q8 = 0;
int best_line = -1;
cell_start = g_render_cell_start[cell_y][cell_x];
for (unsigned char n = 0; n < cell_count; n++) {
int i = g_render_cell_lines[cell_start + n];
u8 line_span = g_render_lines[i].span;
if (accept_mode == 1) {
if (!line_span) continue;
} else if (accept_mode == 0 && line_span) {
continue;
}
int x1 = g_render_lines[i].x1_q8 >> 4;
int y1 = g_render_lines[i].y1_q8 >> 4;
int x2 = g_render_lines[i].x2_q8 >> 4;
int y2 = g_render_lines[i].y2_q8 >> 4;
int seg_x = x2 - x1;
int seg_y = y2 - y1;
u8 flags = g_render_lines[i].flags;
int denom = ray_x_q4 * seg_y - ray_y_q4 * seg_x;
int rel_x;
int rel_y;
int num_t;
int num_u;
int t_q8;
int u_q8;
if (denom == 0) continue;
if (flags & (NG_RENDER_SIDE_POS | NG_RENDER_SIDE_NEG)) {
int side_cross = seg_x * (pos_y_q4 - y1) - seg_y * (pos_x_q4 - x1);
if (side_cross > 0) {
if (!(flags & NG_RENDER_SIDE_POS)) continue;
} else if (side_cross < 0) {
if (!(flags & NG_RENDER_SIDE_NEG)) continue;
}
}
rel_x = x1 - pos_x_q4;
rel_y = y1 - pos_y_q4;
num_t = rel_x * seg_y - rel_y * seg_x;
num_u = rel_x * ray_y_q4 - rel_y * ray_x_q4;
if (denom < 0) {
denom = -denom;
num_t = -num_t;
num_u = -num_u;
}
if (num_t <= 0 || num_u < 0 || num_u > denom) continue;
t_q8 = (num_t << 8) / denom;
if (t_q8 < 24 || t_q8 >= best_t_q8) continue;
u_q8 = (num_u << 8) / denom;
best_t_q8 = t_q8;
best_u_q8 = u_q8;
best_line = i;
}
if (best_line >= 0) {
int seg_x = (g_render_lines[best_line].x2_q8 - g_render_lines[best_line].x1_q8);
int seg_y = (g_render_lines[best_line].y2_q8 - g_render_lines[best_line].y1_q8);
int abs_x = seg_x < 0 ? -seg_x : seg_x;
int abs_y = seg_y < 0 ? -seg_y : seg_y;
int tex_x = ((best_u_q8 * TILE_WALL_ATLAS_COLS) >> 8) & (TILE_WALL_ATLAS_COLS - 1);
tex_x = (tex_x + g_render_lines[best_line].phase) & (TILE_WALL_ATLAS_COLS - 1);
*dist = ((fix)best_t_q8) << (FBITS - 8);
*kind = g_render_lines[best_line].texture;
*tex = (u8)tex_x;
*side = (abs_x > abs_y) ? 1 : 0;
*span = g_render_lines[best_line].span;
*span_height = g_render_lines[best_line].height;
return 1;
}
#else
(void)rayX;
(void)rayY;
(void)cell_x;
(void)cell_y;
(void)accept_mode;
#endif
(void)dist;
(void)kind;
(void)tex;
(void)side;
(void)span;
(void)span_height;
return 0;
}
void rc_render(void) {
if (!view_dirty) return;
update_ray_cache();
int baseMapX = posX >> FBITS;
int baseMapY = posY >> FBITS;
int render_start = 0;
int render_count = NUM_COLS;
u8 allow_span_refinement = 1;
u8 near_refinement_cells = DOOM_NEAR_LINE_REFINEMENT_CELLS;
#if DOOM_MOVING_RENDER_COLUMNS < NUM_COLS
if (!wall_first_upload && (render_motion_active || wall_render_scan != 0)) {
render_start = wall_render_scan;
render_count = DOOM_MOVING_RENDER_COLUMNS;
if (render_count < 1) render_count = 1;
}
#endif
#if DOOM_ADAPTIVE_LINE_REFINEMENT
if (wall_frame_overrun) {
allow_span_refinement = 0;
near_refinement_cells = DOOM_OVERRUN_LINE_REFINEMENT_CELLS;
} else if (render_motion_active) {
#if !DOOM_MOVING_SPAN_REFINEMENT
allow_span_refinement = 0;
#endif
near_refinement_cells = DOOM_MOVING_LINE_REFINEMENT_CELLS;
}
#endif
for (int render_i = 0; render_i < render_count; render_i++) {
int x = render_start + render_i;
while (x >= NUM_COLS) x -= NUM_COLS;
fix rayX = rayXbuf[x];
fix rayY = rayYbuf[x];
#if DOOM_RIPDOOM_RENDER
if (rc_render_ripdoom_column(x, rayX, rayY)) continue;
#endif
int mapX = baseMapX;
int mapY = baseMapY;
fix ddX = rayDdXbuf[x];
fix ddY = rayDdYbuf[x];
int stepX = rayStepXbuf[x];
int stepY = rayStepYbuf[x];
fix sideX, sideY;
fix span_perp = FBIG;
u8 span = 0;
u8 span_height = 0;
u8 span_kind = 0;
u8 span_tex = 0;
int span_side = 0;
u8 visual_line = 0;
if (stepX < 0) sideX = fmul(posX - (mapX << FBITS), ddX);
else sideX = fmul(((mapX + 1) << FBITS) - posX, ddX);
if (stepY < 0) sideY = fmul(posY - (mapY << FBITS), ddY);
else sideY = fmul(((mapY + 1) << FBITS) - posY, ddY);
int side = 0; /* 0 = hit on X grid line (N/S) */
unsigned char hit_cell = 1;
for (;;) {
if (sideX < sideY) { sideX += ddX; mapX += stepX; side = 0; }
else { sideY += ddY; mapY += stepY; side = 1; }
if (map_at(mapX, mapY)) {
hit_cell = map_cell_value(mapX, mapY);
break;
} else {
fix line_perp = FBIG;
u8 line_kind = 0;
u8 line_tex = 0;
int line_side = side;
u8 line_span = 0;
u8 line_height = 0;
#if !DOOM_SIMPLE_MAP
if (allow_span_refinement && g_render_cell_count[mapY][mapX] &&
rc_refine_render_line_hit(
rayX, rayY, mapX, mapY,
DOOM_VISUAL_SOLID_LINE_OCCLUSION ? 2 : 1,
&line_perp, &line_kind, &line_tex, &line_side, &line_span, &line_height)) {
int occlude_h = line_span ? projected_span_height(line_perp, line_height) : projected_height(line_perp);
int min_h = line_span ? PORTAL_SPAN_OCCLUDE_MIN_H : PORTAL_SPAN_REPLACE_MIN_H;
if (occlude_h < min_h) continue;
if (line_perp < span_perp) {
span_kind = line_kind;
span_tex = line_tex;
span_side = line_side;
span_perp = line_perp;
span = line_span;
span_height = line_height;
visual_line = 1;
}
}
#endif
continue;
}
}
kindbuf[x] = (hit_cell >= 2) ? (TILE_WALL_ALT_COUNT + 1) : map_cell_texture(mapX, mapY);
fix perp = (side == 0) ? (sideX - ddX) : (sideY - ddY);
if (perp < FMIN) perp = FMIN;
fix wall = (side == 0) ? posY + fmul(perp, rayY) : posX + fmul(perp, rayX);
int tex_x = (int)(((wall & (FONE - 1)) * TILE_WALL_ATLAS_COLS) >> FBITS);
if (tex_x < 0) tex_x = 0;
if (tex_x >= TILE_WALL_ATLAS_COLS) tex_x = TILE_WALL_ATLAS_COLS - 1;
tex_x = (tex_x + map_cell_texture_phase(mapX, mapY)) & (TILE_WALL_ATLAS_COLS - 1);
texbuf[x] = (u8)tex_x;
u8 solid_height = 0;
#if !DOOM_SIMPLE_MAP && (DOOM_SOLID_LINE_REFINEMENT || DOOM_NEAR_LINE_REFINEMENT)
if (g_render_cell_count[mapY][mapX]) {
u8 solid_span = 0;
u8 solid_span_height = 0;
#if DOOM_SOLID_LINE_REFINEMENT
if (rc_refine_render_line_hit(rayX, rayY, mapX, mapY, 0, &perp, &kindbuf[x], &texbuf[x], &side, &solid_span, &solid_span_height)) {
solid_height = solid_span_height;
}
#else
if (near_refinement_cells && perp <= ((fix)near_refinement_cells << FBITS)) {
if (rc_refine_render_line_hit(rayX, rayY, mapX, mapY, 0, &perp, &kindbuf[x], &texbuf[x], &side, &solid_span, &solid_span_height)) {
solid_height = solid_span_height;
}
}
#endif
}
#endif
fix inv_perp = recip(perp);
int full_h = projected_height_from_inv(inv_perp);
int h = full_h; /* slice height px */
if (solid_height && solid_height < 128) {
h = (full_h * solid_height + 63) / 128;
if (h < 2) h = 2;
}
if (visual_line && span_perp < perp) {
int candidate_h = span ? projected_span_height(span_perp, span_height) : projected_height(span_perp);
int far_floor = full_h / PORTAL_SPAN_REPLACE_FAR_DIV;
if (candidate_h >= PORTAL_SPAN_REPLACE_MIN_H && candidate_h >= far_floor) {
kindbuf[x] = span_kind;
texbuf[x] = span_tex;
side = span_side;
perp = span_perp;
inv_perp = recip(perp);
full_h = projected_height_from_inv(inv_perp);
if (!span) h = full_h;
} else {
visual_line = 0;
span = 0;
span_height = 0;
}
}
distbuf[x] = perp;
if (span && span_height) {
h = (full_h * span_height + 63) / 128;
if (h < 2) h = 2;
if (h < PORTAL_SPAN_DRAW_MIN_H) {
span = 0;
span_height = 0;
h = full_h;
}
}
if (h < 1) h = 1;
if (h > MAX_H) h = MAX_H;
/* Keep close walls on the baked Doom texture atlas. The old coarse
* TILE_BRICK fallback avoided shimmer, but it turned nearby E1M1/E1M2
* walls into unreadable flat slabs and hid the converted texture cues. */
closebuf[x] = 0;
#if DOOM_SIMPLE_MAP
int view_h = SCRH;
#else
int view_h = GAME_H;
#endif
int top = (view_h - h) / 2; /* >=0 because h<=view_h */
if (span == 1) {
int bottom = (view_h + full_h) / 2;
if (bottom > view_h) bottom = view_h;