VInt3.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System;
  2. using UnityEngine;
  3. [Serializable]
  4. public struct VInt3
  5. {
  6. public const int Precision = 1000;
  7. public const float FloatPrecision = 1000f;
  8. public const float PrecisionFactor = 0.001f;
  9. public int x;
  10. public int y;
  11. public int z;
  12. public static readonly VInt3 zero = new VInt3(0, 0, 0);
  13. public static readonly VInt3 one = new VInt3(1000, 1000, 1000);
  14. public static readonly VInt3 half = new VInt3(500, 500, 500);
  15. public static readonly VInt3 forward = new VInt3(0, 0, 1000);
  16. public static readonly VInt3 up = new VInt3(0, 1000, 0);
  17. public static readonly VInt3 right = new VInt3(1000, 0, 0);
  18. public int this[int i]
  19. {
  20. get
  21. {
  22. return (i != 0) ? ((i != 1) ? this.z : this.y) : this.x;
  23. }
  24. set
  25. {
  26. if (i == 0)
  27. {
  28. this.x = value;
  29. }
  30. else if (i == 1)
  31. {
  32. this.y = value;
  33. }
  34. else
  35. {
  36. this.z = value;
  37. }
  38. }
  39. }
  40. public Vector3 vec3
  41. {
  42. get
  43. {
  44. return new Vector3((float)this.x * 0.001f, (float)this.y * 0.001f, (float)this.z * 0.001f);
  45. }
  46. }
  47. public VInt2 xz
  48. {
  49. get
  50. {
  51. return new VInt2(this.x, this.z);
  52. }
  53. }
  54. public int magnitude
  55. {
  56. get
  57. {
  58. long num = (long)this.x;
  59. long num2 = (long)this.y;
  60. long num3 = (long)this.z;
  61. return IntMath.Sqrt(num * num + num2 * num2 + num3 * num3);
  62. }
  63. }
  64. public int magnitude2D
  65. {
  66. get
  67. {
  68. long num = (long)this.x;
  69. long num2 = (long)this.z;
  70. return IntMath.Sqrt(num * num + num2 * num2);
  71. }
  72. }
  73. public int costMagnitude
  74. {
  75. get
  76. {
  77. return this.magnitude;
  78. }
  79. }
  80. public float worldMagnitude
  81. {
  82. get
  83. {
  84. double num = (double)this.x;
  85. double num2 = (double)this.y;
  86. double num3 = (double)this.z;
  87. return (float)Math.Sqrt(num * num + num2 * num2 + num3 * num3) * 0.001f;
  88. }
  89. }
  90. public double sqrMagnitude
  91. {
  92. get
  93. {
  94. double num = (double)this.x;
  95. double num2 = (double)this.y;
  96. double num3 = (double)this.z;
  97. return num * num + num2 * num2 + num3 * num3;
  98. }
  99. }
  100. public long sqrMagnitudeLong
  101. {
  102. get
  103. {
  104. long num = (long)this.x;
  105. long num2 = (long)this.y;
  106. long num3 = (long)this.z;
  107. return num * num + num2 * num2 + num3 * num3;
  108. }
  109. }
  110. public long sqrMagnitudeLong2D
  111. {
  112. get
  113. {
  114. long num = (long)this.x;
  115. long num2 = (long)this.z;
  116. return num * num + num2 * num2;
  117. }
  118. }
  119. public int unsafeSqrMagnitude
  120. {
  121. get
  122. {
  123. return this.x * this.x + this.y * this.y + this.z * this.z;
  124. }
  125. }
  126. public VInt3 abs
  127. {
  128. get
  129. {
  130. return new VInt3(Math.Abs(this.x), Math.Abs(this.y), Math.Abs(this.z));
  131. }
  132. }
  133. [Obsolete("Same implementation as .magnitude")]
  134. public float safeMagnitude
  135. {
  136. get
  137. {
  138. double num = (double)this.x;
  139. double num2 = (double)this.y;
  140. double num3 = (double)this.z;
  141. return (float)Math.Sqrt(num * num + num2 * num2 + num3 * num3);
  142. }
  143. }
  144. [Obsolete(".sqrMagnitude is now per default safe (.unsafeSqrMagnitude can be used for unsafe operations)")]
  145. public float safeSqrMagnitude
  146. {
  147. get
  148. {
  149. float num = (float)this.x * 0.001f;
  150. float num2 = (float)this.y * 0.001f;
  151. float num3 = (float)this.z * 0.001f;
  152. return num * num + num2 * num2 + num3 * num3;
  153. }
  154. }
  155. public VInt3(Vector3 position)
  156. {
  157. this.x = (int)Math.Round((double)(position.x * 1000f));
  158. this.y = (int)Math.Round((double)(position.y * 1000f));
  159. this.z = (int)Math.Round((double)(position.z * 1000f));
  160. }
  161. public VInt3(int _x, int _y, int _z)
  162. {
  163. this.x = _x;
  164. this.y = _y;
  165. this.z = _z;
  166. }
  167. public VInt3 DivBy2()
  168. {
  169. this.x >>= 1;
  170. this.y >>= 1;
  171. this.z >>= 1;
  172. return this;
  173. }
  174. public static float Angle(VInt3 lhs, VInt3 rhs)
  175. {
  176. double num = (double)VInt3.Dot(lhs, rhs) / ((double)lhs.magnitude * (double)rhs.magnitude);
  177. num = ((num >= -1.0) ? ((num <= 1.0) ? num : 1.0) : -1.0);
  178. return (float)Math.Acos(num);
  179. }
  180. public static VFactor AngleInt(VInt3 lhs, VInt3 rhs)
  181. {
  182. long den = (long)lhs.magnitude * (long)rhs.magnitude;
  183. return IntMath.acos((long)VInt3.Dot(ref lhs, ref rhs), den);
  184. }
  185. public static int Dot(ref VInt3 lhs, ref VInt3 rhs)
  186. {
  187. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
  188. }
  189. public static int Dot(VInt3 lhs, VInt3 rhs)
  190. {
  191. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
  192. }
  193. public static long DotLong(VInt3 lhs, VInt3 rhs)
  194. {
  195. return (long)lhs.x * (long)rhs.x + (long)lhs.y * (long)rhs.y + (long)lhs.z * (long)rhs.z;
  196. }
  197. public static long DotLong(ref VInt3 lhs, ref VInt3 rhs)
  198. {
  199. return (long)lhs.x * (long)rhs.x + (long)lhs.y * (long)rhs.y + (long)lhs.z * (long)rhs.z;
  200. }
  201. public static long DotXZLong(ref VInt3 lhs, ref VInt3 rhs)
  202. {
  203. return (long)lhs.x * (long)rhs.x + (long)lhs.z * (long)rhs.z;
  204. }
  205. public static long DotXZLong(VInt3 lhs, VInt3 rhs)
  206. {
  207. return (long)lhs.x * (long)rhs.x + (long)lhs.z * (long)rhs.z;
  208. }
  209. public static VInt3 Cross(ref VInt3 lhs, ref VInt3 rhs)
  210. {
  211. return new VInt3(IntMath.Divide(lhs.y * rhs.z - lhs.z * rhs.y, 1000), IntMath.Divide(lhs.z * rhs.x - lhs.x * rhs.z, 1000), IntMath.Divide(lhs.x * rhs.y - lhs.y * rhs.x, 1000));
  212. }
  213. public static VInt3 Cross(VInt3 lhs, VInt3 rhs)
  214. {
  215. return new VInt3(IntMath.Divide(lhs.y * rhs.z - lhs.z * rhs.y, 1000), IntMath.Divide(lhs.z * rhs.x - lhs.x * rhs.z, 1000), IntMath.Divide(lhs.x * rhs.y - lhs.y * rhs.x, 1000));
  216. }
  217. public static VInt3 MoveTowards(VInt3 from, VInt3 to, int dt)
  218. {
  219. if ((to - from).sqrMagnitudeLong <= (long)(dt * dt))
  220. {
  221. return to;
  222. }
  223. return from + (to - from).NormalizeTo(dt);
  224. }
  225. public VInt3 Normal2D()
  226. {
  227. return new VInt3(this.z, this.y, -this.x);
  228. }
  229. public VInt3 NormalizeTo(int newMagn)
  230. {
  231. long num = (long)(this.x * 100);
  232. long num2 = (long)(this.y * 100);
  233. long num3 = (long)(this.z * 100);
  234. long num4 = num * num + num2 * num2 + num3 * num3;
  235. if (num4 == 0L)
  236. {
  237. return this;
  238. }
  239. long b = (long)IntMath.Sqrt(num4);
  240. long num5 = (long)newMagn;
  241. this.x = (int)IntMath.Divide(num * num5, b);
  242. this.y = (int)IntMath.Divide(num2 * num5, b);
  243. this.z = (int)IntMath.Divide(num3 * num5, b);
  244. return this;
  245. }
  246. public long Normalize()
  247. {
  248. long num = (long)((long)this.x << 7);
  249. long num2 = (long)((long)this.y << 7);
  250. long num3 = (long)((long)this.z << 7);
  251. long num4 = num * num + num2 * num2 + num3 * num3;
  252. if (num4 == 0L)
  253. {
  254. return 0L;
  255. }
  256. long num5 = (long)IntMath.Sqrt(num4);
  257. long num6 = 1000L;
  258. this.x = (int)IntMath.Divide(num * num6, num5);
  259. this.y = (int)IntMath.Divide(num2 * num6, num5);
  260. this.z = (int)IntMath.Divide(num3 * num6, num5);
  261. return num5 >> 7;
  262. }
  263. public VInt3 RotateY(ref VFactor radians)
  264. {
  265. VFactor vFactor;
  266. VFactor vFactor2;
  267. IntMath.sincos(out vFactor, out vFactor2, radians.nom, radians.den);
  268. long num = vFactor2.nom * vFactor.den;
  269. long num2 = vFactor2.den * vFactor.nom;
  270. long b = vFactor2.den * vFactor.den;
  271. VInt3 vInt;
  272. vInt.x = (int)IntMath.Divide((long)this.x * num + (long)this.z * num2, b);
  273. vInt.z = (int)IntMath.Divide((long)(-(long)this.x) * num2 + (long)this.z * num, b);
  274. vInt.y = 0;
  275. return vInt.NormalizeTo(1000);
  276. }
  277. public VInt3 RotateY(int degree)
  278. {
  279. VFactor vFactor;
  280. VFactor vFactor2;
  281. IntMath.sincos(out vFactor, out vFactor2, (long)(31416 * degree), 1800000L);
  282. long num = vFactor2.nom * vFactor.den;
  283. long num2 = vFactor2.den * vFactor.nom;
  284. long b = vFactor2.den * vFactor.den;
  285. VInt3 vInt;
  286. vInt.x = (int)IntMath.Divide((long)this.x * num + (long)this.z * num2, b);
  287. vInt.z = (int)IntMath.Divide((long)(-(long)this.x) * num2 + (long)this.z * num, b);
  288. vInt.y = 0;
  289. return vInt.NormalizeTo(1000);
  290. }
  291. public override string ToString()
  292. {
  293. return string.Concat(new object[]
  294. {
  295. "( ",
  296. this.x,
  297. ", ",
  298. this.y,
  299. ", ",
  300. this.z,
  301. ")"
  302. });
  303. }
  304. public override bool Equals(object o)
  305. {
  306. if (o == null)
  307. {
  308. return false;
  309. }
  310. VInt3 vInt = (VInt3)o;
  311. return this.x == vInt.x && this.y == vInt.y && this.z == vInt.z;
  312. }
  313. public override int GetHashCode()
  314. {
  315. return this.x * 73856093 ^ this.y * 19349663 ^ this.z * 83492791;
  316. }
  317. public static VInt3 Lerp(VInt3 a, VInt3 b, float f)
  318. {
  319. return new VInt3(Mathf.RoundToInt((float)a.x * (1f - f)) + Mathf.RoundToInt((float)b.x * f), Mathf.RoundToInt((float)a.y * (1f - f)) + Mathf.RoundToInt((float)b.y * f), Mathf.RoundToInt((float)a.z * (1f - f)) + Mathf.RoundToInt((float)b.z * f));
  320. }
  321. public static VInt3 Lerp(VInt3 a, VInt3 b, VFactor f)
  322. {
  323. return new VInt3((int)IntMath.Divide((long)(b.x - a.x) * f.nom, f.den) + a.x, (int)IntMath.Divide((long)(b.y - a.y) * f.nom, f.den) + a.y, (int)IntMath.Divide((long)(b.z - a.z) * f.nom, f.den) + a.z);
  324. }
  325. public static VInt3 Lerp(VInt3 a, VInt3 b, int factorNom, int factorDen)
  326. {
  327. return new VInt3(IntMath.Divide((b.x - a.x) * factorNom, factorDen) + a.x, IntMath.Divide((b.y - a.y) * factorNom, factorDen) + a.y, IntMath.Divide((b.z - a.z) * factorNom, factorDen) + a.z);
  328. }
  329. public long XZSqrMagnitude(VInt3 rhs)
  330. {
  331. long num = (long)(this.x - rhs.x);
  332. long num2 = (long)(this.z - rhs.z);
  333. return num * num + num2 * num2;
  334. }
  335. public long XZSqrMagnitude(ref VInt3 rhs)
  336. {
  337. long num = (long)(this.x - rhs.x);
  338. long num2 = (long)(this.z - rhs.z);
  339. return num * num + num2 * num2;
  340. }
  341. public bool IsEqualXZ(VInt3 rhs)
  342. {
  343. return this.x == rhs.x && this.z == rhs.z;
  344. }
  345. public bool IsEqualXZ(ref VInt3 rhs)
  346. {
  347. return this.x == rhs.x && this.z == rhs.z;
  348. }
  349. public static bool operator ==(VInt3 lhs, VInt3 rhs)
  350. {
  351. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
  352. }
  353. public static bool operator !=(VInt3 lhs, VInt3 rhs)
  354. {
  355. return lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z;
  356. }
  357. public static explicit operator VInt3(Vector3 ob)
  358. {
  359. return new VInt3((int)Math.Round((double)(ob.x * 1000f)), (int)Math.Round((double)(ob.y * 1000f)), (int)Math.Round((double)(ob.z * 1000f)));
  360. }
  361. public static explicit operator Vector3(VInt3 ob)
  362. {
  363. return new Vector3((float)ob.x * 0.001f, (float)ob.y * 0.001f, (float)ob.z * 0.001f);
  364. }
  365. public static VInt3 operator -(VInt3 lhs, VInt3 rhs)
  366. {
  367. lhs.x -= rhs.x;
  368. lhs.y -= rhs.y;
  369. lhs.z -= rhs.z;
  370. return lhs;
  371. }
  372. public static VInt3 operator -(VInt3 lhs)
  373. {
  374. lhs.x = -lhs.x;
  375. lhs.y = -lhs.y;
  376. lhs.z = -lhs.z;
  377. return lhs;
  378. }
  379. public static VInt3 operator +(VInt3 lhs, VInt3 rhs)
  380. {
  381. lhs.x += rhs.x;
  382. lhs.y += rhs.y;
  383. lhs.z += rhs.z;
  384. return lhs;
  385. }
  386. public static VInt3 operator *(VInt3 lhs, int rhs)
  387. {
  388. lhs.x *= rhs;
  389. lhs.y *= rhs;
  390. lhs.z *= rhs;
  391. return lhs;
  392. }
  393. public static VInt3 operator *(VInt3 lhs, float rhs)
  394. {
  395. lhs.x = (int)Math.Round((double)((float)lhs.x * rhs));
  396. lhs.y = (int)Math.Round((double)((float)lhs.y * rhs));
  397. lhs.z = (int)Math.Round((double)((float)lhs.z * rhs));
  398. return lhs;
  399. }
  400. public static VInt3 operator *(VInt3 lhs, double rhs)
  401. {
  402. lhs.x = (int)Math.Round((double)lhs.x * rhs);
  403. lhs.y = (int)Math.Round((double)lhs.y * rhs);
  404. lhs.z = (int)Math.Round((double)lhs.z * rhs);
  405. return lhs;
  406. }
  407. public static VInt3 operator *(VInt3 lhs, Vector3 rhs)
  408. {
  409. lhs.x = (int)Math.Round((double)((float)lhs.x * rhs.x));
  410. lhs.y = (int)Math.Round((double)((float)lhs.y * rhs.y));
  411. lhs.z = (int)Math.Round((double)((float)lhs.z * rhs.z));
  412. return lhs;
  413. }
  414. public static VInt3 operator *(VInt3 lhs, VInt3 rhs)
  415. {
  416. lhs.x *= rhs.x;
  417. lhs.y *= rhs.y;
  418. lhs.z *= rhs.z;
  419. return lhs;
  420. }
  421. public static VInt3 operator /(VInt3 lhs, float rhs)
  422. {
  423. lhs.x = (int)Math.Round((double)((float)lhs.x / rhs));
  424. lhs.y = (int)Math.Round((double)((float)lhs.y / rhs));
  425. lhs.z = (int)Math.Round((double)((float)lhs.z / rhs));
  426. return lhs;
  427. }
  428. public static implicit operator string(VInt3 ob)
  429. {
  430. return ob.ToString();
  431. }
  432. }