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