enconvert.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. class bool
  2. {
  3. string ToString()
  4. {
  5. if (value) return "true";
  6. else return "false";
  7. }
  8. };
  9. class func
  10. {
  11. //! For internal usage of VM
  12. private proto void SetInstance(Class inst);
  13. };
  14. enum EBool
  15. {
  16. NO = 0,
  17. YES = 1
  18. }
  19. class int
  20. {
  21. protected const int ZERO_PAD_SIZE = 8;
  22. protected static string m_ZeroPad[ZERO_PAD_SIZE] = {"", "0", "00", "000", "0000", "00000", "000000", "0000000"};
  23. const int MAX = 2147483647;
  24. const int MIN = -2147483648;
  25. proto string ToString();
  26. /**
  27. \brief Converts ASCII code to string
  28. \param ascii ASCII code for convert to \p string.
  29. \return \p string - Converted \p int.
  30. @code
  31. int ascii_code = 77;
  32. string str = ascii_code.AsciiToString();
  33. Print(str);
  34. >> str = 'M'
  35. @endcode
  36. */
  37. proto string AsciiToString();
  38. /**
  39. \brief Integer to string with fixed length, padded with zeroes
  40. \param num \p int integer to convert
  41. \param len \p int fixed length
  42. \return \p vector Converted s as vector
  43. @code
  44. int num = 123;
  45. string s = num.ToStringLen(5);
  46. Print(s);
  47. >> s = '00123'
  48. @endcode
  49. */
  50. string ToStringLen(int len)
  51. {
  52. string str = value.ToString();
  53. int l = len - str.Length();
  54. if (l > 0 && l < ZERO_PAD_SIZE )
  55. return m_ZeroPad[l] + str;
  56. return str;
  57. }
  58. /**
  59. \brief Check whether integer falls into an inclusive range
  60. \param min \p int low end of range
  61. \param max \p int high end of range
  62. \return \p bool True if this value is withing the set range
  63. @code
  64. int num = 123;
  65. bool test = num.InRange( 100, 150 );
  66. Print(test);
  67. >> s = true
  68. @endcode
  69. */
  70. bool InRange( int min, int max, bool inclusive_min = true, bool inclusive_max = true )
  71. {
  72. if( ( !inclusive_min && value <= min ) || value < min )
  73. return false;
  74. if( ( !inclusive_max && value >= max ) || value > max )
  75. return false;
  76. return true;
  77. }
  78. };
  79. class float
  80. {
  81. const float MIN = FLT_MIN;
  82. const float MAX = FLT_MAX;
  83. const float LOWEST = -FLT_MAX;
  84. proto string ToString();
  85. };
  86. class vector
  87. {
  88. static const vector Up = "0 1 0";
  89. static const vector Aside = "1 0 0";
  90. static const vector Forward = "0 0 1";
  91. static const vector Zero = "0 0 0";
  92. /**
  93. \brief Vector to string
  94. \param beautify If true verbose vector in more human readable form "<1, 1, 1>" instead of simple form "1 1 1"
  95. \return \p string Converted vector as parsed string
  96. @code
  97. vector v = "1 0 1";
  98. Print( v.ToString() );
  99. Print( v.ToString(false) );
  100. >> '<1, 0, 1>'
  101. >> '1 0 1'
  102. @endcode
  103. */
  104. proto string ToString(bool beautify = true);
  105. /**
  106. \brief Normalizes vector. Returns length
  107. \return \p float Length of origin vector
  108. @code
  109. vector vec = "1 0 1";
  110. float length = vec.Normalize();
  111. Print( vec );
  112. Print( length );
  113. >> vec = <0.707107,0,0.707107>
  114. >> length = 1.41421
  115. @endcode
  116. */
  117. proto float Normalize();
  118. //! return normalized vector (keeps orginal vector untouched)
  119. proto vector Normalized();
  120. /**
  121. \brief Returns length of vector (magnitude)
  122. \return \p float Length of vector
  123. @code
  124. vector vec = "1 0 1";
  125. float length = vec.Length();
  126. Print( length );
  127. >> length = 1.41421
  128. @endcode
  129. */
  130. proto native float Length();
  131. /**
  132. \brief Returns squared length (magnitudeSqr)
  133. \return \p float Length of vector
  134. @code
  135. vector vec = "1 1 0";
  136. float length = vec.LengthSq();
  137. Print( length );
  138. >> length = 2
  139. @endcode
  140. */
  141. proto native float LengthSq();
  142. /**
  143. \brief Returns the distance between tips of two 3D vectors.
  144. \param v1 \p vector 3D Vector 1
  145. \param v2 \p vector 3D Vector 2
  146. \return \p float Distance
  147. @code
  148. float dist = vector.Distance( "1 2 3", "4 5 6" );
  149. Print( dist );
  150. >> dist = 5.19615
  151. @endcode
  152. */
  153. proto static native float Distance(vector v1, vector v2);
  154. /**
  155. \brief Returns the square distance between tips of two 3D vectors.
  156. \param v1 \p vector 3D Vector 1
  157. \param v2 \p vector 3D Vector 2
  158. \return \p float Squere distance
  159. @code
  160. float dist = vector.DistanceSq( "0 0 0", "0 5 0" );
  161. Print( dist );
  162. >> dist = 25
  163. @endcode
  164. */
  165. proto static native float DistanceSq(vector v1, vector v2);
  166. /**
  167. \brief Returns perpendicular vector. Perpendicular vector is computed as cross product between input vector and up vector (0, 1, 0).
  168. \return \p vector perpendicular vector
  169. @code
  170. vector vec = "1 0 0";
  171. Print( vec.Perpend() );
  172. >> <0,0,1>
  173. @endcode
  174. */
  175. vector Perpend()
  176. {
  177. return value * vector.Up;
  178. }
  179. /**
  180. \brief Returns direction vector from point p1 to point p2
  181. \param p1 \p vector point from
  182. \param p2 \p vector point to
  183. \return \p vector direction vector
  184. */
  185. static vector Direction(vector p1, vector p2)
  186. {
  187. vector dir_vec;
  188. dir_vec[0] = p2[0] - p1[0];
  189. dir_vec[1] = p2[1] - p1[1];
  190. dir_vec[2] = p2[2] - p1[2];
  191. return dir_vec;
  192. }
  193. /**
  194. \brief Returns randomly generated unit vector
  195. \return \p randomly generated unit vector
  196. @code
  197. vector vec = vector.RandomDir();
  198. Print(vec);
  199. Print(vec.Length());
  200. >> <-0.179424,0.966825,0.181816>
  201. >> 1
  202. @endcode
  203. */
  204. static vector RandomDir()
  205. {
  206. return Vector(Math.RandomFloatInclusive(-1,1),Math.RandomFloatInclusive(-1,1),Math.RandomFloatInclusive(-1,1)).Normalized();
  207. }
  208. /**
  209. \brief Returns randomly generated XZ unit vector with the Y(up) axis set to 0
  210. \return \p randomly generated XZ unit vector
  211. @code
  212. vector vec = vector.RandomDir();
  213. Print(vec);
  214. Print(vec.Length());
  215. >> <0.631697,0,0.775216>
  216. >> 1
  217. @endcode
  218. */
  219. static vector RandomDir2D()
  220. {
  221. return Vector(Math.RandomFloatInclusive(-1,1),0,Math.RandomFloatInclusive(-1,1)).Normalized();
  222. }
  223. /**
  224. \brief Returns Dot product of vector v1 and vector v2
  225. \param v1 \p vector input vector
  226. \param v2 \p vector input vector
  227. \return \p vector direction vector
  228. */
  229. static float Dot(vector v1, vector v2)
  230. {
  231. return ((v1[0] * v2[0]) + (v1[1] * v2[1]) + (v1[2] * v2[2]));
  232. }
  233. /**
  234. \brief Returns relative angles between -180 and 180, not 0 to 360
  235. \return \p float Relative angles
  236. @code
  237. vector angles = "-45 190 160";
  238. Print( angles.GetRelAngles() );
  239. >> <-45,-170,160>
  240. @endcode
  241. */
  242. vector GetRelAngles()
  243. {
  244. for(int i = 0; i < 3; i++) {
  245. if(value[i] > 180)
  246. value[i] = value[i] - 360;
  247. if(value[i] < -180)
  248. value[i] = value[i] + 360;
  249. }
  250. return value;
  251. }
  252. /**
  253. \brief Returns yaw of vector
  254. \param vec \p vector Vector to convert yaw
  255. \return \p float Yaw of vector
  256. @code
  257. vector v1 = "0 1 0";
  258. vector v2 = "0.7 0.7 0";
  259. Print( v1.ToYaw() );
  260. Print( v2.ToYaw() );
  261. >> 90
  262. >> 45
  263. @endcode
  264. */
  265. proto float VectorToYaw();
  266. /**
  267. \brief Returns vector of yaw
  268. \param yaw \p float Value of yaw
  269. \return \p vector Yaw converted in vector
  270. @code
  271. Print( vector.Yaw2Vector(90) );
  272. Print( vector.Yaw2Vector(45) );
  273. >> <0,1,0>
  274. >> <0.707107,0.707107,0>
  275. @endcode
  276. */
  277. proto native static vector YawToVector(float yaw);
  278. /**
  279. \brief Converts vector to spherical coordinates with radius = 1
  280. \return \p vector spherical coordinates (yaw, pitch, roll in degrees)
  281. @code
  282. vector v1 = "1 0 1";
  283. vector v2 = "1 1 1";
  284. Print( v1.VectorToAngles() );
  285. Print( v2.VectorToAngles() );
  286. >> <45,-0,0>
  287. >> <45,35.2644,0>
  288. @endcode
  289. */
  290. proto vector VectorToAngles();
  291. /**
  292. \brief Converts spherical coordinates (yaw, pitch, roll in degrees) to unit length vector
  293. \return \p normalized direction vector
  294. @code
  295. vector v1 = "45 0 0";
  296. vector v2 = "15 60 0";
  297. Print( v1.AnglesToVector() );
  298. Print( v2.AnglesToVector() );
  299. >> <0.707107,0,0.707107>
  300. >> <0.12941,0.866025,0.482963>
  301. @endcode
  302. */
  303. proto vector AnglesToVector();
  304. /**
  305. \brief Creates rotation matrix from angles
  306. \param ang \p vector which contains angles
  307. \param[out] mat \p vector created rotation matrix
  308. @code
  309. vector mat[3];
  310. vector ang = "70 15 45";
  311. ang.RotationMatrixFromAngles( mat );
  312. Print( mat );
  313. >> <0.330366,0.0885213,-0.939693>,<0.458809,0.854988,0.241845>,<0.824835,-0.511037,0.241845>
  314. @endcode
  315. */
  316. proto void RotationMatrixFromAngles(out vector mat[3]);
  317. /**
  318. \brief Transforms position
  319. \param mat \p vector[4] transformation matrix
  320. \param vec \p vector position to transform
  321. \return \p vector transformed position
  322. @code
  323. vector mat[4] = { "1 0 0 0", "0 1 0 0", "0 0 1 1", "3 1 2 1" }; // translation matrix
  324. vector pos = "1 1 1";
  325. Print( pos.Multiply4(mat) );
  326. >> <4,2,3>
  327. @endcode
  328. */
  329. proto vector Multiply4(vector mat[4]);
  330. /**
  331. \brief Transforms vector
  332. \param mat \p vector[3] transformation matrix
  333. \param vec \p vector vector to transform
  334. \return \p vector transformed vector
  335. @code
  336. vector mat[3] = { "2 0 0", "0 3 0", "0 0 1" }; // scale matrix
  337. vector vec = "1 1 1";
  338. Print( vec.Multiply3(mat) );
  339. >> <2,3,1>
  340. @endcode
  341. */
  342. proto vector Multiply3(vector mat[3]);
  343. /**
  344. \brief Invert-transforms position
  345. \param mat \p vector[4] transformation matrix
  346. \param vec \p vector position to transform
  347. \return \p vector transformed position
  348. @code
  349. vector mat[4] = { "1 0 0 0", "0 1 0 0", "0 0 1 1", "3 1 2 1" }; // translation matrix
  350. vector pos = "1 1 1";
  351. Print( pos.InvMultiply4(mat) );
  352. >> <-2,0,-1>
  353. @endcode
  354. */
  355. proto vector InvMultiply4(vector mat[4]);
  356. /**
  357. \brief Invert-transforms vector
  358. \param mat \p vector[3] transformation matrix
  359. \param vec \p vector vector to transform
  360. \return \p vector transformed vector
  361. @code
  362. vector mat[3] = { "1.5 2.5 0", "0.1 1.3 0", "0 0 1" }; // rotation matrix
  363. vector vec = "1 1 1";
  364. Print( vec.InvMultiply3(mat) );
  365. >> <4,1.4,1>
  366. @endcode
  367. */
  368. proto vector InvMultiply3(vector mat[3]);
  369. /**
  370. \brief Lerp between two vectors
  371. @code
  372. vector v1 = Vector(0,0,0);
  373. vector v2 = Vector(5,6,1);
  374. Print( vector.Lerp(v1, v2, 0.5) );
  375. @endcode
  376. */
  377. proto static native vector Lerp(vector v1, vector v2, float t);
  378. /**
  379. \brief Rotate a vector around 0,0,0 by an angle in degrees
  380. \param vec \p vector to rotate
  381. \param axis \p axis to rotate around
  382. \param cosAngle \p angle in degrees
  383. \return \p vector transformed vector
  384. */
  385. static vector RotateAroundZeroDeg(vector vec, vector axis, float angle)
  386. {
  387. return (vec * Math.Cos(angle * Math.DEG2RAD)) + ((axis * vec) * Math.Sin(angle * Math.DEG2RAD)) + (axis * vector.Dot(axis, vec)) * (1 - Math.Cos(angle * Math.DEG2RAD));
  388. }
  389. /**
  390. \brief Rotate a vector around 0,0,0 by an angle in radians
  391. \param vec \p vector to rotate
  392. \param axis \p axis to rotate around
  393. \param cosAngle \p angle in radians
  394. \return \p vector transformed vector
  395. */
  396. static vector RotateAroundZeroRad(vector vec, vector axis, float angle)
  397. {
  398. return (vec * Math.Cos(angle)) + ((axis * vec) * Math.Sin(angle)) + (axis * vector.Dot(axis, vec)) * (1 - Math.Cos(angle));
  399. }
  400. /**
  401. \brief Rotate a vector around 0,0,0
  402. \param pos \p vector to rotate
  403. \param axis \p axis to rotate around
  404. \param cosAngle \p cos of angle
  405. \param sinAngle \p sin of angle
  406. \return \p vector transformed vector
  407. */
  408. static vector RotateAroundZero(vector pos, vector axis, float cosAngle, float sinAngle)
  409. {
  410. return (pos * cosAngle) + ((axis * pos) * sinAngle) + (axis * vector.Dot(axis, pos)) * (1 - cosAngle);
  411. }
  412. /**
  413. \brief Rotate a vector around point
  414. \param point \p point to rotate around
  415. \param pos \p vector to rotate
  416. \param axis \p axis to rotate around
  417. \param cosAngle \p cos of angle
  418. \param sinAngle \p sin of angle
  419. \return \p vector transformed vector
  420. */
  421. static vector RotateAroundPoint(vector point, vector pos, vector axis, float cosAngle, float sinAngle)
  422. {
  423. vector offsetPos = pos - point;
  424. return RotateAroundZero(offsetPos, axis, cosAngle, sinAngle) + point;
  425. }
  426. /**
  427. \brief Convert static array of floats into a vector
  428. \param arr \p vector in array format
  429. \return \p vector resulting vector
  430. */
  431. static vector ArrayToVec(float arr[])
  432. {
  433. return Vector(arr[0], arr[1], arr[2]);
  434. }
  435. };
  436. class typename
  437. {
  438. /**
  439. \brief Dynamic variant to 'new' keyword. It creates new instance of class
  440. \returns \p volatile instance of class
  441. @code
  442. ???
  443. @endcode
  444. */
  445. proto volatile Class Spawn();
  446. /**
  447. \brief Get the name of the module the typename belongs to
  448. \returns \p string Name of parent module (1_Core)
  449. */
  450. proto owned string GetModule();
  451. //!Returns type name of variable as string
  452. proto native owned string ToString();
  453. /**
  454. \brief Returns true when type is the same as 'baseType', or inherited one.
  455. \param baseType typename
  456. \returns \p bool true when type is the same as 'baseType', or inherited one.
  457. @code
  458. ???
  459. @endcode
  460. */
  461. proto native bool IsInherited(typename baseType);
  462. proto native int GetVariableCount();
  463. proto native owned string GetVariableName(int vIdx);
  464. proto native typename GetVariableType(int vIdx);
  465. proto bool GetVariableValue(Class var, int vIdx, out void val);
  466. /**
  467. \brief Return string name of enum value
  468. @code
  469. DialogPriority prio = DialogPriority.WARNING;
  470. Print( typename.EnumToString(DialogPriority, prio) );
  471. @endcode
  472. */
  473. static string EnumToString(typename e, int enumValue)
  474. {
  475. int cnt = e.GetVariableCount();
  476. int val;
  477. for (int i = 0; i < cnt; i++)
  478. {
  479. if (e.GetVariableType(i) == int && e.GetVariableValue(null, i, val) && val == enumValue)
  480. {
  481. return e.GetVariableName(i);
  482. }
  483. }
  484. return "unknown";
  485. }
  486. /**
  487. \brief Return enum value from string name
  488. @code
  489. Print( typename.StringToEnum(DialogPriority, "WARNING") );
  490. @endcode
  491. */
  492. static int StringToEnum(typename e, string enumName)
  493. {
  494. int count = e.GetVariableCount();
  495. int value;
  496. for (int i = 0; i < count; i++)
  497. {
  498. if (e.GetVariableType(i) == int && e.GetVariableValue(null, i, value) && e.GetVariableName(i) == enumName)
  499. {
  500. return value;
  501. }
  502. }
  503. return -1;
  504. }
  505. };
  506. class EnumTools
  507. {
  508. private void EnumTools();
  509. private void ~EnumTools();
  510. /**
  511. \brief Return string name of enum value
  512. @code
  513. DialogPriority prio = DialogPriority.WARNING;
  514. Print( EnumTools.EnumToString(DialogPriority, prio) );
  515. @endcode
  516. */
  517. static string EnumToString(typename e, int enumValue)
  518. {
  519. return typename.EnumToString(e, enumValue);
  520. }
  521. /**
  522. \brief Return enum value from string name
  523. @code
  524. Print( EnumTools.StringToEnum(DialogPriority, "WARNING") );
  525. @endcode
  526. */
  527. static int StringToEnum(typename e, string enumName)
  528. {
  529. return typename.StringToEnum(e, enumName);
  530. }
  531. /**
  532. \brief Return amount of values in enum
  533. @code
  534. Print( EnumTools.GetEnumSize(DialogPriority) );
  535. @endcode
  536. */
  537. static int GetEnumSize(typename e)
  538. {
  539. return e.GetVariableCount();
  540. }
  541. /**
  542. \brief Return the nth value in the enum
  543. @code
  544. Print( EnumTools.GetEnumValue(DialogPriority, 1) );
  545. @endcode
  546. */
  547. static int GetEnumValue(typename e, int idx)
  548. {
  549. int value;
  550. e.GetVariableValue(null, idx, value);
  551. return value;
  552. }
  553. /**
  554. \brief Return amount of values in enum
  555. @code
  556. Print( EnumTools.GetLastEnumValue(DialogPriority) );
  557. @endcode
  558. */
  559. static int GetLastEnumValue(typename e)
  560. {
  561. int lastValue;
  562. e.GetVariableValue(null, e.GetVariableCount() - 1, lastValue);
  563. return lastValue;
  564. }
  565. }