q_shared.c 24 KB

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