LSL_Types.cs 74 KB

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