2499
|
1 #define _CRTDBG_MAP_ALLOC
|
|
2 #include <stdlib.h>
|
|
3 #include <crtdbg.h>
|
|
4
|
|
5 #include "OurMath.h"
|
|
6 #include "MM7_data.h"
|
|
7
|
|
8 //----- (00452969) --------------------------------------------------------
|
|
9 stru193_math::stru193_math()
|
|
10 {
|
|
11 double v3; // ST18_8@2
|
|
12
|
|
13 this->pTanTable[0] = 0;
|
|
14 this->pCosTable[0] = 65536;
|
|
15 this->pInvCosTable[0] = 65536;
|
|
16 for(int i = 1; i < (signed int)this->uIntegerHalfPi; i++)
|
|
17 {
|
|
18 v3 = (double)i * 3.141592653589793 / (double)uIntegerPi;
|
|
19 pTanTable[i] = (signed __int64)(tan(v3) * (double)this->pCosTable[0] + 0.5);
|
|
20 pCosTable[i] = (signed __int64)(cos(v3) * (double)this->pCosTable[0] + 0.5);
|
|
21 pInvCosTable[i] = (signed __int64)(1.0 / cos(v3) * (double)this->pCosTable[0] + 0.5);
|
|
22 }
|
|
23 for(int i = this->uIntegerHalfPi; i < 520; i++)
|
|
24 {
|
|
25 this->pTanTable[i] = 0xEFFFFFFFu;
|
|
26 this->pCosTable[i] = 0;
|
|
27 this->pInvCosTable[i] = 0xEFFFFFFFu;
|
|
28 }
|
|
29 }
|
|
30
|
|
31
|
|
32 //----- (00402CAE) --------------------------------------------------------
|
|
33 int stru193_math::Cos(int angle)
|
|
34 {
|
|
35 int v2; // eax@1
|
|
36
|
|
37 //a2: (angle - uIntegerHalfPi) for sin(angle)
|
|
38 // (angle) for cos(angle)
|
|
39
|
|
40 v2 = uDoublePiMask & angle;
|
|
41
|
|
42 if ( v2 > uIntegerPi )
|
|
43 v2 = uIntegerDoublePi - v2;
|
|
44 if ( v2 >= uIntegerHalfPi )
|
|
45 return -pCosTable[uIntegerPi - v2];
|
|
46 else
|
|
47 return pCosTable[v2];
|
|
48 }
|
|
49
|
|
50
|
|
51 //----- (0045281E) --------------------------------------------------------
|
|
52 // Calculates atan2(y/x)
|
|
53 // return value: angle in integer format (multiplier of Pi/1024)
|
|
54 unsigned int stru193_math::Atan2(int x, int y)
|
|
55 {
|
|
56 signed int quadrant;
|
|
57 __int64 dividend;
|
|
58 int quotient;
|
|
59 int lowIdx;
|
|
60 int highIdx;
|
|
61 int angle;
|
|
62
|
|
63 int X = x;
|
|
64 int Y = y;
|
|
65
|
|
66 if ( abs(X) < 65536 )
|
|
67 {
|
|
68 if ( (abs(Y) >> 15) >= abs(X) )
|
|
69 X = 0;
|
|
70 }
|
|
71
|
|
72 if ( !X )
|
|
73 {
|
|
74 if ( Y > 0 )
|
|
75 {
|
|
76 return uIntegerHalfPi; //Pi/2
|
|
77 }
|
|
78 else
|
|
79 {
|
|
80 return uIntegerHalfPi + uIntegerPi; //3*(Pi/2)
|
|
81 }
|
|
82 }
|
|
83
|
|
84 if ( Y )
|
|
85 {
|
|
86 if ( X < 0 )
|
|
87 {
|
|
88 X = -X;
|
|
89 if ( Y > 0 )
|
|
90 {
|
|
91 quadrant = 4;
|
|
92 }
|
|
93 else
|
|
94 {
|
|
95 quadrant = 3;
|
|
96 }
|
|
97 }
|
|
98 else
|
|
99 {
|
|
100 if ( Y > 0 )
|
|
101 {
|
|
102 quadrant = 1;
|
|
103 }
|
|
104 else
|
|
105 {
|
|
106 quadrant = 2;
|
|
107 }
|
|
108 }
|
|
109
|
|
110 if ( Y < 0 )
|
|
111 Y = -Y;
|
|
112
|
|
113 LODWORD(dividend) = Y << 16;
|
|
114 HIDWORD(dividend) = Y >> 16;
|
|
115 quotient = dividend / X;
|
|
116
|
|
117 //looks like binary search
|
|
118 {
|
|
119 int i;
|
|
120 highIdx = uIntegerHalfPi;
|
|
121 lowIdx = 0;
|
|
122
|
|
123 for (i = 0; i < 6; ++i)
|
|
124 {
|
|
125 if (quotient <= pTanTable[(lowIdx + highIdx) / 2])
|
|
126 highIdx = (lowIdx + highIdx) / 2;
|
|
127 else
|
|
128 lowIdx = (lowIdx + highIdx) / 2;
|
|
129 }
|
|
130 }
|
|
131
|
|
132 angle = lowIdx + 1;
|
|
133 while ( angle < (highIdx - 1) && quotient >= pTanTable[angle] )
|
|
134 ++angle;
|
|
135
|
|
136 switch (quadrant)
|
|
137 {
|
|
138 case 1: //X > 0, Y > 0
|
|
139 return angle;
|
|
140
|
|
141 case 2: //X > 0, Y < 0
|
|
142 return uIntegerDoublePi - angle; //2*Pi - angle
|
|
143
|
|
144 case 3: //X > 0, Y < 0
|
|
145 return uIntegerPi + angle; //Pi + angle
|
|
146
|
|
147 case 4: //X < 0, Y > 0
|
|
148 return uIntegerPi - angle; //Pi - angle
|
|
149 }
|
|
150
|
|
151 //should newer get here
|
|
152 return 0;
|
|
153 }
|
|
154
|
|
155 if ( X < 0 ) //Y == 0, X < 0
|
|
156 return uIntegerPi;
|
|
157
|
|
158 return 0;
|
|
159 }
|
|
160
|
|
161 //----- (0042EBDB) --------------------------------------------------------
|
|
162 int stru193_math::Sin(int angle)
|
|
163 {
|
|
164 return Cos(angle - this->uIntegerHalfPi);
|
|
165 }
|
|
166
|
|
167 //----- (0042EBBE) --------------------------------------------------------
|
|
168 //----- (004453C0) mm6-----------------------------------------------------
|
|
169 //----- (004A1760) mm6_chinese---------------------------------------------
|
|
170 __int64 fixpoint_mul(int a1, int a2)
|
|
171 {
|
|
172 return ((__int64)a1 * (__int64)a2) >> 16;
|
|
173 }
|
|
174
|
|
175 __int64 fixpoint_dot(int x1, int x2, int y1, int y2, int z1, int z2)
|
|
176 {
|
|
177 return fixpoint_mul(x1, x2) +
|
|
178 fixpoint_mul(y1, y2) +
|
|
179 fixpoint_mul(z1, z2);
|
|
180 }
|
|
181
|
|
182 //----- (004A1780) mm6_chinese---------------------------------------------
|
|
183 __int64 fixpoint_div(int a1, int a2)
|
|
184 {
|
|
185 return ((__int64)a1 << 16) / a2;
|
|
186 }
|
|
187
|
|
188 __int64 fixpoint_sub_unknown(int a1, int a2)
|
|
189 {
|
|
190 return (((__int64)a1 << 16) * a2) >> 16;
|
|
191 }
|
|
192
|
|
193 //----- (0048B561) --------------------------------------------------------
|
|
194 int fixpoint_from_float(float val)
|
|
195 {
|
|
196 // float X.Yf -> int XXXX YYYY
|
|
197 int left = floorf((val - 0.5f) + 0.5f);
|
|
198 int right = floorf((val - left) * 65536.0f);
|
|
199 return (left << 16) | right;
|
|
200 }
|
|
201
|
|
202 int fixpoint_from_int(int lhv, int rhv)
|
|
203 {
|
|
204 return (lhv << 16) | rhv;
|
|
205 }
|
|
206
|
|
207
|
|
208 //----- (00452A9E) --------------------------------------------------------
|
|
209 int integer_sqrt(int val)
|
|
210 {
|
|
211 ///////////////////////////////
|
|
212 //Получение квадратного корня//
|
|
213 ///////////////////////////////
|
|
214
|
|
215 signed int result; // eax@2
|
|
216 unsigned int v2; // edx@3
|
|
217 unsigned int v3; // edi@3
|
|
218 int v5; // esi@4
|
|
219
|
|
220 if (val < 1)
|
|
221 return val;
|
|
222
|
|
223 v2 = 0;
|
|
224 v3 = val;
|
|
225 result = 0;
|
|
226 for (uint i = 0; i < 16; ++i)
|
|
227 {
|
|
228 result *= 2;
|
|
229 v2 = (v3 >> 30) | 4 * v2;
|
|
230 v5 = 2 * result + 1;
|
|
231 v3 *= 4;
|
|
232 if ( v2 >= v5 )
|
|
233 {
|
|
234 ++result;
|
|
235 v2 -= v5;
|
|
236 }
|
|
237 }
|
|
238 if ( val - result * result >= (unsigned int)(result - 1) )
|
|
239 ++result;
|
|
240 return result;
|
|
241 }
|
|
242
|
|
243 //----- (00452B2E) --------------------------------------------------------
|
|
244 int __fastcall GetDiceResult(unsigned int uNumDice, unsigned int uDiceSides)
|
|
245 {
|
|
246 int v3; // esi@1
|
|
247
|
|
248 v3 = 0;
|
|
249 if ( uDiceSides )
|
|
250 {
|
|
251 for ( uint i = 0; i < uNumDice; ++i)
|
|
252 v3 += rand() % uDiceSides + 1;
|
|
253 return v3;
|
|
254 }
|
|
255 return 0;
|
|
256 }
|