1 // SONIC ROBO BLAST 2
2 //-----------------------------------------------------------------------------
3 // Copyright (C) 1993-1996 by id Software, Inc.
4 // Copyright (C) 1998-2000 by DooM Legacy Team.
5 // Copyright (C) 1999-2020 by Sonic Team Junior.
6 //
7 // This program is free software distributed under the
8 // terms of the GNU General Public License, version 2.
9 // See the 'LICENSE' file for more details.
10 //-----------------------------------------------------------------------------
11 /// \file  tables.h
12 /// \brief Lookup tables
13 
14 #ifndef __TABLES__
15 #define __TABLES__
16 
17 #ifdef LINUX
18 #include <math.h>
19 #endif
20 
21 #include "m_fixed.h"
22 
23 #define FINEANGLES 8192
24 #define FINEMASK (FINEANGLES - 1)
25 #define ANGLETOFINESHIFT 19 // 0x100000000 to 0x2000
26 #define FINEANGLE_C(x) ((FixedAngle((x)*FRACUNIT)>>ANGLETOFINESHIFT) & FINEMASK) // ((x*(ANGLE_45/45))>>ANGLETOFINESHIFT) & FINEMASK
27 
28 // Effective size is 10240.
29 extern fixed_t finesine[5*FINEANGLES/4];
30 
31 // Re-use data, is just PI/2 phase shift.
32 extern fixed_t *finecosine;
33 
34 // Effective size is 4096.
35 extern fixed_t finetangent[FINEANGLES/2];
36 
37 #define ANG1   0x00B60B61 //0.B6~
38 #define ANG2   0x016C16C1 //.6C1~
39 #define ANG10  0x071C71C7 //.1C7~
40 #define ANG15  0x0AAAAAAB //A.AA~
41 #define ANG20  0x0E38E38E //.38E~
42 #define ANG30  0x15555555 //.555~
43 #define ANG60  0x2AAAAAAB //A.AA~
44 #define ANG64h 0x2DDDDDDE //D.DD~
45 #define ANG105 0x4AAAAAAB //A.AA~
46 #define ANG210 0x95555555 //.555~
47 #define ANG255 0xB5555555 //.555~
48 #define ANG340 0xF1C71C72 //1.C7~
49 #define ANG350 0xF8E38E39 //8.E3~
50 
51 #define ANGLE_11hh 0x08000000
52 #define ANGLE_22h  0x10000000
53 #define ANGLE_45   0x20000000
54 #define ANGLE_67h  0x30000000
55 #define ANGLE_90   0x40000000
56 #define ANGLE_112h 0x50000000
57 #define ANGLE_135  0x60000000
58 #define ANGLE_157h 0x70000000
59 #define ANGLE_180  0x80000000
60 #define ANGLE_202h 0x90000000
61 #define ANGLE_225  0xA0000000
62 #define ANGLE_247h 0xB0000000
63 #define ANGLE_270  0xC0000000
64 #define ANGLE_292h 0xD0000000
65 #define ANGLE_315  0xE0000000
66 #define ANGLE_337h 0xF0000000
67 #define ANGLE_MAX  0xFFFFFFFF
68 
69 typedef UINT32 angle_t;
70 
71 // To get a global angle from Cartesian coordinates, the coordinates are
72 // flipped until they are in the first octant of the coordinate system, then
73 // the y (<=x) is scaled and divided by x to get a tangent (slope) value
74 // which is looked up in the tantoangle[] table.
75 #define SLOPERANGE 2048
76 #define SLOPEBITS 11
77 #define DBITS (FRACBITS - SLOPEBITS)
78 
79 // The +1 size is to handle the case when x == y without additional checking.
80 extern angle_t tantoangle[SLOPERANGE+1];
81 
82 // Utility function, called by R_PointToAngle.
83 FUNCMATH unsigned SlopeDiv(unsigned num, unsigned den);
84 // Only called by R_PointToAngleEx
85 UINT64 SlopeDivEx(unsigned int num, unsigned int den);
86 
87 // 360 - angle_t(ANGLE_45) = ANGLE_315
InvAngle(angle_t a)88 FUNCMATH FUNCINLINE static ATTRINLINE angle_t InvAngle(angle_t a)
89 {
90 	return (ANGLE_MAX-a)+1;
91 }
92 // angle_t to fixed_t f(ANGLE_45) = 45*FRACUNIT
93 FUNCMATH fixed_t AngleFixed(angle_t af);
94 // fixed_t to angle_t f(45*FRACUNIT) = ANGLE_45
95 FUNCMATH angle_t FixedAngle(fixed_t fa);
96 // and with a factor, with +factor for (fa/factor) and -factor for (fa*factor)
97 FUNCMATH angle_t FixedAngleC(fixed_t fa, fixed_t factor);
98 
99 /// The FixedAcos function
100 FUNCMATH angle_t FixedAcos(fixed_t x);
101 
102 /// Fixed Point Vector functions
103 angle_t FV2_AngleBetweenVectors(const vector2_t *Vector1, const vector2_t *Vector2);
104 angle_t FV3_AngleBetweenVectors(const vector3_t *Vector1, const vector3_t *Vector2);
105 boolean FV2_InsidePolygon(const vector2_t *vIntersection, const vector2_t *Poly, const INT32 vertexCount);
106 boolean FV3_InsidePolygon(const vector3_t *vIntersection, const vector3_t *Poly, const INT32 vertexCount);
107 boolean FV3_IntersectedPolygon(const vector3_t *vPoly, const vector3_t *vLine, const INT32 vertexCount, vector3_t *collisionPoint);
108 void FV3_Rotate(vector3_t *rotVec, const vector3_t *axisVec, const angle_t angle);
109 /// Fixed Point Matrix functions
110 void FM_Rotate(matrix_t *dest, angle_t angle, fixed_t x, fixed_t y, fixed_t z);
111 
112 // The table values in tables.c are calculated with this many fractional bits.
113 #define FINE_FRACBITS 16
114 
115 // These macros should be used in case FRACBITS < FINE_FRACBITS.
116 #define FINESINE(n) (finesine[n]>>(FINE_FRACBITS-FRACBITS))
117 #define FINECOSINE(n) (finecosine[n]>>(FINE_FRACBITS-FRACBITS))
118 #define FINETANGENT(n) (finetangent[n]>>(FINE_FRACBITS-FRACBITS))
119 
120 #endif
121