World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
OurMath.cpp
См. документацию.
1 #include "Engine/OurMath.h"
2 #include "Engine/Engine.h"
3 
4 //----- (00452969) --------------------------------------------------------
6  double v3; // ST18_8@2
7 
8  this->pTanTable[0] = 0;
9  this->pCosTable[0] = 65536;
10  this->pInvCosTable[0] = 65536;
11  for (int i = 1; i < (signed int)this->uIntegerHalfPi; i++) {
12  v3 = (double)i * pi_double / (double)uIntegerPi;
13  pTanTable[i] =
14  (signed __int64)(tan(v3) * (double)this->pCosTable[0] + 0.5);
15  pCosTable[i] =
16  (signed __int64)(cos(v3) * (double)this->pCosTable[0] + 0.5);
17  pInvCosTable[i] =
18  (signed __int64)(1.0 / cos(v3) * (double)this->pCosTable[0] + 0.5);
19  }
20  for (int i = this->uIntegerHalfPi; i < 520; i++) {
21  this->pTanTable[i] = 0xEFFFFFFFu;
22  this->pCosTable[i] = 0;
23  this->pInvCosTable[i] = 0xEFFFFFFFu;
24  }
25 }
26 
27 //----- (00402CAE) --------------------------------------------------------
29  int v2; // eax@1
30 
31  // a2: (angle - uIntegerHalfPi) for sin(angle)
32  // (angle) for cos(angle)
33 
35 
36  if (v2 > uIntegerPi) v2 = uIntegerDoublePi - v2;
37  if (v2 >= uIntegerHalfPi)
38  return -pCosTable[uIntegerPi - v2];
39  else
40  return pCosTable[v2];
41 }
42 
43 //----- (0045281E) --------------------------------------------------------
44 // Calculates atan2(y/x)
45 // return value: angle in integer format (multiplier of Pi/1024)
46 unsigned int stru193_math::Atan2(int x, int y) {
47  signed int quadrant;
48  __int64 dividend;
49  int quotient;
50  int lowIdx;
51  int highIdx;
52  int angle;
53 
54  int X = x;
55  int Y = y;
56 
57  if (abs(X) < 65536) {
58  if ((abs(Y) >> 15) >= abs(X)) X = 0;
59  }
60 
61  if (!X) {
62  if (Y > 0) {
63  return uIntegerHalfPi; // Pi/2
64  } else {
65  return uIntegerHalfPi + uIntegerPi; // 3*(Pi/2)
66  }
67  }
68 
69  if (Y) {
70  if (X < 0) {
71  X = -X;
72  if (Y > 0) {
73  quadrant = 4;
74  } else {
75  quadrant = 3;
76  }
77  } else {
78  if (Y > 0) {
79  quadrant = 1;
80  } else {
81  quadrant = 2;
82  }
83  }
84 
85  if (Y < 0) Y = -Y;
86 
87  HEXRAYS_LODWORD(dividend) = Y << 16;
88  HEXRAYS_HIDWORD(dividend) = Y >> 16;
89  quotient = dividend / X;
90 
91  // looks like binary search
92  {
93  int i;
94  highIdx = uIntegerHalfPi;
95  lowIdx = 0;
96 
97  for (i = 0; i < 6; ++i) {
98  if (quotient <= pTanTable[(lowIdx + highIdx) / 2])
99  highIdx = (lowIdx + highIdx) / 2;
100  else
101  lowIdx = (lowIdx + highIdx) / 2;
102  }
103  }
104 
105  angle = lowIdx + 1;
106  while (angle < (highIdx - 1) && quotient >= pTanTable[angle]) ++angle;
107 
108  switch (quadrant) {
109  case 1: // X > 0, Y > 0
110  return angle;
111 
112  case 2: // X > 0, Y < 0
113  return uIntegerDoublePi - angle; // 2*Pi - angle
114 
115  case 3: // X > 0, Y < 0
116  return uIntegerPi + angle; // Pi + angle
117 
118  case 4: // X < 0, Y > 0
119  return uIntegerPi - angle; // Pi - angle
120  }
121 
122  // should never get here
123  return 0;
124  }
125 
126  if (X < 0) // Y == 0, X < 0
127  return uIntegerPi;
128 
129  return 0;
130 }
131 
132 //----- (0042EBDB) --------------------------------------------------------
133 int stru193_math::Sin(int angle) { return Cos(angle - this->uIntegerHalfPi); }
134 
135 //----- (0042EBBE) --------------------------------------------------------
136 //----- (004453C0) mm6-----------------------------------------------------
137 //----- (004A1760) mm6_chinese---------------------------------------------
138 __int64 fixpoint_mul(int a1, int a2) {
139  return ((__int64)a1 * (__int64)a2) >> 16;
140 }
141 
142 __int64 fixpoint_dot(int x1, int x2, int y1, int y2, int z1, int z2) {
143  return fixpoint_mul(x1, x2) + fixpoint_mul(y1, y2) + fixpoint_mul(z1, z2);
144 }
145 
146 //----- (004A1780) mm6_chinese---------------------------------------------
147 __int64 fixpoint_div(int a1, int a2) { return ((__int64)a1 << 16) / a2; }
148 
149 __int64 fixpoint_sub_unknown(int a1, int a2) {
150  return (((__int64)a1 << 16) * a2) >> 16;
151 }
152 
153 //----- (0048B561) --------------------------------------------------------
155  // float X.Yf -> int XXXX YYYY
156  int left = floorf((val - 0.5f) + 0.5f);
157  int right = floorf((val - left) * 65536.0f);
158  return (left << 16) | right;
159 }
160 
161 int fixpoint_from_int(int lhv, int rhv) { return (lhv << 16) | rhv; }
162 
163 //----- (00452A9E) --------------------------------------------------------
164 int integer_sqrt(int val) {
166  //Получение квадратного корня//
168 
169  int result; // eax@2
170  unsigned int v2; // edx@3
171  unsigned int v3; // edi@3
172  int v5; // esi@4
173 
174  if (val < 1) return val;
175 
176  v2 = 0;
177  v3 = val;
178  result = 0;
179  for (unsigned int i = 0; i < 16; ++i) {
180  result *= 2;
181  v2 = (v3 >> 30) | 4 * v2;
182  v5 = 2 * result + 1;
183  v3 *= 4;
184  if (v2 >= v5) {
185  ++result;
186  v2 -= v5;
187  }
188  }
189  if (val - result * result >= (unsigned int)(result - 1)) ++result;
190  return result;
191 }
192 
193 //----- (00452B2E) --------------------------------------------------------
194 int GetDiceResult(unsigned int uNumDice, unsigned int uDiceSides) {
195  int v3 = 0;
196  if (uDiceSides) {
197  for (unsigned int i = 0; i < uNumDice; ++i)
198  v3 += rand() % uDiceSides + 1;
199  return v3;
200  }
201  return 0;
202 }
stru193_math::uIntegerDoublePi
static const unsigned int uIntegerDoublePi
Definition: OurMath.h:90
stru193_math::uDoublePiMask
static const unsigned int uDoublePiMask
Definition: OurMath.h:91
right
GLdouble GLdouble right
Definition: SDL_opengl_glext.h:6106
fixpoint_div
__int64 fixpoint_div(int a1, int a2)
Definition: OurMath.cpp:147
stru193_math::uIntegerHalfPi
static const unsigned int uIntegerHalfPi
Definition: OurMath.h:89
stru193_math::pCosTable
int pCosTable[520]
Definition: OurMath.h:86
GetDiceResult
int GetDiceResult(unsigned int uNumDice, unsigned int uDiceSides)
Definition: OurMath.cpp:194
fixpoint_from_int
int fixpoint_from_int(int lhv, int rhv)
Definition: OurMath.cpp:161
x1
GLuint GLfloat GLfloat GLfloat x1
Definition: SDL_opengl_glext.h:8586
Engine.h
fixpoint_sub_unknown
__int64 fixpoint_sub_unknown(int a1, int a2)
Definition: OurMath.cpp:149
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9435
y
EGLSurface EGLint EGLint y
Definition: SDL_egl.h:1596
stru193_math::stru193_math
stru193_math()
Definition: OurMath.cpp:5
fixpoint_dot
__int64 fixpoint_dot(int x1, int x2, int y1, int y2, int z1, int z2)
Definition: OurMath.cpp:142
x
EGLSurface EGLint x
Definition: SDL_egl.h:1596
stru193_math::pTanTable
int pTanTable[520]
Definition: OurMath.h:85
fixpoint_from_float
int fixpoint_from_float(float val)
Definition: OurMath.cpp:154
fixpoint_mul
__int64 fixpoint_mul(int a1, int a2)
Definition: OurMath.cpp:138
f
GLfloat f
Definition: SDL_opengl_glext.h:1873
stru193_math::pInvCosTable
int pInvCosTable[520]
Definition: OurMath.h:87
stru193_math::Atan2
unsigned int Atan2(int x, int y)
Definition: OurMath.cpp:46
val
GLuint GLfloat * val
Definition: SDL_opengl_glext.h:1495
x2
GLfixed GLfixed x2
Definition: SDL_opengl_glext.h:4586
v2
GLfloat GLfloat GLfloat v2
Definition: SDL_opengl_glext.h:695
integer_sqrt
int integer_sqrt(int val)
Definition: OurMath.cpp:164
v3
GLfloat GLfloat GLfloat GLfloat v3
Definition: SDL_opengl_glext.h:696
y1
GLfixed y1
Definition: SDL_opengl_glext.h:4586
y2
GLfixed GLfixed GLfixed y2
Definition: SDL_opengl_glext.h:4586
stru193_math::Cos
int Cos(int angle)
Definition: OurMath.cpp:28
angle
GLfloat angle
Definition: SDL_opengl_glext.h:6100
OurMath.h
stru193_math::Sin
int Sin(int angle)
Definition: OurMath.cpp:133
left
GLint left
Definition: SDL_opengl_glext.h:1952
stru193_math::uIntegerPi
static const unsigned int uIntegerPi
Definition: OurMath.h:88