1
0

SOPVehicle.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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.Generic;
  29. using OpenMetaverse;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.PhysicsModules.SharedBase;
  32. using System.Text;
  33. using System.IO;
  34. using System.Xml;
  35. using OpenSim.Framework.Serialization;
  36. using OpenSim.Framework.Serialization.External;
  37. using OpenSim.Region.Framework.Scenes.Serialization;
  38. namespace OpenSim.Region.Framework.Scenes
  39. {
  40. public class SOPVehicle
  41. {
  42. public VehicleData vd;
  43. public Vehicle Type
  44. {
  45. get { return vd.m_type; }
  46. }
  47. public SOPVehicle()
  48. {
  49. vd = new VehicleData();
  50. ProcessTypeChange(Vehicle.TYPE_NONE); // is needed?
  51. }
  52. public void ProcessFloatVehicleParam(Vehicle pParam, float pValue)
  53. {
  54. float len;
  55. float timestep = 0.01f;
  56. switch (pParam)
  57. {
  58. case Vehicle.ANGULAR_DEFLECTION_EFFICIENCY:
  59. if (pValue < 0f) pValue = 0f;
  60. if (pValue > 1f) pValue = 1f;
  61. vd.m_angularDeflectionEfficiency = pValue;
  62. break;
  63. case Vehicle.ANGULAR_DEFLECTION_TIMESCALE:
  64. if (pValue < timestep) pValue = timestep;
  65. vd.m_angularDeflectionTimescale = pValue;
  66. break;
  67. case Vehicle.ANGULAR_MOTOR_DECAY_TIMESCALE:
  68. if (pValue < timestep) pValue = timestep;
  69. else if (pValue > 120) pValue = 120;
  70. vd.m_angularMotorDecayTimescale = pValue;
  71. break;
  72. case Vehicle.ANGULAR_MOTOR_TIMESCALE:
  73. if (pValue < timestep) pValue = timestep;
  74. vd.m_angularMotorTimescale = pValue;
  75. break;
  76. case Vehicle.BANKING_EFFICIENCY:
  77. if (pValue < -1f) pValue = -1f;
  78. if (pValue > 1f) pValue = 1f;
  79. vd.m_bankingEfficiency = pValue;
  80. break;
  81. case Vehicle.BANKING_MIX:
  82. if (pValue < 0f) pValue = 0f;
  83. if (pValue > 1f) pValue = 1f;
  84. vd.m_bankingMix = pValue;
  85. break;
  86. case Vehicle.BANKING_TIMESCALE:
  87. if (pValue < timestep) pValue = timestep;
  88. vd.m_bankingTimescale = pValue;
  89. break;
  90. case Vehicle.BUOYANCY:
  91. if (pValue < -1f) pValue = -1f;
  92. if (pValue > 1f) pValue = 1f;
  93. vd.m_VehicleBuoyancy = pValue;
  94. break;
  95. case Vehicle.HOVER_EFFICIENCY:
  96. if (pValue < 0f) pValue = 0f;
  97. if (pValue > 1f) pValue = 1f;
  98. vd.m_VhoverEfficiency = pValue;
  99. break;
  100. case Vehicle.HOVER_HEIGHT:
  101. vd.m_VhoverHeight = pValue;
  102. break;
  103. case Vehicle.HOVER_TIMESCALE:
  104. if (pValue < timestep) pValue = timestep;
  105. vd.m_VhoverTimescale = pValue;
  106. break;
  107. case Vehicle.LINEAR_DEFLECTION_EFFICIENCY:
  108. if (pValue < 0f) pValue = 0f;
  109. if (pValue > 1f) pValue = 1f;
  110. vd.m_linearDeflectionEfficiency = pValue;
  111. break;
  112. case Vehicle.LINEAR_DEFLECTION_TIMESCALE:
  113. if (pValue < timestep) pValue = timestep;
  114. vd.m_linearDeflectionTimescale = pValue;
  115. break;
  116. case Vehicle.LINEAR_MOTOR_DECAY_TIMESCALE:
  117. if (pValue < timestep) pValue = timestep;
  118. else if (pValue > 120) pValue = 120;
  119. vd.m_linearMotorDecayTimescale = pValue;
  120. break;
  121. case Vehicle.LINEAR_MOTOR_TIMESCALE:
  122. if (pValue < timestep) pValue = timestep;
  123. vd.m_linearMotorTimescale = pValue;
  124. break;
  125. case Vehicle.VERTICAL_ATTRACTION_EFFICIENCY:
  126. if (pValue < 0f) pValue = 0f;
  127. if (pValue > 1f) pValue = 1f;
  128. vd.m_verticalAttractionEfficiency = pValue;
  129. break;
  130. case Vehicle.VERTICAL_ATTRACTION_TIMESCALE:
  131. if (pValue < timestep) pValue = timestep;
  132. vd.m_verticalAttractionTimescale = pValue;
  133. break;
  134. // These are vector properties but the engine lets you use a single float value to
  135. // set all of the components to the same value
  136. case Vehicle.ANGULAR_FRICTION_TIMESCALE:
  137. if (pValue < timestep) pValue = timestep;
  138. vd.m_angularFrictionTimescale = new Vector3(pValue, pValue, pValue);
  139. break;
  140. case Vehicle.ANGULAR_MOTOR_DIRECTION:
  141. vd.m_angularMotorDirection = new Vector3(pValue, pValue, pValue);
  142. len = vd.m_angularMotorDirection.Length();
  143. if (len > 12.566f)
  144. vd.m_angularMotorDirection *= (12.566f / len);
  145. break;
  146. case Vehicle.LINEAR_FRICTION_TIMESCALE:
  147. if (pValue < timestep) pValue = timestep;
  148. vd.m_linearFrictionTimescale = new Vector3(pValue, pValue, pValue);
  149. break;
  150. case Vehicle.LINEAR_MOTOR_DIRECTION:
  151. vd.m_linearMotorDirection = new Vector3(pValue, pValue, pValue);
  152. len = vd.m_linearMotorDirection.Length();
  153. if (len > 30.0f)
  154. vd.m_linearMotorDirection *= (30.0f / len);
  155. break;
  156. case Vehicle.LINEAR_MOTOR_OFFSET:
  157. vd.m_linearMotorOffset = new Vector3(pValue, pValue, pValue);
  158. len = vd.m_linearMotorOffset.Length();
  159. if (len > 100.0f)
  160. vd.m_linearMotorOffset *= (100.0f / len);
  161. break;
  162. }
  163. }//end ProcessFloatVehicleParam
  164. public void ProcessVectorVehicleParam(Vehicle pParam, Vector3 pValue)
  165. {
  166. float len;
  167. float timestep = 0.01f;
  168. switch (pParam)
  169. {
  170. case Vehicle.ANGULAR_FRICTION_TIMESCALE:
  171. if (pValue.X < timestep) pValue.X = timestep;
  172. if (pValue.Y < timestep) pValue.Y = timestep;
  173. if (pValue.Z < timestep) pValue.Z = timestep;
  174. vd.m_angularFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
  175. break;
  176. case Vehicle.ANGULAR_MOTOR_DIRECTION:
  177. vd.m_angularMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
  178. // Limit requested angular speed to 2 rps= 4 pi rads/sec
  179. len = vd.m_angularMotorDirection.Length();
  180. if (len > 12.566f)
  181. vd.m_angularMotorDirection *= (12.566f / len);
  182. break;
  183. case Vehicle.LINEAR_FRICTION_TIMESCALE:
  184. if (pValue.X < timestep) pValue.X = timestep;
  185. if (pValue.Y < timestep) pValue.Y = timestep;
  186. if (pValue.Z < timestep) pValue.Z = timestep;
  187. vd.m_linearFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
  188. break;
  189. case Vehicle.LINEAR_MOTOR_DIRECTION:
  190. vd.m_linearMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
  191. len = vd.m_linearMotorDirection.Length();
  192. if (len > 30.0f)
  193. vd.m_linearMotorDirection *= (30.0f / len);
  194. break;
  195. case Vehicle.LINEAR_MOTOR_OFFSET:
  196. vd.m_linearMotorOffset = new Vector3(pValue.X, pValue.Y, pValue.Z);
  197. len = vd.m_linearMotorOffset.Length();
  198. if (len > 100.0f)
  199. vd.m_linearMotorOffset *= (100.0f / len);
  200. break;
  201. }
  202. }//end ProcessVectorVehicleParam
  203. public void ProcessRotationVehicleParam(Vehicle pParam, Quaternion pValue)
  204. {
  205. switch (pParam)
  206. {
  207. case Vehicle.REFERENCE_FRAME:
  208. vd.m_referenceFrame = pValue;
  209. break;
  210. }
  211. }//end ProcessRotationVehicleParam
  212. public void ProcessVehicleFlags(int pParam, bool remove)
  213. {
  214. if (remove)
  215. {
  216. vd.m_flags &= ~((VehicleFlag)pParam);
  217. }
  218. else
  219. {
  220. vd.m_flags |= (VehicleFlag)pParam;
  221. }
  222. }//end ProcessVehicleFlags
  223. public void ProcessTypeChange(Vehicle pType)
  224. {
  225. vd.m_linearMotorDirection = Vector3.Zero;
  226. vd.m_angularMotorDirection = Vector3.Zero;
  227. vd.m_linearMotorOffset = Vector3.Zero;
  228. vd.m_referenceFrame = Quaternion.Identity;
  229. // Set Defaults For Type
  230. vd.m_type = pType;
  231. switch (pType)
  232. {
  233. case Vehicle.TYPE_NONE:
  234. vd.m_linearFrictionTimescale = new Vector3(1000, 1000, 1000);
  235. vd.m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
  236. vd.m_linearMotorTimescale = 1000;
  237. vd.m_linearMotorDecayTimescale = 120;
  238. vd.m_angularMotorTimescale = 1000;
  239. vd.m_angularMotorDecayTimescale = 1000;
  240. vd.m_VhoverHeight = 0;
  241. vd.m_VhoverEfficiency = 1;
  242. vd.m_VhoverTimescale = 1000;
  243. vd.m_VehicleBuoyancy = 0;
  244. vd.m_linearDeflectionEfficiency = 0;
  245. vd.m_linearDeflectionTimescale = 1000;
  246. vd.m_angularDeflectionEfficiency = 0;
  247. vd.m_angularDeflectionTimescale = 1000;
  248. vd.m_bankingEfficiency = 0;
  249. vd.m_bankingMix = 1;
  250. vd.m_bankingTimescale = 1000;
  251. vd.m_verticalAttractionEfficiency = 0;
  252. vd.m_verticalAttractionTimescale = 1000;
  253. vd.m_flags = (VehicleFlag)0;
  254. break;
  255. case Vehicle.TYPE_SLED:
  256. vd.m_linearFrictionTimescale = new Vector3(30, 1, 1000);
  257. vd.m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
  258. vd.m_linearMotorTimescale = 1000;
  259. vd.m_linearMotorDecayTimescale = 120;
  260. vd.m_angularMotorTimescale = 1000;
  261. vd.m_angularMotorDecayTimescale = 120;
  262. vd.m_VhoverHeight = 0;
  263. vd.m_VhoverEfficiency = 1;
  264. vd.m_VhoverTimescale = 10;
  265. vd.m_VehicleBuoyancy = 0;
  266. vd.m_linearDeflectionEfficiency = 1;
  267. vd.m_linearDeflectionTimescale = 1;
  268. vd.m_angularDeflectionEfficiency = 0;
  269. vd.m_angularDeflectionTimescale = 1000;
  270. vd.m_bankingEfficiency = 0;
  271. vd.m_bankingMix = 1;
  272. vd.m_bankingTimescale = 10;
  273. vd.m_flags &=
  274. ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY |
  275. VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY);
  276. vd.m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.LIMIT_MOTOR_UP);
  277. break;
  278. case Vehicle.TYPE_CAR:
  279. vd.m_linearFrictionTimescale = new Vector3(100, 2, 1000);
  280. vd.m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
  281. vd.m_linearMotorTimescale = 1;
  282. vd.m_linearMotorDecayTimescale = 60;
  283. vd.m_angularMotorTimescale = 1;
  284. vd.m_angularMotorDecayTimescale = 0.8f;
  285. vd.m_VhoverHeight = 0;
  286. vd.m_VhoverEfficiency = 0;
  287. vd.m_VhoverTimescale = 1000;
  288. vd.m_VehicleBuoyancy = 0;
  289. vd.m_linearDeflectionEfficiency = 1;
  290. vd.m_linearDeflectionTimescale = 2;
  291. vd.m_angularDeflectionEfficiency = 0;
  292. vd.m_angularDeflectionTimescale = 10;
  293. vd.m_verticalAttractionEfficiency = 1f;
  294. vd.m_verticalAttractionTimescale = 10f;
  295. vd.m_bankingEfficiency = -0.2f;
  296. vd.m_bankingMix = 1;
  297. vd.m_bankingTimescale = 1;
  298. vd.m_flags &= ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT);
  299. vd.m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY |
  300. VehicleFlag.LIMIT_MOTOR_UP | VehicleFlag.HOVER_UP_ONLY);
  301. break;
  302. case Vehicle.TYPE_BOAT:
  303. vd.m_linearFrictionTimescale = new Vector3(10, 3, 2);
  304. vd.m_angularFrictionTimescale = new Vector3(10, 10, 10);
  305. vd.m_linearMotorTimescale = 5;
  306. vd.m_linearMotorDecayTimescale = 60;
  307. vd.m_angularMotorTimescale = 4;
  308. vd.m_angularMotorDecayTimescale = 4;
  309. vd.m_VhoverHeight = 0;
  310. vd.m_VhoverEfficiency = 0.5f;
  311. vd.m_VhoverTimescale = 2;
  312. vd.m_VehicleBuoyancy = 1;
  313. vd.m_linearDeflectionEfficiency = 0.5f;
  314. vd.m_linearDeflectionTimescale = 3;
  315. vd.m_angularDeflectionEfficiency = 0.5f;
  316. vd.m_angularDeflectionTimescale = 5;
  317. vd.m_verticalAttractionEfficiency = 0.5f;
  318. vd.m_verticalAttractionTimescale = 5f;
  319. vd.m_bankingEfficiency = -0.3f;
  320. vd.m_bankingMix = 0.8f;
  321. vd.m_bankingTimescale = 1;
  322. vd.m_flags &= ~(VehicleFlag.HOVER_TERRAIN_ONLY |
  323. VehicleFlag.HOVER_GLOBAL_HEIGHT |
  324. VehicleFlag.HOVER_UP_ONLY |
  325. VehicleFlag.LIMIT_ROLL_ONLY);
  326. vd.m_flags |= (VehicleFlag.NO_DEFLECTION_UP |
  327. VehicleFlag.LIMIT_MOTOR_UP |
  328. VehicleFlag.HOVER_WATER_ONLY);
  329. break;
  330. case Vehicle.TYPE_AIRPLANE:
  331. vd.m_linearFrictionTimescale = new Vector3(200, 10, 5);
  332. vd.m_angularFrictionTimescale = new Vector3(20, 20, 20);
  333. vd.m_linearMotorTimescale = 2;
  334. vd.m_linearMotorDecayTimescale = 60;
  335. vd.m_angularMotorTimescale = 4;
  336. vd.m_angularMotorDecayTimescale = 8;
  337. vd.m_VhoverHeight = 0;
  338. vd.m_VhoverEfficiency = 0.5f;
  339. vd.m_VhoverTimescale = 1000;
  340. vd.m_VehicleBuoyancy = 0;
  341. vd.m_linearDeflectionEfficiency = 0.5f;
  342. vd.m_linearDeflectionTimescale = 0.5f;
  343. vd.m_angularDeflectionEfficiency = 1;
  344. vd.m_angularDeflectionTimescale = 2;
  345. vd.m_verticalAttractionEfficiency = 0.9f;
  346. vd.m_verticalAttractionTimescale = 2f;
  347. vd.m_bankingEfficiency = 1;
  348. vd.m_bankingMix = 0.7f;
  349. vd.m_bankingTimescale = 2;
  350. vd.m_flags &= ~(VehicleFlag.HOVER_WATER_ONLY |
  351. VehicleFlag.HOVER_TERRAIN_ONLY |
  352. VehicleFlag.HOVER_GLOBAL_HEIGHT |
  353. VehicleFlag.HOVER_UP_ONLY |
  354. VehicleFlag.NO_DEFLECTION_UP |
  355. VehicleFlag.LIMIT_MOTOR_UP);
  356. vd.m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY);
  357. break;
  358. case Vehicle.TYPE_BALLOON:
  359. vd.m_linearFrictionTimescale = new Vector3(5, 5, 5);
  360. vd.m_angularFrictionTimescale = new Vector3(10, 10, 10);
  361. vd.m_linearMotorTimescale = 5;
  362. vd.m_linearMotorDecayTimescale = 60;
  363. vd.m_angularMotorTimescale = 6;
  364. vd.m_angularMotorDecayTimescale = 10;
  365. vd.m_VhoverHeight = 5;
  366. vd.m_VhoverEfficiency = 0.8f;
  367. vd.m_VhoverTimescale = 10;
  368. vd.m_VehicleBuoyancy = 1;
  369. vd.m_linearDeflectionEfficiency = 0;
  370. vd.m_linearDeflectionTimescale = 5;
  371. vd.m_angularDeflectionEfficiency = 0;
  372. vd.m_angularDeflectionTimescale = 5;
  373. vd.m_verticalAttractionEfficiency = 0f;
  374. vd.m_verticalAttractionTimescale = 1000f;
  375. vd.m_bankingEfficiency = 0;
  376. vd.m_bankingMix = 0.7f;
  377. vd.m_bankingTimescale = 5;
  378. vd.m_flags &= ~(VehicleFlag.HOVER_WATER_ONLY |
  379. VehicleFlag.HOVER_TERRAIN_ONLY |
  380. VehicleFlag.HOVER_UP_ONLY |
  381. VehicleFlag.NO_DEFLECTION_UP |
  382. VehicleFlag.LIMIT_MOTOR_UP);
  383. vd.m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY |
  384. VehicleFlag.HOVER_GLOBAL_HEIGHT);
  385. break;
  386. }
  387. }
  388. public void SetVehicle(PhysicsActor ph)
  389. {
  390. if (ph == null)
  391. return;
  392. ph.SetVehicle(vd);
  393. }
  394. public bool CameraDecoupled
  395. {
  396. get
  397. {
  398. if((vd.m_flags & VehicleFlag.CAMERA_DECOUPLED) != 0)
  399. return true;
  400. return false;
  401. }
  402. }
  403. private XmlTextWriter writer;
  404. private void XWint(string name, int i)
  405. {
  406. writer.WriteElementString(name, i.ToString());
  407. }
  408. private void XWfloat(string name, float f)
  409. {
  410. writer.WriteElementString(name, f.ToString(Culture.FormatProvider));
  411. }
  412. private void XWVector(string name, Vector3 vec)
  413. {
  414. writer.WriteStartElement(name);
  415. writer.WriteElementString("X", vec.X.ToString(Culture.FormatProvider));
  416. writer.WriteElementString("Y", vec.Y.ToString(Culture.FormatProvider));
  417. writer.WriteElementString("Z", vec.Z.ToString(Culture.FormatProvider));
  418. writer.WriteEndElement();
  419. }
  420. private void XWQuat(string name, Quaternion quat)
  421. {
  422. writer.WriteStartElement(name);
  423. writer.WriteElementString("X", quat.X.ToString(Culture.FormatProvider));
  424. writer.WriteElementString("Y", quat.Y.ToString(Culture.FormatProvider));
  425. writer.WriteElementString("Z", quat.Z.ToString(Culture.FormatProvider));
  426. writer.WriteElementString("W", quat.W.ToString(Culture.FormatProvider));
  427. writer.WriteEndElement();
  428. }
  429. public void ToXml2(XmlTextWriter twriter)
  430. {
  431. writer = twriter;
  432. writer.WriteStartElement("Vehicle");
  433. XWint("TYPE", (int)vd.m_type);
  434. XWint("FLAGS", (int)vd.m_flags);
  435. // Linear properties
  436. XWVector("LMDIR", vd.m_linearMotorDirection);
  437. XWVector("LMFTIME", vd.m_linearFrictionTimescale);
  438. XWfloat("LMDTIME", vd.m_linearMotorDecayTimescale);
  439. XWfloat("LMTIME", vd.m_linearMotorTimescale);
  440. XWVector("LMOFF", vd.m_linearMotorOffset);
  441. //Angular properties
  442. XWVector("AMDIR", vd.m_angularMotorDirection);
  443. XWfloat("AMTIME", vd.m_angularMotorTimescale);
  444. XWfloat("AMDTIME", vd.m_angularMotorDecayTimescale);
  445. XWVector("AMFTIME", vd.m_angularFrictionTimescale);
  446. //Deflection properties
  447. XWfloat("ADEFF", vd.m_angularDeflectionEfficiency);
  448. XWfloat("ADTIME", vd.m_angularDeflectionTimescale);
  449. XWfloat("LDEFF", vd.m_linearDeflectionEfficiency);
  450. XWfloat("LDTIME", vd.m_linearDeflectionTimescale);
  451. //Banking properties
  452. XWfloat("BEFF", vd.m_bankingEfficiency);
  453. XWfloat("BMIX", vd.m_bankingMix);
  454. XWfloat("BTIME", vd.m_bankingTimescale);
  455. //Hover and Buoyancy properties
  456. XWfloat("HHEI", vd.m_VhoverHeight);
  457. XWfloat("HEFF", vd.m_VhoverEfficiency);
  458. XWfloat("HTIME", vd.m_VhoverTimescale);
  459. XWfloat("VBUO", vd.m_VehicleBuoyancy);
  460. //Attractor properties
  461. XWfloat("VAEFF", vd.m_verticalAttractionEfficiency);
  462. XWfloat("VATIME", vd.m_verticalAttractionTimescale);
  463. XWQuat("REF_FRAME", vd.m_referenceFrame);
  464. writer.WriteEndElement();
  465. writer = null;
  466. }
  467. XmlReader reader;
  468. private int XRint()
  469. {
  470. return reader.ReadElementContentAsInt();
  471. }
  472. private float XRfloat()
  473. {
  474. return reader.ReadElementContentAsFloat();
  475. }
  476. public Vector3 XRvector()
  477. {
  478. Vector3 vec;
  479. reader.ReadStartElement();
  480. vec.X = reader.ReadElementContentAsFloat();
  481. vec.Y = reader.ReadElementContentAsFloat();
  482. vec.Z = reader.ReadElementContentAsFloat();
  483. reader.ReadEndElement();
  484. return vec;
  485. }
  486. public Quaternion XRquat()
  487. {
  488. Quaternion q;
  489. reader.ReadStartElement();
  490. q.X = reader.ReadElementContentAsFloat();
  491. q.Y = reader.ReadElementContentAsFloat();
  492. q.Z = reader.ReadElementContentAsFloat();
  493. q.W = reader.ReadElementContentAsFloat();
  494. reader.ReadEndElement();
  495. return q;
  496. }
  497. public static bool EReadProcessors(
  498. Dictionary<string, Action> processors,
  499. XmlReader xtr)
  500. {
  501. bool errors = false;
  502. string nodeName = string.Empty;
  503. while (xtr.NodeType != XmlNodeType.EndElement)
  504. {
  505. nodeName = xtr.Name;
  506. // m_log.DebugFormat("[ExternalRepresentationUtils]: Processing: {0}", nodeName);
  507. Action p = null;
  508. if (processors.TryGetValue(xtr.Name, out p))
  509. {
  510. // m_log.DebugFormat("[ExternalRepresentationUtils]: Found {0} processor, nodeName);
  511. try
  512. {
  513. p();
  514. }
  515. catch
  516. {
  517. errors = true;
  518. if (xtr.NodeType == XmlNodeType.EndElement)
  519. xtr.Read();
  520. }
  521. }
  522. else
  523. {
  524. // m_log.DebugFormat("[LandDataSerializer]: caught unknown element {0}", nodeName);
  525. xtr.ReadOuterXml(); // ignore
  526. }
  527. }
  528. return errors;
  529. }
  530. public string ToXml2()
  531. {
  532. using (StringWriter sw = new StringWriter())
  533. {
  534. using (XmlTextWriter xwriter = new XmlTextWriter(sw))
  535. {
  536. ToXml2(xwriter);
  537. }
  538. return sw.ToString();
  539. }
  540. }
  541. public static SOPVehicle FromXml2(string text)
  542. {
  543. if (text.Length == 0)
  544. return null;
  545. UTF8Encoding enc = new UTF8Encoding();
  546. MemoryStream ms = new MemoryStream(enc.GetBytes(text));
  547. XmlTextReader xreader = new XmlTextReader(ms);
  548. xreader.DtdProcessing = DtdProcessing.Ignore;
  549. SOPVehicle v = new SOPVehicle();
  550. bool error;
  551. v.FromXml2(xreader, out error);
  552. xreader.Close();
  553. if (error)
  554. {
  555. v = null;
  556. return null;
  557. }
  558. return v;
  559. }
  560. public static SOPVehicle FromXml2(XmlReader reader)
  561. {
  562. SOPVehicle vehicle = new SOPVehicle();
  563. bool errors = false;
  564. vehicle.FromXml2(reader, out errors);
  565. if (errors)
  566. return null;
  567. return vehicle;
  568. }
  569. private void FromXml2(XmlReader _reader, out bool errors)
  570. {
  571. errors = false;
  572. reader = _reader;
  573. Dictionary<string, Action> m_VehicleXmlProcessors
  574. = new Dictionary<string, Action>();
  575. m_VehicleXmlProcessors.Add("TYPE", ProcessXR_type);
  576. m_VehicleXmlProcessors.Add("FLAGS", ProcessXR_flags);
  577. // Linear properties
  578. m_VehicleXmlProcessors.Add("LMDIR", ProcessXR_linearMotorDirection);
  579. m_VehicleXmlProcessors.Add("LMFTIME", ProcessXR_linearFrictionTimescale);
  580. m_VehicleXmlProcessors.Add("LMDTIME", ProcessXR_linearMotorDecayTimescale);
  581. m_VehicleXmlProcessors.Add("LMTIME", ProcessXR_linearMotorTimescale);
  582. m_VehicleXmlProcessors.Add("LMOFF", ProcessXR_linearMotorOffset);
  583. //Angular properties
  584. m_VehicleXmlProcessors.Add("AMDIR", ProcessXR_angularMotorDirection);
  585. m_VehicleXmlProcessors.Add("AMTIME", ProcessXR_angularMotorTimescale);
  586. m_VehicleXmlProcessors.Add("AMDTIME", ProcessXR_angularMotorDecayTimescale);
  587. m_VehicleXmlProcessors.Add("AMFTIME", ProcessXR_angularFrictionTimescale);
  588. //Deflection properties
  589. m_VehicleXmlProcessors.Add("ADEFF", ProcessXR_angularDeflectionEfficiency);
  590. m_VehicleXmlProcessors.Add("ADTIME", ProcessXR_angularDeflectionTimescale);
  591. m_VehicleXmlProcessors.Add("LDEFF", ProcessXR_linearDeflectionEfficiency);
  592. m_VehicleXmlProcessors.Add("LDTIME", ProcessXR_linearDeflectionTimescale);
  593. //Banking properties
  594. m_VehicleXmlProcessors.Add("BEFF", ProcessXR_bankingEfficiency);
  595. m_VehicleXmlProcessors.Add("BMIX", ProcessXR_bankingMix);
  596. m_VehicleXmlProcessors.Add("BTIME", ProcessXR_bankingTimescale);
  597. //Hover and Buoyancy properties
  598. m_VehicleXmlProcessors.Add("HHEI", ProcessXR_VhoverHeight);
  599. m_VehicleXmlProcessors.Add("HEFF", ProcessXR_VhoverEfficiency);
  600. m_VehicleXmlProcessors.Add("HTIME", ProcessXR_VhoverTimescale);
  601. m_VehicleXmlProcessors.Add("VBUO", ProcessXR_VehicleBuoyancy);
  602. //Attractor properties
  603. m_VehicleXmlProcessors.Add("VAEFF", ProcessXR_verticalAttractionEfficiency);
  604. m_VehicleXmlProcessors.Add("VATIME", ProcessXR_verticalAttractionTimescale);
  605. m_VehicleXmlProcessors.Add("REF_FRAME", ProcessXR_referenceFrame);
  606. vd = new VehicleData();
  607. reader.ReadStartElement("Vehicle", String.Empty);
  608. errors = EReadProcessors(
  609. m_VehicleXmlProcessors,
  610. reader);
  611. reader.ReadEndElement();
  612. reader = null;
  613. }
  614. private void ProcessXR_type()
  615. {
  616. vd.m_type = (Vehicle)XRint();
  617. }
  618. private void ProcessXR_flags()
  619. {
  620. vd.m_flags = (VehicleFlag)XRint();
  621. }
  622. // Linear properties
  623. private void ProcessXR_linearMotorDirection()
  624. {
  625. vd.m_linearMotorDirection = XRvector();
  626. }
  627. private void ProcessXR_linearFrictionTimescale()
  628. {
  629. vd.m_linearFrictionTimescale = XRvector();
  630. }
  631. private void ProcessXR_linearMotorDecayTimescale()
  632. {
  633. vd.m_linearMotorDecayTimescale = XRfloat();
  634. }
  635. private void ProcessXR_linearMotorTimescale()
  636. {
  637. vd.m_linearMotorTimescale = XRfloat();
  638. }
  639. private void ProcessXR_linearMotorOffset()
  640. {
  641. vd.m_linearMotorOffset = XRvector();
  642. }
  643. //Angular properties
  644. private void ProcessXR_angularMotorDirection()
  645. {
  646. vd.m_angularMotorDirection = XRvector();
  647. }
  648. private void ProcessXR_angularMotorTimescale()
  649. {
  650. vd.m_angularMotorTimescale = XRfloat();
  651. }
  652. private void ProcessXR_angularMotorDecayTimescale()
  653. {
  654. vd.m_angularMotorDecayTimescale = XRfloat();
  655. }
  656. private void ProcessXR_angularFrictionTimescale()
  657. {
  658. vd.m_angularFrictionTimescale = XRvector();
  659. }
  660. //Deflection properties
  661. private void ProcessXR_angularDeflectionEfficiency()
  662. {
  663. vd.m_angularDeflectionEfficiency = XRfloat();
  664. }
  665. private void ProcessXR_angularDeflectionTimescale()
  666. {
  667. vd.m_angularDeflectionTimescale = XRfloat();
  668. }
  669. private void ProcessXR_linearDeflectionEfficiency()
  670. {
  671. vd.m_linearDeflectionEfficiency = XRfloat();
  672. }
  673. private void ProcessXR_linearDeflectionTimescale()
  674. {
  675. vd.m_linearDeflectionTimescale = XRfloat();
  676. }
  677. //Banking properties
  678. private void ProcessXR_bankingEfficiency()
  679. {
  680. vd.m_bankingEfficiency = XRfloat();
  681. }
  682. private void ProcessXR_bankingMix()
  683. {
  684. vd.m_bankingMix = XRfloat();
  685. }
  686. private void ProcessXR_bankingTimescale()
  687. {
  688. vd.m_bankingTimescale = XRfloat();
  689. }
  690. //Hover and Buoyancy properties
  691. private void ProcessXR_VhoverHeight()
  692. {
  693. vd.m_VhoverHeight = XRfloat();
  694. }
  695. private void ProcessXR_VhoverEfficiency()
  696. {
  697. vd.m_VhoverEfficiency = XRfloat();
  698. }
  699. private void ProcessXR_VhoverTimescale()
  700. {
  701. vd.m_VhoverTimescale = XRfloat();
  702. }
  703. private void ProcessXR_VehicleBuoyancy()
  704. {
  705. vd.m_VehicleBuoyancy = XRfloat();
  706. }
  707. //Attractor properties
  708. private void ProcessXR_verticalAttractionEfficiency()
  709. {
  710. vd.m_verticalAttractionEfficiency = XRfloat();
  711. }
  712. private void ProcessXR_verticalAttractionTimescale()
  713. {
  714. vd.m_verticalAttractionTimescale = XRfloat();
  715. }
  716. private void ProcessXR_referenceFrame()
  717. {
  718. vd.m_referenceFrame = XRquat();
  719. }
  720. }
  721. }