-
Notifications
You must be signed in to change notification settings - Fork 1
/
voxlap5.cpp
13197 lines (11823 loc) · 386 KB
/
voxlap5.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
// This file has been modified from Ken Silverman's original release
//#include "StdAfx.h"
#include "msvc.h"
#include <math.h>
#include <stdio.h>
#define VOXLAP5
#include "voxlap5.h"
//VOXLAP engine by Ken Silverman (http://advsys.net/ken)
#define USEZBUFFER 1
#define PREC (256*4096)
#define CMPPREC (256*4096)
#define FPREC (256*4096)
#define USEV5ASM 1
#define SCISDIST 1.0
#define GOLDRAT 0.3819660112501052 //Golden Ratio: 1 - 1/((sqrt(5)+1)/2)
#define ESTNORMRAD 2 //Specially optimized for 2: DON'T CHANGE unless testing!
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#define MAX_PATH 260
#endif
#include <stdlib.h>
extern char keystatus[256];
extern long startdirectdraw (long *, long *, long *, long *);
extern void stopdirectdraw ();
extern void nextpage ();
extern void evilquit (const char *);
#define VOXSIZ VSID*VSID*128
#ifdef __cplusplus
extern "C" {
#endif
char *sptr[(VSID*VSID*4)/3];
#ifdef __cplusplus
}
#endif
static long *vbuf = 0, *vbit = 0, vbiti;
//WARNING: loaddta uses last 2MB of vbuf; vbuf:[VOXSIZ>>2], vbit:[VOXSIZ>>7]
//WARNING: loadpng uses last 4MB of vbuf; vbuf:[VOXSIZ>>2], vbit:[VOXSIZ>>7]
// ÚÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄ¿
// vbuf format: ³ 0: ³ 1: ³ 2: ³ 3: ³
//ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄ´
//³ First header: ³ nextptr³ z1 ³ z1c ³ dummy ³
//³ Color 1: ³ b ³ g ³ r ³ intens ³
//³ Color 2: ³ b ³ g ³ r ³ intens ³
//³ ... ³ b ³ g ³ r ³ intens ³
//³ Color n: ³ b ³ g ³ r ³ intens ³
//³ Additional header: ³ nextptr³ z1 ³ z1c ³ z0 ³
//ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÙ
// nextptr: add this # <<2 to index to get to next header (0 if none)
// z1: z floor (top of floor color list)
// z1c: z bottom of floor color list MINUS 1! - needed to calculate
// slab size with slng() and used as a separator for fcol/ccol
// z0: z ceiling (bottom of ceiling color list)
//Memory management variables:
#define MAXCSIZ 1028
char tbuf[MAXCSIZ];
long tbuf2[MAXZDIM*3];
long templongbuf[MAXZDIM];
long cputype = 0; //bit25=1: SSE, bits30&31=1,1:3DNow!+
static char nullst = 0; //nullst always NULL string
#define SETSPHMAXRAD 256
static double logint[SETSPHMAXRAD];
static float tempfloatbuf[SETSPHMAXRAD];
static long factr[SETSPHMAXRAD][2];
#pragma pack(push,1)
//Rendering variables:
#if (USEZBUFFER == 0)
typedef struct { long col; } castdat;
#else
typedef struct { long col, dist; } castdat;
#endif
typedef struct { castdat *i0, *i1; long z0, z1, cx0, cy0, cx1, cy1; } cftype;
typedef struct { unsigned short x, y; } uspoint2d;
typedef struct { long x, y; } lpoint2d;
typedef struct { float x, y; } point2d;
#pragma pack(pop)
#if USEV5ASM
#ifndef __cplusplus
extern void *cfasm;
extern castdat skycast;
#else
extern "C" void *cfasm;
extern "C" castdat skycast;
#endif
#define cf ((cftype *)&cfasm)
#else
cftype cf[256];
#endif
//Screen related variables:
static long xres, yres, bytesperline, frameplace, xres4;
long ylookup[MAXYDIM+1];
static lpoint3d glipos;
static point3d gipos, gistr, gihei, gifor;
static point3d gixs, giys, gizs, giadd;
static float gihx, gihy, gihz, gposxfrac[2], gposyfrac[2], grd;
static long gposz, giforzsgn, gstartz0, gstartz1, gixyi[2];
static char *gstartv;
long backtag, backedup = -1, bacx0, bacy0, bacx1, bacy1;
char *bacsptr[262144];
//Flash variables
#define LOGFLASHVANG 9
static lpoint2d gfc[(1<<LOGFLASHVANG)*8];
static long gfclookup[8] = {4,7,2,5,0,3,6,1}, flashcnt = 0;
__int64 flashbrival;
//Norm flash variables
#define GSIZ 512 //NOTE: GSIZ should be 1<<x, and must be <= 65536
static long bbuf[GSIZ][GSIZ>>5], p2c[32], p2m[32]; //bbuf: 2.0K
static uspoint2d ffx[((GSIZ>>1)+2)*(GSIZ>>1)], *ffxptr; // ffx:16.5K
static long xbsox = -17, xbsoy, xbsof;
static __int64 xbsbuf[25*5+1]; //need few bits before&after for protection
//Look tables for expandbitstack256:
static long xbsceil[32], xbsflor[32];
//float detection & falling code variables...
//WARNING: VLSTSIZ,FSTKSIZ,FLCHKSIZ can all have bounds errors! :(
#define VLSTSIZ 65536 //Theoretically should be at least: VOXSIZ\8
#define LOGHASHEAD 12
#define FSTKSIZ 8192
typedef struct { long v, b; } vlstyp;
vlstyp vlst[VLSTSIZ];
long hhead[1<<LOGHASHEAD], vlstcnt = 0x7fffffff;
lpoint3d fstk[FSTKSIZ]; //Note .z is actually used as a pointer, not z!
#define FLCHKSIZ 4096
lpoint3d flchk[FLCHKSIZ]; long flchkcnt = 0;
//Opticast global variables:
//radar: 320x200 requires 419560*2 bytes (area * 6.56*2)
//radar: 400x300 requires 751836*2 bytes (area * 6.27*2)
//radar: 640x480 requires 1917568*2 bytes (area * 6.24*2)
#define SCPITCH 256
long *radar = 0, *radarmem = 0;
#if (USEZBUFFER == 1)
static long *zbuffermem = 0, zbuffersiz = 0;
#endif
static castdat *angstart[MAXXDIM*4], *gscanptr;
#define CMPRECIPSIZ MAXXDIM+32
static float cmprecip[CMPRECIPSIZ], wx0, wy0, wx1, wy1;
static long iwx0, iwy0, iwx1, iwy1;
static point3d gcorn[4];
point3d ginor[4]; //Should be static, but... necessary for stupid pingball hack :/
static long lastx[max(MAXYDIM,VSID)], uurendmem[MAXXDIM*2+8], *uurend;
void mat0(point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *);
void mat1(point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *);
void mat2(point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *, point3d *);
//Parallaxing sky variables:
static long skypic = 0, nskypic = 0, skybpl, skyysiz, skycurlng, skycurdir;
static float skylngmul;
static point2d *skylng = 0;
#ifdef __cplusplus
extern "C" {
#endif
//Parallaxing sky variables (accessed by assembly code)
long skyoff = 0, skyxsiz, *skylat = 0;
__int64 gi, gcsub[8] =
{
0xff00ff00ff00ff,0xff00ff00ff00ff,0xff00ff00ff00ff,0xff00ff00ff00ff,
0xff00ff00ff00ff,0xff00ff00ff00ff,0xff00ff00ff00ff,0xff00ff00ff00ff
};
long gylookup[512+36], gmipnum = 0; //256+4+128+4+64+4+...
long gpz[2], gdz[2], gxmip, gxmax, gixy[2], gpixy;
static long gmaxscandist;
//long reax, rebx, recx, redx, resi, redi, rebp, resp, remm[16];
void v5_asm_dep_unlock();
void grouscanasm (long);
#if (USEZBUFFER == 1)
long zbufoff;
#endif
#ifdef __cplusplus
}
#endif
#define gi0 (((long *)&gi)[0])
#define gi1 (((long *)&gi)[1])
#ifdef _MSC_VER
#pragma warning(disable:4799) //I know how to use EMMS
static _inline void fcossin (float a, float *c, float *s)
{
_asm
{
fld a
fsincos
mov eax, c
fstp dword ptr [eax]
mov eax, s
fstp dword ptr [eax]
}
}
static _inline void dcossin (double a, double *c, double *s)
{
_asm
{
fld a
fsincos
mov eax, c
fstp qword ptr [eax]
mov eax, s
fstp qword ptr [eax]
}
}
static _inline void ftol (float f, long *a)
{
_asm
{
mov eax, a
fld f
fistp dword ptr [eax]
}
}
static _inline void dtol (double d, long *a)
{
_asm
{
mov eax, a
fld qword ptr d
fistp dword ptr [eax]
}
}
//WARNING: This ASM code requires >= PPRO
static _inline double dbound (double d, double dmin, double dmax)
{
_asm
{
fld dmin
fld d
fucomi st, st(1) ;if (d < dmin)
fcmovb st, st(1) ; d = dmin;
fld dmax
fxch st(1)
fucomi st, st(1) ;if (d > dmax)
fcmovnb st, st(1) ; d = dmax;
fstp d
fucompp
}
return(d);
}
static _inline long mulshr16 (long a, long d)
{
_asm
{
mov eax, a
mov edx, d
imul edx
shrd eax, edx, 16
}
}
static _inline __int64 mul64 (long a, long d)
{
_asm
{
mov eax, a
imul d
}
}
static _inline long shldiv16 (long a, long b)
{
_asm
{
mov eax, a
mov edx, eax
shl eax, 16
sar edx, 16
idiv b
}
}
static _inline long isshldiv16safe (long a, long b)
{
_asm
{
mov edx, a
test edx, edx
js short skipneg0
neg edx
skipneg0:
sar edx, 14
mov eax, b
test eax, eax
js short skipneg1
neg eax
skipneg1:
;abs((a<<16)/b) < (1<<30) ;1 extra for good luck!
;-abs(a)>>14 > -abs(b) ;use -abs because safe for 0x80000000
;eax-edx < 0
sub eax, edx
shr eax, 31
}
}
static _inline long umulshr32 (long a, long d)
{
_asm
{
mov eax, a
mul d
mov eax, edx
}
}
static _inline long scale (long a, long d, long c)
{
_asm
{
mov eax, a
imul d
idiv c
}
}
static _inline long dmulrethigh (long b, long c, long a, long d)
{
_asm
{
mov eax, a
imul d
mov ecx, eax
push edx
mov eax, b
imul c
sub eax, ecx
pop ecx
sbb edx, ecx
mov eax, edx
}
}
static _inline void copybuf (void *s, void *d, long c)
{
_asm
{
push esi
push edi
mov esi, s
mov edi, d
mov ecx, c
rep movsd
pop edi
pop esi
}
}
static _inline void clearbuf (void *d, long c, long a)
{
_asm
{
push edi
mov edi, d
mov ecx, c
mov eax, a
rep stosd
pop edi
}
}
#else
#pragma message ("Compiler says it isn't Visual C.")
#endif
//if (a < 0) return(0); else if (a > b) return(b); else return(a);
static _inline long lbound0 (long a, long b) //b MUST be >= 0
{
if ((unsigned long)a <= b) return(a);
return((~(a>>31))&b);
}
//if (a < b) return(b); else if (a > c) return(c); else return(a);
static _inline long lbound (long a, long b, long c) //c MUST be >= b
{
c -= b;
if ((unsigned long)(a-b) <= c) return(a);
return((((b-a)>>31)&c) + b);
}
#define LSINSIZ 8 //Must be >= 2!
static point2d usintab[(1<<LSINSIZ)+(1<<(LSINSIZ-2))];
static void ucossininit ()
{
long i, j;
double a, ai, s, si, m;
j = 0; usintab[0].y = 0.0;
i = (1<<LSINSIZ)-1;
ai = PI*(-2)/((float)(1<<LSINSIZ)); a = ((float)(-i))*ai;
ai *= .5; m = sin(ai)*2; s = sin(a); si = cos(a+ai)*m; m = -m*m;
for(;i>=0;i--)
{
usintab[i].y = s; s += si; si += s*m; //MUCH faster than next line :)
//usintab[i].y = sin(i*PI*2/((float)(1<<LSINSIZ)));
usintab[i].x = (usintab[j].y-usintab[i].y)/((float)(1<<(32-LSINSIZ)));
j = i;
}
for(i=(1<<(LSINSIZ-2))-1;i>=0;i--) usintab[i+(1<<LSINSIZ)] = usintab[i];
}
//Calculates cos & sin of 32-bit unsigned long angle in ~15 clock cycles
// Accuracy is approximately +/-.0001
static _inline void ucossin (unsigned long a, float *cosin)
{
float f = ((float)(a&((1<<(32-LSINSIZ))-1))); a >>= (32-LSINSIZ);
cosin[0] = usintab[a+(1<<(LSINSIZ-2))].x*f+usintab[a+(1<<(LSINSIZ-2))].y;
cosin[1] = usintab[a ].x*f+usintab[a ].y;
}
static const long font4x6[] = //256 DOS chars, from Ken's Build SMALLFNT
{
0x000000,0x6f9f60,0x69f960,0xaffe40,0x4efe40,0x6ff6f0,0x66f6f0,0x000000,
0xeeaee0,0x000000,0x000000,0x000000,0x000000,0x000000,0x7755c0,0x96f690,
0x8cec80,0x26e620,0x4e4e40,0xaaa0a0,0x7dd550,0x7ca6c0,0x000ee0,0x4e4ee0,
0x4e4440,0x444e40,0x02f200,0x04f400,0x000000,0x000000,0x000000,0x000000,
0x000000,0x444040,0xaa0000,0xafafa0,0x6c46c0,0xa248a0,0x4a4ce0,0x240000,
0x488840,0x422240,0x0a4a00,0x04e400,0x000224,0x00e000,0x000040,0x224480,
0xeaaae0,0x444440,0xe2e8e0,0xe2e2e0,0xaae220,0xe8e2e0,0xe8eae0,0xe22220,
0xeaeae0,0xeae220,0x040400,0x040480,0x248420,0x0e0e00,0x842480,0xc24040,
0xeaece0,0x4aeaa0,0xcacac0,0x688860,0xcaaac0,0xe8c8e0,0xe8c880,0xe8aae0,
0xaaeaa0,0xe444e0,0xe22a60,0xaacaa0,0x8888e0,0xaeeaa0,0xaeeea0,0xeaaae0,
0xeae880,0xeaae60,0xeacaa0,0xe8e2e0,0xe44440,0xaaaae0,0xaaa440,0xaaeea0,
0xaa4aa0,0xaae440,0xe248e0,0xc888c0,0x844220,0x622260,0x4a0000,0x0000e0,
0x420000,0x006a60,0x88eae0,0x00e8e0,0x22eae0,0x006e60,0x24e440,0x06a62c,
0x88eaa0,0x040440,0x040448,0x88aca0,0x444440,0x08eee0,0x00caa0,0x00eae0,
0x00eae8,0x00eae2,0x00e880,0x0064c0,0x04e440,0x00aa60,0x00aa40,0x00eee0,
0x00a4a0,0x00aa6c,0x00c460,0x648460,0x440440,0xc424c0,0x6c0000,0x04ae00,
0x68886c,0xa0aa60,0x606e60,0xe06a60,0xa06a60,0xc06a60,0x046a60,0x00e8e4,
0xe06e60,0xa06e60,0xc06e60,0x0a0440,0x0e0440,0x0c0440,0xa4aea0,0x404ea0,
0x60ece0,0x007a70,0x7afab0,0xe0eae0,0xa0eae0,0xc0eae0,0xe0aa60,0xc0aa60,
0xa0aa6c,0xa0eae0,0xa0aae0,0x4e8e40,0x65c4f0,0xa4ee40,0xcafab0,0x64e4c0,
0x606a60,0x060440,0x60eae0,0x60aa60,0xc0caa0,0xe0aea0,0x6a60e0,0xeae0e0,
0x404860,0x007400,0x00c400,0x8a4e60,0x8a6e20,0x404440,0x05a500,0x0a5a00,
0x282828,0x5a5a5a,0xd7d7d7,0x444444,0x44c444,0x44cc44,0x66e666,0x00e666,
0x00cc44,0x66ee66,0x666666,0x00ee66,0x66ee00,0x66e000,0x44cc00,0x00c444,
0x447000,0x44f000,0x00f444,0x447444,0x00f000,0x44f444,0x447744,0x667666,
0x667700,0x007766,0x66ff00,0x00ff66,0x667766,0x00ff00,0x66ff66,0x44ff00,
0x66f000,0x00ff44,0x00f666,0x667000,0x447700,0x007744,0x007666,0x66f666,
0x44ff44,0x44c000,0x007444,0xffffff,0x000fff,0xcccccc,0x333333,0xfff000,
0x00dad0,0xcacac8,0xea8880,0x00f660,0xe848e0,0x007a40,0x0aac80,0x05a220,
0xe4a4e0,0x4aea40,0x6996f0,0x646ae0,0x06f600,0x16f680,0x68c860,0x4aaaa0,
0xe0e0e0,0x4e40e0,0x4240e0,0x4840e0,0x4a8880,0x222a40,0x40e040,0x06ec00,
0xeae000,0x0cc000,0x00c000,0x644c40,0xcaa000,0xc4e000,0x0eee00,0x000000,
};
void print4x6 (long x, long y, long fcol, long bcol, const char *fmt, ...)
{
va_list arglist;
char st[280], *c;
long i, j;
if (!fmt) return;
va_start(arglist,fmt);
vsprintf(st,fmt,arglist);
va_end(arglist);
y = y*bytesperline+(x<<2)+frameplace;
if (bcol < 0)
{
for(j=20;j>=0;y+=bytesperline,j-=4)
for(c=st,x=y;*c;c++,x+=16)
{
i = (font4x6[*c]>>j);
if (i&8) *(long *)(x ) = fcol;
if (i&4) *(long *)(x+ 4) = fcol;
if (i&2) *(long *)(x+ 8) = fcol;
if (i&1) *(long *)(x+12) = fcol;
if ((*c) == 9) x += 32;
}
return;
}
fcol -= bcol;
for(j=20;j>=0;y+=bytesperline,j-=4)
for(c=st,x=y;*c;c++,x+=16)
{
i = (font4x6[*c]>>j);
*(long *)(x ) = (((i<<28)>>31)&fcol)+bcol;
*(long *)(x+ 4) = (((i<<29)>>31)&fcol)+bcol;
*(long *)(x+ 8) = (((i<<30)>>31)&fcol)+bcol;
*(long *)(x+12) = (((i<<31)>>31)&fcol)+bcol;
if ((*c) == 9) { for(i=16;i<48;i+=4) *(long *)(x+i) = bcol; x += 32; }
}
}
//NOTE: font is stored vertically first! (like .ART files)
static const __int64 font6x8[] = //256 DOS chars, from: DOSAPP.FON (tab blank)
{
0x3E00000000000000,0x6F6B3E003E455145,0x1C3E7C3E1C003E6B,0x3000183C7E3C1800,
0x7E5C180030367F36,0x000018180000185C,0x0000FFFFE7E7FFFF,0xDBDBC3FF00000000,
0x0E364A483000FFC3,0x6000062979290600,0x0A7E600004023F70,0x2A1C361C2A003F35,
0x0800081C3E7F0000,0x7F361400007F3E1C,0x005F005F00001436,0x22007F017F090600,
0x606060002259554D,0x14B6FFB614000060,0x100004067F060400,0x3E08080010307F30,
0x08083E1C0800081C,0x0800404040407800,0x3F3C3000083E083E,0x030F3F0F0300303C,
0x0000000000000000,0x0003070000065F06,0x247E247E24000307,0x630000126A2B2400,
0x5649360063640813,0x0000030700005020,0x00000000413E0000,0x1C3E080000003E41,
0x08083E080800083E,0x0800000060E00000,0x6060000008080808,0x0204081020000000,
0x00003E4549513E00,0x4951620000407F42,0x3649494922004649,0x2F00107F12141800,
0x494A3C0031494949,0x0305097101003049,0x0600364949493600,0x6C6C00001E294949,
0x00006CEC00000000,0x2400004122140800,0x2241000024242424,0x0609590102000814,
0x7E001E555D413E00,0x49497F007E111111,0x224141413E003649,0x7F003E4141417F00,
0x09097F0041494949,0x7A4949413E000109,0x00007F0808087F00,0x4040300000417F41,
0x412214087F003F40,0x7F00404040407F00,0x04027F007F020402,0x3E4141413E007F08,
0x3E00060909097F00,0x09097F005E215141,0x3249494926006619,0x3F0001017F010100,
0x40201F003F404040,0x3F403C403F001F20,0x0700631408146300,0x4549710007087008,
0x0041417F00000043,0x0000201008040200,0x01020400007F4141,0x8080808080800402,
0x2000000007030000,0x44447F0078545454,0x2844444438003844,0x38007F4444443800,
0x097E080008545454,0x7CA4A4A418000009,0x0000007804047F00,0x8480400000407D00,
0x004428107F00007D,0x7C0000407F000000,0x04047C0078041804,0x3844444438000078,
0x380038444444FC00,0x44784400FC444444,0x2054545408000804,0x3C000024443E0400,
0x40201C00007C2040,0x3C6030603C001C20,0x9C00006C10106C00,0x54546400003C60A0,
0x0041413E0800004C,0x0000000077000000,0x02010200083E4141,0x3C2623263C000001,
0x3D001221E1A11E00,0x54543800007D2040,0x7855555520000955,0x2000785554552000,
0x5557200078545555,0x1422E2A21C007857,0x3800085555553800,0x5555380008555455,
0x00417C0100000854,0x0000004279020000,0x2429700000407C01,0x782F252F78007029,
0x3400455554547C00,0x7F097E0058547C54,0x0039454538004949,0x3900003944453800,
0x21413C0000384445,0x007C20413D00007D,0x3D00003D60A19C00,0x40413C00003D4242,
0x002466241800003D,0x29006249493E4800,0x16097F00292A7C2A,0x02097E8840001078,
0x0000785555542000,0x4544380000417D00,0x007D21403C000039,0x7A0000710A097A00,
0x5555080000792211,0x004E51514E005E55,0x3C0020404D483000,0x0404040404040404,
0x506A4C0817001C04,0x0000782A34081700,0x0014080000307D30,0x0814000814001408,
0x55AA114411441144,0xEEBBEEBB55AA55AA,0x0000FF000000EEBB,0x0A0A0000FF080808,
0xFF00FF080000FF0A,0x0000F808F8080000,0xFB0A0000FE0A0A0A,0xFF00FF000000FF00,
0x0000FE02FA0A0000,0x0F0800000F080B0A,0x0F0A0A0A00000F08,0x0000F80808080000,
0x080808080F000000,0xF808080808080F08,0x0808FF0000000808,0x0808080808080808,
0xFF0000000808FF08,0x0808FF00FF000A0A,0xFE000A0A0B080F00,0x0B080B0A0A0AFA02,
0x0A0AFA02FA0A0A0A,0x0A0A0A0AFB00FF00,0xFB00FB0A0A0A0A0A,0x0A0A0B0A0A0A0A0A,
0x0A0A08080F080F08,0xF808F8080A0AFA0A,0x08080F080F000808,0x00000A0A0F000000,
0xF808F8000A0AFE00,0x0808FF00FF080808,0x08080A0AFB0A0A0A,0xF800000000000F08,
0xFFFFFFFFFFFF0808,0xFFFFF0F0F0F0F0F0,0xFF000000000000FF,0x0F0F0F0F0F0FFFFF,
0xFE00241824241800,0x01017F0000344A4A,0x027E027E02000003,0x1800006349556300,
0x2020FC00041C2424,0x000478040800001C,0x3E00085577550800,0x02724C00003E4949,
0x0030595522004C72,0x1800182418241800,0x2A2A1C0018247E24,0x003C02023C00002A,
0x0000002A2A2A2A00,0x4A4A510000242E24,0x00514A4A44000044,0x20000402FC000000,
0x2A08080000003F40,0x0012241224000808,0x0000000609090600,0x0008000000001818,
0x02023E4030000000,0x0900000E010E0100,0x3C3C3C0000000A0D,0x000000000000003C,
};
void print6x8 (long x, long y, long fcol, long bcol, const char *fmt, ...)
{
va_list arglist;
char st[280], *c, *v;
long i, j;
if (!fmt) return;
va_start(arglist,fmt);
vsprintf(st,fmt,arglist);
va_end(arglist);
y = y*bytesperline+(x<<2)+frameplace;
if (bcol < 0)
{
for(j=1;j<256;y+=bytesperline,j<<=1)
for(c=st,x=y;*c;c++,x+=24)
{
v = (char *)(((long)font6x8) + ((long)c[0])*6);
if (v[0]&j) *(long *)(x ) = fcol;
if (v[1]&j) *(long *)(x+ 4) = fcol;
if (v[2]&j) *(long *)(x+ 8) = fcol;
if (v[3]&j) *(long *)(x+12) = fcol;
if (v[4]&j) *(long *)(x+16) = fcol;
if (v[5]&j) *(long *)(x+20) = fcol;
if ((*c) == 9) x += ((2*6)<<2);
}
return;
}
fcol -= bcol;
for(j=1;j<256;y+=bytesperline,j<<=1)
for(c=st,x=y;*c;c++,x+=24)
{
v = (char *)(((long)font6x8) + ((long)c[0])*6);
*(long *)(x ) = (((-(v[0]&j))>>31)&fcol)+bcol;
*(long *)(x+ 4) = (((-(v[1]&j))>>31)&fcol)+bcol;
*(long *)(x+ 8) = (((-(v[2]&j))>>31)&fcol)+bcol;
*(long *)(x+12) = (((-(v[3]&j))>>31)&fcol)+bcol;
*(long *)(x+16) = (((-(v[4]&j))>>31)&fcol)+bcol;
*(long *)(x+20) = (((-(v[5]&j))>>31)&fcol)+bcol;
if ((*c) == 9) { for(i=24;i<72;i+=4) *(long *)(x+i) = bcol; x += ((2*6)<<2); }
}
}
static long gkrand = 0;
long colorjit (long i, long jitamount)
{
gkrand = (gkrand*27584621)+1;
return((gkrand&jitamount)^i);
}
long lightvox (long i)
{
long r, g, b;
b = ((unsigned long)i>>24);
r = min((((i>>16)&255)*b)>>7,255);
g = min((((i>>8 )&255)*b)>>7,255);
b = min((((i )&255)*b)>>7,255);
return((r<<16)+(g<<8)+b);
}
//Note: ebx = 512 is no change
//If PENTIUM III:1.Replace punpcklwd&punpckldq with: pshufw mm1, mm1, 0
// 2.Use pmulhuw, shift by 8 & mul by 256
// :( Can't mix with floating point
//#pragma aux colormul =
// "movd mm0, eax"
// "pxor mm1, mm1"
// "punpcklbw mm0, mm1"
// "psllw mm0, 7"
// "movd mm1, ebx"
// "punpcklwd mm1, mm1"
// "punpckldq mm1, mm1"
// "pmulhw mm0, mm1"
// "packsswb mm0, mm0"
// "movd eax, mm0"
// parm [eax][ebx]
// modify exact [eax]
// value [eax]
long colormul (long i, long mulup8)
{
long r, g, b;
r = ((((i>>16)&255)*mulup8)>>8); if (r > 255) r = 255;
g = ((((i>>8 )&255)*mulup8)>>8); if (g > 255) g = 255;
b = ((((i )&255)*mulup8)>>8); if (b > 255) b = 255;
return((i&0xff000000)+(r<<16)+(g<<8)+b);
}
long curcolfunc (lpoint3d *p) { return(vx5.curcol); }
long floorcolfunc (lpoint3d *p)
{
char *v;
for(v=sptr[p->y*VSID+p->x];(p->z>v[2]) && (v[0]);v+=v[0]*4);
return(*(long *)&v[4]);
}
long jitcolfunc (lpoint3d *p) { return(colorjit(vx5.curcol,vx5.amount)); }
static long manycolukup[64] =
{
0, 1, 2, 5, 10, 15, 21, 29, 37, 47, 57, 67, 79, 90,103,115,
127,140,152,165,176,188,198,208,218,226,234,240,245,250,253,254,
255,254,253,250,245,240,234,226,218,208,198,188,176,165,152,140,
128,115,103, 90, 79, 67, 57, 47, 37, 29, 21, 15, 10, 5, 2, 1
};
long manycolfunc (lpoint3d *p)
{
return((manycolukup[p->x&63]<<16)+(manycolukup[p->y&63]<<8)+manycolukup[p->z&63]+0x80000000);
}
long sphcolfunc (lpoint3d *p)
{
long i;
ftol(sin((p->x+p->y+p->z-vx5.cen)*vx5.daf)*-96,&i);
return(((i+128)<<24)|(vx5.curcol&0xffffff));
}
#define WOODXSIZ 46
#define WOODYSIZ 24
#define WOODZSIZ 24
static float wx[256], wy[256], wz[256], vx[256], vy[256], vz[256];
long woodcolfunc (lpoint3d *p)
{
float col, u, a, f, dx, dy, dz;
long i, c, xof, yof, tx, ty, xoff;
if (*(long *)&wx[0] == 0)
{
for(i=0;i<256;i++)
{
wx[i] = WOODXSIZ * ((float)rand()/32768.0f-.5f) * .5f;
wy[i] = WOODXSIZ * ((float)rand()/32768.0f-.5f) * .5f;
wz[i] = WOODXSIZ * ((float)rand()/32768.0f-.5f) * .5f;
//UNIFORM spherical randomization (see spherand.c)
dz = 1.0f-(float)rand()/32768.0f*.04f;
a = (float)rand()/32768.0f*PI*2.0f; fcossin(a,&dx,&dy);
f = sqrt(1.0f-dz*dz); dx *= f; dy *= f;
//??z: rings, ?z?: vertical, z??: horizontal (nice)
vx[i] = dz; vy[i] = fabs(dy); vz[i] = dx;
}
}
//(tx&,ty&) = top-left corner of current panel
ty = p->y - (p->y%WOODYSIZ);
xoff = ((ty/WOODYSIZ)*(ty/WOODYSIZ)*51721 + (p->z/WOODZSIZ)*357) % WOODXSIZ;
tx = ((p->x+xoff) - (p->x+xoff)%WOODXSIZ) - xoff;
xof = p->x - (tx + (WOODXSIZ>>1));
yof = p->y - (ty + (WOODYSIZ>>1));
c = ((((tx*429 + 4695) ^ (ty*341 + 4355) ^ 13643) * 2797) & 255);
dx = xof - wx[c];
dy = yof - wy[c];
dz = (p->z%WOODZSIZ) - wz[c];
//u = distance to center of randomly oriented cylinder
u = vx[c]*dx + vy[c]*dy + vz[c]*dz;
u = sqrt(dx*dx + dy*dy + dz*dz - u*u);
//ring randomness
u += sin((float)xof*.12 + (float)yof*.15) * .5;
u *= (sin(u)*.05 + 1);
//Ring function: smooth saw-tooth wave
col = sin(u*2)*24;
col *= pow(1.f-vx[c],.3f);
//Thin shaded borders
if ((p->x-tx == 0) || (p->y-ty == 0)) col -= 5;
if ((p->x-tx == WOODXSIZ-1) || (p->y-ty == WOODYSIZ-1)) col -= 3;
//f = col+c*.12+72; i = ftolp3(&f);
ftol(col+c*.12f+72.0f,&i);
return(colormul(vx5.curcol,i<<1));
}
long gxsizcache = 0, gysizcache = 0;
long pngcolfunc (lpoint3d *p)
{
long x, y, z, u, v;
float fx, fy, fz, rx, ry, rz;
if (!vx5.pic) return(vx5.curcol);
switch(vx5.picmode)
{
case 0:
x = p->x-vx5.pico.x; y = p->y-vx5.pico.y; z = p->z-vx5.pico.z;
u = (((x&vx5.picu.x) + (y&vx5.picu.y) + (z&vx5.picu.z))^vx5.xoru);
v = (((x&vx5.picv.x) + (y&vx5.picv.y) + (z&vx5.picv.z))^vx5.xorv);
break;
case 1: case 2:
fx = (float)p->x-vx5.fpico.x;
fy = (float)p->y-vx5.fpico.y;
fz = (float)p->z-vx5.fpico.z;
rx = vx5.fpicu.x*fx + vx5.fpicu.y*fy + vx5.fpicu.z*fz;
ry = vx5.fpicv.x*fx + vx5.fpicv.y*fy + vx5.fpicv.z*fz;
rz = vx5.fpicw.x*fx + vx5.fpicw.y*fy + vx5.fpicw.z*fz;
ftol(atan2(ry,rx)*vx5.xoru/(PI*2),&u);
if (vx5.picmode == 1) ftol(rz,&v);
else ftol((atan2(rz,sqrt(rx*rx+ry*ry))/PI+.5)*vx5.ysiz,&v);
break;
default: //case 3:
fx = (float)p->x-vx5.fpico.x;
fy = (float)p->y-vx5.fpico.y;
fz = (float)p->z-vx5.fpico.z;
ftol(vx5.fpicu.x*fx + vx5.fpicu.y*fy + vx5.fpicu.z*fz,&u);
ftol(vx5.fpicv.x*fx + vx5.fpicv.y*fy + vx5.fpicv.z*fz,&v);
break;
}
if ((unsigned long)(u-gxsizcache) >= (unsigned long)vx5.xsiz)
if (u < 0) gxsizcache = u-(u+1)%vx5.xsiz-vx5.xsiz+1; else gxsizcache = u-(u%vx5.xsiz);
if ((unsigned long)(v-gysizcache) >= (unsigned long)vx5.ysiz)
if (v < 0) gysizcache = v-(v+1)%vx5.ysiz-vx5.ysiz+1; else gysizcache = v-(v%vx5.ysiz);
return((vx5.pic[(v-gysizcache)*(vx5.bpl>>2)+(u-gxsizcache)]&0xffffff)|0x80000000);
}
//Special case for SETSEC & SETCEI bumpmapping (vx5.picmode == 3)
//no safety checks, returns alpha as signed char in range: (-128 to 127)
long hpngcolfunc (point3d *p)
{
long u, v;
float fx, fy, fz;
fx = p->x-vx5.fpico.x;
fy = p->y-vx5.fpico.y;
fz = p->z-vx5.fpico.z;
ftol(vx5.fpicu.x*fx + vx5.fpicu.y*fy + vx5.fpicu.z*fz,&u);
ftol(vx5.fpicv.x*fx + vx5.fpicv.y*fy + vx5.fpicv.z*fz,&v);
if ((unsigned long)(u-gxsizcache) >= (unsigned long)vx5.xsiz)
if (u < 0) gxsizcache = u-(u+1)%vx5.xsiz-vx5.xsiz+1; else gxsizcache = u-(u%vx5.xsiz);
if ((unsigned long)(v-gysizcache) >= (unsigned long)vx5.ysiz)
if (v < 0) gysizcache = v-(v+1)%vx5.ysiz-vx5.ysiz+1; else gysizcache = v-(v%vx5.ysiz);
return(vx5.pic[(v-gysizcache)*(vx5.bpl>>2)+(u-gxsizcache)]>>24);
}
static long slng (const char *s)
{
const char *v;
for(v=s;v[0];v+=v[0]*4);
return((long)v-(long)s+(v[2]-v[1]+1)*4+4);
}
void voxdealloc (const char *v)
{
long i, j;
i = (((long)v-(long)vbuf)>>2); j = (slng(v)>>2)+i;
#if 0
while (i < j) { vbit[i>>5] &= ~(1<<i); i++; }
#else
if (!((j^i)&~31))
vbit[i>>5] &= ~(p2m[j&31]^p2m[i&31]);
else
{
vbit[i>>5] &= p2m[i&31]; i >>= 5;
vbit[j>>5] &= (~p2m[j&31]); j >>= 5;
for(j--;j>i;j--) vbit[j] = 0;
}
#endif
}
//Note: danum MUST be a multiple of 4!
char *voxalloc (long danum)
{
long i, badcnt, p0, p1, vend;
badcnt = 0; danum >>= 2; vend = (VOXSIZ>>2)-danum;
do
{
for(;vbiti<vend;vbiti+=danum)
{
if (vbit[vbiti>>5]&(1<<vbiti)) continue;
for(p0=vbiti;(!(vbit[(p0-1)>>5]&(1<<(p0-1))));p0--);
for(p1=p0+danum-1;p1>vbiti;p1--)
if (vbit[p1>>5]&(1<<p1)) goto allocnothere;
vbiti = p0+danum;
for(i=p0;i<vbiti;i++) vbit[i>>5] |= (1<<i);
return((char *)(&vbuf[p0]));
allocnothere:;
}
vbiti = 0; badcnt++;
} while (badcnt < 2);
evilquit("voxalloc: vbuf full"); return(0);
}
long isvoxelsolid (long x, long y, long z)
{
char *v;
if ((unsigned long)(x|y) >= VSID) return(0);
v = sptr[y*VSID+x];
while (1)
{
if (z < v[1]) return(0);
if (!v[0]) return(1);
v += v[0]*4;
if (z < v[3]) return(1);
}
}
//Returns 1 if any voxels in range (x,y,z0) to (x,y,z1-1) are solid, else 0
long anyvoxelsolid (long x, long y, long z0, long z1)
{
char *v;
// v1.....v3 v1.....v3 v1.......................>
// z0.........z1
if ((unsigned long)(x|y) >= VSID) return(0);
v = sptr[y*VSID+x];
while (1)
{
if (z1 <= v[1]) return(0);
if (!v[0]) return(1);
v += v[0]*4;
if (z0 < v[3]) return(1);
}
}
//Returns 1 if any voxels in range (x,y,z0) to (x,y,z1-1) are empty, else 0
long anyvoxelempty (long x, long y, long z0, long z1)
{
char *v;
// v1.....v3 v1.....v3 v1.......................>
// z0.........z1
if ((unsigned long)(x|y) >= VSID) return(1);
v = sptr[y*VSID+x];
while (1)
{
if (z0 < v[1]) return(1);
if (!v[0]) return(0);
v += v[0]*4;
if (z1 <= v[3]) return(0);
}
}
//Returns z of first solid voxel under (x,y,z). Returns z if in solid.
long getfloorz (long x, long y, long z)
{
char *v;
if ((unsigned long)(x|y) >= VSID) return(z);
v = sptr[y*VSID+x];
while (1)
{
if (z <= v[1]) return(v[1]);
if (!v[0]) break;
v += v[0]*4;
if (z < v[3]) break;
}
return(z);
}
//Returns:
// 0: air
// 1: unexposed solid
//else: address to color in vbuf (this can never be 0 or 1)
long getcube (long x, long y, long z)
{
long ceilnum;
char *v;
if ((unsigned long)(x|y) >= VSID) return(0);
v = sptr[y*VSID+x];
while (1)
{
if (z <= v[2])
{
if (z < v[1]) return(0);
return((long)&v[(z-v[1])*4+4]);
}
ceilnum = v[2]-v[1]-v[0]+2;
if (!v[0]) return(1);
v += v[0]*4;
if (z < v[3])
{
if (z-v[3] < ceilnum) return(1);
return((long)&v[(z-v[3])*4]);
}
}
}
// Inputs: uind[MAXZDIM]: uncompressed 32-bit color buffer (-1: air)
// nind?[MAXZDIM]: neighbor buf:
// -2: unexposed solid
// -1: air
// 0-16777215: exposed solid (color)
// px,py: parameters for setting unexposed voxel colors
//Outputs: cbuf[MAXCSIZ]: compressed output buffer
//Returns: n: length of compressed buffer (in bytes)
long compilestack (long *uind, long *n0, long *n1, long *n2, long *n3, char *cbuf, long px, long py)
{
long oz, onext, n, cp2, cp1, cp0, rp1, rp0;
lpoint3d p;
p.x = px; p.y = py;
//Do top slab (sky)
oz = -1;
p.z = -1; while (uind[p.z+1] == -1) p.z++;
onext = 0;
cbuf[1] = p.z+1;
cbuf[2] = p.z+1;
cbuf[3] = 0; //Top z0 (filler, not used yet)
n = 4;
cp1 = 1; cp0 = 0;
rp1 = -1; rp0 = -1;
do
{
//cp2 = state at p.z-1 (0 = air, 1 = next2air, 2 = solid)
//cp1 = state at p.z (0 = air, 1 = next2air, 2 = solid)
//cp0 = state at p.z+1 (0 = air, 1 = next2air, 2 = solid)
cp2 = cp1; cp1 = cp0; cp0 = 2;
if (p.z < MAXZDIM-2) //Bottom must be solid!
{
if (uind[p.z+1] == -1)
cp0 = 0;
else if ((n0[p.z+1] == -1) || (n1[p.z+1] == -1) ||