/
clip.cpp
1530 lines (1232 loc) · 52.6 KB
/
clip.cpp
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
// "Build Engine & Tools" Copyright (c) 1993-1997 Ken Silverman
// Ken Silverman's official web site: "http://www.advsys.net/ken"
// See the included license file "BUILDLIC.TXT" for license info.
//
// This file has been modified from Ken Silverman's original release
// by Jonathon Fowler (jf@jonof.id.au)
// by the EDuke32 team (development@voidpoint.com)
#include "build.h"
#include "clip.h"
#include "engine_priv.h"
#include "printf.h"
#include "gamefuncs.h"
enum { MAXCLIPDIST = 1024 };
static int16_t clipnum;
static linetype clipit[MAXCLIPNUM];
static int32_t clipsectnum, origclipsectnum, clipspritenum;
int16_t clipsectorlist[MAXCLIPSECTORS];
static int16_t origclipsectorlist[MAXCLIPSECTORS];
static uint8_t clipsectormap[(MAXSECTORS+7)>>3];
static uint8_t origclipsectormap[(MAXSECTORS+7)>>3];
static int16_t clipobjectval[MAXCLIPNUM];
static uint8_t clipignore[(MAXCLIPNUM+7)>>3];
static int32_t rxi[8], ryi[8];
int32_t quickloadboard=0;
vec2_t hitscangoal = { (1<<29)-1, (1<<29)-1 };
int32_t hitallsprites = 0;
////////// CLIPMOVE //////////
// x1, y1: in/out
// rest x/y: out
template <typename T>
static inline void get_wallspr_points(T const * const spr, int32_t *x1, int32_t *x2,
int32_t *y1, int32_t *y2)
{
//These lines get the 2 points of the rotated sprite
//Given: (x1, y1) starts out as the center point
const int32_t tilenum=spr->picnum, ang=spr->ang;
const int32_t xrepeat = spr->xrepeat;
int32_t xoff = tileLeftOffset(tilenum) + spr->xoffset;
int32_t k, l, dax, day;
if (spr->cstat&4)
xoff = -xoff;
dax = bsin(ang) * xrepeat;
day = -bcos(ang) * xrepeat;
l = tileWidth(tilenum);
k = (l>>1)+xoff;
*x1 -= MulScale(dax,k, 16);
*x2 = *x1 + MulScale(dax,l, 16);
*y1 -= MulScale(day,k, 16);
*y2 = *y1 + MulScale(day,l, 16);
}
// x1, y1: in/out
// rest x/y: out
template <typename T>
static inline void get_floorspr_points(T const * const spr, int32_t px, int32_t py,
int32_t *x1, int32_t *x2, int32_t *x3, int32_t *x4,
int32_t *y1, int32_t *y2, int32_t *y3, int32_t *y4)
{
const int32_t tilenum = spr->picnum;
const int32_t cosang = bcos(spr->ang);
const int32_t sinang = bsin(spr->ang);
vec2_t const span = { tileWidth(tilenum), tileHeight(tilenum)};
vec2_t const repeat = { spr->xrepeat, spr->yrepeat };
vec2_t adjofs = { tileLeftOffset(tilenum) + spr->xoffset, tileTopOffset(tilenum) + spr->yoffset };
if (spr->cstat & 4)
adjofs.x = -adjofs.x;
if (spr->cstat & 8)
adjofs.y = -adjofs.y;
vec2_t const center = { ((span.x >> 1) + adjofs.x) * repeat.x, ((span.y >> 1) + adjofs.y) * repeat.y };
vec2_t const rspan = { span.x * repeat.x, span.y * repeat.y };
vec2_t const ofs = { -MulScale(cosang, rspan.y, 16), -MulScale(sinang, rspan.y, 16) };
*x1 += DMulScale(sinang, center.x, cosang, center.y, 16) - px;
*y1 += DMulScale(sinang, center.y, -cosang, center.x, 16) - py;
*x2 = *x1 - MulScale(sinang, rspan.x, 16);
*y2 = *y1 + MulScale(cosang, rspan.x, 16);
*x3 = *x2 + ofs.x, *x4 = *x1 + ofs.x;
*y3 = *y2 + ofs.y, *y4 = *y1 + ofs.y;
}
int32_t clipmoveboxtracenum = 3;
//
// clipinsidebox
//
int clipinsidebox(vec2_t *vect, int wallnum, int walldist)
{
int const r = walldist << 1;
auto const wal1 = (uwallptr_t)&wall[wallnum];
auto const wal2 = (uwallptr_t)&wall[wal1->point2];
vec2_t const v1 = { wal1->x + walldist - vect->x, wal1->y + walldist - vect->y };
vec2_t v2 = { wal2->x + walldist - vect->x, wal2->y + walldist - vect->y };
if (((v1.x < 0) && (v2.x < 0)) || ((v1.y < 0) && (v2.y < 0)) || ((v1.x >= r) && (v2.x >= r)) || ((v1.y >= r) && (v2.y >= r)))
return 0;
v2.x -= v1.x; v2.y -= v1.y;
if (v2.x * (walldist - v1.y) >= v2.y * (walldist - v1.x)) // Front
{
v2.x *= ((v2.x > 0) ? (0 - v1.y) : (r - v1.y));
v2.y *= ((v2.y > 0) ? (r - v1.x) : (0 - v1.x));
return v2.x < v2.y;
}
v2.x *= ((v2.x > 0) ? (r - v1.y) : (0 - v1.y));
v2.y *= ((v2.y > 0) ? (0 - v1.x) : (r - v1.x));
return (v2.x >= v2.y) << 1;
}
//
// clipinsideboxline
//
int clipinsideboxline(int x, int y, int x1, int y1, int x2, int y2, int walldist)
{
int const r = walldist << 1;
x1 += walldist - x;
x2 += walldist - x;
if (((x1 < 0) && (x2 < 0)) || ((x1 >= r) && (x2 >= r)))
return 0;
y1 += walldist - y;
y2 += walldist - y;
if (((y1 < 0) && (y2 < 0)) || ((y1 >= r) && (y2 >= r)))
return 0;
x2 -= x1;
y2 -= y1;
if (x2 * (walldist - y1) >= y2 * (walldist - x1)) // Front
{
x2 *= ((x2 > 0) ? (0 - y1) : (r - y1));
y2 *= ((y2 > 0) ? (r - x1) : (0 - x1));
return x2 < y2;
}
x2 *= ((x2 > 0) ? (r - y1) : (0 - y1));
y2 *= ((y2 > 0) ? (0 - x1) : (r - x1));
return (x2 >= y2) << 1;
}
static int32_t clipmove_warned;
static inline void addclipsect(int const sectnum)
{
if (clipsectnum < MAXCLIPSECTORS)
{
bitmap_set(clipsectormap, sectnum);
clipsectorlist[clipsectnum++] = sectnum;
}
else
clipmove_warned |= 1;
}
static void addclipline(int32_t dax1, int32_t day1, int32_t dax2, int32_t day2, int16_t daoval, int nofix)
{
if (clipnum >= MAXCLIPNUM)
{
clipmove_warned |= 2;
return;
}
clipit[clipnum].x1 = dax1; clipit[clipnum].y1 = day1;
clipit[clipnum].x2 = dax2; clipit[clipnum].y2 = day2;
clipobjectval[clipnum] = daoval;
uint32_t const mask = (1 << (clipnum&7));
uint8_t &value = clipignore[clipnum>>3];
value = (value & ~mask) | (-nofix & mask);
clipnum++;
}
inline void clipmove_tweak_pos(const vec3_t *pos, int32_t gx, int32_t gy, int32_t x1, int32_t y1, int32_t x2,
int32_t y2, int32_t *daxptr, int32_t *dayptr)
{
int32_t daz;
if (enginecompatibility_mode == ENGINECOMPATIBILITY_19950829 ||
rintersect(pos->x, pos->y, 0, gx, gy, 0, x1, y1, x2, y2, daxptr, dayptr, &daz) == -1)
{
*daxptr = pos->x;
*dayptr = pos->y;
}
}
// Returns: should clip?
static int cliptestsector(int const dasect, int const nextsect, int32_t const flordist, int32_t const ceildist, vec2_t const pos, int32_t const posz)
{
assert((unsigned)dasect < (unsigned)numsectors && (unsigned)nextsect < (unsigned)numsectors);
auto const sec2 = (usectorptr_t)§or[nextsect];
switch (enginecompatibility_mode)
{
case ENGINECOMPATIBILITY_NONE:
break;
default:
{
int32_t daz = getflorzofslope(dasect, pos.x, pos.y);
int32_t daz2 = getflorzofslope(nextsect, pos.x, pos.y);
if (daz2 < daz-(1<<8) && (sec2->floorstat&1) == 0)
if (posz >= daz2-(flordist-1)) return 1;
daz = getceilzofslope(dasect, pos.x, pos.y);
daz2 = getceilzofslope(nextsect, pos.x, pos.y);
if (daz2 > daz+(1<<8) && (sec2->ceilingstat&1) == 0)
if (posz <= daz2+(ceildist-1)) return 1;
return 0;
}
}
int32_t daz2 = sec2->floorz;
int32_t dacz2 = sec2->ceilingz;
if ((sec2->floorstat|sec2->ceilingstat) & 2)
getcorrectzsofslope(nextsect, pos.x, pos.y, &dacz2, &daz2);
if (daz2 <= dacz2)
return 1;
auto const sec = (usectorptr_t)§or[dasect];
int32_t daz = sec->floorz;
int32_t dacz = sec->ceilingz;
if ((sec->floorstat|sec->ceilingstat) & 2)
getcorrectzsofslope(dasect, pos.x, pos.y, &dacz, &daz);
int32_t const sec2height = abs(daz2-dacz2);
return ((abs(daz-dacz) > sec2height && // clip if the current sector is taller and the next is too small
sec2height < (ceildist+(CLIPCURBHEIGHT<<1))) ||
((sec2->floorstat&1) == 0 && // parallaxed floor curbs don't clip
posz >= daz2-(flordist-1) && // also account for desired z distance tolerance
daz2 < daz-CLIPCURBHEIGHT) || // curbs less tall than 256 z units don't clip
((sec2->ceilingstat&1) == 0 &&
posz <= dacz2+(ceildist-1) &&
dacz2 > dacz+CLIPCURBHEIGHT)); // ceilings check the same conditions ^^^^^
}
int32_t clipmovex(vec3_t *pos, int16_t *sectnum,
int32_t xvect, int32_t yvect,
int32_t const walldist, int32_t const ceildist, int32_t const flordist, uint32_t const cliptype,
uint8_t const noslidep)
{
const int32_t oboxtracenum = clipmoveboxtracenum;
if (noslidep)
clipmoveboxtracenum = 1;
int32_t ret = clipmove(pos, sectnum, xvect, yvect,
walldist, ceildist, flordist, cliptype);
clipmoveboxtracenum = oboxtracenum;
return ret;
}
//
// raytrace (internal)
//
static inline int32_t cliptrace(vec2_t const pos, vec2_t * const goal)
{
int32_t hitwall = -1;
for (int z=clipnum-1; z>=0; z--)
{
vec2_t const p1 = { clipit[z].x1, clipit[z].y1 };
vec2_t const p2 = { clipit[z].x2, clipit[z].y2 };
vec2_t const area = { p2.x-p1.x, p2.y-p1.y };
int32_t topu = area.x*(pos.y-p1.y) - (pos.x-p1.x)*area.y;
if (topu <= 0 || area.x*(goal->y-p1.y) > (goal->x-p1.x)*area.y)
continue;
vec2_t const diff = { goal->x-pos.x, goal->y-pos.y };
if (diff.x*(p1.y-pos.y) > (p1.x-pos.x)*diff.y || diff.x*(p2.y-pos.y) <= (p2.x-pos.x)*diff.y)
continue;
int32_t const bot = diff.x*area.y - area.x*diff.y;
native_t cnt = 256;
if (!bot)
continue;
vec2_t n;
do
{
if (--cnt < 0)
{
*goal = pos;
return z;
}
n = { pos.x+Scale(diff.x, topu, bot), pos.y+Scale(diff.y, topu, bot) };
topu--;
} while (area.x*(n.y-p1.y) <= (n.x-p1.x)*area.y);
if (abs(pos.x-n.x)+abs(pos.y-n.y) < abs(pos.x-goal->x)+abs(pos.y-goal->y))
{
*goal = n;
hitwall = z;
}
}
return hitwall;
}
//
// keepaway (internal)
//
static inline void keepaway(int32_t *x, int32_t *y, int32_t w)
{
const int32_t x1 = clipit[w].x1, dx = clipit[w].x2-x1;
const int32_t y1 = clipit[w].y1, dy = clipit[w].y2-y1;
const int32_t ox = Sgn(-dy), oy = Sgn(dx);
char first = (abs(dx) <= abs(dy));
do
{
if (dx*(*y-y1) > (*x-x1)*dy)
return;
if (first == 0)
*x += ox;
else
*y += oy;
first ^= 1;
}
while (1);
}
static int get_floorspr_clipyou(vec2_t const v1, vec2_t const v2, vec2_t const v3, vec2_t const v4)
{
int clipyou = 0;
if ((v1.y^v2.y) < 0)
{
if ((v1.x^v2.x) < 0) clipyou ^= (v1.x*v2.y < v2.x*v1.y)^(v1.y<v2.y);
else if (v1.x >= 0) clipyou ^= 1;
}
if ((v2.y^v3.y) < 0)
{
if ((v2.x^v3.x) < 0) clipyou ^= (v2.x*v3.y < v3.x*v2.y)^(v2.y<v3.y);
else if (v2.x >= 0) clipyou ^= 1;
}
if ((v3.y^v4.y) < 0)
{
if ((v3.x^v4.x) < 0) clipyou ^= (v3.x*v4.y < v4.x*v3.y)^(v3.y<v4.y);
else if (v3.x >= 0) clipyou ^= 1;
}
if ((v4.y^v1.y) < 0)
{
if ((v4.x^v1.x) < 0) clipyou ^= (v4.x*v1.y < v1.x*v4.y)^(v4.y<v1.y);
else if (v4.x >= 0) clipyou ^= 1;
}
return clipyou;
}
static void clipupdatesector(vec2_t const pos, int16_t * const sectnum, int walldist)
{
#if 0
if (enginecompatibility_mode != ENGINECOMPATIBILITY_NONE)
{
updatesector(pos.x, pos.y, sectnum);
return;
}
#endif
if (inside_p(pos.x, pos.y, *sectnum))
return;
int16_t nsecs = min<int16_t>(getsectordist(pos, *sectnum), INT16_MAX);
if (nsecs > (walldist + 8))
{
Printf("%s(): initial position (%d, %d) not within initial sector %d; shortest distance %d.\n", __func__, pos.x, pos.y, *sectnum, nsecs);
walldist = 0x7fff;
}
static int16_t sectlist[MAXSECTORS];
static uint8_t sectbitmap[(MAXSECTORS+7)>>3];
bfirst_search_init(sectlist, sectbitmap, &nsecs, MAXSECTORS, *sectnum);
for (int sectcnt = 0; sectcnt < nsecs; sectcnt++)
{
int const listsectnum = sectlist[sectcnt];
if (inside_p(pos.x, pos.y, listsectnum))
SET_AND_RETURN(*sectnum, listsectnum);
auto const sec = §or[listsectnum];
int const startwall = sec->wallptr;
int const endwall = sec->wallptr + sec->wallnum;
auto uwal = (uwallptr_t)&wall[startwall];
for (int j = startwall; j < endwall; j++, uwal++)
if (uwal->nextsector >= 0 && bitmap_test(clipsectormap, uwal->nextsector))
bfirst_search_try(sectlist, sectbitmap, &nsecs, uwal->nextsector);
}
bfirst_search_init(sectlist, sectbitmap, &nsecs, MAXSECTORS, *sectnum);
for (int sectcnt = 0; sectcnt < nsecs; sectcnt++)
{
int const listsectnum = sectlist[sectcnt];
if (inside_p(pos.x, pos.y, listsectnum))
{
// add sector to clipping list so the next call to clipupdatesector()
// finishes in the loop above this one
addclipsect(listsectnum);
SET_AND_RETURN(*sectnum, listsectnum);
}
auto const sec = §or[listsectnum];
int const startwall = sec->wallptr;
int const endwall = sec->wallptr + sec->wallnum;
auto uwal = (uwallptr_t)&wall[startwall];
// check floor curbs here?
for (int j = startwall; j < endwall; j++, uwal++)
if (uwal->nextsector >= 0 && getwalldist(pos, j) <= (walldist + 8))
bfirst_search_try(sectlist, sectbitmap, &nsecs, uwal->nextsector);
}
*sectnum = -1;
}
//
// clipmove
//
int32_t clipmove(vec3_t * const pos, int16_t * const sectnum, int32_t xvect, int32_t yvect,
int32_t const walldist, int32_t const ceildist, int32_t const flordist, uint32_t const cliptype)
{
if ((xvect|yvect) == 0 || *sectnum < 0)
return 0;
uspriteptr_t curspr=NULL; // non-NULL when handling sprite with sector-like clipping
int const initialsectnum = *sectnum;
int32_t const dawalclipmask = (cliptype & 65535); // CLIPMASK0 = 0x00010001
int32_t const dasprclipmask = FixedToInt(cliptype); // CLIPMASK1 = 0x01000040
vec2_t const move = { xvect, yvect };
vec2_t goal = { pos->x + (xvect >> 14), pos->y + (yvect >> 14) };
vec2_t const cent = { (pos->x + goal.x) >> 1, (pos->y + goal.y) >> 1 };
//Extra walldist for sprites on sector lines
vec2_t const diff = { goal.x - (pos->x), goal.y - (pos->y) };
int32_t const rad = ksqrt(compat_maybe_truncate_to_int32(uhypsq(diff.x, diff.y))) + MAXCLIPDIST + walldist + 8;
vec2_t const clipMin = { cent.x - rad, cent.y - rad };
vec2_t const clipMax = { cent.x + rad, cent.y + rad };
int clipshapeidx = -1;
int clipsectcnt = 0;
int clipspritecnt = 0;
clipsectorlist[0] = *sectnum;
clipsectnum = 1;
clipnum = 0;
clipspritenum = 0;
clipmove_warned = 0;
memset(clipsectormap, 0, (numsectors+7)>>3);
bitmap_set(clipsectormap, *sectnum);
do
{
int const dasect = clipsectorlist[clipsectcnt++];
//if (curspr)
// Printf("sprite %d/%d: sect %d/%d (%d)\n", clipspritecnt,clipspritenum, clipsectcnt,clipsectnum,dasect);
////////// Walls //////////
auto const sec = (usectorptr_t)§or[dasect];
int const startwall = sec->wallptr;
int const endwall = startwall + sec->wallnum;
auto wal = (uwallptr_t)&wall[startwall];
for (native_t j=startwall; j<endwall; j++, wal++)
{
auto const wal2 = (uwallptr_t)&wall[wal->point2];
if ((wal->x < clipMin.x && wal2->x < clipMin.x) || (wal->x > clipMax.x && wal2->x > clipMax.x) ||
(wal->y < clipMin.y && wal2->y < clipMin.y) || (wal->y > clipMax.y && wal2->y > clipMax.y))
continue;
vec2_t p1 = wal->pos;
vec2_t p2 = wal2->pos;
vec2_t d = { p2.x-p1.x, p2.y-p1.y };
if (d.x * (pos->y-p1.y) < (pos->x-p1.x) * d.y)
continue; //If wall's not facing you
vec2_t const r = { (d.y > 0) ? clipMax.x : clipMin.x, (d.x > 0) ? clipMin.y : clipMax.y };
vec2_t v = { d.x * (r.y - p1.y), d.y * (r.x - p1.x) };
if (v.x >= v.y)
continue;
int clipyou = 0;
if (wal->nextsector < 0 || (wal->cstat&dawalclipmask))
{
clipyou = 1;
}
else
{
clipmove_tweak_pos(pos, diff.x, diff.y, p1.x, p1.y, p2.x, p2.y, &v.x, &v.y);
clipyou = cliptestsector(dasect, wal->nextsector, flordist, ceildist, v, pos->z);
}
// We're not interested in any sector reached by portal traversal that we're "inside" of.
if (enginecompatibility_mode == ENGINECOMPATIBILITY_NONE && !curspr && dasect != initialsectnum
&& inside(pos->x, pos->y, dasect) == 1)
{
int k;
for (k=startwall; k<endwall; k++)
if (wall[k].nextsector == initialsectnum)
break;
if (k == endwall)
break;
}
if (clipyou)
{
int16_t const objtype = curspr ? (int16_t)(curspr - sprite) + 49152 : (int16_t)j + 32768;
//Add 2 boxes at endpoints
int32_t bsz = walldist; if (diff.x < 0) bsz = -bsz;
addclipline(p1.x-bsz, p1.y-bsz, p1.x-bsz, p1.y+bsz, objtype, false);
addclipline(p2.x-bsz, p2.y-bsz, p2.x-bsz, p2.y+bsz, objtype, false);
bsz = walldist; if (diff.y < 0) bsz = -bsz;
addclipline(p1.x+bsz, p1.y-bsz, p1.x-bsz, p1.y-bsz, objtype, false);
addclipline(p2.x+bsz, p2.y-bsz, p2.x-bsz, p2.y-bsz, objtype, false);
v.x = walldist; if (d.y > 0) v.x = -v.x;
v.y = walldist; if (d.x < 0) v.y = -v.y;
if (enginecompatibility_mode == ENGINECOMPATIBILITY_NONE && d.x * (pos->y-p1.y-v.y) < (pos->x-p1.x-v.x) * d.y)
v.x >>= 1, v.y >>= 1;
addclipline(p1.x+v.x, p1.y+v.y, p2.x+v.x, p2.y+v.y, objtype, false);
}
else if (wal->nextsector>=0)
{
if (bitmap_test(clipsectormap, wal->nextsector) == 0)
addclipsect(wal->nextsector);
}
}
if (clipmove_warned & 1)
Printf("clipsectnum >= MAXCLIPSECTORS!\n");
if (clipmove_warned & 2)
Printf("clipnum >= MAXCLIPNUM!\n");
////////// Sprites //////////
if (dasprclipmask==0)
continue;
int j;
SectIterator it(dasect);
while ((j = it.NextIndex()) >= 0)
{
auto const spr = (uspriteptr_t)&sprite[j];
const int32_t cstat = spr->cstat;
if ((cstat&dasprclipmask) == 0)
continue;
auto p1 = spr->pos.vec2;
switch (cstat & (CSTAT_SPRITE_ALIGNMENT_WALL | CSTAT_SPRITE_ALIGNMENT_FLOOR))
{
case CSTAT_SPRITE_ALIGNMENT_FACING:
if (p1.x >= clipMin.x && p1.x <= clipMax.x && p1.y >= clipMin.y && p1.y <= clipMax.y)
{
int32_t height, daz = spr->z+spriteheightofs(j, &height, 1);
if (pos->z > daz-height-flordist && pos->z < daz+ceildist)
{
int32_t bsz = (spr->clipdist << 2)+walldist;
if (diff.x < 0) bsz = -bsz;
addclipline(p1.x-bsz, p1.y-bsz, p1.x-bsz, p1.y+bsz, (int16_t)j+49152, false);
bsz = (spr->clipdist << 2)+walldist;
if (diff.y < 0) bsz = -bsz;
addclipline(p1.x+bsz, p1.y-bsz, p1.x-bsz, p1.y-bsz, (int16_t)j+49152, false);
}
}
break;
case CSTAT_SPRITE_ALIGNMENT_WALL:
{
int32_t height, daz = spr->z+spriteheightofs(j, &height, 1);
if (pos->z > daz-height-flordist && pos->z < daz+ceildist)
{
vec2_t p2;
get_wallspr_points(spr, &p1.x, &p2.x, &p1.y, &p2.y);
if (clipinsideboxline(cent.x, cent.y, p1.x, p1.y, p2.x, p2.y, rad) != 0)
{
vec2_t v = { MulScale(bcos(spr->ang + 256), walldist, 14),
MulScale(bsin(spr->ang + 256), walldist, 14) };
if ((p1.x-pos->x) * (p2.y-pos->y) >= (p2.x-pos->x) * (p1.y-pos->y)) // Front
addclipline(p1.x+v.x, p1.y+v.y, p2.x+v.y, p2.y-v.x, (int16_t)j+49152, false);
else
{
if ((cstat & 64) != 0)
continue;
addclipline(p2.x-v.x, p2.y-v.y, p1.x-v.y, p1.y+v.x, (int16_t)j+49152, false);
}
//Side blocker
if ((p2.x-p1.x) * (pos->x-p1.x)+(p2.y-p1.y) * (pos->y-p1.y) < 0)
addclipline(p1.x-v.y, p1.y+v.x, p1.x+v.x, p1.y+v.y, (int16_t)j+49152, true);
else if ((p1.x-p2.x) * (pos->x-p2.x)+(p1.y-p2.y) * (pos->y-p2.y) < 0)
addclipline(p2.x+v.y, p2.y-v.x, p2.x-v.x, p2.y-v.y, (int16_t)j+49152, true);
}
}
break;
}
case CSTAT_SPRITE_ALIGNMENT_FLOOR:
{
if (pos->z > spr->z-flordist && pos->z < spr->z+ceildist)
{
if ((cstat&64) != 0)
if ((pos->z > spr->z) == ((cstat&8)==0))
continue;
rxi[0] = p1.x;
ryi[0] = p1.y;
get_floorspr_points((uspriteptr_t) spr, 0, 0, &rxi[0], &rxi[1], &rxi[2], &rxi[3],
&ryi[0], &ryi[1], &ryi[2], &ryi[3]);
vec2_t v = { MulScale(bcos(spr->ang - 256), walldist, 14),
MulScale(bsin(spr->ang - 256), walldist, 14) };
if ((rxi[0]-pos->x) * (ryi[1]-pos->y) < (rxi[1]-pos->x) * (ryi[0]-pos->y))
{
if (clipinsideboxline(cent.x, cent.y, rxi[1], ryi[1], rxi[0], ryi[0], rad) != 0)
addclipline(rxi[1]-v.y, ryi[1]+v.x, rxi[0]+v.x, ryi[0]+v.y, (int16_t)j+49152, false);
}
else if ((rxi[2]-pos->x) * (ryi[3]-pos->y) < (rxi[3]-pos->x) * (ryi[2]-pos->y))
{
if (clipinsideboxline(cent.x, cent.y, rxi[3], ryi[3], rxi[2], ryi[2], rad) != 0)
addclipline(rxi[3]+v.y, ryi[3]-v.x, rxi[2]-v.x, ryi[2]-v.y, (int16_t)j+49152, false);
}
if ((rxi[1]-pos->x) * (ryi[2]-pos->y) < (rxi[2]-pos->x) * (ryi[1]-pos->y))
{
if (clipinsideboxline(cent.x, cent.y, rxi[2], ryi[2], rxi[1], ryi[1], rad) != 0)
addclipline(rxi[2]-v.x, ryi[2]-v.y, rxi[1]-v.y, ryi[1]+v.x, (int16_t)j+49152, false);
}
else if ((rxi[3]-pos->x) * (ryi[0]-pos->y) < (rxi[0]-pos->x) * (ryi[3]-pos->y))
{
if (clipinsideboxline(cent.x, cent.y, rxi[0], ryi[0], rxi[3], ryi[3], rad) != 0)
addclipline(rxi[0]+v.x, ryi[0]+v.y, rxi[3]+v.y, ryi[3]-v.x, (int16_t)j+49152, false);
}
}
break;
}
}
}
} while (clipsectcnt < clipsectnum || clipspritecnt < clipspritenum);
int32_t hitwalls[4], hitwall;
int32_t clipReturn = 0;
int cnt = clipmoveboxtracenum;
do
{
if (enginecompatibility_mode == ENGINECOMPATIBILITY_NONE && (xvect|yvect))
{
for (int i=clipnum-1;i>=0;--i)
{
if (!bitmap_test(clipignore, i) && clipinsideboxline(pos->x, pos->y, clipit[i].x1, clipit[i].y1, clipit[i].x2, clipit[i].y2, walldist))
{
vec2_t const vec = pos->vec2;
keepaway(&pos->x, &pos->y, i);
if (inside(pos->x,pos->y, *sectnum) != 1)
pos->vec2 = vec;
break;
}
}
}
vec2_t vec = goal;
if ((hitwall = cliptrace(pos->vec2, &vec)) >= 0)
{
vec2_t const clipr = { clipit[hitwall].x2 - clipit[hitwall].x1, clipit[hitwall].y2 - clipit[hitwall].y1 };
// clamp to the max value we can utilize without reworking the scaling below
// this works around the overflow issue that affects dukedc2.map
int32_t const templl = (int32_t)clamp(compat_maybe_truncate_to_int32((int64_t)clipr.x * clipr.x + (int64_t)clipr.y * clipr.y), INT32_MIN, INT32_MAX);
if (templl > 0)
{
// I don't know if this one actually overflows or not, but I highly doubt it hurts to check
int32_t const templl2
= (int32_t)clamp(compat_maybe_truncate_to_int32((int64_t)(goal.x - vec.x) * clipr.x + (int64_t)(goal.y - vec.y) * clipr.y), INT32_MIN, INT32_MAX);
int32_t const i = (enginecompatibility_mode == ENGINECOMPATIBILITY_19950829 || (abs(templl2)>>11) < templl) ?
(int)DivScaleL(templl2, templl, 20) : 0;
goal = { MulScale(clipr.x, i, 20)+vec.x, MulScale(clipr.y, i, 20)+vec.y };
}
int32_t tempint;
if (enginecompatibility_mode == ENGINECOMPATIBILITY_19950829)
tempint = clipr.x*(move.x>>6)+clipr.y*(move.y>>6);
else
tempint = DMulScale(clipr.x, move.x, clipr.y, move.y, 6);
for (native_t i=cnt+1, j; i<=clipmoveboxtracenum; ++i)
{
j = hitwalls[i];
int32_t tempint2;
if (enginecompatibility_mode == ENGINECOMPATIBILITY_19950829)
tempint2 = (clipit[j].x2-clipit[j].x1)*(move.x>>6)+(clipit[j].y2-clipit[j].y1)*(move.y>>6);
else
tempint2 = DMulScale(clipit[j].x2-clipit[j].x1, move.x, clipit[j].y2-clipit[j].y1, move.y, 6);
if ((tempint ^ tempint2) < 0)
{
if (enginecompatibility_mode == ENGINECOMPATIBILITY_19961112)
updatesector(pos->x, pos->y, sectnum);
return clipReturn;
}
}
keepaway(&goal.x, &goal.y, hitwall);
xvect = (goal.x-vec.x)<<14;
yvect = (goal.y-vec.y)<<14;
if (cnt == clipmoveboxtracenum)
clipReturn = (uint16_t) clipobjectval[hitwall];
hitwalls[cnt] = hitwall;
}
if (enginecompatibility_mode == ENGINECOMPATIBILITY_NONE)
clipupdatesector(vec, sectnum, rad);
pos->x = vec.x;
pos->y = vec.y;
cnt--;
} while ((xvect|yvect) != 0 && hitwall >= 0 && cnt > 0);
if (enginecompatibility_mode != ENGINECOMPATIBILITY_NONE)
{
for (native_t j=0; j<clipsectnum; j++)
if (inside(pos->x, pos->y, clipsectorlist[j]) == 1)
{
*sectnum = clipsectorlist[j];
return clipReturn;
}
int32_t tempint2, tempint1 = INT32_MAX;
*sectnum = -1;
for (int j=numsectors-1; j>=0; j--)
if (inside(pos->x, pos->y, j) == 1)
{
if (enginecompatibility_mode != ENGINECOMPATIBILITY_19950829 && (sector[j].ceilingstat&2))
tempint2 = getceilzofslope(j, pos->x, pos->y) - pos->z;
else
tempint2 = sector[j].ceilingz - pos->z;
if (tempint2 > 0)
{
if (tempint2 < tempint1)
{
*sectnum = (int16_t)j;
tempint1 = tempint2;
}
}
else
{
if (enginecompatibility_mode != ENGINECOMPATIBILITY_19950829 && (sector[j].floorstat&2))
tempint2 = pos->z - getflorzofslope(j, pos->x, pos->y);
else
tempint2 = pos->z - sector[j].floorz;
if (tempint2 <= 0)
{
*sectnum = (int16_t)j;
return clipReturn;
}
if (tempint2 < tempint1)
{
*sectnum = (int16_t)j;
tempint1 = tempint2;
}
}
}
}
return clipReturn;
}
//
// pushmove
//
int pushmove(vec3_t *const vect, int16_t *const sectnum,
int32_t const walldist, int32_t const ceildist, int32_t const flordist, uint32_t const cliptype, bool clear /*= true*/)
{
int bad;
const int32_t dawalclipmask = (cliptype&65535);
// const int32_t dasprclipmask = FixedToInt(cliptype);
if (*sectnum < 0)
return -1;
int32_t k = 32;
int dir = 1;
do
{
int32_t clipsectcnt = 0;
bad = 0;
if (clear)
{
if (enginecompatibility_mode != ENGINECOMPATIBILITY_NONE && *sectnum < 0)
return 0;
clipsectorlist[0] = *sectnum;
clipsectnum = 1;
memset(clipsectormap, 0, (numsectors + 7) >> 3);
bitmap_set(clipsectormap, *sectnum);
}
do
{
uwallptr_t wal;
int32_t startwall, endwall;
auto sec = (usectorptr_t)§or[clipsectorlist[clipsectcnt]];
if (dir > 0)
startwall = sec->wallptr, endwall = startwall + sec->wallnum;
else
endwall = sec->wallptr, startwall = endwall + sec->wallnum;
int i;
for (i=startwall, wal=(uwallptr_t)&wall[startwall]; i!=endwall; i+=dir, wal+=dir)
if (clipinsidebox(&vect->vec2, i, walldist-4) == 1)
{
int j = 0;
if (wal->nextsector < 0 || wal->cstat&dawalclipmask) j = 1;
else
{
int32_t daz2;
vec2_t closest;
if (enginecompatibility_mode == ENGINECOMPATIBILITY_19950829)
closest = vect->vec2;
else
{
//Find closest point on wall (dax, day) to (vect->x, vect->y)
int32_t dax = wall[wal->point2].x-wal->x;
int32_t day = wall[wal->point2].y-wal->y;
int32_t daz = dax*((vect->x)-wal->x) + day*((vect->y)-wal->y);
int32_t t;
if (daz <= 0)
t = 0;
else
{
daz2 = dax*dax+day*day;
if (daz >= daz2) t = (1<<30); else t = DivScale(daz, daz2, 30);
}
dax = wal->x + MulScale(dax, t, 30);
day = wal->y + MulScale(day, t, 30);
closest = { dax, day };
}
j = cliptestsector(clipsectorlist[clipsectcnt], wal->nextsector, flordist, ceildist, closest, vect->z);
}
if (j != 0)
{
j = getangle(wall[wal->point2].x-wal->x, wall[wal->point2].y-wal->y);
int32_t dx = -bsin(j, -11);
int32_t dy = bcos(j, -11);
int bad2 = 16;
do
{
vect->x = (vect->x) + dx; vect->y = (vect->y) + dy;
bad2--; if (bad2 == 0) break;
} while (clipinsidebox(&vect->vec2, i, walldist-4) != 0);
bad = -1;
k--; if (k <= 0) return bad;
clipupdatesector(vect->vec2, sectnum, walldist);
if (enginecompatibility_mode == ENGINECOMPATIBILITY_NONE && *sectnum < 0) return -1;
}
else if (bitmap_test(clipsectormap, wal->nextsector) == 0)
addclipsect(wal->nextsector);
}
clipsectcnt++;
} while (clipsectcnt < clipsectnum);
dir = -dir;
} while (bad != 0);
return bad;
}
//
// getzrange
//
void getzrange(const vec3_t *pos, int16_t sectnum,
int32_t *ceilz, int32_t *ceilhit, int32_t *florz, int32_t *florhit,
int32_t walldist, uint32_t cliptype)
{
if (sectnum < 0)
{
*ceilz = INT32_MIN; *ceilhit = -1;
*florz = INT32_MAX; *florhit = -1;
return;
}
int32_t clipsectcnt = 0;
uspriteptr_t curspr=NULL; // non-NULL when handling sprite with sector-like clipping
int32_t curidx=-1, clipspritecnt = 0;
//Extra walldist for sprites on sector lines
const int32_t extradist = walldist+MAXCLIPDIST+1;
const int32_t xmin = pos->x-extradist, ymin = pos->y-extradist;
const int32_t xmax = pos->x+extradist, ymax = pos->y+extradist;
const int32_t dawalclipmask = (cliptype&65535);
const int32_t dasprclipmask = FixedToInt(cliptype);
vec2_t closest = pos->vec2;
if (enginecompatibility_mode == ENGINECOMPATIBILITY_NONE)
getsectordist(closest, sectnum, &closest);
else
getzsofslope(sectnum,closest.x,closest.y,ceilz,florz);
*ceilhit = sectnum+16384; *florhit = sectnum+16384;
clipsectorlist[0] = sectnum;
clipsectnum = 1;
clipspritenum = 0;
memset(clipsectormap, 0, (numsectors+7)>>3);
bitmap_set(clipsectormap, sectnum);