q_shared.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "q_shared.h"
  16. #define DEG2RAD( a ) ( a * M_PI ) / 180.0F
  17. vec3_t vec3_origin = {0,0,0};
  18. //============================================================================
  19. #ifdef _WIN32
  20. #pragma optimize( "", off )
  21. #endif
  22. void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees )
  23. {
  24. float m[3][3];
  25. float im[3][3];
  26. float zrot[3][3];
  27. float tmpmat[3][3];
  28. float rot[3][3];
  29. int i;
  30. vec3_t vr, vup, vf;
  31. vf[0] = dir[0];
  32. vf[1] = dir[1];
  33. vf[2] = dir[2];
  34. PerpendicularVector( vr, dir );
  35. CrossProduct( vr, vf, vup );
  36. m[0][0] = vr[0];
  37. m[1][0] = vr[1];
  38. m[2][0] = vr[2];
  39. m[0][1] = vup[0];
  40. m[1][1] = vup[1];
  41. m[2][1] = vup[2];
  42. m[0][2] = vf[0];
  43. m[1][2] = vf[1];
  44. m[2][2] = vf[2];
  45. memcpy( im, m, sizeof( im ) );
  46. im[0][1] = m[1][0];
  47. im[0][2] = m[2][0];
  48. im[1][0] = m[0][1];
  49. im[1][2] = m[2][1];
  50. im[2][0] = m[0][2];
  51. im[2][1] = m[1][2];
  52. memset( zrot, 0, sizeof( zrot ) );
  53. zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
  54. zrot[0][0] = cos( DEG2RAD( degrees ) );
  55. zrot[0][1] = sin( DEG2RAD( degrees ) );
  56. zrot[1][0] = -sin( DEG2RAD( degrees ) );
  57. zrot[1][1] = cos( DEG2RAD( degrees ) );
  58. R_ConcatRotations( m, zrot, tmpmat );
  59. R_ConcatRotations( tmpmat, im, rot );
  60. for ( i = 0; i < 3; i++ )
  61. {
  62. dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
  63. }
  64. }
  65. #ifdef _WIN32
  66. #pragma optimize( "", on )
  67. #endif
  68. void AngleVectors (vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
  69. {
  70. float angle;
  71. static float sr, sp, sy, cr, cp, cy;
  72. // static to help MS compiler fp bugs
  73. angle = angles[YAW] * (M_PI*2 / 360);
  74. sy = sin(angle);
  75. cy = cos(angle);
  76. angle = angles[PITCH] * (M_PI*2 / 360);
  77. sp = sin(angle);
  78. cp = cos(angle);
  79. angle = angles[ROLL] * (M_PI*2 / 360);
  80. sr = sin(angle);
  81. cr = cos(angle);
  82. if (forward)
  83. {
  84. forward[0] = cp*cy;
  85. forward[1] = cp*sy;
  86. forward[2] = -sp;
  87. }
  88. if (right)
  89. {
  90. right[0] = (-1*sr*sp*cy+-1*cr*-sy);
  91. right[1] = (-1*sr*sp*sy+-1*cr*cy);
  92. right[2] = -1*sr*cp;
  93. }
  94. if (up)
  95. {
  96. up[0] = (cr*sp*cy+-sr*-sy);
  97. up[1] = (cr*sp*sy+-sr*cy);
  98. up[2] = cr*cp;
  99. }
  100. }
  101. void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
  102. {
  103. float d;
  104. vec3_t n;
  105. float inv_denom;
  106. inv_denom = 1.0F / DotProduct( normal, normal );
  107. d = DotProduct( normal, p ) * inv_denom;
  108. n[0] = normal[0] * inv_denom;
  109. n[1] = normal[1] * inv_denom;
  110. n[2] = normal[2] * inv_denom;
  111. dst[0] = p[0] - d * n[0];
  112. dst[1] = p[1] - d * n[1];
  113. dst[2] = p[2] - d * n[2];
  114. }
  115. /*
  116. ** assumes "src" is normalized
  117. */
  118. void PerpendicularVector( vec3_t dst, const vec3_t src )
  119. {
  120. int pos;
  121. int i;
  122. float minelem = 1.0F;
  123. vec3_t tempvec;
  124. /*
  125. ** find the smallest magnitude axially aligned vector
  126. */
  127. for ( pos = 0, i = 0; i < 3; i++ )
  128. {
  129. if ( fabs( src[i] ) < minelem )
  130. {
  131. pos = i;
  132. minelem = fabs( src[i] );
  133. }
  134. }
  135. tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
  136. tempvec[pos] = 1.0F;
  137. /*
  138. ** project the point onto the plane defined by src
  139. */
  140. ProjectPointOnPlane( dst, tempvec, src );
  141. /*
  142. ** normalize the result
  143. */
  144. VectorNormalize( dst );
  145. }
  146. /*
  147. ================
  148. R_ConcatRotations
  149. ================
  150. */
  151. void R_ConcatRotations (float in1[3][3], float in2[3][3], float out[3][3])
  152. {
  153. out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  154. in1[0][2] * in2[2][0];
  155. out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  156. in1[0][2] * in2[2][1];
  157. out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  158. in1[0][2] * in2[2][2];
  159. out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  160. in1[1][2] * in2[2][0];
  161. out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  162. in1[1][2] * in2[2][1];
  163. out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  164. in1[1][2] * in2[2][2];
  165. out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  166. in1[2][2] * in2[2][0];
  167. out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  168. in1[2][2] * in2[2][1];
  169. out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  170. in1[2][2] * in2[2][2];
  171. }
  172. /*
  173. ================
  174. R_ConcatTransforms
  175. ================
  176. */
  177. void R_ConcatTransforms (float in1[3][4], float in2[3][4], float out[3][4])
  178. {
  179. out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  180. in1[0][2] * in2[2][0];
  181. out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  182. in1[0][2] * in2[2][1];
  183. out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  184. in1[0][2] * in2[2][2];
  185. out[0][3] = in1[0][0] * in2[0][3] + in1[0][1] * in2[1][3] +
  186. in1[0][2] * in2[2][3] + in1[0][3];
  187. out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  188. in1[1][2] * in2[2][0];
  189. out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  190. in1[1][2] * in2[2][1];
  191. out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  192. in1[1][2] * in2[2][2];
  193. out[1][3] = in1[1][0] * in2[0][3] + in1[1][1] * in2[1][3] +
  194. in1[1][2] * in2[2][3] + in1[1][3];
  195. out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  196. in1[2][2] * in2[2][0];
  197. out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  198. in1[2][2] * in2[2][1];
  199. out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  200. in1[2][2] * in2[2][2];
  201. out[2][3] = in1[2][0] * in2[0][3] + in1[2][1] * in2[1][3] +
  202. in1[2][2] * in2[2][3] + in1[2][3];
  203. }
  204. //============================================================================
  205. float Q_fabs (float f)
  206. {
  207. #if 0
  208. if (f >= 0)
  209. return f;
  210. return -f;
  211. #else
  212. int tmp = * ( int * ) &f;
  213. tmp &= 0x7FFFFFFF;
  214. return * ( float * ) &tmp;
  215. #endif
  216. }
  217. #if defined _M_IX86 && !defined C_ONLY
  218. #pragma warning (disable:4035)
  219. __declspec( naked ) long Q_ftol( float f )
  220. {
  221. static int tmp;
  222. __asm fld dword ptr [esp+4]
  223. __asm fistp tmp
  224. __asm mov eax, tmp
  225. __asm ret
  226. }
  227. #pragma warning (default:4035)
  228. #endif
  229. /*
  230. ===============
  231. LerpAngle
  232. ===============
  233. */
  234. float LerpAngle (float a2, float a1, float frac)
  235. {
  236. if (a1 - a2 > 180)
  237. a1 -= 360;
  238. if (a1 - a2 < -180)
  239. a1 += 360;
  240. return a2 + frac * (a1 - a2);
  241. }
  242. float anglemod(float a)
  243. {
  244. #if 0
  245. if (a >= 0)
  246. a -= 360*(int)(a/360);
  247. else
  248. a += 360*( 1 + (int)(-a/360) );
  249. #endif
  250. a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
  251. return a;
  252. }
  253. int i;
  254. vec3_t corners[2];
  255. // this is the slow, general version
  256. int BoxOnPlaneSide2 (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  257. {
  258. int i;
  259. float dist1, dist2;
  260. int sides;
  261. vec3_t corners[2];
  262. for (i=0 ; i<3 ; i++)
  263. {
  264. if (p->normal[i] < 0)
  265. {
  266. corners[0][i] = emins[i];
  267. corners[1][i] = emaxs[i];
  268. }
  269. else
  270. {
  271. corners[1][i] = emins[i];
  272. corners[0][i] = emaxs[i];
  273. }
  274. }
  275. dist1 = DotProduct (p->normal, corners[0]) - p->dist;
  276. dist2 = DotProduct (p->normal, corners[1]) - p->dist;
  277. sides = 0;
  278. if (dist1 >= 0)
  279. sides = 1;
  280. if (dist2 < 0)
  281. sides |= 2;
  282. return sides;
  283. }
  284. /*
  285. ==================
  286. BoxOnPlaneSide
  287. Returns 1, 2, or 1 + 2
  288. ==================
  289. */
  290. #if !id386 || defined __linux__
  291. int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  292. {
  293. float dist1, dist2;
  294. int sides;
  295. // fast axial cases
  296. if (p->type < 3)
  297. {
  298. if (p->dist <= emins[p->type])
  299. return 1;
  300. if (p->dist >= emaxs[p->type])
  301. return 2;
  302. return 3;
  303. }
  304. // general case
  305. switch (p->signbits)
  306. {
  307. case 0:
  308. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  309. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  310. break;
  311. case 1:
  312. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  313. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  314. break;
  315. case 2:
  316. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  317. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  318. break;
  319. case 3:
  320. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  321. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  322. break;
  323. case 4:
  324. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  325. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  326. break;
  327. case 5:
  328. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  329. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  330. break;
  331. case 6:
  332. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  333. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  334. break;
  335. case 7:
  336. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  337. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  338. break;
  339. default:
  340. dist1 = dist2 = 0; // shut up compiler
  341. assert( 0 );
  342. break;
  343. }
  344. sides = 0;
  345. if (dist1 >= p->dist)
  346. sides = 1;
  347. if (dist2 < p->dist)
  348. sides |= 2;
  349. assert( sides != 0 );
  350. return sides;
  351. }
  352. #else
  353. #pragma warning( disable: 4035 )
  354. __declspec( naked ) int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  355. {
  356. static int bops_initialized;
  357. static int Ljmptab[8];
  358. __asm {
  359. push ebx
  360. cmp bops_initialized, 1
  361. je initialized
  362. mov bops_initialized, 1
  363. mov Ljmptab[0*4], offset Lcase0
  364. mov Ljmptab[1*4], offset Lcase1
  365. mov Ljmptab[2*4], offset Lcase2
  366. mov Ljmptab[3*4], offset Lcase3
  367. mov Ljmptab[4*4], offset Lcase4
  368. mov Ljmptab[5*4], offset Lcase5
  369. mov Ljmptab[6*4], offset Lcase6
  370. mov Ljmptab[7*4], offset Lcase7
  371. initialized:
  372. mov edx,ds:dword ptr[4+12+esp]
  373. mov ecx,ds:dword ptr[4+4+esp]
  374. xor eax,eax
  375. mov ebx,ds:dword ptr[4+8+esp]
  376. mov al,ds:byte ptr[17+edx]
  377. cmp al,8
  378. jge Lerror
  379. fld ds:dword ptr[0+edx]
  380. fld st(0)
  381. jmp dword ptr[Ljmptab+eax*4]
  382. Lcase0:
  383. fmul ds:dword ptr[ebx]
  384. fld ds:dword ptr[0+4+edx]
  385. fxch st(2)
  386. fmul ds:dword ptr[ecx]
  387. fxch st(2)
  388. fld st(0)
  389. fmul ds:dword ptr[4+ebx]
  390. fld ds:dword ptr[0+8+edx]
  391. fxch st(2)
  392. fmul ds:dword ptr[4+ecx]
  393. fxch st(2)
  394. fld st(0)
  395. fmul ds:dword ptr[8+ebx]
  396. fxch st(5)
  397. faddp st(3),st(0)
  398. fmul ds:dword ptr[8+ecx]
  399. fxch st(1)
  400. faddp st(3),st(0)
  401. fxch st(3)
  402. faddp st(2),st(0)
  403. jmp LSetSides
  404. Lcase1:
  405. fmul ds:dword ptr[ecx]
  406. fld ds:dword ptr[0+4+edx]
  407. fxch st(2)
  408. fmul ds:dword ptr[ebx]
  409. fxch st(2)
  410. fld st(0)
  411. fmul ds:dword ptr[4+ebx]
  412. fld ds:dword ptr[0+8+edx]
  413. fxch st(2)
  414. fmul ds:dword ptr[4+ecx]
  415. fxch st(2)
  416. fld st(0)
  417. fmul ds:dword ptr[8+ebx]
  418. fxch st(5)
  419. faddp st(3),st(0)
  420. fmul ds:dword ptr[8+ecx]
  421. fxch st(1)
  422. faddp st(3),st(0)
  423. fxch st(3)
  424. faddp st(2),st(0)
  425. jmp LSetSides
  426. Lcase2:
  427. fmul ds:dword ptr[ebx]
  428. fld ds:dword ptr[0+4+edx]
  429. fxch st(2)
  430. fmul ds:dword ptr[ecx]
  431. fxch st(2)
  432. fld st(0)
  433. fmul ds:dword ptr[4+ecx]
  434. fld ds:dword ptr[0+8+edx]
  435. fxch st(2)
  436. fmul ds:dword ptr[4+ebx]
  437. fxch st(2)
  438. fld st(0)
  439. fmul ds:dword ptr[8+ebx]
  440. fxch st(5)
  441. faddp st(3),st(0)
  442. fmul ds:dword ptr[8+ecx]
  443. fxch st(1)
  444. faddp st(3),st(0)
  445. fxch st(3)
  446. faddp st(2),st(0)
  447. jmp LSetSides
  448. Lcase3:
  449. fmul ds:dword ptr[ecx]
  450. fld ds:dword ptr[0+4+edx]
  451. fxch st(2)
  452. fmul ds:dword ptr[ebx]
  453. fxch st(2)
  454. fld st(0)
  455. fmul ds:dword ptr[4+ecx]
  456. fld ds:dword ptr[0+8+edx]
  457. fxch st(2)
  458. fmul ds:dword ptr[4+ebx]
  459. fxch st(2)
  460. fld st(0)
  461. fmul ds:dword ptr[8+ebx]
  462. fxch st(5)
  463. faddp st(3),st(0)
  464. fmul ds:dword ptr[8+ecx]
  465. fxch st(1)
  466. faddp st(3),st(0)
  467. fxch st(3)
  468. faddp st(2),st(0)
  469. jmp LSetSides
  470. Lcase4:
  471. fmul ds:dword ptr[ebx]
  472. fld ds:dword ptr[0+4+edx]
  473. fxch st(2)
  474. fmul ds:dword ptr[ecx]
  475. fxch st(2)
  476. fld st(0)
  477. fmul ds:dword ptr[4+ebx]
  478. fld ds:dword ptr[0+8+edx]
  479. fxch st(2)
  480. fmul ds:dword ptr[4+ecx]
  481. fxch st(2)
  482. fld st(0)
  483. fmul ds:dword ptr[8+ecx]
  484. fxch st(5)
  485. faddp st(3),st(0)
  486. fmul ds:dword ptr[8+ebx]
  487. fxch st(1)
  488. faddp st(3),st(0)
  489. fxch st(3)
  490. faddp st(2),st(0)
  491. jmp LSetSides
  492. Lcase5:
  493. fmul ds:dword ptr[ecx]
  494. fld ds:dword ptr[0+4+edx]
  495. fxch st(2)
  496. fmul ds:dword ptr[ebx]
  497. fxch st(2)
  498. fld st(0)
  499. fmul ds:dword ptr[4+ebx]
  500. fld ds:dword ptr[0+8+edx]
  501. fxch st(2)
  502. fmul ds:dword ptr[4+ecx]
  503. fxch st(2)
  504. fld st(0)
  505. fmul ds:dword ptr[8+ecx]
  506. fxch st(5)
  507. faddp st(3),st(0)
  508. fmul ds:dword ptr[8+ebx]
  509. fxch st(1)
  510. faddp st(3),st(0)
  511. fxch st(3)
  512. faddp st(2),st(0)
  513. jmp LSetSides
  514. Lcase6:
  515. fmul ds:dword ptr[ebx]
  516. fld ds:dword ptr[0+4+edx]
  517. fxch st(2)
  518. fmul ds:dword ptr[ecx]
  519. fxch st(2)
  520. fld st(0)
  521. fmul ds:dword ptr[4+ecx]
  522. fld ds:dword ptr[0+8+edx]
  523. fxch st(2)
  524. fmul ds:dword ptr[4+ebx]
  525. fxch st(2)
  526. fld st(0)
  527. fmul ds:dword ptr[8+ecx]
  528. fxch st(5)
  529. faddp st(3),st(0)
  530. fmul ds:dword ptr[8+ebx]
  531. fxch st(1)
  532. faddp st(3),st(0)
  533. fxch st(3)
  534. faddp st(2),st(0)
  535. jmp LSetSides
  536. Lcase7:
  537. fmul ds:dword ptr[ecx]
  538. fld ds:dword ptr[0+4+edx]
  539. fxch st(2)
  540. fmul ds:dword ptr[ebx]
  541. fxch st(2)
  542. fld st(0)
  543. fmul ds:dword ptr[4+ecx]
  544. fld ds:dword ptr[0+8+edx]
  545. fxch st(2)
  546. fmul ds:dword ptr[4+ebx]
  547. fxch st(2)
  548. fld st(0)
  549. fmul ds:dword ptr[8+ecx]
  550. fxch st(5)
  551. faddp st(3),st(0)
  552. fmul ds:dword ptr[8+ebx]
  553. fxch st(1)
  554. faddp st(3),st(0)
  555. fxch st(3)
  556. faddp st(2),st(0)
  557. LSetSides:
  558. faddp st(2),st(0)
  559. fcomp ds:dword ptr[12+edx]
  560. xor ecx,ecx
  561. fnstsw ax
  562. fcomp ds:dword ptr[12+edx]
  563. and ah,1
  564. xor ah,1
  565. add cl,ah
  566. fnstsw ax
  567. and ah,1
  568. add ah,ah
  569. add cl,ah
  570. pop ebx
  571. mov eax,ecx
  572. ret
  573. Lerror:
  574. int 3
  575. }
  576. }
  577. #pragma warning( default: 4035 )
  578. #endif
  579. void ClearBounds (vec3_t mins, vec3_t maxs)
  580. {
  581. mins[0] = mins[1] = mins[2] = 99999;
  582. maxs[0] = maxs[1] = maxs[2] = -99999;
  583. }
  584. void AddPointToBounds (vec3_t v, vec3_t mins, vec3_t maxs)
  585. {
  586. int i;
  587. vec_t val;
  588. for (i=0 ; i<3 ; i++)
  589. {
  590. val = v[i];
  591. if (val < mins[i])
  592. mins[i] = val;
  593. if (val > maxs[i])
  594. maxs[i] = val;
  595. }
  596. }
  597. int VectorCompare (vec3_t v1, vec3_t v2)
  598. {
  599. if (v1[0] != v2[0] || v1[1] != v2[1] || v1[2] != v2[2])
  600. return 0;
  601. return 1;
  602. }
  603. vec_t VectorNormalize (vec3_t v)
  604. {
  605. float length, ilength;
  606. length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  607. length = sqrt (length); // FIXME
  608. if (length)
  609. {
  610. ilength = 1/length;
  611. v[0] *= ilength;
  612. v[1] *= ilength;
  613. v[2] *= ilength;
  614. }
  615. return length;
  616. }
  617. vec_t VectorNormalize2 (vec3_t v, vec3_t out)
  618. {
  619. float length, ilength;
  620. length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  621. length = sqrt (length); // FIXME
  622. if (length)
  623. {
  624. ilength = 1/length;
  625. out[0] = v[0]*ilength;
  626. out[1] = v[1]*ilength;
  627. out[2] = v[2]*ilength;
  628. }
  629. return length;
  630. }
  631. void VectorMA (vec3_t veca, float scale, vec3_t vecb, vec3_t vecc)
  632. {
  633. vecc[0] = veca[0] + scale*vecb[0];
  634. vecc[1] = veca[1] + scale*vecb[1];
  635. vecc[2] = veca[2] + scale*vecb[2];
  636. }
  637. vec_t _DotProduct (vec3_t v1, vec3_t v2)
  638. {
  639. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  640. }
  641. void _VectorSubtract (vec3_t veca, vec3_t vecb, vec3_t out)
  642. {
  643. out[0] = veca[0]-vecb[0];
  644. out[1] = veca[1]-vecb[1];
  645. out[2] = veca[2]-vecb[2];
  646. }
  647. void _VectorAdd (vec3_t veca, vec3_t vecb, vec3_t out)
  648. {
  649. out[0] = veca[0]+vecb[0];
  650. out[1] = veca[1]+vecb[1];
  651. out[2] = veca[2]+vecb[2];
  652. }
  653. void _VectorCopy (vec3_t in, vec3_t out)
  654. {
  655. out[0] = in[0];
  656. out[1] = in[1];
  657. out[2] = in[2];
  658. }
  659. void CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
  660. {
  661. cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  662. cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  663. cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  664. }
  665. double sqrt(double x);
  666. vec_t VectorLength(vec3_t v)
  667. {
  668. int i;
  669. float length;
  670. length = 0;
  671. for (i=0 ; i< 3 ; i++)
  672. length += v[i]*v[i];
  673. length = sqrt (length); // FIXME
  674. return length;
  675. }
  676. void VectorInverse (vec3_t v)
  677. {
  678. v[0] = -v[0];
  679. v[1] = -v[1];
  680. v[2] = -v[2];
  681. }
  682. void VectorScale (vec3_t in, vec_t scale, vec3_t out)
  683. {
  684. out[0] = in[0]*scale;
  685. out[1] = in[1]*scale;
  686. out[2] = in[2]*scale;
  687. }
  688. int Q_log2(int val)
  689. {
  690. int answer=0;
  691. while (val>>=1)
  692. answer++;
  693. return answer;
  694. }
  695. //====================================================================================
  696. /*
  697. ============
  698. COM_SkipPath
  699. ============
  700. */
  701. char *COM_SkipPath (char *pathname)
  702. {
  703. char *last;
  704. last = pathname;
  705. while (*pathname)
  706. {
  707. if (*pathname=='/')
  708. last = pathname+1;
  709. pathname++;
  710. }
  711. return last;
  712. }
  713. /*
  714. ============
  715. COM_StripExtension
  716. ============
  717. */
  718. void COM_StripExtension (char *in, char *out)
  719. {
  720. while (*in && *in != '.')
  721. *out++ = *in++;
  722. *out = 0;
  723. }
  724. /*
  725. ============
  726. COM_FileExtension
  727. ============
  728. */
  729. char *COM_FileExtension (char *in)
  730. {
  731. static char exten[8];
  732. int i;
  733. while (*in && *in != '.')
  734. in++;
  735. if (!*in)
  736. return "";
  737. in++;
  738. for (i=0 ; i<7 && *in ; i++,in++)
  739. exten[i] = *in;
  740. exten[i] = 0;
  741. return exten;
  742. }
  743. /*
  744. ============
  745. COM_FileBase
  746. ============
  747. */
  748. void COM_FileBase (char *in, char *out)
  749. {
  750. char *s, *s2;
  751. s = in + strlen(in) - 1;
  752. while (s != in && *s != '.')
  753. s--;
  754. for (s2 = s ; s2 != in && *s2 != '/' ; s2--)
  755. ;
  756. if (s-s2 < 2)
  757. out[0] = 0;
  758. else
  759. {
  760. s--;
  761. strncpy (out,s2+1, s-s2);
  762. out[s-s2] = 0;
  763. }
  764. }
  765. /*
  766. ============
  767. COM_FilePath
  768. Returns the path up to, but not including the last /
  769. ============
  770. */
  771. void COM_FilePath (char *in, char *out)
  772. {
  773. char *s;
  774. s = in + strlen(in) - 1;
  775. while (s != in && *s != '/')
  776. s--;
  777. strncpy (out,in, s-in);
  778. out[s-in] = 0;
  779. }
  780. /*
  781. ==================
  782. COM_DefaultExtension
  783. ==================
  784. */
  785. void COM_DefaultExtension (char *path, char *extension)
  786. {
  787. char *src;
  788. //
  789. // if path doesn't have a .EXT, append extension
  790. // (extension should include the .)
  791. //
  792. src = path + strlen(path) - 1;
  793. while (*src != '/' && src != path)
  794. {
  795. if (*src == '.')
  796. return; // it has an extension
  797. src--;
  798. }
  799. strcat (path, extension);
  800. }
  801. /*
  802. ============================================================================
  803. BYTE ORDER FUNCTIONS
  804. ============================================================================
  805. */
  806. qboolean bigendien;
  807. // can't just use function pointers, or dll linkage can
  808. // mess up when qcommon is included in multiple places
  809. short (*_BigShort) (short l);
  810. short (*_LittleShort) (short l);
  811. int (*_BigLong) (int l);
  812. int (*_LittleLong) (int l);
  813. float (*_BigFloat) (float l);
  814. float (*_LittleFloat) (float l);
  815. short BigShort(short l){return _BigShort(l);}
  816. short LittleShort(short l) {return _LittleShort(l);}
  817. int BigLong (int l) {return _BigLong(l);}
  818. int LittleLong (int l) {return _LittleLong(l);}
  819. float BigFloat (float l) {return _BigFloat(l);}
  820. float LittleFloat (float l) {return _LittleFloat(l);}
  821. short ShortSwap (short l)
  822. {
  823. byte b1,b2;
  824. b1 = l&255;
  825. b2 = (l>>8)&255;
  826. return (b1<<8) + b2;
  827. }
  828. short ShortNoSwap (short l)
  829. {
  830. return l;
  831. }
  832. int LongSwap (int l)
  833. {
  834. byte b1,b2,b3,b4;
  835. b1 = l&255;
  836. b2 = (l>>8)&255;
  837. b3 = (l>>16)&255;
  838. b4 = (l>>24)&255;
  839. return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  840. }
  841. int LongNoSwap (int l)
  842. {
  843. return l;
  844. }
  845. float FloatSwap (float f)
  846. {
  847. union
  848. {
  849. float f;
  850. byte b[4];
  851. } dat1, dat2;
  852. dat1.f = f;
  853. dat2.b[0] = dat1.b[3];
  854. dat2.b[1] = dat1.b[2];
  855. dat2.b[2] = dat1.b[1];
  856. dat2.b[3] = dat1.b[0];
  857. return dat2.f;
  858. }
  859. float FloatNoSwap (float f)
  860. {
  861. return f;
  862. }
  863. /*
  864. ================
  865. Swap_Init
  866. ================
  867. */
  868. void Swap_Init (void)
  869. {
  870. byte swaptest[2] = {1,0};
  871. // set the byte swapping variables in a portable manner
  872. if ( *(short *)swaptest == 1)
  873. {
  874. bigendien = false;
  875. _BigShort = ShortSwap;
  876. _LittleShort = ShortNoSwap;
  877. _BigLong = LongSwap;
  878. _LittleLong = LongNoSwap;
  879. _BigFloat = FloatSwap;
  880. _LittleFloat = FloatNoSwap;
  881. }
  882. else
  883. {
  884. bigendien = true;
  885. _BigShort = ShortNoSwap;
  886. _LittleShort = ShortSwap;
  887. _BigLong = LongNoSwap;
  888. _LittleLong = LongSwap;
  889. _BigFloat = FloatNoSwap;
  890. _LittleFloat = FloatSwap;
  891. }
  892. }
  893. /*
  894. ============
  895. va
  896. does a varargs printf into a temp buffer, so I don't need to have
  897. varargs versions of all text functions.
  898. FIXME: make this buffer size safe someday
  899. ============
  900. */
  901. char *va(char *format, ...)
  902. {
  903. va_list argptr;
  904. static char string[1024];
  905. va_start (argptr, format);
  906. vsprintf (string, format,argptr);
  907. va_end (argptr);
  908. return string;
  909. }
  910. char com_token[MAX_TOKEN_CHARS];
  911. /*
  912. ==============
  913. COM_Parse
  914. Parse a token out of a string
  915. ==============
  916. */
  917. char *COM_Parse (char **data_p)
  918. {
  919. int c;
  920. int len;
  921. char *data;
  922. data = *data_p;
  923. len = 0;
  924. com_token[0] = 0;
  925. if (!data)
  926. {
  927. *data_p = NULL;
  928. return "";
  929. }
  930. // skip whitespace
  931. skipwhite:
  932. while ( (c = *data) <= ' ')
  933. {
  934. if (c == 0)
  935. {
  936. *data_p = NULL;
  937. return "";
  938. }
  939. data++;
  940. }
  941. // skip // comments
  942. if (c=='/' && data[1] == '/')
  943. {
  944. while (*data && *data != '\n')
  945. data++;
  946. goto skipwhite;
  947. }
  948. // handle quoted strings specially
  949. if (c == '\"')
  950. {
  951. data++;
  952. while (1)
  953. {
  954. c = *data++;
  955. if (c=='\"' || !c)
  956. {
  957. com_token[len] = 0;
  958. *data_p = data;
  959. return com_token;
  960. }
  961. if (len < MAX_TOKEN_CHARS)
  962. {
  963. com_token[len] = c;
  964. len++;
  965. }
  966. }
  967. }
  968. // parse a regular word
  969. do
  970. {
  971. if (len < MAX_TOKEN_CHARS)
  972. {
  973. com_token[len] = c;
  974. len++;
  975. }
  976. data++;
  977. c = *data;
  978. } while (c>32);
  979. if (len == MAX_TOKEN_CHARS)
  980. {
  981. // Com_Printf ("Token exceeded %i chars, discarded.\n", MAX_TOKEN_CHARS);
  982. len = 0;
  983. }
  984. com_token[len] = 0;
  985. *data_p = data;
  986. return com_token;
  987. }
  988. /*
  989. ===============
  990. Com_PageInMemory
  991. ===============
  992. */
  993. int paged_total;
  994. void Com_PageInMemory (byte *buffer, int size)
  995. {
  996. int i;
  997. for (i=size-1 ; i>0 ; i-=4096)
  998. paged_total += buffer[i];
  999. }
  1000. /*
  1001. ============================================================================
  1002. LIBRARY REPLACEMENT FUNCTIONS
  1003. ============================================================================
  1004. */
  1005. // FIXME: replace all Q_stricmp with Q_strcasecmp
  1006. int Q_stricmp (char *s1, char *s2)
  1007. {
  1008. #if defined(WIN32)
  1009. return _stricmp (s1, s2);
  1010. #else
  1011. return strcasecmp (s1, s2);
  1012. #endif
  1013. }
  1014. int Q_strncasecmp (char *s1, char *s2, int n)
  1015. {
  1016. int c1, c2;
  1017. do
  1018. {
  1019. c1 = *s1++;
  1020. c2 = *s2++;
  1021. if (!n--)
  1022. return 0; // strings are equal until end point
  1023. if (c1 != c2)
  1024. {
  1025. if (c1 >= 'a' && c1 <= 'z')
  1026. c1 -= ('a' - 'A');
  1027. if (c2 >= 'a' && c2 <= 'z')
  1028. c2 -= ('a' - 'A');
  1029. if (c1 != c2)
  1030. return -1; // strings not equal
  1031. }
  1032. } while (c1);
  1033. return 0; // strings are equal
  1034. }
  1035. int Q_strcasecmp (char *s1, char *s2)
  1036. {
  1037. return Q_strncasecmp (s1, s2, 99999);
  1038. }
  1039. void Com_sprintf (char *dest, int size, char *fmt, ...)
  1040. {
  1041. int len;
  1042. va_list argptr;
  1043. char bigbuffer[0x10000];
  1044. va_start (argptr,fmt);
  1045. len = vsprintf (bigbuffer,fmt,argptr);
  1046. va_end (argptr);
  1047. if (len >= size)
  1048. Com_Printf ("Com_sprintf: overflow of %i in %i\n", len, size);
  1049. strncpy (dest, bigbuffer, size-1);
  1050. }
  1051. /*
  1052. =====================================================================
  1053. INFO STRINGS
  1054. =====================================================================
  1055. */
  1056. /*
  1057. ===============
  1058. Info_ValueForKey
  1059. Searches the string for the given
  1060. key and returns the associated value, or an empty string.
  1061. ===============
  1062. */
  1063. char *Info_ValueForKey (char *s, char *key)
  1064. {
  1065. char pkey[512];
  1066. static char value[2][512]; // use two buffers so compares
  1067. // work without stomping on each other
  1068. static int valueindex;
  1069. char *o;
  1070. valueindex ^= 1;
  1071. if (*s == '\\')
  1072. s++;
  1073. while (1)
  1074. {
  1075. o = pkey;
  1076. while (*s != '\\')
  1077. {
  1078. if (!*s)
  1079. return "";
  1080. *o++ = *s++;
  1081. }
  1082. *o = 0;
  1083. s++;
  1084. o = value[valueindex];
  1085. while (*s != '\\' && *s)
  1086. {
  1087. if (!*s)
  1088. return "";
  1089. *o++ = *s++;
  1090. }
  1091. *o = 0;
  1092. if (!strcmp (key, pkey) )
  1093. return value[valueindex];
  1094. if (!*s)
  1095. return "";
  1096. s++;
  1097. }
  1098. }
  1099. void Info_RemoveKey (char *s, char *key)
  1100. {
  1101. char *start;
  1102. char pkey[512];
  1103. char value[512];
  1104. char *o;
  1105. if (strstr (key, "\\"))
  1106. {
  1107. // Com_Printf ("Can't use a key with a \\\n");
  1108. return;
  1109. }
  1110. while (1)
  1111. {
  1112. start = s;
  1113. if (*s == '\\')
  1114. s++;
  1115. o = pkey;
  1116. while (*s != '\\')
  1117. {
  1118. if (!*s)
  1119. return;
  1120. *o++ = *s++;
  1121. }
  1122. *o = 0;
  1123. s++;
  1124. o = value;
  1125. while (*s != '\\' && *s)
  1126. {
  1127. if (!*s)
  1128. return;
  1129. *o++ = *s++;
  1130. }
  1131. *o = 0;
  1132. if (!strcmp (key, pkey) )
  1133. {
  1134. strcpy (start, s); // remove this part
  1135. return;
  1136. }
  1137. if (!*s)
  1138. return;
  1139. }
  1140. }
  1141. /*
  1142. ==================
  1143. Info_Validate
  1144. Some characters are illegal in info strings because they
  1145. can mess up the server's parsing
  1146. ==================
  1147. */
  1148. qboolean Info_Validate (char *s)
  1149. {
  1150. if (strstr (s, "\""))
  1151. return false;
  1152. if (strstr (s, ";"))
  1153. return false;
  1154. return true;
  1155. }
  1156. void Info_SetValueForKey (char *s, char *key, char *value)
  1157. {
  1158. char newi[MAX_INFO_STRING], *v;
  1159. int c;
  1160. int maxsize = MAX_INFO_STRING;
  1161. if (strstr (key, "\\") || strstr (value, "\\") )
  1162. {
  1163. Com_Printf ("Can't use keys or values with a \\\n");
  1164. return;
  1165. }
  1166. if (strstr (key, ";") )
  1167. {
  1168. Com_Printf ("Can't use keys or values with a semicolon\n");
  1169. return;
  1170. }
  1171. if (strstr (key, "\"") || strstr (value, "\"") )
  1172. {
  1173. Com_Printf ("Can't use keys or values with a \"\n");
  1174. return;
  1175. }
  1176. if (strlen(key) > MAX_INFO_KEY-1 || strlen(value) > MAX_INFO_KEY-1)
  1177. {
  1178. Com_Printf ("Keys and values must be < 64 characters.\n");
  1179. return;
  1180. }
  1181. Info_RemoveKey (s, key);
  1182. if (!value || !strlen(value))
  1183. return;
  1184. Com_sprintf (newi, sizeof(newi), "\\%s\\%s", key, value);
  1185. if (strlen(newi) + strlen(s) > maxsize)
  1186. {
  1187. Com_Printf ("Info string length exceeded\n");
  1188. return;
  1189. }
  1190. // only copy ascii values
  1191. s += strlen(s);
  1192. v = newi;
  1193. while (*v)
  1194. {
  1195. c = *v++;
  1196. c &= 127; // strip high bits
  1197. if (c >= 32 && c < 127)
  1198. *s++ = c;
  1199. }
  1200. *s = 0;
  1201. }
  1202. //====================================================================