LSL_Types.cs 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Globalization;
  30. using System.Text;
  31. using System.Text.RegularExpressions;
  32. using OpenSim.Framework;
  33. using OpenMetaverse;
  34. using OMV_Vector3 = OpenMetaverse.Vector3;
  35. using OMV_Vector3d = OpenMetaverse.Vector3d;
  36. using OMV_Quaternion = OpenMetaverse.Quaternion;
  37. namespace OpenSim.Region.ScriptEngine.Shared
  38. {
  39. public partial class LSL_Types
  40. {
  41. // Types are kept is separate .dll to avoid having to add whatever .dll it is in it to script AppDomain
  42. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  43. public unsafe static bool IsBadNumber(double d)
  44. {
  45. return (*(long*)(&d) & 0x7FFFFFFFFFFFFFFF) >= 0x7FF0000000000000;
  46. }
  47. [Serializable]
  48. public struct Vector3
  49. {
  50. public double x;
  51. public double y;
  52. public double z;
  53. #region Constructors
  54. public Vector3(Vector3 vector)
  55. {
  56. x = (float)vector.x;
  57. y = (float)vector.y;
  58. z = (float)vector.z;
  59. }
  60. public Vector3(OMV_Vector3 vector)
  61. {
  62. x = vector.X;
  63. y = vector.Y;
  64. z = vector.Z;
  65. }
  66. public Vector3(OMV_Vector3d vector)
  67. {
  68. x = vector.X;
  69. y = vector.Y;
  70. z = vector.Z;
  71. }
  72. public Vector3(double X, double Y, double Z)
  73. {
  74. x = X;
  75. y = Y;
  76. z = Z;
  77. }
  78. public Vector3(string str)
  79. {
  80. str = str.Replace('<', ' ');
  81. str = str.Replace('>', ' ');
  82. string[] tmps = str.Split(new Char[] {','});
  83. if (tmps.Length < 3)
  84. {
  85. z = y = x = 0;
  86. return;
  87. }
  88. if (!Double.TryParse(tmps[0], NumberStyles.Float, Culture.NumberFormatInfo, out x))
  89. {
  90. z = y = 0;
  91. return;
  92. }
  93. if (!Double.TryParse(tmps[1], NumberStyles.Float, Culture.NumberFormatInfo, out y))
  94. {
  95. z = x = 0;
  96. return;
  97. }
  98. if (!Double.TryParse(tmps[2], NumberStyles.Float, Culture.NumberFormatInfo, out z))
  99. {
  100. y = x = 0;
  101. }
  102. }
  103. #endregion
  104. #region Overriders
  105. public static implicit operator Boolean(Vector3 vec)
  106. {
  107. if (vec.x != 0)
  108. return true;
  109. if (vec.y != 0)
  110. return true;
  111. if (vec.z != 0)
  112. return true;
  113. return false;
  114. }
  115. public override string ToString()
  116. {
  117. string s = String.Format(Culture.FormatProvider, "<{0:0.000000}, {1:0.000000}, {2:0.000000}>", x, y, z);
  118. return s;
  119. }
  120. public static explicit operator LSLString(Vector3 vec)
  121. {
  122. string s = String.Format(Culture.FormatProvider, "<{0:0.000000}, {1:0.000000}, {2:0.000000}>", vec.x, vec.y, vec.z);
  123. return new LSLString(s);
  124. }
  125. public static explicit operator string(Vector3 vec)
  126. {
  127. string s = String.Format(Culture.FormatProvider, "<{0:0.000000}, {1:0.000000}, {2:0.000000}>", vec.x, vec.y, vec.z);
  128. return s;
  129. }
  130. public static explicit operator Vector3(string s)
  131. {
  132. return new Vector3(s);
  133. }
  134. public static implicit operator list(Vector3 vec)
  135. {
  136. return new list(new object[] { vec });
  137. }
  138. public static implicit operator OMV_Vector3(Vector3 vec)
  139. {
  140. return new OMV_Vector3((float)vec.x, (float)vec.y, (float)vec.z);
  141. }
  142. public static implicit operator Vector3(OMV_Vector3 vec)
  143. {
  144. return new Vector3(vec);
  145. }
  146. public static implicit operator OMV_Vector3d(Vector3 vec)
  147. {
  148. return new OMV_Vector3d(vec.x, vec.y, vec.z);
  149. }
  150. public static implicit operator Vector3(OMV_Vector3d vec)
  151. {
  152. return new Vector3(vec);
  153. }
  154. public static bool operator ==(Vector3 lhs, Vector3 rhs)
  155. {
  156. return (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z);
  157. }
  158. public static bool operator !=(Vector3 lhs, Vector3 rhs)
  159. {
  160. return !(lhs == rhs);
  161. }
  162. public override int GetHashCode()
  163. {
  164. return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode());
  165. }
  166. public override bool Equals(object o)
  167. {
  168. if (!(o is Vector3)) return false;
  169. Vector3 vector = (Vector3)o;
  170. return (x == vector.x && y == vector.y && z == vector.z);
  171. }
  172. public static Vector3 operator -(Vector3 vector)
  173. {
  174. return new Vector3(-vector.x, -vector.y, -vector.z);
  175. }
  176. #endregion
  177. #region Vector & Vector Math
  178. // Vector-Vector Math
  179. public static Vector3 operator +(Vector3 lhs, Vector3 rhs)
  180. {
  181. return new Vector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
  182. }
  183. public static Vector3 operator -(Vector3 lhs, Vector3 rhs)
  184. {
  185. return new Vector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
  186. }
  187. public static LSLFloat operator *(Vector3 lhs, Vector3 rhs)
  188. {
  189. return Dot(lhs, rhs);
  190. }
  191. public static Vector3 operator %(Vector3 v1, Vector3 v2)
  192. {
  193. //Cross product
  194. Vector3 tv;
  195. tv.x = (v1.y * v2.z) - (v1.z * v2.y);
  196. tv.y = (v1.z * v2.x) - (v1.x * v2.z);
  197. tv.z = (v1.x * v2.y) - (v1.y * v2.x);
  198. return tv;
  199. }
  200. #endregion
  201. #region Vector & Float Math
  202. // Vector-Float and Float-Vector Math
  203. public static Vector3 operator *(Vector3 vec, float val)
  204. {
  205. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  206. }
  207. public static Vector3 operator *(float val, Vector3 vec)
  208. {
  209. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  210. }
  211. public static Vector3 operator /(Vector3 v, float f)
  212. {
  213. double r = v.x / f;
  214. if (IsBadNumber(r))
  215. throw new ScriptException("Vector division by zero");
  216. v.x = r;
  217. r = v.y / f;
  218. if (IsBadNumber(r))
  219. throw new ScriptException("Vector division by zero");
  220. v.y = r;
  221. r = v.z / f;
  222. if (IsBadNumber(r))
  223. throw new ScriptException("Vector division by zero");
  224. v.z = r;
  225. return v;
  226. }
  227. #endregion
  228. #region Vector & Double Math
  229. public static Vector3 operator *(Vector3 vec, double val)
  230. {
  231. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  232. }
  233. public static Vector3 operator *(double val, Vector3 vec)
  234. {
  235. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  236. }
  237. public static Vector3 operator /(Vector3 v, double f)
  238. {
  239. double r = v.x / f;
  240. if (IsBadNumber(r))
  241. throw new ScriptException("Vector division by zero");
  242. v.x = r;
  243. r = v.y / f;
  244. if (IsBadNumber(r))
  245. throw new ScriptException("Vector division by zero");
  246. v.y = r;
  247. r = v.z / f;
  248. if (IsBadNumber(r))
  249. throw new ScriptException("Vector division by zero");
  250. v.z = r;
  251. return v;
  252. }
  253. #endregion
  254. #region Vector & Rotation Math
  255. // Vector-Rotation Math
  256. public static Vector3 operator *(Vector3 v, Quaternion r)
  257. {
  258. double rx = r.s * v.x + r.y * v.z - r.z * v.y;
  259. double ry = r.s * v.y + r.z * v.x - r.x * v.z;
  260. double rz = r.s * v.z + r.x * v.y - r.y * v.x;
  261. v.x += 2.0f * (rz * r.y - ry * r.z);
  262. v.y += 2.0f * (rx * r.z - rz * r.x);
  263. v.z += 2.0f * (ry * r.x - rx * r.y);
  264. return v;
  265. }
  266. public static Vector3 operator /(Vector3 v, Quaternion r)
  267. {
  268. r.s = -r.s;
  269. return v * r;
  270. }
  271. #endregion
  272. #region Static Helper Functions
  273. public static double Dot(Vector3 v1, Vector3 v2)
  274. {
  275. return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
  276. }
  277. public static Vector3 Cross(Vector3 v1, Vector3 v2)
  278. {
  279. return new Vector3
  280. (
  281. v1.y * v2.z - v1.z * v2.y,
  282. v1.z * v2.x - v1.x * v2.z,
  283. v1.x * v2.y - v1.y * v2.x
  284. );
  285. }
  286. public static double MagSquare(Vector3 v)
  287. {
  288. return v.x * v.x + v.y * v.y + v.z * v.z;
  289. }
  290. public static double Mag(Vector3 v)
  291. {
  292. return Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  293. }
  294. public static Vector3 Norm(Vector3 vector)
  295. {
  296. double mag = Mag(vector);
  297. if (mag > 0.0)
  298. {
  299. double invMag = 1.0 / mag;
  300. return vector * invMag;
  301. }
  302. return new Vector3(0, 0, 0);
  303. }
  304. public static Vector3 Slerp(Vector3 v1, Vector3 v2, double amount)
  305. {
  306. double angle = (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
  307. double scale;
  308. double invscale;
  309. if (angle < 0.999f)
  310. {
  311. angle = Math.Acos(angle);
  312. invscale = 1.0f / Math.Sin(angle);
  313. scale = Math.Sin((1.0f - amount) * angle) * invscale;
  314. invscale *= Math.Sin((amount * angle));
  315. }
  316. else
  317. {
  318. scale = 1.0f - amount;
  319. invscale = amount;
  320. }
  321. return new Vector3(
  322. v1.x * scale + v2.x * invscale,
  323. v1.y * scale + v2.y * invscale,
  324. v1.z * scale + v2.z * invscale
  325. );
  326. }
  327. #endregion
  328. }
  329. [Serializable]
  330. public struct Quaternion
  331. {
  332. public double x;
  333. public double y;
  334. public double z;
  335. public double s;
  336. #region Constructors
  337. public Quaternion(Quaternion Quat)
  338. {
  339. x = (float)Quat.x;
  340. y = (float)Quat.y;
  341. z = (float)Quat.z;
  342. s = (float)Quat.s;
  343. if (s == 0 && x == 0 && y == 0 && z == 0)
  344. s = 1;
  345. }
  346. public Quaternion(double X, double Y, double Z, double S)
  347. {
  348. x = X;
  349. y = Y;
  350. z = Z;
  351. s = S;
  352. if (s == 0 && x == 0 && y == 0 && z == 0)
  353. s = 1;
  354. }
  355. public Quaternion(string str)
  356. {
  357. str = str.Replace('<', ' ');
  358. str = str.Replace('>', ' ');
  359. string[] tmps = str.Split(new Char[] {','});
  360. if (tmps.Length < 4 ||
  361. !Double.TryParse(tmps[3], NumberStyles.Float, Culture.NumberFormatInfo, out s))
  362. {
  363. z = y = x = 0;
  364. s = 1;
  365. return;
  366. }
  367. if (!Double.TryParse(tmps[0], NumberStyles.Float, Culture.NumberFormatInfo, out x))
  368. {
  369. z = y = 0;
  370. s = 1;
  371. return;
  372. }
  373. if (!Double.TryParse(tmps[1], NumberStyles.Float, Culture.NumberFormatInfo, out y))
  374. {
  375. z = x = 0;
  376. s = 1;
  377. return;
  378. }
  379. if (!Double.TryParse(tmps[2], NumberStyles.Float, Culture.NumberFormatInfo, out z))
  380. {
  381. y = x = 0;
  382. s = 1;
  383. }
  384. }
  385. public Quaternion(OMV_Quaternion rot)
  386. {
  387. x = rot.X;
  388. y = rot.Y;
  389. z = rot.Z;
  390. s = rot.W;
  391. }
  392. #endregion
  393. #region Methods
  394. public Quaternion Normalize()
  395. {
  396. double lengthsq = x * x + y * y + z * z + s * s;
  397. if (lengthsq < float.Epsilon)
  398. {
  399. x = 0;
  400. y = 0;
  401. z = 0;
  402. s = 1;
  403. }
  404. else
  405. {
  406. double invLength = 1.0 / Math.Sqrt(lengthsq);
  407. x *= invLength;
  408. y *= invLength;
  409. z *= invLength;
  410. s *= invLength;
  411. }
  412. return this;
  413. }
  414. public static Quaternion Slerp(Quaternion q1, Quaternion q2, double amount)
  415. {
  416. double angle = (q1.x * q2.x) + (q1.y * q2.y) + (q1.z * q2.z) + (q1.s * q2.s);
  417. if (angle < 0f)
  418. {
  419. q1.x = -q1.x;
  420. q1.y = -q1.y;
  421. q1.z = -q1.z;
  422. q1.s = -q1.s;
  423. angle *= -1.0;
  424. }
  425. double scale;
  426. double invscale;
  427. if ((angle + 1.0) > 0.0005)
  428. {
  429. if ((1f - angle) >= 0.0005)
  430. {
  431. // slerp
  432. double theta = Math.Acos(angle);
  433. double invsintheta = 1.0 / Math.Sin(theta);
  434. scale = Math.Sin(theta * (1.0 - amount)) * invsintheta;
  435. invscale = Math.Sin(theta * amount) * invsintheta;
  436. }
  437. else
  438. {
  439. // lerp
  440. scale = 1.0 - amount;
  441. invscale = amount;
  442. }
  443. }
  444. else
  445. {
  446. q2.x = -q1.y;
  447. q2.y = q1.x;
  448. q2.z = -q1.s;
  449. q2.s = q1.z;
  450. scale = Math.Sin(Math.PI * (0.5 - amount));
  451. invscale = Math.Sin(Math.PI * amount);
  452. }
  453. return new Quaternion(
  454. q1.x * scale + q2.x * invscale,
  455. q1.y * scale + q2.y * invscale,
  456. q1.z * scale + q2.z * invscale,
  457. q1.s * scale + q2.s * invscale
  458. );
  459. }
  460. #endregion
  461. #region Overriders
  462. public static implicit operator Boolean(Quaternion q)
  463. {
  464. if (q.x != 0)
  465. return true;
  466. if (q.y != 0)
  467. return true;
  468. if (q.z != 0)
  469. return true;
  470. if (q.s != 1.0f)
  471. return true;
  472. return false;
  473. }
  474. public override int GetHashCode()
  475. {
  476. return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode() ^ s.GetHashCode());
  477. }
  478. public override bool Equals(object o)
  479. {
  480. if (!(o is Quaternion)) return false;
  481. Quaternion quaternion = (Quaternion)o;
  482. return x == quaternion.x && y == quaternion.y && z == quaternion.z && s == quaternion.s;
  483. }
  484. public override string ToString()
  485. {
  486. string st=String.Format(Culture.FormatProvider, "<{0:0.000000}, {1:0.000000}, {2:0.000000}, {3:0.000000}>", x, y, z, s);
  487. return st;
  488. }
  489. public static explicit operator string(Quaternion r)
  490. {
  491. string st=String.Format(Culture.FormatProvider,"<{0:0.000000}, {1:0.000000}, {2:0.000000}, {3:0.000000}>", r.x, r.y, r.z, r.s);
  492. return st;
  493. }
  494. public static explicit operator LSLString(Quaternion r)
  495. {
  496. string st=String.Format(Culture.FormatProvider,"<{0:0.000000}, {1:0.000000}, {2:0.000000}, {3:0.000000}>", r.x, r.y, r.z, r.s);
  497. return new LSLString(st);
  498. }
  499. public static explicit operator Quaternion(string s)
  500. {
  501. return new Quaternion(s);
  502. }
  503. public static implicit operator list(Quaternion r)
  504. {
  505. return new list(new object[] { r });
  506. }
  507. public static implicit operator OMV_Quaternion(Quaternion rot)
  508. {
  509. // LSL quaternions can normalize to 0, normal Quaternions can't.
  510. if (rot.s == 0 && rot.x == 0 && rot.y == 0 && rot.z == 0)
  511. return OMV_Quaternion.Identity; // ZERO_ROTATION = 0,0,0,1
  512. OMV_Quaternion omvrot = new OMV_Quaternion((float)rot.x, (float)rot.y, (float)rot.z, (float)rot.s);
  513. omvrot.Normalize();
  514. return omvrot;
  515. }
  516. public static implicit operator Quaternion(OMV_Quaternion rot)
  517. {
  518. return new Quaternion(rot);
  519. }
  520. public static bool operator ==(Quaternion lhs, Quaternion rhs)
  521. {
  522. // Return true if the fields match:
  523. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.s == rhs.s;
  524. }
  525. public static bool operator !=(Quaternion lhs, Quaternion rhs)
  526. {
  527. return !(lhs == rhs);
  528. }
  529. public static double Mag(Quaternion q)
  530. {
  531. return Math.Sqrt(q.x * q.x + q.y * q.y + q.z * q.z + q.s * q.s);
  532. }
  533. #endregion
  534. public static Quaternion operator +(Quaternion a, Quaternion b)
  535. {
  536. return new Quaternion(a.x + b.x, a.y + b.y, a.z + b.z, a.s + b.s);
  537. }
  538. public static Quaternion operator /(Quaternion a, Quaternion b)
  539. {
  540. // assume normalized
  541. // if not, sl seems to not normalize either
  542. b.x = -b.x;
  543. b.y = -b.y;
  544. b.z = -b.z;
  545. return a * b;
  546. }
  547. public static Quaternion operator -(Quaternion a, Quaternion b)
  548. {
  549. return new Quaternion(a.x - b.x, a.y - b.y, a.z - b.z, a.s - b.s);
  550. }
  551. // using the equations below, we need to do "b * a" to be compatible with LSL
  552. public static Quaternion operator *(Quaternion b, Quaternion a)
  553. {
  554. Quaternion c;
  555. c.x = a.s * b.x + a.x * b.s + a.y * b.z - a.z * b.y;
  556. c.y = a.s * b.y + a.y * b.s + a.z * b.x - a.x * b.z;
  557. c.z = a.s * b.z + a.z * b.s + a.x * b.y - a.y * b.x;
  558. c.s = a.s * b.s - a.x * b.x - a.y * b.y - a.z * b.z;
  559. return c;
  560. }
  561. }
  562. [Serializable]
  563. public class list
  564. {
  565. private object[] m_data;
  566. public list(params object[] args)
  567. {
  568. m_data = args;
  569. }
  570. public int Length
  571. {
  572. get
  573. {
  574. if (m_data == null)
  575. m_data=new Object[0];
  576. return m_data.Length;
  577. }
  578. }
  579. public int Size
  580. {
  581. get
  582. {
  583. if (m_data == null)
  584. m_data=new Object[0];
  585. int size = 0;
  586. foreach (Object o in m_data)
  587. {
  588. if (o is LSL_Types.LSLInteger)
  589. size += 4;
  590. else if (o is LSL_Types.LSLFloat)
  591. size += 8;
  592. else if (o is LSL_Types.LSLString)
  593. size += ((LSL_Types.LSLString)o).m_string == null ? 0 : ((LSL_Types.LSLString)o).m_string.Length;
  594. else if (o is LSL_Types.key)
  595. size += ((LSL_Types.key)o).value.Length;
  596. else if (o is LSL_Types.Vector3)
  597. size += 32;
  598. else if (o is LSL_Types.Quaternion)
  599. size += 64;
  600. else if (o is int)
  601. size += 4;
  602. else if (o is uint)
  603. size += 4;
  604. else if (o is string)
  605. size += ((string)o).Length;
  606. else if (o is float)
  607. size += 8;
  608. else if (o is double)
  609. size += 16;
  610. else if (o is list)
  611. size += ((list)o).Size;
  612. else
  613. throw new Exception("Unknown type in List.Size: " + o.GetType().ToString());
  614. }
  615. return size;
  616. }
  617. }
  618. public object[] Data
  619. {
  620. get {
  621. if (m_data == null)
  622. m_data=new Object[0];
  623. return m_data;
  624. }
  625. set {m_data = value; }
  626. }
  627. /// <summary>
  628. /// Obtain LSL type from an index.
  629. /// </summary>
  630. /// <remarks>
  631. /// This is needed because LSL lists allow for multiple types, and safely
  632. /// iterating in them requires a type check.
  633. /// </remarks>
  634. /// <returns></returns>
  635. /// <param name='itemIndex'></param>
  636. public Type GetLSLListItemType(int itemIndex)
  637. {
  638. return Data[itemIndex].GetType();
  639. }
  640. /// <summary>
  641. /// Obtain float from an index.
  642. /// </summary>
  643. /// <remarks>
  644. /// For cases where implicit conversions would apply if items
  645. /// were not in a list (e.g. integer to float, but not float
  646. /// to integer) functions check for alternate types so as to
  647. /// down-cast from Object to the correct type.
  648. /// Note: no checks for item index being valid are performed
  649. /// </remarks>
  650. /// <returns></returns>
  651. /// <param name='itemIndex'></param>
  652. public LSL_Types.LSLFloat GetLSLFloatItem(int itemIndex)
  653. {
  654. if (Data[itemIndex] is LSL_Types.LSLInteger)
  655. {
  656. return (LSL_Types.LSLInteger)Data[itemIndex];
  657. }
  658. else if (Data[itemIndex] is Int32)
  659. {
  660. return new LSL_Types.LSLFloat((int)Data[itemIndex]);
  661. }
  662. else if (Data[itemIndex] is float)
  663. {
  664. return new LSL_Types.LSLFloat((float)Data[itemIndex]);
  665. }
  666. else if (Data[itemIndex] is Double)
  667. {
  668. return new LSL_Types.LSLFloat((Double)Data[itemIndex]);
  669. }
  670. else if (Data[itemIndex] is LSL_Types.LSLString)
  671. {
  672. return new LSL_Types.LSLFloat(Data[itemIndex].ToString());
  673. }
  674. else
  675. {
  676. return (LSL_Types.LSLFloat)Data[itemIndex];
  677. }
  678. }
  679. public LSL_Types.LSLString GetLSLStringItem(int itemIndex)
  680. {
  681. if (Data[itemIndex] is LSL_Types.key)
  682. {
  683. return (LSL_Types.key)Data[itemIndex];
  684. }
  685. else
  686. {
  687. return new LSL_Types.LSLString(Data[itemIndex].ToString());
  688. }
  689. }
  690. public LSL_Types.LSLInteger GetLSLIntegerItem(int itemIndex)
  691. {
  692. if (Data[itemIndex] is LSL_Types.LSLInteger)
  693. return (LSL_Types.LSLInteger)Data[itemIndex];
  694. if (Data[itemIndex] is LSL_Types.LSLFloat)
  695. return new LSLInteger((int)Data[itemIndex]);
  696. else if (Data[itemIndex] is Int32)
  697. return new LSLInteger((int)Data[itemIndex]);
  698. else if (Data[itemIndex] is LSL_Types.LSLString)
  699. return new LSLInteger(Data[itemIndex].ToString());
  700. else
  701. throw new InvalidCastException(string.Format(
  702. "{0} expected but {1} given",
  703. typeof(LSL_Types.LSLInteger).Name,
  704. Data[itemIndex] != null ?
  705. Data[itemIndex].GetType().Name : "null"));
  706. }
  707. public LSL_Types.Vector3 GetVector3Item(int itemIndex)
  708. {
  709. if (Data[itemIndex] is LSL_Types.Vector3)
  710. {
  711. return (LSL_Types.Vector3)Data[itemIndex];
  712. }
  713. else if(Data[itemIndex] is OpenMetaverse.Vector3)
  714. {
  715. return new LSL_Types.Vector3(
  716. (OpenMetaverse.Vector3)Data[itemIndex]);
  717. }
  718. else
  719. {
  720. throw new InvalidCastException(string.Format(
  721. "{0} expected but {1} given",
  722. typeof(LSL_Types.Vector3).Name,
  723. Data[itemIndex] != null ?
  724. Data[itemIndex].GetType().Name : "null"));
  725. }
  726. }
  727. // use LSL_Types.Quaternion to parse and store a vector4 for lightShare
  728. public LSL_Types.Quaternion GetVector4Item(int itemIndex)
  729. {
  730. if (Data[itemIndex] is LSL_Types.Quaternion)
  731. {
  732. LSL_Types.Quaternion q = (LSL_Types.Quaternion)Data[itemIndex];
  733. return q;
  734. }
  735. else if(Data[itemIndex] is OpenMetaverse.Quaternion)
  736. {
  737. LSL_Types.Quaternion q = new LSL_Types.Quaternion(
  738. (OpenMetaverse.Quaternion)Data[itemIndex]);
  739. q.Normalize();
  740. return q;
  741. }
  742. else
  743. {
  744. throw new InvalidCastException(string.Format(
  745. "{0} expected but {1} given",
  746. typeof(LSL_Types.Quaternion).Name,
  747. Data[itemIndex] != null ?
  748. Data[itemIndex].GetType().Name : "null"));
  749. }
  750. }
  751. public LSL_Types.Quaternion GetQuaternionItem(int itemIndex)
  752. {
  753. if (Data[itemIndex] is LSL_Types.Quaternion)
  754. {
  755. LSL_Types.Quaternion q = (LSL_Types.Quaternion)Data[itemIndex];
  756. q.Normalize();
  757. return q;
  758. }
  759. else if(Data[itemIndex] is OpenMetaverse.Quaternion)
  760. {
  761. LSL_Types.Quaternion q = new LSL_Types.Quaternion(
  762. (OpenMetaverse.Quaternion)Data[itemIndex]);
  763. q.Normalize();
  764. return q;
  765. }
  766. else
  767. {
  768. throw new InvalidCastException(string.Format(
  769. "{0} expected but {1} given",
  770. typeof(LSL_Types.Quaternion).Name,
  771. Data[itemIndex] != null ?
  772. Data[itemIndex].GetType().Name : "null"));
  773. }
  774. }
  775. public LSL_Types.key GetKeyItem(int itemIndex)
  776. {
  777. return (LSL_Types.key)Data[itemIndex];
  778. }
  779. public static list operator +(list a, list b)
  780. {
  781. object[] tmp;
  782. tmp = new object[a.Length + b.Length];
  783. a.Data.CopyTo(tmp, 0);
  784. b.Data.CopyTo(tmp, a.Length);
  785. return new list(tmp);
  786. }
  787. private void ExtendAndAdd(object o)
  788. {
  789. object[] tmp;
  790. tmp = new object[Data.Length + 1];
  791. Data.CopyTo(tmp, 0);
  792. tmp.SetValue(o, tmp.Length - 1);
  793. Data = tmp;
  794. }
  795. public static implicit operator Boolean(list l)
  796. {
  797. return l.Length != 0;
  798. }
  799. public static list operator +(list a, LSLString s)
  800. {
  801. a.ExtendAndAdd(s);
  802. return a;
  803. }
  804. public static list operator +(list a, LSLInteger i)
  805. {
  806. a.ExtendAndAdd(i);
  807. return a;
  808. }
  809. public static list operator +(list a, LSLFloat d)
  810. {
  811. a.ExtendAndAdd(d);
  812. return a;
  813. }
  814. public static bool operator ==(list a, list b)
  815. {
  816. int la = -1;
  817. int lb = -1;
  818. try { la = a.Length; }
  819. catch (NullReferenceException) { }
  820. try { lb = b.Length; }
  821. catch (NullReferenceException) { }
  822. return la == lb;
  823. }
  824. public static bool operator !=(list a, list b)
  825. {
  826. int la = -1;
  827. int lb = -1;
  828. try { la = a.Length; }
  829. catch (NullReferenceException) { }
  830. try { lb = b.Length; }
  831. catch (NullReferenceException) { }
  832. return la != lb;
  833. }
  834. public void Add(object o)
  835. {
  836. object[] tmp;
  837. tmp = new object[Data.Length + 1];
  838. Data.CopyTo(tmp, 0);
  839. tmp[Data.Length] = o; // Since this is tmp.Length - 1
  840. Data = tmp;
  841. }
  842. public bool Contains(object o)
  843. {
  844. bool ret = false;
  845. foreach (object i in Data)
  846. {
  847. if (i == o)
  848. {
  849. ret = true;
  850. break;
  851. }
  852. }
  853. return ret;
  854. }
  855. public list DeleteSublist(int start, int end)
  856. {
  857. // Not an easy one
  858. // If start <= end, remove that part
  859. // if either is negative, count from the end of the array
  860. // if the resulting start > end, remove all BUT that part
  861. Object[] ret;
  862. if (start < 0)
  863. start=Data.Length+start;
  864. if (start < 0)
  865. start=0;
  866. if (end < 0)
  867. end=Data.Length+end;
  868. if (end < 0)
  869. end=0;
  870. if (start > end)
  871. {
  872. if (end >= Data.Length)
  873. return new list(new Object[0]);
  874. if (start >= Data.Length)
  875. start=Data.Length-1;
  876. return GetSublist(end, start);
  877. }
  878. // start >= 0 && end >= 0 here
  879. if (start >= Data.Length)
  880. {
  881. ret=new Object[Data.Length];
  882. Array.Copy(Data, 0, ret, 0, Data.Length);
  883. return new list(ret);
  884. }
  885. if (end >= Data.Length)
  886. end=Data.Length-1;
  887. // now, this makes the math easier
  888. int remove=end+1-start;
  889. ret=new Object[Data.Length-remove];
  890. if (ret.Length == 0)
  891. return new list(ret);
  892. int src;
  893. int dest=0;
  894. for (src = 0; src < Data.Length; src++)
  895. {
  896. if (src < start || src > end)
  897. ret[dest++]=Data[src];
  898. }
  899. return new list(ret);
  900. }
  901. public list GetSublist(int start, int end)
  902. {
  903. object[] ret;
  904. // Take care of neg start or end's
  905. // NOTE that either index may still be negative after
  906. // adding the length, so we must take additional
  907. // measures to protect against this. Note also that
  908. // after normalisation the negative indices are no
  909. // longer relative to the end of the list.
  910. if (start < 0)
  911. {
  912. start = Data.Length + start;
  913. }
  914. if (end < 0)
  915. {
  916. end = Data.Length + end;
  917. }
  918. // The conventional case is start <= end
  919. // NOTE that the case of an empty list is
  920. // dealt with by the initial test. Start
  921. // less than end is taken to be the most
  922. // common case.
  923. if (start <= end)
  924. {
  925. // Start sublist beyond length
  926. // Also deals with start AND end still negative
  927. if (start >= Data.Length || end < 0)
  928. {
  929. return new list();
  930. }
  931. // Sublist extends beyond the end of the supplied list
  932. if (end >= Data.Length)
  933. {
  934. end = Data.Length - 1;
  935. }
  936. // Sublist still starts before the beginning of the list
  937. if (start < 0)
  938. {
  939. start = 0;
  940. }
  941. ret = new object[end - start + 1];
  942. Array.Copy(Data, start, ret, 0, end - start + 1);
  943. return new list(ret);
  944. }
  945. // Deal with the segmented case: 0->end + start->EOL
  946. else
  947. {
  948. list result;
  949. // If end is negative, then prefix list is empty
  950. if (end < 0)
  951. {
  952. result = new list();
  953. // If start is still negative, then the whole of
  954. // the existing list is returned. This case is
  955. // only admitted if end is also still negative.
  956. if (start < 0)
  957. {
  958. return this;
  959. }
  960. }
  961. else
  962. {
  963. result = GetSublist(0,end);
  964. }
  965. // If start is outside of list, then just return
  966. // the prefix, whatever it is.
  967. if (start >= Data.Length)
  968. {
  969. return result;
  970. }
  971. return result + GetSublist(start, Data.Length);
  972. }
  973. }
  974. private static int compare(object left, object right, int ascending)
  975. {
  976. if (!left.GetType().Equals(right.GetType()))
  977. {
  978. // unequal types are always "equal" for comparison purposes.
  979. // this way, the bubble sort will never swap them, and we'll
  980. // get that feathered effect we're looking for
  981. return 0;
  982. }
  983. int ret = 0;
  984. if (left is key)
  985. {
  986. key l = (key)left;
  987. key r = (key)right;
  988. ret = String.CompareOrdinal(l.value, r.value);
  989. }
  990. else if (left is LSLString)
  991. {
  992. LSLString l = (LSLString)left;
  993. LSLString r = (LSLString)right;
  994. ret = String.CompareOrdinal(l.m_string, r.m_string);
  995. }
  996. else if (left is LSLInteger)
  997. {
  998. LSLInteger l = (LSLInteger)left;
  999. LSLInteger r = (LSLInteger)right;
  1000. ret = Math.Sign(l.value - r.value);
  1001. }
  1002. else if (left is LSLFloat)
  1003. {
  1004. LSLFloat l = (LSLFloat)left;
  1005. LSLFloat r = (LSLFloat)right;
  1006. ret = Math.Sign(l.value - r.value);
  1007. }
  1008. else if (left is Vector3)
  1009. {
  1010. Vector3 l = (Vector3)left;
  1011. Vector3 r = (Vector3)right;
  1012. ret = Math.Sign(Vector3.Mag(l) - Vector3.Mag(r));
  1013. }
  1014. else if (left is Quaternion)
  1015. {
  1016. Quaternion l = (Quaternion)left;
  1017. Quaternion r = (Quaternion)right;
  1018. ret = Math.Sign(Quaternion.Mag(l) - Quaternion.Mag(r));
  1019. }
  1020. if (ascending == 0)
  1021. {
  1022. ret = 0 - ret;
  1023. }
  1024. return ret;
  1025. }
  1026. class HomogeneousComparer : IComparer
  1027. {
  1028. public HomogeneousComparer()
  1029. {
  1030. }
  1031. public int Compare(object lhs, object rhs)
  1032. {
  1033. return compare(lhs, rhs, 1);
  1034. }
  1035. }
  1036. public list Sort(int stride, int ascending)
  1037. {
  1038. if (Data.Length == 0)
  1039. return new list(); // Don't even bother
  1040. object[] ret = new object[Data.Length];
  1041. Array.Copy(Data, 0, ret, 0, Data.Length);
  1042. if (stride <= 0)
  1043. {
  1044. stride = 1;
  1045. }
  1046. if ((Data.Length % stride) != 0)
  1047. return new list(ret);
  1048. // we can optimize here in the case where stride == 1 and the list
  1049. // consists of homogeneous types
  1050. if (stride == 1)
  1051. {
  1052. bool homogeneous = true;
  1053. int index;
  1054. for (index = 1; index < Data.Length; index++)
  1055. {
  1056. if (!Data[0].GetType().Equals(Data[index].GetType()))
  1057. {
  1058. homogeneous = false;
  1059. break;
  1060. }
  1061. }
  1062. if (homogeneous)
  1063. {
  1064. Array.Sort(ret, new HomogeneousComparer());
  1065. if (ascending == 0)
  1066. {
  1067. Array.Reverse(ret);
  1068. }
  1069. return new list(ret);
  1070. }
  1071. }
  1072. // Because of the desired type specific feathered sorting behavior
  1073. // requried by the spec, we MUST use a non-optimized bubble sort here.
  1074. // Anything else will give you the incorrect behavior.
  1075. // begin bubble sort...
  1076. int i;
  1077. int j;
  1078. int k;
  1079. int n = Data.Length;
  1080. for (i = 0; i < (n-stride); i += stride)
  1081. {
  1082. for (j = i + stride; j < n; j += stride)
  1083. {
  1084. if (compare(ret[i], ret[j], ascending) > 0)
  1085. {
  1086. for (k = 0; k < stride; k++)
  1087. {
  1088. object tmp = ret[i + k];
  1089. ret[i + k] = ret[j + k];
  1090. ret[j + k] = tmp;
  1091. }
  1092. }
  1093. }
  1094. }
  1095. // end bubble sort
  1096. return new list(ret);
  1097. }
  1098. #region CSV Methods
  1099. public static list FromCSV(string csv)
  1100. {
  1101. return new list(csv.Split(','));
  1102. }
  1103. public string ToCSV()
  1104. {
  1105. if(m_data == null || m_data.Length == 0)
  1106. return String.Empty;
  1107. Object o = m_data[0];
  1108. int len = m_data.Length;
  1109. if(len == 1)
  1110. return o.ToString();
  1111. StringBuilder sb = new StringBuilder(1024);
  1112. sb.Append(o.ToString());
  1113. for(int i = 1 ; i < len; i++)
  1114. {
  1115. sb.Append(",");
  1116. sb.Append(o.ToString());
  1117. }
  1118. return sb.ToString();
  1119. }
  1120. private string ToSoup()
  1121. {
  1122. if(m_data == null || m_data.Length == 0)
  1123. return String.Empty;
  1124. StringBuilder sb = new StringBuilder(1024);
  1125. foreach (object o in m_data)
  1126. {
  1127. sb.Append(o.ToString());
  1128. }
  1129. return sb.ToString();
  1130. }
  1131. public static explicit operator String(list l)
  1132. {
  1133. return l.ToSoup();
  1134. }
  1135. public static explicit operator LSLString(list l)
  1136. {
  1137. return new LSLString(l.ToSoup());
  1138. }
  1139. public override string ToString()
  1140. {
  1141. return ToSoup();
  1142. }
  1143. #endregion
  1144. #region Statistic Methods
  1145. public double Min()
  1146. {
  1147. double minimum = double.PositiveInfinity;
  1148. double entry;
  1149. for (int i = 0; i < Data.Length; i++)
  1150. {
  1151. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  1152. {
  1153. if (entry < minimum) minimum = entry;
  1154. }
  1155. }
  1156. return minimum;
  1157. }
  1158. public double Max()
  1159. {
  1160. double maximum = double.NegativeInfinity;
  1161. double entry;
  1162. for (int i = 0; i < Data.Length; i++)
  1163. {
  1164. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  1165. {
  1166. if (entry > maximum) maximum = entry;
  1167. }
  1168. }
  1169. return maximum;
  1170. }
  1171. public double Range()
  1172. {
  1173. return (this.Max() / this.Min());
  1174. }
  1175. public int NumericLength()
  1176. {
  1177. int count = 0;
  1178. double entry;
  1179. for (int i = 0; i < Data.Length; i++)
  1180. {
  1181. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  1182. {
  1183. count++;
  1184. }
  1185. }
  1186. return count;
  1187. }
  1188. public static list ToDoubleList(list src)
  1189. {
  1190. list ret = new list();
  1191. double entry;
  1192. for (int i = 0; i < src.Data.Length; i++)
  1193. {
  1194. if (double.TryParse(src.Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  1195. {
  1196. ret.Add(entry);
  1197. }
  1198. }
  1199. return ret;
  1200. }
  1201. public double Sum()
  1202. {
  1203. double sum = 0;
  1204. double entry;
  1205. for (int i = 0; i < Data.Length; i++)
  1206. {
  1207. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  1208. {
  1209. sum = sum + entry;
  1210. }
  1211. }
  1212. return sum;
  1213. }
  1214. public double SumSqrs()
  1215. {
  1216. double sum = 0;
  1217. double entry;
  1218. for (int i = 0; i < Data.Length; i++)
  1219. {
  1220. if (double.TryParse(Data[i].ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out entry))
  1221. {
  1222. sum = sum + Math.Pow(entry, 2);
  1223. }
  1224. }
  1225. return sum;
  1226. }
  1227. public double Mean()
  1228. {
  1229. return (this.Sum() / this.NumericLength());
  1230. }
  1231. public void NumericSort()
  1232. {
  1233. IComparer Numeric = new NumericComparer();
  1234. Array.Sort(Data, Numeric);
  1235. }
  1236. public void AlphaSort()
  1237. {
  1238. IComparer Alpha = new AlphaCompare();
  1239. Array.Sort(Data, Alpha);
  1240. }
  1241. public double Median()
  1242. {
  1243. return Qi(0.5);
  1244. }
  1245. public double GeometricMean()
  1246. {
  1247. double ret = 1.0;
  1248. list nums = ToDoubleList(this);
  1249. for (int i = 0; i < nums.Data.Length; i++)
  1250. {
  1251. ret *= (double)nums.Data[i];
  1252. }
  1253. return Math.Exp(Math.Log(ret) / (double)nums.Data.Length);
  1254. }
  1255. public double HarmonicMean()
  1256. {
  1257. double ret = 0.0;
  1258. list nums = ToDoubleList(this);
  1259. for (int i = 0; i < nums.Data.Length; i++)
  1260. {
  1261. ret += 1.0 / (double)nums.Data[i];
  1262. }
  1263. return ((double)nums.Data.Length / ret);
  1264. }
  1265. public double Variance()
  1266. {
  1267. double s = 0;
  1268. list num = ToDoubleList(this);
  1269. for (int i = 0; i < num.Data.Length; i++)
  1270. {
  1271. s += Math.Pow((double)num.Data[i], 2);
  1272. }
  1273. return (s - num.Data.Length * Math.Pow(num.Mean(), 2)) / (num.Data.Length - 1);
  1274. }
  1275. public double StdDev()
  1276. {
  1277. return Math.Sqrt(this.Variance());
  1278. }
  1279. public double Qi(double i)
  1280. {
  1281. list j = this;
  1282. j.NumericSort();
  1283. if (Math.Ceiling(this.Length * i) == this.Length * i)
  1284. {
  1285. return (double)((double)j.Data[(int)(this.Length * i - 1)] + (double)j.Data[(int)(this.Length * i)]) / 2;
  1286. }
  1287. else
  1288. {
  1289. return (double)j.Data[((int)(Math.Ceiling(this.Length * i))) - 1];
  1290. }
  1291. }
  1292. #endregion
  1293. public string ToPrettyString()
  1294. {
  1295. if(m_data == null || m_data.Length == 0)
  1296. return "[]";
  1297. StringBuilder sb = new StringBuilder(1024);
  1298. int len = m_data.Length;
  1299. int last = len - 1;
  1300. object o;
  1301. sb.Append("[");
  1302. for(int i = 0; i < len; i++ )
  1303. {
  1304. o = m_data[i];
  1305. if (o is String)
  1306. {
  1307. sb.Append("\"");
  1308. sb.Append((String)o);
  1309. sb.Append("\"");
  1310. }
  1311. else
  1312. {
  1313. sb.Append(o.ToString());
  1314. }
  1315. if(i < last)
  1316. sb.Append(",");
  1317. }
  1318. sb.Append("]");
  1319. return sb.ToString();
  1320. }
  1321. public class AlphaCompare : IComparer
  1322. {
  1323. int IComparer.Compare(object x, object y)
  1324. {
  1325. return string.Compare(x.ToString(), y.ToString());
  1326. }
  1327. }
  1328. public class NumericComparer : IComparer
  1329. {
  1330. int IComparer.Compare(object x, object y)
  1331. {
  1332. double a;
  1333. double b;
  1334. if (!double.TryParse(x.ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out a))
  1335. {
  1336. a = 0.0;
  1337. }
  1338. if (!double.TryParse(y.ToString(), NumberStyles.Float, Culture.NumberFormatInfo, out b))
  1339. {
  1340. b = 0.0;
  1341. }
  1342. if (a < b)
  1343. {
  1344. return -1;
  1345. }
  1346. else if (a == b)
  1347. {
  1348. return 0;
  1349. }
  1350. else
  1351. {
  1352. return 1;
  1353. }
  1354. }
  1355. }
  1356. public override bool Equals(object o)
  1357. {
  1358. if (!(o is list))
  1359. return false;
  1360. return Data.Length == ((list)o).Data.Length;
  1361. }
  1362. public override int GetHashCode()
  1363. {
  1364. return Data.GetHashCode();
  1365. }
  1366. }
  1367. [Serializable]
  1368. public struct key
  1369. {
  1370. public string value;
  1371. #region Constructors
  1372. public key(string s)
  1373. {
  1374. value = s;
  1375. }
  1376. #endregion
  1377. #region Methods
  1378. static public bool Parse2Key(string s)
  1379. {
  1380. Regex isuuid = new Regex(@"^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$", RegexOptions.Compiled);
  1381. if (isuuid.IsMatch(s))
  1382. {
  1383. return true;
  1384. }
  1385. else
  1386. {
  1387. return false;
  1388. }
  1389. }
  1390. #endregion
  1391. #region Operators
  1392. static public implicit operator Boolean(key k)
  1393. {
  1394. if (k.value.Length == 0)
  1395. {
  1396. return false;
  1397. }
  1398. if (k.value == "00000000-0000-0000-0000-000000000000")
  1399. {
  1400. return false;
  1401. }
  1402. Regex isuuid = new Regex(@"^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$", RegexOptions.Compiled);
  1403. if (isuuid.IsMatch(k.value))
  1404. {
  1405. return true;
  1406. }
  1407. else
  1408. {
  1409. return false;
  1410. }
  1411. }
  1412. public static bool operator true(key k)
  1413. {
  1414. return (Boolean)k;
  1415. }
  1416. public static bool operator false(key k)
  1417. {
  1418. return !(Boolean)k;
  1419. }
  1420. static public implicit operator key(string s)
  1421. {
  1422. return new key(s);
  1423. }
  1424. static public implicit operator String(key k)
  1425. {
  1426. return k.value;
  1427. }
  1428. static public implicit operator LSLString(key k)
  1429. {
  1430. return k.value;
  1431. }
  1432. public static bool operator ==(key k1, key k2)
  1433. {
  1434. return k1.value == k2.value;
  1435. }
  1436. public static bool operator !=(key k1, key k2)
  1437. {
  1438. return k1.value != k2.value;
  1439. }
  1440. #endregion
  1441. #region Overriders
  1442. public override bool Equals(object o)
  1443. {
  1444. return o.ToString() == value;
  1445. }
  1446. public override int GetHashCode()
  1447. {
  1448. return value.GetHashCode();
  1449. }
  1450. public override string ToString()
  1451. {
  1452. return value;
  1453. }
  1454. #endregion
  1455. }
  1456. [Serializable]
  1457. public struct LSLString
  1458. {
  1459. public string m_string;
  1460. #region Constructors
  1461. public LSLString(string s)
  1462. {
  1463. m_string = s;
  1464. }
  1465. public LSLString(double d)
  1466. {
  1467. string s = String.Format(Culture.FormatProvider, "{0:0.000000}", d);
  1468. m_string = s;
  1469. }
  1470. public LSLString(LSLFloat f)
  1471. {
  1472. string s = String.Format(Culture.FormatProvider, "{0:0.000000}", f.value);
  1473. m_string = s;
  1474. }
  1475. public LSLString(int i)
  1476. {
  1477. string s = String.Format("{0}", i);
  1478. m_string = s;
  1479. }
  1480. public LSLString(LSLInteger i) : this(i.value) {}
  1481. #endregion
  1482. #region Operators
  1483. static public implicit operator Boolean(LSLString s)
  1484. {
  1485. if (s.m_string.Length == 0)
  1486. {
  1487. return false;
  1488. }
  1489. else
  1490. {
  1491. return true;
  1492. }
  1493. }
  1494. static public implicit operator String(LSLString s)
  1495. {
  1496. return s.m_string;
  1497. }
  1498. static public implicit operator LSLString(string s)
  1499. {
  1500. return new LSLString(s);
  1501. }
  1502. public static string ToString(LSLString s)
  1503. {
  1504. return s.m_string;
  1505. }
  1506. public override string ToString()
  1507. {
  1508. return m_string;
  1509. }
  1510. public static bool operator ==(LSLString s1, string s2)
  1511. {
  1512. return s1.m_string == s2;
  1513. }
  1514. public static bool operator !=(LSLString s1, string s2)
  1515. {
  1516. return s1.m_string != s2;
  1517. }
  1518. public static LSLString operator +(LSLString s1, LSLString s2)
  1519. {
  1520. return new LSLString(s1.m_string + s2.m_string);
  1521. }
  1522. public static explicit operator double(LSLString s)
  1523. {
  1524. return new LSLFloat(s).value;
  1525. }
  1526. public static explicit operator LSLInteger(LSLString s)
  1527. {
  1528. return new LSLInteger(s.m_string);
  1529. }
  1530. public static explicit operator LSLString(double d)
  1531. {
  1532. return new LSLString(d);
  1533. }
  1534. static public explicit operator LSLString(int i)
  1535. {
  1536. return new LSLString(i);
  1537. }
  1538. public static explicit operator LSLString(LSLFloat f)
  1539. {
  1540. return new LSLString(f);
  1541. }
  1542. static public explicit operator LSLString(bool b)
  1543. {
  1544. if (b)
  1545. return new LSLString("1");
  1546. else
  1547. return new LSLString("0");
  1548. }
  1549. public static implicit operator Vector3(LSLString s)
  1550. {
  1551. return new Vector3(s.m_string);
  1552. }
  1553. public static implicit operator Quaternion(LSLString s)
  1554. {
  1555. return new Quaternion(s.m_string);
  1556. }
  1557. public static implicit operator LSLFloat(LSLString s)
  1558. {
  1559. return new LSLFloat(s);
  1560. }
  1561. public static implicit operator list(LSLString s)
  1562. {
  1563. return new list(new object[]{s});
  1564. }
  1565. #endregion
  1566. #region Overriders
  1567. public override bool Equals(object o)
  1568. {
  1569. return m_string == o.ToString();
  1570. }
  1571. public override int GetHashCode()
  1572. {
  1573. return m_string.GetHashCode();
  1574. }
  1575. #endregion
  1576. #region " Standard string functions "
  1577. //Clone,CompareTo,Contains
  1578. //CopyTo,EndsWith,Equals,GetEnumerator,GetHashCode,GetType,GetTypeCode
  1579. //IndexOf,IndexOfAny,Insert,IsNormalized,LastIndexOf,LastIndexOfAny
  1580. //Length,Normalize,PadLeft,PadRight,Remove,Replace,Split,StartsWith,Substring,ToCharArray,ToLowerInvariant
  1581. //ToString,ToUpper,ToUpperInvariant,Trim,TrimEnd,TrimStart
  1582. public bool Contains(string value) { return m_string.Contains(value); }
  1583. public int IndexOf(string value) { return m_string.IndexOf(value); }
  1584. public int Length { get { return m_string.Length; } }
  1585. #endregion
  1586. }
  1587. [Serializable]
  1588. public struct LSLInteger
  1589. {
  1590. public int value;
  1591. private static readonly Regex castRegex = new Regex(@"(^[ ]*0[xX][0-9A-Fa-f][0-9A-Fa-f]*)|(^[ ]*(-?|\+?)[0-9][0-9]*)");
  1592. #region Constructors
  1593. public LSLInteger(int i)
  1594. {
  1595. value = i;
  1596. }
  1597. public LSLInteger(uint i)
  1598. {
  1599. value = (int)i;
  1600. }
  1601. public LSLInteger(double d)
  1602. {
  1603. value = (int)d;
  1604. }
  1605. public LSLInteger(string s)
  1606. {
  1607. Match m = castRegex.Match(s);
  1608. string v = m.Groups[0].Value;
  1609. // Leading plus sign is allowed, but ignored
  1610. v = v.Replace("+", "");
  1611. if (v == String.Empty)
  1612. {
  1613. value = 0;
  1614. }
  1615. else
  1616. {
  1617. try
  1618. {
  1619. if (v.Contains("x") || v.Contains("X"))
  1620. {
  1621. value = int.Parse(v.Substring(2), System.Globalization.NumberStyles.HexNumber);
  1622. }
  1623. else
  1624. {
  1625. value = int.Parse(v, System.Globalization.NumberStyles.Integer);
  1626. }
  1627. }
  1628. catch (OverflowException)
  1629. {
  1630. value = -1;
  1631. }
  1632. }
  1633. }
  1634. #endregion
  1635. #region Operators
  1636. static public implicit operator int(LSLInteger i)
  1637. {
  1638. return i.value;
  1639. }
  1640. static public explicit operator uint(LSLInteger i)
  1641. {
  1642. return (uint)i.value;
  1643. }
  1644. static public explicit operator LSLString(LSLInteger i)
  1645. {
  1646. return new LSLString(i.ToString());
  1647. }
  1648. public static implicit operator list(LSLInteger i)
  1649. {
  1650. return new list(new object[] { i });
  1651. }
  1652. static public implicit operator Boolean(LSLInteger i)
  1653. {
  1654. if (i.value == 0)
  1655. {
  1656. return false;
  1657. }
  1658. else
  1659. {
  1660. return true;
  1661. }
  1662. }
  1663. static public implicit operator LSLInteger(int i)
  1664. {
  1665. return new LSLInteger(i);
  1666. }
  1667. static public explicit operator LSLInteger(string s)
  1668. {
  1669. return new LSLInteger(s);
  1670. }
  1671. static public implicit operator LSLInteger(uint u)
  1672. {
  1673. return new LSLInteger(u);
  1674. }
  1675. static public explicit operator LSLInteger(double d)
  1676. {
  1677. return new LSLInteger(d);
  1678. }
  1679. static public explicit operator LSLInteger(LSLFloat f)
  1680. {
  1681. return new LSLInteger(f.value);
  1682. }
  1683. static public implicit operator LSLInteger(bool b)
  1684. {
  1685. if (b)
  1686. return new LSLInteger(1);
  1687. else
  1688. return new LSLInteger(0);
  1689. }
  1690. static public LSLInteger operator ==(LSLInteger i1, LSLInteger i2)
  1691. {
  1692. bool ret = i1.value == i2.value;
  1693. return new LSLInteger((ret ? 1 : 0));
  1694. }
  1695. static public LSLInteger operator !=(LSLInteger i1, LSLInteger i2)
  1696. {
  1697. bool ret = i1.value != i2.value;
  1698. return new LSLInteger((ret ? 1 : 0));
  1699. }
  1700. static public LSLInteger operator <(LSLInteger i1, LSLInteger i2)
  1701. {
  1702. bool ret = i1.value < i2.value;
  1703. return new LSLInteger((ret ? 1 : 0));
  1704. }
  1705. static public LSLInteger operator <=(LSLInteger i1, LSLInteger i2)
  1706. {
  1707. bool ret = i1.value <= i2.value;
  1708. return new LSLInteger((ret ? 1 : 0));
  1709. }
  1710. static public LSLInteger operator >(LSLInteger i1, LSLInteger i2)
  1711. {
  1712. bool ret = i1.value > i2.value;
  1713. return new LSLInteger((ret ? 1 : 0));
  1714. }
  1715. static public LSLInteger operator >=(LSLInteger i1, LSLInteger i2)
  1716. {
  1717. bool ret = i1.value >= i2.value;
  1718. return new LSLInteger((ret ? 1 : 0));
  1719. }
  1720. static public LSLInteger operator +(LSLInteger i1, int i2)
  1721. {
  1722. return new LSLInteger(i1.value + i2);
  1723. }
  1724. static public LSLInteger operator -(LSLInteger i1, int i2)
  1725. {
  1726. return new LSLInteger(i1.value - i2);
  1727. }
  1728. static public LSLInteger operator *(LSLInteger i1, int i2)
  1729. {
  1730. return new LSLInteger(i1.value * i2);
  1731. }
  1732. static public LSLInteger operator /(LSLInteger i1, int i2)
  1733. {
  1734. try
  1735. {
  1736. return new LSLInteger(i1.value / i2);
  1737. }
  1738. catch (DivideByZeroException)
  1739. {
  1740. throw new ScriptException("Integer division by Zero");
  1741. }
  1742. }
  1743. static public LSLInteger operator %(LSLInteger i1, int i2)
  1744. {
  1745. try
  1746. {
  1747. return new LSLInteger(i1.value % i2);
  1748. }
  1749. catch (DivideByZeroException)
  1750. {
  1751. throw new ScriptException("Integer division by Zero");
  1752. }
  1753. }
  1754. // static public LSLFloat operator +(LSLInteger i1, double f)
  1755. // {
  1756. // return new LSLFloat((double)i1.value + f);
  1757. // }
  1758. //
  1759. // static public LSLFloat operator -(LSLInteger i1, double f)
  1760. // {
  1761. // return new LSLFloat((double)i1.value - f);
  1762. // }
  1763. //
  1764. // static public LSLFloat operator *(LSLInteger i1, double f)
  1765. // {
  1766. // return new LSLFloat((double)i1.value * f);
  1767. // }
  1768. //
  1769. // static public LSLFloat operator /(LSLInteger i1, double f)
  1770. // {
  1771. // return new LSLFloat((double)i1.value / f);
  1772. // }
  1773. static public LSLInteger operator -(LSLInteger i)
  1774. {
  1775. return new LSLInteger(-i.value);
  1776. }
  1777. static public LSLInteger operator ~(LSLInteger i)
  1778. {
  1779. return new LSLInteger(~i.value);
  1780. }
  1781. public override bool Equals(Object o)
  1782. {
  1783. if (!(o is LSLInteger))
  1784. {
  1785. if (o is int)
  1786. {
  1787. return value == (int)o;
  1788. }
  1789. else
  1790. {
  1791. return false;
  1792. }
  1793. }
  1794. return value == ((LSLInteger)o).value;
  1795. }
  1796. public override int GetHashCode()
  1797. {
  1798. return value;
  1799. }
  1800. static public LSLInteger operator &(LSLInteger i1, LSLInteger i2)
  1801. {
  1802. int ret = i1.value & i2.value;
  1803. return ret;
  1804. }
  1805. static public LSLInteger operator /(LSLInteger i1, LSLInteger i2)
  1806. {
  1807. try
  1808. {
  1809. int ret = i1.value / i2.value;
  1810. return ret;
  1811. }
  1812. catch (DivideByZeroException)
  1813. {
  1814. throw new ScriptException("Integer division by Zero");
  1815. }
  1816. }
  1817. static public LSLInteger operator %(LSLInteger i1, LSLInteger i2)
  1818. {
  1819. try
  1820. {
  1821. int ret = i1.value % i2.value;
  1822. return ret;
  1823. }
  1824. catch (DivideByZeroException)
  1825. {
  1826. throw new ScriptException("Integer division by Zero");
  1827. }
  1828. }
  1829. static public LSLInteger operator |(LSLInteger i1, LSLInteger i2)
  1830. {
  1831. int ret = i1.value | i2.value;
  1832. return ret;
  1833. }
  1834. static public LSLInteger operator ^(LSLInteger i1, LSLInteger i2)
  1835. {
  1836. int ret = i1.value ^ i2.value;
  1837. return ret;
  1838. }
  1839. static public LSLInteger operator !(LSLInteger i1)
  1840. {
  1841. return i1.value == 0 ? 1 : 0;
  1842. }
  1843. public static LSLInteger operator ++(LSLInteger i)
  1844. {
  1845. i.value++;
  1846. return i;
  1847. }
  1848. public static LSLInteger operator --(LSLInteger i)
  1849. {
  1850. i.value--;
  1851. return i;
  1852. }
  1853. public static LSLInteger operator << (LSLInteger i, int s)
  1854. {
  1855. return i.value << s;
  1856. }
  1857. public static LSLInteger operator >> (LSLInteger i, int s)
  1858. {
  1859. return i.value >> s;
  1860. }
  1861. static public implicit operator System.Double(LSLInteger i)
  1862. {
  1863. return (double)i.value;
  1864. }
  1865. public static bool operator true(LSLInteger i)
  1866. {
  1867. return i.value != 0;
  1868. }
  1869. public static bool operator false(LSLInteger i)
  1870. {
  1871. return i.value == 0;
  1872. }
  1873. #endregion
  1874. #region Overriders
  1875. public override string ToString()
  1876. {
  1877. return this.value.ToString();
  1878. }
  1879. #endregion
  1880. }
  1881. [Serializable]
  1882. public struct LSLFloat
  1883. {
  1884. public double value;
  1885. #region Constructors
  1886. public LSLFloat(int i)
  1887. {
  1888. this.value = (double)i;
  1889. }
  1890. public LSLFloat(double d)
  1891. {
  1892. this.value = d;
  1893. }
  1894. public LSLFloat(string s)
  1895. {
  1896. Regex r = new Regex("^ *(\\+|-)?([0-9]+\\.?[0-9]*|\\.[0-9]+)([eE](\\+|-)?[0-9]+)?");
  1897. Match m = r.Match(s);
  1898. string v = m.Groups[0].Value;
  1899. v = v.Trim();
  1900. if (v == String.Empty || v == null)
  1901. v = "0.0";
  1902. else
  1903. if (!v.Contains(".") && !v.ToLower().Contains("e"))
  1904. v = v + ".0";
  1905. else
  1906. if (v.EndsWith("."))
  1907. v = v + "0";
  1908. this.value = double.Parse(v, System.Globalization.NumberStyles.Float, Culture.NumberFormatInfo);
  1909. }
  1910. #endregion
  1911. #region Operators
  1912. static public explicit operator float(LSLFloat f)
  1913. {
  1914. return (float)f.value;
  1915. }
  1916. static public explicit operator int(LSLFloat f)
  1917. {
  1918. return (int)f.value;
  1919. }
  1920. static public explicit operator uint(LSLFloat f)
  1921. {
  1922. return (uint) Math.Abs(f.value);
  1923. }
  1924. static public implicit operator Boolean(LSLFloat f)
  1925. {
  1926. if (f.value == 0.0)
  1927. {
  1928. return false;
  1929. }
  1930. else
  1931. {
  1932. return true;
  1933. }
  1934. }
  1935. static public implicit operator LSLFloat(int i)
  1936. {
  1937. return new LSLFloat(i);
  1938. }
  1939. static public implicit operator LSLFloat(LSLInteger i)
  1940. {
  1941. return new LSLFloat(i.value);
  1942. }
  1943. static public explicit operator LSLFloat(string s)
  1944. {
  1945. return new LSLFloat(s);
  1946. }
  1947. public static implicit operator list(LSLFloat f)
  1948. {
  1949. return new list(new object[] { f });
  1950. }
  1951. static public implicit operator LSLFloat(double d)
  1952. {
  1953. return new LSLFloat(d);
  1954. }
  1955. static public implicit operator LSLFloat(bool b)
  1956. {
  1957. if (b)
  1958. return new LSLFloat(1.0);
  1959. else
  1960. return new LSLFloat(0.0);
  1961. }
  1962. static public bool operator ==(LSLFloat f1, LSLFloat f2)
  1963. {
  1964. return f1.value == f2.value;
  1965. }
  1966. static public bool operator !=(LSLFloat f1, LSLFloat f2)
  1967. {
  1968. return f1.value != f2.value;
  1969. }
  1970. static public LSLFloat operator ++(LSLFloat f)
  1971. {
  1972. f.value++;
  1973. return f;
  1974. }
  1975. static public LSLFloat operator --(LSLFloat f)
  1976. {
  1977. f.value--;
  1978. return f;
  1979. }
  1980. static public LSLFloat operator +(LSLFloat f, int i)
  1981. {
  1982. return new LSLFloat(f.value + (double)i);
  1983. }
  1984. static public LSLFloat operator -(LSLFloat f, int i)
  1985. {
  1986. return new LSLFloat(f.value - (double)i);
  1987. }
  1988. static public LSLFloat operator *(LSLFloat f, int i)
  1989. {
  1990. return new LSLFloat(f.value * (double)i);
  1991. }
  1992. static public LSLFloat operator /(LSLFloat f, int i)
  1993. {
  1994. double r = f.value / (double)i;
  1995. if (IsBadNumber(r))
  1996. throw new ScriptException("Float division by zero");
  1997. return new LSLFloat(r);
  1998. }
  1999. static public LSLFloat operator %(LSLFloat f, int i)
  2000. {
  2001. double r = f.value % (double)i;
  2002. if (IsBadNumber(r))
  2003. throw new ScriptException("Float division by zero");
  2004. return new LSLFloat(r);
  2005. }
  2006. static public LSLFloat operator +(LSLFloat lhs, LSLFloat rhs)
  2007. {
  2008. return new LSLFloat(lhs.value + rhs.value);
  2009. }
  2010. static public LSLFloat operator -(LSLFloat lhs, LSLFloat rhs)
  2011. {
  2012. return new LSLFloat(lhs.value - rhs.value);
  2013. }
  2014. static public LSLFloat operator *(LSLFloat lhs, LSLFloat rhs)
  2015. {
  2016. return new LSLFloat(lhs.value * rhs.value);
  2017. }
  2018. static public LSLFloat operator /(LSLFloat lhs, LSLFloat rhs)
  2019. {
  2020. double r = lhs.value / rhs.value;
  2021. if (IsBadNumber(r))
  2022. throw new ScriptException("Float division by zero");
  2023. return new LSLFloat(r);
  2024. }
  2025. static public LSLFloat operator %(LSLFloat lhs, LSLFloat rhs)
  2026. {
  2027. double r = lhs.value % rhs.value;
  2028. if (IsBadNumber(r))
  2029. throw new ScriptException("Float division by zero");
  2030. return new LSLFloat(r);
  2031. }
  2032. static public LSLFloat operator -(LSLFloat f)
  2033. {
  2034. return new LSLFloat(-f.value);
  2035. }
  2036. static public implicit operator System.Double(LSLFloat f)
  2037. {
  2038. return f.value;
  2039. }
  2040. #endregion
  2041. #region Overriders
  2042. public override string ToString()
  2043. {
  2044. return String.Format(Culture.FormatProvider, "{0:0.000000}", this.value);
  2045. }
  2046. public override bool Equals(Object o)
  2047. {
  2048. if (!(o is LSLFloat))
  2049. return false;
  2050. return value == ((LSLFloat)o).value;
  2051. }
  2052. public override int GetHashCode()
  2053. {
  2054. return value.GetHashCode();
  2055. }
  2056. #endregion
  2057. }
  2058. }
  2059. }