-
Notifications
You must be signed in to change notification settings - Fork 6
/
MainDrawer.HC
3990 lines (3868 loc) · 108 KB
/
MainDrawer.HC
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
#define DOOM_MAIN "Doom"
#exe {Cd(__DIR__);};;
#ifndef PrintI
U0 PrintI(U8*,I64) {}
U0 PrintF(U8*,F64) {}
U0 DbgPrint(U8*,...) {}
#endif
#ifndef MemSetI64
#define MemSetI64 MemSetU64
#endif
#define mp_cnt 1
#define SKY_PIXEL 0xb0
Bool in_level=FALSE,screen_melt=FALSE;
CDC *doom_scrn=DCNew(GR_WIDTH,GR_HEIGHT);
doom_scrn->flags|=DCF_NO_TRANSPARENTS;
extern U0 PlaySound(U8 *name);
CTask *doom_task=Fs;
#include "ScreenMelt.HC"
#include "Serialize.HC";
#include "FastTrig.HC";
#include "Wad.HC";
#include "Intermission.HC";
#include "Thinker.HC";
#include "Music.HC";
F64 DistFromLine(CD2 *p,CD2 *l1,CD2 *l2) {
return Abs((l2->y-l1->y)*p->x-(l2->x-l1->x)*p->y+(l2->x*l1->y)-(l1->x*l2->y)
)/ApproxDist(l2->x-l1->x,l2->y-l1->y);
}
#define MUZZLE_FLASH_LIGHT 50
extern Bool IsNetworkGameServer();
extern Bool IsNetworkGameClient();
extern U0 ClientPressAWall(CDoomLevel *l,CDoomThing *p);
extern U0 ClientPlayerSpawnMissile(CDoomLevel *l,CDoomThing *p,CDoomThing *target,U64 type);
extern U0 ClientPlayerLineAttack(CDoomLevel *l,CDoomThing *p,CDoomThing *target,F64 rnage,F64 angle,F64 damage,Bool melee=FALSE);
extern Bool ClientTriggerLinedef(CDoomLevel *l,CDoomLinedef *ld,I64 flags=0,CDoomThing *t);
extern U0 ClientPlaySound(U8 *name);
extern U0 ClientUpdate(CDoomLevel *l);
Bool IsPlayer(CDoomThing *p) {
if(!p) return FALSE;
if(p->template)
if(1<=p->template->type<=4)
return TRUE;
return FALSE;
}
extern U0 ThingDel(CDoomLevel *l,CDoomThing *t);
extern U0 TeleportThingToPos(CDoomLevel *l,CDoomThing *t,F64,F64,F64);
extern U0 ST_Message(U8*,U8*); //Frist is CDoomPlayer
extern U0 DamageMob(CDoomLevel *l,CDoomThing *target,CDoomThing *opp,F64 damage);
extern CDoomThing **GetCrushedThingsInSector(CDoomLevel *l,CDoomSector *s);
U0 TallBlot(CDC *to,I64 x,I64 y,I64 h,CDC *from) {
I64 i,w=from->width;
if(x+from->width>to->width)
w=to->width-x;
U8 *src,*dst;
for(i=0;i<h;i++) {
dst=&to->body[x+(y+i)*to->width_internal];
src=&from->body[ToI64(i/ToF64(h)*(from->height-2))*from->width_internal];
MemCpy(dst,src,w);
}
}
F64 ray_cast_table_cos[GR_WIDTH/2];
F64 ray_cast_table_sin[GR_WIDTH/2];
Bool paused = FALSE,gameExit=FALSE;
F64 time_offset=0,base_tS=tS;
F64 paused_at=0;
F64 Server_tS() {
if(paused) {
return paused_at;
}
paused_at=tS-base_tS+time_offset;
return paused_at;
}
U0 Unpause(F64 to=-1) {
base_tS=tS;
if(to<0)
time_offset=paused_at;
else
time_offset=to;
paused_at=time_offset;
paused=FALSE;
}
#include "Lighting.HC";
class CJobRunnerData {
U0 (*fptr)(U8*);
};
I64 doom_scrn_in_draw=0;
CDoomSector *GetSectorForCoord(CDoomLevel *level,F64 x,F64 y) {
CDoomSubsector *ss;
CDoomBSPNode *node=DoomWhichBSPNode(x,y,level,&ss);
if(ss) {
return ss->sector;
}
return NULL;
}
class AmmoType{
U64 ammo;
U64 ammo2; //Used for server stuff
Bool splash;
};
class GunAvail{
Bool available;
I16 baseDamage;
AmmoType *load;
};
class CDoomPlayer:CDoomThing {
//rad_suit_time is the time remaining(same for bezerk_time) in ticks
F64 armor,rad_suit_time,bezerk_time,last_attack;
F64 invisible_time;
//These are used for screen palete effects
F64 hurt_amt,pickup_amt;
F64 muzzle_flash_end_tS;
I64 weapon_bob_x,weapon_bob_y,camera_bob;
F64 movement_start_tS;
F64 side_movement_start_tS;
//When you die,the player stares at the thing that killed the player
CDoomThing *stare_at;
GunAvail arsenal[9];
Bool moving,side_moving;
Bool running,made_a_sound,has_backpack,pad[3];
U8 message[STR_LEN];
//Private
#define WEAPON_IDLE 0
#define WEAPON_FIRE 1
#define WEAPON_REFIRE 2
#define WEAPON_PAUSE 3
I64 weapon_state;
F64 weapon_cooldown,message_end_tS,light_goggles_time,invincible_time;
} *p=NULL;
//IMPORTANT
CacheAnimationFrames;
I64 cursor_x = ms.pos.x, cursor_tx = ms.pos.x, cursor_z = ms.pos.z, cursor_tz = ms.pos.z;
class CVisplane {
I64 color;
I64 fminx,fmaxx; //floor
I64 cminx,cmaxx; //ceil
I64 fminy,fmaxy; //floor
I64 cminy,cmaxy; //ceil
CDoomSector *sector;
//Farthest are per x
I32 sector_limits[GR_WIDTH/2];
};
class CDoomCamera {
F64 x,y,h;
F64 angle,fov;
//Private
CDC *floor_dc; //This is black or white,this is used for reducing floor overdraw
I64 visplane_cnt;
CI64Set *draw_walls;
CVisplane visplanes[256];
F64 cutoff_dists[GR_WIDTH/2];
F64 min_ceils[GR_WIDTH/2];
F64 max_floors[GR_WIDTH/2];
F64 max_cutoff_per_core[mp_cnt];
I64 seg_cnts_per_core[mp_cnt]; //Only draw 128 segs per core
} cam;
cam.fov=pi/4;
cam.draw_walls=I64SetNew;
U0 InsertVisplane(CDoomCamera *cam,CDoomSector *s) {
I64 cnt=cam->visplane_cnt;
CVisplane *vp;
if(Gs->num) return;
if(cnt<255) {
vp=&cam->visplanes[cnt];
vp->fminx=GR_WIDTH;
vp->fminy=GR_HEIGHT;
vp->fmaxy=0;
vp->fmaxx=0;
vp->cminx=GR_WIDTH;
vp->cminy=GR_HEIGHT;
vp->cmaxy=0;
vp->cmaxx=0;
vp->sector=s;
vp->color=cnt+1;
s->visplane_idx=cnt;
cam->visplane_cnt++;
MemSetU32(vp->sector_limits,I32_MAX,GR_WIDTH/2);
}
}
#define GRID_SZ 128
#define Y_SCALE 5
#define DIST_SCALE (GR_HEIGHT*1.4)
#define FLOOR_VPIXEL_WIDTH 2
#define WALL_VPIXEL_WIDTH 2
#define WALL_HPIXEL_HEIGHT 2
#define FLOOR_HPIXEL_HEIGHT 2
/*
* Listen up,here's the deal
* A view plane is FLAT,not a point,so when we look at an angle,we
* need to "straigten" our line,otherwise we will have a curved lense effect
*
* Ask Nroot about this
*/
I64 PointToLinedefSegment(CDoomCamera *cam,CDoomLinedef *def,CD2 *coord) {
CD2 line_s,at;
F64 dist;
line_s.x=cam->x;
line_s.y=cam->y;
PlaneIntersect(&at,&line_s,coord,def->start,def->end);
dist=Sqrt(Sqrt(at.x-def->start->x)+Sqr(at.y-def->start->y));
return ToI64(dist);
}
F64 Lerp(F64 per,F64 min,F64 max) {
F64 tmp;
if(min>max)
return Clamp(per*(max-min)+min,max,min);
return Clamp(per*(max-min)+min,min,max);
}
I64 YSort(CD2 *a,CD2 *b) {
if(a->y>b->y)
return 1;
else if(a->y<b->y)
return -1;
return 0;
}
U0 TransformPointToCamera(CDoomCamera *cam,CD2 *p) {
F64 x,y;
F64 angle=FArg(x=p->x-cam->x,y=p->y-cam->y)-cam->angle;
F64 dist=Sqrt(x*x+y*y); //TODO
p->x=dist*FCos(angle);
p->y=dist*FSin(angle);
}
Bool ClipLineToScrenCoords(CDoomCamera *cam,CD2 *p,CD2 *p2,F64 level=0.,Bool *out_of_view=NULL,Bool clamp=TRUE) {
CD2 s,e,at,*ptr,debug;
Bool dummy;
I64 cnt,fail_cnt;
F64 angle,angle2,slump,dist,slump_angle=cam->fov/2;
TransformPointToCamera(cam,p);
TransformPointToCamera(cam,p2);
s.x=0,s.y=0;
slump=1.75;
e.y=I16_MAX*Sin(cam->fov/2),e.x=I16_MAX*Cos(cam->fov/2);
slump=cam->fov/2.;
angle=FArg(p->x,p->y);
angle2=FArg(p2->x,p2->y);
if(!out_of_view) out_of_view=&dummy;
*out_of_view=FALSE;
if(p->x*e.y<e.x*p->y)
if(p2->x*e.y<e.x*p2->y)
*out_of_view=TRUE;
e.y=-e.y;
if(p->x*e.y>e.x*p->y)
if(p2->x*e.y>e.x*p2->y)
*out_of_view=TRUE;
if(p->x<0.)
if(p2->x<0.)
*out_of_view=TRUE;
e.y=-e.y;
if(clamp) {
//Use intersect if above the line
PlaneIntersect(&at,&s,&e,p,p2,FALSE);
if(p->x*e.y-e.x*p->y<-.1) {
p->x=at.x;
p->y=at.y;
}
if(p2->x*e.y-e.x*p2->y<-.1) {
p2->x=at.x;
p2->y=at.y;
}
//Use intersect if below the line
e.y=-e.y;
PlaneIntersect(&at,&s,&e,p,p2,FALSE);
if(p->x*e.y-e.x*p->y>.1) {
p->x=at.x;
p->y=at.y;
}
if(p2->x*e.y-e.x*p2->y>.1) {
p2->x=at.x;
p2->y=at.y;
}
}
for(cnt=0;cnt!=2;cnt++) {
if(!cnt) ptr=p;
else ptr=p2;
angle=FArg(ptr->x,ptr->y);
at.x=GR_WIDTH/2+angle/(cam->fov/2)*(GR_WIDTH/2);
at.y=GR_HEIGHT/2+DIST_SCALE/(Sqrt(ptr->x*ptr->x+ptr->y*ptr->y)*Cos(angle))*(cam->h-level);
ptr->x=at.x;
ptr->y=at.y;
}
return !*out_of_view;
}
I64 F64SortRev(F64 a,F64 b) {
if(a<b) return 1;
if(a>b) return -1;
return 0;
}
I64 I64Sort(I64 a,I64 b) {
return a-b;
}
F64 LightOffset() {
if(!p) return 0;
if(p->light_goggles_time>5) {
return 255.;
} else if(p->light_goggles_time) {
if(Blink)
return 255.;
}
if(Server_tS<p->muzzle_flash_end_tS)
return MUZZLE_FLASH_LIGHT;
return 0.;
}
Bool GodModeFilter() {
if(!p) return FALSE;
if(p->invincible_time>3.)
return TRUE;
else if(p->invincible_time>0.)
return Blink;
return FALSE;
}
class CD4 {
CD2 start;
CD2 end;
};
I64 YSort(CD4 *a,CD4 *b) {
if(a->start.y>b->start.y) return 1;
if(a->start.y<b->start.y) return -1;
return 0;
}
CD4 *GetFlatPoints(CDoomCamera *cam,CDoomSector *s,F64 level,I64 *cnt) {
CD4 *ret=CAlloc(sizeof(CD4)*s->linedef_cnt);
CDoomLinedef *ld;
I64 idx,idx2=0;
CD2 dummy;
Bool out_of_view=0;
for(idx=0;idx!=s->linedef_cnt;idx++) {
ld=s->linedefs[idx];
ret[idx2].start.x=ld->start->x;
ret[idx2].start.y=ld->start->y;
ret[idx2].end.x=ld->end->x;
ret[idx2].end.y=ld->end->y;
ClipLineToScrenCoords(cam,&ret[idx2].start,&ret[idx2].end,level,&out_of_view);
ret[idx2].start.x=ret[idx2].start.x;
ret[idx2].end.x=ret[idx2].end.x;
if(ret[idx2].start.x>=ret[idx2].end.x) {
SwapI64(&ret[idx2].start.x,&ret[idx2].end.x);
SwapI64(&ret[idx2].start.y,&ret[idx2].end.y);
}
if(!out_of_view)
idx2++;
}
if(cnt) *cnt=idx2;
return ret;
}
I64 class CLine64 {
I16 sx,sy,ex,ey;
};
I64 LineYSort(CLine64 a,CLine64 b) {
return a.ey-b.ey;
}
U0 SectorPlot(CDC *dc,CDC *texture,I64 x,I64 y,F64 level,F64 cos2,F64 sin2,I64 light,Bool god_mode) {
x=GR_WIDTH-x;
if(x<0) return;
if(x>GR_WIDTH-2) return;
if(y<0) return;
if(y>GR_HEIGHT-2) return;
U16 *to=&dc->body[x+y*dc->width_internal];
I32 *db_z=&dc->depth_buf[x+y*dc->width_internal];
if(AbsI64(y-GR_HEIGHT/2)<=1) return;
F64 dist=DIST_SCALE/((ToF64(y)-GR_HEIGHT/2)/(cam.h-level));
F64 cos=ray_cast_table_cos[x>>1];
dist/=cos;
I32 idist=dist;
I64 wx,wy,color;
if(*db_z<idist) return;
//Heres the deal. The dither pixels are always aligned to 2 mofo.
//So that means I can write an I64 as two I32s into the depth buffer mofo at once
idist|=idist<<32;
db_z[0](I64)=idist;
db_z+=dc->width_internal;
db_z[0](I64)=idist;
//End of legendary code
wx=cam.x+dist*cos2;
wy=cam.y+dist*sin2;
//wx<<1 multiples by 2,which means any number*2 is even
wx=(wx<<1)&127;
wy=(wy<<1)&127;
color=LookupLighting(texture->body[wx+wy*128](U16),light,dist,god_mode);
*to=color&0xffff;
to(U8*)+=dc->width_internal;
*to=color>>16;
}
U0 FillPoly(CDC *dc,CDoomCamera *cam,CDoomSector *s,CDC *texture,CD4 *points,I64 cnt,F64 level,Bool floor) {
Bool god_mode=GodModeFilter;
I64 idx,idx2,idx3;
CI64Set *lines=I64SetNew();
I64 minx,x,miny;
I64 maxx,cap;
CD3 dummya,dummyb;
F64 slope,wxf,wyf,midx,y,slope2,angle,dist;
I64 scrn_points[0x100],wall_skip;
CLine64 ln,to_remove,ln2;
static F64 last_angle=I16_MAX; //Not possible to start with this angle
static F64 cos_table[GR_WIDTH];
static F64 sin_table[GR_WIDTH];
if(last_angle!=cam->angle) {
for(x=0;x!=GR_WIDTH;x++) {
angle=(ToF64(x)-GR_WIDTH/2)/GR_WIDTH*(cam->fov);
sin_table[x]=Sin(cam->angle+angle);
cos_table[x]=Cos(cam->angle+angle);
}
last_angle=cam->angle;
}
ln.sx=points[0].start.x;
ln.ex=ln.sx;
if(!floor)
ln.sy=0,ln.ey=0;
else
ln.sy=GR_HEIGHT,ln.ey=GR_HEIGHT;
for(idx=0;idx!=cnt;idx++) {
ln.sx=MinI64(points[idx].start.x,ln.sx);
ln.ex=MaxI64(points[idx].end.x,ln.ex);
}
minx=ClampI64(ln.sx,0,GR_WIDTH-2);
maxx=ClampI64(ln.ex,0,GR_WIDTH-2);
I64SetAdd(lines,ln);
for(idx=0;idx!=cnt;idx++) {
ln.sx=points[idx].start.x;
ln.sy=points[idx].start.y;
ln.ex=points[idx].end.x;
ln.ey=points[idx].end.y;
I64SetAdd(lines,ln);
}
minx=minx&~1; //Avoids Templeos bug with &=~1;
minx=ClampI64(minx,GR_WIDTH/mp_cnt*Gs->num,GR_WIDTH/mp_cnt*(Gs->num+1));
maxx=ClampI64(maxx,GR_WIDTH/mp_cnt*Gs->num,GR_WIDTH/mp_cnt*(Gs->num+1));
for(x=minx-2;x<maxx+2;x+=2) {
idx3=0;
for(idx2=0;idx2!=lines->cnt;idx2++) {
ln=lines->body[idx2];
if(!(ln.sx<=x<=ln.ex))
goto skip;
slope=ToF64(ln.ey-ln.sy)/ToF64(ln.ex-ln.sx);
scrn_points[idx3++]=(x-ln.sx)*slope+ln.sy;
skip:;
}
if(idx3)
QSortI64(&scrn_points,idx3,&I64Sort);
for(idx2=0;idx2<idx3-1;idx2++) {
dummya.x=x;
dummyb.x=x;
dummya.y=scrn_points[idx2];
dummyb.y=scrn_points[idx2+1];
//In floors,the lower cordante is the nearest(idx2+1)
if(floor) {
dist=DIST_SCALE/((dummyb.y-GR_HEIGHT/2)/(cam->h-level));
dist/=ray_cast_table_cos[ToI64(x)>>1];
} else {
//In ceils,the upper cordante is the nearest(idx2+0)
dist=DIST_SCALE/((dummya.y-GR_HEIGHT/2)/(cam->h-level));
dist/=ray_cast_table_cos[ToI64(x)>>1];
}
dist=DIST_SCALE/(((dummya.y+dummyb.y)/2-GR_HEIGHT/2)/(cam->h-level));
if(dist>0.) {
dist/=ray_cast_table_cos[ToI64(x)>>1];
wyf=cam->y+dist*sin_table[ToI64(x)];
wxf=cam->x+dist*cos_table[ToI64(x)];
if(GetSectorForCoord(l,wxf,wyf)==s) {
y=MinI64(dummya.y,dummyb.y);
cap=MaxI64(dummya.y,dummyb.y);
y=ToI64(y)&~1;
cap=ToI64(cap)&~1;
y=ClampI64(y-4,0,GR_HEIGHT-2);
cap=ClampI64(cap+4,0,GR_HEIGHT-2);
for(;y<cap;y+=2) {
if(cam->cutoff_dists[ToI64(x)>>1]<dist)
goto next;
SectorPlot(dc,texture,x,y,
level,
cos_table[x],
sin_table[x],
s->cur_light_level,
god_mode);
}
}
}
next:;
}
}
I64SetDel(lines);
}
U0 PopulateTables() {
I64 inc=0;
F64 angle=-cam.fov/2;
for(inc=0;inc!=GR_WIDTH/2;inc++) {
ray_cast_table_cos[inc]=Cos(angle);
ray_cast_table_sin[inc]=Sin(angle);
angle+=cam.fov/GR_WIDTH*2;
}
}
PopulateTables;
//floor is 0 for floor
//floor is 1 for middle
//floor is 2 for ceil
//floor is 3 if visplane only
Bool RayCastWall(CDC *to_dc,CDC *texture,CD2 *st,CD2 *en,CDoomCamera *cam,I64 level,I64 height,I64 flrh,I64 ceilh,U8 floor,CDoomLinedef *ld,CDoomSidedef *sd,Bool same_side) {
I64 light=sd->sector->cur_light_level;
if(ld->front==sd&&ld->back)
light=MaxI64(light,ld->back->sector->cur_light_level);
if(ld->back==sd&&ld->front)
light=MaxI64(light,ld->front->sector->cur_light_level);
light+=LightOffset;
CD2 clone_st,clone_en,zero,clone_ld_st,clone_ld_en;
CD2 scrn_st,scrn_en;
F64 start_angle=FArg(st->x,st->y);
F64 end_angle=FArg(en->x,en->y),tmp,tyf,sy,ey,slope;
F64 view_cos,view_sin;
CD2 ray,hit,*sw;
I64 ix,iy,ih=0,tx,ty,ih2,xoff=sd->xoff-texture->x,yoff=sd->yoff-texture->y,ex,dir,skip,sx;
CVisplane *vp;
I32 *db_z,idist,ex2;
U32 dither_px;
F64 dist,ratio,dist2;
F64 *dists_ptr;
scrn_st.x=st->x;
scrn_st.y=st->y;
scrn_en.x=en->x;
scrn_en.y=en->y;
clone_st.x=st->x,clone_st.y=st->y;
clone_en.x=en->x,clone_en.y=en->y;
clone_ld_st.x=ld->start->x;
clone_ld_st.y=ld->start->y;
clone_ld_en.x=ld->end->x;
clone_ld_en.y=ld->end->y;
zero.x=0.,zero.y=0.;
TransformPointToCamera(cam,&clone_st);
TransformPointToCamera(cam,&clone_en);
TransformPointToCamera(cam,&clone_ld_st);
TransformPointToCamera(cam,&clone_ld_en);
st=&clone_st;
en=&clone_en;
Bool repeat=FALSE,onesided=ld->front^^ld->back,drawn=FALSE;
Bool is_sky=FALSE,clipped=FALSE,recompute_ty,is_sky2=FALSE;
Bool god_mode=GodModeFilter;
I64 low_ih,high_ih;
CDoomSidedef *opposite;
U16 *color,*to;
I64 cutoff_changed=FALSE;
CDoomScroller1 *scroll1;
if(ld->special_type&&ld->special_type->type=='Scroll1'&&sd==ld->front) {
scroll1=ld->special_type;
if(scroll1->x)
xoff+=Server_tS*30*scroll1->x;
else if(scroll1->offset_type) {
yoff=yoff*Server_tS*30;
xoff=xoff*Server_tS*30;
}
}
if(ld->front==sd)
opposite=ld->back;
else
opposite=ld->front;
if(onesided)
is_sky2=sd->sector->ceil_dc[0]==GetFlatDC("F_SKY1");
else if(!sd->upper_texture[0]&&floor==1)
is_sky2=sd->sector->ceil_dc[0]==GetFlatDC("F_SKY1");
else if(floor==2)
is_sky2=sd->sector->ceil_dc[0]==GetFlatDC("F_SKY1");
//Upper walls arent drawn between 2 sky sectors
//is_sky will trigger no drawing wall
if(opposite&&floor==2&&opposite->sector->ceil_dc[0]==GetFlatDC("F_SKY1")) {
return TRUE;
}
ClipLineToScrenCoords(cam,&scrn_st,&scrn_en,level,&clipped);
repeat=TRUE;
if(clipped) return TRUE;
start_angle=FArg(en->x-st->x,en->y-st->y);
sy=scrn_st.y;
ey=scrn_en.y;
sx=scrn_st.x;
ex=scrn_en.x;
ex2=ClampI64(ex,GR_WIDTH/mp_cnt*Gs->num,GR_WIDTH/mp_cnt*(1+Gs->num));
ix=ClampI64(sx,GR_WIDTH/mp_cnt*Gs->num,GR_WIDTH/mp_cnt*(1+Gs->num));
//Avoids Templeos bug with &=~1;
ix=ix&~1;
ex2=ex2&~1;
if(ix<ex2) {
dir=2;
}else {
dir=-2;
}
view_cos=Cos(start_angle);
view_sin=Sin(start_angle);
for(;ix!=ex2;ix+=dir) {
if(ix>GR_WIDTH) {
break;
} else if(ix<0) {
break;
}
tyf=(ey-sy)/ToF64(ex-sx)*(ix-sx)+sy;
if(Abs(tyf-GR_HEIGHT/2)<=2.) {
ray.x=I16_MAX*ray_cast_table_cos[ix/2];
ray.y=I16_MAX*ray_cast_table_sin[ix/2];
PlaneIntersect(&hit,&zero,&ray,st,en);
dist=Sqrt(hit.x*hit.x+hit.y*hit.y);
} else {
dist=DIST_SCALE/((tyf-GR_HEIGHT/2)/(cam->h-level));
dist/=ray_cast_table_cos[ix>>1];
}
hit.x=ray_cast_table_cos[ix/2]*dist;
hit.y=ray_cast_table_sin[ix/2]*dist;
if(cam->cutoff_dists[ix>>1]+2.5<dist) { //+2.5 for margin
goto next;
}
if(dist>0.) {
drawn=TRUE;
idist=dist;
if(dist<=0.) dist=1;
//Use larger value to avoid nigh-zero
if(Abs(view_cos)>Abs(view_sin))
tx=ToI64((hit.x-clone_ld_st.x)/view_cos)*2+xoff*2;
else
tx=ToI64((hit.y-clone_ld_st.y)/view_sin)*2+xoff*2;
iy=DIST_SCALE/(dist*ray_cast_table_cos[ix/2])*(cam->h-(height+level))+GR_HEIGHT/2;
ih=(DIST_SCALE/(dist*ray_cast_table_cos[ix/2])*(cam->h-level)+GR_HEIGHT/2)-iy;
if(texture->flags&DCF_NO_TRANSPARENTS) {
//Middle walls cut off rendering
if(floor==1) { //Solid wall
//2x2 dithering super pixels
cutoff_changed=TRUE;
cam->cutoff_dists[ix>>1]=dist;
}
}
if(ih&1) ih++;
if(iy&1) ih++,iy--;
//Avoids Templeos bug with &=~1;
iy=iy&~1;
ih=ih&~1;
ih2=ih;
ratio=height/ToF64(ih);
if(floor==3) { //If a "visplane" draw
if(0<=ix<=GR_WIDTH-2) {
vp=&cam->visplanes[sd->sector->visplane_idx];
if(vp->sector_limits[ix>>1]<idist||vp->sector_limits[ix>>1]==I32_MAX)
vp->sector_limits[ix>>1]=idist;
for(ty=0;ty!=2;ty++) {
if(!ty)
ih2=0;
else
ih2=ih;
xoff=ClampI64(ix,0,GR_WIDTH-2);
yoff=ClampI64(iy+ih2,1,GR_HEIGHT-2);
if(ty) { //Y grows down
//If our line is partialy behind the camera,we draw the floor from GR_HEIGHT
if(clipped)
vp->fmaxy=GR_HEIGHT;
if(xoff<vp->fminx)
vp->fminx=xoff;
if(xoff>vp->fmaxx)
vp->fmaxx=xoff;
if(yoff<vp->fminy)
vp->fminy=yoff;
if(yoff>vp->fmaxy)
vp->fmaxy=yoff;
} else {
//If our line is partialy behind the camera,we draw the ceil from 0
if(clipped)
vp->cminy=0;
if(xoff<vp->cminx)
vp->cminx=xoff;
if(xoff>vp->cmaxx)
vp->cmaxx=xoff;
if(yoff<vp->cminy)
vp->cminy=yoff;
if(yoff>vp->cmaxy)
vp->cmaxy=yoff;
}
}
}
goto next;
}
if(0<=ix<=GR_WIDTH-2) {
if(is_sky2) {
ih2=ClampI64(iy,0,GR_HEIGHT-2);
while(ih2>=0) {
db_z=&to_dc->depth_buf[GR_WIDTH-2-ix+ih2*to_dc->width_internal];
to=&to_dc->body[GR_WIDTH-2-ix+ih2*to_dc->width_internal];
if(db_z[0]>idist+1) {
db_z[0]=idist+1;
db_z[1]=idist+1;
*to=SKY_PIXEL|SKY_PIXEL<<8;
db_z+=to_dc->width_internal;
to(U8*)+=to_dc->width_internal;
db_z[0]=idist+1;
db_z[1]=idist+1;
*to=SKY_PIXEL|SKY_PIXEL<<8;
}
ih2-=2;
}
}
{
tx%=texture->width;
if(tx<0)
tx+=texture->width;
tx=ClampI64(tx,0,texture->width-2);
//Avoids Templeos bug with &=~1;
tx=tx&~1;
low_ih=0;
ih2=low_ih;
//https://doomwiki.org/wiki/Texture_alignment
if(repeat) {
if(onesided) {
//Dont ask nroot how any of this works,he typed in random stuff
if(ld->flags&WAD_LINEDEFF_LOWER_UNPEGGED) {
tyf=(ih2*ratio+yoff+texture->height-1-ratio*ih)*2;
} else {
tyf=(ih2*ratio+yoff-sd->sector->door_height_offset)*2;
}
} else {
if(floor==0) {
if(ld->flags&WAD_LINEDEFF_LOWER_UNPEGGED) {
tyf=(ih2*ratio+yoff+texture->height-1-ratio*ih+ceilh-flrh)*2;
} else {
tyf=(ih2*ratio+yoff-sd->sector->door_height_offset)*2;
}
} else{
if(!(ld->flags&WAD_LINEDEFF_UPPER_UNPEGGED)) {
tyf=(ih2*ratio+yoff+texture->height-1-ratio*ih-sd->sector->door_height_offset)*2;
} else {
tyf=(ih2*ratio+yoff-sd->sector->door_height_offset)*2;
}
}
}
} else {
tyf=(ToF64(ih2)/ih)*(texture->height-2);
}
for(ih2=low_ih;TRUE;ih2+=2,tyf+=2*ratio*2) { //2 for dither pixel,2 for ih2+=2
if(ih2>=ih||ih2+iy>=GR_HEIGHT-2)
break;
if(iy+ih2<0) {
skip=-iy;
tyf+=2*ratio*(skip-2); // *2 for dither super pixel
ih2+=skip-2;
goto skip_p;
}
db_z=&to_dc->depth_buf[GR_WIDTH-2-ix+(iy+ih2)*to_dc->width_internal];
if(*db_z>idist) {
ty=tyf;
if(repeat)
ty=ty%texture->height;
if(ty<0) ty=-ty;
ty=ClampI64(ty,0,texture->height-2);
//Avoids Templeos bug with &=~1;
ty=ty&~1;
color=texture->body[tx+ty*texture->width_internal](U16);
dither_px=LookupLighting(color,light,dist,god_mode);
to=&to_dc->body[GR_WIDTH-2-ix+(iy+ih2)*to_dc->width_internal];
if(color&0xff!=TRANSPARENT) {
ld->seen=TRUE;
db_z[0]=idist;
db_z[1]=idist;
db_z+=to_dc->width_internal;
db_z[0]=idist;
db_z[1]=idist;
*to=dither_px&0xffff;
to(U8*)+=to_dc->width_internal;
*to=dither_px>>16;
}
}
skip_p:;
}
}
}
}
next:;
}
sd->sector->drawn=TRUE;
if(cutoff_changed) {
ix=0,ex=GR_WIDTH;
dist=0;
ix=GR_WIDTH/mp_cnt*Gs->num,ex=GR_WIDTH/mp_cnt*(1+Gs->num);
//+1/-1 for off-by-1 margins
for(ix=ix>>1+1;ix<ex>>1-1;ix++) {
dist=Max(dist,cam->cutoff_dists[ix]);
if(dist>=U16_MAX) break;
}
cam->max_cutoff_per_core[Gs->num]=dist;
}
return TRUE;
}
class CRayJobData:CJobRunnerData {
CDC *to_dc,*texture;
CD2 *st,*en;
CDoomCamera *cam;
I64 level,height,flrh,ceilh,floor;
F64 dist_from_cam;
CDoomLinedef *ld;
CDoomSidedef *sd;
Bool same_side;
};
I64 DistFromCamSort(CRayJobData *a,CRayJobData *b) {
if(a->dist_from_cam>b->dist_from_cam) return 1;
if(a->dist_from_cam<b->dist_from_cam) return -1;
return 0;
}
Bool RayCastJob(CRayJobData *data) {
return RayCastWall(
data->to_dc,data->texture,data->st,data->en,data->cam,
data->level,
data->height,
data->flrh,
data->ceilh,
data->floor,
data->ld,
data->sd,
data->same_side
);
}
Bool QueRayCastJob(CDC *to_dc,CDC *texture,CD2 *st,CD2 *en,CDoomCamera *cam,I64 level,I64 height,I64 flrh,I64 ceilh,U8 floor,CDoomLinedef *ld,CDoomSidedef *sd,Bool same_side) {
I64 core=0;
CRayJobData *j=CAlloc(sizeof(CRayJobData));
j->fptr=&RayCastJob;
j->to_dc=to_dc;
j->texture=texture;
j->st=st;
j->en=en;
j->cam=cam;
j->height=height;
j->level=level;
j->flrh=flrh;
j->ceilh=ceilh;
j->floor=floor;
j->ld=ld;
j->sd=sd;
j->fptr=&RayCastJob;
j->same_side=same_side;
Bool ret=RayCastJob(j);
Free(j);
return ret;
}
U0 PlotSpriteIn3DSpace(CDC *to_dc,F64 height,CDC *sprite,CDoomCamera *cam,CD2 *_p,F64 level,Bool reflect=FALSE,Bool transparent=FALSE,I64 light=255) {
if(!sprite) return;
CD2 copy,copy2;
F64 new_height,new_width,angle;
I64 x,y,tox,fromx,toy,fromy,ix,iy,dist,best_color;
U16 *from,*dst,black=BLACK|BLACK<<8,trans_pixel=TRANSPARENT|TRANSPARENT<<8;
U32 dither_px;
I32 *db_z;
Bool god_mode=GodModeFilter;
copy.x=_p->x,copy.y=_p->y;
copy2.x=_p->x,copy2.y=_p->y;
ClipLineToScrenCoords(cam,©,©2,level,,FALSE);
angle=(ToF64(copy.x)-GR_WIDTH/2)/(GR_WIDTH/2)*(cam->fov/2);
angle=Clamp(angle,-cam->fov/2,cam->fov/2);
dist=1+Sqrt(Sqr(_p->x-cam->x)+Sqr(_p->y-cam->y));
new_height=DIST_SCALE/(ToF64(1+dist)*FCos(angle))*sprite->height/2;
new_width=new_height/sprite->height*sprite->width;
fromx=copy.x-new_width/2;
tox=ClampI64(copy.x+new_width/2,0,GR_WIDTH-2);;
fromy=copy.y-new_height;
toy=ClampI64(copy.y,0,GR_HEIGHT-2);
//&~1 aligns to 2 for dithering super pixels
//Avoids Templeos bug with &=~1;
fromy=fromy&~1;
toy=toy&~1;
fromx=fromx&~1;
tox=tox&~1;
for(x=MaxI64(fromx,0);x<tox;x+=2) {
if(cam->cutoff_dists[x>>1]>dist) {
for(y=MaxI64(fromy,0);y<toy;y+=2) {
db_z=&to_dc->depth_buf[GR_WIDTH-2-x+y*to_dc->width_internal];
if(*db_z>dist) {
if(!reflect)
ix=Lerp((x-fromx)/new_width,0,sprite->width-2);
else
ix=Lerp((x-fromx)/new_width,sprite->width-2,0);
iy=Lerp((y-fromy)/new_height,0,sprite->height-2);
from=&sprite->body[(ix&~1)+(iy&~1)*sprite->width_internal];
if(transparent) {
if(RandU16<ToI64(0x10000*.75)) {
if(*from!=trans_pixel)
from=&black;
else
from=&trans_pixel;
} else
from=&trans_pixel;
}
dst=&to_dc->body[GR_WIDTH-2-x+y*to_dc->width_internal];
dither_px=LookupLighting(*from,light,dist,god_mode);
if(from->u8[0]!=TRANSPARENT)
*dst=dither_px&0xffff,*db_z=dist,db_z[1]=dist;
dst(U8*)+=to_dc->width_internal;
db_z+=to_dc->width_internal;
if(from->u8[0]!=TRANSPARENT)
*dst=dither_px>>16,*db_z=dist,db_z[1]=dist;
}
}
nextx:;
}
}
}
U0 DrawAThing(CDC *to_dc,CDoomThing *thing,CDoomCamera *cam) {
if(!thing->template) return;
if(thing==p) {return;} //Dont render player
if(!thing->animation) thing->animation=thing->template->_animation;
if(!thing->animation) {
return; //???
}
I64 anim_frame=(Server_tS-thing->anim_start_tS)*4,frame_cnt,face;
if(anim_frame<0) anim_frame=0;
F64 angle,gap,diff;
CDoomSector *sec;
CDC *sprite;
CD2 p;
Bool reflect;
frame_cnt=0;
while(thing->animation[frame_cnt].chr)
frame_cnt++;
if(!frame_cnt)
return;
if(thing->anim_no_repeat)
anim_frame=MinI64(frame_cnt-1,anim_frame);
else
anim_frame%=frame_cnt;
angle=thing->angle;
angle-=cam->angle;
angle+=pi;
face=1;
for(gap=0.;gap<2*pi;gap+=2.*pi/8.) {
diff=(angle-gap)%(2*pi);
if(diff<0) diff+=2*pi;
diff=(diff+3*pi)%(2*pi)-pi;
//https://stackoverflow.com/questions/12234574/calculating-if-an-angle-is-between-two-angles
if(-2*pi/8./2<=diff<=2*pi/8./2)
break;
face++;
}
if(face>8)
face=1;
reflect=thing->animation[anim_frame].reflect[face-1];
sprite=thing->animation[anim_frame].faces[face-1];
sec=GetSectorForCoord(l,thing->x,thing->y);
p.x=thing->x,p.y=thing->y;
if(sec) {
if(thing->template==doom_thing_types[58]) { //Spectre
PlotSpriteIn3DSpace(to_dc,thing->template->height,sprite,cam,&p,thing->height,reflect,TRUE,sec->cur_light_level); //TRUEfor transparent
} else if(sprite)
PlotSpriteIn3DSpace(to_dc,thing->template->height,sprite,cam,&p,thing->height,reflect,,sec->cur_light_level+LightOffset);
}
}
CDoomSector *OppositeSector(CDoomLinedef *l,CDoomSector *sector) {
if(l->front^^l->back) return sector;
if(l->front->sector==sector) return l->back->sector;
return l->front->sector;
}
F64 FloorRaise(CDoomLinedef *l,CDoomSector *sector) {
F64 ret=Max(sector->floor_height,OppositeSector(l,sector)->floor_height);
return ret;
}